Use fenced code block.

This commit is contained in:
Toshio Sekiya 2021-01-25 18:35:49 +09:00
parent 26fc01e64e
commit 880dd92c82
31 changed files with 3448 additions and 3100 deletions

1
.gitignore vendored
View file

@ -16,6 +16,7 @@ src/tfe5/_build
src/tfe5/hello.txt src/tfe5/hello.txt
src/menu/a.out src/menu/a.out
src/color/_build src/color/_build
src/turtle
html/* html/*
latex/* latex/*

101
Rakefile
View file

@ -156,12 +156,105 @@ EOS
main.gsub!(/@@@ abstract\n/, abstract_latex) main.gsub!(/@@@ abstract\n/, abstract_latex)
helper = <<'EOS' helper = <<'EOS'
\usepackage[pdftex]{graphicx}
\usepackage[colorlinks=true,linkcolor=black]{hyperref}
\usepackage[margin=2.4cm]{geometry}
\usepackage{tikz} \usepackage{tikz}
\usepackage[margin=2.4cm]{geometry}
% -------------------------------------------------------------
% Following codes are extracted from the preamble generated by:
% $ pandoc -s -o sample.tex sample.md
% -------------------------------------------------------------
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
hidelinks,
pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{% \providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\author{}
\date{}
EOS EOS
# tasks # tasks
@ -262,7 +355,7 @@ end
0.upto(srcfiles.size - 1) do |i| 0.upto(srcfiles.size - 1) do |i|
file "latex/#{srcfiles[i].to_tex}" => (srcfiles[i].c_files << srcfiles[i].path) do file "latex/#{srcfiles[i].to_tex}" => (srcfiles[i].c_files << srcfiles[i].path) do
src2md srcfiles[i].path, "latex/#{srcfiles[i].to_md}", 80 src2md srcfiles[i].path, "latex/#{srcfiles[i].to_md}", 86
sh "pandoc -o latex/#{srcfiles[i].to_tex} --top-level-division=chapter latex/#{srcfiles[i].to_md}" sh "pandoc -o latex/#{srcfiles[i].to_tex} --top-level-division=chapter latex/#{srcfiles[i].to_md}"
File.delete("latex/#{srcfiles[i].to_md}") File.delete("latex/#{srcfiles[i].to_md}")
end end

View file

@ -20,7 +20,7 @@ You can read it without download.
1. [Widgets (2)](gfm/sec5.md) 1. [Widgets (2)](gfm/sec5.md)
1. [Widgets (3)](gfm/sec6.md) 1. [Widgets (3)](gfm/sec6.md)
1. [Define Child object](gfm/sec7.md) 1. [Define Child object](gfm/sec7.md)
1. [Ui file and GtkBuiler](gfm/sec8.md) 1. [Ui file and GtkBuilder](gfm/sec8.md)
1. [Build system](gfm/sec9.md) 1. [Build system](gfm/sec9.md)
1. [Instance and class](gfm/sec10.md) 1. [Instance and class](gfm/sec10.md)
1. [Signals](gfm/sec11.md) 1. [Signals](gfm/sec11.md)

View file

@ -95,10 +95,12 @@ The structure of `TfeTextView` is like the following diagram.
The function `tfe_text_view_new` generates a new TfeTextView instance. The function `tfe_text_view_new` generates a new TfeTextView instance.
1 GtkWidget * ~~~C
2 tfe_text_view_new (void) { 1 GtkWidget *
3 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL)); 2 tfe_text_view_new (void) {
4 } 3 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
4 }
~~~
When this function is run, the following procedure is gone through. When this function is run, the following procedure is gone through.
@ -113,14 +115,16 @@ Step four is done by the function `tfe_text_view_init`.
> In the same way, `gtk_text_view_init`, `gtk_widget_init` and `g_object_init` is the initialization functions of GtkTextView, GtkWidget and GObject respectively. > In the same way, `gtk_text_view_init`, `gtk_widget_init` and `g_object_init` is the initialization functions of GtkTextView, GtkWidget and GObject respectively.
> You can find them in the GTK or GLib source files. > You can find them in the GTK or GLib source files.
1 static void ~~~C
2 tfe_text_view_init (TfeTextView *tv) { 1 static void
3 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 2 tfe_text_view_init (TfeTextView *tv) {
4 3 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
5 tv->file = NULL; 4
6 gtk_text_buffer_set_modified (tb, FALSE); 5 tv->file = NULL;
7 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 6 gtk_text_buffer_set_modified (tb, FALSE);
8 } 7 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
8 }
~~~
`tfe_text_view_init` initializes the instance. `tfe_text_view_init` initializes the instance.
@ -151,6 +155,7 @@ Class comprises mainly pointers to functions.
Those functions are used by the object itself or its descendent objects. Those functions are used by the object itself or its descendent objects.
For example, GObject class is declared in `gobject.h` in GLib source files. For example, GObject class is declared in `gobject.h` in GLib source files.
~~~C
1 typedef struct _GObjectClass GObjectClass; 1 typedef struct _GObjectClass GObjectClass;
2 typedef struct _GObjectClass GInitiallyUnownedClass; 2 typedef struct _GObjectClass GInitiallyUnownedClass;
3 3
@ -160,35 +165,36 @@ For example, GObject class is declared in `gobject.h` in GLib source files.
7 GSList *construct_properties; 7 GSList *construct_properties;
8 /*< public >*/ 8 /*< public >*/
9 /* seldom overidden */ 9 /* seldom overidden */
10 GObject* (*constructor) (GType type, 10 GObject* (*constructor) (GType type,
11 guint n_construct_properties, 11 guint n_construct_properties,
12 GObjectConstructParam *construct_properties); 12 GObjectConstructParam *construct_properties);
13 /* overridable methods */ 13 /* overridable methods */
14 void (*set_property) (GObject *object, 14 void (*set_property) (GObject *object,
15 guint property_id, 15 guint property_id,
16 const GValue *value, 16 const GValue *value,
17 GParamSpec *pspec); 17 GParamSpec *pspec);
18 void (*get_property) (GObject *object, 18 void (*get_property) (GObject *object,
19 guint property_id, 19 guint property_id,
20 GValue *value, 20 GValue *value,
21 GParamSpec *pspec); 21 GParamSpec *pspec);
22 void (*dispose) (GObject *object); 22 void (*dispose) (GObject *object);
23 void (*finalize) (GObject *object); 23 void (*finalize) (GObject *object);
24 /* seldom overidden */ 24 /* seldom overidden */
25 void (*dispatch_properties_changed) (GObject *object, 25 void (*dispatch_properties_changed) (GObject *object,
26 guint n_pspecs, 26 guint n_pspecs,
27 GParamSpec **pspecs); 27 GParamSpec **pspecs);
28 /* signals */ 28 /* signals */
29 void (*notify) (GObject *object, 29 void (*notify) (GObject *object,
30 GParamSpec *pspec); 30 GParamSpec *pspec);
31 31
32 /* called when done constructing */ 32 /* called when done constructing */
33 void (*constructed) (GObject *object); 33 void (*constructed) (GObject *object);
34 /*< private >*/ 34 /*< private >*/
35 gsize flags; 35 gsize flags;
36 /* padding */ 36 /* padding */
37 gpointer pdummy[6]; 37 gpointer pdummy[6];
38 }; 38 };
~~~
I'd like to explain some of the members. I'd like to explain some of the members.
There's a pointer to the function `dispose` in line 22. There's a pointer to the function `dispose` in line 22.
@ -218,6 +224,7 @@ Let's look at all the classes from GObject, which is the top level object, to Tf
The following is extracts from the source files (not exactly the same). The following is extracts from the source files (not exactly the same).
~~~C
1 struct _GtkWidgetClass { 1 struct _GtkWidgetClass {
2 GInitiallyUnownedClass parent_class; 2 GInitiallyUnownedClass parent_class;
3 /*< public >*/ 3 /*< public >*/
@ -317,15 +324,16 @@ The following is extracts from the source files (not exactly the same).
97 GtkTextIter *start, 97 GtkTextIter *start,
98 GtkTextIter *end); 98 GtkTextIter *end);
99 void (* insert_emoji) (GtkTextView *text_view); 99 void (* insert_emoji) (GtkTextView *text_view);
100 /*< private >*/ 100 /*< private >*/
101 gpointer padding[8]; 101 gpointer padding[8];
102 }; 102 };
103 103
104 /* The following definition is generated by the macro G_DECLARE_FINAL_TYPE 104 /* The following definition is generated by the macro G_DECLARE_FINAL_TYPE
105 typedef struct { 105 typedef struct {
106 GtkTextView parent_class; 106 GtkTextView parent_class;
107 } TfeTextViewClass; 107 } TfeTextViewClass;
108 108
~~~
- 105-107: This three lines are generated by the macro G\_DECLARE\_FINAL\_TYPE. - 105-107: This three lines are generated by the macro G\_DECLARE\_FINAL\_TYPE.
So, they are not written in either `tfe_text_view.h` or `tfe_text_view.c`. So, they are not written in either `tfe_text_view.h` or `tfe_text_view.c`.
@ -370,15 +378,17 @@ In the destruction process of TfeTextView, the reference count of widgets relate
But GFile pointed by `tv->file` needs to decrease its reference count by one. But GFile pointed by `tv->file` needs to decrease its reference count by one.
You must write the code in the dispose handler `tfe_text_view_dispose`. You must write the code in the dispose handler `tfe_text_view_dispose`.
1 static void ~~~C
2 tfe_text_view_dispose (GObject *gobject) { 1 static void
3 TfeTextView *tv = TFE_TEXT_VIEW (gobject); 2 tfe_text_view_dispose (GObject *gobject) {
4 3 TfeTextView *tv = TFE_TEXT_VIEW (gobject);
5 if (G_IS_FILE (tv->file)) 4
6 g_clear_object (&tv->file); 5 if (G_IS_FILE (tv->file))
7 6 g_clear_object (&tv->file);
8 G_OBJECT_CLASS (tfe_text_view_parent_class)->dispose (gobject); 7
9 } 8 G_OBJECT_CLASS (tfe_text_view_parent_class)->dispose (gobject);
9 }
~~~
- 5,6: If `tv->file` points a GFile, decrease its reference count. - 5,6: If `tv->file` points a GFile, decrease its reference count.
`g_clear_object` decreases the reference count and assigns NULL to `tv->file`. In dispose handlers, we usually use `g_clear_object` rather than `g_object_unref`. `g_clear_object` decreases the reference count and assigns NULL to `tv->file`. In dispose handlers, we usually use `g_clear_object` rather than `g_object_unref`.

View file

@ -55,6 +55,7 @@ If you need to register two or more signals, static array is usually used.
Signal registration codes are written in the class initialization function. Signal registration codes are written in the class initialization function.
~~~C
1 static void 1 static void
2 tfe_text_view_class_init (TfeTextViewClass *class) { 2 tfe_text_view_class_init (TfeTextViewClass *class) {
3 GObjectClass *object_class = G_OBJECT_CLASS (class); 3 GObjectClass *object_class = G_OBJECT_CLASS (class);
@ -64,24 +65,25 @@ Signal registration codes are written in the class initialization function.
7 G_TYPE_FROM_CLASS (class), 7 G_TYPE_FROM_CLASS (class),
8 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 8 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
9 NULL /* closure */, 9 NULL /* closure */,
10 NULL /* accumulator */, 10 NULL /* accumulator */,
11 NULL /* accumulator data */, 11 NULL /* accumulator data */,
12 NULL /* C marshaller */, 12 NULL /* C marshaller */,
13 G_TYPE_NONE /* return_type */, 13 G_TYPE_NONE /* return_type */,
14 0 /* n_params */, 14 0 /* n_params */,
15 NULL /* param_types */); 15 NULL /* param_types */);
16 GType param_types[] = {G_TYPE_INT}; 16 GType param_types[] = {G_TYPE_INT};
17 tfe_text_view_signals[OPEN_RESPONSE] = g_signal_newv ("open-response", 17 tfe_text_view_signals[OPEN_RESPONSE] = g_signal_newv ("open-response",
18 G_TYPE_FROM_CLASS (class), 18 G_TYPE_FROM_CLASS (class),
19 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 19 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
20 NULL /* closure */, 20 NULL /* closure */,
21 NULL /* accumulator */, 21 NULL /* accumulator */,
22 NULL /* accumulator data */, 22 NULL /* accumulator data */,
23 NULL /* C marshaller */, 23 NULL /* C marshaller */,
24 G_TYPE_NONE /* return_type */, 24 G_TYPE_NONE /* return_type */,
25 1 /* n_params */, 25 1 /* n_params */,
26 param_types); 26 param_types);
27 } 27 }
~~~
- 6-15: Register "change-file"signal. - 6-15: Register "change-file"signal.
`g_signal_newv` function is used. `g_signal_newv` function is used.

View file

@ -9,13 +9,16 @@ In this section I will explain each function in TfeTextView object.
`tfe.h` is a top header file and it includes `gtk.h` and all the header files. `tfe.h` is a top header file and it includes `gtk.h` and all the header files.
Every C source files, which are `tfeapplication.c`, `tfenotebook.c` and `tfetextview.c`, include `tfe.h` at the beginning of each file. Every C source files, which are `tfeapplication.c`, `tfenotebook.c` and `tfetextview.c`, include `tfe.h` at the beginning of each file.
1 #include <gtk/gtk.h> ~~~C
2 1 #include <gtk/gtk.h>
3 #include "tfetextview.h" 2
4 #include "tfenotebook.h" 3 #include "tfetextview.h"
4 #include "tfenotebook.h"
~~~
`tfetextview.h` is a header file which describes the public functions in `tfetextview.c`. `tfetextview.h` is a header file which describes the public functions in `tfetextview.c`.
~~~C
1 #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type () 1 #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type ()
2 G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView) 2 G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView)
3 3
@ -25,26 +28,27 @@ Every C source files, which are `tfeapplication.c`, `tfenotebook.c` and `tfetext
7 TFE_OPEN_RESPONSE_SUCCESS, 7 TFE_OPEN_RESPONSE_SUCCESS,
8 TFE_OPEN_RESPONSE_CANCEL, 8 TFE_OPEN_RESPONSE_CANCEL,
9 TFE_OPEN_RESPONSE_ERROR 9 TFE_OPEN_RESPONSE_ERROR
10 }; 10 };
11 11
12 GFile * 12 GFile *
13 tfe_text_view_get_file (TfeTextView *tv); 13 tfe_text_view_get_file (TfeTextView *tv);
14 14
15 void 15 void
16 tfe_text_view_open (TfeTextView *tv, GtkWidget *win); 16 tfe_text_view_open (TfeTextView *tv, GtkWidget *win);
17 17
18 void 18 void
19 tfe_text_view_save (TfeTextView *tv); 19 tfe_text_view_save (TfeTextView *tv);
20 20
21 void 21 void
22 tfe_text_view_saveas (TfeTextView *tv); 22 tfe_text_view_saveas (TfeTextView *tv);
23 23
24 GtkWidget * 24 GtkWidget *
25 tfe_text_view_new_with_file (GFile *file); 25 tfe_text_view_new_with_file (GFile *file);
26 26
27 GtkWidget * 27 GtkWidget *
28 tfe_text_view_new (void); 28 tfe_text_view_new (void);
29 29
~~~
- 1-2: These two lines are used to define TfeTextView. - 1-2: These two lines are used to define TfeTextView.
- 4-10: Definitions of parameter used in the handler of "open-response" signal. - 4-10: Definitions of parameter used in the handler of "open-response" signal.
@ -75,6 +79,7 @@ If an error occures during the genration process, NULL is returned.
Each function is defined as follows. Each function is defined as follows.
~~~C
1 GtkWidget * 1 GtkWidget *
2 tfe_text_view_new_with_file (GFile *file) { 2 tfe_text_view_new_with_file (GFile *file) {
3 g_return_val_if_fail (G_IS_FILE (file), NULL); 3 g_return_val_if_fail (G_IS_FILE (file), NULL);
@ -84,21 +89,22 @@ Each function is defined as follows.
7 char *contents; 7 char *contents;
8 gsize length; 8 gsize length;
9 9
10 if (! g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) /* read error */ 10 if (! g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) /* read error */
11 return NULL; 11 return NULL;
12 12
13 tv = tfe_text_view_new(); 13 tv = tfe_text_view_new();
14 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 14 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
15 gtk_text_buffer_set_text (tb, contents, length); 15 gtk_text_buffer_set_text (tb, contents, length);
16 g_free (contents); 16 g_free (contents);
17 TFE_TEXT_VIEW (tv)->file = g_file_dup (file); 17 TFE_TEXT_VIEW (tv)->file = g_file_dup (file);
18 return tv; 18 return tv;
19 } 19 }
20 20
21 GtkWidget * 21 GtkWidget *
22 tfe_text_view_new (void) { 22 tfe_text_view_new (void) {
23 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL)); 23 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
24 } 24 }
~~~
- 21-24: `tfe_text_view_new`. - 21-24: `tfe_text_view_new`.
Just returns the value from the function `g_object_new` but casted to the pointer to GtkWidget. Just returns the value from the function `g_object_new` but casted to the pointer to GtkWidget.
@ -137,6 +143,7 @@ If `tv->file` is NULL, then it shows GtkFileChooserDialog and lets the user to g
If an error occures, it is shown to the user through the message dialog. If an error occures, it is shown to the user through the message dialog.
The error is managed only in the object and no information is notified to the caller. The error is managed only in the object and no information is notified to the caller.
~~~C
1 static void 1 static void
2 saveas_dialog_response (GtkWidget *dialog, gint response, TfeTextView *tv) { 2 saveas_dialog_response (GtkWidget *dialog, gint response, TfeTextView *tv) {
3 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 3 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
@ -146,67 +153,68 @@ The error is managed only in the object and no information is notified to the ca
7 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog)); 7 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
8 if (G_IS_FILE(file)) { 8 if (G_IS_FILE(file)) {
9 tv->file = file; 9 tv->file = file;
10 gtk_text_buffer_set_modified (tb, TRUE); 10 gtk_text_buffer_set_modified (tb, TRUE);
11 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0); 11 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0);
12 tfe_text_view_save (TFE_TEXT_VIEW (tv)); 12 tfe_text_view_save (TFE_TEXT_VIEW (tv));
13 } 13 }
14 } 14 }
15 gtk_window_destroy (GTK_WINDOW (dialog)); 15 gtk_window_destroy (GTK_WINDOW (dialog));
16 } 16 }
17 17
18 void 18 void
19 tfe_text_view_save (TfeTextView *tv) { 19 tfe_text_view_save (TfeTextView *tv) {
20 g_return_if_fail (TFE_IS_TEXT_VIEW (tv)); 20 g_return_if_fail (TFE_IS_TEXT_VIEW (tv));
21 21
22 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 22 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
23 GtkTextIter start_iter; 23 GtkTextIter start_iter;
24 GtkTextIter end_iter; 24 GtkTextIter end_iter;
25 gchar *contents; 25 gchar *contents;
26 GtkWidget *message_dialog; 26 GtkWidget *message_dialog;
27 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW); 27 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW);
28 GError *err = NULL; 28 GError *err = NULL;
29 29
30 if (! gtk_text_buffer_get_modified (tb)) 30 if (! gtk_text_buffer_get_modified (tb))
31 return; /* no necessary to save it */ 31 return; /* no necessary to save it */
32 else if (tv->file == NULL) 32 else if (tv->file == NULL)
33 tfe_text_view_saveas (tv); 33 tfe_text_view_saveas (tv);
34 else { 34 else {
35 gtk_text_buffer_get_bounds (tb, &start_iter, &end_iter); 35 gtk_text_buffer_get_bounds (tb, &start_iter, &end_iter);
36 contents = gtk_text_buffer_get_text (tb, &start_iter, &end_iter, FALSE); 36 contents = gtk_text_buffer_get_text (tb, &start_iter, &end_iter, FALSE);
37 if (g_file_replace_contents (tv->file, contents, strlen (contents), NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, &err)) 37 if (g_file_replace_contents (tv->file, contents, strlen (contents), NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, &err))
38 gtk_text_buffer_set_modified (tb, FALSE); 38 gtk_text_buffer_set_modified (tb, FALSE);
39 else { 39 else {
40 /* It is possible that tv->file is broken. */ 40 /* It is possible that tv->file is broken. */
41 /* It is a good idea to set tv->file to NULL. */ 41 /* It is a good idea to set tv->file to NULL. */
42 if (G_IS_FILE (tv->file)) 42 if (G_IS_FILE (tv->file))
43 g_object_unref (tv->file); 43 g_object_unref (tv->file);
44 tv->file =NULL; 44 tv->file =NULL;
45 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0); 45 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0);
46 gtk_text_buffer_set_modified (tb, TRUE); 46 gtk_text_buffer_set_modified (tb, TRUE);
47 message_dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, 47 message_dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL,
48 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, 48 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
49 "%s.\n", err->message); 49 "%s.\n", err->message);
50 g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL); 50 g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
51 gtk_widget_show (message_dialog); 51 gtk_widget_show (message_dialog);
52 g_error_free (err); 52 g_error_free (err);
53 } 53 }
54 } 54 }
55 } 55 }
56 56
57 void 57 void
58 tfe_text_view_saveas (TfeTextView *tv) { 58 tfe_text_view_saveas (TfeTextView *tv) {
59 g_return_if_fail (TFE_IS_TEXT_VIEW (tv)); 59 g_return_if_fail (TFE_IS_TEXT_VIEW (tv));
60 60
61 GtkWidget *dialog; 61 GtkWidget *dialog;
62 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW); 62 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW);
63 63
64 dialog = gtk_file_chooser_dialog_new ("Save file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_SAVE, 64 dialog = gtk_file_chooser_dialog_new ("Save file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_SAVE,
65 "_Cancel", GTK_RESPONSE_CANCEL, 65 "_Cancel", GTK_RESPONSE_CANCEL,
66 "_Save", GTK_RESPONSE_ACCEPT, 66 "_Save", GTK_RESPONSE_ACCEPT,
67 NULL); 67 NULL);
68 g_signal_connect (dialog, "response", G_CALLBACK (saveas_dialog_response), tv); 68 g_signal_connect (dialog, "response", G_CALLBACK (saveas_dialog_response), tv);
69 gtk_widget_show (dialog); 69 gtk_widget_show (dialog);
70 } 70 }
~~~
- 18-55: `Tfe_text_view_save` function. - 18-55: `Tfe_text_view_save` function.
- 20: If `tv` is not a pointer to TfeTextView, then it logs an error message and immediately returns. - 20: If `tv` is not a pointer to TfeTextView, then it logs an error message and immediately returns.
@ -258,6 +266,7 @@ Otherwise probably bad things will happen.
GtkWidget `win` is expected to be the top level window of the application. GtkWidget `win` is expected to be the top level window of the application.
It will be used as a transient parent window for the argument to the function `gtk_file_chooser_dialog_new`. It will be used as a transient parent window for the argument to the function `gtk_file_chooser_dialog_new`.
~~~C
1 static void 1 static void
2 open_dialog_response(GtkWidget *dialog, gint response, TfeTextView *tv) { 2 open_dialog_response(GtkWidget *dialog, gint response, TfeTextView *tv) {
3 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 3 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
@ -267,46 +276,47 @@ It will be used as a transient parent window for the argument to the function `g
7 GtkWidget *message_dialog; 7 GtkWidget *message_dialog;
8 GError *err = NULL; 8 GError *err = NULL;
9 9
10 if (response != GTK_RESPONSE_ACCEPT) 10 if (response != GTK_RESPONSE_ACCEPT)
11 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_CANCEL); 11 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_CANCEL);
12 else if (! G_IS_FILE (file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog)))) 12 else if (! G_IS_FILE (file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog))))
13 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_ERROR); 13 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_ERROR);
14 else if (! g_file_load_contents (file, NULL, &contents, &length, NULL, &err)) { /* read error */ 14 else if (! g_file_load_contents (file, NULL, &contents, &length, NULL, &err)) { /* read error */
15 if (G_IS_FILE (file)) 15 if (G_IS_FILE (file))
16 g_object_unref (file); 16 g_object_unref (file);
17 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog), GTK_DIALOG_MODAL, 17 message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog), GTK_DIALOG_MODAL,
18 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, 18 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
19 "%s.\n", err->message); 19 "%s.\n", err->message);
20 g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL); 20 g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
21 gtk_widget_show (message_dialog); 21 gtk_widget_show (message_dialog);
22 g_error_free (err); 22 g_error_free (err);
23 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_ERROR); 23 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_ERROR);
24 } else { 24 } else {
25 gtk_text_buffer_set_text (tb, contents, length); 25 gtk_text_buffer_set_text (tb, contents, length);
26 g_free (contents); 26 g_free (contents);
27 if (G_IS_FILE (tv->file)) 27 if (G_IS_FILE (tv->file))
28 g_object_unref (tv->file); 28 g_object_unref (tv->file);
29 tv->file = file; 29 tv->file = file;
30 gtk_text_buffer_set_modified (tb, FALSE); 30 gtk_text_buffer_set_modified (tb, FALSE);
31 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_SUCCESS); 31 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_SUCCESS);
32 } 32 }
33 gtk_window_destroy (GTK_WINDOW (dialog)); 33 gtk_window_destroy (GTK_WINDOW (dialog));
34 } 34 }
35 35
36 void 36 void
37 tfe_text_view_open (TfeTextView *tv, GtkWidget *win) { 37 tfe_text_view_open (TfeTextView *tv, GtkWidget *win) {
38 g_return_if_fail (TFE_IS_TEXT_VIEW (tv)); 38 g_return_if_fail (TFE_IS_TEXT_VIEW (tv));
39 g_return_if_fail (GTK_IS_WINDOW (win)); 39 g_return_if_fail (GTK_IS_WINDOW (win));
40 40
41 GtkWidget *dialog; 41 GtkWidget *dialog;
42 42
43 dialog = gtk_file_chooser_dialog_new ("Open file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_OPEN, 43 dialog = gtk_file_chooser_dialog_new ("Open file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_OPEN,
44 "Cancel", GTK_RESPONSE_CANCEL, 44 "Cancel", GTK_RESPONSE_CANCEL,
45 "Open", GTK_RESPONSE_ACCEPT, 45 "Open", GTK_RESPONSE_ACCEPT,
46 NULL); 46 NULL);
47 g_signal_connect (dialog, "response", G_CALLBACK (open_dialog_response), tv); 47 g_signal_connect (dialog, "response", G_CALLBACK (open_dialog_response), tv);
48 gtk_widget_show (dialog); 48 gtk_widget_show (dialog);
49 } 49 }
~~~
- 36-49: `tfe_text_view_open` function. - 36-49: `tfe_text_view_open` function.
- 43: Generate GtkFileChooserDialog. - 43: Generate GtkFileChooserDialog.
@ -345,12 +355,14 @@ However, in Gtk4, `gtk_dialog_run`is unavailable any more.
`gtk_text_view_get_file` is a simple function show as follows. `gtk_text_view_get_file` is a simple function show as follows.
1 GFile * ~~~C
2 tfe_text_view_get_file (TfeTextView *tv) { 1 GFile *
3 g_return_val_if_fail (TFE_IS_TEXT_VIEW (tv), NULL); 2 tfe_text_view_get_file (TfeTextView *tv) {
4 3 g_return_val_if_fail (TFE_IS_TEXT_VIEW (tv), NULL);
5 return g_file_dup (tv->file); 4
6 } 5 return g_file_dup (tv->file);
6 }
~~~
The important thing is duplicate `tv->file`. The important thing is duplicate `tv->file`.
Otherwise, if the caller free the GFile object, `tv->file` is no more guaranteed to point the GFile. Otherwise, if the caller free the GFile object, `tv->file` is no more guaranteed to point the GFile.

