Functions in GtkNotebook

GtkNotebook is a very important object in the text file editor tfe. It connects the application and TfeTextView objects. A set of public functions are declared in tfenotebook.h. The word “tfenotebook” is used only in filenames. There’s no “TfeNotebook” object.

@@@include tfe5/tfenotebook.h @@@

This header file describes the public functions in tfenotebook.c.

You probably find that the functions except notebook_page_close are higher level functions of

respectively.

There are two layers. One of them is tfe_text_view ..., which is the lower level layer. The other is note_book ..., which is the higher level layer.

Now let’s look at the program of each function.

notebook_page_new

@@@include tfe5/tfenotebook.c get_untitled notebook_page_build notebook_page_new @@@

notebook_page_new_with_file

@@@include tfe5/tfenotebook.c notebook_page_new_with_file @@@

notebook_page_open

@@@include tfe5/tfenotebook.c open_response notebook_page_open @@@

Floating reference

All the widgets are derived from GInitiallyUnowned. GObject and GInitiallyUnowned are almost the same. The difference is like this. When an instance of GInitiallyUnowned is created, the instance has a floating reference and its reference count is zero. On the other hand, when an instance of GObject (not GInitiallyUnowned) is created, no floating reference is given. And the instance has a normal reference count instead of floating reference. Their descendants inherits them, so every widget has a floating reference at first. Non-widget class, for example, GtkTextBuffer is a direct sub class of GObject and it doesn’t have floating reference. Its reference count is one when it is created.

The function g_object_ref_sink converts the floating reference into an ordinary reference. If the instance doesn’t have a floating reference, g_object_ref_sink simply increases the reference count by one. It is used when an widget is added to another widget as a child.

GtkTextView *tv = gtk_text_view_new (); // floating reference
GtkScrolledWindow *scr = gtk_scrolled_window_new ();
gtk_scrolled_window_set_child (scr, tv); // tv's reference count is one

When tv is added to scr as a child, g_object_ref_sink is used.

g_object_ref_sink (tv);

So, the floating reference is converted into an ordinary reference. That is to say, floating reference is deleted, and reference count turns to one. Thanks to this, the caller doesn’t need to decrease tv’s reference count. If an Object_A is not a descendant of GInitiallyUnowned, the program is like this:

Object_A *obj_a = object_a_new (); // reference count is one
GtkScrolledWindow *scr = gtk_scrolled_window_new ();
gtk_scrolled_window_set_child (scr, obj_a); // obj_a's reference count is two
// obj_a is referred by the caller (this program) and scrolled window
g_object_unref (obj_a); // obj_a's reference count is one because the caller no longer refers obj_a.

This example tells us that the caller needs to unref obj_a.

If you use g_object_unref to an instance that has a floating reference, you need to convert the floating reference to a normal reference in advance. See GObject API reference for further information.

notebook_page_close

@@@include tfe5/tfenotebook.c notebook_page_close @@@

This function closes the current page. If the page is the only page the notebook has, then the function destroys the top-level window and quits the application.

notebook_page_save

@@@include tfe5/tfenotebook.c get_current_textview notebook_page_save @@@

file_changed_cb handler

The function file_changed_cb is a handler connected to “change-file” signal. If a file in a TfeTextView instance is changed, it emits this signal. This handler changes the label of the GtkNotebookPage.

@@@include tfe5/tfenotebook.c file_changed_cb @@@