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