File: sec-comboboxentry.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 (450 lines) | stat: -rw-r--r-- 15,718 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<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>ComboBox with an Entry</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-combobox.html" title="Chapter 13. Combo Boxes">
<link rel="prev" href="combobox-example-simple.html" title="Simple Text Example">
<link rel="next" href="chapter-textview.html" title="Chapter 14. TextView">
</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">ComboBox with an Entry</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="combobox-example-simple.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 13. Combo Boxes</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-textview.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-comboboxentry"></a>ComboBox with an Entry</h2></div></div></div>


<p>A <code class="classname">ComboBox</code> may contain an <code class="classname">Entry</code> widget for entering of arbitrary text, by specifying <code class="literal">true</code> for the constructor's <code class="literal">has_entry</code> parameter.</p>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-comboboxentry-text-column"></a>The text column</h3></div></div></div>


<p>So that the <code class="classname">Entry</code> can interact with the drop-down list of choices, you must specify which of your model columns is the text column, with <code class="methodname">set_entry_text_column()</code>. For instance:
</p>
<pre class="programlisting"><code class="code">m_combo.set_entry_text_column(m_columns.m_col_name);</code></pre>
<p>
When you select a choice from the drop-down menu, the value from this column will be placed in the <code class="classname">Entry</code>.
</p>
</div>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-comboboxentry-model"></a>The entry</h3></div></div></div>


<p>Because the user may enter arbitrary text, an active model row isn't enough to tell us what text the user has entered. Therefore, you should retrieve the <code class="classname">Entry</code> widget with the <code class="methodname">ComboBox::get_entry()</code> method and call <code class="methodname">get_text()</code> on that.
</p>
</div>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-comboboxentry-changes"></a>Responding to changes</h3></div></div></div>


<p>
When the user enters arbitrary text, it may not be enough to connect to the
<code class="literal">changed</code> signal, which is emitted for every typed character.
It is not emitted when the user presses the <span class="keycap"><strong>Enter</strong></span> key.
Pressing the <span class="keycap"><strong>Enter</strong></span> key or moving the keyboard focus to another
widget may signal that the user has finished entering text. To be notified of
these events, connect to the <code class="classname">Entry</code>'s <code class="literal">activate</code>
signal (available since <span class="application">gtkmm</span> 4.8), and add a <code class="classname">Gtk::EventControllerFocus</code>
and connect to its <code class="literal">leave</code> signal, like so
</p>
<pre class="programlisting"><code class="code">auto entry = m_Combo.get_entry();
if (entry)
{
  // Alternatively you can connect to m_Combo.signal_changed().
  entry-&gt;signal_changed().connect(sigc::mem_fun(*this,
    &amp;ExampleWindow::on_entry_changed));
  entry-&gt;signal_activate().connect(sigc::mem_fun(*this,
    &amp;ExampleWindow::on_entry_activate));
  // The Entry shall receive focus-leave events.
  auto controller = Gtk::EventControllerFocus::create();
  m_ConnectionFocusLeave = controller-&gt;signal_leave().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_entry_focus_leave));
  entry-&gt;add_controller(controller);
}</code></pre>
<p>
The <code class="literal">changed</code> signals of <code class="classname">ComboBox</code> and
<code class="classname">Entry</code> are both emitted for every change. It doesn't matter
which one you connect to. But the <code class="classname">EventControllerFocus</code>
must be added to the <code class="classname">Entry</code>.
</p>
</div>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="comboboxentry-example-full"></a>Full Example</h3></div></div></div>


<div class="figure">
<a name="figure-comboboxentry-complex"></a><p class="title"><b>Figure 13.3. ComboBox with Entry</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/comboboxentry_complex.png" alt="ComboBox with Entry"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/combobox/entry_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/window.h&gt;
#include &lt;gtkmm/combobox.h&gt;
#include &lt;gtkmm/liststore.h&gt;
#include &lt;gtkmm/version.h&gt;

#define HAS_SIGNAL_ACTIVATE GTKMM_CHECK_VERSION(4,7,1)

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

protected:
  //Signal handlers:
  void on_entry_changed();
#if HAS_SIGNAL_ACTIVATE
  void on_entry_activate();
