File: sec-dropdown-complex.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 (307 lines) | stat: -rw-r--r-- 11,407 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
<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>Complex Example</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-dropdown.html" title="Chapter 12. The DropDown Widget">
<link rel="prev" href="sec-dropdown-search.html" title="Examples with a Search Entry">
<link rel="next" href="chapter-combobox.html" title="Chapter 13. Combo Boxes">
</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">Complex Example</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-dropdown-search.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 12. The DropDown Widget</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-combobox.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-dropdown-complex"></a>Complex Example</h2></div></div></div>


<p>This is a more complex example with two <code class="classname">SignalListItemFactory</code>
objects and their signal handlers. This example would be simpler without the
checkmark in the dropdown menu.
</p>

<div class="figure">
<a name="figure-dropdown-complex"></a><p class="title"><b>Figure 12.4. Search Font</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/dropdown_complex.png" alt="Search Font"></div>
  </div>
</div>
</div>
<br class="figure-break">

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

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

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  ~ExampleWindow() override;

protected:
  // Signal handlers:
  void on_setup_selected_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_bind_selected_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_setup_list_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_bind_list_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_unbind_list_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_selected_item_changed(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_dropdown_changed();

  void create_model();
  void liststore_add_item(const Glib::ustring&amp; title, const Glib::ustring&amp; icon,
    const Glib::ustring&amp; description);

  // A Gio::ListStore item.
  class ModelColumns : public Glib::Object
  {
  public:
    Glib::ustring m_title;
    Glib::ustring m_icon;
    Glib::ustring m_description;

    static Glib::RefPtr&lt;ModelColumns&gt; create(const Glib::ustring&amp; title,
      const Glib::ustring&amp; icon, const Glib::ustring&amp; description)
    {
      return Glib::make_refptr_for_instance&lt;ModelColumns&gt;(
        new ModelColumns(title, icon, description));
    }

  protected:
    ModelColumns(const Glib::ustring&amp; title, const Glib::ustring&amp; icon,
      const Glib::ustring&amp; description)
    : m_title(title), m_icon(icon), m_description(description)
    {}
  }; // ModelColumns

  // Child widget:
  Gtk::DropDown m_DropDown;

  Glib::RefPtr&lt;Gio::ListStore&lt;ModelColumns&gt;&gt; m_ListStore;
};

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

ExampleWindow::ExampleWindow()
{
  set_title("DropDown example");

  // Add the DropDown to the window.
  set_child(m_DropDown);

  // Factory for presenting the selected item.
  auto factory = Gtk::SignalListItemFactory::create();
  factory-&gt;signal_setup().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_setup_selected_item));
  factory-&gt;signal_bind().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_bind_selected_item));
  m_DropDown.set_factory(factory);

  // Factory for presenting the items in the dropdown list.
  factory = Gtk::SignalListItemFactory::create();
  factory-&gt;signal_setup().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_setup_list_item));
  factory-&gt;signal_bind().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_bind_list_item));
  factory-&gt;signal_unbind().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_unbind_list_item));
  m_DropDown.set_list_factory(factory);

  // Create the model and fill it.
  create_model();
  m_DropDown.set_model(m_ListStore);
  m_DropDown.set_selected(0);

  // Connect signal handler:
  m_DropDown.property_selected().signal_changed().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_dropdown_changed));
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_dropdown_changed()
{
  const auto selected = m_DropDown.get_selected();
  std::cout &lt;&lt; "DropDown changed: Row=" &lt;&lt; selected
    &lt;&lt; ", Title=" &lt;&lt; m_ListStore-&gt;get_item(selected)-&gt;m_title &lt;&lt; std::endl;
}

void ExampleWindow::create_model()
{
  m_ListStore = Gio::ListStore&lt;ModelColumns&gt;::create();

  liststore_add_item("Digital Output", "audio-card-symbolic", "Built-in Audio");
  liststore_add_item("Headphones", "audio-headphones-symbolic", "Built-in Audio");
  liststore_add_item("Digital Output", "audio-card-symbolic", "Thinkpad Tunderbolt 3 Dock USB Audio");
  liststore_add_item("Analog Output", "audio-card-symbolic", "Thinkpad Tunderbolt 3 Dock USB Audio");
}

void ExampleWindow::liststore_add_item(const Glib::ustring&amp; title,
  const Glib::ustring&amp; icon, const Glib::ustring&amp; description)
{
  m_ListStore-&gt;append(ModelColumns::create(title, icon, description));
}

