Template XML and composite widget

The tfe program in the previous section is not so good because many things are crammed into tfepplication.c. And many static variables in tfepplication.c. The file tfeapplication.c should be divided into several files.

The preference dialog is defined by a ui file. And it has GtkBox, GtkLabel and GtkFontButton in it. Such widget can be defined as a composite widget. Composite widget is:

Next subsection shows how to build a preference dialog.

Preference dialog

First, write a template XML file.

@@@include tfe7/tfepref.ui @@@

Template tag specifies a composite widget. The value of a class attribute is the object name. It is “TfePref”. A parent attribute specifies the direct parent class of the composite widget. Therefore. TfePref is a child class of GtkDialog. A parent attribute is optional. But it is recommended to specify it. Other lines are the same as before.

The class TfePref is defined like TfeTextView. There are two files tfepref.h and tfepref.c.

The file tfepref.h defines types and declares public functions. The definitions are public and open to any C files.

@@@include tfe7/tfepref.h @@@

The file tfepref.c includes:

@@@include tfe7/tfepref.c @@@

Now, It is very simple to use this dialog. A caller just creates this object and shows it.

TfePref *pref;
pref = tfe_pref_new ();
gtk_window_set_transient_for (GTK_WINDOW (pref), win); /* win is the main window */
gtk_window_present (GTK_WINDOW (pref));

This instance is automatically destroyed when a user clicks on the close button. That’s all. If you want to show the dialog again, just create and show it.

Preference dialog

Alert dialog

It is almost same as preference dialog.

Its ui file is:

@@@include tfe7/tfealert.ui @@@

The header file is:

@@@include tfe7/tfealert.h @@@

There are three public functions. The functions tfe_alert_set_message and tfe_alert_set_button_label sets the label and button name of the alert dialog. For example, if you want to show an alert that the user tries to close without saving the content, set them like:

tfe_alert_set_message (alert, "Contents aren't saved yet.\nAre you sure to close?");
tfe_alert_set_button_label (alert, "Close");

The function tfe_alert_new creates a TfeAlert dialog.

Alert dialog

The C source file is:

@@@include tfe7/tfealert.c @@@

The program is almost same as tfepref.c.

The Usage of the alert object is as follows.

  1. Write the “response” signal handler.
  2. Create a TfeAlert object.
  3. Connect “response” signal to a handler
  4. Show the dialog
  5. In the signal handler, do something with regard to the response-id and destroy the dialog.

Top-level window

TfeWindow is a child class of GtkApplicationWindow.

@@@include tfe7/tfewindow.ui @@@

This XML file is almost same as before except template tag and “action-name” property in buttons.

GtkButton implements GtkActionable interface, which has “action-name” property. If this property is set, GtkButton activates the action when it is clicked. For example, if an open button is clicked, “win.open” action will be activated and open_activated handler will be invoked.

This action is also used by “<Control>o” accelerator (See tfeapplication.c). If you used “clicked” signal for the button, you would need its signal handler. Then, there would be two handlers:

These two handlers are almost same. It is inefficient. Connecting buttons to actions is a good way to reduce unnecessary codes.

@@@include tfe7/tfewindow.h @@@

There are three public functions. The function tfe_window_notebook_page_new creates a new notebook page. This is a wrapper function for notebook_page_new. It is called by TfeApplication object. The function tfe_window_notebook_page_new_with_files creates notebook pages with a contents read from the given files. The function tfe_window_new creates a TfeWindow instance.

@@@include tfe7/tfewindow.c @@@

TfeApplication

The file tfeaplication.h and tfeapplication.c are now very simple. The following is the header file.

@@@include tfe7/tfeapplication.h @@@

The following is tfeapplication.c. It defines the application and supports:

@@@include tfe7/tfeapplication.c @@@

Other files

main.c

@@@include tfe7/main.c @@@

CSS related files pfd2css.h and pfd2css.c are the same as the previous section.

Resource XML file.

@@@include tfe7/tfe.gresource.xml @@@

GSchema XML file

@@@include tfe7/com.github.ToshioCP.tfe.gschema.xml @@@

Meson.build

@@@include tfe7/meson.build @@@

Compilation and installation.

If you want to install it to your local area, use --prefix=$HOME/.local or --prefix=$HOME option. If you want to install it to the system area, no option is needed. It will be installed under /user/local directory.

$ meson --prefix=$HOME/.local _build
$ ninja -C _build
$ ninja -C _build install

You need root privilege to install it to the system area..

$ meson _build
$ ninja -C _build
$ sudo ninja -C _build install

Source files are in src/tfe7 directory.

Composite widgets give us two advantages.

We made a very small text editor. You can add features to this editor. When you add a new feature, be careful about the structure of the program. Maybe you need to divide a file into several files like this section. It isn’t good to put many things into one file. And it is important to think about the relationship between source files and widget structures.