summaryrefslogtreecommitdiff
path: root/toolbar.c
blob: c47cf21d9320d1481227d2b2981a450829b40b75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "toolbar.h"

#define TOOL_SIZE 72
#define CORNER_SIZE 12
#define BORDER_WIDTH 2
#define MARGIN 6
#define COLOR 1.4 * (0x58 / 255.0), 1.4 * (0x75 / 255.0), 1.4 * (0xA7 / 255.0)
#define STROKE_COLOR (0x58 / 255.0), (0x75 / 255.0), (0xA7 / 255.0)
#define N_TOOLS (sizeof (tools) / sizeof (tools[0]))

typedef struct
{
    const char*			name;
    FooScrollAreaEventFunc	event;
} ToolInfo;

static const ToolInfo tools[] =
{
    { "T", NULL },		/* text */
    { "R", NULL },		/* rectangle */
    { "E", NULL },		/* ellipse */
    { "P", NULL },		/* polygon */
    { "L", NULL },		/* line */
    { "I", NULL },		/* image */
    { "H", NULL },		/* horizontal helper line */
    { "V", NULL },		/* vertical helper line */
    { "N", NULL },		/* post-it note */
};

struct Toolbar
{
    int dummy;
    /* no content */
};

Toolbar *
toolbar_new (void)
{
    return g_new0 (Toolbar, 1);
}

int
toolbar_get_width (Toolbar *bar)
{
    return BORDER_WIDTH + TOOL_SIZE;
}

static void
layout_set_font (PangoLayout *layout, const char *font)
{
    PangoFontDescription *desc = pango_font_description_from_string (font);
    
    pango_layout_set_font_description (layout, desc);
    
    pango_font_description_free (desc);
}

static void
on_input (FooScrollArea *area,
	  FooScrollAreaEvent *event,
	  gpointer data)
{
    switch (event->type)
    {
    case FOO_BUTTON_PRESS:
	g_print ("mouse down\n");
	break;

    case FOO_BUTTON_RELEASE:
	g_print ("mouse up\n");
	break;

    default:
    case FOO_MOTION:
	break;
    }
}

static void
draw_number (FooScrollArea *area,
	     cairo_t *cr,
	     int      number,
	     int      x,
	     int      y)
{
    PangoLayout *layout;
    char *string = g_strdup_printf (tools[number].name);
    PangoRectangle logical, ink;

    cairo_save (cr);
    
    layout = gtk_widget_create_pango_layout (GTK_WIDGET (area), string);

    layout_set_font (layout, "Sans Bold 24");
    
    pango_layout_get_pixel_extents (layout, &ink, &logical);

    x += (TOOL_SIZE - logical.width) / 2 - logical.x - BORDER_WIDTH / 2;
    y += (TOOL_SIZE - logical.height) / 2 - logical.y - BORDER_WIDTH / 2;
    
    cairo_move_to (cr, x, y);

    cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
    
    pango_cairo_show_layout (cr, layout);

    cairo_restore (cr);
    
    g_object_unref (layout);
    g_free (string);
}

void
toolbar_paint (Toolbar *bar,
	       FooScrollArea *scroll_area,
	       cairo_t	     *cr,
	       int	      x,
	       int	      y)
{
    int i;
    int b = BORDER_WIDTH / 2;
    int segment = TOOL_SIZE - 2 * b - 2 * CORNER_SIZE;
    GdkRectangle viewport;

    foo_scroll_area_get_viewport (scroll_area, &viewport);

    cairo_move_to (cr, b + x, MARGIN + y);
    
    for (i = 0; i < N_TOOLS; ++i)
    {
	double x, y;

	cairo_get_current_point (cr, &x, &y);
	
	cairo_rel_move_to (cr, CORNER_SIZE, 0);
	cairo_rel_line_to (cr, segment, 0);
	cairo_rel_curve_to (cr,
			    CORNER_SIZE, 0,
			    CORNER_SIZE, 0,
			    CORNER_SIZE, CORNER_SIZE);
	cairo_rel_line_to (cr, 0, segment);
	cairo_rel_curve_to (cr,
			    0, CORNER_SIZE,
			    0, CORNER_SIZE,
			    -CORNER_SIZE, CORNER_SIZE);
	cairo_rel_line_to (cr, -segment, 0);
	cairo_rel_curve_to (cr,
			    -CORNER_SIZE, 0,
			    -CORNER_SIZE, 0,
			    -CORNER_SIZE, -CORNER_SIZE);
	cairo_rel_line_to (cr, 0, -segment);
	cairo_rel_curve_to (cr,
			    0, -CORNER_SIZE,
			    0, -CORNER_SIZE,
			    CORNER_SIZE, -CORNER_SIZE);

	cairo_set_source_rgba (cr, COLOR, 0.8);

	foo_scroll_area_add_input_from_fill (scroll_area, cr, on_input, GINT_TO_POINTER (i));
	
	cairo_fill_preserve (cr);

	cairo_set_source_rgba (cr, STROKE_COLOR, 0.8);
	cairo_set_line_width (cr, BORDER_WIDTH);

	foo_scroll_area_add_input_from_stroke (scroll_area, cr, on_input, GINT_TO_POINTER (i));
	
	cairo_stroke (cr);

	draw_number (scroll_area, cr, i, x, y);
	
	cairo_move_to (cr, x, y + TOOL_SIZE + MARGIN);
    }
}

void
toolbar_invalidate (Toolbar	  *bar,
		    FooScrollArea *scroll_area,
		    int		   x,
		    int		   y)
{
    foo_scroll_area_invalidate_rect (scroll_area,
				     x, y,
				     TOOL_SIZE + 10,
				     N_TOOLS * (TOOL_SIZE + MARGIN) + 10);
}