gtkmenutoolbutton.c 13.8 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Copyright (C) 2003 Ricardo Fernandez Pascual
 * Copyright (C) 2004 Paolo Borelli
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19
 */

20
#include "config.h"
21

22
#include "gtkmenutoolbutton.h"
23

24
#include "gtktogglebutton.h"
25 26
#include "gtkmenubutton.h"
#include "gtkmenubuttonprivate.h"
27
#include "gtkbox.h"
28 29
#include "gtkmenu.h"
#include "gtkmain.h"
30
#include "gtksizerequest.h"
31
#include "gtkbuildable.h"
32

33
#include "gtkprivate.h"
34
#include "gtkintl.h"
35 36


37 38 39 40 41 42 43 44 45 46 47
/**
 * SECTION:gtkmenutoolbutton
 * @Short_description: A GtkToolItem containing a button with an additional dropdown menu
 * @Title: GtkMenuToolButton
 * @See_also: #GtkToolbar, #GtkToolButton
 *
 * A #GtkMenuToolButton is a #GtkToolItem that contains a button and
 * a small additional button with an arrow. When clicked, the arrow
 * button pops up a dropdown menu.
 *
 * Use gtk_menu_tool_button_new() to create a new
48
 * #GtkMenuToolButton.
49
 *
50
 * # GtkMenuToolButton as GtkBuildable
51
 *
52
 * The GtkMenuToolButton implementation of the GtkBuildable interface
53 54
 * supports adding a menu by specifying “menu” as the “type” attribute
 * of a <child> element.
55
 *
56
 * An example for a UI definition fragment with menus:
57
 * |[
58 59 60 61 62
 * <object class="GtkMenuToolButton">
 *   <child type="menu">
 *     <object class="GtkMenu"/>
 *   </child>
 * </object>
63
 * ]|
64 65 66
 */


67 68 69 70 71 72 73
struct _GtkMenuToolButtonPrivate
{
  GtkWidget *button;
  GtkWidget *arrow_button;
  GtkWidget *box;
};

74 75 76 77 78 79
static void gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface   *iface);
static void gtk_menu_tool_button_buildable_add_child      (GtkBuildable        *buildable,
							   GtkBuilder          *builder,
							   GObject             *child,
							   const gchar         *type);

80 81
enum
{
82
  SHOW_MENU,
83 84 85 86 87 88
  LAST_SIGNAL
};

enum
{
  PROP_0,
89
  PROP_MENU
90 91 92 93
};

static gint signals[LAST_SIGNAL];

94 95 96
static GtkBuildableIface *parent_buildable_iface;

G_DEFINE_TYPE_WITH_CODE (GtkMenuToolButton, gtk_menu_tool_button, GTK_TYPE_TOOL_BUTTON,
97
                         G_ADD_PRIVATE (GtkMenuToolButton)
98 99
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_menu_tool_button_buildable_interface_init))
100 101 102 103

static void
gtk_menu_tool_button_construct_contents (GtkMenuToolButton *button)
{
104
  GtkMenuToolButtonPrivate *priv = button->priv;
105
  GtkWidget *box;
106
  GtkWidget *parent;
107 108 109 110 111 112
  GtkOrientation orientation;

  orientation = gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button));

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
113
      box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
114
      gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), GTK_ARROW_DOWN);
115 116 117
    }
  else
    {
118 119 120
      GtkTextDirection direction;
      GtkArrowType type;

121
      box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
122 123 124
      direction = gtk_widget_get_direction (GTK_WIDGET (button));
      type = (direction == GTK_TEXT_DIR_LTR ? GTK_ARROW_RIGHT : GTK_ARROW_LEFT);
      gtk_menu_button_set_direction (GTK_MENU_BUTTON (priv->arrow_button), type);
125 126
    }

127 128
  parent = gtk_widget_get_parent (priv->button);
  if (priv->button && parent)
129 130
    {
      g_object_ref (priv->button);
131
      gtk_container_remove (GTK_CONTAINER (parent),
132 133 134 135 136
                            priv->button);
      gtk_container_add (GTK_CONTAINER (box), priv->button);
      g_object_unref (priv->button);
    }

137 138
  parent = gtk_widget_get_parent (priv->arrow_button);
  if (priv->arrow_button && parent)
139 140
    {
      g_object_ref (priv->arrow_button);
141
      gtk_container_remove (GTK_CONTAINER (parent),
142 143 144 145 146 147 148 149
                            priv->arrow_button);
      gtk_box_pack_end (GTK_BOX (box), priv->arrow_button,
                        FALSE, FALSE, 0);
      g_object_unref (priv->arrow_button);
    }

  if (priv->box)
    {
150 151 152 153 154 155 156 157 158 159 160
      gchar *tmp;

      /* Transfer a possible tooltip to the new box */
      g_object_get (priv->box, "tooltip-markup", &tmp, NULL);

      if (tmp)
        {
	  g_object_set (box, "tooltip-markup", tmp, NULL);
	  g_free (tmp);
	}

161 162 163 164 165 166 167 168 169 170 171
      /* Note: we are not destroying the button and the arrow_button
       * here because they were removed from their container above
       */
      gtk_widget_destroy (priv->box);
    }

  priv->box = box;

  gtk_container_add (GTK_CONTAINER (button), priv->box);
  gtk_widget_show_all (priv->box);

