1 /*
2 * go-marker-selector.c :
3 *
4 * Copyright (C) 2003-2006 Emmanuel Pacaud (emmanuel.pacaud@lapp.in2p3.fr)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 #include <goffice/goffice-config.h>
23 #include <goffice/goffice.h>
24
25
26 typedef struct {
27 GOColor outline_color;
28 GOColor fill_color;
29 gboolean auto_fill;
30 } GOMarkerSelectorState;
31
32 static void
go_marker_palette_render_func(cairo_t * cr,GdkRectangle const * area,int index,gpointer data)33 go_marker_palette_render_func (cairo_t *cr,
34 GdkRectangle const *area,
35 int index,
36 gpointer data)
37 {
38 GOMarker *marker;
39 GOMarkerSelectorState *state = data;
40 int size = 2 * ((int) (MIN (area->width, area->height) * .30));
41
42 if (size < 1)
43 return;
44
45 marker = go_marker_new ();
46 go_marker_set_outline_color (marker, state->outline_color);
47 go_marker_set_size (marker, size);
48 go_marker_set_shape (marker, index);
49 go_marker_set_fill_color (marker, (state->auto_fill && !go_marker_is_closed_shape (marker))? 0: state->fill_color);
50
51 cairo_set_line_width (cr, 1);
52 cairo_set_source_rgb (cr, 1., 1., 1.);
53 cairo_rectangle (cr, area->x + .5 , area->y + .5 , area->width - 1, area->height - 1);
54 cairo_fill_preserve (cr);
55 cairo_set_source_rgb (cr, .75, .75, .75);
56 cairo_stroke (cr);
57
58 go_marker_render (marker, cr, area->x + area->width / 2.0, area->y + area->height / 2.0, 1.0);
59 g_object_unref (marker);
60 }
61
62 /**
63 * go_marker_selector_new:
64 * @initial_shape: marker shape intially selected
65 * @default_shape: automatic marker shape
66 *
67 * Creates a new marker selector.
68 *
69 * Returns: a new #GtkWidget.
70 **/
71 GtkWidget *
go_marker_selector_new(GOMarkerShape initial_shape,GOMarkerShape default_shape)72 go_marker_selector_new (GOMarkerShape initial_shape,
73 GOMarkerShape default_shape)
74 {
75 GtkWidget *palette;
76 GtkWidget *selector;
77 GOMarkerSelectorState *state;
78
79 state = g_new (GOMarkerSelectorState, 1);
80 state->outline_color = GO_COLOR_BLACK;
81 state->fill_color = GO_COLOR_WHITE;
82
83 palette = go_palette_new (GO_MARKER_MAX, 1.0, 4,
84 go_marker_palette_render_func, NULL,
85 state, g_free);
86 go_palette_show_automatic (GO_PALETTE (palette),
87 CLAMP (default_shape, 0, GO_MARKER_MAX -1),
88 NULL);
89 selector = go_selector_new (GO_PALETTE (palette));
90 go_selector_set_active (GO_SELECTOR (selector),
91 CLAMP (initial_shape, 0, GO_MARKER_MAX - 1));
92 return selector;
93 }
94
95 /**
96 * go_marker_selector_set_colors:
97 * @selector: a #GOSelector
98 * @outline: outline color
99 * @fill: fill color
100 *
101 * Updates swatch colors of @selector.
102 **/
103 void
go_marker_selector_set_colors(GOSelector * selector,GOColor outline,GOColor fill)104 go_marker_selector_set_colors (GOSelector *selector, GOColor outline, GOColor fill)
105 {
106 GOMarkerSelectorState *state;
107
108 g_return_if_fail (GO_IS_SELECTOR (selector));
109
110 state = go_selector_get_user_data (selector);
111 g_return_if_fail (state != NULL);
112 state->outline_color = outline;
113 state->fill_color = fill;
114 go_selector_update_swatch (selector);
115 }
116
117 /**
118 * go_marker_selector_set_shape:
119 * @selector: a #GOSelector
120 * @shape: new marker shape
121 *
122 * Updates marker shape of @selector.
123 **/
124 void
go_marker_selector_set_shape(GOSelector * selector,GOMarkerShape shape)125 go_marker_selector_set_shape (GOSelector *selector, GOMarkerShape shape)
126 {
127 g_return_if_fail (GO_IS_SELECTOR (selector));
128
129 go_selector_set_active (GO_SELECTOR (selector),
130 CLAMP (shape, 0, GO_MARKER_MAX - 1));
131 go_selector_update_swatch (selector);
132 }
133
134
135 /**
136 * go_marker_selector_set_auto_fill:
137 * @selector: a #GOSelector
138 * @auto_fill: whether to use a transparent color for opened markers such as
139 * cross, x, or asterisk.
140 *
141 **/
142 void
go_marker_selector_set_auto_fill(GOSelector * selector,gboolean auto_fill)143 go_marker_selector_set_auto_fill (GOSelector *selector, gboolean auto_fill)
144 {
145 GOMarkerSelectorState *state;
146 g_return_if_fail (GO_IS_SELECTOR (selector));
147 state = go_selector_get_user_data (selector);
148 g_return_if_fail (state != NULL);
149 state->auto_fill = auto_fill;
150 }
151