mirror of
https://github.com/ToshioCP/Gtk4-tutorial.git
synced 2025-01-30 20:34:23 +01:00
Modify section 8.
This commit is contained in:
parent
8f4bf49945
commit
6878ec19ce
7 changed files with 36 additions and 28 deletions
4
sec7.md
4
sec7.md
|
@ -343,9 +343,9 @@ Only functions `on_open` are shown as follows.
|
||||||
47 gtk_window_destroy (GTK_WINDOW (win));
|
47 gtk_window_destroy (GTK_WINDOW (win));
|
||||||
48 }
|
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.
|
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
|
### Using ui string
|
||||||
|
|
||||||
|
|
28
sec8.md
28
sec8.md
|
@ -1,7 +1,7 @@
|
||||||
Up: [Readme.md](Readme.md), Prev: [Section 7](sec7.md), Next: [Section 9](sec9.md)
|
Up: [Readme.md](Readme.md), Prev: [Section 7](sec7.md), Next: [Section 9](sec9.md)
|
||||||
# Build system
|
# 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.
|
We've managed to compile a small editor so far.
|
||||||
But Some bad signs are beginning to appear.
|
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`.
|
- There are two compilers, `gcc` and `glib-compile-resources`.
|
||||||
We want to control them by one building tool.
|
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.
|
## Divide a C source file into two parts.
|
||||||
|
|
||||||
When you divide C source file into several parts, each file should contain only one thing.
|
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`.
|
Now we have three source files, `tfetextview.c`, `tfe.c` and `tfe3.ui`.
|
||||||
The `3` of `tfe3.ui` is like a version number.
|
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.
|
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.
|
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.
|
And header files are included by C source files.
|
||||||
For example, `tfetextview.h` is included by `tfe.c`.
|
For example, `tfetextview.h` is included by `tfe.c`.
|
||||||
|
|
||||||
|
All the source files are listed below.
|
||||||
|
|
||||||
`tfetextview.h`
|
`tfetextview.h`
|
||||||
|
|
||||||
1 #include <gtk/gtk.h>
|
1 #include <gtk/gtk.h>
|
||||||
|
@ -85,7 +89,7 @@ For example, `tfetextview.h` is included by `tfe.c`.
|
||||||
29
|
29
|
||||||
30 GtkWidget *
|
30 GtkWidget *
|
||||||
31 tfe_text_view_new (void) {
|
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 }
|
33 }
|
||||||
34
|
34
|
||||||
|
|
||||||
|
@ -121,7 +125,7 @@ For example, `tfetextview.h` is included by `tfe.c`.
|
||||||
28 g_object_unref (build);
|
28 g_object_unref (build);
|
||||||
29 for (i = 0; i < n_files; i++) {
|
29 for (i = 0; i < n_files; i++) {
|
||||||
30 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) {
|
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 ();
|
32 tv = tfe_text_view_new ();
|
||||||
33 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
|
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);
|
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.o` is called target.
|
||||||
- `sample.c` is prerequisite.
|
- `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.
|
Recipes follow tab characters, not spaces.
|
||||||
(It is very important. Use tab not space, or make won't work as you expected).
|
(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:
|
18 clean:
|
||||||
19 rm -f tfe tfe.o tfetextview.o resources.o resources.c
|
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
|
$ make
|
||||||
gcc -c -o tfe.o `pkg-config --cflags gtk4` tfe.c
|
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.
|
I used only very basic rules to write this Makefile.
|
||||||
There are many more convenient methods to make it more compact.
|
There are many more convenient methods to make it more compact.
|
||||||
But it needs long story to explain.
|
But it will be long to explain it.
|
||||||
So I want to finish the explanation about make.
|
So I want to finish explaining make and move on to the next topic.
|
||||||
|
|
||||||
## Rake
|
## Rake
|
||||||
|
|
||||||
|
@ -337,7 +341,7 @@ Rake has task and file task, which is similar to target, prerequisite and recipe
|
||||||
16 objfiles.each do |obj|
|
16 objfiles.each do |obj|
|
||||||
17 src = obj.gsub(/.o$/,'.c')
|
17 src = obj.gsub(/.o$/,'.c')
|
||||||
18 file obj => src do |t|
|
18 file obj => src do |t|
|
||||||
19 sh "gcc -c -o #{t.name} `pkg-config --cflags gtk4` #{t.source} "
|
19 sh "gcc -c -o #{t.name} `pkg-config --cflags gtk4` #{t.source}"
|
||||||
20 end
|
20 end
|
||||||
21 end
|
21 end
|
||||||
22
|
22
|
||||||
|
@ -357,7 +361,7 @@ The variable `t` is a task object.
|
||||||
- t.name is a target name
|
- t.name is a target name
|
||||||
- t.prerequisites is an array of prerequisits.
|
- t.prerequisites is an array of prerequisits.
|
||||||
- t.source is the first element of prerequisites.
|
- 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.
|
- 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.
|
- 23-25: resouce file depends on xml file and ui file.
|
||||||
|
|
||||||
|
@ -407,13 +411,13 @@ Now run meson and ninja.
|
||||||
$ meson _build
|
$ meson _build
|
||||||
$ ninja -C _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
|
$ _build/tfe tfe.c tfetextview.c
|
||||||
|
|
||||||
Then the window appears.
|
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.
|
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.
|
We divided a file into some categorized files and used a build tool.
|
||||||
|
|
|
@ -97,9 +97,9 @@ Only functions `on_open` are shown as follows.
|
||||||
|
|
||||||
@@@ tfe/tfe3.c on_open
|
@@@ 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.
|
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
|
### Using ui string
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Build system
|
# 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.
|
We've managed to compile a small editor so far.
|
||||||
But Some bad signs are beginning to appear.
|
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`.
|
- There are two compilers, `gcc` and `glib-compile-resources`.
|
||||||
We want to control them by one building tool.
|
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.
|
## Divide a C source file into two parts.
|
||||||
|
|
||||||
When you divide C source file into several parts, each file should contain only one thing.
|
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`.
|
Now we have three source files, `tfetextview.c`, `tfe.c` and `tfe3.ui`.
|
||||||
The `3` of `tfe3.ui` is like a version number.
|
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.
|
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.
|
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.
|
And header files are included by C source files.
|
||||||
For example, `tfetextview.h` is included by `tfe.c`.
|
For example, `tfetextview.h` is included by `tfe.c`.
|
||||||
|
|
||||||
|
All the source files are listed below.
|
||||||
|
|
||||||
`tfetextview.h`
|
`tfetextview.h`
|
||||||
|
|
||||||
@@@ tfe4/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.o` is called target.
|
||||||
- `sample.c` is prerequisite.
|
- `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.
|
Recipes follow tab characters, not spaces.
|
||||||
(It is very important. Use tab not space, or make won't work as you expected).
|
(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
|
@@@ tfe4/Makefile
|
||||||
|
|
||||||
Only you need is to type `make`.
|
You only need to type `make`.
|
||||||
|
|
||||||
$ make
|
$ make
|
||||||
gcc -c -o tfe.o `pkg-config --cflags gtk4` tfe.c
|
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.
|
I used only very basic rules to write this Makefile.
|
||||||
There are many more convenient methods to make it more compact.
|
There are many more convenient methods to make it more compact.
|
||||||
But it needs long story to explain.
|
But it will be long to explain it.
|
||||||
So I want to finish the explanation about make.
|
So I want to finish explaining make and move on to the next topic.
|
||||||
|
|
||||||
## Rake
|
## Rake
|
||||||
|
|
||||||
|
@ -136,7 +140,7 @@ The variable `t` is a task object.
|
||||||
- t.name is a target name
|
- t.name is a target name
|
||||||
- t.prerequisites is an array of prerequisits.
|
- t.prerequisites is an array of prerequisits.
|
||||||
- t.source is the first element of prerequisites.
|
- 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.
|
- 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.
|
- 23-25: resouce file depends on xml file and ui file.
|
||||||
|
|
||||||
|
@ -177,13 +181,13 @@ Now run meson and ninja.
|
||||||
$ meson _build
|
$ meson _build
|
||||||
$ ninja -C _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
|
$ _build/tfe tfe.c tfetextview.c
|
||||||
|
|
||||||
Then the window appears.
|
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.
|
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.
|
We divided a file into some categorized files and used a build tool.
|
||||||
|
|
|
@ -16,7 +16,7 @@ end
|
||||||
objfiles.each do |obj|
|
objfiles.each do |obj|
|
||||||
src = obj.gsub(/.o$/,'.c')
|
src = obj.gsub(/.o$/,'.c')
|
||||||
file obj => src do |t|
|
file obj => src do |t|
|
||||||
sh "gcc -c -o #{t.name} `pkg-config --cflags gtk4` #{t.source} "
|
sh "gcc -c -o #{t.name} `pkg-config --cflags gtk4` #{t.source}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer
|
||||||
g_object_unref (build);
|
g_object_unref (build);
|
||||||
for (i = 0; i < n_files; i++) {
|
for (i = 0; i < n_files; i++) {
|
||||||
if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) {
|
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 ();
|
tv = tfe_text_view_new ();
|
||||||
tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
|
tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
|
||||||
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
|
gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
|
||||||
|
|
|
@ -29,6 +29,6 @@ tfe_text_view_get_file (TfeTextView *tv) {
|
||||||
|
|
||||||
GtkWidget *
|
GtkWidget *
|
||||||
tfe_text_view_new (void) {
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue