From 6878ec19ce82db29856d419f0c85f069fb870868 Mon Sep 17 00:00:00 2001 From: Toshio Sekiya Date: Thu, 7 Jan 2021 23:48:03 +0900 Subject: [PATCH] Modify section 8. --- sec7.md | 4 ++-- sec8.md | 28 ++++++++++++++++------------ src/sec7.src.md | 4 ++-- src/sec8.src.md | 22 +++++++++++++--------- src/tfe4/Rakefile | 2 +- src/tfe4/tfe.c | 2 +- src/tfe4/tfetextview.c | 2 +- 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/sec7.md b/sec7.md index 9dbd8e1..9c6ad7c 100644 --- a/sec7.md +++ b/sec7.md @@ -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 diff --git a/sec8.md b/sec8.md index 774d7bb..efe64a2 100644 --- a/sec8.md +++ b/sec8.md @@ -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 @@ -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 @@ -337,7 +341,7 @@ Rake has task and file task, which is similar to target, prerequisite and recipe 16 objfiles.each do |obj| 17 src = obj.gsub(/.o$/,'.c') 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 21 end 22 @@ -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. diff --git a/src/sec7.src.md b/src/sec7.src.md index 83a9126..18e60c7 100644 --- a/src/sec7.src.md +++ b/src/sec7.src.md @@ -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 diff --git a/src/sec8.src.md b/src/sec8.src.md index 689aaac..1c2c4ff 100644 --- a/src/sec8.src.md +++ b/src/sec8.src.md @@ -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. diff --git a/src/tfe4/Rakefile b/src/tfe4/Rakefile index 959fb93..9f2d555 100644 --- a/src/tfe4/Rakefile +++ b/src/tfe4/Rakefile @@ -16,7 +16,7 @@ end objfiles.each do |obj| src = obj.gsub(/.o$/,'.c') 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 diff --git a/src/tfe4/tfe.c b/src/tfe4/tfe.c index 6132959..3fcf76b 100644 --- a/src/tfe4/tfe.c +++ b/src/tfe4/tfe.c @@ -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); diff --git a/src/tfe4/tfetextview.c b/src/tfe4/tfetextview.c index ef7dc0a..2cb7327 100644 --- a/src/tfe4/tfetextview.c +++ b/src/tfe4/tfetextview.c @@ -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)); }