2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <cairomm/cairomm.h>
23 #include "alignment.h"
27 class AlignmentWidget : public Gtk::DrawingArea
30 void set_text_line (int n, string t)
32 if (int(_text.size()) < (n + 1)) {
41 bool on_expose_event (GdkEventExpose* ev)
47 Cairo::RefPtr<Cairo::Context> c = get_window()->create_cairo_context ();
49 Gtk::Allocation a = get_allocation ();
50 int const w = a.get_width ();
51 int const h = a.get_height ();
53 c->rectangle (0, 0, w, h);
54 c->set_source_rgb (0, 0, 0);
57 c->set_source_rgb (1, 1, 1);
58 c->set_line_width (1);
60 int const arrow_size = h / 8;
61 int const head_size = h / 32;
63 /* arrow to left edge */
64 c->move_to (arrow_size, h / 2);
65 c->line_to (0, h / 2);
66 c->rel_line_to (head_size, head_size);
67 c->move_to (0, h / 2);
68 c->rel_line_to (head_size, -head_size);
71 /* arrow to right edge */
72 c->move_to (w - arrow_size, h / 2);
73 c->line_to (w, h / 2);
74 c->rel_line_to (-head_size, head_size);
75 c->move_to (w, h / 2);
76 c->rel_line_to (-head_size, -head_size);
79 /* arrow to top edge */
80 c->move_to (w / 2, arrow_size);
81 c->line_to (w / 2, 0);
82 c->rel_line_to (head_size, head_size);
83 c->move_to (w / 2, 0);
84 c->rel_line_to (-head_size, head_size);
87 /* arrow to bottom edge */
88 c->move_to (w / 2, h - h / 8);
89 c->line_to (w / 2, h);
90 c->rel_line_to (head_size, -head_size);
91 c->move_to (w / 2, h);
92 c->rel_line_to (-head_size, -head_size);
95 /* arrow to top-left corner */
96 c->move_to (arrow_size, arrow_size);
98 c->rel_line_to (head_size, 0);
100 c->rel_line_to (0, head_size);
103 /* arrow to top-right corner */
104 c->move_to (w - arrow_size, arrow_size);
106 c->rel_line_to (0, head_size);
108 c->rel_line_to (-head_size, 0);
111 /* arrow to bottom-left corner */
112 c->move_to (arrow_size, h - arrow_size);
114 c->rel_line_to (head_size, 0);
116 c->rel_line_to (0, -head_size);
119 /* arrow to bottom-right corner */
120 c->move_to (w - arrow_size, h - arrow_size);
122 c->rel_line_to (-head_size, 0);
124 c->rel_line_to (0, -head_size);
129 for (vector<string>::iterator i = _text.begin(); i != _text.end(); ++i) {
130 Cairo::TextExtents e;
131 c->get_text_extents (*i, e);
132 max_height = max (max_height, int (e.height));
135 int total_height = max_height * _text.size() * 2;
137 for (vector<string>::size_type i = 0; i < _text.size(); ++i) {
138 Cairo::TextExtents e;
139 c->get_text_extents (_text[i], e);
140 c->move_to ((w - e.width) / 2, ((h - total_height) / 2) + ((i * 2) + 1) * max_height);
141 c->text_path (_text[i]);
148 std::vector<std::string> _text;
151 Alignment::Alignment (Position p, Size s)
153 _widget = Gtk::manage (new AlignmentWidget);
157 set_decorated (false);
158 set_resizable (false);
159 set_size_request (s.width, s.height);
164 Alignment::set_text_line (int n, string t)
166 _widget->set_text_line (n, t);