aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-task-bar.c
blob: c7a715decd6e86b05766455ec07bd3c07f0720b6 (plain) (blame)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Ettore Perazzoli <ettore@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-task-bar.h"

struct _ETaskBarPrivate
{
    GtkWidget *message_label;
    GtkHBox  *hbox;
};

/* WARNING: Ugly hack starts here.  */
#define MAX_ACTIVITIES_PER_COMPONENT 2

G_DEFINE_TYPE (ETaskBar, e_task_bar, GTK_TYPE_HBOX)

#if 0
static void
reduce_displayed_activities_per_component (ETaskBar *task_bar)
{
    GHashTable *component_ids_hash;
    GtkBox *box;
    GList *p;

    component_ids_hash = g_hash_table_new (g_str_hash, g_str_equal);

    box = GTK_BOX (task_bar->priv->hbox);

    for (p = box->children; p != NULL; p = p->next) {
        GtkBoxChild *child;
        const char *component_id;
        void *hash_item;

        child = (GtkBoxChild *) p->data;
        component_id = e_task_widget_get_component_id (E_TASK_WIDGET (child->widget));

        hash_item = g_hash_table_lookup (component_ids_hash, component_id);

        if (hash_item == NULL) {
            gtk_widget_show (child->widget);
            g_hash_table_insert (component_ids_hash, (void *) component_id, GINT_TO_POINTER (1));
        } else {
            int num_items;

            num_items = GPOINTER_TO_INT (hash_item);
            g_return_if_fail (num_items <= MAX_ACTIVITIES_PER_COMPONENT);

            if (num_items == MAX_ACTIVITIES_PER_COMPONENT) {
                gtk_widget_hide (child->widget);
            } else {
                num_items ++;
                gtk_widget_show (child->widget);
                g_hash_table_insert (component_ids_hash, (void *) component_id, GINT_TO_POINTER (num_items));
            }
        }
    }

    g_hash_table_destroy (component_ids_hash);
}
#endif


static void impl_finalize (GObject *object);

static void
e_task_bar_class_init (ETaskBarClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = impl_finalize;
}

static void
e_task_bar_init (ETaskBar *task_bar)
{
    GtkWidget *label, *hbox;
    gint height;

    task_bar->priv = g_new (ETaskBarPrivate, 1);

    gtk_box_set_spacing (GTK_BOX (task_bar), 10);

    label = gtk_label_new (NULL);
    gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
    gtk_box_pack_start (GTK_BOX (task_bar), label, TRUE, TRUE, 0);
    gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
    task_bar->priv->message_label = label;

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (task_bar), hbox);
    task_bar->priv->hbox = GTK_HBOX (hbox);

    /* Make the task bar large enough to accomodate a small icon.
     * XXX The "* 2" is a fudge factor to allow for some padding.
     *     The true value is probably buried in a style property. */
    gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, NULL, &height);
    gtk_widget_set_size_request (GTK_WIDGET (task_bar), -1, height * 2);
}

static void
impl_finalize (GObject *object)
{
    ETaskBar *task_bar;
    ETaskBarPrivate *priv;

    task_bar = E_TASK_BAR (object);
    priv = task_bar->priv;

    g_free (priv);

    (* G_OBJECT_CLASS (e_task_bar_parent_class)->finalize) (object);
}


void
e_task_bar_construct (ETaskBar *task_bar)
{
    g_return_if_fail (task_bar != NULL);
    g_return_if_fail (E_IS_TASK_BAR (task_bar));

    /* Nothing to do here.  */
}

GtkWidget *
e_task_bar_new (void)
{
    ETaskBar *task_bar;

    task_bar = g_object_new (e_task_bar_get_type (), NULL);
    e_task_bar_construct (task_bar);

    return GTK_WIDGET (task_bar);
}

void
e_task_bar_set_message (ETaskBar   *task_bar,
            const char *message)
{
    if (message) {
        gtk_label_set_text (
            GTK_LABEL (task_bar->priv->message_label), message);
        gtk_widget_show (task_bar->priv->message_label);
    } else {
        e_task_bar_unset_message (task_bar);
    }
}

void
e_task_bar_unset_message (ETaskBar   *task_bar)
{
    gtk_widget_hide (task_bar->priv->message_label);
}

