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