glade-popup.c 24.2 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) 2001 Ximian, Inc.
 *
 * 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
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Jose Maria Celorio's avatar
Jose Maria Celorio committed
17 18 19
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
20
 *   Tristan Van Berkom <tvb@gnome.org>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
21 22
 */

23 24 25 26 27 28
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n-lib.h>

Jose Maria Celorio's avatar
Jose Maria Celorio committed
29 30
#include "glade.h"
#include "glade-widget.h"
31
#include "glade-widget-adaptor.h"
Jose Maria Celorio's avatar
Jose Maria Celorio committed
32
#include "glade-popup.h"
33
#include "glade-placeholder.h"
34 35
#include "glade-clipboard.h"
#include "glade-command.h"
Joaquín Cuenca Abela's avatar
Joaquín Cuenca Abela committed
36
#include "glade-project.h"
37
#include "glade-app.h"
38

39 40 41
/********************************************************
                      WIDGET POPUP
 *******************************************************/
Jose Maria Celorio's avatar
Jose Maria Celorio committed
42
static void
43
glade_popup_select_cb (GtkMenuItem * item, GladeWidget * widget)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
44
{
45 46
  glade_util_clear_selection ();
  glade_app_selection_set (glade_widget_get_object (widget), TRUE);
47
}
48

49
static GladePlaceholder *
50
find_placeholder (GObject * object)
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
  GtkContainer *container;
  GladePlaceholder *retval = NULL;
  GtkWidget *child;
  GList *c, *l;

  if (!GTK_IS_CONTAINER (object))
    return NULL;

  container = GTK_CONTAINER (object);

  for (c = l = glade_util_container_get_all_children (container);
       l; l = g_list_next (l))
    {
      child = l->data;

      if (GLADE_IS_PLACEHOLDER (child))
        {
          retval = GLADE_PLACEHOLDER (child);
          break;
        }
    }

  g_list_free (c);

  return retval;
77 78 79
}

static void
80 81
glade_popup_placeholder_add_cb (GtkMenuItem * item,
                                GladePlaceholder * placeholder)
82
{
83 84 85 86 87
  GladeWidgetAdaptor *adaptor;
  GladeWidget *parent;

  adaptor = glade_palette_get_current_item (glade_app_get_palette ());
  g_return_if_fail (adaptor != NULL);
88

89
  parent = glade_placeholder_get_parent (placeholder);
90

91 92 93 94 95 96
  if (!glade_util_check_and_warn_scrollable
      (parent, adaptor, glade_app_get_window ()))
    {
      glade_command_create (adaptor, parent,
                            placeholder,
                            glade_placeholder_get_project (placeholder));
97

98 99
      glade_palette_deselect_current_item (glade_app_get_palette (), TRUE);
    }
100 101
}

102
static void
103
glade_popup_action_add_cb (GtkMenuItem * item, GladeWidget * group)
104
{
105
  GladeWidgetAdaptor *adaptor;
106

107 108
  adaptor = glade_palette_get_current_item (glade_app_get_palette ());
  g_return_if_fail (adaptor != NULL);
109

110 111 112
  glade_command_create (adaptor, group, NULL, glade_widget_get_project (group));

  glade_palette_deselect_current_item (glade_app_get_palette (), TRUE);
113 114
}

115
static void
116
glade_popup_root_add_cb (GtkMenuItem * item, gpointer * user_data)
117
{
118 119 120 121 122 123 124 125
  GladeWidgetAdaptor *adaptor = (GladeWidgetAdaptor *) user_data;
  GladePalette *palette = glade_app_get_palette ();

  if (!adaptor)
    adaptor = glade_palette_get_current_item (palette);
  g_return_if_fail (adaptor != NULL);

  glade_palette_create_root_widget (palette, adaptor);
126 127
}

128
static void
129
glade_popup_cut_cb (GtkMenuItem * item, GladeWidget * widget)
130
{
131 132 133 134 135 136 137 138 139 140 141
  GladeProject *project = glade_app_get_project ();

  glade_util_clear_selection ();

  /* Assign selection first */
  if (glade_project_is_selected
      (project, glade_widget_get_object (widget)) == FALSE)
    {
      glade_app_selection_set (glade_widget_get_object (widget), FALSE);
    }
  glade_app_command_cut ();
Jose Maria Celorio's avatar
Jose Maria Celorio committed
142 143 144
}

