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