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