Commit bba9f3c4 authored by jhol's avatar jhol Committed by Johannes Schmid
Browse files

#575367 – Patches for reworking grips

2009-03-18  Joel Holdsworth <joel@airwebreathe.org.uk>

	* gdl/Makefile.am:
	* gdl/gdl-dock-item-grip.c (gdl_dock_item_create_label_widget),
	(gdl_dock_item_grip_item_notify), (gdl_dock_item_grip_destroy),
	(gdl_dock_item_grip_instance_init), (gdl_dock_item_grip_realize),
	(gdl_dock_item_grip_unrealize), (gdl_dock_item_grip_size_request),
	(gdl_dock_item_grip_size_allocate), (gdl_dock_item_grip_forall),
	(gdl_dock_item_grip_class_init), (gdl_dock_item_grip_set_label):
	* gdl/gdl-dock-item-grip.h:
	* gdl/gdl-dock-item.c (gdl_dock_item_get_grip):
	* gdl/gdl-dock-item.h:
	#575367 – Patches for reworking grips

svn path=/trunk/; revision=541
parent 95bb1344
2009-03-18 Joel Holdsworth <joel@airwebreathe.org.uk>
* gdl/Makefile.am:
* gdl/gdl-dock-item-grip.c (gdl_dock_item_create_label_widget),
(gdl_dock_item_grip_item_notify), (gdl_dock_item_grip_destroy),
(gdl_dock_item_grip_instance_init), (gdl_dock_item_grip_realize),
(gdl_dock_item_grip_unrealize), (gdl_dock_item_grip_size_request),
(gdl_dock_item_grip_size_allocate), (gdl_dock_item_grip_forall),
(gdl_dock_item_grip_class_init), (gdl_dock_item_grip_set_label):
* gdl/gdl-dock-item-grip.h:
* gdl/gdl-dock-item.c (gdl_dock_item_get_grip):
* gdl/gdl-dock-item.h:
#575367 – Patches for reworking grips
2009-03-16 Johannes Schmid <jhs@gnome.org>
* NEWS: Update for release
......
......@@ -28,6 +28,7 @@ libgdl_headers = \
gdl-dock-master.h \
gdl-dock.h \
gdl-dock-item.h \
gdl-dock-item-grip.h \
gdl-dock-notebook.h \
gdl-dock-paned.h \
gdl-dock-tablabel.h \
......
......@@ -31,146 +31,55 @@ enum {
};
struct _GdlDockItemGripPrivate {
GtkWidget *label;
GtkWidget *close_button;
GtkWidget *iconify_button;
gboolean icon_pixbuf_valid;
GdkPixbuf *icon_pixbuf;
gchar *title;
PangoLayout *title_layout;
};
GDL_CLASS_BOILERPLATE (GdlDockItemGrip, gdl_dock_item_grip,
GtkContainer, GTK_TYPE_CONTAINER);
/* must be called after size_allocate */
static void
gdl_dock_item_grip_get_title_area (GdlDockItemGrip *grip,
GdkRectangle *area)
{
GtkWidget *widget = GTK_WIDGET (grip);
gint border = GTK_CONTAINER (grip)->border_width;
gint alloc_height;
area->width = (widget->allocation.width - 2 * border - ALIGN_BORDER);
pango_layout_get_pixel_size (grip->_priv->title_layout, NULL, &alloc_height);
alloc_height = MAX (grip->_priv->close_button->allocation.height, alloc_height);
alloc_height = MAX (grip->_priv->iconify_button->allocation.height, alloc_height);
if (GTK_WIDGET_VISIBLE (grip->_priv->close_button)) {
area->width -= grip->_priv->close_button->allocation.width;
}
if (GTK_WIDGET_VISIBLE (grip->_priv->iconify_button)) {
area->width -= grip->_priv->iconify_button->allocation.width;
}
area->x = widget->allocation.x + border + ALIGN_BORDER;
area->y = widget->allocation.y + border;
area->height = alloc_height;
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
area->x += (widget->allocation.width - 2 * border) - area->width;
}
static void
ensure_title_and_icon_pixbuf (GdlDockItemGrip *grip)
GtkWidget*
gdl_dock_item_create_label_widget(GdlDockItemGrip *grip)
{
gchar *stock_id;
g_return_if_fail (GDL_IS_DOCK_ITEM_GRIP (grip));
GtkHBox *label_box;
GtkImage *image;
GtkLabel *label;
gchar *stock_id = NULL;
gchar *title = NULL;
label_box = (GtkHBox*)gtk_hbox_new (FALSE, 0);
/* get long name property from the dock object */
if (!grip->_priv->title) {
g_object_get (G_OBJECT (grip->item), "long-name", &grip->_priv->title, NULL);
if (!grip->_priv->title)
grip->_priv->title = g_strdup ("");
}
/* retrieve stock pixbuf, if any */
if (!grip->_priv->icon_pixbuf_valid) {
g_object_get (G_OBJECT (grip->item), "stock-id", &stock_id, NULL);
g_object_get (G_OBJECT (grip->item), "stock-id", &stock_id, NULL);
if(stock_id) {
image = GTK_IMAGE(gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU));
if (stock_id) {
grip->_priv->icon_pixbuf = gtk_widget_render_icon (GTK_WIDGET (grip),
stock_id,
GTK_ICON_SIZE_MENU, "");
g_free (stock_id);
}
grip->_priv->icon_pixbuf_valid = TRUE;
}
/* create layout: the actual text is reset at size_allocate */
if (!grip->_priv->title_layout) {
grip->_priv->title_layout = gtk_widget_create_pango_layout (GTK_WIDGET (grip),
grip->_priv->title);
pango_layout_set_single_paragraph_mode (grip->_priv->title_layout, TRUE);
gtk_widget_show (GTK_WIDGET(image));
gtk_box_pack_start(GTK_BOX(label_box), GTK_WIDGET(image), FALSE, TRUE, 0);
g_free (stock_id);
}
}
static gint
gdl_dock_item_grip_expose (GtkWidget *widget,
GdkEventExpose *event)
{
GdlDockItemGrip *grip;
GdkRectangle title_area;
GdkRectangle expose_area;
gint layout_width;
gint layout_height;
gint text_x;
gint text_y;
grip = GDL_DOCK_ITEM_GRIP (widget);
gdl_dock_item_grip_get_title_area (grip, &title_area);
if (grip->_priv->icon_pixbuf) {
GdkRectangle pixbuf_rect;
pixbuf_rect.width = gdk_pixbuf_get_width (grip->_priv->icon_pixbuf);
pixbuf_rect.height = gdk_pixbuf_get_height (grip->_priv->icon_pixbuf);
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) {
pixbuf_rect.x = title_area.x + title_area.width - pixbuf_rect.width;
g_object_get (G_OBJECT (grip->item), "long-name", &title, NULL);
if (title) {
label = GTK_LABEL(gtk_label_new(title));
gtk_label_set_ellipsize(label, PANGO_ELLIPSIZE_END);
gtk_label_set_justify(label, GTK_JUSTIFY_LEFT);
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
gtk_widget_show (GTK_WIDGET(label));
if (gtk_widget_get_direction (GTK_WIDGET(grip)) == GTK_TEXT_DIR_RTL) {
gtk_box_pack_end(GTK_BOX(label_box), GTK_WIDGET(label), TRUE, TRUE, 1);
} else {
pixbuf_rect.x = title_area.x;
title_area.x += pixbuf_rect.width + 1;
}
/* shrink title area by the pixbuf width plus a 1px spacing */
title_area.width -= pixbuf_rect.width + 1;
pixbuf_rect.y = title_area.y + (title_area.height - pixbuf_rect.height) / 2;
if (gdk_rectangle_intersect (&event->area, &pixbuf_rect, &expose_area)) {
GdkGC *gc;
GtkStyle *style;
style = gtk_widget_get_style (widget);
gc = style->bg_gc[widget->state];
gdk_draw_pixbuf (GDK_DRAWABLE (widget->window), gc,
grip->_priv->icon_pixbuf,
0, 0, pixbuf_rect.x, pixbuf_rect.y,
pixbuf_rect.width, pixbuf_rect.height,
GDK_RGB_DITHER_NONE, 0, 0);
gtk_box_pack_start(GTK_BOX(label_box), GTK_WIDGET(label), TRUE, TRUE, 1);
}
g_free(title);
}
if (gdk_rectangle_intersect (&title_area, &event->area, &expose_area)) {
pango_layout_get_pixel_size (grip->_priv->title_layout, &layout_width,
&layout_height);
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
text_x = title_area.x + title_area.width - layout_width;
else
text_x = title_area.x;
text_y = title_area.y + (title_area.height - layout_height) / 2;
gtk_paint_layout (widget->style, widget->window, widget->state, TRUE,
&expose_area, widget, NULL, text_x, text_y,
grip->_priv->title_layout);
}
return GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
}
return GTK_WIDGET(label_box);
}
static void
gdl_dock_item_grip_item_notify (GObject *master,
......@@ -182,23 +91,12 @@ gdl_dock_item_grip_item_notify (GObject *master,
grip = GDL_DOCK_ITEM_GRIP (data);
if (strcmp (pspec->name, "stock-id") == 0) {
if (grip->_priv->icon_pixbuf) {
g_object_unref (grip->_priv->icon_pixbuf);
grip->_priv->icon_pixbuf = NULL;
}
grip->_priv->icon_pixbuf_valid = FALSE;
ensure_title_and_icon_pixbuf (grip);
} else if (strcmp (pspec->name, "long-name") == 0) {
if (grip->_priv->title_layout) {
g_object_unref (grip->_priv->title_layout);
grip->_priv->title_layout = NULL;
}
g_free (grip->_priv->title);
grip->_priv->title = NULL;
ensure_title_and_icon_pixbuf (grip);
gtk_widget_queue_draw (GTK_WIDGET (grip));
if ((strcmp (pspec->name, "stock-id") == 0) ||
(strcmp (pspec->name, "long-name") == 0)) {
gdl_dock_item_grip_set_label (grip,
gdl_dock_item_create_label_widget(grip));
} else if (strcmp (pspec->name, "behavior") == 0) {
cursor = FALSE;
if (grip->_priv->close_button) {
......@@ -227,20 +125,13 @@ static void
gdl_dock_item_grip_destroy (GtkObject *object)
{
GdlDockItemGrip *grip = GDL_DOCK_ITEM_GRIP (object);
if (grip->_priv) {
GdlDockItemGripPrivate *priv = grip->_priv;
if (priv->title_layout) {
g_object_unref (priv->title_layout);
priv->title_layout = NULL;
}
g_free (priv->title);
priv->title = NULL;
if (priv->icon_pixbuf) {
g_object_unref (priv->icon_pixbuf);
priv->icon_pixbuf = NULL;
if (priv->label) {
gtk_widget_unparent(grip->_priv->label);
priv->label = NULL;
}
if (grip->item)
......@@ -252,7 +143,7 @@ gdl_dock_item_grip_destroy (GtkObject *object)
grip->_priv = NULL;
g_free (priv);
}
GDL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}
......@@ -324,10 +215,8 @@ gdl_dock_item_grip_instance_init (GdlDockItemGrip *grip)
GTK_WIDGET_SET_FLAGS (grip, GTK_NO_WINDOW);
grip->_priv = g_new0 (GdlDockItemGripPrivate, 1);
grip->_priv->icon_pixbuf_valid = FALSE;
grip->_priv->icon_pixbuf = NULL;
grip->_priv->title_layout = NULL;
grip->_priv->label = NULL;
/* create the close button */
gtk_widget_push_composite_child ();
grip->_priv->close_button = gtk_button_new ();
......@@ -376,33 +265,31 @@ gdl_dock_item_grip_realize (GtkWidget *widget)
GTK_WIDGET_CLASS (parent_class)->realize (widget);
g_return_if_fail (grip->_priv != NULL);
if (!grip->title_window) {
GdkWindowAttr attributes;
GdkRectangle area;
GdkCursor *cursor;
ensure_title_and_icon_pixbuf (grip);
gdl_dock_item_grip_get_title_area (grip, &area);
attributes.x = area.x;
attributes.y = area.y;
attributes.width = area.width;
attributes.height = area.height;
attributes.window_type = GDK_WINDOW_TEMP;
attributes.wclass = GDK_INPUT_ONLY;
attributes.override_redirect = TRUE;
attributes.event_mask = (GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK |
GDK_BUTTON_MOTION_MASK |
gtk_widget_get_events (widget));
g_return_if_fail (grip->_priv->label != NULL);
attributes.x = grip->_priv->label->allocation.x;
attributes.y = grip->_priv->label->allocation.y;
attributes.width = grip->_priv->label->allocation.width;
attributes.height = grip->_priv->label->allocation.height;
attributes.window_type = GDK_WINDOW_CHILD;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.event_mask = GDK_ALL_EVENTS_MASK;
grip->title_window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes,
(GDK_WA_X |
GDK_WA_Y |
GDK_WA_NOREDIR));
&attributes, (GDK_WA_X | GDK_WA_Y));
gdk_window_set_user_data (grip->title_window, widget);
gdk_window_set_user_data (grip->title_window, grip);
widget->window = grip->title_window;
GTK_WIDGET_UNSET_FLAGS(widget, GTK_NO_WINDOW);
/* Unset the background so as to make the colour match the parent window */
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, NULL);
if (GDL_DOCK_ITEM_CANT_CLOSE (grip->item) &&
GDL_DOCK_ITEM_CANT_ICONIFY (grip->item))
......@@ -422,6 +309,7 @@ gdl_dock_item_grip_unrealize (GtkWidget *widget)
GdlDockItemGrip *grip = GDL_DOCK_ITEM_GRIP (widget);
if (grip->title_window) {
GTK_WIDGET_SET_FLAGS(widget, GTK_NO_WINDOW);
gdk_window_set_user_data (grip->title_window, NULL);
gdk_window_destroy (grip->title_window);
grip->title_window = NULL;
......@@ -459,7 +347,7 @@ gdl_dock_item_grip_size_request (GtkWidget *widget,
GtkRequisition child_requisition;
GtkContainer *container;
GdlDockItemGrip *grip;
gint layout_height;
gint layout_height = 0;
g_return_if_fail (GDL_IS_DOCK_ITEM_GRIP (widget));
g_return_if_fail (requisition != NULL);
......@@ -467,12 +355,9 @@ gdl_dock_item_grip_size_request (GtkWidget *widget,
container = GTK_CONTAINER (widget);
grip = GDL_DOCK_ITEM_GRIP (widget);
requisition->width = container->border_width * 2 + ALIGN_BORDER;
requisition->width = container->border_width * 2/* + ALIGN_BORDER*/;
requisition->height = container->border_width * 2;
ensure_title_and_icon_pixbuf (grip);
pango_layout_get_pixel_size (grip->_priv->title_layout, NULL, &layout_height);
gtk_widget_size_request (grip->_priv->close_button, &child_requisition);
layout_height = MAX (layout_height, child_requisition.height);
if (GTK_WIDGET_VISIBLE (grip->_priv->close_button)) {
......@@ -484,54 +369,12 @@ gdl_dock_item_grip_size_request (GtkWidget *widget,
if (GTK_WIDGET_VISIBLE (grip->_priv->iconify_button)) {
requisition->width += child_requisition.width;
}
requisition->height += layout_height;
if (grip->_priv->icon_pixbuf) {
requisition->width += gdk_pixbuf_get_width (grip->_priv->icon_pixbuf) + 1;
}
}
#define ELLIPSIS "..."
static void
ellipsize_layout (PangoLayout *layout, gint width)
{
PangoLayoutLine *line;
PangoLayout *ell;
gint h, w, ell_w, x;
GString *text;
if (width <= 0) {
pango_layout_set_text (layout, "", -1);
return;
}
pango_layout_get_pixel_size (layout, &w, &h);
if (w <= width) return;
gtk_widget_size_request (grip->_priv->label, &child_requisition);
requisition->width += child_requisition.width;
layout_height = MAX (layout_height, child_requisition.height);
/* calculate ellipsis width */
ell = pango_layout_copy (layout);
pango_layout_set_text (ell, ELLIPSIS, -1);
pango_layout_get_pixel_size (ell, &ell_w, NULL);
g_object_unref (ell);
if (width < ell_w) {
/* not even ellipsis fits, so hide the text */
pango_layout_set_text (layout, "", -1);
return;
}
/* shrink total available width by the width of the ellipsis */
width -= ell_w;
line = pango_layout_get_line (layout, 0);
text = g_string_new (pango_layout_get_text (layout));
if (pango_layout_line_x_to_index (line, width * PANGO_SCALE, &x, NULL)) {
g_string_set_size (text, x);
g_string_append (text, ELLIPSIS);
pango_layout_set_text (layout, text->str, -1);
}
g_string_free (text, TRUE);
requisition->height += layout_height;
}
static void
......@@ -542,6 +385,7 @@ gdl_dock_item_grip_size_allocate (GtkWidget *widget,
GtkContainer *container;
GtkRequisition button_requisition = { 0, };
GtkAllocation child_allocation;
GdkRectangle label_area;
g_return_if_fail (GDL_IS_DOCK_ITEM_GRIP (widget));
g_return_if_fail (allocation != NULL);
......@@ -552,7 +396,7 @@ gdl_dock_item_grip_size_allocate (GtkWidget *widget,
GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
child_allocation.x = allocation->x + container->border_width + ALIGN_BORDER;
child_allocation.x = allocation->x + container->border_width/* + ALIGN_BORDER*/;
else
child_allocation.x = allocation->x + allocation->width - container->border_width;
child_allocation.y = allocation->y + container->border_width;
......@@ -587,23 +431,26 @@ gdl_dock_item_grip_size_allocate (GtkWidget *widget,
child_allocation.x += button_requisition.width;
}
if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL) {
child_allocation.width = child_allocation.x;
child_allocation.x = allocation->x + container->border_width/* + ALIGN_BORDER*/;
} else {
child_allocation.width = allocation->width -
(child_allocation.x - allocation->x)/* - ALIGN_BORDER*/;
}
child_allocation.y = allocation->y + container->border_width;
child_allocation.height = allocation->height - container->border_width * 2;
if(grip->_priv->label) {
gtk_widget_size_allocate (grip->_priv->label, &child_allocation);
}
if (grip->title_window) {
GdkRectangle area;
/* set layout text */
ensure_title_and_icon_pixbuf (grip);
pango_layout_set_text (grip->_priv->title_layout, grip->_priv->title, -1);
gdl_dock_item_grip_get_title_area (grip, &area);
gdk_window_move_resize (grip->title_window,
area.x, area.y, area.width, area.height);
if (grip->_priv->icon_pixbuf)
area.width -= gdk_pixbuf_get_width (grip->_priv->icon_pixbuf) + 1;
/* ellipsize title if it doesn't fit the title area */
ellipsize_layout (grip->_priv->title_layout, area.width);
allocation->x,
allocation->y,
allocation->width,
allocation->height);
}
}
......@@ -630,12 +477,17 @@ gdl_dock_item_grip_forall (GtkContainer *container,
GdlDockItemGrip *grip;
g_return_if_fail (GDL_IS_DOCK_ITEM_GRIP (container));
grip = GDL_DOCK_ITEM_GRIP (container);
if (grip->_priv) {
if(grip->_priv->label) {
(* callback) (grip->_priv->label, callback_data);
}
if (include_internals) {
(* callback) (grip->_priv->close_button, callback_data);
(* callback) (grip->_priv->iconify_button, callback_data);
if (include_internals) {
(* callback) (grip->_priv->close_button, callback_data);
(* callback) (grip->_priv->iconify_button, callback_data);
}
}
}
......@@ -663,7 +515,6 @@ gdl_dock_item_grip_class_init (GdlDockItemGripClass *klass)
gtk_object_class->destroy = gdl_dock_item_grip_destroy;
widget_class->expose_event = gdl_dock_item_grip_expose;
widget_class->realize = gdl_dock_item_grip_realize;
widget_class->unrealize = gdl_dock_item_grip_unrealize;
widget_class->map = gdl_dock_item_grip_map;
......@@ -704,3 +555,30 @@ gdl_dock_item_grip_new (GdlDockItem *item)
return GTK_WIDGET (grip);
}
/**
* gdl_dock_item_grip_set_label:
* @grip: The grip that will get it's label widget set.
* @label: The widget that will become the label.
*
* Replaces the current label widget with another widget.
**/
void
gdl_dock_item_grip_set_label (GdlDockItemGrip *grip,
GtkWidget *label)
{
g_return_if_fail (grip != NULL);
if (grip->_priv->label) {
gtk_widget_unparent(grip->_priv->label);
gtk_widget_unref (grip->_priv->label);
grip->_priv->label = NULL;
}
if (label) {
gtk_widget_ref (label);
gtk_widget_set_parent (label, GTK_WIDGET (grip));
gtk_widget_show (label);
grip->_priv->label = label;
}
}
......@@ -50,6 +50,8 @@ struct _GdlDockItemGripClass {
GType gdl_dock_item_grip_get_type (void);
GtkWidget *gdl_dock_item_grip_new (GdlDockItem *item);
void gdl_dock_item_grip_set_label (GdlDockItemGrip *grip,
GtkWidget *label);
G_END_DECLS
......
......@@ -1789,6 +1789,23 @@ gdl_dock_item_set_tablabel (GdlDockItem *item,
}
}
/**
* gdl_dock_item_get_grip:
* @item: The dock item from which to to get the grip of.
*
* This function returns the dock item's grip label widget.
*
* Returns: Returns the current label widget.
**/
GtkWidget *
gdl_dock_item_get_grip(GdlDockItem *item)
{
g_return_if_fail (item != NULL);
g_return_val_if_fail (GDL_IS_DOCK_ITEM (item), NULL);
return item->_priv->grip;
}
/**
* gdl_dock_item_hide_grip:
* @item: The dock item to hide the grip of.
......
......@@ -156,6 +156,7 @@ void gdl_dock_item_set_orientation (GdlDockItem *item,
GtkWidget *gdl_dock_item_get_tablabel (GdlDockItem *item);
void gdl_dock_item_set_tablabel (GdlDockItem *item,
GtkWidget *tablabel);
GtkWidget *gdl_dock_item_get_grip (GdlDockItem *item);
void gdl_dock_item_hide_grip (GdlDockItem *item);
void gdl_dock_item_show_grip (GdlDockItem *item);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment