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
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpcellrendererdashes.c
* Copyright (C) 2005 Sven Neumann <sven@gimp.org>
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <gtk/gtk.h>
#include "widgets-types.h"
#include "core/gimpdashpattern.h"
#include "gimpcellrendererdashes.h"
#define DASHES_WIDTH 96
#define DASHES_HEIGHT 4
#define N_SEGMENTS 24
#define BLOCK_WIDTH (DASHES_WIDTH / (2 * N_SEGMENTS))
enum
{
PROP_0,
PROP_PATTERN
};
static void gimp_cell_renderer_dashes_finalize (GObject *object);
static void gimp_cell_renderer_dashes_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void gimp_cell_renderer_dashes_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_cell_renderer_dashes_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *rectangle,
gint *x_offset,
gint *y_offset,
gint *width,
gint *height);
static void gimp_cell_renderer_dashes_render (GtkCellRenderer *cell,
GdkWindow *window,
GtkWidget *widget,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GdkRectangle *expose_area,
GtkCellRendererState flags);
G_DEFINE_TYPE (GimpCellRendererDashes, gimp_cell_renderer_dashes,
GTK_TYPE_CELL_RENDERER)
#define parent_class gimp_cell_renderer_dashes_parent_class
static void
gimp_cell_renderer_dashes_class_init (GimpCellRendererDashesClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
object_class->finalize = gimp_cell_renderer_dashes_finalize;
object_class->get_property = gimp_cell_renderer_dashes_get_property;
object_class->set_property = gimp_cell_renderer_dashes_set_property;
cell_class->get_size = gimp_cell_renderer_dashes_get_size;
cell_class->render = gimp_cell_renderer_dashes_render;
g_object_class_install_property (object_class, PROP_PATTERN,
g_param_spec_boxed ("pattern", NULL, NULL,
GIMP_TYPE_DASH_PATTERN,
GIMP_PARAM_WRITABLE));
}
static void
gimp_cell_renderer_dashes_init (GimpCellRendererDashes *dashes)
{
dashes->segments = g_new0 (gboolean, N_SEGMENTS);
}
static void
gimp_cell_renderer_dashes_finalize (GObject *object)
{
GimpCellRendererDashes *dashes = GIMP_CELL_RENDERER_DASHES (object);
g_free (dashes->segments);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_cell_renderer_dashes_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
}
static void
gimp_cell_renderer_dashes_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
GimpCellRendererDashes *dashes = GIMP_CELL_RENDERER_DASHES (object);
switch (param_id)
{
case PROP_PATTERN:
gimp_dash_pattern_fill_segments (g_value_get_boxed (value),
dashes->segments, N_SEGMENTS);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
gimp_cell_renderer_dashes_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *cell_area,
gint *x_offset,
gint *y_offset,
gint *width,
gint *height)
{
gfloat xalign, yalign;
gint xpad, ypad;
gtk_cell_renderer_get_alignment (cell, &xalign, &yalign);
gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
if (cell_area)
{
if (x_offset)
{
gdouble align;
align = ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
1.0 - xalign : xalign);
*x_offset = align * (cell_area->width - DASHES_WIDTH);
*x_offset = MAX (*x_offset, 0) + xpad;
}
if (y_offset)
{
*y_offset = yalign * (cell_area->height - DASHES_HEIGHT);
*y_offset = MAX (*y_offset, 0) + ypad;
}
}
else
{
if (x_offset)
*x_offset = 0;
if (y_offset)
*y_offset = 0;
}
*width = DASHES_WIDTH + 2 * xpad;
*height = DASHES_HEIGHT + 2 * ypad;
}
static void
gimp_cell_renderer_dashes_render (GtkCellRenderer *cell,
GdkWindow *window,
GtkWidget *widget,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GdkRectangle *expose_area,
GtkCellRendererState flags)
{
GimpCellRendererDashes *dashes = GIMP_CELL_RENDERER_DASHES (cell);
GtkStyle *style = gtk_widget_get_style (widget);
GtkStateType state;
gint xpad, ypad;
cairo_t *cr;
gint width;
gint x, y;
gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
if (! gtk_cell_renderer_get_sensitive (cell))
{
state = GTK_STATE_INSENSITIVE;
}
else if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
{
if (gtk_widget_has_focus (widget))
state = GTK_STATE_SELECTED;
else
state = GTK_STATE_ACTIVE;
}
else if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT &&
gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
{
state = GTK_STATE_PRELIGHT;
}
else
{
if (gtk_widget_is_sensitive (widget))
state = GTK_STATE_NORMAL;
else
state = GTK_STATE_INSENSITIVE;
}
y = cell_area->y + (cell_area->height - DASHES_HEIGHT) / 2;
width = cell_area->width - 2 * xpad;
cr = gdk_cairo_create (window);
gdk_cairo_rectangle (cr, expose_area);
cairo_clip (cr);
for (x = 0; x < width + BLOCK_WIDTH; x += BLOCK_WIDTH)
{
guint index = ((guint) x / BLOCK_WIDTH) % N_SEGMENTS;
if (dashes->segments[index])
{
cairo_rectangle (cr,
cell_area->x + xpad + x, y,
MIN (BLOCK_WIDTH, width - x), DASHES_HEIGHT);
}
}
gdk_cairo_set_source_color (cr, &style->text[state]);
cairo_fill (cr);
cairo_destroy (cr);
}
GtkCellRenderer *
gimp_cell_renderer_dashes_new (void)
{
return g_object_new (GIMP_TYPE_CELL_RENDERER_DASHES, NULL);
}
|