1 /*
2 * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "GxValueDisplay.h"
20
21 #define P_(s) (s) // FIXME -> gettext
22
23 static gboolean gx_value_display_draw (GtkWidget *widget, cairo_t *cr);
24 static void gx_value_display_get_preferred_width (GtkWidget *widget, gint *min_width, gint *natural_width);
25 static void gx_value_display_get_preferred_height (GtkWidget *widget, gint *min_height, gint *natural_height);
26 static void gx_value_display_size_request (GtkWidget *widget, gint *width, gint *height);
27 static gboolean gx_value_display_button_press (GtkWidget *widget, GdkEventButton *event);
28
G_DEFINE_TYPE(GxValueDisplay,gx_value_display,GX_TYPE_REGLER)29 G_DEFINE_TYPE(GxValueDisplay, gx_value_display, GX_TYPE_REGLER)
30
31 static void gx_value_display_class_init(GxValueDisplayClass *klass)
32 {
33 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
34 widget_class->draw = gx_value_display_draw;
35 widget_class->get_preferred_width = gx_value_display_get_preferred_width;
36 widget_class->get_preferred_height = gx_value_display_get_preferred_height;
37 widget_class->button_press_event = gx_value_display_button_press;
38 gtk_widget_class_set_css_name(widget_class, "gx-value-display");
39 }
40
gx_value_display_get_preferred_width(GtkWidget * widget,gint * min_width,gint * natural_width)41 static void gx_value_display_get_preferred_width (GtkWidget *widget, gint *min_width, gint *natural_width)
42 {
43 gint width, height;
44 gx_value_display_size_request(widget, &width, &height);
45
46 if (min_width) {
47 *min_width = width;
48 }
49 if (natural_width) {
50 *natural_width = width;
51 }
52 }
53
gx_value_display_get_preferred_height(GtkWidget * widget,gint * min_height,gint * natural_height)54 static void gx_value_display_get_preferred_height (GtkWidget *widget, gint *min_height, gint *natural_height)
55 {
56 gint width, height;
57 gx_value_display_size_request(widget, &width, &height);
58
59 if (min_height) {
60 *min_height = height;
61 }
62 if (natural_height) {
63 *natural_height = height;
64 }
65 }
66
gx_value_display_size_request(GtkWidget * widget,gint * width,gint * height)67 static void gx_value_display_size_request(GtkWidget *widget, gint *width, gint *height)
68 {
69 g_assert(GX_IS_VALUE_DISPLAY(widget));
70 *width = 0;
71 *height = 0;
72 _gx_regler_calc_size_request(GX_REGLER(widget), width, height, TRUE);
73 }
74
gx_value_display_draw(GtkWidget * widget,cairo_t * cr)75 static gboolean gx_value_display_draw(GtkWidget *widget, cairo_t *cr)
76 {
77 g_assert(GX_IS_VALUE_DISPLAY(widget));
78 GdkRectangle value_rect;
79 _gx_regler_get_positions(GX_REGLER(widget), NULL, &value_rect, true);
80 _gx_regler_display_value(GX_REGLER(widget), cr, &value_rect);
81 return FALSE;
82 }
83
gx_value_display_button_press(GtkWidget * widget,GdkEventButton * event)84 static gboolean gx_value_display_button_press (GtkWidget *widget, GdkEventButton *event)
85 {
86 g_assert(GX_IS_VALUE_DISPLAY(widget));
87 gtk_widget_grab_focus(widget);
88 if (event->button != 1 && event->button != 3) {
89 return FALSE;
90 }
91 GdkRectangle image_rect, value_rect;
92 image_rect.width = 0;
93 image_rect.height = 0;
94 _gx_regler_get_positions(GX_REGLER(widget), &image_rect, &value_rect, true);
95 if (_approx_in_rectangle(event->x, event->y, &value_rect)) {
96 gboolean ret;
97 g_signal_emit_by_name(GX_REGLER(widget), "value-entry", &value_rect, event, &ret);
98 }
99 return FALSE;
100 }
101
gx_value_display_init(GxValueDisplay * value_display)102 static void gx_value_display_init(GxValueDisplay *value_display)
103 {
104 gx_regler_set_show_value(&value_display->parent, TRUE);
105 }
106