Skip to content
This repository was archived by the owner on Aug 29, 2021. It is now read-only.

Commit b6cc16b

Browse files
committed
Remove hardcoding of scale positions, fix more drawing bugs
1 parent be59bee commit b6cc16b

File tree

9 files changed

+81
-55
lines changed

9 files changed

+81
-55
lines changed

src/goat-plot-enum.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GType goat_orientation_get_type (void)
66

77
if (!t) {
88
static GEnumValue ts[] = {
9+
{GOAT_ORIENTATION_INVALID, "INVALID", "invalid"},
910
{GOAT_ORIENTATION_VERTICAL, "VERTICAL", "vertical"},
1011
{GOAT_ORIENTATION_HORIZONTAL, "HORIZONTAL", "horizontal"},
1112
{0, NULL, NULL},
@@ -21,11 +22,9 @@ GType goat_position_get_type (void)
2122

2223
if (!t) {
2324
static GEnumValue ts[] = {
24-
{GOAT_POSITION_TOP, "TOP", "top"},
25-
{GOAT_POSITION_BOTTOM, "BOTTOM", "bottom"},
26-
{GOAT_POSITION_LEFT, "LEFT", "left"},
27-
{GOAT_POSITION_RIGHT, "RIGHT", "right"},
28-
{0, NULL, NULL},
25+
{GOAT_POSITION_INVALID, "INVALID", "invalid"}, {GOAT_POSITION_TOP, "TOP", "top"},
26+
{GOAT_POSITION_BOTTOM, "BOTTOM", "bottom"}, {GOAT_POSITION_LEFT, "LEFT", "left"},
27+
{GOAT_POSITION_RIGHT, "RIGHT", "right"}, {0, NULL, NULL},
2928
};
3029
t = g_enum_register_static ("GoatPositionTypes", ts);
3130
}

src/goat-plot-enum.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <gtk/gtk.h>
55

66
typedef enum {
7-
GOAT_ORIENTATION_UNKNOWN = 0,
7+
GOAT_ORIENTATION_INVALID = 0,
88
GOAT_ORIENTATION_VERTICAL = 1,
99
GOAT_ORIENTATION_HORIZONTAL = 2
1010
} GoatOrientation;
@@ -15,6 +15,7 @@ GType goat_orientation_get_type (void);
1515

1616

1717
typedef enum {
18+
GOAT_POSITION_INVALID = 0,
1819
GOAT_POSITION_TOP = 1,
1920
GOAT_POSITION_BOTTOM = 2,
2021
GOAT_POSITION_LEFT = 3,

src/goat-plot-internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ gboolean draw_scales (GoatPlot *plot, cairo_t *cr, GtkAllocation *allocation, Gt
4242
g_printf ("scale x : left=%i right=%i top=%i bottom=%i nil=%lf u2pix=%lf\n", left, right, top, bottom, x_nil,
4343
x_unit_to_pixel);
4444

45-
goat_scale_draw (priv->scale_x, cr, left, right, top, bottom, x_nil, x_unit_to_pixel, GOAT_POSITION_TOP, TRUE);
45+
goat_scale_draw (priv->scale_x, cr, left, right, top, bottom, x_nil, x_unit_to_pixel);
4646

4747
g_printf ("scale y : left=%i right=%i top=%i bottom=%i nil=%lf u2pix=%lf\n", left, right, top, bottom, y_nil,
4848
y_unit_to_pixel);
49-
goat_scale_draw (priv->scale_y, cr, left, right, top, bottom, y_nil, y_unit_to_pixel, GOAT_POSITION_LEFT, TRUE);
49+
goat_scale_draw (priv->scale_y, cr, left, right, top, bottom, y_nil, y_unit_to_pixel);
5050
return TRUE;
5151
}
5252

src/goat-scale-interface.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ static void goat_scale_default_init (GoatScaleInterface *iface)
1313
}
1414

1515
void goat_scale_draw (GoatScale *self, cairo_t *cr, gint left, gint right, gint top, gint bottom, gdouble nil,
16-
gdouble factor, GoatPosition where, gboolean grid)
16+
gdouble factor)
1717
{
1818
GoatScaleInterface *iface;
1919

2020
iface = GOAT_SCALE_GET_IFACE (self);
2121
if (iface->draw) {
22-
iface->draw (self, cr, left, right, top, bottom, nil, factor, where, grid);
22+
iface->draw (self, cr, left, right, top, bottom, nil, factor);
2323
} else {
2424
g_error ("Missing draw handler for GoatScaleInterface!");
2525
}
@@ -85,3 +85,13 @@ gboolean goat_scale_is_auto_range (GoatScale *self)
8585
}
8686
return FALSE;
8787
}
88+
89+
void goat_scale_grid_show (GoatScale *self, gboolean show)
90+
{
91+
GoatScaleInterface *iface;
92+
93+
iface = GOAT_SCALE_GET_IFACE (self);
94+
if (iface->show_grid) {
95+
iface->show_grid (self, show);
96+
}
97+
}

