Debian Codegraph @

examples/book/buildapp/step6/exampleappwindow.cc

raw
1 /* gtkmm example Copyright (C) 2016 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include "exampleappwindow.h"
18 #include <iostream>
19 #include <stdexcept>
20
21 ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
22 const Glib::RefPtr<Gtk::Builder>& refBuilder)
23 : Gtk::ApplicationWindow(cobject),
24 m_refBuilder(refBuilder)
25 {
26 // Get widgets from the Gtk::Builder file.
27 m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
28 if (!m_stack)
29 throw std::runtime_error("No \"stack\" object in window.ui");
30
31 m_search = m_refBuilder->get_widget<Gtk::ToggleButton>("search");
32 if (!m_search)
33 throw std::runtime_error("No \"search\" object in window.ui");
34
35 m_searchbar = m_refBuilder->get_widget<Gtk::SearchBar>("searchbar");
36 if (!m_searchbar)
37 throw std::runtime_error("No \"searchbar\" object in window.ui");
38
39 #if HAS_SEARCH_ENTRY2
40 m_searchentry = m_refBuilder->get_widget<Gtk::SearchEntry2>("searchentry");
41 #else
42 m_searchentry = m_refBuilder->get_widget<Gtk::SearchEntry>("searchentry");
43 #endif
44 if (!m_searchentry)
45 throw std::runtime_error("No \"searchentry\" object in window.ui");
46
47 m_gears = m_refBuilder->get_widget<Gtk::MenuButton>("gears");
48 if (!m_gears)
49 throw std::runtime_error("No \"gears\" object in window.ui");
50
51 // Bind settings.
52 m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
53 m_settings->bind("transition", m_stack->property_transition_type());
54
55 // Bind properties.
56 m_prop_binding = Glib::Binding::bind_property(m_search->property_active(),
57 m_searchbar->property_search_mode_enabled(), Glib::Binding::Flags::BIDIRECTIONAL);
58
59 // Connect signal handlers.
60 m_searchentry->signal_search_changed().connect(
61 sigc::mem_fun(*this, &ExampleAppWindow::on_search_text_changed));
62 m_stack->property_visible_child().signal_changed().connect(
63 sigc::mem_fun(*this, &ExampleAppWindow::on_visible_child_changed));
64
65 // Connect the menu to the MenuButton m_gears.
66 // (The connection between action and menu item is specified in gears_menu.ui.)
67 auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
68 auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
69 if (!menu)
70 throw std::runtime_error("No \"menu\" object in gears_menu.ui");
71
72 m_gears->set_menu_model(menu);
73 }
74
75 //static
76 ExampleAppWindow* ExampleAppWindow::create()
77 {
78 // Load the Builder file and instantiate its widgets.
79 auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
80
81 auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
82 if (!window)
83 throw std::runtime_error("No \"app_window\" object in window.ui");
84
85 return window;
86 }
87
88 void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
89 {
90 const Glib::ustring basename = file->get_basename();
91
92 auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
93 scrolled->set_expand(true);
94 auto view = Gtk::make_managed<Gtk::TextView>();
95 view->set_editable(false);
96 view->set_cursor_visible(false);
97 scrolled->set_child(*view);
98 m_stack->add(*scrolled, basename, basename);
99
100 auto buffer = view->get_buffer();
101 try
102 {
103 char* contents = nullptr;
104 gsize length = 0;
105
106 file->load_contents(contents, length);
107 buffer->set_text(contents, contents+length);
108 g_free(contents);
109 }
110 catch (const Glib::Error& ex)
111 {
112 std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
113 << "\"):\n " << ex.what() << std::endl;
114 return;
115 }
116
117 auto tag = buffer->create_tag();
118 m_settings->bind("font", tag->property_font());
119 buffer->apply_tag(tag, buffer->begin(), buffer->end());
120
121 m_search->set_sensitive(true);
122 }
123
124 void ExampleAppWindow::on_search_text_changed()
125 {
126 const auto text = m_searchentry->get_text();
127 if (text.empty())
128 return;
129
130 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
131 if (!tab)
132 {
133 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible child." << std::endl;
134 return;
135 }
136
137 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
138 if (!view)
139 {
140 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible text view." << std::endl;
141 return;
142 }
143
144 // Very simple-minded search implementation.
145 auto buffer = view->get_buffer();
146 Gtk::TextIter match_start;
147 Gtk::TextIter match_end;
148 if (buffer->begin().forward_search(text, Gtk::TextSearchFlags::CASE_INSENSITIVE,
149 match_start, match_end))
150 {
151 buffer->select_range(match_start, match_end);
152 view->scroll_to(match_start);
153 }
154 }
155
156 void ExampleAppWindow::on_visible_child_changed()
157 {
158 m_searchbar->set_search_mode(false);
159 }
160