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