Modify section 8.

This commit is contained in:
Toshio Sekiya 2021-01-07 23:48:03 +09:00
parent 8f4bf49945
commit 6878ec19ce
7 changed files with 36 additions and 28 deletions

View file

@ -343,9 +343,9 @@ Only functions `on_open` are shown as follows.
47 gtk_window_destroy (GTK_WINDOW (win));
48 }
The source code of `tfe3.c` is stored in [src/tfe](src/tfe) directory.
The source code of `tfe3.c` is stored in [src/tfe](https://github.com/ToshioCP/Gtk4-tutorial/tree/main/src/tfe) directory.
If you want to see it, click the link above.
In the same way, you can get the source files below in the directory [src/tfe](src/tfe).
In the same way, you can get the source files below in the directory [src/tfe](https://github.com/ToshioCP/Gtk4-tutorial/tree/main/src/tfe).
### Using ui string

26
sec8.md
View file

@ -1,7 +1,7 @@
Up: [Readme.md](Readme.md), Prev: [Section 7](sec7.md), Next: [Section 9](sec9.md)
# Build system
## What do we need to think about building?
## What do we need to think about to manage big source files?
We've managed to compile a small editor so far.
But Some bad signs are beginning to appear.
@ -11,6 +11,8 @@ We need to sort it out.
- There are two compilers, `gcc` and `glib-compile-resources`.
We want to control them by one building tool.
These ideas are useful to manage big source files.
## Divide a C source file into two parts.
When you divide C source file into several parts, each file should contain only one thing.
@ -22,7 +24,7 @@ It is a good idea to separate them into two files, `tfetextview.c` and `tfe.c`.
Now we have three source files, `tfetextview.c`, `tfe.c` and `tfe3.ui`.
The `3` of `tfe3.ui` is like a version number.
Managing version with filenames is one possible idea but it may make bothersome complicated problem.
Managing version with filenames is one possible idea but it may make bothersome problem.
You need to rewrite filename in each version and it affects to contents of sourcefiles that refer to filenames.
So, we should take `3` away from the filename.
@ -35,6 +37,8 @@ It has `.h` suffix like `tfetextview.h`
And header files are included by C source files.
For example, `tfetextview.h` is included by `tfe.c`.
All the source files are listed below.
`tfetextview.h`
1 #include <gtk/gtk.h>
@ -85,7 +89,7 @@ For example, `tfetextview.h` is included by `tfe.c`.
29
30 GtkWidget *
31 tfe_text_view_new (void) {
32 return gtk_widget_new (TFE_TYPE_TEXT_VIEW, NULL);
32 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
33 }
34
@ -121,7 +125,7 @@ For example, `tfetextview.h` is included by `tfe.c`.
28 g_object_unref (build);
29 for (i = 0; i < n_files; i++) {
30 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) {
31 scr = gtk_scrolled_window_new (NULL, NULL);
31 scr = gtk_scrolled_window_new ();
32 tv = tfe_text_view_new ();
33 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
34 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
@ -260,7 +264,7 @@ The sample of Malefile above consists of three elements, `sample.o`, `sample.c`
- `sample.o` is called target.
- `sample.c` is prerequisite.
- `gcc -0 sample.o sample.c` is recipe.
- `gcc -o sample.o sample.c` is recipe.
Recipes follow tab characters, not spaces.
(It is very important. Use tab not space, or make won't work as you expected).
@ -293,7 +297,7 @@ The Makefile for `tfe` is as follows.
18 clean:
19 rm -f tfe tfe.o tfetextview.o resources.o resources.c
Only you need is to type `make`.
You only need to type `make`.
$ make
gcc -c -o tfe.o `pkg-config --cflags gtk4` tfe.c
@ -304,8 +308,8 @@ Only you need is to type `make`.
I used only very basic rules to write this Makefile.
There are many more convenient methods to make it more compact.
But it needs long story to explain.
So I want to finish the explanation about make.
But it will be long to explain it.
So I want to finish explaining make and move on to the next topic.
## Rake
@ -357,7 +361,7 @@ The variable `t` is a task object.
- t.name is a target name
- t.prerequisites is an array of prerequisits.
- t.source is the first element of prerequisites.
- sh is a method to give the following string to shell as an argument and execute.
- sh is a method to give the following string to shell as an argument and execute the shell.
- 16-21: Loop by each element of the array of objfiles. Each object depends on corresponding source file.
- 23-25: resouce file depends on xml file and ui file.
@ -407,13 +411,13 @@ Now run meson and ninja.
$ meson _build
$ ninja -C _build
Then, the executable file `tfe` has been generated under the directory `_build`.
Then, the executable file `tfe` is generated under the directory `_build`.
$ _build/tfe tfe.c tfetextview.c
Then the window appears.
I show you three build tools.
I've shown you three build tools.
I think meson and ninja is the best choice for the present.
We divided a file into some categorized files and used a build tool.

View file

@ -97,9 +97,9 @@ Only functions `on_open` are shown as follows.
@@@ tfe/tfe3.c on_open
The source code of `tfe3.c` is stored in [src/tfe](src/tfe) directory.
The source code of `tfe3.c` is stored in [src/tfe](https://github.com/ToshioCP/Gtk4-tutorial/tree/main/src/tfe) directory.
If you want to see it, click the link above.
In the same way, you can get the source files below in the directory [src/tfe](src/tfe).
In the same way, you can get the source files below in the directory [src/tfe](https://github.com/ToshioCP/Gtk4-tutorial/tree/main/src/tfe).
### Using ui string

View file

@ -1,6 +1,6 @@
# Build system
## What do we need to think about building?
## What do we need to think about to manage big source files?
We've managed to compile a small editor so far.
But Some bad signs are beginning to appear.
@ -10,6 +10,8 @@ We need to sort it out.
- There are two compilers, `gcc` and `glib-compile-resources`.
We want to control them by one building tool.
These ideas are useful to manage big source files.
## Divide a C source file into two parts.
When you divide C source file into several parts, each file should contain only one thing.
@ -21,7 +23,7 @@ It is a good idea to separate them into two files, `tfetextview.c` and `tfe.c`.
Now we have three source files, `tfetextview.c`, `tfe.c` and `tfe3.ui`.
The `3` of `tfe3.ui` is like a version number.
Managing version with filenames is one possible idea but it may make bothersome complicated problem.
Managing version with filenames is one possible idea but it may make bothersome problem.
You need to rewrite filename in each version and it affects to contents of sourcefiles that refer to filenames.
So, we should take `3` away from the filename.
@ -34,6 +36,8 @@ It has `.h` suffix like `tfetextview.h`
And header files are included by C source files.
For example, `tfetextview.h` is included by `tfe.c`.
All the source files are listed below.
`tfetextview.h`
@@@ tfe4/tfetextview.h
@ -81,7 +85,7 @@ The sample of Malefile above consists of three elements, `sample.o`, `sample.c`
- `sample.o` is called target.
- `sample.c` is prerequisite.
- `gcc -0 sample.o sample.c` is recipe.
- `gcc -o sample.o sample.c` is recipe.
Recipes follow tab characters, not spaces.
(It is very important. Use tab not space, or make won't work as you expected).
@ -96,7 +100,7 @@ The Makefile for `tfe` is as follows.
@@@ tfe4/Makefile
Only you need is to type `make`.
You only need to type `make`.
$ make
gcc -c -o tfe.o `pkg-config --cflags gtk4` tfe.c
@ -107,8 +111,8 @@ Only you need is to type `make`.
I used only very basic rules to write this Makefile.
There are many more convenient methods to make it more compact.
But it needs long story to explain.
So I want to finish the explanation about make.
But it will be long to explain it.
So I want to finish explaining make and move on to the next topic.
## Rake
@ -136,7 +140,7 @@ The variable `t` is a task object.
- t.name is a target name
- t.prerequisites is an array of prerequisits.
- t.source is the first element of prerequisites.
- sh is a method to give the following string to shell as an argument and execute.
- sh is a method to give the following string to shell as an argument and execute the shell.
- 16-21: Loop by each element of the array of objfiles. Each object depends on corresponding source file.
- 23-25: resouce file depends on xml file and ui file.
@ -177,13 +181,13 @@ Now run meson and ninja.
$ meson _build
$ ninja -C _build
Then, the executable file `tfe` has been generated under the directory `_build`.
Then, the executable file `tfe` is generated under the directory `_build`.
$ _build/tfe tfe.c tfetextview.c
Then the window appears.
I show you three build tools.
I've shown you three build tools.
I think meson and ninja is the best choice for the present.
We divided a file into some categorized files and used a build tool.

View file

@ -28,7 +28,7 @@ on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer
g_object_unref (build);
for (i = 0; i < n_files; i++) {
if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) {
scr = gtk_scrolled_window_new (NULL, NULL);
scr = gtk_scrolled_window_new ();
tv = tfe_text_view_new ();
tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);

View file

@ -29,6 +29,6 @@ tfe_text_view_get_file (TfeTextView *tv) {
GtkWidget *
tfe_text_view_new (void) {
return gtk_widget_new (TFE_TYPE_TEXT_VIEW, NULL);
return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
}