2021-01-13 05:29:35 +01:00
|
|
|
# Widgets (2)
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2022-05-01 16:30:26 +02:00
|
|
|
## GtkTextView, GtkTextBuffer and GtkScrolledWindow
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
### GtkTextView and GtkTextBuffer
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2022-05-01 16:30:26 +02:00
|
|
|
GtkTextView is a widget for multi-line text editing.
|
2021-01-13 05:29:35 +01:00
|
|
|
GtkTextBuffer is a text buffer which is connected to GtkTextView.
|
2021-06-14 10:10:23 +02:00
|
|
|
See the sample program `tfv1.c` below.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-02-08 14:24:54 +01:00
|
|
|
@@@include
|
|
|
|
tfv/tfv1.c
|
|
|
|
@@@
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
Look at line 25.
|
2021-06-14 10:10:23 +02:00
|
|
|
A GtkTextView instance is created and its pointer is assigned to `tv`.
|
|
|
|
When the GtkTextView instance is created, a GtkTextBuffer instance is also created and connected to the GtkTextView automatically.
|
|
|
|
"GtkTextBuffer instance" will be referred to simply as "GtkTextBuffer" or "buffer".
|
2022-12-21 14:31:33 +01:00
|
|
|
In the next line, the pointer to the buffer is assigned to `tb`.
|
2021-01-13 05:29:35 +01:00
|
|
|
Then, the text from line 10 to 20 is assigned to the buffer.
|
2022-12-21 14:31:33 +01:00
|
|
|
If the third argument of `gtk_text_buffer_set_text` is a positive integer, it is the length of the text.
|
|
|
|
It it is -1, the string terminates with NULL.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
GtkTextView has a wrap mode.
|
2021-02-06 09:26:57 +01:00
|
|
|
When it is set to `GTK_WRAP_WORD_CHAR`, text wraps in between words, or if that is not enough, also between graphemes.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2022-12-21 14:31:33 +01:00
|
|
|
Wrap mode is written in [Gtk\_WrapMode](https://docs.gtk.org/gtk4/enum.WrapMode.html) in the GTK 4 API document.
|
|
|
|
|
2021-02-06 09:26:57 +01:00
|
|
|
In line 30, `tv` is added to `win` as a child.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
Now compile and run it.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2022-12-21 14:31:33 +01:00
|
|
|
```
|
|
|
|
$ cd src/tfv
|
|
|
|
$ comp tfv1
|
|
|
|
$ ./a.out
|
|
|
|
```
|
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
![GtkTextView](../image/screenshot_tfv1.png){width=6.3cm height=5.325cm}
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
There's an I-beam pointer in the window.
|
2022-05-01 16:30:26 +02:00
|
|
|
You can add or delete any characters on the GtkTextView,
|
2022-03-04 07:05:36 +01:00
|
|
|
and your changes are kept in the GtkTextBuffer.
|
|
|
|
If you add more characters beyond the limit of the window, the height increases and the window extends.
|
|
|
|
If the height gets bigger than the height of the display screen, you won't be
|
2022-12-21 14:31:33 +01:00
|
|
|
able to control the size of the window or change it back to the original size.
|
|
|
|
This is a problem, that is to say a bug.
|
|
|
|
This can be solved by adding a GtkScrolledWindow between the GtkApplicationWindow and GtkTextView.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
### GtkScrolledWindow
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
What we need to do is:
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2022-12-21 14:31:33 +01:00
|
|
|
- Create a GtkScrolledWindow and insert it as a child of the GtkApplicationWindow
|
2022-03-04 07:05:36 +01:00
|
|
|
- Insert the GtkTextView widget to the GtkScrolledWindow as a child.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-01-13 05:29:35 +01:00
|
|
|
Modify `tfv1.c` and save it as `tfv2.c`.
|
2022-12-21 14:31:33 +01:00
|
|
|
There is only a few difference between these two files.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-02-08 14:24:54 +01:00
|
|
|
@@@shell
|
2021-01-13 05:29:35 +01:00
|
|
|
cd tfv; diff tfv1.c tfv2.c
|
2021-02-08 14:24:54 +01:00
|
|
|
@@@
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2022-12-21 14:31:33 +01:00
|
|
|
The whole code of `tfv2.c` is as follows.
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2021-02-08 14:24:54 +01:00
|
|
|
@@@include
|
|
|
|
tfv/tfv2.c
|
|
|
|
@@@
|
2020-12-21 13:12:05 +01:00
|
|
|
|
2022-03-04 07:05:36 +01:00
|
|
|
Compile and run it.
|
2022-12-21 14:31:33 +01:00
|
|
|
|
|
|
|
Now, the window doesn't extend even if you type a lot of characters,
|
|
|
|
it just scrolls.
|