]> git.donarmstrong.com Git - xournal.git/blob - src/main.c
Public release 0.2.2.
[xournal.git] / src / main.c
1 #define ENABLE_XINPUT_BUGFIX  1
2 /* change the above to 0 if you are experiencing calibration problems with
3    XInput and want to try things differently 
4 */
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <string.h>
11 #include <gtk/gtk.h>
12 #include <libgnomecanvas/libgnomecanvas.h>
13
14 #include "xournal.h"
15 #include "xo-interface.h"
16 #include "xo-support.h"
17 #include "xo-callbacks.h"
18 #include "xo-misc.h"
19 #include "xo-file.h"
20
21 GtkWidget *winMain;
22 GnomeCanvas *canvas;
23
24 struct Journal journal; // the journal
25 struct BgPdf bgpdf;  // the PDF loader stuff
26 struct UIData ui;   // the user interface data
27 struct UndoItem *undo, *redo; // the undo and redo stacks
28
29 void hide_unimplemented(void)
30 {
31   gtk_widget_hide(GET_COMPONENT("filePrintOptions"));
32   gtk_widget_hide(GET_COMPONENT("journalFlatten"));
33   gtk_widget_hide(GET_COMPONENT("papercolorOther"));
34   gtk_widget_hide(GET_COMPONENT("toolsText"));
35   gtk_widget_hide(GET_COMPONENT("buttonText"));
36   gtk_widget_hide(GET_COMPONENT("button2Text"));
37   gtk_widget_hide(GET_COMPONENT("button3Text"));
38   gtk_widget_hide(GET_COMPONENT("toolsSelectRegion"));
39   gtk_widget_hide(GET_COMPONENT("buttonSelectRegion"));
40   gtk_widget_hide(GET_COMPONENT("button2SelectRegion"));
41   gtk_widget_hide(GET_COMPONENT("button3SelectRegion"));
42   gtk_widget_hide(GET_COMPONENT("colorOther"));
43   gtk_widget_hide(GET_COMPONENT("toolsTextFont"));
44   gtk_widget_hide(GET_COMPONENT("toolsDefaultText"));
45   gtk_widget_hide(GET_COMPONENT("optionsSavePreferences"));
46   gtk_widget_hide(GET_COMPONENT("helpIndex"));
47 }
48
49 void init_stuff (int argc, char *argv[])
50 {
51   GtkWidget *w;
52   GList *dev_list;
53   GdkDevice *device;
54   GdkScreen *screen;
55   int i, j;
56   struct Brush *b;
57   gboolean can_xinput, success;
58   gchar *tmppath, *tmpfn;
59
60   // we need an empty canvas prior to creating the journal structures
61   canvas = GNOME_CANVAS (gnome_canvas_new_aa ());
62
63   // initialize config file names
64   tmppath = g_build_filename(g_get_home_dir(), CONFIG_DIR, NULL);
65   g_mkdir(tmppath, 0700); // safer (MRU data may be confidential)
66   ui.mrufile = g_build_filename(tmppath, MRU_FILE, NULL);
67   g_free(tmppath);
68
69   // initialize data
70   // TODO: load this from a preferences file
71
72   ui.default_page.height = 792.0;
73   ui.default_page.width = 612.0;
74   ui.default_page.bg = g_new(struct Background, 1);
75   ui.default_page.bg->type = BG_SOLID;
76   ui.default_page.bg->color_no = COLOR_WHITE;
77   ui.default_page.bg->color_rgba = predef_bgcolors_rgba[COLOR_WHITE];
78   ui.default_page.bg->ruling = RULING_LINED;
79   ui.default_page.bg->canvas_item = NULL;
80   
81   ui.zoom = DEFAULT_ZOOM;
82   ui.view_continuous = TRUE;
83   ui.fullscreen = FALSE;
84   
85   ui.allow_xinput = TRUE;
86   ui.layerbox_length = 0;
87
88   if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
89     printf("Invalid command line parameters.\n"
90            "Usage: %s [filename.xoj]\n", argv[0]);
91     gtk_exit(0);
92   }
93    
94   undo = NULL; redo = NULL;
95   journal.pages = NULL;
96   bgpdf.status = STATUS_NOT_INIT;
97
98   new_journal();  
99   
100   ui.cur_item_type = ITEM_NONE;
101   ui.cur_item = NULL;
102   ui.cur_path.coords = NULL;
103   ui.cur_path_storage_alloc = 0;
104   ui.cur_path.ref_count = 1;
105
106   ui.selection = NULL;
107   ui.cursor = NULL;
108   ui.bg_apply_all_pages = FALSE;
109
110   ui.cur_brush = &(ui.brushes[0][TOOL_PEN]);
111   ui.toolno[0] = TOOL_PEN;
112   for (i=1; i<=NUM_BUTTONS; i++) ui.toolno[i] = TOOL_ERASER;
113   for (i=0; i<=NUM_BUTTONS; i++) {
114     ui.ruler[i] = FALSE;
115     ui.linked_brush[i] = BRUSH_LINKED;
116   }
117   ui.brushes[0][TOOL_PEN].color_no = COLOR_BLACK;
118   ui.brushes[0][TOOL_ERASER].color_no = COLOR_WHITE;
119   ui.brushes[0][TOOL_HIGHLIGHTER].color_no = COLOR_YELLOW;
120   for (i=0; i < NUM_STROKE_TOOLS; i++) {
121     b = &(ui.brushes[0][i]);
122     b->tool_type = i;
123     b->color_rgba = predef_colors_rgba[b->color_no];
124     if (i == TOOL_HIGHLIGHTER) {
125       b->color_rgba &= HILITER_ALPHA_MASK;
126     }
127     b->thickness_no = THICKNESS_MEDIUM;
128     b->thickness = predef_thickness[i][b->thickness_no];
129     b->tool_options = 0;
130     g_memmove(ui.default_brushes+i, b, sizeof(struct Brush));
131     for (j=1; j<=NUM_BUTTONS; j++) 
132       g_memmove(&(ui.brushes[j][i]), b, sizeof(struct Brush));
133   }
134   ui.cur_mapping = 0;
135   ui.use_erasertip = FALSE;
136
137   // initialize various interface elements
138   
139   gtk_window_set_default_size(GTK_WINDOW (winMain), 720, 480);
140   update_toolbar_and_menu();
141
142   // set up and initialize the canvas
143
144   gtk_widget_show (GTK_WIDGET (canvas));
145   w = GET_COMPONENT("scrolledwindowMain");
146   gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (canvas));
147   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (w), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
148   gtk_widget_set_events (GTK_WIDGET (canvas), GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
149   gtk_widget_set_extension_events(GTK_WIDGET (canvas), GDK_EXTENSION_EVENTS_ALL);
150   gnome_canvas_set_pixels_per_unit (canvas, ui.zoom);
151   gnome_canvas_set_center_scroll_region (canvas, TRUE);
152   gtk_layout_get_hadjustment(GTK_LAYOUT (canvas))->step_increment = 30;
153   gtk_layout_get_vadjustment(GTK_LAYOUT (canvas))->step_increment = 30;
154
155   // set up the page size and canvas size
156   update_page_stuff();
157
158   g_signal_connect ((gpointer) canvas, "button_press_event",
159                     G_CALLBACK (on_canvas_button_press_event),
160                     NULL);
161   g_signal_connect ((gpointer) canvas, "button_release_event",
162                     G_CALLBACK (on_canvas_button_release_event),
163                     NULL);
164   g_signal_connect ((gpointer) canvas, "enter_notify_event",
165                     G_CALLBACK (on_canvas_enter_notify_event),
166                     NULL);
167   g_signal_connect ((gpointer) canvas, "expose_event",
168                     G_CALLBACK (on_canvas_expose_event),
169                     NULL);
170   g_signal_connect ((gpointer) canvas, "key_press_event",
171                     G_CALLBACK (on_canvas_key_press_event),
172                     NULL);
173   g_signal_connect ((gpointer) canvas, "motion_notify_event",
174                     G_CALLBACK (on_canvas_motion_notify_event),
175                     NULL);
176   g_signal_connect ((gpointer) gtk_layout_get_vadjustment(GTK_LAYOUT(canvas)),
177                     "value-changed", G_CALLBACK (on_vscroll_changed),
178                     NULL);
179   g_object_set_data (G_OBJECT (winMain), "canvas", canvas);
180
181   screen = gtk_widget_get_screen(winMain);
182   ui.screen_width = gdk_screen_get_width(screen);
183   ui.screen_height = gdk_screen_get_height(screen);
184   
185   can_xinput = FALSE;
186   dev_list = gdk_devices_list();
187   while (dev_list != NULL) {
188     device = (GdkDevice *)dev_list->data;
189     if (device->source != GDK_SOURCE_MOUSE) {
190       /* get around a GDK bug: map the valuator range CORRECTLY to [0,1] */
191 #if ENABLE_XINPUT_BUGFIX
192       gdk_device_set_axis_use(device, 0, GDK_AXIS_IGNORE);
193       gdk_device_set_axis_use(device, 1, GDK_AXIS_IGNORE);
194 #endif
195       gdk_device_set_mode(device, GDK_MODE_SCREEN);
196       can_xinput = TRUE;
197     }
198     else gdk_device_set_mode(device, GDK_MODE_DISABLED);
199     dev_list = dev_list->next;
200   }
201   if (!can_xinput)
202     gtk_widget_set_sensitive(GET_COMPONENT("optionsUseXInput"), FALSE);
203
204   ui.use_xinput = ui.allow_xinput && can_xinput;
205   ui.antialias_bg = TRUE;
206   ui.progressive_bg = TRUE;
207
208   gtk_check_menu_item_set_active(
209     GTK_CHECK_MENU_ITEM(GET_COMPONENT("optionsUseXInput")), ui.use_xinput);
210   gtk_check_menu_item_set_active(
211     GTK_CHECK_MENU_ITEM(GET_COMPONENT("optionsAntialiasBG")), ui.antialias_bg);
212   gtk_check_menu_item_set_active(
213     GTK_CHECK_MENU_ITEM(GET_COMPONENT("optionsProgressiveBG")), ui.progressive_bg);
214
215   hide_unimplemented();
216     
217   update_undo_redo_enabled();
218   update_copy_paste_enabled();
219
220   // show everything...
221   
222   gtk_widget_show (winMain);
223   update_cursor();
224
225   // load the MRU
226   
227   init_mru();
228   
229   // and finally, open a file specified on the command line
230   // (moved here because display parameters weren't initialized yet...)
231   
232   if (argc == 1) return;
233   set_cursor_busy(TRUE);
234   if (g_path_is_absolute(argv[1]))
235     tmpfn = g_strdup(argv[1]);
236   else {
237     tmppath = g_get_current_dir();
238     tmpfn = g_build_filename(tmppath, argv[1], NULL);
239     g_free(tmppath);
240   }
241   success = open_journal(tmpfn);
242   g_free(tmpfn);
243   set_cursor_busy(FALSE);
244   if (!success) {
245     w = gtk_message_dialog_new(GTK_WINDOW (winMain), GTK_DIALOG_DESTROY_WITH_PARENT,
246        GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error opening file '%s'", argv[1]);
247     gtk_dialog_run(GTK_DIALOG(w));
248     gtk_widget_destroy(w);
249   }
250 }
251
252
253 int
254 main (int argc, char *argv[])
255 {
256   gchar *path, *path1, *path2;
257   
258   gtk_set_locale ();
259   gtk_init (&argc, &argv);
260
261   add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");
262   path = g_path_get_dirname(argv[0]);
263   path1 = g_build_filename(path, "pixmaps", NULL);
264   path2 = g_build_filename(path, "..", "pixmaps", NULL);
265   add_pixmap_directory (path1);
266   add_pixmap_directory (path2);
267   add_pixmap_directory (path);
268   g_free(path);
269   g_free(path1);
270   g_free(path2);
271
272   /*
273    * The following code was added by Glade to create one of each component
274    * (except popup menus), just so that you see something after building
275    * the project. Delete any components that you don't want shown initially.
276    */
277   winMain = create_winMain ();
278   
279   init_stuff (argc, argv);
280   
281   gtk_main ();
282   
283   if (bgpdf.status != STATUS_NOT_INIT) shutdown_bgpdf();
284   if (bgpdf.status != STATUS_NOT_INIT) end_bgpdf_shutdown();
285
286   save_mru_list();
287   
288   return 0;
289 }
290