void ExampleWindow::on_setup_selected_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto box = Gtk::make_managed&lt;Gtk::Box&gt;(Gtk::Orientation::HORIZONTAL, 10);
  box-&gt;append(*Gtk::make_managed&lt;Gtk::Image&gt;());
  box-&gt;append(*Gtk::make_managed&lt;Gtk::Label&gt;());
  list_item-&gt;set_child(*box);
}

void ExampleWindow::on_bind_selected_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(list_item-&gt;get_item());
  if (!col)
    return;
  auto box = dynamic_cast&lt;Gtk::Box*&gt;(list_item-&gt;get_child());
  if (!box)
    return;
  auto image = dynamic_cast&lt;Gtk::Image*&gt;(box-&gt;get_first_child());
  if (!image)
    return;
  image-&gt;set_from_icon_name(col-&gt;m_icon);
  auto label = dynamic_cast&lt;Gtk::Label*&gt;(image-&gt;get_next_sibling());
  if (!label)
    return;
  label-&gt;set_text(col-&gt;m_title);
}

void ExampleWindow::on_setup_list_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto hbox = Gtk::make_managed&lt;Gtk::Box&gt;(Gtk::Orientation::HORIZONTAL, 10);
  auto vbox = Gtk::make_managed&lt;Gtk::Box&gt;(Gtk::Orientation::VERTICAL, 2);

  hbox-&gt;append(*Gtk::make_managed&lt;Gtk::Image&gt;());
  hbox-&gt;append(*vbox);
  auto title = Gtk::make_managed&lt;Gtk::Label&gt;();
  title-&gt;set_xalign(0.0);
  vbox-&gt;append(*title);
  auto description = Gtk::make_managed&lt;Gtk::Label&gt;();
  description-&gt;set_xalign(0.0);
  description-&gt;add_css_class("dim-label");
  vbox-&gt;append(*description);
  auto checkmark = Gtk::make_managed&lt;Gtk::Image&gt;();
  checkmark-&gt;set_from_icon_name("object-select-symbolic");
  checkmark-&gt;set_visible(false);
  hbox-&gt;append(*checkmark);

  list_item-&gt;set_child(*hbox);
}

void ExampleWindow::on_bind_list_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(list_item-&gt;get_item());
  if (!col)
    return;
  auto hbox = dynamic_cast&lt;Gtk::Box*&gt;(list_item-&gt;get_child());
  if (!hbox)
    return;
  auto image = dynamic_cast&lt;Gtk::Image*&gt;(hbox-&gt;get_first_child());
  if (!image)
    return;
  image-&gt;set_from_icon_name(col-&gt;m_icon);
  auto vbox = dynamic_cast&lt;Gtk::Box*&gt;(image-&gt;get_next_sibling());
  if (!vbox)
    return;
  auto title = dynamic_cast&lt;Gtk::Label*&gt;(vbox-&gt;get_first_child());
  if (!title)
    return;
  title-&gt;set_text(col-&gt;m_title);
  auto description = dynamic_cast&lt;Gtk::Label*&gt;(title-&gt;get_next_sibling());
  if (!description)
    return;
  description-&gt;set_text(col-&gt;m_description);

  auto connection = m_DropDown.property_selected_item().signal_changed().connect(
    sigc::bind(sigc::mem_fun(*this, &amp;ExampleWindow::on_selected_item_changed),
    list_item));
  list_item-&gt;set_data("connection", new sigc::connection(connection),
    Glib::destroy_notify_delete&lt;sigc::connection&gt;);
  on_selected_item_changed(list_item);
}

void ExampleWindow::on_unbind_list_item(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  if (auto connection = static_cast&lt;sigc::connection*&gt;(list_item-&gt;get_data("connection")))
  {
    connection-&gt;disconnect();
    list_item-&gt;set_data("connection", nullptr);
  }
}

void ExampleWindow::on_selected_item_changed(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto hbox = dynamic_cast&lt;Gtk::Box*&gt;(list_item-&gt;get_child());
  if (!hbox)
    return;
  auto checkmark = dynamic_cast&lt;Gtk::Image*&gt;(hbox-&gt;get_last_child());
  if (!checkmark)
    return;
  checkmark-&gt;set_visible(m_DropDown.get_selected_item() == list_item-&gt;get_item());
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;gtkmm/application.h&gt;

int main(int argc, char* argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  // Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv);
}
</code></pre>

</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-dropdown-search.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-dropdown.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-combobox.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Examples with a Search Entry </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 13. Combo Boxes</td>
</tr>
</table>
</div>
</body>
</html>