File: sec-builder-using-derived-widgets.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (408 lines) | stat: -rw-r--r-- 15,737 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<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>Using derived widgets</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-builder.html" title="Chapter 26. Gtk::Builder">
<link rel="prev" href="sec-builder-accessing-widgets.html" title="Accessing widgets">
<link rel="next" href="chapter-internationalization.html" title="Chapter 27. Internationalization and Localization">
</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">Using derived widgets</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-builder-accessing-widgets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 26. Gtk::Builder</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-internationalization.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-builder-using-derived-widgets"></a>Using derived widgets</h2></div></div></div>


<p>
You can use <span class="application">Cambalache</span> and
<code class="classname">Gtk::Builder</code> to layout your own custom widgets
derived from <span class="application">gtkmm</span> widget classes. This keeps your code organized and
encapsulated, separating declarative presentation from business logic, avoiding
having most of your source just be setting properties and packing in containers.
</p>

<p>Use <code class="methodname">Gtk::Builder::get_widget_derived()</code> like so:
</p>
<pre class="programlisting"><code class="code">auto pDialog = Gtk::Builder::get_widget_derived&lt;DerivedDialog&gt;(builder, "DialogDerived");
</code></pre>

<p>
Your derived class must have a constructor that takes a pointer to the
underlying C type, and the <code class="classname">Gtk::Builder</code> instance.
All relevant classes of <span class="application">gtkmm</span> typedef their underlying C type as
<code class="classname">BaseObjectType</code> (<code class="classname">Gtk::Window</code>
typedefs <code class="classname">BaseObjectType</code> as <span class="type">GtkWindow</span>, for instance).
</p>
<p>
You must call the base class's constructor in the initialization list, providing the C pointer. For
instance,
</p>
<pre class="programlisting"><code class="code">DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; builder)
: Gtk::Window(cobject)
{
}
</code></pre>

<p>
You could then encapsulate the manipulation of the child widgets in the
constructor of the derived class, maybe using <code class="methodname">get_widget()</code>
or <code class="methodname">get_widget_derived()</code> again. For instance,
</p>
<pre class="programlisting"><code class="code">DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; builder)
: Gtk::Window(cobject),
  m_builder(builder),
  // Get the GtkBuilder-instantiated Button, and connect a signal handler:
  m_pButton(m_builder-&gt;get_widget&lt;Gtk::Button&gt;("quit_button"))
{
  if (m_pButton)
  {
    m_pButton-&gt;signal_clicked().connect( sigc::mem_fun(*this, &amp;DerivedDialog::on_button_quit) );
  }
}
</code></pre>

<p>
It's possible to pass additional arguments from
<code class="methodname">get_widget_derived()</code> to the constructor of the derived
widget. For instance, this call to <code class="methodname">get_widget_derived()</code>
</p>
<pre class="programlisting"><code class="code">auto pDialog = Gtk::Builder::get_widget_derived&lt;DerivedDialog&gt;(builder, "DialogDerived", true);
</code></pre>
<p>can invoke this constructor
</p>
<pre class="programlisting"><code class="code">DerivedDialog::DerivedDialog(BaseObjectType* cobject,
  const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; builder, bool warning)
: Gtk::Window(cobject),
  m_builder(builder),
  m_pButton(m_builder-&gt;get_widget&lt;Gtk::Button&gt;("quit_button"))
{
  // ....
}
</code></pre>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-builder-and-property"></a>Gtk::Builder and Glib::Property</h3></div></div></div>


<p>
If your derived widget uses <code class="classname">Glib::Property</code>, it becomes slightly
more complicated. A derived widget that contains <code class="classname">Glib::Property</code>
members must be registered with its own name in the <span class="type">GType</span> system.
It must be registered before any of the <code class="methodname">create_from_*()</code> or
<code class="methodname">add_from_*()</code> methods are called, meaning that you may have
to create an instance of your derived widget just to have its class registered.
Your derived widget must have a constructor that has the parameters required by
<code class="methodname">get_widget_derived()</code> and calls the <code class="classname">Glib::ObjectBase</code>
constructor to register the <span class="type">GType</span>.
</p>
<pre class="programlisting"><code class="code">DerivedButton::DerivedButton(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; builder)
: Glib::ObjectBase("MyButton"), // The GType name will be gtkmm__CustomObject_MyButton.
  Gtk::Button(cobject),
  prop_ustring(*this, "button-ustring"),
  prop_int(*this, "button-int", 10)
{
  // ....
}
</code></pre>
<p>
It is possible also to specify properties of derived widgets, declared in C++
using <span class="application">gtkmm</span>, within <code class="filename">.ui</code> files and load/set
these using <code class="classname">Gtk::Builder</code>. See the documentation of
<code class="classname">Gtk::Builder</code> for more details on how to achieve this.
<span class="application">Cambalache</span> won’t recognize such properties as-is,
but you should be able to add a few lines of hand-coded XML to the XML code
generated by <span class="application">Cambalache</span>.
</p>
</div>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="builder-example-derived"></a>Example</h3></div></div></div>


