Debian Codegraph @

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