File: gwyvruler.c

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (286 lines) | stat: -rw-r--r-- 9,044 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *  $Id: gwyvruler.c 20678 2017-12-18 18:26:55Z yeti-dn $
 *  Copyright (C) 2003 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

/*
 * Modified by Yeti 2003.  In fact, rewritten, except the skeleton
 * and a few drawing functions.  Rewritten again in 2005 to minimize
 * code duplication.
 */

#include "config.h"
#include <string.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include "gwyvruler.h"

#define RULER_WIDTH 20

static void     gwy_vruler_realize      (GtkWidget *widget);
static gboolean gwy_vruler_motion_notify(GtkWidget *widget,
                                         GdkEventMotion *event);
static void     gwy_vruler_prepare_sizes(GwyRuler *ruler);
static void     gwy_vruler_draw_frame   (GwyRuler *ruler);
static void     gwy_vruler_draw_layout  (GwyRuler *ruler,
                                         gint hpos,
                                         gint vpos);
static void     gwy_vruler_draw_tick    (GwyRuler *ruler,
                                         gint pos,
                                         gint length);
static void     gwy_vruler_draw_pos     (GwyRuler *ruler);


G_DEFINE_TYPE(GwyVRuler, gwy_vruler, GWY_TYPE_RULER)

static void
gwy_vruler_class_init(GwyVRulerClass *klass)
{
    GtkWidgetClass *widget_class;
    GwyRulerClass *ruler_class;

    widget_class = (GtkWidgetClass*) klass;
    ruler_class = (GwyRulerClass*) klass;

    widget_class->motion_notify_event = gwy_vruler_motion_notify;
    widget_class->realize = gwy_vruler_realize;

    ruler_class->prepare_sizes = gwy_vruler_prepare_sizes;
    ruler_class->draw_frame = gwy_vruler_draw_frame;
    ruler_class->draw_layout = gwy_vruler_draw_layout;
    ruler_class->draw_tick = gwy_vruler_draw_tick;
    ruler_class->draw_pos = gwy_vruler_draw_pos;
}

static void
gwy_vruler_init(GwyVRuler *vruler)
{
    GtkWidget *widget;

    widget = GTK_WIDGET(vruler);
    widget->requisition.width = widget->style->xthickness*2 + RULER_WIDTH;
    widget->requisition.height = widget->style->ythickness*2 + 1;
}

/**
 * gwy_vruler_new:
 *
 * Creates a new #GwyVRuler.
 *
 * Returns: The new ruler as a #GtkWidget.
 **/
GtkWidget*
gwy_vruler_new(void)
{
    return g_object_new(GWY_TYPE_VRULER, NULL);
}

static void
gwy_vruler_realize(GtkWidget *widget)
{
    const PangoMatrix *cmatrix;
    PangoMatrix matrix = PANGO_MATRIX_INIT;
    PangoContext *context;
    GwyRuler *ruler;

    if (GTK_WIDGET_CLASS(gwy_vruler_parent_class)->realize)
        (GTK_WIDGET_CLASS(gwy_vruler_parent_class)->realize)(widget);

    ruler = GWY_RULER(widget);

    context = pango_layout_get_context(ruler->layout);
    if ((cmatrix = pango_context_get_matrix(context)))
        matrix = *cmatrix;
    pango_matrix_rotate(&matrix, -90.0);
    pango_context_set_matrix(context, &matrix);
    pango_layout_context_changed(ruler->layout);
}

static gboolean
gwy_vruler_motion_notify(GtkWidget      *widget,
                         GdkEventMotion *event)
{
    GwyRuler *ruler;
    gint y;

    ruler = GWY_RULER(widget);

    if (event->is_hint)
        gdk_window_get_pointer(widget->window, NULL, &y, NULL);
    else
        y = event->y;

    ruler->position = ruler->lower + ((ruler->upper - ruler->lower) * y)
                                     / widget->allocation.height;
    g_object_notify(G_OBJECT(ruler), "position");

    /*  Make sure the ruler has been allocated already  */
    if (ruler->backing_store != NULL)
        gwy_ruler_draw_pos(ruler);

    return FALSE;
}