src/goat-scale-interface.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ G_DECLARE_INTERFACE (GoatScale, goat_scale, GOAT, SCALE, GObject)
1212
struct _GoatScaleInterface {
1313
GTypeInterface parent_iface;
1414
void (*draw) (GoatScale *self, cairo_t *cr, gint left, gint right, gint top, gint bottom, gdouble nil,
15-
gdouble factor, GoatPosition where, gboolean grid);
15+
gdouble factor);
1616
void (*render) (GoatScale *self);
1717
void (*get_range) (GoatScale *self, gdouble *min, gdouble *max);
1818
void (*set_range) (GoatScale *self, gdouble min, gdouble max);
1919
void (*update_range) (GoatScale *self, gdouble min, gdouble max);
2020
void (*set_range_auto) (GoatScale *self);
2121
void (*set_auto_range) (GoatScale *self);
2222
gboolean (*is_auto_range) (GoatScale *scale);
23+
void (*show_grid) (GoatScale *scale, gboolean show);
2324
};
2425

2526

@@ -33,14 +34,14 @@ void goat_scale_render (GoatScale *self);
3334
* does the actual drawing
3435
*/
3536
void goat_scale_draw (GoatScale *self, cairo_t *cr, gint left, gint right, gint top, gint bottom, gdouble nil,
36-
gdouble factor, GoatPosition where, gboolean grid);
37+
gdouble factor);
3738

3839
void goat_scale_get_range (GoatScale *scale, gdouble *min, gdouble *max);
3940
void goat_scale_set_range_auto (GoatScale *scale);
4041
void goat_scale_set_range (GoatScale *scale, gdouble min, gdouble max);
4142
void goat_scale_update_range (GoatScale *scale, gdouble min, gdouble max);
4243
gboolean goat_scale_is_auto_range (GoatScale *scale);
43-
44+
void goat_scale_grid_show (GoatScale *scale, gboolean show);
4445

4546
G_END_DECLS
4647