View file

@ -6,6 +6,7 @@ GtkNotebook is a very important object in the text file editor `tfe`.
It connects the application and TfeTextView objects. It connects the application and TfeTextView objects.
`tfenotebook.h` and `tfenotebook.c` have a set of functions related to GtkTextbook. `tfenotebook.h` and `tfenotebook.c` have a set of functions related to GtkTextbook.
~~~C
1 void 1 void
2 notebook_page_save(GtkNotebook *nb); 2 notebook_page_save(GtkNotebook *nb);
3 3
@ -15,9 +16,10 @@ It connects the application and TfeTextView objects.
7 void 7 void
8 notebook_page_new_with_file (GtkNotebook *nb, GFile *file); 8 notebook_page_new_with_file (GtkNotebook *nb, GFile *file);
9 9
10 void 10 void
11 notebook_page_new (GtkNotebook *nb); 11 notebook_page_new (GtkNotebook *nb);
12 12
~~~
This header file shows the public functions in `tfenotebook.c`. This header file shows the public functions in `tfenotebook.c`.
@ -44,6 +46,7 @@ Now let's look at each program of the functions.
## notebook\_page\_new ## notebook\_page\_new
~~~C
1 static gchar* 1 static gchar*
2 get_untitled () { 2 get_untitled () {
3 static int c = -1; 3 static int c = -1;
@ -53,34 +56,35 @@ Now let's look at each program of the functions.
7 return g_strdup_printf ("Untitled%u", c); 7 return g_strdup_printf ("Untitled%u", c);
8 } 8 }
9 9
10 static void 10 static void
11 notebook_page_build (GtkNotebook *nb, GtkWidget *tv, char *filename) { 11 notebook_page_build (GtkNotebook *nb, GtkWidget *tv, char *filename) {
12 GtkWidget *scr; 12 GtkWidget *scr;
13 GtkNotebookPage *nbp; 13 GtkNotebookPage *nbp;
14 GtkWidget *lab; 14 GtkWidget *lab;
15 gint i; 15 gint i;
16 scr = gtk_scrolled_window_new (); 16 scr = gtk_scrolled_window_new ();
17 17
18 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 18 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
19 lab = gtk_label_new (filename); 19 lab = gtk_label_new (filename);
20 i = gtk_notebook_append_page (nb, scr, lab); 20 i = gtk_notebook_append_page (nb, scr, lab);
21 nbp = gtk_notebook_get_page (nb, scr); 21 nbp = gtk_notebook_get_page (nb, scr);
22 g_object_set (nbp, "tab-expand", TRUE, NULL); 22 g_object_set (nbp, "tab-expand", TRUE, NULL);
23 gtk_notebook_set_current_page (nb, i); 23 gtk_notebook_set_current_page (nb, i);
24 g_signal_connect (GTK_TEXT_VIEW (tv), "change-file", G_CALLBACK (file_changed), nb); 24 g_signal_connect (GTK_TEXT_VIEW (tv), "change-file", G_CALLBACK (file_changed), nb);
25 } 25 }
26 26
27 void 27 void
28 notebook_page_new (GtkNotebook *nb) { 28 notebook_page_new (GtkNotebook *nb) {
29 g_return_if_fail(GTK_IS_NOTEBOOK (nb)); 29 g_return_if_fail(GTK_IS_NOTEBOOK (nb));
30 30
31 GtkWidget *tv; 31 GtkWidget *tv;
32 char *filename; 32 char *filename;
33 33
34 tv = tfe_text_view_new (); 34 tv = tfe_text_view_new ();
35 filename = get_untitled (); 35 filename = get_untitled ();
36 notebook_page_build (nb, tv, filename); 36 notebook_page_build (nb, tv, filename);
37 } 37 }
~~~
- 27-37: `notebook_page_new` function. - 27-37: `notebook_page_new` function.
- 29: `g_return_if_fail` is used to check the argument. - 29: `g_return_if_fail` is used to check the argument.
@ -102,6 +106,7 @@ The caller of `get_untitled` is in charge of freeing the memories of the string.
## notebook\_page\_new\_with\_file ## notebook\_page\_new\_with\_file
~~~C
1 void 1 void
2 notebook_page_new_with_file (GtkNotebook *nb, GFile *file) { 2 notebook_page_new_with_file (GtkNotebook *nb, GFile *file) {
3 g_return_if_fail(GTK_IS_NOTEBOOK (nb)); 3 g_return_if_fail(GTK_IS_NOTEBOOK (nb));
@ -111,10 +116,11 @@ The caller of `get_untitled` is in charge of freeing the memories of the string.
7 char *filename; 7 char *filename;
8 8
9 if ((tv = tfe_text_view_new_with_file (file)) == NULL) 9 if ((tv = tfe_text_view_new_with_file (file)) == NULL)
10 return; /* read error */ 10 return; /* read error */
11 filename = g_file_get_basename (file); 11 filename = g_file_get_basename (file);
12 notebook_page_build (nb, tv, filename); 12 notebook_page_build (nb, tv, filename);
13 } 13 }
~~~
- 9-10: Call `tfe_text_view_new_with_file`. - 9-10: Call `tfe_text_view_new_with_file`.
If it returns NULL, then do nothing and return because of an error. If it returns NULL, then do nothing and return because of an error.
@ -122,6 +128,7 @@ If it returns NULL, then do nothing and return because of an error.
## notebook\_page\_open ## notebook\_page\_open
~~~C
1 static void 1 static void
2 open_response (TfeTextView *tv, gint response, GtkNotebook *nb) { 2 open_response (TfeTextView *tv, gint response, GtkNotebook *nb) {
3 GFile *file; 3 GFile *file;
@ -131,25 +138,26 @@ If it returns NULL, then do nothing and return because of an error.
7 g_object_ref_sink (tv); 7 g_object_ref_sink (tv);
8 g_object_unref (tv); 8 g_object_unref (tv);
9 }else if (! G_IS_FILE (file = tfe_text_view_get_file (tv))) { 9 }else if (! G_IS_FILE (file = tfe_text_view_get_file (tv))) {
10 g_object_ref_sink (tv); 10 g_object_ref_sink (tv);
11 g_object_unref (tv); 11 g_object_unref (tv);
12 }else { 12 }else {
13 filename = g_file_get_basename (file); 13 filename = g_file_get_basename (file);
14 g_object_unref (file); 14 g_object_unref (file);
15 notebook_page_build (nb, GTK_WIDGET (tv), filename); 15 notebook_page_build (nb, GTK_WIDGET (tv), filename);
16 } 16 }
17 } 17 }
18 18
19 void 19 void
20 notebook_page_open (GtkNotebook *nb) { 20 notebook_page_open (GtkNotebook *nb) {
21 g_return_if_fail(GTK_IS_NOTEBOOK (nb)); 21 g_return_if_fail(GTK_IS_NOTEBOOK (nb));
22 22
23 GtkWidget *tv; 23 GtkWidget *tv;
24 24
25 tv = tfe_text_view_new (); 25 tv = tfe_text_view_new ();
26 g_signal_connect (TFE_TEXT_VIEW (tv), "open-response", G_CALLBACK (open_response), nb); 26 g_signal_connect (TFE_TEXT_VIEW (tv), "open-response", G_CALLBACK (open_response), nb);
27 tfe_text_view_open (TFE_TEXT_VIEW (tv), gtk_widget_get_ancestor (GTK_WIDGET (nb), GTK_TYPE_WINDOW)); 27 tfe_text_view_open (TFE_TEXT_VIEW (tv), gtk_widget_get_ancestor (GTK_WIDGET (nb), GTK_TYPE_WINDOW));
28 } 28 }
~~~
- 19-28: `notebook_page_open` function. - 19-28: `notebook_page_open` function.
- 25: Generate TfeTextView object. - 25: Generate TfeTextView object.
@ -169,6 +177,7 @@ Get the filename, build the contents of the page.
## notebook\_page\_save ## notebook\_page\_save
~~~C
1 void 1 void
2 notebook_page_save(GtkNotebook *nb) { 2 notebook_page_save(GtkNotebook *nb) {
3 gint i; 3 gint i;
@ -178,8 +187,9 @@ Get the filename, build the contents of the page.
7 i = gtk_notebook_get_current_page (nb); 7 i = gtk_notebook_get_current_page (nb);
8 scr = gtk_notebook_get_nth_page (nb, i); 8 scr = gtk_notebook_get_nth_page (nb, i);
9 tv = gtk_scrolled_window_get_child (GTK_SCROLLED_WINDOW (scr)); 9 tv = gtk_scrolled_window_get_child (GTK_SCROLLED_WINDOW (scr));
10 tfe_text_view_save (TFE_TEXT_VIEW (tv)); 10 tfe_text_view_save (TFE_TEXT_VIEW (tv));
11 } 11 }
~~~
- 7-9: Get TfeTextView belongs to the current notebook page. - 7-9: Get TfeTextView belongs to the current notebook page.
- 10: Call `tfe_text_view_save`. - 10: Call `tfe_text_view_save`.
@ -190,6 +200,7 @@ The function `file_changed` is a handler connected to "change-file" signal.
If `tv->file` is changed, TfeTextView emits this signal. If `tv->file` is changed, TfeTextView emits this signal.
This handler changes the label of GtkNotebookPage. This handler changes the label of GtkNotebookPage.
~~~C
1 static void 1 static void
2 file_changed (TfeTextView *tv, GtkNotebook *nb) { 2 file_changed (TfeTextView *tv, GtkNotebook *nb) {
3 GFile *file; 3 GFile *file;
@ -199,15 +210,16 @@ This handler changes the label of GtkNotebookPage.
7 7
8 file = tfe_text_view_get_file (tv); 8 file = tfe_text_view_get_file (tv);
9 scr = gtk_widget_get_parent (GTK_WIDGET (tv)); 9 scr = gtk_widget_get_parent (GTK_WIDGET (tv));
10 if (G_IS_FILE (file)) 10 if (G_IS_FILE (file))
11 filename = g_file_get_basename (file); 11 filename = g_file_get_basename (file);
12 else 12 else
13 filename = get_untitled (); 13 filename = get_untitled ();
14 label = gtk_label_new (filename); 14 label = gtk_label_new (filename);
15 gtk_notebook_set_tab_label (nb, scr, label); 15 gtk_notebook_set_tab_label (nb, scr, label);
16 g_object_unref (file); 16 g_object_unref (file);
17 g_free (filename); 17 g_free (filename);
18 } 18 }
~~~
- 8: Get GFile from TfeTextView. - 8: Get GFile from TfeTextView.
- 9: Get GkScrolledWindow which is the parent widget of `tv`. - 9: Get GkScrolledWindow which is the parent widget of `tv`.

View file

