vectors-commands.c 18.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

Hans Breuer's avatar
Hans Breuer committed
21 22
#include <string.h>

23 24 25 26 27 28 29 30
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gui-types.h"

#include "core/gimp.h"
#include "core/gimpchannel.h"
31
#include "core/gimpcontext.h"
32 33
#include "core/gimpimage.h"
#include "core/gimpimage-mask.h"
34
#include "core/gimpimage-mask-select.h"
35
#include "core/gimplist.h"
36
#include "core/gimppaintinfo.h"
37
#include "core/gimptoolinfo.h"
38

39 40 41
#include "paint/gimppaintcore.h"
#include "paint/gimppaintcore-stroke.h"

42 43 44
#include "vectors/gimpvectors.h"

#include "widgets/gimpitemfactory.h"
45
#include "widgets/gimpviewabledialog.h"
46 47
#include "widgets/gimpwidgets-utils.h"

48
#include "tools/gimppainttool.h"
49 50 51
#include "tools/gimpvectortool.h"
#include "tools/tool_manager.h"

52 53 54 55 56 57 58
#include "vectors-commands.h"

#include "undo.h"

#include "libgimp/gimpintl.h"


59 60 61 62 63 64 65 66 67 68 69 70 71 72
#define return_if_no_image(gimage) \
  gimage = (GimpImage *) gimp_widget_get_callback_context (widget); \
  if (! GIMP_IS_IMAGE (gimage)) \
    gimage = gimp_context_get_image (gimp_get_user_context (GIMP (data))); \
  if (! gimage) \
    return

#define return_if_no_vectors(gimage,vectors) \
  return_if_no_image (gimage); \
  vectors = gimp_image_get_active_vectors (gimage); \
  if (! vectors) \
    return


73 74 75 76 77 78 79
/*  public functions  */

void
vectors_new_vectors_cmd_callback (GtkWidget *widget,
                                  gpointer   data)
{
  GimpImage *gimage;
80
  return_if_no_image (gimage);
81 82 83 84 85 86 87 88

  vectors_new_vectors_query (gimage, NULL);
}

void
vectors_raise_vectors_cmd_callback (GtkWidget *widget,
                                    gpointer   data)
{
89 90 91
  GimpImage   *gimage;
  GimpVectors *active_vectors;
  return_if_no_vectors (gimage, active_vectors);
92

93
  gimp_image_raise_vectors (gimage, active_vectors);
94
  gimp_image_flush (gimage);
95 96 97 98 99 100
}

void
vectors_lower_vectors_cmd_callback (GtkWidget *widget,
                                    gpointer   data)
{
101 102 103
  GimpImage   *gimage;
  GimpVectors *active_vectors;
  return_if_no_vectors (gimage, active_vectors);
104

105
  gimp_image_lower_vectors (gimage, active_vectors);
106
  gimp_image_flush (gimage);
107 108 109 110 111 112 113 114 115
}

void
vectors_duplicate_vectors_cmd_callback (GtkWidget *widget,
                                        gpointer   data)
{
  GimpImage   *gimage;
  GimpVectors *active_vectors;
  GimpVectors *new_vectors;
116 117 118 119 120 121 122 123
  return_if_no_vectors (gimage, active_vectors);

  new_vectors = NULL;
#if 0
  new_vectors = gimp_vectors_copy (active_vectors,
                                   G_TYPE_FROM_INSTANCE (active_vectors),
                                   TRUE);
  gimp_image_add_vectors (gimage, new_vectors, -1);
124
  gimp_image_flush (gimage);
125
#endif
126 127 128 129 130 131 132 133
}

void
vectors_delete_vectors_cmd_callback (GtkWidget *widget,
                                     gpointer   data)
{
  GimpImage   *gimage;
  GimpVectors *active_vectors;
134
  return_if_no_vectors (gimage, active_vectors);
135

136
  gimp_image_remove_vectors (gimage, active_vectors);
137
  gimp_image_flush (gimage);
138 139
}

140
static void
141 142 143
vectors_vectors_to_sel (GtkWidget      *widget,
                        gpointer        data,
                        GimpChannelOps  op)
