33f5d57575c33b0566213167ef334cc45895275a
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / sketcher / sketcherwindow.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2014 Adam Williams <broadcast at earthling dot 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "automation.h"
22 #include "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "sketcher.h"
25 #include "sketcherwindow.h"
26 #include "cstrdup.h"
27 #include "cwindow.h"
28 #include "cwindowgui.h"
29 #include "edl.h"
30 #include "edlsession.h"
31 #include "keys.h"
32 #include "language.h"
33 #include "mainerror.h"
34 #include "mwindow.h"
35 #include "plugin.h"
36 #include "pluginserver.h"
37 #include "theme.h"
38 #include "track.h"
39
40 #define AltMask Mod1Mask
41
42 #define COLOR_W 40
43 #define COLOR_H 24
44
45 const char *SketcherPoint::types[] = {
46         N_("off"),
47         N_("line"),
48         N_("curve"),
49         N_("fill"),
50 };
51 const char *SketcherCurve::pens[] = {
52         N_("off"),
53         N_("box"),
54         N_("+"),
55         N_("/"),
56         N_("X"),
57 };
58
59
60 SketcherCurvePenItem::SketcherCurvePenItem(int pen)
61  : BC_MenuItem(_(SketcherCurve::pens[pen]))
62 {
63         this->pen = pen;
64 }
65 int SketcherCurvePenItem::handle_event()
66 {
67         SketcherCurvePen *popup = (SketcherCurvePen*)get_popup_menu();
68         popup->update(pen);
69         SketcherWindow *gui = popup->gui;
70         SketcherConfig &config = gui->plugin->config;
71         int ci = config.cv_selected;
72         if( ci >= 0 && ci < config.curves.size() ) {
73                 SketcherCurve *cv = config.curves[ci];
74                 cv->pen = pen;
75                 gui->curve_list->update(ci);
76                 gui->send_configure_change();
77         }
78         return 1;
79 }
80
81 SketcherCurvePen::SketcherCurvePen(SketcherWindow *gui, int x, int y, int pen)
82  : BC_PopupMenu(x,y,72,_(cv_pen[pen]))
83 {
84         this->gui = gui;
85         this->pen = pen;
86 }
87 void SketcherCurvePen::create_objects()
88 {
89         int n = sizeof(cv_pen)/sizeof(cv_pen[0]);
90         for( int pen=0; pen<n; ++pen )
91                 add_item(new SketcherCurvePenItem(pen));
92 }
93 void SketcherCurvePen::update(int pen)
94 {
95         set_text(_(cv_pen[this->pen=pen]));
96 }
97
98
99 SketcherCurveColor::SketcherCurveColor(SketcherWindow *gui, int x, int y, int w, int h)
100  : BC_Button(x, y, w, vframes)
101 {
102         this->gui = gui;
103         this->color = CV_COLOR;
104         for( int i=0; i<3; ++i ) {
105                 vframes[i] = new VFrame(w, h, BC_RGB888);
106                 vframes[i]->clear_frame();
107         }
108 }
109
110 SketcherCurveColor::~SketcherCurveColor()
111 {
112         for( int i=0; i<3; ++i )
113                 delete vframes[i];
114 }
115
116 void SketcherCurveColor::set_color(int color)
117 {
118         this->color = color;
119         int r = (color>>16) & 0xff;
120         int g = (color>>8) & 0xff;
121         int b = (color>>0) & 0xff;
122         for( int i=0; i<3; ++i ) {
123                 VFrame *vframe = vframes[i];
124                 int ww = vframe->get_w(), hh = vframe->get_h();
125                 uint8_t **rows = vframe->get_rows();
126                 int rr = r, gg = g, bb = b;
127                 switch( i ) {
128                 case BUTTON_UP:
129                         break;
130                 case BUTTON_UPHI:
131                         if( (rr+=48) > 0xff ) rr = 0xff;
132                         if( (gg+=48) > 0xff ) gg = 0xff;
133                         if( (bb+=48) > 0xff ) bb = 0xff;
134                         break;
135                 case BUTTON_DOWNHI:
136                         if( (rr-=48) < 0x00 ) rr = 0x00;
137                         if( (gg-=48) < 0x00 ) gg = 0x00;
138                         if( (bb-=48) < 0x00 ) bb = 0x00;
139                         break;
140                 }
141                 for( int y=0; y<hh; ++y ) {
142                         uint8_t *rp = rows[y];
143                         for( int x=0; x<ww; ++x ) {
144                                 *rp++ = rr;  *rp++ = gg;  *rp++ = bb;
145                         }
146                 }
147         }
148         set_images(vframes);
149 }
150
151 void SketcherCurveColor::update_gui(int color)
152 {
153         set_color(color);
154         draw_face();
155 }
156
157 int SketcherCurveColor::handle_event()
158 {
159         gui->start_color_thread(this);
160         return 1;
161 }
162
163 SketcherCurveColorPicker::SketcherCurveColorPicker(SketcherWindow *gui, SketcherCurveColor *color_button)
164  : ColorPicker(1, _("Color"))
165 {
166         this->gui = gui;
167         this->color_button = color_button;
168         this->color = CV_COLOR;
169         color_update = new SketcherCurveColorThread(this);
170 }
171
172 SketcherCurveColorPicker::~SketcherCurveColorPicker()
173 {
174         delete color_update;
175 }
176
177 void SketcherCurveColorPicker::start(int color)
178 {
179         this->color = color;
180         int alpha = (~color>>24) & 0xff;
181         start_window(color & 0xffffff, alpha, 1);
182         color_update->start();
183 }
184
185 void SketcherCurveColorPicker::handle_done_event(int result)
186 {
187         color_update->stop();
188         gui->lock_window("SketcherCurveColorPicker::handle_done_event");
189         if( result ) { color = orig_color | (~orig_alpha<<24); }
190         color_button->update_gui(color);
191         gui->unlock_window();
192         SketcherConfig &config = gui->plugin->config;
193         int ci = config.cv_selected;
194         if( ci >= 0 && ci < config.curves.size() ) {
195                 SketcherCurve *cv = config.curves[ci];
196                 cv->color = color;
197                 gui->curve_list->update(ci);
198                 gui->send_configure_change();
199         }
200 }
201
202 int SketcherCurveColorPicker::handle_new_color(int color, int alpha)
203 {
204         this->color = color | (~alpha<<24);
205         color_update->update_lock->unlock();
206         return 1;
207 }
208
209 void SketcherCurveColorPicker::update_gui()
210 {
211         gui->lock_window("SketcherCurveColorPicker::update_gui");
212         color_button->update_gui(color);
213         SketcherConfig &config = gui->plugin->config;
214         int ci = config.cv_selected;
215         if( ci >= 0 ) {
216                 SketcherCurve *cv = config.curves[ci];
217                 cv->color = color;
218                 gui->curve_list->update(ci);
219                 gui->send_configure_change();
220         }
221         gui->unlock_window();
222 }
223
224 SketcherCurveColorThread::SketcherCurveColorThread(SketcherCurveColorPicker *color_picker)
225  : Thread(1, 0, 0)
226 {
227         this->color_picker = color_picker;
228         this->update_lock = new Condition(0,"SketcherCurveColorThread::update_lock");
229         done = 1;
230 }
231
232 SketcherCurveColorThread::~SketcherCurveColorThread()
233 {
234         stop();
235         delete update_lock;
236 }
237
238 void SketcherCurveColorThread::start()
239 {
240         if( done ) {
241                 done = 0;
242                 Thread::start();
243         }
244 }
245
246 void SketcherCurveColorThread::stop()
247 {
248         if( !done ) {
249                 done = 1;
250                 update_lock->unlock();
251                 join();
252         }
253 }
254
255 void SketcherCurveColorThread::run()
256 {
257         while( !done ) {
258                 update_lock->lock("SketcherCurveColorThread::run");
259                 if( done ) break;
260                 color_picker->update_gui();
261         }
262 }
263
264
265 SketcherCoord::SketcherCoord(SketcherWindow *gui, int x, int y,
266                 coord output, coord mn, coord mx)
267  : BC_TumbleTextBox(gui, output, mn, mx, x, y, 64, 1)
268 {
269         this->gui = gui;
270         set_increment(1);
271 }
272 SketcherCoord::~SketcherCoord()
273 {
274 }
275
276 SketcherNum::SketcherNum(SketcherWindow *gui, int x, int y,
277                 int output, int mn, int mx)
278  : BC_TumbleTextBox(gui, output, mn, mx, x, y, 54)
279 {
280         this->gui = gui;
281         set_increment(1);
282 }
283 SketcherNum::~SketcherNum()
284 {
285 }
286
287 int SketcherPointX::handle_event()
288 {
289         if( !SketcherCoord::handle_event() ) return 0;
290         SketcherConfig &config = gui->plugin->config;
291         int ci = config.cv_selected;
292         if( ci >= 0 && ci < config.curves.size() ) {
293                 SketcherCurve *cv = config.curves[ci];
294                 SketcherPointList *point_list = gui->point_list;
295                 int pi = config.pt_selected;
296                 SketcherPoints &points = cv->points;
297                 if( pi >= 0 && pi < points.size() ) {
298                         coord v = atof(get_text());
299                         points[pi]->x = v;
300                         point_list->set_point(pi, PT_X, v);
301                         point_list->update_list(pi);
302                         gui->send_configure_change();
303                 }
304         }
305         return 1;
306 }
307 int SketcherPointY::handle_event()
308 {
309         if( !SketcherCoord::handle_event() ) return 0;
310         SketcherConfig &config = gui->plugin->config;
311         int ci = config.cv_selected;
312         if( ci >= 0 && ci < config.curves.size() ) {
313                 SketcherCurve *cv = config.curves[ci];
314                 SketcherPointList *point_list = gui->point_list;
315                 int pi = config.pt_selected;
316                 SketcherPoints &points = cv->points;
317                 if( pi >= 0 && pi < points.size() ) {
318                         coord v = atof(get_text());
319                         points[pi]->y = v;
320                         point_list->set_point(pi, PT_Y, v);
321                         point_list->update_list(pi);
322                         gui->send_configure_change();
323                 }
324         }
325         return 1;
326 }
327
328 int SketcherPointId::handle_event()
329 {
330         if( !SketcherNum::handle_event() ) return 0;
331         SketcherConfig &config = gui->plugin->config;
332         int ci = config.cv_selected;
333         if( ci >= 0 && ci < config.curves.size() ) {
334                 SketcherCurve *cv = config.curves[ci];
335                 SketcherPointList *point_list = gui->point_list;
336                 int pi = config.pt_selected;
337                 SketcherPoints &points = cv->points;
338                 if( pi >= 0 && pi < points.size() ) {
339                         int id = atoi(get_text());
340                         points[pi]->id = id;
341                         point_list->set_point(pi, PT_ID, id);
342                         point_list->update_list(pi);
343                         gui->send_configure_change();
344                 }
345         }
346         return 1;
347 }
348
349 SketcherCurveWidth::SketcherCurveWidth(SketcherWindow *gui, int x, int y, int width)
350  : SketcherNum(gui, x, y, width, 0, 255)
351 {
352         this->width = width;
353 }
354 SketcherCurveWidth::~SketcherCurveWidth()
355 {
356 }
357
358 int SketcherCurveWidth::handle_event()
359 {
360         if( !SketcherNum::handle_event() ) return 0;
361         SketcherConfig &config = gui->plugin->config;
362         int ci = config.cv_selected;
363         if( ci >= 0 && ci < config.curves.size() ) {
364                 SketcherCurve *cv = config.curves[ci];
365                 int v = atoi(get_text());
366                 cv->width = v;
367                 gui->curve_list->update(ci);
368                 gui->send_configure_change();
369         }
370         return 1;
371 }
372
373 void SketcherCurveWidth::update(int width)
374 {
375         SketcherNum::update(this->width=width);
376 }
377
378
379 SketcherWindow::SketcherWindow(Sketcher *plugin)
380  : PluginClientWindow(plugin, 380, 620, 380, 620, 0)
381 {
382         this->plugin = plugin;
383         this->title_pen = 0;  this->curve_pen = 0;
384         this->title_color = 0; this->curve_color = 0;
385         this->color_picker = 0; this->drag = 0;
386         this->new_curve = 0;  this->del_curve = 0;
387         this->curve_up = 0;   this->curve_dn = 0;
388         this->title_x = 0;    this->point_x = 0;
389         this->title_y = 0;    this->point_y = 0;
390         this->title_id = 0;   this->point_id = 0;
391         this->new_point = 0;  this->del_point = 0;
392         this->point_up = 0;   this->point_dn = 0;
393         this->point_list = 0; this->notes0 = 0;
394         this->notes1 = 0;     this->notes2 = 0;
395
396         position = -1;
397         track_w = track_h -1;
398         cursor_x = cursor_y = -1;
399         output_x = output_y = -1;
400         last_x = last_y = -1;
401         projector_x = projector_y = projector_z = -1;
402         state = 0;  dragging = 0;
403         new_points = 0;
404         pending_motion = 0;
405         pending_config = 0;
406 }
407
408 SketcherWindow::~SketcherWindow()
409 {
410         delete curve_width;
411         delete point_x;
412         delete point_y;
413         delete color_picker;
414 }
415
416 void SketcherWindow::create_objects()
417 {
418         int x = 10, y = 10, dy = 0, x1, y1;
419         int margin = plugin->get_theme()->widget_border;
420         BC_Title *title;
421         int ci = plugin->config.cv_selected;
422         if( ci < 0 || ci >= plugin->config.curves.size() )
423                 ci = plugin->new_curve();
424         SketcherCurve *cv = plugin->config.curves[ci];
425
426         reset_curves = new SketcherResetCurves(this, plugin, x1=x, y+3);
427         add_subwindow(reset_curves);    dy = bmax(dy,reset_curves->get_h());
428         x1 += reset_curves->get_w() + 2*margin;
429         const char *curve_text = _("Curve");
430         title_width = new BC_Title(x1, y, _("Width:"));
431         add_subwindow(title_width);     dy = bmax(dy,title_width->get_h());
432         x1 += title_width->get_w() + margin;
433         curve_width = new SketcherCurveWidth(this, x1, y, cv->width);
434         curve_width->create_objects();
435         y += dy + 2*margin;             dy = 0;
436
437         x1 = get_w()-x - BC_Title::calculate_w(this, curve_text, LARGEFONT);
438         y1 = y-margin - BC_Title::calculate_h(this, curve_text, LARGEFONT);
439         title = new BC_Title(x1, y1, curve_text, LARGEFONT,
440                 get_resources()->menu_highlighted_fontcolor);
441         add_subwindow(title);           dy = bmax(dy,title->get_h());
442         curve_list = new SketcherCurveList(this, plugin, x, y);
443         add_subwindow(curve_list);      dy = bmax(dy,curve_list->get_h());
444         y += dy + margin;               dy = 0;
445
446         new_curve = new SketcherNewCurve(this, plugin, x1=x, y);
447         add_subwindow(new_curve);       dy = bmax(dy,new_curve->get_h());
448         x1 += new_curve->get_w() + margin;
449         curve_up = new SketcherCurveUp(this, x1, y);
450         add_subwindow(curve_up);        dy = bmax(dy,curve_up->get_h());
451         x1 += curve_up->get_w() + 4*margin;
452         y1 = BC_Title::calculate_h(this, _("Pen:"));
453         title_pen = new BC_Title(x1+30, y+dy-y1, _("Pen:"));
454         add_subwindow(title_pen);       dy = bmax(dy,title_pen->get_h());
455         int x2 = (get_w()+x1)/2 + 20;
456         y1 = BC_Title::calculate_h(this, _("Color:"));
457         title_color = new BC_Title(x2, y+dy-y1, _("Color:"));
458         add_subwindow(title_color);     dy = bmax(dy,title_color->get_h());
459         y += dy + margin;               dy = 0;
460
461         del_curve = new SketcherDelCurve(this, plugin, x1=x, y);
462         add_subwindow(del_curve);       dy = bmax(dy,del_curve->get_h());
463         x1 += del_curve->get_w() + margin;
464         curve_dn = new SketcherCurveDn(this, x1, y);
465         add_subwindow(curve_dn);        dy = bmax(dy,curve_dn->get_h());
466         x1 += curve_dn->get_w() + 4*margin;
467         curve_pen = new SketcherCurvePen(this, x1, y, cv->pen);
468         add_subwindow(curve_pen);       dy = bmax(dy,curve_pen->get_h());
469         curve_pen->create_objects();
470         curve_color = new SketcherCurveColor(this, x2, y, COLOR_W, COLOR_H);
471         add_subwindow(curve_color);     dy = bmax(dy,curve_color->get_h());
472         curve_color->set_color(cv->color);
473         curve_color->draw_face();
474         y += dy + margin;  dy = 0;
475         curve_list->update(ci);
476
477         BC_Bar *bar;
478         bar = new BC_Bar(x, y, get_w()-2*x);
479         add_subwindow(bar);             dy = bmax(dy,bar->get_h());
480         bar = new BC_Bar(x, y+=dy, get_w()-2*x);
481         add_subwindow(bar);             dy = bmax(dy,bar->get_h());
482         y += dy + 2*margin;
483
484         int pi = plugin->config.pt_selected;
485         SketcherPoint *pt = pi >= 0 && pi < cv->points.size() ? cv->points[pi] : 0;
486         reset_points = new SketcherResetPoints(this, plugin, x1=x, y+3);
487         add_subwindow(reset_points);    dy = bmax(dy,reset_points->get_h());
488         x1 += reset_points->get_w() + 2*margin; 
489         if( plugin->config.drag ) {
490                 if( !grab(plugin->server->mwindow->cwindow->gui) ) {
491                         eprintf("drag enabled, but compositor already grabbed\n");
492                         plugin->config.drag = 0;
493                 }
494         }
495         drag = new SketcherDrag(this, x1, y);
496         add_subwindow(drag);            dy = bmax(dy,drag->get_h());
497         x1 += drag->get_w() + 2*margin;
498         int arc = pt ? pt->arc : ARC_LINE;
499         point_type = new SketcherPointType(this, x1, y, arc);
500         add_subwindow(point_type);      dy = bmax(dy,point_type->get_h());
501         point_type->create_objects();
502         y += dy + margin;  dy = 0;
503
504         const char *point_text = _("Point");
505         x1 = get_w()-x - BC_Title::calculate_w(this, point_text, LARGEFONT);
506         y1 = y-margin - BC_Title::calculate_h(this, point_text, LARGEFONT);
507         add_subwindow(title = new BC_Title(x1, y1, point_text, LARGEFONT,
508                 get_resources()->menu_highlighted_fontcolor));
509         point_list = new SketcherPointList(this, plugin, x, y);
510         add_subwindow(point_list);      dy = bmax(dy,point_list->get_h());
511         y += dy + margin;               dy = 0;
512
513         new_point = new SketcherNewPoint(this, plugin, x1=x, y);
514         add_subwindow(new_point);       dy = bmax(dy,new_point->get_h());
515         x1 += new_point->get_w() + margin;
516         point_up = new SketcherPointUp(this, x1, y);
517         add_subwindow(point_up);        dy = bmax(dy,point_up->get_h());
518         x1 += point_up->get_w() + 2*margin;
519         title_x = new BC_Title(x1, y, _("X:"));
520         add_subwindow(title_x);         dy = bmax(dy,title_x->get_h());
521         x1 += title_x->get_w() + margin;
522         point_x = new SketcherPointX(this, x1, y, !pt ? 0.f : pt->x);
523         point_x->create_objects();      dy = bmax(dy, point_x->get_h());
524         x2 = x1 + point_x->get_w() + 2*margin + 10;
525         y1 = BC_Title::calculate_h(this, _("ID:"));
526         title_id = new BC_Title(x2+16, y+dy-y1, _("ID:"));
527         add_subwindow(title_id);        dy = bmax(dy, title_id->get_h());
528         y += dy + margin;  dy = 0;
529
530         del_point = new SketcherDelPoint(this, plugin, x1=x, y);
531         add_subwindow(del_point);       dy = bmax(dy,del_point->get_h());
532         x1 += del_point->get_w() + margin;
533         point_dn = new SketcherPointDn(this, x1, y);
534         add_subwindow(point_dn);        dy = bmax(dy,point_dn->get_h());
535         x1 += point_dn->get_w() + 2*margin;
536         title_y = new BC_Title(x1, y, _("Y:"));
537         add_subwindow(title_y);         dy = bmax(dy,title_y->get_h());
538         x1 += title_y->get_w() + margin;
539         point_y = new SketcherPointY(this, x1, y, !pt ? 0.f : pt->y);
540         point_y->create_objects();      dy = bmax(dy, point_y->get_h());
541         point_id = new SketcherPointId(this, x2, y, !pt ? 0 : pt->id);
542         point_id->create_objects();     dy = bmax(dy, point_id->get_h());
543         y += dy + margin + 5;           dy = 0;
544         point_list->update(pi);
545
546         bar = new BC_Bar(x, y, get_w()-2*x);
547         add_subwindow(bar);             dy = bmax(dy,bar->get_h());
548         y += dy + 2*margin;
549
550         add_subwindow(notes0 = new BC_Title(x, y,
551                  _("\n"
552                    "Shift=\n"
553                    "None=\n"
554                    "Ctrl=\n"
555                    "Alt=\n"
556                    "Ctrl+Shift=")));    dy = bmax(dy, notes0->get_h());
557         add_subwindow(notes1 = new BC_Title(x+100, y,
558                  _("     LMB\n"
559                    "new line point\n"
560                    "select point\n"
561                    "drag point\n"
562                    "drag all curves\n"
563                    "new fill point"))); dy = bmax(dy, notes1->get_h());
564         add_subwindow(notes2 = new BC_Title(x+220, y,
565                  _("      RMB\n"
566                    "new arc point\n"
567                    "select curve\n"
568                    "drag curve\n"
569                    "new curve\n"
570                    "new off point"))); dy = bmax(dy, notes2->get_h());
571         y += dy + margin + 10;
572
573         add_subwindow(notes3 = new BC_Title(x, y,
574                    "Key DEL= delete point, +Shift= delete curve\n"));
575         show_window(1);
576 }
577
578 void SketcherWindow::done_event(int result)
579 {
580         delete color_picker;  color_picker = 0;
581 }
582
583 void SketcherWindow::send_configure_change()
584 {
585         pending_config = 0;
586         plugin->send_configure_change();
587 }
588
589
590 int SketcherWindow::grab_event(XEvent *event)
591 {
592         int ret = do_grab_event(event);
593         if( !grab_event_count() ) {
594                 if( grab_cursor_motion() )
595                         pending_config = 1;
596                 if( pending_config ) {
597                         last_x = output_x;  last_y = output_y;
598                         send_configure_change();
599                 }
600         }
601         return ret;
602 }
603
604 int SketcherWindow::do_grab_event(XEvent *event)
605 {
606         switch( event->type ) {
607         case ButtonPress: break;
608         case ButtonRelease: break;
609         case MotionNotify: break;
610         case KeyPress:
611                 if( keysym_lookup(event) > 0 ) {
612                         switch( get_keysym() ) {
613                         case XK_Delete:
614                                 pending_config = 1;
615                                 return (event->xkey.state & ShiftMask) ?
616                                         del_curve->handle_event() :
617                                         del_point->handle_event() ;
618                         }
619                 } // fall thru
620         default:
621                 return 0;
622         }
623
624         MWindow *mwindow = plugin->server->mwindow;
625         CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
626         CWindowCanvas *canvas = cwindow_gui->canvas;
627         int cx, cy;  cwindow_gui->get_relative_cursor(cx, cy);
628         cx -= mwindow->theme->ccanvas_x;
629         cy -= mwindow->theme->ccanvas_y;
630
631         if( !dragging ) {
632                 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ||
633                     cy < 0 || cy >= mwindow->theme->ccanvas_h )
634                         return 0;
635         }
636
637         switch( event->type ) {
638         case ButtonPress:
639                 if( dragging ) return 0;
640                 dragging = 1;
641                 break;
642         case ButtonRelease:
643                 dragging = 0;
644                 break;
645         case MotionNotify:
646                 if( dragging ) break;
647         default: // fall thru
648                 return 0;
649         }
650
651         cursor_x = cx, cursor_y = cy;
652         canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
653         position = plugin->get_source_position();
654         Track *track = plugin->server->plugin->track;
655         track_w = track->track_w;
656         track_h = track->track_h;
657         track->automation->get_projector(
658                 &projector_x, &projector_y, &projector_z,
659                 position, PLAY_FORWARD);
660         projector_x += mwindow->edl->session->output_w / 2;
661         projector_y += mwindow->edl->session->output_h / 2;
662         output_x = (cursor_x - projector_x) / projector_z + track_w / 2;
663         output_y = (cursor_y - projector_y) / projector_z + track_h / 2;
664         state = event->xmotion.state;
665
666         if( event->type == MotionNotify ) {
667                 memcpy(&motion_event, event, sizeof(motion_event));
668                 pending_motion = 1;
669                 return 1;
670         }
671         if( grab_cursor_motion() )
672                 pending_config = 1;
673
674         switch( event->type ) {
675         case ButtonPress:
676                 pending_config = grab_button_press(event);
677                 break;
678         case ButtonRelease:
679                 new_points = 0;
680                 break;
681         }
682
683         return 1;
684 }
685
686 int SketcherWindow::grab_button_press(XEvent *event)
687 {
688         SketcherConfig &config = plugin->config;
689         int ci = config.cv_selected;
690         if( ci < 0 || ci >= plugin->config.curves.size() )
691                 return 0;
692         SketcherCurves &curves = config.curves;
693         SketcherCurve *cv = curves[ci];
694         SketcherPoints &points = cv->points;
695         int pi = config.pt_selected;
696
697         int button_no = event->xbutton.button;
698         switch( button_no ) {
699         case LEFT_BUTTON: {
700                 if( (state & ShiftMask) ) { // create new point/string
701                         ++new_points;
702                         pi = plugin->new_point(cv,
703                                 !(state & ControlMask) ? ARC_LINE : ARC_FILL,
704                                 output_x, output_y, pi+1);
705                         point_list->update(pi);
706                         break;
707                 }
708                 SketcherPoint *pt = 0; // select point
709                 double dist = cv->nearest_point(pi, output_x,output_y);
710                 if( dist >= 0 ) {
711                         pt = points[pi];
712                         Track *track = plugin->server->plugin->track;
713                         int track_w = track->track_w, track_h = track->track_h;
714                         float px = (pt->x - track_w / 2) * projector_z + projector_x;
715                         float py = (pt->y - track_h / 2) * projector_z + projector_y;
716                         float pix = DISTANCE(px, py, cursor_x,cursor_y);
717                         if( (state & ControlMask) && pix >= HANDLE_W ) { pi = -1;  pt = 0; }
718                 }
719                 point_list->set_selected(pi);
720                 break; }
721         case RIGHT_BUTTON: {
722                 if( (state & ShiftMask) ) { // create new curve point
723                         ++new_points;
724                         pi = plugin->new_point(cv,
725                                 !(state & ControlMask) ? ARC_CURVE : ARC_OFF,
726                                 output_x, output_y, pi+1);
727                         point_list->update(pi);
728                         break;
729                 }
730                 if( (state & AltMask) ) { // create new curve
731                         ci = plugin->new_curve(cv->pen, cv->width, curve_color->color);
732                         curve_list->update(ci);
733                         point_list->update(-1);
734                         break;
735                 }
736                 SketcherPoint *pt = 0;
737                 double dist = config.nearest_point(ci, pi, output_x,output_y);
738                 if( dist >= 0 ) {
739                         pt = curves[ci]->points[pi];
740                         Track *track = plugin->server->plugin->track;
741                         int track_w = track->track_w, track_h = track->track_h;
742                         float px = (pt->x - track_w / 2) * projector_z + projector_x;
743                         float py = (pt->y - track_h / 2) * projector_z + projector_y;
744                         float pix = DISTANCE(px, py, cursor_x,cursor_y);
745                         if( (state & ControlMask) && pix >= HANDLE_W ) { ci = pi = -1;  pt = 0; }
746                 }
747                 if( pt ) {
748                         curve_list->update(ci);
749                         point_list->update(pi);
750                 }
751                 break; }
752         }
753         return 1;
754 }
755
756 int SketcherWindow::grab_cursor_motion()
757 {
758         if( !pending_motion )
759                 return 0;
760         pending_motion = 0;
761         SketcherConfig &config = plugin->config;
762         int ci = config.cv_selected;
763         if( ci < 0 || ci >= plugin->config.curves.size() )
764                 return 0;
765         SketcherCurves &curves = config.curves;
766         SketcherCurve *cv = curves[ci];
767         SketcherPoints &points = cv->points;
768         int pi = config.pt_selected;
769
770         if( (state & ShiftMask) ) {  // string of points
771                 if( (state & (Button1Mask|Button3Mask)) ) {
772                         SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
773                         if( pt ) {
774                                 float dist = DISTANCE(pt->x, pt->y, output_x, output_y);
775                                 if( dist < get_w()*0.1 ) return 0; // tolerance w/10
776                         }
777                         ++new_points;
778                         int arc = (state & Button1Mask) ? ARC_LINE : ARC_CURVE;
779                         pi = plugin->new_point(cv, arc, output_x, output_y, pi+1);
780                         point_list->update(pi);
781                 }
782                 return 1;
783         }
784         if( (state & Button1Mask) ) {
785                 if( (state & ControlMask) ) { // drag selected point
786                         SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
787                         if( pt ) {
788                                 point_list->set_point(pi, PT_X, pt->x = output_x);
789                                 point_list->set_point(pi, PT_Y, pt->y = output_y);
790                                 point_list->update_list(pi);
791                                 point_x->update(pt->x);
792                                 point_y->update(pt->y);
793                         }
794                         return 1;
795                 }
796                 if( (state & AltMask) ) { // drag all curves
797                         int dx = round(output_x - last_x);
798                         int dy = round(output_y - last_y);
799                         for( int i=0; i<curves.size(); ++i ) {
800                                 SketcherCurve *crv = plugin->config.curves[i];
801                                 int pts = crv->points.size();
802                                 for( int k=0; k<pts; ++k ) {
803                                         SketcherPoint *pt = crv->points[k];
804                                         pt->x += dx;  pt->y += dy;
805                                 }
806                         }
807                         SketcherPoint *pt = pi >= 0 && pi < points.size() ?
808                                 points[pi] : 0;
809                         point_x->update(pt ? pt->x : 0.f);
810                         point_y->update(pt ? pt->y : 0.f);
811                         point_id->update(pt ? pt->id : 0);
812                         point_list->update(pi);
813                         return 1;
814                 }
815                 double dist = cv->nearest_point(pi, output_x,output_y);
816                 if( dist >= 0 )
817                         point_list->set_selected(pi);
818                 return 1;
819         }
820         if( (state & Button3Mask) ) {
821                 if( (state & (ControlMask | AltMask)) ) { // drag selected curve(s)
822                         int dx = round(output_x - last_x);
823                         int dy = round(output_y - last_y);
824                         for( int i=0; i<points.size(); ++i ) {
825                                 SketcherPoint *pt = points[i];
826                                 pt->x += dx;  pt->y += dy;
827                         }
828                         SketcherPoint *pt = pi >= 0 && pi < points.size() ?
829                                 points[pi] : 0;
830                         point_x->update(pt ? pt->x : 0.f);
831                         point_y->update(pt ? pt->y : 0.f);
832                         point_id->update(pt ? pt->id : 0);
833                         point_list->update(pi);
834                         return 1;
835                 }
836                 double dist = config.nearest_point(ci, pi, output_x,output_y);
837                 if( dist >= 0 ) {
838                         curve_list->update(ci);
839                         point_list->update(pi);
840                 }
841                 return 1;
842         }
843         return 0;
844 }
845
846 int SketcherWindow::keypress_event()
847 {
848         int key = get_keypress();
849         switch( key ) {
850         case DELETE: return shift_down() ?
851                         del_curve->handle_event() :
852                         del_point->handle_event() ;
853         }
854         return 0;
855 }
856
857 void SketcherWindow::start_color_thread(SketcherCurveColor *color_button)
858 {
859         unlock_window();
860         delete color_picker;
861         color_picker = new SketcherCurveColorPicker(this, color_button);
862         int color = CV_COLOR, ci = plugin->config.cv_selected;
863         if( ci >= 0 && ci < plugin->config.curves.size() ) {
864                 SketcherCurve *cv = plugin->config.curves[ci];
865                 color = cv->color;
866         }
867         color_picker->start(color);
868         lock_window("SketcherWindow::start_color_thread");
869 }
870
871
872 SketcherCurveList::SketcherCurveList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
873  : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
874 {
875         this->gui = gui;
876         this->plugin = plugin;
877         col_titles[CV_ID] = _("ID");      col_widths[CV_ID] = 64;
878         col_titles[CV_RAD] = _("width");  col_widths[CV_RAD] = 64;
879         col_titles[CV_PEN] = _("pen");    col_widths[CV_PEN] = 64;
880         col_titles[CV_CLR] = _("color");  col_widths[CV_CLR] = 80;
881         col_titles[CV_ALP] = _("alpha");  col_widths[CV_ALP] = 64;
882 }
883 SketcherCurveList::~SketcherCurveList()
884 {
885         clear();
886 }
887 void SketcherCurveList::clear()
888 {
889         for( int i=CV_SZ; --i>=0; )
890                 cols[i].remove_all_objects();
891 }
892
893 int SketcherCurveList::column_resize_event()
894 {
895         for( int i=CV_SZ; --i>=0; )
896                 col_widths[i] = get_column_width(i);
897         return 1;
898 }
899
900 int SketcherCurveList::handle_event()
901 {
902         int ci = get_selection_number(0, 0);
903         set_selected(ci);
904         gui->point_list->update(0);
905         gui->send_configure_change();
906         return 1;
907 }
908
909 int SketcherCurveList::selection_changed()
910 {
911         handle_event();
912         return 1;
913 }
914
915 void SketcherCurveList::set_selected(int k)
916 {
917         int ci = -1;
918         if( k >= 0 && k < plugin->config.curves.size() ) {
919                 SketcherCurve *cv = plugin->config.curves[k];
920                 gui->curve_width->update(cv->width);
921                 gui->curve_pen->update(cv->pen);
922                 gui->curve_color->update_gui(cv->color);
923                 ci = k;
924         }
925         plugin->config.cv_selected = ci;
926         update_list(ci);
927 }
928
929 void SketcherCurveList::update_list(int k)
930 {
931         int xpos = get_xposition(), ypos = get_yposition();
932         if( k >= 0 ) update_selection(&cols[0], k);
933         BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],CV_SZ, xpos,ypos,k);
934         center_selection();
935 }
936
937 void SketcherCurveList::update(int k)
938 {
939         clear();
940         SketcherCurves &curves = plugin->config.curves;
941         int sz = curves.size();
942         for( int i=0; i<sz; ++i ) {
943                 SketcherCurve *cv = curves[i];
944                 char itxt[BCSTRLEN];  sprintf(itxt,"%d", cv->id);
945                 char ptxt[BCSTRLEN];  sprintf(ptxt,"%s", cv_pen[cv->pen]);
946                 char rtxt[BCSTRLEN];  sprintf(rtxt,"%d", cv->width);
947                 int color = cv->color;
948                 int r = (color>>16)&0xff;
949                 int g = (color>> 8)&0xff;
950                 int b = (color>> 0)&0xff;
951                 int a = (~color>>24)&0xff;
952                 char ctxt[BCSTRLEN];  sprintf(ctxt,"#%02x%02x%02x", r, g, b);
953                 char atxt[BCSTRLEN];  sprintf(atxt,"%5.3f", a/255.);
954                 add_curve(itxt, ptxt, rtxt, ctxt, atxt);
955         }
956         set_selected(k);
957 }
958
959 void SketcherCurveList::add_curve(const char *id, const char *pen,
960                 const char *width, const char *color, const char *alpha)
961 {
962         cols[CV_ID].append(new BC_ListBoxItem(id));
963         cols[CV_RAD].append(new BC_ListBoxItem(width));
964         cols[CV_PEN].append(new BC_ListBoxItem(pen));
965         cols[CV_CLR].append(new BC_ListBoxItem(color));
966         cols[CV_ALP].append(new BC_ListBoxItem(alpha));
967 }
968
969 SketcherNewCurve::SketcherNewCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
970  : BC_GenericButton(x, y, 64, _("New"))
971 {
972         this->gui = gui;
973         this->plugin = plugin;
974 }
975 SketcherNewCurve::~SketcherNewCurve()
976 {
977 }
978 int SketcherNewCurve::handle_event()
979 {
980         int pen = gui->curve_pen->pen;
981         int color = gui->curve_color->color;
982         int width = gui->curve_width->width;
983         int ci = plugin->config.cv_selected;
984         if( ci >= 0 && ci < plugin->config.curves.size() ) {
985                 SketcherCurve *cv = plugin->config.curves[ci];
986                 pen = cv->pen;  width = cv->width;  color = cv->color;
987         }
988         ci = plugin->new_curve(pen, width, color);
989         gui->curve_list->update(ci);
990         gui->point_list->update(-1);
991         gui->send_configure_change();
992         return 1;
993 }
994
995 SketcherDelCurve::SketcherDelCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
996  : BC_GenericButton(x, y, 64, C_("Del"))
997 {
998         this->gui = gui;
999         this->plugin = plugin;
1000 }
1001 SketcherDelCurve::~SketcherDelCurve()
1002 {
1003 }
1004 int SketcherDelCurve::handle_event()
1005 {
1006         SketcherConfig &config = plugin->config;
1007         int ci = config.cv_selected;
1008         SketcherCurves &curves = config.curves;
1009         if( ci >= 0 && ci < curves.size() ) {
1010                 curves.remove_object_number(ci--);
1011                 if( ci < 0 ) ci = 0;
1012                 if( !curves.size() )
1013                         ci = plugin->new_curve();
1014                 gui->curve_list->update(ci);
1015                 gui->point_list->update(-1);
1016                 gui->send_configure_change();
1017         }
1018         return 1;
1019 }
1020
1021 SketcherCurveUp::SketcherCurveUp(SketcherWindow *gui, int x, int y)
1022  : BC_GenericButton(x, y, _("Up"))
1023 {
1024         this->gui = gui;
1025 }
1026 SketcherCurveUp::~SketcherCurveUp()
1027 {
1028 }
1029
1030 int SketcherCurveUp::handle_event()
1031 {
1032         SketcherConfig &config = gui->plugin->config;
1033         int ci = config.cv_selected;
1034         SketcherCurves &curves = config.curves;
1035         if( ci > 0 && ci < curves.size() ) {
1036                 SketcherCurve *&cv0 = curves[ci];
1037                 SketcherCurve *&cv1 = curves[--ci];
1038                 SketcherCurve *t = cv0;  cv0 = cv1;  cv1 = t;
1039                 gui->curve_list->update(ci);
1040         }
1041         gui->send_configure_change();
1042         return 1;
1043 }
1044
1045 SketcherCurveDn::SketcherCurveDn(SketcherWindow *gui, int x, int y)
1046  : BC_GenericButton(x, y, _("Dn"))
1047 {
1048         this->gui = gui;
1049 }
1050 SketcherCurveDn::~SketcherCurveDn()
1051 {
1052 }
1053
1054 int SketcherCurveDn::handle_event()
1055 {
1056         SketcherConfig &config = gui->plugin->config;
1057         int ci = config.cv_selected;
1058         SketcherCurves &curves = config.curves;
1059         if( ci >= 0 && ci < curves.size()-1 ) {
1060                 SketcherCurve *&cv0 = curves[ci];
1061                 SketcherCurve *&cv1 = curves[++ci];
1062                 SketcherCurve *t = cv0;  cv0 = cv1;  cv1 = t;
1063                 gui->curve_list->update(ci);
1064         }
1065         gui->send_configure_change();
1066         return 1;
1067 }
1068
1069
1070 SketcherPointTypeItem::SketcherPointTypeItem(int arc)
1071  : BC_MenuItem(_(pt_type[arc]))
1072 {
1073         this->arc = arc;
1074 }
1075 int SketcherPointTypeItem::handle_event()
1076 {
1077         SketcherPointType *popup = (SketcherPointType*)get_popup_menu();
1078         popup->update(arc);
1079         SketcherWindow *gui = popup->gui;
1080         SketcherConfig &config = gui->plugin->config;
1081         SketcherCurves &curves = config.curves;
1082         int ci = config.cv_selected;
1083         if( ci < 0 || ci >= curves.size() )
1084                 return 1;
1085         SketcherCurve *cv = curves[ci];
1086         SketcherPoints &points = cv->points;
1087         int pi = config.pt_selected;
1088
1089         ArrayList<int> selected;
1090         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1091                 selected.append(v);
1092
1093         for( int i=selected.size(); --i>=0; ) {
1094                 int k = selected[i];
1095                 if( k < 0 || k >= points.size() ) continue;
1096                 SketcherPoint *pt = cv->points[k];
1097                 pt->arc = arc;
1098                 gui->point_list->set_point(k, PT_TY, _(pt_type[arc]));
1099         }
1100
1101         gui->point_list->update_list(pi);
1102         gui->send_configure_change();
1103         return 1;
1104 }
1105
1106 SketcherPointType::SketcherPointType(SketcherWindow *gui, int x, int y, int arc)
1107  : BC_PopupMenu(x,y,64,_(pt_type[arc]))
1108 {
1109         this->gui = gui;
1110         this->type = arc;
1111 }
1112 void SketcherPointType::create_objects()
1113 {
1114         for( int arc=0; arc<PT_SZ; ++arc )
1115                 add_item(new SketcherPointTypeItem(arc));
1116 }
1117 void SketcherPointType::update(int arc)
1118 {
1119         set_text(_(pt_type[this->type=arc]));
1120 }
1121
1122
1123 SketcherPointList::SketcherPointList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1124  : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
1125 {
1126         this->gui = gui;
1127         this->plugin = plugin;
1128         col_titles[PT_ID] = _("ID");    col_widths[PT_ID] = 50;
1129         col_titles[PT_TY] = _("Type");  col_widths[PT_TY] = 80;
1130         col_titles[PT_X] = _("X");      col_widths[PT_X] = 90;
1131         col_titles[PT_Y] = _("Y");      col_widths[PT_Y] = 90;
1132         set_selection_mode(LISTBOX_MULTIPLE);
1133 }
1134 SketcherPointList::~SketcherPointList()
1135 {
1136         clear();
1137 }
1138 void SketcherPointList::clear()
1139 {
1140         for( int i=PT_SZ; --i>=0; )
1141                 cols[i].remove_all_objects();
1142 }
1143
1144 int SketcherPointList::column_resize_event()
1145 {
1146         for( int i=PT_SZ; --i>=0; )
1147                 col_widths[i] = get_column_width(i);
1148         return 1;
1149 }
1150
1151 int SketcherPointList::handle_event()
1152 {
1153         int pi = get_selection_number(0, 0);
1154         if( get_selection_number(0, 1) >= 0 ) pi = -1;
1155         set_selected(pi);
1156         gui->send_configure_change();
1157         return 1;
1158 }
1159
1160 int SketcherPointList::selection_changed()
1161 {
1162         handle_event();
1163         return 1;
1164 }
1165
1166 void SketcherPointList::add_point(const char *id, const char *ty, const char *xp, const char *yp)
1167 {
1168         cols[PT_ID].append(new BC_ListBoxItem(id));
1169         cols[PT_TY].append(new BC_ListBoxItem(ty));
1170         cols[PT_X].append(new BC_ListBoxItem(xp));
1171         cols[PT_Y].append(new BC_ListBoxItem(yp));
1172 }
1173
1174 void SketcherPointList::set_point(int i, int c, int v)
1175 {
1176         char stxt[BCSTRLEN];
1177         sprintf(stxt,"%d",v);
1178         set_point(i,c,stxt);
1179 }
1180 void SketcherPointList::set_point(int i, int c, coord v)
1181 {
1182         char stxt[BCSTRLEN];
1183         sprintf(stxt,"%0.1f",v);
1184         set_point(i,c,stxt);
1185 }
1186 void SketcherPointList::set_point(int i, int c, const char *cp)
1187 {
1188         cols[c].get(i)->set_text(cp);
1189 }
1190
1191 void SketcherPointList::set_selected(int k)
1192 {
1193         SketcherPoint *pt = 0;
1194         int ci = plugin->config.cv_selected, pi = -1;
1195         if( ci >= 0 && ci < plugin->config.curves.size() ) {
1196                 SketcherCurve *cv = plugin->config.curves[ci];
1197                 pt = k >= 0 && k < cv->points.size() ? cv->points[pi=k] : 0;
1198         }
1199         gui->point_type->update(pt ? pt->arc : ARC_OFF);
1200         gui->point_x->update(pt ? pt->x : 0.f);
1201         gui->point_y->update(pt ? pt->y : 0.f);
1202         gui->point_id->update(pt ? pt->id : 0);
1203         plugin->config.pt_selected = pi;
1204         update_list(pi);
1205 }
1206 void SketcherPointList::update_list(int k)
1207 {
1208         int xpos = get_xposition(), ypos = get_yposition();
1209         if( k >= 0 ) update_selection(&cols[0], k);
1210         BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],PT_SZ, xpos,ypos,k);
1211         center_selection();
1212 }
1213 void SketcherPointList::update(int k)
1214 {
1215         clear();
1216         int ci = plugin->config.cv_selected, sz = 0;
1217         if( ci >= 0 && ci < plugin->config.curves.size() ) {
1218                 SketcherCurve *cv = plugin->config.curves[ci];
1219                 SketcherPoints &points = cv->points;
1220                 sz = points.size();
1221                 for( int i=0; i<sz; ++i ) {
1222                         SketcherPoint *pt = points[i];
1223                         char itxt[BCSTRLEN];  sprintf(itxt,"%d", pt->id);
1224                         char ttxt[BCSTRLEN];  sprintf(ttxt,"%s", _(pt_type[pt->arc]));
1225                         char xtxt[BCSTRLEN];  sprintf(xtxt,"%0.1f", pt->x);
1226                         char ytxt[BCSTRLEN];  sprintf(ytxt,"%0.1f", pt->y);
1227                         add_point(itxt, ttxt, xtxt, ytxt);
1228                 }
1229         }
1230         set_selected(k);
1231 }
1232
1233 void SketcherWindow::update_gui()
1234 {
1235         SketcherConfig &config = plugin->config;
1236         int ci = config.cv_selected;
1237         int pi = config.pt_selected;
1238         curve_list->update(ci);
1239         point_list->update(pi);
1240         SketcherCurve *cv = ci >= 0 ? config.curves[ci] : 0;
1241         curve_width->update(cv ? cv->width : 1);
1242         curve_pen->update(cv ? cv->pen : PEN_SQUARE);
1243         curve_color->set_color(cv ? cv->color : CV_COLOR);
1244         SketcherPoint *pt = pi >= 0 ? cv->points[pi] : 0;
1245         point_x->update(pt ? pt->x : 0);
1246         point_y->update(pt ? pt->y : 0);
1247         point_id->update(pt ? pt->id : 0);
1248         drag->update(plugin->config.drag);
1249 }
1250
1251
1252 SketcherPointUp::SketcherPointUp(SketcherWindow *gui, int x, int y)
1253  : BC_GenericButton(x, y, _("Up"))
1254 {
1255         this->gui = gui;
1256 }
1257 SketcherPointUp::~SketcherPointUp()
1258 {
1259 }
1260
1261 int SketcherPointUp::handle_event()
1262 {
1263         SketcherConfig &config = gui->plugin->config;
1264         int ci = config.cv_selected;
1265         if( ci < 0 || ci >= config.curves.size() )
1266                 return 1;
1267         SketcherCurve *cv = config.curves[ci];
1268         SketcherPoints &points = cv->points;
1269         if( points.size() < 2 )
1270                 return 1;
1271         int pi = config.pt_selected;
1272
1273         ArrayList<int> selected;
1274         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1275                 selected.append(v);
1276
1277         for( int i=0; i<selected.size(); ++i ) {
1278                 int k = selected[i];
1279                 if( k <= 0 ) continue;
1280                 if( k == pi ) --pi;
1281                 SketcherPoint *&pt0 = points[k];
1282                 SketcherPoint *&pt1 = points[--k];
1283                 SketcherPoint *t = pt0;  pt0 = pt1;  pt1 = t;
1284         }
1285         gui->point_list->update(pi);
1286         gui->send_configure_change();
1287         return 1;
1288 }
1289
1290 SketcherPointDn::SketcherPointDn(SketcherWindow *gui, int x, int y)
1291  : BC_GenericButton(x, y, _("Dn"))
1292 {
1293         this->gui = gui;
1294 }
1295 SketcherPointDn::~SketcherPointDn()
1296 {
1297 }
1298
1299 int SketcherPointDn::handle_event()
1300 {
1301         SketcherConfig &config = gui->plugin->config;
1302         int ci = config.cv_selected;
1303         if( ci < 0 || ci >= config.curves.size() )
1304                 return 1;
1305         SketcherCurve *cv = config.curves[ci];
1306         SketcherPoints &points = cv->points;
1307         int sz1 = points.size()-1;
1308         if( sz1 < 1 )
1309                 return 1;
1310         int pi = config.pt_selected;
1311
1312         ArrayList<int> selected;
1313         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1314                 selected.append(v);
1315
1316         for( int i=selected.size(); --i>=0; ) {
1317                 int k = selected[i];
1318                 if( k >= sz1 ) continue;
1319                 if( k == pi ) ++pi;
1320                 SketcherPoint *&pt0 = points[k];
1321                 SketcherPoint *&pt1 = points[++k];
1322                 SketcherPoint *t = pt0;  pt0 = pt1;  pt1 = t;
1323         }
1324         gui->point_list->update(pi);
1325         gui->send_configure_change();
1326         return 1;
1327 }
1328
1329 SketcherDrag::SketcherDrag(SketcherWindow *gui, int x, int y)
1330  : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
1331 {
1332         this->gui = gui;
1333 }
1334 int SketcherDrag::handle_event()
1335 {
1336         CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
1337         int value = get_value();
1338         if( value ) {
1339                 if( !gui->grab(cwindow_gui) ) {
1340                         update(value = 0);
1341                         flicker(10,50);
1342                 }
1343         }
1344         else
1345                 gui->ungrab(cwindow_gui);
1346         gui->plugin->config.drag = value;
1347         gui->send_configure_change();
1348         return 1;
1349 }
1350
1351 SketcherNewPoint::SketcherNewPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1352  : BC_GenericButton(x, y, 64, _("New"))
1353 {
1354         this->gui = gui;
1355         this->plugin = plugin;
1356 }
1357 SketcherNewPoint::~SketcherNewPoint()
1358 {
1359 }
1360 int SketcherNewPoint::handle_event()
1361 {
1362         int pi = plugin->config.pt_selected;
1363         int arc = gui->point_type->type;
1364         int k = plugin->new_point(pi+1, arc);
1365         gui->point_list->update(k);
1366         gui->send_configure_change();
1367         return 1;
1368 }
1369
1370 SketcherDelPoint::SketcherDelPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1371  : BC_GenericButton(x, y, 64, C_("Del"))
1372 {
1373         this->gui = gui;
1374         this->plugin = plugin;
1375 }
1376 SketcherDelPoint::~SketcherDelPoint()
1377 {
1378 }
1379 int SketcherDelPoint::handle_event()
1380 {
1381         SketcherConfig &config = gui->plugin->config;
1382         SketcherCurves &curves = config.curves;
1383         int ci = config.cv_selected;
1384         if( ci < 0 || ci >= curves.size() )
1385                 return 1;
1386         SketcherCurve *cv = curves[ci];
1387         SketcherPoints &points = cv->points;
1388         int pi = config.pt_selected;
1389
1390         ArrayList<int> selected;
1391         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1392                 selected.append(v);
1393
1394         for( int i=selected.size(); --i>=0; ) {
1395                 int k = selected[i];
1396                 if( k < 0 || k >= points.size() ) continue;
1397                 points.remove_object_number(k);
1398                 if( k == pi && --pi < 0 && points.size() > 0 ) pi = 0;
1399         }
1400         gui->point_list->update(pi);
1401         gui->send_configure_change();
1402         return 1;
1403 }
1404
1405 SketcherResetCurves::SketcherResetCurves(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1406  : BC_GenericButton(x, y, _("Reset"))
1407 {
1408         this->gui = gui;
1409         this->plugin = plugin;
1410 }
1411 SketcherResetCurves::~SketcherResetCurves()
1412 {
1413 }
1414 int SketcherResetCurves::handle_event()
1415 {
1416         SketcherConfig &config = plugin->config;
1417         config.curves.remove_all_objects();
1418         int ci = plugin->new_curve();
1419         gui->curve_list->update(ci);
1420         gui->point_list->update(-1);
1421         gui->send_configure_change();
1422         return 1;
1423 }
1424
1425 SketcherResetPoints::SketcherResetPoints(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1426  : BC_GenericButton(x, y, _("Reset"))
1427 {
1428         this->gui = gui;
1429         this->plugin = plugin;
1430 }
1431 SketcherResetPoints::~SketcherResetPoints()
1432 {
1433 }
1434 int SketcherResetPoints::handle_event()
1435 {
1436         SketcherConfig &config = gui->plugin->config;
1437         int ci = config.cv_selected;
1438         if( ci >= 0 && ci < config.curves.size() ) {
1439                 SketcherCurve *cv = config.curves[ci];
1440                 cv->points.remove_all_objects();
1441                 gui->point_list->update(-1);
1442                 gui->send_configure_change();
1443         }
1444         return 1;
1445 }
1446