#endif
  void on_entry_focus_leave();

  //Signal connection:
  sigc::connection m_ConnectionFocusLeave;

  //Tree model columns:
  class ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
  public:

    ModelColumns()
    { add(m_col_id); add(m_col_name); }

    Gtk::TreeModelColumn&lt;Glib::ustring&gt; m_col_id; //The data to choose - this must be text.
    Gtk::TreeModelColumn&lt;Glib::ustring&gt; m_col_name;
  };

  ModelColumns m_Columns;

  //Child widgets:
  Gtk::ComboBox m_Combo;
  Glib::RefPtr&lt;Gtk::ListStore&gt; m_refTreeModel;
};

#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;gtkmm/eventcontrollerfocus.h&gt;
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow()
: m_Combo(true /* has_entry */)
{
  set_title("ComboBox example");

  //Create the Tree model:
  //m_refTreeModel = Gtk::TreeStore::create(m_Columns);
  m_refTreeModel = Gtk::ListStore::create(m_Columns);
  m_Combo.set_model(m_refTreeModel);

  //Fill the ComboBox's Tree Model:
  auto row = *(m_refTreeModel-&gt;append());
  row[m_Columns.m_col_id] = "1";
  row[m_Columns.m_col_name] = "Billy Bob";
  /*
  auto childrow = *(m_refTreeModel-&gt;append(row.children()));
  childrow[m_Columns.m_col_id] = 11;
  childrow[m_Columns.m_col_name] = "Billy Bob Junior";

  childrow = *(m_refTreeModel-&gt;append(row.children()));
  childrow[m_Columns.m_col_id] = 12;
  childrow[m_Columns.m_col_name] = "Sue Bob";
  */

  row = *(m_refTreeModel-&gt;append());
  row[m_Columns.m_col_id] = "2";
  row[m_Columns.m_col_name] = "Joey Jojo";

  row = *(m_refTreeModel-&gt;append());
  row[m_Columns.m_col_id] = "3";
  row[m_Columns.m_col_name] = "Rob McRoberts";

  /*
  childrow = *(m_refTreeModel-&gt;append(row.children()));
  childrow[m_Columns.m_col_id] = 31;
  childrow[m_Columns.m_col_name] = "Xavier McRoberts";
  */

  //Add the model columns to the Combo (which is a kind of view),
  //rendering them in the default way:
  //This is automatically rendered when we use set_entry_text_column().
  //m_Combo.pack_start(m_Columns.m_col_id, Gtk::PackOptions::EXPAND_WIDGET);
  m_Combo.pack_start(m_Columns.m_col_name);

  m_Combo.set_entry_text_column(m_Columns.m_col_id);
  m_Combo.set_active(1);

  //Add the ComboBox to the window.
  set_child(m_Combo);

  //Connect signal handlers:
  auto entry = m_Combo.get_entry();
  if (entry)
  {
    // Alternatively you can connect to m_Combo.signal_changed().
    entry-&gt;signal_changed().connect(sigc::mem_fun(*this,
      &amp;ExampleWindow::on_entry_changed));
#if HAS_SIGNAL_ACTIVATE
    entry-&gt;signal_activate().connect(sigc::mem_fun(*this,
      &amp;ExampleWindow::on_entry_activate));
#endif
    // The Entry shall receive focus-leave events.
    auto controller = Gtk::EventControllerFocus::create();
    m_ConnectionFocusLeave = controller-&gt;signal_leave().connect(
      sigc::mem_fun(*this, &amp;ExampleWindow::on_entry_focus_leave));
    entry-&gt;add_controller(controller);
  }
  else
    std::cout &lt;&lt; "No Entry ???" &lt;&lt; std::endl;
}

ExampleWindow::~ExampleWindow()
{
  // The focus-leave signal may be emitted while m_Combo is being destructed.
  // The signal handler can generate critical messages, if it's called when
  // m_Combo has been partly destructed.
  m_ConnectionFocusLeave.disconnect();
}

void ExampleWindow::on_entry_changed()
{
  auto entry = m_Combo.get_entry();
  if (entry)
  {
    std::cout &lt;&lt; "on_entry_changed(): Row=" &lt;&lt; m_Combo.get_active_row_number()
      &lt;&lt; ", ID=" &lt;&lt; entry-&gt;get_text() &lt;&lt; std::endl;
  }
}