144 145 146
{
  GimpImage   *gimage;
  GimpVectors *active_vectors;
147 148 149 150 151 152 153
  return_if_no_vectors (gimage, active_vectors);

  gimp_image_mask_select_vectors (gimage,
                                  active_vectors,
                                  op,
                                  TRUE,
                                  FALSE, 0, 0);
154
  gimp_image_flush (gimage);
155 156
}

157 158 159 160
void
vectors_vectors_to_sel_cmd_callback (GtkWidget *widget,
                                     gpointer   data)
{
161
  vectors_vectors_to_sel (widget, data, GIMP_CHANNEL_OP_REPLACE);
162 163
}

164 165 166 167
void
vectors_add_vectors_to_sel_cmd_callback (GtkWidget *widget,
                                         gpointer   data)
{
168
  vectors_vectors_to_sel (widget, data, GIMP_CHANNEL_OP_ADD);
169 170 171 172 173 174
}

void
vectors_sub_vectors_from_sel_cmd_callback (GtkWidget *widget,
                                           gpointer   data)
{
175
  vectors_vectors_to_sel (widget, data, GIMP_CHANNEL_OP_SUBTRACT);
176 177 178 179 180 181
}

void
vectors_intersect_vectors_with_sel_cmd_callback (GtkWidget *widget,
                                                 gpointer   data)
{
182
  vectors_vectors_to_sel (widget, data, GIMP_CHANNEL_OP_INTERSECT);
183 184 185 186 187 188
}

void
vectors_sel_to_vectors_cmd_callback (GtkWidget *widget,
                                     gpointer   data)
{
189 190
  GimpImage *gimage;
  return_if_no_image (gimage);
191

192
  /* TODO */
193 194 195 196 197 198
}

void
vectors_stroke_vectors_cmd_callback (GtkWidget *widget,
                                     gpointer   data)
{
199 200
  GimpImage   *gimage;
  GimpVectors *active_vectors;
201
  return_if_no_vectors (gimage, active_vectors);
202

203
  vectors_stroke_vectors (active_vectors);
204 205 206 207 208 209 210 211
}

void
vectors_copy_vectors_cmd_callback (GtkWidget *widget,
                                   gpointer   data)
{
  GimpImage   *gimage;
  GimpVectors *active_vectors;
212
  return_if_no_vectors (gimage, active_vectors);
213

214
  /* TODO */
215 216 217 218 219 220
}

void
vectors_paste_vectors_cmd_callback (GtkWidget *widget,
                                    gpointer   data)
{
221 222
  GimpImage *gimage;
  return_if_no_image (gimage);
223

224
  /* TODO */
225 226 227 228 229 230
}

void
vectors_import_vectors_cmd_callback (GtkWidget *widget,
                                     gpointer   data)
{
231 232
  GimpImage *gimage;
  return_if_no_image (gimage);
233

234
  /* TODO */
235 236 237 238 239 240 241 242
}

void
vectors_export_vectors_cmd_callback (GtkWidget *widget,
                                     gpointer   data)
{
  GimpImage   *gimage;
  GimpVectors *active_vectors;
243
  return_if_no_vectors (gimage, active_vectors);
244

245
  /* TODO */
246 247 248 249 250 251 252 253
}

void
vectors_vectors_tool_cmd_callback (GtkWidget   *widget,
                                   gpointer     data)
{
  GimpImage   *gimage;
  GimpVectors *active_vectors;
254
  return_if_no_vectors (gimage, active_vectors);
255

256
  vectors_vectors_tool (active_vectors);
257 258 259 260 261 262 263 264
}

void
vectors_edit_vectors_attributes_cmd_callback (GtkWidget *widget,
                                              gpointer   data)
{
  GimpImage   *gimage;
  GimpVectors *active_vectors;
265
  return_if_no_vectors (gimage, active_vectors);
266

267
  vectors_edit_vectors_query (active_vectors);
268 269 270 271 272 273 274 275 276 277 278 279
}

