1 /* This file contains utility functions to create what would be a typical "main
2 * window" for an application.
3 *
4 * TODO:
5 *
6 * Measurements happen from the start of the destruction of the last window. Use
7 * GTimer rather than X timestamps to fix this (it uses gettimeofday() internally!)
8 *
9 * Make non-interactive as well by using the above.
10 *
11 */
12
13 #include <string.h>
14 #include <gtk/gtk.h>
15
16 #include "widgets.h"
17
18 static void
quit_cb(GtkWidget * widget,gpointer data)19 quit_cb (GtkWidget *widget, gpointer data)
20 {
21 gtk_main_quit ();
22 }
23
24 static void
noop_cb(GtkWidget * widget,gpointer data)25 noop_cb (GtkWidget *widget, gpointer data)
26 {
27 /* nothing */
28 }
29
30 static const GtkActionEntry menu_action_entries[] = {
31 { "FileMenu", NULL, "_File" },
32 { "EditMenu", NULL, "_Edit" },
33 { "ViewMenu", NULL, "_View" },
34 { "HelpMenu", NULL, "_Help" },
35
36 { "New", GTK_STOCK_NEW, "_New", "<control>N", "Create a new document", G_CALLBACK (noop_cb) },
37 { "Open", GTK_STOCK_OPEN, "_Open", "<control>O", "Open a file", G_CALLBACK (noop_cb) },
38 { "Save", GTK_STOCK_SAVE, "_Save", "<control>S", "Save the document", G_CALLBACK (noop_cb) },
39 { "SaveAs", GTK_STOCK_SAVE_AS, "Save _As...", NULL, "Save the document with a different name", NULL},
40 { "PrintPreview", GTK_STOCK_PRINT_PREVIEW, "Print Previe_w", NULL, "See how the document will be printed", G_CALLBACK (noop_cb) },
41 { "Print", GTK_STOCK_PRINT, "_Print", "<control>P", "Print the document", G_CALLBACK (noop_cb) },
42 { "Close", GTK_STOCK_CLOSE, "_Close", "<control>W", "Close the document", G_CALLBACK (noop_cb) },
43 { "Quit", GTK_STOCK_QUIT, "_Quit", "<control>Q", "Quit the program", G_CALLBACK (quit_cb) },
44
45 { "Undo", GTK_STOCK_UNDO, "_Undo", "<control>Z", "Undo the last action", G_CALLBACK (noop_cb) },
46 { "Redo", GTK_STOCK_REDO, "_Redo", "<control>Y", "Redo the last action", G_CALLBACK (noop_cb) },
47 { "Cut", GTK_STOCK_CUT, "Cu_t", "<control>X", "Cut the selection to the clipboard", G_CALLBACK (noop_cb) },
48 { "Copy", GTK_STOCK_COPY, "_Copy", "<control>C", "Copy the selection to the clipboard", G_CALLBACK (noop_cb) },
49 { "Paste", GTK_STOCK_PASTE, "_Paste", "<control>V", "Paste the contents of the clipboard", G_CALLBACK (noop_cb) },
50 { "Delete", GTK_STOCK_DELETE, "_Delete", "Delete", "Delete the selection", G_CALLBACK (noop_cb) },
51 { "SelectAll", NULL, "Select _All", "<control>A", "Select the whole document", G_CALLBACK (noop_cb) },
52 { "Preferences", GTK_STOCK_PREFERENCES, "Pr_eferences", NULL, "Configure the application", G_CALLBACK (noop_cb) },
53
54 { "ZoomFit", GTK_STOCK_ZOOM_FIT, "Zoom to _Fit", NULL, "Zoom the document to fit the window", G_CALLBACK (noop_cb) },
55 { "Zoom100", GTK_STOCK_ZOOM_100, "Zoom _1:1", NULL, "Zoom to 1:1 scale", G_CALLBACK (noop_cb) },
56 { "ZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", NULL, "Zoom into the document", G_CALLBACK (noop_cb) },
57 { "ZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", NULL, "Zoom away from the document", G_CALLBACK (noop_cb) },
58 { "FullScreen", GTK_STOCK_FULLSCREEN, "Full _Screen", "F11", "Use the whole screen to view the document", G_CALLBACK (noop_cb) },
59
60 { "HelpContents", GTK_STOCK_HELP, "_Contents", "F1", "Show the table of contents for the help system", G_CALLBACK (noop_cb) },
61 { "About", GTK_STOCK_ABOUT, "_About", NULL, "About this application", G_CALLBACK (noop_cb) }
62 };
63
64 static const char ui_description[] =
65 "<ui>"
66 " <menubar name=\"MainMenu\">"
67 " <menu action=\"FileMenu\">"
68 " <menuitem action=\"New\"/>"
69 " <menuitem action=\"Open\"/>"
70 " <menuitem action=\"Save\"/>"
71 " <menuitem action=\"SaveAs\"/>"
72 " <separator/>"
73 " <menuitem action=\"PrintPreview\"/>"
74 " <menuitem action=\"Print\"/>"
75 " <separator/>"
76 " <menuitem action=\"Close\"/>"
77 " <menuitem action=\"Quit\"/>"
78 " </menu>"
79 " <menu action=\"EditMenu\">"
80 " <menuitem action=\"Undo\"/>"
81 " <menuitem action=\"Redo\"/>"
82 " <separator/>"
83 " <menuitem action=\"Cut\"/>"
84 " <menuitem action=\"Copy\"/>"
85 " <menuitem action=\"Paste\"/>"
86 " <menuitem action=\"Delete\"/>"
87 " <separator/>"
88 " <menuitem action=\"SelectAll\"/>"
89 " <separator/>"
90 " <menuitem action=\"Preferences\"/>"
91 " </menu>"
92 " <menu action=\"ViewMenu\">"
93 " <menuitem action=\"ZoomFit\"/>"
94 " <menuitem action=\"Zoom100\"/>"
95 " <menuitem action=\"ZoomIn\"/>"
96 " <menuitem action=\"ZoomOut\"/>"
97 " <separator/>"
98 " <menuitem action=\"FullScreen\"/>"
99 " </menu>"
100 " <menu action=\"HelpMenu\">"
101 " <menuitem action=\"HelpContents\"/>"
102 " <menuitem action=\"About\"/>"
103 " </menu>"
104 " </menubar>"
105 " <toolbar name=\"MainToolbar\">"
106 " <toolitem action=\"New\"/>"
107 " <toolitem action=\"Open\"/>"
108 " <toolitem action=\"Save\"/>"
109 " <separator/>"
110 " <toolitem action=\"Print\"/>"
111 " <separator/>"
112 " <toolitem action=\"Undo\"/>"
113 " <toolitem action=\"Redo\"/>"
114 " <separator/>"
115 " <toolitem action=\"Cut\"/>"
116 " <toolitem action=\"Copy\"/>"
117 " <toolitem action=\"Paste\"/>"
118 " </toolbar>"
119 "</ui>";
120
121 static GtkUIManager *
uimanager_new(void)122 uimanager_new (void)
123 {
124 GtkUIManager *ui;
125 GtkActionGroup *action_group;
126 GError *error;
127
128 ui = gtk_ui_manager_new ();
129
130 action_group = gtk_action_group_new ("Actions");
131 gtk_action_group_add_actions (action_group, menu_action_entries, G_N_ELEMENTS (menu_action_entries), NULL);
132
133 gtk_ui_manager_insert_action_group (ui, action_group, 0);
134 g_object_unref (action_group);
135
136 error = NULL;
137 if (!gtk_ui_manager_add_ui_from_string (ui, ui_description, -1, &error))
138 g_error ("Could not parse the uimanager XML: %s", error->message);
139
140 return ui;
141 }
142
143 static GtkWidget *
menubar_new(GtkUIManager * ui)144 menubar_new (GtkUIManager *ui)
145 {
146 return gtk_ui_manager_get_widget (ui, "/MainMenu");
147 }
148
149 static GtkWidget *
toolbar_new(GtkUIManager * ui)150 toolbar_new (GtkUIManager *ui)
151 {
152 return gtk_ui_manager_get_widget (ui, "/MainToolbar");
153 }
154
155 static GtkWidget *
drawing_area_new(void)156 drawing_area_new (void)
157 {
158 GtkWidget *darea;
159
160 darea = gtk_drawing_area_new ();
161 gtk_widget_set_size_request (darea, 640, 480);
162 return darea;
163 }
164
165 static GtkWidget *
content_area_new(void)166 content_area_new (void)
167 {
168 GtkWidget *notebook;
169
170 notebook = gtk_notebook_new ();
171
172 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
173 drawing_area_new (),
174 gtk_label_new ("First"));
175
176 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
177 drawing_area_new (),
178 gtk_label_new ("Second"));
179
180 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
181 drawing_area_new (),
182 gtk_label_new ("Third"));
183
184 return notebook;
185 }
186
187 static GtkWidget *
status_bar_new(void)188 status_bar_new (void)
189 {
190 return gtk_statusbar_new ();
191 }
192
193 static gboolean
delete_event_cb(GtkWidget * widget,GdkEvent * event,gpointer data)194 delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
195 {
196 gtk_main_quit ();
197 return FALSE;
198 }
199
200 GtkWidget *
appwindow_new(void)201 appwindow_new (void)
202 {
203 GtkWidget *window;
204 GtkUIManager *ui;
205 GtkWidget *vbox;
206 GtkWidget *widget;
207
208 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
209 gtk_window_set_title (GTK_WINDOW (window), "Main window");
210 g_signal_connect (window, "delete-event",
211 G_CALLBACK (delete_event_cb), NULL);
212
213 ui = uimanager_new ();
214 g_signal_connect_swapped (window, "destroy",
215 G_CALLBACK (g_object_unref), ui);
216
217 vbox = gtk_vbox_new (FALSE, 0);
218 gtk_container_add (GTK_CONTAINER (window), vbox);
219
220 widget = menubar_new (ui);
221 gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
222
223 widget = toolbar_new (ui);
224 gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
225
226 widget = content_area_new ();
227 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
228
229 widget = status_bar_new ();
230 gtk_box_pack_end (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
231
232 return window;
233 }
234