sketcher tweaks + strengthen, yuv alpha draw_pixel fix
[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::send_configure_change()
579 {
580         pending_config = 0;
581         plugin->send_configure_change();
582 }
583
584
585 int SketcherWindow::grab_event(XEvent *event)
586 {
587         int ret = do_grab_event(event);
588         if( !grab_event_count() ) {
589                 if( grab_cursor_motion() )
590                         pending_config = 1;
591                 if( pending_config ) {
592                         last_x = output_x;  last_y = output_y;
593                         send_configure_change();
594                 }
595         }
596         return ret;
597 }
598
599 int SketcherWindow::do_grab_event(XEvent *event)
600 {
601         switch( event->type ) {
602         case ButtonPress: break;
603         case ButtonRelease: break;
604         case MotionNotify: break;
605         case KeyPress:
606                 if( keysym_lookup(event) > 0 ) {
607                         switch( get_keysym() ) {
608                         case XK_Delete:
609                                 pending_config = 1;
610                                 return (event->xkey.state & ShiftMask) ?
611                                         del_curve->handle_event() :
612                                         del_point->handle_event() ;
613                         }
614                 } // fall thru
615         default:
616                 return 0;
617         }
618
619         MWindow *mwindow = plugin->server->mwindow;
620         CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
621         CWindowCanvas *canvas = cwindow_gui->canvas;
622         int cx, cy;  cwindow_gui->get_relative_cursor(cx, cy);
623         cx -= mwindow->theme->ccanvas_x;
624         cy -= mwindow->theme->ccanvas_y;
625
626         if( !dragging ) {
627                 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ||
628                     cy < 0 || cy >= mwindow->theme->ccanvas_h )
629                         return 0;
630         }
631
632         switch( event->type ) {
633         case ButtonPress:
634                 if( dragging ) return 0;
635                 dragging = 1;
636                 break;
637         case ButtonRelease:
638                 dragging = 0;
639                 break;
640         case MotionNotify:
641                 if( dragging ) break;
642         default: // fall thru
643                 return 0;
644         }
645
646         cursor_x = cx, cursor_y = cy;
647         canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
648         position = plugin->get_source_position();
649         Track *track = plugin->server->plugin->track;
650         track_w = track->track_w;
651         track_h = track->track_h;
652         track->automation->get_projector(
653                 &projector_x, &projector_y, &projector_z,
654                 position, PLAY_FORWARD);
655         projector_x += mwindow->edl->session->output_w / 2;
656         projector_y += mwindow->edl->session->output_h / 2;
657         output_x = (cursor_x - projector_x) / projector_z + track_w / 2;
658         output_y = (cursor_y - projector_y) / projector_z + track_h / 2;
659         state = event->xmotion.state;
660
661         if( event->type == MotionNotify ) {
662                 memcpy(&motion_event, event, sizeof(motion_event));
663                 pending_motion = 1;
664                 return 1;
665         }
666         if( grab_cursor_motion() )
667                 pending_config = 1;
668
669         switch( event->type ) {
670         case ButtonPress:
671                 pending_config = grab_button_press(event);
672                 break;
673         case ButtonRelease:
674                 new_points = 0;
675                 break;
676         }
677
678         return 1;
679 }
680
681 int SketcherWindow::grab_button_press(XEvent *event)
682 {
683         SketcherConfig &config = plugin->config;
684         int ci = config.cv_selected;
685         if( ci < 0 || ci >= plugin->config.curves.size() )
686                 return 0;
687         SketcherCurves &curves = config.curves;
688         SketcherCurve *cv = curves[ci];
689         SketcherPoints &points = cv->points;
690         int pi = config.pt_selected;
691
692         int button_no = event->xbutton.button;
693         switch( button_no ) {
694         case LEFT_BUTTON: {
695                 if( (state & ShiftMask) ) { // create new point/string
696                         ++new_points;
697                         pi = plugin->new_point(cv,
698                                 !(state & ControlMask) ? ARC_LINE : ARC_FILL,
699                                 output_x, output_y, pi+1);
700                         point_list->update(pi);
701                         break;
702                 }
703                 SketcherPoint *pt = 0; // select point
704                 double dist = cv->nearest_point(pi, output_x,output_y);
705                 if( dist >= 0 ) {
706                         pt = points[pi];
707                         Track *track = plugin->server->plugin->track;
708                         int track_w = track->track_w, track_h = track->track_h;
709                         float px = (pt->x - track_w / 2) * projector_z + projector_x;
710                         float py = (pt->y - track_h / 2) * projector_z + projector_y;
711                         float pix = DISTANCE(px, py, cursor_x,cursor_y);
712                         if( (state & ControlMask) && pix >= HANDLE_W ) { pi = -1;  pt = 0; }
713                 }
714                 point_list->set_selected(pi);
715                 break; }
716         case RIGHT_BUTTON: {
717                 if( (state & ShiftMask) ) { // create new curve point
718                         ++new_points;
719                         pi = plugin->new_point(cv,
720                                 !(state & ControlMask) ? ARC_CURVE : ARC_OFF,
721                                 output_x, output_y, pi+1);
722                         point_list->update(pi);
723                         break;
724                 }
725                 if( (state & AltMask) ) { // create new curve
726                         ci = plugin->new_curve(cv->pen, cv->width, curve_color->color);
727                         curve_list->update(ci);
728                         point_list->update(-1);
729                         break;
730                 }
731                 SketcherPoint *pt = 0;
732                 double dist = config.nearest_point(ci, pi, output_x,output_y);
733                 if( dist >= 0 ) {
734                         pt = curves[ci]->points[pi];
735                         Track *track = plugin->server->plugin->track;
736                         int track_w = track->track_w, track_h = track->track_h;
737                         float px = (pt->x - track_w / 2) * projector_z + projector_x;
738                         float py = (pt->y - track_h / 2) * projector_z + projector_y;
739                         float pix = DISTANCE(px, py, cursor_x,cursor_y);
740                         if( (state & ControlMask) && pix >= HANDLE_W ) { ci = pi = -1;  pt = 0; }
741                 }
742                 if( pt ) {
743                         curve_list->update(ci);
744                         point_list->update(pi);
745                 }
746                 break; }
747         }
748         return 1;
749 }
750
751 int SketcherWindow::grab_cursor_motion()
752 {
753         if( !pending_motion )
754                 return 0;
755         pending_motion = 0;
756         SketcherConfig &config = plugin->config;
757         int ci = config.cv_selected;
758         if( ci < 0 || ci >= plugin->config.curves.size() )
759                 return 0;
760         SketcherCurves &curves = config.curves;
761         SketcherCurve *cv = curves[ci];
762         SketcherPoints &points = cv->points;
763         int pi = config.pt_selected;
764
765         if( (state & ShiftMask) ) {  // string of points
766                 if( (state & (Button1Mask|Button3Mask)) ) {
767                         SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
768                         if( pt ) {
769                                 float dist = DISTANCE(pt->x, pt->y, output_x, output_y);
770                                 if( dist < get_w()*0.1 ) return 0; // tolerance w/10
771                         }
772                         ++new_points;
773                         int arc = (state & Button1Mask) ? ARC_LINE : ARC_CURVE;
774                         pi = plugin->new_point(cv, arc, output_x, output_y, pi+1);
775                         point_list->update(pi);
776                 }
777                 return 1;
778         }
779         if( (state & Button1Mask) ) {
780                 if( (state & ControlMask) ) { // drag selected point
781                         SketcherPoint *pt = pi >= 0 && pi < points.size() ? points[pi] : 0;
782                         if( pt ) {
783                                 point_list->set_point(pi, PT_X, pt->x = output_x);
784                                 point_list->set_point(pi, PT_Y, pt->y = output_y);
785                                 point_list->update_list(pi);
786                                 point_x->update(pt->x);
787                                 point_y->update(pt->y);
788                         }
789                         return 1;
790                 }
791                 if( (state & AltMask) ) { // drag all curves
792                         int dx = round(output_x - last_x);
793                         int dy = round(output_y - last_y);
794                         for( int i=0; i<curves.size(); ++i ) {
795                                 SketcherCurve *crv = plugin->config.curves[i];
796                                 int pts = crv->points.size();
797                                 for( int k=0; k<pts; ++k ) {
798                                         SketcherPoint *pt = crv->points[k];
799                                         pt->x += dx;  pt->y += dy;
800                                 }
801                         }
802                         SketcherPoint *pt = pi >= 0 && pi < points.size() ?
803                                 points[pi] : 0;
804                         point_x->update(pt ? pt->x : 0.f);
805                         point_y->update(pt ? pt->y : 0.f);
806                         point_id->update(pt ? pt->id : 0);
807                         point_list->update(pi);
808                         return 1;
809                 }
810                 double dist = cv->nearest_point(pi, output_x,output_y);
811                 if( dist >= 0 )
812                         point_list->set_selected(pi);
813                 return 1;
814         }
815         if( (state & Button3Mask) ) {
816                 if( (state & (ControlMask | AltMask)) ) { // drag selected curve(s)
817                         int dx = round(output_x - last_x);
818                         int dy = round(output_y - last_y);
819                         for( int i=0; i<points.size(); ++i ) {
820                                 SketcherPoint *pt = points[i];
821                                 pt->x += dx;  pt->y += dy;
822                         }
823                         SketcherPoint *pt = pi >= 0 && pi < points.size() ?
824                                 points[pi] : 0;
825                         point_x->update(pt ? pt->x : 0.f);
826                         point_y->update(pt ? pt->y : 0.f);
827                         point_id->update(pt ? pt->id : 0);
828                         point_list->update(pi);
829                         return 1;
830                 }
831                 double dist = config.nearest_point(ci, pi, output_x,output_y);
832                 if( dist >= 0 ) {
833                         curve_list->update(ci);
834                         point_list->update(pi);
835                 }
836                 return 1;
837         }
838         return 0;
839 }
840
841 int SketcherWindow::keypress_event()
842 {
843         int key = get_keypress();
844         switch( key ) {
845         case DELETE: return shift_down() ?
846                         del_curve->handle_event() :
847                         del_point->handle_event() ;
848         }
849         return 0;
850 }
851
852 void SketcherWindow::start_color_thread(SketcherCurveColor *color_button)
853 {
854         unlock_window();
855         delete color_picker;
856         color_picker = new SketcherCurveColorPicker(this, color_button);
857         int color = CV_COLOR, ci = plugin->config.cv_selected;
858         if( ci >= 0 && ci < plugin->config.curves.size() ) {
859                 SketcherCurve *cv = plugin->config.curves[ci];
860                 color = cv->color;
861         }
862         color_picker->start(color);
863         lock_window("SketcherWindow::start_color_thread");
864 }
865
866
867 SketcherCurveList::SketcherCurveList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
868  : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
869 {
870         this->gui = gui;
871         this->plugin = plugin;
872         col_titles[CV_ID] = _("ID");      col_widths[CV_ID] = 64;
873         col_titles[CV_RAD] = _("width");  col_widths[CV_RAD] = 64;
874         col_titles[CV_PEN] = _("pen");    col_widths[CV_PEN] = 64;
875         col_titles[CV_CLR] = _("color");  col_widths[CV_CLR] = 80;
876         col_titles[CV_ALP] = _("alpha");  col_widths[CV_ALP] = 64;
877 }
878 SketcherCurveList::~SketcherCurveList()
879 {
880         clear();
881 }
882 void SketcherCurveList::clear()
883 {
884         for( int i=CV_SZ; --i>=0; )
885                 cols[i].remove_all_objects();
886 }
887
888 int SketcherCurveList::column_resize_event()
889 {
890         for( int i=CV_SZ; --i>=0; )
891                 col_widths[i] = get_column_width(i);
892         return 1;
893 }
894
895 int SketcherCurveList::handle_event()
896 {
897         int ci = get_selection_number(0, 0);
898         set_selected(ci);
899         gui->point_list->update(0);
900         gui->send_configure_change();
901         return 1;
902 }
903
904 int SketcherCurveList::selection_changed()
905 {
906         handle_event();
907         return 1;
908 }
909
910 void SketcherCurveList::set_selected(int k)
911 {
912         int ci = -1;
913         if( k >= 0 && k < plugin->config.curves.size() ) {
914                 SketcherCurve *cv = plugin->config.curves[k];
915                 gui->curve_width->update(cv->width);
916                 gui->curve_pen->update(cv->pen);
917                 gui->curve_color->update_gui(cv->color);
918                 ci = k;
919         }
920         plugin->config.cv_selected = ci;
921         update_list(ci);
922 }
923
924 void SketcherCurveList::update_list(int k)
925 {
926         int xpos = get_xposition(), ypos = get_yposition();
927         if( k >= 0 ) update_selection(&cols[0], k);
928         BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],CV_SZ, xpos,ypos,k);
929         center_selection();
930 }
931
932 void SketcherCurveList::update(int k)
933 {
934         clear();
935         SketcherCurves &curves = plugin->config.curves;
936         int sz = curves.size();
937         for( int i=0; i<sz; ++i ) {
938                 SketcherCurve *cv = curves[i];
939                 char itxt[BCSTRLEN];  sprintf(itxt,"%d", cv->id);
940                 char ptxt[BCSTRLEN];  sprintf(ptxt,"%s", cv_pen[cv->pen]);
941                 char rtxt[BCSTRLEN];  sprintf(rtxt,"%d", cv->width);
942                 int color = cv->color;
943                 int r = (color>>16)&0xff;
944                 int g = (color>> 8)&0xff;
945                 int b = (color>> 0)&0xff;
946                 int a = (~color>>24)&0xff;
947                 char ctxt[BCSTRLEN];  sprintf(ctxt,"#%02x%02x%02x", r, g, b);
948                 char atxt[BCSTRLEN];  sprintf(atxt,"%5.3f", a/255.);
949                 add_curve(itxt, ptxt, rtxt, ctxt, atxt);
950         }
951         set_selected(k);
952 }
953
954 void SketcherCurveList::add_curve(const char *id, const char *pen,
955                 const char *width, const char *color, const char *alpha)
956 {
957         cols[CV_ID].append(new BC_ListBoxItem(id));
958         cols[CV_RAD].append(new BC_ListBoxItem(width));
959         cols[CV_PEN].append(new BC_ListBoxItem(pen));
960         cols[CV_CLR].append(new BC_ListBoxItem(color));
961         cols[CV_ALP].append(new BC_ListBoxItem(alpha));
962 }
963
964 SketcherNewCurve::SketcherNewCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
965  : BC_GenericButton(x, y, 64, _("New"))
966 {
967         this->gui = gui;
968         this->plugin = plugin;
969 }
970 SketcherNewCurve::~SketcherNewCurve()
971 {
972 }
973 int SketcherNewCurve::handle_event()
974 {
975         int pen = gui->curve_pen->pen;
976         int color = gui->curve_color->color;
977         int width = gui->curve_width->width;
978         int ci = plugin->config.cv_selected;
979         if( ci >= 0 && ci < plugin->config.curves.size() ) {
980                 SketcherCurve *cv = plugin->config.curves[ci];
981                 pen = cv->pen;  width = cv->width;  color = cv->color;
982         }
983         ci = plugin->new_curve(pen, width, color);
984         gui->curve_list->update(ci);
985         gui->point_list->update(-1);
986         gui->send_configure_change();
987         return 1;
988 }
989
990 SketcherDelCurve::SketcherDelCurve(SketcherWindow *gui, Sketcher *plugin, int x, int y)
991  : BC_GenericButton(x, y, 64, C_("Del"))
992 {
993         this->gui = gui;
994         this->plugin = plugin;
995 }
996 SketcherDelCurve::~SketcherDelCurve()
997 {
998 }
999 int SketcherDelCurve::handle_event()
1000 {
1001         SketcherConfig &config = plugin->config;
1002         int ci = config.cv_selected;
1003         SketcherCurves &curves = config.curves;
1004         if( ci >= 0 && ci < curves.size() ) {
1005                 curves.remove_object_number(ci--);
1006                 if( ci < 0 ) ci = 0;
1007                 if( !curves.size() )
1008                         ci = plugin->new_curve();
1009                 gui->curve_list->update(ci);
1010                 gui->point_list->update(-1);
1011                 gui->send_configure_change();
1012         }
1013         return 1;
1014 }
1015
1016 SketcherCurveUp::SketcherCurveUp(SketcherWindow *gui, int x, int y)
1017  : BC_GenericButton(x, y, _("Up"))
1018 {
1019         this->gui = gui;
1020 }
1021 SketcherCurveUp::~SketcherCurveUp()
1022 {
1023 }
1024
1025 int SketcherCurveUp::handle_event()
1026 {
1027         SketcherConfig &config = gui->plugin->config;
1028         int ci = config.cv_selected;
1029         SketcherCurves &curves = config.curves;
1030         if( ci > 0 && ci < curves.size() ) {
1031                 SketcherCurve *&cv0 = curves[ci];
1032                 SketcherCurve *&cv1 = curves[--ci];
1033                 SketcherCurve *t = cv0;  cv0 = cv1;  cv1 = t;
1034                 gui->curve_list->update(ci);
1035         }
1036         gui->send_configure_change();
1037         return 1;
1038 }
1039
1040 SketcherCurveDn::SketcherCurveDn(SketcherWindow *gui, int x, int y)
1041  : BC_GenericButton(x, y, _("Dn"))
1042 {
1043         this->gui = gui;
1044 }
1045 SketcherCurveDn::~SketcherCurveDn()
1046 {
1047 }
1048
1049 int SketcherCurveDn::handle_event()
1050 {
1051         SketcherConfig &config = gui->plugin->config;
1052         int ci = config.cv_selected;
1053         SketcherCurves &curves = config.curves;
1054         if( ci >= 0 && ci < curves.size()-1 ) {
1055                 SketcherCurve *&cv0 = curves[ci];
1056                 SketcherCurve *&cv1 = curves[++ci];
1057                 SketcherCurve *t = cv0;  cv0 = cv1;  cv1 = t;
1058                 gui->curve_list->update(ci);
1059         }
1060         gui->send_configure_change();
1061         return 1;
1062 }
1063
1064
1065 SketcherPointTypeItem::SketcherPointTypeItem(int arc)
1066  : BC_MenuItem(_(pt_type[arc]))
1067 {
1068         this->arc = arc;
1069 }
1070 int SketcherPointTypeItem::handle_event()
1071 {
1072         SketcherPointType *popup = (SketcherPointType*)get_popup_menu();
1073         popup->update(arc);
1074         SketcherWindow *gui = popup->gui;
1075         SketcherConfig &config = gui->plugin->config;
1076         SketcherCurves &curves = config.curves;
1077         int ci = config.cv_selected;
1078         if( ci < 0 || ci >= curves.size() )
1079                 return 1;
1080         SketcherCurve *cv = curves[ci];
1081         SketcherPoints &points = cv->points;
1082         int pi = config.pt_selected;
1083
1084         ArrayList<int> selected;
1085         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1086                 selected.append(v);
1087
1088         for( int i=selected.size(); --i>=0; ) {
1089                 int k = selected[i];
1090                 if( k < 0 || k >= points.size() ) continue;
1091                 SketcherPoint *pt = cv->points[k];
1092                 pt->arc = arc;
1093                 gui->point_list->set_point(k, PT_TY, _(pt_type[arc]));
1094         }
1095
1096         gui->point_list->update_list(pi);
1097         gui->send_configure_change();
1098         return 1;
1099 }
1100
1101 SketcherPointType::SketcherPointType(SketcherWindow *gui, int x, int y, int arc)
1102  : BC_PopupMenu(x,y,64,_(pt_type[arc]))
1103 {
1104         this->gui = gui;
1105         this->type = arc;
1106 }
1107 void SketcherPointType::create_objects()
1108 {
1109         for( int arc=0; arc<PT_SZ; ++arc )
1110                 add_item(new SketcherPointTypeItem(arc));
1111 }
1112 void SketcherPointType::update(int arc)
1113 {
1114         set_text(_(pt_type[this->type=arc]));
1115 }
1116
1117
1118 SketcherPointList::SketcherPointList(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1119  : BC_ListBox(x, y, 360, 130, LISTBOX_TEXT)
1120 {
1121         this->gui = gui;
1122         this->plugin = plugin;
1123         col_titles[PT_ID] = _("ID");    col_widths[PT_ID] = 50;
1124         col_titles[PT_TY] = _("Type");  col_widths[PT_TY] = 80;
1125         col_titles[PT_X] = _("X");      col_widths[PT_X] = 90;
1126         col_titles[PT_Y] = _("Y");      col_widths[PT_Y] = 90;
1127         set_selection_mode(LISTBOX_MULTIPLE);
1128 }
1129 SketcherPointList::~SketcherPointList()
1130 {
1131         clear();
1132 }
1133 void SketcherPointList::clear()
1134 {
1135         for( int i=PT_SZ; --i>=0; )
1136                 cols[i].remove_all_objects();
1137 }
1138
1139 int SketcherPointList::column_resize_event()
1140 {
1141         for( int i=PT_SZ; --i>=0; )
1142                 col_widths[i] = get_column_width(i);
1143         return 1;
1144 }
1145
1146 int SketcherPointList::handle_event()
1147 {
1148         int pi = get_selection_number(0, 0);
1149         if( get_selection_number(0, 1) >= 0 ) pi = -1;
1150         set_selected(pi);
1151         gui->send_configure_change();
1152         return 1;
1153 }
1154
1155 int SketcherPointList::selection_changed()
1156 {
1157         handle_event();
1158         return 1;
1159 }
1160
1161 void SketcherPointList::add_point(const char *id, const char *ty, const char *xp, const char *yp)
1162 {
1163         cols[PT_ID].append(new BC_ListBoxItem(id));
1164         cols[PT_TY].append(new BC_ListBoxItem(ty));
1165         cols[PT_X].append(new BC_ListBoxItem(xp));
1166         cols[PT_Y].append(new BC_ListBoxItem(yp));
1167 }
1168
1169 void SketcherPointList::set_point(int i, int c, int v)
1170 {
1171         char stxt[BCSTRLEN];
1172         sprintf(stxt,"%d",v);
1173         set_point(i,c,stxt);
1174 }
1175 void SketcherPointList::set_point(int i, int c, coord v)
1176 {
1177         char stxt[BCSTRLEN];
1178         sprintf(stxt,"%0.1f",v);
1179         set_point(i,c,stxt);
1180 }
1181 void SketcherPointList::set_point(int i, int c, const char *cp)
1182 {
1183         cols[c].get(i)->set_text(cp);
1184 }
1185
1186 void SketcherPointList::set_selected(int k)
1187 {
1188         SketcherPoint *pt = 0;
1189         int ci = plugin->config.cv_selected, pi = -1;
1190         if( ci >= 0 && ci < plugin->config.curves.size() ) {
1191                 SketcherCurve *cv = plugin->config.curves[ci];
1192                 pt = k >= 0 && k < cv->points.size() ? cv->points[pi=k] : 0;
1193         }
1194         gui->point_type->update(pt ? pt->arc : ARC_OFF);
1195         gui->point_x->update(pt ? pt->x : 0.f);
1196         gui->point_y->update(pt ? pt->y : 0.f);
1197         gui->point_id->update(pt ? pt->id : 0);
1198         plugin->config.pt_selected = pi;
1199         update_list(pi);
1200 }
1201 void SketcherPointList::update_list(int k)
1202 {
1203         int xpos = get_xposition(), ypos = get_yposition();
1204         if( k >= 0 ) update_selection(&cols[0], k);
1205         BC_ListBox::update(&cols[0], &col_titles[0],&col_widths[0],PT_SZ, xpos,ypos,k);
1206         center_selection();
1207 }
1208 void SketcherPointList::update(int k)
1209 {
1210         clear();
1211         int ci = plugin->config.cv_selected, sz = 0;
1212         if( ci >= 0 && ci < plugin->config.curves.size() ) {
1213                 SketcherCurve *cv = plugin->config.curves[ci];
1214                 SketcherPoints &points = cv->points;
1215                 sz = points.size();
1216                 for( int i=0; i<sz; ++i ) {
1217                         SketcherPoint *pt = points[i];
1218                         char itxt[BCSTRLEN];  sprintf(itxt,"%d", pt->id);
1219                         char ttxt[BCSTRLEN];  sprintf(ttxt,"%s", _(pt_type[pt->arc]));
1220                         char xtxt[BCSTRLEN];  sprintf(xtxt,"%0.1f", pt->x);
1221                         char ytxt[BCSTRLEN];  sprintf(ytxt,"%0.1f", pt->y);
1222                         add_point(itxt, ttxt, xtxt, ytxt);
1223                 }
1224         }
1225         set_selected(k);
1226 }
1227
1228 void SketcherWindow::update_gui()
1229 {
1230         SketcherConfig &config = plugin->config;
1231         int ci = config.cv_selected;
1232         int pi = config.pt_selected;
1233         curve_list->update(ci);
1234         point_list->update(pi);
1235         SketcherCurve *cv = ci >= 0 ? config.curves[ci] : 0;
1236         curve_width->update(cv ? cv->width : 1);
1237         curve_pen->update(cv ? cv->pen : PEN_SQUARE);
1238         curve_color->set_color(cv ? cv->color : CV_COLOR);
1239         SketcherPoint *pt = pi >= 0 ? cv->points[pi] : 0;
1240         point_x->update(pt ? pt->x : 0);
1241         point_y->update(pt ? pt->y : 0);
1242         point_id->update(pt ? pt->id : 0);
1243         drag->update(plugin->config.drag);
1244 }
1245
1246
1247 SketcherPointUp::SketcherPointUp(SketcherWindow *gui, int x, int y)
1248  : BC_GenericButton(x, y, _("Up"))
1249 {
1250         this->gui = gui;
1251 }
1252 SketcherPointUp::~SketcherPointUp()
1253 {
1254 }
1255
1256 int SketcherPointUp::handle_event()
1257 {
1258         SketcherConfig &config = gui->plugin->config;
1259         int ci = config.cv_selected;
1260         if( ci < 0 || ci >= config.curves.size() )
1261                 return 1;
1262         SketcherCurve *cv = config.curves[ci];
1263         SketcherPoints &points = cv->points;
1264         if( points.size() < 2 )
1265                 return 1;
1266         int pi = config.pt_selected;
1267
1268         ArrayList<int> selected;
1269         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1270                 selected.append(v);
1271
1272         for( int i=0; i<selected.size(); ++i ) {
1273                 int k = selected[i];
1274                 if( k <= 0 ) continue;
1275                 if( k == pi ) --pi;
1276                 SketcherPoint *&pt0 = points[k];
1277                 SketcherPoint *&pt1 = points[--k];
1278                 SketcherPoint *t = pt0;  pt0 = pt1;  pt1 = t;
1279         }
1280         gui->point_list->update(pi);
1281         gui->send_configure_change();
1282         return 1;
1283 }
1284
1285 SketcherPointDn::SketcherPointDn(SketcherWindow *gui, int x, int y)
1286  : BC_GenericButton(x, y, _("Dn"))
1287 {
1288         this->gui = gui;
1289 }
1290 SketcherPointDn::~SketcherPointDn()
1291 {
1292 }
1293
1294 int SketcherPointDn::handle_event()
1295 {
1296         SketcherConfig &config = gui->plugin->config;
1297         int ci = config.cv_selected;
1298         if( ci < 0 || ci >= config.curves.size() )
1299                 return 1;
1300         SketcherCurve *cv = config.curves[ci];
1301         SketcherPoints &points = cv->points;
1302         int sz1 = points.size()-1;
1303         if( sz1 < 1 )
1304                 return 1;
1305         int pi = config.pt_selected;
1306
1307         ArrayList<int> selected;
1308         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1309                 selected.append(v);
1310
1311         for( int i=selected.size(); --i>=0; ) {
1312                 int k = selected[i];
1313                 if( k >= sz1 ) continue;
1314                 if( k == pi ) ++pi;
1315                 SketcherPoint *&pt0 = points[k];
1316                 SketcherPoint *&pt1 = points[++k];
1317                 SketcherPoint *t = pt0;  pt0 = pt1;  pt1 = t;
1318         }
1319         gui->point_list->update(pi);
1320         gui->send_configure_change();
1321         return 1;
1322 }
1323
1324 SketcherDrag::SketcherDrag(SketcherWindow *gui, int x, int y)
1325  : BC_CheckBox(x, y, gui->plugin->config.drag, _("Drag"))
1326 {
1327         this->gui = gui;
1328 }
1329 int SketcherDrag::handle_event()
1330 {
1331         CWindowGUI *cwindow_gui = gui->plugin->server->mwindow->cwindow->gui;
1332         int value = get_value();
1333         if( value ) {
1334                 if( !gui->grab(cwindow_gui) ) {
1335                         update(value = 0);
1336                         flicker(10,50);
1337                 }
1338         }
1339         else
1340                 gui->ungrab(cwindow_gui);
1341         gui->plugin->config.drag = value;
1342         gui->send_configure_change();
1343         return 1;
1344 }
1345
1346 SketcherNewPoint::SketcherNewPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1347  : BC_GenericButton(x, y, 64, _("New"))
1348 {
1349         this->gui = gui;
1350         this->plugin = plugin;
1351 }
1352 SketcherNewPoint::~SketcherNewPoint()
1353 {
1354 }
1355 int SketcherNewPoint::handle_event()
1356 {
1357         int pi = plugin->config.pt_selected;
1358         int arc = gui->point_type->type;
1359         int k = plugin->new_point(pi+1, arc);
1360         gui->point_list->update(k);
1361         gui->send_configure_change();
1362         return 1;
1363 }
1364
1365 SketcherDelPoint::SketcherDelPoint(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1366  : BC_GenericButton(x, y, 64, C_("Del"))
1367 {
1368         this->gui = gui;
1369         this->plugin = plugin;
1370 }
1371 SketcherDelPoint::~SketcherDelPoint()
1372 {
1373 }
1374 int SketcherDelPoint::handle_event()
1375 {
1376         SketcherConfig &config = gui->plugin->config;
1377         SketcherCurves &curves = config.curves;
1378         int ci = config.cv_selected;
1379         if( ci < 0 || ci >= curves.size() )
1380                 return 1;
1381         SketcherCurve *cv = curves[ci];
1382         SketcherPoints &points = cv->points;
1383         int pi = config.pt_selected;
1384
1385         ArrayList<int> selected;
1386         for( int v,i=0; (v=gui->point_list->get_selection_number(0, i))>=0; ++i )
1387                 selected.append(v);
1388
1389         for( int i=selected.size(); --i>=0; ) {
1390                 int k = selected[i];
1391                 if( k < 0 || k >= points.size() ) continue;
1392                 points.remove_object_number(k);
1393                 if( k == pi && --pi < 0 && points.size() > 0 ) pi = 0;
1394         }
1395         gui->point_list->update(pi);
1396         gui->send_configure_change();
1397         return 1;
1398 }
1399
1400 SketcherResetCurves::SketcherResetCurves(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1401  : BC_GenericButton(x, y, _("Reset"))
1402 {
1403         this->gui = gui;
1404         this->plugin = plugin;
1405 }
1406 SketcherResetCurves::~SketcherResetCurves()
1407 {
1408 }
1409 int SketcherResetCurves::handle_event()
1410 {
1411         SketcherConfig &config = plugin->config;
1412         config.curves.remove_all_objects();
1413         int ci = plugin->new_curve();
1414         gui->curve_list->update(ci);
1415         gui->point_list->update(-1);
1416         gui->send_configure_change();
1417         return 1;
1418 }
1419
1420 SketcherResetPoints::SketcherResetPoints(SketcherWindow *gui, Sketcher *plugin, int x, int y)
1421  : BC_GenericButton(x, y, _("Reset"))
1422 {
1423         this->gui = gui;
1424         this->plugin = plugin;
1425 }
1426 SketcherResetPoints::~SketcherResetPoints()
1427 {
1428 }
1429 int SketcherResetPoints::handle_event()
1430 {
1431         SketcherConfig &config = gui->plugin->config;
1432         int ci = config.cv_selected;
1433         if( ci >= 0 && ci < config.curves.size() ) {
1434                 SketcherCurve *cv = config.curves[ci];
1435                 cv->points.remove_all_objects();
1436                 gui->point_list->update(-1);
1437                 gui->send_configure_change();
1438         }
1439         return 1;
1440 }
1441