172 173 174
  gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
			 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
  
175 176 177 178 179 180 181 182 183
  gtk_widget_queue_resize (GTK_WIDGET (button));
}

static void
gtk_menu_tool_button_toolbar_reconfigured (GtkToolItem *toolitem)
{
  gtk_menu_tool_button_construct_contents (GTK_MENU_TOOL_BUTTON (toolitem));

  /* chain up */
Matthias Clasen's avatar
Matthias Clasen committed
184
  GTK_TOOL_ITEM_CLASS (gtk_menu_tool_button_parent_class)->toolbar_reconfigured (toolitem);
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
}

static void
gtk_menu_tool_button_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);

  switch (prop_id)
    {
    case PROP_MENU:
      gtk_menu_tool_button_set_menu (button, g_value_get_object (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_menu_tool_button_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  GtkMenuToolButton *button = GTK_MENU_TOOL_BUTTON (object);

  switch (prop_id)
    {
    case PROP_MENU:
218
      g_value_set_object (value, gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button)));
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_menu_tool_button_class_init (GtkMenuToolButtonClass *klass)
{
  GObjectClass *object_class;
  GtkToolItemClass *toolitem_class;

  object_class = (GObjectClass *)klass;
  toolitem_class = (GtkToolItemClass *)klass;

  object_class->set_property = gtk_menu_tool_button_set_property;
  object_class->get_property = gtk_menu_tool_button_get_property;
238

239 240
  toolitem_class->toolbar_reconfigured = gtk_menu_tool_button_toolbar_reconfigured;

Matthias Clasen's avatar
Matthias Clasen committed
241 242 243 244 245 246
  /**
   * GtkMenuToolButton::show-menu:
   * @button: the object on which the signal is emitted
   *
   * The ::show-menu signal is emitted before the menu is shown.
   *
247
   * It can be used to populate the menu on demand, using
248
   * gtk_menu_tool_button_set_menu().
Matthias Clasen's avatar
Matthias Clasen committed
249

250
   * Note that even if you populate the menu dynamically in this way,
Matthias Clasen's avatar
Matthias Clasen committed
251 252 253
   * you must set an empty menu on the #GtkMenuToolButton beforehand,
   * since the arrow is made insensitive if the menu is not set.
   */
254
  signals[SHOW_MENU] =
255
    g_signal_new (I_("show-menu"),
256
                  G_OBJECT_CLASS_TYPE (klass),
257 258 259 260 261
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuToolButtonClass, show_menu),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
262 263 264 265 266 267 268

  g_object_class_install_property (object_class,
                                   PROP_MENU,
                                   g_param_spec_object ("menu",
                                                        P_("Menu"),
                                                        P_("The dropdown menu"),
                                                        GTK_TYPE_MENU,
269
                                                        GTK_PARAM_READWRITE));
270 271 272 273 274 275 276 277 278
}

static void
gtk_menu_tool_button_init (GtkMenuToolButton *button)
{
  GtkWidget *box;
  GtkWidget *arrow_button;
  GtkWidget *real_button;

279
  button->priv = gtk_menu_tool_button_get_instance_private (button);
280 281 282

  gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (button), FALSE);

283
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
284

Javier Jardón's avatar
Javier Jardón committed
285
  real_button = gtk_bin_get_child (GTK_BIN (button));
286 287 288 289 290
  g_object_ref (real_button);
  gtk_container_remove (GTK_CONTAINER (button), real_button);
  gtk_container_add (GTK_CONTAINER (box), real_button);
  g_object_unref (real_button);

291
  arrow_button = gtk_menu_button_new ();
292 293 294 295 296 297 298 299 300
  gtk_box_pack_end (GTK_BOX (box), arrow_button,
                    FALSE, FALSE, 0);

  /* the arrow button is insentive until we set a menu */
  gtk_widget_set_sensitive (arrow_button, FALSE);

  gtk_widget_show_all (box);

  gtk_container_add (GTK_CONTAINER (button), box);
301 302
  gtk_menu_button_set_align_widget (GTK_MENU_BUTTON (arrow_button),
                                    GTK_WIDGET (button));
303 304 305 306 307 308

  button->priv->button = real_button;
  button->priv->arrow_button = arrow_button;
  button->priv->box = box;
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static void
gtk_menu_tool_button_buildable_add_child (GtkBuildable *buildable,
					  GtkBuilder   *builder,
					  GObject      *child,
					  const gchar  *type)
{
  if (type && strcmp (type, "menu") == 0)
    gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (buildable),
                                   GTK_WIDGET (child));
  else
    parent_buildable_iface->add_child (buildable, builder, child, type);
}

static void
gtk_menu_tool_button_buildable_interface_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
  iface->add_child = gtk_menu_tool_button_buildable_add_child;
}