static void
145
glade_popup_copy_cb (GtkMenuItem * item, GladeWidget * widget)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
146
{
147
  GladeProject *project = glade_app_get_project ();
148

149
  glade_util_clear_selection ();
150

151 152 153 154
  /* Assign selection first */
  if (glade_project_is_selected
      (project, glade_widget_get_object (widget)) == FALSE)
    glade_app_selection_set (glade_widget_get_object (widget), FALSE);
155

156
  glade_app_command_copy ();
157
}
Jose Maria Celorio's avatar
Jose Maria Celorio committed
158

159
static void
160
glade_popup_paste_cb (GtkMenuItem * item, GladeWidget * widget)
161
{
162
  glade_util_clear_selection ();
163

164 165 166 167 168
  /* The selected widget is the paste destination */
  if (widget)
    glade_app_selection_set (glade_widget_get_object (widget), FALSE);
  else
    glade_app_selection_clear (FALSE);
169

170
  glade_app_command_paste (NULL);
171
}
Jose Maria Celorio's avatar
Jose Maria Celorio committed
172

173
static void
174
glade_popup_delete_cb (GtkMenuItem * item, GladeWidget * widget)
175
{
176
  GladeProject *project = glade_app_get_project ();
177

178 179 180 181
  /* Assign selection first */
  if (glade_project_is_selected
      (project, glade_widget_get_object (widget)) == FALSE)
    glade_app_selection_set (glade_widget_get_object (widget), FALSE);
182

183
  glade_app_command_delete ();
Jose Maria Celorio's avatar
Jose Maria Celorio committed
184
}
185

186 187 188
/********************************************************
                  PLACEHOLDER POPUP
 *******************************************************/
189
static void
190 191
glade_popup_placeholder_paste_cb (GtkMenuItem * item,
                                  GladePlaceholder * placeholder)
192
{
193 194 195 196
  glade_util_clear_selection ();
  glade_app_selection_clear (FALSE);

  glade_app_command_paste (placeholder);
197 198
}

199 200 201 202

/********************************************************
                    CLIPBOARD POPUP
 *******************************************************/
203
static void
204
glade_popup_clipboard_paste_cb (GtkMenuItem * item, GladeWidget * widget)
205
{
206 207
  glade_util_clear_selection ();
  glade_app_selection_clear (FALSE);
208

209
  glade_app_command_paste (NULL);
210 211 212
}

static void
213
glade_popup_clipboard_delete_cb (GtkMenuItem * item, GladeWidget * widget)
214
{
215
  glade_app_command_delete_clipboard ();
216 217
}

218
static void
219
glade_popup_docs_cb (GtkMenuItem * item, GladeWidgetAdaptor * adaptor)
220
{
221
  gchar *book;
222

223
  g_return_if_fail (GLADE_IS_WIDGET_ADAPTOR (adaptor));
224

225 226 227 228
  g_object_get (adaptor, "book", &book, NULL);
  glade_editor_search_doc_search (glade_app_get_editor (), book, adaptor->name,
                                  NULL);
  g_free (book);
229 230 231
}


232 233 234
/********************************************************
                    POPUP BUILDING
 *******************************************************/
235
static GtkWidget *
236 237 238 239 240
glade_popup_append_item (GtkWidget * popup_menu,
                         const gchar * stock_id,
                         const gchar * label,
                         GtkWidget * image,
                         gboolean sensitive, gpointer callback, gpointer data)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
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
  GtkWidget *menu_item;

  if (stock_id && label)
    {
      menu_item = gtk_image_menu_item_new_with_mnemonic (label);
      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
                                     gtk_image_new_from_stock (stock_id,
                                                               GTK_ICON_SIZE_MENU));
    }
  else if (image && label)
    {
      menu_item = gtk_image_menu_item_new_with_mnemonic (label);
      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);
    }
  else if (stock_id)
    menu_item = gtk_image_menu_item_new_from_stock (stock_id, NULL);
  else
    menu_item = gtk_menu_item_new_with_mnemonic (label);

  if (callback)
    g_signal_connect (G_OBJECT (menu_item), "activate",
                      G_CALLBACK (callback), data);

  gtk_widget_set_sensitive (menu_item, sensitive);
  gtk_widget_show (menu_item);
  gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), menu_item);

  return menu_item;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
270 271
}

