fix issue with initialization of a BBT_Time variable.
[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, Gtkmm2ext::Color outline_color, Gtkmm2ext::Color fill_color, Duple position, bool invert)
29         : Container (canvas)
30         , _outline_color (outline_color)
31         , _fill_color (fill_color)
32         , _invert (invert)
33 {
34         setup (height, position);
35 }
36
37 Flag::Flag (Item* parent, Distance height, Gtkmm2ext::Color outline_color, Gtkmm2ext::Color fill_color, Duple position, bool invert)
38         : Container (parent)
39         , _outline_color (outline_color)
40         , _fill_color (fill_color)
41         , _invert (invert)
42 {
43         setup (height, position);
44 }
45
46 void
47 Flag::setup (Distance height, Duple position)
48 {
49         _text = new Text (this);
50         _text->set_alignment (Pango::ALIGN_CENTER);
51         _text->set_color (_outline_color);
52
53         _line = new Line (this);
54         _line->set_outline_color (_outline_color);
55
56         _rectangle = new Rectangle (this);
57         _rectangle->set_outline_color (_outline_color);
58         _rectangle->set_fill_color (_fill_color);
59
60         _text->raise_to_top ();
61
62         set_height (height);
63         set_position (position);
64 }
65
66 void
67 Flag::set_font_description (Pango::FontDescription font_description)
68 {
69         _text->set_font_description (font_description);
70 }
71
72 void
73 Flag::set_text (string const & text)
74 {
75         if (text == _text->text()) {
76                 return;
77         } else if (text.empty ()) {
78                 _text->set (" ");
79         } else {
80                 _text->set (text);
81         }
82
83         Rect bbox = _text->bounding_box ();
84         assert (bbox);
85
86         Duple flag_size (bbox.width() + 10, bbox.height() + 4);
87
88         if (_invert) {
89                 const Distance h = fabs(_line->y1() - _line->y0());
90                 _text->set_position (Duple (5, h - flag_size.y + 2));
91                 _rectangle->set (Rect (0, h - flag_size.y, flag_size.x, h));
92         } else {
93                 _text->set_position (Duple (5, 2));
94                 _rectangle->set (Rect (0, 0, flag_size.x, flag_size.y));
95         }
96 }
97
98 void
99 Flag::set_height (Distance h)
100 {
101         _line->set (Duple (0, 0), Duple (0, h));
102
103         if (_invert) {
104                 Rect bbox = _text->bounding_box ();
105                 if (bbox) {
106                         Duple flag_size (bbox.width() + 10, bbox.height() + 4);
107                         _rectangle->set (Rect (0, h - flag_size.y, flag_size.x, h));
108                         _text->set_position (Duple (5, h - flag_size.y + 2));
109                 }
110         }
111 }
112
113 bool
114 Flag::covers (Duple const & point) const
115 {
116         if (_rectangle) {
117                 return _rectangle->covers (point);
118         }
119
120         return false;
121 }
122
123 double
124 Flag::width () const
125 {
126         Rect bbox = _text->bounding_box ();
127         assert (bbox);
128
129         return bbox.width() + 10;
130 }