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)) #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) static void menu_deactivate (GtkMenu *menu, XeditStatusComboBox *combo);
#define gtk_hbox_new(X,Y) gtk_box_new(GTK_ORIENTATION_HORIZONTAL,Y)
#endif
struct _XeditStatusComboBoxPrivate struct _XeditStatusComboBoxPrivate
{ {
@ -43,6 +41,11 @@ struct _XeditStatusComboBoxPrivate
GtkWidget *current_item; GtkWidget *current_item;
}; };
struct _XeditStatusComboBoxClassPrivate
{
GtkCssProvider *css;
};
/* Signals */ /* Signals */
enum enum
{ {
@ -60,7 +63,8 @@ enum
static guint signals[NUM_SIGNALS] = { 0 }; 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 static void
xedit_status_combo_box_finalize (GObject *object) 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 static void
xedit_status_combo_box_changed (XeditStatusComboBox *combo, xedit_status_combo_box_changed (XeditStatusComboBox *combo,
GtkMenuItem *item) GtkMenuItem *item)
@ -125,11 +145,23 @@ static void
xedit_status_combo_box_class_init (XeditStatusComboBoxClass *klass) xedit_status_combo_box_class_init (XeditStatusComboBoxClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (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->finalize = xedit_status_combo_box_finalize;
object_class->get_property = xedit_status_combo_box_get_property; object_class->get_property = xedit_status_combo_box_get_property;
object_class->set_property = xedit_status_combo_box_set_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; klass->changed = xedit_status_combo_box_changed;
signals[CHANGED] = signals[CHANGED] =
@ -148,18 +180,12 @@ xedit_status_combo_box_class_init (XeditStatusComboBoxClass *klass)
NULL, NULL,
G_PARAM_READWRITE)); 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)); 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 static void
@ -181,11 +207,7 @@ menu_position_func (GtkMenu *menu,
*push_in = FALSE; *push_in = FALSE;
#if GTK_CHECK_VERSION (3, 0, 0) gtk_widget_get_preferred_size (gtk_widget_get_toplevel (GTK_WIDGET (menu)), &request, NULL);
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
/* get the origin... */ /* get the origin... */
gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (combo)), x, y); gdk_window_get_origin (gtk_widget_get_window (GTK_WIDGET (combo)), x, y);
@ -202,22 +224,19 @@ menu_position_func (GtkMenu *menu,
} }
static void static void
button_press_event (GtkWidget *widget, show_menu (XeditStatusComboBox *combo,
GdkEventButton *event, guint button,
XeditStatusComboBox *combo) guint32 time)
{ {
GtkRequisition request; GtkRequisition request;
GtkAllocation allocation;
gint max_height; gint max_height;
GtkAllocation allocation;
#if GTK_CHECK_VERSION (3, 0, 0) gtk_widget_get_preferred_size (combo->priv->menu,
gtk_widget_get_preferred_size (combo->priv->menu, NULL, &request); &request, NULL);
#else
gtk_widget_size_request (combo->priv->menu, &request);
#endif
gtk_widget_get_allocation (GTK_WIDGET (combo), &allocation);
/* do something relative to our own height here, maybe we can do better */ /* 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; max_height = allocation.height * 20;
if (request.height > max_height) if (request.height > max_height)
@ -231,8 +250,8 @@ button_press_event (GtkWidget *widget,
NULL, NULL,
(GtkMenuPositionFunc)menu_position_func, (GtkMenuPositionFunc)menu_position_func,
combo, combo,
event->button, button,
event->time); time);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo->priv->button), TRUE); 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 static void
set_shadow_type (XeditStatusComboBox *combo) set_shadow_type (XeditStatusComboBox *combo)
{ {
GtkStyleContext *context;
GtkShadowType shadow_type; GtkShadowType shadow_type;
GtkWidget *statusbar; GtkWidget *statusbar;
/* This is a hack needed to use the shadow type of a statusbar */ /* This is a hack needed to use the shadow type of a statusbar */
statusbar = gtk_statusbar_new (); 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_frame_set_shadow_type (GTK_FRAME (combo->priv->frame), shadow_type);
gtk_widget_destroy (statusbar); gtk_widget_destroy (statusbar);
@ -262,6 +307,8 @@ set_shadow_type (XeditStatusComboBox *combo)
static void static void
xedit_status_combo_box_init (XeditStatusComboBox *self) xedit_status_combo_box_init (XeditStatusComboBox *self)
{ {
GtkStyleContext *context;
self->priv = XEDIT_STATUS_COMBO_BOX_GET_PRIVATE (self); self->priv = XEDIT_STATUS_COMBO_BOX_GET_PRIVATE (self);
gtk_event_box_set_visible_window (GTK_EVENT_BOX (self), TRUE); 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); gtk_widget_show (self->priv->frame);
self->priv->button = gtk_toggle_button_new (); 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_button_set_relief (GTK_BUTTON (self->priv->button), GTK_RELIEF_NONE);
gtk_widget_show (self->priv->button); gtk_widget_show (self->priv->button);
set_shadow_type (self); 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_widget_show (self->priv->hbox);
gtk_container_add (GTK_CONTAINER (self), self->priv->frame); 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_widget_show (self->priv->label);
gtk_label_set_single_line_mode (GTK_LABEL (self->priv->label), TRUE); gtk_label_set_single_line_mode (GTK_LABEL (self->priv->label), TRUE);
#if GTK_CHECK_VERSION (3, 16, 0) gtk_widget_set_halign (GTK_WIDGET (self->priv->label), GTK_ALIGN_START);
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_box_pack_start (GTK_BOX (self->priv->hbox), self->priv->label, FALSE, TRUE, 0); 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_widget_show (self->priv->item);
gtk_label_set_single_line_mode (GTK_LABEL (self->priv->item), TRUE); 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); 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); 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); self->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
gtk_widget_show (self->priv->arrow); 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_halign (self->priv->arrow, GTK_ALIGN_CENTER);
gtk_widget_set_valign (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); gtk_box_pack_start (GTK_BOX (self->priv->hbox), self->priv->arrow, FALSE, TRUE, 0);
self->priv->menu = gtk_menu_new (); 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, g_signal_connect (self->priv->button,
"button-press-event", "button-press-event",
@ -329,6 +366,15 @@ xedit_status_combo_box_init (XeditStatusComboBox *self)
"deactivate", "deactivate",
G_CALLBACK (menu_deactivate), G_CALLBACK (menu_deactivate),
self); 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 */ /* 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)) #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 _XeditStatusComboBox XeditStatusComboBox;
typedef struct _XeditStatusComboBoxClass XeditStatusComboBoxClass;
typedef struct _XeditStatusComboBoxPrivate XeditStatusComboBoxPrivate; typedef struct _XeditStatusComboBoxPrivate XeditStatusComboBoxPrivate;
typedef struct _XeditStatusComboBoxClass XeditStatusComboBoxClass;
typedef struct _XeditStatusComboBoxClassPrivate XeditStatusComboBoxClassPrivate;
struct _XeditStatusComboBox { struct _XeditStatusComboBox
{
GtkEventBox parent; GtkEventBox parent;
XeditStatusComboBoxPrivate *priv; XeditStatusComboBoxPrivate *priv;
}; };
struct _XeditStatusComboBoxClass { struct _XeditStatusComboBoxClass
{
GtkEventBoxClass parent_class; GtkEventBoxClass parent_class;
XeditStatusComboBoxClassPrivate *priv;
void (*changed) (XeditStatusComboBox *combo, void (*changed) (XeditStatusComboBox *combo,
GtkMenuItem *item); GtkMenuItem *item);
}; };

View File

@ -42,10 +42,6 @@
XEDIT_TYPE_STATUSBAR,\ XEDIT_TYPE_STATUSBAR,\
XeditStatusbarPrivate)) XeditStatusbarPrivate))
#if GTK_CHECK_VERSION (3, 0, 0)
#define gtk_hbox_new(X,Y) gtk_box_new(GTK_ORIENTATION_HORIZONTAL,Y)
#endif
struct _XeditStatusbarPrivate struct _XeditStatusbarPrivate
{ {
GtkWidget *overwrite_mode_label; GtkWidget *overwrite_mode_label;
@ -114,10 +110,8 @@ xedit_statusbar_init (XeditStatusbar *statusbar)
statusbar->priv = XEDIT_STATUSBAR_GET_PRIVATE (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_top (GTK_WIDGET (statusbar), 0);
gtk_widget_set_margin_bottom (GTK_WIDGET (statusbar), 0); gtk_widget_set_margin_bottom (GTK_WIDGET (statusbar), 0);
#endif
statusbar->priv->overwrite_mode_label = gtk_label_new (NULL); statusbar->priv->overwrite_mode_label = gtk_label_new (NULL);
gtk_label_set_width_chars (GTK_LABEL (statusbar->priv->overwrite_mode_label), 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_frame_set_shadow_type (GTK_FRAME (statusbar->priv->state_frame),
GTK_SHADOW_IN); 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); 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->load_image = gtk_image_new_from_icon_name ("document-open-symbolic", GTK_ICON_SIZE_MENU);
statusbar->priv->save_image = gtk_image_new_from_stock (GTK_STOCK_SAVE, 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_stock (GTK_STOCK_PRINT, GTK_ICON_SIZE_MENU); statusbar->priv->print_image = gtk_image_new_from_icon_name ("printer-symbolic", GTK_ICON_SIZE_MENU);
gtk_widget_show (hbox); gtk_widget_show (hbox);
@ -165,11 +159,8 @@ xedit_statusbar_init (XeditStatusbar *statusbar)
statusbar->priv->error_frame = gtk_frame_new (NULL); statusbar->priv->error_frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (statusbar->priv->error_frame), GTK_SHADOW_IN); 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); 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) #if GTK_CHECK_VERSION (3, 14, 0)
gtk_widget_set_margin_start (error_image, 4); gtk_widget_set_margin_start (error_image, 4);
gtk_widget_set_margin_end (error_image, 4); gtk_widget_set_margin_end (error_image, 4);