b68f9f74ec211c672f5f358970cb6cb7e804d3e6
[goodguy/history.git] / cinelerra-5.1 / cinelerra / maincursor.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  */
21
22 #include "bcsignals.h"
23 #include "edl.h"
24 #include "edlsession.h"
25 #include "localsession.h"
26 #include "maincursor.h"
27 #include "mwindow.h"
28 #include "mwindowgui.h"
29 #include "timelinepane.h"
30 #include "trackcanvas.h"
31
32
33 MainCursor::MainCursor(MWindow *mwindow, MWindowGUI *gui)
34 {
35         this->mwindow = mwindow;
36         this->gui = gui;
37         visible = 0;
38         active = 0;
39         playing_back = 0;
40         pane = 0;
41 }
42
43 MainCursor::MainCursor(MWindow *mwindow, TimelinePane *pane)
44 {
45         this->mwindow = mwindow;
46         this->gui = mwindow->gui;
47         this->pane = pane;
48         visible = 0;
49         active = 0;
50         playing_back = 0;
51 }
52
53 MainCursor::~MainCursor()
54 {
55 }
56
57 void MainCursor::create_objects()
58 {
59 //      draw();
60 }
61
62 void MainCursor::focus_in_event()
63 {
64 }
65
66 void MainCursor::focus_out_event()
67 {
68         show();
69         flash();
70 }
71
72 void MainCursor::activate()
73 {
74 //printf("MainCursor::activate 1 %d\n", BC_WindowBase::get_resources()->blink_rate);
75         if(!active)
76         {
77                 active = 1;
78                 gui->set_repeat(BC_WindowBase::get_resources()->blink_rate);
79         }
80 }
81
82 void MainCursor::deactivate()
83 {
84         if(active)
85         {
86                 active = 0;
87                 gui->unset_repeat(BC_WindowBase::get_resources()->blink_rate);
88                 show();
89                 flash();
90         }
91 }
92
93 int MainCursor::repeat_event(int64_t duration)
94 {
95         if(!active || !gui->get_has_focus()) return 0;
96         if(duration != BC_WindowBase::get_resources()->blink_rate) return 0;
97
98 // Only flash a single sample selection
99         if(selectionstart == selectionend)
100         {
101                 if(!playing_back || (playing_back && !visible))
102                 {
103                         draw(1);
104                         flash();
105                 }
106         }
107         return 1;
108 }
109
110 void MainCursor::draw(int do_plugintoggles)
111 {
112         if(!visible)
113         {
114                 selectionstart = mwindow->edl->local_session->get_selectionstart(1);
115                 selectionend = mwindow->edl->local_session->get_selectionend(1);
116                 view_start = mwindow->edl->local_session->view_start[pane->number];
117                 zoom_sample = mwindow->edl->local_session->zoom_sample;
118 //printf("MainCursor::draw %f %f\n", selectionstart, selectionend);
119
120                 pixel1 = Units::to_int64((selectionstart * 
121                         mwindow->edl->session->sample_rate / 
122                         zoom_sample - 
123                         view_start));
124                 pixel2 = Units::to_int64((selectionend *
125                         mwindow->edl->session->sample_rate / 
126                         zoom_sample - 
127                         view_start));
128                 if(pixel1 < -10) pixel1 = -10;
129                 if(pixel2 > pane->canvas->get_w() + 10) 
130                         pixel2 = pane->canvas->get_w() + 10;
131                 if(pixel2 < pixel1) pixel2 = pixel1;
132 //printf("MainCursor::draw 2\n");
133         }
134
135         pane->canvas->set_color(WHITE);
136         pane->canvas->set_inverse();
137         pane->canvas->draw_box(pixel1, 0, pixel2 - pixel1 + 1, pane->canvas->get_h());
138         pane->canvas->set_opaque();
139         if(do_plugintoggles) pane->canvas->refresh_plugintoggles();
140         visible = !visible;
141 }
142
143 // Draw the cursor in a new location
144 void MainCursor::update()
145 {
146         int64_t old_pixel1 = pixel1;
147         int64_t old_pixel2 = pixel2;
148
149         if(visible)
150         {
151                 hide(0);
152         }
153
154         show(1);
155         if(old_pixel1 != pixel1 || old_pixel2 != pixel2)
156                 pane->canvas->flash(old_pixel1, 
157                         0, 
158                         old_pixel2 - old_pixel1 + 1, 
159                         pane->canvas->get_h());
160         flash();
161 }
162
163
164 void MainCursor::flash()
165 {
166         pane->canvas->flash(pixel1, 0, pixel2 - pixel1 + 1, pane->canvas->get_h());
167 }
168
169 void MainCursor::hide(int do_plugintoggles)
170 {
171         if(visible) draw(do_plugintoggles);
172 }
173
174 void MainCursor::show(int do_plugintoggles)
175 {
176         if(!visible) draw(do_plugintoggles);
177 }
178
179 // Constitutively redraw the cursor after it is overwritten by a draw
180 void MainCursor::restore(int do_plugintoggles)
181 {
182         if(visible)
183         {
184                 draw(do_plugintoggles);
185                 visible = 1;
186         }
187 }