@ -15,6 +15,7 @@ It does following things.
Th function `main` is the first invoked function in C language. Th function `main` is the first invoked function in C language.
It connects the command line given by the user and GTK application. It connects the command line given by the user and GTK application.
~~~C
1 int 1 int
2 main (int argc, char **argv) { 2 main (int argc, char **argv) {
3 GtkApplication *app; 3 GtkApplication *app;
@ -24,12 +25,13 @@ It connects the command line given by the user and GTK application.
7 7
8 g_signal_connect (app, "startup", G_CALLBACK (tfe_startup), NULL); 8 g_signal_connect (app, "startup", G_CALLBACK (tfe_startup), NULL);
9 g_signal_connect (app, "activate", G_CALLBACK (tfe_activate), NULL); 9 g_signal_connect (app, "activate", G_CALLBACK (tfe_activate), NULL);
10 g_signal_connect (app, "open", G_CALLBACK (tfe_open), NULL); 10 g_signal_connect (app, "open", G_CALLBACK (tfe_open), NULL);
11 11
12 stat =g_application_run (G_APPLICATION (app), argc, argv); 12 stat =g_application_run (G_APPLICATION (app), argc, argv);
13 g_object_unref (app); 13 g_object_unref (app);
14 return stat; 14 return stat;
15 } 15 }
~~~
- 6: Generate GtkApplication object. - 6: Generate GtkApplication object.
- 8-10: Connect "startup", "activate" and "open signals to their handlers. - 8-10: Connect "startup", "activate" and "open signals to their handlers.
@ -47,6 +49,7 @@ What the signal handler needs to do is initialization of the application.
The handler is as follows. The handler is as follows.
~~~C
1 static void 1 static void
2 tfe_startup (GApplication *application) { 2 tfe_startup (GApplication *application) {
3 GtkApplication *app = GTK_APPLICATION (application); 3 GtkApplication *app = GTK_APPLICATION (application);
@ -56,29 +59,30 @@ The handler is as follows.
7 GtkButton *btno; 7 GtkButton *btno;
8 GtkButton *btnn; 8 GtkButton *btnn;
9 GtkButton *btns; 9 GtkButton *btns;
10 GtkButton *btnc; 10 GtkButton *btnc;
11 11
12 build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe/tfe.ui"); 12 build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe/tfe.ui");
13 win = GTK_APPLICATION_WINDOW (gtk_builder_get_object (build, "win")); 13 win = GTK_APPLICATION_WINDOW (gtk_builder_get_object (build, "win"));
14 nb = GTK_NOTEBOOK (gtk_builder_get_object (build, "nb")); 14 nb = GTK_NOTEBOOK (gtk_builder_get_object (build, "nb"));
15 gtk_window_set_application (GTK_WINDOW (win), app); 15 gtk_window_set_application (GTK_WINDOW (win), app);
16 btno = GTK_BUTTON (gtk_builder_get_object (build, "btno")); 16 btno = GTK_BUTTON (gtk_builder_get_object (build, "btno"));
17 btnn = GTK_BUTTON (gtk_builder_get_object (build, "btnn")); 17 btnn = GTK_BUTTON (gtk_builder_get_object (build, "btnn"));
18 btns = GTK_BUTTON (gtk_builder_get_object (build, "btns")); 18 btns = GTK_BUTTON (gtk_builder_get_object (build, "btns"));
19 btnc = GTK_BUTTON (gtk_builder_get_object (build, "btnc")); 19 btnc = GTK_BUTTON (gtk_builder_get_object (build, "btnc"));
20 g_signal_connect (btno, "clicked", G_CALLBACK (open_clicked), nb); 20 g_signal_connect (btno, "clicked", G_CALLBACK (open_clicked), nb);
21 g_signal_connect (btnn, "clicked", G_CALLBACK (new_clicked), nb); 21 g_signal_connect (btnn, "clicked", G_CALLBACK (new_clicked), nb);
22 g_signal_connect (btns, "clicked", G_CALLBACK (save_clicked), nb); 22 g_signal_connect (btns, "clicked", G_CALLBACK (save_clicked), nb);
23 g_signal_connect (btnc, "clicked", G_CALLBACK (close_clicked), nb); 23 g_signal_connect (btnc, "clicked", G_CALLBACK (close_clicked), nb);
24 g_object_unref(build); 24 g_object_unref(build);
25 25
26 GdkDisplay *display; 26 GdkDisplay *display;
27 27
28 display = gtk_widget_get_display (GTK_WIDGET (win)); 28 display = gtk_widget_get_display (GTK_WIDGET (win));
29 GtkCssProvider *provider = gtk_css_provider_new (); 29 GtkCssProvider *provider = gtk_css_provider_new ();
30 gtk_css_provider_load_from_data (provider, "textview {padding: 10px; font-family: monospace; font-size: 12pt;}", -1); 30 gtk_css_provider_load_from_data (provider, "textview {padding: 10px; font-family: monospace; font-size: 12pt;}", -1);
31 gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER); 31 gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
32 } 32 }
~~~
- 12-15: Build widgets using ui file (resource). - 12-15: Build widgets using ui file (resource).
Connect the top window and the application using `gtk_window_set_application`. Connect the top window and the application using `gtk_window_set_application`.
@ -168,6 +172,7 @@ CSS set to the context takes precedence over the one set to the display.
The handler of "activate" and "open" signal are `tfe_activate` and `tfe_open` respectively. The handler of "activate" and "open" signal are `tfe_activate` and `tfe_open` respectively.
They just generate a new GtkNotebookPage. They just generate a new GtkNotebookPage.
~~~C
1 static void 1 static void
2 tfe_activate (GApplication *application) { 2 tfe_activate (GApplication *application) {
3 GtkApplication *app = GTK_APPLICATION (application); 3 GtkApplication *app = GTK_APPLICATION (application);
@ -177,30 +182,31 @@ They just generate a new GtkNotebookPage.
7 7
8 win = GTK_WIDGET (gtk_application_get_active_window (app)); 8 win = GTK_WIDGET (gtk_application_get_active_window (app));
9 boxv = gtk_window_get_child (GTK_WINDOW (win)); 9 boxv = gtk_window_get_child (GTK_WINDOW (win));
10 nb = GTK_NOTEBOOK (gtk_widget_get_last_child (boxv)); 10 nb = GTK_NOTEBOOK (gtk_widget_get_last_child (boxv));
11 11
12 notebook_page_new (nb); 12 notebook_page_new (nb);
13 gtk_widget_show (GTK_WIDGET (win)); 13 gtk_widget_show (GTK_WIDGET (win));
14 } 14 }
15 15
16 static void 16 static void
17 tfe_open (GApplication *application, GFile ** files, gint n_files, const gchar *hint) { 17 tfe_open (GApplication *application, GFile ** files, gint n_files, const gchar *hint) {
18 GtkApplication *app = GTK_APPLICATION (application); 18 GtkApplication *app = GTK_APPLICATION (application);
19 GtkWidget *win; 19 GtkWidget *win;
20 GtkWidget *boxv; 20 GtkWidget *boxv;
21 GtkNotebook *nb; 21 GtkNotebook *nb;
22 int i; 22 int i;
23 23
24 win = GTK_WIDGET (gtk_application_get_active_window (app)); 24 win = GTK_WIDGET (gtk_application_get_active_window (app));
25 boxv = gtk_window_get_child (GTK_WINDOW (win)); 25 boxv = gtk_window_get_child (GTK_WINDOW (win));
26 nb = GTK_NOTEBOOK (gtk_widget_get_last_child (boxv)); 26 nb = GTK_NOTEBOOK (gtk_widget_get_last_child (boxv));
27 27
28 for (i = 0; i < n_files; i++) 28 for (i = 0; i < n_files; i++)
29 notebook_page_new_with_file (nb, files[i]); 29 notebook_page_new_with_file (nb, files[i]);
30 if (gtk_notebook_get_n_pages (nb) == 0) 30 if (gtk_notebook_get_n_pages (nb) == 0)
31 notebook_page_new (nb); 31 notebook_page_new (nb);
32 gtk_widget_show (win); 32 gtk_widget_show (win);
33 } 33 }
~~~
- 1-14: `tfe_activate`. - 1-14: `tfe_activate`.
- 8-10: Get GtkNotebook object. - 8-10: Get GtkNotebook object.
@ -249,6 +255,7 @@ The second instance immediately quits so shell prompt soon appears.
## a series of handlers correspond to the button signals ## a series of handlers correspond to the button signals
~~~C
1 static void 1 static void
2 open_clicked (GtkWidget *btno, GtkNotebook *nb) { 2 open_clicked (GtkWidget *btno, GtkNotebook *nb) {
3 notebook_page_open (nb); 3 notebook_page_open (nb);
@ -258,27 +265,28 @@ The second instance immediately quits so shell prompt soon appears.
7 new_clicked (GtkWidget *btnn, GtkNotebook *nb) { 7 new_clicked (GtkWidget *btnn, GtkNotebook *nb) {
8 notebook_page_new (nb); 8 notebook_page_new (nb);
9 } 9 }
10 10
11 static void 11 static void
12 save_clicked (GtkWidget *btns, GtkNotebook *nb) { 12 save_clicked (GtkWidget *btns, GtkNotebook *nb) {
13 notebook_page_save (nb); 13 notebook_page_save (nb);
14 } 14 }
15 15
16 static void 16 static void
17 close_clicked (GtkWidget *btnc, GtkNotebook *nb) { 17 close_clicked (GtkWidget *btnc, GtkNotebook *nb) {
18 GtkWidget *win; 18 GtkWidget *win;
19 GtkWidget *boxv; 19 GtkWidget *boxv;
20 gint i; 20 gint i;
21 21
22 if (gtk_notebook_get_n_pages (nb) == 1) { 22 if (gtk_notebook_get_n_pages (nb) == 1) {
23 boxv = gtk_widget_get_parent (GTK_WIDGET (nb)); 23 boxv = gtk_widget_get_parent (GTK_WIDGET (nb));
24 win = gtk_widget_get_parent (boxv); 24 win = gtk_widget_get_parent (boxv);
25 gtk_window_destroy (GTK_WINDOW (win)); 25 gtk_window_destroy (GTK_WINDOW (win));
26 } else { 26 } else {
27 i = gtk_notebook_get_current_page (nb); 27 i = gtk_notebook_get_current_page (nb);
28 gtk_notebook_remove_page (GTK_NOTEBOOK (nb), i); 28 gtk_notebook_remove_page (GTK_NOTEBOOK (nb), i);
29 } 29 }
30 } 30 }
~~~
`open_clicked`, `new_clicked` and `save_clicked` just call corresponding notebook page functions. `open_clicked`, `new_clicked` and `save_clicked` just call corresponding notebook page functions.
`close_clicked` is a bit complicated. `close_clicked` is a bit complicated.
@ -289,6 +297,7 @@ First, get the top level window and call `gtk_window_destroy`.
## meson.build ## meson.build
~~~meson
1 project('tfe', 'c') 1 project('tfe', 'c')
2 2
3 gtkdep = dependency('gtk4') 3 gtkdep = dependency('gtk4')
@ -298,7 +307,8 @@ First, get the top level window and call `gtk_window_destroy`.
7 7
8 sourcefiles=files('tfeapplication.c', 'tfenotebook.c', 'tfetextview.c') 8 sourcefiles=files('tfeapplication.c', 'tfenotebook.c', 'tfetextview.c')
9 9
10 executable('tfe', sourcefiles, resources, dependencies: gtkdep) 10 executable('tfe', sourcefiles, resources, dependencies: gtkdep)
~~~
In this file, just the source file names are modified. In this file, just the source file names are modified.

View file

@ -33,6 +33,7 @@ It is a good practice for you to add more features.
## meson.buld ## meson.buld
~~~meson
1 project('tfe', 'c') 1 project('tfe', 'c')
2 2
3 gtkdep = dependency('gtk4') 3 gtkdep = dependency('gtk4')
@ -42,19 +43,23 @@ It is a good practice for you to add more features.
7 7
8 sourcefiles=files('tfeapplication.c', 'tfenotebook.c', 'tfetextview.c') 8 sourcefiles=files('tfeapplication.c', 'tfenotebook.c', 'tfetextview.c')
9 9
10 executable('tfe', sourcefiles, resources, dependencies: gtkdep) 10 executable('tfe', sourcefiles, resources, dependencies: gtkdep)
~~~
## tfe.gresource.xml ## tfe.gresource.xml
1 <?xml version="1.0" encoding="UTF-8"?> ~~~xml
2 <gresources> 1 <?xml version="1.0" encoding="UTF-8"?>
3 <gresource prefix="/com/github/ToshioCP/tfe"> 2 <gresources>
4 <file>tfe.ui</file> 3 <gresource prefix="/com/github/ToshioCP/tfe">
5 </gresource> 4 <file>tfe.ui</file>
6 </gresources> 5 </gresource>
6 </gresources>
~~~
## tfe.ui ## tfe.ui
~~~xml
1 <interface> 1 <interface>
2 <object class="GtkApplicationWindow" id="win"> 2 <object class="GtkApplicationWindow" id="win">
3 <property name="title">file editor</property> 3 <property name="title">file editor</property>
@ -64,71 +69,75 @@ It is a good practice for you to add more features.
7 <object class="GtkBox" id="boxv"> 7 <object class="GtkBox" id="boxv">
8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property> 8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property>
9 <child> 9 <child>
10 <object class="GtkBox" id="boxh"> 10 <object class="GtkBox" id="boxh">
11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property> 11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
12 <child> 12 <child>
13 <object class="GtkLabel" id="dmy1"> 13 <object class="GtkLabel" id="dmy1">
14 <property name="width-chars">10</property> 14 <property name="width-chars">10</property>
15 </object> 15 </object>
16 </child> 16 </child>
17 <child> 17 <child>
18 <object class="GtkButton" id="btnn"> 18 <object class="GtkButton" id="btnn">
19 <property name="label">_New</property> 19 <property name="label">_New</property>
20 <property name="use-underline">TRUE</property> 20 <property name="use-underline">TRUE</property>
21 </object> 21 </object>
22 </child> 22 </child>
23 <child> 23 <child>
24 <object class="GtkButton" id="btno"> 24 <object class="GtkButton" id="btno">
25 <property name="label">_Open</property> 25 <property name="label">_Open</property>
26 <property name="use-underline">TRUE</property> 26 <property name="use-underline">TRUE</property>
27 </object> 27 </object>
28 </child> 28 </child>
29 <child> 29 <child>
30 <object class="GtkLabel" id="dmy2"> 30 <object class="GtkLabel" id="dmy2">
31 <property name="hexpand">TRUE</property> 31 <property name="hexpand">TRUE</property>
32 </object> 32 </object>
33 </child> 33 </child>
34 <child> 34 <child>
35 <object class="GtkButton" id="btns"> 35 <object class="GtkButton" id="btns">
36 <property name="label">_Save</property> 36 <property name="label">_Save</property>
37 <property name="use-underline">TRUE</property> 37 <property name="use-underline">TRUE</property>
38 </object> 38 </object>
39 </child> 39 </child>
40 <child> 40 <child>
41 <object class="GtkButton" id="btnc"> 41 <object class="GtkButton" id="btnc">
42 <property name="label">_Close</property> 42 <property name="label">_Close</property>
43 <property name="use-underline">TRUE</property> 43 <property name="use-underline">TRUE</property>
44 </object> 44 </object>
45 </child> 45 </child>
46 <child> 46 <child>
47 <object class="GtkLabel" id="dmy3"> 47 <object class="GtkLabel" id="dmy3">
48 <property name="width-chars">10</property> 48 <property name="width-chars">10</property>
49 </object> 49 </object>
50 </child> 50 </child>
51 </object> 51 </object>
52 </child> 52 </child>
53 <child> 53 <child>
54 <object class="GtkNotebook" id="nb"> 54 <object class="GtkNotebook" id="nb">
55 <property name="scrollable">TRUE</property> 55 <property name="scrollable">TRUE</property>
56 <property name="hexpand">TRUE</property> 56 <property name="hexpand">TRUE</property>
57 <property name="vexpand">TRUE</property> 57 <property name="vexpand">TRUE</property>
58 </object> 58 </object>
59 </child> 59 </child>
60 </object> 60 </object>
61 </child> 61 </child>
62 </object> 62 </object>
63 </interface> 63 </interface>
64 64
~~~
## tfe.h ## tfe.h
1 #include <gtk/gtk.h> ~~~C
2 1 #include <gtk/gtk.h>
3 #include "tfetextview.h" 2
4 #include "tfenotebook.h" 3 #include "tfetextview.h"
4 #include "tfenotebook.h"
~~~
## tfeapplication.c ## tfeapplication.c
~~~C
1 #include "tfe.h" 1 #include "tfe.h"
2 2
3 static void 3 static void
@ -228,27 +237,29 @@ It is a good practice for you to add more features.
97 GtkCssProvider *provider = gtk_css_provider_new (); 97 GtkCssProvider *provider = gtk_css_provider_new ();
98 gtk_css_provider_load_from_data (provider, "textview {padding: 10px; font-family: monospace; font-size: 12pt;}", -1); 98 gtk_css_provider_load_from_data (provider, "textview {padding: 10px; font-family: monospace; font-size: 12pt;}", -1);
99 gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER); 99 gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
100 } 100 }
101 101
102 int 102 int
103 main (int argc, char **argv) { 103 main (int argc, char **argv) {
104 GtkApplication *app; 104 GtkApplication *app;
105 int stat; 105 int stat;
106 106
107 app = gtk_application_new ("com.github.ToshioCP.tfe", G_APPLICATION_HANDLES_OPEN); 107 app = gtk_application_new ("com.github.ToshioCP.tfe", G_APPLICATION_HANDLES_OPEN);
108 108
109 g_signal_connect (app, "startup", G_CALLBACK (tfe_startup), NULL); 109 g_signal_connect (app, "startup", G_CALLBACK (tfe_startup), NULL);
110 g_signal_connect (app, "activate", G_CALLBACK (tfe_activate), NULL); 110 g_signal_connect (app, "activate", G_CALLBACK (tfe_activate), NULL);
111 g_signal_connect (app, "open", G_CALLBACK (tfe_open), NULL); 111 g_signal_connect (app, "open", G_CALLBACK (tfe_open), NULL);
112 112
113 stat =g_application_run (G_APPLICATION (app), argc, argv); 113 stat =g_application_run (G_APPLICATION (app), argc, argv);
114 g_object_unref (app); 114 g_object_unref (app);
115 return stat; 115 return stat;
116 } 116 }
117 117
~~~
## tfenotebook.h ## tfenotebook.h
~~~C
1 void 1 void
2 notebook_page_save(GtkNotebook *nb); 2 notebook_page_save(GtkNotebook *nb);
3 3
@ -258,12 +269,14 @@ It is a good practice for you to add more features.
7 void 7 void
8 notebook_page_new_with_file (GtkNotebook *nb, GFile *file); 8 notebook_page_new_with_file (GtkNotebook *nb, GFile *file);
9 9
10 void 10 void
11 notebook_page_new (GtkNotebook *nb); 11 notebook_page_new (GtkNotebook *nb);
12 12
~~~
## tfenotebook.c ## tfenotebook.c
~~~C
1 #include "tfe.h" 1 #include "tfe.h"
2 2
3 /* The returned string should be freed with g_free() when no longer needed. */ 3 /* The returned string should be freed with g_free() when no longer needed. */
@ -363,26 +376,28 @@ It is a good practice for you to add more features.
97 char *filename; 97 char *filename;
98 98
99 if ((tv = tfe_text_view_new_with_file (file)) == NULL) 99 if ((tv = tfe_text_view_new_with_file (file)) == NULL)
100 return; /* read error */ 100 return; /* read error */
101 filename = g_file_get_basename (file); 101 filename = g_file_get_basename (file);
102 notebook_page_build (nb, tv, filename); 102 notebook_page_build (nb, tv, filename);
103 } 103 }
104 104
105 void 105 void
106 notebook_page_new (GtkNotebook *nb) { 106 notebook_page_new (GtkNotebook *nb) {
107 g_return_if_fail(GTK_IS_NOTEBOOK (nb)); 107 g_return_if_fail(GTK_IS_NOTEBOOK (nb));
108 108
109 GtkWidget *tv; 109 GtkWidget *tv;
110 char *filename; 110 char *filename;
111 111
112 tv = tfe_text_view_new (); 112 tv = tfe_text_view_new ();
113 filename = get_untitled (); 113 filename = get_untitled ();
114 notebook_page_build (nb, tv, filename); 114 notebook_page_build (nb, tv, filename);
115 } 115 }
116 116
~~~
## tfetextview.h ## tfetextview.h
~~~C
1 #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type () 1 #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type ()
2 G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView) 2 G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView)
3 3
@ -392,29 +407,31 @@ It is a good practice for you to add more features.
7 TFE_OPEN_RESPONSE_SUCCESS, 7 TFE_OPEN_RESPONSE_SUCCESS,
8 TFE_OPEN_RESPONSE_CANCEL, 8 TFE_OPEN_RESPONSE_CANCEL,
9 TFE_OPEN_RESPONSE_ERROR 9 TFE_OPEN_RESPONSE_ERROR
10 }; 10 };
11 11
12 GFile * 12 GFile *
13 tfe_text_view_get_file (TfeTextView *tv); 13 tfe_text_view_get_file (TfeTextView *tv);
14 14
15 void 15 void
16 tfe_text_view_open (TfeTextView *tv, GtkWidget *win); 16 tfe_text_view_open (TfeTextView *tv, GtkWidget *win);
17 17
18 void 18 void
19 tfe_text_view_save (TfeTextView *tv); 19 tfe_text_view_save (TfeTextView *tv);
20 20
21 void 21 void
22 tfe_text_view_saveas (TfeTextView *tv); 22 tfe_text_view_saveas (TfeTextView *tv);
23 23
24 GtkWidget * 24 GtkWidget *
25 tfe_text_view_new_with_file (GFile *file); 25 tfe_text_view_new_with_file (GFile *file);
26 26
27 GtkWidget * 27 GtkWidget *
28 tfe_text_view_new (void); 28 tfe_text_view_new (void);
29 29
~~~
## tfetextview.c ## tfetextview.c
~~~C
1 #include "tfe.h" 1 #include "tfe.h"
2 2
3 struct _TfeTextView 3 struct _TfeTextView
@ -514,125 +531,126 @@ It is a good practice for you to add more features.
97 gtk_text_buffer_set_text (tb, contents, length); 97 gtk_text_buffer_set_text (tb, contents, length);
98 g_free (contents); 98 g_free (contents);
99 if (G_IS_FILE (tv->file)) 99 if (G_IS_FILE (tv->file))
100 g_object_unref (tv->file); 100 g_object_unref (tv->file);
101 tv->file = file; 101 tv->file = file;
102 gtk_text_buffer_set_modified (tb, FALSE); 102 gtk_text_buffer_set_modified (tb, FALSE);
103 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_SUCCESS); 103 g_signal_emit (tv, tfe_text_view_signals[OPEN_RESPONSE], 0, TFE_OPEN_RESPONSE_SUCCESS);
104 } 104 }
105 gtk_window_destroy (GTK_WINDOW (dialog)); 105 gtk_window_destroy (GTK_WINDOW (dialog));
106 } 106 }
107 107
108 void 108 void
109 tfe_text_view_open (TfeTextView *tv, GtkWidget *win) { 109 tfe_text_view_open (TfeTextView *tv, GtkWidget *win) {
110 g_return_if_fail (TFE_IS_TEXT_VIEW (tv)); 110 g_return_if_fail (TFE_IS_TEXT_VIEW (tv));
111 g_return_if_fail (GTK_IS_WINDOW (win)); 111 g_return_if_fail (GTK_IS_WINDOW (win));
112 112
113 GtkWidget *dialog; 113 GtkWidget *dialog;
114 114
115 dialog = gtk_file_chooser_dialog_new ("Open file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_OPEN, 115 dialog = gtk_file_chooser_dialog_new ("Open file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_OPEN,
116 "Cancel", GTK_RESPONSE_CANCEL, 116 "Cancel", GTK_RESPONSE_CANCEL,
117 "Open", GTK_RESPONSE_ACCEPT, 117 "Open", GTK_RESPONSE_ACCEPT,
118 NULL); 118 NULL);
119 g_signal_connect (dialog, "response", G_CALLBACK (open_dialog_response), tv); 119 g_signal_connect (dialog, "response", G_CALLBACK (open_dialog_response), tv);
120 gtk_widget_show (dialog); 120 gtk_widget_show (dialog);
121 } 121 }
122 122
123 static void 123 static void
124 saveas_dialog_response (GtkWidget *dialog, gint response, TfeTextView *tv) { 124 saveas_dialog_response (GtkWidget *dialog, gint response, TfeTextView *tv) {
125 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 125 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
126 GFile *file; 126 GFile *file;
127 127
128 if (response == GTK_RESPONSE_ACCEPT) { 128 if (response == GTK_RESPONSE_ACCEPT) {
129 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog)); 129 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
130 if (G_IS_FILE(file)) { 130 if (G_IS_FILE(file)) {
131 tv->file = file; 131 tv->file = file;
132 gtk_text_buffer_set_modified (tb, TRUE); 132 gtk_text_buffer_set_modified (tb, TRUE);
133 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0); 133 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0);
134 tfe_text_view_save (TFE_TEXT_VIEW (tv)); 134 tfe_text_view_save (TFE_TEXT_VIEW (tv));
135 } 135 }
136 } 136 }
137 gtk_window_destroy (GTK_WINDOW (dialog)); 137 gtk_window_destroy (GTK_WINDOW (dialog));
138 } 138 }
139 139
140 void 140 void
141 tfe_text_view_save (TfeTextView *tv) { 141 tfe_text_view_save (TfeTextView *tv) {
142 g_return_if_fail (TFE_IS_TEXT_VIEW (tv)); 142 g_return_if_fail (TFE_IS_TEXT_VIEW (tv));
143 143
144 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 144 GtkTextBuffer *tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
145 GtkTextIter start_iter; 145 GtkTextIter start_iter;
146 GtkTextIter end_iter; 146 GtkTextIter end_iter;
147 gchar *contents; 147 gchar *contents;
148 GtkWidget *message_dialog; 148 GtkWidget *message_dialog;
149 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW); 149 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW);
150 GError *err = NULL; 150 GError *err = NULL;
151 151
152 if (! gtk_text_buffer_get_modified (tb)) 152 if (! gtk_text_buffer_get_modified (tb))
153 return; /* no necessary to save it */ 153 return; /* no necessary to save it */
154 else if (tv->file == NULL) 154 else if (tv->file == NULL)
155 tfe_text_view_saveas (tv); 155 tfe_text_view_saveas (tv);
156 else { 156 else {
157 gtk_text_buffer_get_bounds (tb, &start_iter, &end_iter); 157 gtk_text_buffer_get_bounds (tb, &start_iter, &end_iter);
158 contents = gtk_text_buffer_get_text (tb, &start_iter, &end_iter, FALSE); 158 contents = gtk_text_buffer_get_text (tb, &start_iter, &end_iter, FALSE);
159 if (g_file_replace_contents (tv->file, contents, strlen (contents), NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, &err)) 159 if (g_file_replace_contents (tv->file, contents, strlen (contents), NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, &err))
160 gtk_text_buffer_set_modified (tb, FALSE); 160 gtk_text_buffer_set_modified (tb, FALSE);
161 else { 161 else {
162 /* It is possible that tv->file is broken. */ 162 /* It is possible that tv->file is broken. */
163 /* It is a good idea to set tv->file to NULL. */ 163 /* It is a good idea to set tv->file to NULL. */
164 if (G_IS_FILE (tv->file)) 164 if (G_IS_FILE (tv->file))
165 g_object_unref (tv->file); 165 g_object_unref (tv->file);
166 tv->file =NULL; 166 tv->file =NULL;
167 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0); 167 g_signal_emit (tv, tfe_text_view_signals[CHANGE_FILE], 0);
168 gtk_text_buffer_set_modified (tb, TRUE); 168 gtk_text_buffer_set_modified (tb, TRUE);
169 message_dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, 169 message_dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL,
170 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, 170 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
171 "%s.\n", err->message); 171 "%s.\n", err->message);
172 g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL); 172 g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_window_destroy), NULL);
173 gtk_widget_show (message_dialog); 173 gtk_widget_show (message_dialog);
174 g_error_free (err); 174 g_error_free (err);
175 } 175 }
176 } 176 }
177 } 177 }
178 178
179 void 179 void
180 tfe_text_view_saveas (TfeTextView *tv) { 180 tfe_text_view_saveas (TfeTextView *tv) {
181 g_return_if_fail (TFE_IS_TEXT_VIEW (tv)); 181 g_return_if_fail (TFE_IS_TEXT_VIEW (tv));
182 182
183 GtkWidget *dialog; 183 GtkWidget *dialog;
184 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW); 184 GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (tv), GTK_TYPE_WINDOW);
185 185
186 dialog = gtk_file_chooser_dialog_new ("Save file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_SAVE, 186 dialog = gtk_file_chooser_dialog_new ("Save file", GTK_WINDOW (win), GTK_FILE_CHOOSER_ACTION_SAVE,
187 "_Cancel", GTK_RESPONSE_CANCEL, 187 "_Cancel", GTK_RESPONSE_CANCEL,
188 "_Save", GTK_RESPONSE_ACCEPT, 188 "_Save", GTK_RESPONSE_ACCEPT,
189 NULL); 189 NULL);
190 g_signal_connect (dialog, "response", G_CALLBACK (saveas_dialog_response), tv); 190 g_signal_connect (dialog, "response", G_CALLBACK (saveas_dialog_response), tv);
191 gtk_widget_show (dialog); 191 gtk_widget_show (dialog);
192 } 192 }
193 193
194 GtkWidget * 194 GtkWidget *
195 tfe_text_view_new_with_file (GFile *file) { 195 tfe_text_view_new_with_file (GFile *file) {
196 g_return_val_if_fail (G_IS_FILE (file), NULL); 196 g_return_val_if_fail (G_IS_FILE (file), NULL);
197 197
198 GtkWidget *tv; 198 GtkWidget *tv;
199 GtkTextBuffer *tb; 199 GtkTextBuffer *tb;
200 char *contents; 200 char *contents;
201 gsize length; 201 gsize length;
202 202
203 if (! g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) /* read error */ 203 if (! g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) /* read error */
204 return NULL; 204 return NULL;
205 205
206 tv = tfe_text_view_new(); 206 tv = tfe_text_view_new();
207 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 207 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
208 gtk_text_buffer_set_text (tb, contents, length); 208 gtk_text_buffer_set_text (tb, contents, length);
209 g_free (contents); 209 g_free (contents);
210 TFE_TEXT_VIEW (tv)->file = g_file_dup (file); 210 TFE_TEXT_VIEW (tv)->file = g_file_dup (file);
211 return tv; 211 return tv;
212 } 212 }
213 213
214 GtkWidget * 214 GtkWidget *
215 tfe_text_view_new (void) { 215 tfe_text_view_new (void) {
216 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL)); 216 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
217 } 217 }
218 218
~~~
## Total number of lines, words and charcters ## Total number of lines, words and charcters

View file

@ -119,6 +119,7 @@ So, if the action is activated, the handler will be invoked.
The following is a simple example of menus and actions. The following is a simple example of menus and actions.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -128,44 +129,45 @@ The following is a simple example of menus and actions.
7 } 7 }
8 8
9 static void 9 static void
10 on_activate (GApplication *app, gpointer user_data) { 10 on_activate (GApplication *app, gpointer user_data) {
11 GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app)); 11 GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
12 gtk_window_set_title (GTK_WINDOW (win), "menu1"); 12 gtk_window_set_title (GTK_WINDOW (win), "menu1");
13 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 13 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
14 14
15 GSimpleAction *act_quit = g_simple_action_new ("quit", NULL); 15 GSimpleAction *act_quit = g_simple_action_new ("quit", NULL);
16 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_quit)); 16 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_quit));
17 g_signal_connect (act_quit, "activate", G_CALLBACK (quit_activated), app); 17 g_signal_connect (act_quit, "activate", G_CALLBACK (quit_activated), app);
18 18
19 GMenu *menubar = g_menu_new (); 19 GMenu *menubar = g_menu_new ();
20 GMenuItem *menu_item_menu = g_menu_item_new ("Menu", NULL); 20 GMenuItem *menu_item_menu = g_menu_item_new ("Menu", NULL);
21 GMenu *menu = g_menu_new (); 21 GMenu *menu = g_menu_new ();
22 GMenuItem *menu_item_quit = g_menu_item_new ("Quit", "app.quit"); 22 GMenuItem *menu_item_quit = g_menu_item_new ("Quit", "app.quit");
23 g_menu_append_item (menu, menu_item_quit); 23 g_menu_append_item (menu, menu_item_quit);
24 g_object_unref (menu_item_quit); 24 g_object_unref (menu_item_quit);
25 g_menu_item_set_submenu (menu_item_menu, G_MENU_MODEL (menu)); 25 g_menu_item_set_submenu (menu_item_menu, G_MENU_MODEL (menu));
26 g_menu_append_item (menubar, menu_item_menu); 26 g_menu_append_item (menubar, menu_item_menu);
27 g_object_unref (menu_item_menu); 27 g_object_unref (menu_item_menu);
28 28
29 gtk_application_set_menubar (GTK_APPLICATION (app), G_MENU_MODEL (menubar)); 29 gtk_application_set_menubar (GTK_APPLICATION (app), G_MENU_MODEL (menubar));
30 gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (win), TRUE); 30 gtk_application_window_set_show_menubar (GTK_APPLICATION_WINDOW (win), TRUE);
31 gtk_window_present (GTK_WINDOW (win)); 31 gtk_window_present (GTK_WINDOW (win));
32 /* gtk_widget_show (win); is also OKay instead of gtk_window_present. */ 32 /* gtk_widget_show (win); is also OKay instead of gtk_window_present. */
33 } 33 }
34 34
35 int 35 int
36 main (int argc, char **argv) { 36 main (int argc, char **argv) {
37 GtkApplication *app; 37 GtkApplication *app;
38 int stat; 38 int stat;
39 39
40 app = gtk_application_new ("com.github.ToshioCP.menu1", G_APPLICATION_FLAGS_NONE); 40 app = gtk_application_new ("com.github.ToshioCP.menu1", G_APPLICATION_FLAGS_NONE);
41 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 41 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
42 42
43 stat =g_application_run (G_APPLICATION (app), argc, argv); 43 stat =g_application_run (G_APPLICATION (app), argc, argv);
44 g_object_unref (app); 44 g_object_unref (app);
45 return stat; 45 return stat;
46 } 46 }
47 47
~~~
- 3-7: `quit_activated` is a handler of an action `act_quit`. - 3-7: `quit_activated` is a handler of an action `act_quit`.
Handlers of actions have three parameters. Handlers of actions have three parameters.

