@@ -26,6 +26,8 @@ struct _GoatScaleLinearPrivate {
2626gint width_minor ;
2727gint width_major ;
2828
29+ gboolean draw_grid ;
30+
2931GoatOrientation orientation ;
3032GoatPosition position ;
3133};
@@ -39,6 +41,7 @@ G_DEFINE_TYPE_WITH_CODE (GoatScaleLinear, goat_scale_linear, G_TYPE_OBJECT,
3941enum {
4042PROP_0 ,
4143
44+ PROP_GRID_VISIBLE ,
4245PROP_ORIENTATION ,
4346PROP_POSITION ,
4447
@@ -55,6 +58,9 @@ static void goat_scale_linear_set_property (GObject *object, guint property_id,
5558GoatScaleLinearPrivate * priv = goat_scale_linear_get_instance_private (self );
5659
5760switch (property_id ) {
61+ case PROP_GRID_VISIBLE :
62+ priv -> draw_grid = g_value_get_boolean (value );
63+ break ;
5864case PROP_ORIENTATION :
5965priv -> orientation = g_value_get_enum (value );
6066break ;
@@ -73,6 +79,9 @@ static void goat_scale_linear_get_property (GObject *object, guint property_id,
7379GoatScaleLinearPrivate * priv = goat_scale_linear_get_instance_private (self );
7480
7581switch (property_id ) {
82+ case PROP_GRID_VISIBLE :
83+ g_value_set_boolean (value , priv -> draw_grid );
84+ break ;
7685case PROP_ORIENTATION :
7786g_value_set_enum (value , priv -> orientation );
7887break ;
@@ -97,9 +106,12 @@ static void goat_scale_linear_class_init (GoatScaleLinearClass *klass)
97106object_class -> set_property = goat_scale_linear_set_property ;
98107object_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+
100112obj_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 );
103115obj_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{
114126GoatScaleLinearPrivate * priv = self -> priv = goat_scale_linear_get_instance_private (self );
115127goat_scale_set_range_auto (GOAT_SCALE (self ));
128+ priv -> draw_grid = TRUE;
116129priv -> minors_per_major = 4 ;
117130priv -> major_delta = 10. ;
118131priv -> width_minor = 5 ;
@@ -129,8 +142,8 @@ static void goat_scale_linear_init (GoatScaleLinear *self)
129142priv -> color_minor_grid .alpha = 0.3 ;
130143priv -> color_major_grid = priv -> color_major ;
131144priv -> 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
136149GoatScaleLinear * 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 */
167180static 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{
170183GoatScaleLinear * self = GOAT_SCALE_LINEAR (scale );
171184GoatScaleLinearPrivate * 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
183196GdkRGBA color_minor_grid = priv -> color_minor_grid ;
184197GdkRGBA color_major_grid = priv -> color_major_grid ;
185198
199+ GoatPosition where = priv -> position ;
200+ gboolean grid = priv -> draw_grid ;
201+
186202cairo_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 );
191207const double register y = nil + top + step_minor * factor * i ;
192208if (grid ) {
193209cairo_move_to (cr , right , y );
@@ -213,7 +229,10 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
213229const double register x = left - off ;
214230goat_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 );
217236const double register y = nil + top + step_minor * factor * i ;
218237if (grid ) {
219238cairo_move_to (cr , left , y );
@@ -225,9 +244,9 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
225244}
226245cairo_stroke (cr );
227246}
228- cairo_move_to (cr , left , y );
247+ cairo_move_to (cr , right , y );
229248if (majorstip ) {
230- cairo_line_to (cr , left + width_major , y );
249+ cairo_line_to (cr , right + width_major , y );
231250gdk_cairo_set_source_rgba (cr , & color_major );
232251} else {
233252cairo_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
239258const double register x = right + off ;
240259goat_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 );
243265const double register x = nil + left + step_minor * factor * i ;
244266if (grid ) {
245267cairo_move_to (cr , x , top );
@@ -253,19 +275,22 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
253275}
254276cairo_move_to (cr , x , bottom );
255277if (majorstip ) {
256- cairo_line_to (cr , x , bottom + width_major );
278+ cairo_line_to (cr , x , top - width_major );
257279gdk_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 );
260282gdk_cairo_set_source_rgba (cr , & color_minor );
261283}
262284cairo_stroke (cr );
263285
264286const double off = majorstip ? width_major : width_minor ;
265- const double register y = bottom + off ;
287+ const double register y = top - off ;
266288goat_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 );
269294const double register x = nil + left + step_minor * factor * i ;
270295if (grid ) {
271296cairo_move_to (cr , x , bottom );
@@ -277,19 +302,18 @@ static void draw (GoatScale *scale, cairo_t *cr, gint left, gint right, gint top
277302}
278303cairo_stroke (cr );
279304}
280- cairo_move_to (cr , x , top );
305+ cairo_move_to (cr , x , bottom );
281306if (majorstip ) {
282- cairo_line_to (cr , x , top - width_major );
307+ cairo_line_to (cr , x , bottom + width_major );
283308gdk_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 );
286311gdk_cairo_set_source_rgba (cr , & color_minor );
287312}
288313cairo_stroke (cr );
289314
290315const 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)
349373return 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+
352383static void goat_scale_linear_interface_init (GoatScaleInterface * iface )
353384{
354385iface -> draw = draw ;
@@ -359,4 +390,5 @@ static void goat_scale_linear_interface_init (GoatScaleInterface *iface)
359390iface -> update_range = update_range ;
360391iface -> is_auto_range = is_auto_range ;
361392iface -> get_range = get_range ;
393+ iface -> show_grid = show_grid ;
362394}
0 commit comments