Kludgey fix for invisible playhead when scrolled.
[ardour.git] / libs / canvas / scroll_group.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3
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.
8
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.
13
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.
17 */
18
19 #include <iostream>
20
21 #include "pbd/compose.h"
22
23 #include "canvas/canvas.h"
24 #include "canvas/debug.h"
25 #include "canvas/scroll_group.h"
26
27 using namespace std;
28 using namespace ArdourCanvas;
29
30 ScrollGroup::ScrollGroup (Canvas* c, ScrollSensitivity s, bool clip)
31         : Container (c)
32         , _scroll_sensitivity (s)       
33         , _clip(clip)
34 {
35 }
36
37 ScrollGroup::ScrollGroup (Item* parent, ScrollSensitivity s, bool clip)
38         : Container (parent)
39         , _scroll_sensitivity (s)       
40         , _clip(clip)
41 {
42 }
43
44 void
45 ScrollGroup::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
46 {
47         /* clip the draw to the area that this scroll group nominally occupies
48          * WITHOUT scroll offsets in effect
49          */
50
51         boost::optional<Rect> r = bounding_box();
52
53         if (!r) {
54                 return;
55         }
56
57         if (!_clip) {
58                 /* kludge: clip disabled, because for some reason the cursor scroll
59                    group requires scroll offsets here to clip correctly yet everything
60                    else does not.  I can't figure this out, so since not clipping a
61                    single arrow isn't the end of the world, just don't clip. */
62                 Container::render (area, context);
63         }
64
65         Rect self (_position.x + r.get().x0,
66                    _position.y + r.get().y0,
67                    _position.x + r.get().x1,
68                    _position.y + r.get().y1);
69
70         self.x1 = min (_position.x + _canvas->width(), self.x1);
71         self.y1 = min (_position.y + _canvas->height(), self.y1);
72
73         context->save ();
74         context->rectangle (self.x0, self.y0, self.width(), self.height());
75         context->clip ();
76         
77         Container::render (area, context);
78
79         context->restore ();
80 }
81
82 void
83 ScrollGroup::scroll_to (Duple const& d)
84 {
85         if (_scroll_sensitivity & ScrollsHorizontally) {
86                 _scroll_offset.x = d.x;
87         }
88
89         if (_scroll_sensitivity & ScrollsVertically) {
90                 _scroll_offset.y = d.y;
91         }
92 }
93
94 bool
95 ScrollGroup::covers_canvas (Duple const& d) const
96 {
97         boost::optional<Rect> r = bounding_box ();
98
99         if (!r) {
100                 return false;
101         }
102
103         return r->contains (d);
104 }
105
106 bool
107 ScrollGroup::covers_window (Duple const& d) const
108 {
109         boost::optional<Rect> r = bounding_box ();
110
111         if (!r) {
112                 return false;
113         }
114
115         Rect w = r->translate (-_scroll_offset);
116
117         return w.contains (d);
118 }