View file

@ -21,7 +21,8 @@ The signal handler will be described after the explanation of this code.
static void static void
on_activate (GApplication *app, gpointer user_data) { on_activate (GApplication *app, gpointer user_data) {
... ... ... ... ... ...
GSimpleAction *act_fullscreen = g_simple_action_new_stateful ("fullscreen", NULL, g_variant_new_boolean (FALSE)); GSimpleAction *act_fullscreen = g_simple_action_new_stateful ("fullscreen",
NULL, g_variant_new_boolean (FALSE));
GMenuItem *menu_item_fullscreen = g_menu_item_new ("Full Screen", "win.fullscreen"); GMenuItem *menu_item_fullscreen = g_menu_item_new ("Full Screen", "win.fullscreen");
g_signal_connect (act_fullscreen, "change-state", G_CALLBACK (fullscreen_changed), win); g_signal_connect (act_fullscreen, "change-state", G_CALLBACK (fullscreen_changed), win);
... ... ... ... ... ...
@ -116,7 +117,8 @@ Those colors are given to the signal handler as a parameter.
static void static void
on_activate (GApplication *app, gpointer user_data) { on_activate (GApplication *app, gpointer user_data) {
... ... ... ... ... ...
GSimpleAction *act_color = g_simple_action_new_stateful ("color", g_variant_type_new("s"), g_variant_new_string ("red")); GSimpleAction *act_color = g_simple_action_new_stateful ("color",
g_variant_type_new("s"), g_variant_new_string ("red"));
GMenuItem *menu_item_red = g_menu_item_new ("Red", "win.color::red"); GMenuItem *menu_item_red = g_menu_item_new ("Red", "win.color::red");
GMenuItem *menu_item_green = g_menu_item_new ("Green", "win.color::green"); GMenuItem *menu_item_green = g_menu_item_new ("Green", "win.color::green");
GMenuItem *menu_item_blue = g_menu_item_new ("Blue", "win.color::blue"); GMenuItem *menu_item_blue = g_menu_item_new ("Blue", "win.color::blue");
@ -150,7 +152,8 @@ The following is the "activate" signal handler.
static void static void
color_activated(GSimpleAction *action, GVariant *parameter, gpointer win) { color_activated(GSimpleAction *action, GVariant *parameter, gpointer win) {
gchar *color = g_strdup_printf ("label#lb {background-color: %s;}", g_variant_get_string (parameter, NULL)); gchar *color = g_strdup_printf ("label#lb {background-color: %s;}",
g_variant_get_string (parameter, NULL));
gtk_css_provider_load_from_data (provider, color, -1); gtk_css_provider_load_from_data (provider, color, -1);
g_free (color); g_free (color);
g_action_change_state (G_ACTION (action), parameter); g_action_change_state (G_ACTION (action), parameter);
@ -187,14 +190,16 @@ When GVariantType is generated, the type is expressed by the string.
The following program is a simple example. The following program is a simple example.
It finally output the string "s". It finally output the string "s".
1 #include <glib.h> ~~~C
2 1 #include <glib.h>
3 int 2
4 main (int argc, char **argv) { 3 int
5 GVariantType *vtype = g_variant_type_new ("s"); 4 main (int argc, char **argv) {
6 const gchar *type_string = g_variant_type_peek_string (vtype); 5 GVariantType *vtype = g_variant_type_new ("s");
7 g_print ("%s\n",type_string); 6 const gchar *type_string = g_variant_type_peek_string (vtype);
8 } 7 g_print ("%s\n",type_string);
8 }
~~~
- `g_variant_tpe_new` generates GVariantType. - `g_variant_tpe_new` generates GVariantType.
It uses a type string "s" which means string. It uses a type string "s" which means string.
@ -217,6 +222,7 @@ And the radio button of the selected menu turns on.
The code is as follows. The code is as follows.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static GtkCssProvider *provider; 3 static GtkCssProvider *provider;
@ -316,12 +322,13 @@ The code is as follows.
97 97
98 app = gtk_application_new ("com.github.ToshioCP.menu2", G_APPLICATION_FLAGS_NONE); 98 app = gtk_application_new ("com.github.ToshioCP.menu2", G_APPLICATION_FLAGS_NONE);
99 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 99 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
100 100
101 stat =g_application_run (G_APPLICATION (app), argc, argv); 101 stat =g_application_run (G_APPLICATION (app), argc, argv);
102 g_object_unref (app); 102 g_object_unref (app);
103 return stat; 103 return stat;
104 } 104 }
105 105
~~~
- 5-26: Signal handlers. - 5-26: Signal handlers.
They have been explained in this section. They have been explained in this section.

View file

@ -1,4 +1,4 @@
Up: [Readme.md](../Readme.md), Prev: [Section 17](sec17.md) Up: [Readme.md](../Readme.md), Prev: [Section 17](sec17.md), Next: [Section 19](sec19.md)
# Ui file for menu and action entries # Ui file for menu and action entries
@ -13,22 +13,26 @@ The same goes for menus.
The ui file for menus has interface, menu tags. The ui file for menus has interface, menu tags.
The file starts and ends with interface tag. The file starts and ends with interface tag.
<interface> ~~~xml
<interface>
<menu id="menubar"> <menu id="menubar">
</menu> </menu>
</interface> </interface>
~~~
`menu` tag corresponds to GMenu object. `menu` tag corresponds to GMenu object.
`id` attribute defines the name of the object. `id` attribute defines the name of the object.
It will be refered by GtkBuilder. It will be refered by GtkBuilder.
<submenu> ~~~xml
<submenu>
<attribute name="label">File</attribute> <attribute name="label">File</attribute>
<item> <item>
<attribute name="label">New</attribute> <attribute name="label">New</attribute>
<attribute name="action">win.new</attribute> <attribute name="action">win.new</attribute>
</item> </item>
</submenu> </submenu>
~~~
`item` tag corresponds to item in GMenu which has the same structure as GMenuItem. `item` tag corresponds to item in GMenu which has the same structure as GMenuItem.
The item above has a label attribute. The item above has a label attribute.
@ -40,7 +44,8 @@ The GMenuItem has a link to GMenu.
The ui file above can be described as follows. The ui file above can be described as follows.
<item> ~~~xml
<item>
<attribute name="label">File</attribute> <attribute name="label">File</attribute>
<link name="submenu"> <link name="submenu">
<item> <item>
@ -48,7 +53,8 @@ The ui file above can be described as follows.
<attribute name="action">win.new</attribute> <attribute name="action">win.new</attribute>
</item> </item>
</link> </link>
</item> </item>
~~~
`link` tag expresses the link to submenu. `link` tag expresses the link to submenu.
And at the same time it also expresses the submenu itself. And at the same time it also expresses the submenu itself.
@ -63,6 +69,7 @@ Its name is `menu3`.
The following is the ui file of the menu in `menu3`. The following is the ui file of the menu in `menu3`.
~~~xml
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <interface> 2 <interface>
3 <menu id="menubar"> 3 <menu id="menubar">
@ -72,86 +79,91 @@ The following is the ui file of the menu in `menu3`.
7 <item> 7 <item>
8 <attribute name="label">New</attribute> 8 <attribute name="label">New</attribute>
9 <attribute name="action">win.new</attribute> 9 <attribute name="action">win.new</attribute>
10 </item> 10 </item>
11 <item> 11 <item>
12 <attribute name="label">Open</attribute> 12 <attribute name="label">Open</attribute>
13 <attribute name="action">win.open</attribute> 13 <attribute name="action">win.open</attribute>
14 </item> 14 </item>
15 </section> 15 </section>
16 <section> 16 <section>
17 <item> 17 <item>
18 <attribute name="label">Save</attribute> 18 <attribute name="label">Save</attribute>
19 <attribute name="action">win.save</attribute> 19 <attribute name="action">win.save</attribute>
20 </item> 20 </item>
21 <item> 21 <item>
22 <attribute name="label">Save As…</attribute> 22 <attribute name="label">Save As…</attribute>
23 <attribute name="action">win.saveas</attribute> 23 <attribute name="action">win.saveas</attribute>
24 </item> 24 </item>
25 </section> 25 </section>
26 <section> 26 <section>
27 <item> 27 <item>
28 <attribute name="label">Close</attribute> 28 <attribute name="label">Close</attribute>
29 <attribute name="action">win.close</attribute> 29 <attribute name="action">win.close</attribute>
30 </item> 30 </item>
31 </section> 31 </section>
32 <section> 32 <section>
33 <item> 33 <item>
34 <attribute name="label">Quit</attribute> 34 <attribute name="label">Quit</attribute>
35 <attribute name="action">app.quit</attribute> 35 <attribute name="action">app.quit</attribute>
36 </item> 36 </item>
37 </section> 37 </section>
38 </submenu> 38 </submenu>
39 <submenu> 39 <submenu>
40 <attribute name="label">Edit</attribute> 40 <attribute name="label">Edit</attribute>
41 <section> 41 <section>
42 <item> 42 <item>
43 <attribute name="label">Cut</attribute> 43 <attribute name="label">Cut</attribute>
44 <attribute name="action">win.cut</attribute> 44 <attribute name="action">win.cut</attribute>
45 </item> 45 </item>
46 <item> 46 <item>
47 <attribute name="label">Copy</attribute> 47 <attribute name="label">Copy</attribute>
48 <attribute name="action">win.copy</attribute> 48 <attribute name="action">win.copy</attribute>
49 </item> 49 </item>
50 <item> 50 <item>
51 <attribute name="label">Paste</attribute> 51 <attribute name="label">Paste</attribute>
52 <attribute name="action">win.paste</attribute> 52 <attribute name="action">win.paste</attribute>
53 </item> 53 </item>
54 </section> 54 </section>
55 <section> 55 <section>
56 <item> 56 <item>
57 <attribute name="label">Select All</attribute> 57 <attribute name="label">Select All</attribute>
58 <attribute name="action">win.selectall</attribute> 58 <attribute name="action">win.selectall</attribute>
59 </item> 59 </item>
60 </section> 60 </section>
61 </submenu> 61 </submenu>
62 <submenu> 62 <submenu>
63 <attribute name="label">View</attribute> 63 <attribute name="label">View</attribute>
64 <section> 64 <section>
65 <item> 65 <item>
66 <attribute name="label">Full Screen</attribute> 66 <attribute name="label">Full Screen</attribute>
67 <attribute name="action">win.fullscreen</attribute> 67 <attribute name="action">win.fullscreen</attribute>
68 </item> 68 </item>
69 </section> 69 </section>
70 </submenu> 70 </submenu>
71 </menu> 71 </menu>
72 </interface> 72 </interface>
~~~
The ui file is converted to the resource by the resouce compiler `glib-compile-resouces` with xml file below. The ui file is converted to the resource by the resouce compiler `glib-compile-resouces` with xml file below.
1 <?xml version="1.0" encoding="UTF-8"?> ~~~xml
2 <gresources> 1 <?xml version="1.0" encoding="UTF-8"?>
3 <gresource prefix="/com/github/ToshioCP/menu3"> 2 <gresources>
4 <file>menu3.ui</file> 3 <gresource prefix="/com/github/ToshioCP/menu3">
5 </gresource> 4 <file>menu3.ui</file>
6 </gresources> 5 </gresource>
6 </gresources>
~~~
GtkBuilder builds menus from the resource. GtkBuilder builds menus from the resource.
GtkBuilder *builder = gtk_builder_new_from_resource ("/com/github/ToshioCP/menu3/menu3.ui"); ~~~C
GMenuModel *menubar = G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")); GtkBuilder *builder = gtk_builder_new_from_resource ("/com/github/ToshioCP/menu3/menu3.ui");
GMenuModel *menubar = G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"));
gtk_application_set_menubar (GTK_APPLICATION (app), menubar); gtk_application_set_menubar (GTK_APPLICATION (app), menubar);
g_object_unref (builder); g_object_unref (builder);
~~~
It is important that `builder` is unreferred after the GMenuModel `menubar` is set to the application. It is important that `builder` is unreferred after the GMenuModel `menubar` is set to the application.
If you do it before setting, bad thing will happen -- your computer might freeze. If you do it before setting, bad thing will happen -- your computer might freeze.
@ -164,31 +176,42 @@ You can implement them easily with GActionEntry structure and `g_action_map_add_
GActionEntry contains action name, signal handlers, parameter and state. GActionEntry contains action name, signal handlers, parameter and state.
typedef struct _GActionEntry GActionEntry; ~~~C
typedef struct _GActionEntry GActionEntry;
struct _GActionEntry struct _GActionEntry
{ {
const gchar *name; /* action name */ /* action name */
void (* activate) (GSimpleAction *action, GVariant *parameter, gpointer user_data); /* activate handler */ const gchar *name;
const gchar *parameter_type; /* the type of the parameter given as a single GVariant type string */ /* activate handler */
const gchar *state; /* initial state given in GVariant text format */ void (* activate) (GSimpleAction *action, GVariant *parameter, gpointer user_data);
void (* change_state) (GSimpleAction *action, GVariant *value, gpointer user_data); /* change-state handler */ /* the type of the parameter given as a single GVariant type string */
const gchar *parameter_type;
/* initial state given in GVariant text format */
const gchar *state;
/* change-state handler */
void (* change_state) (GSimpleAction *action, GVariant *value, gpointer user_data);
/*< private >*/ /*< private >*/
gsize padding[3]; gsize padding[3];
}; };
~~~
For example, the actions in the previous section are: For example, the actions in the previous section are:
{ "fullscreen", NULL, NULL, "false", fullscreen_changed } ~~~C
{ "color", color_activated, "s", "red", NULL } { "fullscreen", NULL, NULL, "false", fullscreen_changed }
{ "quit", quit_activated, NULL, NULL, NULL }, { "color", color_activated, "s", "red", NULL }
{ "quit", quit_activated, NULL, NULL, NULL },
~~~
And `g_action_map_add_action_entries` does all the process instead of the functions you have needed. And `g_action_map_add_action_entries` does all the process instead of the functions you have needed.
const GActionEntry app_entries[] = { ~~~C
const GActionEntry app_entries[] = {
{ "quit", quit_activated, NULL, NULL, NULL } { "quit", quit_activated, NULL, NULL, NULL }
}; };
g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries, G_N_ELEMENTS (app_entries), app); g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries,
G_N_ELEMENTS (app_entries), app);
~~~
The code above does: The code above does:
@ -198,12 +221,14 @@ The code above does:
The same goes for the other actions. The same goes for the other actions.
const GActionEntry win_entries[] = { ~~~C
const GActionEntry win_entries[] = {
{ "fullscreen", NULL, NULL, "false", fullscreen_changed }, { "fullscreen", NULL, NULL, "false", fullscreen_changed },
{ "color", color_activated, "s", "red", NULL } { "color", color_activated, "s", "red", NULL }
}; };
g_action_map_add_action_entries (G_ACTION_MAP (win), win_entries, G_N_ELEMENTS (win_entries), win); g_action_map_add_action_entries (G_ACTION_MAP (win), win_entries,
G_N_ELEMENTS (win_entries), win);
~~~
The code above does: The code above does:
- Build a "fullscreen" action and "color" action. - Build a "fullscreen" action and "color" action.
@ -217,6 +242,7 @@ The code above does:
The C source code of `menu3` and `meson.build` is as follows. The C source code of `menu3` and `meson.build` is as follows.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -316,16 +342,18 @@ The C source code of `menu3` and `meson.build` is as follows.
97 97
98 app = gtk_application_new ("com.github.ToshioCP.menu3", G_APPLICATION_FLAGS_NONE); 98 app = gtk_application_new ("com.github.ToshioCP.menu3", G_APPLICATION_FLAGS_NONE);
99 g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL); 99 g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
100 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 100 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
101 101
102 stat =g_application_run (G_APPLICATION (app), argc, argv); 102 stat =g_application_run (G_APPLICATION (app), argc, argv);
103 g_object_unref (app); 103 g_object_unref (app);
104 return stat; 104 return stat;
105 } 105 }
106 106
~~~
meson.build meson.build
~~~meson
1 project('menu3', 'c') 1 project('menu3', 'c')
2 2
3 gtkdep = dependency('gtk4') 3 gtkdep = dependency('gtk4')
@ -335,7 +363,8 @@ meson.build
7 7
8 sourcefiles=files('menu3.c') 8 sourcefiles=files('menu3.c')
9 9
10 executable('menu3', sourcefiles, resources, dependencies: gtkdep) 10 executable('menu3', sourcefiles, resources, dependencies: gtkdep)
~~~
Up: [Readme.md](../Readme.md), Prev: [Section 17](sec17.md) Up: [Readme.md](../Readme.md), Prev: [Section 17](sec17.md), Next: [Section 19](sec19.md)

View file

@ -1,4 +1,4 @@
Up: [Readme.md](../Readme.md), Prev: [Section 18](sec18.md) Up: [Readme.md](../Readme.md), Prev: [Section 18](sec18.md), Next: [Section 20](sec20.md)
# GtkDrawingArea and Cairo # GtkDrawingArea and Cairo
@ -46,6 +46,7 @@ This will be a destnation.
Here's a simple example code that draws a small square and save it as a png file. Here's a simple example code that draws a small square and save it as a png file.
~~~C
1 #include <cairo.h> 1 #include <cairo.h>
2 2
3 int 3 int
@ -55,28 +56,29 @@ Here's a simple example code that draws a small square and save it as a png file
7 cairo_t *cr; 7 cairo_t *cr;
8 int width = 100; 8 int width = 100;
9 int height = 100; 9 int height = 100;
10 10
11 /* Generate surface and cairo */ 11 /* Generate surface and cairo */
12 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height); 12 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
13 cr = cairo_create (surface); 13 cr = cairo_create (surface);
14 14
15 /* Drawing starts here. */ 15 /* Drawing starts here. */
16 /* Paint the background white */ 16 /* Paint the background white */
17 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); 17 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
18 cairo_paint (cr); 18 cairo_paint (cr);
19 /* Draw a black rectangle */ 19 /* Draw a black rectangle */
20 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); 20 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
21 cairo_set_line_width (cr, 2.0); 21 cairo_set_line_width (cr, 2.0);
22 cairo_rectangle (cr, width/2.0 - 20.0, height/2.0 - 20.0, 40.0, 40.0); 22 cairo_rectangle (cr, width/2.0 - 20.0, height/2.0 - 20.0, 40.0, 40.0);
23 cairo_stroke (cr); 23 cairo_stroke (cr);
24 24
25 /* Write the surface to a png file and clean up cairo and surface. */ 25 /* Write the surface to a png file and clean up cairo and surface. */
26 cairo_surface_write_to_png (surface, "rectangle.png"); 26 cairo_surface_write_to_png (surface, "rectangle.png");
27 cairo_destroy (cr); 27 cairo_destroy (cr);
28 cairo_surface_destroy (surface); 28 cairo_surface_destroy (surface);
29 29
30 return 0; 30 return 0;
31 } 31 }
~~~
- 1: Include the header file of cairo. - 1: Include the header file of cairo.
- 12: `cairo_image_surface_create` creates an image surface. - 12: `cairo_image_surface_create` creates an image surface.
@ -117,6 +119,7 @@ If you aren't familiar with cairo, it is strongly recommended to read the [tutor
The following is a very simple example. The following is a very simple example.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -126,36 +129,37 @@ The following is a very simple example.
7 cairo_set_line_width (cr, 2.0); 7 cairo_set_line_width (cr, 2.0);
8 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */ 8 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
9 cairo_rectangle (cr, width/2.0 - 20.0, height/2.0 - 20.0, 40.0, 40.0); 9 cairo_rectangle (cr, width/2.0 - 20.0, height/2.0 - 20.0, 40.0, 40.0);
10 cairo_stroke (cr); 10 cairo_stroke (cr);
11 } 11 }
12 12
13 static void 13 static void
14 on_activate (GApplication *app, gpointer user_data) { 14 on_activate (GApplication *app, gpointer user_data) {
15 GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app)); 15 GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
16 GtkWidget *area = gtk_drawing_area_new (); 16 GtkWidget *area = gtk_drawing_area_new ();
17 17
18 gtk_window_set_title (GTK_WINDOW (win), "da1"); 18 gtk_window_set_title (GTK_WINDOW (win), "da1");
19 /* Set initial size of width and height */ 19 /* Set initial size of width and height */
20 gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 100); 20 gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 100);
21 gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 100); 21 gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 100);
22 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area), draw_function, NULL, NULL); 22 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area), draw_function, NULL, NULL);
23 gtk_window_set_child (GTK_WINDOW (win), area); 23 gtk_window_set_child (GTK_WINDOW (win), area);
24 24
25 gtk_widget_show (win); 25 gtk_widget_show (win);
26 } 26 }
27 27
28 int 28 int
29 main (int argc, char **argv) { 29 main (int argc, char **argv) {
30 GtkApplication *app; 30 GtkApplication *app;
31 int stat; 31 int stat;
32 32
33 app = gtk_application_new ("com.github.ToshioCP.da1", G_APPLICATION_FLAGS_NONE); 33 app = gtk_application_new ("com.github.ToshioCP.da1", G_APPLICATION_FLAGS_NONE);
34 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 34 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
35 stat =g_application_run (G_APPLICATION (app), argc, argv); 35 stat =g_application_run (G_APPLICATION (app), argc, argv);
36 g_object_unref (app); 36 g_object_unref (app);
37 return stat; 37 return stat;
38 } 38 }
39 39
~~~
The function `main` is almost same as before. The function `main` is almost same as before.
The two functions `on_activate` and `draw_function` is important in this example. The two functions `on_activate` and `draw_function` is important in this example.
@ -194,4 +198,4 @@ The square always appears at the center of the window because the drawing functi
![Square in the window](../image/da1.png) ![Square in the window](../image/da1.png)
Up: [Readme.md](../Readme.md), Prev: [Section 18](sec18.md) Up: [Readme.md](../Readme.md), Prev: [Section 18](sec18.md), Next: [Section 20](sec20.md)

View file

@ -143,7 +143,8 @@ Modify `env.sh`.
# compiler # compiler
CPPFLAGS="-I$HOME/local/include" CPPFLAGS="-I$HOME/local/include"
LDFLAGS="-L$HOME/local/lib" LDFLAGS="-L$HOME/local/lib"
PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig:$HOME/local/lib/x86_64-linux-gnu/pkgconfig:$HOME/local/share/pkgconfig" PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig:$HOME/local/lib/x86_64-linux-gnu/pkgconfig:
$HOME/local/share/pkgconfig"
export CPPFLAGS LDFLAGS PKG_CONFIG_PATH export CPPFLAGS LDFLAGS PKG_CONFIG_PATH
# linker # linker
LD_LIBRARY_PATH="$HOME/local/lib/x86_64-linux-gnu/" LD_LIBRARY_PATH="$HOME/local/lib/x86_64-linux-gnu/"

View file

@ -34,6 +34,7 @@ The image in the previous subsection gives us the structure of the widgets.
Title bar, four buttons in the tool bar and two widgets textview and drawing area. Title bar, four buttons in the tool bar and two widgets textview and drawing area.
The ui file is as follows. The ui file is as follows.
~~~xml
1 <interface> 1 <interface>
2 <object class="GtkApplicationWindow" id="win"> 2 <object class="GtkApplicationWindow" id="win">
3 <property name="title">color changer</property> 3 <property name="title">color changer</property>
@ -43,78 +44,79 @@ The ui file is as follows.
7 <object class="GtkBox" id="boxv"> 7 <object class="GtkBox" id="boxv">
8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property> 8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property>
9 <child> 9 <child>
10 <object class="GtkBox" id="boxh1"> 10 <object class="GtkBox" id="boxh1">
11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property> 11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
12 <child> 12 <child>
13 <object class="GtkLabel" id="dmy1"> 13 <object class="GtkLabel" id="dmy1">
14 <property name="width-chars">10</property> 14 <property name="width-chars">10</property>
15 </object> 15 </object>
16 </child> 16 </child>
17 <child> 17 <child>
18 <object class="GtkButton" id="btnr"> 18 <object class="GtkButton" id="btnr">
19 <property name="label">Run</property> 19 <property name="label">Run</property>
20 <signal name="clicked" handler="run_cb"></signal> 20 <signal name="clicked" handler="run_cb"></signal>
21 21
22 </object> 22 </object>
23 </child> 23 </child>
24 <child> 24 <child>
25 <object class="GtkButton" id="btno"> 25 <object class="GtkButton" id="btno">
26 <property name="label">Open</property> 26 <property name="label">Open</property>
27 <signal name="clicked" handler="open_cb"></signal> 27 <signal name="clicked" handler="open_cb"></signal>
28 </object> 28 </object>
29 </child> 29 </child>
30 <child> 30 <child>
31 <object class="GtkLabel" id="dmy2"> 31 <object class="GtkLabel" id="dmy2">
32 <property name="hexpand">TRUE</property> 32 <property name="hexpand">TRUE</property>
33 </object> 33 </object>
34 </child> 34 </child>
35 <child> 35 <child>
36 <object class="GtkButton" id="btns"> 36 <object class="GtkButton" id="btns">
37 <property name="label">Save</property> 37 <property name="label">Save</property>
38 <signal name="clicked" handler="save_cb"></signal> 38 <signal name="clicked" handler="save_cb"></signal>
39 </object> 39 </object>
40 </child> 40 </child>
41 <child> 41 <child>
42 <object class="GtkButton" id="btnc"> 42 <object class="GtkButton" id="btnc">
43 <property name="label">Close</property> 43 <property name="label">Close</property>
44 <signal name="clicked" handler="close_cb"></signal> 44 <signal name="clicked" handler="close_cb"></signal>
45 </object> 45 </object>
46 </child> 46 </child>
47 <child> 47 <child>
48 <object class="GtkLabel" id="dmy3"> 48 <object class="GtkLabel" id="dmy3">
49 <property name="width-chars">10</property> 49 <property name="width-chars">10</property>
50 </object> 50 </object>
51 </child> 51 </child>
52 </object> 52 </object>
53 </child> 53 </child>
54 <child> 54 <child>
55 <object class="GtkBox" id="boxh2"> 55 <object class="GtkBox" id="boxh2">
56 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property> 56 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
57 <property name="homogeneous">TRUE</property> 57 <property name="homogeneous">TRUE</property>
58 <child> 58 <child>
59 <object class="GtkScrolledWindow" id="scr"> 59 <object class="GtkScrolledWindow" id="scr">
60 <property name="hexpand">TRUE</property> 60 <property name="hexpand">TRUE</property>
61 <property name="vexpand">TRUE</property> 61 <property name="vexpand">TRUE</property>
62 <child> 62 <child>
63 <object class="TfeTextView" id="tv"> 63 <object class="TfeTextView" id="tv">
64 <property name="wrap-mode">GTK_WRAP_WORD_CHAR</property> 64 <property name="wrap-mode">GTK_WRAP_WORD_CHAR</property>
65 </object> 65 </object>
66 </child> 66 </child>
67 </object> 67 </object>
68 </child> 68 </child>
69 <child> 69 <child>
70 <object class="GtkDrawingArea" id="da"> 70 <object class="GtkDrawingArea" id="da">
71 <property name="hexpand">TRUE</property> 71 <property name="hexpand">TRUE</property>
72 <property name="vexpand">TRUE</property> 72 <property name="vexpand">TRUE</property>
73 </object> 73 </object>
74 </child> 74 </child>
75 </object> 75 </object>
76 </child> 76 </child>
77 </object> 77 </object>
78 </child> 78 </child>
79 </object> 79 </object>
80 </interface> 80 </interface>
81 81
~~~
- 9-53: This part describes the tool bar which has four buttons, `Run`, `Open`, `Save` and `Close`. - 9-53: This part describes the tool bar which has four buttons, `Run`, `Open`, `Save` and `Close`.
This is similar to the toolbar of tfe text editor in [Section 8](sec8.md). This is similar to the toolbar of tfe text editor in [Section 8](sec8.md).
@ -132,12 +134,14 @@ TfeTextView is a child of GtkScrolledWindow.
The xml file for the resource compiler is almost same as before. The xml file for the resource compiler is almost same as before.
Just substitute "color" for "tfe". Just substitute "color" for "tfe".
1 <?xml version="1.0" encoding="UTF-8"?> ~~~xml
2 <gresources> 1 <?xml version="1.0" encoding="UTF-8"?>
3 <gresource prefix="/com/github/ToshioCP/color"> 2 <gresources>
4 <file>color.ui</file> 3 <gresource prefix="/com/github/ToshioCP/color">
5 </gresource> 4 <file>color.ui</file>
6 </gresources> 5 </gresource>
6 </gresources>
~~~
# Tfetextview.h, tfetextview.c and color.h # Tfetextview.h, tfetextview.c and color.h
@ -152,9 +156,11 @@ The only difference is the header file in tfettextview.c.
Color.h just includes tfetextview.h. Color.h just includes tfetextview.h.
1 #include <gtk/gtk.h> ~~~C
2 1 #include <gtk/gtk.h>
3 #include "tfetextview.h" 2
3 #include "tfetextview.h"
~~~
# Colorapplication.c # Colorapplication.c
@ -169,6 +175,7 @@ Particularly, `Run` signal handler is the point in this program.
The following is `colorapplication.c`. The following is `colorapplication.c`.
~~~C
1 #include "color.h" 1 #include "color.h"
2 2
3 static GtkWidget *win; 3 static GtkWidget *win;
@ -268,29 +275,30 @@ The following is `colorapplication.c`.
97 g_signal_connect (GTK_DRAWING_AREA (da), "resize", G_CALLBACK (resize_cb), NULL); 97 g_signal_connect (GTK_DRAWING_AREA (da), "resize", G_CALLBACK (resize_cb), NULL);
98 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_func, NULL, NULL); 98 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (da), draw_func, NULL, NULL);
99 99
100 GdkDisplay *display; 100 GdkDisplay *display;
101 101
102 display = gtk_widget_get_display (GTK_WIDGET (win)); 102 display = gtk_widget_get_display (GTK_WIDGET (win));
103 GtkCssProvider *provider = gtk_css_provider_new (); 103 GtkCssProvider *provider = gtk_css_provider_new ();
104 gtk_css_provider_load_from_data (provider, "textview {padding: 10px; font-family: monospace; font-size: 12pt;}", -1); 104 gtk_css_provider_load_from_data (provider, "textview {padding: 10px; font-family: monospace; font-size: 12pt;}", -1);
105 gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER); 105 gtk_style_context_add_provider_for_display (display, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
106 } 106 }
107 107
108 int 108 int
109 main (int argc, char **argv) { 109 main (int argc, char **argv) {
110 GtkApplication *app; 110 GtkApplication *app;
111 int stat; 111 int stat;
112 112
113 app = gtk_application_new ("com.github.ToshioCP.color", G_APPLICATION_FLAGS_NONE); 113 app = gtk_application_new ("com.github.ToshioCP.color", G_APPLICATION_FLAGS_NONE);
114 114
115 g_signal_connect (app, "startup", G_CALLBACK (startup), NULL); 115 g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
116 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); 116 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
117 117
118 stat =g_application_run (G_APPLICATION (app), argc, argv); 118 stat =g_application_run (G_APPLICATION (app), argc, argv);
119 g_object_unref (app); 119 g_object_unref (app);
120 return stat; 120 return stat;
121 } 121 }
122 122
~~~
- 108-121: The function `main` is almost same as before but there are some differences. - 108-121: The function `main` is almost same as before but there are some differences.
The application ID is "com.github.ToshioCP.color". The application ID is "com.github.ToshioCP.color".
@ -332,6 +340,7 @@ Alpha channel is used.
This file is almost same as before. This file is almost same as before.
An argument "export_dynamic: true" is added to executable function. An argument "export_dynamic: true" is added to executable function.
~~~meson
1 project('color', 'c') 1 project('color', 'c')
2 2
3 gtkdep = dependency('gtk4') 3 gtkdep = dependency('gtk4')
@ -341,7 +350,8 @@ An argument "export_dynamic: true" is added to executable function.
7 7
8 sourcefiles=files('colorapplication.c', 'tfetextview.c') 8 sourcefiles=files('colorapplication.c', 'tfetextview.c')
9 9
10 executable('color', sourcefiles, resources, dependencies: gtkdep, export_dynamic: true) 10 executable('color', sourcefiles, resources, dependencies: gtkdep, export_dynamic: true)
~~~
# Compile and execute it # Compile and execute it

View file

@ -20,6 +20,7 @@ That's all.
Very simple. Very simple.
The following is the C code representing the scenario above. The following is the C code representing the scenario above.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 int 3 int
@ -29,10 +30,11 @@ The following is the C code representing the scenario above.
7 7
8 app = gtk_application_new ("com.github.ToshioCP.pr1", G_APPLICATION_FLAGS_NONE); 8 app = gtk_application_new ("com.github.ToshioCP.pr1", G_APPLICATION_FLAGS_NONE);
9 stat =g_application_run (G_APPLICATION (app), argc, argv); 9 stat =g_application_run (G_APPLICATION (app), argc, argv);
10 g_object_unref (app); 10 g_object_unref (app);
11 return stat; 11 return stat;
12 } 12 }
13 13
~~~
The first line says that this program includes the header files of the Gtk libraries. The first line says that this program includes the header files of the Gtk libraries.
The function `main` above is a startup function in C language. The function `main` above is a startup function in C language.
@ -54,7 +56,9 @@ Let's run it.
$ ./a.out $ ./a.out
(a.out:13533): GLib-GIO-WARNING **: 15:30:17.449: Your application does not implement g_application_activate() and has no handlers connected to the "activate" signal. It should do one of these. (a.out:13533): GLib-GIO-WARNING **: 15:30:17.449: Your application does not implement
g_application_activate() and has no handlers connected to the "activate" signal.
It should do one of these.
$ $
Oh, just an error message. Oh, just an error message.
@ -97,6 +101,7 @@ Now we can solve the problem in `pr1.c`.
We need to connect the activate signal to a handler. We need to connect the activate signal to a handler.
We use a function `g_signal_connect` which connects a signal to a handler. We use a function `g_signal_connect` which connects a signal to a handler.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -106,16 +111,17 @@ We use a function `g_signal_connect` which connects a signal to a handler.
7 7
8 int 8 int
9 main (int argc, char **argv) { 9 main (int argc, char **argv) {
10 GtkApplication *app; 10 GtkApplication *app;
11 int stat; 11 int stat;
12 12
13 app = gtk_application_new ("com.github.ToshioCP.pr2", G_APPLICATION_FLAGS_NONE); 13 app = gtk_application_new ("com.github.ToshioCP.pr2", G_APPLICATION_FLAGS_NONE);
14 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 14 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
15 stat =g_application_run (G_APPLICATION (app), argc, argv); 15 stat =g_application_run (G_APPLICATION (app), argc, argv);
16 g_object_unref (app); 16 g_object_unref (app);
17 return stat; 17 return stat;
18 } 18 }
19 19
~~~
First, we define the handler `on_activate` which simply displays a message. First, we define the handler `on_activate` which simply displays a message.
In the function `main`, we add `g_signal_connect` before `g_application_run`. In the function `main`, we add `g_signal_connect` before `g_application_run`.
@ -185,14 +191,16 @@ Now rewrite the function `on_activate`.
#### Generate a GtkWindow #### Generate a GtkWindow
1 static void ~~~C
2 on_activate (GApplication *app, gpointer user_data) { 1 static void
3 GtkWidget *win; 2 on_activate (GApplication *app, gpointer user_data) {
4 3 GtkWidget *win;
5 win = gtk_window_new (); 4
6 gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app)); 5 win = gtk_window_new ();
7 gtk_widget_show (win); 6 gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app));
8 } 7 gtk_widget_show (win);
8 }
~~~
Widget is an abstract concept that includes all the GUI interfaces such as windows, dialogs, buttons, multiline text, containers and so on. Widget is an abstract concept that includes all the GUI interfaces such as windows, dialogs, buttons, multiline text, containers and so on.
And GtkWidget is a base object from which all the GUI objects derive. And GtkWidget is a base object from which all the GUI objects derive.
@ -265,15 +273,17 @@ It is recommended to use it instead of GtkWindow when you use GtkApplication.
Now rewrite the program and use GtkAppliction Window. Now rewrite the program and use GtkAppliction Window.
1 static void ~~~C
2 on_activate (GApplication *app, gpointer user_data) { 1 static void
3 GtkWidget *win; 2 on_activate (GApplication *app, gpointer user_data) {
4 3 GtkWidget *win;
5 win = gtk_application_window_new (GTK_APPLICATION (app)); 4
6 gtk_window_set_title (GTK_WINDOW (win), "pr4"); 5 win = gtk_application_window_new (GTK_APPLICATION (app));
7 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 6 gtk_window_set_title (GTK_WINDOW (win), "pr4");
8 gtk_widget_show (win); 7 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
9 } 8 gtk_widget_show (win);
9 }
~~~
When you generate GtkApplicationWindow, you need to give GtkApplication object as an argument. When you generate GtkApplicationWindow, you need to give GtkApplication object as an argument.
Then it automatically connect these two objects. Then it automatically connect these two objects.

View file

@ -11,6 +11,7 @@ Now we go on to the next topic, widgets in the 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 a string in it.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -20,26 +21,27 @@ It is a widget with a string in it.
7 7
8 win = gtk_application_window_new (GTK_APPLICATION (app)); 8 win = gtk_application_window_new (GTK_APPLICATION (app));
9 gtk_window_set_title (GTK_WINDOW (win), "lb1"); 9 gtk_window_set_title (GTK_WINDOW (win), "lb1");
10 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 10 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
11 11
12 lab = gtk_label_new ("Hello."); 12 lab = gtk_label_new ("Hello.");
13 gtk_window_set_child (GTK_WINDOW (win), lab); 13 gtk_window_set_child (GTK_WINDOW (win), lab);
14 14
15 gtk_widget_show (win); 15 gtk_widget_show (win);
16 } 16 }
17 17
18 int 18 int
19 main (int argc, char **argv) { 19 main (int argc, char **argv) {
20 GtkApplication *app; 20 GtkApplication *app;
21 int stat; 21 int stat;
22 22
23 app = gtk_application_new ("com.github.ToshioCP.lb1", G_APPLICATION_FLAGS_NONE); 23 app = gtk_application_new ("com.github.ToshioCP.lb1", G_APPLICATION_FLAGS_NONE);
24 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 24 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
25 stat =g_application_run (G_APPLICATION (app), argc, argv); 25 stat =g_application_run (G_APPLICATION (app), argc, argv);
26 g_object_unref (app); 26 g_object_unref (app);
27 return stat; 27 return stat;
28 } 28 }
29 29
~~~
Save this program to a file `lb1.c`. Save this program to a file `lb1.c`.
Then compile and run it. Then compile and run it.
@ -100,6 +102,7 @@ In this subsection, we will make a button with a label.
When a button is clicked on, it emits a "clicked" signal. When a button is clicked on, 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 and do something.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -109,32 +112,33 @@ The following program shows how to catch the signal and do something.
7 7
8 static void 8 static void
9 on_activate (GApplication *app, gpointer user_data) { 9 on_activate (GApplication *app, gpointer user_data) {
10 GtkWidget *win; 10 GtkWidget *win;
11 GtkWidget *btn; 11 GtkWidget *btn;
12 12
13 win = gtk_application_window_new (GTK_APPLICATION (app)); 13 win = gtk_application_window_new (GTK_APPLICATION (app));
14 gtk_window_set_title (GTK_WINDOW (win), "lb2"); 14 gtk_window_set_title (GTK_WINDOW (win), "lb2");
15 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 15 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
16 16
17 btn = gtk_button_new_with_label ("Click me"); 17 btn = gtk_button_new_with_label ("Click me");
18 gtk_window_set_child (GTK_WINDOW (win), btn); 18 gtk_window_set_child (GTK_WINDOW (win), btn);
19 g_signal_connect (btn, "clicked", G_CALLBACK (click_cb), NULL); 19 g_signal_connect (btn, "clicked", G_CALLBACK (click_cb), NULL);
20 20
21 gtk_widget_show (win); 21 gtk_widget_show (win);
22 } 22 }
23 23
24 int 24 int
25 main (int argc, char **argv) { 25 main (int argc, char **argv) {
26 GtkApplication *app; 26 GtkApplication *app;
27 int stat; 27 int stat;
28 28
29 app = gtk_application_new ("com.github.ToshioCP.lb2", G_APPLICATION_FLAGS_NONE); 29 app = gtk_application_new ("com.github.ToshioCP.lb2", G_APPLICATION_FLAGS_NONE);
30 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 30 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
31 stat =g_application_run (G_APPLICATION (app), argc, argv); 31 stat =g_application_run (G_APPLICATION (app), argc, argv);
32 g_object_unref (app); 32 g_object_unref (app);
33 return stat; 33 return stat;
34 } 34 }
35 35
~~~
Look at the line 17 to 19. Look at the line 17 to 19.
First, generate a GtkButton widget `btn` with a label "Click me". First, generate a GtkButton widget `btn` with a label "Click me".
@ -157,6 +161,7 @@ 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`.
~~~C
1 static void 1 static void
2 click_cb (GtkButton *btn, gpointer user_data) { 2 click_cb (GtkButton *btn, gpointer user_data) {
3 GtkWindow *win = GTK_WINDOW (user_data); 3 GtkWindow *win = GTK_WINDOW (user_data);
@ -166,18 +171,19 @@ The following code is `lb3.c`.
7 static void 7 static void
8 on_activate (GApplication *app, gpointer user_data) { 8 on_activate (GApplication *app, gpointer user_data) {
9 GtkWidget *win; 9 GtkWidget *win;
10 GtkWidget *btn; 10 GtkWidget *btn;
11 11
12 win = gtk_application_window_new (GTK_APPLICATION (app)); 12 win = gtk_application_window_new (GTK_APPLICATION (app));
13 gtk_window_set_title (GTK_WINDOW (win), "lb3"); 13 gtk_window_set_title (GTK_WINDOW (win), "lb3");
14 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 14 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
15 15
16 btn = gtk_button_new_with_label ("Quit"); 16 btn = gtk_button_new_with_label ("Quit");
17 gtk_window_set_child (GTK_WINDOW (win), btn); 17 gtk_window_set_child (GTK_WINDOW (win), btn);
18 g_signal_connect (btn, "clicked", G_CALLBACK (click_cb), win); 18 g_signal_connect (btn, "clicked", G_CALLBACK (click_cb), win);
19 19
20 gtk_widget_show (win); 20 gtk_widget_show (win);
21 } 21 }
~~~
And the difference between `lb2.c` and `lb3.c` is as follows. And the difference between `lb2.c` and `lb3.c` is as follows.
@ -237,6 +243,7 @@ After this, the Widgets are connected as following diagram.
Now, code it. Now, code it.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -246,54 +253,55 @@ Now, code it.
7 s = gtk_button_get_label (btn); 7 s = gtk_button_get_label (btn);
8 if (g_strcmp0 (s, "Hello.") == 0) 8 if (g_strcmp0 (s, "Hello.") == 0)
9 gtk_button_set_label (btn, "Good-bye."); 9 gtk_button_set_label (btn, "Good-bye.");
10 else 10 else
11 gtk_button_set_label (btn, "Hello."); 11 gtk_button_set_label (btn, "Hello.");
12 } 12 }
13 13
14 static void 14 static void
15 click2_cb (GtkButton *btn, gpointer user_data) { 15 click2_cb (GtkButton *btn, gpointer user_data) {
16 GtkWindow *win = GTK_WINDOW (user_data); 16 GtkWindow *win = GTK_WINDOW (user_data);
17 gtk_window_destroy (win); 17 gtk_window_destroy (win);
18 } 18 }
19 19
20 static void 20 static void
21 on_activate (GApplication *app, gpointer user_data) { 21 on_activate (GApplication *app, gpointer user_data) {
22 GtkWidget *win; 22 GtkWidget *win;
23 GtkWidget *box; 23 GtkWidget *box;
24 GtkWidget *btn1; 24 GtkWidget *btn1;
25 GtkWidget *btn2; 25 GtkWidget *btn2;
26 26
27 win = gtk_application_window_new (GTK_APPLICATION (app)); 27 win = gtk_application_window_new (GTK_APPLICATION (app));
28 gtk_window_set_title (GTK_WINDOW (win), "lb4"); 28 gtk_window_set_title (GTK_WINDOW (win), "lb4");
29 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 29 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
30 30
31 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); 31 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
32 gtk_box_set_homogeneous (GTK_BOX (box), TRUE); 32 gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
33 gtk_window_set_child (GTK_WINDOW (win), box); 33 gtk_window_set_child (GTK_WINDOW (win), box);
34 34
35 btn1 = gtk_button_new_with_label ("Hello."); 35 btn1 = gtk_button_new_with_label ("Hello.");
36 g_signal_connect (btn1, "clicked", G_CALLBACK (click1_cb), NULL); 36 g_signal_connect (btn1, "clicked", G_CALLBACK (click1_cb), NULL);
37 37
38 btn2 = gtk_button_new_with_label ("Quit"); 38 btn2 = gtk_button_new_with_label ("Quit");
39 g_signal_connect (btn2, "clicked", G_CALLBACK (click2_cb), win); 39 g_signal_connect (btn2, "clicked", G_CALLBACK (click2_cb), win);
40 40
41 gtk_box_append (GTK_BOX (box), btn1); 41 gtk_box_append (GTK_BOX (box), btn1);
42 gtk_box_append (GTK_BOX (box), btn2); 42 gtk_box_append (GTK_BOX (box), btn2);
43 43
44 gtk_widget_show (win); 44 gtk_widget_show (win);
45 } 45 }
46 46
47 int 47 int
48 main (int argc, char **argv) { 48 main (int argc, char **argv) {
49 GtkApplication *app; 49 GtkApplication *app;
50 int stat; 50 int stat;
51 51
52 app = gtk_application_new ("com.github.ToshioCP.lb4", G_APPLICATION_FLAGS_NONE); 52 app = gtk_application_new ("com.github.ToshioCP.lb4", G_APPLICATION_FLAGS_NONE);
53 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 53 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
54 stat =g_application_run (G_APPLICATION (app), argc, argv); 54 stat =g_application_run (G_APPLICATION (app), argc, argv);
55 g_object_unref (app); 55 g_object_unref (app);
56 return stat; 56 return stat;
57 } 57 }
~~~
Look at the function `on_activate`. Look at the function `on_activate`.

View file

@ -10,6 +10,7 @@ GtkTextview is a widget for multiline text editing.
GtkTextBuffer is a text buffer which is connected to GtkTextView. GtkTextBuffer is a text buffer which is connected to GtkTextView.
See a sample program `tfv1.c` below. See a sample program `tfv1.c` below.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -19,43 +20,44 @@ See a sample program `tfv1.c` below.
7 GtkTextBuffer *tb; 7 GtkTextBuffer *tb;
8 gchar *text; 8 gchar *text;
9 9
10 text = 10 text =
11 "Once upon a time, there was an old man who was called Taketori-no-Okina." 11 "Once upon a time, there was an old man who was called Taketori-no-Okina."
12 "It is a japanese word that means a man whose work is making bamboo baskets.\n" 12 "It is a japanese word that means a man whose work is making bamboo baskets.\n"
13 "One day, he went into a mountain and found a shining bamboo." 13 "One day, he went into a mountain and found a shining bamboo."
14 "\"What a mysterious bamboo it is!,\" he said." 14 "\"What a mysterious bamboo it is!,\" he said."
15 "He cut it, then there was a small cute baby girl in it." 15 "He cut it, then there was a small cute baby girl in it."
16 "The girl was shining faintly." 16 "The girl was shining faintly."
17 "He thought this baby girl is a gift from Heaven and took her home.\n" 17 "He thought this baby girl is a gift from Heaven and took her home.\n"
18 "His wife was surprized at his tale." 18 "His wife was surprized at his tale."
19 "They were very happy because they had no children." 19 "They were very happy because they had no children."
20 ; 20 ;
21 win = gtk_application_window_new (GTK_APPLICATION (app)); 21 win = gtk_application_window_new (GTK_APPLICATION (app));
22 gtk_window_set_title (GTK_WINDOW (win), "Taketori"); 22 gtk_window_set_title (GTK_WINDOW (win), "Taketori");
23 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 23 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
24 24
25 tv = gtk_text_view_new (); 25 tv = gtk_text_view_new ();
26 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 26 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
27 gtk_text_buffer_set_text (tb, text, -1); 27 gtk_text_buffer_set_text (tb, text, -1);
28 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 28 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
29 29
30 gtk_window_set_child (GTK_WINDOW (win), tv); 30 gtk_window_set_child (GTK_WINDOW (win), tv);
31 31
32 gtk_widget_show (win); 32 gtk_widget_show (win);
33 } 33 }
34 34
35 int 35 int
36 main (int argc, char **argv) { 36 main (int argc, char **argv) {
37 GtkApplication *app; 37 GtkApplication *app;
38 int stat; 38 int stat;
39 39
40 app = gtk_application_new ("com.github.ToshioCP.tfv1", G_APPLICATION_FLAGS_NONE); 40 app = gtk_application_new ("com.github.ToshioCP.tfv1", G_APPLICATION_FLAGS_NONE);
41 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 41 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
42 stat =g_application_run (G_APPLICATION (app), argc, argv); 42 stat =g_application_run (G_APPLICATION (app), argc, argv);
43 g_object_unref (app); 43 g_object_unref (app);
44 return stat; 44 return stat;
45 } 45 }
46 46
~~~
Look at line 25. Look at line 25.
GtkTextView is generated and its pointer is assigned to `tv`. GtkTextView is generated and its pointer is assigned to `tv`.
@ -108,6 +110,7 @@ The difference between these two files is very little.
Though you can modify the source file by this diff output, It's good for you to show `tfv2.c`. Though you can modify the source file by this diff output, It's good for you to show `tfv2.c`.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -117,47 +120,48 @@ Though you can modify the source file by this diff output, It's good for you to
7 GtkWidget *tv; 7 GtkWidget *tv;
8 GtkTextBuffer *tb; 8 GtkTextBuffer *tb;
9 gchar *text; 9 gchar *text;
10 10
11 text = 11 text =
12 "Once upon a time, there was an old man who was called Taketori-no-Okina." 12 "Once upon a time, there was an old man who was called Taketori-no-Okina."
13 "It is a japanese word that means a man whose work is making bamboo baskets.\n" 13 "It is a japanese word that means a man whose work is making bamboo baskets.\n"
14 "One day, he went into a mountain and found a shining bamboo." 14 "One day, he went into a mountain and found a shining bamboo."
15 "\"What a mysterious bamboo it is!,\" he said." 15 "\"What a mysterious bamboo it is!,\" he said."
16 "He cut it, then there was a small cute baby girl in it." 16 "He cut it, then there was a small cute baby girl in it."
17 "The girl was shining faintly." 17 "The girl was shining faintly."
18 "He thought this baby girl is a gift from Heaven and took her home.\n" 18 "He thought this baby girl is a gift from Heaven and took her home.\n"
19 "His wife was surprized at his tale." 19 "His wife was surprized at his tale."
20 "They were very happy because they had no children." 20 "They were very happy because they had no children."
21 ; 21 ;
22 win = gtk_application_window_new (GTK_APPLICATION (app)); 22 win = gtk_application_window_new (GTK_APPLICATION (app));
23 gtk_window_set_title (GTK_WINDOW (win), "Taketori"); 23 gtk_window_set_title (GTK_WINDOW (win), "Taketori");
24 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 24 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
25 25
26 scr = gtk_scrolled_window_new (); 26 scr = gtk_scrolled_window_new ();
27 gtk_window_set_child (GTK_WINDOW (win), scr); 27 gtk_window_set_child (GTK_WINDOW (win), scr);
28 28
29 tv = gtk_text_view_new (); 29 tv = gtk_text_view_new ();
30 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 30 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
31 gtk_text_buffer_set_text (tb, text, -1); 31 gtk_text_buffer_set_text (tb, text, -1);
32 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 32 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
33 33
34 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 34 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
35 35
36 gtk_widget_show (win); 36 gtk_widget_show (win);
37 } 37 }
38 38
39 int 39 int
40 main (int argc, char **argv) { 40 main (int argc, char **argv) {
41 GtkApplication *app; 41 GtkApplication *app;
42 int stat; 42 int stat;
43 43
44 app = gtk_application_new ("com.github.ToshioCP.tfv2", G_APPLICATION_FLAGS_NONE); 44 app = gtk_application_new ("com.github.ToshioCP.tfv2", G_APPLICATION_FLAGS_NONE);
45 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 45 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
46 stat =g_application_run (G_APPLICATION (app), argc, argv); 46 stat =g_application_run (G_APPLICATION (app), argc, argv);
47 g_object_unref (app); 47 g_object_unref (app);
48 return stat; 48 return stat;
49 } 49 }
50 50
~~~
Now compile and run it. Now compile and run it.
This time the window doesn't extend even if you type a lot of characters. This time the window doesn't extend even if you type a lot of characters.