<p>
This example shows how to load a <code class="filename">.ui</code> file
at runtime and access the widgets via derived classes.
</p>

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/builder/derived" target="_top">Source Code</a></p>

<p>File: <code class="filename">derivedbutton.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_DERIVED_BUTTON_H
#define GTKMM_EXAMPLE_DERIVED_BUTTON_H

#include &lt;gtkmm.h&gt;

class DerivedButton : public Gtk::Button
{
public:
  DerivedButton();
  DerivedButton(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; refBuilder);
  virtual ~DerivedButton();

  // Provide proxies for the properties. The proxy allows you to connect to
  // the 'changed' signal, etc.
  Glib::PropertyProxy&lt;Glib::ustring&gt; property_ustring() { return prop_ustring.get_proxy(); }
  Glib::PropertyProxy&lt;int&gt; property_int() { return prop_int.get_proxy(); }

private:
  Glib::Property&lt;Glib::ustring&gt; prop_ustring;
  Glib::Property&lt;int&gt; prop_int;
};

#endif //GTKMM_EXAMPLE_DERIVED_BUTTON_H
</code></pre>
<p>File: <code class="filename">deriveddialog.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_DERIVED_DIALOG_H
#define GTKMM_EXAMPLE_DERIVED_DIALOG_H

#include &lt;gtkmm.h&gt;
#include "derivedbutton.h"

class DerivedDialog : public Gtk::Window
{
public:
  DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; refBuilder);
  DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; refBuilder,
    bool is_glad);
  ~DerivedDialog() override;

protected:
  //Signal handlers:
  void on_button_quit();

  Glib::RefPtr&lt;Gtk::Builder&gt; m_refBuilder;
  DerivedButton* m_pButton;
};

#endif //GTKMM_EXAMPLE_DERIVED_DIALOG_H
</code></pre>
<p>File: <code class="filename">derivedbutton.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "derivedbutton.h"
#include &lt;iostream&gt;

// For creating a dummy object in main.cc.
DerivedButton::DerivedButton()
: Glib::ObjectBase("MyButton"),
  prop_ustring(*this, "button-ustring"),
  prop_int(*this, "button-int", 10)
{
}

DerivedButton::DerivedButton(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; /* refBuilder */)
: // To register custom properties, you must register a custom GType.  If
  // you don't know what that means, don't worry, just remember to add
  // this Glib::ObjectBase constructor call to your class' constructor.
  // The GType name will be gtkmm__CustomObject_MyButton.
  Glib::ObjectBase("MyButton"),
  Gtk::Button(cobject),
  // register the properties with the object and give them names
  prop_ustring(*this, "button-ustring"),
  // this one has a default value
  prop_int(*this, "button-int", 10)
{
  // Register some handlers that will be called when the values of the
  // specified parameters are changed.
  property_ustring().signal_changed().connect(
    [](){ std::cout &lt;&lt; "- ustring property changed!" &lt;&lt; std::endl; }
  );
  property_int().signal_changed().connect(
    [](){ std::cout &lt;&lt; "- int property changed!" &lt;&lt; std::endl; }
  );
}

DerivedButton::~DerivedButton()
{
}
</code></pre>
<p>File: <code class="filename">deriveddialog.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "deriveddialog.h"
#include &lt;iostream&gt;

DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; refBuilder)
: Gtk::Window(cobject),
  m_refBuilder(refBuilder),
  m_pButton(nullptr)
{
  // Get the GtkBuilder-instantiated Button, and connect a signal handler:
  m_pButton = Gtk::Builder::get_widget_derived&lt;DerivedButton&gt;(m_refBuilder, "quit_button");
  if (m_pButton)
  {
    m_pButton-&gt;signal_clicked().connect( sigc::mem_fun(*this, &amp;DerivedDialog::on_button_quit) );
    std::cout &lt;&lt; "ustring, int: " &lt;&lt; m_pButton-&gt;property_ustring()
              &lt;&lt; ", " &lt;&lt; m_pButton-&gt;property_int() &lt;&lt; std::endl;
    m_pButton-&gt;property_int() = 99;
    std::cout &lt;&lt; "ustring, int: " &lt;&lt; m_pButton-&gt;property_ustring()
              &lt;&lt; ", " &lt;&lt; m_pButton-&gt;property_int() &lt;&lt; std::endl;
  }
}

