1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Populating the window</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-building-applications.html" title="Chapter 31. Building applications">
<link rel="prev" href="chapter-building-applications.html" title="Chapter 31. Building applications">
<link rel="next" href="sec-buildapp-opening-files.html" title="Opening files">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Populating the window</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="chapter-building-applications.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 31. Building applications</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-buildapp-opening-files.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-buildapp-populating-window"></a>Populating the window</h2></div></div></div>
<p>
In this step, we use a <code class="classname">Gtk::Builder</code> instance to associate a
<code class="classname">Gtk::Builder</code> ui file with our application window class.
</p>
<p>
Our simple ui file gives the window a title, and puts a <code class="classname">Gtk::Stack</code>
widget as the main content.
</p>
<p>
To make use of this file in our application, we revisit our
<code class="classname">Gtk::ApplicationWindow</code> subclass, and call
<code class="methodname">Gtk::Builder::create_from_resource()</code> and
<code class="methodname">Gtk::Builder::get_widget_derived()</code> from the
<code class="methodname">ExampleAppWindow::create()</code> method to get an instance of
our subclassed <code class="classname">Gtk::ApplicationWindow</code>. See the
<a class="link" href="sec-builder-using-derived-widgets.html" title="Using derived widgets">Using derived widgets</a> section
for more information about <code class="methodname">get_widget_derived()</code>.
</p>
<p>
You may have noticed that we use the <code class="methodname">_from_resource()</code> variant
of the method that reads the ui file. Now we need to use <span class="application">GLib</span>'s
resource functionality to include the ui file in the binary. This is commonly done by
listing all resources in a .gresource.xml file.
This file has to be converted into a C source file that will be compiled and linked
into the application together with the other source files. To do so, we use the
<span class="application">glib-compile-resources</span> utility:
</p>
<pre class="screen">$ glib-compile-resources --target=resources.c --generate-source exampleapp.gresource.xml</pre>
<p>
The <a class="link" href="sec-gio-resource.html" title="Gio::Resource and glib-compile-resources">Gio::Resource and glib-compile-resources</a>
section contains more information about resource files. If you build with Meson,
use the <code class="function">compile_resources()</code> function in Meson's
<a class="ulink" href="https://mesonbuild.com/Gnome-module.html" target="_top">GNOME module</a>.
</p>
<p>
Our application now looks like this:
</p>
<div class="figure">
<a name="figure-buildapp-populating-window"></a><p class="title"><b>Figure 31.2. Populating the window</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/buildapp_populating_window.png" alt="Populating the window"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/buildapp/step2" target="_top">Source Code</a></p>
<p>File: <code class="filename">exampleapplication.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "../step1/exampleapplication.h"
// Equal to the corresponding file in step1
</code></pre>
<p>File: <code class="filename">exampleappwindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEAPPWINDOW_H_
#define GTKMM_EXAMPLEAPPWINDOW_H_
#include <gtkmm.h>
class ExampleAppWindow : public Gtk::ApplicationWindow
{
public:
ExampleAppWindow(BaseObjectType* cobject,
const Glib::RefPtr<Gtk::Builder>& refBuilder);
static ExampleAppWindow* create();
void open_file_view(const Glib::RefPtr<Gio::File>& file);
protected:
Glib::RefPtr<Gtk::Builder> m_refBuilder;
};
#endif /* GTKMM_EXAMPLEAPPWINDOW_H */
</code></pre>
<p>File: <code class="filename">exampleapplication.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "exampleapplication.h"
#include "exampleappwindow.h"
#include <iostream>
#include <exception>
ExampleApplication::ExampleApplication()
: Gtk::Application("org.gtkmm.examples.application", Gio::Application::Flags::HANDLES_OPEN)
{
}
Glib::RefPtr<ExampleApplication> ExampleApplication::create()
{
return Glib::make_refptr_for_instance<ExampleApplication>(new ExampleApplication());
}
ExampleAppWindow* ExampleApplication::create_appwindow()
{
auto appwindow = ExampleAppWindow::create();
// Make sure that the application runs for as long this window is still open.
add_window(*appwindow);
// A window can be added to an application with Gtk::Application::add_window()
// or Gtk::Window::set_application(). When all added windows have been hidden
// or removed, the application stops running (Gtk::Application::run() returns()),
// unless Gio::Application::hold() has been called.
// Delete the window when it is hidden.
appwindow->signal_hide().connect([appwindow](){ delete appwindow; });
return appwindow;
}
void ExampleApplication::on_activate()
{
try
{
// The application has been started, so let's show a window.
auto appwindow = create_appwindow();
appwindow->present();
}
// If create_appwindow() throws an exception (perhaps from Gtk::Builder),
// no window has been created, no window has been added to the application,
// and therefore the application will stop running.
catch (const Glib::Error& ex)
{
std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl;
}
catch (const std::exception& ex)
{
std::cerr << "ExampleApplication::on_activate(): " << ex.what() << std::endl;
}
}
void ExampleApplication::on_open(const Gio::Application::type_vec_files& files,
const Glib::ustring& /* hint */)
{
// The application has been asked to open some files,
// so let's open a new view for each one.
ExampleAppWindow* appwindow = nullptr;
auto windows = get_windows();
if (windows.size() > 0)
appwindow = dynamic_cast<ExampleAppWindow*>(windows[0]);
try
{
if (!appwindow)
appwindow = create_appwindow();
for (const auto& file : files)
appwindow->open_file_view(file);
appwindow->present();
}
catch (const Glib::Error& ex)
{
std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl;
}
catch (const std::exception& ex)
{
std::cerr << "ExampleApplication::on_open(): " << ex.what() << std::endl;
}
}
</code></pre>
<p>File: <code class="filename">exampleappwindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "exampleappwindow.h"
#include <stdexcept>
ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
const Glib::RefPtr<Gtk::Builder>& refBuilder)
: Gtk::ApplicationWindow(cobject),
m_refBuilder(refBuilder)
{
}
//static
ExampleAppWindow* ExampleAppWindow::create()
{
// Load the Builder file and instantiate its widgets.
auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
if (!window)
throw std::runtime_error("No \"app_window\" object in window.ui");
return window;
}
void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& /* file */)
{
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "../step1/main.cc"
// Equal to the corresponding file in step1
</code></pre>
<p>File: <code class="filename">exampleapp.gresource.xml</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code"><?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gtkmm/exampleapp">
<file preprocess="xml-stripblanks">window.ui</file>
</gresource>
</gresources>
</code></pre>
<p>File: <code class="filename">window.ui</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code"><?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkApplicationWindow" id="app_window">
<property name="title" translatable="yes">Example Application</property>
<property name="default-width">600</property>
<property name="default-height">400</property>
<property name="hide-on-close">True</property>
<child>
<object class="GtkBox" id="content_box">
<property name="orientation">vertical</property>
<child>
<object class="GtkStack" id="stack"/>
</child>
</object>
</child>
</object>
</interface>
</code></pre>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="chapter-building-applications.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-building-applications.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-buildapp-opening-files.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 31. Building applications </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Opening files</td>
</tr>
</table>
</div>
</body>
</html>
|