272

Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
273
static void
274
glade_popup_menuitem_activated (GtkMenuItem * item, const gchar * action_path)
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
275
{
276 277 278
  GladeWidget *widget;

  if ((widget = g_object_get_data (G_OBJECT (item), "gwa-data")))
279 280
    glade_widget_adaptor_action_activate (glade_widget_get_adaptor (widget),
                                          glade_widget_get_object (widget), action_path);
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
281 282
}

283
static void
284 285
glade_popup_menuitem_packing_activated (GtkMenuItem * item,
                                        const gchar * action_path)
286
{
287
  GladeWidget *widget, *parent;
288 289

  if ((widget = g_object_get_data (G_OBJECT (item), "gwa-data")))
290 291 292 293 294 295
    {
      parent = glade_widget_get_parent (widget);
      glade_widget_adaptor_child_action_activate (glade_widget_get_adaptor (parent),
						  glade_widget_get_object (parent),
						  glade_widget_get_object (widget), action_path);
    }
296 297
}

298
static void
299 300
glade_popup_menuitem_ph_packing_activated (GtkMenuItem * item,
                                           const gchar * action_path)
301
{
302 303 304 305 306 307
  GladePlaceholder *ph;
  GladeWidget *parent;

  if ((ph = g_object_get_data (G_OBJECT (item), "gwa-data")))
    {
      parent = glade_placeholder_get_parent (ph);
308 309
      glade_widget_adaptor_child_action_activate (glade_widget_get_adaptor (parent),
                                                  glade_widget_get_object (parent),
310 311
                                                  G_OBJECT (ph), action_path);
    }
312 313
}

314
static gint
315 316 317 318
glade_popup_action_populate_menu_real (GtkWidget * menu,
                                       GladeWidget * gwidget,
                                       GList * actions,
                                       GCallback callback, gpointer data)
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
319
{
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  GtkWidget *item;
  GList *list;
  gint n = 0;

  for (list = actions; list; list = g_list_next (list))
    {
      GladeWidgetAction *a = list->data;
      GtkWidget *submenu = NULL;

      if (a->actions)
        {
          submenu = gtk_menu_new ();
          n += glade_popup_action_populate_menu_real (submenu,
                                                      gwidget,
                                                      a->actions,
                                                      callback, data);
        }
      else
338 339
        submenu = glade_widget_adaptor_action_submenu (glade_widget_get_adaptor (gwidget),
                                                       glade_widget_get_object (gwidget),
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
                                                       a->klass->path);


      item = glade_popup_append_item (menu,
                                      a->klass->stock,
                                      a->klass->label, NULL, TRUE,
                                      (a->actions) ? NULL : callback,
                                      (a->actions) ? NULL : a->klass->path);

      g_object_set_data (G_OBJECT (item), "gwa-data", data);

      gtk_widget_set_sensitive (item, a->sensitive);

      if (submenu)
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);

      n++;
    }

  return n;
360 361
}

362 363 364 365 366
/*
 * glade_popup_action_populate_menu:
 * @menu: a GtkMenu to put the actions menu items.
 * @widget: A #GladeWidget
 * @action: a @widget subaction or NULL to include all actions.
367
 * @packing: TRUE to include packing actions
368 369 370 371 372 373
 *
 * Populate a GtkMenu with widget's actions
 *
 * Returns the number of action appended to the menu.
 */
gint
374 375 376
glade_popup_action_populate_menu (GtkWidget * menu,
                                  GladeWidget * widget,
                                  GladeWidgetAction * action, gboolean packing)
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
  gint n;

  g_return_val_if_fail (GTK_IS_MENU (menu), 0);
  g_return_val_if_fail (GLADE_IS_WIDGET (widget), 0);

  if (action)
    {
      g_return_val_if_fail (GLADE_IS_WIDGET_ACTION (action), 0);
      if (glade_widget_get_action (widget, action->klass->path))
        return glade_popup_action_populate_menu_real (menu,
                                                      widget,
                                                      action->actions,
                                                      G_CALLBACK
                                                      (glade_popup_menuitem_activated),
                                                      widget);

      if (glade_widget_get_pack_action (widget, action->klass->path))
        return glade_popup_action_populate_menu_real (menu,
                                                      glade_widget_get_parent
                                                      (widget), action->actions,
                                                      G_CALLBACK
                                                      (glade_popup_menuitem_packing_activated),
                                                      widget);

      return 0;
    }

  n = glade_popup_action_populate_menu_real (menu,
                                             widget,
407
                                             glade_widget_get_actions (widget),
408 409 410 411
                                             G_CALLBACK
                                             (glade_popup_menuitem_activated),
                                             widget);

