1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2013 Maxime DOYEN
4 * This file is part of HomeBank.
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * HomeBank 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "gtk-chart-colors.h"
27 #include "gtk-chart-stack.h"
42 /* --- prototypes --- */
43 static void ui_chart_stack_class_init (ChartStackClass
*klass
);
44 static void ui_chart_stack_init (ChartStack
*chart
);
45 static void ui_chart_stack_destroy (GtkWidget
*chart
);
46 /*static void ui_chart_stack_set_property (GObject *object,
51 static gboolean
drawarea_configure_event_callback (GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer user_data
);
52 static gboolean
drawarea_draw_callback(GtkWidget
*widget
, cairo_t
*cr
, gpointer user_data
);
53 static gboolean
drawarea_scroll_event_callback( GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
);
54 static gboolean
drawarea_motionnotifyevent_callback(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer user_data
);
55 static void ui_chart_stack_first_changed( GtkAdjustment
*adj
, gpointer user_data
);
57 static void ui_chart_stack_clear(ChartStack
*chart
, gboolean store
);
59 static void ui_chart_stack_queue_redraw(ChartStack
*chart
);
61 /* --- variables --- */
62 static GtkBoxClass
*parent_class
= NULL
;
65 /* --- functions --- */
66 GType
ui_chart_stack_get_type ()
68 static GType ui_chart_stack_type
= 0;
70 if (G_UNLIKELY(ui_chart_stack_type
== 0))
72 const GTypeInfo ui_chart_stack_info
=
74 sizeof (ChartStackClass
),
76 NULL
, /* base_finalize */
77 (GClassInitFunc
) ui_chart_stack_class_init
,
78 NULL
, /* class_finalize */
79 NULL
, /* class_init */
82 (GInstanceInitFunc
) ui_chart_stack_init
,
83 NULL
/* value_table */
86 ui_chart_stack_type
= g_type_register_static (GTK_TYPE_BOX
, "ChartStack",
87 &ui_chart_stack_info
, 0);
90 return ui_chart_stack_type
;
94 static void ui_chart_stack_class_init (ChartStackClass
* class)
96 //GObjectClass *gobject_class;
97 GtkWidgetClass
*widget_class
;
99 DB( g_print("\n[chartstack] class_init\n") );
101 //gobject_class = (GObjectClass*) class;
102 widget_class
= (GtkWidgetClass
*) class;
104 parent_class
= g_type_class_peek_parent (class);
106 //gobject_class->dispose = ui_chart_stack_dispose;
107 //gobject_class->finalize = ui_chart_stack_finalize;
108 //gobject_class->set_property = ui_chart_stack_set_property;
109 //gobject_class->get_property = ui_chart_stack_get_property;
111 widget_class
->destroy
= ui_chart_stack_destroy
;
116 /* get/set properties goes here */
120 ui_chart_stack_init (ChartStack
* chart
)
122 GtkWidget
*widget
, *hbox
, *scrollwin
;
125 DB( g_print("\n[chartstack] init\n") );
127 chart
->surface
= NULL
;
131 chart
->subtitle
= NULL
;
134 chart
->budget_title
= "Budget";
135 chart
->result_title
= "Result";
137 chart
->barw
= GTK_CHARTSTACK_BARW
;
138 ui_chart_stack_set_color_scheme(chart
, CHART_COLMAP_HOMEBANK
);
140 widget
=GTK_WIDGET(chart
);
142 gtk_box_set_homogeneous(GTK_BOX(widget
), FALSE
);
144 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
145 gtk_box_pack_start (GTK_BOX (widget
), hbox
, TRUE
, TRUE
, 0);
148 scrollwin
= gtk_frame_new(NULL
);
149 gtk_frame_set_shadow_type (GTK_FRAME(scrollwin
), GTK_SHADOW_ETCHED_IN
);
150 gtk_box_pack_start (GTK_BOX (hbox
), scrollwin
, TRUE
, TRUE
, 0);
152 //scrollwin = gtk_scrolled_window_new(NULL,NULL);
153 //gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwin), GTK_SHADOW_ETCHED_IN);
154 //gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
155 //gtk_box_pack_start (GTK_BOX (hbox), scrollwin, TRUE, TRUE, 0);
157 chart
->drawarea
= gtk_drawing_area_new();
158 //gtk_widget_set_double_buffered (GTK_WIDGET(widget), FALSE);
160 gtk_container_add( GTK_CONTAINER(scrollwin
), chart
->drawarea
);
161 gtk_widget_set_size_request(chart
->drawarea
, 150, 150 );
162 gtk_widget_set_has_tooltip(chart
->drawarea
, FALSE
);
163 gtk_widget_show(chart
->drawarea
);
166 chart
->adjustment
= GTK_ADJUSTMENT(gtk_adjustment_new (0.0, 0.0, 1.0, 1.0, 1.0, 1.0));
167 chart
->scrollbar
= gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL
,GTK_ADJUSTMENT (chart
->adjustment
));
168 gtk_box_pack_start (GTK_BOX (hbox
), chart
->scrollbar
, FALSE
, TRUE
, 0);
170 gtk_widget_set_events(GTK_WIDGET(chart
->drawarea
),
172 //GDK_POINTER_MOTION_MASK |
173 //GDK_POINTER_MOTION_HINT_MASK |
177 g_signal_connect( G_OBJECT(chart
->drawarea
), "configure-event", G_CALLBACK (drawarea_configure_event_callback
), chart
);
178 //g_signal_connect( G_OBJECT(chart->drawarea), "realize", G_CALLBACK(drawarea_realize_callback), chart ) ;
179 g_signal_connect( G_OBJECT(chart
->drawarea
), "draw", G_CALLBACK(drawarea_draw_callback
), chart
);
180 //g_signal_connect( G_OBJECT(chart->drawarea), "query-tooltip", G_CALLBACK(drawarea_querytooltip_callback), chart );
181 g_signal_connect( G_OBJECT(chart
->drawarea
), "scroll-event", G_CALLBACK(drawarea_scroll_event_callback
), chart
) ;
182 g_signal_connect( G_OBJECT(chart
->drawarea
), "motion-notify-event", G_CALLBACK(drawarea_motionnotifyevent_callback
), chart
);
184 g_signal_connect (G_OBJECT(chart
->adjustment
), "value-changed", G_CALLBACK (ui_chart_stack_first_changed
), chart
);
187 g_signal_connect( G_OBJECT(chart->drawarea), "leave-notify-event", G_CALLBACK(ui_chart_stack_leave), chart );
188 g_signal_connect( G_OBJECT(chart->drawarea), "enter-notify-event", G_CALLBACK(ui_chart_stack_enter), chart );
189 g_signal_connect( G_OBJECT(chart->drawarea), "button-press-event", G_CALLBACK(ui_chart_stack_button_press), chart );
190 g_signal_connect( G_OBJECT(chart->drawarea), "button-release-event", G_CALLBACK(ui_chart_stack_button_release), chart );
198 ui_chart_stack_new (void)
202 chart
= (GtkWidget
*)g_object_new (GTK_TYPE_CHARTSTACK
, NULL
);
209 ui_chart_stack_destroy (GtkWidget
* object
)
211 ChartStack
*chart
= GTK_CHARTSTACK(object
);
213 g_return_if_fail (GTK_IS_CHARTSTACK (object
));
215 ui_chart_stack_clear(GTK_CHARTSTACK (object
), FALSE
);
219 pango_font_description_free (chart
->pfd
);
225 cairo_surface_destroy (chart
->surface
);
226 chart
->surface
= NULL
;
229 GTK_WIDGET_CLASS (parent_class
)->destroy (object
);
236 ** print a integer number
238 static gchar
*ui_chart_stack_print_int(ChartStack
*chart
, gdouble value
)
241 hb_strfmon(chart
->buffer
, CHART_BUFFER_LENGTH
-1, value
, chart
->kcur
, chart
->minor
);
242 return chart
->buffer
;
246 static void ui_chart_stack_clear(ChartStack
*chart
, gboolean store
)
249 DB( g_print("\n[chartstack] clear\n") );
251 //free & clear any previous allocated datas
252 if(chart
->title
!= NULL
)
254 g_free(chart
->title
);
258 if(chart
->subtitle
!= NULL
)
260 g_free(chart
->subtitle
);
261 chart
->subtitle
= NULL
;
264 if(chart
->items
!= NULL
)
266 /*for(i=0;i<chart->nb_items;i++)
268 StackItem *item = &g_array_index(chart->items, StackItem, i);
270 //g_free(item->legend);
272 g_array_free(chart
->items
, TRUE
);
281 static void ui_chart_stack_setup_with_model(ChartStack
*chart
, GtkTreeModel
*list_store
, gchar
*coltitle1
, gchar
*coltitle2
)
287 DB( g_print("\n[chartstack] setup with model\n") );
289 ui_chart_stack_clear(chart
, TRUE
);
291 chart
->nb_items
= gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list_store
), NULL
);
293 chart
->items
= g_array_sized_new(FALSE
, FALSE
, sizeof(StackItem
), chart
->nb_items
);
295 DB( g_print(" nb=%d\n", chart
->nb_items
) );
298 chart
->budget_title
= coltitle1
;
300 chart
->result_title
= coltitle2
;
302 /* Get the first iter in the list */
303 valid
= gtk_tree_model_get_iter_first (GTK_TREE_MODEL(list_store
), &iter
);
308 gchar
*label
, *status
;
309 gdouble value1
, value2
;
312 gtk_tree_model_get (GTK_TREE_MODEL(list_store
), &iter
,
324 item
.budget
= value2
;
325 item
.status
= status
;
327 /* additional pre-compute */
328 item
.result
= item
.spent
- item
.budget
;
330 if(ABS(item
.budget
) > 0)
332 item
.rawrate
= item
.spent
/ item
.budget
;
335 item
.warn
= item
.result
< 0.0 ? TRUE
: FALSE
;
337 item
.rate
= CLAMP(item
.rawrate
, 0, 1.0);
339 g_array_append_vals(chart
->items
, &item
, 1);
342 valid
= gtk_tree_model_iter_next (list_store
, &iter
);
348 static void ui_chart_stack_set_font_size(ChartStack
*chart
, gint font_size
)
352 DB( g_print("\n[chartstack] set font size\n") );
356 case CHART_FONT_SIZE_TITLE
:
357 size
= chart
->pfd_size
+ 3;
359 case CHART_FONT_SIZE_SUBTITLE
:
360 size
= chart
->pfd_size
+ 1;
362 case CHART_FONT_SIZE_NORMAL
:
363 size
= chart
->pfd_size
- 1;
367 DB( g_print(" size=%d\n", size
) );
369 pango_font_description_set_size(chart
->pfd
, size
* PANGO_SCALE
);
375 static void ui_chart_stack_calculation(ChartStack
*chart
)
377 GtkWidget
*drawarea
= chart
->drawarea
;
378 cairo_surface_t
*surf
;
384 GtkAllocation allocation
;
387 DB( g_print("\n[chartstack] bar calculation\n") );
389 gtk_widget_get_allocation(drawarea
, &allocation
);
391 chart
->l
= CHART_MARGIN
;
392 chart
->t
= CHART_MARGIN
;
393 chart
->r
= allocation
.width
- CHART_MARGIN
;
394 chart
->b
= allocation
.height
- CHART_MARGIN
;
395 chart
->w
= allocation
.width
- (CHART_MARGIN
*2);
396 chart
->h
= allocation
.height
- (CHART_MARGIN
*2);
398 //todo: seems not working well...
399 surf
= cairo_image_surface_create(CAIRO_FORMAT_ARGB32
, allocation
.width
, allocation
.height
);
400 cr
= cairo_create (surf
);
402 /* Create a PangoLayout, set the font and text */
403 layout
= pango_cairo_create_layout (cr
);
410 //pango_font_description_set_size(chart->pfd, CHART_FONT_SIZE_TITLE * PANGO_SCALE);
411 ui_chart_stack_set_font_size(chart
, CHART_FONT_SIZE_TITLE
);
412 pango_layout_set_font_description (layout
, chart
->pfd
);
414 pango_layout_set_text (layout
, chart
->title
, -1);
415 pango_layout_get_size (layout
, &tw
, &th
);
416 chart
->title_zh
= (th
/ PANGO_SCALE
) + CHART_SPACING
;
417 DB( g_print(" - title: %s w=%d h=%d\n", chart
->title
, tw
, th
) );
421 chart
->subtitle_zh
= 0;
424 //pango_font_description_set_size(chart->pfd, CHART_FONT_SIZE_PERIOD * PANGO_SCALE);
425 ui_chart_stack_set_font_size(chart
, CHART_FONT_SIZE_SUBTITLE
);
426 pango_layout_set_font_description (layout
, chart
->pfd
);
428 pango_layout_set_text (layout
, chart
->subtitle
, -1);
429 pango_layout_get_size (layout
, &tw
, &th
);
430 chart
->subtitle_zh
= (th
/ PANGO_SCALE
) + CHART_SPACING
;
431 DB( g_print(" - period: %s w=%d h=%d\n", chart
->subtitle
, tw
, th
) );
434 // compute other text
435 //pango_font_description_set_size(chart->pfd, CHART_FONT_SIZE_NORMAL * PANGO_SCALE);
436 ui_chart_stack_set_font_size(chart
, CHART_FONT_SIZE_NORMAL
);
437 pango_layout_set_font_description (layout
, chart
->pfd
);
440 chart
->bud_col_w
= 0;
441 chart
->rel_col_w
= 0;
443 gdouble maxbudget
= 0;
444 gdouble maxresult
= 0;
446 for(i
=0;i
<chart
->nb_items
;i
++)
448 StackItem
*item
= &g_array_index(chart
->items
, StackItem
, i
);
451 pango_layout_set_text (layout
, item
->label
, -1);
452 pango_layout_get_size (layout
, &tw
, &th
);
453 title_w
= MAX(title_w
, (tw
/ PANGO_SCALE
));
455 DB( g_print(" - calc '%s' title_w=%f (w=%d)\n", item
->label
, title_w
, tw
) );
457 //result = ABS(chart->spent[i]) - ABS(chart->budget[i]);
458 result
= ABS(item
->spent
- item
->budget
);
460 maxbudget
= MAX(maxbudget
, ABS(item
->budget
) );
461 maxresult
= MAX(maxresult
, result
);
463 DB( g_print(" - maxbudget maxbudget=%f (w=%d)\n", maxbudget
, tw
) );
465 pango_layout_set_text (layout
, item
->status
, -1);
466 pango_layout_get_size (layout
, &tw
, &th
);
467 chart
->rel_col_w
= MAX(chart
->rel_col_w
, (tw
/ PANGO_SCALE
));
470 chart
->rel_col_w
+= CHART_SPACING
;
472 // compute budget/result width
473 valstr
= ui_chart_stack_print_int(chart
, -maxbudget
);
474 pango_layout_set_text (layout
, valstr
, -1);
475 pango_layout_get_size (layout
, &tw
, &th
);
476 chart
->bud_col_w
= (tw
/ PANGO_SCALE
);
477 pango_layout_set_text (layout
, chart
->budget_title
, -1);
478 pango_layout_get_size (layout
, &tw
, &th
);
479 chart
->bud_col_w
= MAX(chart
->bud_col_w
, (tw
/ PANGO_SCALE
));
480 DB( g_print(" - budget-col: w=%f, %.2f, '%s'\n", chart
->bud_col_w
, maxbudget
, valstr
) );
483 valstr
= ui_chart_stack_print_int(chart
, -maxresult
);
484 pango_layout_set_text (layout
, valstr
, -1);
485 pango_layout_get_size (layout
, &tw
, &th
);
486 chart
->res_col_w
= (tw
/ PANGO_SCALE
);
487 pango_layout_set_text (layout
, chart
->result_title
, -1);
488 pango_layout_get_size (layout
, &tw
, &th
);
489 chart
->res_col_w
= MAX(chart
->res_col_w
, (tw
/ PANGO_SCALE
));
490 DB( g_print(" - result-col: w=%f, %.2f, '%s'\n", chart
->res_col_w
, maxresult
, valstr
) );
493 // collect other width, add margins
494 chart
->header_zh
= (th
/ PANGO_SCALE
) + CHART_SPACING
;
495 chart
->cat_col_w
= title_w
+ CHART_SPACING
;
497 //chart->title_y = chart->t;
498 chart
->subtitle_y
= chart
->t
+ chart
->title_zh
;
499 chart
->header_y
= chart
->subtitle_y
+ chart
->subtitle_zh
;
502 chart
->graph_width
= chart
->w
- chart
->cat_col_w
- chart
->bud_col_w
- chart
->res_col_w
- chart
->rel_col_w
- (double)(CHART_SPACING
*4);
503 chart
->graph_height
= chart
->h
- chart
->title_zh
- chart
->subtitle_zh
-chart
->header_zh
;
506 DB( g_print("gfx_w = %.2f - %.2f - %.2f - %.2f - %.2f \n",
507 chart
->w
, chart
->cat_col_w
, chart
->bud_col_w
, chart
->res_col_w
, (double)(CHART_SPACING
*4)) );
509 DB( g_print("gfx_w = %.2f\n", chart
->graph_width
) );
511 //if expand : we compute available space
512 //chart->barw = MAX(32, (chart->graph_width)/chart->nb_items);
513 //chart->barw = 32; // usr setted or defaut to BARW
515 blkw
= chart
->barw
+ floor(chart
->barw
* 0.2);
518 chart
->visible
= (chart
->graph_height
- chart
->t
) / blkw
;
519 chart
->visible
= MIN(chart
->visible
, chart
->nb_items
);
521 g_object_unref (layout
);
524 cairo_surface_destroy(surf
);
530 static void ui_chart_stack_draw_help(GtkWidget
*widget
, gpointer user_data
)
532 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
538 //cr = gdk_cairo_create (gtk_widget_get_window(widget));
539 cr
= cairo_create (chart
->surface
);
541 cairo_set_line_width (cr
, 1);
544 cairo_set_line_width(cr
, 1.0);
545 cairo_set_source_rgb(cr
, 0.0, 1.0, 0.0); //green
546 cairo_rectangle(cr
, chart
->l
+0.5, chart
->t
+0.5, chart
->w
, chart
->h
);
551 cairo_set_source_rgb(cr
, .0, .0, 1.0);
552 cairo_rectangle(cr
, chart
->l
+0.5, chart
->t
+0.5, chart
->w
, chart
->title_zh
);
556 cairo_set_source_rgb(cr
, .0, 0, 1.0);
557 cairo_rectangle(cr
, chart
->l
+0.5, chart
->subtitle_y
+0.5, chart
->w
, chart
->subtitle_zh
);
561 cairo_set_source_rgb(cr
, .0, 1.0, 1.0);
562 cairo_rectangle(cr
, chart
->l
+0.5, chart
->header_y
+0.5, chart
->w
, chart
->header_zh
);
566 y
= chart
->t
+ chart
->title_zh
+ chart
->header_zh
+ chart
->subtitle_zh
;
567 cairo_set_source_rgb(cr
, 0.0, 0.0, 1.0); //blue
568 cairo_rectangle(cr
, chart
->l
+0.5, y
+0.5, chart
->cat_col_w
, chart
->h
- y
);
572 x
= chart
->l
+ chart
->cat_col_w
+ chart
->graph_width
+ CHART_SPACING
;
573 cairo_set_source_rgb(cr
, 0.0, 0.0, 1.0); //blue
574 cairo_rectangle(cr
, x
+0.5, y
+0.5, chart
->bud_col_w
, chart
->h
- y
);
578 x
= chart
->l
+ chart
->cat_col_w
+ chart
->graph_width
+ chart
->bud_col_w
+ (CHART_SPACING
*3);
579 cairo_set_source_rgb(cr
, 0.0, 0.0, 1.0); //blue
580 cairo_rectangle(cr
, x
+0.5, y
+0.5, chart
->res_col_w
, chart
->h
- y
);
585 y
= chart
->header_y
+ chart
->header_zh
;
587 cairo_set_source_rgb(cr
, 1.0, .0, .0);
588 cairo_rectangle(cr
, chart
->l
+chart
->cat_col_w
+0.5, y
+0.5, chart
->graph_width
+0.5, chart
->graph_height
+0.5);
594 cairo_set_line_width(cr
, 1.0);
597 cairo_set_dash (cr
, &dashlength
, 1, 0);
598 cairo_set_source_rgb(cr
, 1.0, 0.0, 1.0); // violet
599 for(i
=first
; i
<=(first
+chart
->visible
) ;i
++)
601 cairo_move_to(cr
, chart
->l
, y2
);
602 cairo_line_to(cr
, chart
->r
, y2
);
615 ** draw all visible bars
617 static void ui_chart_stack_draw_bars(ChartStack
*chart
, cairo_t
*cr
)
619 double x
, y
, x2
, y2
, h
;
626 DB( g_print("\n[chartstack] bar draw bars\n") );
628 layout
= pango_cairo_create_layout (cr
);
630 x
= chart
->l
+ chart
->cat_col_w
;
631 y
= chart
->t
+ chart
->title_zh
+ chart
->header_zh
+ chart
->subtitle_zh
;
632 first
= (gint
)gtk_adjustment_get_value(GTK_ADJUSTMENT(chart
->adjustment
));
637 //pango_font_description_set_size(chart->pfd, CHART_FONT_SIZE_TITLE * PANGO_SCALE);
638 ui_chart_stack_set_font_size(chart
, CHART_FONT_SIZE_TITLE
);
639 pango_layout_set_font_description (layout
, chart
->pfd
);
640 pango_layout_set_text (layout
, chart
->title
, -1);
641 pango_layout_get_size (layout
, &tw
, &th
);
643 cairo_user_set_rgbcol(cr
, &global_colors
[THTEXT
]);
644 cairo_move_to(cr
, chart
->l
, chart
->t
);
645 pango_cairo_show_layout (cr
, layout
);
651 //pango_font_description_set_size(chart->pfd, CHART_FONT_SIZE_PERIOD * PANGO_SCALE);
652 ui_chart_stack_set_font_size(chart
, CHART_FONT_SIZE_SUBTITLE
);
653 pango_layout_set_font_description (layout
, chart
->pfd
);
654 pango_layout_set_text (layout
, chart
->subtitle
, -1);
655 pango_layout_get_size (layout
, &tw
, &th
);
657 cairo_user_set_rgbcol(cr
, &global_colors
[THTEXT
]);
658 cairo_move_to(cr
, chart
->l
, chart
->subtitle_y
);
659 pango_cairo_show_layout (cr
, layout
);
663 //cairo_user_set_rgbcol(cr, &global_colors[BLACK]);
664 cairo_user_set_rgbacol (cr
, &global_colors
[THTEXT
], 0.78);
665 //pango_font_description_set_size(chart->pfd, CHART_FONT_SIZE_NORMAL * PANGO_SCALE);
666 ui_chart_stack_set_font_size(chart
, CHART_FONT_SIZE_NORMAL
);
667 pango_layout_set_font_description (layout
, chart
->pfd
);
669 pango_layout_set_text (layout
, chart
->budget_title
, -1);
670 pango_layout_get_size (layout
, &tw
, &th
);
671 cairo_move_to(cr
, chart
->l
+ chart
->cat_col_w
+ chart
->graph_width
+ chart
->bud_col_w
+ CHART_SPACING
- (tw
/PANGO_SCALE
), chart
->header_y
);
672 pango_cairo_show_layout (cr
, layout
);
674 pango_layout_set_text (layout
, chart
->result_title
, -1);
675 pango_layout_get_size (layout
, &tw
, &th
);
676 cairo_move_to(cr
, chart
->l
+ chart
->cat_col_w
+ chart
->graph_width
+ chart
->bud_col_w
+ chart
->res_col_w
- (tw
/PANGO_SCALE
) + (CHART_SPACING
*3), chart
->header_y
);
677 pango_cairo_show_layout (cr
, layout
);
681 //pango_font_description_set_size(chart->pfd, CHART_FONT_SIZE_NORMAL * PANGO_SCALE);
682 ui_chart_stack_set_font_size(chart
, CHART_FONT_SIZE_NORMAL
);
683 pango_layout_set_font_description (layout
, chart
->pfd
);
685 for(i
=0; i
<chart
->visible
;i
++)
688 gint barw
= chart
->barw
;
689 gint blkw
= chart
->blkw
;
693 item
= &g_array_index(chart
->items
, StackItem
, idx
);
696 y2
= y
+ (CHART_SPACING
/2) + (blkw
* i
);
698 DB( g_print("'%-32s' wrn=%d %.2f%% (%.2f%%) :: r=% 4.2f s=% 4.2f b=% 4.2f\n",
699 item
->label
, item
->warn
, item
->rawrate
, item
->rate
, item
->result
, item
->spent
, item
->budget
) );
701 valstr
= item
->label
;
702 pango_layout_set_text (layout
, valstr
, -1);
703 pango_layout_get_size (layout
, &tw
, &th
);
705 double ytext
= y2
+ ((barw
- (th
/ PANGO_SCALE
))/2);
707 //cairo_user_set_rgbcol(cr, &global_colors[BLACK]);
708 cairo_user_set_rgbacol (cr
, &global_colors
[THTEXT
], 0.78);
709 cairo_move_to(cr
, chart
->l
+ chart
->cat_col_w
- (tw
/ PANGO_SCALE
) - CHART_SPACING
, ytext
);
710 pango_cairo_show_layout (cr
, layout
);
713 cairo_user_set_rgbacol(cr
, &global_colors
[THTEXT
], 0.15);
714 cairo_rectangle(cr
, x2
, y2
, chart
->graph_width
, barw
);
717 //bar with color :: todo migrate this
718 h
= floor(item
->rate
* chart
->graph_width
);
721 cairo_user_set_rgbcol_over(cr
, &chart
->color_scheme
.colors
[chart
->color_scheme
.cs_red
], idx
== chart
->active
);
725 if(item
->rate
> 0.8 && item
->rate
< 1.0)
726 cairo_user_set_rgbcol_over(cr
, &chart
->color_scheme
.colors
[chart
->color_scheme
.cs_orange
], idx
== chart
->active
);
728 cairo_user_set_rgbcol_over(cr
, &chart
->color_scheme
.colors
[chart
->color_scheme
.cs_green
], idx
== chart
->active
);
732 cairo_rectangle(cr
, x2
, y2
, h
, barw
);
736 if( item
->result
!= 0)
738 valstr
= ui_chart_stack_print_int(chart
, item
->spent
);
739 pango_layout_set_text (layout
, valstr
, -1);
740 pango_layout_get_size (layout
, &tw
, &th
);
742 if( h
>= ( (tw
/ PANGO_SCALE
) + (CHART_SPACING
*2)) )
745 cairo_user_set_rgbcol(cr
, &global_colors
[WHITE
]);
746 //cairo_user_set_rgbcol (cr, &global_colors[THBASE]);
747 cairo_move_to(cr
, x2
+ h
- (tw
/ PANGO_SCALE
) - CHART_SPACING
, ytext
);
752 //cairo_user_set_rgbcol(cr, &global_colors[TEXT]);
753 cairo_user_set_rgbacol (cr
, &global_colors
[THTEXT
], 0.78);
754 cairo_move_to(cr
, x2
+ h
+ CHART_SPACING
, ytext
);
757 pango_cairo_show_layout (cr
, layout
);
761 valstr
= ui_chart_stack_print_int(chart
, item
->budget
);
762 pango_layout_set_text (layout
, valstr
, -1);
763 pango_layout_get_size (layout
, &tw
, &th
);
764 //cairo_user_set_rgbcol(cr, &global_colors[BLACK]);
765 cairo_user_set_rgbacol (cr
, &global_colors
[THTEXT
], 0.78);
766 cairo_move_to(cr
, chart
->l
+ chart
->cat_col_w
+ chart
->graph_width
+ chart
->bud_col_w
+ CHART_SPACING
- (tw
/ PANGO_SCALE
), ytext
);
767 pango_cairo_show_layout (cr
, layout
);
771 if( item
->result
!= 0)
773 valstr
= ui_chart_stack_print_int(chart
, item
->result
);
776 //cairo_set_source_rgb(cr, COLTOCAIRO(164), COLTOCAIRO(0), COLTOCAIRO(0));
777 cairo_user_set_rgbcol(cr
, &chart
->color_scheme
.colors
[chart
->color_scheme
.cs_red
]);
779 //cairo_user_set_rgbcol(cr, &global_colors[TEXT]);
780 cairo_user_set_rgbacol (cr
, &global_colors
[THTEXT
], 0.78);
782 pango_layout_set_text (layout
, valstr
, -1);
783 pango_layout_get_size (layout
, &tw
, &th
);
784 cairo_move_to(cr
, chart
->l
+ chart
->cat_col_w
+ chart
->graph_width
+ chart
->bud_col_w
+ chart
->res_col_w
- (tw
/ PANGO_SCALE
) + (CHART_SPACING
*3), ytext
);
785 pango_cairo_show_layout (cr
, layout
);
788 pango_layout_set_text (layout
, item
->status
, -1);
789 pango_layout_get_size (layout
, &tw
, &th
);
790 cairo_move_to(cr
, chart
->l
+ chart
->cat_col_w
+ chart
->graph_width
+ chart
->bud_col_w
+ chart
->res_col_w
+ (CHART_SPACING
*4), ytext
);
791 pango_cairo_show_layout (cr
, layout
);
798 g_object_unref (layout
);
804 ** get the bar under the mouse pointer
806 static gint
ui_chart_stack_get_active(GtkWidget
*widget
, gint x
, gint y
, gpointer user_data
)
808 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
809 gint retval
, first
, index
, py
;
810 gint blkw
= chart
->blkw
;
815 oy
= chart
->t
+ chart
->title_zh
+ chart
->header_zh
+ chart
->subtitle_zh
;
817 //DB( g_print(" y=%d, oy=%f, cb=%f\n", y, oy, chart->b) );
820 if( (y
<= chart
->b
&& y
>= oy
) && (x
>= chart
->l
&& x
<= chart
->r
) )
822 first
= gtk_adjustment_get_value(GTK_ADJUSTMENT(chart
->adjustment
));
824 index
= first
+ (py
/ blkw
);
827 if(index
< chart
->nb_items
)
830 //DB( g_print(" hover=%d\n", retval) );
837 static void ui_chart_stack_first_changed( GtkAdjustment
*adj
, gpointer user_data
)
839 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
842 DB( g_print("\n[chartstack] bar first changed\n") );
844 //first = gtk_adjustment_get_value(GTK_ADJUSTMENT(adj));
846 //DB( g_print(" first=%d\n", first) );
849 DB( g_print("scrollbar\n adj=%8x, low=%.2f upp=%.2f val=%.2f step=%.2f page=%.2f size=%.2f\n", adj,
850 adj->lower, adj->upper, adj->value, adj->step_increment, adj->page_increment, adj->page_size) );
852 /* Set the number of decimal places to which adj->value is rounded */
853 //gtk_scale_set_digits (GTK_SCALE (hscale), (gint) adj->value);
854 //gtk_scale_set_digits (GTK_SCALE (vscale), (gint) adj->value);
855 gtk_widget_queue_draw(chart
->drawarea
);
860 ** scrollbar set values for upper, page size, and also show/hide
862 static void ui_chart_stack_scrollbar_setvalues(ChartStack
*chart
)
864 GtkAdjustment
*adj
= chart
->adjustment
;
867 g_return_if_fail (GTK_IS_ADJUSTMENT (adj
));
869 DB( g_print("\n[chartstack] sb_set_values\n") );
871 //if(visible < entries)
873 first
= gtk_adjustment_get_value(GTK_ADJUSTMENT(adj
));
875 DB( g_print(" entries=%d, visible=%d\n", chart
->nb_items
, chart
->visible
) );
876 DB( g_print(" first=%d, upper=%d, pagesize=%d\n", first
, chart
->nb_items
, chart
->visible
) );
878 gtk_adjustment_set_upper(adj
, (gdouble
)chart
->nb_items
);
879 gtk_adjustment_set_page_size(adj
, (gdouble
)chart
->visible
);
880 gtk_adjustment_set_page_increment(adj
, (gdouble
)chart
->visible
);
882 if(first
+chart
->visible
> chart
->nb_items
)
884 gtk_adjustment_set_value(adj
, (gdouble
)chart
->nb_items
- chart
->visible
);
887 gtk_adjustment_changed (adj
);
889 //gtk_widget_show(GTK_WIDGET(scrollbar));
892 //gtk_widget_hide(GTK_WIDGET(scrollbar));
898 static void ui_chart_stack_recompute(ChartStack
*chart
)
901 DB( g_print("\n[chartstack] recompute\n") );
903 ui_chart_stack_calculation(chart
);
904 gtk_adjustment_set_value(chart
->adjustment
, 0);
905 ui_chart_stack_scrollbar_setvalues(chart
);
906 gtk_widget_show(chart
->scrollbar
);
908 gtk_widget_queue_draw( chart
->drawarea
);
912 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
913 static gboolean
drawarea_full_redraw(GtkWidget
*widget
, gpointer user_data
)
915 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
918 DB( g_print("\n[gtkchart] drawarea full redraw\n") );
920 cr
= cairo_create (chart
->surface
);
922 /* fillin the back in white */
923 //cairo_user_set_rgbcol(cr, &global_colors[WHITE]);
924 cairo_user_set_rgbcol(cr
, &global_colors
[THBASE
]);
928 if(chart
->nb_items
== 0)
935 ui_chart_stack_draw_help(widget
, user_data
);
938 ui_chart_stack_draw_bars(chart
, cr
);
947 drawarea_configure_event_callback (GtkWidget
*widget
,
948 GdkEventConfigure
*event
,
951 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
952 GtkAllocation allocation
;
953 GtkStyleContext
*context
;
954 PangoFontDescription
*desc
;
958 DB( g_print("\n[chartstack] drawarea configure \n") );
960 DB( g_print("w=%d h=%d\n", allocation
.width
, allocation
.height
) );
962 gtk_widget_get_allocation (widget
, &allocation
);
965 cairo_surface_destroy (chart
->surface
);
967 chart
->surface
= gdk_window_create_similar_surface (gtk_widget_get_window (widget
),
972 context
= gtk_widget_get_style_context (widget
);
974 chart_color_global_default();
977 colfound
= gtk_style_context_lookup_color(context
, "theme_base_color", &color
);
979 colfound
= gtk_style_context_lookup_color(context
, "base_color", &color
);
983 struct rgbcol
*tcol
= &global_colors
[THBASE
];
984 tcol
->r
= color
.red
* 255;
985 tcol
->g
= color
.green
* 255;
986 tcol
->b
= color
.blue
* 255;
987 DB( g_print(" - theme base col: %x %x %x\n", tcol
->r
, tcol
->g
, tcol
->b
) );
991 colfound
= gtk_style_context_lookup_color(context
, "theme_fg_color", &color
);
993 gtk_style_context_lookup_color(context
, "fg_color", &color
);
997 struct rgbcol
*tcol
= &global_colors
[THTEXT
];
998 tcol
->r
= color
.red
* 255;
999 tcol
->g
= color
.green
* 255;
1000 tcol
->b
= color
.blue
* 255;
1001 DB( g_print(" - theme text (bg) col: %x %x %x\n", tcol
->r
, tcol
->g
, tcol
->b
) );
1004 /* get and copy the font */
1005 gtk_style_context_get(context
, GTK_STATE_FLAG_NORMAL
, "font", &desc
, NULL
);
1008 pango_font_description_free (chart
->pfd
);
1011 chart
->pfd
= pango_font_description_copy(desc
);
1012 chart
->pfd_size
= pango_font_description_get_size (desc
) / PANGO_SCALE
;
1013 chart
->barw
= (6 + chart
->pfd_size
) * PHI
;
1015 DB( g_print("family: %s\n", pango_font_description_get_family(chart
->pfd
) ) );
1016 DB( g_print("size : %d (%d)\n", chart
->pfd_size
, chart
->pfd_size
/PANGO_SCALE
) );
1017 DB( g_print("isabs : %d\n", pango_font_description_get_size_is_absolute (chart
->pfd
) ) );
1019 if( gtk_widget_get_realized(widget
) )
1021 ui_chart_stack_recompute(chart
);
1022 drawarea_full_redraw(widget
, user_data
);
1025 /* We've handled the configure event, no need for further processing. */
1030 static gboolean
drawarea_draw_callback(GtkWidget
*widget
, cairo_t
*cr
, gpointer user_data
)
1032 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
1034 if( !gtk_widget_get_realized(widget
) || chart
->surface
== NULL
)
1037 //DB( g_print("\n[chartstack] drawarea draw cb\n") );
1039 cairo_set_source_surface (cr
, chart
->surface
, 0, 0);
1042 /* always redraw directly the active block */
1046 first
= (gint
)gtk_adjustment_get_value(GTK_ADJUSTMENT(chart
->adjustment
));
1047 ox
= chart
->l
+ chart
->cat_col_w
;
1048 oy
= chart
->t
+ chart
->title_zh
+ chart
->header_zh
+ chart
->subtitle_zh
;
1051 if(chart
->active
!= -1)
1053 DB( g_print(" draw active\n") );
1055 oy
+= CHART_SPACING
/2 + (chart
->active
- first
) * chart
->blkw
;
1056 //cairo_user_set_rgbacol (cr, &global_colors[THTEXT], 0.78);
1057 cairo_user_set_rgbacol(cr
, &global_colors
[WHITE
], OVER_ALPHA
);
1058 //cairo_move_to(cr, chart->l, chart->t);
1059 cairo_rectangle(cr
, ox
, oy
, chart
->graph_width
, chart
->barw
);
1067 static gboolean
drawarea_motionnotifyevent_callback(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer user_data
)
1069 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
1072 if(chart
->nb_items
== 0)
1075 DB( g_print("\n[chartstack] drawarea motion cb\n") );
1082 //DB( g_print(" is hint\n") );
1084 gdk_window_get_device_position(event
->window
, event
->device
, &x
, &y
, NULL
);
1085 //gdk_window_get_pointer(event->window, &x, &y, NULL);
1089 chart
->active
= ui_chart_stack_get_active(widget
, x
, y
, chart
);
1092 // rollover redraw ?
1093 DB( g_print(" %d, %d :: active: last=%d, curr=%d\n", x
, y
, chart
->lastactive
, chart
->active
) );
1095 if(chart
->lastactive
!= chart
->active
)
1097 GdkRectangle update_rect
;
1101 DB( g_print(" motion rollover redraw :: active=%d\n", chart
->active
) );
1103 first
= (gint
)gtk_adjustment_get_value(GTK_ADJUSTMENT(chart
->adjustment
));
1104 oy
= chart
->t
+ chart
->title_zh
+ chart
->header_zh
+ chart
->subtitle_zh
;
1106 if(chart
->lastactive
!= -1)
1108 update_rect
.x
= chart
->l
;
1109 update_rect
.y
= oy
+ (chart
->lastactive
- first
) * chart
->blkw
;
1110 update_rect
.width
= chart
->r
;
1111 update_rect
.height
= chart
->blkw
;
1113 /* Now invalidate the affected region of the drawing area. */
1114 gdk_window_invalidate_rect (gtk_widget_get_window (widget
),
1119 update_rect
.x
= chart
->l
;
1120 update_rect
.y
= oy
+ (chart
->active
- first
) * chart
->blkw
;
1121 update_rect
.width
= chart
->r
;
1122 update_rect
.height
= chart
->blkw
;
1124 /* Now invalidate the affected region of the drawing area. */
1125 gdk_window_invalidate_rect (gtk_widget_get_window (widget
),
1129 //gtk_widget_queue_draw( widget );
1133 chart
->lastactive
= chart
->active
;
1140 static gboolean
drawarea_scroll_event_callback( GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
1142 ChartStack
*chart
= GTK_CHARTSTACK(user_data
);
1143 GtkAdjustment
*adj
= chart
->adjustment
;
1144 gdouble first
, upper
, pagesize
;
1146 DB( g_print("\n[chartstack] scroll\n") );
1148 first
= gtk_adjustment_get_value(GTK_ADJUSTMENT(adj
));
1149 //lower = gtk_adjustment_get_lower(GTK_ADJUSTMENT(adj));
1150 upper
= gtk_adjustment_get_upper(GTK_ADJUSTMENT(adj
));
1151 pagesize
= gtk_adjustment_get_page_size(GTK_ADJUSTMENT(adj
));
1153 DB( g_print("- pos is %.2f, [%.2f - %.2f]\n", first
, 0.0, upper
) );
1155 switch(event
->direction
)
1158 gtk_adjustment_set_value(adj
, first
- 1);
1160 case GDK_SCROLL_DOWN
:
1161 gtk_adjustment_set_value(adj
, CLAMP(first
+ 1, 0, upper
- pagesize
) );
1167 drawarea_full_redraw(widget
, user_data
);
1173 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
1174 /* public functions */
1175 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
1177 static void ui_chart_stack_queue_redraw(ChartStack
*chart
)
1180 if( gtk_widget_get_realized(GTK_WIDGET(chart
)) )
1182 ui_chart_stack_recompute(chart
);
1183 drawarea_full_redraw(chart
->drawarea
, chart
);
1184 //gtk_widget_queue_draw( chart->drawarea );
1189 ** change the model and/or column
1191 void ui_chart_stack_set_dualdatas(ChartStack
*chart
, GtkTreeModel
*model
, gchar
*coltitle1
, gchar
*coltitle2
, gchar
*title
, gchar
*subtitle
)
1193 g_return_if_fail (GTK_IS_CHARTSTACK (chart
));
1195 if( GTK_IS_TREE_MODEL(model
) )
1197 ui_chart_stack_setup_with_model(chart
, model
, coltitle1
, coltitle2
);
1199 chart
->title
= g_strdup(title
);
1200 if(subtitle
!= NULL
)
1201 chart
->subtitle
= g_strdup(subtitle
);
1203 ui_chart_stack_queue_redraw(chart
);
1207 ui_chart_stack_clear(chart
, TRUE
);
1212 ** change the tooltip title
1214 void ui_chart_stack_set_title(ChartStack
* chart
, gchar
*title
)
1216 g_return_if_fail (GTK_IS_CHARTSTACK (chart
));
1218 chart
->title
= g_strdup(title
);
1220 DB( g_print("\n[chartstack] set title = %s\n", chart
->title
) );
1222 ui_chart_stack_recompute(chart
);
1226 void ui_chart_stack_set_subtitle(ChartStack
* chart
, gchar
*subtitle
)
1228 g_return_if_fail (GTK_IS_CHARTSTACK (chart
));
1230 chart
->subtitle
= g_strdup(subtitle
);
1232 DB( g_print("\n[chartstack] set period = %s\n", chart
->subtitle
) );
1234 ui_chart_stack_recompute(chart
);
1240 ** change the minor visibility
1242 void ui_chart_stack_show_minor(ChartStack
* chart
, gboolean minor
)
1244 g_return_if_fail (GTK_IS_CHARTSTACK (chart
));
1246 chart
->minor
= minor
;
1248 ui_chart_stack_queue_redraw(chart
);
1252 void ui_chart_stack_set_color_scheme(ChartStack
* chart
, gint index
)
1254 colorscheme_init(&chart
->color_scheme
, index
);
1259 ** set the minor parameters
1261 /*void ui_chart_stack_set_minor_prefs(ChartStack * chart, gdouble rate, gchar *symbol)
1263 g_return_if_fail (GTK_IS_CHARTSTACK (chart));
1265 chart->minor_rate = rate;
1266 chart->minor_symbol = symbol;
1269 void ui_chart_stack_set_currency(ChartStack
* chart
, guint32 kcur
)
1271 g_return_if_fail (GTK_IS_CHARTSTACK (chart
));