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