1 /* Application main window
2  *
3  * Demonstrates a typical application window, with menubar, toolbar, statusbar.
4  */
5 
6 #include <gtkmm.h>
7 
8 class Example_AppWindow : public Gtk::Window
9 {
10 public:
11   Example_AppWindow();
12   virtual ~Example_AppWindow();
13 
14 protected:
15   //Signal handlers:
16   virtual void on_menu_item();
17   virtual void on_text_changed();
18   virtual void on_text_mark_set(const Gtk::TextBuffer::iterator& new_location, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark);
19 
20   //Member widgets:
21   Gtk::Table m_Table;
22   Gtk::Menu m_Menubar;
23   Gtk::Toolbar m_Toolbar;
24   Gtk::ScrolledWindow m_ScrolledWindow;
25   Gtk::TextView m_TextView;
26   Gtk::Statusbar m_Statusbar;
27 };
28 
29 
30 //Called by DemoWindow;
do_appwindow()31 Gtk::Window* do_appwindow()
32 {
33   return new Example_AppWindow();
34 }
35 
36 
Example_AppWindow()37 Example_AppWindow::Example_AppWindow()
38 : m_Table(1, 4)
39 {
40   set_title("Application Window");
41 
42   add(m_Table);
43 
44 /*
45   //Menu:
46   {
47     using namespace Gtk::Menu_Helpers;
48 
49     //File menu:
50     Gtk::Menu* pMenuFile = Gtk::manage( new Gtk::Menu() );
51     MenuList& list_file = pMenuFile->items();
52     list_file.push_back( MenuElem("_New", "<control>N", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
53     list_file.push_back( MenuElem("_Open", "<control>O", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
54     list_file.push_back( MenuElem("_Save", "<control>S", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
55     list_file.push_back( MenuElem("Save _As", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
56     list_file.push_back(SeparatorElem());
57     list_file.push_back( MenuElem("_Quit", "<control>Q", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
58 
59     //Preferences menu:
60     Gtk::Menu* pMenuPreferences = Gtk::manage( new Gtk::Menu() );
61     MenuList& list_preferences = pMenuPreferences->items();
62 
63     // Create a submenu
64     Gtk::Menu* pMenuSub_Color = Gtk::manage( new Gtk::Menu());
65     MenuList& list_sub = pMenuSub_Color->items();
66     list_sub.push_back( MenuElem("_Red", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
67     list_sub.push_back( MenuElem("_Green", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
68     list_sub.push_back( MenuElem("_Blue", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
69 
70     list_preferences.push_back( MenuElem("_Color", *pMenuSub_Color) );
71 
72     // Create a submenu
73     Gtk::Menu* pMenuSub_Shape = Gtk::manage( new Gtk::Menu());
74     list_sub = pMenuSub_Shape->items();
75     list_sub.push_back( MenuElem("_Square", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
76     list_sub.push_back( MenuElem("_Rectangle", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
77     list_sub.push_back( MenuElem("_Oval", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
78 
79     list_preferences.push_back( MenuElem("_Shape", *pMenuSub_Shape) );
80 
81     //Help menu:
82     Gtk::Menu* pMenuHelp = Gtk::manage( new Gtk::Menu() );
83     MenuList& list_help = pMenuHelp->items();
84     list_help.push_back( MenuElem("_About", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) );
85 
86 
87     //Create the menu bar
88     MenuList& list_bar = m_Menubar.items();
89     list_bar.push_front(MenuElem("_Help", *pMenuHelp));
90     list_bar.front()->set_right_justified();
91     list_bar.push_front(MenuElem("_Preferences", *pMenuPreferences));
92     list_bar.push_front(MenuElem("_File", *pMenuFile));
93 
94     //Add the menu bar to the Table:
95     m_Table.attach(m_Menubar,
96                     // X direction             Y direction
97                     0, 1,                      0, 1,
98                     Gtk::FILL|Gtk::EXPAND, Gtk::AttachOptions(0)
99                     );
100   } //menu
101 
102 */
103   //Toolbar:
104   {
105     m_Table.attach(m_Toolbar,
106                    /* X direction */       /* Y direction */
107                    0, 1,                   1, 2,
108                    Gtk::FILL|Gtk::EXPAND, Gtk::AttachOptions(0)
109                    );
110   }
111 
112 
113   m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
114   m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_IN);
115   m_Table.attach(m_ScrolledWindow,
116                  /* X direction */       /* Y direction */
117                  0, 1,                   2, 3);
118 
119   set_default_size(200, 200);
120 
121   m_ScrolledWindow.add(m_TextView);
122 
123 
124   /* Create statusbar */
125 
126   m_Table.attach(m_Statusbar,
127                  /* X direction */       /* Y direction */
128                  0, 1,                   3, 4,
129                  Gtk::FILL|Gtk::EXPAND, Gtk::AttachOptions(0)
130                  );
131 
132 
133   /* Show text widget info in the statusbar */
134   Glib::RefPtr<Gtk::TextBuffer> refTextBuffer = m_TextView.get_buffer();
135   refTextBuffer->signal_changed().connect(sigc::mem_fun(*this, &Example_AppWindow::on_text_changed));
136   refTextBuffer->signal_mark_set().connect(sigc::mem_fun(*this, &Example_AppWindow::on_text_mark_set));
137   on_text_changed();
138 
139   show_all();
140 }
141 
~Example_AppWindow()142 Example_AppWindow::~Example_AppWindow()
143 {
144 }
145 
on_menu_item()146 void Example_AppWindow::on_menu_item()
147 {
148   Gtk::MessageDialog dialog(*this, "You selected or toggled the menu item", false,
149                             Gtk::MESSAGE_INFO, Gtk::BUTTONS_CLOSE);
150   dialog.run();
151 }
152 
on_text_changed()153 void Example_AppWindow::on_text_changed()
154 {
155   m_Statusbar.pop();
156 
157   Glib::RefPtr<Gtk::TextBuffer> refBuffer = m_TextView.get_buffer();
158   gint count = refBuffer->get_char_count();
159 
160   Gtk::TextBuffer::iterator iter = refBuffer->get_iter_at_mark(refBuffer->get_insert());
161 
162   gint row = iter.get_line();
163   gint col = iter.get_line_offset();
164 
165   gchar* msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
166                          row, col, count);
167   m_Statusbar.push(msg);
168   g_free (msg);
169 }
170 
171 
on_text_mark_set(const Gtk::TextBuffer::iterator &,const Glib::RefPtr<Gtk::TextBuffer::Mark> &)172 void Example_AppWindow::on_text_mark_set(const Gtk::TextBuffer::iterator&, const Glib::RefPtr<Gtk::TextBuffer::Mark>&)
173 {
174   on_text_changed();
175 }
176 
177