Edges are clickable now.
[av.git] / gtk / src / vertex.cpp
blobc675f4375294339bbd7eb8388cf5998ca091f09c
1 #include "vertex.hpp"
3 Vertex::Vertex(Gtk::Widget &parent, const Word &word) :
4 Drawable<Vertex *>(),
5 _PADDING(2),
6 _x(0),
7 _y(0),
8 _is_highlight(false),
9 _path(NULL),
10 _word(word),
11 _layout(parent.create_pango_layout(_word.get_word()))
13 Glib::RefPtr<Pango::LayoutLine> line;
14 Pango::Rectangle ink_extents;
15 Pango::Rectangle extents;
17 // Set Font
18 set_font("Sans 12");
20 // Set text and get layout information
21 _layout->set_text(_word.get_word());
23 line = _layout->get_line(0);
24 if (line) {
25 line->get_extents(ink_extents, extents);
26 _descent = extents.get_descent() / Pango::SCALE;
27 _width = extents.get_width() / Pango::SCALE +
28 _PADDING * 2;
29 _height = extents.get_height() / Pango::SCALE +
30 _PADDING * 2;
34 Vertex::~Vertex(void)
39 void Vertex::set_font(const Glib::ustring &font)
41 _layout->set_font_description(Pango::FontDescription(font));
44 void Vertex::draw(Cairo::RefPtr<Cairo::Context> cr)
46 Glib::RefPtr<Pango::LayoutLine> line;
48 if (cr) {
49 line = _layout->get_line(0);
50 if (line) {
51 // Draw a rectangular box around the text
52 cr->save();
53 cr->begin_new_path();
54 cr->rectangle(_x, _y, _width, _height);
55 delete _path;
56 _path = cr->copy_path_flat();
57 if (_is_highlight) {
58 cr->set_source_rgb(0.6, 0.8, 0.6);
59 cr->fill_preserve();
60 cr->set_source_rgb(0.1, 0.6, 0.1);
61 } else {
62 cr->set_source_rgb(0.8, 0.5, 0.5);
63 cr->fill_preserve();
64 cr->set_source_rgb(0.2, 0.2, 0);
66 cr->set_line_width(0.5);
67 cr->stroke();
68 cr->restore();
70 cr->save();
71 cr->translate(_x + _PADDING,
72 _y + _height - _descent - _PADDING);
73 line->show_in_cairo_context(cr);
74 cr->restore();
79 void Vertex::add_friend(const Vertex &v)
81 if (!is_friend(v)) {
82 _friends.push_back(&v);
86 void Vertex::delete_friend(const Vertex &v)
88 std::vector<const Vertex *>::iterator iter;
90 for (iter = _friends.begin(); iter != _friends.end(); iter++) {
91 if (&v == *iter) {
92 _friends.erase(iter);
93 break;
98 bool Vertex::is_friend(const Vertex &v) const
100 std::vector<const Vertex *>::const_iterator iter;
102 for (iter = _friends.begin(); iter != _friends.end(); iter++) {
103 if (&v == *iter) {
104 return true;
108 return false;
111 bool Vertex::select(Cairo::RefPtr<Cairo::Context> cr, gdouble x, gdouble y)
113 if (!_is_in_region(cr, x, y)) {
114 return false;
117 if (!_is_highlight) {
118 _is_highlight = true;
119 _set_changed();
122 return true;
125 void Vertex::deselect(void)
127 if (_is_highlight) {
128 _is_highlight = false;
129 _set_changed();
133 bool Vertex::_is_in_region(Cairo::RefPtr<Cairo::Context> cr,
134 gdouble x, gdouble y) const
136 bool result;
138 cr->save();
139 cr->begin_new_path();
140 cr->append_path(*_path);
141 result = cr->in_fill(x, y);
142 cr->restore();
144 return result;