From 88055228d2cd8780d281eb31bf688170c23254ce Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Sat, 2 Jul 2011 10:55:29 +0000 Subject: the status bar is now a custom gtk widget it gives the left side of the status bar as much space as it needs (but importantly, no more than the size of the window. this should put the window resize bug to rest for good... --- src/status-bar.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/status-bar.c (limited to 'src/status-bar.c') diff --git a/src/status-bar.c b/src/status-bar.c new file mode 100644 index 0000000..6365505 --- /dev/null +++ b/src/status-bar.c @@ -0,0 +1,114 @@ +#include "status-bar.h" + +G_DEFINE_TYPE (UzblStatusBar, uzbl_status_bar, GTK_TYPE_HBOX) + +static void uzbl_status_bar_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); + +static void +uzbl_status_bar_class_init (UzblStatusBarClass *class) { + GtkWidgetClass *widget_class; + + widget_class = (GtkWidgetClass*) class; + + /* override the size_allocate method */ + widget_class->size_allocate = uzbl_status_bar_size_allocate; +} + +static void +uzbl_status_bar_init (UzblStatusBar *status_bar) { + gtk_box_set_homogeneous (GTK_BOX(status_bar), FALSE); + gtk_box_set_spacing (GTK_BOX(status_bar), 0); + + /* create left panel */ + status_bar->left_label = gtk_label_new (""); + gtk_label_set_selectable (GTK_LABEL(status_bar->left_label), TRUE); + gtk_misc_set_alignment (GTK_MISC(status_bar->left_label), 0, 0); + gtk_misc_set_padding (GTK_MISC(status_bar->left_label), 2, 2); + gtk_label_set_ellipsize(GTK_LABEL(status_bar->left_label), PANGO_ELLIPSIZE_END); + + /* create right panel */ + status_bar->right_label = gtk_label_new (""); + gtk_label_set_selectable(GTK_LABEL(status_bar->right_label), TRUE); + gtk_misc_set_alignment (GTK_MISC(status_bar->right_label), 1, 0); + gtk_misc_set_padding (GTK_MISC(status_bar->right_label), 2, 2); + gtk_label_set_ellipsize(GTK_LABEL(status_bar->right_label), PANGO_ELLIPSIZE_START); + + /* add the labels to the status bar */ + gtk_box_pack_start (GTK_BOX (status_bar), status_bar->left_label, FALSE, FALSE, 0); + gtk_box_pack_start (GTK_BOX (status_bar), status_bar->right_label, TRUE, TRUE, 0); +} + +static void +uzbl_status_bar_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) { + GtkRequisition left_requisition, right_requisition; + GtkAllocation left_allocation, right_allocation; + UzblStatusBar *status_bar = UZBL_STATUS_BAR(widget); + + int left_natural_width; + +#if GTK_CHECK_VERSION(3,0,0) + GtkRequisition left_requisition_nat; + + gtk_widget_get_preferred_size (status_bar->left_label, &left_requisition, &left_requisition_nat); + gtk_widget_get_preferred_size (status_bar->right_label, &right_requisition, NULL); + + left_natural_width = left_requisition_nat.width; +#else + gtk_widget_size_request(status_bar->left_label, &left_requisition); + gtk_widget_size_request(status_bar->left_label, &right_requisition); + + PangoLayout *left_layout = gtk_label_get_layout(GTK_LABEL(status_bar->left_label)); + pango_layout_get_pixel_size(left_layout, &left_natural_width, NULL); +#endif + + gtk_widget_set_allocation (widget, allocation); + + /* the entire allocation, minus the space needed for the right label's ellipsis */ + int left_max_width = allocation->width - right_requisition.width; + + /* the left label gets max(as much space as it needs, the status bar's allocation) */ + left_allocation.width = (left_max_width > left_natural_width) ? left_natural_width : left_max_width; + + /* the right label gets whatever is left over. it gets at least enough space + * for an ellipsis, it seems that it will just display everything if you give + * it 0. */ + right_allocation.width = allocation->width - left_allocation.width; + + /* don't fight guys, you can both have as much vertical space as you want! */ + left_allocation.height = right_allocation.height = allocation->height; + + left_allocation.x = 0; + right_allocation.x = left_allocation.width; + + left_allocation.y = right_allocation.y = allocation->y; + + gtk_widget_size_allocate (status_bar->left_label, &left_allocation); + gtk_widget_size_allocate (status_bar->right_label, &right_allocation); +} + +void +uzbl_status_bar_update_left(GtkWidget *widget, const gchar *format) { + UzblStatusBar *status_bar = UZBL_STATUS_BAR(widget); + + if(!format || !GTK_IS_LABEL(status_bar->left_label)) + return; + + gtk_label_set_markup(GTK_LABEL(status_bar->left_label), format); +} + +void +uzbl_status_bar_update_right(GtkWidget *widget, const gchar *format) { + UzblStatusBar *status_bar = UZBL_STATUS_BAR(widget); + + if(!format || !GTK_IS_LABEL(status_bar->right_label)) + return; + + gtk_label_set_markup(GTK_LABEL(status_bar->right_label), format); +} + +GtkWidget * +uzbl_status_bar_new() { + return g_object_new (UZBL_TYPE_STATUS_BAR, NULL); +} -- cgit v1.2.3