View file

@ -89,6 +89,7 @@ It works as follows.
The program is as follows. The program is as follows.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -98,53 +99,54 @@ The program is as follows.
7 7
8 static void 8 static void
9 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) { 9 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) {
10 GtkWidget *win; 10 GtkWidget *win;
11 GtkWidget *scr; 11 GtkWidget *scr;
12 GtkWidget *tv; 12 GtkWidget *tv;
13 GtkTextBuffer *tb; 13 GtkTextBuffer *tb;
14 char *contents; 14 char *contents;
15 gsize length; 15 gsize length;
16 char *filename; 16 char *filename;
17 17
18 win = gtk_application_window_new (GTK_APPLICATION (app)); 18 win = gtk_application_window_new (GTK_APPLICATION (app));
19 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 19 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
20 20
21 scr = gtk_scrolled_window_new (); 21 scr = gtk_scrolled_window_new ();
22 gtk_window_set_child (GTK_WINDOW (win), scr); 22 gtk_window_set_child (GTK_WINDOW (win), scr);
23 23
24 tv = gtk_text_view_new (); 24 tv = gtk_text_view_new ();
25 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 25 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
26 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 26 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
27 gtk_text_view_set_editable (GTK_TEXT_VIEW (tv), FALSE); 27 gtk_text_view_set_editable (GTK_TEXT_VIEW (tv), FALSE);
28 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 28 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
29 29
30 if (g_file_load_contents (files[0], NULL, &contents, &length, NULL, NULL)) { 30 if (g_file_load_contents (files[0], NULL, &contents, &length, NULL, NULL)) {
31 gtk_text_buffer_set_text (tb, contents, length); 31 gtk_text_buffer_set_text (tb, contents, length);
32 g_free (contents); 32 g_free (contents);
33 filename = g_file_get_basename (files[0]); 33 filename = g_file_get_basename (files[0]);
34 gtk_window_set_title (GTK_WINDOW (win), filename); 34 gtk_window_set_title (GTK_WINDOW (win), filename);
35 g_free (filename); 35 g_free (filename);
36 gtk_widget_show (win); 36 gtk_widget_show (win);
37 } else { 37 } else {
38 filename = g_file_get_path (files[0]); 38 filename = g_file_get_path (files[0]);
39 g_print ("No such file: %s.\n", filename); 39 g_print ("No such file: %s.\n", filename);
40 gtk_window_destroy (GTK_WINDOW (win)); 40 gtk_window_destroy (GTK_WINDOW (win));
41 } 41 }
42 } 42 }
43 43
44 int 44 int
45 main (int argc, char **argv) { 45 main (int argc, char **argv) {
46 GtkApplication *app; 46 GtkApplication *app;
47 int stat; 47 int stat;
48 48
49 app = gtk_application_new ("com.github.ToshioCP.tfv3", G_APPLICATION_HANDLES_OPEN); 49 app = gtk_application_new ("com.github.ToshioCP.tfv3", G_APPLICATION_HANDLES_OPEN);
50 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 50 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
51 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL); 51 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
52 stat =g_application_run (G_APPLICATION (app), argc, argv); 52 stat =g_application_run (G_APPLICATION (app), argc, argv);
53 g_object_unref (app); 53 g_object_unref (app);
54 return stat; 54 return stat;
55 } 55 }
56 56
~~~
Save it as `tfv3.c`. Save it as `tfv3.c`.
Then compile and run it. Then compile and run it.
@ -211,6 +213,7 @@ It is shown in the right screenshot.
GtkNotebook widget is between GtkApplicationWindow and GtkScrolledWindow. GtkNotebook widget is between GtkApplicationWindow and GtkScrolledWindow.
Now I want to show you the program `tfv4.c`. Now I want to show you the program `tfv4.c`.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 static void 3 static void
@ -220,68 +223,69 @@ Now I want to show you the program `tfv4.c`.
7 7
8 static void 8 static void
9 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) { 9 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) {
10 GtkWidget *win; 10 GtkWidget *win;
11 GtkWidget *nb; 11 GtkWidget *nb;
12 GtkWidget *lab; 12 GtkWidget *lab;
13 GtkNotebookPage *nbp; 13 GtkNotebookPage *nbp;
14 GtkWidget *scr; 14 GtkWidget *scr;
15 GtkWidget *tv; 15 GtkWidget *tv;
16 GtkTextBuffer *tb; 16 GtkTextBuffer *tb;
17 char *contents; 17 char *contents;
18 gsize length; 18 gsize length;
19 char *filename; 19 char *filename;
20 int i; 20 int i;
21 21
22 win = gtk_application_window_new (GTK_APPLICATION (app)); 22 win = gtk_application_window_new (GTK_APPLICATION (app));
23 gtk_window_set_title (GTK_WINDOW (win), "file viewer"); 23 gtk_window_set_title (GTK_WINDOW (win), "file viewer");
24 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); 24 gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);
25 gtk_window_maximize (GTK_WINDOW (win)); 25 gtk_window_maximize (GTK_WINDOW (win));
26 26
27 nb = gtk_notebook_new (); 27 nb = gtk_notebook_new ();
28 gtk_window_set_child (GTK_WINDOW (win), nb); 28 gtk_window_set_child (GTK_WINDOW (win), nb);
29 29
30 for (i = 0; i < n_files; i++) { 30 for (i = 0; i < n_files; i++) {
31 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) { 31 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) {
32 scr = gtk_scrolled_window_new (); 32 scr = gtk_scrolled_window_new ();
33 tv = gtk_text_view_new (); 33 tv = gtk_text_view_new ();
34 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 34 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
35 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 35 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
36 gtk_text_view_set_editable (GTK_TEXT_VIEW (tv), FALSE); 36 gtk_text_view_set_editable (GTK_TEXT_VIEW (tv), FALSE);
37 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 37 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
38 38
39 gtk_text_buffer_set_text (tb, contents, length); 39 gtk_text_buffer_set_text (tb, contents, length);
40 g_free (contents); 40 g_free (contents);
41 filename = g_file_get_basename (files[i]); 41 filename = g_file_get_basename (files[i]);
42 lab = gtk_label_new (filename); 42 lab = gtk_label_new (filename);
43 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab); 43 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab);
44 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr); 44 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr);
45 g_object_set (nbp, "tab-expand", TRUE, NULL); 45 g_object_set (nbp, "tab-expand", TRUE, NULL);
46 g_free (filename); 46 g_free (filename);
47 } else { 47 } else {
48 filename = g_file_get_path (files[i]); 48 filename = g_file_get_path (files[i]);
49 g_print ("No such file: %s.\n", filename); 49 g_print ("No such file: %s.\n", filename);
50 g_free (filename); 50 g_free (filename);
51 } 51 }
52 } 52 }
53 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) 53 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0)
54 gtk_widget_show (win); 54 gtk_widget_show (win);
55 else 55 else
56 gtk_window_destroy (GTK_WINDOW (win)); 56 gtk_window_destroy (GTK_WINDOW (win));
57 } 57 }
58 58
59 int 59 int
60 main (int argc, char **argv) { 60 main (int argc, char **argv) {
61 GtkApplication *app; 61 GtkApplication *app;
62 int stat; 62 int stat;
63 63
64 app = gtk_application_new ("com.github.ToshioCP.tfv4", G_APPLICATION_HANDLES_OPEN); 64 app = gtk_application_new ("com.github.ToshioCP.tfv4", G_APPLICATION_HANDLES_OPEN);
65 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 65 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
66 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL); 66 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
67 stat =g_application_run (G_APPLICATION (app), argc, argv); 67 stat =g_application_run (G_APPLICATION (app), argc, argv);
68 g_object_unref (app); 68 g_object_unref (app);
69 return stat; 69 return stat;
70 } 70 }
71 71
~~~
Most of the change is in the function `on_open`. Most of the change is in the function `on_open`.
The numbers at the left of the following items are line numbers in the source code. The numbers at the left of the following items are line numbers in the source code.

