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