Debian Codegraph @

examples/book/buildapp/step9/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 #include <set>
21
22 ExampleAppWindow::ExampleAppWindow(BaseObjectType* cobject,
23 const Glib::RefPtr<Gtk::Builder>& refBuilder)
24 : Gtk::ApplicationWindow(cobject),
25 m_refBuilder(refBuilder)
26 {
27 // Get widgets from the Gtk::Builder file.
28 m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
29 if (!m_stack)
30 throw std::runtime_error("No \"stack\" object in window.ui");
31
32 m_search = m_refBuilder->get_widget<Gtk::ToggleButton>("search");
33 if (!m_search)
34 throw std::runtime_error("No \"search\" object in window.ui");
35
36 m_searchbar = m_refBuilder->get_widget<Gtk::SearchBar>("searchbar");
37 if (!m_searchbar)
38 throw std::runtime_error("No \"searchbar\" object in window.ui");
39
40 #if HAS_SEARCH_ENTRY2
41 m_searchentry = m_refBuilder->get_widget<Gtk::SearchEntry2>("searchentry");
42 #else
43 m_searchentry = m_refBuilder->get_widget<Gtk::SearchEntry>("searchentry");
44 #endif
45 if (!m_searchentry)
46 throw std::runtime_error("No \"searchentry\" object in window.ui");
47
48 m_gears = m_refBuilder->get_widget<Gtk::MenuButton>("gears");
49 if (!m_gears)
50 throw std::runtime_error("No \"gears\" object in window.ui");
51
52 m_sidebar = m_refBuilder->get_widget<Gtk::Revealer>("sidebar");
53 if (!m_sidebar)
54 throw std::runtime_error("No \"sidebar\" object in window.ui");
55
56 m_words = m_refBuilder->get_widget<Gtk::ListBox>("words");
57 if (!m_words)
58 throw std::runtime_error("No \"words\" object in window.ui");
59
60 m_lines = m_refBuilder->get_widget<Gtk::Label>("lines");
61 if (!m_lines)
62 throw std::runtime_error("No \"lines\" object in window.ui");
63
64 m_lines_label = m_refBuilder->get_widget<Gtk::Label>("lines_label");
65 if (!m_lines_label)
66 throw std::runtime_error("No \"lines_label\" object in window.ui");
67
68 // Bind settings.
69 m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
70 m_settings->bind("transition", m_stack->property_transition_type());
71 m_settings->bind("show-words", m_sidebar->property_reveal_child());
72
73 // Bind properties of the search button to the search bar.
74 m_binding_search_enabled = Glib::Binding::bind_property(m_search->property_active(),
75 m_searchbar->property_search_mode_enabled(), Glib::Binding::Flags::BIDIRECTIONAL);
76
77 // Connect signal handlers.
78 m_searchentry->signal_search_changed().connect(
79 sigc::mem_fun(*this, &ExampleAppWindow::on_search_text_changed));
80 m_stack->property_visible_child().signal_changed().connect(
81 sigc::mem_fun(*this, &ExampleAppWindow::on_visible_child_changed));
82 m_sidebar->property_reveal_child().signal_changed().connect(
83 sigc::mem_fun(*this, &ExampleAppWindow::on_reveal_child_changed));
84
85 // Connect the menu to the MenuButton m_gears, and bind the show-words setting
86 // to the win.show-words action and the "Words" menu item.
87 // (The connection between action and menu item is specified in gears_menu.ui.)
88 auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
89 auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
90 if (!menu)
91 throw std::runtime_error("No \"menu\" object in gears_menu.ui");
92
93 m_gears->set_menu_model(menu);
94 add_action(m_settings->create_action("show-words"));
95
96 // Bind the "visible" property of m_lines to the win.show-lines action, to
97 // the "Lines" menu item and to the "visible" property of m_lines_label.
98 add_action(Gio::PropertyAction::create("show-lines", m_lines->property_visible()));
99 m_binding_lines_visible = Glib::Binding::bind_property(m_lines->property_visible(),
100 m_lines_label->property_visible());
101
102 // Set the window icon.
103 Gtk::IconTheme::get_for_display(get_display())->add_resource_path("/org/gtkmm/exampleapp");
104 set_icon_name("exampleapp");
105 }
106
107 //static
108 ExampleAppWindow* ExampleAppWindow::create()
109 {
110 // Load the Builder file and instantiate its widgets.
111 auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
112
113 auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
114 if (!window)
115 throw std::runtime_error("No \"app_window\" object in window.ui");
116
117 return window;
118 }
119
120 void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
121 {
122 const Glib::ustring basename = file->get_basename();
123
124 auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
125 scrolled->set_expand(true);
126 auto view = Gtk::make_managed<Gtk::TextView>();
127 view->set_editable(false);
128 view->set_cursor_visible(false);
129 scrolled->set_child(*view);
130 m_stack->add(*scrolled, basename, basename);
131
132 auto buffer = view->get_buffer();
133 try
134 {
135 char* contents = nullptr;
136 gsize length = 0;
137
138 file->load_contents(contents, length);
139 buffer->set_text(contents, contents+length);
140 g_free(contents);
141 }
142 catch (const Glib::Error& ex)
143 {
144 std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
145 << "\"):\n " << ex.what() << std::endl;
146 return;
147 }
148
149 auto tag = buffer->create_tag();
150 m_settings->bind("font", tag->property_font());
151 buffer->apply_tag(tag, buffer->begin(), buffer->end());
152
153 m_search->set_sensitive(true);
154 update_words();
155 update_lines();
156 }
157
158 void ExampleAppWindow::on_search_text_changed()
159 {
160 const auto text = m_searchentry->get_text();
161 if (text.empty())
162 return;
163
164 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
165 if (!tab)
166 {
167 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible child." << std::endl;
168 return;
169 }
170
171 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
172 if (!view)
173 {
174 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible text view." << std::endl;
175 return;
176 }
177
178 // Very simple-minded search implementation.
179 auto buffer = view->get_buffer();
180 Gtk::TextIter match_start;
181 Gtk::TextIter match_end;
182 if (buffer->begin().forward_search(text, Gtk::TextSearchFlags::CASE_INSENSITIVE,
183 match_start, match_end))
184 {
185 buffer->select_range(match_start, match_end);
186 view->scroll_to(match_start);
187 }
188 }
189
190 void ExampleAppWindow::on_visible_child_changed()
191 {
192 m_searchbar->set_search_mode(false);
193 update_words();
194 update_lines();
195 }
196
197 void ExampleAppWindow::on_find_word(const Gtk::Button* button)
198 {
199 m_searchentry->set_text(button->get_label());
200 }
201
202 void ExampleAppWindow::on_reveal_child_changed()
203 {
204 update_words();
205 }
206
207 void ExampleAppWindow::update_words()
208 {
209 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
210 if (!tab)
211 return;
212
213 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
214 if (!view)
215 {
216 std::cout << "ExampleAppWindow::update_words(): No visible text view." << std::endl;
217 return;
218 }
219 auto buffer = view->get_buffer();
220
221 // Find all words in the text buffer.
222 std::set<Glib::ustring> words;
223 auto start = buffer->begin();
224 Gtk::TextIter end;
225 while (start)
226 {
227 while (start && !start.starts_word())
228 ++start;
229
230 if (!start)
231 break;
232
233 end = start;
234 end.forward_word_end();
235 if (start == end)
236 break;
237
238 auto word = buffer->get_text(start, end, false);
239 words.insert(word.lowercase());
240 start = end;
241 }
242
243 // Remove old children from the ListBox.
244 while (auto child = m_words->get_first_child())
245 m_words->remove(*child);
246
247 // Add new child buttons, one per unique word.
248 for (const auto& word : words)
249 {
250 auto row = Gtk::make_managed<Gtk::Button>(word);
251 row->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
252 &ExampleAppWindow::on_find_word), row));
253 m_words->append(*row);
254 }
255 }
256
257 void ExampleAppWindow::update_lines()
258 {
259 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
260 if (!tab)
261 return;
262
263 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
264 if (!view)
265 {
266 std::cout << "ExampleAppWindow::update_lines(): No visible text view." << std::endl;
267 return;
268 }
269 auto buffer = view->get_buffer();
270
271 int count = 0;
272 auto iter = buffer->begin();
273 while (iter)
274 {
275 ++count;
276 if (!iter.forward_line())
277 break;
278 }
279 m_lines->set_text(Glib::ustring::format(count));
280 }