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