void
e_task_bar_prepend_task (ETaskBar *task_bar,
             ETaskWidget *task_widget)
{
    GtkBoxChild *child_info;
    GtkBox *box;

    g_return_if_fail (task_bar != NULL);
    g_return_if_fail (E_IS_TASK_BAR (task_bar));
    g_return_if_fail (task_widget != NULL);
    g_return_if_fail (E_IS_TASK_WIDGET (task_widget));

    /* Hah hah.  GTK+ sucks.  This is adapted from `gtkhbox.c'.  */

    child_info = g_new (GtkBoxChild, 1);
    child_info->widget = GTK_WIDGET (task_widget);
    child_info->padding = 0;
    child_info->expand = TRUE;
    child_info->fill = TRUE;
    child_info->pack = GTK_PACK_START;

    box = GTK_BOX (task_bar->priv->hbox);

    box->children = g_list_prepend (box->children, child_info);

    gtk_widget_set_parent (GTK_WIDGET (task_widget), GTK_WIDGET (task_bar->priv->hbox));

    if (GTK_WIDGET_REALIZED (task_bar))
        gtk_widget_realize (GTK_WIDGET (task_widget));

    if (GTK_WIDGET_VISIBLE (task_bar) && GTK_WIDGET_VISIBLE (task_widget)) {
        if (GTK_WIDGET_MAPPED (task_bar))
            gtk_widget_map (GTK_WIDGET (task_widget));
        gtk_widget_queue_resize (GTK_WIDGET (task_widget));
    }

    /* We don't restrict */
    /* reduce_displayed_activities_per_component (task_bar);*/

    gtk_widget_show (GTK_WIDGET (task_bar->priv->hbox));
}

void
e_task_bar_remove_task_from_id (ETaskBar *task_bar,
            guint id)
{
    ETaskWidget *task_widget;

    g_return_if_fail (task_bar != NULL);
    g_return_if_fail (E_IS_TASK_BAR (task_bar));

    task_widget = e_task_bar_get_task_widget_from_id (task_bar, id);
    if (!task_widget) {
        printf("Failed...\n");
        return;
    }

    gtk_widget_destroy (GTK_WIDGET (task_widget));

    /* We don't restrict here on */
    /* reduce_displayed_activities_per_component (task_bar); */

    if (g_list_length (GTK_BOX (task_bar->priv->hbox)->children) == 0)
        gtk_widget_hide (GTK_WIDGET (task_bar->priv->hbox));
}

void
e_task_bar_remove_task (ETaskBar *task_bar,
            int n)
{
    ETaskWidget *task_widget;

    g_return_if_fail (task_bar != NULL);
    g_return_if_fail (E_IS_TASK_BAR (task_bar));
    g_return_if_fail (n >= 0);

    task_widget = e_task_bar_get_task_widget (task_bar, n);
    gtk_widget_destroy (GTK_WIDGET (task_widget));

    /* We don't restrict here on */
    /* reduce_displayed_activities_per_component (task_bar); */

    if (g_list_length (GTK_BOX (task_bar->priv->hbox)->children) == 0)
        gtk_widget_hide (GTK_WIDGET (task_bar->priv->hbox));
}

ETaskWidget *
e_task_bar_get_task_widget_from_id (ETaskBar *task_bar,
                guint id)
{
    GtkBoxChild *child_info;
    ETaskWidget *w = NULL;
    GList *list;

    g_return_val_if_fail (task_bar != NULL, NULL);
    g_return_val_if_fail (E_IS_TASK_BAR (task_bar), NULL);

    list = GTK_BOX (task_bar->priv->hbox)->children;
    while (list) {
        child_info = list->data;
        w = (ETaskWidget *) child_info->widget;
        if (w && w->id == id)
            break;

        w = NULL;
        list = list->next;
    }

    return w;
}

ETaskWidget *

e_task_bar_get_task_widget (ETaskBar *task_bar,
                int n)
{
    GtkBoxChild *child_info;

    g_return_val_if_fail (task_bar != NULL, NULL);
    g_return_val_if_fail (E_IS_TASK_BAR (task_bar), NULL);

    child_info = (GtkBoxChild *) g_list_nth (GTK_BOX (task_bar->priv->hbox)->children, n)->data;

    return E_TASK_WIDGET (child_info->widget);
}