mirror of
https://github.com/ToshioCP/Gtk4-tutorial.git
synced 2025-01-12 20:03:28 +01:00
Minor edits for readability to section 4
This commit is contained in:
parent
0940b05be7
commit
2ed0969574
1 changed files with 21 additions and 22 deletions
|
@ -4,10 +4,10 @@
|
||||||
|
|
||||||
### GtkLabel
|
### GtkLabel
|
||||||
|
|
||||||
We made a window and show it on the screen in the previous section.
|
In the previous section we made a window and displayed it on the screen.
|
||||||
Now we go on to the next topic, widgets in the window.
|
Now we go on to the next topic, where we add widgets to this window.
|
||||||
The simplest widget is GtkLabel.
|
The simplest widget is GtkLabel.
|
||||||
It is a widget with a string in it.
|
It is a widget with text in it.
|
||||||
|
|
||||||
@@@include
|
@@@include
|
||||||
misc/lb1.c
|
misc/lb1.c
|
||||||
|
@ -32,7 +32,7 @@ cd misc; diff pr4.c lb1.c
|
||||||
|
|
||||||
This tells us:
|
This tells us:
|
||||||
|
|
||||||
- The definition of a variable `lab` is added.
|
- The definition of a new variable `lab` is added.
|
||||||
- The title of the window is changed.
|
- The title of the window is changed.
|
||||||
- A label is created and connected to the window as a child.
|
- A label is created and connected to the window as a child.
|
||||||
|
|
||||||
|
@ -44,20 +44,20 @@ But these two relationships are totally different.
|
||||||
Don't be confused.
|
Don't be confused.
|
||||||
In the program `lb1.c`, `lab` is a child widget of `win`.
|
In the program `lb1.c`, `lab` is a child widget of `win`.
|
||||||
Child widgets are always located in their parent widget on the screen.
|
Child widgets are always located in their parent widget on the screen.
|
||||||
See the window appeared on the screen.
|
See how the window has appeared on the screen.
|
||||||
The window includes the label.
|
The application window includes the label.
|
||||||
|
|
||||||
The window `win` doesn't have any parents.
|
The window `win` doesn't have any parents.
|
||||||
We call such a window top-level window.
|
We call such a window top-level window.
|
||||||
One application can have two or more top-level windows.
|
An application can have more than one top-level window.
|
||||||
|
|
||||||
### GtkButton
|
### GtkButton
|
||||||
|
|
||||||
Next widget is GtkButton.
|
The next widget to introduce is GtkButton.
|
||||||
It has a label or icon on it.
|
It displays a button on the screen with a label or icon on it.
|
||||||
In this subsection, we will make a button with a label.
|
In this subsection, we will make a button with a label.
|
||||||
When a button is clicked on, it emits a "clicked" signal.
|
When the button is clicked, it emits a "clicked" signal.
|
||||||
The following program shows how to catch the signal and do something.
|
The following program shows how to catch the signal to then do something.
|
||||||
|
|
||||||
@@@include
|
@@@include
|
||||||
misc/lb2.c
|
misc/lb2.c
|
||||||
|
@ -79,7 +79,7 @@ A window with the button appears.
|
||||||
Click the button (it is a large button, you can click everywhere in the window), then a string "Clicked." appears on the terminal.
|
Click the button (it is a large button, you can click everywhere in the window), then a string "Clicked." appears on the terminal.
|
||||||
It shows the handler was invoked by clicking the button.
|
It shows the handler was invoked by clicking the button.
|
||||||
|
|
||||||
It's fairly good for us to make sure that the clicked signal was caught and the handler was invoked.
|
It's good that we make sure that the clicked signal was caught and the handler was invoked by using `g_print`.
|
||||||
However, using g_print is out of harmony with Gtk which is a GUI library.
|
However, using g_print is out of harmony with Gtk which is a GUI library.
|
||||||
So, we will change the handler.
|
So, we will change the handler.
|
||||||
The following code is `lb3.c`.
|
The following code is `lb3.c`.
|
||||||
|
@ -94,17 +94,17 @@ And the difference between `lb2.c` and `lb3.c` is as follows.
|
||||||
cd misc; diff lb2.c lb3.c
|
cd misc; diff lb2.c lb3.c
|
||||||
@@@
|
@@@
|
||||||
|
|
||||||
The change is:
|
The changes are:
|
||||||
|
|
||||||
- The function `g_print` in `lb2.c` was deleted and two lines above are inserted instead.
|
- The function `g_print` in `lb2.c` was deleted and the two lines above are inserted instead.
|
||||||
- The label of `btn` is changed from "Click me" to "Quit".
|
- The label of `btn` is changed from "Click me" to "Quit".
|
||||||
- The fourth argument of `g_signal_connect` is changed from `NULL` to `win`.
|
- The fourth argument of `g_signal_connect` is changed from `NULL` to `win`.
|
||||||
|
|
||||||
Most important is the fourth argument of `g_signal_connect`.
|
The most important change is the fourth argument of `g_signal_connect`.
|
||||||
It is described as "data to pass to handler" in the definition of `g_signal_connect` in [GObject API Reference](https://docs.gtk.org/gobject/func.signal_connect.html).
|
This argument is described as "data to pass to handler" in the definition of `g_signal_connect` in [GObject API Reference](https://docs.gtk.org/gobject/func.signal_connect.html).
|
||||||
Therefore, `win` which is a pointer to GtkApplicationWindow is passed to the handler as a second parameter `user_data`.
|
Therefore, `win` which is a pointer to GtkApplicationWindow is passed to the handler as a second parameter `user_data`.
|
||||||
Then, the handler cast it to a pointer to GtkWindow and call `gtk_window_destroy` to destroy the top-level window.
|
The handler then casts it to a pointer to GtkWindow and calls `gtk_window_destroy` to destroy the top-level window.
|
||||||
Then, the application quits.
|
The application then quits.
|
||||||
|
|
||||||
### GtkBox
|
### GtkBox
|
||||||
|
|
||||||
|
@ -148,4 +148,3 @@ Then, these two buttons are appended to the box.
|
||||||
|
|
||||||
The handler corresponds to `btn1` toggles its label.
|
The handler corresponds to `btn1` toggles its label.
|
||||||
The handler corresponds to `btn2` destroys the top-level window and the application quits.
|
The handler corresponds to `btn2` destroys the top-level window and the application quits.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue