crikey rework, KP4 fix, added Shift-KPx, valgrind cleanups, hex eye-dropper, resize...
[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         switch( event->type ) {
469         case ButtonPress: break;
470         case ButtonRelease: break;
471         case MotionNotify: break;
472         default: return 0;
473         }
474
475         MWindow *mwindow = client->server->mwindow;
476         CWindowGUI *cwindow_gui = mwindow->cwindow->gui;
477         CWindowCanvas *canvas = cwindow_gui->canvas;
478         int cx, cy;  cwindow_gui->get_relative_cursor_xy(cx, cy);
479         cx -= mwindow->theme->ccanvas_x;
480         cy -= mwindow->theme->ccanvas_y;
481
482         if( !dragging ) {
483                 if( cx < 0 || cx >= mwindow->theme->ccanvas_w ) return 0;
484                 if( cy < 0 || cy >= mwindow->theme->ccanvas_h ) return 0;
485         }
486
487         switch( event->type ) {
488         case ButtonPress:
489                 if( !dragging ) break;
490                 return 1;
491         case ButtonRelease:
492                 if( !dragging ) return 0;
493                 dragging = 0;
494                 return 1;
495         case MotionNotify:
496                 if( !dragging ) return 0;
497                 break;
498         default:
499                 return 0;
500         }
501
502         float cursor_x = cx, cursor_y = cy;
503         canvas->canvas_to_output(mwindow->edl, 0, cursor_x, cursor_y);
504         int64_t position = client->get_source_position();
505         float projector_x, projector_y, projector_z;
506         Track *track = client->server->plugin->track;
507         int track_w = track->track_w, track_h = track->track_h;
508         track->automation->get_projector(
509                 &projector_x, &projector_y, &projector_z,
510                 position, PLAY_FORWARD);
511         projector_x += mwindow->edl->session->output_w / 2;
512         projector_y += mwindow->edl->session->output_h / 2;
513         cursor_x = (cursor_x - projector_x) / projector_z + track_w / 2;
514         cursor_y = (cursor_y - projector_y) / projector_z + track_h / 2;
515         int title_x = client->config.title_x, title_y = client->config.title_y;
516         int title_w = client->config.title_w, title_h = client->config.title_h;
517         if( !title_w ) title_w = track_w;
518         if( !title_h ) title_h = track_h;
519         int r = MIN(track_w, track_h)/100 + 2;
520         int x0 = title_x, x1 = title_x+(title_w+1)/2, x2 = title_x+title_w;
521         int y0 = title_y, y1 = title_y+(title_h+1)/2, y2 = title_y+title_h;
522         int drag_dx = 0, drag_dy = 0;
523         if( !dragging ) {  // clockwise
524                      if( abs(drag_dx = cursor_x-x0) < r &&  // x0,y0
525                          abs(drag_dy = cursor_y-y0) < r ) dragging = 1;
526                 else if( abs(drag_dx = cursor_x-x1) < r &&  // x1,y0
527                          abs(drag_dy = cursor_y-y0) < r ) dragging = 2;
528                 else if( abs(drag_dx = cursor_x-x2) < r &&  // x2,y0
529                          abs(drag_dy = cursor_y-y0) < r ) dragging = 3;
530                 else if( abs(drag_dx = cursor_x-x2) < r &&  // x2,y1
531                          abs(drag_dy = cursor_y-y1) < r ) dragging = 4;
532                 else if( abs(drag_dx = cursor_x-x2) < r &&  // x2,y2
533                          abs(drag_dy = cursor_y-y2) < r ) dragging = 5;
534                 else if( abs(drag_dx = cursor_x-x1) < r &&  // x1,y2
535                          abs(drag_dy = cursor_y-y2) < r ) dragging = 6;
536                 else if( abs(drag_dx = cursor_x-x0) < r &&  // x0,y2
537                          abs(drag_dy = cursor_y-y2) < r ) dragging = 7;
538                 else if( abs(drag_dx = cursor_x-x0) < r &&  // x0,y1
539                          abs(drag_dy = cursor_y-y1) < r ) dragging = 8;
540                 else if( abs(drag_dx = cursor_x-x1) < r &&  // x1,y1
541                          abs(drag_dy = cursor_y-y1) < r ) dragging = 9;
542                         return 0;
543         }
544         switch( dragging ) {
545         case 1: { // x0,y0
546                 int cur_x = cursor_x - drag_dx, dx = cur_x - x0;
547                 int cur_y = cursor_y - drag_dy, dy = cur_y - y0;
548                 if( !dx && !dy ) return 1;
549                 int cur_w = title_w - dx;  if( cur_w < 1 ) cur_w = 1;
550                 int cur_h = title_h - dy;  if( cur_h < 1 ) cur_h = 1;
551                 this->title_x->update((int64_t)(client->config.title_x = cur_x));
552                 this->title_y->update((int64_t)(client->config.title_y = cur_y));
553                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
554                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
555                 break; }
556         case 2: { // x1,y0
557                 int cur_y = cursor_y - drag_dy, dy = cur_y - y0;
558                 if( !dy ) return 1;
559                 int cur_h = title_h - dy;  if( cur_h < 1 ) cur_h = 1;
560                 this->title_y->update((int64_t)(client->config.title_y = cur_y));
561                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
562                 break; }
563         case 3: { // x2,y0
564                 int cur_x = cursor_x - drag_dx, dx = cur_x - x2;
565                 int cur_y = cursor_y - drag_dy, dy = cur_y - y0;
566                 int cur_w = title_w + dx;  if( cur_w < 1 ) cur_w = 1;
567                 int cur_h = title_h - dy;  if( cur_h < 1 ) cur_h = 1;
568                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
569                 this->title_y->update((int64_t)(client->config.title_y = cur_y));
570                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
571                 break; }
572         case 4: { // x2,y1
573                 int cur_x = cursor_x - drag_dx, dx = cur_x - x2;
574                 if( !dx ) return 1;
575                 int cur_w = title_w + dx;  if( cur_w < 1 ) cur_w = 1;
576                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
577                 break; }
578         case 5: { // x2,y2
579                 int cur_x = cursor_x - drag_dx, dx = cur_x - x2;
580                 int cur_y = cursor_y - drag_dy, dy = cur_y - y2;
581                 int cur_w = title_w + dx;  if( cur_w < 1 ) cur_w = 1;
582                 int cur_h = title_h + dy;  if( cur_h < 1 ) cur_h = 1;
583                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
584                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
585                 break; }
586         case 6: { // x1,y2
587                 int cur_y = cursor_y - drag_dy, dy = cur_y - y2;
588                 if( client->config.title_h == cur_y ) return 1;
589                 int cur_h = title_h + dy;  if( cur_h < 1 ) cur_h = 1;
590                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
591                 break; }
592         case 7: { // x0,y2
593                 int cur_x = cursor_x - drag_dx, dx = cur_x - x0;
594                 int cur_y = cursor_y - drag_dy, dy = cur_y - y2;
595                 int cur_w = title_w - dx;  if( cur_w < 1 ) cur_w = 1;
596                 int cur_h = title_h + dy;  if( cur_h < 1 ) cur_h = 1;
597                 this->title_x->update((int64_t)(client->config.title_x = cur_x));
598                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
599                 this->title_h->update((int64_t)(client->config.title_h = cur_h));
600                 break; }
601         case 8: { // x0,y1
602                 int cur_x = cursor_x - drag_dx, dx = cur_x - x0;
603                 if( !dx ) return 1;
604                 int cur_w = title_w - dx;  if( cur_w < 1 ) cur_w = 1;
605                 this->title_x->update((int64_t)(client->config.title_x = cur_x));
606                 this->title_w->update((int64_t)(client->config.title_w = cur_w));
607                 break; }
608         case 9: { // x1,y1
609                 int cur_x = cursor_x - drag_dx, dx = cur_x - x1;
610                 int cur_y = cursor_y - drag_dy, dy = cur_y - y1;
611                 if( title_x == cur_x && title_y == cur_y ) return 1;
612                 this->title_x->update((int64_t)(client->config.title_x += dx));
613                 this->title_y->update((int64_t)(client->config.title_y += dy));
614                 }
615         }
616         client->send_configure_change();
617         return 1;
618 }
619
620 void TitleWindow::previous_font()
621 {
622         int current_font = font->get_number();
623         current_font--;
624         if( current_font < 0 ) current_font = fonts.total - 1;
625
626         if( current_font < 0 || current_font >= fonts.total ) return;
627
628         for( int i=0; i<fonts.total; ++i ) {
629                 fonts.values[i]->set_selected(i == current_font);
630         }
631
632         font->update(fonts.values[current_font]->get_text());
633         strcpy(client->config.font, fonts.values[current_font]->get_text());
634         check_style(client->config.font,1);
635         client->send_configure_change();
636 }
637
638 void  TitleWindow::next_font()
639 {
640         int current_font = font->get_number();
641         current_font++;
642         if( current_font >= fonts.total ) current_font = 0;
643
644         if( current_font < 0 || current_font >= fonts.total ) return;
645
646         for( int i=0; i<fonts.total; ++i ) {
647                 fonts.values[i]->set_selected(i == current_font);
648         }
649
650         font->update(fonts.values[current_font]->get_text());
651         strcpy(client->config.font, fonts.values[current_font]->get_text());
652         check_style(client->config.font,1);
653         client->send_configure_change();
654 }
655
656 int TitleWindow::insert_ibeam(const char *txt, int ofs)
657 {
658         int ibeam = cur_ibeam;
659         int ilen = strlen(txt)+1;
660         wchar_t wtxt[ilen];
661         int len = BC_Resources::encode(client->config.encoding, BC_Resources::wide_encoding,
662                 (char*)txt,ilen, (char *)wtxt,ilen*sizeof(wtxt[0])) / sizeof(wchar_t);
663         client->insert_text(wtxt, ibeam);
664         while( len > 0 && !wtxt[len] ) --len;
665         int adv = len+1 + ofs;
666         if( (ibeam += adv) >= client->config.wlen)
667                 ibeam = client->config.wlen;
668         text->wset_selection(-1, -1, ibeam);
669         text->update(client->config.wtext);
670         client->send_configure_change();
671         return 1;
672 }
673
674 void TitleWindow::update_color()
675 {
676 //printf("TitleWindow::update_color %x\n", client->config.color);
677         set_color(client->config.color);
678         draw_box(color_x, color_y, COLOR_W, COLOR_H);
679         flash(color_x, color_y, COLOR_W, COLOR_H);
680         set_color(client->config.outline_color);
681         draw_box(outline_color_x, outline_color_y, COLOR_W, COLOR_H);
682         set_color(BLACK);
683         draw_rectangle(color_x, color_y, COLOR_W, COLOR_H);
684         draw_rectangle(outline_color_x, outline_color_y, COLOR_W, COLOR_H);
685         flash(outline_color_x, outline_color_y, COLOR_W, COLOR_H);
686 }
687
688 void TitleWindow::update_justification()
689 {
690         left->update(client->config.hjustification == JUSTIFY_LEFT);
691         center->update(client->config.hjustification == JUSTIFY_CENTER);
692         right->update(client->config.hjustification == JUSTIFY_RIGHT);
693         top->update(client->config.vjustification == JUSTIFY_TOP);
694         mid->update(client->config.vjustification == JUSTIFY_MID);
695         bottom->update(client->config.vjustification == JUSTIFY_BOTTOM);
696 }
697
698 void TitleWindow::update_stats()
699 {
700         text_chars->update(client->config.wlen);
701         int len = MESSAGESIZE - BCTEXTLEN - strlen(text->get_text()) - 1;
702         if( len < 0 ) len = 0;
703         text_bfrsz->update(len);
704 }
705
706 void TitleWindow::update()
707 {
708         title_x->update((int64_t)client->config.title_x);
709         title_y->update((int64_t)client->config.title_y);
710         title_w->update((int64_t)client->config.title_w);
711         title_h->update((int64_t)client->config.title_h);
712         italic->update(client->config.style & BC_FONT_ITALIC);
713         bold->update(client->config.style & BC_FONT_BOLD);
714         size->update(client->config.size);
715         motion->update(TitleMain::motion_to_text(client->config.motion_strategy));
716         loop->update(client->config.loop);
717         dropshadow->update((int64_t)client->config.dropshadow);
718         fade_in->update((float)client->config.fade_in);
719         fade_out->update((float)client->config.fade_out);
720         font->update(client->config.font);
721         check_style(client->config.font,0);
722         text->update(&client->config.wtext[0]);
723         speed->update(client->config.pixels_per_second);
724         outline->update((int64_t)client->config.outline_size);
725 #ifdef USE_STROKER
726         stroker->update((int64_t)client->config.stroke_width);
727 #endif
728         timecode->update(client->config.timecode);
729         timecode_format->update(client->config.timecode_format);
730         background->update(client->config.background);
731         background_path->update(client->config.background_path);
732         loop_playback->update((int64_t)client->config.loop_playback);
733
734         char string[BCTEXTLEN];
735         for( int i=0; i<lengthof(timeunit_formats); ++i ) {
736                 if( timeunit_formats[i] == client->config.timecode_format ) {
737                         timecode_format->set_text(
738                                 Units::print_time_format(timeunit_formats[i], string));
739                         break;
740                 }
741         }
742         update_justification();
743         update_stats();
744         update_color();
745 }
746
747
748 TitleFontTumble::TitleFontTumble(TitleMain *client, TitleWindow *window, int x, int y)
749  : BC_Tumbler(x, y)
750 {
751         this->client = client;
752         this->window = window;
753 }
754 int TitleFontTumble::handle_up_event()
755 {
756         window->previous_font();
757         return 1;
758 }
759
760 int TitleFontTumble::handle_down_event()
761 {
762         window->next_font();
763         return 1;
764 }
765
766
767
768 TitleSizeTumble::TitleSizeTumble(TitleMain *client, TitleWindow *window, int x, int y)
769  : BC_Tumbler(x, y)
770 {
771         this->client = client;
772         this->window = window;
773 }
774
775 int TitleSizeTumble::handle_up_event()
776 {
777         int current_index = -1;
778         int current_difference = -1;
779         for( int i=0; i<window->sizes.size(); ++i ) {
780                 int size = atoi(window->sizes.get(i)->get_text());
781                 if( current_index < 0 ||
782                         abs(size - client->config.size) < current_difference ) {
783                         current_index = i;
784                         current_difference = abs(size - client->config.size);
785                 }
786         }
787
788         current_index++;
789         if( current_index >= window->sizes.size() ) current_index = 0;
790
791
792         client->config.size = atoi(window->sizes.get(current_index)->get_text());
793         window->size->update(client->config.size);
794         client->send_configure_change();
795         return 1;
796 }
797
798 int TitleSizeTumble::handle_down_event()
799 {
800         int current_index = -1;
801         int current_difference = -1;
802         for( int i=0; i<window->sizes.size(); ++i ) {
803                 int size = atoi(window->sizes.get(i)->get_text());
804                 if( current_index < 0 ||
805                         abs(size - client->config.size) < current_difference ) {
806                         current_index = i;
807                         current_difference = abs(size - client->config.size);
808                 }
809         }
810
811         current_index--;
812         if( current_index < 0 ) current_index = window->sizes.size() - 1;
813
814
815         client->config.size = atoi(window->sizes.get(current_index)->get_text());
816         window->size->update(client->config.size);
817         client->send_configure_change();
818         return 1;
819 }
820
821 TitleBold::TitleBold(TitleMain *client, TitleWindow *window, int x, int y)
822  : BC_CheckBox(x, y, client->config.style & BC_FONT_BOLD, _("Bold"))
823 {
824         this->client = client;
825         this->window = window;
826 }
827
828 int TitleBold::handle_event()
829 {
830         client->config.style =
831                 (client->config.style & ~BC_FONT_BOLD) |
832                         (get_value() ? BC_FONT_BOLD : 0);
833         client->send_configure_change();
834         return 1;
835 }
836
837 TitleItalic::TitleItalic(TitleMain *client, TitleWindow *window, int x, int y)
838  : BC_CheckBox(x, y, client->config.style & BC_FONT_ITALIC, _("Italic"))
839 {
840         this->client = client;
841         this->window = window;
842 }
843 int TitleItalic::handle_event()
844 {
845         client->config.style =
846                 (client->config.style & ~BC_FONT_ITALIC) |
847                         (get_value() ? BC_FONT_ITALIC : 0);
848         client->send_configure_change();
849         return 1;
850 }
851
852
853
854 TitleSize::TitleSize(TitleMain *client, TitleWindow *window, int x, int y, char *text)
855  : BC_PopupTextBox(window, &window->sizes, text, x, y, 64, 300)
856 {
857         this->client = client;
858         this->window = window;
859 }
860 TitleSize::~TitleSize()
861 {
862 }
863 int TitleSize::handle_event()
864 {
865         client->config.size = atol(get_text());
866 //printf("TitleSize::handle_event 1 %s\n", get_text());
867         client->send_configure_change();
868         return 1;
869 }
870 void TitleSize::update(int size)
871 {
872         char string[BCTEXTLEN];
873         sprintf(string, "%d", size);
874         BC_PopupTextBox::update(string);
875 }
876
877 TitlePitch::
878 TitlePitch(TitleMain *client, TitleWindow *window, int x, int y, int *value)
879  : BC_TumbleTextBox(window, *value, 0, INT_MAX, x, y, 64)
880 {
881         this->client = client;
882         this->window = window;
883         this->value = value;
884 }
885
886 TitlePitch::
887 ~TitlePitch()
888 {
889 }
890
891 int TitlePitch::handle_event()
892 {
893         *value = atol(get_text());
894         client->send_configure_change();
895         return 1;
896 }
897
898 TitleColorButton::TitleColorButton(TitleMain *client, TitleWindow *window, int x, int y)
899  : BC_GenericButton(x, y, _("Text Color..."))
900 {
901         this->client = client;
902         this->window = window;
903 }
904 int TitleColorButton::handle_event()
905 {
906         window->color_thread->start_window(client->config.color,
907                 client->config.alpha);
908         return 1;
909 }
910 TitleOutlineColorButton::TitleOutlineColorButton(TitleMain *client, TitleWindow *window, int x, int y)
911  : BC_GenericButton(x, y, _("Outline color..."))
912 {
913         this->client = client;
914         this->window = window;
915 }
916 int TitleOutlineColorButton::handle_event()
917 {
918         window->outline_color_thread->start_window(client->config.outline_color,
919                 client->config.outline_alpha);
920         return 1;
921 }
922
923
924 TitleMotion::TitleMotion(TitleMain *client, TitleWindow *window, int x, int y)
925  : BC_PopupTextBox(window, &window->paths,
926                 client->motion_to_text(client->config.motion_strategy),
927                 x, y, 120, 100)
928 {
929         this->client = client;
930         this->window = window;
931 }
932 int TitleMotion::handle_event()
933 {
934         client->config.motion_strategy = client->text_to_motion(get_text());
935         client->send_configure_change();
936         return 1;
937 }
938
939 TitleLoop::TitleLoop(TitleMain *client, int x, int y)
940  : BC_CheckBox(x, y, client->config.loop, _("Loop"))
941 {
942         this->client = client;
943 }
944 int TitleLoop::handle_event()
945 {
946         client->config.loop = get_value();
947         client->send_configure_change();
948         return 1;
949 }
950 TitleTimecode::TitleTimecode(TitleMain *client, int x, int y)
951  : BC_CheckBox(x, y, client->config.timecode, _("Stamp timecode"))
952 {
953         this->client = client;
954 }
955 int TitleTimecode::handle_event()
956 {
957         client->config.timecode = get_value();
958         client->send_configure_change();
959         return 1;
960 }
961
962 TitleTimecodeFormat::TitleTimecodeFormat(TitleMain *client, int x, int y, const char *text)
963  : BC_PopupMenu(x, y, 100, text, 1)
964 {
965         this->client = client;
966 }
967
968 int TitleTimecodeFormat::handle_event()
969 {
970         client->config.timecode_format = Units::text_to_format(get_text());
971         client->send_configure_change();
972         return 1;
973 }
974
975 void TitleTimecodeFormat::create_objects()
976 {
977         char string[BCTEXTLEN];
978         for( int i=0; i<lengthof(timeunit_formats); ++i ) {
979                 add_item(new BC_MenuItem(
980                         Units::print_time_format(timeunit_formats[i], string)));
981         }
982 }
983
984
985 int TitleTimecodeFormat::update(int timecode_format)
986 {
987         char string[BCTEXTLEN];
988         for( int i=0; i<lengthof(timeunit_formats); ++i ) {
989                 if( timeunit_formats[i] == timecode_format ) {
990                         set_text(Units::print_time_format(timeunit_formats[i], string));
991                         break;
992                 }
993         }
994         return 0;
995 }
996
997 TitleFade::TitleFade(TitleMain *client, TitleWindow *window,
998         double *value, int x, int y)
999  : BC_TextBox(x, y, 80, 1, (float)*value)
1000 {
1001         this->client = client;
1002         this->window = window;
1003         this->value = value;
1004         set_precision(2);
1005 }
1006
1007 int TitleFade::handle_event()
1008 {
1009         *value = atof(get_text());
1010         client->send_configure_change();
1011         return 1;
1012 }
1013
1014 void TitleWindow::check_style(const char *font_name, int update)
1015 {
1016         BC_FontEntry *font_nrm = TitleMain::get_font(font_name, 0);
1017         BC_FontEntry *font_itl = TitleMain::get_font(font_name, BC_FONT_ITALIC);
1018         BC_FontEntry *font_bld = TitleMain::get_font(font_name, BC_FONT_BOLD);
1019         BC_FontEntry *font_bit = TitleMain::get_font(font_name, BC_FONT_ITALIC | BC_FONT_BOLD);
1020         int has_norm = font_nrm != 0 ? 1 : 0;
1021         int has_ital = font_itl != 0 || font_bit != 0 ? 1 : 0;
1022         int has_bold = font_bld != 0 || font_bit != 0 ? 1 : 0;
1023         if( bold->get_value() ) {
1024                 if( !has_bold ) bold->update(0);
1025         }
1026         else {
1027                 if( !has_norm && has_bold ) bold->update(1);
1028         }
1029         if( italic->get_value() ) {
1030                 if( !has_ital ) italic->update(0);
1031         }
1032         else {
1033                 if( !has_norm && has_ital ) italic->update(1);
1034         }
1035         if( has_norm && has_bold ) bold->enable();   else bold->disable();
1036         if( has_norm && has_ital ) italic->enable(); else italic->disable();
1037         if( update ) {
1038                 int style = stroker && atof(stroker->get_text()) ? BC_FONT_OUTLINE : 0;
1039                 if( bold->get_value() ) style |= BC_FONT_BOLD;
1040                 if( italic->get_value() ) style |= BC_FONT_ITALIC;
1041                 client->config.style = style;
1042         }
1043 }
1044
1045 TitleFont::TitleFont(TitleMain *client, TitleWindow *window, int x, int y)
1046  : BC_PopupTextBox(window, &window->fonts, client->config.font,
1047                 x, y, 240, 300, LISTBOX_ICON_LIST)
1048 {
1049         this->client = client;
1050         this->window = window;
1051 }
1052 int TitleFont::handle_event()
1053 {
1054         strcpy(client->config.font, get_text());
1055         window->check_style(client->config.font, 1);
1056         client->send_configure_change();
1057         return 1;
1058 }
1059
1060 TitleText::TitleText(TitleMain *client, TitleWindow *window,
1061         int x, int y, int w, int h)
1062  : BC_ScrollTextBox(window, x, y, w,
1063                 BC_TextBox::pixels_to_rows(window, MEDIUMFONT, h),
1064                 client->config.wtext, 8192)
1065 {
1066         this->client = client;
1067         this->window = window;
1068 //printf("TitleText::TitleText %s\n", client->config.text);
1069 }
1070
1071 int TitleText::button_press_event()
1072 {
1073         if( get_buttonpress() == 3 ) {
1074                 window->cur_ibeam = get_ibeam_letter();
1075                 window->cur_popup->activate_menu();
1076                 return 1;
1077         }
1078         return BC_ScrollTextBox::button_press_event();
1079 }
1080
1081 int TitleText::handle_event()
1082 {
1083         window->fonts_popup->deactivate();
1084         int text_len = strlen(get_text());
1085         int avail = MESSAGESIZE - BCTEXTLEN;
1086         if( text_len >= avail ) { // back off last utf8 char
1087                 char text[2*sizeof(client->config.wtext)];
1088                 strcpy(text, get_text());
1089                 text_len = avail;
1090                 while( text_len > 0 && (text[text_len-1] & 0xc0) == 0x80 )
1091                         text[--text_len] = 0;
1092                 if( text_len > 0 )
1093                         text[--text_len] = 0;
1094                 update(text);
1095         }
1096         int len =  sizeof(client->config.wtext) / sizeof(wchar_t);
1097         wcsncpy(client->config.wtext, get_wtext(), len);
1098         client->config.wtext[len-1] = 0;
1099         client->config.wlen = wcslen(client->config.wtext);
1100         window->update_stats();
1101         client->send_configure_change();
1102         return 1;
1103 }
1104 TitleTextChars::TitleTextChars(int x, int y, int w)
1105  : BC_Title(x, y, "", MEDIUMFONT, -1, 0, w)
1106 {
1107 }
1108 TitleTextChars::~TitleTextChars()
1109 {
1110 }
1111 int TitleTextChars::update(int n)
1112 {
1113         char text[BCSTRLEN];
1114         sprintf(text, _("chars: %d  "),n);
1115         return BC_Title::update(text, 0);
1116 }
1117
1118 TitleTextBfrSz::TitleTextBfrSz(int x, int y, int w)
1119  : BC_Title(x, y, "", MEDIUMFONT, -1, 0, w)
1120 {
1121 }
1122 TitleTextBfrSz::~TitleTextBfrSz()
1123 {
1124 }
1125 int TitleTextBfrSz::update(int n)
1126 {
1127         char text[BCSTRLEN];
1128         sprintf(text, _("bfrsz: %d  "),n);
1129         return BC_Title::update(text, 0);
1130 }
1131
1132
1133 TitleDropShadow::TitleDropShadow(TitleMain *client, TitleWindow *window, int x, int y)
1134  : BC_TumbleTextBox(window, (int64_t)client->config.dropshadow,
1135         (int64_t)-1000, (int64_t)1000, x, y, 70)
1136 {
1137         this->client = client;
1138         this->window = window;
1139 }
1140 int TitleDropShadow::handle_event()
1141 {
1142         client->config.dropshadow = atol(get_text());
1143         client->send_configure_change();
1144         return 1;
1145 }
1146
1147
1148 TitleOutline::TitleOutline(TitleMain *client, TitleWindow *window, int x, int y)
1149  : BC_TumbleTextBox(window, (int64_t)client->config.outline_size,
1150         (int64_t)0, (int64_t)1000, x, y, 70)
1151 {
1152         this->client = client;
1153         this->window = window;
1154 }
1155 int TitleOutline::handle_event()
1156 {
1157         client->config.outline_size = atol(get_text());
1158         client->send_configure_change();
1159         return 1;
1160 }
1161
1162
1163 TitleStroker::TitleStroker(TitleMain *client, TitleWindow *window, int x, int y)
1164  : BC_TumbleTextBox(window, (int64_t)client->config.stroke_width,
1165         (int64_t)0, (int64_t)1000, x, y, 70)
1166 {
1167         this->client = client;
1168         this->window = window;
1169 }
1170 int TitleStroker::handle_event()
1171 {
1172         client->config.stroke_width = atof(get_text());
1173         if( client->config.stroke_width )
1174                 client->config.style |= BC_FONT_OUTLINE;
1175         else
1176                 client->config.style &= ~BC_FONT_OUTLINE;
1177         client->send_configure_change();
1178         return 1;
1179 }
1180
1181
1182 TitleX::TitleX(TitleMain *client, TitleWindow *window, int x, int y)
1183  : BC_TumbleTextBox(window, (int64_t)client->config.title_x,
1184         (int64_t)-32767, (int64_t)32767, x, y, 50)
1185 {
1186         this->client = client;
1187         this->window = window;
1188 }
1189 int TitleX::handle_event()
1190 {
1191         client->config.title_x = atol(get_text());
1192         client->send_configure_change();
1193         return 1;
1194 }
1195
1196 TitleY::TitleY(TitleMain *client, TitleWindow *window, int x, int y)
1197  : BC_TumbleTextBox(window, (int64_t)client->config.title_y,
1198         (int64_t)-32767, (int64_t)32767, x, y, 50)
1199 {
1200         this->client = client;
1201         this->window = window;
1202 }
1203 int TitleY::handle_event()
1204 {
1205         client->config.title_y = atol(get_text());
1206         client->send_configure_change();
1207         return 1;
1208 }
1209
1210 TitleW::TitleW(TitleMain *client, TitleWindow *window, int x, int y)
1211  : BC_TumbleTextBox(window, (int64_t)client->config.title_w,
1212         (int64_t)0, (int64_t)32767, x, y, 50)
1213 {
1214         this->client = client;
1215         this->window = window;
1216 }
1217 int TitleW::handle_event()
1218 {
1219         client->config.title_w = atol(get_text());
1220         client->send_configure_change();
1221         return 1;
1222 }
1223
1224 TitleH::TitleH(TitleMain *client, TitleWindow *window, int x, int y)
1225  : BC_TumbleTextBox(window, (int64_t)client->config.title_h,
1226         (int64_t)0, (int64_t)32767, x, y, 50)
1227 {
1228         this->client = client;
1229         this->window = window;
1230 }
1231 int TitleH::handle_event()
1232 {
1233         client->config.title_h = atol(get_text());
1234         client->send_configure_change();
1235         return 1;
1236 }
1237
1238 TitleSpeed::TitleSpeed(TitleMain *client, TitleWindow *window, int x, int y)
1239  : BC_TumbleTextBox(window, (float)client->config.pixels_per_second,
1240         (float)0, (float)1000, x, y, 100)
1241 {
1242         this->client = client;
1243         set_precision(2);
1244         set_increment(10);
1245 }
1246
1247
1248 int TitleSpeed::handle_event()
1249 {
1250         client->config.pixels_per_second = atof(get_text());
1251         client->send_configure_change();
1252         return 1;
1253 }
1254
1255
1256 TitleLeft::TitleLeft(TitleMain *client, TitleWindow *window, int x, int y)
1257  : BC_Radial(x, y, client->config.hjustification == JUSTIFY_LEFT, _("Left"))
1258 {
1259         this->client = client;
1260         this->window = window;
1261 }
1262 int TitleLeft::handle_event()
1263 {
1264         client->config.hjustification = JUSTIFY_LEFT;
1265         window->update_justification();
1266         client->send_configure_change();
1267         return 1;
1268 }
1269
1270 TitleCenter::TitleCenter(TitleMain *client, TitleWindow *window, int x, int y)
1271  : BC_Radial(x, y, client->config.hjustification == JUSTIFY_CENTER, _("Center"))
1272 {
1273         this->client = client;
1274         this->window = window;
1275 }
1276 int TitleCenter::handle_event()
1277 {
1278         client->config.hjustification = JUSTIFY_CENTER;
1279         window->update_justification();
1280         client->send_configure_change();
1281         return 1;
1282 }
1283
1284 TitleRight::TitleRight(TitleMain *client, TitleWindow *window, int x, int y)
1285  : BC_Radial(x, y, client->config.hjustification == JUSTIFY_RIGHT, _("Right"))
1286 {
1287         this->client = client;
1288         this->window = window;
1289 }
1290 int TitleRight::handle_event()
1291 {
1292         client->config.hjustification = JUSTIFY_RIGHT;
1293         window->update_justification();
1294         client->send_configure_change();
1295         return 1;
1296 }
1297
1298
1299
1300 TitleTop::TitleTop(TitleMain *client, TitleWindow *window, int x, int y)
1301  : BC_Radial(x, y, client->config.vjustification == JUSTIFY_TOP, _("Top"))
1302 {
1303         this->client = client;
1304         this->window = window;
1305 }
1306 int TitleTop::handle_event()
1307 {
1308         client->config.vjustification = JUSTIFY_TOP;
1309         window->update_justification();
1310         client->send_configure_change();
1311         return 1;
1312 }
1313
1314 TitleMid::TitleMid(TitleMain *client, TitleWindow *window, int x, int y)
1315  : BC_Radial(x, y, client->config.vjustification == JUSTIFY_MID, _("Mid"))
1316 {
1317         this->client = client;
1318         this->window = window;
1319 }
1320 int TitleMid::handle_event()
1321 {
1322         client->config.vjustification = JUSTIFY_MID;
1323         window->update_justification();
1324         client->send_configure_change();
1325         return 1;
1326 }
1327
1328 TitleBottom::TitleBottom(TitleMain *client, TitleWindow *window, int x, int y)
1329  : BC_Radial(x, y, client->config.vjustification == JUSTIFY_BOTTOM, _("Bottom"))
1330 {
1331         this->client = client;
1332         this->window = window;
1333 }
1334 int TitleBottom::handle_event()
1335 {
1336         client->config.vjustification = JUSTIFY_BOTTOM;
1337         window->update_justification();
1338         client->send_configure_change();
1339         return 1;
1340 }
1341
1342
1343
1344 TitleColorThread::TitleColorThread(TitleMain *client, TitleWindow *window, int is_outline)
1345  : ColorPicker(1, is_outline? _("Outline Color") : _("Text Color"))
1346 {
1347         this->client = client;
1348         this->window = window;
1349         this->is_outline = is_outline;
1350 }
1351
1352 int TitleColorThread::handle_new_color(int output, int alpha)
1353 {
1354         if( is_outline ) {
1355                 client->config.outline_color = output;
1356                 client->config.outline_alpha = alpha;
1357         }
1358         else {
1359                 client->config.color = output;
1360                 client->config.alpha = alpha;
1361         }
1362
1363         window->lock_window("TitleColorThread::handle_new_color");
1364         window->update_color();
1365         window->flush();
1366         window->unlock_window();
1367
1368         client->send_configure_change();
1369         return 1;
1370 }
1371
1372 TitleDrag::TitleDrag(TitleMain *client, TitleWindow *window, int x, int y)
1373  : BC_CheckBox(x, y, client->config.drag, _("Drag"))
1374 {
1375         this->client = client;
1376         this->window = window;
1377 }
1378
1379 int TitleDrag::handle_event()
1380 {
1381         int value = get_value();
1382         client->config.drag = value;
1383         if( value )
1384                 window->grab(client->server->mwindow->cwindow->gui);
1385         else
1386                 window->ungrab(client->server->mwindow->cwindow->gui);
1387         client->send_configure_change();
1388         return 1;
1389 }
1390
1391 TitleBackground::TitleBackground(TitleMain *client, TitleWindow *window, int x, int y)
1392  : BC_CheckBox(x, y, client->config.background, _("Background:"))
1393 {
1394         this->client = client;
1395         this->window = window;
1396 }
1397
1398 int TitleBackground::handle_event()
1399 {
1400         client->config.background = get_value();
1401         client->send_configure_change();
1402         return 1;
1403 }
1404
1405 TitleBackgroundPath::TitleBackgroundPath(TitleMain *client, TitleWindow *window, int x, int y)
1406  : BC_TextBox(x, y, 240, 1, client->config.background_path)
1407 {
1408         this->client = client;
1409         this->window = window;
1410 }
1411
1412 int TitleBackgroundPath::handle_event()
1413 {
1414         strncpy(client->config.background_path, get_text(), sizeof(client->config.background_path));
1415         client->send_configure_change();
1416         return 1;
1417 }
1418
1419 TitleLoopPlayback::TitleLoopPlayback(TitleMain *client, int x, int y)
1420  : BC_CheckBox(x, y, client->config.loop_playback, _("Loop playback"))
1421 {
1422         this->client = client;
1423 }
1424 int TitleLoopPlayback::handle_event()
1425 {
1426         client->config.loop_playback = get_value();
1427         client->send_configure_change();
1428         return 1;
1429 }
1430
1431
1432 TitleCurPopup::TitleCurPopup(TitleMain *client, TitleWindow *window)
1433  : BC_PopupMenu(0, 0, 0, "", 0)
1434 {
1435         this->client = client;
1436         this->window = window;
1437 }
1438 int TitleCurPopup::handle_event()
1439 {
1440         return 1;
1441 }
1442
1443 void TitleCurSubMenu::add_subitemx(int popup_type, va_list ap, const char *fmt)
1444 {
1445         char item[BCSTRLEN];
1446         vsnprintf(item, sizeof(item)-1, fmt, ap);
1447         item[sizeof(item)-1] = 0;
1448         add_submenuitem(new TitleCurSubMenuItem(this, item, popup_type));
1449 }
1450
1451 void TitleCurPopup::create_objects()
1452 {
1453         TitleCurItem *cur_item;
1454         TitleCurSubMenu *sub_menu;
1455         char *item;
1456         add_item(cur_item = new TitleCurItem(this, item = KW_NUDGE));
1457         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1458         sub_menu->add_subitem("%s dx,dy",item);
1459         sub_menu->add_subitem("/%s",item);
1460         add_item(cur_item = new TitleCurItem(this, item = KW_COLOR));
1461         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1462         sub_menu->add_subitem(POPUP_COLOR,"%s %s",item,_("#"));
1463         sub_menu->add_subitem("%s ",item);
1464         sub_menu->add_subitem("/%s",item);
1465         add_item(cur_item = new TitleCurItem(this, item = KW_ALPHA));
1466         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1467         sub_menu->add_subitem("%s ",item);
1468         sub_menu->add_subitem("%s 0.",item);
1469         sub_menu->add_subitem("%s .5",item);
1470         sub_menu->add_subitem("%s 1.",item);
1471         sub_menu->add_subitem("/%s",item);
1472         add_item(cur_item = new TitleCurItem(this, item = KW_FONT));
1473         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1474         sub_menu->add_subitem(POPUP_FONT,"%s %s",item, _("name"));
1475         sub_menu->add_subitem(POPUP_OFFSET, "%s ",item);
1476         sub_menu->add_subitem("/%s",item);
1477         add_item(cur_item = new TitleCurItem(this, item = KW_SIZE));
1478         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1479         sub_menu->add_subitem("%s +",item);
1480         sub_menu->add_subitem("%s -",item);
1481         sub_menu->add_subitem("%s ",item);
1482         sub_menu->add_subitem("/%s",item);
1483         add_item(cur_item = new TitleCurItem(this, item = KW_BOLD));
1484         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1485         sub_menu->add_subitem("%s 1",item);
1486         sub_menu->add_subitem("%s 0",item);
1487         sub_menu->add_subitem("/%s",item);
1488         add_item(cur_item = new TitleCurItem(this, item = KW_ITALIC));
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_CAPS));
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 0",item);
1497         sub_menu->add_subitem("%s -1",item);
1498         sub_menu->add_subitem("/%s",item);
1499         add_item(cur_item = new TitleCurItem(this, item = KW_UL));
1500         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1501         sub_menu->add_subitem("%s 1",item);
1502         sub_menu->add_subitem("%s 0",item);
1503         sub_menu->add_subitem("/%s",item);
1504         add_item(cur_item = new TitleCurItem(this, item = KW_BLINK));
1505         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1506         sub_menu->add_subitem("%s 1",item);
1507         sub_menu->add_subitem("%s -1",item);
1508         sub_menu->add_subitem("%s ",item);
1509         sub_menu->add_subitem("%s 0",item);
1510         sub_menu->add_subitem("/%s",item);
1511         add_item(cur_item = new TitleCurItem(this, item = KW_FIXED));
1512         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1513         sub_menu->add_subitem("%s ",item);
1514         sub_menu->add_subitem("%s 20",item);
1515         sub_menu->add_subitem("%s 10",item);
1516         sub_menu->add_subitem("%s 0",item);
1517         sub_menu->add_subitem("/%s",item);
1518         add_item(cur_item = new TitleCurItem(this, item = KW_SUP));
1519         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1520         sub_menu->add_subitem("%s 1",item);
1521         sub_menu->add_subitem("%s 0",item);
1522         sub_menu->add_subitem("%s -1",item);
1523         sub_menu->add_subitem("/%s",item);
1524         add_item(cur_item = new TitleCurItem(this, item = KW_PNG));
1525         cur_item->add_submenu(sub_menu = new TitleCurSubMenu(cur_item));
1526         sub_menu->add_subitem(POPUP_PNG,"%s %s", item, _("file"));
1527 }
1528
1529 TitleCurItem::TitleCurItem(TitleCurPopup *popup, const char *text)
1530  : BC_MenuItem(text)
1531 {
1532         this->popup = popup;
1533 }
1534 int TitleCurItem::handle_event()
1535 {
1536         return 1;
1537 }
1538
1539 TitleCurSubMenu::TitleCurSubMenu(TitleCurItem *cur_item)
1540 {
1541         this->cur_item = cur_item;
1542 }
1543 TitleCurSubMenu::~TitleCurSubMenu()
1544 {
1545 }
1546
1547 TitleCurSubMenuItem::TitleCurSubMenuItem(TitleCurSubMenu *submenu, const char *text, int popup_type)
1548  : BC_MenuItem(text)
1549 {
1550         this->submenu = submenu;
1551         this->popup_type = popup_type;
1552 }
1553 TitleCurSubMenuItem::~TitleCurSubMenuItem()
1554 {
1555 }
1556 int TitleCurSubMenuItem::handle_event()
1557 {
1558         TitleCurPopup *popup = submenu->cur_item->popup;
1559         TitleWindow *window = popup->window;
1560         const char *item_text = get_text();
1561         int ofs = *item_text == '/' ? 0 : -1;
1562         switch( popup_type ) {
1563         case POPUP_FONT: {
1564                 int px, py;
1565                 window->get_pop_cursor_xy(px ,py);
1566                 window->fonts_popup->activate(px, py, 300,200);
1567                 return 1; }
1568         case POPUP_COLOR: {
1569                 window->color_popup->activate();
1570                 return 1; }
1571         case POPUP_PNG: {
1572                 window->png_popup->activate();
1573                 return 1; }
1574         case POPUP_OFFSET:
1575                 ofs = -1;
1576                 break;
1577         }
1578         char txt[BCSTRLEN];
1579         sprintf(txt, "<%s>", item_text);
1580         return window->insert_ibeam(txt, ofs);
1581 }
1582
1583 TitleFontsPopup::TitleFontsPopup(TitleMain *client, TitleWindow *window)
1584  : BC_ListBox(-1, -1, 1, 1, LISTBOX_ICON_LIST,
1585         &window->fonts, 0, 0, 1, 0, 1)
1586 {
1587         this->client = client;
1588         this->window = window;
1589         set_use_button(0);
1590         set_show_query(1);
1591 }
1592 TitleFontsPopup::~TitleFontsPopup()
1593 {
1594 }
1595 int TitleFontsPopup::keypress_event()
1596 {
1597         switch( get_keypress() ) {
1598         case ESC:
1599         case DELETE:
1600                 deactivate();
1601                 return 1;
1602         default:
1603                 break;
1604         }
1605         return BC_ListBox::keypress_event();
1606 }
1607
1608 int TitleFontsPopup::handle_event()
1609 {
1610         deactivate();
1611         BC_ListBoxItem *item = get_selection(0, 0);
1612         if( !item ) return 1;
1613         const char *item_text = item->get_text();
1614         char txt[BCTEXTLEN];  sprintf(txt, "<%s %s>", KW_FONT, item_text);
1615         return window->insert_ibeam(txt);
1616 }
1617
1618 TitleColorPopup::TitleColorPopup(TitleMain *client, TitleWindow *window)
1619  : ColorPicker(0, _("Color"))
1620 {
1621         this->client = client;
1622         this->window = window;
1623         this->color_value = client->config.color;
1624 }
1625 TitleColorPopup::~TitleColorPopup()
1626 {
1627 }
1628 int TitleColorPopup::handle_new_color(int output, int alpha)
1629 {
1630         color_value = output;
1631         return 1;
1632 }
1633 int TitleColorPopup::activate()
1634 {
1635         start_window(client->config.color, 255, 1);
1636         return 1;
1637 }
1638 void TitleColorPopup::handle_done_event(int result)
1639 {
1640         if( result ) return;
1641         char txt[BCSTRLEN];  sprintf(txt, "<%s #%06x>", KW_COLOR, color_value);
1642         window->insert_ibeam(txt);
1643 }
1644
1645 TitlePngPopup::TitlePngPopup(TitleMain *client, TitleWindow *window)
1646  : BC_DialogThread()
1647 {
1648         this->client = client;
1649         this->window = window;
1650 }
1651
1652 TitlePngPopup::~TitlePngPopup()
1653 {
1654         close_window();
1655 }
1656
1657 void TitlePngPopup::handle_done_event(int result)
1658 {
1659         if( result ) return;
1660         BrowseButtonWindow *gui = (BrowseButtonWindow *)get_gui();
1661         const char *path = gui->get_submitted_path();
1662         char txt[BCSTRLEN];  sprintf(txt, "<%s %s>", KW_PNG, path);
1663         window->insert_ibeam(txt);
1664 }
1665
1666 BC_Window *TitlePngPopup::new_gui()
1667 {
1668         BC_DisplayInfo display_info;
1669         int x = display_info.get_abs_cursor_x();
1670         int y = display_info.get_abs_cursor_y();
1671
1672         BC_Window *gui = new BrowseButtonWindow(client->server->mwindow->theme,
1673                 x-25, y-100, window, "", _("Png file"), _("Png path"), 0);
1674         gui->create_objects();
1675         return gui;
1676 }
1677
1678 int TitlePngPopup::activate()
1679 {
1680         BC_DialogThread::start();
1681         return 1;
1682 }
1683