View file

@ -57,39 +57,41 @@ So, I will just show you the way how to write the code and avoid the theoretical
Let's define TfeTextView object which is a child object of GtkTextView. Let's define TfeTextView object which is a child object of GtkTextView.
First, look at the program below. First, look at the program below.
#define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type () ~~~C
G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView) #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type ()
G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView)
struct _TfeTextView struct _TfeTextView
{ {
GtkTextView parent; GtkTextView parent;
GFile *file; GFile *file;
}; };
G_DEFINE_TYPE (TfeTextView, tfe_text_view, GTK_TYPE_TEXT_VIEW); G_DEFINE_TYPE (TfeTextView, tfe_text_view, GTK_TYPE_TEXT_VIEW);
static void static void
tfe_text_view_init (TfeTextView *tv) { tfe_text_view_init (TfeTextView *tv) {
} }
static void static void
tfe_text_view_class_init (TfeTextViewClass *class) { tfe_text_view_class_init (TfeTextViewClass *class) {
} }
void void
tfe_text_view_set_file (TfeTextView *tv, GFile *f) { tfe_text_view_set_file (TfeTextView *tv, GFile *f) {
tv -> file = f; tv -> file = f;
} }
GFile * GFile *
tfe_text_view_get_file (TfeTextView *tv) { tfe_text_view_get_file (TfeTextView *tv) {
return tv -> file; return tv -> file;
} }
GtkWidget * GtkWidget *
tfe_text_view_new (void) { tfe_text_view_new (void) {
return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL)); return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
} }
~~~
If you are curious about the background theory of this program, It's very good for you. If you are curious about the background theory of this program, It's very good for you.
Because to know the theory is very important for you to program GTK applications. Because to know the theory is very important for you to program GTK applications.
@ -157,6 +159,7 @@ The argument win is GtkApplicationWindow, in which the signal "close-request" is
`G_CALLBACK` cast is necessary for the handler. `G_CALLBACK` cast is necessary for the handler.
The program of before\_close is as follows. The program of before\_close is as follows.
~~~C
1 static gboolean 1 static gboolean
2 before_close (GtkWindow *win, GtkWidget *nb) { 2 before_close (GtkWindow *win, GtkWidget *nb) {
3 GtkWidget *scr; 3 GtkWidget *scr;
@ -166,22 +169,23 @@ The program of before\_close is as follows.
7 GtkTextIter start_iter; 7 GtkTextIter start_iter;
8 GtkTextIter end_iter; 8 GtkTextIter end_iter;
9 char *contents; 9 char *contents;
10 unsigned int n; 10 unsigned int n;
11 unsigned int i; 11 unsigned int i;
12 12
13 n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)); 13 n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb));
14 for (i = 0; i < n; ++i) { 14 for (i = 0; i < n; ++i) {
15 scr = gtk_notebook_get_nth_page (GTK_NOTEBOOK (nb), i); 15 scr = gtk_notebook_get_nth_page (GTK_NOTEBOOK (nb), i);
16 tv = gtk_scrolled_window_get_child (GTK_SCROLLED_WINDOW (scr)); 16 tv = gtk_scrolled_window_get_child (GTK_SCROLLED_WINDOW (scr));
17 file = tfe_text_view_get_file (TFE_TEXT_VIEW (tv)); 17 file = tfe_text_view_get_file (TFE_TEXT_VIEW (tv));
18 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 18 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
19 gtk_text_buffer_get_bounds (tb, &start_iter, &end_iter); 19 gtk_text_buffer_get_bounds (tb, &start_iter, &end_iter);
20 contents = gtk_text_buffer_get_text (tb, &start_iter, &end_iter, FALSE); 20 contents = gtk_text_buffer_get_text (tb, &start_iter, &end_iter, FALSE);
21 if (! g_file_replace_contents (file, contents, strlen (contents), NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, NULL)) 21 if (! g_file_replace_contents (file, contents, strlen (contents), NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, NULL))
22 g_print ("ERROR : Can't save %s.", g_file_get_path (file)); 22 g_print ("ERROR : Can't save %s.", g_file_get_path (file));
23 } 23 }
24 return FALSE; 24 return FALSE;
25 } 25 }
~~~
The numbers on the left of items are line numbers in the source code. The numbers on the left of items are line numbers in the source code.
@ -195,6 +199,7 @@ The numbers on the left of items are line numbers in the source code.
Now I will show you all the source code of `tfe1`.c. Now I will show you all the source code of `tfe1`.c.
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 /* Define TfeTextView Widget which is the child object of GtkTextView */ 3 /* Define TfeTextView Widget which is the child object of GtkTextView */
@ -294,43 +299,44 @@ Now I will show you all the source code of `tfe1`.c.
97 tv = tfe_text_view_new (); 97 tv = tfe_text_view_new ();
98 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 98 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
99 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 99 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
100 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 100 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
101 101
102 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i])); 102 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i]));
103 gtk_text_buffer_set_text (tb, contents, length); 103 gtk_text_buffer_set_text (tb, contents, length);
104 g_free (contents); 104 g_free (contents);
105 filename = g_file_get_basename (files[i]); 105 filename = g_file_get_basename (files[i]);
106 lab = gtk_label_new (filename); 106 lab = gtk_label_new (filename);
107 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab); 107 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab);
108 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr); 108 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr);
109 g_object_set (nbp, "tab-expand", TRUE, NULL); 109 g_object_set (nbp, "tab-expand", TRUE, NULL);
110 g_free (filename); 110 g_free (filename);
111 } else { 111 } else {
112 filename = g_file_get_path (files[i]); 112 filename = g_file_get_path (files[i]);
113 g_print ("No such file: %s.\n", filename); 113 g_print ("No such file: %s.\n", filename);
114 g_free (filename); 114 g_free (filename);
115 } 115 }
116 } 116 }
117 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) { 117 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) {
118 g_signal_connect (win, "close-request", G_CALLBACK (before_close), nb); 118 g_signal_connect (win, "close-request", G_CALLBACK (before_close), nb);
119 gtk_widget_show (win); 119 gtk_widget_show (win);
120 } else 120 } else
121 gtk_window_destroy (GTK_WINDOW (win)); 121 gtk_window_destroy (GTK_WINDOW (win));
122 } 122 }
123 123
124 int 124 int
125 main (int argc, char **argv) { 125 main (int argc, char **argv) {
126 GtkApplication *app; 126 GtkApplication *app;
127 int stat; 127 int stat;
128 128
129 app = gtk_application_new ("com.github.ToshioCP.tfe1", G_APPLICATION_HANDLES_OPEN); 129 app = gtk_application_new ("com.github.ToshioCP.tfe1", G_APPLICATION_HANDLES_OPEN);
130 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 130 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
131 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL); 131 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
132 stat =g_application_run (G_APPLICATION (app), argc, argv); 132 stat =g_application_run (G_APPLICATION (app), argc, argv);
133 g_object_unref (app); 133 g_object_unref (app);
134 return stat; 134 return stat;
135 } 135 }
136 136
~~~
- 102: set the pointer to GFile into TfeTextView. - 102: set the pointer to GFile into TfeTextView.
`files[i]` is a pointer to GFile structure. `files[i]` is a pointer to GFile structure.

View file

