2020-12-22 03:30:06 +01:00
# Ui file and GtkBuiler
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
## New, open and save button
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
We made the simplest editor in the previous section.
It reads the files in `on_open` funciton at start-up and writes it at closing window.
It works but is not good.
It is better to make "New", "Open", "Save" and "Close" buttons.
This section describes how to put those buttons into the window.
Signals and handlers will be explained later.
2020-12-21 13:12:05 +01:00
2021-01-11 03:15:04 +01:00
![Screenshot of the file editor ](../image/screenshot_tfe2.png ){width=9.3cm height=6.825cm}
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
The screenshot above shows the layout.
The function `on_open` in the source code `tfe2.c` is as follows.
2021-01-07 13:38:32 +01:00
@@@ tfe/tfe2.c on_open
2020-12-22 03:30:06 +01:00
The point is how to build the window.
2021-01-07 13:38:32 +01:00
- 25-27: Generate GtkApplicationWindow and set its title and default size.
- 29-30: Generate GtkBox `boxv` .
2020-12-22 03:30:06 +01:00
It is a vertical box and a child of GtkApplicationWindow.
It has two children.
The first child is a horizontal box includes buttons.
The second child is GtkNotebook.
2021-01-07 13:38:32 +01:00
- 32-33: Generate GtkBox `boxh` and append it to 'boxv' as a first child.
- 35-40: Generate three dummy labels.
2020-12-22 03:30:06 +01:00
The labels `dmy1` and `dmy3` has a character width of ten.
The other label `dmy2` is set hexpand property TRUE.
This makes the label expands horizontally as long as possible.
2021-01-07 13:38:32 +01:00
- 41-44: Generate four buttons.
- 46-52: Append these GtkLabel and GtkButton to `boxh` .
- 54-57: Generate GtkNotebook and set hexpand and vexpand properties TRUE.
2020-12-22 03:30:06 +01:00
This makes it expands horizontally and vertically as big as possible.
It is appended to `boxv` as the second child.
2021-01-07 13:38:32 +01:00
The number of lines is 33(=57-25+1) to build the widgets.
2020-12-22 03:30:06 +01:00
And we needed many variables (boxv, boxh, dmy1 ...).
Most of them aren't necessary except building the widgets.
Are there any good solution to reduce these work?
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
Gtk provides GtkBuilder.
It reads ui data and builds a window.
It reduces the cumbersom work.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
## Ui file
First, let's look at the ui file `tfe3.ui` that defines a structure of the widgets.
2020-12-21 13:12:05 +01:00
2021-01-07 13:38:32 +01:00
@@@ tfe/tfe3.ui
2020-12-22 03:30:06 +01:00
This is coded with XML structure.
Constructs begin with `<` and end with `>` is called tags.
And it is divided into two parts, start tag and end tag.
For example, `<interface>` is a start tag and `</interface>` is an end tag.
Ui file begins and ends with interface tags.
Some tags, for example, object tags can have a class and id attributes inside the start tag.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
- 2-5: An object with `GtkApplicationWindow` class and `win` id is defined.
This is the top level window.
And the three properties of the window are defined.
`title` property is "file editor", `default-width` property is 400 and `default-height` property is 300.
- 6: child tag means a child of the object above.
For example, line 7 tells us that GtkBox object which id is "boxv" is a child of `win` .
2020-12-21 13:12:05 +01:00
2021-01-07 13:38:32 +01:00
Compare this ui file and the lines 25-57 in the source code of `on_open` function.
2020-12-22 03:30:06 +01:00
Those two decribe the same structure of widgets.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
## GtkBuilder
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
GtkBuilder builds widgets based on the ui file.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
GtkBuilder *build;
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
build = gtk_builder_new_from_file ("tfe3.ui");
win = GTK_WIDGET (gtk_builder_get_object (build, "win"));
gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app));
nb = GTK_WIDGET (gtk_builder_get_object (build, "nb"));
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
The function `gtk_builder_new_from_file` reads the file given as an argument, build the widgets, generate GtkBuilder object and set pointers to the widgets in it.
The function `gtk_builder_get_object (build, "win")` returns the pointer to the widget `win` , which is the id in the ui file.
All the widgets are connected based on the parent-children relationship described in the ui file.
We only need `win` and `nb` for the program after this, so we don't need to take out any other widgets.
This reduces lines in the C source file.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
$$$
2021-01-07 13:38:32 +01:00
cd tfe; diff tfe2.c tfe3.c
2020-12-22 03:30:06 +01:00
$$$
2020-12-21 13:12:05 +01:00
2021-01-07 13:38:32 +01:00
`60,103c61,65` means 42 (=103-60+1) lines change to 5 (=65-61+1) lines.
Therefore 37 lines are reduced.
2020-12-22 03:30:06 +01:00
Using ui file not only shortens C source files, but also makes the widgets' structure clear.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
Now I'll show you the C source code `tfe3.c` .
Only functions `on_open` are shown as follows.
2020-12-21 13:12:05 +01:00
2021-01-07 13:38:32 +01:00
@@@ tfe/tfe3.c on_open
2021-01-11 03:15:04 +01:00
The source code of `tfe3.c` is stored in [src/tfe ](tfe ) directory.
2021-01-07 13:38:32 +01:00
If you want to see it, click the link above.
2021-01-11 03:15:04 +01:00
In the same way, you can get the source files below in the directory [src/tfe ](tfe ).
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
### Using ui string
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
GtkBuilder can build widgets using string.
Use the function gtk\_builder\_new\_from\_string instead of gtk\_builder\_new\_from\_file.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
char *uistring;
uistring =
"< interface > "
"< object class = "GtkApplicationWindow" id = "win" > "
"< property name = \"title \"> file editor</ property > "
"< property name = \"default-width \"> 600</ property > "
"< property name = \"default-height \"> 400</ property > "
"< child > "
"< object class = \"GtkBox \" id = \"boxv \"> "
"< property name = "orientation" > GTK_ORIENTATION_VERTICAL< / property > "
... ... ...
... ... ...
"< / interface > ";
build = gtk_builder_new_from_stringfile (uistring);
This method has an advantage and disadvantage.
The advantage is that the ui string is written in the source code.
So ui file is not necessary on runtime.
The disadvantage is that writing C string is a bit bothersome because of the double quotes.
If you want to use this method, you should write a script that transforms ui file into C-string.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
- add backslash before each double quote.
- add double quote at the left and right.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
### Using Gresource
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
Using Gresource is similar to using string.
But Gresource is compressed binary data, not text data.
And there's a compiler that compiles ui file into Gresource.
It can compile not only text files but also binary files such as images, sounds and so on.
And after compilation, it bundles them up into one Gresource object.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
An xml file is necessary for the resource compiler `glib-compile-resources` .
It describes resource files.
2020-12-21 13:12:05 +01:00
2021-01-07 13:38:32 +01:00
@@@ tfe/tfe3.gresource.xml
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
- 2: gresources tag can include mulitple gresources (gresource tags).
However, this xml has only one gresource.
- 3: The gresource has a prefix `/com/github/ToshioCP/tfe3` .
- 4: The gresource has tfe3.ui.
And it is pointed by `/com/github/ToshioCP/tfe3/tfe3.ui` because it needs prefix.
If you want to add more files, then insert them between line 4 and 5.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
Save this xml text to `tfe3.gresource.xml` .
2021-01-07 13:38:32 +01:00
The gresource compiler `glib-compile-resources` shows its usage with the argument `--help` .
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
$$$
LANG=C glib-compile-resources --help
$$$
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
Now run the compiler.
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
$ glib-compile-resources tfe3.gresource.xml --target=resources.c --generate-source
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
Then a C source file `resources.c` is generated.
Modify tfe3.c and save it as tfe3_r.c
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
# include "resources.c"
... ... ...
... ... ...
build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe3.ui");
... ... ...
... ... ...
2020-12-21 13:12:05 +01:00
2020-12-22 03:30:06 +01:00
Then, compile and run it.
The window appears and it is the same as the screenshot at the beginning of this page.
2020-12-21 13:12:05 +01:00