412
  if (packing && glade_widget_get_pack_actions (widget))
413 414 415 416 417 418 419
    {
      if (n)
        {
          GtkWidget *separator = gtk_menu_item_new ();
          gtk_menu_shell_append (GTK_MENU_SHELL (menu), separator);
          gtk_widget_show (separator);
        }
420 421 422 423
      n += glade_popup_action_populate_menu_real 
	(menu, glade_widget_get_parent (widget),
	 glade_widget_get_pack_actions (widget),
	 G_CALLBACK (glade_popup_menuitem_packing_activated), widget);
424 425 426
    }

  return n;
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
427
}
428

429
static GtkWidget *
430 431
glade_popup_create_menu (GladeWidget * widget,
                         GladePlaceholder * placeholder, gboolean packing)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
432
{
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  GladeWidgetAdaptor *current_item;
  GtkWidget *popup_menu;
  GtkWidget *separator;
  gboolean sensitive;
  GladePlaceholder *tmp_placeholder;
  gchar *book;

  sensitive = (current_item =
               glade_palette_get_current_item (glade_app_get_palette ())) !=
      NULL;

  popup_menu = gtk_menu_new ();

  if (current_item)
    {

      /* Special case for GtkAction accelerators  */
450
      if (widget && GTK_IS_ACTION_GROUP (glade_widget_get_object (widget)) &&
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
          (current_item->type == GTK_TYPE_ACTION ||
           g_type_is_a (current_item->type, GTK_TYPE_ACTION)))
        {
          glade_popup_append_item (popup_menu, NULL, _("_Add widget here"),
                                   NULL, TRUE, glade_popup_action_add_cb,
                                   widget);
        }
      else
        {
          tmp_placeholder = placeholder;
          if (!tmp_placeholder && widget)
            tmp_placeholder =
                find_placeholder (glade_widget_get_object (widget));

          glade_popup_append_item (popup_menu, NULL, _("_Add widget here"),
                                   NULL, tmp_placeholder != NULL,
                                   glade_popup_placeholder_add_cb,
                                   tmp_placeholder);
        }

      glade_popup_append_item (popup_menu, NULL, _("Add widget as _toplevel"),
                               NULL, TRUE, glade_popup_root_add_cb, NULL);

      separator = gtk_menu_item_new ();
      gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), separator);
      gtk_widget_show (separator);
    }

  sensitive = (widget != NULL);

  glade_popup_append_item (popup_menu, NULL, _("_Select"), NULL, sensitive,
                           glade_popup_select_cb, widget);
  glade_popup_append_item (popup_menu, GTK_STOCK_CUT, NULL, NULL, sensitive,
                           glade_popup_cut_cb, widget);
  glade_popup_append_item (popup_menu, GTK_STOCK_COPY, NULL, NULL, sensitive,
                           glade_popup_copy_cb, widget);

  /* paste is placholder specific when the popup is on a placeholder */
  sensitive = glade_clipboard_get_has_selection (glade_app_get_clipboard ());

  if (placeholder)
    glade_popup_append_item (popup_menu, GTK_STOCK_PASTE, NULL, NULL, sensitive,
                             glade_popup_placeholder_paste_cb, placeholder);
  else if (widget)
    glade_popup_append_item (popup_menu, GTK_STOCK_PASTE, NULL, NULL, sensitive,
                             glade_popup_paste_cb, widget);
  else
    glade_popup_append_item (popup_menu, GTK_STOCK_PASTE, NULL, NULL, sensitive,
                             glade_popup_paste_cb, NULL);


  glade_popup_append_item (popup_menu, GTK_STOCK_DELETE, NULL, NULL,
                           (widget != NULL), glade_popup_delete_cb, widget);


  /* packing actions are a little different on placholders */
  if (placeholder)
    {
509
      if (widget && glade_widget_get_actions (widget))
510 511 512 513 514 515 516 517
        {
          GtkWidget *separator = gtk_menu_item_new ();
          gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), separator);
          gtk_widget_show (separator);

          glade_popup_action_populate_menu_real
              (popup_menu,
               widget,
518
               glade_widget_get_actions (widget),
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
               G_CALLBACK (glade_popup_menuitem_activated), widget);
        }

      if (placeholder->packing_actions)
        {
          GtkWidget *separator = gtk_menu_item_new ();
          gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), separator);
          gtk_widget_show (separator);

          glade_popup_action_populate_menu_real
              (popup_menu,
               widget,
               placeholder->packing_actions,
               G_CALLBACK (glade_popup_menuitem_ph_packing_activated),
               placeholder);
        }
    }
