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
|
<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>Examples with a Search 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-dropdown.html" title="Chapter 12. The DropDown Widget">
<link rel="prev" href="sec-dropdown-simple.html" title="Simple String Example">
<link rel="next" href="sec-dropdown-complex.html" title="Complex Example">
</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">Examples with a Search Entry</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-dropdown-simple.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="sec-dropdown-complex.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-search"></a>Examples with a Search Entry</h2></div></div></div>
<p>The dropdown menu may contain an <code class="classname">Entry</code> that allows
to search for items in the list. Call <code class="methodname">set_enable_search()</code>
and <code class="methodname">set_expression()</code>. For instance:
</p>
<pre class="programlisting"><code class="code">m_DropDown.set_enable_search(true);
auto expression = Gtk::ClosureExpression<Glib::ustring>::create(
sigc::mem_fun(*this, &ExampleWindow::get_col_name));
m_DropDown.set_expression(expression);
//-------
Glib::ustring ExampleWindow::get_col_name(const Glib::RefPtr<Glib::ObjectBase>& item)
{
const auto col = std::dynamic_pointer_cast<ModelColumns>(item);
return col ? col->m_col_name : "";
}
</code></pre>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-dropdown-search-string"></a>String Example</h3></div></div></div>
<div class="figure">
<a name="figure-dropdown-search-string"></a><p class="title"><b>Figure 12.2. Search String</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/dropdown_search_string.png" alt="Search String"></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/search_string" 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 <gtkmm/window.h>
#include <gtkmm/dropdown.h>
#include <gtkmm/stringlist.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
// Signal handler:
void on_dropdown_changed();
// Child widget:
Gtk::DropDown m_DropDown;
Glib::RefPtr<Gtk::StringList> m_StringList;
};
#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 <gtkmm/expression.h>
#include <gtkmm/stringobject.h>
#include <iostream>
ExampleWindow::ExampleWindow()
{
set_title("Searchable DropDown example");
set_child(m_DropDown);
// Fill the dropdown:
const std::vector<Glib::ustring> many_times{
"1 minute", "2 minutes", "5 minutes", "10 minutes", "15 minutes", "20 minutes",
"25 minutes", "30 minutes", "35 minutes", "40 minutes", "45 minutes", "50 minutes",
"55 minutes", "1 hour", "2 hours", "3 hours", "5 hours", "6 hours", "7 hours",
"8 hours", "9 hours", "10 hours", "11 hours", "12 hours"
};
m_StringList = Gtk::StringList::create(many_times);
m_DropDown.set_model(m_StringList);
m_DropDown.set_selected(0);
// Show a search entry.
m_DropDown.set_enable_search(true);
auto expression = Gtk::ClosureExpression<Glib::ustring>::create(
[](const Glib::RefPtr<Glib::ObjectBase>& item)->Glib::ustring
{
// An item in a StringList is a StringObject.
const auto string_object = std::dynamic_pointer_cast<Gtk::StringObject>(item);
return string_object ? string_object->get_string() : "";
});
m_DropDown.set_expression(expression);
// Connect signal handler:
m_DropDown.property_selected().signal_changed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_dropdown_changed));
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_dropdown_changed()
{
const auto selected = m_DropDown.get_selected();
std::cout << "DropDown changed: Row=" << selected
<< ", String=" << m_StringList->get_string(selected) << 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 <gtkmm/application.h>
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->make_window_and_run<ExampleWindow>(argc, argv);
}
</code></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-dropdown-search-font"></a>Font Example</h3></div></div></div>
<p>This example uses a <code class="classname">Pango::FontMap</code> as its model.
This is possible because <code class="classname">Pango::FontMap</code> implements
the <code class="classname">Gio::ListModel</code> interface. Of course you can use a
<code class="classname">FontDialogButton</code> instead.
</p>
<div class="figure">
<a name="figure-dropdown-search-font"></a><p class="title"><b>Figure 12.3. Search Font</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/dropdown_search_font.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/search_font" 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 <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
// Signal handler:
void on_dropdown_changed();
Glib::ustring get_family_name(const Glib::RefPtr<Glib::ObjectBase>& item);
// Child widget:
Gtk::DropDown m_DropDown;
};
#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 <pangomm/cairofontmap.h>
#include <iostream>
//#include <typeinfo>
ExampleWindow::ExampleWindow()
{
set_title("Searchable DropDown example");
// Add the DropDown to the window.
set_child(m_DropDown);
// Show a search entry.
m_DropDown.set_enable_search(true);
auto expression = Gtk::ClosureExpression<Glib::ustring>::create(
sigc::mem_fun(*this, &ExampleWindow::get_family_name));
m_DropDown.set_expression(expression);
// Pango::FontMap is a Gio::ListModel.
auto model = Pango::CairoFontMap::get_default();
m_DropDown.set_model(model);
m_DropDown.set_selected(0);
// Connect signal handler.
m_DropDown.property_selected().signal_changed().connect(
sigc::mem_fun(*this, &ExampleWindow::on_dropdown_changed));
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_dropdown_changed()
{
const auto selected = m_DropDown.get_selected();
const auto item = m_DropDown.get_selected_item();
std::cout << "DropDown changed: Row=" << selected
<< ", Font=" << get_family_name(item) << std::endl;
// Pango::FontMap is often a list of items of type Pango::FontFamily.
// From the description of Pango::CairoFontMap: "The actual type of the font map
// will depend on the particular font technology Cairo was compiled to use."
// If get_family_name() does not return a family name, you can add
// #include <typeinfo> and query the type of the items with:
// std::cout << typeid(*item.get()).name() << std::endl;
}
Glib::ustring ExampleWindow::get_family_name(const Glib::RefPtr<Glib::ObjectBase>& item)
{
auto family = std::dynamic_pointer_cast<Pango::FontFamily>(item);
return family ? family->get_name() : "";
}
</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 <gtkmm/application.h>
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->make_window_and_run<ExampleWindow>(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="sec-dropdown-simple.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="sec-dropdown-complex.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Simple String 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"> Complex Example</td>
</tr>
</table>
</div>
</body>
</html>
|