// The first two parameters are mandatory in a constructor that will be called
// from Gtk::Builder::get_widget_derived().
// Additional parameters, if any, correspond to additional arguments in the call
// to Gtk::Builder::get_widget_derived().
DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr&lt;Gtk::Builder&gt;&amp; refBuilder,
  bool is_glad)
: DerivedDialog(cobject, refBuilder) // Delegate to the other constructor
{
  // Show an icon.
  auto content_area = refBuilder-&gt;get_widget&lt;Gtk::Box&gt;("dialog-content_area");
  if (!content_area)
  {
    std::cerr &lt;&lt; "Could not get the content area" &lt;&lt; std::endl;
    return;
  }
  auto pImage = Gtk::make_managed&lt;Gtk::Image&gt;();
  pImage-&gt;set_from_icon_name(is_glad ? "face-smile" : "face-sad");
  pImage-&gt;set_icon_size(Gtk::IconSize::LARGE);
  pImage-&gt;set_expand();
  content_area-&gt;append(*pImage);
}

DerivedDialog::~DerivedDialog()
{
}

void DerivedDialog::on_button_quit()
{
  set_visible(false); // set_visible(false) will cause Gtk::Application::run() to end.
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "deriveddialog.h"
#include &lt;iostream&gt;
#include &lt;cstring&gt;

namespace
{
bool show_icon = false;
bool is_glad = true;

DerivedDialog* pDialog = nullptr;
Glib::RefPtr&lt;Gtk::Application&gt; app;

void on_app_activate()
{
  // Create a dummy instance before the call to refBuilder-&gt;add_from_file().
  // This creation registers DerivedButton's class in the GType system.
  // This is necessary because DerivedButton contains user-defined properties
  // (Glib::Property) and is created by Gtk::Builder.
  static_cast&lt;void&gt;(DerivedButton());

  // Load the GtkBuilder file and instantiate its widgets:
  auto refBuilder = Gtk::Builder::create();
  try
  {
    refBuilder-&gt;add_from_file("derived.ui");
  }
  catch(const Glib::FileError&amp; ex)
  {
    std::cerr &lt;&lt; "FileError: " &lt;&lt; ex.what() &lt;&lt; std::endl;
    return;
  }
  catch(const Glib::MarkupError&amp; ex)
  {
    std::cerr &lt;&lt; "MarkupError: " &lt;&lt; ex.what() &lt;&lt; std::endl;
    return;
  }
  catch(const Gtk::BuilderError&amp; ex)
  {
    std::cerr &lt;&lt; "BuilderError: " &lt;&lt; ex.what() &lt;&lt; std::endl;
    return;
  }

  // Get the GtkBuilder-instantiated dialog:
  if (show_icon)
    pDialog = Gtk::Builder::get_widget_derived&lt;DerivedDialog&gt;(refBuilder, "DialogDerived", is_glad);
  else
    pDialog = Gtk::Builder::get_widget_derived&lt;DerivedDialog&gt;(refBuilder, "DialogDerived");

  if (!pDialog)
  {
    std::cerr &lt;&lt; "Could not get the dialog" &lt;&lt; std::endl;
    return;
  }

  // It's not possible to delete widgets after app-&gt;run() has returned.
  // Delete the dialog with its child widgets before app-&gt;run() returns.
  pDialog-&gt;signal_hide().connect([] () { delete pDialog; });

  app-&gt;add_window(*pDialog);
  pDialog-&gt;set_visible(true);
}
} // anonymous namespace

int main(int argc, char** argv)
{
  int argc1 = argc;
  if (argc &gt; 1)
  {
    if (std::strcmp(argv[1], "--glad") == 0)
    {
      show_icon = true;
      is_glad = true;
      argc1 = 1; // Don't give the command line arguments to Gtk::Application.
    }
    else if (std::strcmp(argv[1], "--sad") == 0)
    {
      show_icon = true;
      is_glad = false;
      argc1 = 1; // Don't give the command line arguments to Gtk::Application.
    }
  }

  app = Gtk::Application::create("org.gtkmm.example");

  // Instantiate a dialog when the application has been activated.
  // This can only be done after the application has been registered.
  // It's possible to call app-&gt;register_application() explicitly, but
  // usually it's easier to let app-&gt;run() do it for you.
  app-&gt;signal_activate().connect([] () { on_app_activate(); });

  return app-&gt;run(argc1, argv);
}
</code></pre>


</div>

</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-builder-accessing-widgets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-builder.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-internationalization.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Accessing widgets </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"> Chapter 27. Internationalization and Localization</td>
</tr>
</table>
</div>
</body>
</html>