comparison src/gtkticker.c @ 430:0cd2ba801eb4

[gaim-migrate @ 440] First pass at a buddy ticker. committer: Tailor Script <tailor@pidgin.im>
author Syd Logan <slogan>
date Sat, 24 Jun 2000 23:15:48 +0000
parents
children c61f9c384413
comparison
equal deleted inserted replaced
429:ccf7fc40263f 430:0cd2ba801eb4
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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * GtkTicker Copyright 2000 Syd Logan
22 */
23
24 #include "gtkticker.h"
25
26 static void gtk_ticker_compute_offsets (GtkTicker *ticker);
27 static void gtk_ticker_class_init (GtkTickerClass *klass);
28 static void gtk_ticker_init (GtkTicker *ticker);
29 static void gtk_ticker_map (GtkWidget *widget);
30 static void gtk_ticker_realize (GtkWidget *widget);
31 static void gtk_ticker_size_request (GtkWidget *widget,
32 GtkRequisition *requisition);
33 static void gtk_ticker_size_allocate (GtkWidget *widget,
34 GtkAllocation *allocation);
35 static void gtk_ticker_paint (GtkWidget *widget,
36 GdkRectangle *area);
37 static void gtk_ticker_draw (GtkWidget *widget,
38 GdkRectangle *area);
39 static gint gtk_ticker_expose (GtkWidget *widget,
40 GdkEventExpose *event);
41 static void gtk_ticker_add_real (GtkContainer *container,
42 GtkWidget *widget);
43 static void gtk_ticker_remove_real (GtkContainer *container,
44 GtkWidget *widget);
45 static void gtk_ticker_forall (GtkContainer *container,
46 gboolean include_internals,
47 GtkCallback callback,
48 gpointer callback_data);
49 static GtkType gtk_ticker_child_type (GtkContainer *container);
50
51
52 static GtkContainerClass *parent_class = NULL;
53
54
55 GtkType
56 gtk_ticker_get_type (void)
57 {
58 static GtkType ticker_type = 0;
59
60 if (!ticker_type)
61 {
62 static const GtkTypeInfo ticker_info =
63 {
64 "GtkTicker",
65 sizeof (GtkTicker),
66 sizeof (GtkTickerClass),
67 (GtkClassInitFunc) gtk_ticker_class_init,
68 (GtkObjectInitFunc) gtk_ticker_init,
69 /* reserved_1 */ NULL,
70 /* reserved_2 */ NULL,
71 (GtkClassInitFunc) NULL,
72 };
73
74 ticker_type = gtk_type_unique (GTK_TYPE_CONTAINER, &ticker_info);
75 }
76
77 return ticker_type;
78 }
79
80 static void
81 gtk_ticker_class_init (GtkTickerClass *class)
82 {
83 GtkObjectClass *object_class;
84 GtkWidgetClass *widget_class;
85 GtkContainerClass *container_class;
86
87 object_class = (GtkObjectClass*) class;
88 widget_class = (GtkWidgetClass*) class;
89 container_class = (GtkContainerClass*) class;
90
91 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
92
93 widget_class->map = gtk_ticker_map;
94 widget_class->realize = gtk_ticker_realize;
95 widget_class->size_request = gtk_ticker_size_request;
96 widget_class->size_allocate = gtk_ticker_size_allocate;
97 widget_class->draw = gtk_ticker_draw;
98 widget_class->expose_event = gtk_ticker_expose;
99
100 container_class->add = gtk_ticker_add_real;
101 container_class->remove = gtk_ticker_remove_real;
102 container_class->forall = gtk_ticker_forall;
103 container_class->child_type = gtk_ticker_child_type;
104 }
105
106 static GtkType
107 gtk_ticker_child_type (GtkContainer *container)
108 {
109 return GTK_TYPE_WIDGET;
110 }
111
112 static void
113 gtk_ticker_init (GtkTicker *ticker)
114 {
115 GTK_WIDGET_UNSET_FLAGS (ticker, GTK_NO_WINDOW);
116
117 ticker->interval = (guint) 200;
118 ticker->scootch = (guint) 2;
119 ticker->children = NULL;
120 ticker->timer = 0;
121 ticker->dirty = TRUE;
122 }
123
124 GtkWidget*
125 gtk_ticker_new (void)
126 {
127 GtkTicker *ticker;
128
129 ticker = gtk_type_new (GTK_TYPE_TICKER);
130 return GTK_WIDGET (ticker);
131 }
132
133 static void
134 gtk_ticker_put (GtkTicker *ticker,
135 GtkWidget *widget)
136 {
137 GtkTickerChild *child_info;
138
139 g_return_if_fail (ticker != NULL);
140 g_return_if_fail (GTK_IS_TICKER (ticker));
141 g_return_if_fail (widget != NULL);
142
143 child_info = g_new(GtkTickerChild, 1);
144 child_info->widget = widget;
145 child_info->x = 0;
146
147 gtk_widget_set_parent(widget, GTK_WIDGET (ticker));
148
149 ticker->children = g_list_append (ticker->children, child_info);
150
151 if (GTK_WIDGET_REALIZED (ticker))
152 gtk_widget_realize (widget);
153
154 if (GTK_WIDGET_VISIBLE (ticker) && GTK_WIDGET_VISIBLE (widget))
155 {
156 if (GTK_WIDGET_MAPPED (ticker))
157 gtk_widget_map (widget);
158
159 gtk_widget_queue_resize (GTK_WIDGET (ticker));
160 }
161 }
162
163 void
164 gtk_ticker_set_interval (GtkTicker *ticker, gint interval )
165 {
166 g_return_if_fail (ticker != NULL);
167 g_return_if_fail (GTK_IS_TICKER (ticker));
168
169 if ( interval < 0 )
170 interval = 200;
171 ticker->interval = interval;
172
173 }
174
175 guint
176 gtk_ticker_get_interval (GtkTicker *ticker )
177 {
178 g_return_val_if_fail (ticker != NULL, -1);
179 g_return_val_if_fail (GTK_IS_TICKER (ticker), -1);
180
181 return ticker->interval;
182 }
183
184 void
185 gtk_ticker_set_scootch (GtkTicker *ticker, gint scootch )
186 {
187 g_return_if_fail (ticker != NULL);
188 g_return_if_fail (GTK_IS_TICKER (ticker));
189
190 if ( scootch <= 0 )
191 scootch = 2;
192 ticker->scootch = scootch;
193 ticker->dirty = TRUE;
194 }
195
196 guint
197 gtk_ticker_get_scootch (GtkTicker *ticker )
198 {
199 g_return_val_if_fail (ticker != NULL, -1);
200 g_return_val_if_fail (GTK_IS_TICKER (ticker), -1);
201
202 return ticker->scootch;
203 }
204
205 void
206 gtk_ticker_set_spacing (GtkTicker *ticker, gint spacing )
207 {
208 g_return_if_fail (ticker != NULL);
209 g_return_if_fail (GTK_IS_TICKER (ticker));
210
211 if ( spacing < 0 )
212 spacing = 0;
213 ticker->spacing = spacing;
214 ticker->dirty = TRUE;
215
216 }
217
218 static int
219 ticker_timeout( gpointer data )
220 {
221 GtkTicker *ticker = (GtkTicker *) data;
222
223 if (GTK_WIDGET_VISIBLE (ticker))
224 gtk_widget_queue_resize (GTK_WIDGET (ticker));
225
226 return( TRUE );
227 }
228
229 void
230 gtk_ticker_start_scroll(GtkTicker *ticker)
231 {
232 g_return_if_fail (ticker != NULL);
233 g_return_if_fail (GTK_IS_TICKER (ticker));
234 if ( ticker->timer != 0 )
235 return;
236 ticker->timer = gtk_timeout_add(ticker->interval,
237 ticker_timeout, ticker);
238 }
239
240 void
241 gtk_ticker_stop_scroll(GtkTicker *ticker)
242 {
243 g_return_if_fail (ticker != NULL);
244 g_return_if_fail (GTK_IS_TICKER (ticker));
245 if ( ticker->timer == 0 )
246 return;
247 gtk_timeout_remove( ticker->timer );
248 ticker->timer = 0;
249
250 }
251
252 guint
253 gtk_ticker_get_spacing (GtkTicker *ticker )
254 {
255 g_return_val_if_fail (ticker != NULL, -1);
256 g_return_val_if_fail (GTK_IS_TICKER (ticker), -1);
257
258 return ticker->spacing;
259 }
260
261 static void
262 gtk_ticker_map (GtkWidget *widget)
263 {
264 GtkTicker *ticker;
265 GtkTickerChild *child;
266 GList *children;
267
268 g_return_if_fail (widget != NULL);
269 g_return_if_fail (GTK_IS_TICKER (widget));
270
271 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
272 ticker = GTK_TICKER (widget);
273
274 children = ticker->children;
275 while (children)
276 {
277 child = children->data;
278 children = children->next;
279
280 if (GTK_WIDGET_VISIBLE (child->widget) &&
281 !GTK_WIDGET_MAPPED (child->widget))
282 gtk_widget_map (child->widget);
283 }
284
285 gdk_window_show (widget->window);
286 }
287
288 static void
289 gtk_ticker_realize (GtkWidget *widget)
290 {
291 GdkWindowAttr attributes;
292 gint attributes_mask;
293
294 g_return_if_fail (widget != NULL);
295 g_return_if_fail (GTK_IS_TICKER (widget));
296
297 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
298
299 attributes.window_type = GDK_WINDOW_CHILD;
300 attributes.x = widget->allocation.x;
301 attributes.y = widget->allocation.y;
302 attributes.width = widget->allocation.width;
303 attributes.height = widget->allocation.height;
304 attributes.wclass = GDK_INPUT_OUTPUT;
305 attributes.visual = gtk_widget_get_visual (widget);
306 attributes.colormap = gtk_widget_get_colormap (widget);
307 attributes.event_mask = gtk_widget_get_events (widget);
308 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
309
310 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
311
312 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
313 &attributes, attributes_mask);
314 gdk_window_set_user_data (widget->window, widget);
315
316 widget->style = gtk_style_attach (widget->style, widget->window);
317 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
318 }
319
320 static void
321 gtk_ticker_size_request (GtkWidget *widget,
322 GtkRequisition *requisition)
323 {
324 GtkTicker *ticker;
325 GtkTickerChild *child;
326 GList *children;
327 GtkRequisition child_requisition;
328
329 g_return_if_fail (widget != NULL);
330 g_return_if_fail (GTK_IS_TICKER (widget));
331 g_return_if_fail (requisition != NULL);
332
333 ticker = GTK_TICKER (widget);
334 requisition->width = 0;
335 requisition->height = 0;
336
337 children = ticker->children;
338 while (children)
339 {
340 child = children->data;
341 children = children->next;
342
343 if (GTK_WIDGET_VISIBLE (child->widget))
344 {
345 gtk_widget_size_request (child->widget, &child_requisition);
346
347 requisition->height = MAX (requisition->height,
348 child_requisition.height);
349 requisition->width += child_requisition.width + ticker->spacing;
350 }
351 }
352 if ( requisition->width > ticker->spacing )
353 requisition->width -= ticker->spacing;
354
355 requisition->height += GTK_CONTAINER (ticker)->border_width * 2;
356 requisition->width += GTK_CONTAINER (ticker)->border_width * 2;
357 }
358
359 static void
360 gtk_ticker_compute_offsets (GtkTicker *ticker)
361 {
362 GtkTickerChild *child;
363 GtkRequisition child_requisition;
364 GList *children;
365 guint16 border_width;
366
367 g_return_if_fail (ticker != NULL);
368 g_return_if_fail (GTK_IS_TICKER(ticker));
369
370 border_width = GTK_CONTAINER (ticker)->border_width;
371
372 ticker->width = GTK_WIDGET(ticker)->allocation.width;
373 ticker->total = 0;
374 children = ticker->children;
375 while (children) {
376 child = children->data;
377
378 child->x = 0;
379 if (GTK_WIDGET_VISIBLE (child->widget)) {
380 gtk_widget_get_child_requisition (child->widget, &child_requisition);
381 child->offset = ticker->total;
382 ticker->total +=
383 child_requisition.width + border_width + ticker->spacing;
384 }
385 children = children->next;
386 }
387 ticker->dirty = FALSE;
388 }
389
390 static void
391 gtk_ticker_size_allocate (GtkWidget *widget,
392 GtkAllocation *allocation)
393 {
394 GtkTicker *ticker;
395 GtkTickerChild *child;
396 GtkAllocation child_allocation;
397 GtkRequisition child_requisition;
398 GList *children;
399 guint16 border_width;
400
401 g_return_if_fail (widget != NULL);
402 g_return_if_fail (GTK_IS_TICKER(widget));
403 g_return_if_fail (allocation != NULL);
404
405 ticker = GTK_TICKER (widget);
406
407 if ( GTK_WIDGET(ticker)->allocation.width != ticker->width )
408 ticker->dirty = TRUE;
409
410 if ( ticker->dirty == TRUE ) {
411 gtk_ticker_compute_offsets( ticker );
412 }
413
414 widget->allocation = *allocation;
415 if (GTK_WIDGET_REALIZED (widget))
416 gdk_window_move_resize (widget->window,
417 allocation->x,
418 allocation->y,
419 allocation->width,
420 allocation->height);
421
422 border_width = GTK_CONTAINER (ticker)->border_width;
423
424 children = ticker->children;
425 while (children)
426 {
427 child = children->data;
428 child->x -= ticker->scootch;
429
430 if (GTK_WIDGET_VISIBLE (child->widget)) {
431 gtk_widget_get_child_requisition (child->widget, &child_requisition);
432 child_allocation.width = child_requisition.width;
433 child_allocation.x = child->offset + border_width + child->x;
434 if ( ( child_allocation.x + child_allocation.width ) < GTK_WIDGET(ticker)->allocation.x ) {
435 if ( ticker->total >= GTK_WIDGET(ticker)->allocation.width ) {
436 child->x += GTK_WIDGET(ticker)->allocation.x + GTK_WIDGET(ticker)->allocation.width + ( ticker->total - ( GTK_WIDGET(ticker)->allocation.x + GTK_WIDGET(ticker)->allocation.width ) );
437 }
438 else {
439 child->x += GTK_WIDGET(ticker)->allocation.x + GTK_WIDGET(ticker)->allocation.width;
440 }
441 }
442 child_allocation.y = border_width;
443 child_allocation.height = child_requisition.height;
444 gtk_widget_size_allocate (child->widget, &child_allocation);
445 }
446 children = children->next;
447 }
448 }
449
450 static void
451 gtk_ticker_paint (GtkWidget *widget,
452 GdkRectangle *area)
453 {
454 g_return_if_fail (widget != NULL);
455 g_return_if_fail (GTK_IS_TICKER (widget));
456 g_return_if_fail (area != NULL);
457
458 if (GTK_WIDGET_DRAWABLE (widget))
459 gdk_window_clear_area (widget->window, 0, 0, widget->allocation.width,
460 widget->allocation.height);
461 }
462
463 static void
464 gtk_ticker_draw (GtkWidget *widget,
465 GdkRectangle *area)
466 {
467 GtkTicker *ticker;
468 GtkTickerChild *child;
469 GdkRectangle child_area;
470 GList *children;
471
472 g_return_if_fail (widget != NULL);
473 g_return_if_fail (GTK_IS_TICKER (widget));
474
475 if (GTK_WIDGET_DRAWABLE (widget))
476 {
477 ticker = GTK_TICKER (widget);
478 gtk_ticker_paint (widget, area);
479
480 children = ticker->children;
481 while (children)
482 {
483 child = children->data;
484 children = children->next;
485 gtk_widget_draw (child->widget, NULL);
486 }
487 }
488 }
489
490 static gint
491 gtk_ticker_expose (GtkWidget *widget, GdkEventExpose *event)
492 {
493 GtkTicker *ticker;
494 GtkTickerChild *child;
495 GdkEventExpose child_event;
496 GList *children;
497
498 g_return_val_if_fail (widget != NULL, FALSE);
499 g_return_val_if_fail (GTK_IS_TICKER (widget), FALSE);
500 g_return_val_if_fail (event != NULL, FALSE);
501
502 if (GTK_WIDGET_DRAWABLE (widget))
503 {
504 ticker = GTK_TICKER (widget);
505
506 child_event = *event;
507
508 children = ticker->children;
509 while (children)
510 {
511 child = children->data;
512 children = children->next;
513
514 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
515 gtk_widget_intersect (child->widget, &event->area,
516 &child_event.area))
517 gtk_widget_event (child->widget, (GdkEvent*) &child_event);
518 }
519 }
520
521 return FALSE;
522 }
523
524 void
525 gtk_ticker_add(GtkTicker *ticker, GtkWidget *widget)
526 {
527 gtk_ticker_add_real( GTK_CONTAINER( ticker ), widget );
528 ticker->dirty = TRUE;
529 }
530
531 void
532 gtk_ticker_remove(GtkTicker *ticker, GtkWidget *widget)
533 {
534 gtk_ticker_remove_real( GTK_CONTAINER( ticker ), widget );
535 ticker->dirty = TRUE;
536 }
537
538 static void
539 gtk_ticker_add_real(GtkContainer *container,
540 GtkWidget *widget)
541 {
542 g_return_if_fail (container != NULL);
543 g_return_if_fail (GTK_IS_TICKER (container));
544 g_return_if_fail (widget != NULL);
545
546 gtk_ticker_put(GTK_TICKER (container), widget);
547 }
548
549 static void
550 gtk_ticker_remove_real(GtkContainer *container,
551 GtkWidget *widget)
552 {
553 GtkTicker *ticker;
554 GtkTickerChild *child;
555 GList *children;
556
557 g_return_if_fail (container != NULL);
558 g_return_if_fail (GTK_IS_TICKER (container));
559 g_return_if_fail (widget != NULL);
560
561 ticker = GTK_TICKER (container);
562
563 children = ticker->children;
564 while (children)
565 {
566 child = children->data;
567
568 if (child->widget == widget)
569 {
570 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
571
572 gtk_widget_unparent (widget);
573
574 ticker->children = g_list_remove_link (ticker->children, children);
575 g_list_free (children);
576 g_free (child);
577
578 if (was_visible && GTK_WIDGET_VISIBLE (container))
579 gtk_widget_queue_resize (GTK_WIDGET (container));
580
581 break;
582 }
583
584 children = children->next;
585 }
586 }
587
588 static void
589 gtk_ticker_forall (GtkContainer *container,
590 gboolean include_internals,
591 GtkCallback callback,
592 gpointer callback_data)
593 {
594 GtkTicker *ticker;
595 GtkTickerChild *child;
596 GList *children;
597
598 g_return_if_fail (container != NULL);
599 g_return_if_fail (GTK_IS_TICKER (container));
600 g_return_if_fail (callback != NULL);
601
602 ticker = GTK_TICKER (container);
603
604 children = ticker->children;
605 while (children)
606 {
607 child = children->data;
608 children = children->next;
609
610 (* callback) (child->widget, callback_data);
611 }
612 }