1 /*
2  *  This program is free software; you can redistribute it and/or
3  *  modify it under the terms of the GNU General Public
4  *  License as published by the Free Software Foundation; either
5  *  version 2 of the License, or (at your option) any later version.
6  *
7  *  This software is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #include <gtk/gtk.h>
17 #include <libgnomecanvas/libgnomecanvas.h>
18 #include <poppler/glib/poppler.h>
19 
20 // #define INPUT_DEBUG
21 /* uncomment this line if you experience event-processing problems
22    and want to list the input events received by xournal. Caution, lots
23    of output (redirect to a file). */
24 
25 // #define ENABLE_XINPUT_BUGFIX
26 /* uncomment this line if you are experiencing calibration problems with
27    XInput and want to try things differently. Especially useful on older
28    distributions (up to around 2010). */
29 
30 #define FILE_DIALOG_SIZE_BUGFIX
31 /* ugly, but should help users with versions of GTK+ that suffer from the
32    "tiny file dialog" syndrome, without hurting those with well-behaved
33    versions of GTK+. Comment out if you'd prefer not to include this fix. */
34 
35 // PREF FILES INFO
36 
37 #define CONFIG_DIR ".xournal"
38 #define MRU_FILE "recent-files"
39 #define MRU_SIZE 8
40 #define CONFIG_FILE "config"
41 
42 // apparently, not all Win32/64 compilers define WIN32 (?)
43 
44 #ifdef _WIN32
45 #define WIN32
46 #endif
47 
48 // version string for about box
49 
50 #ifdef WIN32
51 #define VERSION_STRING VERSION "-win32"
52 #else
53 #define VERSION_STRING VERSION
54 #endif
55 
56 // DATA STRUCTURES AND CONSTANTS
57 
58 #define PIXEL_MOTION_THRESHOLD 0.3
59 #define MAX_AXES 12
60 #define EPSILON 1E-7
61 #define MAX_ZOOM 20.0
62 #define DISPLAY_DPI_DEFAULT 96.0
63 #define MIN_ZOOM 0.2
64 #define RESIZE_MARGIN 6.0
65 #define MAX_SAFE_RENDER_DPI 720 // max dpi at which PDF bg's get rendered
66 
67 #define VBOX_MAIN_NITEMS 5 // number of interface items in vboxMain
68 
69 // default touch device
70 #define DEFAULT_DEVICE_FOR_TOUCH "Touchscr"
71 
72 /* a string (+ aux data) that maintains a refcount */
73 
74 typedef struct Refstring {
75   int nref;
76   char *s;
77   gpointer aux;
78 } Refstring;
79 
80 
81 /* The journal is mostly a list of pages. Each page is a list of layers,
82    and a background. Each layer is a list of items, from bottom to top.
83 */
84 
85 typedef struct Background {
86   int type;
87   GnomeCanvasItem *canvas_item;
88   int color_no;
89   guint color_rgba;
90   int ruling;
91   GdkPixbuf *pixbuf;
92   Refstring *filename;
93   int file_domain;
94   int file_page_seq;
95   double pixbuf_scale; // for PIXMAP, this is the *current* zoom value
96                        // for PDF, this is the *requested* zoom value
97   int pixel_height, pixel_width; // PDF only: pixel size of current pixbuf
98 } Background;
99 
100 #define BG_SOLID 0
101 #define BG_PIXMAP 1
102 #define BG_PDF 2
103 
104 #define RULING_NONE 0
105 #define RULING_LINED 1
106 #define RULING_RULED 2
107 #define RULING_GRAPH 3
108 
109 #define DOMAIN_ABSOLUTE 0
110 #define DOMAIN_ATTACH 1
111 #define DOMAIN_CLONE 2  // only while loading file
112 
113 typedef struct Brush {
114   int tool_type;
115   int color_no;
116   guint color_rgba;
117   int thickness_no;
118   double thickness;
119   int tool_options;
120   gboolean ruler, recognizer, variable_width;
121 } Brush;
122 
123 #define COLOR_BLACK      0
124 #define COLOR_BLUE       1
125 #define COLOR_RED        2
126 #define COLOR_GREEN      3
127 #define COLOR_GRAY       4
128 #define COLOR_LIGHTBLUE  5
129 #define COLOR_LIGHTGREEN 6
130 #define COLOR_MAGENTA    7
131 #define COLOR_ORANGE     8
132 #define COLOR_YELLOW     9
133 #define COLOR_WHITE     10
134 #define COLOR_OTHER     -1
135 #define COLOR_MAX       11
136 
137 extern guint predef_colors_rgba[COLOR_MAX];
138 extern guint predef_bgcolors_rgba[COLOR_MAX];
139 
140 #define THICKNESS_VERYFINE  0
141 #define THICKNESS_FINE      1
142 #define THICKNESS_MEDIUM    2
143 #define THICKNESS_THICK     3
144 #define THICKNESS_VERYTHICK 4
145 #define THICKNESS_MAX       5
146 
147 #define TOOL_PEN          0
148 #define TOOL_ERASER       1
149 #define TOOL_HIGHLIGHTER  2
150 #define TOOL_TEXT         3
151 #define TOOL_SELECTREGION 4
152 #define TOOL_SELECTRECT   5
153 #define TOOL_VERTSPACE    6
154 #define TOOL_HAND         7
155 #define TOOL_IMAGE        8
156 #define NUM_STROKE_TOOLS  3
157 #define NUM_TOOLS         9
158 #define NUM_BUTTONS       3
159 
160 #define TOOLOPT_ERASER_STANDARD     0
161 #define TOOLOPT_ERASER_WHITEOUT     1
162 #define TOOLOPT_ERASER_STROKES      2
163 
164 extern double predef_thickness[NUM_STROKE_TOOLS][THICKNESS_MAX];
165 
166 typedef struct BBox {
167   double left, right, top, bottom;
168 } BBox;
169 
170 struct UndoErasureData;
171 
172 typedef struct Item {
173   int type;
174   struct Brush brush; // the brush to use, if ITEM_STROKE
175   // 'brush' also contains color info for text items
176   GnomeCanvasPoints *path;
177   gdouble *widths;
178   GnomeCanvasItem *canvas_item; // the corresponding canvas item, or NULL
179   struct BBox bbox;
180   struct UndoErasureData *erasure; // for temporary use during erasures
181   // the following fields for ITEM_TEXT:
182   gchar *text;
183   gchar *font_name;
184   gdouble font_size;
185   GtkWidget *widget; // the widget while text is being edited (ITEM_TEMP_TEXT)
186   // the following fields for ITEM_IMAGE:
187   GdkPixbuf *image;  // the image
188   gchar *image_png;  // PNG of original image, for save and clipboard
189   gsize image_png_len;
190 } Item;
191 
192 // item type values for Item.type, UndoItem.type, ui.cur_item_type ...
193 // (not all are valid in all places)
194 #define ITEM_NONE -1
195 #define ITEM_STROKE 0
196 #define ITEM_TEMP_STROKE 1
197 #define ITEM_ERASURE 2
198 #define ITEM_SELECTRECT 3
199 #define ITEM_MOVESEL 4
200 #define ITEM_PASTE 5
201 #define ITEM_NEW_LAYER 6
202 #define ITEM_DELETE_LAYER 7
203 #define ITEM_NEW_BG_ONE 8
204 #define ITEM_NEW_BG_RESIZE 9
205 #define ITEM_PAPER_RESIZE 10
206 #define ITEM_NEW_DEFAULT_BG 11
207 #define ITEM_NEW_PAGE 13
208 #define ITEM_DELETE_PAGE 14
209 #define ITEM_REPAINTSEL 15
210 #define ITEM_MOVESEL_VERT 16
211 #define ITEM_HAND 17
212 #define ITEM_TEXT 18
213 #define ITEM_TEMP_TEXT 19
214 #define ITEM_TEXT_EDIT 20
215 #define ITEM_TEXT_ATTRIB 21
216 #define ITEM_RESIZESEL 22
217 #define ITEM_RECOGNIZER 23
218 #define ITEM_IMAGE 24
219 #define ITEM_SELECTREGION 25
220 #define ITEM_TEXT_PENDING 26 // will do when button release
221 #define ITEM_IMAGE_PENDING 27 // will do when button release
222 
223 typedef struct Layer {
224   GList *items; // the items on the layer, from bottom to top
225   int nitems;
226   GnomeCanvasGroup *group;
227 } Layer;
228 
229 typedef struct Page {
230   GList *layers; // the layers on the page
231   int nlayers;
232   double height, width;
233   double hoffset, voffset; // offsets of canvas group rel. to canvas root
234   struct Background *bg;
235   GnomeCanvasGroup *group;
236 } Page;
237 
238 typedef struct Journal {
239   GList *pages;  // the pages in the journal
240   int npages;
241   int last_attach_no; // for naming of attached backgrounds
242 } Journal;
243 
244 typedef struct Selection {
245   int type;  // ITEM_SELECTRECT, ITEM_MOVESEL_VERT, ITEM_SELECTREGION
246   BBox bbox; // the rectangle bbox of the selection
247   struct Layer *layer; // the layer on which the selection lives
248   double anchor_x, anchor_y, last_x, last_y; // for selection motion
249   gboolean resizing_top, resizing_bottom, resizing_left, resizing_right; // for selection resizing
250   double new_x1, new_x2, new_y1, new_y2; // for selection resizing
251   GnomeCanvasItem *canvas_item; // if the selection box is on screen
252   GList *items; // the selected items (a list of struct Item)
253   int move_pageno, orig_pageno; // if selection moves to a different page
254   struct Layer *move_layer;
255   float move_pagedelta;
256 } Selection;
257 
258 typedef struct UIData {
259   int pageno, layerno; // the current page and layer
260   struct Page *cur_page;
261   struct Layer *cur_layer;
262   gboolean saved; // is file saved ?
263   struct Brush *cur_brush;  // the brush in use (one of brushes[...])
264   int toolno[NUM_BUTTONS+2];  // the number of the currently selected tool; two more reserved for eraser tip and touch device
265   struct Brush brushes[NUM_BUTTONS+1][NUM_STROKE_TOOLS]; // the current pen, eraser, hiliter
266   struct Brush default_brushes[NUM_STROKE_TOOLS]; // the default ones
267   int linked_brush[NUM_BUTTONS+1]; // whether brushes are linked across buttons
268   int cur_mapping; // the current button number for mappings
269   gboolean button_switch_mapping; // button clicks switch button 1 mappings
270   gboolean use_erasertip;
271   gboolean touch_as_handtool; // always map touch device to hand tool?
272   gboolean pen_disables_touch; // pen proximity should disable touch device?
273   gboolean in_proximity;
274   char *device_for_touch;
275   int which_mouse_button; // the mouse button drawing the current path
276   int which_unswitch_button; // if button_switch_mapping, the mouse button that switched the mapping
277   struct Page default_page;  // the model for the default page
278   int layerbox_length;  // the number of entries registered in the layers combo-box
279   struct Item *cur_item; // the item being drawn, or NULL
280   int cur_item_type;
281   GnomeCanvasPoints cur_path; // the path being drawn
282   gdouble *cur_widths; // width array for the path being drawn
283   int cur_path_storage_alloc;
284   int cur_widths_storage_alloc;
285   double zoom; // zoom factor, in pixels per pt
286   gboolean use_xinput; // use input devices instead of core pointer
287   gboolean allow_xinput; // allow use of xinput ?
288   gboolean discard_corepointer; // discard core pointer events in XInput mode
289   gboolean pressure_sensitivity; // use pen pressure to control stroke width?
290   double width_minimum_multiplier, width_maximum_multiplier; // calibration for pressure sensitivity
291   gboolean is_corestroke; // this stroke is painted with core pointer
292   gboolean saved_is_corestroke;
293   GdkDevice *stroke_device; // who's painting this stroke
294   gboolean ignore_other_devices;
295   gboolean ignore_btn_reported_up; // config setting: ignore button reported up
296   gboolean current_ignore_btn_reported_up;
297   int screen_width, screen_height; // initial screen size, for XInput events
298   double hand_refpt[2];
299   int hand_scrollto_cx, hand_scrollto_cy;
300   gboolean hand_scrollto_pending;
301   char *filename;
302   gchar *default_path; // default path for new notes
303   gchar *default_image; // path for previous image
304   gboolean fullscreen, maximize_at_start;
305   int view_continuous; // view mode is continuous ?
306   gboolean in_update_page_stuff; // semaphore to avoid scrollbar retroaction
307   struct Selection *selection;
308   GdkCursor *cursor;
309   GdkPixbuf *pen_cursor_pix, *hiliter_cursor_pix;
310   gboolean pen_cursor; // use pencil cursor (default is a dot in current color)
311   gboolean progressive_bg; // update PDF bg's one at a time
312   char *mrufile, *configfile; // file names for MRU & config
313   char *mru[MRU_SIZE]; // MRU data
314   GtkWidget *mrumenu[MRU_SIZE];
315   gboolean bg_apply_all_pages;
316   int window_default_width, window_default_height, scrollbar_step_increment;
317   gboolean print_ruling; // print the paper ruling ?
318   gboolean exportpdf_prefer_legacy; // prefer legacy code for export-to-pdf?
319   gboolean exportpdf_layers; // export PDF one layer at a time
320   gboolean new_page_bg_from_pdf; // do new pages get a duplicated PDF/image background?
321   int default_unit; // the default unit for paper sizes
322   int startuptool; // the default tool at startup
323   int zoom_step_increment; // the increment in the zoom dialog box
324   double zoom_step_factor; // the multiplicative factor in zoom in/out
325   double startup_zoom;
326   gboolean autoload_pdf_xoj;
327   gboolean autocreate_new_xoj;
328   gboolean autosave_enabled, autosave_loop_running, autosave_need_catchup;
329   GList *autosave_filename_list;
330   int autosave_delay;
331   gboolean need_autosave;
332 #if GLIB_CHECK_VERSION(2,6,0)
333   GKeyFile *config_data;
334 #endif
335   int vertical_order[2][VBOX_MAIN_NITEMS]; // the order of interface components
336   gchar *default_font_name, *font_name;
337   gdouble default_font_size, font_size;
338   gulong resize_signal_handler;
339   gdouble hiliter_opacity;
340   guint hiliter_alpha_mask;
341   gboolean left_handed; // left-handed mode?
342   gboolean auto_save_prefs; // auto-save preferences ?
343   gboolean shorten_menus; // shorten menus ?
344   gchar *shorten_menu_items; // which items to hide
345   gboolean is_sel_cursor; // displaying a selection-related cursor
346   gint pre_fullscreen_width, pre_fullscreen_height; // for win32 fullscreen
347 #if GTK_CHECK_VERSION(2,10,0)
348   GtkPrintSettings *print_settings;
349 #endif
350   gboolean poppler_force_cairo; // force poppler to use cairo
351   gboolean warned_generate_fontconfig; // for win32 fontconfig cache
352 } UIData;
353 
354 #define BRUSH_LINKED 0
355 #define BRUSH_COPIED 1
356 #define BRUSH_STATIC 2
357 
358 typedef struct UndoErasureData {
359   struct Item *item; // the item that got erased
360   int npos; // its position in its layer
361   int nrepl; // the number of replacement items
362   GList *replacement_items;
363 } UndoErasureData;
364 
365 typedef struct UndoItem {
366   int type;
367   struct Item *item; // for ITEM_STROKE, ITEM_TEXT, ITEM_TEXT_EDIT, ITEM_TEXT_ATTRIB, ITEM_IMAGE
368   struct Layer *layer; // for ITEM_STROKE, ITEM_ERASURE, ITEM_PASTE, ITEM_NEW_LAYER, ITEM_DELETE_LAYER, ITEM_MOVESEL, ITEM_TEXT, ITEM_TEXT_EDIT, ITEM_RECOGNIZER, ITEM_IMAGE
369   struct Layer *layer2; // for ITEM_DELETE_LAYER with val=-1, ITEM_MOVESEL
370   struct Page *page;  // for ITEM_NEW_BG_ONE/RESIZE, ITEM_NEW_PAGE, ITEM_NEW_LAYER, ITEM_DELETE_LAYER, ITEM_DELETE_PAGE
371   GList *erasurelist; // for ITEM_ERASURE, ITEM_RECOGNIZER
372   GList *itemlist;  // for ITEM_MOVESEL, ITEM_PASTE, ITEM_REPAINTSEL, ITEM_RESIZESEL
373   GList *auxlist;   // for ITEM_REPAINTSEL (brushes), ITEM_MOVESEL (depths)
374   struct Background *bg;  // for ITEM_NEW_BG_ONE/RESIZE, ITEM_NEW_DEFAULT_BG
375   int val; // for ITEM_NEW_PAGE, ITEM_NEW_LAYER, ITEM_DELETE_LAYER, ITEM_DELETE_PAGE
376   double val_x, val_y; // for ITEM_MOVESEL, ITEM_NEW_BG_RESIZE, ITEM_PAPER_RESIZE, ITEM_NEW_DEFAULT_BG, ITEM_TEXT_ATTRIB, ITEM_RESIZESEL
377   double scaling_x, scaling_y; // for ITEM_RESIZESEL
378   gchar *str; // for ITEM_TEXT_EDIT, ITEM_TEXT_ATTRIB
379   struct Brush *brush; // for ITEM_TEXT_ATTRIB
380   struct UndoItem *next;
381   int multiop;
382 } UndoItem;
383 
384 #define MULTIOP_CONT_REDO 1 // not the last in a multiop, so keep redoing
385 #define MULTIOP_CONT_UNDO 2 // not the first in a multiop, so keep undoing
386 
387 
388 typedef struct BgPdfRequest {
389   int pageno;
390   double dpi;
391 } BgPdfRequest;
392 
393 typedef struct BgPdfPage {
394   double dpi;
395   GdkPixbuf *pixbuf;
396   int pixel_height, pixel_width; // pixel size of pixbuf
397 } BgPdfPage;
398 
399 typedef struct BgPdf {
400   int status; // the rest only makes sense if this is not STATUS_NOT_INIT
401   guint pid; // the identifier of the idle callback
402   Refstring *filename;
403   int file_domain;
404   gchar *file_contents; // buffer containing a copy of file data
405   gsize file_length;  // size of above buffer
406   int npages;
407   GList *pages; // a list of BgPdfPage structures
408   GList *requests; // a list of BgPdfRequest structures
409   gboolean has_failed; // has failed in the past...
410   PopplerDocument *document; // the poppler document
411 } BgPdf;
412 
413 #define STATUS_NOT_INIT 0
414 #define STATUS_READY    1  // things are initialized and can work
415 // there used to be more possible values, things got streamlined...
416 
417 // UTILITY MACROS
418 
419 // getting a component of the interface by name
420 #define GET_COMPONENT(a)  GTK_WIDGET (g_object_get_data(G_OBJECT (winMain), a))
421 
422 // the margin between consecutive pages in continuous view
423 #define VIEW_CONTINUOUS_SKIP 20.0
424 
425 #define VIEW_MODE_ONE_PAGE 0
426 #define VIEW_MODE_CONTINUOUS 1
427 #define VIEW_MODE_HORIZONTAL 2
428 
429 // GLOBAL VARIABLES
430 
431 // the main window and the canvas
432 
433 extern GtkWidget *winMain;
434 extern GnomeCanvas *canvas;
435 
436 // the data
437 
438 extern struct Journal journal;
439 extern struct UIData ui;
440 extern struct BgPdf bgpdf;
441 extern struct UndoItem *undo, *redo;
442 
443 extern double DEFAULT_ZOOM;
444 
445 #define UNIT_CM 0
446 #define UNIT_IN 1
447 #define UNIT_PX 2
448 #define UNIT_PT 3
449