void
vectors_stroke_vectors (GimpVectors *vectors)
{
  GimpImage    *gimage;
  GimpDrawable *active_drawable;

  g_return_if_fail (GIMP_IS_VECTORS (vectors));

  gimage = gimp_item_get_image (GIMP_ITEM (vectors));

280
  active_drawable = gimp_image_active_drawable (gimage);
281

282
  if (! active_drawable)
283
    {
284 285 286 287
      g_message (_("There is no active Layer or Channel to stroke to"));
      return;
    }

288
  if (vectors && vectors->strokes)
289
    {
290
      GimpTool         *active_tool;
291 292 293 294 295
      GimpPaintCore    *core;
      GimpToolInfo     *tool_info;
      GimpPaintOptions *paint_options;
      GimpDisplay      *gdisp;

296 297 298 299 300 301 302 303 304 305
      active_tool = tool_manager_get_active (gimage->gimp);

      if (GIMP_IS_PAINT_TOOL (active_tool))
        {
          tool_info = active_tool->tool_info;
        }
      else
        {
          tool_info = (GimpToolInfo *)
            gimp_container_get_child_by_name (gimage->gimp->tool_info_list,
306
                                              "gimp-paintbrush-tool");
307
        }
308 309 310

      paint_options = (GimpPaintOptions *) tool_info->tool_options;

311
      core = g_object_new (tool_info->paint_info->paint_type, NULL);
312 313 314 315 316

      gdisp = gimp_context_get_display (gimp_get_current_context (gimage->gimp));

      tool_manager_control_active (gimage->gimp, PAUSE, gdisp);

317
      gimp_paint_core_stroke_vectors (core,
318 319
                                      active_drawable,
                                      paint_options,
320
                                      vectors);
321 322 323 324 325

      tool_manager_control_active (gimage->gimp, RESUME, gdisp);

      g_object_unref (G_OBJECT (core));

326
      gimp_image_flush (gimage);
327 328 329 330
    }
}

void
331
vectors_vectors_tool (GimpVectors *vectors)
332
{
333 334
  GimpImage *gimage;
  GimpTool  *active_tool;
335

336
  g_return_if_fail (GIMP_IS_VECTORS (vectors));
337

338
  gimage = gimp_item_get_image (GIMP_ITEM (vectors));
339

340
  active_tool = tool_manager_get_active (gimage->gimp);
341

342
  if (! GIMP_IS_VECTOR_TOOL (active_tool))
343
    {
344 345 346 347 348 349 350 351 352
      GimpToolInfo *tool_info;

      tool_info = tool_manager_get_info_by_type (gimage->gimp,
                                                 GIMP_TYPE_VECTOR_TOOL);

      gimp_context_set_tool (gimp_get_current_context (gimage->gimp),
                             tool_info);

      active_tool = tool_manager_get_active (gimage->gimp);
353 354
    }

355 356 357
  gimp_vector_tool_set_vectors (GIMP_VECTOR_TOOL (active_tool),
                                vectors);
}
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

/**********************************/
/*  The new vectors query dialog  */
/**********************************/

typedef struct _NewVectorsOptions NewVectorsOptions;

struct _NewVectorsOptions
{
  GtkWidget  *query_box;
  GtkWidget  *name_entry;

  GimpImage  *gimage;
};

static gchar *vectors_name = NULL;


static void
new_vectors_query_ok_callback (GtkWidget *widget,
			       gpointer   data)
{
  NewVectorsOptions *options;
  GimpVectors       *new_vectors;
  GimpImage         *gimage;

  options = (NewVectorsOptions *) data;

  if (vectors_name)
    g_free (vectors_name);
  vectors_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (options->name_entry)));

  if ((gimage = options->gimage))
    {
      new_vectors = g_object_new (GIMP_TYPE_VECTORS, NULL);

      gimp_image_add_vectors (gimage, new_vectors, -1);

      gimp_object_set_name (GIMP_OBJECT (new_vectors), vectors_name);

398
      gimp_image_flush (gimage);
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
    }

  gtk_widget_destroy (options->query_box);
}

