243e71cd170e822735a278a8695d1efe81b3f7ca
[ardour.git] / libs / canvas / flag.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "canvas/flag.h"
21 #include "canvas/text.h"
22 #include "canvas/rectangle.h"
23 #include "canvas/line.h"
24
25 using namespace std;
26 using namespace ArdourCanvas;
27
28 Flag::Flag (Canvas* canvas, Distance height, Color outline_color, Color fill_color, Duple position)
29         : Container (canvas)
30         , _outline_color (outline_color)
31         , _fill_color (fill_color)
32 {
33         setup (height, position);
34 }
35
36 Flag::Flag (Item* parent, Distance height, Color outline_color, Color fill_color, Duple position)
37         : Container (parent)
38         , _outline_color (outline_color)
39         , _fill_color (fill_color)
40 {
41         setup (height, position);
42 }
43
44 void 
45 Flag::setup (Distance height, Duple position)
46 {
47         _text = new Text (this);
48         _text->set_alignment (Pango::ALIGN_CENTER);
49         _text->set_color (_outline_color);
50
51         _line = new Line (this);
52         _line->set_outline_color (_outline_color);
53         set_height (height);
54
55         _rectangle = new Rectangle (this);
56         _rectangle->set_outline_color (_outline_color);
57         _rectangle->set_fill_color (_fill_color);
58
59         _text->raise_to_top ();
60
61         set_position (position);
62 }
63
64 void
65 Flag::set_text (string const & text)
66 {
67         _text->set (text);
68         boost::optional<Rect> bbox = _text->bounding_box ();
69         assert (bbox);
70
71         Duple flag_size (bbox.get().width() + 10, bbox.get().height() + 4);
72         
73         _text->set_position (Duple (5, 2));
74         _rectangle->set (Rect (0, 0, flag_size.x, flag_size.y));
75 }
76
77 void
78 Flag::set_height (Distance h)
79 {
80         _line->set (Duple (0, 0), Duple (0, h));
81 }
82
83 bool
84 Flag::covers (Duple const & point) const
85 {
86         if (_rectangle) {
87                 return _rectangle->covers (point);
88         } 
89
90         return false;
91 }