@ -1,6 +1,6 @@
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)
# Ui file and GtkBuiler # Ui file and GtkBuilder
## New, open and save button ## New, open and save button
@ -16,6 +16,7 @@ Signals and handlers will be explained later.
The screenshot above shows the layout. The screenshot above shows the layout.
The function `on_open` in the source code `tfe2.c` is as follows. The function `on_open` in the source code `tfe2.c` is as follows.
~~~C
1 static void 1 static void
2 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) { 2 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) {
3 GtkWidget *win; 3 GtkWidget *win;
@ -25,83 +26,84 @@ The function `on_open` in the source code `tfe2.c` is as follows.
7 GtkWidget *scr; 7 GtkWidget *scr;
8 GtkWidget *tv; 8 GtkWidget *tv;
9 GtkTextBuffer *tb; 9 GtkTextBuffer *tb;
10 char *contents; 10 char *contents;
11 gsize length; 11 gsize length;
12 char *filename; 12 char *filename;
13 int i; 13 int i;
14 14
15 GtkWidget *boxv; 15 GtkWidget *boxv;
16 GtkWidget *boxh; 16 GtkWidget *boxh;
17 GtkWidget *dmy1; 17 GtkWidget *dmy1;
18 GtkWidget *dmy2; 18 GtkWidget *dmy2;
19 GtkWidget *dmy3; 19 GtkWidget *dmy3;
20 GtkWidget *btnn; /* button for new */ 20 GtkWidget *btnn; /* button for new */
21 GtkWidget *btno; /* button for open */ 21 GtkWidget *btno; /* button for open */
22 GtkWidget *btns; /* button for save */ 22 GtkWidget *btns; /* button for save */
23 GtkWidget *btnc; /* button for close */ 23 GtkWidget *btnc; /* button for close */
24 24
25 win = gtk_application_window_new (GTK_APPLICATION (app)); 25 win = gtk_application_window_new (GTK_APPLICATION (app));
26 gtk_window_set_title (GTK_WINDOW (win), "file editor"); 26 gtk_window_set_title (GTK_WINDOW (win), "file editor");
27 gtk_window_set_default_size (GTK_WINDOW (win), 600, 400); 27 gtk_window_set_default_size (GTK_WINDOW (win), 600, 400);
28 28
29 boxv = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); 29 boxv = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
30 gtk_window_set_child (GTK_WINDOW (win), boxv); 30 gtk_window_set_child (GTK_WINDOW (win), boxv);
31 31
32 boxh = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); 32 boxh = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
33 gtk_box_append (GTK_BOX (boxv), boxh); 33 gtk_box_append (GTK_BOX (boxv), boxh);
34 34
35 dmy1 = gtk_label_new(NULL); /* dummy label for left space */ 35 dmy1 = gtk_label_new(NULL); /* dummy label for left space */
36 gtk_label_set_width_chars (GTK_LABEL (dmy1), 10); 36 gtk_label_set_width_chars (GTK_LABEL (dmy1), 10);
37 dmy2 = gtk_label_new(NULL); /* dummy label for center space */ 37 dmy2 = gtk_label_new(NULL); /* dummy label for center space */
38 gtk_widget_set_hexpand (dmy2, TRUE); 38 gtk_widget_set_hexpand (dmy2, TRUE);
39 dmy3 = gtk_label_new(NULL); /* dummy label for right space */ 39 dmy3 = gtk_label_new(NULL); /* dummy label for right space */
40 gtk_label_set_width_chars (GTK_LABEL (dmy3), 10); 40 gtk_label_set_width_chars (GTK_LABEL (dmy3), 10);
41 btnn = gtk_button_new_with_label ("New"); 41 btnn = gtk_button_new_with_label ("New");
42 btno = gtk_button_new_with_label ("Open"); 42 btno = gtk_button_new_with_label ("Open");
43 btns = gtk_button_new_with_label ("Save"); 43 btns = gtk_button_new_with_label ("Save");
44 btnc = gtk_button_new_with_label ("Close"); 44 btnc = gtk_button_new_with_label ("Close");
45 45
46 gtk_box_append (GTK_BOX (boxh), dmy1); 46 gtk_box_append (GTK_BOX (boxh), dmy1);
47 gtk_box_append (GTK_BOX (boxh), btnn); 47 gtk_box_append (GTK_BOX (boxh), btnn);
48 gtk_box_append (GTK_BOX (boxh), btno); 48 gtk_box_append (GTK_BOX (boxh), btno);
49 gtk_box_append (GTK_BOX (boxh), dmy2); 49 gtk_box_append (GTK_BOX (boxh), dmy2);
50 gtk_box_append (GTK_BOX (boxh), btns); 50 gtk_box_append (GTK_BOX (boxh), btns);
51 gtk_box_append (GTK_BOX (boxh), btnc); 51 gtk_box_append (GTK_BOX (boxh), btnc);
52 gtk_box_append (GTK_BOX (boxh), dmy3); 52 gtk_box_append (GTK_BOX (boxh), dmy3);
53 53
54 nb = gtk_notebook_new (); 54 nb = gtk_notebook_new ();
55 gtk_widget_set_hexpand (nb, TRUE); 55 gtk_widget_set_hexpand (nb, TRUE);
56 gtk_widget_set_vexpand (nb, TRUE); 56 gtk_widget_set_vexpand (nb, TRUE);
57 gtk_box_append (GTK_BOX (boxv), nb); 57 gtk_box_append (GTK_BOX (boxv), nb);
58 58
59 for (i = 0; i < n_files; i++) { 59 for (i = 0; i < n_files; i++) {
60 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) { 60 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) {
61 scr = gtk_scrolled_window_new (); 61 scr = gtk_scrolled_window_new ();
62 tv = tfe_text_view_new (); 62 tv = tfe_text_view_new ();
63 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 63 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
64 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 64 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
65 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 65 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
66 66
67 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i])); 67 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i]));
68 gtk_text_buffer_set_text (tb, contents, length); 68 gtk_text_buffer_set_text (tb, contents, length);
69 g_free (contents); 69 g_free (contents);
70 filename = g_file_get_basename (files[i]); 70 filename = g_file_get_basename (files[i]);
71 lab = gtk_label_new (filename); 71 lab = gtk_label_new (filename);
72 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab); 72 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab);
73 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr); 73 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr);
74 g_object_set (nbp, "tab-expand", TRUE, NULL); 74 g_object_set (nbp, "tab-expand", TRUE, NULL);
75 g_free (filename); 75 g_free (filename);
76 } else { 76 } else {
77 filename = g_file_get_path (files[i]); 77 filename = g_file_get_path (files[i]);
78 g_print ("No such file: %s.\n", filename); 78 g_print ("No such file: %s.\n", filename);
79 g_free (filename); 79 g_free (filename);
80 } 80 }
81 } 81 }
82 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) { 82 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) {
83 gtk_widget_show (win); 83 gtk_widget_show (win);
84 } else 84 } else
85 gtk_window_destroy (GTK_WINDOW (win)); 85 gtk_window_destroy (GTK_WINDOW (win));
86 } 86 }
~~~
The point is how to build the window. The point is how to build the window.
@ -135,6 +137,7 @@ It reduces the cumbersom work.
First, let's look at the ui file `tfe3.ui` that defines a structure of the widgets. First, let's look at the ui file `tfe3.ui` that defines a structure of the widgets.
~~~xml
1 <interface> 1 <interface>
2 <object class="GtkApplicationWindow" id="win"> 2 <object class="GtkApplicationWindow" id="win">
3 <property name="title">file editor</property> 3 <property name="title">file editor</property>
@ -144,56 +147,57 @@ First, let's look at the ui file `tfe3.ui` that defines a structure of the widge
7 <object class="GtkBox" id="boxv"> 7 <object class="GtkBox" id="boxv">
8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property> 8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property>
9 <child> 9 <child>
10 <object class="GtkBox" id="boxh"> 10 <object class="GtkBox" id="boxh">
11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property> 11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
12 <child> 12 <child>
13 <object class="GtkLabel" id="dmy1"> 13 <object class="GtkLabel" id="dmy1">
14 <property name="width-chars">10</property> 14 <property name="width-chars">10</property>
15 </object> 15 </object>
16 </child> 16 </child>
17 <child> 17 <child>
18 <object class="GtkButton" id="btnn"> 18 <object class="GtkButton" id="btnn">
19 <property name="label">New</property> 19 <property name="label">New</property>
20 </object> 20 </object>
21 </child> 21 </child>
22 <child> 22 <child>
23 <object class="GtkButton" id="btno"> 23 <object class="GtkButton" id="btno">
24 <property name="label">Open</property> 24 <property name="label">Open</property>
25 </object> 25 </object>
26 </child> 26 </child>
27 <child> 27 <child>
28 <object class="GtkLabel" id="dmy2"> 28 <object class="GtkLabel" id="dmy2">
29 <property name="hexpand">TRUE</property> 29 <property name="hexpand">TRUE</property>
30 </object> 30 </object>
31 </child> 31 </child>
32 <child> 32 <child>
33 <object class="GtkButton" id="btns"> 33 <object class="GtkButton" id="btns">
34 <property name="label">Save</property> 34 <property name="label">Save</property>
35 </object> 35 </object>
36 </child> 36 </child>
37 <child> 37 <child>
38 <object class="GtkButton" id="btnc"> 38 <object class="GtkButton" id="btnc">
39 <property name="label">Close</property> 39 <property name="label">Close</property>
40 </object> 40 </object>
41 </child> 41 </child>
42 <child> 42 <child>
43 <object class="GtkLabel" id="dmy3"> 43 <object class="GtkLabel" id="dmy3">
44 <property name="width-chars">10</property> 44 <property name="width-chars">10</property>
45 </object> 45 </object>
46 </child> 46 </child>
47 </object> 47 </object>
48 </child> 48 </child>
49 <child> 49 <child>
50 <object class="GtkNotebook" id="nb"> 50 <object class="GtkNotebook" id="nb">
51 <property name="hexpand">TRUE</property> 51 <property name="hexpand">TRUE</property>
52 <property name="vexpand">TRUE</property> 52 <property name="vexpand">TRUE</property>
53 </object> 53 </object>
54 </child> 54 </child>
55 </object> 55 </object>
56 </child> 56 </child>
57 </object> 57 </object>
58 </interface> 58 </interface>
59 59
~~~
This is coded with XML structure. This is coded with XML structure.
Constructs begin with `<` and end with `>` is called tags. Constructs begin with `<` and end with `>` is called tags.
@ -216,12 +220,14 @@ Those two decribe the same structure of widgets.
GtkBuilder builds widgets based on the ui file. GtkBuilder builds widgets based on the ui file.
GtkBuilder *build; ~~~C
GtkBuilder *build;
build = gtk_builder_new_from_file ("tfe3.ui"); build = gtk_builder_new_from_file ("tfe3.ui");
win = GTK_WIDGET (gtk_builder_get_object (build, "win")); win = GTK_WIDGET (gtk_builder_get_object (build, "win"));
gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app)); gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app));
nb = GTK_WIDGET (gtk_builder_get_object (build, "nb")); nb = GTK_WIDGET (gtk_builder_get_object (build, "nb"));
~~~
The function `gtk_builder_new_from_file` reads the file given as an argument, build the widgets, generate GtkBuilder object and set pointers to the widgets in it. The function `gtk_builder_new_from_file` reads the file given as an argument, build the widgets, generate GtkBuilder object and set pointers to the widgets in it.
The function `gtk_builder_get_object (build, "win")` returns the pointer to the widget `win`, which is the id in the ui file. The function `gtk_builder_get_object (build, "win")` returns the pointer to the widget `win`, which is the id in the ui file.
@ -295,6 +301,7 @@ Using ui file not only shortens C source files, but also makes the widgets' stru
Now I'll show you the C source code `tfe3.c`. Now I'll show you the C source code `tfe3.c`.
Only functions `on_open` are shown as follows. Only functions `on_open` are shown as follows.
~~~C
1 static void 1 static void
2 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) { 2 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) {
3 GtkWidget *win; 3 GtkWidget *win;
@ -304,45 +311,46 @@ Only functions `on_open` are shown as follows.
7 GtkWidget *scr; 7 GtkWidget *scr;
8 GtkWidget *tv; 8 GtkWidget *tv;
9 GtkTextBuffer *tb; 9 GtkTextBuffer *tb;
10 char *contents; 10 char *contents;
11 gsize length; 11 gsize length;
12 char *filename; 12 char *filename;
13 int i; 13 int i;
14 GtkBuilder *build; 14 GtkBuilder *build;
15 15
16 build = gtk_builder_new_from_file ("tfe3.ui"); 16 build = gtk_builder_new_from_file ("tfe3.ui");
17 win = GTK_WIDGET (gtk_builder_get_object (build, "win")); 17 win = GTK_WIDGET (gtk_builder_get_object (build, "win"));
18 gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app)); 18 gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app));
19 nb = GTK_WIDGET (gtk_builder_get_object (build, "nb")); 19 nb = GTK_WIDGET (gtk_builder_get_object (build, "nb"));
20 g_object_unref(build); 20 g_object_unref(build);
21 for (i = 0; i < n_files; i++) { 21 for (i = 0; i < n_files; i++) {
22 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) { 22 if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) {
23 scr = gtk_scrolled_window_new (); 23 scr = gtk_scrolled_window_new ();
24 tv = tfe_text_view_new (); 24 tv = tfe_text_view_new ();
25 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); 25 tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv));
26 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); 26 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR);
27 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 27 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
28 28
29 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i])); 29 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i]));
30 gtk_text_buffer_set_text (tb, contents, length); 30 gtk_text_buffer_set_text (tb, contents, length);
31 g_free (contents); 31 g_free (contents);
32 filename = g_file_get_basename (files[i]); 32 filename = g_file_get_basename (files[i]);
33 lab = gtk_label_new (filename); 33 lab = gtk_label_new (filename);
34 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab); 34 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab);
35 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr); 35 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr);
36 g_object_set (nbp, "tab-expand", TRUE, NULL); 36 g_object_set (nbp, "tab-expand", TRUE, NULL);
37 g_free (filename); 37 g_free (filename);
38 } else { 38 } else {
39 filename = g_file_get_path (files[i]); 39 filename = g_file_get_path (files[i]);
40 g_print ("No such file: %s.\n", filename); 40 g_print ("No such file: %s.\n", filename);
41 g_free (filename); 41 g_free (filename);
42 } 42 }
43 } 43 }
44 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) { 44 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) {
45 gtk_widget_show (win); 45 gtk_widget_show (win);
46 } else 46 } else
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](https://github.com/ToshioCP/Gtk4-tutorial/tree/main/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.
@ -353,10 +361,11 @@ In the same way, you can get the source files below in the directory [src/tfe](h
GtkBuilder can build widgets using string. GtkBuilder can build widgets using string.
Use the function gtk\_builder\_new\_from\_string instead of gtk\_builder\_new\_from\_file. Use the function gtk\_builder\_new\_from\_string instead of gtk\_builder\_new\_from\_file.
char *uistring; ~~~C
char *uistring;
uistring = uistring =
"<interface>" "<interface>"
"<object class="GtkApplicationWindow" id="win">" "<object class="GtkApplicationWindow" id="win">"
"<property name=\"title\">file editor</property>" "<property name=\"title\">file editor</property>"
"<property name=\"default-width\">600</property>" "<property name=\"default-width\">600</property>"
@ -364,11 +373,12 @@ Use the function gtk\_builder\_new\_from\_string instead of gtk\_builder\_new\_f
"<child>" "<child>"
"<object class=\"GtkBox\" id=\"boxv\">" "<object class=\"GtkBox\" id=\"boxv\">"
"<property name="orientation">GTK_ORIENTATION_VERTICAL</property>" "<property name="orientation">GTK_ORIENTATION_VERTICAL</property>"
... ... ... ... ... ...
... ... ... ... ... ...
"</interface>"; "</interface>";
build = gtk_builder_new_from_stringfile (uistring); build = gtk_builder_new_from_stringfile (uistring);
~~~
This method has an advantage and disadvantage. This method has an advantage and disadvantage.
The advantage is that the ui string is written in the source code. The advantage is that the ui string is written in the source code.
@ -390,12 +400,14 @@ And after compilation, it bundles them up into one Gresource object.
An xml file is necessary for the resource compiler `glib-compile-resources`. An xml file is necessary for the resource compiler `glib-compile-resources`.
It describes resource files. It describes resource files.
1 <?xml version="1.0" encoding="UTF-8"?> ~~~xml
2 <gresources> 1 <?xml version="1.0" encoding="UTF-8"?>
3 <gresource prefix="/com/github/ToshioCP/tfe3"> 2 <gresources>
4 <file>tfe3.ui</file> 3 <gresource prefix="/com/github/ToshioCP/tfe3">
5 </gresource> 4 <file>tfe3.ui</file>
6 </gresources> 5 </gresource>
6 </gresources>
~~~
- 2: gresources tag can include mulitple gresources (gresource tags). - 2: gresources tag can include mulitple gresources (gresource tags).
However, this xml has only one gresource. However, this xml has only one gresource.
@ -441,12 +453,14 @@ Now run the compiler.
Then a C source file `resources.c` is generated. Then a C source file `resources.c` is generated.
Modify tfe3.c and save it as tfe3_r.c Modify tfe3.c and save it as tfe3_r.c
# include "resources.c" ~~~C
... ... ... #include "resources.c"
... ... ... ... ... ...
build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe3.ui"); ... ... ...
... ... ... build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe3.ui");
... ... ... ... ... ...
... ... ...
~~~
Then, compile and run it. Then, compile and run it.
The window appears and it is the same as the screenshot at the beginning of this page. The window appears and it is the same as the screenshot at the beginning of this page.

View file

@ -42,6 +42,7 @@ All the source files are listed below.
`tfetextview.h` `tfetextview.h`
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 2
3 #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type () 3 #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type ()
@ -51,14 +52,16 @@ All the source files are listed below.
7 tfe_text_view_set_file (TfeTextView *tv, GFile *f); 7 tfe_text_view_set_file (TfeTextView *tv, GFile *f);
8 8
9 GFile * 9 GFile *
10 tfe_text_view_get_file (TfeTextView *tv); 10 tfe_text_view_get_file (TfeTextView *tv);
11 11
12 GtkWidget * 12 GtkWidget *
13 tfe_text_view_new (void); 13 tfe_text_view_new (void);
14 14
~~~
`tfetextview.c` `tfetextview.c`
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 #include "tfetextview.h" 2 #include "tfetextview.h"
3 3
@ -68,34 +71,36 @@ All the source files are listed below.
7 GFile *file; 7 GFile *file;
8 }; 8 };
9 9
10 G_DEFINE_TYPE (TfeTextView, tfe_text_view, GTK_TYPE_TEXT_VIEW); 10 G_DEFINE_TYPE (TfeTextView, tfe_text_view, GTK_TYPE_TEXT_VIEW);
11 11
12 static void 12 static void
13 tfe_text_view_init (TfeTextView *tv) { 13 tfe_text_view_init (TfeTextView *tv) {
14 } 14 }
15 15
16 static void 16 static void
17 tfe_text_view_class_init (TfeTextViewClass *class) { 17 tfe_text_view_class_init (TfeTextViewClass *class) {
18 } 18 }
19 19
20 void 20 void
21 tfe_text_view_set_file (TfeTextView *tv, GFile *f) { 21 tfe_text_view_set_file (TfeTextView *tv, GFile *f) {
22 tv -> file = f; 22 tv -> file = f;
23 } 23 }
24 24
25 GFile * 25 GFile *
26 tfe_text_view_get_file (TfeTextView *tv) { 26 tfe_text_view_get_file (TfeTextView *tv) {
27 return tv -> file; 27 return tv -> file;
28 } 28 }
29 29
30 GtkWidget * 30 GtkWidget *
31 tfe_text_view_new (void) { 31 tfe_text_view_new (void) {
32 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL)); 32 return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
33 } 33 }
34 34
~~~
`tfe.c` `tfe.c`
~~~C
1 #include <gtk/gtk.h> 1 #include <gtk/gtk.h>
2 #include "tfetextview.h" 2 #include "tfetextview.h"
3 3
@ -105,70 +110,72 @@ All the source files are listed below.
7 } 7 }
8 8
9 static void 9 static void
10 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) { 10 on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) {
11 GtkWidget *win; 11 GtkWidget *win;
12 GtkWidget *nb; 12 GtkWidget *nb;
13 GtkWidget *lab; 13 GtkWidget *lab;
14 GtkNotebookPage *nbp; 14 GtkNotebookPage *nbp;
15 GtkWidget *scr; 15 GtkWidget *scr;
16 GtkWidget *tv; 16 GtkWidget *tv;
17 GtkTextBuffer *tb; 17 GtkTextBuffer *tb;
18 char *contents; 18 char *contents;
19 gsize length; 19 gsize length;
20 char *filename; 20 char *filename;
21 int i; 21 int i;
22 GtkBuilder *build; 22 GtkBuilder *build;
23 23
24 build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe.ui"); 24 build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe.ui");
25 win = GTK_WIDGET (gtk_builder_get_object (build, "win")); 25 win = GTK_WIDGET (gtk_builder_get_object (build, "win"));
26 gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app)); 26 gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app));
27 nb = GTK_WIDGET (gtk_builder_get_object (build, "nb")); 27 nb = GTK_WIDGET (gtk_builder_get_object (build, "nb"));
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 (); 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);
35 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); 35 gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv);
36 36
37 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i])); 37 tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i]));
38 gtk_text_buffer_set_text (tb, contents, length); 38 gtk_text_buffer_set_text (tb, contents, length);
39 g_free (contents); 39 g_free (contents);
40 filename = g_file_get_basename (files[i]); 40 filename = g_file_get_basename (files[i]);
41 lab = gtk_label_new (filename); 41 lab = gtk_label_new (filename);
42 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab); 42 gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab);
43 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr); 43 nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr);
44 g_object_set (nbp, "tab-expand", TRUE, NULL); 44 g_object_set (nbp, "tab-expand", TRUE, NULL);
45 g_free (filename); 45 g_free (filename);
46 } else { 46 } else {
47 filename = g_file_get_path (files[i]); 47 filename = g_file_get_path (files[i]);
48 g_print ("No such file: %s.\n", filename); 48 g_print ("No such file: %s.\n", filename);
49 g_free (filename); 49 g_free (filename);
50 } 50 }
51 } 51 }
52 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) { 52 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) {
53 gtk_widget_show (win); 53 gtk_widget_show (win);
54 } else 54 } else
55 gtk_window_destroy (GTK_WINDOW (win)); 55 gtk_window_destroy (GTK_WINDOW (win));
56 } 56 }
57 57
58 int 58 int
59 main (int argc, char **argv) { 59 main (int argc, char **argv) {
60 GtkApplication *app; 60 GtkApplication *app;
61 int stat; 61 int stat;
62 62
63 app = gtk_application_new ("com.github.ToshioCP.tfe3", G_APPLICATION_HANDLES_OPEN); 63 app = gtk_application_new ("com.github.ToshioCP.tfe3", G_APPLICATION_HANDLES_OPEN);
64 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); 64 g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
65 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL); 65 g_signal_connect (app, "open", G_CALLBACK (on_open), NULL);
66 stat =g_application_run (G_APPLICATION (app), argc, argv); 66 stat =g_application_run (G_APPLICATION (app), argc, argv);
67 g_object_unref (app); 67 g_object_unref (app);
68 return stat; 68 return stat;
69 } 69 }
70 70
~~~
`tfe.ui` `tfe.ui`
~~~xml
1 <interface> 1 <interface>
2 <object class="GtkApplicationWindow" id="win"> 2 <object class="GtkApplicationWindow" id="win">
3 <property name="title">file editor</property> 3 <property name="title">file editor</property>
@ -178,65 +185,68 @@ All the source files are listed below.
7 <object class="GtkBox" id="boxv"> 7 <object class="GtkBox" id="boxv">
8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property> 8 <property name="orientation">GTK_ORIENTATION_VERTICAL</property>
9 <child> 9 <child>
10 <object class="GtkBox" id="boxh"> 10 <object class="GtkBox" id="boxh">
11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property> 11 <property name="orientation">GTK_ORIENTATION_HORIZONTAL</property>
12 <child> 12 <child>
13 <object class="GtkLabel" id="dmy1"> 13 <object class="GtkLabel" id="dmy1">
14 <property name="width-chars">10</property> 14 <property name="width-chars">10</property>
15 </object> 15 </object>
16 </child> 16 </child>
17 <child> 17 <child>
18 <object class="GtkButton" id="btnn"> 18 <object class="GtkButton" id="btnn">
19 <property name="label">New</property> 19 <property name="label">New</property>
20 </object> 20 </object>
21 </child> 21 </child>
22 <child> 22 <child>
23 <object class="GtkButton" id="btno"> 23 <object class="GtkButton" id="btno">
24 <property name="label">Open</property> 24 <property name="label">Open</property>
25 </object> 25 </object>
26 </child> 26 </child>
27 <child> 27 <child>
28 <object class="GtkLabel" id="dmy2"> 28 <object class="GtkLabel" id="dmy2">
29 <property name="hexpand">TRUE</property> 29 <property name="hexpand">TRUE</property>
30 </object> 30 </object>
31 </child> 31 </child>
32 <child> 32 <child>
33 <object class="GtkButton" id="btns"> 33 <object class="GtkButton" id="btns">
34 <property name="label">Save</property> 34 <property name="label">Save</property>
35 </object> 35 </object>
36 </child> 36 </child>
37 <child> 37 <child>
38 <object class="GtkButton" id="btnc"> 38 <object class="GtkButton" id="btnc">
39 <property name="label">Close</property> 39 <property name="label">Close</property>
40 </object> 40 </object>
41 </child> 41 </child>
42 <child> 42 <child>
43 <object class="GtkLabel" id="dmy3"> 43 <object class="GtkLabel" id="dmy3">
44 <property name="width-chars">10</property> 44 <property name="width-chars">10</property>
45 </object> 45 </object>
46 </child> 46 </child>
47 </object> 47 </object>
48 </child> 48 </child>
49 <child> 49 <child>
50 <object class="GtkNotebook" id="nb"> 50 <object class="GtkNotebook" id="nb">
51 <property name="hexpand">TRUE</property> 51 <property name="hexpand">TRUE</property>
52 <property name="vexpand">TRUE</property> 52 <property name="vexpand">TRUE</property>
53 </object> 53 </object>
54 </child> 54 </child>
55 </object> 55 </object>
56 </child> 56 </child>
57 </object> 57 </object>
58 </interface> 58 </interface>
59 59
~~~
`tfe.gresource.xml` `tfe.gresource.xml`
1 <?xml version="1.0" encoding="UTF-8"?> ~~~xml
2 <gresources> 1 <?xml version="1.0" encoding="UTF-8"?>
3 <gresource prefix="/com/github/ToshioCP/tfe3"> 2 <gresources>
4 <file>tfe.ui</file> 3 <gresource prefix="/com/github/ToshioCP/tfe3">
5 </gresource> 4 <file>tfe.ui</file>
6 </gresources> 5 </gresource>
6 </gresources>
~~~
## Make ## Make
@ -278,6 +288,7 @@ If the modification time of `sample.c` is older then the generation of `sample.o
The Makefile for `tfe` is as follows. The Makefile for `tfe` is as follows.
~~~makefile
1 all: tfe 1 all: tfe
2 2
3 tfe: tfe.o tfetextview.o resources.o 3 tfe: tfe.o tfetextview.o resources.o
@ -287,16 +298,17 @@ The Makefile for `tfe` is as follows.
7 gcc -c -o tfe.o `pkg-config --cflags gtk4` tfe.c 7 gcc -c -o tfe.o `pkg-config --cflags gtk4` tfe.c
8 tfetextview.o: tfetextview.c tfetextview.h 8 tfetextview.o: tfetextview.c tfetextview.h
9 gcc -c -o tfetextview.o `pkg-config --cflags gtk4` tfetextview.c 9 gcc -c -o tfetextview.o `pkg-config --cflags gtk4` tfetextview.c
10 resources.o: resources.c 10 resources.o: resources.c
11 gcc -c -o resources.o `pkg-config --cflags gtk4` resources.c 11 gcc -c -o resources.o `pkg-config --cflags gtk4` resources.c
12 12
13 resources.c: tfe.gresource.xml tfe.ui 13 resources.c: tfe.gresource.xml tfe.ui
14 glib-compile-resources tfe.gresource.xml --target=resources.c --generate-source 14 glib-compile-resources tfe.gresource.xml --target=resources.c --generate-source
15 15
16 .Phony: clean 16 .Phony: clean
17 17
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
~~~
You only need to type `make`. You only need to type `make`.
@ -324,6 +336,7 @@ However, Ruby is really sophisticated and recommendable script language.
Rake has task and file task, which is similar to target, prerequisite and recipe in make. Rake has task and file task, which is similar to target, prerequisite and recipe in make.
~~~ruby
1 require 'rake/clean' 1 require 'rake/clean'
2 2
3 targetfile = "tfe" 3 targetfile = "tfe"
@ -333,22 +346,23 @@ Rake has task and file task, which is similar to target, prerequisite and recipe
7 7
8 CLEAN.include(targetfile, objfiles, rscfile) 8 CLEAN.include(targetfile, objfiles, rscfile)
9 9
10 task default: targetfile 10 task default: targetfile
11 11
12 file targetfile => objfiles do |t| 12 file targetfile => objfiles do |t|
13 sh "gcc -o #{t.name} #{t.prerequisites.join(' ')} `pkg-config --libs gtk4`" 13 sh "gcc -o #{t.name} #{t.prerequisites.join(' ')} `pkg-config --libs gtk4`"
14 end 14 end
15 15
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
23 file rscfile => ["tfe.gresource.xml", "tfe.ui"] do |t| 23 file rscfile => ["tfe.gresource.xml", "tfe.ui"] do |t|
24 sh "glib-compile-resources #{t.prerequisites[0]} --target=#{t.name} --generate-source" 24 sh "glib-compile-resources #{t.prerequisites[0]} --target=#{t.name} --generate-source"
25 end 25 end
~~~
What `Rakefile` describes is almost same as `Makefile` in the previous subsection. What `Rakefile` describes is almost same as `Makefile` in the previous subsection.
@ -380,6 +394,7 @@ Many developers are using meson and ninja now.
To use meson, you first need to write `meson.build` file. To use meson, you first need to write `meson.build` file.
~~~meson
1 project('tfe', 'c') 1 project('tfe', 'c')
2 2
3 gtkdep = dependency('gtk4') 3 gtkdep = dependency('gtk4')
@ -389,7 +404,8 @@ To use meson, you first need to write `meson.build` file.
7 7
8 sourcefiles=files('tfe.c', 'tfetextview.c') 8 sourcefiles=files('tfe.c', 'tfetextview.c')
9 9
10 executable('tfe', sourcefiles, resources, dependencies: gtkdep) 10 executable('tfe', sourcefiles, resources, dependencies: gtkdep)
~~~
- 1: The function `project` defines things about the project. - 1: The function `project` defines things about the project.
The first parameter is the name of the project and the second is the programing language. The first parameter is the name of the project and the second is the programing language.

View file

@ -45,16 +45,28 @@ require 'pathname'
# ---- Folding verbatim lines ---- # ---- Folding verbatim lines ----
# When C sourcefiles or subshell output are included, the lines are folded to fit in 'width'. # When C sourcefiles or subshell output are included, the lines are folded to fit in 'width'.
# Before they are folded, four space characters are prepended to the line. # Width must be positive integer.
# Therefore, 'width' must be at least five.
# Otherwise the lines are not folded. # Otherwise the lines are not folded.
# This script uses "fenced code blocks" for verbatim lines.
# It is available in GFM and pandoc's markdown but not in original markdown.
# Two characters backtick (`) and tilde (~) are possible for fences.
# This script uses tilde because info string cannot contain any backticks for the backtick code fence.
# Info string follows opening fence and it is usually a language name.
# ~~~C
# int main (int argc, char **argv) {
# ........
# ~~~
# Then the contents are highlighted based on C language syntax.
# This script find the language by the suffix of the file name.
# .c => C, .h => C, .rb => ruby, Rakefile, => ruby, .xml => xml, .ui => xml, .y => bison, .lex => lex, .build => meson, .md => markdown
# Makefile => makefile
def src2md srcmd, md, width def src2md srcmd, md, width
src_buf = IO.readlines srcmd src_buf = IO.readlines srcmd
src_dir = File.dirname srcmd src_dir = File.dirname srcmd
md_dir = File.dirname md md_dir = File.dirname md
# type is 'the type of the target', which is one of "markdown", "html" and "latex". type = File.basename md_dir # gfm, html or latex
type = md_dir == "." ? "markdown" : md_dir
md_buf = [] md_buf = []
comflag = false comflag = false
@ -65,14 +77,16 @@ def src2md srcmd, md, width
else else
md_buf << " $ "+line md_buf << " $ "+line
`cd #{src_dir}; #{line.chomp}`.each_line do |l| `cd #{src_dir}; #{line.chomp}`.each_line do |l|
md_buf << l.gsub(/^/," ") fold(l, width).each_line do |l2|
md_buf << l2.gsub(/^/," ")
end
end end
end end
elsif line == "$$$\n" elsif line == "$$$\n"
comflag = true comflag = true
elsif line =~ /^@@@\s+(\S+)\s*(.*)\s*$/ elsif line =~ /^@@@\s+(\S+)\s*(.*)$/
c_file = $1 c_file = $1
c_functions = $2.split(" ") c_functions = $2.strip.split(" ")
if c_file =~ /^\// # absolute path if c_file =~ /^\// # absolute path
c_file_buf = IO.readlines(c_file) c_file_buf = IO.readlines(c_file)
else #relative path else #relative path
@ -108,38 +122,33 @@ def src2md srcmd, md, width
end end
end end
end end
md_buf << "~~~#{lang(c_file)}\n"
ln_width = tmp_buf.size.to_s.length ln_width = tmp_buf.size.to_s.length
n = 1 n = 1
tmp_buf.each do |l| tmp_buf.each do |l|
l = sprintf(" %#{ln_width}d %s", n, l) l = sprintf("%#{ln_width}d %s", n, l)
md_buf << l fold(l, width).each_line do |l2|
md_buf << l2
end
n += 1 n += 1
end end
md_buf << "~~~\n"
else else
md_buf << change_rel_link(line, src_dir, File.dirname(md)) line = change_rel_link(line, src_dir, md_dir)
end if type == "latex" # remove relative link
end line.gsub!(/(^|[^!])\[([^\]]*)\]\((?~http)\)/,"\\1\\2")
tmp_buf = md_buf else # type == "gfm" or "html", then remove size option from link to image files.
md_buf = [] line.gsub!(/(!\[[^\]]*\]\([^\)]*\)) *{width *= *\d*(|\.\d*)cm *height *= *\d*(|\.\d*)cm}/,"\\1")
tmp_buf.each do |line|
if line =~ /^ / && width.instance_of?(Integer) && width >= 5
indent = line =~ /^( *\d+ +)/ ? " "*$1.length : " "
while line.instance_of?(String) && line.length > width
md_buf << line[0, width]+"\n"
line = line[width .. -1].gsub(/^/,indent)
end
elsif type == "latex"
line.gsub!(/(^|[^!])\[([^\]]*)\]\((?~http)\)/,"\\1\\2") # remove link
else # type == "markdown" or "html"
line.gsub!(/(!\[[^\]]*\]\([^\)]*\)) *{width *= *\d*(|\.\d*)cm *height *= *\d*(|\.\d*)cm}/,"\\1") # remove size option from link to image files.
end end
md_buf << line md_buf << line
end end
end
IO.write(md,md_buf.join) IO.write(md,md_buf.join)
end end
def change_rel_link line, src_dir, basedir # Change the base of relative links from org_dir to new_dir
p_basedir = Pathname.new basedir def change_rel_link line, org_dir, new_dir
p_new_dir = Pathname.new new_dir
left = "" left = ""
right = line right = line
while right =~ /(!?\[[^\]]*\])\(([^\)]*)\)/ while right =~ /(!?\[[^\]]*\])\(([^\)]*)\)/
@ -150,11 +159,42 @@ def change_rel_link line, src_dir, basedir
if name =~ /\[(S|s)ection (\d+)\]/ if name =~ /\[(S|s)ection (\d+)\]/
link = "sec#{$2}.md" link = "sec#{$2}.md"
elsif ! (link =~ /^(http|\/)/) elsif ! (link =~ /^(http|\/)/)
p_link = Pathname.new "#{src_dir}/#{link}" p_link = Pathname.new "#{org_dir}/#{link}"
link = p_link.relative_path_from(p_basedir).to_s link = p_link.relative_path_from(p_new_dir).to_s
end end
left += "#{name}(#{link})" left += "#{name}(#{link})"
end end
left + right left + right
end end
def fold line, width
if width <=0
return line
end
tmp = []
while line.length > width
tmp << line[0, width]+"\n"
line = line[width .. -1]
end
tmp << line
tmp.join
end
def lang file
tbl = {".c" => "C", ".h" => "C", ".rb" => "ruby", ".xml" => "xml", ".ui" => "xml",
".y" => "bison", ".lex" => "lex", ".build" => "meson", ".md" => "markdown" }
name = File.basename file
if name == "Makefile"
return "makefile"
elsif name == "Rakefile"
return "ruby"
else
suffix = File.extname name
tbl.each do |key, val|
if suffix == key
return val
end
end
end
return ""
end

