<h1id="defining-a-child-object">Defining a Child object</h1>
<h2id="a-very-simple-editor">A Very Simple Editor</h2>
<p>In the previous section we made a very simple file viewer. Now we go on to rewrite it and turn it into very simple editor. Its source file is in tfe1.c (text file editor 1).</p>
<p>GtkTextView has a feature for editing multiple lines. Therefore, we don’t need to write the program from scratch, we just add two things to the file viewer:</p>
<ul>
<li>Memory to store a pointer to the GFile instance.</li>
<li>A function to write the file.</li>
</ul>
<p>There are a couple of ways to store the details of GFile.</p>
<ul>
<li>Use global variables; or</li>
<li>Make a child object, which can extend the instance memory for the GFile object.</li>
</ul>
<p>Using global variables is easy to implement. Define a sufficient size array of pointers to GFile. For example,</p>
<p>The variable <code>f[i]</code> corresponds to the file associated to the i-th GtkNotebookPage. There are however two problems with this. The first concerns the size of the array. If a user gives too many arguments (more than 20 in the example above), it is impossible to store the additional pointers to the GFile instances. The second is the increasing difficulty for maintenance of the program. We have a small program so far, but however, if you continue developing it, the size of the program will grow. Generally speaking, the bigger the program size, the more difficult it is to keep track of and maintain global variables. Global variables can be used and changed anywhere throughout the entire program.</p>
<p>Making a child object is a good idea in terms of maintenance. One thing you need to be careful of is the difference between “child object” and “child widget”. Here we are describing a “child object”. A child object includes, and expands on its parent object, as a child object derives everything from the parent object.</p>
<figure>
<imgsrc="../image/child.png"alt=""/><figcaption>Child object of GtkTextView</figcaption>
</figure>
<p>We will define TfeTextView as a child object of GtkTextView. It has everything that GtkTextView has. Specifically, TfeTextView has a GtkTextbuffer which corresponds to the GtkTextView inside TfeTextView. The additional important thing is that TfeTextView can also keep an additional pointer to GFile.</p>
<p>In general, this is how GObjects work. Understanding the general theory about Gobject’s is difficult, particularly for beginners. So, I will just show you the way how to write the code and avoid the theoretical side. If you want to know about GObject system, refer to the separate tutorial](https://github.com/ToshioCP/Gobject-tutorial).</p>
<h2id="how-to-define-a-child-object-of-gtktextview">How to Define a Child Object of GtkTextView</h2>
<p>Let’s define the TfeTextView object, which is a child object of GtkTextView. First, look at the program below.</p>
<p>If you are curious about the background theory of this program, that’s good, because knowing the theory is very important if you want to program GTK applications. Look at <ahref="https://docs.gtk.org/gobject/">GObject API Reference</a>. All you need is described there, or refer to <ahref="https://github.com/ToshioCP/Gobject-tutorial">GObject tutorial</a>. It’s a tough journey especially for beginners so for now, you don’t need to know about this difficult theory. It is enough to just remember the instructions below.</p>
<ul>
<li>TfeTextView is divided into two parts. Tfe and TextView. Tfe is called the prefix, namespace or module. TextView is called the object.</li>
<li>There are three differnet identifier patterns. TfeTextView (camel case), tfe_text_view (this is used to write functions) and TFE_TEXT_VIEW (This is used to cast a pointer to point TfeTextView type).</li>
<li>First, define TFE_TYPE_TEXT_VIEW macro as tfe_text_view_get_type (). The name is always (prefix)_TYPE_(object) and the letters are upper case. And the replacement text is always (prefix)_(object)_get_type () and the letters are lower case.</li>
<li>Next, use G_DECLARE_FINAL_TYPE macro. The arguments are the child object name in camel case, lower case with underscore, prefix (upper case), object (upper case with underscore) and parent object name (camel case).</li>
<li>Declare the structure _TfeTextView. The underscore is necessary. The first member is the parent object. Notice this is not a pointer but the object itself. The second member and after are members of the child object. TfeTextView structure has a pointer to a GFile instance as a member.</li>
<li>Use G_DEFINE_TYPE macro. The arguments are the child object name in camel case, lower case with underscore and parent object type (prefix)_TYPE_(module).</li>
<li>Define instance init function (tfe_text_view_init). Usually you don’t need to do anything.</li>
<li>Define class init function (tfe_text_view_class_init). You don’t need to do anything in this object.</li>
<li>Write function codes you want to add (tfe_text_view_set_file and tfe_text_view_get_file). <code>tv</code> is a pointer to the TfeTextView object instance which is a C-structure declared with the tag _TfeTextView. So, the structure has a member <code>file</code> as a pointer to a GFile instance. <code>tv->file = f</code> is an assignment of <code>f</code> to a member <code>file</code> of the structure pointed by <code>tv</code>. This is an example how to use the extended memory in a child widget.</li>
<li>Write a function to create an instance. Its name is (prefix)_(object)_new. If the parent object function needs parameters, this function also need them. You sometimes might want to add some parameters. It’s your choice. Use g_object_new function to create the instance. The arguments are (prefix)_TYPE_(object), a list to initialize properties and NULL. In this code no property needs to be initialized. And the return value is casted to GtkWidget.</li>
</ul>
<p>This program is not perfect. It has some problems. It will be modified later.</p>
<p>Imagine that you are using this editor. First, you run the editor with arguments. The arguments are filenames. The editor reads the files and shows the window with the text of files in it. Then you edit the text. After you finish editing, you exit the editor. The editor updates files just before the window closes.</p>
<p>GtkWindow emits the “close-request” signal before it closes. We connect the signal and the handler <code>before_close</code>. A handler is a C function. When a function is connected to a certain signal, we call it a handler. The function <code>before_close</code> is invoked when the signal “close-request” is emitted.</p>
<p>The argument <code>win</code> is a GtkApplicationWindow, in which the signal “close-request” is defined, and <code>before_close</code> is the handler. <code>G_CALLBACK</code> cast is necessary for the handler. The program of <code>before_close</code> is as follows.</p>
<divclass="sourceCode"id="cb4"><preclass="sourceCode numberSource C numberLines"><codeclass="sourceCode c"><spanid="cb4-1"><ahref="#cb4-1"></a><spanclass="dt">static</span> gboolean</span>
<p>The numbers on the left of items are line numbers in the source code.</p>
<ul>
<li>15: Gets the number of pages <code>nb</code> has.</li>
<li>16-29: For loop with regard to the index to each pages.</li>
<li>17-19: Gets GtkScrolledWindow, TfeTextView and a pointer to GFile. The pointer was stored when <code>app_open</code> handler had run. It will be shown later.</li>
<li>20-22: Gets GtkTextBuffer and contents. <code>start_iter</code> and <code>end_iter</code> are iterators of the buffer. I don’t want to explain them now because it would take a lot of time. Just remember these lines for the present.</li>
<li>23-27: Writes the contents to the file. If it fails, it outputs an error message.</li>
<li>28: Frees <code>contents</code>.</li>
</ul>
<h2id="source-code-of-tfe1.c">Source code of tfe1.c</h2>
<p>The following is the complete source code of <code>tfe1.c</code>.</p>
<divclass="sourceCode"id="cb5"><preclass="sourceCode numberSource C numberLines"><codeclass="sourceCode c"><spanid="cb5-1"><ahref="#cb5-1"></a><spanclass="pp">#include </span><spanclass="im"><gtk/gtk.h></span></span>
<spanid="cb5-2"><ahref="#cb5-2"></a></span>
<spanid="cb5-3"><ahref="#cb5-3"></a><spanclass="co">/* Define TfeTextView Widget which is the child object of GtkTextView */</span></span>
<spanid="cb5-75"><ahref="#cb5-75"></a> g_print (<spanclass="st">"You need to give filenames as arguments.</span><spanclass="sc">\n</span><spanclass="st">"</span>);</span>
<li>107: Sets the pointer to GFile into TfeTextView. <code>files[i]</code> is a pointer to GFile structure. It will be freed by the system. So you need to copy it. <code>g_file_dup</code> duplicates the given GFile structure.</li>
<li>123: Connects “close-request” signal and <code>before_close</code> handler. The fourth argument is called user data and it is given to the signal handler. So, <code>nb</code> is given to <code>before_close</code> as the second argument.</li>
</ul>
<p>Now compile and run it. There’s a sample file in the directory <code>tfe</code>. Type <code>./a.out taketori.txt</code>. Modify the contents and close the window. Make sure that the file is modified.</p>
<p>Now we got a very simple editor. It’s not smart. We need more features like open, save, saveas, change font and so on. We will add them in the next section and after.</p>