aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/status-bar.c
blob: d80dd51ee634c82b561994c7b00e2a45f0ca992c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "status-bar.h"

G_DEFINE_TYPE (UzblStatusBar, uzbl_status_bar, GTK_TYPE_BOX)

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->right_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);

    /* some kind of fudge factor seems to be needed here */
    left_natural_width += 16;
#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);
}