1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
19 #include "gtklistitem.h"
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,
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);
45 static GtkItemClass *parent_class = NULL;
49 gtk_list_item_get_type ()
51 static guint list_item_type = 0;
55 GtkTypeInfo list_item_info =
59 sizeof (GtkListItemClass),
60 (GtkClassInitFunc) gtk_list_item_class_init,
61 (GtkObjectInitFunc) gtk_list_item_init,
65 list_item_type = gtk_type_unique (gtk_item_get_type (), &list_item_info);
68 return list_item_type;
72 gtk_list_item_class_init (GtkListItemClass *class)
74 GtkWidgetClass *widget_class;
75 GtkItemClass *item_class;
77 widget_class = (GtkWidgetClass*) class;
78 item_class = (GtkItemClass*) class;
80 parent_class = gtk_type_class (gtk_item_get_type ());
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;
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;
98 gtk_list_item_init (GtkListItem *list_item)
100 GTK_WIDGET_SET_FLAGS (list_item, GTK_CAN_FOCUS);
106 return GTK_WIDGET (gtk_type_new (gtk_list_item_get_type ()));
110 gtk_list_item_new_with_label (const gchar *label)
112 GtkWidget *list_item;
113 GtkWidget *label_widget;
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);
119 gtk_container_add (GTK_CONTAINER (list_item), label_widget);
120 gtk_widget_show (label_widget);
126 gtk_list_item_select (GtkListItem *list_item)
128 gtk_item_select (GTK_ITEM (list_item));
132 gtk_list_item_deselect (GtkListItem *list_item)
134 gtk_item_deselect (GTK_ITEM (list_item));
139 gtk_list_item_realize (GtkWidget *widget)
141 g_return_if_fail (widget != NULL);
142 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
144 if (GTK_WIDGET_CLASS (parent_class)->realize)
145 (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);
147 gdk_window_set_background (widget->window, &widget->style->white);
151 gtk_list_item_size_request (GtkWidget *widget,
152 GtkRequisition *requisition)
156 g_return_if_fail (widget != NULL);
157 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
158 g_return_if_fail (requisition != NULL);
160 bin = GTK_BIN (widget);
162 requisition->width = (GTK_CONTAINER (widget)->border_width +
163 widget->style->klass->xthickness) * 2;
164 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
166 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
168 gtk_widget_size_request (bin->child, &bin->child->requisition);
170 requisition->width += bin->child->requisition.width;
171 requisition->height += bin->child->requisition.height;
176 gtk_list_item_size_allocate (GtkWidget *widget,
177 GtkAllocation *allocation)
180 GtkAllocation child_allocation;
182 g_return_if_fail (widget != NULL);
183 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
184 g_return_if_fail (allocation != NULL);
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);
192 bin = GTK_BIN (widget);
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;
202 gtk_widget_size_allocate (bin->child, &child_allocation);
207 gtk_list_item_draw (GtkWidget *widget,
211 GdkRectangle child_area;
213 g_return_if_fail (widget != NULL);
214 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
215 g_return_if_fail (area != NULL);
217 if (GTK_WIDGET_DRAWABLE (widget))
219 bin = GTK_BIN (widget);
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);
226 gtk_style_set_background (widget->style, widget->window, widget->state);
228 gdk_window_clear_area (widget->window, area->x, area->y,
229 area->width, area->height);
231 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
232 gtk_widget_draw (bin->child, &child_area);
234 gtk_widget_draw_focus (widget);
239 gtk_list_item_draw_focus (GtkWidget *widget)
243 g_return_if_fail (widget != NULL);
244 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
246 if (GTK_WIDGET_DRAWABLE (widget))
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;
255 gc = widget->style->bg_gc[widget->state];
257 gdk_draw_rectangle (widget->window, gc, FALSE, 0, 0,
258 widget->allocation.width - 1,
259 widget->allocation.height - 1);
264 gtk_list_item_button_press (GtkWidget *widget,
265 GdkEventButton *event)
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);
271 if (event->type == GDK_BUTTON_PRESS)
272 if (!GTK_WIDGET_HAS_FOCUS (widget))
273 gtk_widget_grab_focus (widget);
279 gtk_list_item_expose (GtkWidget *widget,
280 GdkEventExpose *event)
283 GdkEventExpose child_event;
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);
289 if (GTK_WIDGET_DRAWABLE (widget))
291 bin = GTK_BIN (widget);
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);
298 gdk_window_set_background (widget->window, &widget->style->bg[widget->state]);
300 gdk_window_clear_area (widget->window, event->area.x, event->area.y,
301 event->area.width, event->area.height);
305 child_event = *event;
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);
312 gtk_widget_draw_focus (widget);
319 gtk_list_item_focus_in (GtkWidget *widget,
320 GdkEventFocus *event)
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);
326 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
327 gtk_widget_draw_focus (widget);
333 gtk_list_item_focus_out (GtkWidget *widget,
334 GdkEventFocus *event)
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);
340 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
341 gtk_widget_draw_focus (widget);
347 gtk_real_list_item_select (GtkItem *item)
349 g_return_if_fail (item != NULL);
350 g_return_if_fail (GTK_IS_LIST_ITEM (item));
352 if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
355 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
356 gtk_widget_queue_draw (GTK_WIDGET (item));
360 gtk_real_list_item_deselect (GtkItem *item)
362 g_return_if_fail (item != NULL);
363 g_return_if_fail (GTK_IS_LIST_ITEM (item));
365 if (GTK_WIDGET (item)->state == GTK_STATE_NORMAL)
368 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
369 gtk_widget_queue_draw (GTK_WIDGET (item));
373 gtk_real_list_item_toggle (GtkItem *item)
375 g_return_if_fail (item != NULL);
376 g_return_if_fail (GTK_IS_LIST_ITEM (item));
378 if (GTK_WIDGET (item)->parent && GTK_IS_LIST (GTK_WIDGET (item)->parent))
379 gtk_list_select_child (GTK_LIST (GTK_WIDGET (item)->parent),
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!
388 if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
389 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
391 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
392 gtk_widget_queue_draw (GTK_WIDGET (item));