xed/pluma/pluma-status-combo-box.c

432 lines
11 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
2011-11-07 16:52:18 -06:00
* pluma-status-combo-box.c
* This file is part of pluma
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2008 - Jesse van den Kieboom
*
* 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
2012-11-18 19:54:49 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
2011-11-07 16:52:18 -06:00
#include "pluma-status-combo-box.h"
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
#define COMBO_BOX_TEXT_DATA "PlumaStatusComboBoxTextData"
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
#define PLUMA_STATUS_COMBO_BOX_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), PLUMA_TYPE_STATUS_COMBO_BOX, PlumaStatusComboBoxPrivate))
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
struct _PlumaStatusComboBoxPrivate
2011-11-07 13:46:58 -06:00
{
GtkWidget *frame;
GtkWidget *button;
GtkWidget *hbox;
GtkWidget *label;
GtkWidget *item;
GtkWidget *arrow;
GtkWidget *menu;
GtkWidget *current_item;
};
/* Signals */
enum
{
CHANGED,
NUM_SIGNALS
};
/* Properties */
enum
{
PROP_0,
PROP_LABEL
};
static guint signals[NUM_SIGNALS] = { 0 };
2011-11-07 16:52:18 -06:00
G_DEFINE_TYPE(PlumaStatusComboBox, pluma_status_combo_box, GTK_TYPE_EVENT_BOX)
2011-11-07 13:46:58 -06:00
static void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_finalize (GObject *object)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
G_OBJECT_CLASS (pluma_status_combo_box_parent_class)->finalize (object);
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_get_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
2011-11-07 16:52:18 -06:00
PlumaStatusComboBox *obj = PLUMA_STATUS_COMBO_BOX (object);
2011-11-07 13:46:58 -06:00
switch (prop_id)
{
case PROP_LABEL:
2011-11-07 16:52:18 -06:00
g_value_set_string (value, pluma_status_combo_box_get_label (obj));
2011-11-07 13:46:58 -06:00
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_set_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
2011-11-07 16:52:18 -06:00
PlumaStatusComboBox *obj = PLUMA_STATUS_COMBO_BOX (object);
2011-11-07 13:46:58 -06:00
switch (prop_id)
{
case PROP_LABEL:
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_set_label (obj, g_value_get_string (value));
2011-11-07 13:46:58 -06:00
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_changed (PlumaStatusComboBox *combo,
2011-11-07 13:46:58 -06:00
GtkMenuItem *item)
{
const gchar *text;
text = g_object_get_data (G_OBJECT (item), COMBO_BOX_TEXT_DATA);
if (text != NULL)
{
gtk_label_set_markup (GTK_LABEL (combo->priv->item), text);
combo->priv->current_item = GTK_WIDGET (item);
}
}
static void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_class_init (PlumaStatusComboBoxClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2011-11-07 16:52:18 -06:00
object_class->finalize = pluma_status_combo_box_finalize;
object_class->get_property = pluma_status_combo_box_get_property;
object_class->set_property = pluma_status_combo_box_set_property;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
klass->changed = pluma_status_combo_box_changed;
2011-11-07 13:46:58 -06:00
signals[CHANGED] =
g_signal_new ("changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
2011-11-07 16:52:18 -06:00
G_STRUCT_OFFSET (PlumaStatusComboBoxClass,
2011-11-07 13:46:58 -06:00
changed), NULL, NULL,
g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
GTK_TYPE_MENU_ITEM);
g_object_class_install_property (object_class, PROP_LABEL,
g_param_spec_string ("label",
"LABEL",
"The label",
NULL,
G_PARAM_READWRITE));
/* Set up a style for the button to decrease spacing. */
gtk_rc_parse_string (
2011-11-07 16:52:18 -06:00
"style \"pluma-status-combo-button-style\"\n"
2011-11-07 13:46:58 -06:00
"{\n"
" GtkWidget::focus-padding = 0\n"
" GtkWidget::focus-line-width = 0\n"
" xthickness = 0\n"
" ythickness = 0\n"
"}\n"
2011-11-07 16:52:18 -06:00
"widget \"*.pluma-status-combo-button\" style \"pluma-status-combo-button-style\"");
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
g_type_class_add_private (object_class, sizeof(PlumaStatusComboBoxPrivate));
2011-11-07 13:46:58 -06:00
}
static void
menu_deactivate (GtkMenu *menu,
2011-11-07 16:52:18 -06:00
PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo->priv->button), FALSE);
}
static void
menu_position_func (GtkMenu *menu,
gint *x,
gint *y,
gboolean *push_in,
2011-11-07 16:52:18 -06:00
PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
GtkRequisition request;
2013-10-29 18:56:56 -05:00
GtkAllocation allocation;
2011-11-07 13:46:58 -06:00
*push_in = FALSE;
gtk_widget_size_request (gtk_widget_get_toplevel (GTK_WIDGET (menu)), &request);
/* get the origin... */
2013-10-29 18:56:56 -05:00
#if GTK_CHECK_VERSION (3, 0, 0)
gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (combo)), x, y);
gtk_widget_get_allocation (GTK_WIDGET (combo), &allocation);
#else
2011-11-07 13:46:58 -06:00
gdk_window_get_origin (GTK_WIDGET (combo)->window, x, y);
2013-10-29 18:56:56 -05:00
allocation = GTK_WIDGET (combo)->allocation;
#endif
2011-11-07 13:46:58 -06:00
/* make the menu as wide as the widget */
2013-10-29 18:56:56 -05:00
if (request.width < allocation.width)
2011-11-07 13:46:58 -06:00
{
2013-10-29 18:56:56 -05:00
gtk_widget_set_size_request (GTK_WIDGET (menu), allocation.width, -1);
2011-11-07 13:46:58 -06:00
}
/* position it above the widget */
*y -= request.height;
}
static void
button_press_event (GtkWidget *widget,
GdkEventButton *event,
2011-11-07 16:52:18 -06:00
PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
GtkRequisition request;
2013-10-29 18:56:56 -05:00
GtkAllocation allocation;
2011-11-07 13:46:58 -06:00
gint max_height;
gtk_widget_size_request (combo->priv->menu, &request);
2013-10-29 18:56:56 -05:00
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_widget_get_allocation (GTK_WIDGET (combo), &allocation);
#else
allocation = GTK_WIDGET (combo)->allocation;
#endif
2011-11-07 13:46:58 -06:00
/* do something relative to our own height here, maybe we can do better */
2013-10-29 18:56:56 -05:00
max_height = allocation.height * 20;
2011-11-07 13:46:58 -06:00
if (request.height > max_height)
{
gtk_widget_set_size_request (combo->priv->menu, -1, max_height);
gtk_widget_set_size_request (gtk_widget_get_toplevel (combo->priv->menu), -1, max_height);
}
gtk_menu_popup (GTK_MENU (combo->priv->menu),
NULL,
NULL,
(GtkMenuPositionFunc)menu_position_func,
combo,
event->button,
event->time);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo->priv->button), TRUE);
if (combo->priv->current_item)
{
gtk_menu_shell_select_item (GTK_MENU_SHELL (combo->priv->menu),
combo->priv->current_item);
}
}
static void
2011-11-07 16:52:18 -06:00
set_shadow_type (PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
GtkShadowType shadow_type;
GtkWidget *statusbar;
/* This is a hack needed to use the shadow type of a statusbar */
statusbar = gtk_statusbar_new ();
gtk_widget_ensure_style (statusbar);
gtk_widget_style_get (statusbar, "shadow-type", &shadow_type, NULL);
gtk_frame_set_shadow_type (GTK_FRAME (combo->priv->frame), shadow_type);
gtk_widget_destroy (statusbar);
}
static void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_init (PlumaStatusComboBox *self)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
self->priv = PLUMA_STATUS_COMBO_BOX_GET_PRIVATE (self);
2011-11-07 13:46:58 -06:00
gtk_event_box_set_visible_window (GTK_EVENT_BOX (self), TRUE);
self->priv->frame = gtk_frame_new (NULL);
gtk_widget_show (self->priv->frame);
self->priv->button = gtk_toggle_button_new ();
2011-11-07 16:52:18 -06:00
gtk_widget_set_name (self->priv->button, "pluma-status-combo-button");
2011-11-07 13:46:58 -06:00
gtk_button_set_relief (GTK_BUTTON (self->priv->button), GTK_RELIEF_NONE);
gtk_widget_show (self->priv->button);
set_shadow_type (self);
self->priv->hbox = gtk_hbox_new (FALSE, 3);
gtk_widget_show (self->priv->hbox);
gtk_container_add (GTK_CONTAINER (self), self->priv->frame);
gtk_container_add (GTK_CONTAINER (self->priv->frame), self->priv->button);
gtk_container_add (GTK_CONTAINER (self->priv->button), self->priv->hbox);
self->priv->label = gtk_label_new ("");
gtk_widget_show (self->priv->label);
gtk_label_set_single_line_mode (GTK_LABEL (self->priv->label), TRUE);
gtk_misc_set_alignment (GTK_MISC (self->priv->label), 0.0, 0.5);
gtk_box_pack_start (GTK_BOX (self->priv->hbox), self->priv->label, FALSE, TRUE, 0);
self->priv->item = gtk_label_new ("");
gtk_widget_show (self->priv->item);
gtk_label_set_single_line_mode (GTK_LABEL (self->priv->item), TRUE);
gtk_misc_set_alignment (GTK_MISC (self->priv->item), 0, 0.5);
gtk_box_pack_start (GTK_BOX (self->priv->hbox), self->priv->item, TRUE, TRUE, 0);
self->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
gtk_widget_show (self->priv->arrow);
gtk_misc_set_alignment (GTK_MISC (self->priv->arrow), 0.5, 0.5);
gtk_box_pack_start (GTK_BOX (self->priv->hbox), self->priv->arrow, FALSE, TRUE, 0);
self->priv->menu = gtk_menu_new ();
g_object_ref_sink (self->priv->menu);
g_signal_connect (self->priv->button,
"button-press-event",
G_CALLBACK (button_press_event),
self);
g_signal_connect (self->priv->menu,
"deactivate",
G_CALLBACK (menu_deactivate),
self);
}
/* public functions */
GtkWidget *
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_new (const gchar *label)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
return g_object_new (PLUMA_TYPE_STATUS_COMBO_BOX, "label", label, NULL);
2011-11-07 13:46:58 -06:00
}
void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_set_label (PlumaStatusComboBox *combo,
2011-11-07 13:46:58 -06:00
const gchar *label)
{
gchar *text;
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo));
2011-11-07 13:46:58 -06:00
text = g_strconcat (" ", label, ": ", NULL);
gtk_label_set_markup (GTK_LABEL (combo->priv->label), text);
g_free (text);
}
const gchar *
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_get_label (PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo), NULL);
2011-11-07 13:46:58 -06:00
return gtk_label_get_label (GTK_LABEL (combo->priv->label));
}
static void
item_activated (GtkMenuItem *item,
2011-11-07 16:52:18 -06:00
PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_set_item (combo, item);
2011-11-07 13:46:58 -06:00
}
void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_add_item (PlumaStatusComboBox *combo,
2011-11-07 13:46:58 -06:00
GtkMenuItem *item,
const gchar *text)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo));
2011-11-07 13:46:58 -06:00
g_return_if_fail (GTK_IS_MENU_ITEM (item));
gtk_menu_shell_append (GTK_MENU_SHELL (combo->priv->menu), GTK_WIDGET (item));
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_set_item_text (combo, item, text);
2011-11-07 13:46:58 -06:00
g_signal_connect (item, "activate", G_CALLBACK (item_activated), combo);
}
void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_remove_item (PlumaStatusComboBox *combo,
2011-11-07 13:46:58 -06:00
GtkMenuItem *item)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo));
2011-11-07 13:46:58 -06:00
g_return_if_fail (GTK_IS_MENU_ITEM (item));
gtk_container_remove (GTK_CONTAINER (combo->priv->menu),
GTK_WIDGET (item));
}
GList *
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_get_items (PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo), NULL);
2011-11-07 13:46:58 -06:00
return gtk_container_get_children (GTK_CONTAINER (combo->priv->menu));
}
const gchar *
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_get_item_text (PlumaStatusComboBox *combo,
2011-11-07 13:46:58 -06:00
GtkMenuItem *item)
{
const gchar *ret = NULL;
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo), NULL);
2011-11-07 13:46:58 -06:00
g_return_val_if_fail (GTK_IS_MENU_ITEM (item), NULL);
ret = g_object_get_data (G_OBJECT (item), COMBO_BOX_TEXT_DATA);
return ret;
}
void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_set_item_text (PlumaStatusComboBox *combo,
2011-11-07 13:46:58 -06:00
GtkMenuItem *item,
const gchar *text)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo));
2011-11-07 13:46:58 -06:00
g_return_if_fail (GTK_IS_MENU_ITEM (item));
g_object_set_data_full (G_OBJECT (item),
COMBO_BOX_TEXT_DATA,
g_strdup (text),
(GDestroyNotify)g_free);
}
void
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_set_item (PlumaStatusComboBox *combo,
2011-11-07 13:46:58 -06:00
GtkMenuItem *item)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo));
2011-11-07 13:46:58 -06:00
g_return_if_fail (GTK_IS_MENU_ITEM (item));
g_signal_emit (combo, signals[CHANGED], 0, item, NULL);
}
GtkLabel *
2011-11-07 16:52:18 -06:00
pluma_status_combo_box_get_item_label (PlumaStatusComboBox *combo)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_STATUS_COMBO_BOX (combo), NULL);
2011-11-07 13:46:58 -06:00
return GTK_LABEL (combo->priv->item);
}