File: example_appwindow.cc

package info (click to toggle)
gtkmm2.4 1%3A2.20.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 73,860 kB
  • ctags: 20,553
  • sloc: xml: 79,575; sh: 10,120; cpp: 8,347; makefile: 290
file content (176 lines) | stat: -rw-r--r-- 5,753 bytes parent folder | download | duplicates (6)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Application main window
 *
 * Demonstrates a typical application window, with menubar, toolbar, statusbar.
 */

#include <gtkmm.h>

class Example_AppWindow : public Gtk::Window
{
public:
  Example_AppWindow();
  virtual ~Example_AppWindow();

protected:
  //Signal handlers:
  virtual void on_menu_item();
  virtual void on_text_changed();
  virtual void on_text_mark_set(const Gtk::TextBuffer::iterator& new_location, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark);

  //Member widgets:
  Gtk::Table m_Table;
  Gtk::Menu m_Menubar;
  Gtk::Toolbar m_Toolbar;
  Gtk::ScrolledWindow m_ScrolledWindow;
  Gtk::TextView m_TextView;
  Gtk::Statusbar m_Statusbar;
};


//Called by DemoWindow;
Gtk::Window* do_appwindow()
{
  return new Example_AppWindow();
}


Example_AppWindow::Example_AppWindow()
: m_Table(1, 4)
{
  set_title("Application Window");

  add(m_Table);

/*
  //Menu:
  {
    using namespace Gtk::Menu_Helpers;

    //File menu:
    Gtk::Menu* pMenuFile = Gtk::manage( new Gtk::Menu() );
    MenuList& list_file = pMenuFile->items();
    list_file.push_back( MenuElem("_New", "<control>N", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_file.push_back( MenuElem("_Open", "<control>O", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_file.push_back( MenuElem("_Save", "<control>S", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_file.push_back( MenuElem("Save _As", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_file.push_back(SeparatorElem());
    list_file.push_back( MenuElem("_Quit", "<control>Q", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );

    //Preferences menu:
    Gtk::Menu* pMenuPreferences = Gtk::manage( new Gtk::Menu() );
    MenuList& list_preferences = pMenuPreferences->items();

    // Create a submenu
    Gtk::Menu* pMenuSub_Color = Gtk::manage( new Gtk::Menu());
    MenuList& list_sub = pMenuSub_Color->items();
    list_sub.push_back( MenuElem("_Red", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_sub.push_back( MenuElem("_Green", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_sub.push_back( MenuElem("_Blue", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );

    list_preferences.push_back( MenuElem("_Color", *pMenuSub_Color) );

    // Create a submenu
    Gtk::Menu* pMenuSub_Shape = Gtk::manage( new Gtk::Menu());
    list_sub = pMenuSub_Shape->items();
    list_sub.push_back( MenuElem("_Square", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_sub.push_back( MenuElem("_Rectangle", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
    list_sub.push_back( MenuElem("_Oval", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );

    list_preferences.push_back( MenuElem("_Shape", *pMenuSub_Shape) );

    //Help menu:
    Gtk::Menu* pMenuHelp = Gtk::manage( new Gtk::Menu() );
    MenuList& list_help = pMenuHelp->items();
    list_help.push_back( MenuElem("_About", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );


    //Create the menu bar
    MenuList& list_bar = m_Menubar.items();
    list_bar.push_front(MenuElem("_Help", *pMenuHelp));
    list_bar.front()->set_right_justified();
    list_bar.push_front(MenuElem("_Preferences", *pMenuPreferences));
    list_bar.push_front(MenuElem("_File", *pMenuFile));

    //Add the menu bar to the Table:
    m_Table.attach(m_Menubar,
                    // X direction             Y direction
                    0, 1,                      0, 1,
                    Gtk::FILL|Gtk::EXPAND, Gtk::AttachOptions(0)
                    );
  } //menu

*/
  //Toolbar:
  {
    m_Table.attach(m_Toolbar,
                   /* X direction */       /* Y direction */
                   0, 1,                   1, 2,
                   Gtk::FILL|Gtk::EXPAND, Gtk::AttachOptions(0)
                   );
  }


  m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_IN);
  m_Table.attach(m_ScrolledWindow,
                 /* X direction */       /* Y direction */
                 0, 1,                   2, 3);

  set_default_size(200, 200);

  m_ScrolledWindow.add(m_TextView);


  /* Create statusbar */

  m_Table.attach(m_Statusbar,
                 /* X direction */       /* Y direction */
                 0, 1,                   3, 4,
                 Gtk::FILL|Gtk::EXPAND, Gtk::AttachOptions(0)
                 );


  /* Show text widget info in the statusbar */
  Glib::RefPtr<Gtk::TextBuffer> refTextBuffer = m_TextView.get_buffer();
  refTextBuffer->signal_changed().connect(sigc::mem_fun(*this, &Example_AppWindow::on_text_changed));
  refTextBuffer->signal_mark_set().connect(sigc::mem_fun(*this, &Example_AppWindow::on_text_mark_set));
  on_text_changed();

  show_all();
}

Example_AppWindow::~Example_AppWindow()
{
}

void Example_AppWindow::on_menu_item()
{
  Gtk::MessageDialog dialog(*this, "You selected or toggled the menu item", false,
                            Gtk::MESSAGE_INFO, Gtk::BUTTONS_CLOSE);
  dialog.run();
}

void Example_AppWindow::on_text_changed()
{
  m_Statusbar.pop();

  Glib::RefPtr<Gtk::TextBuffer> refBuffer = m_TextView.get_buffer();
  gint count = refBuffer->get_char_count();

  Gtk::TextBuffer::iterator iter = refBuffer->get_iter_at_mark(refBuffer->get_insert());

  gint row = iter.get_line();
  gint col = iter.get_line_offset();

  gchar* msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
                         row, col, count);
  m_Statusbar.push(msg);
  g_free (msg);
}


void Example_AppWindow::on_text_mark_set(const Gtk::TextBuffer::iterator&, const Glib::RefPtr<Gtk::TextBuffer::Mark>&)
{
  on_text_changed();
}