536 537
  else if (widget && (glade_widget_get_actions (widget) || 
		      (packing && glade_widget_get_pack_actions (widget))))
538 539 540 541 542 543 544 545 546 547
    {
      GtkWidget *separator = gtk_menu_item_new ();
      gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), separator);
      gtk_widget_show (separator);

      glade_popup_action_populate_menu (popup_menu, widget, NULL, packing);
    }

  if (widget)
    {
548
      g_object_get (glade_widget_get_adaptor (widget), "book", &book, NULL);
549 550 551 552 553 554 555 556 557
      if (book)
        {
          GtkWidget *icon = glade_util_get_devhelp_icon (GTK_ICON_SIZE_MENU);
          GtkWidget *separator = gtk_menu_item_new ();
          gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), separator);
          gtk_widget_show (separator);

          glade_popup_append_item (popup_menu, NULL, _("Read _documentation"),
                                   icon, TRUE, glade_popup_docs_cb,
558
                                   glade_widget_get_adaptor (widget));
559 560 561 562 563
        }
      g_free (book);
    }

  return popup_menu;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
564 565
}

566
static GtkWidget *
567
glade_popup_create_clipboard_menu (GladeWidget * widget)
568
{
569
  GtkWidget *popup_menu;
570

571
  popup_menu = gtk_menu_new ();
572

573 574 575 576 577 578
  if (gtk_widget_is_toplevel (GTK_WIDGET (glade_widget_get_object (widget))))
    {
      glade_popup_append_item (popup_menu, GTK_STOCK_PASTE,
                               NULL, NULL, TRUE,
                               glade_popup_clipboard_paste_cb, NULL);
    }
579

580 581 582 583
  glade_popup_append_item (popup_menu, GTK_STOCK_DELETE, NULL, NULL, TRUE,
                           glade_popup_clipboard_delete_cb, widget);

  return popup_menu;
584 585
}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
586
void
587 588
glade_popup_widget_pop (GladeWidget * widget,
                        GdkEventButton * event, gboolean packing)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
589
{
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
  GtkWidget *popup_menu;
  gint button;
  gint event_time;

  g_return_if_fail (GLADE_IS_WIDGET (widget) || widget == NULL);

  popup_menu = glade_popup_create_menu (widget, NULL, packing);

  if (event)
    {
      button = event->button;
      event_time = event->time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }
  gtk_menu_popup (GTK_MENU (popup_menu), NULL, NULL,
                  NULL, NULL, button, event_time);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
610
}
611 612

void
613 614
glade_popup_placeholder_pop (GladePlaceholder * placeholder,
                             GdkEventButton * event)
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
  GladeWidget *widget;
  GtkWidget *popup_menu;
  gint button;
  gint event_time;

  g_return_if_fail (GLADE_IS_PLACEHOLDER (placeholder));

  widget = glade_placeholder_get_parent (placeholder);

  popup_menu = glade_popup_create_menu (widget, placeholder, TRUE);

  if (event)
    {
      button = event->button;
      event_time = event->time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }

  gtk_menu_popup (GTK_MENU (popup_menu), NULL, NULL,
                  NULL, NULL, button, event_time);
640
}
641 642

void
643
glade_popup_clipboard_pop (GladeWidget * widget, GdkEventButton * event)
644
{
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
  GtkWidget *popup_menu;
  gint button;
  gint event_time;

  g_return_if_fail (GLADE_IS_WIDGET (widget));

  popup_menu = glade_popup_create_clipboard_menu (widget);

  if (event)
    {
      button = event->button;
      event_time = event->time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }

  gtk_menu_popup (GTK_MENU (popup_menu), NULL, NULL,
                  NULL, NULL, button, event_time);
666
}
667

