mask tweaks, focus follows centroid, gradient/colorpicker rework, no hard edges in...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / dragcheckbox.C
1 #include "automation.h"
2 #include "bctoggle.h"
3 #include "canvas.h"
4 #include "cwindow.h"
5 #include "cwindowgui.h"
6 #include "dragcheckbox.h"
7 #include "edl.h"
8 #include "edlsession.h"
9 #include "mwindow.h"
10 #include "theme.h"
11 #include "track.h"
12 #include "vframe.h"
13
14 DragCheckBox::DragCheckBox(MWindow *mwindow,
15         int x, int y, const char *text, int *value,
16         float drag_x, float drag_y, float drag_w, float drag_h)
17  : BC_CheckBox(x, y, value, text)
18 {
19         this->mwindow = mwindow;
20         this->drag_x = drag_x;  this->drag_y = drag_y;
21         this->drag_w = drag_w;  this->drag_h = drag_h;
22         drag_dx = drag_dy = 0;
23         grabbed = 0;
24         dragging = 0;
25         pending = 0;
26 }
27
28 DragCheckBox::~DragCheckBox()
29 {
30         drag_deactivate();
31 }
32
33 int DragCheckBox::get_track_w()
34 {
35         Track *track = get_drag_track();
36         return track ? track->track_w : mwindow->edl->session->output_w;
37 }
38 int DragCheckBox::get_track_h()
39 {
40         Track *track = get_drag_track();
41         return track ? track->track_h : mwindow->edl->session->output_h;
42 }
43
44 void DragCheckBox::create_objects()
45 {
46         if( !drag_w ) drag_w = get_track_w();
47         if( !drag_h ) drag_h = get_track_h();
48         if( get_value() )
49                 drag_activate();
50 }
51
52 int DragCheckBox::handle_event()
53 {
54         int ret = BC_CheckBox::handle_event();
55         if( get_value() ) {
56                 if( drag_activate() ) {
57                         update(*value=0);
58                         flicker(10,50);
59                 }
60         }
61         else
62                 drag_deactivate();
63         return ret;
64 }
65
66 int DragCheckBox::drag_activate()
67 {
68         int ret = 0;
69         if( !grabbed && !(grabbed = grab(mwindow->cwindow->gui)) ) {
70                 ret = 1;
71         }
72         pending = 0;
73         dragging = 0;
74         return ret;
75 }
76
77 void DragCheckBox::drag_deactivate()
78 {
79         if( grabbed ) {
80                 ungrab(mwindow->cwindow->gui);
81                 grabbed = 0;
82         }
83         pending = 0;
84         dragging = 0;
85 }
86
87 int DragCheckBox::check_pending()
88 {
89         if( pending && !grab_event_count() ) {
90                 pending = 0;
91                 update_gui();
92         }
93         return 0;
94 }
95
96 int DragCheckBox::grab_event(XEvent *event)
97 {
98         switch( event->type ) {
99         case ButtonPress: break;
100         case ButtonRelease: break;
101         case MotionNotify: break;
102         default:
103                 return check_pending();
104         }
105
106         CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
107         CWindowCanvas *canvas = cwindow_gui->canvas;
108         int cx, cy;  cwindow_gui->get_relative_cursor(cx, cy);
109         cx -= mwindow->theme->ccanvas_x;
110         cy -= mwindow->theme->ccanvas_y;
111
112         if( !dragging ) {
113                 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ||
114                     cy < 0 || cy >= mwindow->theme->ccanvas_h )
115                         return check_pending();
116         }
117
118         switch( event->type ) {
119         case ButtonPress:
120                 if( !dragging ) break;
121                 return 1;
122         case ButtonRelease:
123                 if( !dragging ) return check_pending();
124                 dragging = 0;
125                 return 1;
126         case MotionNotify:
127                 if( !dragging ) return check_pending();
128                 break;
129         default:
130                 return check_pending();
131         }
132
133         int track_w = get_track_w(), track_h = get_track_h();
134         if( !drag_w ) drag_w = track_w;
135         if( !drag_h ) drag_h = track_h;
136
137         int64_t position = get_drag_position();
138         float cursor_x = cx, cursor_y = cy;
139         canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
140         float projector_x, projector_y, projector_z;
141         Track *track = get_drag_track();
142         if( track ) {
143                 track->automation->get_projector(
144                         &projector_x, &projector_y, &projector_z,
145                         position, PLAY_FORWARD);
146                 projector_x += mwindow->edl->session->output_w / 2;
147                 projector_y += mwindow->edl->session->output_h / 2;
148                 cursor_x = (cursor_x - projector_x) / projector_z + track_w / 2;
149                 cursor_y = (cursor_y - projector_y) / projector_z + track_h / 2;
150         }
151         float r = MIN(track_w, track_h)/100.f + 2;
152         float x0 = drag_x, x1 = drag_x+(drag_w+1)/2, x2 = drag_x+drag_w;
153         float y0 = drag_y, y1 = drag_y+(drag_h+1)/2, y2 = drag_y+drag_h;
154         if( !dragging ) {  // clockwise
155                      if( fabs(drag_dx = cursor_x-x0) < r &&  // x0,y0
156                          fabs(drag_dy = cursor_y-y0) < r ) dragging = 1;
157                 else if( fabs(drag_dx = cursor_x-x1) < r &&  // x1,y0
158                          fabs(drag_dy = cursor_y-y0) < r ) dragging = 2;
159                 else if( fabs(drag_dx = cursor_x-x2) < r &&  // x2,y0
160                          fabs(drag_dy = cursor_y-y0) < r ) dragging = 3;
161                 else if( fabs(drag_dx = cursor_x-x2) < r &&  // x2,y1
162                          fabs(drag_dy = cursor_y-y1) < r ) dragging = 4;
163                 else if( fabs(drag_dx = cursor_x-x2) < r &&  // x2,y2
164                          fabs(drag_dy = cursor_y-y2) < r ) dragging = 5;
165                 else if( fabs(drag_dx = cursor_x-x1) < r &&  // x1,y2
166                          fabs(drag_dy = cursor_y-y2) < r ) dragging = 6;
167                 else if( fabs(drag_dx = cursor_x-x0) < r &&  // x0,y2
168                          fabs(drag_dy = cursor_y-y2) < r ) dragging = 7;
169                 else if( fabs(drag_dx = cursor_x-x0) < r &&  // x0,y1
170                          fabs(drag_dy = cursor_y-y1) < r ) dragging = 8;
171                 else if( fabs(drag_dx = cursor_x-x1) < r &&  // x1,y1
172                          fabs(drag_dy = cursor_y-y1) < r ) dragging = 9;
173                 else
174                         return 0;
175                 return 1;
176         }
177         int cur_x = cursor_x - drag_dx;
178         int cur_y = cursor_y - drag_dy;
179         switch( dragging ) {
180         case 1: { // x0,y0
181                 float dx = cur_x - x0;
182                 float dy = cur_y - y0;
183                 if( !dx && !dy ) return 1;
184                 if( (drag_w-=dx) < 1 ) drag_w = 1;
185                 if( (drag_h-=dy) < 1 ) drag_h = 1;
186                 drag_x = cur_x;   drag_y = cur_y;
187                 break; }
188         case 2: { // x1,y0
189                 float dy = cur_y - y0;
190                 if( !dy ) return 1;
191                 drag_y = cur_y;
192                 if( (drag_h-=dy) < 1 ) drag_h = 1;
193                 break; }
194         case 3: { // x2,y0
195                 float dx = cur_x - x2;
196                 float dy = cur_y - y0;
197                 if( (drag_w+=dx) < 1 ) drag_w = 1;
198                 if( (drag_h-=dy) < 1 ) drag_h = 1;
199                 drag_y = cur_y;
200                 break; }
201         case 4: { // x2,y1
202                 float dx = cur_x - x2;
203                 if( !dx ) return 1;
204                 if( (drag_w+=dx) < 1 ) drag_w = 1;
205                 break; }
206         case 5: { // x2,y2
207                 float dx = cur_x - x2;
208                 float dy = cur_y - y2;
209                 if( (drag_w+=dx) < 1 ) drag_w = 1;
210                  if( (drag_h+=dy) < 1 ) drag_h = 1;
211                 break; }
212         case 6: { // x1,y2
213                 float dy = cur_y - y2;
214                 if( !dy ) return 1;
215                 if( (drag_h+=dy) < 1 ) drag_h = 1;
216                 break; }
217         case 7: { // x0,y2
218                 float dx = cur_x - x0;
219                 float dy = cur_y - y2;
220                 if( (drag_w-=dx) < 1 ) drag_w = 1;
221                 if( (drag_h+=dy) < 1 ) drag_h = 1;
222                 drag_x = cur_x;
223                 break; }
224         case 8: { // x0,y1
225                 float dx = cur_x - x0;
226                 if( !dx ) return 1;
227                 if( (drag_w-=dx) < 1 ) drag_w = 1;
228                 drag_x = cur_x;
229                 break; }
230         case 9: { // x1,y1
231                 float dx = cur_x - x1;
232                 float dy = cur_y - y1;
233                 if( !dx && !dy ) return 1;
234                 drag_x += dx;
235                 drag_y += dy;
236                 }
237         }
238         if( grab_event_count() )
239                 pending = 1;
240         else if( dragging ) {
241                 pending = 0;
242                 update_gui();
243         }
244         return 1;
245 }
246
247
248 void DragCheckBox::bound()
249 {
250         int trk_w = get_track_w(), trk_h = get_track_h();
251         float x1 = drag_x, x2 = x1 + drag_w;
252         float y1 = drag_y, y2 = y1 + drag_h;
253         bclamp(x1, 0, trk_w);  bclamp(x2, 0, trk_w);
254         bclamp(y1, 0, trk_h);  bclamp(y2, 0, trk_h);
255         if( x1 >= x2 ) { if( x2 > 0 ) x1 = x2-1; else x2 = (x1=0)+1; }
256         if( y1 >= y2 ) { if( x2 > 0 ) y1 = y2-1; else y2 = (y1=0)+1; }
257         drag_x = x1;  drag_y = y1;  drag_w = x2-x1;  drag_h = y2-y1;
258 }
259
260 void DragCheckBox::draw_boundary(VFrame *out,
261         int x, int y, int w, int h)
262 {
263         int iw = out->get_w(), ih = out->get_h();
264         int mr = MIN(iw, ih)/200 + 2, rr = 2*mr;
265         int r2 = (rr+1)/2;
266         int x0 = x-r2, x1 = x+(w+1)/2, x2 = x+w+r2;
267         int y0 = y-r2, y1 = y+(h+1)/2, y2 = y+h+r2;
268         int st = 1;
269         for( int r=2; r<mr; r<<=1 ) st = r;
270         out->set_stiple(st);
271
272         for( int r=mr/2; --r>=0; ) { // bbox
273                 int lft = x+r, rgt = x+w-1-r;
274                 int top = y+r, bot = y+h-1-r;
275                 out->draw_line(lft,top, rgt,top);
276                 out->draw_line(rgt,top, rgt,bot);
277                 out->draw_line(rgt,bot, lft,bot);
278                 out->draw_line(lft,bot, lft,top);
279         }
280
281         for( int r=mr; r<rr; ++r ) { // center
282                 out->draw_smooth(x1-r,y1, x1-r,y1+r, x1,y1+r);
283                 out->draw_smooth(x1,y1+r, x1+r,y1+r, x1+r,y1);
284                 out->draw_smooth(x1+r,y1, x1+r,y1-r, x1,y1-r);
285                 out->draw_smooth(x1,y1-r, x1-r,y1-r, x1-r,y1);
286         }
287
288         for( int r=rr; --r>=0; ) { // edge arrows
289                 out->draw_line(x1-r,y0+r, x1+r,y0+r);
290                 out->draw_line(x2-r,y1-r, x2-r,y1+r);
291                 out->draw_line(x1-r,y2-r, x1+r,y2-r);
292                 out->draw_line(x0+r,y1+r, x0+r,y1-r);
293         }
294         x0 += r2;  y0 += r2;  x2 -= r2;  y2 -= r2;
295         for( int r=2*mr; --r>=0; ) { // corner arrows
296                 out->draw_line(x0,y0+r, x0+r,y0);
297                 out->draw_line(x2,y0+r, x2-r,y0);
298                 out->draw_line(x2,y2-r, x2-r,y2);
299                 out->draw_line(x0,y2-r, x0+r,y2);
300         }
301         out->set_stiple(0);
302 }
303