]> Pileus Git - ~andy/gtk/blob - examples/gtkdial/gtkdial.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / examples / gtkdial / gtkdial.c
1
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 #include <config.h>
21 #include <math.h>
22 #include <stdio.h>
23 #include <gtk/gtkmain.h>
24 #include <gtk/gtksignal.h>
25
26 #include "gtkdial.h"
27
28 #define SCROLL_DELAY_LENGTH  300
29 #define DIAL_DEFAULT_SIZE 100
30
31 /* Forward declarations */
32
33 static void gtk_dial_class_init               (GtkDialClass    *klass);
34 static void gtk_dial_init                     (GtkDial         *dial);
35 static void gtk_dial_destroy                  (GtkObject        *object);
36 static void gtk_dial_realize                  (GtkWidget        *widget);
37 static void gtk_dial_size_request             (GtkWidget      *widget,
38                                                GtkRequisition *requisition);
39 static void gtk_dial_size_allocate            (GtkWidget     *widget,
40                                                GtkAllocation *allocation);
41 static gint gtk_dial_expose                   (GtkWidget        *widget,
42                                                 GdkEventExpose   *event);
43 static gint gtk_dial_button_press             (GtkWidget        *widget,
44                                                 GdkEventButton   *event);
45 static gint gtk_dial_button_release           (GtkWidget        *widget,
46                                                 GdkEventButton   *event);
47 static gint gtk_dial_motion_notify            (GtkWidget        *widget,
48                                                 GdkEventMotion   *event);
49 static gboolean gtk_dial_timer                (GtkDial         *dial);
50
51 static void gtk_dial_update_mouse             (GtkDial *dial, gint x, gint y);
52 static void gtk_dial_update                   (GtkDial *dial);
53 static void gtk_dial_adjustment_changed       (GtkAdjustment    *adjustment,
54                                                 gpointer          data);
55 static void gtk_dial_adjustment_value_changed (GtkAdjustment    *adjustment,
56                                                 gpointer          data);
57
58 /* Local data */
59
60 static GtkWidgetClass *parent_class = NULL;
61
62 GType
63 gtk_dial_get_type ()
64 {
65   static GType dial_type = 0;
66
67   if (!dial_type)
68     {
69       static const GTypeInfo dial_info =
70       {
71         sizeof (GtkDialClass),
72         NULL,
73         NULL,
74         (GClassInitFunc) gtk_dial_class_init,
75         NULL,
76         NULL,
77         sizeof (GtkDial),
78         0,
79         (GInstanceInitFunc) gtk_dial_init,
80       };
81
82       dial_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkDial", &dial_info, 0);
83     }
84
85   return dial_type;
86 }
87
88 static void
89 gtk_dial_class_init (GtkDialClass *class)
90 {
91   GtkObjectClass *object_class;
92   GtkWidgetClass *widget_class;
93
94   object_class = (GtkObjectClass*) class;
95   widget_class = (GtkWidgetClass*) class;
96
97   parent_class = gtk_type_class (gtk_widget_get_type ());
98
99   object_class->destroy = gtk_dial_destroy;
100
101   widget_class->realize = gtk_dial_realize;
102   widget_class->expose_event = gtk_dial_expose;
103   widget_class->size_request = gtk_dial_size_request;
104   widget_class->size_allocate = gtk_dial_size_allocate;
105   widget_class->button_press_event = gtk_dial_button_press;
106   widget_class->button_release_event = gtk_dial_button_release;
107   widget_class->motion_notify_event = gtk_dial_motion_notify;
108 }
109
110 static void
111 gtk_dial_init (GtkDial *dial)
112 {
113   dial->button = 0;
114   dial->policy = GTK_UPDATE_CONTINUOUS;
115   dial->timer = 0;
116   dial->radius = 0;
117   dial->pointer_width = 0;
118   dial->angle = 0.0;
119   dial->old_value = 0.0;
120   dial->old_lower = 0.0;
121   dial->old_upper = 0.0;
122   dial->adjustment = NULL;
123 }
124
125 GtkWidget*
126 gtk_dial_new (GtkAdjustment *adjustment)
127 {
128   GtkDial *dial;
129
130   dial = g_object_new (gtk_dial_get_type (), NULL);
131
132   if (!adjustment)
133     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
134
135   gtk_dial_set_adjustment (dial, adjustment);
136
137   return GTK_WIDGET (dial);
138 }
139
140 static void
141 gtk_dial_destroy (GtkObject *object)
142 {
143   GtkDial *dial;
144
145   g_return_if_fail (object != NULL);
146   g_return_if_fail (GTK_IS_DIAL (object));
147
148   dial = GTK_DIAL (object);
149
150   if (dial->adjustment)
151     {
152       g_object_unref (GTK_OBJECT (dial->adjustment));
153       dial->adjustment = NULL;
154     }
155
156   if (GTK_OBJECT_CLASS (parent_class)->destroy)
157     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
158 }
159
160 GtkAdjustment*
161 gtk_dial_get_adjustment (GtkDial *dial)
162 {
163   g_return_val_if_fail (dial != NULL, NULL);
164   g_return_val_if_fail (GTK_IS_DIAL (dial), NULL);
165
166   return dial->adjustment;
167 }
168
169 void
170 gtk_dial_set_update_policy (GtkDial      *dial,
171                              GtkUpdateType  policy)
172 {
173   g_return_if_fail (dial != NULL);
174   g_return_if_fail (GTK_IS_DIAL (dial));
175
176   dial->policy = policy;
177 }
178
179 void
180 gtk_dial_set_adjustment (GtkDial      *dial,
181                           GtkAdjustment *adjustment)
182 {
183   g_return_if_fail (dial != NULL);
184   g_return_if_fail (GTK_IS_DIAL (dial));
185
186   if (dial->adjustment)
187     {
188       g_signal_handlers_disconnect_by_func (GTK_OBJECT (dial->adjustment), NULL, (gpointer) dial);
189       g_object_unref (GTK_OBJECT (dial->adjustment));
190     }
191
192   dial->adjustment = adjustment;
193   g_object_ref (GTK_OBJECT (dial->adjustment));
194
195   g_signal_connect (GTK_OBJECT (adjustment), "changed",
196                     GTK_SIGNAL_FUNC (gtk_dial_adjustment_changed),
197                     (gpointer) dial);
198   g_signal_connect (GTK_OBJECT (adjustment), "value_changed",
199                     GTK_SIGNAL_FUNC (gtk_dial_adjustment_value_changed),
200                     (gpointer) dial);
201
202   dial->old_value = adjustment->value;
203   dial->old_lower = adjustment->lower;
204   dial->old_upper = adjustment->upper;
205
206   gtk_dial_update (dial);
207 }
208
209 static void
210 gtk_dial_realize (GtkWidget *widget)
211 {
212   GtkDial *dial;
213   GdkWindowAttr attributes;
214   gint attributes_mask;
215
216   g_return_if_fail (widget != NULL);
217   g_return_if_fail (GTK_IS_DIAL (widget));
218
219   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
220   dial = GTK_DIAL (widget);
221
222   attributes.x = widget->allocation.x;
223   attributes.y = widget->allocation.y;
224   attributes.width = widget->allocation.width;
225   attributes.height = widget->allocation.height;
226   attributes.wclass = GDK_INPUT_OUTPUT;
227   attributes.window_type = GDK_WINDOW_CHILD;
228   attributes.event_mask = gtk_widget_get_events (widget) | 
229     GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | 
230     GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
231     GDK_POINTER_MOTION_HINT_MASK;
232   attributes.visual = gtk_widget_get_visual (widget);
233   attributes.colormap = gtk_widget_get_colormap (widget);
234
235   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
236   widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
237
238   widget->style = gtk_style_attach (widget->style, widget->window);
239
240   gdk_window_set_user_data (widget->window, widget);
241
242   gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
243 }
244
245 static void 
246 gtk_dial_size_request (GtkWidget      *widget,
247                        GtkRequisition *requisition)
248 {
249   requisition->width = DIAL_DEFAULT_SIZE;
250   requisition->height = DIAL_DEFAULT_SIZE;
251 }
252
253 static void
254 gtk_dial_size_allocate (GtkWidget     *widget,
255                         GtkAllocation *allocation)
256 {
257   GtkDial *dial;
258
259   g_return_if_fail (widget != NULL);
260   g_return_if_fail (GTK_IS_DIAL (widget));
261   g_return_if_fail (allocation != NULL);
262
263   widget->allocation = *allocation;
264   dial = GTK_DIAL (widget);
265
266   if (GTK_WIDGET_REALIZED (widget))
267     {
268
269       gdk_window_move_resize (widget->window,
270                               allocation->x, allocation->y,
271                               allocation->width, allocation->height);
272
273     }
274   dial->radius = MIN (allocation->width, allocation->height) * 0.45;
275   dial->pointer_width = dial->radius / 5;
276 }
277
278 static gint
279 gtk_dial_expose (GtkWidget      *widget,
280                  GdkEventExpose *event)
281 {
282   GtkDial *dial;
283   GdkPoint points[6];
284   gdouble s,c;
285   gdouble theta, last, increment;
286   GtkStyle      *blankstyle;
287   gint xc, yc;
288   gint upper, lower;
289   gint tick_length;
290   gint i, inc;
291
292   g_return_val_if_fail (widget != NULL, FALSE);
293   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
294   g_return_val_if_fail (event != NULL, FALSE);
295
296   if (event->count > 0)
297     return FALSE;
298   
299   dial = GTK_DIAL (widget);
300
301 /*  gdk_window_clear_area (widget->window,
302                          0, 0,
303                          widget->allocation.width,
304                          widget->allocation.height);
305 */
306   xc = widget->allocation.width / 2;
307   yc = widget->allocation.height / 2;
308
309   upper = dial->adjustment->upper;
310   lower = dial->adjustment->lower;
311
312   /* Erase old pointer */
313
314   s = sin (dial->last_angle);
315   c = cos (dial->last_angle);
316   dial->last_angle = dial->angle;
317
318   points[0].x = xc + s*dial->pointer_width/2;
319   points[0].y = yc + c*dial->pointer_width/2;
320   points[1].x = xc + c*dial->radius;
321   points[1].y = yc - s*dial->radius;
322   points[2].x = xc - s*dial->pointer_width/2;
323   points[2].y = yc - c*dial->pointer_width/2;
324   points[3].x = xc - c*dial->radius/10;
325   points[3].y = yc + s*dial->radius/10;
326   points[4].x = points[0].x;
327   points[4].y = points[0].y;
328
329   blankstyle = gtk_style_new ();
330   blankstyle->bg_gc[GTK_STATE_NORMAL] =
331                 widget->style->bg_gc[GTK_STATE_NORMAL];
332   blankstyle->dark_gc[GTK_STATE_NORMAL] =
333                 widget->style->bg_gc[GTK_STATE_NORMAL];
334   blankstyle->light_gc[GTK_STATE_NORMAL] =
335                 widget->style->bg_gc[GTK_STATE_NORMAL];
336   blankstyle->black_gc =
337                 widget->style->bg_gc[GTK_STATE_NORMAL];
338
339   gtk_paint_polygon (blankstyle,
340                     widget->window,
341                     GTK_STATE_NORMAL,
342                     GTK_SHADOW_OUT,
343                     NULL,
344                     widget,
345                     NULL,
346                     points, 5,
347                     FALSE);
348
349   g_object_unref (blankstyle);
350
351
352   /* Draw ticks */
353
354   if ((upper - lower) == 0)
355     return FALSE;
356
357   increment = (100*M_PI) / (dial->radius*dial->radius);
358
359   inc = (upper - lower);
360
361   while (inc < 100) inc *= 10;
362   while (inc >= 1000) inc /= 10;
363   last = -1;
364
365   for (i = 0; i <= inc; i++)
366     {
367       theta = ((gfloat)i*M_PI / (18*inc/24.) - M_PI/6.);
368
369       if ((theta - last) < (increment))
370         continue;     
371       last = theta;
372
373       s = sin (theta);
374       c = cos (theta);
375
376       tick_length = (i%(inc/10) == 0) ? dial->pointer_width : dial->pointer_width / 2;
377
378       gdk_draw_line (widget->window,
379                      widget->style->fg_gc[widget->state],
380                      xc + c*(dial->radius - tick_length),
381                      yc - s*(dial->radius - tick_length),
382                      xc + c*dial->radius,
383                      yc - s*dial->radius);
384     }
385
386   /* Draw pointer */
387
388   s = sin (dial->angle);
389   c = cos (dial->angle);
390   dial->last_angle = dial->angle;
391
392   points[0].x = xc + s*dial->pointer_width/2;
393   points[0].y = yc + c*dial->pointer_width/2;
394   points[1].x = xc + c*dial->radius;
395   points[1].y = yc - s*dial->radius;
396   points[2].x = xc - s*dial->pointer_width/2;
397   points[2].y = yc - c*dial->pointer_width/2;
398   points[3].x = xc - c*dial->radius/10;
399   points[3].y = yc + s*dial->radius/10;
400   points[4].x = points[0].x;
401   points[4].y = points[0].y;
402
403
404   gtk_paint_polygon (widget->style,
405                     widget->window,
406                     GTK_STATE_NORMAL,
407                     GTK_SHADOW_OUT,
408                     NULL,
409                     widget,
410                     NULL,
411                     points, 5,
412                     TRUE);
413
414   return FALSE;
415 }
416
417 static gint
418 gtk_dial_button_press (GtkWidget      *widget,
419                        GdkEventButton *event)
420 {
421   GtkDial *dial;
422   gint dx, dy;
423   double s, c;
424   double d_parallel;
425   double d_perpendicular;
426
427   g_return_val_if_fail (widget != NULL, FALSE);
428   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
429   g_return_val_if_fail (event != NULL, FALSE);
430
431   dial = GTK_DIAL (widget);
432
433   /* Determine if button press was within pointer region - we 
434      do this by computing the parallel and perpendicular distance of
435      the point where the mouse was pressed from the line passing through
436      the pointer */
437   
438   dx = event->x - widget->allocation.width / 2;
439   dy = widget->allocation.height / 2 - event->y;
440   
441   s = sin (dial->angle);
442   c = cos (dial->angle);
443   
444   d_parallel = s*dy + c*dx;
445   d_perpendicular = fabs (s*dx - c*dy);
446   
447   if (!dial->button &&
448       (d_perpendicular < dial->pointer_width/2) &&
449       (d_parallel > - dial->pointer_width))
450     {
451       gtk_grab_add (widget);
452
453       dial->button = event->button;
454
455       gtk_dial_update_mouse (dial, event->x, event->y);
456     }
457
458   return FALSE;
459 }
460
461 static gint
462 gtk_dial_button_release (GtkWidget      *widget,
463                           GdkEventButton *event)
464 {
465   GtkDial *dial;
466
467   g_return_val_if_fail (widget != NULL, FALSE);
468   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
469   g_return_val_if_fail (event != NULL, FALSE);
470
471   dial = GTK_DIAL (widget);
472
473   if (dial->button == event->button)
474     {
475       gtk_grab_remove (widget);
476
477       dial->button = 0;
478
479       if (dial->policy == GTK_UPDATE_DELAYED)
480         g_source_remove (dial->timer);
481       
482       if ((dial->policy != GTK_UPDATE_CONTINUOUS) &&
483           (dial->old_value != dial->adjustment->value))
484         g_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
485     }
486
487   return FALSE;
488 }
489
490 static gint
491 gtk_dial_motion_notify (GtkWidget      *widget,
492                          GdkEventMotion *event)
493 {
494   GtkDial *dial;
495   GdkModifierType mods;
496   gint x, y, mask;
497
498   g_return_val_if_fail (widget != NULL, FALSE);
499   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
500   g_return_val_if_fail (event != NULL, FALSE);
501
502   dial = GTK_DIAL (widget);
503
504   if (dial->button != 0)
505     {
506       x = event->x;
507       y = event->y;
508
509       if (event->is_hint || (event->window != widget->window))
510         gdk_window_get_pointer (widget->window, &x, &y, &mods);
511
512       switch (dial->button)
513         {
514         case 1:
515           mask = GDK_BUTTON1_MASK;
516           break;
517         case 2:
518           mask = GDK_BUTTON2_MASK;
519           break;
520         case 3:
521           mask = GDK_BUTTON3_MASK;
522           break;
523         default:
524           mask = 0;
525           break;
526         }
527
528       if (mods & mask)
529         gtk_dial_update_mouse (dial, x,y);
530     }
531
532   return FALSE;
533 }
534
535 static gboolean
536 gtk_dial_timer (GtkDial *dial)
537 {
538   g_return_val_if_fail (dial != NULL, FALSE);
539   g_return_val_if_fail (GTK_IS_DIAL (dial), FALSE);
540
541   if (dial->policy == GTK_UPDATE_DELAYED)
542     g_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
543
544   return FALSE;
545 }
546
547 static void
548 gtk_dial_update_mouse (GtkDial *dial, gint x, gint y)
549 {
550   gint xc, yc;
551   gfloat old_value;
552
553   g_return_if_fail (dial != NULL);
554   g_return_if_fail (GTK_IS_DIAL (dial));
555
556   xc = GTK_WIDGET(dial)->allocation.width / 2;
557   yc = GTK_WIDGET(dial)->allocation.height / 2;
558
559   old_value = dial->adjustment->value;
560   dial->angle = atan2(yc-y, x-xc);
561
562   if (dial->angle < -M_PI/2.)
563     dial->angle += 2*M_PI;
564
565   if (dial->angle < -M_PI/6)
566     dial->angle = -M_PI/6;
567
568   if (dial->angle > 7.*M_PI/6.)
569     dial->angle = 7.*M_PI/6.;
570
571   dial->adjustment->value = dial->adjustment->lower + (7.*M_PI/6 - dial->angle) *
572     (dial->adjustment->upper - dial->adjustment->lower) / (4.*M_PI/3.);
573
574   if (dial->adjustment->value != old_value)
575     {
576       if (dial->policy == GTK_UPDATE_CONTINUOUS)
577         {
578           g_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
579         }
580       else
581         {
582           gtk_widget_queue_draw (GTK_WIDGET (dial));
583
584           if (dial->policy == GTK_UPDATE_DELAYED)
585             {
586               if (dial->timer)
587                 g_source_remove (dial->timer);
588
589               dial->timer = g_timeout_add (SCROLL_DELAY_LENGTH,
590                                            (GtkFunction) gtk_dial_timer,
591                                            (gpointer) dial);
592             }
593         }
594     }
595 }
596
597 static void
598 gtk_dial_update (GtkDial *dial)
599 {
600   gfloat new_value;
601   
602   g_return_if_fail (dial != NULL);
603   g_return_if_fail (GTK_IS_DIAL (dial));
604
605   new_value = dial->adjustment->value;
606   
607   if (new_value < dial->adjustment->lower)
608     new_value = dial->adjustment->lower;
609
610   if (new_value > dial->adjustment->upper)
611     new_value = dial->adjustment->upper;
612
613   if (new_value != dial->adjustment->value)
614     {
615       dial->adjustment->value = new_value;
616       g_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
617     }
618
619   dial->angle = 7.*M_PI/6. - (new_value - dial->adjustment->lower) * 4.*M_PI/3. /
620     (dial->adjustment->upper - dial->adjustment->lower);
621
622   gtk_widget_queue_draw (GTK_WIDGET (dial));
623 }
624
625 static void
626 gtk_dial_adjustment_changed (GtkAdjustment *adjustment,
627                               gpointer       data)
628 {
629   GtkDial *dial;
630
631   g_return_if_fail (adjustment != NULL);
632   g_return_if_fail (data != NULL);
633
634   dial = GTK_DIAL (data);
635
636   if ((dial->old_value != adjustment->value) ||
637       (dial->old_lower != adjustment->lower) ||
638       (dial->old_upper != adjustment->upper))
639     {
640       gtk_dial_update (dial);
641
642       dial->old_value = adjustment->value;
643       dial->old_lower = adjustment->lower;
644       dial->old_upper = adjustment->upper;
645     }
646 }
647
648 static void
649 gtk_dial_adjustment_value_changed (GtkAdjustment *adjustment,
650                                     gpointer       data)
651 {
652   GtkDial *dial;
653
654   g_return_if_fail (adjustment != NULL);
655   g_return_if_fail (data != NULL);
656
657   dial = GTK_DIAL (data);
658
659   if (dial->old_value != adjustment->value)
660     {
661       gtk_dial_update (dial);
662
663       dial->old_value = adjustment->value;
664     }
665 }