]> Pileus Git - ~andy/gtk/blob - examples/gtkdial/gtkdial.c
Changed LGPL address for FSF in all .h and .c files
[~andy/gtk] / examples / gtkdial / gtkdial.c
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 #include <math.h>
20 #include <stdio.h>
21 #include <gtk/gtkmain.h>
22 #include <gtk/gtksignal.h>
23
24 #include "gtkdial.h"
25
26 #define SCROLL_DELAY_LENGTH  300
27 #define DIAL_DEFAULT_SIZE 100
28
29 /* Forward declararations */
30
31 static void gtk_dial_class_init               (GtkDialClass    *klass);
32 static void gtk_dial_init                     (GtkDial         *dial);
33 static void gtk_dial_destroy                  (GtkObject        *object);
34 static void gtk_dial_realize                  (GtkWidget        *widget);
35 static void gtk_dial_size_request             (GtkWidget      *widget,
36                                                GtkRequisition *requisition);
37 static void gtk_dial_size_allocate            (GtkWidget     *widget,
38                                                GtkAllocation *allocation);
39 static gint gtk_dial_expose                   (GtkWidget        *widget,
40                                                 GdkEventExpose   *event);
41 static gint gtk_dial_button_press             (GtkWidget        *widget,
42                                                 GdkEventButton   *event);
43 static gint gtk_dial_button_release           (GtkWidget        *widget,
44                                                 GdkEventButton   *event);
45 static gint gtk_dial_motion_notify            (GtkWidget        *widget,
46                                                 GdkEventMotion   *event);
47 static gint gtk_dial_timer                    (GtkDial         *dial);
48
49 static void gtk_dial_update_mouse             (GtkDial *dial, gint x, gint y);
50 static void gtk_dial_update                   (GtkDial *dial);
51 static void gtk_dial_adjustment_changed       (GtkAdjustment    *adjustment,
52                                                 gpointer          data);
53 static void gtk_dial_adjustment_value_changed (GtkAdjustment    *adjustment,
54                                                 gpointer          data);
55
56 /* Local data */
57
58 static GtkWidgetClass *parent_class = NULL;
59
60 guint
61 gtk_dial_get_type ()
62 {
63   static guint dial_type = 0;
64
65   if (!dial_type)
66     {
67       GtkTypeInfo dial_info =
68       {
69         "GtkDial",
70         sizeof (GtkDial),
71         sizeof (GtkDialClass),
72         (GtkClassInitFunc) gtk_dial_class_init,
73         (GtkObjectInitFunc) gtk_dial_init,
74         (GtkArgSetFunc) NULL,
75         (GtkArgGetFunc) NULL,
76       };
77
78       dial_type = gtk_type_unique (gtk_widget_get_type (), &dial_info);
79     }
80
81   return dial_type;
82 }
83
84 static void
85 gtk_dial_class_init (GtkDialClass *class)
86 {
87   GtkObjectClass *object_class;
88   GtkWidgetClass *widget_class;
89
90   object_class = (GtkObjectClass*) class;
91   widget_class = (GtkWidgetClass*) class;
92
93   parent_class = gtk_type_class (gtk_widget_get_type ());
94
95   object_class->destroy = gtk_dial_destroy;
96
97   widget_class->realize = gtk_dial_realize;
98   widget_class->expose_event = gtk_dial_expose;
99   widget_class->size_request = gtk_dial_size_request;
100   widget_class->size_allocate = gtk_dial_size_allocate;
101   widget_class->button_press_event = gtk_dial_button_press;
102   widget_class->button_release_event = gtk_dial_button_release;
103   widget_class->motion_notify_event = gtk_dial_motion_notify;
104 }
105
106 static void
107 gtk_dial_init (GtkDial *dial)
108 {
109   dial->button = 0;
110   dial->policy = GTK_UPDATE_CONTINUOUS;
111   dial->timer = 0;
112   dial->radius = 0;
113   dial->pointer_width = 0;
114   dial->angle = 0.0;
115   dial->old_value = 0.0;
116   dial->old_lower = 0.0;
117   dial->old_upper = 0.0;
118   dial->adjustment = NULL;
119 }
120
121 GtkWidget*
122 gtk_dial_new (GtkAdjustment *adjustment)
123 {
124   GtkDial *dial;
125
126   dial = gtk_type_new (gtk_dial_get_type ());
127
128   if (!adjustment)
129     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
130
131   gtk_dial_set_adjustment (dial, adjustment);
132
133   return GTK_WIDGET (dial);
134 }
135
136 static void
137 gtk_dial_destroy (GtkObject *object)
138 {
139   GtkDial *dial;
140
141   g_return_if_fail (object != NULL);
142   g_return_if_fail (GTK_IS_DIAL (object));
143
144   dial = GTK_DIAL (object);
145
146   if (dial->adjustment)
147     gtk_object_unref (GTK_OBJECT (dial->adjustment));
148
149   if (GTK_OBJECT_CLASS (parent_class)->destroy)
150     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
151 }
152
153 GtkAdjustment*
154 gtk_dial_get_adjustment (GtkDial *dial)
155 {
156   g_return_val_if_fail (dial != NULL, NULL);
157   g_return_val_if_fail (GTK_IS_DIAL (dial), NULL);
158
159   return dial->adjustment;
160 }
161
162 void
163 gtk_dial_set_update_policy (GtkDial      *dial,
164                              GtkUpdateType  policy)
165 {
166   g_return_if_fail (dial != NULL);
167   g_return_if_fail (GTK_IS_DIAL (dial));
168
169   dial->policy = policy;
170 }
171
172 void
173 gtk_dial_set_adjustment (GtkDial      *dial,
174                           GtkAdjustment *adjustment)
175 {
176   g_return_if_fail (dial != NULL);
177   g_return_if_fail (GTK_IS_DIAL (dial));
178
179   if (dial->adjustment)
180     {
181       gtk_signal_disconnect_by_data (GTK_OBJECT (dial->adjustment), (gpointer) dial);
182       gtk_object_unref (GTK_OBJECT (dial->adjustment));
183     }
184
185   dial->adjustment = adjustment;
186   gtk_object_ref (GTK_OBJECT (dial->adjustment));
187
188   gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
189                       (GtkSignalFunc) gtk_dial_adjustment_changed,
190                       (gpointer) dial);
191   gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
192                       (GtkSignalFunc) gtk_dial_adjustment_value_changed,
193                       (gpointer) dial);
194
195   dial->old_value = adjustment->value;
196   dial->old_lower = adjustment->lower;
197   dial->old_upper = adjustment->upper;
198
199   gtk_dial_update (dial);
200 }
201
202 static void
203 gtk_dial_realize (GtkWidget *widget)
204 {
205   GtkDial *dial;
206   GdkWindowAttr attributes;
207   gint attributes_mask;
208
209   g_return_if_fail (widget != NULL);
210   g_return_if_fail (GTK_IS_DIAL (widget));
211
212   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
213   dial = GTK_DIAL (widget);
214
215   attributes.x = widget->allocation.x;
216   attributes.y = widget->allocation.y;
217   attributes.width = widget->allocation.width;
218   attributes.height = widget->allocation.height;
219   attributes.wclass = GDK_INPUT_OUTPUT;
220   attributes.window_type = GDK_WINDOW_CHILD;
221   attributes.event_mask = gtk_widget_get_events (widget) | 
222     GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | 
223     GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
224     GDK_POINTER_MOTION_HINT_MASK;
225   attributes.visual = gtk_widget_get_visual (widget);
226   attributes.colormap = gtk_widget_get_colormap (widget);
227
228   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
229   widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
230
231   widget->style = gtk_style_attach (widget->style, widget->window);
232
233   gdk_window_set_user_data (widget->window, widget);
234
235   gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
236 }
237
238 static void 
239 gtk_dial_size_request (GtkWidget      *widget,
240                        GtkRequisition *requisition)
241 {
242   requisition->width = DIAL_DEFAULT_SIZE;
243   requisition->height = DIAL_DEFAULT_SIZE;
244 }
245
246 static void
247 gtk_dial_size_allocate (GtkWidget     *widget,
248                         GtkAllocation *allocation)
249 {
250   GtkDial *dial;
251
252   g_return_if_fail (widget != NULL);
253   g_return_if_fail (GTK_IS_DIAL (widget));
254   g_return_if_fail (allocation != NULL);
255
256   widget->allocation = *allocation;
257   dial = GTK_DIAL (widget);
258
259   if (GTK_WIDGET_REALIZED (widget))
260     {
261
262       gdk_window_move_resize (widget->window,
263                               allocation->x, allocation->y,
264                               allocation->width, allocation->height);
265
266     }
267   dial->radius = MIN(allocation->width,allocation->height) * 0.45;
268   dial->pointer_width = dial->radius / 5;
269 }
270
271 static gint
272 gtk_dial_expose (GtkWidget      *widget,
273                  GdkEventExpose *event)
274 {
275   GtkDial *dial;
276   GdkPoint points[3];
277   gdouble s,c;
278   gdouble theta;
279   gint xc, yc;
280   gint tick_length;
281   gint i;
282
283   g_return_val_if_fail (widget != NULL, FALSE);
284   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
285   g_return_val_if_fail (event != NULL, FALSE);
286
287   if (event->count > 0)
288     return FALSE;
289   
290   dial = GTK_DIAL (widget);
291
292   gdk_window_clear_area (widget->window,
293                          0, 0,
294                          widget->allocation.width,
295                          widget->allocation.height);
296
297   xc = widget->allocation.width/2;
298   yc = widget->allocation.height/2;
299
300   /* Draw ticks */
301
302   for (i=0; i<25; i++)
303     {
304       theta = (i*M_PI/18. - M_PI/6.);
305       s = sin(theta);
306       c = cos(theta);
307
308       tick_length = (i%6 == 0) ? dial->pointer_width : dial->pointer_width/2;
309       
310       gdk_draw_line (widget->window,
311                      widget->style->fg_gc[widget->state],
312                      xc + c*(dial->radius - tick_length),
313                      yc - s*(dial->radius - tick_length),
314                      xc + c*dial->radius,
315                      yc - s*dial->radius);
316     }
317
318   /* Draw pointer */
319
320   s = sin(dial->angle);
321   c = cos(dial->angle);
322
323
324   points[0].x = xc + s*dial->pointer_width/2;
325   points[0].y = yc + c*dial->pointer_width/2;
326   points[1].x = xc + c*dial->radius;
327   points[1].y = yc - s*dial->radius;
328   points[2].x = xc - s*dial->pointer_width/2;
329   points[2].y = yc - c*dial->pointer_width/2;
330
331   gtk_draw_polygon (widget->style,
332                     widget->window,
333                     GTK_STATE_NORMAL,
334                     GTK_SHADOW_OUT,
335                     points, 3,
336                     TRUE);
337   
338   return FALSE;
339 }
340
341 static gint
342 gtk_dial_button_press (GtkWidget      *widget,
343                        GdkEventButton *event)
344 {
345   GtkDial *dial;
346   gint dx, dy;
347   double s, c;
348   double d_parallel;
349   double d_perpendicular;
350
351   g_return_val_if_fail (widget != NULL, FALSE);
352   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
353   g_return_val_if_fail (event != NULL, FALSE);
354
355   dial = GTK_DIAL (widget);
356
357   /* Determine if button press was within pointer region - we 
358      do this by computing the parallel and perpendicular distance of
359      the point where the mouse was pressed from the line passing through
360      the pointer */
361   
362   dx = event->x - widget->allocation.width / 2;
363   dy = widget->allocation.height / 2 - event->y;
364   
365   s = sin(dial->angle);
366   c = cos(dial->angle);
367   
368   d_parallel = s*dy + c*dx;
369   d_perpendicular = fabs(s*dx - c*dy);
370   
371   if (!dial->button &&
372       (d_perpendicular < dial->pointer_width/2) &&
373       (d_parallel > - dial->pointer_width))
374     {
375       gtk_grab_add (widget);
376
377       dial->button = event->button;
378
379       gtk_dial_update_mouse (dial, event->x, event->y);
380     }
381
382   return FALSE;
383 }
384
385 static gint
386 gtk_dial_button_release (GtkWidget      *widget,
387                           GdkEventButton *event)
388 {
389   GtkDial *dial;
390
391   g_return_val_if_fail (widget != NULL, FALSE);
392   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
393   g_return_val_if_fail (event != NULL, FALSE);
394
395   dial = GTK_DIAL (widget);
396
397   if (dial->button == event->button)
398     {
399       gtk_grab_remove (widget);
400
401       dial->button = 0;
402
403       if (dial->policy == GTK_UPDATE_DELAYED)
404         gtk_timeout_remove (dial->timer);
405       
406       if ((dial->policy != GTK_UPDATE_CONTINUOUS) &&
407           (dial->old_value != dial->adjustment->value))
408         gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
409     }
410
411   return FALSE;
412 }
413
414 static gint
415 gtk_dial_motion_notify (GtkWidget      *widget,
416                          GdkEventMotion *event)
417 {
418   GtkDial *dial;
419   GdkModifierType mods;
420   gint x, y, mask;
421
422   g_return_val_if_fail (widget != NULL, FALSE);
423   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
424   g_return_val_if_fail (event != NULL, FALSE);
425
426   dial = GTK_DIAL (widget);
427
428   if (dial->button != 0)
429     {
430       x = event->x;
431       y = event->y;
432
433       if (event->is_hint || (event->window != widget->window))
434         gdk_window_get_pointer (widget->window, &x, &y, &mods);
435
436       switch (dial->button)
437         {
438         case 1:
439           mask = GDK_BUTTON1_MASK;
440           break;
441         case 2:
442           mask = GDK_BUTTON2_MASK;
443           break;
444         case 3:
445           mask = GDK_BUTTON3_MASK;
446           break;
447         default:
448           mask = 0;
449           break;
450         }
451
452       if (mods & mask)
453         gtk_dial_update_mouse (dial, x,y);
454     }
455
456   return FALSE;
457 }
458
459 static gint
460 gtk_dial_timer (GtkDial *dial)
461 {
462   g_return_val_if_fail (dial != NULL, FALSE);
463   g_return_val_if_fail (GTK_IS_DIAL (dial), FALSE);
464
465   if (dial->policy == GTK_UPDATE_DELAYED)
466     gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
467
468   return FALSE;
469 }
470
471 static void
472 gtk_dial_update_mouse (GtkDial *dial, gint x, gint y)
473 {
474   gint xc, yc;
475   gfloat old_value;
476
477   g_return_if_fail (dial != NULL);
478   g_return_if_fail (GTK_IS_DIAL (dial));
479
480   xc = GTK_WIDGET(dial)->allocation.width / 2;
481   yc = GTK_WIDGET(dial)->allocation.height / 2;
482
483   old_value = dial->adjustment->value;
484   dial->angle = atan2(yc-y, x-xc);
485
486   if (dial->angle < -M_PI/2.)
487     dial->angle += 2*M_PI;
488
489   if (dial->angle < -M_PI/6)
490     dial->angle = -M_PI/6;
491
492   if (dial->angle > 7.*M_PI/6.)
493     dial->angle = 7.*M_PI/6.;
494
495   dial->adjustment->value = dial->adjustment->lower + (7.*M_PI/6 - dial->angle) *
496     (dial->adjustment->upper - dial->adjustment->lower) / (4.*M_PI/3.);
497
498   if (dial->adjustment->value != old_value)
499     {
500       if (dial->policy == GTK_UPDATE_CONTINUOUS)
501         {
502           gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
503         }
504       else
505         {
506           gtk_widget_draw (GTK_WIDGET(dial), NULL);
507
508           if (dial->policy == GTK_UPDATE_DELAYED)
509             {
510               if (dial->timer)
511                 gtk_timeout_remove (dial->timer);
512
513               dial->timer = gtk_timeout_add (SCROLL_DELAY_LENGTH,
514                                              (GtkFunction) gtk_dial_timer,
515                                              (gpointer) dial);
516             }
517         }
518     }
519 }
520
521 static void
522 gtk_dial_update (GtkDial *dial)
523 {
524   gfloat new_value;
525   
526   g_return_if_fail (dial != NULL);
527   g_return_if_fail (GTK_IS_DIAL (dial));
528
529   new_value = dial->adjustment->value;
530   
531   if (new_value < dial->adjustment->lower)
532     new_value = dial->adjustment->lower;
533
534   if (new_value > dial->adjustment->upper)
535     new_value = dial->adjustment->upper;
536
537   if (new_value != dial->adjustment->value)
538     {
539       dial->adjustment->value = new_value;
540       gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
541     }
542
543   dial->angle = 7.*M_PI/6. - (new_value - dial->adjustment->lower) * 4.*M_PI/3. /
544     (dial->adjustment->upper - dial->adjustment->lower);
545
546   gtk_widget_draw (GTK_WIDGET(dial), NULL);
547 }
548
549 static void
550 gtk_dial_adjustment_changed (GtkAdjustment *adjustment,
551                               gpointer       data)
552 {
553   GtkDial *dial;
554
555   g_return_if_fail (adjustment != NULL);
556   g_return_if_fail (data != NULL);
557
558   dial = GTK_DIAL (data);
559
560   if ((dial->old_value != adjustment->value) ||
561       (dial->old_lower != adjustment->lower) ||
562       (dial->old_upper != adjustment->upper))
563     {
564       gtk_dial_update (dial);
565
566       dial->old_value = adjustment->value;
567       dial->old_lower = adjustment->lower;
568       dial->old_upper = adjustment->upper;
569     }
570 }
571
572 static void
573 gtk_dial_adjustment_value_changed (GtkAdjustment *adjustment,
574                                     gpointer       data)
575 {
576   GtkDial *dial;
577
578   g_return_if_fail (adjustment != NULL);
579   g_return_if_fail (data != NULL);
580
581   dial = GTK_DIAL (data);
582
583   if (dial->old_value != adjustment->value)
584     {
585       gtk_dial_update (dial);
586
587       dial->old_value = adjustment->value;
588     }
589 }
590
591