Example 2. Text Handling

By Barry Drake

Introduction

This third example example of the building of a program using Glade will show you how to interact with a text screen:

Example screenshot

You have already learned how to create your front end. First make a front end containing three buttons.  You will need an Exit button the same as the one you had in the previous example, and two other buttons. Label these however you want, and make them signal to functions in callbacks.c.  In addition, you will need a Text Box. You will find this in the right hand column of the second row down. Make this a good size.

In the callback function called by the second button (assuming that you have made button 1 your exit), put the following code:

GtkWidget *main_window, *text;
  /*  gchar *window_txt;  */  /* uncomment when using the example of text retrieval below */
char *mytxt="The Classic \"Hello World!\" string\n";
/* We use the Glade utility function lookup_widget() to get a pointer to the widget. The first argument is any widget in the window/dialog and the second argument is the name of the widget you want to get. */
  main_window = lookup_widget (GTK_WIDGET (button), "window1");
  /* above line is not neccessary but avoids a warning */
  text = lookup_widget (main_window, "text1");
/* text in window needs to be editable before you can delete it, assuming your window is not already an editable text window. The first parameter is the start point for the delete and the second is the end point.  Use - 0 and -1 to delete all text in the window */
/* uncomment the lines below if you want the text to be deleted each time this button is clicked  */
  /*   gtk_text_set_editable (GTK_TEXT (text), TRUE);
  gtk_editable_delete_text (GTK_EDITABLE (text), 0, -1);
  gtk_text_set_editable (GTK_TEXT (text), FALSE); */
/* glib variable type gchar* is used to retrieve text. Parameters are same as for delete (above). The function g_free() must be used after the call to get the text. Once again we need to make the text editable if it is not already so. Uncomment the section below if you need to do this */
  /*   gtk_text_set_editable (GTK_TEXT (text), TRUE);
  window_txt = gtk_editable_get_chars (GTK_EDITABLE (text), 0, -1);    */
  /*   gtk_text_set_editable (GTK_TEXT (text), FALSE);  */  /* maybe you want to leave it editable? */
/* it can be pasted back into the window as is, just for a test using the line below*/
  /*   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, window_txt, -1);   */
  /* then de-allocate the buffer that was automatically created */
  /*  g_free(window_txt);    */
/* put the famous "Hello World" string into the window */
  gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, mytxt, -1);

 Now take a look at the function call from the other button.   Here is the code that you can paste into this function.

GtkWidget *main_window, *text;
gchar *window_txt;
/* gnome variable type gchar* is used to retrieve text. Parameters are same as for delete (above). The function g_free() must be used after the call to get the text. Once again we need to make the text editable if it is not already so */

main_window = lookup_widget (GTK_WIDGET (button), "window1");

text = lookup_widget (main_window, "text1");
  /* gtk_text_set_editable (GTK_TEXT (text), TRUE);  */
window_txt = gtk_editable_get_chars (GTK_EDITABLE (text), 0, -1);
/* gtk_text_set_editable (GTK_TEXT (text), FALSE);  */    /* maybe you want to leave it editable? */
  /* it can be pasted back into the window as is, just for a test */
gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, window_txt, -1);

  /* then de-allocate the buffer that was automatically created */
  g_free(window_txt);

 Compiling the Project

Compiling is carried out in the same way that you compiled your first project.

All the examples in these exercises are available in one file (Projects.zip) which you can download.

Overview of this tutorial


This small tutorial will let you understand how GTK+ handles text in windows. As you need to interact with the GTK+ library on a regular basis, you will benefit from having the GTK+ information and API documentation to hand.  You can find this at the GTK+ site.

<< Example 1: Entry, Pixmap & Dialog widgets <<


Home | Getting Started | Download | Applications | Mailing List

Download Zipfile of all the examples (63 KB)

SourceForge Logo