2 * Copyright (C) 2007 Tilman Sauerbeck (tilman at code-monkey de)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
31 #include "rakt-window.h"
34 GtkWidget *content_vbox;
37 GtkUIManager *ui_manager;
38 GtkAction *action_prev;
39 GtkAction *action_next;
41 GtkAction *action_zoom_in;
42 GtkAction *action_zoom_out;
43 GtkAction *action_zoom_100;
45 GtkWidget *drawing_area;
46 PopplerDocument *document;
52 static void window_finalize (GObject *object);
54 static void on_add_widget (GtkUIManager *merge, GtkWidget *widget, RaktWindow *window);
56 static void on_action_quit (GtkAction *action, RaktWindow *window);
57 static void on_action_open (GtkAction *action, RaktWindow *window);
58 static void on_action_go_next (GtkAction *action, RaktWindow *window);
59 static void on_action_go_previous (GtkAction *action, RaktWindow *window);
60 static void on_action_zoom (GtkAction *action, RaktWindow *window);
61 static void on_action_about (GtkAction *action, RaktWindow *window);
63 static const GtkActionEntry action_entries[] = {
65 "FileMenu", NULL, "_File", NULL, NULL, NULL
68 "GoMenu", NULL, "_Go", NULL, NULL, NULL
71 "ViewMenu", NULL, "_View", NULL, NULL, NULL
74 "HelpMenu", NULL, "_Help", NULL, NULL, NULL
77 "Open", GTK_STOCK_OPEN, "_Open",
78 "<control>O", "Open a PDF file",
79 G_CALLBACK (on_action_open)
82 "Next", GTK_STOCK_GO_FORWARD, "_Next",
84 G_CALLBACK (on_action_go_next)
87 "Previous", GTK_STOCK_GO_BACK, "_Previous",
88 "<control>p", "Previous",
89 G_CALLBACK (on_action_go_previous)
92 "Zoom In", GTK_STOCK_ZOOM_IN, "Zoom _In",
93 "<control>plus", "Zoom In",
94 G_CALLBACK (on_action_zoom)
97 "Zoom Out", GTK_STOCK_ZOOM_OUT, "Zoom _Out",
98 "<control>minus", "Zoom Out",
99 G_CALLBACK (on_action_zoom)
102 "Zoom 100", GTK_STOCK_ZOOM_100, "Zoom 100%",
104 G_CALLBACK (on_action_zoom)
107 "Quit", GTK_STOCK_QUIT, "_Quit",
108 "<control>Q", "Quit the application",
109 G_CALLBACK (on_action_quit)
112 "About", GTK_STOCK_ABOUT, "_About",
113 NULL, "About this application",
114 G_CALLBACK (on_action_about)
118 static const gchar *ui_layout =
120 " <menubar name='MenuBar'>"
121 " <menu action='FileMenu'>"
122 " <menuitem action='Open'/>"
123 " <separator action='Sep1'/>"
124 " <menuitem action='Quit'/>"
126 " <menu action='GoMenu'>"
127 " <menuitem action='Previous'/>"
128 " <menuitem action='Next'/>"
130 " <menu action='ViewMenu'>"
131 " <menuitem action='Zoom In'/>"
132 " <menuitem action='Zoom Out'/>"
133 " <menuitem action='Zoom 100'/>"
135 " <menu action='HelpMenu'>"
136 " <menuitem action='About'/>"
139 " <toolbar name='ToolBar'>"
140 " <toolitem action='Open'/>"
141 " <toolitem action='Previous'/>"
142 " <toolitem action='Next'/>"
143 " <toolitem action='Zoom In'/>"
144 " <toolitem action='Zoom Out'/>"
145 " <toolitem action='Zoom 100'/>"
149 G_DEFINE_TYPE (RaktWindow, rakt_window, GTK_TYPE_WINDOW)
151 #define GET_PRIV(obj) \
152 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), RAKT_TYPE_WINDOW, RaktWindowPriv))
155 rakt_window_class_init (RaktWindowClass *class)
157 GObjectClass *object_class = G_OBJECT_CLASS (class);
159 object_class->finalize = window_finalize;
161 g_type_class_add_private (object_class, sizeof (RaktWindowPriv));
165 render_page (RaktWindow *window)
167 RaktWindowPriv *priv;
169 int width, height, n_pages;
171 priv = GET_PRIV (window);
173 n_pages = poppler_document_get_n_pages (priv->document);
174 gtk_action_set_sensitive (priv->action_prev, priv->page_no > 0);
175 gtk_action_set_sensitive (priv->action_next, priv->page_no < n_pages - 1);
177 gtk_action_set_sensitive (priv->action_zoom_in, priv->scale < 3.0);
178 gtk_action_set_sensitive (priv->action_zoom_out, priv->scale > 0.4);
179 gtk_action_set_sensitive (priv->action_zoom_100, priv->scale != 1.0);
181 priv->page = poppler_document_get_page (priv->document, priv->page_no);
183 poppler_page_get_size (priv->page, &tmpw, &tmph);
185 width = (int) (tmpw * priv->scale + 0.5);
186 height = (int) (tmph * priv->scale + 0.5);
188 gtk_widget_set_size_request (priv->drawing_area, width, height);
189 gtk_widget_queue_draw_area (priv->drawing_area, 0, 0, width, height);
193 open_document (RaktWindow *window, gchar *uri)
195 RaktWindowPriv *priv;
196 GError *error = NULL;
198 priv = GET_PRIV (window);
200 priv->document = poppler_document_new_from_file (uri, NULL, &error);
203 render_page (window);
207 window_create_menu (RaktWindow *window)
209 RaktWindowPriv *priv;
210 GtkActionGroup *action_group;
211 GtkAccelGroup *accel_group;
212 GError *error = NULL;
214 priv = GET_PRIV (window);
216 priv->ui_manager = gtk_ui_manager_new ();
218 g_signal_connect (priv->ui_manager,
220 G_CALLBACK (on_add_widget),
223 action_group = gtk_action_group_new ("Actions");
224 gtk_action_group_add_actions (action_group, action_entries,
225 G_N_ELEMENTS (action_entries), window);
226 gtk_ui_manager_insert_action_group (priv->ui_manager, action_group, 0);
228 accel_group = gtk_ui_manager_get_accel_group (priv->ui_manager);
229 gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
231 g_object_unref (action_group);
233 gtk_ui_manager_add_ui_from_string (priv->ui_manager, ui_layout, -1, &error);
236 g_error ("Couldn't create UI: %s\n", error->message);
238 gtk_ui_manager_ensure_update (priv->ui_manager);
240 priv->action_prev = gtk_ui_manager_get_action (priv->ui_manager,
241 "ui/ToolBar/Previous");
242 priv->action_next = gtk_ui_manager_get_action (priv->ui_manager,
244 priv->action_zoom_in = gtk_ui_manager_get_action (priv->ui_manager,
245 "ui/ToolBar/Zoom In");
246 priv->action_zoom_out = gtk_ui_manager_get_action (priv->ui_manager,
247 "ui/ToolBar/Zoom Out");
248 priv->action_zoom_100 = gtk_ui_manager_get_action (priv->ui_manager,
249 "ui/ToolBar/Zoom 100");
251 gtk_action_set_sensitive (priv->action_prev, false);
252 gtk_action_set_sensitive (priv->action_next, false);
254 gtk_action_set_sensitive (priv->action_zoom_in, false);
255 gtk_action_set_sensitive (priv->action_zoom_out, false);
256 gtk_action_set_sensitive (priv->action_zoom_100, false);
260 on_expose_event (GtkWidget *widget, GdkEvent *event, gpointer user_data)
262 RaktWindowPriv *priv;
265 priv = GET_PRIV (user_data);
267 cr = gdk_cairo_create (priv->drawing_area->window);
269 cairo_set_source_rgb (cr, 255, 255, 255);
272 cairo_scale (cr, priv->scale, priv->scale);
275 poppler_page_render (priv->page, cr);
283 on_delete_event (GtkWidget *widget, GdkEvent *event, gpointer user_data)
291 rakt_window_init (RaktWindow *window)
293 RaktWindowPriv *priv;
296 priv = GET_PRIV (window);
300 gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
301 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
303 priv->content_vbox = gtk_vbox_new (FALSE, 0);
304 gtk_widget_show (priv->content_vbox);
305 gtk_container_add (GTK_CONTAINER (window), priv->content_vbox);
307 window_create_menu (window);
309 priv->drawing_area = gtk_drawing_area_new ();
310 gtk_widget_show (priv->drawing_area);
312 g_signal_connect (priv->drawing_area, "expose-event",
313 G_CALLBACK (on_expose_event), window);
315 scrolled = gtk_scrolled_window_new (NULL, NULL);
317 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
318 GTK_POLICY_AUTOMATIC,
319 GTK_POLICY_AUTOMATIC);
321 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled),
323 gtk_widget_show (scrolled);
325 gtk_box_pack_start (GTK_BOX (priv->content_vbox), scrolled,
328 g_signal_connect (GTK_WINDOW (window), "delete-event",
329 G_CALLBACK (on_delete_event), NULL);
333 window_finalize (GObject *object)
335 RaktWindowPriv *priv;
337 priv = GET_PRIV (object);
339 g_object_unref (priv->ui_manager);
341 G_OBJECT_CLASS (rakt_window_parent_class)->finalize (object);
345 on_add_widget (GtkUIManager *merge, GtkWidget *widget, RaktWindow *window)
347 RaktWindowPriv *priv;
349 priv = GET_PRIV (window);
351 gtk_box_pack_start (GTK_BOX (priv->content_vbox), widget, FALSE, FALSE, 0);
355 on_action_quit (GtkAction *action, RaktWindow *window)
361 on_action_open (GtkAction *action, RaktWindow *window)
364 GtkFileFilter *filter;
367 dialog = gtk_file_chooser_dialog_new ("Open PDF",
369 GTK_FILE_CHOOSER_ACTION_OPEN,
373 GTK_RESPONSE_CANCEL, NULL);
375 filter = gtk_file_filter_new ();
376 gtk_file_filter_set_name (filter, "PDF files");
377 gtk_file_filter_add_pattern (filter, "*.pdf");
379 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
381 /* now run the dialog */
382 n = gtk_dialog_run (GTK_DIALOG (dialog));
384 if (n == GTK_RESPONSE_ACCEPT) {
387 uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
388 open_document (window, uri);
392 gtk_widget_destroy (dialog);
396 on_action_go_next (GtkAction *action, RaktWindow *window)
398 RaktWindowPriv *priv;
400 priv = GET_PRIV (window);
403 render_page (window);
407 on_action_go_previous (GtkAction *action, RaktWindow *window)
409 RaktWindowPriv *priv;
411 priv = GET_PRIV (window);
414 render_page (window);
418 on_action_zoom (GtkAction *action, RaktWindow *window)
420 RaktWindowPriv *priv;
422 priv = GET_PRIV (window);
424 if (action == priv->action_zoom_in)
426 else if (action == priv->action_zoom_out)
431 render_page (window);
435 on_action_about (GtkAction *action, RaktWindow *window)
437 const gchar *authors[] = {
442 gtk_show_about_dialog (GTK_WINDOW (window),
443 "name", PACKAGE_NAME,
444 "copyright", "Copyright (c) 2007 Tilman Sauerbeck",
451 rakt_window_new (void)
453 return g_object_new (RAKT_TYPE_WINDOW, NULL);