Debian Codegraph @

examples/book/buildapp/step7/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 // Bind settings.
61 m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
62 m_settings->bind("transition", m_stack->property_transition_type());
63 m_settings->bind("show-words", m_sidebar->property_reveal_child());
64
65 // Bind properties.
66 m_prop_binding = Glib::Binding::bind_property(m_search->property_active(),
67 m_searchbar->property_search_mode_enabled(), Glib::Binding::Flags::BIDIRECTIONAL);
68
69 // Connect signal handlers.
70 m_searchentry->signal_search_changed().connect(
71 sigc::mem_fun(*this, &ExampleAppWindow::on_search_text_changed));
72 m_stack->property_visible_child().signal_changed().connect(
73 sigc::mem_fun(*this, &ExampleAppWindow::on_visible_child_changed));
74 m_sidebar->property_reveal_child().signal_changed().connect(
75 sigc::mem_fun(*this, &ExampleAppWindow::on_reveal_child_changed));
76
77 // Connect the menu to the MenuButton m_gears, and bind the show-words setting
78 // to the win.show-words action and the "Words" menu item.
79 // (The connection between action and menu item is specified in gears_menu.ui.)
80 auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
81 auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
82 if (!menu)
83 throw std::runtime_error("No \"menu\" object in gears_menu.ui");
84
85 m_gears->set_menu_model(menu);
86 add_action(m_settings->create_action("show-words"));
87 }
88
89 //static
90 ExampleAppWindow* ExampleAppWindow::create()
91 {
92 // Load the Builder file and instantiate its widgets.
93 auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
94
95 auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
96 if (!window)
97 throw std::runtime_error("No \"app_window\" object in window.ui");
98
99 return window;
100 }
101
102 void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
103 {
104 const Glib::ustring basename = file->get_basename();
105
106 auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
107 scrolled->set_expand(true);
108 auto view = Gtk::make_managed<Gtk::TextView>();
109 view->set_editable(false);
110 view->set_cursor_visible(false);
111 scrolled->set_child(*view);
112 m_stack->add(*scrolled, basename, basename);
113
114 auto buffer = view->get_buffer();
115 try
116 {
117 char* contents = nullptr;
118 gsize length = 0;
119
120 file->load_contents(contents, length);
121 buffer->set_text(contents, contents+length);
122 g_free(contents);
123 }
124 catch (const Glib::Error& ex)
125 {
126 std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
127 << "\"):\n " << ex.what() << std::endl;
128 return;
129 }
130
131 auto tag = buffer->create_tag();
132 m_settings->bind("font", tag->property_font());
133 buffer->apply_tag(tag, buffer->begin(), buffer->end());
134
135 m_search->set_sensitive(true);
136 update_words();
137 }
138
139 void ExampleAppWindow::on_search_text_changed()
140 {
141 const auto text = m_searchentry->get_text();
142 if (text.empty())
143 return;
144
145 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
146 if (!tab)
147 {
148 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible child." << std::endl;
149 return;
150 }
151
152 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
153 if (!view)
154 {
155 std::cout << "ExampleAppWindow::on_search_text_changed(): No visible text view." << std::endl;
156 return;
157 }
158
159 // Very simple-minded search implementation.
160 auto buffer = view->get_buffer();
161 Gtk::TextIter match_start;
162 Gtk::TextIter match_end;
163 if (buffer->begin().forward_search(text, Gtk::TextSearchFlags::CASE_INSENSITIVE,
164 match_start, match_end))
165 {
166 buffer->select_range(match_start, match_end);
167 view->scroll_to(match_start);
168 }
169 }
170
171 void ExampleAppWindow::on_visible_child_changed()
172 {
173 m_searchbar->set_search_mode(false);
174 update_words();
175 }
176
177 void ExampleAppWindow::on_find_word(const Gtk::Button* button)
178 {
179 m_searchentry->set_text(button->get_label());
180 }
181
182 void ExampleAppWindow::on_reveal_child_changed()
183 {
184 update_words();
185 }
186
187 void ExampleAppWindow::update_words()
188 {
189 auto tab = dynamic_cast<Gtk::ScrolledWindow*>(m_stack->get_visible_child());
190 if (!tab)
191 return;
192
193 auto view = dynamic_cast<Gtk::TextView*>(tab->get_child());
194 if (!view)
195 {
196 std::cout << "ExampleAppWindow::update_words(): No visible text view." << std::endl;
197 return;
198 }
199 auto buffer = view->get_buffer();
200
201 // Find all words in the text buffer.
202 std::set<Glib::ustring> words;
203 auto start = buffer->begin();
204 Gtk::TextIter end;
205 while (start)
206 {
207 while (start && !start.starts_word())
208 ++start;
209
210 if (!start)
211 break;
212
213 end = start;
214 end.forward_word_end();
215 if (start == end)
216 break;
217
218 auto word = buffer->get_text(start, end, false);
219 words.insert(word.lowercase());
220 start = end;
221 }
222
223 // Remove old children from the ListBox.
224 while (auto child = m_words->get_first_child())
225 m_words->remove(*child);
226
227 // Add new child buttons, one per unique word.
228 for (const auto& word : words)
229 {
230 auto row = Gtk::make_managed<Gtk::Button>(word);
231 row->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
232 &ExampleAppWindow::on_find_word), row));
233 m_words->append(*row);
234 }
235 }