void
vectors_new_vectors_query (GimpImage   *gimage,
                           GimpVectors *template)
{
  NewVectorsOptions *options;
  GtkWidget         *hbox;
  GtkWidget         *vbox;
  GtkWidget         *table;
  GtkWidget         *label;

  g_return_if_fail (GIMP_IS_IMAGE (gimage));
  g_return_if_fail (! template || GIMP_IS_VECTORS (template));

  if (template)
    {
      GimpVectors *new_vectors;

      /* undo_push_group_start (gimage, EDIT_PASTE_UNDO_GROUP); */

      new_vectors = NULL; /*gimp_vectors_new (gimage, _("Empty Vectors Copy"));*/

      gimp_image_add_vectors (gimage, new_vectors, -1);

      /* undo_push_group_end (gimage); */

429
      gimp_image_flush (gimage);
430 431 432 433 434
      return;
    }

  /*  the new options structure  */
  options = g_new (NewVectorsOptions, 1);
435
  options->gimage = gimage;
436 437 438
  
  /*  The dialog  */
  options->query_box =
439 440 441 442 443 444
    gimp_viewable_dialog_new (GIMP_VIEWABLE (gimage),
                              _("New Path"), "new_path_options",
                              GTK_STOCK_NEW,
                              _("New Path Options"),
                              gimp_standard_help_func,
                              "dialogs/vectors/new_vectors.html",
445

446 447
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
448

449 450
                              GTK_STOCK_OK, new_vectors_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
451

452
                              NULL);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

  g_object_weak_ref (G_OBJECT (options->query_box),
		     (GWeakNotify) g_free,
		     options);

  /*  The main hbox  */
  hbox = gtk_hbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (options->query_box)->vbox),
		     hbox);

  /*  The vbox  */
  vbox = gtk_vbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);

  /*  The table  */
  table = gtk_table_new (2, 3, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  /*  The name entry hbox, label and entry  */
475
  label = gtk_label_new (_("Path name:"));
476 477 478 479 480 481 482 483 484 485
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (label);

  options->name_entry = gtk_entry_new ();
  gtk_widget_set_size_request (options->name_entry, 150, -1);
  gtk_table_attach_defaults (GTK_TABLE (table), options->name_entry,
			     1, 2, 0, 1);
  gtk_entry_set_text (GTK_ENTRY (options->name_entry),
486
		      (vectors_name ? vectors_name : _("New Path")));
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
  gtk_widget_show (options->name_entry);

  gtk_widget_show (table);
  gtk_widget_show (vbox);
  gtk_widget_show (hbox);
  gtk_widget_show (options->query_box);
}

/****************************************/
/*  The edit vectors attributes dialog  */
/****************************************/

typedef struct _EditVectorsOptions EditVectorsOptions;

struct _EditVectorsOptions
{
  GtkWidget     *query_box;
  GtkWidget     *name_entry;

  GimpVectors   *vectors;
  GimpImage     *gimage;
};

static void
edit_vectors_query_ok_callback (GtkWidget *widget,
				gpointer   data)
{
  EditVectorsOptions *options;
  GimpVectors        *vectors;

  options = (EditVectorsOptions *) data;
  vectors = options->vectors;

  if (options->gimage)
    {
522 523 524 525 526 527 528 529 530 531
      const gchar *new_name;

      new_name = gtk_entry_get_text (GTK_ENTRY (options->name_entry));

      if (strcmp (new_name, gimp_object_get_name (GIMP_OBJECT (vectors))))
        {
          undo_push_item_rename (options->gimage, GIMP_ITEM (vectors));

          gimp_object_set_name (GIMP_OBJECT (vectors), new_name);
        }
532 533 534 535 536 537 538 539 540 541 542 543 544 545
    }

  gtk_widget_destroy (options->query_box);
}