static void
gwy_vruler_prepare_sizes(GwyRuler *ruler)
{
    GtkWidget *widget;

    widget = GTK_WIDGET(ruler);
    ruler->hthickness = widget->style->ythickness;
    ruler->vthickness = widget->style->xthickness;
    ruler->height = widget->allocation.width - 2*ruler->vthickness;
    ruler->pixelsize = widget->allocation.height;
}

static void
gwy_vruler_draw_frame(GwyRuler *ruler)
{
    GdkGC *gc;
    GtkWidget *widget;

    widget = GTK_WIDGET(ruler);
    gc = widget->style->fg_gc[GTK_STATE_NORMAL];
    gdk_draw_line(ruler->backing_store, gc,
                  ruler->pixelsize + ruler->vthickness,
                  ruler->hthickness,
                  ruler->pixelsize + ruler->vthickness,
                  ruler->pixelsize - ruler->hthickness);
}

static void
gwy_vruler_draw_layout(GwyRuler *ruler,
                       gint hpos,
                       gint vpos)
{
    GtkWidget *widget;

    widget = GTK_WIDGET(ruler);
    gtk_paint_layout(widget->style,
                     ruler->backing_store,
                     GTK_WIDGET_STATE(widget),
                     FALSE,
                     NULL,
                     widget,
                     "vruler",
                     vpos, hpos + 3,
                     ruler->layout);
}

static void
gwy_vruler_draw_tick(GwyRuler *ruler,
                     gint pos,
                     gint length)
{
    GdkGC *gc;
    GtkWidget *widget;

    widget = GTK_WIDGET(ruler);
    gc = widget->style->fg_gc[GTK_STATE_NORMAL];
    gdk_draw_line(ruler->backing_store, gc,
                  ruler->height + ruler->vthickness, pos,
                  ruler->height - length + ruler->vthickness, pos);
}

static void
gwy_vruler_draw_pos(GwyRuler *ruler)
{
    GtkWidget *widget;
    GdkGC *gc;
    int i;
    gint x, y;
    gint bs_width, bs_height;
    gdouble increment;

    if (GTK_WIDGET_DRAWABLE(ruler)) {
        widget = GTK_WIDGET(ruler);
        gc = widget->style->fg_gc[GTK_STATE_NORMAL];

        bs_height = ruler->height/2;
        bs_height |= 1;  /* make sure it's odd */
        bs_width = bs_height/2 + 1;

        if ((bs_width > 0) && (bs_height > 0)) {
            /*  If a backing store exists, restore the ruler  */
            if (ruler->backing_store && ruler->non_gr_exp_gc)
                gdk_draw_drawable(ruler->widget.window,
                                  ruler->non_gr_exp_gc,
                                  ruler->backing_store,
                                  ruler->xsrc, ruler->ysrc,
                                  ruler->xsrc, ruler->ysrc,
                                  bs_width, bs_height);

            increment = (gdouble)ruler->pixelsize/(ruler->upper - ruler->lower);

            x = (ruler->height + bs_width)/2 + ruler->vthickness;
            y = GWY_ROUND((ruler->position - ruler->lower) * increment)
                + (ruler->hthickness - bs_height)/2 - 1;

            for (i = 0; i < bs_width; i++)
                gdk_draw_line(widget->window, gc,
                              x + i, y + i,
                              x + i, y + bs_height - 1 - i);

            ruler->xsrc = x;
            ruler->ysrc = y;
        }
    }
}

/************************** Documentation ****************************/

/**
 * SECTION:gwyvruler
 * @title: GwyVRuler
 * @short_description: Vertical ruler, similar to GtkRuler
 * @see_also: #GwyHRuler -- horizontal ruler
 *
 * Please see #GwyRuler for differences from #GtkVRuler.
 *
 * A specific #GwyVRuler feature is that it's drawn vertically including text,
 * like a rotated horizontal ruler.
 **/

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */