<p>The GtkTextView, GtkTextBuffer and GtkScrolledWindow widgets have given us a minimum editor in the previous section. We will now add a function to read a file and rework the program into a file viewer. There are many ways to implement the function and because this is a tutorial for beginners, we’ll take the easiest one.</p>
<p>When the program starts, we will give the filename to open as an argument.</p>
<pre><code>$ ./a.out filename</code></pre>
<p>It will open the file and insert its contents into the GtkTextBuffer.</p>
<p>To do this, we need to know how GtkApplication (or GApplication) recognizes arguments. This is described in the <ahref="https://docs.gtk.org/gio/class.Application.html">GIO API Reference, Application</a>.</p>
<p>When GtkApplication is created, a flag (with the type GApplicationFlags) is provided as an argument.</p>
<p>This tutorial explains only two flags, <code>G_APPLICATION_FLAGS_NONE</code> and <code>G_APPLICATION_HANDLES_OPEN</code>. If you want to handle command line arguments, the <code>G_APPLICATION_HANDLES_COMMAND_LINE</code> flag is what you need. How to use the new application method is described in <ahref="https://docs.gtk.org/gio/method.Application.run.html">GIO API Reference, g_application_run</a>, and the flag is described in the <ahref="https://docs.gtk.org/gio/flags.ApplicationFlags.html">GIO API Reference, ApplicationFlags</a>.</p>
<pre><code>GApplicationFlags' Members
G_APPLICATION_FLAGS_NONE Default. (No argument allowed)
... ... ...
G_APPLICATION_HANDLES_OPEN This application handles opening files (in the primary instance).
... ... ...</code></pre>
<p>There are ten flags in total, but we only need two of them so far. We’ve already used <code>G_APPLICATION_FLAGS_NONE</code>, as it is the simplest option, and no arguments are allowed. If you provide arguments when running the application, an error will occur.</p>
<p>The flag <code>G_APPLICATION_HANDLES_OPEN</code> is the second simplest option. It allows arguments but only files. The application assumes all the arguments are filenames and we will use this flag when creating our GtkApplication.</p>
<spanid="cb6-5"><ahref="#cb6-5"></a> g_print (<spanclass="st">"You need a filename argument.</span><spanclass="sc">\n</span><spanclass="st">"</span>);</span>
<p>Let’s explain how the program <code>tfv3.c</code> works. First, the function <code>main</code> has only two changes from the previous version.</p>
<ul>
<li><code>G_APPLICATION_FLAGS_NONE</code> is replaced by <code>G_APPLICATION_HANDLES_OPEN</code>; and</li>
<li><code>g_signal_connect (app, "open", G_CALLBACK (on_open), NULL)</code> is added.</li>
</ul>
<p>Next, the handler <code>app_activate</code> is added and is very simple. It just outputs the error message and the application quits immediately because no window is created.</p>
<p>The main functionality is the in the handler <code>app_open</code>. It</p>
<ul>
<li>Creates GtkApplicationWindow, GtkScrolledWindow, GtkTextView and GtkTextBuffer and connects them together;</li>
<li>Sets wrap mode to <code>GTK_WRAP_WORD_CHAR</code> in GtktextView;</li>
<li>Sets GtkTextView to non-editable because the program isn’t an editor but only a viewer;</li>
<li>Reads the file and inserts the text into GtkTextBuffer (this will be explained in detail later); and</li>
<li>If the file is not opened then outputs an error message and destroys the window. This makes the application quit.</li>
</ul>
<p>The following is the important file reading part of the program and is shown again below.</p>
<p>The function <code>g_file_load_contents</code> loads the file contents into a buffer, which is automatically allocated and sets <code>contents</code> to point that buffer. The length of the buffer is set to <code>length</code>. It returns <code>TRUE</code> if the file’s contents are successfully loaded and <code>FALSE</code> if an error occurs.</p>
<p>If this function succeeds, it inserts the contents into GtkTextBuffer, frees the buffer pointed by <code>contents</code>, sets the title of the window, frees the memories pointed by <code>filename</code> and then shows the window. If it fails, it outputs an error message and destroys the window, causing the program to quit.</p>
<h2id="gtknotebook">GtkNotebook</h2>
<p>GtkNotebook is a container widget that uses tabs and contains multiple children. The child that is displayed depends on which tab has been selected.</p>
<p>Looking at the screenshots above, the left one is the window at the startup. It shows the file <code>pr1.c</code> and the filename is in the left tab. After clicking on the right tab, the contents of the file <code>tfv1.c</code> are shown instead. This is shown in the right screenshot.</p>
<p>The GtkNotebook widget is inserted as a child of GtkApplicationWindow and contains a GtkScrolledWindow for each file that is being displayed. The code to do this is given in <code>tfv4.c</code> and is:</p>
<divclass="sourceCode"id="cb9"><preclass="sourceCode numberSource C numberLines"><codeclass="sourceCode c"><spanid="cb9-1"><ahref="#cb9-1"></a><spanclass="pp">#include </span><spanclass="im"><gtk/gtk.h></span></span>
<spanid="cb9-5"><ahref="#cb9-5"></a> g_print (<spanclass="st">"You need a filename argument.</span><spanclass="sc">\n</span><spanclass="st">"</span>);</span>
<p>Most of the changes are in the function <code>app_open</code>. The numbers at the left of the following items are line numbers in the source code.</p>
<ul>
<li>11-13: Variables <code>nb</code>, <code>lab</code> and <code>nbp</code> are defined and will point to a new GtkNotebook, GtkLabel and GtkNotebookPage widget respectively.</li>
<li>23: The window’s title is set to “file viewer”.</li>
<li>25: The size of the window is set to maximum because a big window is appropriate for file viewers.</li>
<li>27-28 GtkNotebook is created and inserted to the GtkApplicationWindow as a child.</li>
<li>30-59 For-loop. Each loop corresponds to a filename argument, and <code>files[i]</code> is GFile object containing the i-th argument.</li>
<li>32-37 GtkScrollledWindow, GtkTextView are created and GtkTextBuffer found from the new GtkTextView. GtkTextView is connected to GtkScrolledWindow as a child. Each file gets their own copy of these widgets, so they are created inside the for-loop.</li>
<li>39-40 inserts the contents of the file into GtkTextBuffer and frees the memory pointed by <code>contents</code>.</li>
<li>41-43: Gets the filename and creates GtkLabel with the filename and then frees <code>filename</code>.</li>
<li>44-45: If <code>filename</code> is NULL, creates GtkLabel with the empty string.</li>
<li>46: Appends GtkScrolledWindow as a page, with the tab GtkLabel, to GtkNotebook. At this time a GtkNoteBookPage widget is created automatically. The GtkScrolledWindow widget is connected to the GtkNotebookPage. Therefore, the structure is like this:</li>
<li>47: Gets GtkNotebookPage widget and sets <code>nbp</code> to point to this GtkNotebookPage.</li>
<li>48: GtkNotebookPage has a property set called “tab-expand”. If it is set to TRUE then the tab expands horizontally as long as possible. If it is FALSE, then the width of the tab is determined by the size of the label. <code>g_object_set</code> is a general function to set properties in objects. See <ahref="https://docs.gtk.org/gobject/method.Object.set.html">GObject API Reference, g_object_set</a>.</li>
<li>49-51: If the file cannot be read, “No such file” message is displayed and the <code>filename</code> buffer is freed.</li>
<li>52-53: If <code>filename</code> is NULL, the “No valid file is given” message is outputted.</li>
<li>55-58: If at least one file was read, then the number of GtkNotebookPage is greater than zero. If it’s true, it shows the window. If it’s false, it destroys the window, which causes the program to quit.</li>