Merge pull request #1 from JosephMcc/update-1

Clean up GTK support
This commit is contained in:
Clement Lefebvre 2016-01-27 14:14:29 +00:00
commit 5cdbafaa2c
3 changed files with 544 additions and 502 deletions

View File

@ -26,9 +26,7 @@
#define XEDIT_STATUS_COMBO_BOX_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), XEDIT_TYPE_STATUS_COMBO_BOX, XeditStatusComboBoxPrivate))
#if GTK_CHECK_VERSION (3, 0, 0)
#define gtk_hbox_new(X,Y) gtk_box_new(GTK_ORIENTATION_HORIZONTAL,Y)
#endif
static void menu_deactivate (GtkMenu *menu, XeditStatusComboBox *combo);
struct _XeditStatusComboBoxPrivate
{
@ -43,6 +41,11 @@ struct _XeditStatusComboBoxPrivate
GtkWidget *current_item;
};
struct _XeditStatusComboBoxClassPrivate
{
GtkCssProvider *css;
};
/* Signals */
enum
{
@ -60,7 +63,8 @@ enum
static guint signals[NUM_SIGNALS] = { 0 };
G_DEFINE_TYPE(XeditStatusComboBox, xedit_status_combo_box, GTK_TYPE_EVENT_BOX)
G_DEFINE_TYPE_WITH_CODE (XeditStatusComboBox, xedit_status_combo_box, GTK_TYPE_EVENT_BOX,
g_type_add_class_private (g_define_type_id, sizeof (XeditStatusComboBoxClassPrivate)))
static void
xedit_status_combo_box_finalize (GObject *object)
@ -106,6 +110,22 @@ xedit_status_combo_box_set_property (GObject *object,
}
}
static void
xedit_status_combo_box_destroy (GtkWidget *widget)
{
XeditStatusComboBox *combo = XEDIT_STATUS_COMBO_BOX (widget);
if (combo->priv->menu)
{
g_signal_handlers_disconnect_by_func (combo->priv->menu,
menu_deactivate,
combo);
gtk_menu_detach (GTK_MENU (combo->priv->menu));
}
GTK_WIDGET_CLASS (xedit_status_combo_box_parent_class)->destroy (widget);
}
static void
xedit_status_combo_box_changed (XeditStatusComboBox *combo,
GtkMenuItem *item)
@ -125,11 +145,23 @@ static void
xedit_status_combo_box_class_init (XeditStatusComboBoxClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
static const gchar style[] =
"* {\n"
"-GtkButton-default-border : 0;\n"
"-GtkButton-default-outside-border : 0;\n"
"-GtkButton-inner-border: 0;\n"
"-GtkWidget-focus-line-width : 0;\n"
"-GtkWidget-focus-padding : 0;\n"
"padding: 0;\n"
"}";
object_class->finalize = xedit_status_combo_box_finalize;
object_class->get_property = xedit_status_combo_box_get_property;
object_class->set_property = xedit_status_combo_box_set_property;
widget_class->destroy = xedit_status_combo_box_destroy;
klass->changed = xedit_status_combo_box_changed;
signals[CHANGED] =
@ -148,18 +180,12 @@ xedit_status_combo_box_class_init (XeditStatusComboBoxClass *klass)
NULL,
G_PARAM_READWRITE));
/* Set up a style for the button to decrease spacing. */
gtk_rc_parse_string (
"style \"xedit-status-combo-button-style\"\n"
"{\n"
" GtkWidget::focus-padding = 0\n"
" GtkWidget::focus-line-width = 0\n"
" xthickness = 0\n"
" ythickness = 0\n"
"}\n"
"widget \"*.xedit-status-combo-button\" style \"xedit-status-combo-button-style\"");
g_type_class_add_private (object_class, sizeof(XeditStatusComboBoxPrivate));
klass->priv = G_TYPE_CLASS_GET_PRIVATE (klass, XEDIT_TYPE_STATUS_COMBO_BOX, XeditStatusComboBoxClassPrivate);
klass->priv->css = gtk_css_provider_new ();
gtk_css_provider_load_from_data (klass->priv->css, style, -1, NULL);
}
static void
@ -181,11 +207,7 @@ menu_position_func (GtkMenu *menu,
*push_in = FALSE;
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_widget_get_preferred_size (gtk_widget_get_toplevel (GTK_WIDGET (menu)), NULL, &request);
#else
gtk_widget_size_request (gtk_widget_get_toplevel (GTK_WIDGET (menu)), &request);
#endif
gtk_widget_get_preferred_size (gtk_widget_get_toplevel (GTK_WIDGET (menu)), &request, NULL);
/* get the origin... */
gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (combo)), x, y);
@ -202,22 +224,19 @@ menu_position_func (GtkMenu *menu,
}
static void
button_press_event (GtkWidget *widget,
GdkEventButton *event,
XeditStatusComboBox *combo)
show_menu (XeditStatusComboBox *combo,
guint button,
guint32 time)
{
GtkRequisition request;
GtkAllocation allocation;
gint max_height;
GtkAllocation allocation;
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_widget_get_preferred_size (combo->priv->menu, NULL, &request);
#else
gtk_widget_size_request (combo->priv->menu, &request);
#endif
gtk_widget_get_allocation (GTK_WIDGET (combo), &allocation);
gtk_widget_get_preferred_size (combo->priv->menu,
&request, NULL);
/* do something relative to our own height here, maybe we can do better */
gtk_widget_get_allocation (GTK_WIDGET (combo), &allocation);
max_height = allocation.height * 20;
if (request.height > max_height)
@ -231,8 +250,8 @@ button_press_event (GtkWidget *widget,
NULL,
(GtkMenuPositionFunc)menu_position_func,
combo,
event->button,
event->time);
button,
time);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo->priv->button), TRUE);
@ -243,17 +262,43 @@ button_press_event (GtkWidget *widget,
}
}
static void
menu_detached (GtkWidget *widget,
GtkMenu *menu)
{
XeditStatusComboBox *combo = XEDIT_STATUS_COMBO_BOX (widget);
g_return_if_fail (GTK_MENU (combo->priv->menu) == menu);
combo->priv->menu = NULL;
}
static gboolean
button_press_event (GtkWidget *widget,
GdkEventButton *event,
XeditStatusComboBox *combo)
{
if (event->type == GDK_BUTTON_PRESS && event->button == 1)
{
show_menu (combo, event->button, event->time);
return TRUE;
}
return FALSE;
}
static void
set_shadow_type (XeditStatusComboBox *combo)
{
GtkStyleContext *context;
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);
context = gtk_widget_get_style_context (statusbar);
gtk_widget_style_get (statusbar, "shadow-type", &shadow_type, NULL);
gtk_style_context_get_style (context, "shadow-type", &shadow_type, NULL);
gtk_frame_set_shadow_type (GTK_FRAME (combo->priv->frame), shadow_type);
gtk_widget_destroy (statusbar);
@ -262,6 +307,8 @@ set_shadow_type (XeditStatusComboBox *combo)
static void
xedit_status_combo_box_init (XeditStatusComboBox *self)
{
GtkStyleContext *context;
self->priv = XEDIT_STATUS_COMBO_BOX_GET_PRIVATE (self);
gtk_event_box_set_visible_window (GTK_EVENT_BOX (self), TRUE);
@ -270,13 +317,12 @@ xedit_status_combo_box_init (XeditStatusComboBox *self)
gtk_widget_show (self->priv->frame);
self->priv->button = gtk_toggle_button_new ();
gtk_widget_set_name (self->priv->button, "xedit-status-combo-button");
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);
self->priv->hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
gtk_widget_show (self->priv->hbox);
gtk_container_add (GTK_CONTAINER (self), self->priv->frame);
@ -287,11 +333,7 @@ xedit_status_combo_box_init (XeditStatusComboBox *self)
gtk_widget_show (self->priv->label);
gtk_label_set_single_line_mode (GTK_LABEL (self->priv->label), TRUE);
#if GTK_CHECK_VERSION (3, 16, 0)
gtk_label_set_xalign (GTK_LABEL (self->priv->label), 0.0);
#else
gtk_misc_set_alignment (GTK_MISC (self->priv->label), 0.0, 0.5);
#endif
gtk_widget_set_halign (GTK_WIDGET (self->priv->label), GTK_ALIGN_START);
gtk_box_pack_start (GTK_BOX (self->priv->hbox), self->priv->label, FALSE, TRUE, 0);
@ -299,27 +341,22 @@ xedit_status_combo_box_init (XeditStatusComboBox *self)
gtk_widget_show (self->priv->item);
gtk_label_set_single_line_mode (GTK_LABEL (self->priv->item), TRUE);
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_widget_set_halign (self->priv->item, GTK_ALIGN_START);
#else
gtk_misc_set_alignment (GTK_MISC (self->priv->item), 0, 0.5);
#endif
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);
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_widget_set_halign (self->priv->arrow, GTK_ALIGN_CENTER);
gtk_widget_set_valign (self->priv->arrow, GTK_ALIGN_CENTER);
#else
gtk_misc_set_alignment (GTK_MISC (self->priv->arrow), 0.5, 0.5);
#endif
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);
gtk_menu_attach_to_widget (GTK_MENU (self->priv->menu),
GTK_WIDGET (self),
menu_detached);
g_signal_connect (self->priv->button,
"button-press-event",
@ -329,6 +366,15 @@ xedit_status_combo_box_init (XeditStatusComboBox *self)
"deactivate",
G_CALLBACK (menu_deactivate),
self);
context = gtk_widget_get_style_context (GTK_WIDGET (self->priv->button));
gtk_style_context_add_provider (context,
GTK_STYLE_PROVIDER (XEDIT_STATUS_COMBO_BOX_GET_CLASS (self)->priv->css),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
context = gtk_widget_get_style_context (GTK_WIDGET (self->priv->frame));
gtk_style_context_add_provider (context,
GTK_STYLE_PROVIDER (XEDIT_STATUS_COMBO_BOX_GET_CLASS (self)->priv->css),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
/* public functions */

View File

@ -36,18 +36,23 @@ G_BEGIN_DECLS
#define XEDIT_STATUS_COMBO_BOX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), XEDIT_TYPE_STATUS_COMBO_BOX, XeditStatusComboBoxClass))
typedef struct _XeditStatusComboBox XeditStatusComboBox;
typedef struct _XeditStatusComboBoxClass XeditStatusComboBoxClass;
typedef struct _XeditStatusComboBoxPrivate XeditStatusComboBoxPrivate;
typedef struct _XeditStatusComboBoxClass XeditStatusComboBoxClass;
typedef struct _XeditStatusComboBoxClassPrivate XeditStatusComboBoxClassPrivate;
struct _XeditStatusComboBox {
struct _XeditStatusComboBox
{
GtkEventBox parent;
XeditStatusComboBoxPrivate *priv;
};
struct _XeditStatusComboBoxClass {
struct _XeditStatusComboBoxClass
{
GtkEventBoxClass parent_class;
XeditStatusComboBoxClassPrivate *priv;
void (*changed) (XeditStatusComboBox *combo,
GtkMenuItem *item);
};

View File

@ -42,10 +42,6 @@
XEDIT_TYPE_STATUSBAR,\
XeditStatusbarPrivate))
#if GTK_CHECK_VERSION (3, 0, 0)
#define gtk_hbox_new(X,Y) gtk_box_new(GTK_ORIENTATION_HORIZONTAL,Y)
#endif
struct _XeditStatusbarPrivate
{
GtkWidget *overwrite_mode_label;
@ -114,10 +110,8 @@ xedit_statusbar_init (XeditStatusbar *statusbar)
statusbar->priv = XEDIT_STATUSBAR_GET_PRIVATE (statusbar);
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_widget_set_margin_top (GTK_WIDGET (statusbar), 0);
gtk_widget_set_margin_bottom (GTK_WIDGET (statusbar), 0);
#endif
statusbar->priv->overwrite_mode_label = gtk_label_new (NULL);
gtk_label_set_width_chars (GTK_LABEL (statusbar->priv->overwrite_mode_label),
@ -139,12 +133,12 @@ xedit_statusbar_init (XeditStatusbar *statusbar)
gtk_frame_set_shadow_type (GTK_FRAME (statusbar->priv->state_frame),
GTK_SHADOW_IN);
hbox = gtk_hbox_new (FALSE, 0);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_container_add (GTK_CONTAINER (statusbar->priv->state_frame), hbox);
statusbar->priv->load_image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
statusbar->priv->save_image = gtk_image_new_from_stock (GTK_STOCK_SAVE, GTK_ICON_SIZE_MENU);
statusbar->priv->print_image = gtk_image_new_from_stock (GTK_STOCK_PRINT, GTK_ICON_SIZE_MENU);
statusbar->priv->load_image = gtk_image_new_from_icon_name ("document-open-symbolic", GTK_ICON_SIZE_MENU);
statusbar->priv->save_image = gtk_image_new_from_icon_name ("document-save-symbolic", GTK_ICON_SIZE_MENU);
statusbar->priv->print_image = gtk_image_new_from_icon_name ("printer-symbolic", GTK_ICON_SIZE_MENU);
gtk_widget_show (hbox);
@ -165,11 +159,8 @@ xedit_statusbar_init (XeditStatusbar *statusbar)
statusbar->priv->error_frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (statusbar->priv->error_frame), GTK_SHADOW_IN);
#if GTK_CHECK_VERSION (3, 10, 0)
error_image = gtk_image_new_from_icon_name ("dialog-error", GTK_ICON_SIZE_MENU);
#else
error_image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_MENU);
#endif
#if GTK_CHECK_VERSION (3, 14, 0)
gtk_widget_set_margin_start (error_image, 4);
gtk_widget_set_margin_end (error_image, 4);