668
void
669
glade_popup_palette_pop (GladeWidgetAdaptor * adaptor, GdkEventButton * event)
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
  GladeProject *project;
  GtkWidget *popup_menu;
  gchar *book = NULL;
  gint button;
  gint event_time;

  g_return_if_fail (GLADE_IS_WIDGET_ADAPTOR (adaptor));

  popup_menu = gtk_menu_new ();

  project = glade_app_get_project ();

  glade_popup_append_item (popup_menu, NULL, _("Add widget as _toplevel"), NULL,
                           TRUE, glade_popup_root_add_cb, adaptor);

  g_object_get (adaptor, "book", &book, NULL);
  if (book && glade_util_have_devhelp ())
    {
      GtkWidget *icon = glade_util_get_devhelp_icon (GTK_ICON_SIZE_MENU);
      glade_popup_append_item (popup_menu, NULL, _("Read _documentation"), icon,
                               TRUE, glade_popup_docs_cb, adaptor);
    }
  g_free (book);


  if (event)
    {
      button = event->button;
      event_time = event->time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }

  gtk_menu_popup (GTK_MENU (popup_menu), NULL, NULL,
                  NULL, NULL, button, event_time);
709 710
}

711
static void
712
glade_popup_clear_property_cb (GtkMenuItem * item, GladeProperty * property)
713
{
714
  GValue value = { 0, };
715

716 717 718
  glade_property_get_default (property, &value);
  glade_command_set_property_value (property, &value);
  g_value_unset (&value);
719 720 721
}

static void
722
glade_popup_property_docs_cb (GtkMenuItem * item, GladeProperty * property)
723
{
724 725
  GladeWidgetAdaptor *adaptor, *prop_adaptor;
  gchar *search, *book;
726

727 728 729 730
  prop_adaptor = glade_widget_adaptor_from_pclass (property->klass);
  adaptor =
      glade_widget_adaptor_from_pspec (prop_adaptor, property->klass->pspec);
  search = g_strdup_printf ("The %s property", property->klass->id);
731

732
  g_object_get (adaptor, "book", &book, NULL);
733

734 735 736 737
  glade_editor_search_doc_search (glade_app_get_editor (),
                                  book,
                                  g_type_name (property->klass->pspec->
                                               owner_type), search);
738

739 740
  g_free (book);
  g_free (search);
741 742 743
}

void
744
glade_popup_property_pop (GladeProperty * property, GdkEventButton * event)
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
  GladeWidgetAdaptor *adaptor, *prop_adaptor;
  GtkWidget *popup_menu;
  gchar *book = NULL;
  gint button;
  gint event_time;

  prop_adaptor = glade_widget_adaptor_from_pclass (property->klass);
  adaptor =
      glade_widget_adaptor_from_pspec (prop_adaptor, property->klass->pspec);

  g_return_if_fail (GLADE_IS_WIDGET_ADAPTOR (adaptor));

  popup_menu = gtk_menu_new ();

  glade_popup_append_item (popup_menu, GTK_STOCK_CLEAR, _("Set default value"),
                           NULL, TRUE, glade_popup_clear_property_cb, property);

  g_object_get (adaptor, "book", &book, NULL);
  if (book && glade_util_have_devhelp ())
    {
      GtkWidget *icon = glade_util_get_devhelp_icon (GTK_ICON_SIZE_MENU);
      glade_popup_append_item (popup_menu, NULL, _("Read _documentation"), icon,
                               TRUE, glade_popup_property_docs_cb, property);
    }
  g_free (book);


  if (event)
    {
      button = event->button;
      event_time = event->time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }

  gtk_menu_popup (GTK_MENU (popup_menu), NULL, NULL,
                  NULL, NULL, button, event_time);
787 788
}

789

790
void
791
glade_popup_simple_pop (GdkEventButton * event)
792
{
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
  GtkWidget *popup_menu;
  gint button;
  gint event_time;

  popup_menu = glade_popup_create_menu (NULL, NULL, FALSE);
  if (!popup_menu)
    return;

  if (event)
    {
      button = event->button;
      event_time = event->time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }
  gtk_menu_popup (GTK_MENU (popup_menu), NULL, NULL,
                  NULL, NULL, button, event_time);
813 814
}

815
gboolean
816
glade_popup_is_popup_event (GdkEventButton * event)
817
{
818
  g_return_val_if_fail (event, FALSE);
819 820

#ifdef MAC_INTEGRATION
821 822
  return (event->type == GDK_BUTTON_PRESS && event->button == 1 &&
          ((event->state & GDK_MOD1_MASK) != 0));
823
#else
824
  return (event->type == GDK_BUTTON_PRESS && event->button == 3);
825 826
#endif
}