622 lines
18 KiB
C
622 lines
18 KiB
C
/* LIBPIKA - The PIKA Library
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
*
|
|
* pikachainbutton.c
|
|
* Copyright (C) 1999-2000 Sven Neumann <sven@gimp.org>
|
|
*
|
|
* 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 3 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
|
|
* Library 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
|
|
* <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include "libpikabase/pikabase.h"
|
|
|
|
#include "pikawidgetstypes.h"
|
|
|
|
#include "pikachainbutton.h"
|
|
#include "pikaicons.h"
|
|
|
|
|
|
/**
|
|
* SECTION: pikachainbutton
|
|
* @title: PikaChainButton
|
|
* @short_description: Widget to visually connect two entry widgets.
|
|
* @see_also: You may want to use the convenience function
|
|
* pika_coordinates_new() to set up two PikaSizeEntries
|
|
* (see #PikaSizeEntry) linked with a #PikaChainButton.
|
|
*
|
|
* This widget provides a button showing either a linked or a broken
|
|
* chain that can be used to link two entries, spinbuttons, colors or
|
|
* other GUI elements and show that they may be locked. Use it for
|
|
* example to connect X and Y ratios to provide the possibility of a
|
|
* constrained aspect ratio.
|
|
*
|
|
* The #PikaChainButton only gives visual feedback, it does not really
|
|
* connect widgets. You have to take care of locking the values
|
|
* yourself by checking the state of the #PikaChainButton whenever a
|
|
* value changes in one of the connected widgets and adjusting the
|
|
* other value if necessary.
|
|
**/
|
|
|
|
|
|
enum
|
|
{
|
|
PROP_0,
|
|
PROP_POSITION,
|
|
PROP_ICON_SIZE,
|
|
PROP_ACTIVE
|
|
};
|
|
|
|
enum
|
|
{
|
|
TOGGLED,
|
|
LAST_SIGNAL
|
|
};
|
|
|
|
|
|
struct _PikaChainButtonPrivate
|
|
{
|
|
PikaChainPosition position;
|
|
gboolean active;
|
|
|
|
GtkWidget *button;
|
|
GtkWidget *line1;
|
|
GtkWidget *line2;
|
|
GtkWidget *image;
|
|
};
|
|
|
|
#define GET_PRIVATE(obj) (((PikaChainButton *) (obj))->priv)
|
|
|
|
|
|
static void pika_chain_button_constructed (GObject *object);
|
|
static void pika_chain_button_set_property (GObject *object,
|
|
guint property_id,
|
|
const GValue *value,
|
|
GParamSpec *pspec);
|
|
static void pika_chain_button_get_property (GObject *object,
|
|
guint property_id,
|
|
GValue *value,
|
|
GParamSpec *pspec);
|
|
|
|
static void pika_chain_button_compute_expand (GtkWidget *widget,
|
|
gboolean *hexpand_p,
|
|
gboolean *vexpand_p);
|
|
|
|
static void pika_chain_button_clicked_callback (GtkWidget *widget,
|
|
PikaChainButton *button);
|
|
static void pika_chain_button_update_image (PikaChainButton *button);
|
|
|
|
static GtkWidget * pika_chain_line_new (PikaChainPosition position,
|
|
gint which);
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (PikaChainButton, pika_chain_button, GTK_TYPE_GRID)
|
|
|
|
#define parent_class pika_chain_button_parent_class
|
|
|
|
static guint pika_chain_button_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
static const gchar * const pika_chain_icon_names[] =
|
|
{
|
|
PIKA_ICON_CHAIN_HORIZONTAL,
|
|
PIKA_ICON_CHAIN_HORIZONTAL_BROKEN,
|
|
PIKA_ICON_CHAIN_VERTICAL,
|
|
PIKA_ICON_CHAIN_VERTICAL_BROKEN
|
|
};
|
|
|
|
|
|
static void
|
|
pika_chain_button_class_init (PikaChainButtonClass *klass)
|
|
{
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
object_class->constructed = pika_chain_button_constructed;
|
|
object_class->set_property = pika_chain_button_set_property;
|
|
object_class->get_property = pika_chain_button_get_property;
|
|
|
|
widget_class->compute_expand = pika_chain_button_compute_expand;
|
|
|
|
pika_chain_button_signals[TOGGLED] =
|
|
g_signal_new ("toggled",
|
|
G_TYPE_FROM_CLASS (klass),
|
|
G_SIGNAL_RUN_FIRST,
|
|
G_STRUCT_OFFSET (PikaChainButtonClass, toggled),
|
|
NULL, NULL, NULL,
|
|
G_TYPE_NONE, 0);
|
|
|
|
klass->toggled = NULL;
|
|
|
|
/**
|
|
* PikaChainButton:position:
|
|
*
|
|
* The position in which the chain button will be used.
|
|
*
|
|
* Since: 2.4
|
|
*/
|
|
g_object_class_install_property (object_class, PROP_POSITION,
|
|
g_param_spec_enum ("position",
|
|
"Position",
|
|
"The chain's position",
|
|
PIKA_TYPE_CHAIN_POSITION,
|
|
PIKA_CHAIN_TOP,
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
PIKA_PARAM_READWRITE));
|
|
|
|
/**
|
|
* PikaChainButton:icon-size:
|
|
*
|
|
* The chain button icon size.
|
|
*
|
|
* Since: 2.10.10
|
|
*/
|
|
g_object_class_install_property (object_class, PROP_ICON_SIZE,
|
|
g_param_spec_enum ("icon-size",
|
|
"Icon Size",
|
|
"The chain's icon size",
|
|
GTK_TYPE_ICON_SIZE,
|
|
GTK_ICON_SIZE_BUTTON,
|
|
G_PARAM_CONSTRUCT |
|
|
PIKA_PARAM_READWRITE));
|
|
|
|
/**
|
|
* PikaChainButton:active:
|
|
*
|
|
* The toggled state of the chain button.
|
|
*
|
|
* Since: 2.10.10
|
|
*/
|
|
g_object_class_install_property (object_class, PROP_ACTIVE,
|
|
g_param_spec_boolean ("active",
|
|
"Active",
|
|
"The chain's toggled state",
|
|
FALSE,
|
|
G_PARAM_CONSTRUCT |
|
|
PIKA_PARAM_READWRITE));
|
|
}
|
|
|
|
static void
|
|
pika_chain_button_init (PikaChainButton *button)
|
|
{
|
|
PikaChainButtonPrivate *private;
|
|
|
|
button->priv = pika_chain_button_get_instance_private (button);
|
|
|
|
private = GET_PRIVATE (button);
|
|
private->position = PIKA_CHAIN_TOP;
|
|
private->active = FALSE;
|
|
private->image = gtk_image_new ();
|
|
private->button = gtk_button_new ();
|
|
|
|
gtk_button_set_relief (GTK_BUTTON (private->button), GTK_RELIEF_NONE);
|
|
gtk_container_add (GTK_CONTAINER (private->button), private->image);
|
|
gtk_widget_show (private->image);
|
|
|
|
g_signal_connect (private->button, "clicked",
|
|
G_CALLBACK (pika_chain_button_clicked_callback),
|
|
button);
|
|
}
|
|
|
|
static void
|
|
pika_chain_button_constructed (GObject *object)
|
|
{
|
|
PikaChainButton *button = PIKA_CHAIN_BUTTON (object);
|
|
PikaChainButtonPrivate *private = GET_PRIVATE (button);
|
|
|
|
G_OBJECT_CLASS (parent_class)->constructed (object);
|
|
|
|
private->line1 = pika_chain_line_new (private->position, 1);
|
|
private->line2 = pika_chain_line_new (private->position, -1);
|
|
|
|
pika_chain_button_update_image (button);
|
|
|
|
if (private->position & PIKA_CHAIN_LEFT) /* are we a vertical chainbutton? */
|
|
{
|
|
gtk_widget_set_vexpand (private->line1, TRUE);
|
|
gtk_widget_set_vexpand (private->line2, TRUE);
|
|
|
|
gtk_grid_attach (GTK_GRID (button), private->line1, 0, 0, 1, 1);
|
|
gtk_grid_attach (GTK_GRID (button), private->button, 0, 1, 1, 1);
|
|
gtk_grid_attach (GTK_GRID (button), private->line2, 0, 2, 1, 1);
|
|
}
|
|
else
|
|
{
|
|
gtk_widget_set_hexpand (private->line1, TRUE);
|
|
gtk_widget_set_hexpand (private->line2, TRUE);
|
|
|
|
gtk_grid_attach (GTK_GRID (button), private->line1, 0, 0, 1, 1);
|
|
gtk_grid_attach (GTK_GRID (button), private->button, 1, 0, 1, 1);
|
|
gtk_grid_attach (GTK_GRID (button), private->line2, 2, 0, 1, 1);
|
|
}
|
|
|
|
gtk_widget_show (private->button);
|
|
gtk_widget_show (private->line1);
|
|
gtk_widget_show (private->line2);
|
|
}
|
|
|
|
static void
|
|
pika_chain_button_set_property (GObject *object,
|
|
guint property_id,
|
|
const GValue *value,
|
|
GParamSpec *pspec)
|
|
{
|
|
PikaChainButton *button = PIKA_CHAIN_BUTTON (object);
|
|
PikaChainButtonPrivate *private = GET_PRIVATE (button);
|
|
|
|
switch (property_id)
|
|
{
|
|
case PROP_POSITION:
|
|
private->position = g_value_get_enum (value);
|
|
break;
|
|
|
|
case PROP_ICON_SIZE:
|
|
g_object_set_property (G_OBJECT (private->image), "icon-size", value);
|
|
break;
|
|
|
|
case PROP_ACTIVE:
|
|
pika_chain_button_set_active (button, g_value_get_boolean (value));
|
|
break;
|
|
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
pika_chain_button_get_property (GObject *object,
|
|
guint property_id,
|
|
GValue *value,
|
|
GParamSpec *pspec)
|
|
{
|
|
PikaChainButton *button = PIKA_CHAIN_BUTTON (object);
|
|
PikaChainButtonPrivate *private = GET_PRIVATE (button);
|
|
|
|
switch (property_id)
|
|
{
|
|
case PROP_POSITION:
|
|
g_value_set_enum (value, private->position);
|
|
break;
|
|
|
|
case PROP_ICON_SIZE:
|
|
g_object_get_property (G_OBJECT (private->image), "icon-size", value);
|
|
break;
|
|
|
|
case PROP_ACTIVE:
|
|
g_value_set_boolean (value, pika_chain_button_get_active (button));
|
|
break;
|
|
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void
|
|
pika_chain_button_compute_expand (GtkWidget *widget,
|
|
gboolean *hexpand_p,
|
|
gboolean *vexpand_p)
|
|
{
|
|
/* don't inherit [hv]expand from the chain lines. see issue #3876. */
|
|
*hexpand_p = FALSE;
|
|
*vexpand_p = FALSE;
|
|
}
|
|
|
|
/**
|
|
* pika_chain_button_new:
|
|
* @position: The position you are going to use for the button
|
|
* with respect to the widgets you want to chain.
|
|
*
|
|
* Creates a new #PikaChainButton widget.
|
|
*
|
|
* This returns a button showing either a broken or a linked chain and
|
|
* small clamps attached to both sides that visually group the two
|
|
* widgets you want to connect. This widget looks best when attached
|
|
* to a grid taking up two columns (or rows respectively) next to the
|
|
* widgets that it is supposed to connect. It may work for more than
|
|
* two widgets, but the look is optimized for two.
|
|
*
|
|
* Returns: Pointer to the new #PikaChainButton, which is inactive
|
|
* by default. Use pika_chain_button_set_active() to
|
|
* change its state.
|
|
*/
|
|
GtkWidget *
|
|
pika_chain_button_new (PikaChainPosition position)
|
|
{
|
|
return g_object_new (PIKA_TYPE_CHAIN_BUTTON,
|
|
"position", position,
|
|
NULL);
|
|
}
|
|
|
|
/**
|
|
* pika_chain_button_set_icon_size:
|
|
* @button: Pointer to a #PikaChainButton.
|
|
* @size: The new icon size.
|
|
*
|
|
* Sets the icon size of the #PikaChainButton.
|
|
*
|
|
* Since: 2.10.10
|
|
*/
|
|
void
|
|
pika_chain_button_set_icon_size (PikaChainButton *button,
|
|
GtkIconSize size)
|
|
{
|
|
g_return_if_fail (PIKA_IS_CHAIN_BUTTON (button));
|
|
|
|
g_object_set (button,
|
|
"icon-size", size,
|
|
NULL);
|
|
}
|
|
|
|
/**
|
|
* pika_chain_button_get_icon_size:
|
|
* @button: Pointer to a #PikaChainButton.
|
|
*
|
|
* Gets the icon size of the #PikaChainButton.
|
|
*
|
|
* Returns: The icon size.
|
|
*
|
|
* Since: 2.10.10
|
|
*/
|
|
GtkIconSize
|
|
pika_chain_button_get_icon_size (PikaChainButton *button)
|
|
{
|
|
GtkIconSize size;
|
|
|
|
g_return_val_if_fail (PIKA_IS_CHAIN_BUTTON (button), GTK_ICON_SIZE_BUTTON);
|
|
|
|
g_object_get (button,
|
|
"icon-size", &size,
|
|
NULL);
|
|
|
|
return size;
|
|
}
|
|
|
|
/**
|
|
* pika_chain_button_set_active:
|
|
* @button: Pointer to a #PikaChainButton.
|
|
* @active: The new state.
|
|
*
|
|
* Sets the state of the #PikaChainButton to be either locked (%TRUE) or
|
|
* unlocked (%FALSE) and changes the showed pixmap to reflect the new state.
|
|
*/
|
|
void
|
|
pika_chain_button_set_active (PikaChainButton *button,
|
|
gboolean active)
|
|
{
|
|
PikaChainButtonPrivate *private;
|
|
|
|
g_return_if_fail (PIKA_IS_CHAIN_BUTTON (button));
|
|
|
|
private = GET_PRIVATE (button);
|
|
|
|
if (private->active != active)
|
|
{
|
|
private->active = active ? TRUE : FALSE;
|
|
|
|
pika_chain_button_update_image (button);
|
|
|
|
g_signal_emit (button, pika_chain_button_signals[TOGGLED], 0);
|
|
|
|
g_object_notify (G_OBJECT (button), "active");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* pika_chain_button_get_active
|
|
* @button: Pointer to a #PikaChainButton.
|
|
*
|
|
* Checks the state of the #PikaChainButton.
|
|
*
|
|
* Returns: %TRUE if the #PikaChainButton is active (locked).
|
|
*/
|
|
gboolean
|
|
pika_chain_button_get_active (PikaChainButton *button)
|
|
{
|
|
PikaChainButtonPrivate *private;
|
|
|
|
g_return_val_if_fail (PIKA_IS_CHAIN_BUTTON (button), FALSE);
|
|
|
|
private = GET_PRIVATE (button);
|
|
|
|
return private->active;
|
|
}
|
|
|
|
/**
|
|
* pika_chain_button_get_button
|
|
* @button: A #PikaChainButton.
|
|
*
|
|
* Returns: (transfer none) (type GtkButton): The #PikaChainButton's button.
|
|
*
|
|
* Since: 3.0
|
|
*/
|
|
GtkWidget *
|
|
pika_chain_button_get_button (PikaChainButton *button)
|
|
{
|
|
PikaChainButtonPrivate *private;
|
|
|
|
g_return_val_if_fail (PIKA_IS_CHAIN_BUTTON (button), FALSE);
|
|
|
|
private = GET_PRIVATE (button);
|
|
|
|
return private->button;
|
|
}
|
|
|
|
|
|
/* private functions */
|
|
|
|
static void
|
|
pika_chain_button_clicked_callback (GtkWidget *widget,
|
|
PikaChainButton *button)
|
|
{
|
|
PikaChainButtonPrivate *private = GET_PRIVATE (button);
|
|
|
|
pika_chain_button_set_active (button, ! private->active);
|
|
}
|
|
|
|
static void
|
|
pika_chain_button_update_image (PikaChainButton *button)
|
|
{
|
|
PikaChainButtonPrivate *private = GET_PRIVATE (button);
|
|
guint i;
|
|
|
|
i = ((private->position & PIKA_CHAIN_LEFT) << 1) + (private->active ? 0 : 1);
|
|
|
|
gtk_image_set_from_icon_name (GTK_IMAGE (private->image),
|
|
pika_chain_icon_names[i],
|
|
pika_chain_button_get_icon_size (button));
|
|
}
|
|
|
|
|
|
/* PikaChainLine is a simple no-window widget for drawing the lines.
|
|
*
|
|
* Originally this used to be a GtkDrawingArea but this turned out to
|
|
* be a bad idea. We don't need an extra window to draw on and we also
|
|
* don't need any input events.
|
|
*/
|
|
|
|
static GType pika_chain_line_get_type (void) G_GNUC_CONST;
|
|
static gboolean pika_chain_line_draw (GtkWidget *widget,
|
|
cairo_t *cr);
|
|
|
|
struct _PikaChainLine
|
|
{
|
|
GtkWidget parent_instance;
|
|
PikaChainPosition position;
|
|
gint which;
|
|
};
|
|
|
|
typedef struct _PikaChainLine PikaChainLine;
|
|
typedef GtkWidgetClass PikaChainLineClass;
|
|
|
|
G_DEFINE_TYPE (PikaChainLine, pika_chain_line, GTK_TYPE_WIDGET)
|
|
|
|
static void
|
|
pika_chain_line_class_init (PikaChainLineClass *klass)
|
|
{
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
widget_class->draw = pika_chain_line_draw;
|
|
}
|
|
|
|
static void
|
|
pika_chain_line_init (PikaChainLine *line)
|
|
{
|
|
gtk_widget_set_has_window (GTK_WIDGET (line), FALSE);
|
|
}
|
|
|
|
static GtkWidget *
|
|
pika_chain_line_new (PikaChainPosition position,
|
|
gint which)
|
|
{
|
|
PikaChainLine *line = g_object_new (pika_chain_line_get_type (), NULL);
|
|
|
|
line->position = position;
|
|
line->which = which;
|
|
|
|
return GTK_WIDGET (line);
|
|
}
|
|
|
|
static gboolean
|
|
pika_chain_line_draw (GtkWidget *widget,
|
|
cairo_t *cr)
|
|
{
|
|
GtkStyleContext *context = gtk_widget_get_style_context (widget);
|
|
PikaChainLine *line = ((PikaChainLine *) widget);
|
|
GtkAllocation allocation;
|
|
GdkPoint points[3];
|
|
PikaChainPosition position;
|
|
GdkRGBA color;
|
|
|
|
gtk_widget_get_allocation (widget, &allocation);
|
|
|
|
#define SHORT_LINE 4
|
|
points[0].x = allocation.width / 2;
|
|
points[0].y = allocation.height / 2;
|
|
|
|
position = line->position;
|
|
|
|
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
|
|
{
|
|
switch (position)
|
|
{
|
|
case PIKA_CHAIN_TOP:
|
|
case PIKA_CHAIN_BOTTOM:
|
|
break;
|
|
|
|
case PIKA_CHAIN_LEFT:
|
|
position = PIKA_CHAIN_RIGHT;
|
|
break;
|
|
|
|
case PIKA_CHAIN_RIGHT:
|
|
position = PIKA_CHAIN_LEFT;
|
|
break;
|
|
}
|
|
}
|
|
|
|
switch (position)
|
|
{
|
|
case PIKA_CHAIN_LEFT:
|
|
points[0].x += SHORT_LINE;
|
|
points[1].x = points[0].x - SHORT_LINE;
|
|
points[1].y = points[0].y;
|
|
points[2].x = points[1].x;
|
|
points[2].y = (line->which == 1 ? allocation.height - 1 : 0);
|
|
break;
|
|
|
|
case PIKA_CHAIN_RIGHT:
|
|
points[0].x -= SHORT_LINE;
|
|
points[1].x = points[0].x + SHORT_LINE;
|
|
points[1].y = points[0].y;
|
|
points[2].x = points[1].x;
|
|
points[2].y = (line->which == 1 ? allocation.height - 1 : 0);
|
|
break;
|
|
|
|
case PIKA_CHAIN_TOP:
|
|
points[0].y += SHORT_LINE;
|
|
points[1].x = points[0].x;
|
|
points[1].y = points[0].y - SHORT_LINE;
|
|
points[2].x = (line->which == 1 ? allocation.width - 1 : 0);
|
|
points[2].y = points[1].y;
|
|
break;
|
|
|
|
case PIKA_CHAIN_BOTTOM:
|
|
points[0].y -= SHORT_LINE;
|
|
points[1].x = points[0].x;
|
|
points[1].y = points[0].y + SHORT_LINE;
|
|
points[2].x = (line->which == 1 ? allocation.width - 1 : 0);
|
|
points[2].y = points[1].y;
|
|
break;
|
|
|
|
default:
|
|
return FALSE;
|
|
}
|
|
|
|
cairo_move_to (cr, points[0].x, points[0].y);
|
|
cairo_line_to (cr, points[1].x, points[1].y);
|
|
cairo_line_to (cr, points[2].x, points[2].y);
|
|
|
|
cairo_set_line_width (cr, 2.0);
|
|
cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
|
|
gtk_style_context_get_color (context, gtk_widget_get_state_flags (widget), &color);
|
|
gdk_cairo_set_source_rgba (cr, &color);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
return TRUE;
|
|
}
|