File: example_builder.cc

package info (click to toggle)
gtkmm3.0 3.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,680 kB
  • sloc: xml: 121,333; cpp: 8,531; sh: 4,250; makefile: 262; perl: 236
file content (143 lines) | stat: -rw-r--r-- 4,061 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Builder
 *
 * Demonstrates an interface loaded from a XML description.
 */

#include <gtkmm.h>
#include <gtk/gtk.h>
#include <iostream> // For std::cout

class Example_Builder : public Gtk::Window
{
public:
  Example_Builder(BaseObjectType* cobject,
                  const Glib::RefPtr<Gtk::Builder>& builder);
  ~Example_Builder() override;

protected:
  void on_file_quit();
  void on_help_about();
  void on_help_help();

  Glib::RefPtr<Gtk::Builder> m_builder;
};

//Called by DemoWindow:
Gtk::Window* do_builder()
{
  // Load the XML file and instantiate its widgets:
  Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create();
  try
  {
    builder->add_from_resource("/builder/example_builder.ui");
  }
  catch (const Glib::Error& error)
  {
    std::cout << "Error loading example_builder.ui: " << error.what() << std::endl;
    return nullptr;
  }

  // Get the GtkBuilder-instantiated window:
  Example_Builder* pWindow = nullptr;
  builder->get_widget_derived("window1", pWindow);
  if (!pWindow)
  {
    std::cout << "Could not get 'window1' from the builder." << std::endl;
    return nullptr;
  }
  return pWindow;
}

Example_Builder::Example_Builder(
  BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::Window(cobject),
  m_builder(builder)
{
  Gtk::Toolbar* pToolbar = nullptr;
  builder->get_widget("toolbar1", pToolbar);
  if (pToolbar)
    pToolbar->get_style_context()->add_class("primary-toolbar");

  Glib::RefPtr<Gio::SimpleActionGroup> refActions = Gio::SimpleActionGroup::create();
  refActions->add_action("quit", sigc::mem_fun(*this, &Example_Builder::on_file_quit));
  refActions->add_action("about", sigc::mem_fun(*this, &Example_Builder::on_help_about));
  refActions->add_action("help", sigc::mem_fun(*this, &Example_Builder::on_help_help));
  insert_action_group("win", refActions);

  Glib::RefPtr<Gtk::AccelGroup> refAccelGroup = Gtk::AccelGroup::create();
  add_accel_group(refAccelGroup);

  Gtk::MenuItem* pMenuItem = nullptr;
  builder->get_widget("new_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_n, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);

  builder->get_widget("open_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_o, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);

  builder->get_widget("save_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_s, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);

  builder->get_widget("quit_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_q, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);

  builder->get_widget("copy_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_c, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);

  builder->get_widget("cut_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_x, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);

  builder->get_widget("paste_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_v, Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);

  builder->get_widget("help_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_F1, (Gdk::ModifierType)0, Gtk::ACCEL_VISIBLE);

  builder->get_widget("about_item", pMenuItem);
  if (pMenuItem)
    pMenuItem->add_accelerator("activate", refAccelGroup,
      GDK_KEY_F7, (Gdk::ModifierType)0, Gtk::ACCEL_VISIBLE);

  show_all();
}

Example_Builder::~Example_Builder()
{
}

void Example_Builder::on_file_quit()
{
  hide();
}

void Example_Builder::on_help_about()
{
  Gtk::AboutDialog* pDialog = nullptr;
  m_builder->get_widget("aboutdialog1", pDialog);
  if (pDialog)
  {
    pDialog->set_transient_for(*this);
    pDialog->run();
    pDialog->hide();
  }
}

void Example_Builder::on_help_help()
{
  std::cout << "Help not available." << std::endl;
}