Debian Codegraph @

examples/book/buildapp/step5/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_gears = m_refBuilder->get_widget<Gtk::MenuButton>("gears");
32 if (!m_gears)
33 throw std::runtime_error("No \"gears\" object in window.ui");
34
35 // Bind settings.
36 m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
37 m_settings->bind("transition", m_stack->property_transition_type());
38
39 // Connect the menu to the MenuButton m_gears.
40 // (The connection between action and menu item is specified in gears_menu.ui.)
41 auto menu_builder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/gears_menu.ui");
42 auto menu = menu_builder->get_object<Gio::MenuModel>("menu");
43 if (!menu)
44 throw std::runtime_error("No \"menu\" object in gears_menu.ui");
45
46 m_gears->set_menu_model(menu);
47 }
48
49 //static
50 ExampleAppWindow* ExampleAppWindow::create()
51 {
52 // Load the Builder file and instantiate its widgets.
53 auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
54
55 auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
56 if (!window)
57 throw std::runtime_error("No \"app_window\" object in window.ui");
58
59 return window;
60 }
61
62 void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
63 {
64 const Glib::ustring basename = file->get_basename();
65
66 auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
67 scrolled->set_expand(true);
68 auto view = Gtk::make_managed<Gtk::TextView>();
69 view->set_editable(false);
70 view->set_cursor_visible(false);
71 scrolled->set_child(*view);
72 m_stack->add(*scrolled, basename, basename);
73
74 auto buffer = view->get_buffer();
75 try
76 {
77 char* contents = nullptr;
78 gsize length = 0;
79
80 file->load_contents(contents, length);
81 buffer->set_text(contents, contents+length);
82 g_free(contents);
83 }
84 catch (const Glib::Error& ex)
85 {
86 std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
87 << "\"):\n " << ex.what() << std::endl;
88 }
89
90 auto tag = buffer->create_tag();
91 m_settings->bind("font", tag->property_font());
92 buffer->apply_tag(tag, buffer->begin(), buffer->end());
93 }