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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
<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>Adding a side bar</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="sec-buildapp-search-bar.html" title="Adding a search bar">
<link rel="next" href="sec-buildapp-properties.html" title="Properties">
</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">Adding a side bar</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-buildapp-search-bar.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-properties.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-side-bar"></a>Adding a side bar</h2></div></div></div>
<p>
As another piece of functionality, we are adding a sidebar, which demonstrates
<code class="classname">Gtk::Revealer</code> and <code class="classname">Gtk::ListBox</code>.
The new widgets are added in the <code class="filename">window.ui</code> file.
</p>
<p>
The code to populate the sidebar with buttons for the words found in each
file is a little too involved to go into here. But we'll look at the code
to add a checkbutton for the new feature to the menu. A menu item is added to
the ui file <code class="filename">gears_menu.ui</code>.
</p>
<p>
To connect the menu item to the new <code class="literal">show-words</code> setting, we use a
<code class="classname">Gio::Action</code> corresponding to the given <code class="classname">Gio::Settings</code>
key. In <code class="classname">ExampleAppWindow</code>'s constructor:
</p>
<pre class="programlisting"><code class="code">// Connect the menu to the MenuButton m_gears, and bind the show-words setting
// to the win.show-words action and the "Words" menu item.
// (The connection between action and menu item is specified in gears_menu.ui.)
auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
m_gears->set_menu_model(menu);
add_action(m_settings->create_action("show-words"));
</code></pre>
<p>
What our application looks like now:
</p>
<div class="figure">
<a name="figure-buildapp-side-bar"></a><p class="title"><b>Figure 31.7. Adding a side bar</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/buildapp_side_bar.png" alt="Adding a side bar"></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/step7" 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 "../step4/exampleapplication.h"
// Equal to the corresponding file in step4
</code></pre>
<p>File: <code class="filename">exampleappprefs.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "../step5/exampleappprefs.h"
// Equal to the corresponding file in step5
</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>
#define HAS_SEARCH_ENTRY2 GTKMM_CHECK_VERSION(4,13,2)
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:
// Signal handlers
void on_search_text_changed();
void on_visible_child_changed();
void on_find_word(const Gtk::Button* button);
void on_reveal_child_changed();
void update_words();
Glib::RefPtr<Gtk::Builder> m_refBuilder;
Glib::RefPtr<Gio::Settings> m_settings;
Gtk::Stack* m_stack {nullptr};
Gtk::ToggleButton* m_search {nullptr};
Gtk::SearchBar* m_searchbar {nullptr};
#if HAS_SEARCH_ENTRY2
Gtk::SearchEntry2* m_searchentry {nullptr};
#else
Gtk::SearchEntry* m_searchentry {nullptr};
#endif
Gtk::MenuButton* m_gears {nullptr};
Gtk::Revealer* m_sidebar {nullptr};
Gtk::ListBox* m_words {nullptr};
Glib::RefPtr<Glib::Binding> m_prop_binding;
};
#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 "../step5/exampleapplication.cc"
// Equal to the corresponding file in step5
</code></pre>
<p>File: <code class="filename">exampleappprefs.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "../step5/exampleappprefs.cc"
// Equal to the corresponding file in step5
</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 <iostream>
#include <stdexcept>
#include <set>
ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
const Glib::RefPtr<Gtk::Builder>& refBuilder)
: Gtk::ApplicationWindow(cobject),
m_refBuilder(refBuilder)
{
// Get widgets from the Gtk::Builder file.
m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
if (!m_stack)
throw std::runtime_error("No \"stack\" object in window.ui");
m_search = m_refBuilder->get_widget<Gtk::ToggleButton>("search");
if (!m_search)
throw std::runtime_error("No \"search\" object in window.ui");
m_searchbar = m_refBuilder->get_widget<Gtk::SearchBar>("searchbar");
if (!m_searchbar)
throw std::runtime_error("No \"searchbar\" object in window.ui");
#if HAS_SEARCH_ENTRY2
m_searchentry = m_refBuilder->get_widget<Gtk::SearchEntry2>("searchentry");
#else
m_searchentry = m_refBuilder->get_widget<Gtk::SearchEntry>("searchentry");
#endif
if (!m_searchentry)
throw std::runtime_error("No \"searchentry\" object in window.ui");
m_gears = m_refBuilder->get_widget<Gtk::MenuButton>("gears");
if (!m_gears)
throw std::runtime_error("No \"gears\" object in window.ui");
m_sidebar = m_refBuilder->get_widget<Gtk::Revealer>("sidebar");
if (!m_sidebar)
throw std::runtime_error("No \"sidebar\" object in window.ui");
m_words = m_refBuilder->get_widget<Gtk::ListBox>("words");
if (!m_words)
throw std::runtime_error("No \"words\" object in window.ui");
// Bind settings.
m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
m_settings->bind("transition", m_stack->property_transition_type());
m_settings->bind("show-words", m_sidebar->property_reveal_child());
// Bind properties.
m_prop_binding = Glib::Binding::bind_property(m_search->property_active(),
m_searchbar->property_search_mode_enabled(), Glib::Binding::Flags::BIDIRECTIONAL);
// Connect signal handlers.
m_searchentry->signal_search_changed().connect(
sigc::mem_fun(*this, &ExampleAppWindow::on_search_text_changed));
m_stack->property_visible_child().signal_changed().connect(
sigc::mem_fun(*this, &ExampleAppWindow::on_visible_child_changed));
m_sidebar->property_reveal_child().signal_changed().connect(
sigc::mem_fun(*this, &ExampleAppWindow::on_reveal_child_changed));
// Connect the menu to the MenuButton m_gears, and bind the show-words setting
// to the win.show-words action and the "Words" menu item.
// (The connection between action and menu item is specified in gears_menu.ui.)
auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
if (!menu)
throw std::runtime_error("No \"menu\" object in gears_menu.ui");
m_gears->set_menu_model(menu);
add_action(m_settings->create_action("show-words"));
}
//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)
{
const Glib::ustring basename = file->get_basename();
auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
scrolled->set_expand(true);
auto view = Gtk::make_managed<Gtk::TextView>();
view->set_editable(false);
view->set_cursor_visible(false);
scrolled->set_child(*view);
m_stack->add(*scrolled, basename, basename);
auto buffer = view->get_buffer();
try
{
char* contents = nullptr;
gsize length = 0;
file->load_contents(contents, length);
buffer->set_text(contents, contents+length);
g_free(contents);
}
catch (const Glib::Error& ex)
{
std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
<< "\"):\n " << ex.what() << std::endl;
return;
}
auto tag = buffer->create_tag();
m_settings->bind("font", tag->property_font());
buffer->apply_tag(tag, buffer->begin(), buffer->end());
m_search->set_sensitive(true);
update_words();
}
void ExampleAppWindow::on_search_text_changed()
{
const auto text = m_searchentry->get_text();
if (text.empty())
return;
auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
if (!tab)
{
std::cout << "ExampleAppWindow::on_search_text_changed(): No visible child." << std::endl;
return;
}
auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
if (!view)
{
std::cout << "ExampleAppWindow::on_search_text_changed(): No visible text view." << std::endl;
return;
}
// Very simple-minded search implementation.
auto buffer = view->get_buffer();
Gtk::TextIter match_start;
Gtk::TextIter match_end;
if (buffer->begin().forward_search(text, Gtk::TextSearchFlags::CASE_INSENSITIVE,
match_start, match_end))
{
buffer->select_range(match_start, match_end);
view->scroll_to(match_start);
}
}
void ExampleAppWindow::on_visible_child_changed()
{
m_searchbar->set_search_mode(false);
update_words();
}
void ExampleAppWindow::on_find_word(const Gtk::Button* button)
{
m_searchentry->set_text(button->get_label());
}
void ExampleAppWindow::on_reveal_child_changed()
{
update_words();
}
void ExampleAppWindow::update_words()
{
auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
if (!tab)
return;
auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
if (!view)
{
std::cout << "ExampleAppWindow::update_words(): No visible text view." << std::endl;
return;
}
auto buffer = view->get_buffer();
// Find all words in the text buffer.
std::set<Glib::ustring> words;
auto start = buffer->begin();
Gtk::TextIter end;
while (start)
{
while (start && !start.starts_word())
++start;
if (!start)
break;
end = start;
end.forward_word_end();
if (start == end)
break;
auto word = buffer->get_text(start, end, false);
words.insert(word.lowercase());
start = end;
}
// Remove old children from the ListBox.
while (auto child = m_words->get_first_child())
m_words->remove(*child);
// Add new child buttons, one per unique word.
for (const auto& word : words)
{
auto row = Gtk::make_managed<Gtk::Button>(word);
row->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
&ExampleAppWindow::on_find_word), row));
m_words->append(*row);
}
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "../step6/main.cc"
// Equal to the corresponding file in step6
</code></pre>
<p>File: <code class="filename">gears_menu.ui</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code"><?xml version="1.0" encoding="UTF-8"?>
<interface>
<menu id="menu">
<section>
<item>
<attribute name="label" translatable="yes">_Words</attribute>
<attribute name="action">win.show-words</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="action">app.quit</attribute>
</item>
</section>
</menu>
</interface>
</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 type="titlebar">
<object class="GtkHeaderBar" id="header">
<child type="title">
<object class="GtkStackSwitcher" id="tabs">
<property name="stack">stack</property>
</object>
</child>
<child type="end">
<object class="GtkToggleButton" id="search">
<property name="sensitive">False</property>
<property name="icon-name">edit-find-symbolic</property>
</object>
</child>
<child type="end">
<object class="GtkMenuButton" id="gears">
<property name="direction">none</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="content_box">
<property name="orientation">vertical</property>
<child>
<object class="GtkSearchBar" id="searchbar">
<child>
<object class="GtkSearchEntry" id="searchentry">
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="hbox">
<child>
<object class="GtkRevealer" id="sidebar">
<property name="transition-type">slide-right</property>
<child>
<object class="GtkScrolledWindow" id="sidebar-sw">
<property name="hscrollbar-policy">never</property>
<child>
<object class="GtkListBox" id="words">
<property name="selection-mode">none</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkStack" id="stack">
<property name="transition-duration">500</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
</code></pre>
<p>File: <code class="filename">org.gtkmm.exampleapp.gschema.xml</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code"><?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema path="/org/gtkmm/exampleapp/" id="org.gtkmm.exampleapp">
<key name="font" type="s">
<default>'Monospace 12'</default>
<summary>Font</summary>
<description>The font to be used for content.</description>
</key>
<key name="transition" type="s">
<choices>
<choice value='none'/>
<choice value='crossfade'/>
<choice value='slide-left-right'/>
</choices>
<default>'none'</default>
<summary>Transition</summary>
<description>The transition to use when switching tabs.</description>
</key>
<key name="show-words" type="b">
<default>false</default>
<summary>Show words</summary>
<description>Whether to show a word list in the sidebar.</description>
</key>
</schema>
</schemalist>
</code></pre>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-buildapp-search-bar.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-properties.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Adding a search bar </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"> Properties</td>
</tr>
</table>
</div>
</body>
</html>
|