Debian Codegraph @

examples/book/buildapp/step3/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 m_stack = m_refBuilder->get_widget<Gtk::Stack>("stack");
27 if (!m_stack)
28 throw std::runtime_error("No \"stack\" object in window.ui");
29 }
30
31 //static
32 ExampleAppWindow* ExampleAppWindow::create()
33 {
34 // Load the Builder file and instantiate its widgets.
35 auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/window.ui");
36
37 auto window = Gtk::Builder::get_widget_derived<ExampleAppWindow>(refBuilder, "app_window");
38 if (!window)
39 throw std::runtime_error("No \"app_window\" object in window.ui");
40
41 return window;
42 }
43
44 void ExampleAppWindow::open_file_view(const Glib::RefPtr<Gio::File>& file)
45 {
46 const Glib::ustring basename = file->get_basename();
47
48 auto scrolled = Gtk::make_managed<Gtk::ScrolledWindow>();
49 scrolled->set_expand(true);
50 auto view = Gtk::make_managed<Gtk::TextView>();
51 view->set_editable(false);
52 view->set_cursor_visible(false);
53 scrolled->set_child(*view);
54 m_stack->add(*scrolled, basename, basename);
55
56 try
57 {
58 char* contents = nullptr;
59 gsize length = 0;
60
61 file->load_contents(contents, length);
62 view->get_buffer()->set_text(contents, contents+length);
63 g_free(contents);
64 }
65 catch (const Glib::Error& ex)
66 {
67 std::cout << "ExampleAppWindow::open_file_view(\"" << file->get_parse_name()
68 << "\"):\n " << ex.what() << std::endl;
69 }
70 }