src/goat-scale-linear.c

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ struct _GoatScaleLinearPrivate {
2626
gint width_minor;
2727
gint width_major;
2828

29+
gboolean draw_grid;
30+
2931
GoatOrientation orientation;
3032
GoatPosition position;
3133
};
@@ -39,6 +41,7 @@ G_DEFINE_TYPE_WITH_CODE (GoatScaleLinear, goat_scale_linear, G_TYPE_OBJECT,
3941
enum {
4042
PROP_0,
4143

44+
PROP_GRID_VISIBLE,
4245
PROP_ORIENTATION,
4346
PROP_POSITION,
4447

@@ -55,6 +58,9 @@ static void goat_scale_linear_set_property (GObject *object, guint property_id,
5558
GoatScaleLinearPrivate *priv = goat_scale_linear_get_instance_private (self);
5659

5760
switch (property_id) {
61+
case PROP_GRID_VISIBLE:
62+
priv->draw_grid = g_value_get_boolean (value);
63+
break;
5864
case PROP_ORIENTATION:
5965
priv->orientation = g_value_get_enum (value);
6066
break;
@@ -73,6 +79,9 @@ static void goat_scale_linear_get_property (GObject *object, guint property_id,
7379
GoatScaleLinearPrivate *priv = goat_scale_linear_get_instance_private (self);
7480

7581
switch (property_id) {
82+
case PROP_GRID_VISIBLE:
83+
g_value_set_boolean (value, priv->draw_grid);
84+
break;
7685
case PROP_ORIENTATION:
7786
g_value_set_enum (value, priv->orientation);
7887
break;
@@ -97,9 +106,12 @@ static void goat_scale_linear_class_init (GoatScaleLinearClass *klass)
97106
object_class->set_property = goat_scale_linear_set_property;
98107
object_class->get_property = goat_scale_linear_get_property;
99108

109+
obj_properties[PROP_GRID_VISIBLE] =
110+
g_param_spec_boolean ("grid-visible", "Show the grid", "To show or not to show", FALSE, G_PARAM_READWRITE);
111+
100112
obj_properties[PROP_ORIENTATION] =
101113
g_param_spec_enum ("orientation", "Set orientation property", "Set the orientation ot vertical of horizontal",
102-
GOAT_TYPE_ORIENTATION, GOAT_ORIENTATION_HORIZONTAL, G_PARAM_READWRITE);
114+
GOAT_TYPE_ORIENTATION, GOAT_ORIENTATION_INVALID, G_PARAM_READWRITE);
103115
obj_properties[PROP_POSITION] =
104116
g_param_spec_enum ("position", "Set position property", "Set the position to left,right,top or bottom",
105117
GOAT_TYPE_POSITION, GOAT_POSITION_LEFT, G_PARAM_READWRITE);
@@ -113,6 +125,7 @@ static void goat_scale_linear_init (GoatScaleLinear *self)
113125
{
114126
GoatScaleLinearPrivate *priv = self->priv = goat_scale_linear_get_instance_private (self);
115127
goat_scale_set_range_auto (GOAT_SCALE (self));
128+
priv->draw_grid = TRUE;
116129
priv->minors_per_major = 4;
117130
priv->major_delta = 10.;
118131
priv->width_minor = 5;
@@ -129,8 +142,8 @@ static void goat_scale_linear_init (GoatScaleLinear *self)
129142
priv->color_minor_grid.alpha = 0.3;
130143
priv->color_major_grid = priv->color_major;
131144
priv->color_major_grid.alpha = 0.3;
132-
priv->orientation = GOAT_ORIENTATION_HORIZONTAL;
133-
priv->position = GOAT_POSITION_TOP;
145+
priv->orientation = GOAT_ORIENTATION_INVALID;
146+
priv->position = GOAT_POSITION_INVALID;
134147
}
135148

136149
GoatScaleLinear *goat_scale_linear_new (GoatPosition position, GoatOrientation orientation)
@@ -165,7 +178,7 @@ void goat_scale_linear_set_ticks (GoatScaleLinear *scale, gdouble major, gint mi
165178
* @param x/y-factor convert unit to pixel
166179
*/
167180
static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top, gint bottom, gdouble nil,
168-
gdouble factor, GoatPosition where, gboolean grid)
181+
gdouble factor)
169182
{
170183
GoatScaleLinear *self = GOAT_SCALE_LINEAR (scale);
171184
GoatScaleLinearPrivate *priv = goat_scale_linear_get_instance_private (self);
@@ -183,11 +196,14 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
183196
GdkRGBA color_minor_grid = priv->color_minor_grid;
184197
GdkRGBA color_major_grid = priv->color_major_grid;
185198

199+
GoatPosition where = priv->position;
200+
gboolean grid = priv->draw_grid;
201+
186202
cairo_set_line_width (cr, 1.);
187203

188-
for (i = start; i <= end; i++) {
189-
const gboolean register majorstip = (i % priv->minors_per_major == 0);
190-
if (where == GOAT_POSITION_LEFT) {
204+
if (where == GOAT_POSITION_LEFT) {
205+
for (i = start; i <= end; i++) {
206+
const gboolean register majorstip = (i % priv->minors_per_major == 0);
191207
const double register y = nil + top + step_minor * factor * i;
192208
if (grid) {
193209
cairo_move_to (cr, right, y);
@@ -213,7 +229,10 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
213229
const double register x = left - off;
214230
goat_util_draw_num (cr, x, y, step_minor * i, where);
215231
}
216-
if (where == GOAT_POSITION_RIGHT) {
232+
}
233+
if (where == GOAT_POSITION_RIGHT) {
234+
for (i = start; i <= end; i++) {
235+
const gboolean register majorstip = (i % priv->minors_per_major == 0);
217236
const double register y = nil + top + step_minor * factor * i;
218237
if (grid) {
219238
cairo_move_to (cr, left, y);
@@ -225,9 +244,9 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
225244
}
226245
cairo_stroke (cr);
227246
}
228-
cairo_move_to (cr, left, y);
247+
cairo_move_to (cr, right, y);
229248
if (majorstip) {
230-
cairo_line_to (cr, left + width_major, y);
249+
cairo_line_to (cr, right + width_major, y);
231250
gdk_cairo_set_source_rgba (cr, &color_major);
232251
} else {
233252
cairo_line_to (cr, right + width_minor, y);
@@ -239,7 +258,10 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
239258
const double register x = right + off;
240259
goat_util_draw_num (cr, x, y, step_minor * i, where);
241260
}
242-
if (where == GOAT_POSITION_BOTTOM) {
261+
}
262+
if (where == GOAT_POSITION_BOTTOM) {
263+
for (i = start; i <= end; i++) {
264+
const gboolean register majorstip = (i % priv->minors_per_major == 0);
243265
const double register x = nil + left + step_minor * factor * i;
244266
if (grid) {
245267
cairo_move_to (cr, x, top);
@@ -253,19 +275,22 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
253275
}
254276
cairo_move_to (cr, x, bottom);
255277
if (majorstip) {
256-
cairo_line_to (cr, x, bottom + width_major);
278+
cairo_line_to (cr, x, top - width_major);
257279
gdk_cairo_set_source_rgba (cr, &color_major);
258280
} else {
259-
cairo_line_to (cr, x, bottom + width_minor);
281+
cairo_line_to (cr, x, top - width_minor);
260282
gdk_cairo_set_source_rgba (cr, &color_minor);
261283
}
262284
cairo_stroke (cr);
263285

264286
const double off = majorstip ? width_major : width_minor;
265-
const double register y = bottom + off;
287+
const double register y = top - off;
266288
goat_util_draw_num (cr, x, y, step_minor * i, where);
267289
}
268-
if (where == GOAT_POSITION_TOP) {
290+
}
291+
if (where == GOAT_POSITION_TOP) {
292+
for (i = start; i <= end; i++) {
293+
const gboolean register majorstip = (i % priv->minors_per_major == 0);
269294
const double register x = nil + left + step_minor * factor * i;
270295
if (grid) {
271296
cairo_move_to (cr, x, bottom);
@@ -277,19 +302,18 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
277302
}
278303
cairo_stroke (cr);
279304
}
280-
cairo_move_to (cr, x, top);
305+
cairo_move_to (cr, x, bottom);
281306
if (majorstip) {
282-
cairo_line_to (cr, x, top - width_major);
307+
cairo_line_to (cr, x, bottom + width_major);
283308
gdk_cairo_set_source_rgba (cr, &color_major);
284309
} else {
285-
cairo_line_to (cr, x, top - width_minor);
310+
cairo_line_to (cr, x, bottom + width_minor);
286311
gdk_cairo_set_source_rgba (cr, &color_minor);
287312
}
288313
cairo_stroke (cr);
289314

290315
const double off = majorstip ? width_major : width_minor;
291-
const double register y = top + off;
292-
goat_util_draw_num (cr, x, y, step_minor * i, where);
316+
goat_util_draw_num (cr, x, bottom + off, step_minor * i, where);
293317
}
294318
}
295319
}
@@ -349,6 +373,13 @@ static gboolean is_auto_range (GoatScale *scale)
349373
return self->priv->autorange;
350374
}
351375

376+
static void show_grid (GoatScale *scale, gboolean show)
377+
{
378+
GoatScaleLinear *self = GOAT_SCALE_LINEAR (scale);
379+
380+
self->priv->draw_grid = show;
381+
}
382+
352383
static void goat_scale_linear_interface_init (GoatScaleInterface *iface)
353384
{
354385
iface->draw = draw;
@@ -359,4 +390,5 @@ static void goat_scale_linear_interface_init (GoatScaleInterface *iface)
359390
iface->update_range = update_range;
360391
iface->is_auto_range = is_auto_range;
361392
iface->get_range = get_range;
393+
iface->show_grid = show_grid;
362394
}

src/goat-scale-linear.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ struct _GoatScaleLinearClass {
3333
GType goat_scale_linear_get_type (void) G_GNUC_CONST;
3434
GoatScaleLinear *goat_scale_linear_new (GoatPosition position, GoatOrientation orientation);
3535

36-
gboolean goat_scale_linear_draw (GoatScaleLinear *scale, cairo_t *cr, int left, int right, int top, int bottom,
37-
double nil, gdouble factor, GoatPosition where, gboolean grid);
38-
3936
void goat_scale_linear_set_ticks (GoatScaleLinear *scale, gdouble major_step, gint minors_per_major);
4037
void goat_scale_linear_set_label (GoatScaleLinear *plot, gchar *label);
4138

src/goat-utils.h

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,23 @@ void goat_util_draw_num (cairo_t *cr, double x, double y, double d, GoatPosition
2222

2323
PangoRectangle logrect;
2424
pango_layout_get_pixel_extents (lay, NULL, &logrect);
25-
g_print ("logrect//// x=%i y=%i w=%i h=%i\n", logrect.x, logrect.y, logrect.width, logrect.height);
26-
g_print ("x=%lf y=%lf\n\n", x, y);
2725
double modifierx, modifiery;
28-
{
29-
GEnumClass *enum_class;
30-
GEnumValue *enum_value;
31-
32-
enum_class = g_type_class_ref (GOAT_TYPE_POSITION);
33-
enum_value = g_enum_get_value (enum_class, penalty);
34-
35-
g_print ("number where: %s\n", enum_value->value_name);
36-
37-
g_type_class_unref (enum_class);
38-
}
3926
switch (penalty) {
4027
case GOAT_POSITION_BOTTOM:
4128
modifierx = (double)(logrect.width) * -0.5;
42-
modifiery = (double)(logrect.height) * -1.;
29+
modifiery = (double)(logrect.height) * 0.;
4330
break;
4431
case GOAT_POSITION_LEFT:
4532
modifierx = (double)(logrect.width) * -1.0;
4633
modifiery = (double)(logrect.height) * 0.5;
4734
break;
4835
case GOAT_POSITION_RIGHT:
49-
modifierx = (double)(logrect.width);
36+
modifierx = (double)(logrect.width) * 0.;
5037
modifiery = (double)(logrect.height) * 0.5;
5138
break;
5239
case GOAT_POSITION_TOP:
53-
modifierx = (double)(logrect.width)* -0.5;
54-
modifiery = (double)(logrect.height) * -1.;
40+
modifierx = (double)(logrect.width) * -0.5;
41+
modifiery = (double)(logrect.height) * 1.0;
5542
break;
5643
}
5744
cairo_move_to (cr, x + modifierx, y + modifiery);

tests/dynamic.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ int main (int argc, char *argv[])
3030
{
3131
GtkWidget *window;
3232
GoatPlot *plot;
33-
int i;
3433

3534
gtk_init (&argc, &argv);
3635

3736
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
3837

3938
GoatScale *scale_x = GOAT_SCALE (goat_scale_linear_new (GOAT_POSITION_BOTTOM, GOAT_ORIENTATION_HORIZONTAL));
40-
GoatScale *scale_y = GOAT_SCALE (goat_scale_linear_new (GOAT_POSITION_LEFT, GOAT_ORIENTATION_VERTICAL));
39+
GoatScale *scale_y = GOAT_SCALE (goat_scale_linear_new (GOAT_POSITION_RIGHT, GOAT_ORIENTATION_VERTICAL));
4140

4241
plot = goat_plot_new (scale_x, scale_y);
4342

0 commit comments

Comments
 (0)