mirror of
https://github.com/ToshioCP/Gtk4-tutorial.git
synced 2025-01-12 20:03:28 +01:00
772 lines
82 KiB
HTML
772 lines
82 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
|||
|
<head>
|
|||
|
<meta charset="utf-8" />
|
|||
|
<meta name="generator" content="pandoc" />
|
|||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
|||
|
<title>Gtk4 tutorial for beginners</title>
|
|||
|
<style>
|
|||
|
code{white-space: pre-wrap;}
|
|||
|
span.smallcaps{font-variant: small-caps;}
|
|||
|
span.underline{text-decoration: underline;}
|
|||
|
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
|||
|
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
|||
|
ul.task-list{list-style: none;}
|
|||
|
pre > code.sourceCode { white-space: pre; position: relative; }
|
|||
|
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
|||
|
pre > code.sourceCode > span:empty { height: 1.2em; }
|
|||
|
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
|||
|
div.sourceCode { margin: 1em 0; }
|
|||
|
pre.sourceCode { margin: 0; }
|
|||
|
@media screen {
|
|||
|
div.sourceCode { overflow: auto; }
|
|||
|
}
|
|||
|
@media print {
|
|||
|
pre > code.sourceCode { white-space: pre-wrap; }
|
|||
|
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
|||
|
}
|
|||
|
pre.numberSource code
|
|||
|
{ counter-reset: source-line 0; }
|
|||
|
pre.numberSource code > span
|
|||
|
{ position: relative; left: -4em; counter-increment: source-line; }
|
|||
|
pre.numberSource code > span > a:first-child::before
|
|||
|
{ content: counter(source-line);
|
|||
|
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
|||
|
border: none; display: inline-block;
|
|||
|
-webkit-touch-callout: none; -webkit-user-select: none;
|
|||
|
-khtml-user-select: none; -moz-user-select: none;
|
|||
|
-ms-user-select: none; user-select: none;
|
|||
|
padding: 0 4px; width: 4em;
|
|||
|
color: #aaaaaa;
|
|||
|
}
|
|||
|
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
|||
|
div.sourceCode
|
|||
|
{ }
|
|||
|
@media screen {
|
|||
|
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
|||
|
}
|
|||
|
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
|||
|
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
|||
|
code span.at { color: #7d9029; } /* Attribute */
|
|||
|
code span.bn { color: #40a070; } /* BaseN */
|
|||
|
code span.bu { } /* BuiltIn */
|
|||
|
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
|||
|
code span.ch { color: #4070a0; } /* Char */
|
|||
|
code span.cn { color: #880000; } /* Constant */
|
|||
|
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
|||
|
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
|||
|
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
|||
|
code span.dt { color: #902000; } /* DataType */
|
|||
|
code span.dv { color: #40a070; } /* DecVal */
|
|||
|
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
|||
|
code span.ex { } /* Extension */
|
|||
|
code span.fl { color: #40a070; } /* Float */
|
|||
|
code span.fu { color: #06287e; } /* Function */
|
|||
|
code span.im { } /* Import */
|
|||
|
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
|||
|
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
|||
|
code span.op { color: #666666; } /* Operator */
|
|||
|
code span.ot { color: #007020; } /* Other */
|
|||
|
code span.pp { color: #bc7a00; } /* Preprocessor */
|
|||
|
code span.sc { color: #4070a0; } /* SpecialChar */
|
|||
|
code span.ss { color: #bb6688; } /* SpecialString */
|
|||
|
code span.st { color: #4070a0; } /* String */
|
|||
|
code span.va { color: #19177c; } /* Variable */
|
|||
|
code span.vs { color: #4070a0; } /* VerbatimString */
|
|||
|
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
|||
|
body {width: 1080px; margin: 0 auto; font-size: large;}
|
|||
|
h2 {padding: 10px; background-color: #d0f0d0; }
|
|||
|
div.sourceCode { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
|
|||
|
pre:not(.sourceCode) { margin: 10px; padding: 16px 10px 8px 10px; border: 2px solid silver; background-color: ghostwhite; overflow-x:scroll}
|
|||
|
table {margin-left: auto; margin-right: auto; border-collapse: collapse; border: 1px solid;}
|
|||
|
th {padding: 2px 6px; border: 1px solid; background-color: ghostwhite;}
|
|||
|
td {padding: 2px 6px; border: 1px solid;}
|
|||
|
img {display: block; margin-left: auto; margin-right: auto;}
|
|||
|
figcaption {text-align: center;}
|
|||
|
</style>
|
|||
|
</head>
|
|||
|
<body>
|
|||
|
<p>Up: <a href="index.html">index.html</a>, Prev: <a href="sec20.html">Section 20</a>, Next: <a href="sec22.html">Section 22</a></p>
|
|||
|
<h1 id="template-xml-and-composite-widget">Template XML and composite widget</h1>
|
|||
|
<p>The tfe program in the previous section is not so good because many things are crammed into <code>tfepplication.c</code>. Many static variables in <code>tfepplication.c</code> shows that.</p>
|
|||
|
<div class="sourceCode" id="cb1"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="dt">static</span> GtkDialog *pref;</span>
|
|||
|
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a><span class="dt">static</span> GtkFontButton *fontbtn;</span>
|
|||
|
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="dt">static</span> GSettings *settings;</span>
|
|||
|
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a><span class="dt">static</span> GtkDialog *alert;</span>
|
|||
|
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a><span class="dt">static</span> GtkLabel *lb_alert;</span>
|
|||
|
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a><span class="dt">static</span> GtkButton *btn_accept;</span>
|
|||
|
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a></span>
|
|||
|
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a><span class="dt">static</span> gulong pref_close_request_handler_id = <span class="dv">0</span>;</span>
|
|||
|
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a><span class="dt">static</span> gulong alert_close_request_handler_id = <span class="dv">0</span>;</span>
|
|||
|
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true"></a><span class="dt">static</span> gboolean is_quit;</span></code></pre></div>
|
|||
|
<p>Generally, if there are many global or static variables in the program, it is not a good program. Such programs are difficult to maintain.</p>
|
|||
|
<p>The file <code>tfeapplication.c</code> should be divided into several files.</p>
|
|||
|
<ul>
|
|||
|
<li><code>tfeapplication.c</code> only has codes related to GtkApplication.</li>
|
|||
|
<li>A file for GtkApplicationWindow</li>
|
|||
|
<li>A file for a preference dialog</li>
|
|||
|
<li>A file for an alert dialog</li>
|
|||
|
</ul>
|
|||
|
<p>The preference dialog is defined by a ui file. And it has GtkBox, GtkLabel and GtkFontButton in it. Such widget is called composite widget. Composite widget is a child object (not child widget) of a widget. For example, the preference composite widget is a child object of GtkDialog. Composite widget can be built from template XML. Next subsection shows how to build a preference dialog.</p>
|
|||
|
<h2 id="preference-dialog">Preference dialog</h2>
|
|||
|
<p>First, write a template XML file.</p>
|
|||
|
<div class="sourceCode" id="cb2"><pre class="sourceCode numberSource xml numberLines"><code class="sourceCode xml"><span id="cb2-1"><a href="#cb2-1"></a><span class="kw"><?xml</span> version="1.0" encoding="UTF-8"<span class="kw">?></span></span>
|
|||
|
<span id="cb2-2"><a href="#cb2-2"></a><span class="kw"><interface></span></span>
|
|||
|
<span id="cb2-3"><a href="#cb2-3"></a> <span class="kw"><template</span><span class="ot"> class=</span><span class="st">"TfePref"</span><span class="ot"> parent=</span><span class="st">"GtkDialog"</span><span class="kw">></span></span>
|
|||
|
<span id="cb2-4"><a href="#cb2-4"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"title"</span><span class="kw">></span>Preferences<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-5"><a href="#cb2-5"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"resizable"</span><span class="kw">></span>FALSE<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-6"><a href="#cb2-6"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"modal"</span><span class="kw">></span>TRUE<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-7"><a href="#cb2-7"></a> <span class="kw"><child</span><span class="ot"> internal-child=</span><span class="st">"content_area"</span><span class="kw">></span></span>
|
|||
|
<span id="cb2-8"><a href="#cb2-8"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkBox"</span><span class="ot"> id=</span><span class="st">"content_area"</span><span class="kw">></span></span>
|
|||
|
<span id="cb2-9"><a href="#cb2-9"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb2-10"><a href="#cb2-10"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkBox"</span><span class="ot"> id=</span><span class="st">"pref_boxh"</span><span class="kw">></span></span>
|
|||
|
<span id="cb2-11"><a href="#cb2-11"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"orientation"</span><span class="kw">></span>GTK_ORIENTATION_HORIZONTAL<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-12"><a href="#cb2-12"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"spacing"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-13"><a href="#cb2-13"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-start"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-14"><a href="#cb2-14"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-end"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-15"><a href="#cb2-15"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-top"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-16"><a href="#cb2-16"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-bottom"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-17"><a href="#cb2-17"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb2-18"><a href="#cb2-18"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkLabel"</span><span class="ot"> id=</span><span class="st">"fontlabel"</span><span class="kw">></span></span>
|
|||
|
<span id="cb2-19"><a href="#cb2-19"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"label"</span><span class="kw">></span>Font:<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-20"><a href="#cb2-20"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"xalign"</span><span class="kw">></span>1<span class="kw"></property></span></span>
|
|||
|
<span id="cb2-21"><a href="#cb2-21"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb2-22"><a href="#cb2-22"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb2-23"><a href="#cb2-23"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb2-24"><a href="#cb2-24"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkFontButton"</span><span class="ot"> id=</span><span class="st">"fontbtn"</span><span class="kw">></span></span>
|
|||
|
<span id="cb2-25"><a href="#cb2-25"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb2-26"><a href="#cb2-26"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb2-27"><a href="#cb2-27"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb2-28"><a href="#cb2-28"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb2-29"><a href="#cb2-29"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb2-30"><a href="#cb2-30"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb2-31"><a href="#cb2-31"></a> <span class="kw"></template></span></span>
|
|||
|
<span id="cb2-32"><a href="#cb2-32"></a><span class="kw"></interface></span></span></code></pre></div>
|
|||
|
<ul>
|
|||
|
<li>3: Template tag specifies a composite widget. The value of a class attribute is the object name of the composite widget. This XML file names the object “TfePref”. It is defined in a C source file and it will be shown later. A parent attribute specifies the direct parent object of the composite widget. <code>TfePref</code> is a child object of <code>GtkDialog</code>. Therefore the value of the attribute is “GtkDialog”. A parent attribute is optional but it is recommended to specify.</li>
|
|||
|
</ul>
|
|||
|
<p>Other lines are the same as before. The object <code>TfePref</code> is defined in <code>tfepref.h</code> and <code>tfepref.c</code>.</p>
|
|||
|
<div class="sourceCode" id="cb3"><pre class="sourceCode numberSource C numberLines"><code class="sourceCode c"><span id="cb3-1"><a href="#cb3-1"></a><span class="pp">#ifndef __TFE_PREF_H__</span></span>
|
|||
|
<span id="cb3-2"><a href="#cb3-2"></a><span class="pp">#define __TFE_PREF_H__</span></span>
|
|||
|
<span id="cb3-3"><a href="#cb3-3"></a></span>
|
|||
|
<span id="cb3-4"><a href="#cb3-4"></a><span class="pp">#include </span><span class="im"><gtk/gtk.h></span></span>
|
|||
|
<span id="cb3-5"><a href="#cb3-5"></a></span>
|
|||
|
<span id="cb3-6"><a href="#cb3-6"></a><span class="pp">#define TFE_TYPE_PREF tfe_pref_get_type ()</span></span>
|
|||
|
<span id="cb3-7"><a href="#cb3-7"></a>G_DECLARE_FINAL_TYPE (TfePref, tfe_pref, TFE, PREF, GtkDialog)</span>
|
|||
|
<span id="cb3-8"><a href="#cb3-8"></a></span>
|
|||
|
<span id="cb3-9"><a href="#cb3-9"></a>GtkWidget *</span>
|
|||
|
<span id="cb3-10"><a href="#cb3-10"></a>tfe_pref_new (GtkWindow *win);</span>
|
|||
|
<span id="cb3-11"><a href="#cb3-11"></a></span>
|
|||
|
<span id="cb3-12"><a href="#cb3-12"></a><span class="pp">#endif </span><span class="co">/* __TFE_PREF_H__ */</span></span></code></pre></div>
|
|||
|
<ul>
|
|||
|
<li>6-7: When you define a new object, you need to write these two lines. Refer to <a href="sec8.html">Section 8</a>.</li>
|
|||
|
<li>9-10: <code>tfe_pref_new</code> creates a new TfePref object. It has a parameter <code>win</code> which is used as a transient parent window to show the dialog.</li>
|
|||
|
</ul>
|
|||
|
<div class="sourceCode" id="cb4"><pre class="sourceCode numberSource C numberLines"><code class="sourceCode c"><span id="cb4-1"><a href="#cb4-1"></a><span class="pp">#include </span><span class="im">"tfepref.h"</span></span>
|
|||
|
<span id="cb4-2"><a href="#cb4-2"></a></span>
|
|||
|
<span id="cb4-3"><a href="#cb4-3"></a><span class="kw">struct</span> _TfePref</span>
|
|||
|
<span id="cb4-4"><a href="#cb4-4"></a>{</span>
|
|||
|
<span id="cb4-5"><a href="#cb4-5"></a> GtkDialog parent;</span>
|
|||
|
<span id="cb4-6"><a href="#cb4-6"></a> GSettings *settings;</span>
|
|||
|
<span id="cb4-7"><a href="#cb4-7"></a> GtkFontButton *fontbtn;</span>
|
|||
|
<span id="cb4-8"><a href="#cb4-8"></a>};</span>
|
|||
|
<span id="cb4-9"><a href="#cb4-9"></a></span>
|
|||
|
<span id="cb4-10"><a href="#cb4-10"></a>G_DEFINE_TYPE (TfePref, tfe_pref, GTK_TYPE_DIALOG);</span>
|
|||
|
<span id="cb4-11"><a href="#cb4-11"></a></span>
|
|||
|
<span id="cb4-12"><a href="#cb4-12"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb4-13"><a href="#cb4-13"></a>tfe_pref_dispose (GObject *gobject) {</span>
|
|||
|
<span id="cb4-14"><a href="#cb4-14"></a> TfePref *pref = TFE_PREF (gobject);</span>
|
|||
|
<span id="cb4-15"><a href="#cb4-15"></a></span>
|
|||
|
<span id="cb4-16"><a href="#cb4-16"></a> g_clear_object (&pref->settings);</span>
|
|||
|
<span id="cb4-17"><a href="#cb4-17"></a> G_OBJECT_CLASS (tfe_pref_parent_class)->dispose (gobject);</span>
|
|||
|
<span id="cb4-18"><a href="#cb4-18"></a>}</span>
|
|||
|
<span id="cb4-19"><a href="#cb4-19"></a></span>
|
|||
|
<span id="cb4-20"><a href="#cb4-20"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb4-21"><a href="#cb4-21"></a>tfe_pref_init (TfePref *pref) {</span>
|
|||
|
<span id="cb4-22"><a href="#cb4-22"></a> gtk_widget_init_template (GTK_WIDGET (pref));</span>
|
|||
|
<span id="cb4-23"><a href="#cb4-23"></a> pref->settings = g_settings_new (<span class="st">"com.github.ToshioCP.tfe"</span>);</span>
|
|||
|
<span id="cb4-24"><a href="#cb4-24"></a> g_settings_bind (pref->settings, <span class="st">"font"</span>, pref->fontbtn, <span class="st">"font"</span>, G_SETTINGS_BIND_DEFAULT);</span>
|
|||
|
<span id="cb4-25"><a href="#cb4-25"></a>}</span>
|
|||
|
<span id="cb4-26"><a href="#cb4-26"></a></span>
|
|||
|
<span id="cb4-27"><a href="#cb4-27"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb4-28"><a href="#cb4-28"></a>tfe_pref_class_init (TfePrefClass *class) {</span>
|
|||
|
<span id="cb4-29"><a href="#cb4-29"></a> GObjectClass *object_class = G_OBJECT_CLASS (class);</span>
|
|||
|
<span id="cb4-30"><a href="#cb4-30"></a></span>
|
|||
|
<span id="cb4-31"><a href="#cb4-31"></a> object_class->dispose = tfe_pref_dispose;</span>
|
|||
|
<span id="cb4-32"><a href="#cb4-32"></a> gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class), <span class="st">"/com/github/ToshioCP/tfe/tfepref.ui"</span>);</span>
|
|||
|
<span id="cb4-33"><a href="#cb4-33"></a> gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), TfePref, fontbtn);</span>
|
|||
|
<span id="cb4-34"><a href="#cb4-34"></a>}</span>
|
|||
|
<span id="cb4-35"><a href="#cb4-35"></a></span>
|
|||
|
<span id="cb4-36"><a href="#cb4-36"></a>GtkWidget *</span>
|
|||
|
<span id="cb4-37"><a href="#cb4-37"></a>tfe_pref_new (GtkWindow *win) {</span>
|
|||
|
<span id="cb4-38"><a href="#cb4-38"></a> <span class="cf">return</span> GTK_WIDGET (g_object_new (TFE_TYPE_PREF, <span class="st">"transient-for"</span>, win, NULL));</span>
|
|||
|
<span id="cb4-39"><a href="#cb4-39"></a>}</span></code></pre></div>
|
|||
|
<ul>
|
|||
|
<li>3-8: The structure of an instance of this object. It has two variables, settings and fontbtn.</li>
|
|||
|
<li>10: <code>G_DEFINE_TYPE</code> macro. This macro registers the TfePref type.</li>
|
|||
|
<li>12-18: Dispose handler. This handler is called when the instance is destroyed. The destruction process has two stages, disposing and finalizing. When disposing, the instance releases all the references (to the other instances). TfePref object holds a reference to the GSettings instance. It is released in line 16. After that parents dispose handler is called in line 17. For further information about destruction process, refer to <a href="sec11.html">Section 11</a>.</li>
|
|||
|
<li>27-34: Class initialization function.</li>
|
|||
|
<li>31: Set the dispose handler.</li>
|
|||
|
<li>32: <code>gtk_widget_class_set_template_from_resource</code> function associates the description in the XML file (<code>tfepref.ui</code>) with the widget. At this moment no instance is created. It just make the class to know the structure of the object. That’s why the top level tag is not <code><object></code> but <code><template></code> in the XML file.</li>
|
|||
|
<li>33: <code>gtk_widget_class_bind_template_child</code> function binds a private variable of the object with a child object in the template. This function is a macro. The name of the private variable (<code>fontbtn</code> in line 7) and the id <code>fontbtn</code> in the XML file (line 24) must be the same. The pointer to the instance will be assigned to the variable <code>fontbtn</code> when the instance is created.</li>
|
|||
|
<li>20-25: Instance initialization function.</li>
|
|||
|
<li>22: Creates the instance based on the template in the class. The template has been made during the class initialization process.</li>
|
|||
|
<li>23: Create GSettings instance with the id “com.github.ToshioCP.tfe”.</li>
|
|||
|
<li>24: Bind the font key in the GSettings object to the font property in the GtkFontButton.</li>
|
|||
|
<li>36-39: The function <code>tfe_pref_new</code> creates an instance of TfePref. The parameter <code>win</code> is a transient parent.</li>
|
|||
|
</ul>
|
|||
|
<p>Now, It is very simple to use this dialog. A caller just creates this object and shows it.</p>
|
|||
|
<div class="sourceCode" id="cb5"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true"></a>TfePref *pref;</span>
|
|||
|
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true"></a>pref = tfe_pref_new (win) <span class="co">/* win is the top-level window */</span></span>
|
|||
|
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true"></a>gtk_widget_show (GTK_WINDOW (win));</span></code></pre></div>
|
|||
|
<p>This instance is automatically destroyed when a user clicks on the close button. That’s all. If you want to show the dialog again, just create and show it.</p>
|
|||
|
<h2 id="alert-dialog">Alert dialog</h2>
|
|||
|
<p>It is almost same as preference dialog.</p>
|
|||
|
<p>Its XML file is:</p>
|
|||
|
<div class="sourceCode" id="cb6"><pre class="sourceCode numberSource xml numberLines"><code class="sourceCode xml"><span id="cb6-1"><a href="#cb6-1"></a><span class="kw"><?xml</span> version="1.0" encoding="UTF-8"<span class="kw">?></span></span>
|
|||
|
<span id="cb6-2"><a href="#cb6-2"></a><span class="kw"><interface></span></span>
|
|||
|
<span id="cb6-3"><a href="#cb6-3"></a> <span class="kw"><template</span><span class="ot"> class=</span><span class="st">"TfeAlert"</span><span class="ot"> parent=</span><span class="st">"GtkDialog"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-4"><a href="#cb6-4"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"title"</span><span class="kw">></span>Are you sure?<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-5"><a href="#cb6-5"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"resizable"</span><span class="kw">></span>FALSE<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-6"><a href="#cb6-6"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"modal"</span><span class="kw">></span>TRUE<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-7"><a href="#cb6-7"></a> <span class="kw"><child</span><span class="ot"> internal-child=</span><span class="st">"content_area"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-8"><a href="#cb6-8"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkBox"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-9"><a href="#cb6-9"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb6-10"><a href="#cb6-10"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkBox"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-11"><a href="#cb6-11"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"orientation"</span><span class="kw">></span>GTK_ORIENTATION_HORIZONTAL<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-12"><a href="#cb6-12"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"spacing"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-13"><a href="#cb6-13"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-start"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-14"><a href="#cb6-14"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-end"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-15"><a href="#cb6-15"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-top"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-16"><a href="#cb6-16"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"margin-bottom"</span><span class="kw">></span>12<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-17"><a href="#cb6-17"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb6-18"><a href="#cb6-18"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkImage"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-19"><a href="#cb6-19"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"icon-name"</span><span class="kw">></span>dialog-warning<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-20"><a href="#cb6-20"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"icon-size"</span><span class="kw">></span>GTK_ICON_SIZE_LARGE<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-21"><a href="#cb6-21"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb6-22"><a href="#cb6-22"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb6-23"><a href="#cb6-23"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb6-24"><a href="#cb6-24"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkLabel"</span><span class="ot"> id=</span><span class="st">"lb_alert"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-25"><a href="#cb6-25"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb6-26"><a href="#cb6-26"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb6-27"><a href="#cb6-27"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb6-28"><a href="#cb6-28"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb6-29"><a href="#cb6-29"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb6-30"><a href="#cb6-30"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb6-31"><a href="#cb6-31"></a> <span class="kw"><child</span><span class="ot"> type=</span><span class="st">"action"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-32"><a href="#cb6-32"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkButton"</span><span class="ot"> id=</span><span class="st">"btn_cancel"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-33"><a href="#cb6-33"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"label"</span><span class="kw">></span>Cancel<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-34"><a href="#cb6-34"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb6-35"><a href="#cb6-35"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb6-36"><a href="#cb6-36"></a> <span class="kw"><child</span><span class="ot"> type=</span><span class="st">"action"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-37"><a href="#cb6-37"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkButton"</span><span class="ot"> id=</span><span class="st">"btn_accept"</span><span class="kw">></span></span>
|
|||
|
<span id="cb6-38"><a href="#cb6-38"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"label"</span><span class="kw">></span>Close<span class="kw"></property></span></span>
|
|||
|
<span id="cb6-39"><a href="#cb6-39"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb6-40"><a href="#cb6-40"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb6-41"><a href="#cb6-41"></a> <span class="kw"><action-widgets></span></span>
|
|||
|
<span id="cb6-42"><a href="#cb6-42"></a> <span class="kw"><action-widget</span><span class="ot"> response=</span><span class="st">"cancel"</span><span class="ot"> default=</span><span class="st">"true"</span><span class="kw">></span>btn_cancel<span class="kw"></action-widget></span></span>
|
|||
|
<span id="cb6-43"><a href="#cb6-43"></a> <span class="kw"><action-widget</span><span class="ot"> response=</span><span class="st">"accept"</span><span class="kw">></span>btn_accept<span class="kw"></action-widget></span></span>
|
|||
|
<span id="cb6-44"><a href="#cb6-44"></a> <span class="kw"></action-widgets></span></span>
|
|||
|
<span id="cb6-45"><a href="#cb6-45"></a> <span class="kw"></template></span></span>
|
|||
|
<span id="cb6-46"><a href="#cb6-46"></a><span class="kw"></interface></span></span></code></pre></div>
|
|||
|
<p>The header file is:</p>
|
|||
|
<div class="sourceCode" id="cb7"><pre class="sourceCode numberSource C numberLines"><code class="sourceCode c"><span id="cb7-1"><a href="#cb7-1"></a><span class="pp">#ifndef __TFE_ALERT_H__</span></span>
|
|||
|
<span id="cb7-2"><a href="#cb7-2"></a><span class="pp">#define __TFE_ALERT_H__</span></span>
|
|||
|
<span id="cb7-3"><a href="#cb7-3"></a></span>
|
|||
|
<span id="cb7-4"><a href="#cb7-4"></a><span class="pp">#include </span><span class="im"><gtk/gtk.h></span></span>
|
|||
|
<span id="cb7-5"><a href="#cb7-5"></a></span>
|
|||
|
<span id="cb7-6"><a href="#cb7-6"></a><span class="pp">#define TFE_TYPE_ALERT tfe_alert_get_type ()</span></span>
|
|||
|
<span id="cb7-7"><a href="#cb7-7"></a>G_DECLARE_FINAL_TYPE (TfeAlert, tfe_alert, TFE, ALERT, GtkDialog)</span>
|
|||
|
<span id="cb7-8"><a href="#cb7-8"></a></span>
|
|||
|
<span id="cb7-9"><a href="#cb7-9"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb7-10"><a href="#cb7-10"></a>tfe_alert_set_message (TfeAlert *alert, <span class="dt">const</span> <span class="dt">char</span> *message);</span>
|
|||
|
<span id="cb7-11"><a href="#cb7-11"></a></span>
|
|||
|
<span id="cb7-12"><a href="#cb7-12"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb7-13"><a href="#cb7-13"></a>tfe_alert_set_button_label (TfeAlert *alert, <span class="dt">const</span> <span class="dt">char</span> *label);</span>
|
|||
|
<span id="cb7-14"><a href="#cb7-14"></a></span>
|
|||
|
<span id="cb7-15"><a href="#cb7-15"></a>GtkWidget *</span>
|
|||
|
<span id="cb7-16"><a href="#cb7-16"></a>tfe_alert_new (GtkWindow *win);</span>
|
|||
|
<span id="cb7-17"><a href="#cb7-17"></a></span>
|
|||
|
<span id="cb7-18"><a href="#cb7-18"></a><span class="pp">#endif </span><span class="co">/* __TFE_ALERT_H__ */</span></span></code></pre></div>
|
|||
|
<p>There are three public functions. The functions <code>tfe_alert_set_message</code> and <code>tfe_alert_set_button_label</code> sets the label and button name of the alert dialog. For example, if you want to show an alert that the user tries to close without saving the content, set them like:</p>
|
|||
|
<div class="sourceCode" id="cb8"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true"></a>tfe_alert_set_message (alert, <span class="st">"Are you really close without saving?"</span>); <span class="co">/* alert points to a TfeAlert instance */</span></span>
|
|||
|
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true"></a>tfe_alert_set_button_label (alert, <span class="st">"Close"</span>);</span></code></pre></div>
|
|||
|
<p>The function <code>tfe_alert_new</code> creates a TfeAlert dialog.</p>
|
|||
|
<p>The C source file is:</p>
|
|||
|
<div class="sourceCode" id="cb9"><pre class="sourceCode numberSource C numberLines"><code class="sourceCode c"><span id="cb9-1"><a href="#cb9-1"></a><span class="pp">#include </span><span class="im">"tfealert.h"</span></span>
|
|||
|
<span id="cb9-2"><a href="#cb9-2"></a></span>
|
|||
|
<span id="cb9-3"><a href="#cb9-3"></a><span class="kw">struct</span> _TfeAlert</span>
|
|||
|
<span id="cb9-4"><a href="#cb9-4"></a>{</span>
|
|||
|
<span id="cb9-5"><a href="#cb9-5"></a> GtkDialog parent;</span>
|
|||
|
<span id="cb9-6"><a href="#cb9-6"></a> GtkLabel *lb_alert;</span>
|
|||
|
<span id="cb9-7"><a href="#cb9-7"></a> GtkButton *btn_accept;</span>
|
|||
|
<span id="cb9-8"><a href="#cb9-8"></a>};</span>
|
|||
|
<span id="cb9-9"><a href="#cb9-9"></a></span>
|
|||
|
<span id="cb9-10"><a href="#cb9-10"></a>G_DEFINE_TYPE (TfeAlert, tfe_alert, GTK_TYPE_DIALOG);</span>
|
|||
|
<span id="cb9-11"><a href="#cb9-11"></a></span>
|
|||
|
<span id="cb9-12"><a href="#cb9-12"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb9-13"><a href="#cb9-13"></a>tfe_alert_set_message (TfeAlert *alert, <span class="dt">const</span> <span class="dt">char</span> *message) {</span>
|
|||
|
<span id="cb9-14"><a href="#cb9-14"></a> gtk_label_set_text (alert->lb_alert, message);</span>
|
|||
|
<span id="cb9-15"><a href="#cb9-15"></a>}</span>
|
|||
|
<span id="cb9-16"><a href="#cb9-16"></a></span>
|
|||
|
<span id="cb9-17"><a href="#cb9-17"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb9-18"><a href="#cb9-18"></a>tfe_alert_set_button_label (TfeAlert *alert, <span class="dt">const</span> <span class="dt">char</span> *label) {</span>
|
|||
|
<span id="cb9-19"><a href="#cb9-19"></a> gtk_button_set_label (alert->btn_accept, label);</span>
|
|||
|
<span id="cb9-20"><a href="#cb9-20"></a>}</span>
|
|||
|
<span id="cb9-21"><a href="#cb9-21"></a></span>
|
|||
|
<span id="cb9-22"><a href="#cb9-22"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb9-23"><a href="#cb9-23"></a>tfe_alert_init (TfeAlert *alert) {</span>
|
|||
|
<span id="cb9-24"><a href="#cb9-24"></a> gtk_widget_init_template (GTK_WIDGET (alert));</span>
|
|||
|
<span id="cb9-25"><a href="#cb9-25"></a>}</span>
|
|||
|
<span id="cb9-26"><a href="#cb9-26"></a></span>
|
|||
|
<span id="cb9-27"><a href="#cb9-27"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb9-28"><a href="#cb9-28"></a>tfe_alert_class_init (TfeAlertClass *class) {</span>
|
|||
|
<span id="cb9-29"><a href="#cb9-29"></a> gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class), <span class="st">"/com/github/ToshioCP/tfe/tfealert.ui"</span>);</span>
|
|||
|
<span id="cb9-30"><a href="#cb9-30"></a> gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), TfeAlert, lb_alert);</span>
|
|||
|
<span id="cb9-31"><a href="#cb9-31"></a> gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), TfeAlert, btn_accept);</span>
|
|||
|
<span id="cb9-32"><a href="#cb9-32"></a>}</span>
|
|||
|
<span id="cb9-33"><a href="#cb9-33"></a></span>
|
|||
|
<span id="cb9-34"><a href="#cb9-34"></a>GtkWidget *</span>
|
|||
|
<span id="cb9-35"><a href="#cb9-35"></a>tfe_alert_new (GtkWindow *win) {</span>
|
|||
|
<span id="cb9-36"><a href="#cb9-36"></a> <span class="cf">return</span> GTK_WIDGET (g_object_new (TFE_TYPE_ALERT, <span class="st">"transient-for"</span>, win, NULL));</span>
|
|||
|
<span id="cb9-37"><a href="#cb9-37"></a>}</span></code></pre></div>
|
|||
|
<p>The program is almost same as <code>tfepref.c</code>.</p>
|
|||
|
<p>The instruction how to use this object is as follows.</p>
|
|||
|
<ol type="1">
|
|||
|
<li>Write a “response” signal handler.</li>
|
|||
|
<li>Create a TfeAlert object.</li>
|
|||
|
<li>Connect “response” signal to a handler</li>
|
|||
|
<li>Show the dialog</li>
|
|||
|
<li>In the signal handler, do something with regard to the response-id. Then destroy the dialog.</li>
|
|||
|
</ol>
|
|||
|
<h2 id="top-level-window">Top-level window</h2>
|
|||
|
<p>In the same way, create a child object of GtkApplicationWindow. The object name is “TfeWindow”.</p>
|
|||
|
<div class="sourceCode" id="cb10"><pre class="sourceCode numberSource xml numberLines"><code class="sourceCode xml"><span id="cb10-1"><a href="#cb10-1"></a><span class="kw"><?xml</span> version="1.0" encoding="UTF-8"<span class="kw">?></span></span>
|
|||
|
<span id="cb10-2"><a href="#cb10-2"></a><span class="kw"><interface></span></span>
|
|||
|
<span id="cb10-3"><a href="#cb10-3"></a> <span class="kw"><template</span><span class="ot"> class=</span><span class="st">"TfeWindow"</span><span class="ot"> parent=</span><span class="st">"GtkApplicationWindow"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-4"><a href="#cb10-4"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"title"</span><span class="kw">></span>file editor<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-5"><a href="#cb10-5"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"default-width"</span><span class="kw">></span>600<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-6"><a href="#cb10-6"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"default-height"</span><span class="kw">></span>400<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-7"><a href="#cb10-7"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-8"><a href="#cb10-8"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkBox"</span><span class="ot"> id=</span><span class="st">"boxv"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-9"><a href="#cb10-9"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"orientation"</span><span class="kw">></span>GTK_ORIENTATION_VERTICAL<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-10"><a href="#cb10-10"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-11"><a href="#cb10-11"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkBox"</span><span class="ot"> id=</span><span class="st">"boxh"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-12"><a href="#cb10-12"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"orientation"</span><span class="kw">></span>GTK_ORIENTATION_HORIZONTAL<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-13"><a href="#cb10-13"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-14"><a href="#cb10-14"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkLabel"</span><span class="ot"> id=</span><span class="st">"dmy1"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-15"><a href="#cb10-15"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"width-chars"</span><span class="kw">></span>10<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-16"><a href="#cb10-16"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-17"><a href="#cb10-17"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-18"><a href="#cb10-18"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-19"><a href="#cb10-19"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkButton"</span><span class="ot"> id=</span><span class="st">"btno"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-20"><a href="#cb10-20"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"label"</span><span class="kw">></span>Open<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-21"><a href="#cb10-21"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"action-name"</span><span class="kw">></span>win.open<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-22"><a href="#cb10-22"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-23"><a href="#cb10-23"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-24"><a href="#cb10-24"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-25"><a href="#cb10-25"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkButton"</span><span class="ot"> id=</span><span class="st">"btns"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-26"><a href="#cb10-26"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"label"</span><span class="kw">></span>Save<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-27"><a href="#cb10-27"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"action-name"</span><span class="kw">></span>win.save<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-28"><a href="#cb10-28"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-29"><a href="#cb10-29"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-30"><a href="#cb10-30"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-31"><a href="#cb10-31"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkLabel"</span><span class="ot"> id=</span><span class="st">"dmy2"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-32"><a href="#cb10-32"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"hexpand"</span><span class="kw">></span>TRUE<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-33"><a href="#cb10-33"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-34"><a href="#cb10-34"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-35"><a href="#cb10-35"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-36"><a href="#cb10-36"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkButton"</span><span class="ot"> id=</span><span class="st">"btnc"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-37"><a href="#cb10-37"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"label"</span><span class="kw">></span>Close<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-38"><a href="#cb10-38"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"action-name"</span><span class="kw">></span>win.close<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-39"><a href="#cb10-39"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-40"><a href="#cb10-40"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-41"><a href="#cb10-41"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-42"><a href="#cb10-42"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkMenuButton"</span><span class="ot"> id=</span><span class="st">"btnm"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-43"><a href="#cb10-43"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"direction"</span><span class="kw">></span>down<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-44"><a href="#cb10-44"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"halign"</span><span class="kw">></span>start<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-45"><a href="#cb10-45"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"icon-name"</span><span class="kw">></span>open-menu-symbolic<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-46"><a href="#cb10-46"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-47"><a href="#cb10-47"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-48"><a href="#cb10-48"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-49"><a href="#cb10-49"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkLabel"</span><span class="ot"> id=</span><span class="st">"dmy3"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-50"><a href="#cb10-50"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"width-chars"</span><span class="kw">></span>10<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-51"><a href="#cb10-51"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-52"><a href="#cb10-52"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-53"><a href="#cb10-53"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-54"><a href="#cb10-54"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-55"><a href="#cb10-55"></a> <span class="kw"><child></span></span>
|
|||
|
<span id="cb10-56"><a href="#cb10-56"></a> <span class="kw"><object</span><span class="ot"> class=</span><span class="st">"GtkNotebook"</span><span class="ot"> id=</span><span class="st">"nb"</span><span class="kw">></span></span>
|
|||
|
<span id="cb10-57"><a href="#cb10-57"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"scrollable"</span><span class="kw">></span>TRUE<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-58"><a href="#cb10-58"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"hexpand"</span><span class="kw">></span>TRUE<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-59"><a href="#cb10-59"></a> <span class="kw"><property</span><span class="ot"> name=</span><span class="st">"vexpand"</span><span class="kw">></span>TRUE<span class="kw"></property></span></span>
|
|||
|
<span id="cb10-60"><a href="#cb10-60"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-61"><a href="#cb10-61"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-62"><a href="#cb10-62"></a> <span class="kw"></object></span></span>
|
|||
|
<span id="cb10-63"><a href="#cb10-63"></a> <span class="kw"></child></span></span>
|
|||
|
<span id="cb10-64"><a href="#cb10-64"></a> <span class="kw"></template></span></span>
|
|||
|
<span id="cb10-65"><a href="#cb10-65"></a><span class="kw"></interface></span></span></code></pre></div>
|
|||
|
<p>This XML file is almost same as before except template tag and “action-name” property in buttons.</p>
|
|||
|
<p>GtkButton implements GtkActionable interface, which has “action-name” property. If this property is set, GtkButton activates the action when it is clicked. For example, if an open button is clicked, “win.open” action will be activated and <code>open_activated</code> handler will be invoked.</p>
|
|||
|
<p>This action is also used by “<Control>o” accelerator (See the source code of <code>tfewindow.c</code> below). If you use “clicked” signal for the button, you need its signal handler. Then, there are two handlers:</p>
|
|||
|
<ul>
|
|||
|
<li>a handler for the “clicked” signal on the button</li>
|
|||
|
<li>a handler for the “activate” signal on the “win.open” action, to which “<Control>o” accelerator is connected</li>
|
|||
|
</ul>
|
|||
|
<p>These two handlers do almost same thing. It is inefficient. Connecting buttons to actions is a good way to reduce unnecessary codes.</p>
|
|||
|
<div class="sourceCode" id="cb11"><pre class="sourceCode numberSource C numberLines"><code class="sourceCode c"><span id="cb11-1"><a href="#cb11-1"></a><span class="pp">#ifndef __TFE_WINDOW_H__</span></span>
|
|||
|
<span id="cb11-2"><a href="#cb11-2"></a><span class="pp">#define __TFE_WINDOW_H__</span></span>
|
|||
|
<span id="cb11-3"><a href="#cb11-3"></a></span>
|
|||
|
<span id="cb11-4"><a href="#cb11-4"></a><span class="pp">#include </span><span class="im"><gtk/gtk.h></span></span>
|
|||
|
<span id="cb11-5"><a href="#cb11-5"></a></span>
|
|||
|
<span id="cb11-6"><a href="#cb11-6"></a><span class="pp">#define TFE_TYPE_WINDOW tfe_window_get_type ()</span></span>
|
|||
|
<span id="cb11-7"><a href="#cb11-7"></a>G_DECLARE_FINAL_TYPE (TfeWindow, tfe_window, TFE, WINDOW, GtkApplicationWindow)</span>
|
|||
|
<span id="cb11-8"><a href="#cb11-8"></a></span>
|
|||
|
<span id="cb11-9"><a href="#cb11-9"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb11-10"><a href="#cb11-10"></a>tfe_window_notebook_page_new (TfeWindow *win);</span>
|
|||
|
<span id="cb11-11"><a href="#cb11-11"></a></span>
|
|||
|
<span id="cb11-12"><a href="#cb11-12"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb11-13"><a href="#cb11-13"></a>tfe_window_notebook_page_new_with_files (TfeWindow *win, GFile **files, <span class="dt">int</span> n_files);</span>
|
|||
|
<span id="cb11-14"><a href="#cb11-14"></a></span>
|
|||
|
<span id="cb11-15"><a href="#cb11-15"></a>GtkWidget *</span>
|
|||
|
<span id="cb11-16"><a href="#cb11-16"></a>tfe_window_new (GtkApplication *app);</span>
|
|||
|
<span id="cb11-17"><a href="#cb11-17"></a></span>
|
|||
|
<span id="cb11-18"><a href="#cb11-18"></a><span class="pp">#endif </span><span class="co">/* __TFE_WINDOW_H__ */</span></span></code></pre></div>
|
|||
|
<p>There are three public functions. The function <code>tfe_window_notebook_page_new</code> creates a new notebook page. This is a wrapper function for <code>notebook_page_new</code>. It is called by GtkApplication object. The function <code>tfe_window_notebook_page_new_with_files</code> creates notebook pages with a contents read from the given files. The function <code>tfe_window_new</code> creates a TfeWindow instance.</p>
|
|||
|
<div class="sourceCode" id="cb12"><pre class="sourceCode numberSource C numberLines"><code class="sourceCode c"><span id="cb12-1"><a href="#cb12-1"></a><span class="pp">#include </span><span class="im">"tfewindow.h"</span></span>
|
|||
|
<span id="cb12-2"><a href="#cb12-2"></a><span class="pp">#include </span><span class="im">"tfenotebook.h"</span></span>
|
|||
|
<span id="cb12-3"><a href="#cb12-3"></a><span class="pp">#include </span><span class="im">"tfepref.h"</span></span>
|
|||
|
<span id="cb12-4"><a href="#cb12-4"></a><span class="pp">#include </span><span class="im">"tfealert.h"</span></span>
|
|||
|
<span id="cb12-5"><a href="#cb12-5"></a><span class="pp">#include </span><span class="im">"css.h"</span></span>
|
|||
|
<span id="cb12-6"><a href="#cb12-6"></a></span>
|
|||
|
<span id="cb12-7"><a href="#cb12-7"></a><span class="kw">struct</span> _TfeWindow {</span>
|
|||
|
<span id="cb12-8"><a href="#cb12-8"></a> GtkApplicationWindow parent;</span>
|
|||
|
<span id="cb12-9"><a href="#cb12-9"></a> GtkMenuButton *btnm;</span>
|
|||
|
<span id="cb12-10"><a href="#cb12-10"></a> GtkNotebook *nb;</span>
|
|||
|
<span id="cb12-11"><a href="#cb12-11"></a> GSettings *settings;</span>
|
|||
|
<span id="cb12-12"><a href="#cb12-12"></a> gboolean is_quit;</span>
|
|||
|
<span id="cb12-13"><a href="#cb12-13"></a>};</span>
|
|||
|
<span id="cb12-14"><a href="#cb12-14"></a></span>
|
|||
|
<span id="cb12-15"><a href="#cb12-15"></a>G_DEFINE_TYPE (TfeWindow, tfe_window, GTK_TYPE_APPLICATION_WINDOW);</span>
|
|||
|
<span id="cb12-16"><a href="#cb12-16"></a></span>
|
|||
|
<span id="cb12-17"><a href="#cb12-17"></a><span class="co">/* alert response signal handler */</span></span>
|
|||
|
<span id="cb12-18"><a href="#cb12-18"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-19"><a href="#cb12-19"></a>alert_response_cb (GtkDialog *alert, <span class="dt">int</span> response_id, gpointer user_data) {</span>
|
|||
|
<span id="cb12-20"><a href="#cb12-20"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-21"><a href="#cb12-21"></a></span>
|
|||
|
<span id="cb12-22"><a href="#cb12-22"></a> <span class="cf">if</span> (response_id == GTK_RESPONSE_ACCEPT) {</span>
|
|||
|
<span id="cb12-23"><a href="#cb12-23"></a> <span class="cf">if</span> (win->is_quit)</span>
|
|||
|
<span id="cb12-24"><a href="#cb12-24"></a> gtk_window_destroy(GTK_WINDOW (win));</span>
|
|||
|
<span id="cb12-25"><a href="#cb12-25"></a> <span class="cf">else</span></span>
|
|||
|
<span id="cb12-26"><a href="#cb12-26"></a> notebook_page_close (win->nb);</span>
|
|||
|
<span id="cb12-27"><a href="#cb12-27"></a> }</span>
|
|||
|
<span id="cb12-28"><a href="#cb12-28"></a> gtk_window_destroy (GTK_WINDOW (alert));</span>
|
|||
|
<span id="cb12-29"><a href="#cb12-29"></a>}</span>
|
|||
|
<span id="cb12-30"><a href="#cb12-30"></a></span>
|
|||
|
<span id="cb12-31"><a href="#cb12-31"></a><span class="co">/* ----- action activated handlers ----- */</span></span>
|
|||
|
<span id="cb12-32"><a href="#cb12-32"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-33"><a href="#cb12-33"></a>open_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) {</span>
|
|||
|
<span id="cb12-34"><a href="#cb12-34"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-35"><a href="#cb12-35"></a></span>
|
|||
|
<span id="cb12-36"><a href="#cb12-36"></a> notebook_page_open (GTK_NOTEBOOK (win->nb));</span>
|
|||
|
<span id="cb12-37"><a href="#cb12-37"></a>}</span>
|
|||
|
<span id="cb12-38"><a href="#cb12-38"></a></span>
|
|||
|
<span id="cb12-39"><a href="#cb12-39"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-40"><a href="#cb12-40"></a>save_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) {</span>
|
|||
|
<span id="cb12-41"><a href="#cb12-41"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-42"><a href="#cb12-42"></a></span>
|
|||
|
<span id="cb12-43"><a href="#cb12-43"></a> notebook_page_save (GTK_NOTEBOOK (win->nb));</span>
|
|||
|
<span id="cb12-44"><a href="#cb12-44"></a>}</span>
|
|||
|
<span id="cb12-45"><a href="#cb12-45"></a></span>
|
|||
|
<span id="cb12-46"><a href="#cb12-46"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-47"><a href="#cb12-47"></a>close_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) {</span>
|
|||
|
<span id="cb12-48"><a href="#cb12-48"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-49"><a href="#cb12-49"></a> TfeAlert *alert;</span>
|
|||
|
<span id="cb12-50"><a href="#cb12-50"></a></span>
|
|||
|
<span id="cb12-51"><a href="#cb12-51"></a> <span class="cf">if</span> (has_saved (win->nb))</span>
|
|||
|
<span id="cb12-52"><a href="#cb12-52"></a> notebook_page_close (win->nb);</span>
|
|||
|
<span id="cb12-53"><a href="#cb12-53"></a> <span class="cf">else</span> {</span>
|
|||
|
<span id="cb12-54"><a href="#cb12-54"></a> win->is_quit = false;</span>
|
|||
|
<span id="cb12-55"><a href="#cb12-55"></a> alert = TFE_ALERT (tfe_alert_new (GTK_WINDOW (win)));</span>
|
|||
|
<span id="cb12-56"><a href="#cb12-56"></a> tfe_alert_set_message (alert, <span class="st">"Contents aren't saved yet.</span><span class="sc">\n</span><span class="st">Are you sure to close?"</span>);</span>
|
|||
|
<span id="cb12-57"><a href="#cb12-57"></a> tfe_alert_set_button_label (alert, <span class="st">"Close"</span>);</span>
|
|||
|
<span id="cb12-58"><a href="#cb12-58"></a> g_signal_connect (GTK_DIALOG (alert), <span class="st">"response"</span>, G_CALLBACK (alert_response_cb), win);</span>
|
|||
|
<span id="cb12-59"><a href="#cb12-59"></a> gtk_widget_show (GTK_WIDGET (alert));</span>
|
|||
|
<span id="cb12-60"><a href="#cb12-60"></a> }</span>
|
|||
|
<span id="cb12-61"><a href="#cb12-61"></a>}</span>
|
|||
|
<span id="cb12-62"><a href="#cb12-62"></a></span>
|
|||
|
<span id="cb12-63"><a href="#cb12-63"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-64"><a href="#cb12-64"></a>new_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) {</span>
|
|||
|
<span id="cb12-65"><a href="#cb12-65"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-66"><a href="#cb12-66"></a></span>
|
|||
|
<span id="cb12-67"><a href="#cb12-67"></a> notebook_page_new (GTK_NOTEBOOK (win->nb));</span>
|
|||
|
<span id="cb12-68"><a href="#cb12-68"></a>}</span>
|
|||
|
<span id="cb12-69"><a href="#cb12-69"></a></span>
|
|||
|
<span id="cb12-70"><a href="#cb12-70"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-71"><a href="#cb12-71"></a>saveas_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) {</span>
|
|||
|
<span id="cb12-72"><a href="#cb12-72"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-73"><a href="#cb12-73"></a></span>
|
|||
|
<span id="cb12-74"><a href="#cb12-74"></a> notebook_page_saveas (GTK_NOTEBOOK (win->nb));</span>
|
|||
|
<span id="cb12-75"><a href="#cb12-75"></a>}</span>
|
|||
|
<span id="cb12-76"><a href="#cb12-76"></a></span>
|
|||
|
<span id="cb12-77"><a href="#cb12-77"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-78"><a href="#cb12-78"></a>pref_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) {</span>
|
|||
|
<span id="cb12-79"><a href="#cb12-79"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-80"><a href="#cb12-80"></a> GtkWidget *pref;</span>
|
|||
|
<span id="cb12-81"><a href="#cb12-81"></a></span>
|
|||
|
<span id="cb12-82"><a href="#cb12-82"></a> pref = tfe_pref_new (GTK_WINDOW (win));</span>
|
|||
|
<span id="cb12-83"><a href="#cb12-83"></a> gtk_widget_show (pref);</span>
|
|||
|
<span id="cb12-84"><a href="#cb12-84"></a>}</span>
|
|||
|
<span id="cb12-85"><a href="#cb12-85"></a></span>
|
|||
|
<span id="cb12-86"><a href="#cb12-86"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-87"><a href="#cb12-87"></a>quit_activated (GSimpleAction *action, GVariant *parameter, gpointer user_data) {</span>
|
|||
|
<span id="cb12-88"><a href="#cb12-88"></a> TfeWindow *win = TFE_WINDOW (user_data);</span>
|
|||
|
<span id="cb12-89"><a href="#cb12-89"></a></span>
|
|||
|
<span id="cb12-90"><a href="#cb12-90"></a> TfeAlert *alert;</span>
|
|||
|
<span id="cb12-91"><a href="#cb12-91"></a></span>
|
|||
|
<span id="cb12-92"><a href="#cb12-92"></a> <span class="cf">if</span> (has_saved_all (GTK_NOTEBOOK (win->nb)))</span>
|
|||
|
<span id="cb12-93"><a href="#cb12-93"></a> gtk_window_destroy (GTK_WINDOW (win));</span>
|
|||
|
<span id="cb12-94"><a href="#cb12-94"></a> <span class="cf">else</span> {</span>
|
|||
|
<span id="cb12-95"><a href="#cb12-95"></a> win->is_quit = true;</span>
|
|||
|
<span id="cb12-96"><a href="#cb12-96"></a> alert = TFE_ALERT (tfe_alert_new (GTK_WINDOW (win)));</span>
|
|||
|
<span id="cb12-97"><a href="#cb12-97"></a> tfe_alert_set_message (alert, <span class="st">"Contents aren't saved yet.</span><span class="sc">\n</span><span class="st">Are you sure to quit?"</span>);</span>
|
|||
|
<span id="cb12-98"><a href="#cb12-98"></a> tfe_alert_set_button_label (alert, <span class="st">"Quit"</span>);</span>
|
|||
|
<span id="cb12-99"><a href="#cb12-99"></a> g_signal_connect (GTK_DIALOG (alert), <span class="st">"response"</span>, G_CALLBACK (alert_response_cb), win);</span>
|
|||
|
<span id="cb12-100"><a href="#cb12-100"></a> gtk_widget_show (GTK_WIDGET (alert));</span>
|
|||
|
<span id="cb12-101"><a href="#cb12-101"></a> }</span>
|
|||
|
<span id="cb12-102"><a href="#cb12-102"></a>}</span>
|
|||
|
<span id="cb12-103"><a href="#cb12-103"></a></span>
|
|||
|
<span id="cb12-104"><a href="#cb12-104"></a><span class="co">/* gsettings changed::font signal handler */</span></span>
|
|||
|
<span id="cb12-105"><a href="#cb12-105"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-106"><a href="#cb12-106"></a>changed_font_cb (GSettings *settings, <span class="dt">char</span> *key, gpointer user_data) {</span>
|
|||
|
<span id="cb12-107"><a href="#cb12-107"></a> GtkWindow *win = GTK_WINDOW (user_data); </span>
|
|||
|
<span id="cb12-108"><a href="#cb12-108"></a> <span class="dt">char</span> *font;</span>
|
|||
|
<span id="cb12-109"><a href="#cb12-109"></a> PangoFontDescription *pango_font_desc;</span>
|
|||
|
<span id="cb12-110"><a href="#cb12-110"></a></span>
|
|||
|
<span id="cb12-111"><a href="#cb12-111"></a> font = g_settings_get_string (settings, <span class="st">"font"</span>);</span>
|
|||
|
<span id="cb12-112"><a href="#cb12-112"></a> pango_font_desc = pango_font_description_from_string (font);</span>
|
|||
|
<span id="cb12-113"><a href="#cb12-113"></a> g_free (font);</span>
|
|||
|
<span id="cb12-114"><a href="#cb12-114"></a> set_font_for_display_with_pango_font_desc (win, pango_font_desc);</span>
|
|||
|
<span id="cb12-115"><a href="#cb12-115"></a>}</span>
|
|||
|
<span id="cb12-116"><a href="#cb12-116"></a></span>
|
|||
|
<span id="cb12-117"><a href="#cb12-117"></a><span class="co">/* --- public functions --- */</span></span>
|
|||
|
<span id="cb12-118"><a href="#cb12-118"></a></span>
|
|||
|
<span id="cb12-119"><a href="#cb12-119"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb12-120"><a href="#cb12-120"></a>tfe_window_notebook_page_new (TfeWindow *win) {</span>
|
|||
|
<span id="cb12-121"><a href="#cb12-121"></a> notebook_page_new (win->nb);</span>
|
|||
|
<span id="cb12-122"><a href="#cb12-122"></a>}</span>
|
|||
|
<span id="cb12-123"><a href="#cb12-123"></a></span>
|
|||
|
<span id="cb12-124"><a href="#cb12-124"></a><span class="dt">void</span></span>
|
|||
|
<span id="cb12-125"><a href="#cb12-125"></a>tfe_window_notebook_page_new_with_files (TfeWindow *win, GFile **files, <span class="dt">int</span> n_files) {</span>
|
|||
|
<span id="cb12-126"><a href="#cb12-126"></a> <span class="dt">int</span> i;</span>
|
|||
|
<span id="cb12-127"><a href="#cb12-127"></a></span>
|
|||
|
<span id="cb12-128"><a href="#cb12-128"></a> <span class="cf">for</span> (i = <span class="dv">0</span>; i < n_files; i++)</span>
|
|||
|
<span id="cb12-129"><a href="#cb12-129"></a> notebook_page_new_with_file (win->nb, files[i]);</span>
|
|||
|
<span id="cb12-130"><a href="#cb12-130"></a> <span class="cf">if</span> (gtk_notebook_get_n_pages (win->nb) == <span class="dv">0</span>)</span>
|
|||
|
<span id="cb12-131"><a href="#cb12-131"></a> notebook_page_new (win->nb);</span>
|
|||
|
<span id="cb12-132"><a href="#cb12-132"></a>}</span>
|
|||
|
<span id="cb12-133"><a href="#cb12-133"></a></span>
|
|||
|
<span id="cb12-134"><a href="#cb12-134"></a><span class="co">/* --- TfeWindow object construction/destruction --- */</span> </span>
|
|||
|
<span id="cb12-135"><a href="#cb12-135"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-136"><a href="#cb12-136"></a>tfe_window_dispose (GObject *gobject) {</span>
|
|||
|
<span id="cb12-137"><a href="#cb12-137"></a> TfeWindow *window = TFE_WINDOW (gobject);</span>
|
|||
|
<span id="cb12-138"><a href="#cb12-138"></a></span>
|
|||
|
<span id="cb12-139"><a href="#cb12-139"></a> g_clear_object (&window->settings);</span>
|
|||
|
<span id="cb12-140"><a href="#cb12-140"></a> G_OBJECT_CLASS (tfe_window_parent_class)->dispose (gobject);</span>
|
|||
|
<span id="cb12-141"><a href="#cb12-141"></a>}</span>
|
|||
|
<span id="cb12-142"><a href="#cb12-142"></a></span>
|
|||
|
<span id="cb12-143"><a href="#cb12-143"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-144"><a href="#cb12-144"></a>tfe_window_init (TfeWindow *win) {</span>
|
|||
|
<span id="cb12-145"><a href="#cb12-145"></a> GtkBuilder *build;</span>
|
|||
|
<span id="cb12-146"><a href="#cb12-146"></a> GMenuModel *menu;</span>
|
|||
|
<span id="cb12-147"><a href="#cb12-147"></a></span>
|
|||
|
<span id="cb12-148"><a href="#cb12-148"></a> gtk_widget_init_template (GTK_WIDGET (win));</span>
|
|||
|
<span id="cb12-149"><a href="#cb12-149"></a></span>
|
|||
|
<span id="cb12-150"><a href="#cb12-150"></a> build = gtk_builder_new_from_resource (<span class="st">"/com/github/ToshioCP/tfe/menu.ui"</span>);</span>
|
|||
|
<span id="cb12-151"><a href="#cb12-151"></a> menu = G_MENU_MODEL (gtk_builder_get_object (build, <span class="st">"menu"</span>));</span>
|
|||
|
<span id="cb12-152"><a href="#cb12-152"></a> gtk_menu_button_set_menu_model (win->btnm, menu);</span>
|
|||
|
<span id="cb12-153"><a href="#cb12-153"></a> g_object_unref(build);</span>
|
|||
|
<span id="cb12-154"><a href="#cb12-154"></a></span>
|
|||
|
<span id="cb12-155"><a href="#cb12-155"></a> win->settings = g_settings_new (<span class="st">"com.github.ToshioCP.tfe"</span>);</span>
|
|||
|
<span id="cb12-156"><a href="#cb12-156"></a> g_signal_connect (win->settings, <span class="st">"changed::font"</span>, G_CALLBACK (changed_font_cb), win);</span>
|
|||
|
<span id="cb12-157"><a href="#cb12-157"></a></span>
|
|||
|
<span id="cb12-158"><a href="#cb12-158"></a><span class="co">/* ----- action ----- */</span></span>
|
|||
|
<span id="cb12-159"><a href="#cb12-159"></a> <span class="dt">const</span> GActionEntry win_entries[] = {</span>
|
|||
|
<span id="cb12-160"><a href="#cb12-160"></a> { <span class="st">"open"</span>, open_activated, NULL, NULL, NULL },</span>
|
|||
|
<span id="cb12-161"><a href="#cb12-161"></a> { <span class="st">"save"</span>, save_activated, NULL, NULL, NULL },</span>
|
|||
|
<span id="cb12-162"><a href="#cb12-162"></a> { <span class="st">"close"</span>, close_activated, NULL, NULL, NULL },</span>
|
|||
|
<span id="cb12-163"><a href="#cb12-163"></a> { <span class="st">"new"</span>, new_activated, NULL, NULL, NULL },</span>
|
|||
|
<span id="cb12-164"><a href="#cb12-164"></a> { <span class="st">"saveas"</span>, saveas_activated, NULL, NULL, NULL },</span>
|
|||
|
<span id="cb12-165"><a href="#cb12-165"></a> { <span class="st">"pref"</span>, pref_activated, NULL, NULL, NULL },</span>
|
|||
|
<span id="cb12-166"><a href="#cb12-166"></a> { <span class="st">"close-all"</span>, quit_activated, NULL, NULL, NULL }</span>
|
|||
|
<span id="cb12-167"><a href="#cb12-167"></a> };</span>
|
|||
|
<span id="cb12-168"><a href="#cb12-168"></a> g_action_map_add_action_entries (G_ACTION_MAP (win), win_entries, G_N_ELEMENTS (win_entries), win);</span>
|
|||
|
<span id="cb12-169"><a href="#cb12-169"></a></span>
|
|||
|
<span id="cb12-170"><a href="#cb12-170"></a> changed_font_cb(win->settings, <span class="st">"font"</span>, win);</span>
|
|||
|
<span id="cb12-171"><a href="#cb12-171"></a>}</span>
|
|||
|
<span id="cb12-172"><a href="#cb12-172"></a></span>
|
|||
|
<span id="cb12-173"><a href="#cb12-173"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb12-174"><a href="#cb12-174"></a>tfe_window_class_init (TfeWindowClass *class) {</span>
|
|||
|
<span id="cb12-175"><a href="#cb12-175"></a> GObjectClass *object_class = G_OBJECT_CLASS (class);</span>
|
|||
|
<span id="cb12-176"><a href="#cb12-176"></a></span>
|
|||
|
<span id="cb12-177"><a href="#cb12-177"></a> object_class->dispose = tfe_window_dispose;</span>
|
|||
|
<span id="cb12-178"><a href="#cb12-178"></a> gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class), <span class="st">"/com/github/ToshioCP/tfe/tfewindow.ui"</span>);</span>
|
|||
|
<span id="cb12-179"><a href="#cb12-179"></a> gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), TfeWindow, btnm);</span>
|
|||
|
<span id="cb12-180"><a href="#cb12-180"></a> gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), TfeWindow, nb);</span>
|
|||
|
<span id="cb12-181"><a href="#cb12-181"></a>}</span>
|
|||
|
<span id="cb12-182"><a href="#cb12-182"></a></span>
|
|||
|
<span id="cb12-183"><a href="#cb12-183"></a>GtkWidget *</span>
|
|||
|
<span id="cb12-184"><a href="#cb12-184"></a>tfe_window_new (GtkApplication *app) {</span>
|
|||
|
<span id="cb12-185"><a href="#cb12-185"></a> <span class="cf">return</span> GTK_WIDGET (g_object_new (TFE_TYPE_WINDOW, <span class="st">"application"</span>, app, NULL));</span>
|
|||
|
<span id="cb12-186"><a href="#cb12-186"></a>}</span></code></pre></div>
|
|||
|
<ul>
|
|||
|
<li>17-29: <code>alert_response_cb</code> is a call back function of the “response” signal of TfeAlert dialog. This is the same as before except <code>gtk_window_destroy(GTK_WINDOW (win))</code> is used instead of <code>tfe_application_quit</code>.</li>
|
|||
|
<li>31-102: Handlers of action activated signal. The <code>user_data</code> is a pointer to TfeWindow instance.</li>
|
|||
|
<li>104-115: A handler of “changed::font” signal of GSettings object.</li>
|
|||
|
<li>111: Gets the font from GSettings data.</li>
|
|||
|
<li>112: Gets a PangoFontDescription from the font. In the previous version, the program gets the font description from the GtkFontButton. The button data and GSettings data are the same. Therefore, the data got here is the same as the data in the GtkFontButton. In addition, we don’t need to worry about the preference dialog is alive or not thanks to the GSettings.</li>
|
|||
|
<li>114: Sets CSS on the display with the font description.</li>
|
|||
|
<li>117-132: Public functions.</li>
|
|||
|
<li>134-141: Dispose handler. The GSettings object needs to be released.</li>
|
|||
|
<li>143-171: Instance initialization function.</li>
|
|||
|
<li>148: Creates a composite widget instance with the template.</li>
|
|||
|
<li>151-153: Insert <code>menu</code> to the menu button.</li>
|
|||
|
<li>155-156: Creates a GSettings instance with the id “com.github.ToshioCP.tfe”. Connects “changed::font” signal to the handler <code>changed_font_cb</code>. This signal emits when the GSettings data is changed. The second part “font” of the signal name “changed::font” is called details. Signals can have details. If a GSettings instance has more than one key, “changed” signal emits only if the key, which has the same name as the detail, changes its value. For example, Suppose a GSettings object has three keys “a”, “b” and “c”.
|
|||
|
<ul>
|
|||
|
<li>“changed::a” is emitted when the value of the key “a” is changed. It isn’t emitted when the value of “b” or “c” is changed.</li>
|
|||
|
<li>“changed::b” is emitted when the value of the key “b” is changed. It isn’t emitted when the value of “a” or “c” is changed.</li>
|
|||
|
<li>“changed::c” is emitted when the value of the key “c” is changed. It isn’t emitted when the value of “a” or “b” is changed. In this version of tfe, there is only one key (“font”). So, even if the signal doesn’t have a detail, the result is the same. But in the future version, it will probably need details.</li>
|
|||
|
</ul></li>
|
|||
|
<li>158-168: Creates actions.</li>
|
|||
|
<li>170: Sets CSS font.</li>
|
|||
|
<li>173-181: Class initialization function.</li>
|
|||
|
<li>177: Sets the dispose handler.</li>
|
|||
|
<li>178: Sets the composite widget template</li>
|
|||
|
<li>179-180: Binds private variable with child objects in the template.</li>
|
|||
|
<li>183-186: <code>tfe_window_new</code>. This function creates TfeWindow instance.</li>
|
|||
|
</ul>
|
|||
|
<h2 id="tfeapplication">TfeApplication</h2>
|
|||
|
<p>The file <code>tfeapplication.c</code> is now very simple.</p>
|
|||
|
<div class="sourceCode" id="cb13"><pre class="sourceCode numberSource C numberLines"><code class="sourceCode c"><span id="cb13-1"><a href="#cb13-1"></a><span class="pp">#include </span><span class="im">"tfewindow.h"</span></span>
|
|||
|
<span id="cb13-2"><a href="#cb13-2"></a></span>
|
|||
|
<span id="cb13-3"><a href="#cb13-3"></a><span class="co">/* ----- activate, open, startup handlers ----- */</span></span>
|
|||
|
<span id="cb13-4"><a href="#cb13-4"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb13-5"><a href="#cb13-5"></a>app_activate (GApplication *application) {</span>
|
|||
|
<span id="cb13-6"><a href="#cb13-6"></a> GtkApplication *app = GTK_APPLICATION (application);</span>
|
|||
|
<span id="cb13-7"><a href="#cb13-7"></a> GtkWidget *win = GTK_WIDGET (gtk_application_get_active_window (app));</span>
|
|||
|
<span id="cb13-8"><a href="#cb13-8"></a></span>
|
|||
|
<span id="cb13-9"><a href="#cb13-9"></a> tfe_window_notebook_page_new (TFE_WINDOW (win));</span>
|
|||
|
<span id="cb13-10"><a href="#cb13-10"></a> gtk_widget_show (GTK_WIDGET (win));</span>
|
|||
|
<span id="cb13-11"><a href="#cb13-11"></a>}</span>
|
|||
|
<span id="cb13-12"><a href="#cb13-12"></a></span>
|
|||
|
<span id="cb13-13"><a href="#cb13-13"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb13-14"><a href="#cb13-14"></a>app_open (GApplication *application, GFile ** files, gint n_files, <span class="dt">const</span> gchar *hint) {</span>
|
|||
|
<span id="cb13-15"><a href="#cb13-15"></a> GtkApplication *app = GTK_APPLICATION (application);</span>
|
|||
|
<span id="cb13-16"><a href="#cb13-16"></a> GtkWidget *win = GTK_WIDGET (gtk_application_get_active_window (app));</span>
|
|||
|
<span id="cb13-17"><a href="#cb13-17"></a></span>
|
|||
|
<span id="cb13-18"><a href="#cb13-18"></a> tfe_window_notebook_page_new_with_files (TFE_WINDOW (win), files, n_files);</span>
|
|||
|
<span id="cb13-19"><a href="#cb13-19"></a> gtk_widget_show (win);</span>
|
|||
|
<span id="cb13-20"><a href="#cb13-20"></a>}</span>
|
|||
|
<span id="cb13-21"><a href="#cb13-21"></a></span>
|
|||
|
<span id="cb13-22"><a href="#cb13-22"></a><span class="dt">static</span> <span class="dt">void</span></span>
|
|||
|
<span id="cb13-23"><a href="#cb13-23"></a>app_startup (GApplication *application) {</span>
|
|||
|
<span id="cb13-24"><a href="#cb13-24"></a> GtkApplication *app = GTK_APPLICATION (application);</span>
|
|||
|
<span id="cb13-25"><a href="#cb13-25"></a> <span class="dt">int</span> i;</span>
|
|||
|
<span id="cb13-26"><a href="#cb13-26"></a></span>
|
|||
|
<span id="cb13-27"><a href="#cb13-27"></a> tfe_window_new (app);</span>
|
|||
|
<span id="cb13-28"><a href="#cb13-28"></a></span>
|
|||
|
<span id="cb13-29"><a href="#cb13-29"></a><span class="co">/* ----- accelerator ----- */</span> </span>
|
|||
|
<span id="cb13-30"><a href="#cb13-30"></a> <span class="kw">struct</span> {</span>
|
|||
|
<span id="cb13-31"><a href="#cb13-31"></a> <span class="dt">const</span> <span class="dt">char</span> *action;</span>
|
|||
|
<span id="cb13-32"><a href="#cb13-32"></a> <span class="dt">const</span> <span class="dt">char</span> *accels[<span class="dv">2</span>];</span>
|
|||
|
<span id="cb13-33"><a href="#cb13-33"></a> } action_accels[] = {</span>
|
|||
|
<span id="cb13-34"><a href="#cb13-34"></a> { <span class="st">"win.open"</span>, { <span class="st">"<Control>o"</span>, NULL } },</span>
|
|||
|
<span id="cb13-35"><a href="#cb13-35"></a> { <span class="st">"win.save"</span>, { <span class="st">"<Control>s"</span>, NULL } },</span>
|
|||
|
<span id="cb13-36"><a href="#cb13-36"></a> { <span class="st">"win.close"</span>, { <span class="st">"<Control>w"</span>, NULL } },</span>
|
|||
|
<span id="cb13-37"><a href="#cb13-37"></a> { <span class="st">"win.new"</span>, { <span class="st">"<Control>n"</span>, NULL } },</span>
|
|||
|
<span id="cb13-38"><a href="#cb13-38"></a> { <span class="st">"win.saveas"</span>, { <span class="st">"<Shift><Control>s"</span>, NULL } },</span>
|
|||
|
<span id="cb13-39"><a href="#cb13-39"></a> { <span class="st">"win.close-all"</span>, { <span class="st">"<Control>q"</span>, NULL } },</span>
|
|||
|
<span id="cb13-40"><a href="#cb13-40"></a> };</span>
|
|||
|
<span id="cb13-41"><a href="#cb13-41"></a></span>
|
|||
|
<span id="cb13-42"><a href="#cb13-42"></a> <span class="cf">for</span> (i = <span class="dv">0</span>; i < G_N_ELEMENTS(action_accels); i++)</span>
|
|||
|
<span id="cb13-43"><a href="#cb13-43"></a> gtk_application_set_accels_for_action(GTK_APPLICATION(app), action_accels[i].action, action_accels[i].accels);</span>
|
|||
|
<span id="cb13-44"><a href="#cb13-44"></a>}</span>
|
|||
|
<span id="cb13-45"><a href="#cb13-45"></a></span>
|
|||
|
<span id="cb13-46"><a href="#cb13-46"></a><span class="co">/* ----- main ----- */</span></span>
|
|||
|
<span id="cb13-47"><a href="#cb13-47"></a></span>
|
|||
|
<span id="cb13-48"><a href="#cb13-48"></a><span class="pp">#define APPLICATION_ID "com.github.ToshioCP.tfe"</span></span>
|
|||
|
<span id="cb13-49"><a href="#cb13-49"></a></span>
|
|||
|
<span id="cb13-50"><a href="#cb13-50"></a><span class="dt">int</span></span>
|
|||
|
<span id="cb13-51"><a href="#cb13-51"></a>main (<span class="dt">int</span> argc, <span class="dt">char</span> **argv) {</span>
|
|||
|
<span id="cb13-52"><a href="#cb13-52"></a> GtkApplication *app;</span>
|
|||
|
<span id="cb13-53"><a href="#cb13-53"></a> <span class="dt">int</span> stat;</span>
|
|||
|
<span id="cb13-54"><a href="#cb13-54"></a></span>
|
|||
|
<span id="cb13-55"><a href="#cb13-55"></a> app = gtk_application_new (APPLICATION_ID, G_APPLICATION_HANDLES_OPEN);</span>
|
|||
|
<span id="cb13-56"><a href="#cb13-56"></a></span>
|
|||
|
<span id="cb13-57"><a href="#cb13-57"></a> g_signal_connect (app, <span class="st">"startup"</span>, G_CALLBACK (app_startup), NULL);</span>
|
|||
|
<span id="cb13-58"><a href="#cb13-58"></a> g_signal_connect (app, <span class="st">"activate"</span>, G_CALLBACK (app_activate), NULL);</span>
|
|||
|
<span id="cb13-59"><a href="#cb13-59"></a> g_signal_connect (app, <span class="st">"open"</span>, G_CALLBACK (app_open), NULL);</span>
|
|||
|
<span id="cb13-60"><a href="#cb13-60"></a></span>
|
|||
|
<span id="cb13-61"><a href="#cb13-61"></a> stat =g_application_run (G_APPLICATION (app), argc, argv);</span>
|
|||
|
<span id="cb13-62"><a href="#cb13-62"></a> g_object_unref (app);</span>
|
|||
|
<span id="cb13-63"><a href="#cb13-63"></a> <span class="cf">return</span> stat;</span>
|
|||
|
<span id="cb13-64"><a href="#cb13-64"></a>}</span></code></pre></div>
|
|||
|
<ul>
|
|||
|
<li>4-11: Activate signal handler. It uses <code>tfe_window_notebook_page_new</code> instead of <code>notebook_page_new</code>.</li>
|
|||
|
<li>13-20: Open signal handler. Thanks to <code>tfe_window_notebook_page_new_with_files</code>, this handler becomes very simple.</li>
|
|||
|
<li>22-44: Startup signal handler. Most of the tasks are moved to TfeWindow, the remaining tasks are creating a window and setting accelerations.</li>
|
|||
|
<li>48-64: A function <code>main</code>.</li>
|
|||
|
</ul>
|
|||
|
<h2 id="other-files">Other files</h2>
|
|||
|
<p>Resource XML file.</p>
|
|||
|
<div class="sourceCode" id="cb14"><pre class="sourceCode numberSource xml numberLines"><code class="sourceCode xml"><span id="cb14-1"><a href="#cb14-1"></a><span class="kw"><?xml</span> version="1.0" encoding="UTF-8"<span class="kw">?></span></span>
|
|||
|
<span id="cb14-2"><a href="#cb14-2"></a><span class="kw"><gresources></span></span>
|
|||
|
<span id="cb14-3"><a href="#cb14-3"></a> <span class="kw"><gresource</span><span class="ot"> prefix=</span><span class="st">"/com/github/ToshioCP/tfe"</span><span class="kw">></span></span>
|
|||
|
<span id="cb14-4"><a href="#cb14-4"></a> <span class="kw"><file></span>tfewindow.ui<span class="kw"></file></span></span>
|
|||
|
<span id="cb14-5"><a href="#cb14-5"></a> <span class="kw"><file></span>tfepref.ui<span class="kw"></file></span></span>
|
|||
|
<span id="cb14-6"><a href="#cb14-6"></a> <span class="kw"><file></span>tfealert.ui<span class="kw"></file></span></span>
|
|||
|
<span id="cb14-7"><a href="#cb14-7"></a> <span class="kw"><file></span>menu.ui<span class="kw"></file></span></span>
|
|||
|
<span id="cb14-8"><a href="#cb14-8"></a> <span class="kw"></gresource></span></span>
|
|||
|
<span id="cb14-9"><a href="#cb14-9"></a><span class="kw"></gresources></span></span></code></pre></div>
|
|||
|
<p>GSchema XML file</p>
|
|||
|
<div class="sourceCode" id="cb15"><pre class="sourceCode numberSource xml numberLines"><code class="sourceCode xml"><span id="cb15-1"><a href="#cb15-1"></a><span class="kw"><?xml</span> version="1.0" encoding="UTF-8"<span class="kw">?></span></span>
|
|||
|
<span id="cb15-2"><a href="#cb15-2"></a><span class="kw"><schemalist></span></span>
|
|||
|
<span id="cb15-3"><a href="#cb15-3"></a> <span class="kw"><schema</span><span class="ot"> path=</span><span class="st">"/com/github/ToshioCP/tfe/"</span><span class="ot"> id=</span><span class="st">"com.github.ToshioCP.tfe"</span><span class="kw">></span></span>
|
|||
|
<span id="cb15-4"><a href="#cb15-4"></a> <span class="kw"><key</span><span class="ot"> name=</span><span class="st">"font"</span><span class="ot"> type=</span><span class="st">"s"</span><span class="kw">></span></span>
|
|||
|
<span id="cb15-5"><a href="#cb15-5"></a> <span class="kw"><default></span>'Monospace 12'<span class="kw"></default></span></span>
|
|||
|
<span id="cb15-6"><a href="#cb15-6"></a> <span class="kw"><summary></span>Font<span class="kw"></summary></span></span>
|
|||
|
<span id="cb15-7"><a href="#cb15-7"></a> <span class="kw"><description></span>The font to be used for textview.<span class="kw"></description></span></span>
|
|||
|
<span id="cb15-8"><a href="#cb15-8"></a> <span class="kw"></key></span></span>
|
|||
|
<span id="cb15-9"><a href="#cb15-9"></a> <span class="kw"></schema></span></span>
|
|||
|
<span id="cb15-10"><a href="#cb15-10"></a><span class="kw"></schemalist></span></span></code></pre></div>
|
|||
|
<p>Meson.build</p>
|
|||
|
<div class="sourceCode" id="cb16"><pre class="sourceCode numberSource numberLines"><code class="sourceCode"><span id="cb16-1"><a href="#cb16-1"></a>project('tfe', 'c')</span>
|
|||
|
<span id="cb16-2"><a href="#cb16-2"></a></span>
|
|||
|
<span id="cb16-3"><a href="#cb16-3"></a>gtkdep = dependency('gtk4')</span>
|
|||
|
<span id="cb16-4"><a href="#cb16-4"></a></span>
|
|||
|
<span id="cb16-5"><a href="#cb16-5"></a>gnome=import('gnome')</span>
|
|||
|
<span id="cb16-6"><a href="#cb16-6"></a>resources = gnome.compile_resources('resources','tfe.gresource.xml')</span>
|
|||
|
<span id="cb16-7"><a href="#cb16-7"></a>gnome.compile_schemas(build_by_default: true, depend_files: 'com.github.ToshioCP.tfe.gschema.xml')</span>
|
|||
|
<span id="cb16-8"><a href="#cb16-8"></a></span>
|
|||
|
<span id="cb16-9"><a href="#cb16-9"></a>sourcefiles=files('tfeapplication.c', 'tfewindow.c', 'tfenotebook.c', 'tfepref.c', 'tfealert.c', 'css.c', '../tfetextview/tfetextview.c')</span>
|
|||
|
<span id="cb16-10"><a href="#cb16-10"></a></span>
|
|||
|
<span id="cb16-11"><a href="#cb16-11"></a>executable('tfe', sourcefiles, resources, dependencies: gtkdep, export_dynamic: true, install: true)</span>
|
|||
|
<span id="cb16-12"><a href="#cb16-12"></a></span>
|
|||
|
<span id="cb16-13"><a href="#cb16-13"></a>schema_dir = get_option('prefix') / get_option('datadir') / 'glib-2.0/schemas/'</span>
|
|||
|
<span id="cb16-14"><a href="#cb16-14"></a>install_data('com.github.ToshioCP.tfe.gschema.xml', install_dir: schema_dir)</span>
|
|||
|
<span id="cb16-15"><a href="#cb16-15"></a>meson.add_install_script('glib-compile-schemas', schema_dir)</span></code></pre></div>
|
|||
|
<h2 id="compilation-and-installation.">Compilation and installation.</h2>
|
|||
|
<p>If you build Gtk4 from the source, use <code>--prefix</code> option.</p>
|
|||
|
<pre><code>$ meson --prefix=$HOME/local _build
|
|||
|
$ ninja -C _build
|
|||
|
$ ninja -C _build install</code></pre>
|
|||
|
<p>If you install Gtk4 from the distribution packages, you don’t need the prefix option. Maybe you need root privilege to install it.</p>
|
|||
|
<pre><code>$ meson _build
|
|||
|
$ ninja -C _build
|
|||
|
$ ninja -C _build install # or 'sudo ninja -C _build install'</code></pre>
|
|||
|
<p>Source files are in <a href="../src/tfe7">src/tfe7</a> directory.</p>
|
|||
|
<p>We made a very small text editor. You can add features to this editor. When you add a new feature, care about the structure of the program. Maybe you need to divide a file into several files like this section. It isn’t good to put many things into one file. And it is important to think about the relationship between source files and widget structures. It is appropriate that they correspond to each other in many cases.</p>
|
|||
|
<p>Up: <a href="index.html">index.html</a>, Prev: <a href="sec20.html">Section 20</a>, Next: <a href="sec22.html">Section 22</a></p>
|
|||
|
</body>
|
|||
|
</html>
|