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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gtklistitem.h"
23 static void gtk_list_item_class_init (GtkListItemClass *klass);
24 static void gtk_list_item_init (GtkListItem *list_item);
25 static void gtk_list_item_realize (GtkWidget *widget);
26 static void gtk_list_item_size_request (GtkWidget *widget,
27 GtkRequisition *requisition);
28 static void gtk_list_item_size_allocate (GtkWidget *widget,
29 GtkAllocation *allocation);
30 static void gtk_list_item_draw (GtkWidget *widget,
32 static void gtk_list_item_draw_focus (GtkWidget *widget);
33 static gint gtk_list_item_button_press (GtkWidget *widget,
34 GdkEventButton *event);
35 static gint gtk_list_item_expose (GtkWidget *widget,
36 GdkEventExpose *event);
37 static gint gtk_list_item_focus_in (GtkWidget *widget,
38 GdkEventFocus *event);
39 static gint gtk_list_item_focus_out (GtkWidget *widget,
40 GdkEventFocus *event);
41 static void gtk_real_list_item_select (GtkItem *item);
42 static void gtk_real_list_item_deselect (GtkItem *item);
43 static void gtk_real_list_item_toggle (GtkItem *item);
46 static GtkItemClass *parent_class = NULL;
50 gtk_list_item_get_type (void)
52 static GtkType list_item_type = 0;
56 GtkTypeInfo list_item_info =
60 sizeof (GtkListItemClass),
61 (GtkClassInitFunc) gtk_list_item_class_init,
62 (GtkObjectInitFunc) gtk_list_item_init,
67 list_item_type = gtk_type_unique (gtk_item_get_type (), &list_item_info);
70 return list_item_type;
74 gtk_list_item_class_init (GtkListItemClass *class)
76 GtkWidgetClass *widget_class;
77 GtkItemClass *item_class;
79 widget_class = (GtkWidgetClass*) class;
80 item_class = (GtkItemClass*) class;
82 parent_class = gtk_type_class (gtk_item_get_type ());
84 widget_class->realize = gtk_list_item_realize;
85 widget_class->size_request = gtk_list_item_size_request;
86 widget_class->size_allocate = gtk_list_item_size_allocate;
87 widget_class->draw = gtk_list_item_draw;
88 widget_class->draw_focus = gtk_list_item_draw_focus;
89 widget_class->button_press_event = gtk_list_item_button_press;
90 widget_class->expose_event = gtk_list_item_expose;
91 widget_class->focus_in_event = gtk_list_item_focus_in;
92 widget_class->focus_out_event = gtk_list_item_focus_out;
94 item_class->select = gtk_real_list_item_select;
95 item_class->deselect = gtk_real_list_item_deselect;
96 item_class->toggle = gtk_real_list_item_toggle;
100 gtk_list_item_init (GtkListItem *list_item)
102 GTK_WIDGET_SET_FLAGS (list_item, GTK_CAN_FOCUS);
106 gtk_list_item_new (void)
108 return GTK_WIDGET (gtk_type_new (gtk_list_item_get_type ()));
112 gtk_list_item_new_with_label (const gchar *label)
114 GtkWidget *list_item;
115 GtkWidget *label_widget;
117 list_item = gtk_list_item_new ();
118 label_widget = gtk_label_new (label);
119 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
121 gtk_container_add (GTK_CONTAINER (list_item), label_widget);
122 gtk_widget_show (label_widget);
128 gtk_list_item_select (GtkListItem *list_item)
130 gtk_item_select (GTK_ITEM (list_item));
134 gtk_list_item_deselect (GtkListItem *list_item)
136 gtk_item_deselect (GTK_ITEM (list_item));
141 gtk_list_item_realize (GtkWidget *widget)
143 g_return_if_fail (widget != NULL);
144 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
146 if (GTK_WIDGET_CLASS (parent_class)->realize)
147 (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);
149 gdk_window_set_background (widget->window,
150 &widget->style->base[GTK_STATE_NORMAL]);
154 gtk_list_item_size_request (GtkWidget *widget,
155 GtkRequisition *requisition)
159 g_return_if_fail (widget != NULL);
160 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
161 g_return_if_fail (requisition != NULL);
163 bin = GTK_BIN (widget);
165 requisition->width = (GTK_CONTAINER (widget)->border_width +
166 widget->style->klass->xthickness) * 2;
167 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
169 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
171 gtk_widget_size_request (bin->child, &bin->child->requisition);
173 requisition->width += bin->child->requisition.width;
174 requisition->height += bin->child->requisition.height;
179 gtk_list_item_size_allocate (GtkWidget *widget,
180 GtkAllocation *allocation)
183 GtkAllocation child_allocation;
185 g_return_if_fail (widget != NULL);
186 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
187 g_return_if_fail (allocation != NULL);
189 widget->allocation = *allocation;
190 if (GTK_WIDGET_REALIZED (widget))
191 gdk_window_move_resize (widget->window,
192 allocation->x, allocation->y,
193 allocation->width, allocation->height);
195 bin = GTK_BIN (widget);
199 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
200 widget->style->klass->xthickness);
201 child_allocation.y = GTK_CONTAINER (widget)->border_width;
202 child_allocation.width = allocation->width - child_allocation.x * 2;
203 child_allocation.height = allocation->height - child_allocation.y * 2;
205 gtk_widget_size_allocate (bin->child, &child_allocation);
210 gtk_list_item_draw (GtkWidget *widget,
214 GdkRectangle child_area;
216 g_return_if_fail (widget != NULL);
217 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
218 g_return_if_fail (area != NULL);
220 if (GTK_WIDGET_DRAWABLE (widget))
222 bin = GTK_BIN (widget);
224 if (!GTK_WIDGET_IS_SENSITIVE (widget))
225 gtk_style_set_background (widget->style, widget->window, GTK_STATE_INSENSITIVE);
226 else if (widget->state == GTK_STATE_NORMAL)
227 gdk_window_set_background (widget->window,
228 &widget->style->base[GTK_STATE_NORMAL]);
230 gtk_style_set_background (widget->style, widget->window, widget->state);
232 gdk_window_clear_area (widget->window, area->x, area->y,
233 area->width, area->height);
235 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
236 gtk_widget_draw (bin->child, &child_area);
238 gtk_widget_draw_focus (widget);
243 gtk_list_item_draw_focus (GtkWidget *widget)
247 g_return_if_fail (widget != NULL);
248 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
250 if (GTK_WIDGET_DRAWABLE (widget))
252 if (GTK_WIDGET_HAS_FOCUS (widget))
253 gc = widget->style->black_gc;
254 else if (!GTK_WIDGET_IS_SENSITIVE (widget))
255 gc = widget->style->bg_gc[GTK_STATE_INSENSITIVE];
256 else if (widget->state == GTK_STATE_NORMAL)
257 gc = widget->style->base_gc[GTK_STATE_NORMAL];
259 gc = widget->style->bg_gc[widget->state];
261 gdk_draw_rectangle (widget->window, gc, FALSE, 0, 0,
262 widget->allocation.width - 1,
263 widget->allocation.height - 1);
268 gtk_list_item_button_press (GtkWidget *widget,
269 GdkEventButton *event)
271 g_return_val_if_fail (widget != NULL, FALSE);
272 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
273 g_return_val_if_fail (event != NULL, FALSE);
275 if (event->type == GDK_BUTTON_PRESS)
276 if (!GTK_WIDGET_HAS_FOCUS (widget))
277 gtk_widget_grab_focus (widget);
283 gtk_list_item_expose (GtkWidget *widget,
284 GdkEventExpose *event)
287 GdkEventExpose child_event;
289 g_return_val_if_fail (widget != NULL, FALSE);
290 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
291 g_return_val_if_fail (event != NULL, FALSE);
293 if (GTK_WIDGET_DRAWABLE (widget))
295 bin = GTK_BIN (widget);
297 if (!GTK_WIDGET_IS_SENSITIVE (widget))
298 gdk_window_set_background (widget->window, &widget->style->bg[GTK_STATE_INSENSITIVE]);
299 else if (widget->state == GTK_STATE_NORMAL)
300 gdk_window_set_background (widget->window, &widget->style->base[GTK_STATE_NORMAL]);
302 gdk_window_set_background (widget->window, &widget->style->bg[widget->state]);
304 gdk_window_clear_area (widget->window, event->area.x, event->area.y,
305 event->area.width, event->area.height);
309 child_event = *event;
311 if (GTK_WIDGET_NO_WINDOW (bin->child) &&
312 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
313 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
316 gtk_widget_draw_focus (widget);
323 gtk_list_item_focus_in (GtkWidget *widget,
324 GdkEventFocus *event)
326 g_return_val_if_fail (widget != NULL, FALSE);
327 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
328 g_return_val_if_fail (event != NULL, FALSE);
330 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
331 gtk_widget_draw_focus (widget);
337 gtk_list_item_focus_out (GtkWidget *widget,
338 GdkEventFocus *event)
340 g_return_val_if_fail (widget != NULL, FALSE);
341 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
342 g_return_val_if_fail (event != NULL, FALSE);
344 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
345 gtk_widget_draw_focus (widget);
351 gtk_real_list_item_select (GtkItem *item)
353 g_return_if_fail (item != NULL);
354 g_return_if_fail (GTK_IS_LIST_ITEM (item));
356 if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
359 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
360 gtk_widget_queue_draw (GTK_WIDGET (item));
364 gtk_real_list_item_deselect (GtkItem *item)
366 g_return_if_fail (item != NULL);
367 g_return_if_fail (GTK_IS_LIST_ITEM (item));
369 if (GTK_WIDGET (item)->state == GTK_STATE_NORMAL)
372 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
373 gtk_widget_queue_draw (GTK_WIDGET (item));
377 gtk_real_list_item_toggle (GtkItem *item)
379 g_return_if_fail (item != NULL);
380 g_return_if_fail (GTK_IS_LIST_ITEM (item));
382 if (GTK_WIDGET (item)->parent && GTK_IS_LIST (GTK_WIDGET (item)->parent))
383 gtk_list_select_child (GTK_LIST (GTK_WIDGET (item)->parent),
387 /* Should we really bother with this bit? A listitem not in a list?
388 * -Johannes Keukelaar
389 * yes, always be on the save side!
392 if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
393 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
395 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
396 gtk_widget_queue_draw (GTK_WIDGET (item));