2020-12-22 03:30:06 +01:00
|
|
|
# Widgets (2)
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
## GtkTextView, GtkTextbuffer and GtkScrolledWindow
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
### GtkTextView and GtkTextBuffer
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
GtkTextview is a widget for multiline text editing.
|
|
|
|
GtkTextBuffer is a text buffer which is connected to GtkTextView.
|
|
|
|
See a sample program `tfv1.c` below.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
@@@ tfv1.c
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
Look at line 25.
|
|
|
|
GtkTextView is generated and its pointer is assigned to `tv`.
|
|
|
|
When GtkTextView is generated, the connected GtkTextBuffer is also generated automatically.
|
|
|
|
In the next line, the pointer to the buffer is got and assigned to `tb`.
|
|
|
|
Then, the text from line 10 to 20 is assigned to the buffer.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
GtkTextView has a wrap mode.
|
|
|
|
When `GTK_WRAP_WORD_CHAR` is set, text wraps in between words, or if that is not enough, also between graphemes.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
In line 30, `tv` is set to `win` as a child.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
Now compile and run it.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
![GtkTextView](../image/screenshot_tfv1.png)
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
There's an I-beam pointer in the window.
|
|
|
|
You can add or delete any character on GtkTextview.
|
|
|
|
And your change is kept in GtkTextBuffer.
|
|
|
|
If you add more characters than the limit of the window, the height of the window extends.
|
|
|
|
If the height gets bigger than the height of the display screen, you won't be able to control the size of the window back to the original size.
|
|
|
|
It's a problem.
|
|
|
|
You can solve it by putting GtkScrolledWindow between GtkApplicationWindow and GtkTextView.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
### GtkScrolledWindow
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
What we need to do is:
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
- Generate GtkScrolledWindow and set it as a child of GtkApplicationWindow.
|
|
|
|
- Set GtkTextVies as a child of GtkScrolledWindow.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
Modify `tfv1.c` and save it as `tfv2.c`.
|
|
|
|
The difference between these two files is very little.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
$$$
|
|
|
|
diff tfv1.c tfv2.c
|
|
|
|
$$$
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
Though you can modify the source file by this diff output, It's good for you to show `tfv2.c`.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
@@@ tfv2.c
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2020-12-22 03:30:06 +01:00
|
|
|
Now compile and run it.
|
|
|
|
This time the window doesn't extend even if you type a lot of characters.
|
|
|
|
It just scrolls.
|
2020-12-21 13:12:05 +01:00
|
|
|
|