void
vectors_edit_vectors_query (GimpVectors *vectors)
{
  EditVectorsOptions *options;
  GtkWidget          *hbox;
  GtkWidget          *vbox;
  GtkWidget          *table;
  GtkWidget          *label;

546
  g_return_if_fail (GIMP_IS_VECTORS (vectors));
547

548 549 550 551 552 553 554
  options = g_new0 (EditVectorsOptions, 1);

  options->vectors = vectors;
  options->gimage  = gimp_item_get_image (GIMP_ITEM (vectors));

  /*  The dialog  */
  options->query_box =
555 556 557 558 559 560
    gimp_viewable_dialog_new (GIMP_VIEWABLE (vectors),
                              _("Path Attributes"), "edit_path_attributes",
                              GIMP_STOCK_EDIT,
                              _("Edit Path Attributes"),
                              gimp_standard_help_func,
                              "dialogs/paths/edit_path_attributes.html",
561

562 563
                              GTK_STOCK_CANCEL, gtk_widget_destroy,
                              NULL, 1, NULL, FALSE, TRUE,
564

565 566
                              GTK_STOCK_OK, edit_vectors_query_ok_callback,
                              options, NULL, NULL, TRUE, FALSE,
567

568
                              NULL);
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

  g_object_weak_ref (G_OBJECT (options->query_box),
		     (GWeakNotify) g_free,
		     options);

  g_signal_connect_object (G_OBJECT (vectors), "removed",
			   G_CALLBACK (gtk_widget_destroy),
			   G_OBJECT (options->query_box),
			   G_CONNECT_SWAPPED);

  /*  The main hbox  */
  hbox = gtk_hbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (options->query_box)->vbox),
		     hbox);

  /*  The vbox  */
  vbox = gtk_vbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);

  /*  The table  */
  table = gtk_table_new (2, 3, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  /*  The name entry  */
596
  label = gtk_label_new (_("Path name:"));
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
		    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_widget_show (label);

  options->name_entry = gtk_entry_new ();
  gtk_widget_set_size_request (options->name_entry, 150, -1);
  gtk_table_attach_defaults (GTK_TABLE (table), options->name_entry,
			     1, 2, 0, 1);
  gtk_entry_set_text (GTK_ENTRY (options->name_entry),
		      gimp_object_get_name (GIMP_OBJECT (vectors)));
  gtk_widget_show (options->name_entry);

  gtk_widget_show (table);
  gtk_widget_show (vbox);
  gtk_widget_show (hbox);
  gtk_widget_show (options->query_box);
}

void
vectors_menu_update (GtkItemFactory *factory,
618
                     gpointer        data)
619 620 621
{
  GimpImage   *gimage;
  GimpVectors *vectors;
622
  gboolean     mask_empty;
623
  gboolean     global_buf;
624 625 626 627 628 629 630 631
  GList       *list;
  GList       *next = NULL;
  GList       *prev = NULL;

  gimage = GIMP_IMAGE (data);

  vectors = gimp_image_get_active_vectors (gimage);

632
  mask_empty = gimp_image_mask_is_empty (gimage);
633

634 635
  global_buf = FALSE;

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
  for (list = GIMP_LIST (gimage->vectors)->list;
       list;
       list = g_list_next (list))
    {
      if (vectors == (GimpVectors *) list->data)
	{
	  prev = g_list_previous (list);
	  next = g_list_next (list);
	  break;
	}
    }

#define SET_SENSITIVE(menu,condition) \
        gimp_item_factory_set_sensitive (factory, menu, (condition) != 0)

651 652 653 654 655 656 657 658 659 660 661
  SET_SENSITIVE ("/New Path...",              TRUE);
  SET_SENSITIVE ("/Raise Path",               vectors && prev);
  SET_SENSITIVE ("/Lower Path",               vectors && next);
  SET_SENSITIVE ("/Duplicate Path",           vectors);
  SET_SENSITIVE ("/Path to Selection",        vectors);
  SET_SENSITIVE ("/Add to Selection",         vectors);
  SET_SENSITIVE ("/Subtract from Selection",  vectors);
  SET_SENSITIVE ("/Intersect with Selection", vectors);
  SET_SENSITIVE ("/Selection to Path",        ! mask_empty);
  SET_SENSITIVE ("/Stroke Path",              vectors);
  SET_SENSITIVE ("/Delete Path",              vectors);
662 663 664 665 666
  SET_SENSITIVE ("/Copy Path",                vectors);
  SET_SENSITIVE ("/Paste Path",               global_buf);
  SET_SENSITIVE ("/Import Path...",           TRUE);
  SET_SENSITIVE ("/Export Path...",           vectors);
  SET_SENSITIVE ("/Path Tool",                vectors);
667 668 669
  SET_SENSITIVE ("/Edit Path Attributes...",  vectors);

#undef SET_SENSITIVE
670
}