1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995-2001 Spencer Kimball, Peter Mattis, and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "tools-types.h"
#include "core/gimpimage.h"
#include "core/gimppickable.h"
#include "display/gimpcanvas.h"
#include "display/gimpcanvasarc.h"
#include "display/gimpcanvasboundary.h"
#include "display/gimpcanvasgroup.h"
#include "display/gimpcanvasguide.h"
#include "display/gimpcanvashandle.h"
#include "display/gimpcanvasitem-utils.h"
#include "display/gimpcanvasline.h"
#include "display/gimpcanvaspen.h"
#include "display/gimpcanvaspolygon.h"
#include "display/gimpcanvasrectangle.h"
#include "display/gimpcanvassamplepoint.h"
#include "display/gimpcanvastextcursor.h"
#include "display/gimpcanvastransformpreview.h"
#include "display/gimpcanvastext.h"
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-items.h"
#include "display/gimpdisplayshell-transform.h"
#include "display/gimptoolwidget.h"
#include "gimpdrawtool.h"
#include "gimptoolcontrol.h"
#define USE_TIMEOUT
#define DRAW_FPS 120
#define DRAW_TIMEOUT (1000 /* milliseconds */ / (2 * DRAW_FPS))
#define MINIMUM_DRAW_INTERVAL (G_TIME_SPAN_SECOND / DRAW_FPS)
static void gimp_draw_tool_dispose (GObject *object);
static gboolean gimp_draw_tool_has_display (GimpTool *tool,
GimpDisplay *display);
static GimpDisplay * gimp_draw_tool_has_image (GimpTool *tool,
GimpImage *image);
static void gimp_draw_tool_control (GimpTool *tool,
GimpToolAction action,
GimpDisplay *display);
static gboolean gimp_draw_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display);
static gboolean gimp_draw_tool_key_release (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display);
static void gimp_draw_tool_modifier_key (GimpTool *tool,
GdkModifierType key,
gboolean press,
GdkModifierType state,
GimpDisplay *display);
static void gimp_draw_tool_active_modifier_key
(GimpTool *tool,
GdkModifierType key,
gboolean press,
GdkModifierType state,
GimpDisplay *display);
static void gimp_draw_tool_oper_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity,
GimpDisplay *display);
static void gimp_draw_tool_cursor_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display);
static GimpUIManager * gimp_draw_tool_get_popup (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display,
const gchar **ui_path);
static void gimp_draw_tool_widget_status (GimpToolWidget *widget,
const gchar *status,
GimpTool *tool);
static void gimp_draw_tool_widget_status_coords
(GimpToolWidget *widget,
const gchar *title,
gdouble x,
const gchar *separator,
gdouble y,
const gchar *help,
GimpTool *tool);
static void gimp_draw_tool_widget_message
(GimpToolWidget *widget,
const gchar *message,
GimpTool *tool);
static void gimp_draw_tool_widget_snap_offsets
(GimpToolWidget *widget,
gint offset_x,
gint offset_y,
gint width,
gint height,
GimpTool *tool);
static void gimp_draw_tool_draw (GimpDrawTool *draw_tool);
static void gimp_draw_tool_undraw (GimpDrawTool *draw_tool);
static void gimp_draw_tool_real_draw (GimpDrawTool *draw_tool);
G_DEFINE_TYPE (GimpDrawTool, gimp_draw_tool, GIMP_TYPE_TOOL)
#define parent_class gimp_draw_tool_parent_class
static void
gimp_draw_tool_class_init (GimpDrawToolClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
object_class->dispose = gimp_draw_tool_dispose;
tool_class->has_display = gimp_draw_tool_has_display;
tool_class->has_image = gimp_draw_tool_has_image;
tool_class->control = gimp_draw_tool_control;
tool_class->key_press = gimp_draw_tool_key_press;
tool_class->key_release = gimp_draw_tool_key_release;
tool_class->modifier_key = gimp_draw_tool_modifier_key;
tool_class->active_modifier_key = gimp_draw_tool_active_modifier_key;
tool_class->oper_update = gimp_draw_tool_oper_update;
tool_class->cursor_update = gimp_draw_tool_cursor_update;
tool_class->get_popup = gimp_draw_tool_get_popup;
klass->draw = gimp_draw_tool_real_draw;
}
static void
gimp_draw_tool_init (GimpDrawTool *draw_tool)
{
draw_tool->display = NULL;
draw_tool->paused_count = 0;
draw_tool->preview = NULL;
draw_tool->item = NULL;
}
static void
gimp_draw_tool_dispose (GObject *object)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (object);
if (draw_tool->draw_timeout)
{
g_source_remove (draw_tool->draw_timeout);
draw_tool->draw_timeout = 0;
}
gimp_draw_tool_set_widget (draw_tool, NULL);
gimp_draw_tool_set_default_status (draw_tool, NULL);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static gboolean
gimp_draw_tool_has_display (GimpTool *tool,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
return (display == draw_tool->display ||
GIMP_TOOL_CLASS (parent_class)->has_display (tool, display));
}
static GimpDisplay *
gimp_draw_tool_has_image (GimpTool *tool,
GimpImage *image)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
GimpDisplay *display;
display = GIMP_TOOL_CLASS (parent_class)->has_image (tool, image);
if (! display && draw_tool->display)
{
if (image && gimp_display_get_image (draw_tool->display) == image)
display = draw_tool->display;
/* NULL image means any display */
if (! image)
display = draw_tool->display;
}
return display;
}
static void
gimp_draw_tool_control (GimpTool *tool,
GimpToolAction action,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
switch (action)
{
case GIMP_TOOL_ACTION_PAUSE:
case GIMP_TOOL_ACTION_RESUME:
break;
case GIMP_TOOL_ACTION_HALT:
if (gimp_draw_tool_is_active (draw_tool))
gimp_draw_tool_stop (draw_tool);
gimp_draw_tool_set_widget (draw_tool, NULL);
break;
case GIMP_TOOL_ACTION_COMMIT:
break;
}
GIMP_TOOL_CLASS (parent_class)->control (tool, action, display);
}
static gboolean
gimp_draw_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (draw_tool->widget && display == draw_tool->display)
{
return gimp_tool_widget_key_press (draw_tool->widget, kevent);
}
return GIMP_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
}
static gboolean
gimp_draw_tool_key_release (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (draw_tool->widget && display == draw_tool->display)
{
return gimp_tool_widget_key_release (draw_tool->widget, kevent);
}
return GIMP_TOOL_CLASS (parent_class)->key_release (tool, kevent, display);
}
static void
gimp_draw_tool_modifier_key (GimpTool *tool,
GdkModifierType key,
gboolean press,
GdkModifierType state,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (draw_tool->widget && display == draw_tool->display)
{
gimp_tool_widget_hover_modifier (draw_tool->widget, key, press, state);
}
GIMP_TOOL_CLASS (parent_class)->modifier_key (tool, key, press, state,
display);
}
static void
gimp_draw_tool_active_modifier_key (GimpTool *tool,
GdkModifierType key,
gboolean press,
GdkModifierType state,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (draw_tool->widget && display == draw_tool->display)
{
gimp_tool_widget_motion_modifier (draw_tool->widget, key, press, state);
}
GIMP_TOOL_CLASS (parent_class)->active_modifier_key (tool, key, press, state,
display);
}
static void
gimp_draw_tool_oper_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (draw_tool->widget && display == draw_tool->display)
{
gimp_tool_widget_hover (draw_tool->widget, coords, state, proximity);
}
else if (proximity && draw_tool->default_status)
{
gimp_tool_replace_status (tool, display, "%s", draw_tool->default_status);
}
else if (! proximity)
{
gimp_tool_pop_status (tool, display);
}
else
{
GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state,
proximity, display);
}
}
static void
gimp_draw_tool_cursor_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (draw_tool->widget && display == draw_tool->display)
{
GimpCursorType cursor;
GimpToolCursorType tool_cursor;
GimpCursorModifier modifier;
cursor = gimp_tool_control_get_cursor (tool->control);
tool_cursor = gimp_tool_control_get_tool_cursor (tool->control);
modifier = gimp_tool_control_get_cursor_modifier (tool->control);
if (gimp_tool_widget_get_cursor (draw_tool->widget, coords, state,
&cursor, &tool_cursor, &modifier))
{
gimp_tool_set_cursor (tool, display,
cursor, tool_cursor, modifier);
return;
}
}
GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state,
display);
}
static GimpUIManager *
gimp_draw_tool_get_popup (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display,
const gchar **ui_path)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (draw_tool->widget && display == draw_tool->display)
{
GimpUIManager *ui_manager;
ui_manager = gimp_tool_widget_get_popup (draw_tool->widget,
coords, state,
ui_path);
if (ui_manager)
return ui_manager;
}
return GIMP_TOOL_CLASS (parent_class)->get_popup (tool,
coords, state, display,
ui_path);
}
static void
gimp_draw_tool_widget_status (GimpToolWidget *widget,
const gchar *status,
GimpTool *tool)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (gimp_draw_tool_is_active (draw_tool))
{
if (status)
gimp_tool_replace_status (tool, draw_tool->display, "%s", status);
else
gimp_tool_pop_status (tool, draw_tool->display);
}
}
static void
gimp_draw_tool_widget_status_coords (GimpToolWidget *widget,
const gchar *title,
gdouble x,
const gchar *separator,
gdouble y,
const gchar *help,
GimpTool *tool)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (gimp_draw_tool_is_active (draw_tool))
{
gimp_tool_pop_status (tool, draw_tool->display);
gimp_tool_push_status_coords (tool, draw_tool->display,
gimp_tool_control_get_precision (
tool->control),
title, x, separator, y, help);
}
}
static void
gimp_draw_tool_widget_message (GimpToolWidget *widget,
const gchar *message,
GimpTool *tool)
{
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (gimp_draw_tool_is_active (draw_tool))
gimp_tool_message_literal (tool, draw_tool->display, message);
}
static void
gimp_draw_tool_widget_snap_offsets (GimpToolWidget *widget,
gint offset_x,
gint offset_y,
gint width,
gint height,
GimpTool *tool)
{
gimp_tool_control_set_snap_offsets (tool->control,
offset_x, offset_y,
width, height);
}
#ifdef USE_TIMEOUT
static gboolean
gimp_draw_tool_draw_timeout (GimpDrawTool *draw_tool)
{
guint64 now = g_get_monotonic_time ();
/* keep the timeout running if the last drawing just happened */
if ((now - draw_tool->last_draw_time) <= MINIMUM_DRAW_INTERVAL)
return TRUE;
draw_tool->draw_timeout = 0;
gimp_draw_tool_draw (draw_tool);
return FALSE;
}
#endif
static void
gimp_draw_tool_draw (GimpDrawTool *draw_tool)
{
guint64 now = g_get_monotonic_time ();
if (draw_tool->display &&
draw_tool->paused_count == 0 &&
(! draw_tool->draw_timeout ||
(now - draw_tool->last_draw_time) > MINIMUM_DRAW_INTERVAL))
{
GimpDisplayShell *shell = gimp_display_get_shell (draw_tool->display);
if (draw_tool->draw_timeout)
{
g_source_remove (draw_tool->draw_timeout);
draw_tool->draw_timeout = 0;
}
gimp_draw_tool_undraw (draw_tool);
GIMP_DRAW_TOOL_GET_CLASS (draw_tool)->draw (draw_tool);
if (draw_tool->group_stack)
{
g_warning ("%s: draw_tool->group_stack not empty after calling "
"GimpDrawTool::draw() of %s",
G_STRFUNC,
g_type_name (G_TYPE_FROM_INSTANCE (draw_tool)));
while (draw_tool->group_stack)
gimp_draw_tool_pop_group (draw_tool);
}
if (draw_tool->preview)
gimp_display_shell_add_preview_item (shell, draw_tool->preview);
if (draw_tool->item)
gimp_display_shell_add_tool_item (shell, draw_tool->item);
draw_tool->last_draw_time = g_get_monotonic_time ();
#if 0
g_printerr ("drawing tool stuff took %f seconds\n",
(draw_tool->last_draw_time - now) / 1000000.0);
#endif
}
}
static void
gimp_draw_tool_undraw (GimpDrawTool *draw_tool)
{
if (draw_tool->display)
{
GimpDisplayShell *shell = gimp_display_get_shell (draw_tool->display);
if (draw_tool->preview)
{
gimp_display_shell_remove_preview_item (shell, draw_tool->preview);
g_clear_object (&draw_tool->preview);
}
if (draw_tool->item)
{
gimp_display_shell_remove_tool_item (shell, draw_tool->item);
g_clear_object (&draw_tool->item);
}
}
}
static void
gimp_draw_tool_real_draw (GimpDrawTool *draw_tool)
{
if (draw_tool->widget)
{
GimpCanvasItem *item = gimp_tool_widget_get_item (draw_tool->widget);
gimp_draw_tool_add_item (draw_tool, item);
}
}
void
gimp_draw_tool_start (GimpDrawTool *draw_tool,
GimpDisplay *display)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_DISPLAY (display));
g_return_if_fail (gimp_draw_tool_is_active (draw_tool) == FALSE);
draw_tool->display = display;
gimp_draw_tool_draw (draw_tool);
}
void
gimp_draw_tool_stop (GimpDrawTool *draw_tool)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (gimp_draw_tool_is_active (draw_tool) == TRUE);
gimp_draw_tool_undraw (draw_tool);
if (draw_tool->draw_timeout)
{
g_source_remove (draw_tool->draw_timeout);
draw_tool->draw_timeout = 0;
}
draw_tool->last_draw_time = 0;
draw_tool->display = NULL;
}
gboolean
gimp_draw_tool_is_active (GimpDrawTool *draw_tool)
{
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
return draw_tool->display != NULL;
}
void
gimp_draw_tool_pause (GimpDrawTool *draw_tool)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
draw_tool->paused_count++;
if (draw_tool->draw_timeout)
{
g_source_remove (draw_tool->draw_timeout);
draw_tool->draw_timeout = 0;
}
}
void
gimp_draw_tool_resume (GimpDrawTool *draw_tool)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (draw_tool->paused_count > 0);
draw_tool->paused_count--;
if (draw_tool->paused_count == 0)
{
#ifdef USE_TIMEOUT
/* Don't install the timeout if the draw tool isn't active, so
* suspend()/resume() can always be called, and have no side
* effect on an inactive tool. See bug #687851.
*/
if (gimp_draw_tool_is_active (draw_tool) && ! draw_tool->draw_timeout)
{
draw_tool->draw_timeout =
gdk_threads_add_timeout_full (G_PRIORITY_HIGH_IDLE,
DRAW_TIMEOUT,
(GSourceFunc) gimp_draw_tool_draw_timeout,
draw_tool, NULL);
}
#endif
/* call draw() anyway, it will do nothing if the timeout is
* running, but will additionally check the drawing times to
* ensure the minimum framerate
*/
gimp_draw_tool_draw (draw_tool);
}
}
/**
* gimp_draw_tool_calc_distance:
* @draw_tool: a #GimpDrawTool
* @display: a #GimpDisplay
* @x1: start point X in image coordinates
* @y1: start point Y in image coordinates
* @x2: end point X in image coordinates
* @y2: end point Y in image coordinates
*
* If you just need to compare distances, consider to use
* gimp_draw_tool_calc_distance_square() instead.
*
* Returns: the distance between the given points in display coordinates
**/
gdouble
gimp_draw_tool_calc_distance (GimpDrawTool *draw_tool,
GimpDisplay *display,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2)
{
return sqrt (gimp_draw_tool_calc_distance_square (draw_tool, display,
x1, y1, x2, y2));
}
/**
* gimp_draw_tool_calc_distance_square:
* @draw_tool: a #GimpDrawTool
* @display: a #GimpDisplay
* @x1: start point X in image coordinates
* @y1: start point Y in image coordinates
* @x2: end point X in image coordinates
* @y2: end point Y in image coordinates
*
* This function is more effective than gimp_draw_tool_calc_distance()
* as it doesn't perform a sqrt(). Use this if you just need to compare
* distances.
*
* Returns: the square of the distance between the given points in
* display coordinates
**/
gdouble
gimp_draw_tool_calc_distance_square (GimpDrawTool *draw_tool,
GimpDisplay *display,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2)
{
GimpDisplayShell *shell;
gdouble tx1, ty1;
gdouble tx2, ty2;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), 0.0);
g_return_val_if_fail (GIMP_IS_DISPLAY (display), 0.0);
shell = gimp_display_get_shell (display);
gimp_display_shell_transform_xy_f (shell, x1, y1, &tx1, &ty1);
gimp_display_shell_transform_xy_f (shell, x2, y2, &tx2, &ty2);
return SQR (tx2 - tx1) + SQR (ty2 - ty1);
}
void
gimp_draw_tool_set_widget (GimpDrawTool *draw_tool,
GimpToolWidget *widget)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (widget == NULL || GIMP_IS_TOOL_WIDGET (widget));
if (widget == draw_tool->widget)
return;
if (draw_tool->widget)
{
gimp_tool_widget_set_focus (draw_tool->widget, FALSE);
g_signal_handlers_disconnect_by_func (draw_tool->widget,
gimp_draw_tool_widget_status,
draw_tool);
g_signal_handlers_disconnect_by_func (draw_tool->widget,
gimp_draw_tool_widget_status_coords,
draw_tool);
g_signal_handlers_disconnect_by_func (draw_tool->widget,
gimp_draw_tool_widget_message,
draw_tool);
g_signal_handlers_disconnect_by_func (draw_tool->widget,
gimp_draw_tool_widget_snap_offsets,
draw_tool);
if (gimp_draw_tool_is_active (draw_tool))
{
GimpCanvasItem *item = gimp_tool_widget_get_item (draw_tool->widget);
gimp_draw_tool_remove_item (draw_tool, item);
}
g_object_unref (draw_tool->widget);
}
draw_tool->widget = widget;
if (draw_tool->widget)
{
g_object_ref (draw_tool->widget);
if (gimp_draw_tool_is_active (draw_tool))
{
GimpCanvasItem *item = gimp_tool_widget_get_item (draw_tool->widget);
gimp_draw_tool_add_item (draw_tool, item);
}
g_signal_connect (draw_tool->widget, "status",
G_CALLBACK (gimp_draw_tool_widget_status),
draw_tool);
g_signal_connect (draw_tool->widget, "status-coords",
G_CALLBACK (gimp_draw_tool_widget_status_coords),
draw_tool);
g_signal_connect (draw_tool->widget, "message",
G_CALLBACK (gimp_draw_tool_widget_message),
draw_tool);
g_signal_connect (draw_tool->widget, "snap-offsets",
G_CALLBACK (gimp_draw_tool_widget_snap_offsets),
draw_tool);
gimp_tool_widget_set_focus (draw_tool->widget, TRUE);
}
}
void
gimp_draw_tool_set_default_status (GimpDrawTool *draw_tool,
const gchar *status)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
if (draw_tool->default_status)
g_free (draw_tool->default_status);
draw_tool->default_status = g_strdup (status);
}
void
gimp_draw_tool_add_preview (GimpDrawTool *draw_tool,
GimpCanvasItem *item)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
if (! draw_tool->preview)
draw_tool->preview =
gimp_canvas_group_new (gimp_display_get_shell (draw_tool->display));
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (draw_tool->preview), item);
}
void
gimp_draw_tool_remove_preview (GimpDrawTool *draw_tool,
GimpCanvasItem *item)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
g_return_if_fail (draw_tool->preview != NULL);
gimp_canvas_group_remove_item (GIMP_CANVAS_GROUP (draw_tool->preview), item);
}
void
gimp_draw_tool_add_item (GimpDrawTool *draw_tool,
GimpCanvasItem *item)
{
GimpCanvasGroup *group;
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
if (! draw_tool->item)
draw_tool->item =
gimp_canvas_group_new (gimp_display_get_shell (draw_tool->display));
group = GIMP_CANVAS_GROUP (draw_tool->item);
if (draw_tool->group_stack)
group = draw_tool->group_stack->data;
gimp_canvas_group_add_item (group, item);
}
void
gimp_draw_tool_remove_item (GimpDrawTool *draw_tool,
GimpCanvasItem *item)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
g_return_if_fail (draw_tool->item != NULL);
gimp_canvas_group_remove_item (GIMP_CANVAS_GROUP (draw_tool->item), item);
}
GimpCanvasGroup *
gimp_draw_tool_add_stroke_group (GimpDrawTool *draw_tool)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_group_new (gimp_display_get_shell (draw_tool->display));
gimp_canvas_group_set_group_stroking (GIMP_CANVAS_GROUP (item), TRUE);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return GIMP_CANVAS_GROUP (item);
}
GimpCanvasGroup *
gimp_draw_tool_add_fill_group (GimpDrawTool *draw_tool)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_group_new (gimp_display_get_shell (draw_tool->display));
gimp_canvas_group_set_group_filling (GIMP_CANVAS_GROUP (item), TRUE);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return GIMP_CANVAS_GROUP (item);
}
void
gimp_draw_tool_push_group (GimpDrawTool *draw_tool,
GimpCanvasGroup *group)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_CANVAS_GROUP (group));
draw_tool->group_stack = g_list_prepend (draw_tool->group_stack, group);
}
void
gimp_draw_tool_pop_group (GimpDrawTool *draw_tool)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (draw_tool->group_stack != NULL);
draw_tool->group_stack = g_list_remove (draw_tool->group_stack,
draw_tool->group_stack->data);
}
/**
* gimp_draw_tool_add_line:
* @draw_tool: the #GimpDrawTool
* @x1: start point X in image coordinates
* @y1: start point Y in image coordinates
* @x2: end point X in image coordinates
* @y2: end point Y in image coordinates
*
* This function takes image space coordinates and transforms them to
* screen window coordinates, then draws a line between the resulting
* coordinates.
**/
GimpCanvasItem *
gimp_draw_tool_add_line (GimpDrawTool *draw_tool,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_line_new (gimp_display_get_shell (draw_tool->display),
x1, y1, x2, y2);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
/**
* gimp_draw_tool_add_guide:
* @draw_tool: the #GimpDrawTool
* @orientation: the orientation of the guide line
* @position: the position of the guide line in image coordinates
*
* This function draws a guide line across the canvas.
**/
GimpCanvasItem *
gimp_draw_tool_add_guide (GimpDrawTool *draw_tool,
GimpOrientationType orientation,
gint position,
GimpGuideStyle style)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_guide_new (gimp_display_get_shell (draw_tool->display),
orientation, position,
style);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
/**
* gimp_draw_tool_add_crosshair:
* @draw_tool: the #GimpDrawTool
* @position_x: the position of the vertical guide line in image coordinates
* @position_y: the position of the horizontal guide line in image coordinates
*
* This function draws two crossing guide lines across the canvas.
**/
GimpCanvasItem *
gimp_draw_tool_add_crosshair (GimpDrawTool *draw_tool,
gint position_x,
gint position_y)
{
GimpCanvasGroup *group;
group = gimp_draw_tool_add_stroke_group (draw_tool);
gimp_draw_tool_push_group (draw_tool, group);
gimp_draw_tool_add_guide (draw_tool,
GIMP_ORIENTATION_VERTICAL, position_x,
GIMP_GUIDE_STYLE_NONE);
gimp_draw_tool_add_guide (draw_tool,
GIMP_ORIENTATION_HORIZONTAL, position_y,
GIMP_GUIDE_STYLE_NONE);
gimp_draw_tool_pop_group (draw_tool);
return GIMP_CANVAS_ITEM (group);
}
/**
* gimp_draw_tool_add_sample_point:
* @draw_tool: the #GimpDrawTool
* @x: X position of the sample point
* @y: Y position of the sample point
* @index: Index of the sample point
*
* This function draws a sample point
**/
GimpCanvasItem *
gimp_draw_tool_add_sample_point (GimpDrawTool *draw_tool,
gint x,
gint y,
gint index)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_sample_point_new (gimp_display_get_shell (draw_tool->display),
x, y, index, TRUE);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
/**
* gimp_draw_tool_add_rectangle:
* @draw_tool: the #GimpDrawTool
* @filled: whether to fill the rectangle
* @x: horizontal image coordinate
* @y: vertical image coordinate
* @width: width in image coordinates
* @height: height in image coordinates
*
* This function takes image space coordinates and transforms them to
* screen window coordinates, then draws the resulting rectangle.
**/
GimpCanvasItem *
gimp_draw_tool_add_rectangle (GimpDrawTool *draw_tool,
gboolean filled,
gdouble x,
gdouble y,
gdouble width,
gdouble height)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_rectangle_new (gimp_display_get_shell (draw_tool->display),
x, y, width, height, filled);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_arc (GimpDrawTool *draw_tool,
gboolean filled,
gdouble x,
gdouble y,
gdouble width,
gdouble height,
gdouble start_angle,
gdouble slice_angle)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_arc_new (gimp_display_get_shell (draw_tool->display),
x + width / 2.0,
y + height / 2.0,
width / 2.0,
height / 2.0,
start_angle,
slice_angle,
filled);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_handle (GimpDrawTool *draw_tool,
GimpHandleType type,
gdouble x,
gdouble y,
gint width,
gint height,
GimpHandleAnchor anchor)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_handle_new (gimp_display_get_shell (draw_tool->display),
type, anchor, x, y, width, height);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_lines (GimpDrawTool *draw_tool,
const GimpVector2 *points,
gint n_points,
GimpMatrix3 *transform,
gboolean filled)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
if (points == NULL || n_points < 2)
return NULL;
item = gimp_canvas_polygon_new (gimp_display_get_shell (draw_tool->display),
points, n_points, transform, filled);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_strokes (GimpDrawTool *draw_tool,
const GimpCoords *points,
gint n_points,
GimpMatrix3 *transform,
gboolean filled)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
if (points == NULL || n_points < 2)
return NULL;
item = gimp_canvas_polygon_new_from_coords (gimp_display_get_shell (draw_tool->display),
points, n_points, transform, filled);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_pen (GimpDrawTool *draw_tool,
const GimpVector2 *points,
gint n_points,
GimpContext *context,
GimpActiveColor color,
gint width)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
if (points == NULL || n_points < 2)
return NULL;
item = gimp_canvas_pen_new (gimp_display_get_shell (draw_tool->display),
points, n_points, context, color, width);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
/**
* gimp_draw_tool_add_boundary:
* @draw_tool: a #GimpDrawTool
* @bound_segs: the sorted brush outline
* @n_bound_segs: the number of segments in @bound_segs
* @matrix: transform matrix for the boundary
* @offset_x: x offset
* @offset_y: y offset
*
* Draw the boundary of the brush that @draw_tool uses. The boundary
* should be sorted with sort_boundary(), and @n_bound_segs should
* include the sentinel segments inserted by sort_boundary() that
* indicate the end of connected segment sequences (groups) .
*/
GimpCanvasItem *
gimp_draw_tool_add_boundary (GimpDrawTool *draw_tool,
const GimpBoundSeg *bound_segs,
gint n_bound_segs,
GimpMatrix3 *transform,
gdouble offset_x,
gdouble offset_y)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
g_return_val_if_fail (n_bound_segs > 0, NULL);
g_return_val_if_fail (bound_segs != NULL, NULL);
item = gimp_canvas_boundary_new (gimp_display_get_shell (draw_tool->display),
bound_segs, n_bound_segs,
transform,
offset_x, offset_y);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_text_cursor (GimpDrawTool *draw_tool,
PangoRectangle *cursor,
gboolean overwrite,
GimpTextDirection direction)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_text_cursor_new (gimp_display_get_shell (draw_tool->display),
cursor, overwrite, direction);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_transform_preview (GimpDrawTool *draw_tool,
GimpPickable *pickable,
const GimpMatrix3 *transform,
gdouble x1,
gdouble y1,
gdouble x2,
gdouble y2)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), NULL);
g_return_val_if_fail (transform != NULL, NULL);
item = gimp_canvas_transform_preview_new (gimp_display_get_shell (draw_tool->display),
pickable, transform,
x1, y1, x2, y2);
gimp_draw_tool_add_preview (draw_tool, item);
g_object_unref (item);
return item;
}
GimpCanvasItem *
gimp_draw_tool_add_text (GimpDrawTool *draw_tool,
gdouble x,
gdouble y,
gdouble font_size,
gchar *text)
{
GimpCanvasItem *item;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), NULL);
item = gimp_canvas_text_new (gimp_display_get_shell (draw_tool->display),
x, y, font_size, text);
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
}
gboolean
gimp_draw_tool_on_handle (GimpDrawTool *draw_tool,
GimpDisplay *display,
gdouble x,
gdouble y,
GimpHandleType type,
gdouble handle_x,
gdouble handle_y,
gint width,
gint height,
GimpHandleAnchor anchor)
{
GimpDisplayShell *shell;
gdouble tx, ty;
gdouble handle_tx, handle_ty;
g_return_val_if_fail (GIMP_IS_DRAW_TOOL (draw_tool), FALSE);
g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
shell = gimp_display_get_shell (display);
gimp_display_shell_zoom_xy_f (shell,
x, y,
&tx, &ty);
gimp_display_shell_zoom_xy_f (shell,
handle_x, handle_y,
&handle_tx, &handle_ty);
switch (type)
{
case GIMP_HANDLE_SQUARE:
case GIMP_HANDLE_FILLED_SQUARE:
case GIMP_HANDLE_CROSS:
case GIMP_HANDLE_CROSSHAIR:
gimp_canvas_item_shift_to_north_west (anchor,
handle_tx, handle_ty,
width, height,
&handle_tx, &handle_ty);
return (tx == CLAMP (tx, handle_tx, handle_tx + width) &&
ty == CLAMP (ty, handle_ty, handle_ty + height));
case GIMP_HANDLE_CIRCLE:
case GIMP_HANDLE_FILLED_CIRCLE:
gimp_canvas_item_shift_to_center (anchor,
handle_tx, handle_ty,
width, height,
&handle_tx, &handle_ty);
/* FIXME */
if (width != height)
width = (width + height) / 2;
width /= 2;
return ((SQR (handle_tx - tx) + SQR (handle_ty - ty)) < SQR (width));
default:
g_warning ("%s: invalid handle type %d", G_STRFUNC, type);
break;
}
return FALSE;
}
|