#if HAS_SIGNAL_ACTIVATE
void ExampleWindow::on_entry_activate()
{
  auto entry = m_Combo.get_entry();
  if (entry)
  {
    std::cout &lt;&lt; "on_entry_activate(): Row=" &lt;&lt; m_Combo.get_active_row_number()
      &lt;&lt; ", ID=" &lt;&lt; entry-&gt;get_text() &lt;&lt; std::endl;
  }
}
#endif

void ExampleWindow::on_entry_focus_leave()
{
  auto entry = m_Combo.get_entry();
  if (entry)
  {
    std::cout &lt;&lt; "on_entry_focus_leave(): Row=" &lt;&lt; m_Combo.get_active_row_number()
      &lt;&lt; ", ID=" &lt;&lt; entry-&gt;get_text() &lt;&lt; std::endl;
  }
}
</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="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="comboboxentry-example-simple"></a>Simple Text Example</h3></div></div></div>


<div class="figure">
<a name="figure-comboboxentry-text"></a><p class="title"><b>Figure 13.4. ComboBoxText with Entry</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/comboboxentry_text.png" alt="ComboBoxText with Entry"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/combobox/entry_text" 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/window.h&gt;
#include &lt;gtkmm/comboboxtext.h&gt;
#include &lt;gtkmm/version.h&gt;

#define HAS_SIGNAL_ACTIVATE GTKMM_CHECK_VERSION(4,7,1)

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

protected:
  //Signal handlers:
  void on_combo_changed();
#if HAS_SIGNAL_ACTIVATE
  void on_entry_activate();
#endif
  void on_entry_focus_leave();

  //Signal connection:
  sigc::connection m_ConnectionFocusLeave;

  //Child widgets:
  Gtk::ComboBoxText m_Combo;
};

#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;gtkmm/eventcontrollerfocus.h&gt;
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow()
: m_Combo(true /* has_entry */)
{
  set_title("ComboBoxText example");

  //Fill the combo:
  m_Combo.append("something");
  m_Combo.append("something else");
  m_Combo.append("something or other");
  m_Combo.set_active(0);

  set_child(m_Combo);

  //Connect signal handlers:
  auto entry = m_Combo.get_entry();
  // Alternatively you can connect to entry-&gt;signal_changed().
  m_Combo.signal_changed().connect(sigc::mem_fun(*this,
    &amp;ExampleWindow::on_combo_changed) );

  if (entry)
  {
#if HAS_SIGNAL_ACTIVATE
    entry-&gt;signal_activate().connect(sigc::mem_fun(*this,
      &amp;ExampleWindow::on_entry_activate));
#endif
    // The Entry shall receive focus-leave events.
    auto controller = Gtk::EventControllerFocus::create();
    m_ConnectionFocusLeave = controller-&gt;signal_leave().connect(
      sigc::mem_fun(*this, &amp;ExampleWindow::on_entry_focus_leave));
    entry-&gt;add_controller(controller);
  }
  else
    std::cout &lt;&lt; "No Entry ???" &lt;&lt; std::endl;

  m_Combo.property_has_frame() = false;
}

ExampleWindow::~ExampleWindow()
{
  // The focus-leave signal may be emitted while m_Combo is being destructed.
  // The signal handler can generate critical messages, if it's called when
  // m_Combo has been partly destructed.
  m_ConnectionFocusLeave.disconnect();
}

void ExampleWindow::on_combo_changed()
{
  std::cout &lt;&lt; "on_combo_changed(): Row=" &lt;&lt; m_Combo.get_active_row_number()
    &lt;&lt; ", Text=" &lt;&lt; m_Combo.get_active_text() &lt;&lt; std::endl;
}

#if HAS_SIGNAL_ACTIVATE
void ExampleWindow::on_entry_activate()
{
  std::cout &lt;&lt; "on_entry_activate(): Row=" &lt;&lt; m_Combo.get_active_row_number()
    &lt;&lt; ", Text=" &lt;&lt; m_Combo.get_active_text() &lt;&lt; std::endl;
}
#endif

void ExampleWindow::on_entry_focus_leave()
{
  std::cout &lt;&lt; "on_entry_focus_leave(): Row=" &lt;&lt; m_Combo.get_active_row_number()
    &lt;&lt; ", Text=" &lt;&lt; m_Combo.get_active_text() &lt;&lt; std::endl;
}
</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>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="combobox-example-simple.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-combobox.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-textview.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Simple Text Example </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 14. TextView</td>
</tr>
</table>
</div>
</body>
</html>