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,
66 list_item_type = gtk_type_unique (gtk_item_get_type (), &list_item_info);
69 return list_item_type;
73 gtk_list_item_class_init (GtkListItemClass *class)
75 GtkWidgetClass *widget_class;
76 GtkItemClass *item_class;
78 widget_class = (GtkWidgetClass*) class;
79 item_class = (GtkItemClass*) class;
81 parent_class = gtk_type_class (gtk_item_get_type ());
83 widget_class->realize = gtk_list_item_realize;
84 widget_class->size_request = gtk_list_item_size_request;
85 widget_class->size_allocate = gtk_list_item_size_allocate;
86 widget_class->draw = gtk_list_item_draw;
87 widget_class->draw_focus = gtk_list_item_draw_focus;
88 widget_class->button_press_event = gtk_list_item_button_press;
89 widget_class->expose_event = gtk_list_item_expose;
90 widget_class->focus_in_event = gtk_list_item_focus_in;
91 widget_class->focus_out_event = gtk_list_item_focus_out;
93 item_class->select = gtk_real_list_item_select;
94 item_class->deselect = gtk_real_list_item_deselect;
95 item_class->toggle = gtk_real_list_item_toggle;
99 gtk_list_item_init (GtkListItem *list_item)
101 GTK_WIDGET_SET_FLAGS (list_item, GTK_CAN_FOCUS);
107 return GTK_WIDGET (gtk_type_new (gtk_list_item_get_type ()));
111 gtk_list_item_new_with_label (const gchar *label)
113 GtkWidget *list_item;
114 GtkWidget *label_widget;
116 list_item = gtk_list_item_new ();
117 label_widget = gtk_label_new (label);
118 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
120 gtk_container_add (GTK_CONTAINER (list_item), label_widget);
121 gtk_widget_show (label_widget);
127 gtk_list_item_select (GtkListItem *list_item)
129 gtk_item_select (GTK_ITEM (list_item));
133 gtk_list_item_deselect (GtkListItem *list_item)
135 gtk_item_deselect (GTK_ITEM (list_item));
140 gtk_list_item_realize (GtkWidget *widget)
142 g_return_if_fail (widget != NULL);
143 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
145 if (GTK_WIDGET_CLASS (parent_class)->realize)
146 (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);
148 gdk_window_set_background (widget->window, &widget->style->white);
152 gtk_list_item_size_request (GtkWidget *widget,
153 GtkRequisition *requisition)
157 g_return_if_fail (widget != NULL);
158 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
159 g_return_if_fail (requisition != NULL);
161 bin = GTK_BIN (widget);
163 requisition->width = (GTK_CONTAINER (widget)->border_width +
164 widget->style->klass->xthickness) * 2;
165 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
167 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
169 gtk_widget_size_request (bin->child, &bin->child->requisition);
171 requisition->width += bin->child->requisition.width;
172 requisition->height += bin->child->requisition.height;
177 gtk_list_item_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation)
181 GtkAllocation child_allocation;
183 g_return_if_fail (widget != NULL);
184 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
185 g_return_if_fail (allocation != NULL);
187 widget->allocation = *allocation;
188 if (GTK_WIDGET_REALIZED (widget))
189 gdk_window_move_resize (widget->window,
190 allocation->x, allocation->y,
191 allocation->width, allocation->height);
193 bin = GTK_BIN (widget);
197 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
198 widget->style->klass->xthickness);
199 child_allocation.y = GTK_CONTAINER (widget)->border_width;
200 child_allocation.width = allocation->width - child_allocation.x * 2;
201 child_allocation.height = allocation->height - child_allocation.y * 2;
203 gtk_widget_size_allocate (bin->child, &child_allocation);
208 gtk_list_item_draw (GtkWidget *widget,
212 GdkRectangle child_area;
214 g_return_if_fail (widget != NULL);
215 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
216 g_return_if_fail (area != NULL);
218 if (GTK_WIDGET_DRAWABLE (widget))
220 bin = GTK_BIN (widget);
222 if (!GTK_WIDGET_IS_SENSITIVE (widget))
223 gtk_style_set_background (widget->style, widget->window, GTK_STATE_INSENSITIVE);
224 else if (widget->state == GTK_STATE_NORMAL)
225 gdk_window_set_background (widget->window, &widget->style->white);
227 gtk_style_set_background (widget->style, widget->window, widget->state);
229 gdk_window_clear_area (widget->window, area->x, area->y,
230 area->width, area->height);
232 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
233 gtk_widget_draw (bin->child, &child_area);
235 gtk_widget_draw_focus (widget);
240 gtk_list_item_draw_focus (GtkWidget *widget)
244 g_return_if_fail (widget != NULL);
245 g_return_if_fail (GTK_IS_LIST_ITEM (widget));
247 if (GTK_WIDGET_DRAWABLE (widget))
249 if (GTK_WIDGET_HAS_FOCUS (widget))
250 gc = widget->style->black_gc;
251 else if (!GTK_WIDGET_IS_SENSITIVE (widget))
252 gc = widget->style->bg_gc[GTK_STATE_INSENSITIVE];
253 else if (widget->state == GTK_STATE_NORMAL)
254 gc = widget->style->white_gc;
256 gc = widget->style->bg_gc[widget->state];
258 gdk_draw_rectangle (widget->window, gc, FALSE, 0, 0,
259 widget->allocation.width - 1,
260 widget->allocation.height - 1);
265 gtk_list_item_button_press (GtkWidget *widget,
266 GdkEventButton *event)
268 g_return_val_if_fail (widget != NULL, FALSE);
269 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
270 g_return_val_if_fail (event != NULL, FALSE);
272 if (event->type == GDK_BUTTON_PRESS)
273 if (!GTK_WIDGET_HAS_FOCUS (widget))
274 gtk_widget_grab_focus (widget);
280 gtk_list_item_expose (GtkWidget *widget,
281 GdkEventExpose *event)
284 GdkEventExpose child_event;
286 g_return_val_if_fail (widget != NULL, FALSE);
287 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
288 g_return_val_if_fail (event != NULL, FALSE);
290 if (GTK_WIDGET_DRAWABLE (widget))
292 bin = GTK_BIN (widget);
294 if (!GTK_WIDGET_IS_SENSITIVE (widget))
295 gdk_window_set_background (widget->window, &widget->style->bg[GTK_STATE_INSENSITIVE]);
296 else if (widget->state == GTK_STATE_NORMAL)
297 gdk_window_set_background (widget->window, &widget->style->white);
299 gdk_window_set_background (widget->window, &widget->style->bg[widget->state]);
301 gdk_window_clear_area (widget->window, event->area.x, event->area.y,
302 event->area.width, event->area.height);
306 child_event = *event;
308 if (GTK_WIDGET_NO_WINDOW (bin->child) &&
309 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
310 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
313 gtk_widget_draw_focus (widget);
320 gtk_list_item_focus_in (GtkWidget *widget,
321 GdkEventFocus *event)
323 g_return_val_if_fail (widget != NULL, FALSE);
324 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
325 g_return_val_if_fail (event != NULL, FALSE);
327 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
328 gtk_widget_draw_focus (widget);
334 gtk_list_item_focus_out (GtkWidget *widget,
335 GdkEventFocus *event)
337 g_return_val_if_fail (widget != NULL, FALSE);
338 g_return_val_if_fail (GTK_IS_LIST_ITEM (widget), FALSE);
339 g_return_val_if_fail (event != NULL, FALSE);
341 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
342 gtk_widget_draw_focus (widget);
348 gtk_real_list_item_select (GtkItem *item)
350 g_return_if_fail (item != NULL);
351 g_return_if_fail (GTK_IS_LIST_ITEM (item));
353 if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
356 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
357 gtk_widget_queue_draw (GTK_WIDGET (item));
361 gtk_real_list_item_deselect (GtkItem *item)
363 g_return_if_fail (item != NULL);
364 g_return_if_fail (GTK_IS_LIST_ITEM (item));
366 if (GTK_WIDGET (item)->state == GTK_STATE_NORMAL)
369 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
370 gtk_widget_queue_draw (GTK_WIDGET (item));
374 gtk_real_list_item_toggle (GtkItem *item)
376 g_return_if_fail (item != NULL);
377 g_return_if_fail (GTK_IS_LIST_ITEM (item));
379 if (GTK_WIDGET (item)->parent && GTK_IS_LIST (GTK_WIDGET (item)->parent))
380 gtk_list_select_child (GTK_LIST (GTK_WIDGET (item)->parent),
384 /* Should we really bother with this bit? A listitem not in a list?
385 * -Johannes Keukelaar
386 * yes, always be on the save side!
389 if (GTK_WIDGET (item)->state == GTK_STATE_SELECTED)
390 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_NORMAL);
392 gtk_widget_set_state (GTK_WIDGET (item), GTK_STATE_SELECTED);
393 gtk_widget_queue_draw (GTK_WIDGET (item));