2014-02-07 19:51:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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 licence, 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
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Matthias Clasen
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkmodelbutton.h"
|
|
|
|
|
|
|
|
#include "gtkbutton.h"
|
|
|
|
#include "gtkbuttonprivate.h"
|
|
|
|
#include "gtkwidgetprivate.h"
|
|
|
|
#include "gtkmenutrackeritem.h"
|
|
|
|
#include "gtkimage.h"
|
|
|
|
#include "gtklabel.h"
|
|
|
|
#include "gtkbox.h"
|
2014-06-15 15:57:59 +00:00
|
|
|
#include "gtkrender.h"
|
2014-02-07 19:51:34 +00:00
|
|
|
#include "gtkstylecontext.h"
|
|
|
|
#include "gtktypebuiltins.h"
|
2014-10-24 03:38:46 +00:00
|
|
|
#include "gtkstack.h"
|
|
|
|
#include "gtkpopover.h"
|
2014-10-26 22:08:26 +00:00
|
|
|
#include "gtkintl.h"
|
2015-10-30 04:29:47 +00:00
|
|
|
#include "gtkcssnodeprivate.h"
|
2015-12-05 06:42:41 +00:00
|
|
|
#include "gtkcsscustomgadgetprivate.h"
|
2015-12-19 19:53:57 +00:00
|
|
|
#include "gtkbuiltiniconprivate.h"
|
|
|
|
#include "gtkcsstypesprivate.h"
|
2015-10-30 04:29:47 +00:00
|
|
|
#include "gtkstylecontextprivate.h"
|
2015-12-05 06:42:41 +00:00
|
|
|
#include "gtkcontainerprivate.h"
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2014-10-26 23:04:28 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gtkmodelbutton
|
|
|
|
* @Short_description: A button that uses a GAction as model
|
|
|
|
* @Title: GtkModelButton
|
|
|
|
*
|
|
|
|
* GtkModelButton is a button class that can use a #GAction as its model.
|
|
|
|
* In contrast to #GtkToggleButton or #GtkRadioButton, which can also
|
2015-02-18 10:57:05 +00:00
|
|
|
* be backed by a #GAction via the #GtkActionable:action-name property,
|
2014-10-26 23:04:28 +00:00
|
|
|
* GtkModelButton will adapt its appearance according to the kind of
|
|
|
|
* action it is backed by, and appear either as a plain, check or
|
|
|
|
* radio button.
|
|
|
|
*
|
|
|
|
* Model buttons are used when popovers from a menu model with
|
|
|
|
* gtk_popover_new_from_model(); they can also be used manually in
|
|
|
|
* a #GtkPopoverMenu.
|
|
|
|
*
|
|
|
|
* When the action is specified via the #GtkActionable:action-name
|
|
|
|
* and #GtkActionable:action-target properties, the role of the button
|
|
|
|
* (i.e. whether it is a plain, check or radio button) is determined by
|
|
|
|
* the type of the action and doesn't have to be explicitly specified
|
|
|
|
* with the #GtkModelButton:role property.
|
|
|
|
*
|
|
|
|
* The content of the button is specified by the #GtkModelButton:text
|
|
|
|
* and #GtkModelButton:icon properties.
|
|
|
|
*
|
|
|
|
* The appearance of model buttons can be influenced with the
|
|
|
|
* #GtkModelButton:centered and #GtkModelButton:iconic properties.
|
|
|
|
*
|
|
|
|
* Model buttons have built-in support for submenus in #GtkPopoverMenu.
|
|
|
|
* To make a GtkModelButton that opens a submenu when activated, set
|
|
|
|
* the #GtkModelButton:menu-name property. To make a button that goes
|
|
|
|
* back to the parent menu, you should set the #GtkModelButton:inverted
|
|
|
|
* property to place the submenu indicator at the opposite side.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* |[
|
|
|
|
* <object class="GtkPopoverMenu">
|
|
|
|
* <child>
|
|
|
|
* <object class="GtkBox">
|
|
|
|
* <property name="visible">True</property>
|
|
|
|
* <property name="margin">10</property>
|
|
|
|
* <child>
|
|
|
|
* <object class="GtkModelButton">
|
|
|
|
* <property name="visible">True</property>
|
|
|
|
* <property name="action-name">view.cut</property>
|
|
|
|
* <property name="text" translatable="yes">Cut</property>
|
|
|
|
* </object>
|
|
|
|
* </child>
|
|
|
|
* <child>
|
|
|
|
* <object class="GtkModelButton">
|
|
|
|
* <property name="visible">True</property>
|
|
|
|
* <property name="action-name">view.copy</property>
|
|
|
|
* <property name="text" translatable="yes">Copy</property>
|
|
|
|
* </object>
|
|
|
|
* </child>
|
|
|
|
* <child>
|
|
|
|
* <object class="GtkModelButton">
|
|
|
|
* <property name="visible">True</property>
|
|
|
|
* <property name="action-name">view.paste</property>
|
|
|
|
* <property name="text" translatable="yes">Paste</property>
|
|
|
|
* </object>
|
|
|
|
* </child>
|
|
|
|
* </object>
|
|
|
|
* </child>
|
|
|
|
* </object>
|
|
|
|
* ]|
|
2015-11-03 17:51:52 +00:00
|
|
|
*
|
|
|
|
* # CSS nodes
|
|
|
|
*
|
|
|
|
* |[<!-- language="plain" -->
|
|
|
|
* modelbutton
|
2015-12-16 15:55:52 +00:00
|
|
|
* ├── <child>
|
2015-11-03 17:51:52 +00:00
|
|
|
* ╰── check
|
|
|
|
* ]|
|
|
|
|
*
|
|
|
|
* |[<!-- language="plain" -->
|
|
|
|
* modelbutton
|
2015-12-16 15:55:52 +00:00
|
|
|
* ├── <child>
|
2015-11-03 17:51:52 +00:00
|
|
|
* ╰── radio
|
|
|
|
* ]|
|
|
|
|
*
|
|
|
|
* |[<!-- language="plain" -->
|
|
|
|
* modelbutton
|
2015-12-16 15:55:52 +00:00
|
|
|
* ├── <child>
|
2015-11-03 17:51:52 +00:00
|
|
|
* ╰── arrow
|
|
|
|
* ]|
|
|
|
|
*
|
|
|
|
* GtkModelButton has a main CSS node with name modelbutton, and a subnode,
|
|
|
|
* which will have the name check, radio or arrow, depending on the role
|
|
|
|
* of the button and whether it has a menu name set.
|
|
|
|
*
|
|
|
|
* The subnode is positioned before or after the content nodes and gets the
|
|
|
|
* .left or .right style class, depending on where it is located.
|
2015-12-16 16:37:49 +00:00
|
|
|
*
|
|
|
|
* |[<!-- language="plain" -->
|
|
|
|
* button.model
|
|
|
|
* ├── <child>
|
|
|
|
* ╰── check
|
|
|
|
* ]|
|
|
|
|
*
|
2016-02-08 21:49:01 +00:00
|
|
|
* Iconic model buttons (see #GtkModelButton:iconic) change the name of
|
2015-12-16 16:37:49 +00:00
|
|
|
* their main node to button and add a .model style class to it. The indicator
|
|
|
|
* subnode is invisible in this case.
|
2014-10-26 23:04:28 +00:00
|
|
|
*/
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
struct _GtkModelButton
|
|
|
|
{
|
|
|
|
GtkButton parent_instance;
|
2014-10-26 23:04:28 +00:00
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *image;
|
|
|
|
GtkWidget *label;
|
2015-12-05 06:42:41 +00:00
|
|
|
GtkCssGadget *gadget;
|
|
|
|
GtkCssGadget *indicator_gadget;
|
2014-10-24 01:43:28 +00:00
|
|
|
gboolean active;
|
2014-04-26 20:37:16 +00:00
|
|
|
gboolean centered;
|
2014-02-07 19:51:34 +00:00
|
|
|
gboolean inverted;
|
2014-04-26 17:50:55 +00:00
|
|
|
gboolean iconic;
|
2014-10-24 03:38:46 +00:00
|
|
|
gchar *menu_name;
|
2014-10-26 22:08:26 +00:00
|
|
|
GtkButtonRole role;
|
2014-02-07 19:51:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef GtkButtonClass GtkModelButtonClass;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkModelButton, gtk_model_button, GTK_TYPE_BUTTON)
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2014-10-26 22:08:26 +00:00
|
|
|
PROP_ROLE,
|
2014-02-07 19:51:34 +00:00
|
|
|
PROP_ICON,
|
|
|
|
PROP_TEXT,
|
2014-10-24 01:43:28 +00:00
|
|
|
PROP_ACTIVE,
|
2014-10-24 03:38:46 +00:00
|
|
|
PROP_MENU_NAME,
|
2014-02-07 19:51:34 +00:00
|
|
|
PROP_INVERTED,
|
2014-04-26 17:50:55 +00:00
|
|
|
PROP_CENTERED,
|
2014-10-26 22:08:26 +00:00
|
|
|
PROP_ICONIC,
|
|
|
|
LAST_PROPERTY
|
2014-02-07 19:51:34 +00:00
|
|
|
};
|
|
|
|
|
2014-10-26 22:08:26 +00:00
|
|
|
static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
|
|
|
|
|
2015-11-03 17:20:53 +00:00
|
|
|
static gboolean
|
|
|
|
indicator_is_left (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkModelButton *button = GTK_MODEL_BUTTON (widget);
|
|
|
|
|
|
|
|
return ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL && !button->inverted) ||
|
|
|
|
(gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR && button->inverted));
|
|
|
|
}
|
|
|
|
|
2014-10-26 22:13:02 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_update_state (GtkModelButton *button)
|
|
|
|
{
|
|
|
|
GtkStateFlags state;
|
2016-01-27 15:32:39 +00:00
|
|
|
GtkStateFlags indicator_state;
|
2015-12-19 19:53:57 +00:00
|
|
|
GtkCssImageBuiltinType image_type = GTK_CSS_IMAGE_BUILTIN_NONE;
|
2014-10-26 22:13:02 +00:00
|
|
|
|
|
|
|
state = gtk_widget_get_state_flags (GTK_WIDGET (button));
|
2016-01-27 15:32:39 +00:00
|
|
|
indicator_state = state;
|
2014-10-26 22:13:02 +00:00
|
|
|
|
2016-01-13 05:05:16 +00:00
|
|
|
gtk_css_gadget_set_state (button->gadget, state);
|
2015-12-05 06:42:41 +00:00
|
|
|
|
2015-12-19 19:53:57 +00:00
|
|
|
if (button->role == GTK_BUTTON_ROLE_CHECK)
|
|
|
|
{
|
|
|
|
if (button->active && !button->menu_name)
|
|
|
|
{
|
2016-01-27 15:32:39 +00:00
|
|
|
indicator_state |= GTK_STATE_FLAG_CHECKED;
|
2016-05-03 10:57:50 +00:00
|
|
|
image_type = GTK_CSS_IMAGE_BUILTIN_CHECK;
|
2015-12-19 19:53:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-27 15:32:39 +00:00
|
|
|
indicator_state &= ~GTK_STATE_FLAG_CHECKED;
|
2015-12-19 19:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (button->role == GTK_BUTTON_ROLE_RADIO)
|
2015-11-03 17:20:53 +00:00
|
|
|
{
|
|
|
|
if (button->active && !button->menu_name)
|
2015-12-19 19:53:57 +00:00
|
|
|
{
|
2016-01-27 15:32:39 +00:00
|
|
|
indicator_state |= GTK_STATE_FLAG_CHECKED;
|
2016-05-03 10:57:50 +00:00
|
|
|
image_type = GTK_CSS_IMAGE_BUILTIN_OPTION;
|
2015-12-19 19:53:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-27 15:32:39 +00:00
|
|
|
indicator_state &= ~GTK_STATE_FLAG_CHECKED;
|
2015-12-19 19:53:57 +00:00
|
|
|
}
|
2015-11-03 17:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (button->menu_name)
|
|
|
|
{
|
|
|
|
if (indicator_is_left (GTK_WIDGET (button)))
|
2015-12-19 19:53:57 +00:00
|
|
|
image_type = GTK_CSS_IMAGE_BUILTIN_ARROW_LEFT;
|
2015-11-03 17:20:53 +00:00
|
|
|
else
|
2015-12-19 19:53:57 +00:00
|
|
|
image_type = GTK_CSS_IMAGE_BUILTIN_ARROW_RIGHT;
|
2015-11-03 17:20:53 +00:00
|
|
|
}
|
2014-10-26 22:13:02 +00:00
|
|
|
|
2015-12-19 19:53:57 +00:00
|
|
|
gtk_builtin_icon_set_image (GTK_BUILTIN_ICON (button->indicator_gadget), image_type);
|
2016-01-27 15:32:39 +00:00
|
|
|
|
|
|
|
if (button->iconic)
|
|
|
|
gtk_css_gadget_set_state (button->gadget, indicator_state);
|
|
|
|
else
|
|
|
|
gtk_css_gadget_set_state (button->gadget, state);
|
|
|
|
|
|
|
|
gtk_css_gadget_set_state (button->indicator_gadget, indicator_state);
|
2014-10-26 22:13:02 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:20:53 +00:00
|
|
|
static void
|
|
|
|
update_node_ordering (GtkModelButton *button)
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
GtkCssNode *widget_node, *indicator_node, *node;
|
2015-11-03 17:20:53 +00:00
|
|
|
|
|
|
|
widget_node = gtk_widget_get_css_node (GTK_WIDGET (button));
|
2015-12-05 06:42:41 +00:00
|
|
|
indicator_node = gtk_css_gadget_get_node (button->indicator_gadget);
|
2015-11-03 17:20:53 +00:00
|
|
|
|
|
|
|
if (indicator_is_left (GTK_WIDGET (button)))
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_css_gadget_add_class (button->indicator_gadget, GTK_STYLE_CLASS_LEFT);
|
|
|
|
gtk_css_gadget_remove_class (button->indicator_gadget, GTK_STYLE_CLASS_RIGHT);
|
2015-11-03 17:20:53 +00:00
|
|
|
|
|
|
|
node = gtk_css_node_get_first_child (widget_node);
|
2015-12-05 06:42:41 +00:00
|
|
|
if (node != indicator_node)
|
|
|
|
gtk_css_node_insert_before (widget_node, indicator_node, node);
|
2015-11-03 17:20:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_css_gadget_remove_class (button->indicator_gadget, GTK_STYLE_CLASS_LEFT);
|
|
|
|
gtk_css_gadget_add_class (button->indicator_gadget, GTK_STYLE_CLASS_RIGHT);
|
2015-11-03 17:20:53 +00:00
|
|
|
|
|
|
|
node = gtk_css_node_get_last_child (widget_node);
|
2015-12-05 06:42:41 +00:00
|
|
|
if (node != indicator_node)
|
|
|
|
gtk_css_node_insert_after (widget_node, indicator_node, node);
|
2015-11-03 17:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 04:08:00 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_state_flags_changed (GtkWidget *widget,
|
|
|
|
GtkStateFlags previous_flags)
|
|
|
|
{
|
|
|
|
gtk_model_button_update_state (GTK_MODEL_BUTTON (widget));
|
2015-12-02 03:36:31 +00:00
|
|
|
|
|
|
|
GTK_WIDGET_CLASS (gtk_model_button_parent_class)->state_flags_changed (widget, previous_flags);
|
2015-10-31 04:08:00 +00:00
|
|
|
}
|
2014-10-26 22:13:02 +00:00
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
static void
|
2015-11-03 17:20:53 +00:00
|
|
|
gtk_model_button_direction_changed (GtkWidget *widget,
|
|
|
|
GtkTextDirection previous_dir)
|
|
|
|
{
|
|
|
|
GtkModelButton *button = GTK_MODEL_BUTTON (widget);
|
|
|
|
|
|
|
|
gtk_model_button_update_state (button);
|
|
|
|
update_node_ordering (button);
|
|
|
|
|
|
|
|
GTK_WIDGET_CLASS (gtk_model_button_parent_class)->direction_changed (widget, previous_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_node_name (GtkModelButton *button)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
|
|
|
AtkObject *accessible;
|
|
|
|
AtkRole a11y_role;
|
2015-12-05 06:42:41 +00:00
|
|
|
const gchar *indicator_name;
|
|
|
|
gboolean indicator_visible;
|
|
|
|
GtkCssNode *indicator_node;
|
2014-02-07 19:51:34 +00:00
|
|
|
|
|
|
|
accessible = gtk_widget_get_accessible (GTK_WIDGET (button));
|
2015-11-03 17:20:53 +00:00
|
|
|
switch (button->role)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2014-10-26 22:08:26 +00:00
|
|
|
case GTK_BUTTON_ROLE_NORMAL:
|
2014-02-07 19:51:34 +00:00
|
|
|
a11y_role = ATK_ROLE_PUSH_BUTTON;
|
2015-11-03 17:20:53 +00:00
|
|
|
if (button->menu_name)
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
indicator_name = I_("arrow");
|
|
|
|
indicator_visible = TRUE;
|
2015-11-03 17:20:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
indicator_name = I_("check");
|
|
|
|
indicator_visible = FALSE;
|
2015-11-03 17:20:53 +00:00
|
|
|
}
|
2014-02-07 19:51:34 +00:00
|
|
|
break;
|
|
|
|
|
2014-10-26 22:08:26 +00:00
|
|
|
case GTK_BUTTON_ROLE_CHECK:
|
2014-02-07 19:51:34 +00:00
|
|
|
a11y_role = ATK_ROLE_CHECK_BOX;
|
2015-12-05 06:42:41 +00:00
|
|
|
indicator_name = I_("check");
|
|
|
|
indicator_visible = TRUE;
|
2014-02-07 19:51:34 +00:00
|
|
|
break;
|
|
|
|
|
2014-10-26 22:08:26 +00:00
|
|
|
case GTK_BUTTON_ROLE_RADIO:
|
2014-02-07 19:51:34 +00:00
|
|
|
a11y_role = ATK_ROLE_RADIO_BUTTON;
|
2015-12-05 06:42:41 +00:00
|
|
|
indicator_name = I_("radio");
|
|
|
|
indicator_visible = TRUE;
|
2014-02-07 19:51:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
if (button->iconic)
|
|
|
|
indicator_visible = FALSE;
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
atk_object_set_role (accessible, a11y_role);
|
2015-12-05 06:42:41 +00:00
|
|
|
|
|
|
|
indicator_node = gtk_css_gadget_get_node (button->indicator_gadget);
|
|
|
|
gtk_css_node_set_name (indicator_node, indicator_name);
|
|
|
|
gtk_css_node_set_visible (indicator_node, indicator_visible);
|
2015-11-03 17:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_model_button_set_role (GtkModelButton *button,
|
|
|
|
GtkButtonRole role)
|
|
|
|
{
|
|
|
|
if (role == button->role)
|
|
|
|
return;
|
|
|
|
|
|
|
|
button->role = role;
|
|
|
|
|
|
|
|
update_node_name (button);
|
2014-10-26 22:13:02 +00:00
|
|
|
|
|
|
|
gtk_model_button_update_state (button);
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (button));
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ROLE]);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 17:50:55 +00:00
|
|
|
static void
|
|
|
|
update_visibility (GtkModelButton *button)
|
|
|
|
{
|
|
|
|
gboolean has_icon;
|
|
|
|
gboolean has_text;
|
|
|
|
|
|
|
|
has_icon = gtk_image_get_storage_type (GTK_IMAGE (button->image)) != GTK_IMAGE_EMPTY;
|
|
|
|
has_text = gtk_label_get_text (GTK_LABEL (button->label))[0] != '\0';
|
|
|
|
|
2014-11-19 13:40:10 +00:00
|
|
|
gtk_widget_set_visible (button->image, has_icon && (button->iconic || !has_text));
|
2014-04-26 17:50:55 +00:00
|
|
|
gtk_widget_set_visible (button->label, has_text && (!button->iconic || !has_icon));
|
|
|
|
}
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_set_icon (GtkModelButton *button,
|
|
|
|
GIcon *icon)
|
|
|
|
{
|
|
|
|
gtk_image_set_from_gicon (GTK_IMAGE (button->image), icon, GTK_ICON_SIZE_MENU);
|
2014-04-26 17:50:55 +00:00
|
|
|
update_visibility (button);
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ICON]);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_model_button_set_text (GtkModelButton *button,
|
|
|
|
const gchar *text)
|
|
|
|
{
|
2014-04-26 17:50:55 +00:00
|
|
|
gtk_label_set_text_with_mnemonic (GTK_LABEL (button->label), text);
|
|
|
|
update_visibility (button);
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_TEXT]);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-24 01:43:28 +00:00
|
|
|
gtk_model_button_set_active (GtkModelButton *button,
|
|
|
|
gboolean active)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2014-10-26 22:08:26 +00:00
|
|
|
if (button->active == active)
|
|
|
|
return;
|
|
|
|
|
2014-10-24 01:43:28 +00:00
|
|
|
button->active = active;
|
2014-07-02 19:12:46 +00:00
|
|
|
gtk_model_button_update_state (button);
|
2014-02-07 19:51:34 +00:00
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (button));
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ACTIVE]);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 03:38:46 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_set_menu_name (GtkModelButton *button,
|
|
|
|
const gchar *menu_name)
|
|
|
|
{
|
|
|
|
g_free (button->menu_name);
|
|
|
|
button->menu_name = g_strdup (menu_name);
|
2015-11-03 17:20:53 +00:00
|
|
|
|
|
|
|
update_node_name (button);
|
2014-10-24 03:38:46 +00:00
|
|
|
gtk_model_button_update_state (button);
|
2015-11-03 17:20:53 +00:00
|
|
|
|
2014-10-24 03:38:46 +00:00
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (button));
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_MENU_NAME]);
|
2014-10-24 03:38:46 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_set_inverted (GtkModelButton *button,
|
|
|
|
gboolean inverted)
|
|
|
|
{
|
2014-10-26 22:08:26 +00:00
|
|
|
if (button->inverted == inverted)
|
|
|
|
return;
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
button->inverted = inverted;
|
2015-11-03 17:20:53 +00:00
|
|
|
gtk_model_button_update_state (button);
|
|
|
|
update_node_ordering (button);
|
2014-02-07 19:51:34 +00:00
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (button));
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_INVERTED]);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_model_button_set_centered (GtkModelButton *button,
|
2014-04-26 20:37:16 +00:00
|
|
|
gboolean centered)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2014-10-26 22:08:26 +00:00
|
|
|
if (button->centered == centered)
|
|
|
|
return;
|
|
|
|
|
2014-04-26 20:37:16 +00:00
|
|
|
button->centered = centered;
|
|
|
|
gtk_widget_set_halign (button->box, button->centered ? GTK_ALIGN_CENTER : GTK_ALIGN_FILL);
|
2014-02-07 19:51:34 +00:00
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (button));
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_CENTERED]);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 17:50:55 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_set_iconic (GtkModelButton *button,
|
|
|
|
gboolean iconic)
|
|
|
|
{
|
2015-10-30 04:29:47 +00:00
|
|
|
GtkCssNode *widget_node;
|
2015-12-05 06:42:41 +00:00
|
|
|
GtkCssNode *indicator_node;
|
2014-06-28 04:00:16 +00:00
|
|
|
|
2014-10-26 22:08:26 +00:00
|
|
|
if (button->iconic == iconic)
|
|
|
|
return;
|
|
|
|
|
2014-04-26 17:50:55 +00:00
|
|
|
button->iconic = iconic;
|
2014-06-28 04:00:16 +00:00
|
|
|
|
2015-10-30 04:29:47 +00:00
|
|
|
widget_node = gtk_widget_get_css_node (GTK_WIDGET (button));
|
2015-12-05 06:42:41 +00:00
|
|
|
indicator_node = gtk_css_gadget_get_node (button->indicator_gadget);
|
2014-04-26 17:50:55 +00:00
|
|
|
if (iconic)
|
|
|
|
{
|
2015-10-30 04:29:47 +00:00
|
|
|
gtk_css_node_set_name (widget_node, I_("button"));
|
2015-12-16 16:37:49 +00:00
|
|
|
gtk_css_gadget_add_class (button->gadget, "model");
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_css_gadget_add_class (button->gadget, "image-button");
|
2014-04-26 17:50:55 +00:00
|
|
|
gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NORMAL);
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_css_node_set_visible (indicator_node, FALSE);
|
2014-04-26 17:50:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-30 04:29:47 +00:00
|
|
|
gtk_css_node_set_name (widget_node, I_("modelbutton"));
|
2015-12-16 16:37:49 +00:00
|
|
|
gtk_css_gadget_remove_class (button->gadget, "model");
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_css_gadget_remove_class (button->gadget, "image-button");
|
2014-04-26 17:50:55 +00:00
|
|
|
gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_css_node_set_visible (indicator_node,
|
|
|
|
button->role != GTK_BUTTON_ROLE_NORMAL ||
|
|
|
|
button->menu_name == NULL);
|
2014-04-26 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update_visibility (button);
|
|
|
|
gtk_widget_queue_resize (GTK_WIDGET (button));
|
2014-10-26 22:08:26 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ICONIC]);
|
2014-04-26 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 01:43:28 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkModelButton *button = GTK_MODEL_BUTTON (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2014-10-26 22:08:26 +00:00
|
|
|
case PROP_ROLE:
|
|
|
|
g_value_set_enum (value, button->role);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ICON:
|
|
|
|
{
|
|
|
|
GIcon *icon;
|
|
|
|
gtk_image_get_gicon (GTK_IMAGE (button->image), &icon, NULL);
|
|
|
|
g_value_set_object (value, icon);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TEXT:
|
|
|
|
g_value_set_string (value, gtk_label_get_text (GTK_LABEL (button->label)));
|
|
|
|
break;
|
|
|
|
|
2014-10-24 01:43:28 +00:00
|
|
|
case PROP_ACTIVE:
|
|
|
|
g_value_set_boolean (value, button->active);
|
|
|
|
break;
|
|
|
|
|
2014-10-24 03:38:46 +00:00
|
|
|
case PROP_MENU_NAME:
|
|
|
|
g_value_set_string (value, button->menu_name);
|
|
|
|
break;
|
|
|
|
|
2014-10-26 22:08:26 +00:00
|
|
|
case PROP_INVERTED:
|
|
|
|
g_value_set_boolean (value, button->inverted);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_CENTERED:
|
|
|
|
g_value_set_boolean (value, button->centered);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ICONIC:
|
|
|
|
g_value_set_boolean (value, button->iconic);
|
|
|
|
break;
|
|
|
|
|
2014-10-24 01:43:28 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkModelButton *button = GTK_MODEL_BUTTON (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
2014-10-26 22:08:26 +00:00
|
|
|
case PROP_ROLE:
|
|
|
|
gtk_model_button_set_role (button, g_value_get_enum (value));
|
2014-02-07 19:51:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ICON:
|
|
|
|
gtk_model_button_set_icon (button, g_value_get_object (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_TEXT:
|
|
|
|
gtk_model_button_set_text (button, g_value_get_string (value));
|
|
|
|
break;
|
|
|
|
|
2014-10-24 01:43:28 +00:00
|
|
|
case PROP_ACTIVE:
|
|
|
|
gtk_model_button_set_active (button, g_value_get_boolean (value));
|
2014-02-07 19:51:34 +00:00
|
|
|
break;
|
|
|
|
|
2014-10-24 03:38:46 +00:00
|
|
|
case PROP_MENU_NAME:
|
|
|
|
gtk_model_button_set_menu_name (button, g_value_get_string (value));
|
|
|
|
break;
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
case PROP_INVERTED:
|
|
|
|
gtk_model_button_set_inverted (button, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_CENTERED:
|
|
|
|
gtk_model_button_set_centered (button, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
2014-04-26 17:50:55 +00:00
|
|
|
case PROP_ICONIC:
|
|
|
|
gtk_model_button_set_iconic (button, g_value_get_boolean (value));
|
|
|
|
break;
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-26 20:37:16 +00:00
|
|
|
static gboolean
|
|
|
|
has_sibling_with_indicator (GtkWidget *button)
|
|
|
|
{
|
|
|
|
GtkWidget *parent;
|
|
|
|
gboolean has_indicator;
|
|
|
|
GList *children, *l;
|
|
|
|
GtkModelButton *sibling;
|
|
|
|
|
|
|
|
has_indicator = FALSE;
|
|
|
|
|
|
|
|
parent = gtk_widget_get_parent (button);
|
|
|
|
children = gtk_container_get_children (GTK_CONTAINER (parent));
|
|
|
|
|
|
|
|
for (l = children; l; l = l->next)
|
|
|
|
{
|
|
|
|
sibling = l->data;
|
|
|
|
|
|
|
|
if (!GTK_IS_MODEL_BUTTON (sibling))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!gtk_widget_is_visible (GTK_WIDGET (sibling)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!sibling->centered &&
|
2014-10-26 22:08:26 +00:00
|
|
|
(sibling->menu_name || sibling->role != GTK_BUTTON_ROLE_NORMAL))
|
2014-04-26 20:37:16 +00:00
|
|
|
{
|
|
|
|
has_indicator = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free (children);
|
|
|
|
|
|
|
|
return has_indicator;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
needs_indicator (GtkModelButton *button)
|
|
|
|
{
|
2014-10-26 22:08:26 +00:00
|
|
|
if (button->role != GTK_BUTTON_ROLE_NORMAL)
|
2014-04-26 20:37:16 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return has_sibling_with_indicator (GTK_WIDGET (button));
|
|
|
|
}
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
static void
|
2016-10-22 14:06:14 +00:00
|
|
|
gtk_model_button_measure_ (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
GtkCssGadget *gadget;
|
2014-04-26 20:37:16 +00:00
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
if (GTK_MODEL_BUTTON (widget)->iconic)
|
|
|
|
gadget = GTK_BUTTON (widget)->priv->gadget;
|
2014-02-07 19:51:34 +00:00
|
|
|
else
|
2015-12-05 06:42:41 +00:00
|
|
|
gadget = GTK_MODEL_BUTTON (widget)->gadget;
|
2014-04-26 20:37:16 +00:00
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_css_gadget_get_preferred_size (gadget,
|
2016-10-22 14:06:14 +00:00
|
|
|
orientation,
|
|
|
|
for_size,
|
2015-12-05 06:42:41 +00:00
|
|
|
minimum, natural,
|
|
|
|
minimum_baseline, natural_baseline);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_model_button_measure (GtkCssGadget *gadget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
GtkModelButton *button;
|
|
|
|
GtkWidget *child;
|
|
|
|
|
|
|
|
widget = gtk_css_gadget_get_owner (gadget);
|
|
|
|
button = GTK_MODEL_BUTTON (widget);
|
2014-02-07 19:51:34 +00:00
|
|
|
child = gtk_bin_get_child (GTK_BIN (widget));
|
2015-12-05 06:42:41 +00:00
|
|
|
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
gint check_min, check_nat;
|
|
|
|
|
|
|
|
gtk_css_gadget_get_preferred_size (button->indicator_gadget,
|
|
|
|
GTK_ORIENTATION_HORIZONTAL,
|
|
|
|
-1,
|
|
|
|
&check_min, &check_nat,
|
|
|
|
NULL, NULL);
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
if (child && gtk_widget_get_visible (child))
|
2014-04-26 20:37:16 +00:00
|
|
|
{
|
2016-10-22 14:06:14 +00:00
|
|
|
gtk_widget_measure (child,
|
|
|
|
orientation,
|
|
|
|
for_size,
|
|
|
|
minimum, natural,
|
|
|
|
minimum_baseline, natural_baseline);
|
2015-12-05 06:42:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*minimum = 0;
|
|
|
|
*natural = 0;
|
2014-04-26 20:37:16 +00:00
|
|
|
}
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2014-04-26 20:37:16 +00:00
|
|
|
if (button->centered)
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
*minimum += 2 * check_min;
|
|
|
|
*natural += 2 * check_nat;
|
2014-04-26 20:37:16 +00:00
|
|
|
}
|
|
|
|
else if (needs_indicator (button))
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
*minimum += check_min;
|
|
|
|
*natural += check_nat;
|
2014-04-26 20:37:16 +00:00
|
|
|
}
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
gint check_min, check_nat;
|
|
|
|
|
|
|
|
gtk_css_gadget_get_preferred_size (button->indicator_gadget,
|
|
|
|
GTK_ORIENTATION_VERTICAL,
|
|
|
|
-1,
|
|
|
|
&check_min, &check_nat,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
if (child && gtk_widget_get_visible (child))
|
2014-04-26 20:37:16 +00:00
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
gint child_min, child_nat;
|
|
|
|
gint child_min_baseline = -1, child_nat_baseline = -1;
|
|
|
|
|
|
|
|
if (for_size > -1)
|
|
|
|
{
|
|
|
|
if (button->centered)
|
|
|
|
for_size -= 2 * check_nat;
|
|
|
|
else if (needs_indicator (button))
|
|
|
|
for_size -= check_nat;
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:05:09 +00:00
|
|
|
gtk_widget_measure (child, GTK_ORIENTATION_VERTICAL,
|
|
|
|
for_size,
|
|
|
|
&child_min, &child_nat,
|
|
|
|
&child_min_baseline, &child_nat_baseline);
|
2015-12-05 06:42:41 +00:00
|
|
|
|
|
|
|
if (button->centered)
|
|
|
|
{
|
|
|
|
*minimum = MAX (2 * check_min, child_min);
|
|
|
|
*natural = MAX (2 * check_nat, child_nat);
|
|
|
|
}
|
|
|
|
else if (needs_indicator (button))
|
|
|
|
{
|
|
|
|
*minimum = MAX (check_min, child_min);
|
|
|
|
*natural = MAX (check_nat, child_nat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*minimum = child_min;
|
|
|
|
*natural = child_nat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minimum_baseline && child_min_baseline >= 0)
|
|
|
|
*minimum_baseline = child_min_baseline + (*minimum - child_min) / 2;
|
|
|
|
if (natural_baseline && child_nat_baseline >= 0)
|
|
|
|
*natural_baseline = child_nat_baseline + (*natural - child_nat) / 2;
|
2014-04-26 20:37:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
if (button->centered)
|
|
|
|
{
|
|
|
|
*minimum = 2 * check_min;
|
|
|
|
*natural = 2 * check_nat;
|
|
|
|
}
|
|
|
|
else if (needs_indicator (button))
|
|
|
|
{
|
|
|
|
*minimum = check_min;
|
|
|
|
*natural = check_nat;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*minimum = 0;
|
|
|
|
*natural = 0;
|
|
|
|
}
|
2014-04-26 20:37:16 +00:00
|
|
|
}
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_model_button_size_allocate (GtkWidget *widget,
|
|
|
|
GtkAllocation *allocation)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
GtkCssGadget *gadget;
|
|
|
|
GdkRectangle clip;
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
if (GTK_MODEL_BUTTON (widget)->iconic)
|
|
|
|
gadget = GTK_BUTTON (widget)->priv->gadget;
|
|
|
|
else
|
|
|
|
gadget = GTK_MODEL_BUTTON (widget)->gadget;
|
|
|
|
|
|
|
|
gtk_widget_set_allocation (widget, allocation);
|
|
|
|
gtk_css_gadget_allocate (gadget,
|
|
|
|
allocation,
|
|
|
|
gtk_widget_get_allocated_baseline (widget),
|
|
|
|
&clip);
|
|
|
|
|
|
|
|
gtk_widget_set_clip (widget, &clip);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_model_button_allocate (GtkCssGadget *gadget,
|
|
|
|
const GtkAllocation *allocation,
|
|
|
|
int baseline,
|
|
|
|
GtkAllocation *out_clip,
|
|
|
|
gpointer unused)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
GtkWidget *widget;
|
|
|
|
GtkModelButton *button;
|
2014-02-07 19:51:34 +00:00
|
|
|
PangoContext *pango_context;
|
|
|
|
PangoFontMetrics *metrics;
|
|
|
|
GtkAllocation child_allocation;
|
|
|
|
GtkWidget *child;
|
2015-12-05 06:42:41 +00:00
|
|
|
gint check_min_width, check_nat_width;
|
|
|
|
gint check_min_height, check_nat_height;
|
|
|
|
GdkRectangle check_clip;
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
widget = gtk_css_gadget_get_owner (gadget);
|
|
|
|
button = GTK_MODEL_BUTTON (widget);
|
|
|
|
child = gtk_bin_get_child (GTK_BIN (widget));
|
2014-02-07 19:51:34 +00:00
|
|
|
|
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
|
|
|
|
gtk_css_gadget_get_preferred_size (button->indicator_gadget,
|
|
|
|
GTK_ORIENTATION_HORIZONTAL,
|
|
|
|
-1,
|
|
|
|
&check_min_width, &check_nat_width,
|
|
|
|
NULL, NULL);
|
|
|
|
gtk_css_gadget_get_preferred_size (button->indicator_gadget,
|
|
|
|
GTK_ORIENTATION_VERTICAL,
|
|
|
|
-1,
|
|
|
|
&check_min_height, &check_nat_height,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
if (indicator_is_left (widget))
|
2016-01-07 17:25:22 +00:00
|
|
|
child_allocation.x = allocation->x;
|
2015-12-05 06:42:41 +00:00
|
|
|
else
|
2016-01-07 17:25:22 +00:00
|
|
|
child_allocation.x = allocation->x + allocation->width - check_nat_width;
|
2015-12-05 06:42:41 +00:00
|
|
|
child_allocation.y = allocation->y + (allocation->height - check_nat_height) / 2;
|
|
|
|
child_allocation.width = check_nat_width;
|
|
|
|
child_allocation.height = check_nat_height;
|
|
|
|
|
|
|
|
gtk_css_gadget_allocate (button->indicator_gadget,
|
|
|
|
&child_allocation,
|
|
|
|
baseline,
|
|
|
|
&check_clip);
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
if (child && gtk_widget_get_visible (child))
|
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
GtkBorder border = { 0, };
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2014-04-26 20:37:16 +00:00
|
|
|
if (button->centered)
|
2014-02-07 19:51:34 +00:00
|
|
|
{
|
2015-12-05 06:42:41 +00:00
|
|
|
border.left = check_nat_width;
|
|
|
|
border.right = check_nat_width;
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
2014-04-26 20:37:16 +00:00
|
|
|
else if (needs_indicator (button))
|
|
|
|
{
|
|
|
|
if (indicator_is_left (widget))
|
2015-12-05 06:42:41 +00:00
|
|
|
border.left += check_nat_width;
|
2014-04-26 20:37:16 +00:00
|
|
|
else
|
2015-12-05 06:42:41 +00:00
|
|
|
border.right += check_nat_width;
|
2014-04-26 20:37:16 +00:00
|
|
|
}
|
2015-12-05 06:42:41 +00:00
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
child_allocation.x = allocation->x + border.left;
|
|
|
|
child_allocation.y = allocation->y + border.top;
|
|
|
|
child_allocation.width = allocation->width - border.left - border.right;
|
|
|
|
child_allocation.height = allocation->height - border.top - border.bottom;
|
|
|
|
|
|
|
|
baseline = gtk_widget_get_allocated_baseline (widget);
|
|
|
|
if (baseline != -1)
|
|
|
|
baseline -= border.top;
|
2015-12-05 06:42:41 +00:00
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
gtk_widget_size_allocate_with_baseline (child, &child_allocation, baseline);
|
|
|
|
}
|
|
|
|
|
|
|
|
pango_context = gtk_widget_get_pango_context (widget);
|
|
|
|
metrics = pango_context_get_metrics (pango_context,
|
|
|
|
pango_context_get_font_description (pango_context),
|
|
|
|
pango_context_get_language (pango_context));
|
|
|
|
GTK_BUTTON (button)->priv->baseline_align =
|
|
|
|
(double)pango_font_metrics_get_ascent (metrics) /
|
|
|
|
(pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics));
|
|
|
|
pango_font_metrics_unref (metrics);
|
2015-12-05 06:42:41 +00:00
|
|
|
|
2016-01-12 19:25:25 +00:00
|
|
|
|
|
|
|
if (gtk_widget_get_realized (widget))
|
|
|
|
{
|
|
|
|
GtkAllocation border_allocation;
|
|
|
|
gtk_css_gadget_get_border_allocation (gadget, &border_allocation, NULL);
|
|
|
|
|
2016-11-05 07:39:10 +00:00
|
|
|
gdk_window_move_resize (GTK_BUTTON (widget)->priv->event_window,
|
2016-01-12 19:25:25 +00:00
|
|
|
border_allocation.x,
|
|
|
|
border_allocation.y,
|
|
|
|
border_allocation.width,
|
|
|
|
border_allocation.height);
|
|
|
|
}
|
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
gtk_container_get_children_clip (GTK_CONTAINER (widget), out_clip);
|
|
|
|
gdk_rectangle_union (out_clip, &check_clip, out_clip);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:55:03 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_snapshot (GtkWidget *widget,
|
|
|
|
GtkSnapshot *snapshot)
|
|
|
|
{
|
|
|
|
GtkCssGadget *gadget;
|
|
|
|
|
|
|
|
if (GTK_MODEL_BUTTON (widget)->iconic)
|
|
|
|
gadget = GTK_BUTTON (widget)->priv->gadget;
|
|
|
|
else
|
|
|
|
gadget = GTK_MODEL_BUTTON (widget)->gadget;
|
|
|
|
|
|
|
|
gtk_css_gadget_snapshot (gadget, snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_model_button_render (GtkCssGadget *gadget,
|
|
|
|
GtkSnapshot *snapshot,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
GtkModelButton *button;
|
|
|
|
GtkWidget *child;
|
|
|
|
|
|
|
|
widget = gtk_css_gadget_get_owner (gadget);
|
|
|
|
button = GTK_MODEL_BUTTON (widget);
|
|
|
|
|
|
|
|
if (gtk_css_node_get_visible (gtk_css_gadget_get_node (button->indicator_gadget)))
|
|
|
|
gtk_css_gadget_snapshot (button->indicator_gadget, snapshot);
|
|
|
|
|
|
|
|
child = gtk_bin_get_child (GTK_BIN (widget));
|
|
|
|
if (child)
|
2017-01-07 16:02:20 +00:00
|
|
|
gtk_widget_snapshot_child (widget, child, snapshot);
|
2016-11-14 20:55:03 +00:00
|
|
|
|
|
|
|
return gtk_widget_has_visible_focus (widget);
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:56:59 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_destroy (GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkModelButton *model_button = GTK_MODEL_BUTTON (widget);
|
|
|
|
|
|
|
|
g_clear_pointer (&model_button->menu_name, g_free);
|
|
|
|
|
|
|
|
GTK_WIDGET_CLASS (gtk_model_button_parent_class)->destroy (widget);
|
|
|
|
}
|
|
|
|
|
2014-10-24 03:38:46 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_clicked (GtkButton *button)
|
|
|
|
{
|
|
|
|
GtkModelButton *model_button = GTK_MODEL_BUTTON (button);
|
2015-12-05 06:42:41 +00:00
|
|
|
|
2014-10-24 03:38:46 +00:00
|
|
|
if (model_button->menu_name != NULL)
|
|
|
|
{
|
|
|
|
GtkWidget *stack;
|
|
|
|
|
|
|
|
stack = gtk_widget_get_ancestor (GTK_WIDGET (button), GTK_TYPE_STACK);
|
|
|
|
if (stack != NULL)
|
|
|
|
gtk_stack_set_visible_child_name (GTK_STACK (stack), model_button->menu_name);
|
|
|
|
}
|
2014-10-26 22:08:26 +00:00
|
|
|
else if (model_button->role == GTK_BUTTON_ROLE_NORMAL)
|
2014-10-24 03:38:46 +00:00
|
|
|
{
|
|
|
|
GtkWidget *popover;
|
|
|
|
|
|
|
|
popover = gtk_widget_get_ancestor (GTK_WIDGET (button), GTK_TYPE_POPOVER);
|
|
|
|
if (popover != NULL)
|
2016-08-10 17:18:27 +00:00
|
|
|
gtk_popover_popdown (GTK_POPOVER (popover));
|
2014-10-24 03:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkModelButton *button = GTK_MODEL_BUTTON (object);
|
|
|
|
|
|
|
|
g_clear_object (&button->indicator_gadget);
|
|
|
|
g_clear_object (&button->gadget);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_model_button_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_class_init (GtkModelButtonClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
|
2014-10-24 03:38:46 +00:00
|
|
|
GtkButtonClass *button_class = GTK_BUTTON_CLASS (class);
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2015-12-05 06:42:41 +00:00
|
|
|
object_class->finalize = gtk_model_button_finalize;
|
2014-10-24 01:43:28 +00:00
|
|
|
object_class->get_property = gtk_model_button_get_property;
|
2014-02-07 19:51:34 +00:00
|
|
|
object_class->set_property = gtk_model_button_set_property;
|
|
|
|
|
2016-10-22 14:06:14 +00:00
|
|
|
widget_class->measure = gtk_model_button_measure_;
|
2014-02-07 19:51:34 +00:00
|
|
|
widget_class->size_allocate = gtk_model_button_size_allocate;
|
2016-11-14 20:55:03 +00:00
|
|
|
widget_class->snapshot = gtk_model_button_snapshot;
|
2015-06-25 12:56:59 +00:00
|
|
|
widget_class->destroy = gtk_model_button_destroy;
|
2015-10-31 04:08:00 +00:00
|
|
|
widget_class->state_flags_changed = gtk_model_button_state_flags_changed;
|
2015-11-03 17:20:53 +00:00
|
|
|
widget_class->direction_changed = gtk_model_button_direction_changed;
|
2014-02-07 19:51:34 +00:00
|
|
|
|
2014-10-24 03:38:46 +00:00
|
|
|
button_class->clicked = gtk_model_button_clicked;
|
|
|
|
|
2014-10-26 23:04:28 +00:00
|
|
|
/**
|
|
|
|
* GtkModelButton:role:
|
|
|
|
*
|
|
|
|
* Specifies whether the button is a plain, check or radio button.
|
|
|
|
* When #GtkActionable:action-name is set, the role will be determined
|
|
|
|
* from the action and does not have to be set explicitly.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_ROLE] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_enum ("role",
|
|
|
|
P_("Role"),
|
|
|
|
P_("The role of this button"),
|
2014-10-26 22:08:26 +00:00
|
|
|
GTK_TYPE_BUTTON_ROLE,
|
|
|
|
GTK_BUTTON_ROLE_NORMAL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2014-10-26 23:04:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkModelButton:icon:
|
|
|
|
*
|
|
|
|
* A #GIcon that will be used if iconic appearance for the button is
|
|
|
|
* desired.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_ICON] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_object ("icon",
|
|
|
|
P_("Icon"),
|
|
|
|
P_("The icon"),
|
2014-10-26 22:08:26 +00:00
|
|
|
G_TYPE_ICON,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2014-10-26 23:04:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkModelButton:text:
|
|
|
|
*
|
|
|
|
* The label for the button.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_TEXT] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_string ("text",
|
|
|
|
P_("Text"),
|
|
|
|
P_("The text"),
|
2014-11-23 20:05:01 +00:00
|
|
|
"",
|
2014-10-26 22:08:26 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2014-10-26 23:04:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkModelButton:active:
|
|
|
|
*
|
|
|
|
* The state of the button. This is reflecting the state of the associated
|
|
|
|
* #GAction.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_ACTIVE] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_boolean ("active",
|
|
|
|
P_("Active"),
|
|
|
|
P_("Active"),
|
2014-10-26 22:08:26 +00:00
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2014-10-26 23:04:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkModelButton:menu-name:
|
|
|
|
*
|
|
|
|
* The name of a submenu to open when the button is activated.
|
|
|
|
* If this is set, the button should not have an action associated with it.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_MENU_NAME] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_string ("menu-name",
|
|
|
|
P_("Menu name"),
|
|
|
|
P_("The name of the menu to open"),
|
2014-10-26 22:08:26 +00:00
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2014-10-26 23:04:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkModelButton:inverted:
|
|
|
|
*
|
|
|
|
* Whether to show the submenu indicator at the opposite side than normal.
|
|
|
|
* This property should be set for model buttons that 'go back' to a parent
|
|
|
|
* menu.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_INVERTED] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_boolean ("inverted",
|
|
|
|
P_("Inverted"),
|
|
|
|
P_("Whether the menu is a parent"),
|
2014-10-26 22:08:26 +00:00
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2014-10-26 23:04:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkModelButton:centered:
|
|
|
|
*
|
|
|
|
* Wether to render the button contents centered instead of left-aligned.
|
|
|
|
* This property should be set for title-like items.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_CENTERED] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_boolean ("centered",
|
|
|
|
P_("Centered"),
|
|
|
|
P_("Whether to center the contents"),
|
2014-10-26 22:08:26 +00:00
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
2014-10-26 23:04:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkModelButton:iconic:
|
|
|
|
*
|
|
|
|
* If this property is set, the button will show an icon if one is set.
|
|
|
|
* If no icon is set, the text will be used. This is typically used for
|
|
|
|
* horizontal sections of linked buttons.
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-10-26 22:08:26 +00:00
|
|
|
properties[PROP_ICONIC] =
|
2014-10-26 23:04:28 +00:00
|
|
|
g_param_spec_boolean ("iconic",
|
|
|
|
P_("Iconic"),
|
|
|
|
P_("Whether to prefer the icon over text"),
|
2014-11-23 20:05:01 +00:00
|
|
|
FALSE,
|
2014-10-26 22:08:26 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
|
|
|
|
g_object_class_install_properties (object_class, LAST_PROPERTY, properties);
|
2014-02-07 19:51:34 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_accessible_role (GTK_WIDGET_CLASS (class), ATK_ROLE_PUSH_BUTTON);
|
2015-10-30 04:29:47 +00:00
|
|
|
gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (class), "modelbutton");
|
|
|
|
}
|
|
|
|
|
2014-02-07 19:51:34 +00:00
|
|
|
static void
|
|
|
|
gtk_model_button_init (GtkModelButton *button)
|
|
|
|
{
|
2015-10-30 04:29:47 +00:00
|
|
|
GtkCssNode *widget_node;
|
|
|
|
|
2015-12-20 01:16:51 +00:00
|
|
|
button->role = GTK_BUTTON_ROLE_NORMAL;
|
2014-02-07 19:51:34 +00:00
|
|
|
gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
|
|
|
|
button->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
|
|
|
|
gtk_widget_set_halign (button->box, GTK_ALIGN_FILL);
|
|
|
|
button->image = gtk_image_new ();
|
2017-01-19 09:31:03 +00:00
|
|
|
gtk_widget_hide (button->image);
|
2014-02-07 19:51:34 +00:00
|
|
|
button->label = gtk_label_new ("");
|
2017-01-19 09:31:03 +00:00
|
|
|
gtk_widget_hide (button->label);
|
2014-02-07 19:51:34 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (button->box), button->image);
|
|
|
|
gtk_container_add (GTK_CONTAINER (button->box), button->label);
|
|
|
|
gtk_container_add (GTK_CONTAINER (button), button->box);
|
2015-10-30 04:29:47 +00:00
|
|
|
|
|
|
|
widget_node = gtk_widget_get_css_node (GTK_WIDGET (button));
|
2015-12-05 06:42:41 +00:00
|
|
|
button->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
|
|
|
|
GTK_WIDGET (button),
|
|
|
|
gtk_model_button_measure,
|
|
|
|
gtk_model_button_allocate,
|
2016-11-14 20:55:03 +00:00
|
|
|
gtk_model_button_render,
|
2016-11-13 20:08:34 +00:00
|
|
|
NULL,
|
2015-12-05 06:42:41 +00:00
|
|
|
NULL);
|
2015-12-19 19:53:57 +00:00
|
|
|
button->indicator_gadget = gtk_builtin_icon_new ("check",
|
|
|
|
GTK_WIDGET (button),
|
|
|
|
button->gadget,
|
|
|
|
NULL);
|
|
|
|
gtk_builtin_icon_set_default_size (GTK_BUILTIN_ICON (button->indicator_gadget), 16);
|
|
|
|
update_node_ordering (button);
|
2015-12-20 01:16:51 +00:00
|
|
|
gtk_css_node_set_visible (gtk_css_gadget_get_node (button->indicator_gadget), FALSE);
|
2014-02-07 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 00:16:49 +00:00
|
|
|
/**
|
|
|
|
* gtk_model_button_new:
|
|
|
|
*
|
|
|
|
* Creates a new GtkModelButton.
|
|
|
|
*
|
|
|
|
* Returns: the newly created #GtkModelButton widget
|
|
|
|
*
|
|
|
|
* Since: 3.16
|
|
|
|
*/
|
2014-02-07 19:51:34 +00:00
|
|
|
GtkWidget *
|
|
|
|
gtk_model_button_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_MODEL_BUTTON, NULL);
|
|
|
|
}
|