]> Pileus Git - ~andy/gtk/blob - gtk/gtklistitem.c
Initial revision
[~andy/gtk] / gtk / gtklistitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include "gtklabel.h"
19 #include "gtklistitem.h"
20 #include "gtklist.h"
21
22 static void gtk_list_item_class_init    (GtkListItemClass *klass);
23 static void gtk_list_item_init          (GtkListItem      *list_item);
24 static void gtk_list_item_realize       (GtkWidget        *widget);
25 static void gtk_list_item_size_request  (GtkWidget        *widget,
26                                          GtkRequisition   *requisition);
27 static void gtk_list_item_size_allocate (GtkWidget        *widget,
28                                          GtkAllocation    *allocation);
29 static void gtk_list_item_draw          (GtkWidget        *widget,
30                                          GdkRectangle     *area);
31 static void gtk_list_item_draw_focus    (GtkWidget        *widget);
32 static gint gtk_list_item_button_press  (GtkWidget        *widget,
33                                          GdkEventButton   *event);
34 static gint gtk_list_item_expose        (GtkWidget        *widget,
35                                          GdkEventExpose   *event);
36 static gint gtk_list_item_focus_in      (GtkWidget        *widget,
37                                          GdkEventFocus    *event);
38 static gint gtk_list_item_focus_out     (GtkWidget        *widget,
39                                          GdkEventFocus    *event);
40 static void gtk_real_list_item_select   (GtkItem          *item);
41 static void gtk_real_list_item_deselect (GtkItem          *item);
42 static void gtk_real_list_item_toggle   (GtkItem          *item);
43
44
45 static GtkItemClass *parent_class = NULL;
46
47
48 guint
49 gtk_list_item_get_type ()
50 {
51   static guint list_item_type = 0;
52
53   if (!list_item_type)
54     {
55       GtkTypeInfo list_item_info =
56       {
57         "GtkListItem",
58         sizeof (GtkListItem),
59         sizeof (GtkListItemClass),
60         (GtkClassInitFunc) gtk_list_item_class_init,
61         (GtkObjectInitFunc) gtk_list_item_init,
62         (GtkArgFunc) NULL,
63       };
64
65       list_item_type = gtk_type_unique (gtk_item_get_type (), &list_item_info);
66     }
67
68   return list_item_type;
69 }
70
71 static void
72 gtk_list_item_class_init (GtkListItemClass *class)
73 {
74   GtkWidgetClass *widget_class;
75   GtkItemClass *item_class;
76
77   widget_class = (GtkWidgetClass*) class;
78   item_class = (GtkItemClass*) class;
79
80   parent_class = gtk_type_class (gtk_item_get_type ());
81
82   widget_class->realize = gtk_list_item_realize;
83   widget_class->size_request = gtk_list_item_size_request;
84   widget_class->size_allocate = gtk_list_item_size_allocate;
85   widget_class->draw = gtk_list_item_draw;
86   widget_class->draw_focus = gtk_list_item_draw_focus;
87   widget_class->button_press_event = gtk_list_item_button_press;
88   widget_class->expose_event = gtk_list_item_expose;
89   widget_class->focus_in_event = gtk_list_item_focus_in;
90   widget_class->focus_out_event = gtk_list_item_focus_out;
91
92   item_class->select = gtk_real_list_item_select;
93   item_class->deselect = gtk_real_list_item_deselect;
94   item_class->toggle = gtk_real_list_item_toggle;
95 }
96
97 static void
98 gtk_list_item_init (GtkListItem *list_item)
99 {
100   GTK_WIDGET_SET_FLAGS (list_item, GTK_CAN_FOCUS);
101 }
102
103 GtkWidget*
104 gtk_list_item_new ()
105 {
106   return GTK_WIDGET (gtk_type_new (gtk_list_item_get_type ()));
107 }
108
109 GtkWidget*
110 gtk_list_item_new_with_label (const gchar *label)
111 {
112   GtkWidget *list_item;
113   GtkWidget *label_widget;
114
115   list_item = gtk_list_item_new ();
116   label_widget = gtk_label_new (label);
117   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
118
119   gtk_container_add (GTK_CONTAINER (list_item), label_widget);
120   gtk_widget_show (label_widget);
121
122   return list_item;
123 }
124
125 void
126 gtk_list_item_select (GtkListItem *list_item)
127 {
128   gtk_item_select (GTK_ITEM (list_item));
129 }
130
131 void
132 gtk_list_item_deselect (GtkListItem *list_item)
133 {
134   gtk_item_deselect (GTK_ITEM (list_item));
135 }
136
137
138 static void
139 gtk_list_item_realize (GtkWidget *widget)
140 {
141   g_return_if_fail (widget != NULL);
142   g_return_if_fail (GTK_IS_LIST_ITEM (widget));
143
144   if (GTK_WIDGET_CLASS (parent_class)->realize)
145     (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);
146
147   gdk_window_set_background (widget->window, &widget->style->white);
148 }
149
150 static void
151 gtk_list_item_size_request (GtkWidget      *widget,
152                             GtkRequisition *requisition)
153 {
154   GtkBin *bin;
155
156   g_return_if_fail (widget != NULL);
157   g_return_if_fail (GTK_IS_LIST_ITEM (widget));
158   g_return_if_fail (requisition != NULL);
159
160   bin = GTK_BIN (widget);
161
162   requisition->width = (GTK_CONTAINER (widget)->border_width +
163                         widget->style->klass->xthickness) * 2;
164   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
165
166   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
167     {
168       gtk_widget_size_request (bin->child, &bin->child->requisition);
169
170       requisition->width += bin->child->requisition.width;
171       requisition->height += bin->child->requisition.height;
172     }
173 }
174
175 static void
176 gtk_list_item_size_allocate (GtkWidget     *widget,
177                              GtkAllocation *allocation)
178 {
179   GtkBin *bin;
180   GtkAllocation child_allocation;
181
182   g_return_if_fail (widget != NULL);
183   g_return_if_fail (GTK_IS_LIST_ITEM (widget));
184   g_return_if_fail (allocation != NULL);
185
186   widget->allocation = *allocation;
187   if (GTK_WIDGET_REALIZED (widget))
188     gdk_window_move_resize (widget->window,
189                             allocation->x, allocation->y,
190                             allocation->width, allocation->height);
191
192   bin = GTK_BIN (widget);
193
194   if (bin->child)
195     {
196       child_allocation.x = (GTK_CONTAINER (widget)->border_width +
197                             widget->style->klass->xthickness);
198       child_allocation.y = GTK_CONTAINER (widget)->border_width;
199       child_allocation.width = allocation->width - child_allocation.x * 2;
200       child_allocation.height = allocation->height - child_allocation.y * 2;
201
202       gtk_widget_size_allocate (bin->child, &child_allocation);
203     }
204 }
205
206 static void
207 gtk_list_item_draw (GtkWidget    *widget,
208                     GdkRectangle *area)
209 {
210   GtkBin *bin;
211   GdkRectangle child_area;
212
213   g_return_if_fail (widget != NULL);
214   g_return_if_fail (GTK_IS_LIST_ITEM (widget));
215   g_return_if_fail (area != NULL);
216
217   if (GTK_WIDGET_DRAWABLE (widget))
218     {
219       bin = GTK_BIN (widget);
220
221       if (!GTK_WIDGET_IS_SENSITIVE (widget))
222         gtk_style_set_background (widget->style, widget->window, GTK_STATE_INSENSITIVE);
223       else if (widget->state == GTK_STATE_NORMAL)
224         gdk_window_set_background (widget->window, &widget->style->white);
225       else
226         gtk_style_set_background (widget->style, widget->window, widget->state);
227
228       gdk_window_clear_area (widget->window, area->x, area->y,
229                              area->width, area->height);
230
231       if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
232         gtk_widget_draw (bin->child, &child_area);
233
234       gtk_widget_draw_focus (widget);
235     }
236 }
237
238 static void
239 gtk_list_item_draw_focus (GtkWidget *widget)
240 {
241   GdkGC *gc;
242
243   g_return_if_fail (widget != NULL);
244   g_return_if_fail (GTK_IS_LIST_ITEM (widget));
245
246   if (GTK_WIDGET_DRAWABLE (widget))
247     {
248       if (GTK_WIDGET_HAS_FOCUS (widget))
249         gc = widget->style->black_gc;
250       else if (!GTK_WIDGET_IS_SENSITIVE (widget))
251         gc = widget->style->bg_gc[GTK_STATE_INSENSITIVE];
252       else if (widget->state == GTK_STATE_NORMAL)
253         gc = widget->style->white_gc;
254       else
255         gc = widget->style->bg_gc[widget->state];
256
257       gdk_draw_rectangle (widget->window, gc, FALSE, 0, 0,
258                           widget->allocation.width - 1,
259                           widget->allocation.height - 1);
260     }
261 }
262
263 static gint
264 gtk_list_item_button_press (GtkWidget      *widget,
265                             GdkEventButton *event)
266 {
267   g_return_val_if_fail (widget != NULL, FALSE);
268   g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
269   g_return_val_if_fail (event != NULL, FALSE);
270
271   if (event->type == GDK_BUTTON_PRESS)
272     if (!GTK_WIDGET_HAS_FOCUS (widget))
273       gtk_widget_grab_focus (widget);
274
275   return FALSE;
276 }
277
278 static gint
279 gtk_list_item_expose (GtkWidget      *widget,
280                       GdkEventExpose *event)
281 {
282   GtkBin *bin;
283   GdkEventExpose child_event;
284
285   g_return_val_if_fail (widget != NULL, FALSE);
286   g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
287   g_return_val_if_fail (event != NULL, FALSE);
288
289   if (GTK_WIDGET_DRAWABLE (widget))
290     {
291       bin = GTK_BIN (widget);
292
293       if (!GTK_WIDGET_IS_SENSITIVE (widget))
294         gdk_window_set_background (widget->window, &widget->style->bg[GTK_STATE_INSENSITIVE]);
295       else if (widget->state == GTK_STATE_NORMAL)
296         gdk_window_set_background (widget->window, &widget->style->white);
297       else
298         gdk_window_set_background (widget->window, &widget->style->bg[widget->state]);
299
300       gdk_window_clear_area (widget->window, event->area.x, event->area.y,
301                              event->area.width, event->area.height);
302
303       if (bin->child)
304         {
305           child_event = *event;
306
307           if (GTK_WIDGET_NO_WINDOW (bin->child) &&
308               gtk_widget_intersect (bin->child, &event->area, &child_event.area))
309             gtk_widget_event (bin->child, (GdkEvent*) &child_event);
310         }
311
312       gtk_widget_draw_focus (widget);
313     }
314
315   return FALSE;
316 }
317
318 static gint
319 gtk_list_item_focus_in (GtkWidget     *widget,
320                         GdkEventFocus *event)
321 {
322   g_return_val_if_fail (widget != NULL, FALSE);
323   g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
324   g_return_val_if_fail (event != NULL, FALSE);
325
326   GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
327   gtk_widget_draw_focus (widget);
328
329   return FALSE;
330 }
331
332 static gint
333 gtk_list_item_focus_out (GtkWidget     *widget,
334                          GdkEventFocus *event)
335 {
336   g_return_val_if_fail (widget != NULL, FALSE);
337   g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
338   g_return_val_if_fail (event != NULL, FALSE);
339
340   GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
341   gtk_widget_draw_focus (widget);
342
343   return FALSE;
344 }
345
346 static void
347 gtk_real_list_item_select (GtkItem *item)
348 {
349   g_return_if_fail (item != NULL);
350   g_return_if_fail (GTK_IS_LIST_ITEM (item));
351
352   if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
353     return;
354
355   gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
356   gtk_widget_queue_draw (GTK_WIDGET (item));
357 }
358
359 static void
360 gtk_real_list_item_deselect (GtkItem *item)
361 {
362   g_return_if_fail (item != NULL);
363   g_return_if_fail (GTK_IS_LIST_ITEM (item));
364
365   if (GTK_WIDGET (item)->state == GTK_STATE_NORMAL)
366     return;
367
368   gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
369   gtk_widget_queue_draw (GTK_WIDGET (item));
370 }
371
372 static void
373 gtk_real_list_item_toggle (GtkItem *item)
374 {
375   g_return_if_fail (item != NULL);
376   g_return_if_fail (GTK_IS_LIST_ITEM (item));
377   
378   if (GTK_WIDGET (item)->parent && GTK_IS_LIST (GTK_WIDGET (item)->parent))
379     gtk_list_select_child (GTK_LIST (GTK_WIDGET (item)->parent),
380                            GTK_WIDGET (item));
381   else
382     {
383       /* Should we really bother with this bit? A listitem not in a list?
384        * -Johannes Keukelaar
385        * yes, always be on the save side!
386        * -timj
387        */
388       if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
389         gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
390       else
391         gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
392       gtk_widget_queue_draw (GTK_WIDGET (item));
393     }
394 }