Debian Codegraph @

examples/book/buildapp/step8/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
103 //static
104 ExampleAppWindow* ExampleAppWindow::create()
105 {
106 // Load the Builder file and instantiate its widgets.
107 auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
108
109 auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
110 if (!window)
111 throw std::runtime_error("No \"app_window\" object in window.ui");
112
113 return window;
114 }
115
116 void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
117 {
118 const Glib::ustring basename = file->get_basename();
119
120 auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
121 scrolled->set_expand(true);
122 auto view = Gtk::make_managed<Gtk::TextView>();
123 view->set_editable(false);
124 view->set_cursor_visible(false);
125 scrolled->set_child(*view);
126 m_stack->add(*scrolled, basename, basename);
127
128 auto buffer = view->get_buffer();
129 try
130 {
131 char* contents = nullptr;
132 gsize length = 0;
133
134 file->load_contents(contents, length);
135 buffer->set_text(contents, contents+length);
136 g_free(contents);
137 }
138 catch (const Glib::Error& ex)
139 {
140 std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
141 << "\"):\n " << ex.what() << std::endl;
142 return;
143 }
144
145 auto tag = buffer->create_tag();
146 m_settings->bind("font", tag->property_font());
147 buffer->apply_tag(tag, buffer->begin(), buffer->end());
148
149 m_search->set_sensitive(true);
150 update_words();
151 update_lines();
152 }
153
154 void ExampleAppWindow::on_search_text_changed()
155 {
156 const auto text = m_searchentry->get_text();
157 if (text.empty())
158 return;
159
160 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
161 if (!tab)
162 {
163 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible child." << std::endl;
164 return;
165 }
166
167 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
168 if (!view)
169 {
170 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible text view." << std::endl;
171 return;
172 }
173
174 // Very simple-minded search implementation.
175 auto buffer = view->get_buffer();
176 Gtk::TextIter match_start;
177 Gtk::TextIter match_end;
178 if (buffer->begin().forward_search(text, Gtk::TextSearchFlags::CASE_INSENSITIVE,
179 match_start, match_end))
180 {
181 buffer->select_range(match_start, match_end);
182 view->scroll_to(match_start);
183 }
184 }
185
186 void ExampleAppWindow::on_visible_child_changed()
187 {
188 m_searchbar->set_search_mode(false);
189 update_words();
190 update_lines();
191 }
192
193 void ExampleAppWindow::on_find_word(const Gtk::Button* button)
194 {
195 m_searchentry->set_text(button->get_label());
196 }
197
198 void ExampleAppWindow::on_reveal_child_changed()
199 {
200 update_words();
201 }
202
203 void ExampleAppWindow::update_words()
204 {
205 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
206 if (!tab)
207 return;
208
209 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
210 if (!view)
211 {
212 std::cout << "ExampleAppWindow::update_words(): No visible text view." << std::endl;
213 return;
214 }
215 auto buffer = view->get_buffer();
216
217 // Find all words in the text buffer.
218 std::set<Glib::ustring> words;
219 auto start = buffer->begin();
220 Gtk::TextIter end;
221 while (start)
222 {
223 while (start && !start.starts_word())
224 ++start;
225
226 if (!start)
227 break;
228
229 end = start;
230 end.forward_word_end();
231 if (start == end)
232 break;
233
234 auto word = buffer->get_text(start, end, false);
235 words.insert(word.lowercase());
236 start = end;
237 }
238
239 // Remove old children from the ListBox.
240 while (auto child = m_words->get_first_child())
241 m_words->remove(*child);
242
243 // Add new child buttons, one per unique word.
244 for (const auto& word : words)
245 {
246 auto row = Gtk::make_managed<Gtk::Button>(word);
247 row->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
248 &ExampleAppWindow::on_find_word), row));
249 m_words->append(*row);
250 }
251 }
252
253 void ExampleAppWindow::update_lines()
254 {
255 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
256 if (!tab)
257 return;
258
259 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
260 if (!view)
261 {
262 std::cout << "ExampleAppWindow::update_lines(): No visible text view." << std::endl;
263 return;
264 }
265 auto buffer = view->get_buffer();
266
267 int count = 0;
268 auto iter = buffer->begin();
269 while (iter)
270 {
271 ++count;
272 if (!iter.forward_line())
273 break;
274 }
275 m_lines->set_text(Glib::ustring::format(count));
276 }