descratch again reworked + icons, add agingtv plugin params, add alias chkbox in...
[goodguy/history.git] / cinelerra-5.1 / plugins / titler / titlerwindow.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bcdialog.h"
24 #include "bcsignals.h"
25 #include "browsebutton.h"
26 #include "clip.h"
27 #include "cstrdup.h"
28 #include "automation.h"
29 #include "cwindow.h"
30 #include "cwindowgui.h"
31 #include "edl.h"
32 #include "edlsession.h"
33 #include "keys.h"
34 #include "language.h"
35 #include "mainerror.h"
36 #include "mwindow.h"
37 #include "mwindowgui.h"
38 #include "plugin.h"
39 #include "pluginserver.h"
40 #include "theme.h"
41 #include "track.h"
42 #include "titlerwindow.h"
43 #include "bcfontentry.h"
44
45 static const int timeunit_formats[] =
46 {
47         TIME_HMS,
48         TIME_SECONDS,
49         TIME_HMSF,
50         TIME_SAMPLES,
51         TIME_SAMPLES_HEX,
52         TIME_FRAMES,
53         TIME_FEET_FRAMES
54 };
55
56 TitleWindow::TitleWindow(TitleMain *client)
57  : PluginClientWindow(client,
58         client->config.window_w, client->config.window_h, 100, 100, 1)
59 {
60 //printf("TitleWindow::TitleWindow %d %d %d\n", __LINE__, client->config.window_w, client->config.window_h);
61         this->client = client;
62
63         font_title = 0;
64         font = 0;
65         font_tumbler = 0;
66         x_title = 0; title_x = 0;
67         y_title = 0; title_y = 0;
68         w_title = 0; title_w = 0;
69         h_title = 0; title_h = 0;
70         dropshadow_title = 0; dropshadow = 0;
71         outline_title = 0;    outline = 0;
72         stroker_title = 0;    stroker = 0;
73         style_title = 0;
74         italic = 0;
75         bold = 0;
76         drag = 0;
77         cur_popup = 0;
78         fonts_popup = 0;
79
80         color_x = color_y = 0;
81         outline_color_x = outline_color_y = 0;
82         drag_dx = drag_dy = dragging = 0;
83         cur_ibeam = -1;
84
85         size_title = 0;
86         size = 0;
87         size_tumbler = 0;
88         pitch_title = 0;
89         pitch = 0;
90         encoding_title = 0;
91         encoding = 0;
92         color_button = 0;
93         color_thread = 0;
94         outline_color_button = 0;
95         outline_color_thread = 0;
96         motion_title = 0;
97         motion = 0;
98         line_pitch = 0;
99         loop = 0;
100         fadein_title = 0;
101         fade_in = 0;
102         fadeout_title = 0;
103         fade_out = 0;
104         text_title = 0;
105         text = 0;
106         text_chars = 0;
107         text_bfrsz = 0;
108         justify_title = 0;
109         left = 0;  center = 0;  right = 0;
110         top = 0;   mid = 0;     bottom = 0;
111         speed_title = 0;
112         speed = 0;
113         timecode = 0;
114         timecode_format = 0;
115         background = 0;
116         background_path = 0;
117         loop_playback = 0;
118         pending_config = 0;
119 }
120
121 void TitleWindow::done_event(int result)
122 {
123         ungrab(client->server->mwindow->cwindow->gui);
124         color_thread->close_window();
125         outline_color_thread->close_window();
126         color_popup->close_window();
127         png_popup->close_window();
128 }
129
130 TitleWindow::~TitleWindow()
131 {
132         delete color_popup;
133         delete png_popup;
134         for( int i=0; i<fonts.size(); ++i )
135                 delete fonts[i]->get_icon();
136
137         sizes.remove_all_objects();
138         delete timecode_format;
139         delete color_thread;
140         delete title_x;
141         delete title_y;
142 }
143
144 void TitleWindow::create_objects()
145 {
146         int x = 10, y = 10;
147         int margin = client->get_theme()->widget_border;
148         char string[BCTEXTLEN];
149
150 #define COLOR_W 50
151 #define COLOR_H 30
152         client->build_previews(this);
153
154         sizes.append(new BC_ListBoxItem("8"));
155         sizes.append(new BC_ListBoxItem("9"));
156         sizes.append(new BC_ListBoxItem("10"));
157         sizes.append(new BC_ListBoxItem("11"));
158         sizes.append(new BC_ListBoxItem("12"));
159         sizes.append(new BC_ListBoxItem("13"));
160         sizes.append(new BC_ListBoxItem("14"));
161         sizes.append(new BC_ListBoxItem("16"));
162         sizes.append(new BC_ListBoxItem("18"));
163         sizes.append(new BC_ListBoxItem("20"));
164         sizes.append(new BC_ListBoxItem("22"));
165         sizes.append(new BC_ListBoxItem("24"));
166         sizes.append(new BC_ListBoxItem("26"));
167         sizes.append(new BC_ListBoxItem("28"));
168         sizes.append(new BC_ListBoxItem("32"));
169         sizes.append(new BC_ListBoxItem("36"));
170         sizes.append(new BC_ListBoxItem("40"));
171         sizes.append(new BC_ListBoxItem("48"));
172         sizes.append(new BC_ListBoxItem("56"));
173         sizes.append(new BC_ListBoxItem("64"));
174         sizes.append(new BC_ListBoxItem("72"));
175         sizes.append(new BC_ListBoxItem("100"));
176         sizes.append(new BC_ListBoxItem("128"));
177         sizes.append(new BC_ListBoxItem("256"));
178         sizes.append(new BC_ListBoxItem("512"));
179         sizes.append(new BC_ListBoxItem("1024"));
180
181         paths.append(new BC_ListBoxItem(TitleMain::motion_to_text(NO_MOTION)));
182         paths.append(new BC_ListBoxItem(TitleMain::motion_to_text(BOTTOM_TO_TOP)));
183         paths.append(new BC_ListBoxItem(TitleMain::motion_to_text(TOP_TO_BOTTOM)));
184         paths.append(new BC_ListBoxItem(TitleMain::motion_to_text(RIGHT_TO_LEFT)));
185         paths.append(new BC_ListBoxItem(TitleMain::motion_to_text(LEFT_TO_RIGHT)));
186
187
188
189 // Construct font list
190         ArrayList<BC_FontEntry*> *fontlist = get_resources()->fontlist;
191
192         for( int i=0; i<fontlist->size(); ++i ) {
193                 int exists = 0;
194                 for( int j=0; j<fonts.size(); ++j ) {
195                         if( !strcasecmp(fonts.get(j)->get_text(),
196                                 fontlist->get(i)->displayname) ) {
197                                 exists = 1;
198                                 break;
199                         }
200                 }
201
202                 BC_ListBoxItem *item = 0;
203                 if( !exists ) {
204                         fonts.append(item = new
205                                 BC_ListBoxItem(fontlist->get(i)->displayname));
206                         if( !strcmp(client->config.font, item->get_text()) )
207                                 item->set_selected(1);
208                         if( fontlist->values[i]->image ) {
209                                 VFrame *vframe = fontlist->get(i)->image;
210                                 BC_Pixmap *icon = new BC_Pixmap(this, vframe, PIXMAP_ALPHA);
211                                 item->set_icon(icon);
212                                 item->set_icon_vframe(vframe);
213                         }
214                 }
215         }
216
217 // Sort font list
218         int done = 0;
219         while(!done) {
220                 done = 1;
221                 for( int i=0; i<fonts.size()-1; ++i ) {
222                         if( strcmp(fonts.values[i]->get_text(),
223                                 fonts.values[i + 1]->get_text()) > 0 ) {
224                                 BC_ListBoxItem *temp = fonts.values[i + 1];
225                                 fonts.values[i + 1] = fonts.values[i];
226                                 fonts.values[i] = temp;
227                                 done = 0;
228                         }
229                 }
230         }
231
232         add_tool(font_title = new BC_Title(x, y, _("Font:")));
233         font = new TitleFont(client, this, x, y + font_title->get_h());
234         font->create_objects();
235         font->set_show_query(1);
236         x += font->get_w();
237         add_subwindow(font_tumbler = new TitleFontTumble(client, this, x, y+margin));
238         x += font_tumbler->get_w() + margin;
239
240         int x1 = x, y1 = y;
241         add_tool(size_title = new BC_Title(x1, y1+margin, _("Size:")));
242         sprintf(string, "%.2f", client->config.size);
243         x1 += size_title->get_w() + margin;
244         size = new TitleSize(client, this, x1, y1+margin, string);
245         size->create_objects();
246         int x2 = x1 + size->get_w(), y2 = y1 + size->get_h() + margin;
247         add_subwindow(size_tumbler = new TitleSizeTumble(client, this, x2, y1+margin));
248
249         add_tool(pitch_title = new BC_Title(x-5, y2+margin, _("Pitch:")));
250         pitch = new TitlePitch(client, this, x1, y2+margin, &client->config.line_pitch);
251         pitch->create_objects();
252
253         int x3 = x2 + size_tumbler->get_w() + 50;
254         int y3 = pitch->get_y() + pitch->get_h();
255
256         add_tool(style_title = new BC_Title(x=x3, y, _("Style:")));
257         add_tool(italic = new TitleItalic(client, this, x, y + 20));
258         int w1 = italic->get_w();
259         add_tool(bold = new TitleBold(client, this, x, y + 50));
260         if( bold->get_w() > w1 ) w1 = bold->get_w();
261
262         add_tool(drag = new TitleDrag(client, this, x, y + 80));
263         if( drag->get_w() > w1 ) w1 = drag->get_w();
264         if( client->config.drag ) {
265                 if( !grab(client->server->mwindow->cwindow->gui) )
266                         eprintf("drag enabled, but compositor already grabbed\n");
267         }
268
269         add_tool(alias = new TitleAlias(client, this, x, y+110));
270         if( alias->get_w() > w1 ) w1 = drag->get_w();
271
272         x += w1 + margin;
273         add_tool(justify_title = new BC_Title(x, y, _("Justify:")));
274         add_tool(left = new TitleLeft(client, this, x, y + 20));
275         w1 = left->get_w();
276         add_tool(center = new TitleCenter(client, this, x, y + 50));
277         if( center->get_w() > w1 ) w1 = center->get_w();
278         add_tool(right = new TitleRight(client, this, x, y + 80));
279         if( right->get_w() > w1 ) w1 = right->get_w();
280
281         x += w1 + margin;
282         add_tool(top = new TitleTop(client, this, x, y + 20));
283         add_tool(mid = new TitleMid(client, this, x, y + 50));
284         add_tool(bottom= new TitleBottom(client, this, x, y + 80));
285
286         x = margin;
287         y = y3+10;
288
289         w1 = BC_Title::calculate_w(this, _("X:"));
290         if( (x1 = BC_Title::calculate_w(this, _("Y:"))) > w1 ) w1 = x1;
291         if( (x1 = BC_Title::calculate_w(this, _("W:"))) > w1 ) w1 = x1;
292         if( (x1 = BC_Title::calculate_w(this, _("H:"))) > w1 ) w1 = x1;
293         add_tool(x_title = new BC_Title(x1=x, y, _("X:")));
294         x1 += w1;
295         title_x = new TitleX(client, this, x1, y);
296         title_x->create_objects();
297         x1 += title_x->get_w()+margin;
298         add_tool(y_title = new BC_Title(x1, y, _("Y:")));
299         x1 += w1;
300         title_y = new TitleY(client, this, x1, y);
301         title_y->create_objects();
302         x1 += title_y->get_w();
303         y1 = y + title_y->get_h();
304
305         add_tool(w_title = new BC_Title(x1=x, y1, _("W:")));
306         x1 += w1;
307         title_w = new TitleW(client, this, x1, y1);
308         title_w->create_objects();
309         x1 += title_w->get_w()+margin;
310         add_tool(h_title = new BC_Title(x1, y1, _("H:")));
311         x1 += w1;
312         title_h = new TitleH(client, this, x1, y1);
313         title_h->create_objects();
314         x1 += title_h->get_w();
315
316         x = x1+2*margin;
317         add_tool(motion_title = new BC_Title(x1=x, y, _("Motion:")));
318         x1 += motion_title->get_w()+margin;
319         motion = new TitleMotion(client, this, x1, y);
320         motion->create_objects();
321         add_tool(loop = new TitleLoop(client, this, x, y1));
322         x = margin;    y = y1 + loop->get_h()+20;
323
324         add_tool(dropshadow_title = new BC_Title(x, y, _("Drop shadow:")));
325         w1 = dropshadow_title->get_w();
326         dropshadow = new TitleDropShadow(client, this, x, y + 20);
327         dropshadow->create_objects();
328         if( dropshadow->get_w() > w1 ) w1 = dropshadow->get_w();
329         x += w1 + margin;
330
331         add_tool(fadein_title = new BC_Title(x, y, _("Fade in (sec):")));
332         w1 = fadein_title->get_w();
333         add_tool(fade_in = new TitleFade(client, this, &client->config.fade_in, x, y + 20));
334         if( fade_in->get_w() > w1 ) w1 = fade_in->get_w();
335         x += w1 + margin;
336
337         add_tool(fadeout_title = new BC_Title(x, y, _("Fade out (sec):")));
338         w1 = fadeout_title->get_w();
339         add_tool(fade_out = new TitleFade(client, this, &client->config.fade_out, x, y + 20));
340         if( fade_out->get_w() > w1 ) w1 = fade_out->get_w();
341         x += w1 + margin;
342
343         add_tool(speed_title = new BC_Title(x, y, _("Speed:")));
344         w1 = speed_title->get_w();
345         y += speed_title->get_h() + 5;  y1 = y;
346         speed = new TitleSpeed(client, this, x, y);
347         speed->create_objects();
348         if( speed->get_w() > w1 ) w1 = speed->get_w();
349         x += w1 + margin;
350         y2 = y + speed->get_h() + 10;
351
352         color_x = x3;  color_y = y = y1;
353         color_thread = new TitleColorThread(client, this, 0);
354         x1 = color_x + COLOR_W + 2*margin;
355         y1 = color_y + 5;
356         add_tool(color_button = new TitleColorButton(client, this, x1, y1));
357         y += COLOR_H + 5;
358         outline_color_x = x3;  outline_color_y = y;
359         outline_color_thread = new TitleColorThread(client, this, 1);
360         y1 = outline_color_y + 5;
361         add_tool(outline_color_button = new TitleOutlineColorButton(client, this, x1, y1));
362
363         x = 10;  y = y2;
364         add_tool(outline_title = new BC_Title(x, y, _("Outline:")));
365         y1 =  y + outline_title->get_h() + margin;
366         outline = new TitleOutline(client, this, x, y1);
367         outline->create_objects();
368         x += outline->get_w() + 2*margin;
369 #ifdef USE_STROKER
370         add_tool(stroker_title = new BC_Title(x, y, _("Stroker:")));
371         stroker = new TitleStroker(client, this, x, y1);
372         stroker->create_objects();
373         x += stroker->get_w() + margin;
374 #endif
375         y += outline_title->get_h() + margin;
376         add_tool(timecode = new TitleTimecode(client, this, x1=x, y));
377         x += timecode->get_w() + margin;
378         add_tool(timecode_format = new TitleTimecodeFormat(client, this, x, y,
379                 Units::print_time_format(client->config.timecode_format, string)));
380         timecode_format->create_objects();
381         y += timecode_format->get_h() + margin;
382
383         x = 10;
384         add_tool(background = new TitleBackground(client, this, x, y));
385         x += background->get_w() + margin;
386         add_tool(background_path = new TitleBackgroundPath(client, this, x, y));
387         x += background_path->get_w() + margin;
388         add_tool(background_browse = new BrowseButton(
389                 client->server->mwindow->theme, this, background_path,
390                 x, y, "", _("background media"), _("Select background media path")));
391         x += background_browse->get_w() + 3*margin;
392         add_tool(loop_playback = new TitleLoopPlayback(client, this, x, y));
393         y += loop_playback->get_h() + 10;
394
395         x = 10;
396         add_tool(text_title = new BC_Title(x, y, _("Text:")));
397         x += text_title->get_w() + 20;
398         int wid = BC_Title::calculate_w(this,"0")*10;
399         add_tool(text_chars = new TitleTextChars(x,y,wid));
400         x += text_chars->get_w() + 20;
401         add_tool(text_bfrsz = new TitleTextBfrSz(x,y,wid));
402
403         y += text_title->get_h() + margin;
404         x = margin;
405         text = new TitleText(client, this, x, y, get_w()-margin - x, get_h() - y - 10);
406         text->create_objects();
407
408         add_tool(cur_popup = new TitleCurPopup(client, this));
409         cur_popup->create_objects();
410         add_tool(fonts_popup = new TitleFontsPopup(client, this));
411         color_popup = new TitleColorPopup(client, this);
412         png_popup = new TitlePngPopup(client, this);
413
414         show_window(1);
415         update();
416 }
417
418 int TitleWindow::resize_event(int w, int h)
419 {
420         client->config.window_w = w;
421         client->config.window_h = h;
422
423         clear_box(0, 0, w, h);
424         font_title->reposition_window(font_title->get_x(), font_title->get_y());
425         font->reposition_window(font->get_x(), font->get_y());
426         font_tumbler->reposition_window(font_tumbler->get_x(), font_tumbler->get_y());
427         x_title->reposition_window(x_title->get_x(), x_title->get_y());
428         title_x->reposition_window(title_x->get_x(), title_x->get_y());
429         y_title->reposition_window(y_title->get_x(), y_title->get_y());
430         title_y->reposition_window(title_y->get_x(), title_y->get_y());
431         w_title->reposition_window(w_title->get_x(), w_title->get_y());
432         title_w->reposition_window(title_w->get_x(), title_w->get_y());
433         h_title->reposition_window(h_title->get_x(), h_title->get_y());
434         title_h->reposition_window(title_h->get_x(), title_h->get_y());
435         style_title->reposition_window(style_title->get_x(), style_title->get_y());
436         italic->reposition_window(italic->get_x(), italic->get_y());
437         bold->reposition_window(bold->get_x(), bold->get_y());
438         drag->reposition_window(drag->get_x(), drag->get_y());
439         alias->reposition_window(alias->get_x(), alias->get_y());
440         size_title->reposition_window(size_title->get_x(), size_title->get_y());
441         size->reposition_window(size->get_x(), size->get_y());
442         size_tumbler->reposition_window(size_tumbler->get_x(), size_tumbler->get_y());
443         pitch_title->reposition_window(pitch_title->get_x(), pitch_title->get_y());
444         pitch->reposition_window(pitch->get_x(), pitch->get_y());
445
446         color_button->reposition_window(color_button->get_x(), color_button->get_y());
447         outline_color_button->reposition_window(outline_color_button->get_x(), outline_color_button->get_y());
448         motion_title->reposition_window(motion_title->get_x(), motion_title->get_y());
449         motion->reposition_window(motion->get_x(), motion->get_y());
450         loop->reposition_window(loop->get_x(), loop->get_y());
451         dropshadow_title->reposition_window(dropshadow_title->get_x(), dropshadow_title->get_y());
452         dropshadow->reposition_window(dropshadow->get_x(), dropshadow->get_y());
453         fadein_title->reposition_window(fadein_title->get_x(), fadein_title->get_y());
454         fade_in->reposition_window(fade_in->get_x(), fade_in->get_y());
455         fadeout_title->reposition_window(fadeout_title->get_x(), fadeout_title->get_y());
456         fade_out->reposition_window(fade_out->get_x(), fade_out->get_y());
457         text_title->reposition_window(text_title->get_x(), text_title->get_y());
458         timecode->reposition_window(timecode->get_x(), timecode->get_y());
459         text->reposition_window(text->get_x(), text->get_y(), w - text->get_x() - 10,
460                 BC_TextBox::pixels_to_rows(this, MEDIUMFONT, h - text->get_y() - 10));
461         justify_title->reposition_window(justify_title->get_x(), justify_title->get_y());
462         left->reposition_window(left->get_x(), left->get_y());
463         center->reposition_window(center->get_x(), center->get_y());
464         right->reposition_window(right->get_x(), right->get_y());
465         top->reposition_window(top->get_x(), top->get_y());
466         mid->reposition_window(mid->get_x(), mid->get_y());
467         bottom->reposition_window(bottom->get_x(), bottom->get_y());
468         speed_title->reposition_window(speed_title->get_x(), speed_title->get_y());
469         speed->reposition_window(speed->get_x(), speed->get_y());
470         update_color();
471         flash();
472
473         return 1;
474 }
475
476 void TitleWindow::send_configure_change()
477 {
478         pending_config = 0;
479         client->send_configure_change();
480 }
481 int TitleWindow::check_configure_change(int ret)
482 {
483         if( pending_config && !grab_event_count() )
484                 send_configure_change();
485         return ret;
486 }
487
488 int TitleWindow::grab_event(XEvent *event)
489 {
490         switch( event->type ) {
491         case ButtonPress: break;
492         case ButtonRelease: break;
493         case MotionNotify: break;
494         default:
495                 return check_configure_change(0);
496         }
497
498         MWindow *mwindow = client->server->mwindow;
499         CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
500         CWindowCanvas *canvas = cwindow_gui->canvas;
501         int cx, cy;  cwindow_gui->get_relative_cursor(cx, cy);
502         cx -= mwindow->theme->ccanvas_x;
503         cy -= mwindow->theme->ccanvas_y;
504
505         if( !dragging ) {
506                 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ||
507                     cy < 0 || cy >= mwindow->theme->ccanvas_h )
508                         return check_configure_change(0);
509         }
510
511         switch( event->type ) {
512         case ButtonPress:
513                 if( !dragging ) break;
514                 return 1;
515         case ButtonRelease:
516                 if( !dragging ) return check_configure_change(0);
517                 dragging = 0;
518                 return 1;
519         case MotionNotify:
520                 if( !dragging ) return check_configure_change(0);
521                 break;
522         default:
523                 return check_configure_change(0);
524         }
525
526         float cursor_x = cx, cursor_y = cy;
527         canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
528         int64_t position = client->get_source_position();
529         float projector_x, projector_y, projector_z;
530         Track *track = client->server->plugin->track;
531         int track_w = track->track_w, track_h = track->track_h;
532         track->automation->get_projector(
533                 &projector_x, &projector_y, &projector_z,
534                 position, PLAY_FORWARD);
535         projector_x += mwindow->edl->session->output_w / 2;
536         projector_y += mwindow->edl->session->output_h / 2;
537         cursor_x = (cursor_x - projector_x) / projector_z + track_w / 2;
538         cursor_y = (cursor_y - projector_y) / projector_z + track_h / 2;
539         int title_x = client->config.title_x, title_y = client->config.title_y;
540         int title_w = client->config.title_w, title_h = client->config.title_h;
541         if( !title_w ) title_w = track_w;
542         if( !title_h ) title_h = track_h;
543         int r = MIN(track_w, track_h)/100 + 2;
544         int x0 = title_x, x1 = title_x+(title_w+1)/2, x2 = title_x+title_w;
545         int y0 = title_y, y1 = title_y+(title_h+1)/2, y2 = title_y+title_h;
546         int drag_dx = 0, drag_dy = 0;
547         if( !dragging ) {  // clockwise
548                      if( abs(drag_dx = cursor_x-x0) < r &&  // x0,y0
549                          abs(drag_dy = cursor_y-y0) < r ) dragging = 1;
550                 else if( abs(drag_dx = cursor_x-x1) < r &&  // x1,y0
551                          abs(drag_dy = cursor_y-y0) < r ) dragging = 2;
552                 else if( abs(drag_dx = cursor_x-x2) < r &&  // x2,y0
553                          abs(drag_dy = cursor_y-y0) < r ) dragging = 3;
554                 else if( abs(drag_dx = cursor_x-x2) < r &&  // x2,y1
555                          abs(drag_dy = cursor_y-y1) < r ) dragging = 4;
556                 else if( abs(drag_dx = cursor_x-x2) < r &&  // x2,y2
557                          abs(drag_dy = cursor_y-y2) < r ) dragging = 5;
558                 else if( abs(drag_dx = cursor_x-x1) < r &&  // x1,y2
559                          abs(drag_dy = cursor_y-y2) < r ) dragging = 6;
560                 else if( abs(drag_dx = cursor_x-x0) < r &&  // x0,y2
561                          abs(drag_dy = cursor_y-y2) < r ) dragging = 7;
562                 else if( abs(drag_dx = cursor_x-x0) < r &&  // x0,y1
563                          abs(drag_dy = cursor_y-y1) < r ) dragging = 8;
564                 else if( abs(drag_dx = cursor_x-x1) < r &&  // x1,y1
565                          abs(drag_dy = cursor_y-y1) < r ) dragging = 9;
566                         return 0;
567         }
568         switch( dragging ) {
569         case 1: { // x0,y0
570                 int cur_x = cursor_x - drag_dx, dx = cur_x - x0;
571                 int cur_y = cursor_y - drag_dy, dy = cur_y - y0;
572                 if( !dx && !dy ) return 1;
573                 int cur_w = title_w - dx;  if( cur_w < 1 ) cur_w = 1;
574                 int cur_h = title_h - dy;  if( cur_h < 1 ) cur_h = 1;
575                 this->title_x->update((int64_t)(client->config.title_x = cur_x));
576                 this->title_y->update((int64_t)(client->config.title_y = cur_y));
577                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
578                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
579                 break; }
580         case 2: { // x1,y0
581                 int cur_y = cursor_y - drag_dy, dy = cur_y - y0;
582                 if( !dy ) return 1;
583                 int cur_h = title_h - dy;  if( cur_h < 1 ) cur_h = 1;
584                 this->title_y->update((int64_t)(client->config.title_y = cur_y));
585                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
586                 break; }
587         case 3: { // x2,y0
588                 int cur_x = cursor_x - drag_dx, dx = cur_x - x2;
589                 int cur_y = cursor_y - drag_dy, dy = cur_y - y0;
590                 int cur_w = title_w + dx;  if( cur_w < 1 ) cur_w = 1;
591                 int cur_h = title_h - dy;  if( cur_h < 1 ) cur_h = 1;
592                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
593                 this->title_y->update((int64_t)(client->config.title_y = cur_y));
594                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
595                 break; }
596         case 4: { // x2,y1
597                 int cur_x = cursor_x - drag_dx, dx = cur_x - x2;
598                 if( !dx ) return 1;
599                 int cur_w = title_w + dx;  if( cur_w < 1 ) cur_w = 1;
600                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
601                 break; }
602         case 5: { // x2,y2
603                 int cur_x = cursor_x - drag_dx, dx = cur_x - x2;
604                 int cur_y = cursor_y - drag_dy, dy = cur_y - y2;
605                 int cur_w = title_w + dx;  if( cur_w < 1 ) cur_w = 1;
606                 int cur_h = title_h + dy;  if( cur_h < 1 ) cur_h = 1;
607                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
608                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
609                 break; }
610         case 6: { // x1,y2
611                 int cur_y = cursor_y - drag_dy, dy = cur_y - y2;
612                 if( client->config.title_h == cur_y ) return 1;
613                 int cur_h = title_h + dy;  if( cur_h < 1 ) cur_h = 1;
614                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
615                 break; }
616         case 7: { // x0,y2
617                 int cur_x = cursor_x - drag_dx, dx = cur_x - x0;
618                 int cur_y = cursor_y - drag_dy, dy = cur_y - y2;
619                 int cur_w = title_w - dx;  if( cur_w < 1 ) cur_w = 1;
620                 int cur_h = title_h + dy;  if( cur_h < 1 ) cur_h = 1;
621                 this->title_x->update((int64_t)(client->config.title_x = cur_x));
622                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
623                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
624                 break; }
625         case 8: { // x0,y1
626                 int cur_x = cursor_x - drag_dx, dx = cur_x - x0;
627                 if( !dx ) return 1;
628                 int cur_w = title_w - dx;  if( cur_w < 1 ) cur_w = 1;
629                 this->title_x->update((int64_t)(client->config.title_x = cur_x));
630                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
631                 break; }
632         case 9: { // x1,y1
633                 int cur_x = cursor_x - drag_dx, dx = cur_x - x1;
634                 int cur_y = cursor_y - drag_dy, dy = cur_y - y1;
635                 if( title_x == cur_x && title_y == cur_y ) return 1;
636                 this->title_x->update((int64_t)(client->config.title_x += dx));
637                 this->title_y->update((int64_t)(client->config.title_y += dy));
638                 }
639         }
640         if( !grab_event_count() )
641                 send_configure_change();
642         else
643                 pending_config = 1;
644         return 1;
645 }
646
647 void TitleWindow::previous_font()
648 {
649         int current_font = font->get_number();
650         current_font--;
651         if( current_font < 0 ) current_font = fonts.total - 1;
652
653         if( current_font < 0 || current_font >= fonts.total ) return;
654
655         for( int i=0; i<fonts.total; ++i ) {
656                 fonts.values[i]->set_selected(i == current_font);
657         }
658
659         font->update(fonts.values[current_font]->get_text());
660         strcpy(client->config.font, fonts.values[current_font]->get_text());
661         check_style(client->config.font,1);
662         send_configure_change();
663 }
664
665 void  TitleWindow::next_font()
666 {
667         int current_font = font->get_number();
668         current_font++;
669         if( current_font >= fonts.total ) current_font = 0;
670
671         if( current_font < 0 || current_font >= fonts.total ) return;
672
673         for( int i=0; i<fonts.total; ++i ) {
674                 fonts.values[i]->set_selected(i == current_font);
675         }
676
677         font->update(fonts.values[current_font]->get_text());
678         strcpy(client->config.font, fonts.values[current_font]->get_text());
679         check_style(client->config.font,1);
680         send_configure_change();
681 }
682
683 int TitleWindow::insert_ibeam(const char *txt, int ofs)
684 {
685         int ibeam = cur_ibeam;
686         int ilen = strlen(txt)+1;
687         wchar_t wtxt[ilen];
688         int len = BC_Resources::encode(client->config.encoding, BC_Resources::wide_encoding,
689                 (char*)txt,ilen, (char *)wtxt,ilen*sizeof(wtxt[0])) / sizeof(wchar_t);
690         client->insert_text(wtxt, ibeam);
691         while( len > 0 && !wtxt[len] ) --len;
692         int adv = len+1 + ofs;
693         if( (ibeam += adv) >= client->config.wlen)
694                 ibeam = client->config.wlen;
695         text->wset_selection(-1, -1, ibeam);
696         text->update(client->config.wtext);
697         send_configure_change();
698         return 1;
699 }
700
701 void TitleWindow::update_color()
702 {
703 //printf("TitleWindow::update_color %x\n", client->config.color);
704         set_color(client->config.color);
705         draw_box(color_x, color_y, COLOR_W, COLOR_H);
706         flash(color_x, color_y, COLOR_W, COLOR_H);
707         set_color(client->config.outline_color);
708         draw_box(outline_color_x, outline_color_y, COLOR_W, COLOR_H);
709         set_color(BLACK);
710         draw_rectangle(color_x, color_y, COLOR_W, COLOR_H);
711         draw_rectangle(outline_color_x, outline_color_y, COLOR_W, COLOR_H);
712         flash(outline_color_x, outline_color_y, COLOR_W, COLOR_H);
713 }
714
715 void TitleWindow::update_justification()
716 {
717         left->update(client->config.hjustification == JUSTIFY_LEFT);
718         center->update(client->config.hjustification == JUSTIFY_CENTER);
719         right->update(client->config.hjustification == JUSTIFY_RIGHT);
720         top->update(client->config.vjustification == JUSTIFY_TOP);
721         mid->update(client->config.vjustification == JUSTIFY_MID);
722         bottom->update(client->config.vjustification == JUSTIFY_BOTTOM);
723 }
724
725 void TitleWindow::update_stats()
726 {
727         text_chars->update(client->config.wlen);
728         int len = MESSAGESIZE - BCTEXTLEN - strlen(text->get_text()) - 1;
729         if( len < 0 ) len = 0;
730         text_bfrsz->update(len);
731 }
732
733 void TitleWindow::update()
734 {
735         title_x->update((int64_t)client->config.title_x);
736         title_y->update((int64_t)client->config.title_y);
737         title_w->update((int64_t)client->config.title_w);
738         title_h->update((int64_t)client->config.title_h);
739         italic->update(client->config.style & BC_FONT_ITALIC);
740         bold->update(client->config.style & BC_FONT_BOLD);
741         alias->update(client->config.style & FONT_ALIAS);
742         size->update(client->config.size);
743         motion->update(TitleMain::motion_to_text(client->config.motion_strategy));
744         loop->update(client->config.loop);
745         dropshadow->update((int64_t)client->config.dropshadow);
746         fade_in->update((float)client->config.fade_in);
747         fade_out->update((float)client->config.fade_out);
748         font->update(client->config.font);
749         check_style(client->config.font,0);
750         text->update(&client->config.wtext[0]);
751         speed->update(client->config.pixels_per_second);
752         outline->update((int64_t)client->config.outline_size);
753 #ifdef USE_STROKER
754         stroker->update((int64_t)client->config.stroke_width);
755 #endif
756         timecode->update(client->config.timecode);
757         timecode_format->update(client->config.timecode_format);
758         background->update(client->config.background);
759         background_path->update(client->config.background_path);
760         loop_playback->update((int64_t)client->config.loop_playback);
761
762         char string[BCTEXTLEN];
763         for( int i=0; i<lengthof(timeunit_formats); ++i ) {
764                 if( timeunit_formats[i] == client->config.timecode_format ) {
765                         timecode_format->set_text(
766                                 Units::print_time_format(timeunit_formats[i], string));
767                         break;
768                 }
769         }
770         update_justification();
771         update_stats();
772         update_color();
773 }
774
775
776 TitleFontTumble::TitleFontTumble(TitleMain *client, TitleWindow *window, int x, int y)
777  : BC_Tumbler(x, y)
778 {
779         this->client = client;
780         this->window = window;
781 }
782 int TitleFontTumble::handle_up_event()
783 {
784         window->previous_font();
785         return 1;
786 }
787
788 int TitleFontTumble::handle_down_event()
789 {
790         window->next_font();
791         return 1;
792 }
793
794
795
796 TitleSizeTumble::TitleSizeTumble(TitleMain *client, TitleWindow *window, int x, int y)
797  : BC_Tumbler(x, y)
798 {
799         this->client = client;
800         this->window = window;
801 }
802
803 int TitleSizeTumble::handle_up_event()
804 {
805         int current_index = -1;
806         int current_difference = -1;
807         for( int i=0; i<window->sizes.size(); ++i ) {
808                 int size = atoi(window->sizes.get(i)->get_text());
809                 if( current_index < 0 ||
810                         abs(size - client->config.size) < current_difference ) {
811                         current_index = i;
812                         current_difference = abs(size - client->config.size);
813                 }
814         }
815
816         current_index++;
817         if( current_index >= window->sizes.size() ) current_index = 0;
818
819
820         client->config.size = atoi(window->sizes.get(current_index)->get_text());
821         window->size->update(client->config.size);
822         window->send_configure_change();
823         return 1;
824 }
825
826 int TitleSizeTumble::handle_down_event()
827 {
828         int current_index = -1;
829         int current_difference = -1;
830         for( int i=0; i<window->sizes.size(); ++i ) {
831                 int size = atoi(window->sizes.get(i)->get_text());
832                 if( current_index < 0 ||
833                         abs(size - client->config.size) < current_difference ) {
834                         current_index = i;
835                         current_difference = abs(size - client->config.size);
836                 }
837         }
838
839         current_index--;
840         if( current_index < 0 ) current_index = window->sizes.size() - 1;
841
842
843         client->config.size = atoi(window->sizes.get(current_index)->get_text());
844         window->size->update(client->config.size);
845         window->send_configure_change();
846         return 1;
847 }
848
849 TitleAlias::TitleAlias(TitleMain *client, TitleWindow *window, int x, int y)
850  : BC_CheckBox(x, y, client->config.style & FONT_ALIAS, _("Alias"))
851 {
852         this->client = client;
853         this->window = window;
854 }
855
856 int TitleAlias::handle_event()
857 {
858         client->config.style =
859                 (client->config.style & ~FONT_ALIAS) |
860                         (get_value() ? FONT_ALIAS : 0);
861         window->send_configure_change();
862         return 1;
863 }
864
865 TitleBold::TitleBold(TitleMain *client, TitleWindow *window, int x, int y)
866  : BC_CheckBox(x, y, client->config.style & BC_FONT_BOLD, _("Bold"))
867 {
868         this->client = client;
869         this->window = window;
870 }
871
872 int TitleBold::handle_event()
873 {
874         client->config.style =
875                 (client->config.style & ~BC_FONT_BOLD) |
876                         (get_value() ? BC_FONT_BOLD : 0);
877         window->send_configure_change();
878         return 1;
879 }
880
881 TitleItalic::TitleItalic(TitleMain *client, TitleWindow *window, int x, int y)
882  : BC_CheckBox(x, y, client->config.style & BC_FONT_ITALIC, _("Italic"))
883 {
884         this->client = client;
885         this->window = window;
886 }
887 int TitleItalic::handle_event()
888 {
889         client->config.style =
890                 (client->config.style & ~BC_FONT_ITALIC) |
891                         (get_value() ? BC_FONT_ITALIC : 0);
892         window->send_configure_change();
893         return 1;
894 }
895
896
897
898 TitleSize::TitleSize(TitleMain *client, TitleWindow *window, int x, int y, char *text)
899  : BC_PopupTextBox(window, &window->sizes, text, x, y, 64, 300)
900 {
901         this->client = client;
902         this->window = window;
903 }
904 TitleSize::~TitleSize()
905 {
906 }
907 int TitleSize::handle_event()
908 {
909         client->config.size = atol(get_text());
910 //printf("TitleSize::handle_event 1 %s\n", get_text());
911         window->send_configure_change();
912         return 1;
913 }
914 void TitleSize::update(int size)
915 {
916         char string[BCTEXTLEN];
917         sprintf(string, "%d", size);
918         BC_PopupTextBox::update(string);
919 }
920
921 TitlePitch::
922 TitlePitch(TitleMain *client, TitleWindow *window, int x, int y, int *value)
923  : BC_TumbleTextBox(window, *value, 0, INT_MAX, x, y, 64)
924 {
925         this->client = client;
926         this->window = window;
927         this->value = value;
928 }
929
930 TitlePitch::
931 ~TitlePitch()
932 {
933 }
934
935 int TitlePitch::handle_event()
936 {
937         *value = atol(get_text());
938         window->send_configure_change();
939         return 1;
940 }
941
942 TitleColorButton::TitleColorButton(TitleMain *client, TitleWindow *window, int x, int y)
943  : BC_GenericButton(x, y, _("Text Color..."))
944 {
945         this->client = client;
946         this->window = window;
947 }
948 int TitleColorButton::handle_event()
949 {
950         window->color_thread->start_window(client->config.color,
951                 client->config.alpha);
952         return 1;
953 }
954 TitleOutlineColorButton::TitleOutlineColorButton(TitleMain *client, TitleWindow *window, int x, int y)
955  : BC_GenericButton(x, y, _("Outline color..."))
956 {
957         this->client = client;
958         this->window = window;
959 }
960 int TitleOutlineColorButton::handle_event()
961 {
962         window->outline_color_thread->start_window(client->config.outline_color,
963                 client->config.outline_alpha);
964         return 1;
965 }
966
967
968 TitleMotion::TitleMotion(TitleMain *client, TitleWindow *window, int x, int y)
969  : BC_PopupTextBox(window, &window->paths,
970                 client->motion_to_text(client->config.motion_strategy),
971                 x, y, 120, 100)
972 {
973         this->client = client;
974         this->window = window;
975 }
976 int TitleMotion::handle_event()
977 {
978         client->config.motion_strategy = client->text_to_motion(get_text());
979         window->send_configure_change();
980         return 1;
981 }
982
983 TitleLoop::TitleLoop(TitleMain *client, TitleWindow *window, int x, int y)
984  : BC_CheckBox(x, y, client->config.loop, _("Loop"))
985 {
986         this->client = client;
987         this->window = window;
988 }
989 int TitleLoop::handle_event()
990 {
991         client->config.loop = get_value();
992         window->send_configure_change();
993         return 1;
994 }
995 TitleTimecode::TitleTimecode(TitleMain *client, TitleWindow *window, int x, int y)
996  : BC_CheckBox(x, y, client->config.timecode, _("Stamp timecode"))
997 {
998         this->client = client;
999         this->window = window;
1000 }
1001 int TitleTimecode::handle_event()
1002 {
1003         client->config.timecode = get_value();
1004         client->send_configure_change();
1005         return 1;
1006 }
1007
1008 TitleTimecodeFormat::TitleTimecodeFormat(TitleMain *client, TitleWindow *window,
1009                 int x, int y, const char *text)
1010  : BC_PopupMenu(x, y, 100, text, 1)
1011 {
1012         this->client = client;
1013         this->window = window;
1014 }
1015
1016 int TitleTimecodeFormat::handle_event()
1017 {
1018         client->config.timecode_format = Units::text_to_format(get_text());
1019         window->send_configure_change();
1020         return 1;
1021 }
1022
1023 void TitleTimecodeFormat::create_objects()
1024 {
1025         char string[BCTEXTLEN];
1026         for( int i=0; i<lengthof(timeunit_formats); ++i ) {
1027                 add_item(new BC_MenuItem(
1028                         Units::print_time_format(timeunit_formats[i], string)));
1029         }
1030 }
1031
1032
1033 int TitleTimecodeFormat::update(int timecode_format)
1034 {
1035         char string[BCTEXTLEN];
1036         for( int i=0; i<lengthof(timeunit_formats); ++i ) {
1037                 if( timeunit_formats[i] == timecode_format ) {
1038                         set_text(Units::print_time_format(timeunit_formats[i], string));
1039                         break;
1040                 }
1041         }
1042         return 0;
1043 }
1044
1045 TitleFade::TitleFade(TitleMain *client, TitleWindow *window,
1046         double *value, int x, int y)
1047  : BC_TextBox(x, y, 80, 1, (float)*value)
1048 {
1049         this->client = client;
1050         this->window = window;
1051         this->value = value;
1052         set_precision(2);
1053 }
1054
1055 int TitleFade::handle_event()
1056 {
1057         *value = atof(get_text());
1058         window->send_configure_change();
1059         return 1;
1060 }
1061
1062 void TitleWindow::check_style(const char *font_name, int update)
1063 {
1064         BC_FontEntry *font_nrm = TitleMain::get_font(font_name, 0);
1065         BC_FontEntry *font_itl = TitleMain::get_font(font_name, BC_FONT_ITALIC);
1066         BC_FontEntry *font_bld = TitleMain::get_font(font_name, BC_FONT_BOLD);
1067         BC_FontEntry *font_bit = TitleMain::get_font(font_name, BC_FONT_ITALIC | BC_FONT_BOLD);
1068         int has_norm = font_nrm != 0 ? 1 : 0;
1069         int has_ital = font_itl != 0 || font_bit != 0 ? 1 : 0;
1070         int has_bold = font_bld != 0 || font_bit != 0 ? 1 : 0;
1071         if( bold->get_value() ) {
1072                 if( !has_bold ) bold->update(0);
1073         }
1074         else {
1075                 if( !has_norm && has_bold ) bold->update(1);
1076         }
1077         if( italic->get_value() ) {
1078                 if( !has_ital ) italic->update(0);
1079         }
1080         else {
1081                 if( !has_norm && has_ital ) italic->update(1);
1082         }
1083         if( has_norm && has_bold ) bold->enable();   else bold->disable();
1084         if( has_norm && has_ital ) italic->enable(); else italic->disable();
1085         if( update ) {
1086                 int style = stroker && atof(stroker->get_text()) ? BC_FONT_OUTLINE : 0;
1087                 if( bold->get_value() ) style |= BC_FONT_BOLD;
1088                 if( italic->get_value() ) style |= BC_FONT_ITALIC;
1089                 client->config.style = style;
1090         }
1091 }
1092
1093 TitleFont::TitleFont(TitleMain *client, TitleWindow *window, int x, int y)
1094  : BC_PopupTextBox(window, &window->fonts, client->config.font,
1095                 x, y, 240, 300, LISTBOX_ICON_LIST)
1096 {
1097         this->client = client;
1098         this->window = window;
1099 }
1100 int TitleFont::handle_event()
1101 {
1102         strcpy(client->config.font, get_text());
1103         window->check_style(client->config.font, 1);
1104         window->send_configure_change();
1105         return 1;
1106 }
1107
1108 TitleText::TitleText(TitleMain *client, TitleWindow *window,
1109         int x, int y, int w, int h)
1110  : BC_ScrollTextBox(window, x, y, w,
1111                 BC_TextBox::pixels_to_rows(window, MEDIUMFONT, h),
1112                 client->config.wtext, 8192)
1113 {
1114         this->client = client;
1115         this->window = window;
1116 //printf("TitleText::TitleText %s\n", client->config.text);
1117 }
1118
1119 int TitleText::button_press_event()
1120 {
1121         if( get_buttonpress() == 3 ) {
1122                 window->cur_ibeam = get_ibeam_letter();
1123                 window->cur_popup->activate_menu();
1124                 return 1;
1125         }
1126         return BC_ScrollTextBox::button_press_event();
1127 }
1128
1129 int TitleText::handle_event()
1130 {
1131         window->fonts_popup->deactivate();
1132         int text_len = strlen(get_text());
1133         int avail = MESSAGESIZE - BCTEXTLEN;
1134         if( text_len >= avail ) { // back off last utf8 char
1135                 char text[2*sizeof(client->config.wtext)];
1136                 strcpy(text, get_text());
1137                 text_len = avail;
1138                 while( text_len > 0 && (text[text_len-1] & 0xc0) == 0x80 )
1139                         text[--text_len] = 0;
1140                 if( text_len > 0 )
1141                         text[--text_len] = 0;
1142                 update(text);
1143         }
1144         int len =  sizeof(client->config.wtext) / sizeof(wchar_t);
1145         wcsncpy(client->config.wtext, get_wtext(), len);
1146         client->config.wtext[len-1] = 0;
1147         client->config.wlen = wcslen(client->config.wtext);
1148         window->update_stats();
1149         window->send_configure_change();
1150         return 1;
1151 }
1152 TitleTextChars::TitleTextChars(int x, int y, int w)
1153  : BC_Title(x, y, "", MEDIUMFONT, -1, 0, w)
1154 {
1155 }
1156 TitleTextChars::~TitleTextChars()
1157 {
1158 }
1159 int TitleTextChars::update(int n)
1160 {
1161         char text[BCSTRLEN];
1162         sprintf(text, _("chars: %d  "),n);
1163         return BC_Title::update(text, 0);
1164 }
1165
1166 TitleTextBfrSz::TitleTextBfrSz(int x, int y, int w)
1167  : BC_Title(x, y, "", MEDIUMFONT, -1, 0, w)
1168 {
1169 }
1170 TitleTextBfrSz::~TitleTextBfrSz()
1171 {
1172 }
1173 int TitleTextBfrSz::update(int n)
1174 {
1175         char text[BCSTRLEN];
1176         sprintf(text, _("bfrsz: %d  "),n);
1177         return BC_Title::update(text, 0);
1178 }
1179
1180
1181 TitleDropShadow::TitleDropShadow(TitleMain *client, TitleWindow *window, int x, int y)
1182  : BC_TumbleTextBox(window, client->config.dropshadow,
1183                 -1000, 1000, x, y, 70)
1184 {
1185         this->client = client;
1186         this->window = window;
1187 }
1188 int TitleDropShadow::handle_event()
1189 {
1190         client->config.dropshadow = atol(get_text());
1191         window->send_configure_change();
1192         return 1;
1193 }
1194
1195
1196 TitleOutline::TitleOutline(TitleMain *client, TitleWindow *window, int x, int y)
1197  : BC_TumbleTextBox(window, client->config.outline_size,
1198                 0.f, 1000.f, x, y, 70)
1199 {
1200         this->client = client;
1201         this->window = window;
1202         set_precision(1);
1203 }
1204 int TitleOutline::handle_event()
1205 {
1206         client->config.outline_size = atof(get_text());
1207         window->send_configure_change();
1208         return 1;
1209 }
1210
1211
1212 TitleStroker::TitleStroker(TitleMain *client, TitleWindow *window, int x, int y)
1213  : BC_TumbleTextBox(window, client->config.stroke_width,
1214                 0.f, 1000.f, x, y, 70)
1215 {
1216         this->client = client;
1217         this->window = window;
1218         set_precision(1);
1219 }
1220 int TitleStroker::handle_event()
1221 {
1222         client->config.stroke_width = atof(get_text());
1223         if( client->config.stroke_width )
1224                 client->config.style |= BC_FONT_OUTLINE;
1225         else
1226                 client->config.style &= ~BC_FONT_OUTLINE;
1227         window->send_configure_change();
1228         return 1;
1229 }
1230
1231
1232 TitleX::TitleX(TitleMain *client, TitleWindow *window, int x, int y)
1233  : BC_TumbleTextBox(window, client->config.title_x,
1234                 -32767.f, 32767.f, x, y, 50)
1235 {
1236         this->client = client;
1237         this->window = window;
1238         set_precision(1);
1239 }
1240 int TitleX::handle_event()
1241 {
1242         client->config.title_x = atof(get_text());
1243         window->send_configure_change();
1244         return 1;
1245 }
1246
1247 TitleY::TitleY(TitleMain *client, TitleWindow *window, int x, int y)
1248  : BC_TumbleTextBox(window, client->config.title_y,
1249                 -32767.f, 32767.f, x, y, 50)
1250 {
1251         this->client = client;
1252         this->window = window;
1253         set_precision(1);
1254 }
1255 int TitleY::handle_event()
1256 {
1257         client->config.title_y = atof(get_text());
1258         window->send_configure_change();
1259         return 1;
1260 }
1261
1262 TitleW::TitleW(TitleMain *client, TitleWindow *window, int x, int y)
1263  : BC_TumbleTextBox(window, client->config.title_w,
1264                 0, 32767, x, y, 50)
1265 {
1266         this->client = client;
1267         this->window = window;
1268 }
1269 int TitleW::handle_event()
1270 {
1271         client->config.title_w = atol(get_text());
1272         window->send_configure_change();
1273         return 1;
1274 }
1275
1276 TitleH::TitleH(TitleMain *client, TitleWindow *window, int x, int y)
1277  : BC_TumbleTextBox(window, client->config.title_h,
1278                 0, 32767, x, y, 50)
1279 {
1280         this->client = client;
1281         this->window = window;
1282 }
1283 int TitleH::handle_event()
1284 {
1285         client->config.title_h = atol(get_text());
1286         window->send_configure_change();
1287         return 1;
1288 }
1289
1290 TitleSpeed::TitleSpeed(TitleMain *client, TitleWindow *window, int x, int y)
1291  : BC_TumbleTextBox(window, client->config.pixels_per_second,
1292                 0.f, 1000.f, x, y, 100)
1293 {
1294         this->client = client;
1295         this->window = window;
1296         set_precision(2);
1297         set_increment(10);
1298 }
1299
1300
1301 int TitleSpeed::handle_event()
1302 {
1303         client->config.pixels_per_second = atof(get_text());
1304         window->send_configure_change();
1305         return 1;
1306 }
1307
1308
1309 TitleLeft::TitleLeft(TitleMain *client, TitleWindow *window, int x, int y)
1310  : BC_Radial(x, y, client->config.hjustification == JUSTIFY_LEFT, _("Left"))
1311 {
1312         this->client = client;
1313         this->window = window;
1314 }
1315 int TitleLeft::handle_event()
1316 {
1317         client->config.hjustification = JUSTIFY_LEFT;
1318         window->update_justification();
1319         window->send_configure_change();
1320         return 1;
1321 }
1322
1323 TitleCenter::TitleCenter(TitleMain *client, TitleWindow *window, int x, int y)
1324  : BC_Radial(x, y, client->config.hjustification == JUSTIFY_CENTER, _("Center"))
1325 {
1326         this->client = client;
1327         this->window = window;
1328 }
1329 int TitleCenter::handle_event()
1330 {
1331         client->config.hjustification = JUSTIFY_CENTER;
1332         window->update_justification();
1333         window->send_configure_change();
1334         return 1;
1335 }
1336
1337 TitleRight::TitleRight(TitleMain *client, TitleWindow *window, int x, int y)
1338  : BC_Radial(x, y, client->config.hjustification == JUSTIFY_RIGHT, _("Right"))
1339 {
1340         this->client = client;
1341         this->window = window;
1342 }
1343 int TitleRight::handle_event()
1344 {
1345         client->config.hjustification = JUSTIFY_RIGHT;
1346         window->update_justification();
1347         window->send_configure_change();
1348         return 1;
1349 }
1350
1351
1352
1353 TitleTop::TitleTop(TitleMain *client, TitleWindow *window, int x, int y)
1354  : BC_Radial(x, y, client->config.vjustification == JUSTIFY_TOP, _("Top"))
1355 {
1356         this->client = client;
1357         this->window = window;
1358 }
1359 int TitleTop::handle_event()
1360 {
1361         client->config.vjustification = JUSTIFY_TOP;
1362         window->update_justification();
1363         window->send_configure_change();
1364         return 1;
1365 }
1366
1367 TitleMid::TitleMid(TitleMain *client, TitleWindow *window, int x, int y)
1368  : BC_Radial(x, y, client->config.vjustification == JUSTIFY_MID, _("Mid"))
1369 {
1370         this->client = client;
1371         this->window = window;
1372 }
1373 int TitleMid::handle_event()
1374 {
1375         client->config.vjustification = JUSTIFY_MID;
1376         window->update_justification();
1377         window->send_configure_change();
1378         return 1;
1379 }
1380
1381 TitleBottom::TitleBottom(TitleMain *client, TitleWindow *window, int x, int y)
1382  : BC_Radial(x, y, client->config.vjustification == JUSTIFY_BOTTOM, _("Bottom"))
1383 {
1384         this->client = client;
1385         this->window = window;
1386 }
1387 int TitleBottom::handle_event()
1388 {
1389         client->config.vjustification = JUSTIFY_BOTTOM;
1390         window->update_justification();
1391         window->send_configure_change();
1392         return 1;
1393 }
1394
1395
1396
1397 TitleColorThread::TitleColorThread(TitleMain *client, TitleWindow *window, int is_outline)
1398  : ColorPicker(1, is_outline? _("Outline Color") : _("Text Color"))
1399 {
1400         this->client = client;
1401         this->window = window;
1402         this->is_outline = is_outline;
1403 }
1404
1405 int TitleColorThread::handle_new_color(int output, int alpha)
1406 {
1407         if( is_outline ) {
1408                 client->config.outline_color = output;
1409                 client->config.outline_alpha = alpha;
1410         }
1411         else {
1412                 client->config.color = output;
1413                 client->config.alpha = alpha;
1414         }
1415
1416         window->lock_window("TitleColorThread::handle_new_color");
1417         window->update_color();
1418         window->flush();
1419         window->unlock_window();
1420
1421         window->send_configure_change();
1422         return 1;
1423 }
1424
1425 TitleDrag::TitleDrag(TitleMain *client, TitleWindow *window, int x, int y)
1426  : BC_CheckBox(x, y, client->config.drag, _("Drag"))
1427 {
1428         this->client = client;
1429         this->window = window;
1430 }
1431
1432 int TitleDrag::handle_event()
1433 {
1434         int value = get_value();
1435         CWindowGUI *cwindow_gui = client->server->mwindow->cwindow->gui;
1436         if( value ) {
1437                 if( !window->grab(cwindow_gui) ) {
1438                         update(value = 0);
1439                         flicker(10,50);
1440                 }
1441         }
1442         else
1443                 window->ungrab(cwindow_gui);
1444         client->config.drag = value;
1445         window->send_configure_change();
1446         return 1;
1447 }
1448
1449 TitleBackground::TitleBackground(TitleMain *client, TitleWindow *window, int x, int y)
1450  : BC_CheckBox(x, y, client->config.background, _("Background:"))
1451 {
1452         this->client = client;
1453         this->window = window;
1454 }
1455
1456 int TitleBackground::handle_event()
1457 {
1458         client->config.background = get_value();
1459         window->send_configure_change();
1460         return 1;
1461 }
1462
1463 TitleBackgroundPath::TitleBackgroundPath(TitleMain *client, TitleWindow *window, int x, int y)
1464  : BC_TextBox(x, y, 240, 1, client->config.background_path)
1465 {
1466         this->client = client;
1467         this->window = window;
1468 }
1469
1470 int TitleBackgroundPath::handle_event()
1471 {
1472         strncpy(client->config.background_path, get_text(), sizeof(client->config.background_path));
1473         window->send_configure_change();
1474         return 1;
1475 }
1476
1477 TitleLoopPlayback::TitleLoopPlayback(TitleMain *client, TitleWindow *window, int x, int y)
1478  : BC_CheckBox(x, y, client->config.loop_playback, _("Loop playback"))
1479 {
1480         this->client = client;
1481         this->window = window;
1482 }
1483 int TitleLoopPlayback::handle_event()
1484 {
1485         client->config.loop_playback = get_value();
1486         window->send_configure_change();
1487         return 1;
1488 }
1489
1490
1491 TitleCurPopup::TitleCurPopup(TitleMain *client, TitleWindow *window)
1492  : BC_PopupMenu(0, 0, 0, "", 0)
1493 {
1494         this->client = client;
1495         this->window = window;
1496 }
1497 int TitleCurPopup::handle_event()
1498 {
1499         return 1;
1500 }
1501
1502 void TitleCurSubMenu::add_subitemx(int popup_type, va_list ap, const char *fmt)
1503 {
1504         char item[BCSTRLEN];
1505         vsnprintf(item, sizeof(item)-1, fmt, ap);
1506         item[sizeof(item)-1] = 0;
1507         add_submenuitem(new TitleCurSubMenuItem(this, item, popup_type));
1508 }
1509
1510 void TitleCurPopup::create_objects()
1511 {
1512         TitleCurItem *cur_item;
1513         TitleCurSubMenu *sub_menu;
1514         char *item;
1515         add_item(cur_item = new TitleCurItem(this, item = KW_NUDGE));
1516         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1517         sub_menu->add_subitem("%s dx,dy",item);
1518         sub_menu->add_subitem("/%s",item);
1519         add_item(cur_item = new TitleCurItem(this, item = KW_COLOR));
1520         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1521         sub_menu->add_subitem(POPUP_COLOR,"%s %s",item,_("#"));
1522         sub_menu->add_subitem("%s ",item);
1523         sub_menu->add_subitem("/%s",item);
1524         add_item(cur_item = new TitleCurItem(this, item = KW_ALPHA));
1525         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1526         sub_menu->add_subitem("%s ",item);
1527         sub_menu->add_subitem("%s 0.",item);
1528         sub_menu->add_subitem("%s .5",item);
1529         sub_menu->add_subitem("%s 1.",item);
1530         sub_menu->add_subitem("/%s",item);
1531         add_item(cur_item = new TitleCurItem(this, item = KW_FONT));
1532         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1533         sub_menu->add_subitem(POPUP_FONT,"%s %s",item, _("name"));
1534         sub_menu->add_subitem(POPUP_OFFSET, "%s ",item);
1535         sub_menu->add_subitem("/%s",item);
1536         add_item(cur_item = new TitleCurItem(this, item = KW_SIZE));
1537         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1538         sub_menu->add_subitem("%s +",item);
1539         sub_menu->add_subitem("%s -",item);
1540         sub_menu->add_subitem("%s ",item);
1541         sub_menu->add_subitem("/%s",item);
1542         add_item(cur_item = new TitleCurItem(this, item = KW_BOLD));
1543         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1544         sub_menu->add_subitem("%s 1",item);
1545         sub_menu->add_subitem("%s 0",item);
1546         sub_menu->add_subitem("/%s",item);
1547         add_item(cur_item = new TitleCurItem(this, item = KW_ITALIC));
1548         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1549         sub_menu->add_subitem("%s 1",item);
1550         sub_menu->add_subitem("%s 0",item);
1551         sub_menu->add_subitem("/%s",item);
1552         add_item(cur_item = new TitleCurItem(this, item = KW_CAPS));
1553         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1554         sub_menu->add_subitem("%s 1",item);
1555         sub_menu->add_subitem("%s 0",item);
1556         sub_menu->add_subitem("%s -1",item);
1557         sub_menu->add_subitem("/%s",item);
1558         add_item(cur_item = new TitleCurItem(this, item = KW_UL));
1559         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1560         sub_menu->add_subitem("%s 1",item);
1561         sub_menu->add_subitem("%s 0",item);
1562         sub_menu->add_subitem("/%s",item);
1563         add_item(cur_item = new TitleCurItem(this, item = KW_BLINK));
1564         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1565         sub_menu->add_subitem("%s 1",item);
1566         sub_menu->add_subitem("%s -1",item);
1567         sub_menu->add_subitem("%s ",item);
1568         sub_menu->add_subitem("%s 0",item);
1569         sub_menu->add_subitem("/%s",item);
1570         add_item(cur_item = new TitleCurItem(this, item = KW_FIXED));
1571         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1572         sub_menu->add_subitem("%s ",item);
1573         sub_menu->add_subitem("%s 20",item);
1574         sub_menu->add_subitem("%s 10",item);
1575         sub_menu->add_subitem("%s 0",item);
1576         sub_menu->add_subitem("/%s",item);
1577         add_item(cur_item = new TitleCurItem(this, item = KW_ALIAS));
1578         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1579         sub_menu->add_subitem("%s 1",item);
1580         sub_menu->add_subitem("%s 0",item);
1581         sub_menu->add_subitem("/%s",item);
1582         add_item(cur_item = new TitleCurItem(this, item = KW_SUP));
1583         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1584         sub_menu->add_subitem("%s 1",item);
1585         sub_menu->add_subitem("%s 0",item);
1586         sub_menu->add_subitem("%s -1",item);
1587         sub_menu->add_subitem("/%s",item);
1588         add_item(cur_item = new TitleCurItem(this, item = KW_PNG));
1589         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1590         sub_menu->add_subitem(POPUP_PNG,"%s %s", item, _("file"));
1591 }
1592
1593 TitleCurItem::TitleCurItem(TitleCurPopup *popup, const char *text)
1594  : BC_MenuItem(text)
1595 {
1596         this->popup = popup;
1597 }
1598 int TitleCurItem::handle_event()
1599 {
1600         return 1;
1601 }
1602
1603 TitleCurSubMenu::TitleCurSubMenu(TitleCurItem *cur_item)
1604 {
1605         this->cur_item = cur_item;
1606 }
1607 TitleCurSubMenu::~TitleCurSubMenu()
1608 {
1609 }
1610
1611 TitleCurSubMenuItem::TitleCurSubMenuItem(TitleCurSubMenu *submenu, const char *text, int popup_type)
1612  : BC_MenuItem(text)
1613 {
1614         this->submenu = submenu;
1615         this->popup_type = popup_type;
1616 }
1617 TitleCurSubMenuItem::~TitleCurSubMenuItem()
1618 {
1619 }
1620 int TitleCurSubMenuItem::handle_event()
1621 {
1622         TitleCurPopup *popup = submenu->cur_item->popup;
1623         TitleWindow *window = popup->window;
1624         const char *item_text = get_text();
1625         int ofs = *item_text == '/' ? 0 : -1;
1626         switch( popup_type ) {
1627         case POPUP_FONT: {
1628                 int px, py;
1629                 window->get_pop_cursor(px ,py);
1630                 window->fonts_popup->activate(px, py, 300,200);
1631                 return 1; }
1632         case POPUP_COLOR: {
1633                 window->color_popup->activate();
1634                 return 1; }
1635         case POPUP_PNG: {
1636                 window->png_popup->activate();
1637                 return 1; }
1638         case POPUP_OFFSET:
1639                 ofs = -1;
1640                 break;
1641         }
1642         char txt[BCSTRLEN];
1643         sprintf(txt, "<%s>", item_text);
1644         return window->insert_ibeam(txt, ofs);
1645 }
1646
1647 TitleFontsPopup::TitleFontsPopup(TitleMain *client, TitleWindow *window)
1648  : BC_ListBox(-1, -1, 1, 1, LISTBOX_ICON_LIST,
1649         &window->fonts, 0, 0, 1, 0, 1)
1650 {
1651         this->client = client;
1652         this->window = window;
1653         set_use_button(0);
1654         set_show_query(1);
1655 }
1656 TitleFontsPopup::~TitleFontsPopup()
1657 {
1658 }
1659 int TitleFontsPopup::keypress_event()
1660 {
1661         switch( get_keypress() ) {
1662         case ESC:
1663         case DELETE:
1664                 deactivate();
1665                 return 1;
1666         default:
1667                 break;
1668         }
1669         return BC_ListBox::keypress_event();
1670 }
1671
1672 int TitleFontsPopup::handle_event()
1673 {
1674         deactivate();
1675         BC_ListBoxItem *item = get_selection(0, 0);
1676         if( !item ) return 1;
1677         const char *item_text = item->get_text();
1678         char txt[BCTEXTLEN];  sprintf(txt, "<%s %s>", KW_FONT, item_text);
1679         return window->insert_ibeam(txt);
1680 }
1681
1682 TitleColorPopup::TitleColorPopup(TitleMain *client, TitleWindow *window)
1683  : ColorPicker(0, _("Color"))
1684 {
1685         this->client = client;
1686         this->window = window;
1687         this->color_value = client->config.color;
1688 }
1689 TitleColorPopup::~TitleColorPopup()
1690 {
1691 }
1692 int TitleColorPopup::handle_new_color(int output, int alpha)
1693 {
1694         color_value = output;
1695         return 1;
1696 }
1697 int TitleColorPopup::activate()
1698 {
1699         start_window(client->config.color, 255, 1);
1700         return 1;
1701 }
1702 void TitleColorPopup::handle_done_event(int result)
1703 {
1704         if( result ) return;
1705         char txt[BCSTRLEN];  sprintf(txt, "<%s #%06x>", KW_COLOR, color_value);
1706         window->insert_ibeam(txt);
1707 }
1708
1709 TitlePngPopup::TitlePngPopup(TitleMain *client, TitleWindow *window)
1710  : BC_DialogThread()
1711 {
1712         this->client = client;
1713         this->window = window;
1714 }
1715
1716 TitlePngPopup::~TitlePngPopup()
1717 {
1718         close_window();
1719 }
1720
1721 void TitlePngPopup::handle_done_event(int result)
1722 {
1723         if( result ) return;
1724         BrowseButtonWindow *gui = (BrowseButtonWindow *)get_gui();
1725         const char *path = gui->get_submitted_path();
1726         char txt[BCSTRLEN];  sprintf(txt, "<%s %s>", KW_PNG, path);
1727         window->insert_ibeam(txt);
1728 }
1729
1730 BC_Window *TitlePngPopup::new_gui()
1731 {
1732         MWindow *mwindow = client->server->mwindow;
1733         int x, y;  mwindow->gui->get_abs_cursor(x, y);
1734
1735         BC_Window *gui = new BrowseButtonWindow(mwindow->theme,
1736                 x-25, y-100, window, "", _("Png file"), _("Png path"), 0);
1737         gui->create_objects();
1738         return gui;
1739 }
1740
1741 int TitlePngPopup::activate()
1742 {
1743         BC_DialogThread::start();
1744         return 1;
1745 }
1746