329 330
/**
 * gtk_menu_tool_button_new:
331 332
 * @icon_widget: (allow-none): a widget that will be used as icon widget, or %NULL
 * @label: (allow-none): a string that will be used as label, or %NULL
333 334 335 336
 *
 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
 * @label as label.
 *
337
 * Returns: the new #GtkMenuToolButton
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
 *
 * Since: 2.6
 **/
GtkToolItem *
gtk_menu_tool_button_new (GtkWidget   *icon_widget,
                          const gchar *label)
{
  GtkMenuToolButton *button;

  button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON, NULL);

  if (label)
    gtk_tool_button_set_label (GTK_TOOL_BUTTON (button), label);

  if (icon_widget)
    gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (button), icon_widget);

  return GTK_TOOL_ITEM (button);
}

/**
 * gtk_menu_tool_button_new_from_stock:
 * @stock_id: the name of a stock item
 *
 * Creates a new #GtkMenuToolButton.
 * The new #GtkMenuToolButton will contain an icon and label from
 * the stock item indicated by @stock_id.
 *
366
 * Returns: the new #GtkMenuToolButton
367 368
 *
 * Since: 2.6
369 370
 *
 * Deprecated: 3.10: Use gtk_menu_tool_button_new() instead.
371 372 373 374 375 376 377 378 379
 **/
GtkToolItem *
gtk_menu_tool_button_new_from_stock (const gchar *stock_id)
{
  GtkMenuToolButton *button;

  g_return_val_if_fail (stock_id != NULL, NULL);

  button = g_object_new (GTK_TYPE_MENU_TOOL_BUTTON,
380
			 "stock-id", stock_id,
381 382 383 384 385
			 NULL);

  return GTK_TOOL_ITEM (button);
}

386
static void
387
_show_menu_emit (gpointer user_data)
388
{
389 390
  GtkMenuToolButton *button = (GtkMenuToolButton *) user_data;
  g_signal_emit (button, signals[SHOW_MENU], 0);
391 392
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/**
 * gtk_menu_tool_button_set_menu:
 * @button: a #GtkMenuToolButton
 * @menu: the #GtkMenu associated with #GtkMenuToolButton
 *
 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
 * If @menu is NULL, the arrow button becomes insensitive.
 *
 * Since: 2.6
 **/
void
gtk_menu_tool_button_set_menu (GtkMenuToolButton *button,
                               GtkWidget         *menu)
{
  GtkMenuToolButtonPrivate *priv;

  g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));
  g_return_if_fail (GTK_IS_MENU (menu) || menu == NULL);

  priv = button->priv;

414 415 416 417
  _gtk_menu_button_set_popup_with_func (GTK_MENU_BUTTON (priv->arrow_button),
                                        menu,
                                        _show_menu_emit,
                                        button);
418 419 420 421 422 423 424 425 426 427

  g_object_notify (G_OBJECT (button), "menu");
}

/**
 * gtk_menu_tool_button_get_menu:
 * @button: a #GtkMenuToolButton
 *
 * Gets the #GtkMenu associated with #GtkMenuToolButton.
 *
428
 * Returns: (transfer none): the #GtkMenu associated
429
 *     with #GtkMenuToolButton
430 431 432 433 434 435
 *
 * Since: 2.6
 **/
GtkWidget *
gtk_menu_tool_button_get_menu (GtkMenuToolButton *button)
{
436 437
  GtkMenu *ret;

438 439
  g_return_val_if_fail (GTK_IS_MENU_TOOL_BUTTON (button), NULL);

440
  ret = gtk_menu_button_get_popup (GTK_MENU_BUTTON (button->priv->arrow_button));
441 442 443 444
  if (!ret)
    return NULL;

  return GTK_WIDGET (ret);
445 446
}

447 448 449
/**
 * gtk_menu_tool_button_set_arrow_tooltip_text:
 * @button: a #GtkMenuToolButton
450
 * @text: text to be used as tooltip text for button’s arrow button
451 452
 *
 * Sets the tooltip text to be used as tooltip for the arrow button which
453
 * pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a tooltip
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
 * on the whole #GtkMenuToolButton.
 *
 * Since: 2.12
 **/
void
gtk_menu_tool_button_set_arrow_tooltip_text (GtkMenuToolButton *button,
					     const gchar       *text)
{
  g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));

  gtk_widget_set_tooltip_text (button->priv->arrow_button, text);
}

/**
 * gtk_menu_tool_button_set_arrow_tooltip_markup:
 * @button: a #GtkMenuToolButton
470
 * @markup: markup text to be used as tooltip text for button’s arrow button
471 472
 *
 * Sets the tooltip markup text to be used as tooltip for the arrow button
473 474
 * which pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting
 * a tooltip on the whole #GtkMenuToolButton.
475 476 477 478 479 480 481 482 483 484 485
 *
 * Since: 2.12
 **/
void
gtk_menu_tool_button_set_arrow_tooltip_markup (GtkMenuToolButton *button,
					       const gchar       *markup)
{
  g_return_if_fail (GTK_IS_MENU_TOOL_BUTTON (button));

  gtk_widget_set_tooltip_markup (button->priv->arrow_button, markup);
}