1 /* canvas.h 2 Copyright (C) 2004-2020 Mark Tyler and Dmitry Groshev 3 4 This file is part of mtPaint. 5 6 mtPaint is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 mtPaint is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with mtPaint in the file COPYING. 18 */ 19 20 float can_zoom; // Zoom factor 1..MAX_ZOOM 21 int margin_main_xy[2]; // Top left of image from top left of canvas 22 int margin_view_xy[2]; 23 int marq_status, marq_xy[4]; // Selection marquee 24 int marq_drag_x, marq_drag_y; // Marquee dragging offset 25 int line_status, line_xy[4]; // Line tool 26 int poly_status; // Polygon selection tool 27 int clone_status, clone_x, clone_y, clone_dx, clone_dy; // Clone tool state 28 int clone_mode, clone_x0, clone_y0, clone_dx0, clone_dy0; // Clone settings 29 int brush_spacing; // Step in non-continuous mode 30 int lasso_sel; // Lasso by selection channel 31 32 #define margin_main_x margin_main_xy[0] 33 #define margin_main_y margin_main_xy[1] 34 35 #define margin_view_x margin_view_xy[0] 36 #define margin_view_y margin_view_xy[1] 37 38 #define marq_x1 marq_xy[0] 39 #define marq_y1 marq_xy[1] 40 #define marq_x2 marq_xy[2] 41 #define marq_y2 marq_xy[3] 42 43 #define line_x1 line_xy[0] 44 #define line_y1 line_xy[1] 45 #define line_x2 line_xy[2] 46 #define line_y2 line_xy[3] 47 48 #define MAX_RECENT 20 49 int recent_files; // Current recent files setting 50 char *recent_filenames[MAX_RECENT]; // Recent filenames themselves 51 52 int preserved_gif_delay, undo_load; 53 54 #define STATUS_ITEMS 5 55 #define STATUS_GEOMETRY 0 56 #define STATUS_CURSORXY 1 57 #define STATUS_PIXELRGB 2 58 #define STATUS_SELEGEOM 3 59 #define STATUS_UNDOREDO 4 60 61 void **label_bar[STATUS_ITEMS]; 62 63 int col_reverse, // Painting with right button 64 show_paste, // Show contents of clipboard while pasting 65 status_on[STATUS_ITEMS], // Show status bar items? 66 text_paste, // Are we pasting text? 67 canvas_image_centre, // Are we centering the image? 68 chequers_optimize, // Are we optimizing the chequers for speed? 69 cursor_zoom // Are we zooming at cursor position? 70 ; 71 72 #define LINE_NONE 0 73 #define LINE_START 1 74 #define LINE_LINE 2 75 76 #define MARQUEE_NONE 0 77 #define MARQUEE_SELECTING 1 78 #define MARQUEE_DONE 2 79 #define MARQUEE_PASTE 3 80 #define MARQUEE_PASTE_DRAG 4 81 82 #define POLY_NONE 0 83 #define POLY_SELECTING 1 84 #define POLY_DRAGGING 2 85 #define POLY_DONE 3 86 87 #define GRAD_NONE 0 88 #define GRAD_START 1 89 #define GRAD_END 2 90 #define GRAD_DONE 3 91 92 #define CLONE_REL 0 93 #define CLONE_ABS 1 94 #define CLONE_TRACK 2 /* Reposition flag */ 95 #define CLONE_DRAG 4 /* Stroke flag */ 96 97 #define MIN_ZOOM 0.1 98 #define MAX_ZOOM 80 99 100 /* File selector codes */ 101 enum { 102 FS_PNG_LOAD = 1, 103 FS_PNG_SAVE, 104 FS_PALETTE_LOAD, 105 FS_PALETTE_SAVE, 106 FS_CLIP_FILE, 107 FS_EXPORT_UNDO, 108 FS_EXPORT_UNDO2, 109 FS_EXPORT_ASCII, 110 FS_LAYER_SAVE, 111 FS_EXPLODE_FRAMES, 112 FS_EXPORT_GIF, 113 FS_CHANNEL_LOAD, 114 FS_CHANNEL_SAVE, 115 FS_COMPOSITE_SAVE, 116 FS_SELECT_FILE, 117 FS_SELECT_DIR, 118 FS_LAYER_LOAD, 119 FS_PATTERN_LOAD, 120 FS_CLIPBOARD, 121 FS_PALETTE_DEF 122 }; 123 124 int do_a_load_x(char *fname, int undo, void *v); 125 #define do_a_load(A,B) do_a_load_x(A, B, NULL) 126 void canvas_center(float ic[2]); 127 void align_size(float new_zoom); 128 void realign_size(); 129 int ftype_selector(int mask, char *ext, int def, char **names, int *ftypes); 130 int tiff_type_selector(int mask, int def, char **names); 131 void init_ls_settings(ls_settings *settings, void **wdata); 132 void fs_setup(GtkWidget *fs, int action_type); 133 void file_selector_x(int action_type, void **xdata); 134 void file_selector(int action_type); 135 void set_new_filename(int layer, char *fname); 136 137 void pressed_do_undo(int redo, int cnt); 138 139 void line_to_gradient(); // Setup gradient along line tool 140 141 /* Tool action codes */ 142 enum { 143 TC_NONE = 0, 144 TC_LINE_START, 145 TC_LINE_ARROW, 146 TC_LINE_NEXT, 147 TC_LINE_STOP, 148 TC_SEL_CLEAR, 149 TC_SEL_START, 150 TC_SEL_SET_0, 151 TC_SEL_SET_1, 152 TC_SEL_SET_2, 153 TC_SEL_SET_3, 154 TC_SEL_TO, 155 TC_SEL_STOP, 156 TC_POLY_START, 157 TC_POLY_START_D, 158 TC_POLY_ADD, 159 TC_POLY_DEL, 160 TC_POLY_CLOSE, 161 TC_PASTE_DRAG, 162 TC_PASTE_COMMIT, 163 TC_PASTE_PAINT, 164 TC_PASTE_PSWAP, 165 TC_PAINT, 166 TC_PAINT_B, 167 TC_GRAD_START, 168 TC_GRAD_SET0, 169 TC_GRAD_SET1, 170 TC_GRAD_PICK0, 171 TC_GRAD_PICK1, 172 TC_GRAD_DRAG0, 173 TC_GRAD_DRAG1, 174 TC_GRAD_CLEAR, 175 }; 176 177 #define TC_OPMASK 0x0FF 178 #define TCF_PRES 0x100 /* Use pressure */ 179 #define TCF_ONCE 0x200 /* Skip repeats */ 180 181 void do_tool_action(int cmd, int x, int y, int pressure); // Paint some pixels! 182 int tool_action(int count, int button, int x, int y); // Decide what to paint 183 void update_menus(); // Update undo/edit menu 184 185 int close_to( int x1, int y1 ); 186 187 #define MARQ_SHOW 0 188 #define MARQ_SNAP 1 /* show+snap */ 189 #define MARQ_HIDE 2 190 #define MARQ_MOVE 3 /* +snap */ 191 #define MARQ_SIZE 4 /* +snap */ 192 193 void paint_marquee(int action, int new_x, int new_y, rgbcontext *ctx); // Draw/clear marquee 194 void paint_poly_marquee(rgbcontext *ctx); // Paint polygon marquee 195 void stretch_poly_line(int x, int y); // Clear old temp line, draw next temp line 196 197 void update_sel_bar(int now); // Update selection stats on status bar 198 void update_xy_bar(int x, int y); // Update cursor tracking on status bar 199 void init_status_bar(); // Initialize status bar 200 201 void pressed_lasso(int cut); 202 void pressed_copy(int cut); 203 void pressed_paste(int centre); 204 void pressed_greyscale(int mode); 205 void pressed_convert_rgb(); 206 void pressed_inv(int what); 207 void pressed_map(); 208 void pressed_rectangle(int filled); 209 void pressed_ellipse(int filled); 210 211 void pressed_edge_detect(); 212 void pressed_sharpen(); 213 void pressed_soften(); 214 void pressed_fx(int what); 215 void pressed_gauss(); 216 void pressed_unsharp(); 217 void pressed_dog(); 218 void pressed_kuwahara(); 219 220 void pressed_clip_alpha_scale(); 221 void pressed_clip_alphamask(); 222 void pressed_clip_mask(int val); 223 void pressed_clip_mask_all(); 224 void pressed_clip_mask_clear(); 225 226 void pressed_flip_image_v(); 227 void pressed_flip_image_h(); 228 void pressed_flip_sel_v(); 229 void pressed_flip_sel_h(); 230 231 void pressed_rotate_image(int dir); 232 void pressed_rotate_sel(int dir); 233 void pressed_rotate_free(); 234 235 void iso_trans(int mode); 236 237 void marquee_at(int *rect); // Read marquee location & size 238 void check_marquee(); 239 void commit_paste(int swap, int *update); 240 241 void repaint_grad(const int *old); // Repaint gradient line and maybe clear old one 242 void repaint_line(const int *old); // Repaint line on canvas and maybe clear old one 243 void refresh_line(int mode, const int *lxy, rgbcontext *ctx); // Refresh a part of line/gradient if needed 244 void register_file( char *filename ); // Called after successful load/save 245 void update_recent_files(int save); // Update the menu items / save to inifile 246 247 void create_default_image(); // Create default new image 248 249 /// UPDATE STUFF 250 251 /* Atomic updates */ 252 253 #define CF_NAME 0x00000001 /* Name in titlebar */ 254 #define CF_GEOM 0x00000002 /* Image geometry */ 255 #define CF_CGEOM 0x00000004 /* Clipboard geometry */ 256 #define CF_PAL 0x00000008 /* Palette */ 257 #define CF_CAB 0x00000010 /* Current channel's A & B (w/redraw) */ 258 #define CF_AB 0x00000020 /* Colors A & B */ 259 #define CF_GRAD 0x00000040 /* Gradients */ 260 #define CF_MENU 0x00000080 /* Menus and controls */ 261 #define CF_SET 0x00000100 /* Settings toolbar */ 262 #define CF_IMGBAR 0x00000200 /* Image statusbar */ 263 #define CF_SELBAR 0x00000400 /* Selection statusbar */ 264 #define CF_PIXEL 0x00000800 /* Pixel statusbar */ 265 #define CF_PREFS 0x00001000 /* Preferences */ 266 #define CF_CURSOR 0x00002000 /* Cursor */ 267 #define CF_PMODE 0x00004000 /* Paste preview */ 268 #define CF_GMODE 0x00008000 /* Gradient preview */ 269 #define CF_DRAW 0x00010000 /* Image window */ 270 #define CF_VDRAW 0x00020000 /* View window */ 271 #define CF_PDRAW 0x00040000 /* Palette window */ 272 #define CF_TDRAW 0x00080000 /* Color/brush/pattern window */ 273 #define CF_ALIGN 0x00100000 /* Realign image window */ 274 #define CF_VALIGN 0x00200000 /* Realign view window */ 275 #define CF_TRANS 0x00400000 /* Transparent color in layers window */ 276 #define CF_NOW 0x00800000 /* Do not wait for cumulative update */ 277 278 /* Compound updates */ 279 280 // Changed image contents 281 #define UPD_IMG (CF_DRAW | CF_VDRAW | CF_PIXEL) 282 // Changed image geometry (+undo) 283 #define UPD_GEOM (CF_GEOM | CF_MENU | CF_IMGBAR | UPD_IMG | UPD_SGEOM) 284 // Added a new channel (+) 285 #define UPD_ADDCH (CF_SET | CF_MENU | CF_IMGBAR | UPD_IMG) 286 // Deleted an existing channel (+) 287 #define UPD_DELCH UPD_ADDCH 288 // Switched to new channel (+) 289 #define UPD_NEWCH (UPD_ADDCH | UPD_CAB | CF_SELBAR) 290 // Switched to existing channel 291 #define UPD_CHAN UPD_NEWCH /* May avoid view window redraw, but won't bother */ 292 // Changed color or value A or B 293 #define UPD_CAB CF_CAB 294 // Changed color A or B 295 #define UPD_AB (CF_AB | CF_GRAD | CF_SET | CF_GMODE | CF_TDRAW | CF_PDRAW) 296 // Changed pattern 297 #define UPD_PAT (CF_AB | CF_TDRAW) 298 // Changed A, B, and pattern 299 #define UPD_ABP UPD_AB 300 // Changed palette 301 #define UPD_PAL (CF_PAL | CF_IMGBAR | CF_PDRAW | UPD_AB | UPD_IMG) 302 // Changed palette and transparent color 303 #define UPD_TPAL (UPD_PAL | CF_TRANS) 304 // Changed drawing mode in some way 305 #define UPD_MODE (CF_PMODE | CF_GMODE) 306 // Toggled gradient mode 307 #define UPD_GMODE CF_DRAW 308 // Changed palette if indexed / image if RGB 309 #define UPD_COL UPD_PAL /* May avoid palette update for RGB, but... */ 310 // Changed image contents (+undo, -redraw) 311 #define UPD_IMGP (CF_MENU | CF_PIXEL) 312 // Copied selection to clipboard 313 #define UPD_COPY CF_MENU 314 // Imported clipboard 315 #define UPD_XCOPY CF_MENU 316 // Converted indexed image to RGB 317 #define UPD_2RGB (CF_MENU | CF_IMGBAR | UPD_IMG | UPD_AB) 318 // Converted RGB image to indexed 319 #define UPD_2IDX (UPD_2RGB | UPD_PAL) 320 // Created or loaded a new image (+) 321 #define UPD_ALL (UPD_GEOM | UPD_PAL | UPD_CAB | CF_TRANS) 322 // Changed clipboard contents 323 #define UPD_CLIP CF_PMODE 324 // Changed rendering options 325 #define UPD_RENDER CF_DRAW 326 // Changed viewing options 327 #define UPD_VIEW CF_VDRAW 328 // Update all views 329 #define UPD_ALLV (CF_DRAW | CF_VDRAW) 330 // Changed clipboard geometry 331 #define UPD_CGEOM (CF_CGEOM | CF_SELBAR) 332 // Changed polygonal selection (-redraw) 333 #define UPD_PSEL (CF_MENU | CF_SELBAR) 334 // Changed selection (-) 335 #define UPD_SEL (UPD_PSEL | CF_CURSOR) 336 // Changed selection geometry (-) 337 #define UPD_SGEOM UPD_PSEL 338 // Initiated pasting something 339 #define UPD_PASTE (UPD_SEL | CF_DRAW | CF_CGEOM) 340 // Changed filename 341 #define UPD_NAME CF_NAME 342 // Changed transparent color (+undo) 343 #define UPD_TRANS (UPD_IMG | CF_IMGBAR | CF_MENU | CF_TRANS) 344 // Changed image contents (+) 345 #define UPD_UIMG (UPD_IMG | CF_MENU) 346 // Cut selection 347 #define UPD_CUT UPD_UIMG 348 // Switched layers 349 #define UPD_LAYER (UPD_ALL | CF_NAME | CF_VALIGN) 350 // Changed color masking 351 #define UPD_CMASK (CF_PDRAW | UPD_MODE) 352 // Changed tool opacity 353 #define UPD_OPAC (CF_GRAD | CF_SET | UPD_MODE) 354 // Done "reset tools" (in addition to UPD_ALL) 355 #define UPD_RESET (CF_NAME | CF_ALIGN | UPD_PAL | UPD_OPAC) 356 // Changed brush 357 #define UPD_BRUSH (CF_SET | CF_CURSOR | CF_TDRAW) 358 // Changed gradient 359 #define UPD_GRAD (CF_GRAD | CF_SET | CF_GMODE) 360 // Changed preferences 361 #define UPD_PREFS (CF_PREFS | CF_MENU | CF_CURSOR | CF_DRAW | CF_VDRAW) 362 // Moved color in palette (no image redraw desired) 363 #define UPD_MVPAL (UPD_PAL & ~UPD_IMG) 364 // Mask covering all kinds of image redraw - for disabling them 365 #define UPD_IMGMASK (UPD_MODE | CF_DRAW | CF_VDRAW) 366 // Changed palette (+undo) 367 #define UPD_UPAL (UPD_PAL | CF_MENU) 368 // !!! Do not forget: CF_MENU also tracks undo stack changes 369 370 void update_stuff(int flags); 371