View file

@ -19,7 +19,8 @@ The signal handler will be described after the explanation of this code.
static void static void
on_activate (GApplication *app, gpointer user_data) { on_activate (GApplication *app, gpointer user_data) {
... ... ... ... ... ...
GSimpleAction *act_fullscreen = g_simple_action_new_stateful ("fullscreen", NULL, g_variant_new_boolean (FALSE)); GSimpleAction *act_fullscreen = g_simple_action_new_stateful ("fullscreen",
NULL, g_variant_new_boolean (FALSE));
GMenuItem *menu_item_fullscreen = g_menu_item_new ("Full Screen", "win.fullscreen"); GMenuItem *menu_item_fullscreen = g_menu_item_new ("Full Screen", "win.fullscreen");
g_signal_connect (act_fullscreen, "change-state", G_CALLBACK (fullscreen_changed), win); g_signal_connect (act_fullscreen, "change-state", G_CALLBACK (fullscreen_changed), win);
... ... ... ... ... ...
@ -114,7 +115,8 @@ Those colors are given to the signal handler as a parameter.
static void static void
on_activate (GApplication *app, gpointer user_data) { on_activate (GApplication *app, gpointer user_data) {
... ... ... ... ... ...
GSimpleAction *act_color = g_simple_action_new_stateful ("color", g_variant_type_new("s"), g_variant_new_string ("red")); GSimpleAction *act_color = g_simple_action_new_stateful ("color",
g_variant_type_new("s"), g_variant_new_string ("red"));
GMenuItem *menu_item_red = g_menu_item_new ("Red", "win.color::red"); GMenuItem *menu_item_red = g_menu_item_new ("Red", "win.color::red");
GMenuItem *menu_item_green = g_menu_item_new ("Green", "win.color::green"); GMenuItem *menu_item_green = g_menu_item_new ("Green", "win.color::green");
GMenuItem *menu_item_blue = g_menu_item_new ("Blue", "win.color::blue"); GMenuItem *menu_item_blue = g_menu_item_new ("Blue", "win.color::blue");
@ -148,7 +150,8 @@ The following is the "activate" signal handler.
static void static void
color_activated(GSimpleAction *action, GVariant *parameter, gpointer win) { color_activated(GSimpleAction *action, GVariant *parameter, gpointer win) {
gchar *color = g_strdup_printf ("label#lb {background-color: %s;}", g_variant_get_string (parameter, NULL)); gchar *color = g_strdup_printf ("label#lb {background-color: %s;}",
g_variant_get_string (parameter, NULL));
gtk_css_provider_load_from_data (provider, color, -1); gtk_css_provider_load_from_data (provider, color, -1);
g_free (color); g_free (color);
g_action_change_state (G_ACTION (action), parameter); g_action_change_state (G_ACTION (action), parameter);

View file

@ -11,22 +11,26 @@ The same goes for menus.
The ui file for menus has interface, menu tags. The ui file for menus has interface, menu tags.
The file starts and ends with interface tag. The file starts and ends with interface tag.
<interface> ~~~xml
<interface>
<menu id="menubar"> <menu id="menubar">
</menu> </menu>
</interface> </interface>
~~~
`menu` tag corresponds to GMenu object. `menu` tag corresponds to GMenu object.
`id` attribute defines the name of the object. `id` attribute defines the name of the object.
It will be refered by GtkBuilder. It will be refered by GtkBuilder.
<submenu> ~~~xml
<submenu>
<attribute name="label">File</attribute> <attribute name="label">File</attribute>
<item> <item>
<attribute name="label">New</attribute> <attribute name="label">New</attribute>
<attribute name="action">win.new</attribute> <attribute name="action">win.new</attribute>
</item> </item>
</submenu> </submenu>
~~~
`item` tag corresponds to item in GMenu which has the same structure as GMenuItem. `item` tag corresponds to item in GMenu which has the same structure as GMenuItem.
The item above has a label attribute. The item above has a label attribute.
@ -38,7 +42,8 @@ The GMenuItem has a link to GMenu.
The ui file above can be described as follows. The ui file above can be described as follows.
<item> ~~~xml
<item>
<attribute name="label">File</attribute> <attribute name="label">File</attribute>
<link name="submenu"> <link name="submenu">
<item> <item>
@ -46,7 +51,8 @@ The ui file above can be described as follows.
<attribute name="action">win.new</attribute> <attribute name="action">win.new</attribute>
</item> </item>
</link> </link>
</item> </item>
~~~
`link` tag expresses the link to submenu. `link` tag expresses the link to submenu.
And at the same time it also expresses the submenu itself. And at the same time it also expresses the submenu itself.
@ -69,11 +75,13 @@ The ui file is converted to the resource by the resouce compiler `glib-compile-r
GtkBuilder builds menus from the resource. GtkBuilder builds menus from the resource.
GtkBuilder *builder = gtk_builder_new_from_resource ("/com/github/ToshioCP/menu3/menu3.ui"); ~~~C
GMenuModel *menubar = G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")); GtkBuilder *builder = gtk_builder_new_from_resource ("/com/github/ToshioCP/menu3/menu3.ui");
GMenuModel *menubar = G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"));
gtk_application_set_menubar (GTK_APPLICATION (app), menubar); gtk_application_set_menubar (GTK_APPLICATION (app), menubar);
g_object_unref (builder); g_object_unref (builder);
~~~
It is important that `builder` is unreferred after the GMenuModel `menubar` is set to the application. It is important that `builder` is unreferred after the GMenuModel `menubar` is set to the application.
If you do it before setting, bad thing will happen -- your computer might freeze. If you do it before setting, bad thing will happen -- your computer might freeze.
@ -86,31 +94,42 @@ You can implement them easily with GActionEntry structure and `g_action_map_add_
GActionEntry contains action name, signal handlers, parameter and state. GActionEntry contains action name, signal handlers, parameter and state.
typedef struct _GActionEntry GActionEntry; ~~~C
typedef struct _GActionEntry GActionEntry;
struct _GActionEntry struct _GActionEntry
{ {
const gchar *name; /* action name */ /* action name */
void (* activate) (GSimpleAction *action, GVariant *parameter, gpointer user_data); /* activate handler */ const gchar *name;
const gchar *parameter_type; /* the type of the parameter given as a single GVariant type string */ /* activate handler */
const gchar *state; /* initial state given in GVariant text format */ void (* activate) (GSimpleAction *action, GVariant *parameter, gpointer user_data);
void (* change_state) (GSimpleAction *action, GVariant *value, gpointer user_data); /* change-state handler */ /* the type of the parameter given as a single GVariant type string */
const gchar *parameter_type;
/* initial state given in GVariant text format */
const gchar *state;
/* change-state handler */
void (* change_state) (GSimpleAction *action, GVariant *value, gpointer user_data);
/*< private >*/ /*< private >*/
gsize padding[3]; gsize padding[3];
}; };
~~~
For example, the actions in the previous section are: For example, the actions in the previous section are:
{ "fullscreen", NULL, NULL, "false", fullscreen_changed } ~~~C
{ "color", color_activated, "s", "red", NULL } { "fullscreen", NULL, NULL, "false", fullscreen_changed }
{ "quit", quit_activated, NULL, NULL, NULL }, { "color", color_activated, "s", "red", NULL }
{ "quit", quit_activated, NULL, NULL, NULL },
~~~
And `g_action_map_add_action_entries` does all the process instead of the functions you have needed. And `g_action_map_add_action_entries` does all the process instead of the functions you have needed.
const GActionEntry app_entries[] = { ~~~C
const GActionEntry app_entries[] = {
{ "quit", quit_activated, NULL, NULL, NULL } { "quit", quit_activated, NULL, NULL, NULL }
}; };
g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries, G_N_ELEMENTS (app_entries), app); g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries,
G_N_ELEMENTS (app_entries), app);
~~~
The code above does: The code above does:
@ -120,12 +139,14 @@ The code above does:
The same goes for the other actions. The same goes for the other actions.
const GActionEntry win_entries[] = { ~~~C
const GActionEntry win_entries[] = {
{ "fullscreen", NULL, NULL, "false", fullscreen_changed }, { "fullscreen", NULL, NULL, "false", fullscreen_changed },
{ "color", color_activated, "s", "red", NULL } { "color", color_activated, "s", "red", NULL }
}; };
g_action_map_add_action_entries (G_ACTION_MAP (win), win_entries, G_N_ELEMENTS (win_entries), win); g_action_map_add_action_entries (G_ACTION_MAP (win), win_entries,
G_N_ELEMENTS (win_entries), win);
~~~
The code above does: The code above does:
- Build a "fullscreen" action and "color" action. - Build a "fullscreen" action and "color" action.

View file

@ -30,7 +30,7 @@ In this section, we don't use it.
That means we only use identity transformation. That means we only use identity transformation.
Therefore, the coordinate in source and mask is the same as the coordinate in destination. Therefore, the coordinate in source and mask is the same as the coordinate in destination.
![Stroke a rectangle](../image/cairo.png) ![Stroke a rectangle](../image/cairo.png){width=9.0cm height=6.0cm}
The instruction is as follows: The instruction is as follows:

View file

@ -141,7 +141,8 @@ Modify `env.sh`.
# compiler # compiler
CPPFLAGS="-I$HOME/local/include" CPPFLAGS="-I$HOME/local/include"
LDFLAGS="-L$HOME/local/lib" LDFLAGS="-L$HOME/local/lib"
PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig:$HOME/local/lib/x86_64-linux-gnu/pkgconfig:$HOME/local/share/pkgconfig" PKG_CONFIG_PATH="$HOME/local/lib/pkgconfig:$HOME/local/lib/x86_64-linux-gnu/pkgconfig:
$HOME/local/share/pkgconfig"
export CPPFLAGS LDFLAGS PKG_CONFIG_PATH export CPPFLAGS LDFLAGS PKG_CONFIG_PATH
# linker # linker
LD_LIBRARY_PATH="$HOME/local/lib/x86_64-linux-gnu/" LD_LIBRARY_PATH="$HOME/local/lib/x86_64-linux-gnu/"

View file

@ -4,7 +4,7 @@ Now, we will make a new application which has GtkDrawingArea and TfeTextView in
Its name is "color". Its name is "color".
If you write a color in TfeTextView and click on the `run` button, then the color of GtkDrawingArea changes to the color given by you. If you write a color in TfeTextView and click on the `run` button, then the color of GtkDrawingArea changes to the color given by you.
![color](../image/color.png) ![color](../image/color.png){width=7.0cm height=5.13cm}
The following colors are available. The following colors are available.

View file

@ -40,7 +40,9 @@ Let's run it.
$ ./a.out $ ./a.out
(a.out:13533): GLib-GIO-WARNING **: 15:30:17.449: Your application does not implement g_application_activate() and has no handlers connected to the "activate" signal. It should do one of these. (a.out:13533): GLib-GIO-WARNING **: 15:30:17.449: Your application does not implement
g_application_activate() and has no handlers connected to the "activate" signal.
It should do one of these.
$ $
Oh, just an error message. Oh, just an error message.

View file

@ -55,39 +55,41 @@ So, I will just show you the way how to write the code and avoid the theoretical
Let's define TfeTextView object which is a child object of GtkTextView. Let's define TfeTextView object which is a child object of GtkTextView.
First, look at the program below. First, look at the program below.
#define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type () ~~~C
G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView) #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type ()
G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView)
struct _TfeTextView struct _TfeTextView
{ {
GtkTextView parent; GtkTextView parent;
GFile *file; GFile *file;
}; };
G_DEFINE_TYPE (TfeTextView, tfe_text_view, GTK_TYPE_TEXT_VIEW); G_DEFINE_TYPE (TfeTextView, tfe_text_view, GTK_TYPE_TEXT_VIEW);
static void static void
tfe_text_view_init (TfeTextView *tv) { tfe_text_view_init (TfeTextView *tv) {
} }
static void static void
tfe_text_view_class_init (TfeTextViewClass *class) { tfe_text_view_class_init (TfeTextViewClass *class) {
} }
void void
tfe_text_view_set_file (TfeTextView *tv, GFile *f) { tfe_text_view_set_file (TfeTextView *tv, GFile *f) {
tv -> file = f; tv -> file = f;
} }
GFile * GFile *
tfe_text_view_get_file (TfeTextView *tv) { tfe_text_view_get_file (TfeTextView *tv) {
return tv -> file; return tv -> file;
} }
GtkWidget * GtkWidget *
tfe_text_view_new (void) { tfe_text_view_new (void) {
return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL)); return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL));
} }
~~~
If you are curious about the background theory of this program, It's very good for you. If you are curious about the background theory of this program, It's very good for you.
Because to know the theory is very important for you to program GTK applications. Because to know the theory is very important for you to program GTK applications.

View file

@ -1,4 +1,4 @@
# Ui file and GtkBuiler # Ui file and GtkBuilder
## New, open and save button ## New, open and save button
@ -71,12 +71,14 @@ Those two decribe the same structure of widgets.
GtkBuilder builds widgets based on the ui file. GtkBuilder builds widgets based on the ui file.
GtkBuilder *build; ~~~C
GtkBuilder *build;
build = gtk_builder_new_from_file ("tfe3.ui"); build = gtk_builder_new_from_file ("tfe3.ui");
win = GTK_WIDGET (gtk_builder_get_object (build, "win")); win = GTK_WIDGET (gtk_builder_get_object (build, "win"));
gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app)); gtk_window_set_application (GTK_WINDOW (win), GTK_APPLICATION (app));
nb = GTK_WIDGET (gtk_builder_get_object (build, "nb")); nb = GTK_WIDGET (gtk_builder_get_object (build, "nb"));
~~~
The function `gtk_builder_new_from_file` reads the file given as an argument, build the widgets, generate GtkBuilder object and set pointers to the widgets in it. The function `gtk_builder_new_from_file` reads the file given as an argument, build the widgets, generate GtkBuilder object and set pointers to the widgets in it.
The function `gtk_builder_get_object (build, "win")` returns the pointer to the widget `win`, which is the id in the ui file. The function `gtk_builder_get_object (build, "win")` returns the pointer to the widget `win`, which is the id in the ui file.
@ -106,10 +108,11 @@ In the same way, you can get the source files below in the directory [src/tfe](h
GtkBuilder can build widgets using string. GtkBuilder can build widgets using string.
Use the function gtk\_builder\_new\_from\_string instead of gtk\_builder\_new\_from\_file. Use the function gtk\_builder\_new\_from\_string instead of gtk\_builder\_new\_from\_file.
char *uistring; ~~~C
char *uistring;
uistring = uistring =
"<interface>" "<interface>"
"<object class="GtkApplicationWindow" id="win">" "<object class="GtkApplicationWindow" id="win">"
"<property name=\"title\">file editor</property>" "<property name=\"title\">file editor</property>"
"<property name=\"default-width\">600</property>" "<property name=\"default-width\">600</property>"
@ -117,11 +120,12 @@ Use the function gtk\_builder\_new\_from\_string instead of gtk\_builder\_new\_f
"<child>" "<child>"
"<object class=\"GtkBox\" id=\"boxv\">" "<object class=\"GtkBox\" id=\"boxv\">"
"<property name="orientation">GTK_ORIENTATION_VERTICAL</property>" "<property name="orientation">GTK_ORIENTATION_VERTICAL</property>"
... ... ... ... ... ...
... ... ... ... ... ...
"</interface>"; "</interface>";
build = gtk_builder_new_from_stringfile (uistring); build = gtk_builder_new_from_stringfile (uistring);
~~~
This method has an advantage and disadvantage. This method has an advantage and disadvantage.
The advantage is that the ui string is written in the source code. The advantage is that the ui string is written in the source code.
@ -166,12 +170,14 @@ Now run the compiler.
Then a C source file `resources.c` is generated. Then a C source file `resources.c` is generated.
Modify tfe3.c and save it as tfe3_r.c Modify tfe3.c and save it as tfe3_r.c
# include "resources.c" ~~~C
... ... ... #include "resources.c"
... ... ... ... ... ...
build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe3.ui"); ... ... ...
... ... ... build = gtk_builder_new_from_resource ("/com/github/ToshioCP/tfe3/tfe3.ui");
... ... ... ... ... ...
... ... ...
~~~
Then, compile and run it. Then, compile and run it.
The window appears and it is the same as the screenshot at the beginning of this page. The window appears and it is the same as the screenshot at the beginning of this page.