Chapter 1. Creating a GtkEditor Widget

The GtkEditor widget is derived from the GtkTextBuffer object, and in most situations it will behave exactly as its parent class. The deviation from the parent class is only found in the configuration of the editor. This configuration is the topic of the following chapters. In this chapter we will show how to create an instant of the widget and how this instant can be used.

A new GtkEditor object is created with the function gtk_editor_new:

Example 1-1. Creating a GtkEditor Widget

  GtkWidget *editor = gtk_editor_new (NULL);
      

After creation, the editor can be used as the GtkTextBuffer object. If no further configuration is done, this is exactly what it will behave as. You can connect to the signals emitted by the text buffer classe and you can use the methods defined by this class. For example, you can connect it to a text view with gtk_text_view_new_with_buffer to show it:

Example 1-2. Creating a view of an editor

  GtkEditor *editor;
  GtkWidget *tkxt;
  editor = gtk_editor_new();
  tkxt = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(editor));
      

We complete this chapter with a complete program to get you started experimenting with the GtkEditor. The program shown in Example 1-3 creates an editor widget, turns editing on, and shows the editor in a toplevel window. In the following chapters we discuss configuration of the editor widget. You can extend this program with the examples shown in the rest of the tutorial to experiment with the editor.

Example 1-3. Creating a GtkEditor Widget and Showing it in a Toplevel Window.

#include <stdlib.h>
#include <gtkeditor.h>

int
main (int argc, char *argv[]) 
{
  GtkWidget *window;
  GtkWidget *sw;
  GtkWidget *tkxt;
  GtkEditor *editor;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  editor = gtk_editor_new ();

  sw = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
                                 GTK_POLICY_AUTOMATIC,
                                 GTK_POLICY_AUTOMATIC);

  tkxt = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(editor));

  gtk_container_add (GTK_CONTAINER (window), sw);
  gtk_container_add (GTK_CONTAINER(sw), tkxt);

  gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

  gtk_main ();

  return EXIT_SUCCESS;
}
      

Assuming you have the GtkEditor library installed somewhere in your LD_LIBRARY_PATH and the header file somewhere in your C_INCLUDE_PATH, you should be able to compile the program with

Example 1-4. Compiling the program

 % gcc `gtk-config --cflags` gtkeditor-test.c `gtk-config --libs` -lgtkeditor