It begins.
[ardour.git] / gtk2_ardour / gtk-custom-hruler.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
20 /*
21  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* modified by andreas meyer <hexx3000@gmx.de> */
28 /* subsequently specialized for audio time displays by paul davis <paul@linuxaudiosystems.com> */
29
30 #include <math.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include "gtk-custom-hruler.h"
34
35 #define RULER_HEIGHT          14
36 #define MINIMUM_INCR          5
37 #define MAXIMUM_SUBDIVIDE     5
38
39 #define ROUND(x) ((int) ((x) + 0.5))
40
41 static void gtk_custom_hruler_class_init (GtkCustomHRulerClass * klass);
42 static void gtk_custom_hruler_init (GtkCustomHRuler * custom_hruler);
43 static gint gtk_custom_hruler_motion_notify (GtkWidget * widget, GdkEventMotion * event);
44 static void gtk_custom_hruler_draw_ticks (GtkCustomRuler * ruler);
45 static void gtk_custom_hruler_draw_pos (GtkCustomRuler * ruler);
46
47 guint gtk_custom_hruler_get_type (void)
48 {
49         static guint custom_hruler_type = 0;
50
51         if (!custom_hruler_type) {
52                 static const GtkTypeInfo custom_hruler_info = {
53                         "GtkCustomHRuler",
54                         sizeof (GtkCustomHRuler),
55                         sizeof (GtkCustomHRulerClass),
56                         (GtkClassInitFunc) gtk_custom_hruler_class_init,
57                         (GtkObjectInitFunc) gtk_custom_hruler_init,
58                         /* reserved_1 */ NULL,
59                         /* reserved_2 */ NULL,
60                         (GtkClassInitFunc) NULL,
61                 };
62                 custom_hruler_type = gtk_type_unique (gtk_custom_ruler_get_type (), &custom_hruler_info);
63         }
64         return custom_hruler_type;
65 }
66
67 static void
68 gtk_custom_hruler_class_init (GtkCustomHRulerClass * klass)
69 {
70         GtkWidgetClass *widget_class;
71         GtkCustomRulerClass *ruler_class;
72
73         widget_class = (GtkWidgetClass *) klass;
74         ruler_class = (GtkCustomRulerClass *) klass;
75
76         widget_class->motion_notify_event = gtk_custom_hruler_motion_notify;
77
78         ruler_class->draw_ticks = gtk_custom_hruler_draw_ticks;
79         ruler_class->draw_pos = gtk_custom_hruler_draw_pos;
80 }
81
82 static void
83 gtk_custom_hruler_init (GtkCustomHRuler * custom_hruler)
84 {
85         GtkWidget *widget;
86
87         widget = GTK_WIDGET (custom_hruler);
88         widget->requisition.width = widget->style->xthickness * 2 + 1;
89         widget->requisition.height = widget->style->ythickness * 2 + RULER_HEIGHT;
90 }
91
92
93 GtkWidget *
94 gtk_custom_hruler_new (void)
95 {
96         return GTK_WIDGET (gtk_type_new (gtk_custom_hruler_get_type ()));
97 }
98
99 static gint
100 gtk_custom_hruler_motion_notify (GtkWidget * widget, GdkEventMotion * event)
101 {
102         GtkCustomRuler *ruler;
103         gint x;
104
105         g_return_val_if_fail (widget != NULL, FALSE);
106         g_return_val_if_fail (GTK_IS_CUSTOM_HRULER (widget), FALSE);
107         g_return_val_if_fail (event != NULL, FALSE);
108
109         ruler = GTK_CUSTOM_RULER (widget);
110
111         if (event->is_hint)
112                 gdk_window_get_pointer (widget->window, &x, NULL, NULL);
113         else
114                 x = event->x;
115
116         ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * x) / widget->allocation.width;
117
118         /*  Make sure the ruler has been allocated already  */
119         if (ruler->backing_store != NULL)
120                 gtk_custom_ruler_draw_pos (ruler);
121
122         return FALSE;
123 }
124
125 static void
126 gtk_custom_hruler_draw_ticks (GtkCustomRuler * ruler)
127 {
128         GtkWidget *widget;
129         GdkGC *gc, *bg_gc;
130         GdkFont *font;
131         gint i;
132         GtkCustomRulerMark *marks;
133         gint nmarks;
134         gint max_chars;
135
136         g_return_if_fail (ruler != NULL);
137         g_return_if_fail (GTK_IS_CUSTOM_HRULER (ruler));
138
139         if (!GTK_WIDGET_DRAWABLE (ruler))
140                 return;
141
142         widget = GTK_WIDGET (ruler);
143
144         gc = widget->style->fg_gc[GTK_STATE_NORMAL];
145         bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
146         font = gtk_style_get_font(widget->style);
147
148         gtk_paint_box (widget->style, ruler->backing_store,
149                        GTK_STATE_NORMAL, GTK_SHADOW_NONE,
150                        NULL, widget, "custom_hruler", 0, 0, widget->allocation.width, widget->allocation.height);
151
152         gdk_draw_line (ruler->backing_store, gc, 0, widget->allocation.height - 1, 
153                        widget->allocation.width, widget->allocation.height - 1);
154
155         if ((ruler->upper - ruler->lower) == 0) {
156                 return;
157         }
158         
159         /* we have to assume a fixed width font here */
160
161         max_chars = widget->allocation.width / gdk_string_width (font, "8");
162
163         nmarks = ruler->metric->get_marks (&marks, ruler->lower, ruler->upper, max_chars);
164
165         for (i = 0; i < nmarks; i++) {
166                 gint pos;
167                 gint height;
168
169                 pos = ROUND ((marks[i].position - ruler->lower) / ruler->metric->units_per_pixel);
170                 height = widget->allocation.height;
171
172                 switch (marks[i].style) {
173                 case GtkCustomRulerMarkMajor:
174                         gdk_draw_line (ruler->backing_store, gc, pos, height, pos, 0);
175                         break;
176                 case GtkCustomRulerMarkMinor:
177                         gdk_draw_line (ruler->backing_store, gc, pos, height, pos, height - (height/2));
178                         break;
179                 case GtkCustomRulerMarkMicro:
180                         gdk_draw_line (ruler->backing_store, gc, pos, height, pos, height - 3);
181                         break;
182                 }
183
184                 gdk_draw_string (ruler->backing_store, font, gc, pos + 2, font->ascent - 1, marks[i].label);
185                 g_free (marks[i].label);
186         }
187         
188         if (nmarks) {
189                 g_free (marks);
190         }
191 }
192
193 static void
194 gtk_custom_hruler_draw_pos (GtkCustomRuler * ruler)
195 {
196         GtkWidget *widget;
197         GdkGC *gc;
198         int i;
199         gint x, y;
200         gint width, height;
201         gint bs_width, bs_height;
202         gint xthickness;
203         gint ythickness;
204         gfloat increment;
205
206         g_return_if_fail (ruler != NULL);
207         g_return_if_fail (GTK_IS_CUSTOM_HRULER (ruler));
208         if (GTK_WIDGET_DRAWABLE (ruler) && (ruler->upper - ruler->lower) > 0) {
209                 widget = GTK_WIDGET (ruler);
210                 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
211                 xthickness = widget->style->xthickness;
212                 ythickness = widget->style->ythickness;
213                 width = widget->allocation.width;
214                 height = widget->allocation.height - ythickness * 2;
215
216                 bs_width = height / 2;
217                 bs_width |= 1;                  /* make sure it's odd */
218                 bs_height = bs_width / 2 + 1;
219
220                 if ((bs_width > 0) && (bs_height > 0)) {
221                         /*  If a backing store exists, restore the ruler  */
222                         if (ruler->backing_store && ruler->non_gr_exp_gc)
223                                 gdk_draw_pixmap (ruler->widget.window,
224                                                  ruler->non_gr_exp_gc,
225                                                  ruler->backing_store, ruler->xsrc, ruler->ysrc, ruler->xsrc, ruler->ysrc, bs_width, bs_height);
226                         
227                         increment = (gfloat) width / (ruler->upper - ruler->lower);
228                         x = ROUND ((ruler->position - ruler->lower) * increment) + (xthickness - bs_width) / 2 - 1;
229                         y = (height + bs_height) / 2 + ythickness;
230
231                         for (i = 0; i < bs_height; i++)
232                                 gdk_draw_line (widget->window, gc, x + i, y + i, x + bs_width - 1 - i, y + i);
233
234
235                         ruler->xsrc = x;
236                         ruler->ysrc = y;
237                 }
238         }
239 }