upgrade ffmpeg/x265, guard avcodec_flush_buffers, filebox fixups, remove unused grid...
[goodguy/history.git] / cinelerra-5.0 / guicast / bcwindowbase.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 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 "bcbitmap.h"
23 #include "bcclipboard.h"
24 #include "bcdisplay.h"
25 #include "bcdisplayinfo.h"
26 #include "bcmenubar.h"
27 #include "bcpixmap.h"
28 #include "bcpopup.h"
29 #include "bcpopupmenu.h"
30 #include "bcrepeater.h"
31 #include "bcresources.h"
32 #include "bcsignals.h"
33 #include "bcsubwindow.h"
34 #include "bcsynchronous.h"
35 #include "bctimer.h"
36 #include "bcwindowbase.h"
37 #include "bcwindowevents.h"
38 #include "bccmodels.h"
39 #include "colors.h"
40 #include "condition.h"
41 #include "cursors.h"
42 #include "bchash.h"
43 #include "fonts.h"
44 #include "format.inc"
45 #include "keys.h"
46 #include "language.h"
47 #include "mutex.h"
48 #include "sizes.h"
49 #include "vframe.h"
50
51 #ifdef HAVE_GL
52 #include <GL/gl.h>
53 #endif
54 #include <string.h>
55 #include <unistd.h>
56 #include <wchar.h>
57
58 #include <X11/extensions/Xinerama.h>
59 #include <X11/extensions/Xvlib.h>
60 #include <X11/extensions/shape.h>
61 #include <X11/XF86keysym.h>
62 #include <X11/Sunkeysym.h>
63
64
65 BC_ResizeCall::BC_ResizeCall(int w, int h)
66 {
67         this->w = w;
68         this->h = h;
69 }
70
71
72
73
74
75
76
77 int BC_WindowBase::shm_completion_event = -1;
78
79
80
81 BC_Resources BC_WindowBase::resources;
82
83 Window XGroupLeader = 0;
84
85 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
86 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
87
88 BC_WindowBase::BC_WindowBase(int opts)
89 {
90         this->options = opts;
91 //printf("BC_WindowBase::BC_WindowBase 1\n");
92         BC_WindowBase::initialize();
93 }
94
95 BC_WindowBase::~BC_WindowBase()
96 {
97 #ifdef SINGLE_THREAD
98         BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
99 #else
100         if(window_type == MAIN_WINDOW)
101                 lock_window("BC_WindowBase::~BC_WindowBase");
102 #endif
103
104 #ifdef HAVE_LIBXXF86VM
105    if(window_type == VIDMODE_SCALED_WINDOW && vm_switched)
106    {
107            restore_vm();
108    }
109 #endif
110
111         hide_tooltip();
112         if(window_type != MAIN_WINDOW)
113         {
114                 if(top_level->active_menubar == this) top_level->active_menubar = 0;
115                 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
116                 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
117 // Remove pointer from parent window to this
118                 parent_window->subwindows->remove(this);
119         }
120
121         if(icon_window) delete icon_window;
122         if(window_type == POPUP_WINDOW) parent_window->remove_popup(this);
123
124 // Delete the subwindows
125         is_deleting = 1;
126         if(subwindows)
127         {
128                 while(subwindows->total)
129                 {
130 // Subwindow removes its own pointer
131                         delete subwindows->values[0];
132                 }
133                 delete subwindows;
134         }
135
136         delete pixmap;
137
138 #ifdef HAVE_GL
139         if( get_resources()->get_synchronous() && top_level->options & GLX_WINDOW ) {
140                 if( !glx_win ) {
141 // NVIDIA library threading problem, XCloseDisplay SEGVs without this
142                         sync_lock("BC_WindowBase::~BC_WindowBase:XDestroyWindow");
143                         glXMakeContextCurrent(top_level->display, 0, 0, 0);
144                         XDestroyWindow(top_level->display, win);
145                         sync_unlock();
146                 }
147                 else
148                         get_resources()->get_synchronous()->delete_window(this);
149         }
150         else
151 #endif
152                 XDestroyWindow(top_level->display, win);
153
154         if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
155         if(icon_pixmap) delete icon_pixmap;
156         if(temp_bitmap) delete temp_bitmap;
157         top_level->active_bitmaps.remove_buffers(this);
158         if(_7segment_pixmaps)
159         {
160                 for(int i = 0; i < TOTAL_7SEGMENT; i++)
161                         delete _7segment_pixmaps[i];
162
163                 delete [] _7segment_pixmaps;
164         }
165
166
167
168         if(window_type == MAIN_WINDOW)
169         {
170                 XFreeGC(display, gc);
171                 XFreeFont(display, smallfont);
172                 XFreeFont(display, mediumfont);
173                 XFreeFont(display, largefont);
174                 XFreeFont(display, bigfont);
175
176 #ifdef HAVE_XFT
177                 if(bigfont_xft)
178                         XftFontClose (display, (XftFont*)bigfont_xft);
179                 if(largefont_xft)
180                         XftFontClose (display, (XftFont*)largefont_xft);
181                 if(mediumfont_xft)
182                         XftFontClose (display, (XftFont*)mediumfont_xft);
183                 if(smallfont_xft)
184                         XftFontClose (display, (XftFont*)smallfont_xft);
185 #endif
186                 finit_im();
187
188                 flush();
189                 sync_display();
190
191                 if( xinerama_info )
192                         XFree(xinerama_info);
193                 xinerama_screens = 0;
194                 xinerama_info = 0;
195
196                 unlock_window();
197 // Can't close display if another thread is waiting for events.
198 // Synchronous thread must delete display it owns a GLX_WINDOW
199 // Must be last reference to display.
200 #ifndef SINGLE_THREAD
201 #ifdef HAVE_GL
202                 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
203                         printf("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
204                                 "implemented for BC_Pixmap.\n");
205                         get_resources()->get_synchronous()->delete_display(this);
206                 }
207                 else
208 #endif
209                         XCloseDisplay(display);
210 // clipboard uses a different display connection
211                 clipboard->stop_clipboard();
212                 delete clipboard;
213 #endif // SINGLE_THREAD
214         }
215
216         resize_history.remove_all_objects();
217
218 #ifndef SINGLE_THREAD
219         common_events.remove_all_objects();
220         delete event_lock;
221         delete event_condition;
222         delete init_lock;
223 #else
224         top_level->window_lock = 0;
225         BC_Display::unlock_display();
226 #endif
227         delete cursor_timer;
228
229 #if HAVE_GL
230         if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
231         if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
232         if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
233 #endif
234
235         UNSET_ALL_LOCKS(this)
236 }
237
238 int BC_WindowBase::initialize()
239 {
240         done = 0;
241         done_set = 0;
242         window_running = 0;
243         test_keypress = 0;
244         keys_return[0] = 0;
245         is_deleting = 0;
246         window_lock = 0;
247         x = 0;
248         y = 0;
249         w = 0;
250         h = 0;
251         bg_color = -1;
252         line_width = 1;
253         line_dashes = 0;
254         top_level = 0;
255         parent_window = 0;
256         subwindows = 0;
257         xinerama_info = 0;
258         xinerama_screens = 0;
259         xvideo_port_id = -1;
260         video_on = 0;
261         motion_events = 0;
262         resize_events = 0;
263         translation_events = 0;
264         ctrl_mask = shift_mask = alt_mask = 0;
265         cursor_x = cursor_y = button_number = 0;
266         button_down = 0;
267         button_pressed = 0;
268         button_time1 = 0;
269         button_time2 = 0;
270         button_time3 = 0;
271         double_click = 0;
272         triple_click = 0;
273         event_win = 0;
274         last_motion_win = 0;
275         key_pressed = 0;
276         active_menubar = 0;
277         active_popup_menu = 0;
278         active_subwindow = 0;
279         pixmap = 0;
280         bg_pixmap = 0;
281         _7segment_pixmaps = 0;
282         tooltip_text = 0;
283         force_tooltip = 0;
284 //      next_repeat_id = 0;
285         tooltip_popup = 0;
286         tooltip_done = 0;
287         current_font = MEDIUMFONT;
288         current_color = BLACK;
289         current_cursor = ARROW_CURSOR;
290         hourglass_total = 0;
291         is_dragging = 0;
292         shared_bg_pixmap = 0;
293         icon_pixmap = 0;
294         icon_window = 0;
295         window_type = MAIN_WINDOW;
296         translation_count = 0;
297         x_correction = y_correction = 0;
298         temp_bitmap = 0;
299         tooltip_on = 0;
300         temp_cursor = 0;
301         toggle_value = 0;
302         toggle_drag = 0;
303         has_focus = 0;
304         is_hourglass = 0;
305         is_transparent = 0;
306 #ifdef HAVE_LIBXXF86VM
307         vm_switched = 0;
308 #endif
309         smallfont_xft = 0;
310         bold_largefont_xft = 0;
311         bold_mediumfont_xft = 0;
312         bold_smallfont_xft = 0;
313         input_method = 0;
314         input_context = 0;
315
316         mediumfont_xft = 0;
317         largefont_xft = 0;
318         bigfont_xft = 0;
319 #ifdef SINGLE_THREAD
320         completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
321 #else
322 // Need these right away since put_event is called before run_window sometimes.
323         event_lock = new Mutex("BC_WindowBase::event_lock");
324         event_condition = new Condition(0, "BC_WindowBase::event_condition");
325         init_lock = new Condition(0, "BC_WindowBase::init_lock");
326 #endif
327
328         cursor_timer = new Timer;
329         event_thread = 0;
330 #ifdef HAVE_GL
331         glx_fbcfgs_window = 0;  n_fbcfgs_window = 0;
332         glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
333         glx_fbcfgs_pixmap = 0;  n_fbcfgs_pixmap = 0;
334
335         glx_fb_config = 0;
336         glx_win_context = 0;
337         glx_win = 0;
338 #endif
339
340         return 0;
341 }
342
343
344
345 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
346                         LeaveWindowMask | \
347                         ButtonPressMask | \
348                         ButtonReleaseMask | \
349                         PointerMotionMask | \
350                         FocusChangeMask
351
352
353 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
354                                 const char *title,
355                                 int x,
356                                 int y,
357                                 int w,
358                                 int h,
359                                 int minw,
360                                 int minh,
361                                 int allow_resize,
362                                 int private_color,
363                                 int hide,
364                                 int bg_color,
365                                 const char *display_name,
366                                 int window_type,
367                                 BC_Pixmap *bg_pixmap,
368                                 int group_it)
369 {
370         XSetWindowAttributes attr;
371         unsigned long mask;
372         XSizeHints size_hints;
373         int root_w;
374         int root_h;
375 #ifdef HAVE_LIBXXF86VM
376         int vm;
377 #endif
378
379         id = get_resources()->get_id();
380         int need_lock = 0;
381         if(parent_window) top_level = parent_window->top_level;
382         if( top_level ) { // need this to avoid deadlock with Xlib's locks
383                 need_lock = 1;
384                 lock_window("BC_WindowBase::create_window");
385         }
386         get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
387
388 #ifdef HAVE_LIBXXF86VM
389         if(window_type == VIDMODE_SCALED_WINDOW)
390                 closest_vm(&vm,&w,&h);
391 #endif
392
393         this->x = x;
394         this->y = y;
395         this->w = w;
396         this->h = h;
397         this->bg_color = bg_color;
398         this->window_type = window_type;
399         this->hidden = hide;
400         this->private_color = private_color;
401         this->parent_window = parent_window;
402         this->bg_pixmap = bg_pixmap;
403         this->allow_resize = allow_resize;
404         if(display_name)
405                 strcpy(this->display_name, display_name);
406         else
407                 this->display_name[0] = 0;
408
409         strcpy(this->title, _(title));
410         if(bg_pixmap) shared_bg_pixmap = 1;
411
412         subwindows = new BC_SubWindowList;
413
414         if(window_type == MAIN_WINDOW)
415         {
416                 top_level = this;
417                 parent_window = this;
418
419
420 #ifdef SINGLE_THREAD
421                 display = BC_Display::get_display(display_name);
422                 BC_Display::lock_display("BC_WindowBase::create_window");
423 //              BC_Display::display_global->new_window(this);
424 #else
425
426 // get the display connection
427
428 // This function must be the first Xlib
429 // function a multi-threaded program calls
430                 XInitThreads();
431                 display = init_display(display_name);
432                 if( shm_completion_event < 0 ) shm_completion_event =
433                         ShmCompletion + XShmGetEventBase(display);
434 #endif
435
436                 screen = DefaultScreen(display);
437                 rootwin = RootWindow(display, screen);
438 // window placement boundaries
439                 if( !xinerama_screens && XineramaIsActive(display) )
440                         xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
441                 root_w = get_root_w(0);
442                 root_h = get_root_h(0);
443
444 #if HAVE_GL
445                 vis = get_glx_visual(display);
446                 if( !vis )
447 #endif
448                         vis = DefaultVisual(display, screen);
449
450                 default_depth = DefaultDepth(display, screen);
451
452                 client_byte_order = (*(const u_int32_t*)"a   ") & 0x00000001;
453                 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
454
455
456 // This must be done before fonts to know if antialiasing is available.
457                 init_colors();
458 // get the resources
459                 if(resources.use_shm < 0) resources.initialize_display(this);
460                 x_correction = get_resources()->get_left_border();
461                 y_correction = get_resources()->get_top_border();
462
463 // clamp window placement
464                 if(this->x + this->w + x_correction > root_w)
465                         this->x = root_w - this->w - x_correction;
466                 if(this->y + this->h + y_correction > root_h)
467                         this->y = root_h - this->h - y_correction;
468                 if(this->x < 0) this->x = 0;
469                 if(this->y < 0) this->y = 0;
470
471                 if(this->bg_color == -1)
472                         this->bg_color = resources.get_bg_color();
473
474 // printf("bcwindowbase 1 %s\n", title);
475 // if(window_type == MAIN_WINDOW) sleep(1);
476 // printf("bcwindowbase 10\n");
477                 init_fonts();
478                 init_gc();
479                 init_cursors();
480
481 // Create the window
482                 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
483
484                 attr.event_mask = DEFAULT_EVENT_MASKS |
485                         StructureNotifyMask |
486                         KeyPressMask |
487                         KeyReleaseMask;
488
489                 attr.background_pixel = get_color(this->bg_color);
490                 attr.colormap = cmap;
491                 attr.cursor = get_cursor_struct(ARROW_CURSOR);
492
493                 win = XCreateWindow(display, rootwin,
494                         this->x, this->y, this->w, this->h, 0,
495                         top_level->default_depth, InputOutput,
496                         vis, mask, &attr);
497                 XGetNormalHints(display, win, &size_hints);
498
499                 size_hints.flags = PSize | PMinSize | PMaxSize;
500                 size_hints.width = this->w;
501                 size_hints.height = this->h;
502                 size_hints.min_width = allow_resize ? minw : this->w;
503                 size_hints.max_width = allow_resize ? 32767 : this->w;
504                 size_hints.min_height = allow_resize ? minh : this->h;
505                 size_hints.max_height = allow_resize ? 32767 : this->h;
506                 if(x > -BC_INFINITY && x < BC_INFINITY)
507                 {
508                         size_hints.flags |= PPosition;
509                         size_hints.x = this->x;
510                         size_hints.y = this->y;
511                 }
512
513                 char *txlist[2];
514                 txlist[0] = this->title;
515                 txlist[1] = 0;
516                 XTextProperty titleprop;
517                 if(options & WINDOW_UTF8)
518                         Xutf8TextListToTextProperty(display, txlist,  1,
519                                 XUTF8StringStyle, &titleprop);
520                 else
521                         XmbTextListToTextProperty(display, txlist, 1,
522                                 XStdICCTextStyle, &titleprop);
523                 XSetWMProperties(display, win, &titleprop, &titleprop,
524                         0, 0, &size_hints, 0, 0);
525                 XFree(titleprop.value);
526                 get_atoms();
527
528 #ifndef SINGLE_THREAD
529                 clipboard = new BC_Clipboard(display_name);
530                 clipboard->start_clipboard();
531 #endif
532
533
534                 if (group_it)
535                 {
536                         Atom ClientLeaderXAtom;
537                         if (XGroupLeader == 0)
538                                 XGroupLeader = win;
539                         const char *instance_name = "cinelerra";
540                         const char *class_name = "Cinelerra";
541                         XClassHint *class_hints = XAllocClassHint();
542                         class_hints->res_name = (char*)instance_name;
543                         class_hints->res_class = (char*)class_name;
544                         XSetClassHint(top_level->display, win, class_hints);
545                         XFree(class_hints);
546                         ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
547                         XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
548                                 PropModeReplace, (unsigned char *)&XGroupLeader, true);
549                 }
550                 init_im();
551         }
552
553 #ifdef HAVE_LIBXXF86VM
554         if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
555         {
556                 scale_vm (vm);
557                 vm_switched = 1;
558         }
559 #endif
560
561 #ifdef HAVE_LIBXXF86VM
562         if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
563 #else
564         if(window_type == POPUP_WINDOW)
565 #endif
566         {
567                 mask = CWEventMask | CWBackPixel | CWColormap |
568                         CWOverrideRedirect | CWSaveUnder | CWCursor;
569
570                 attr.event_mask = DEFAULT_EVENT_MASKS |
571                         KeyPressMask |
572                         KeyReleaseMask;
573
574                 if(this->bg_color == -1)
575                         this->bg_color = resources.get_bg_color();
576                 attr.background_pixel = top_level->get_color(bg_color);
577                 attr.colormap = top_level->cmap;
578                 if(top_level->is_hourglass)
579                         attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
580                 else
581                         attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
582                 attr.override_redirect = True;
583                 attr.save_under = True;
584
585                 win = XCreateWindow(top_level->display,
586                         top_level->rootwin, this->x, this->y, this->w, this->h, 0,
587                         top_level->default_depth, InputOutput, top_level->vis, mask,
588                         &attr);
589                 top_level->add_popup(this);
590         }
591
592         if(window_type == SUB_WINDOW)
593         {
594                 mask = CWEventMask | CWBackPixel | CWCursor;
595                 attr.event_mask = DEFAULT_EVENT_MASKS;
596                 attr.background_pixel = top_level->get_color(this->bg_color);
597                 if(top_level->is_hourglass)
598                         attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
599                 else
600                         attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
601                 win = XCreateWindow(top_level->display,
602                         parent_window->win, this->x, this->y, this->w, this->h, 0,
603                         top_level->default_depth, InputOutput, top_level->vis, mask,
604                         &attr);
605                 init_window_shape();
606                 if(!hidden) XMapWindow(top_level->display, win);
607         }
608
609 // Create pixmap for all windows
610         pixmap = new BC_Pixmap(this, this->w, this->h);
611
612 // Set up options for main window
613         if(window_type == MAIN_WINDOW)
614         {
615                 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
616                 {
617                         this->bg_pixmap = new BC_Pixmap(this,
618                                 get_resources()->bg_image,
619                                 PIXMAP_OPAQUE);
620                 }
621
622                 if(!hidden) show_window();
623
624         }
625
626
627
628
629         draw_background(0, 0, this->w, this->h);
630
631         flash(-1, -1, -1, -1, 0);
632
633 // Set up options for popup window
634 #ifdef HAVE_LIBXXF86VM
635         if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
636 #else
637         if(window_type == POPUP_WINDOW)
638 #endif
639         {
640                 init_window_shape();
641                 if(!hidden) show_window();
642         }
643         get_resources()->create_window_lock->unlock();
644         if(need_lock) unlock_window();
645
646         return 0;
647 }
648
649 Display* BC_WindowBase::init_display(const char *display_name)
650 {
651         Display* display;
652
653         if(display_name && display_name[0] == 0) display_name = NULL;
654         if((display = XOpenDisplay(display_name)) == NULL) {
655                 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
656                         display_name);
657                 if(getenv("DISPLAY") == NULL) {
658                         printf("'DISPLAY' environment variable not set.\n");
659                         exit(1);
660                 }
661                 else {
662 // Try again with default display.
663                         if((display = XOpenDisplay(0)) == NULL) {
664                                 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
665                                 exit(1);
666                         }
667                 }
668         }
669         return display;
670 }
671
672 Display* BC_WindowBase::get_display()
673 {
674         return top_level->display;
675 }
676
677 int BC_WindowBase::get_screen()
678 {
679         return top_level->screen;
680 }
681
682 int BC_WindowBase::run_window()
683 {
684         done_set = done = 0;
685         return_value = 0;
686
687
688 // Events may have been sent before run_window so can't initialize them here.
689
690 #ifdef SINGLE_THREAD
691         set_repeat(get_resources()->tooltip_delay);
692         BC_Display::display_global->new_window(this);
693
694 // If the first window created, run the display loop in this thread.
695         if(BC_Display::display_global->is_first(this))
696         {
697                 BC_Display::unlock_display();
698                 BC_Display::display_global->loop();
699         }
700         else
701         {
702                 BC_Display::unlock_display();
703                 completion_lock->lock("BC_WindowBase::run_window");
704         }
705
706         BC_Display::lock_display("BC_WindowBase::run_window");
707         BC_Display::display_global->delete_window(this);
708
709         unset_all_repeaters();
710         hide_tooltip();
711         BC_Display::unlock_display();
712
713 #else // SINGLE_THREAD
714
715
716
717 // Start tooltips
718         set_repeat(get_resources()->tooltip_delay);
719
720 // Start X server events
721         event_thread = new BC_WindowEvents(this);
722         event_thread->start();
723
724 // Release wait lock
725         window_running = 1;
726         init_lock->unlock();
727
728 // Handle common events
729         while(!done)
730         {
731                 dispatch_event(0);
732         }
733
734         unset_all_repeaters();
735         hide_tooltip();
736         delete event_thread;
737         event_thread = 0;
738         event_condition->reset();
739         common_events.remove_all_objects();
740         window_running = 0;
741         done = 0;
742
743 #endif // SINGLE_THREAD
744
745         return return_value;
746 }
747
748 int BC_WindowBase::get_key_masks(unsigned int key_state)
749 {
750 // printf("BC_WindowBase::get_key_masks %llx\n",
751 // event->xkey.state);
752         ctrl_mask = (key_state & ControlMask) ? 1 : 0;  // ctrl key down
753         shift_mask = (key_state & ShiftMask) ? 1 : 0;   // shift key down
754         alt_mask = (key_state & Mod1Mask) ? 1 : 0;      // alt key down
755         return 0;
756 }
757
758
759 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
760 {
761         BC_KeyboardHandlerLock set;
762         BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
763 }
764
765 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
766 {
767         BC_KeyboardHandlerLock set;
768         int i = BC_KeyboardHandler::listeners.size();
769         while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
770         if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
771 }
772
773 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
774 {
775         int result = (win->*handler)(wp);
776         return result;
777 }
778
779 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
780 {
781         int result = 0;
782         BC_KeyboardHandlerLock set;
783         for( int i=0; !result && i<listeners.size(); ++i ) {
784                 BC_KeyboardHandler *listener = listeners[i];
785                 result = listener->run_event(wp);
786         }
787         return result;
788 }
789
790 void BC_KeyboardHandler::kill_grabs()
791 {
792         BC_KeyboardHandlerLock set;
793         for( int i=0; i<listeners.size(); ++i ) {
794                 BC_WindowBase *win = listeners[i]->win;
795                 if( win->get_window_type() != POPUP_WINDOW ) continue;
796                 ((BC_Popup *)win)->ungrab_keyboard();
797         }
798 }
799
800 void BC_ActiveBitmaps::reque(XEvent *event)
801 {
802         XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
803         ShmSeg shmseg = shm_ev->shmseg;
804         Drawable drawable = shm_ev->drawable;
805 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
806         active_lock.lock("BC_BitmapImage::reque");
807         BC_BitmapImage *bfr = first;
808         while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
809         if( bfr && bfr->drawable == drawable )
810                 remove_pointer(bfr);
811         active_lock.unlock();
812         if( !bfr ) {
813 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
814 //              printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
815 //                       (int)shmseg, (int)drawable);
816                 return;
817         }
818         if( bfr->drawable != drawable ) return;
819         if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
820         bfr->bitmap->reque(bfr);
821 }
822
823 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
824 {
825         active_lock.lock("BC_BitmapImage::insert");
826         bfr->drawable = pixmap;
827         append(bfr);
828         active_lock.unlock();
829 }
830
831 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
832 {
833         active_lock.lock("BC_ActiveBitmaps::remove");
834         for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
835                 nxt = bfr->next;
836                 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
837                 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
838         }
839         active_lock.unlock();
840 }
841
842 BC_ActiveBitmaps::BC_ActiveBitmaps()
843 {
844 }
845
846 BC_ActiveBitmaps::~BC_ActiveBitmaps()
847 {
848 }
849
850
851
852 int BC_WindowBase::keysym_lookup(XEvent *event)
853 {
854         for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
855         for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
856
857         if( event->xany.send_event && !event->xany.serial ) {
858                 keysym = (KeySym) event->xkey.keycode;
859                 keys_return[0] = keysym;
860                 return 0;
861         }
862
863         wkey_string_length = XwcLookupString(input_context,
864                 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
865 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
866 //  wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
867
868         Status stat;
869         int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
870                         keys_return, KEYPRESSLEN, &keysym, &stat);
871 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
872         if( stat == XLookupBoth ) return ret;
873         if( stat == XLookupKeySym ) return 0;
874
875         return XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
876 }
877
878 pthread_t locking_task = -1;
879 int locking_event = -1;
880 int locking_message = -1;
881
882 int BC_WindowBase::dispatch_event(XEvent *event)
883 {
884         Window tempwin;
885         int result;
886         XClientMessageEvent *ptr;
887         int cancel_resize, cancel_translation;
888         const int debug = 0;
889
890         key_pressed = 0;
891
892 #ifndef SINGLE_THREAD
893 // If an event is waiting get it, otherwise
894 // wait for next event only if there are no compressed events.
895         if(get_event_count() ||
896                 (!motion_events && !resize_events && !translation_events))
897         {
898                 event = get_event();
899 // Lock out window deletions
900                 lock_window("BC_WindowBase::dispatch_event 1");
901 locking_event = event->type;
902 locking_task = pthread_self();
903 locking_message = event->xclient.message_type;
904         }
905         else
906 // Handle compressed events
907         {
908                 lock_window("BC_WindowBase::dispatch_event 2");
909                 if(resize_events)
910                         dispatch_resize_event(last_resize_w, last_resize_h);
911                 if(motion_events)
912                         dispatch_motion_event();
913                 if(translation_events)
914                         dispatch_translation_event();
915
916                 unlock_window();
917                 return 0;
918         }
919
920 #endif
921
922
923
924
925 //static const char *event_names[] = {
926 //  "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
927 //  "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
928 //  "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
929 //  "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
930 //  "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
931 //  "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
932 //  "GenericEvent", "LASTEvent",
933 //};
934 //
935 //if(debug)
936 //if(event->type != ClientMessage) {
937 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
938 //__LINE__, title, event, event->type,
939 //  event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
940 //   event_names[event->type] : "Unknown");
941 //}
942
943
944
945
946         switch(event->type) {
947         case ClientMessage:
948 // Clear the resize buffer
949                 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
950 // Clear the motion buffer since this can clear the window
951                 if(motion_events)
952                 {
953                         dispatch_motion_event();
954                 }
955
956                 ptr = (XClientMessageEvent*)event;
957
958
959                 if(ptr->message_type == ProtoXAtom &&
960                         (Atom)ptr->data.l[0] == DelWinXAtom)
961                 {
962                         close_event();
963                 }
964                 else
965                 if(ptr->message_type == RepeaterXAtom)
966                 {
967                         dispatch_repeat_event(ptr->data.l[0]);
968 // Make sure the repeater still exists.
969 //                              for(int i = 0; i < repeaters.total; i++)
970 //                              {
971 //                                      if(repeaters.values[i]->repeat_id == ptr->data.l[0])
972 //                                      {
973 //                                              dispatch_repeat_event_master(ptr->data.l[0]);
974 //                                              break;
975 //                                      }
976 //                              }
977                 }
978                 else
979                 if(ptr->message_type == SetDoneXAtom)
980                 {
981                         done = 1;
982                         } else
983                         { // We currently use X marshalling for xatom events, we can switch to something else later
984                                 recieve_custom_xatoms((xatom_event *)ptr);
985                 }
986                 break;
987
988         case FocusIn:
989                 has_focus = 1;
990                 dispatch_focus_in();
991                 break;
992
993         case FocusOut:
994                 has_focus = 0;
995                 dispatch_focus_out();
996                 break;
997
998 // Maximized
999         case MapNotify:
1000                 break;
1001
1002 // Minimized
1003         case UnmapNotify:
1004                 break;
1005
1006         case ButtonPress:
1007                 get_key_masks(event->xbutton.state);
1008                 cursor_x = event->xbutton.x;
1009                 cursor_y = event->xbutton.y;
1010                 button_number = event->xbutton.button;
1011
1012 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1013                 event_win = event->xany.window;
1014                 if (button_number < 6)
1015                 {
1016                         if(button_number < 4)
1017                                 button_down = 1;
1018                         button_pressed = event->xbutton.button;
1019                         button_time1 = button_time2;
1020                         button_time2 = button_time3;
1021                         button_time3 = event->xbutton.time;
1022                         drag_x = cursor_x;
1023                         drag_y = cursor_y;
1024                         drag_win = event_win;
1025                         drag_x1 = cursor_x - get_resources()->drag_radius;
1026                         drag_x2 = cursor_x + get_resources()->drag_radius;
1027                         drag_y1 = cursor_y - get_resources()->drag_radius;
1028                         drag_y2 = cursor_y + get_resources()->drag_radius;
1029
1030                         if((long)(button_time3 - button_time1) < resources.double_click * 2)
1031                         {
1032                                 triple_click = 1;
1033                                 button_time3 = button_time2 = button_time1 = 0;
1034                         }
1035                         if((long)(button_time3 - button_time2) < resources.double_click)
1036                         {
1037                                 double_click = 1;
1038 //                              button_time3 = button_time2 = button_time1 = 0;
1039                         }
1040                         else
1041                         {
1042                                 triple_click = 0;
1043                                 double_click = 0;
1044                         }
1045
1046                         dispatch_button_press();
1047                 }
1048                 break;
1049
1050         case ButtonRelease:
1051                 get_key_masks(event->xbutton.state);
1052                 button_number = event->xbutton.button;
1053                 event_win = event->xany.window;
1054                 if (button_number < 6)
1055                 {
1056                         if(button_number < 4)
1057                                 button_down = 0;
1058 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1059
1060                         dispatch_button_release();
1061                 }
1062                 break;
1063
1064         case Expose:
1065                 event_win = event->xany.window;
1066                 dispatch_expose_event();
1067                 break;
1068
1069         case MotionNotify:
1070                 get_key_masks(event->xmotion.state);
1071 // Dispatch previous motion event if this is a subsequent motion from a different window
1072                 if(motion_events && last_motion_win != event->xany.window)
1073                 {
1074                         dispatch_motion_event();
1075                 }
1076
1077 // Buffer the current motion
1078                 motion_events = 1;
1079                 last_motion_state = event->xmotion.state;
1080                 last_motion_x = event->xmotion.x;
1081                 last_motion_y = event->xmotion.y;
1082                 last_motion_win = event->xany.window;
1083                 break;
1084
1085         case ConfigureNotify:
1086 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1087 // __LINE__,
1088 // event->xany.window,
1089 // win);
1090 // dump_windows();
1091                 XTranslateCoordinates(top_level->display,
1092                         top_level->win,
1093                         top_level->rootwin,
1094                         0,
1095                         0,
1096                         &last_translate_x,
1097                         &last_translate_y,
1098                         &tempwin);
1099                 last_resize_w = event->xconfigure.width;
1100                 last_resize_h = event->xconfigure.height;
1101
1102                 cancel_resize = 0;
1103                 cancel_translation = 0;
1104
1105 // Resize history prevents responses to recursive resize requests
1106                 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1107                 {
1108                         if(resize_history.values[i]->w == last_resize_w &&
1109                                 resize_history.values[i]->h == last_resize_h)
1110                         {
1111                                 delete resize_history.values[i];
1112                                 resize_history.remove_number(i);
1113                                 cancel_resize = 1;
1114                         }
1115                 }
1116
1117                 if(last_resize_w == w && last_resize_h == h)
1118                         cancel_resize = 1;
1119
1120                 if(!cancel_resize)
1121                 {
1122                         resize_events = 1;
1123                 }
1124
1125                 if((last_translate_x == x && last_translate_y == y))
1126                         cancel_translation = 1;
1127
1128                 if(!cancel_translation)
1129                 {
1130                         translation_events = 1;
1131                 }
1132
1133                 translation_count++;
1134                 break;
1135
1136         case KeyPress:
1137                 get_key_masks(event->xkey.state);
1138                 keys_return[0] = 0;  keysym = -1;
1139                 if(XFilterEvent(event, win)) {
1140                         break;
1141                 }
1142                 if( keysym_lookup(event) < 0 ) {
1143                         printf("keysym %x\n", (uint32_t)keysym);
1144                         break;
1145                 }
1146
1147 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1148 //__LINE__,
1149 //keysym);
1150
1151 // block out control keys
1152                 if(keysym > 0xffe0 && keysym < 0xffff) break;
1153 // block out Alt_GR key
1154                 if(keysym == 0xfe03) break;
1155
1156                 if(test_keypress)
1157                          printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1158
1159 #ifdef X_HAVE_UTF8_STRING
1160 //It's Ascii or UTF8?
1161 //              if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1162 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1163
1164                 if( ((keys_return[1] & 0xff) > 0x80) &&
1165                     ((keys_return[0] & 0xff) > 0xC0) ) {
1166 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1167                         key_pressed = keysym & 0xff;
1168                 }
1169                 else {
1170 #endif
1171
1172                 switch(keysym) {
1173 // block out extra keys
1174                 case XK_Alt_L:
1175                 case XK_Alt_R:
1176                 case XK_Shift_L:
1177                 case XK_Shift_R:
1178                 case XK_Control_L:
1179                 case XK_Control_R:
1180                         key_pressed = 0;
1181                         break;
1182
1183 // Translate key codes
1184                 case XK_Return:         key_pressed = RETURN;   break;
1185                 case XK_Up:             key_pressed = UP;       break;
1186                 case XK_Down:           key_pressed = DOWN;     break;
1187                 case XK_Left:           key_pressed = LEFT;     break;
1188                 case XK_Right:          key_pressed = RIGHT;    break;
1189                 case XK_Next:           key_pressed = PGDN;     break;
1190                 case XK_Prior:          key_pressed = PGUP;     break;
1191                 case XK_BackSpace:      key_pressed = BACKSPACE; break;
1192                 case XK_Escape:         key_pressed = ESC;      break;
1193                 case XK_Tab:
1194                         if(shift_down())
1195                                 key_pressed = LEFTTAB;
1196                         else
1197                                 key_pressed = TAB;
1198                         break;
1199                 case XK_ISO_Left_Tab:   key_pressed = LEFTTAB;  break;
1200                 case XK_underscore:     key_pressed = '_';      break;
1201                 case XK_asciitilde:     key_pressed = '~';      break;
1202                 case XK_Delete:         key_pressed = DELETE;   break;
1203                 case XK_Home:           key_pressed = HOME;     break;
1204                 case XK_End:            key_pressed = END;      break;
1205
1206 // number pad
1207                 case XK_KP_Enter:       key_pressed = KPENTER;  break;
1208                 case XK_KP_Add:         key_pressed = KPPLUS;   break;
1209                 case XK_KP_Subtract:    key_pressed = KPMINUS;  break;
1210                 case XK_KP_Multiply:    key_pressed = KPSTAR;   break;
1211                 case XK_KP_Divide:      key_pressed = KPSLASH;  break;
1212                 case XK_KP_1:
1213                 case XK_KP_End:         key_pressed = KP1;      break;
1214                 case XK_KP_2:
1215                 case XK_KP_Down:        key_pressed = KP2;      break;
1216                 case XK_KP_3:
1217                 case XK_KP_Page_Down:   key_pressed = KP3;      break;
1218                 case XK_KP_4:
1219                 case XK_KP_Left:        key_pressed = KP4;      break;
1220                 case XK_KP_5:
1221                 case XK_KP_Begin:       key_pressed = KP5;      break;
1222                 case XK_KP_6:
1223                 case XK_KP_Right:       key_pressed = KP6;      break;
1224                 case XK_KP_7:
1225                 case XK_KP_Home:        key_pressed = KP7;      break;
1226                 case XK_KP_8:
1227                 case XK_KP_Up:          key_pressed = KP8;      break;
1228                 case XK_KP_9:
1229                 case XK_KP_Page_Up:     key_pressed = KP9;      break;
1230                 case XK_KP_0:
1231                 case XK_KP_Insert:      key_pressed = KPINS;    break;
1232                 case XK_KP_Decimal:
1233                 case XK_KP_Delete:      key_pressed = KPDEL;    break;
1234                 case XK_Menu:           key_pressed = KPMENU;   break;  /* menu */
1235 // remote control
1236 // above        case XK_KP_Enter:       key_pressed = KPENTER;  break;  /* check */
1237                 case XF86XK_MenuKB:     key_pressed = KPMENU;   break;  /* menu */
1238 // intercepted  case XF86XK_PowerDown: key_pressed = KPPOWER;   break;  /* Power */
1239                 case XF86XK_Launch1:    key_pressed = KPTV;     break;  /* TV */
1240                 case XF86XK_Launch2:    key_pressed = KPDVD;    break;  /* DVD */
1241 // intercepted  case XF86XK_WWW:        key_pressed = KPWWEB;   break;  /* WEB */
1242                 case XF86XK_Launch3:    key_pressed = KPBOOK;   break;  /* book */
1243                 case XF86XK_Launch4:    key_pressed = KPHAND;   break;  /* hand */
1244                 case XF86XK_Reply:      key_pressed = KPTMR;    break;  /* timer */
1245                 case SunXK_Front:       key_pressed = KPMAXW;   break;  /* max */
1246 // above        case XK_Left:           key_pressed = LEFT;     break;  /* left */
1247 // above        case XK_Right:          key_pressed = RIGHT;    break;  /* right */
1248 // above        case XK_Down:           key_pressed = DOWN;     break;  /* down */
1249 // above        case XK_Up:             key_pressed = UP;       break;  /* up */
1250 // above        case XK_SPACE:          key_pressed = KPSPACE;  break;  /* ok */
1251 // intercepted  case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU;     break;  /* VOL + */
1252 // intercepted  case XF86XK_AudioMute: key_pressed = KPMUTE;    break;  /* MUTE */
1253 // intercepted  case XF86XK_AudioLowerVolume: key_pressed = KPVOLD;     break;  /* VOL - */
1254                 case XF86XK_ScrollUp:   key_pressed = KPCHUP;   break;  /* CH + */
1255                 case XF86XK_ScrollDown: key_pressed = KPCHDN;   break;  /* CH - */
1256                 case XF86XK_AudioRecord: key_pressed = KPRECD;  break;  /* ( o) red */
1257                 case XF86XK_Forward:    key_pressed = KPPLAY;   break;  /* ( >) */
1258                 case XK_Redo:           key_pressed = KPFWRD;   break;  /* (>>) */
1259                 case XF86XK_Back:       key_pressed = KPBACK;   break;  /* (<<) */
1260                 case XK_Cancel:         key_pressed = KPSTOP;   break;  /* ([]) */
1261                 case XK_Pause:          key_pressed = KPAUSE;   break;  /* ('') */
1262
1263                 default:
1264                         key_pressed = keysym & 0xff;
1265 #ifdef X_HAVE_UTF8_STRING
1266 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1267                         keys_return[1] = 0;
1268 #endif
1269                         break;
1270                 }
1271 #ifdef X_HAVE_UTF8_STRING
1272                 }
1273                 key_pressed_utf8 = keys_return;
1274 #endif
1275
1276
1277                 result = 0;
1278                 if( top_level == this )
1279                         result = BC_KeyboardHandler::run_listeners(this);
1280
1281 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1282                 if( !result )
1283                         result = dispatch_keypress_event();
1284 // Handle some default keypresses
1285                 if(!result)
1286                 {
1287                         if(key_pressed == 'w' ||
1288                                 key_pressed == 'W')
1289                         {
1290                                 close_event();
1291                         }
1292                 }
1293                 break;
1294
1295         case KeyRelease:
1296                 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1297                 dispatch_keyrelease_event();
1298 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1299 // keysym, event->xkey.state);
1300                 break;
1301
1302         case LeaveNotify:
1303                 event_win = event->xany.window;
1304                 dispatch_cursor_leave();
1305                 break;
1306
1307         case EnterNotify:
1308                 event_win = event->xany.window;
1309                 cursor_x = event->xcrossing.x;
1310                 cursor_y = event->xcrossing.y;
1311                 dispatch_cursor_enter();
1312                 break;
1313         default:
1314                 break;
1315         }
1316 //printf("100 %s %p %d\n", title, event, event->type);
1317 //if(event->type != ClientMessage) dump();
1318
1319 #ifndef SINGLE_THREAD
1320         unlock_window();
1321         if(event) delete event;
1322 #else
1323 //      if(done) completion_lock->unlock();
1324 #endif
1325
1326 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1327         return 0;
1328 }
1329
1330 int BC_WindowBase::dispatch_expose_event()
1331 {
1332         int result = 0;
1333         for(int i = 0; i < subwindows->total && !result; i++)
1334         {
1335                 result = subwindows->values[i]->dispatch_expose_event();
1336         }
1337
1338 // Propagate to user
1339         if(!result) expose_event();
1340         return result;
1341 }
1342
1343 int BC_WindowBase::dispatch_resize_event(int w, int h)
1344 {
1345 // Can't store new w and h until the event is handles
1346 // because bcfilebox depends on the old w and h to
1347 // reposition widgets.
1348         if(window_type == MAIN_WINDOW)
1349         {
1350                 resize_events = 0;
1351                 delete pixmap;
1352                 pixmap = new BC_Pixmap(this, w, h);
1353
1354                 clear_box(0, 0, w, h);
1355         }
1356
1357 // Propagate to subwindows
1358         for(int i = 0; i < subwindows->total; i++)
1359         {
1360                 subwindows->values[i]->dispatch_resize_event(w, h);
1361         }
1362
1363 // Propagate to user
1364         resize_event(w, h);
1365
1366         if(window_type == MAIN_WINDOW)
1367         {
1368                 this->w = w;
1369                 this->h = h;
1370         }
1371         return 0;
1372 }
1373
1374 int BC_WindowBase::dispatch_translation_event()
1375 {
1376         translation_events = 0;
1377         if(window_type == MAIN_WINDOW)
1378         {
1379                 prev_x = x;
1380                 prev_y = y;
1381                 x = last_translate_x;
1382                 y = last_translate_y;
1383 // Correct for window manager offsets
1384                 x -= x_correction;
1385                 y -= y_correction;
1386         }
1387
1388         for(int i = 0; i < subwindows->total; i++)
1389         {
1390                 subwindows->values[i]->dispatch_translation_event();
1391         }
1392
1393         translation_event();
1394         return 0;
1395 }
1396
1397 int BC_WindowBase::dispatch_motion_event()
1398 {
1399         int result = 0;
1400         unhide_cursor();
1401
1402         if(top_level == this)
1403         {
1404                 motion_events = 0;
1405                 event_win = last_motion_win;
1406                 get_key_masks(last_motion_state);
1407
1408 // Test for grab
1409                 if(get_button_down() && !active_menubar && !active_popup_menu)
1410                 {
1411                         if(!result)
1412                         {
1413                                 cursor_x = last_motion_x;
1414                                 cursor_y = last_motion_y;
1415                                 result = dispatch_drag_motion();
1416                         }
1417
1418                         if(!result &&
1419                                 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1420                                 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1421                         {
1422                                 cursor_x = drag_x;
1423                                 cursor_y = drag_y;
1424
1425                                 result = dispatch_drag_start();
1426                         }
1427                 }
1428
1429                 cursor_x = last_motion_x;
1430                 cursor_y = last_motion_y;
1431
1432 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1433 // __LINE__,
1434 // active_menubar,
1435 // active_popup_menu,
1436 // active_subwindow);
1437
1438                 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1439                 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1440                 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1441         }
1442
1443 // Dispatch in stacking order
1444         for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1445         {
1446                 result = subwindows->values[i]->dispatch_motion_event();
1447         }
1448
1449         if(!result) result = cursor_motion_event();    // give to user
1450         return result;
1451 }
1452
1453 int BC_WindowBase::dispatch_keypress_event()
1454 {
1455         int result = 0;
1456         if(top_level == this)
1457         {
1458                 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1459         }
1460
1461         for(int i = 0; i < subwindows->total && !result; i++)
1462         {
1463                 result = subwindows->values[i]->dispatch_keypress_event();
1464         }
1465
1466         if(!result) result = keypress_event();
1467
1468         return result;
1469 }
1470
1471 int BC_WindowBase::dispatch_keyrelease_event()
1472 {
1473         int result = 0;
1474         if(top_level == this)
1475         {
1476                 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1477         }
1478
1479         for(int i = 0; i < subwindows->total && !result; i++)
1480         {
1481                 result = subwindows->values[i]->dispatch_keyrelease_event();
1482         }
1483
1484         if(!result) result = keyrelease_event();
1485
1486         return result;
1487 }
1488
1489 int BC_WindowBase::dispatch_focus_in()
1490 {
1491         for(int i = 0; i < subwindows->total; i++)
1492         {
1493                 subwindows->values[i]->dispatch_focus_in();
1494         }
1495
1496         focus_in_event();
1497
1498         return 0;
1499 }
1500
1501 int BC_WindowBase::dispatch_focus_out()
1502 {
1503         for(int i = 0; i < subwindows->total; i++)
1504         {
1505                 subwindows->values[i]->dispatch_focus_out();
1506         }
1507
1508         focus_out_event();
1509
1510         return 0;
1511 }
1512
1513 int BC_WindowBase::get_has_focus()
1514 {
1515         return top_level->has_focus;
1516 }
1517
1518 int BC_WindowBase::get_deleting()
1519 {
1520         if(is_deleting) return 1;
1521         if(parent_window && parent_window->get_deleting()) return 1;
1522         return 0;
1523 }
1524
1525 int BC_WindowBase::dispatch_button_press()
1526 {
1527         int result = 0;
1528
1529
1530         if(top_level == this)
1531         {
1532                 if(active_menubar) result = active_menubar->dispatch_button_press();
1533                 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1534                 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1535         }
1536
1537         for(int i = 0; i < subwindows->total && !result; i++)
1538         {
1539                 result = subwindows->values[i]->dispatch_button_press();
1540         }
1541
1542         if(!result) result = button_press_event();
1543
1544
1545         return result;
1546 }
1547
1548 int BC_WindowBase::dispatch_button_release()
1549 {
1550         int result = 0;
1551         if(top_level == this)
1552         {
1553                 if(active_menubar) result = active_menubar->dispatch_button_release();
1554                 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1555                 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1556                 if(!result && button_number != 4 && button_number != 5)
1557                         result = dispatch_drag_stop();
1558         }
1559
1560         for(int i = 0; i < subwindows->total && !result; i++)
1561         {
1562                 result = subwindows->values[i]->dispatch_button_release();
1563         }
1564
1565         if(!result)
1566         {
1567                 result = button_release_event();
1568         }
1569
1570         return result;
1571 }
1572
1573
1574 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1575 {
1576
1577 // all repeat event handlers get called and decide based on activity and duration
1578 // whether to respond
1579         for(int i = 0; i < subwindows->total; i++)
1580         {
1581                 subwindows->values[i]->dispatch_repeat_event(duration);
1582         }
1583
1584
1585         repeat_event(duration);
1586
1587
1588
1589 // Unlock next repeat signal
1590         if(window_type == MAIN_WINDOW)
1591         {
1592 #ifdef SINGLE_THREAD
1593                 BC_Display::display_global->unlock_repeaters(duration);
1594 #else
1595                 for(int i = 0; i < repeaters.total; i++)
1596                 {
1597                         if(repeaters.values[i]->delay == duration)
1598                         {
1599                                 repeaters.values[i]->repeat_lock->unlock();
1600                         }
1601                 }
1602 #endif
1603         }
1604         return 0;
1605 }
1606
1607 void BC_WindowBase::unhide_cursor()
1608 {
1609         if(is_transparent)
1610         {
1611                 is_transparent = 0;
1612                 if(top_level->is_hourglass)
1613                         set_cursor(HOURGLASS_CURSOR, 1, 0);
1614                 else
1615                         set_cursor(current_cursor, 1, 0);
1616         }
1617         cursor_timer->update();
1618 }
1619
1620
1621 void BC_WindowBase::update_video_cursor()
1622 {
1623         if(video_on && !is_transparent)
1624         {
1625                 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1626                 {
1627                         is_transparent = 1;
1628                         set_cursor(TRANSPARENT_CURSOR, 1, 1);
1629                         cursor_timer->update();
1630                 }
1631         }
1632         else
1633         {
1634                 cursor_timer->update();
1635         }
1636 }
1637
1638
1639 int BC_WindowBase::dispatch_cursor_leave()
1640 {
1641         unhide_cursor();
1642
1643         for(int i = 0; i < subwindows->total; i++)
1644         {
1645                 subwindows->values[i]->dispatch_cursor_leave();
1646         }
1647
1648         cursor_leave_event();
1649         return 0;
1650 }
1651
1652 int BC_WindowBase::dispatch_cursor_enter()
1653 {
1654         int result = 0;
1655
1656         unhide_cursor();
1657
1658         if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1659         if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1660         if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1661
1662         for(int i = 0; !result && i < subwindows->total; i++)
1663         {
1664                 result = subwindows->values[i]->dispatch_cursor_enter();
1665         }
1666
1667         if(!result) result = cursor_enter_event();
1668         return result;
1669 }
1670
1671 int BC_WindowBase::cursor_enter_event()
1672 {
1673         return 0;
1674 }
1675
1676 int BC_WindowBase::cursor_leave_event()
1677 {
1678         return 0;
1679 }
1680
1681 int BC_WindowBase::close_event()
1682 {
1683         set_done(1);
1684         return 1;
1685 }
1686
1687 int BC_WindowBase::dispatch_drag_start()
1688 {
1689         int result = 0;
1690         if(active_menubar) result = active_menubar->dispatch_drag_start();
1691         if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1692         if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1693
1694         for(int i = 0; i < subwindows->total && !result; i++)
1695         {
1696                 result = subwindows->values[i]->dispatch_drag_start();
1697         }
1698
1699         if(!result) result = is_dragging = drag_start_event();
1700         return result;
1701 }
1702
1703 int BC_WindowBase::dispatch_drag_stop()
1704 {
1705         int result = 0;
1706
1707         for(int i = 0; i < subwindows->total && !result; i++)
1708         {
1709                 result = subwindows->values[i]->dispatch_drag_stop();
1710         }
1711
1712         if(is_dragging && !result)
1713         {
1714                 drag_stop_event();
1715                 is_dragging = 0;
1716                 result = 1;
1717         }
1718
1719         return result;
1720 }
1721
1722 int BC_WindowBase::dispatch_drag_motion()
1723 {
1724         int result = 0;
1725         for(int i = 0; i < subwindows->total && !result; i++)
1726         {
1727                 result = subwindows->values[i]->dispatch_drag_motion();
1728         }
1729
1730         if(is_dragging && !result)
1731         {
1732                 drag_motion_event();
1733                 result = 1;
1734         }
1735
1736         return result;
1737 }
1738
1739
1740
1741
1742
1743 int BC_WindowBase::show_tooltip(int w, int h)
1744 {
1745         Window tempwin;
1746
1747         if(tooltip_text && !tooltip_on &&
1748                 (force_tooltip || get_resources()->tooltips_enabled))
1749         {
1750                 int x, y;
1751                 top_level->hide_tooltip();
1752
1753                 tooltip_on = 1;
1754                 if(w < 0)
1755                         w = get_text_width(MEDIUMFONT, tooltip_text);
1756
1757                 if(h < 0)
1758                         h = get_text_height(MEDIUMFONT, tooltip_text);
1759
1760                 w += TOOLTIP_MARGIN * 2;
1761                 h += TOOLTIP_MARGIN * 2;
1762
1763                 XTranslateCoordinates(top_level->display, win,
1764                                 top_level->rootwin, get_w(), get_h(),
1765                                 &x, &y, &tempwin);
1766                 // keep the tip inside the window/display
1767                 int top_x = top_level->get_x();
1768                 if( x < top_x ) x = top_x;
1769                 int top_w = top_x + top_level->get_w();
1770                 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1771                 if( top_w < lmt_w ) lmt_w = top_w;
1772                 if( x+w > lmt_w ) x = lmt_w-w;
1773                 if( x < 0 ) x = 0;
1774                 int top_y = top_level->get_y();
1775                 if( y < top_y ) y = top_y;
1776                 int top_h = top_y + top_level->get_h();
1777                 int lmt_h = top_level->get_root_h(0);
1778                 if( top_h < lmt_h ) lmt_h = top_h;
1779                 if( y+h > lmt_h ) y = lmt_h-h;
1780                 if( y < 0 ) y = 0;
1781                 int abs_x, abs_y, win_x, win_y;
1782                 unsigned int temp_mask;
1783                 Window temp_win;
1784                 XQueryPointer(top_level->display, top_level->win,
1785                         &temp_win, &temp_win, &abs_x, &abs_y,
1786                         &win_x, &win_y, &temp_mask);
1787                 // check for cursor inside popup
1788                 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1789                 {
1790                         if( x-abs_x < y-abs_y )
1791                                 x = abs_x+1;
1792                         else
1793                                 y = abs_y+1;
1794                 }
1795                 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1796                                         get_resources()->tooltip_bg_color);
1797
1798                 draw_tooltip();
1799                 tooltip_popup->set_font(MEDIUMFONT);
1800                 tooltip_popup->flash();
1801                 tooltip_popup->flush();
1802         }
1803         return 0;
1804 }
1805
1806 int BC_WindowBase::hide_tooltip()
1807 {
1808         if(subwindows)
1809                 for(int i = 0; i < subwindows->total; i++)
1810                 {
1811                         subwindows->values[i]->hide_tooltip();
1812                 }
1813
1814         if(tooltip_on)
1815         {
1816                 tooltip_on = 0;
1817                 delete tooltip_popup;
1818                 tooltip_popup = 0;
1819         }
1820         return 0;
1821 }
1822
1823 const char *BC_WindowBase::get_tooltip()
1824 {
1825         return tooltip_text;
1826 }
1827
1828 int BC_WindowBase::set_tooltip(const char *text)
1829 {
1830         tooltip_text = text;
1831
1832 // Update existing tooltip if it is visible
1833         if(tooltip_on)
1834         {
1835                 draw_tooltip();
1836                 tooltip_popup->flash();
1837         }
1838         return 0;
1839 }
1840
1841 // signal the event handler to repeat
1842 int BC_WindowBase::set_repeat(int64_t duration)
1843 {
1844         if(duration <= 0)
1845         {
1846                 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1847                 return 0;
1848         }
1849         if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1850
1851 #ifdef SINGLE_THREAD
1852         BC_Display::display_global->set_repeat(this, duration);
1853 #else
1854 // test repeater database for duplicates
1855         for(int i = 0; i < repeaters.total; i++)
1856         {
1857 // Already exists
1858                 if(repeaters.values[i]->delay == duration)
1859                 {
1860                         repeaters.values[i]->start_repeating(this);
1861                         return 0;
1862                 }
1863         }
1864
1865         BC_Repeater *repeater = new BC_Repeater(this, duration);
1866         repeater->initialize();
1867         repeaters.append(repeater);
1868         repeater->start_repeating();
1869 #endif
1870         return 0;
1871 }
1872
1873 int BC_WindowBase::unset_repeat(int64_t duration)
1874 {
1875         if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1876
1877 #ifdef SINGLE_THREAD
1878         BC_Display::display_global->unset_repeat(this, duration);
1879 #else
1880         for(int i = 0; i < repeaters.total; i++)
1881         {
1882                 if(repeaters.values[i]->delay == duration)
1883                 {
1884                         repeaters.values[i]->stop_repeating();
1885                 }
1886         }
1887 #endif
1888         return 0;
1889 }
1890
1891
1892 int BC_WindowBase::unset_all_repeaters()
1893 {
1894 #ifdef SINGLE_THREAD
1895         BC_Display::display_global->unset_all_repeaters(this);
1896 #else
1897         for(int i = 0; i < repeaters.total; i++)
1898         {
1899                 repeaters.values[i]->stop_repeating();
1900         }
1901         repeaters.remove_all_objects();
1902 #endif
1903         return 0;
1904 }
1905
1906 // long BC_WindowBase::get_repeat_id()
1907 // {
1908 //      return top_level->next_repeat_id++;
1909 // }
1910
1911
1912 #ifndef SINGLE_THREAD
1913 int BC_WindowBase::arm_repeat(int64_t duration)
1914 {
1915         XEvent *event = new XEvent;
1916         XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1917         ptr->type = ClientMessage;
1918         ptr->message_type = RepeaterXAtom;
1919         ptr->format = 32;
1920         ptr->data.l[0] = duration;
1921
1922 // Couldn't use XSendEvent since it locked up randomly.
1923         put_event(event);
1924         return 0;
1925 }
1926 #endif
1927
1928 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1929 {
1930         return 0;
1931 }
1932
1933 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1934 {
1935         XEvent *myevent = new XEvent;
1936         XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1937         ptr->type = ClientMessage;
1938         ptr->message_type = event->message_type;
1939         ptr->format = event->format;
1940         ptr->data.l[0] = event->data.l[0];
1941         ptr->data.l[1] = event->data.l[1];
1942         ptr->data.l[2] = event->data.l[2];
1943         ptr->data.l[3] = event->data.l[3];
1944         ptr->data.l[4] = event->data.l[4];
1945
1946         put_event(myevent);
1947         return 0;
1948 }
1949
1950
1951
1952 Atom BC_WindowBase::create_xatom(const char *atom_name)
1953 {
1954         return XInternAtom(display, atom_name, False);
1955 }
1956
1957 int BC_WindowBase::get_atoms()
1958 {
1959         SetDoneXAtom =  create_xatom("BC_REPEAT_EVENT");
1960         RepeaterXAtom = create_xatom("BC_CLOSE_EVENT");
1961         DelWinXAtom = create_xatom("WM_DELETE_WINDOW");
1962         ProtoXAtom = create_xatom("WM_PROTOCOLS");
1963         SetDoneXAtom =  XInternAtom(display, "BC_REPEAT_EVENT", False);
1964         RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1965         DelWinXAtom =   XInternAtom(display, "WM_DELETE_WINDOW", False);
1966         if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
1967                 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
1968                         PropModeReplace, (unsigned char *)&DelWinXAtom, True);
1969         return 0;
1970
1971 }
1972
1973
1974 void BC_WindowBase::init_cursors()
1975 {
1976         arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
1977         cross_cursor = XCreateFontCursor(display, XC_crosshair);
1978         ibeam_cursor = XCreateFontCursor(display, XC_xterm);
1979         vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
1980         hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
1981         move_cursor = XCreateFontCursor(display, XC_fleur);
1982         left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
1983         right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
1984         upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
1985         upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
1986         upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
1987         downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
1988         downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
1989         hourglass_cursor = XCreateFontCursor(display, XC_watch);
1990
1991
1992         char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
1993         Colormap colormap = DefaultColormap(display, screen);
1994         Pixmap pixmap_bottom = XCreateBitmapFromData(display,
1995                 rootwin, cursor_data, 8, 8);
1996         XColor black, dummy;
1997         XAllocNamedColor(display, colormap, "black", &black, &dummy);
1998         transparent_cursor = XCreatePixmapCursor(display,
1999                 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2000 //      XDefineCursor(display, win, transparent_cursor);
2001         XFreePixmap(display, pixmap_bottom);
2002 }
2003
2004 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2005 {
2006         int color_model = BC_TRANSPARENCY;
2007         switch(depth)
2008         {
2009                 case 8:
2010                         color_model = BC_RGB8;
2011                         break;
2012                 case 16:
2013                         color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2014                         break;
2015                 case 24:
2016                         color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2017                         break;
2018                 case 32:
2019                         color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2020                         break;
2021         }
2022         return color_model;
2023 }
2024
2025 int BC_WindowBase::init_colors()
2026 {
2027         total_colors = 0;
2028         current_color_value = current_color_pixel = 0;
2029
2030 // Get the real depth
2031         char *data = 0;
2032         XImage *ximage;
2033         ximage = XCreateImage(top_level->display,
2034                                         top_level->vis,
2035                                         top_level->default_depth,
2036                                         ZPixmap,
2037                                         0,
2038                                         data,
2039                                         16,
2040                                         16,
2041                                         8,
2042                                         0);
2043         bits_per_pixel = ximage->bits_per_pixel;
2044         XDestroyImage(ximage);
2045
2046         color_model = evaluate_color_model(client_byte_order,
2047                 server_byte_order,
2048                 bits_per_pixel);
2049 // Get the color model
2050         switch(color_model)
2051         {
2052                 case BC_RGB8:
2053                         if(private_color)
2054                         {
2055                                 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2056                                 create_private_colors();
2057                         }
2058                         else
2059                         {
2060                                 cmap = DefaultColormap(display, screen);
2061                                 create_shared_colors();
2062                         }
2063
2064                         allocate_color_table();
2065                         break;
2066
2067                 default:
2068                         //cmap = DefaultColormap(display, screen);
2069                         cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2070                         break;
2071         }
2072         return 0;
2073 }
2074
2075 int BC_WindowBase::create_private_colors()
2076 {
2077         int color;
2078         total_colors = 256;
2079
2080         for(int i = 0; i < 255; i++)
2081         {
2082                 color = (i & 0xc0) << 16;
2083                 color += (i & 0x38) << 10;
2084                 color += (i & 0x7) << 5;
2085                 color_table[i][0] = color;
2086         }
2087         create_shared_colors();        // overwrite the necessary colors on the table
2088         return 0;
2089 }
2090
2091
2092 int BC_WindowBase::create_color(int color)
2093 {
2094         if(total_colors == 256)
2095         {
2096 // replace the closest match with an exact match
2097                 color_table[get_color_rgb8(color)][0] = color;
2098         }
2099         else
2100         {
2101 // add the color to the table
2102                 color_table[total_colors][0] = color;
2103                 total_colors++;
2104         }
2105         return 0;
2106 }
2107
2108 int BC_WindowBase::create_shared_colors()
2109 {
2110         create_color(BLACK);
2111         create_color(WHITE);
2112
2113         create_color(LTGREY);
2114         create_color(MEGREY);
2115         create_color(MDGREY);
2116         create_color(DKGREY);
2117
2118         create_color(LTCYAN);
2119         create_color(MECYAN);
2120         create_color(MDCYAN);
2121         create_color(DKCYAN);
2122
2123         create_color(LTGREEN);
2124         create_color(GREEN);
2125         create_color(DKGREEN);
2126
2127         create_color(LTPINK);
2128         create_color(PINK);
2129         create_color(RED);
2130
2131         create_color(LTBLUE);
2132         create_color(BLUE);
2133         create_color(DKBLUE);
2134
2135         create_color(LTYELLOW);
2136         create_color(MEYELLOW);
2137         create_color(MDYELLOW);
2138         create_color(DKYELLOW);
2139
2140         create_color(LTPURPLE);
2141         create_color(MEPURPLE);
2142         create_color(MDPURPLE);
2143         create_color(DKPURPLE);
2144
2145         create_color(FGGREY);
2146         create_color(MNBLUE);
2147         create_color(ORANGE);
2148         create_color(FTGREY);
2149
2150         return 0;
2151 }
2152
2153 int BC_WindowBase::allocate_color_table()
2154 {
2155         int red, green, blue, color;
2156         XColor col;
2157
2158         for(int i = 0; i < total_colors; i++)
2159         {
2160                 color = color_table[i][0];
2161                 red = (color & 0xFF0000) >> 16;
2162                 green = (color & 0x00FF00) >> 8;
2163                 blue = color & 0xFF;
2164
2165                 col.flags = DoRed | DoGreen | DoBlue;
2166                 col.red   = red<<8   | red;
2167                 col.green = green<<8 | green;
2168                 col.blue  = blue<<8  | blue;
2169
2170                 XAllocColor(display, cmap, &col);
2171                 color_table[i][1] = col.pixel;
2172         }
2173
2174         XInstallColormap(display, cmap);
2175         return 0;
2176 }
2177
2178 int BC_WindowBase::init_window_shape()
2179 {
2180         if(bg_pixmap && bg_pixmap->use_alpha())
2181         {
2182                 XShapeCombineMask(top_level->display,
2183                         this->win, ShapeBounding, 0, 0,
2184                         bg_pixmap->get_alpha(), ShapeSet);
2185         }
2186         return 0;
2187 }
2188
2189
2190 int BC_WindowBase::init_gc()
2191 {
2192         unsigned long gcmask;
2193         gcmask = GCFont | GCGraphicsExposures;
2194
2195         XGCValues gcvalues;
2196         gcvalues.font = mediumfont->fid;        // set the font
2197         gcvalues.graphics_exposures = 0;        // prevent expose events for every redraw
2198         gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2199
2200 // gcmask = GCCapStyle | GCJoinStyle;
2201 // XGetGCValues(display, gc, gcmask, &gcvalues);
2202 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2203         return 0;
2204 }
2205
2206 int BC_WindowBase::init_fonts()
2207 {
2208         if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2209                 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2210                         smallfont = XLoadQueryFont(display, "fixed");
2211         if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2212                 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2213                         mediumfont = XLoadQueryFont(display, "fixed");
2214         if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2215                 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2216                         largefont = XLoadQueryFont(display, "fixed");
2217         if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2218                 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2219                         bigfont = XLoadQueryFont(display, "fixed");
2220
2221         init_xft();
2222         if(get_resources()->use_fontset)
2223         {
2224                 char **m, *d;
2225                 int n;
2226
2227 // FIXME: should check the m,d,n values
2228                 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2229                 if( !smallfontset )
2230                         smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2231                 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2232                 if( !mediumfontset )
2233                         mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2234                 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2235                 if( !largefontset )
2236                         largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2237                 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2238                 if( !bigfontset )
2239                         largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2240                 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2241                         curr_fontset = mediumfontset;
2242                         get_resources()->use_fontset = 1;
2243                 }
2244                 else {
2245                         curr_fontset = 0;
2246                         get_resources()->use_fontset = 0;
2247                 }
2248         }
2249
2250         return 0;
2251 }
2252
2253 void BC_WindowBase::init_xft()
2254 {
2255 #ifdef HAVE_XFT
2256         if(!(smallfont_xft =
2257                 (resources.small_font_xft[0] == '-' ?
2258                         XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2259                         XftFontOpenName(display, screen, resources.small_font_xft))) )
2260                 if(!(smallfont_xft =
2261                         XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2262                         smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2263         if(!(mediumfont_xft =
2264                 (resources.medium_font_xft[0] == '-' ?
2265                         XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2266                         XftFontOpenName(display, screen, resources.medium_font_xft))) )
2267                 if(!(mediumfont_xft =
2268                         XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2269                         mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2270         if(!(largefont_xft =
2271                 (resources.large_font_xft[0] == '-' ?
2272                         XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2273                         XftFontOpenXlfd(display, screen, resources.large_font_xft))) )
2274                 if(!(largefont_xft =
2275                         XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2276                         largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2277         if(!(bigfont_xft =
2278                 (resources.big_font_xft[0] == '-' ?
2279                         XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2280                         XftFontOpenName(display, screen, resources.big_font_xft))) )
2281                 if(!(bigfont_xft =
2282                         XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2283                         bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2284
2285 // Extension failed to locate fonts
2286         if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft) {
2287                 printf("BC_WindowBase::init_fonts: no xft fonts found %s=%p %s=%p %s=%p %s=%p\n",
2288                         resources.small_font_xft, smallfont_xft,
2289                         resources.medium_font_xft, mediumfont_xft,
2290                         resources.large_font_xft, largefont_xft,
2291                         resources.big_font_xft, bigfont_xft);
2292                 get_resources()->use_xft = 0;
2293                 exit(1);
2294         }
2295 #endif // HAVE_XFT
2296 }
2297
2298 void BC_WindowBase::init_im()
2299 {
2300         XIMStyles *xim_styles;
2301         XIMStyle xim_style;
2302
2303         if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2304         {
2305                 printf("BC_WindowBase::init_im: Could not open input method.\n");
2306                 exit(1);
2307         }
2308         if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2309                         xim_styles == NULL)
2310         {
2311                 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2312                 XCloseIM(input_method);
2313                 exit(1);
2314         }
2315
2316         xim_style = 0;
2317         for(int z = 0;  z < xim_styles->count_styles;  z++)
2318         {
2319                 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2320                 {
2321                         xim_style = xim_styles->supported_styles[z];
2322                         break;
2323                 }
2324         }
2325         XFree(xim_styles);
2326
2327         if(xim_style == 0)
2328         {
2329                 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2330                 XCloseIM(input_method);
2331                 exit(1);
2332         }
2333
2334         input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2335                 XNClientWindow, win, XNFocusWindow, win, NULL);
2336         if(!input_context)
2337         {
2338                 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2339                 XCloseIM(input_method);
2340                 exit(1);
2341         }
2342 }
2343
2344 void BC_WindowBase::finit_im()
2345 {
2346         if( input_context ) {
2347                 XDestroyIC(input_context);
2348                 input_context = 0;
2349         }
2350         if( input_method ) {
2351                 XCloseIM(input_method);
2352                 input_method = 0;
2353         }
2354 }
2355
2356
2357 int BC_WindowBase::get_color(int64_t color)
2358 {
2359 // return pixel of color
2360 // use this only for drawing subwindows not for bitmaps
2361          int i, test, difference;
2362
2363         switch(color_model)
2364         {
2365         case BC_RGB8:
2366                 if(private_color)
2367                         return get_color_rgb8(color);
2368 // test last color looked up
2369                 if(current_color_value == color)
2370                         return current_color_pixel;
2371
2372 // look up in table
2373                 current_color_value = color;
2374                 for(i = 0; i < total_colors; i++)
2375                 {
2376                         if(color_table[i][0] == color)
2377                         {
2378                                 current_color_pixel = color_table[i][1];
2379                                 return current_color_pixel;
2380                         }
2381                 }
2382
2383 // find nearest match
2384                 difference = 0xFFFFFF;
2385
2386                 for(i = 0; i < total_colors; i++)
2387                 {
2388                         test = abs((int)(color_table[i][0] - color));
2389
2390                         if(test < difference)
2391                         {
2392                                 current_color_pixel = color_table[i][1];
2393                                 difference = test;
2394                         }
2395                 }
2396                 return current_color_pixel;
2397
2398         case BC_RGB565:
2399                 return get_color_rgb16(color);
2400
2401         case BC_BGR565:
2402                 return get_color_bgr16(color);
2403
2404         case BC_RGB888:
2405         case BC_BGR888:
2406                 return client_byte_order == server_byte_order ?
2407                         color : get_color_bgr24(color);
2408
2409         default:
2410                 return color;
2411         }
2412         return 0;
2413 }
2414
2415 int BC_WindowBase::get_color_rgb8(int color)
2416 {
2417         int pixel;
2418
2419         pixel = (color & 0xc00000) >> 16;
2420         pixel += (color & 0xe000) >> 10;
2421         pixel += (color & 0xe0) >> 5;
2422         return pixel;
2423 }
2424
2425 int64_t BC_WindowBase::get_color_rgb16(int color)
2426 {
2427         int64_t result;
2428         result = (color & 0xf80000) >> 8;
2429         result += (color & 0xfc00) >> 5;
2430         result += (color & 0xf8) >> 3;
2431
2432         return result;
2433 }
2434
2435 int64_t BC_WindowBase::get_color_bgr16(int color)
2436 {
2437         int64_t result;
2438         result = (color & 0xf80000) >> 19;
2439         result += (color & 0xfc00) >> 5;
2440         result += (color & 0xf8) << 8;
2441
2442         return result;
2443 }
2444
2445 int64_t BC_WindowBase::get_color_bgr24(int color)
2446 {
2447         int64_t result;
2448         result = (color & 0xff) << 16;
2449         result += (color & 0xff00);
2450         result += (color & 0xff0000) >> 16;
2451         return result;
2452 }
2453
2454 void BC_WindowBase::start_video()
2455 {
2456         cursor_timer->update();
2457         video_on = 1;
2458 //      set_color(BLACK);
2459 //      draw_box(0, 0, get_w(), get_h());
2460 //      flash();
2461 }
2462
2463 void BC_WindowBase::stop_video()
2464 {
2465         video_on = 0;
2466         unhide_cursor();
2467 }
2468
2469
2470
2471 int64_t BC_WindowBase::get_color()
2472 {
2473         return top_level->current_color;
2474 }
2475
2476 void BC_WindowBase::set_color(int64_t color)
2477 {
2478         top_level->current_color = color;
2479         XSetForeground(top_level->display,
2480                 top_level->gc,
2481                 top_level->get_color(color));
2482 }
2483
2484 void BC_WindowBase::set_opaque()
2485 {
2486         XSetFunction(top_level->display, top_level->gc, GXcopy);
2487 }
2488
2489 void BC_WindowBase::set_inverse()
2490 {
2491         XSetFunction(top_level->display, top_level->gc, GXxor);
2492 }
2493
2494 void BC_WindowBase::set_line_width(int value)
2495 {
2496         this->line_width = value;
2497         XSetLineAttributes(top_level->display, top_level->gc, value,    /* line_width */
2498                 line_dashes == 0 ? LineSolid : LineOnOffDash,           /* line_style */
2499                 line_dashes == 0 ? CapRound : CapNotLast,               /* cap_style */
2500                 JoinMiter);                                             /* join_style */
2501
2502         if(line_dashes > 0) {
2503                 const char dashes = line_dashes;
2504                 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2505         }
2506
2507 // XGCValues gcvalues;
2508 // unsigned long gcmask;
2509 // gcmask = GCCapStyle | GCJoinStyle;
2510 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2511 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2512 }
2513
2514 void BC_WindowBase::set_line_dashes(int value)
2515 {
2516         line_dashes = value;
2517 // call XSetLineAttributes
2518         set_line_width(line_width);
2519 }
2520
2521
2522 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2523 {
2524         switch(cursor)
2525         {
2526                 case ARROW_CURSOR:         return top_level->arrow_cursor;                 break;
2527                 case CROSS_CURSOR:         return top_level->cross_cursor;
2528                 case IBEAM_CURSOR:         return top_level->ibeam_cursor;                 break;
2529                 case VSEPARATE_CURSOR:     return top_level->vseparate_cursor;             break;
2530                 case HSEPARATE_CURSOR:     return top_level->hseparate_cursor;             break;
2531                 case MOVE_CURSOR:              return top_level->move_cursor;                  break;
2532                 case LEFT_CURSOR:          return top_level->left_cursor;                  break;
2533                 case RIGHT_CURSOR:         return top_level->right_cursor;                 break;
2534                 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor;         break;
2535                 case UPLEFT_RESIZE:        return top_level->upleft_resize_cursor;         break;
2536                 case UPRIGHT_RESIZE:       return top_level->upright_resize_cursor;        break;
2537                 case DOWNLEFT_RESIZE:      return top_level->downleft_resize_cursor;       break;
2538                 case DOWNRIGHT_RESIZE:     return top_level->downright_resize_cursor;      break;
2539                 case HOURGLASS_CURSOR:     return top_level->hourglass_cursor;                 break;
2540                 case TRANSPARENT_CURSOR:   return top_level->transparent_cursor;               break;
2541         }
2542         return 0;
2543 }
2544
2545 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2546 {
2547 // inherit cursor from parent
2548         if(cursor < 0)
2549         {
2550                 XUndefineCursor(top_level->display, win);
2551                 current_cursor = cursor;
2552         }
2553         else
2554 // don't change cursor if overridden
2555         if((!top_level->is_hourglass && !is_transparent) ||
2556                 override)
2557         {
2558                 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2559                 if(flush) this->flush();
2560         }
2561
2562         if(!override) current_cursor = cursor;
2563 }
2564
2565 void BC_WindowBase::set_x_cursor(int cursor)
2566 {
2567         temp_cursor = XCreateFontCursor(top_level->display, cursor);
2568         XDefineCursor(top_level->display, win, temp_cursor);
2569         current_cursor = cursor;
2570         flush();
2571 }
2572
2573 int BC_WindowBase::get_cursor()
2574 {
2575         return current_cursor;
2576 }
2577
2578 void BC_WindowBase::start_hourglass()
2579 {
2580         top_level->start_hourglass_recursive();
2581         top_level->flush();
2582 }
2583
2584 void BC_WindowBase::stop_hourglass()
2585 {
2586         top_level->stop_hourglass_recursive();
2587         top_level->flush();
2588 }
2589
2590 void BC_WindowBase::start_hourglass_recursive()
2591 {
2592         if(this == top_level)
2593         {
2594                 hourglass_total++;
2595                 is_hourglass = 1;
2596         }
2597
2598         if(!is_transparent)
2599         {
2600                 set_cursor(HOURGLASS_CURSOR, 1, 0);
2601                 for(int i = 0; i < subwindows->total; i++)
2602                 {
2603                         subwindows->values[i]->start_hourglass_recursive();
2604                 }
2605         }
2606 }
2607
2608 void BC_WindowBase::stop_hourglass_recursive()
2609 {
2610         if(this == top_level)
2611         {
2612                 if(hourglass_total == 0) return;
2613                 top_level->hourglass_total--;
2614         }
2615
2616         if(!top_level->hourglass_total)
2617         {
2618                 top_level->is_hourglass = 0;
2619
2620 // Cause set_cursor to perform change
2621                 if(!is_transparent)
2622                         set_cursor(current_cursor, 1, 0);
2623
2624                 for(int i = 0; i < subwindows->total; i++)
2625                 {
2626                         subwindows->values[i]->stop_hourglass_recursive();
2627                 }
2628         }
2629 }
2630
2631
2632
2633
2634 XFontStruct* BC_WindowBase::get_font_struct(int font)
2635 {
2636 // Clear out unrelated flags
2637         if(font & BOLDFACE) font ^= BOLDFACE;
2638
2639         switch(font) {
2640                 case SMALLFONT:  return top_level->smallfont;  break;
2641                 case MEDIUMFONT: return top_level->mediumfont; break;
2642                 case LARGEFONT:  return top_level->largefont;  break;
2643                 case BIGFONT:    return top_level->bigfont;    break;
2644         }
2645         return 0;
2646 }
2647
2648 XFontSet BC_WindowBase::get_fontset(int font)
2649 {
2650         XFontSet fs = 0;
2651
2652         if(get_resources()->use_fontset)
2653         {
2654                 switch(font & 0xff) {
2655                         case SMALLFONT:  fs = top_level->smallfontset; break;
2656                         case MEDIUMFONT: fs = top_level->mediumfontset; break;
2657                         case LARGEFONT:  fs = top_level->largefontset; break;
2658                         case BIGFONT:    fs = top_level->bigfontset;   break;
2659                 }
2660         }
2661
2662         return fs;
2663 }
2664
2665 #ifdef HAVE_XFT
2666 XftFont* BC_WindowBase::get_xft_struct(int font)
2667 {
2668         switch(font) {
2669                 case SMALLFONT:    return (XftFont*)top_level->smallfont_xft;
2670                 case MEDIUMFONT:   return (XftFont*)top_level->mediumfont_xft;
2671                 case LARGEFONT:    return (XftFont*)top_level->largefont_xft;
2672                 case BIGFONT:      return (XftFont*)top_level->bigfont_xft;
2673                 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2674                 case SMALLFONT_3D:  return (XftFont*)top_level->bold_smallfont_xft;
2675                 case LARGEFONT_3D:  return (XftFont*)top_level->bold_largefont_xft;
2676         }
2677
2678         return 0;
2679 }
2680 #endif
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690 int BC_WindowBase::get_current_font()
2691 {
2692         return top_level->current_font;
2693 }
2694
2695 void BC_WindowBase::set_font(int font)
2696 {
2697         top_level->current_font = font;
2698
2699 #ifdef HAVE_XFT
2700         if(get_resources()->use_xft) {}
2701         else
2702 #endif
2703         if(get_resources()->use_fontset) {
2704                 set_fontset(font);
2705         }
2706
2707         if(get_font_struct(font))
2708         {
2709                 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2710         }
2711
2712         return;
2713 }
2714
2715 void BC_WindowBase::set_fontset(int font)
2716 {
2717         XFontSet fs = 0;
2718
2719         if(get_resources()->use_fontset) {
2720                 switch(font) {
2721                         case SMALLFONT:  fs = top_level->smallfontset; break;
2722                         case MEDIUMFONT: fs = top_level->mediumfontset; break;
2723                         case LARGEFONT:  fs = top_level->largefontset; break;
2724                         case BIGFONT:    fs = top_level->bigfontset;   break;
2725                 }
2726         }
2727
2728         curr_fontset = fs;
2729 }
2730
2731
2732 XFontSet BC_WindowBase::get_curr_fontset(void)
2733 {
2734         if(get_resources()->use_fontset)
2735                 return curr_fontset;
2736         return 0;
2737 }
2738
2739 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2740 {
2741 #ifdef HAVE_XFT
2742         if(get_resources()->use_xft && get_xft_struct(font))
2743         {
2744                 XGlyphInfo extents;
2745 #ifdef X_HAVE_UTF8_STRING
2746                 if(get_resources()->locale_utf8)
2747                 {
2748                         XftTextExtentsUtf8(top_level->display,
2749                                 get_xft_struct(font),
2750                                 (const XftChar8 *)text,
2751                                 length,
2752                                 &extents);
2753                 }
2754                 else
2755 #endif
2756                 {
2757                         XftTextExtents8(top_level->display,
2758                                 get_xft_struct(font),
2759                                 (const XftChar8 *)text,
2760                                 length,
2761                                 &extents);
2762                 }
2763                 return extents.xOff;
2764         }
2765         else
2766 #endif
2767         if(get_resources()->use_fontset && top_level->get_fontset(font))
2768                 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2769         else
2770         if(get_font_struct(font)) 
2771                 return XTextWidth(get_font_struct(font), text, length);
2772         else
2773         {
2774                 int w = 0;
2775                 switch(font)
2776                 {
2777                         case MEDIUM_7SEGMENT:
2778                                 return get_resources()->medium_7segment[0]->get_w() * length;
2779                                 break;
2780
2781                         default:
2782                                 return 0;
2783                 }
2784                 return w;
2785         }
2786 }
2787
2788 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2789 {
2790 //#ifdef HAVE_XFT
2791         XGlyphInfo extents;
2792
2793         XftTextExtents32(top_level->display,
2794                 get_xft_struct(font),
2795                 (const FcChar32*)text,
2796                 length,
2797                 &extents);
2798         return extents.xOff;
2799 //#endif
2800 }
2801
2802 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2803 {
2804         int i, j, w = 0, line_w = 0;
2805         if(length < 0) length = strlen(text);
2806
2807         for(i = 0, j = 0; i <= length; i++)
2808         {
2809                 line_w = 0;
2810                 if(text[i] == '\n')
2811                 {
2812                         line_w = get_single_text_width(font, &text[j], i - j);
2813                         j = i + 1;
2814                 }
2815                 else
2816                 if(text[i] == 0)
2817                 {
2818                         line_w = get_single_text_width(font, &text[j], length - j);
2819                 }
2820                 if(line_w > w) w = line_w;
2821         }
2822
2823         if(i > length && w == 0)
2824         {
2825                 w = get_single_text_width(font, text, length);
2826         }
2827
2828         return w;
2829 }
2830
2831 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2832 {
2833         int i, j, w = 0, line_w = 0;
2834
2835         if(length < 0) length = wcslen(text);
2836
2837         for(i = 0, j = 0; i <= length; i++)
2838         {
2839                 line_w = 0;
2840                 if(text[i] == '\n')
2841                 {
2842                         line_w = get_single_text_width(font, &text[j], i - j);
2843                         j = i + 1;
2844                 }
2845                 else
2846                 if(text[i] == 0)
2847                         line_w = get_single_text_width(font, &text[j], length - j);
2848
2849                 if(line_w > w) w = line_w;
2850         }
2851
2852         if(i > length && w == 0)
2853                 w = get_single_text_width(font, text, length);
2854
2855         return w;
2856 }
2857
2858 int BC_WindowBase::get_text_ascent(int font)
2859 {
2860 #ifdef HAVE_XFT
2861         XftFont *fstruct;
2862         if( (fstruct = get_xft_struct(font)) != 0 )
2863                 return fstruct->ascent;
2864 #endif
2865         if(get_resources()->use_fontset && top_level->get_fontset(font))
2866         {
2867                 XFontSetExtents *extents;
2868
2869                 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2870                 return -extents->max_logical_extent.y;
2871         }
2872
2873         if(get_font_struct(font))
2874                 return top_level->get_font_struct(font)->ascent;
2875
2876         switch(font) {
2877                 case MEDIUM_7SEGMENT:
2878                         return get_resources()->medium_7segment[0]->get_h();
2879         }
2880         return 0;
2881 }
2882
2883 int BC_WindowBase::get_text_descent(int font)
2884 {
2885 #ifdef HAVE_XFT
2886         XftFont *fstruct;
2887         if( (fstruct = get_xft_struct(font)) != 0 )
2888                 return fstruct->descent;
2889 #endif
2890         if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2891                 XFontSetExtents *extents;
2892                 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2893                 return (extents->max_logical_extent.height
2894                         + extents->max_logical_extent.y);
2895         }
2896
2897         if(get_font_struct(font))
2898                 return top_level->get_font_struct(font)->descent;
2899
2900         return 0;
2901 }
2902
2903 int BC_WindowBase::get_text_height(int font, const char *text)
2904 {
2905         int rowh;
2906 #ifdef HAVE_XFT
2907         XftFont *fstruct;
2908         if( (fstruct = get_xft_struct(font)) != 0 )
2909                 rowh = fstruct->height;
2910         else
2911 #endif
2912                 rowh = get_text_ascent(font) + get_text_descent(font);
2913
2914         if(!text) return rowh;
2915
2916 // Add height of lines
2917         int h = 0, i, length = strlen(text);
2918         for(i = 0; i <= length; i++)
2919         {
2920                 if(text[i] == '\n')
2921                         h++;
2922                 else
2923                 if(text[i] == 0)
2924                         h++;
2925         }
2926         return h * rowh;
2927 }
2928
2929 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2930 {
2931         if(color_model < 0) color_model = top_level->get_color_model();
2932         return new BC_Bitmap(top_level, w, h, color_model);
2933 }
2934
2935 void BC_WindowBase::init_wait()
2936 {
2937 #ifndef SINGLE_THREAD
2938         if(window_type != MAIN_WINDOW)
2939                 top_level->init_wait();
2940         init_lock->lock("BC_WindowBase::init_wait");
2941         init_lock->unlock();
2942 #endif
2943 }
2944
2945 int BC_WindowBase::accel_available(int color_model, int lock_it)
2946 {
2947         if(window_type != MAIN_WINDOW)
2948                 return top_level->accel_available(color_model, lock_it);
2949
2950         int result = 0;
2951
2952         if(lock_it) lock_window("BC_WindowBase::accel_available");
2953         switch(color_model)
2954         {
2955                 case BC_YUV420P:
2956                         result = grab_port_id(this, color_model);
2957                         if(result >= 0)
2958                         {
2959                                 xvideo_port_id = result;
2960                                 result = 1;
2961                         }
2962                         else
2963                                 result = 0;
2964                         break;
2965
2966                 case BC_YUV422P:
2967                         result = 0;
2968                         break;
2969
2970                 case BC_YUV422:
2971 //printf("BC_WindowBase::accel_available 1\n");
2972                         result = grab_port_id(this, color_model);
2973 //printf("BC_WindowBase::accel_available 2 %d\n", result);
2974                         if(result >= 0)
2975                         {
2976                                 xvideo_port_id = result;
2977                                 result = 1;
2978                         }
2979                         else
2980                                 result = 0;
2981 //printf("BC_WindowBase::accel_available 3 %d\n", xvideo_port_id);
2982                         break;
2983
2984                 default:
2985                         result = 0;
2986                         break;
2987         }
2988
2989         if(lock_it) unlock_window();
2990 //printf("BC_WindowBase::accel_available %d %d\n", color_model, result);
2991         return result;
2992 }
2993
2994
2995 int BC_WindowBase::grab_port_id(BC_WindowBase *window, int color_model)
2996 {
2997         int numFormats, i, j, k;
2998         unsigned int ver, rev, numAdapt, reqBase, eventBase, errorBase;
2999         XvAdaptorInfo *info;
3000         XvImageFormatValues *formats;
3001         int x_color_model;
3002
3003         if(!get_resources()->use_xvideo) return -1;
3004
3005 // Translate from color_model to X color model
3006         x_color_model = BC_CModels::bc_to_x(color_model);
3007
3008 // Only local server is fast enough.
3009         if(!resources.use_shm) return -1;
3010
3011 // XV extension is available
3012         if(Success != XvQueryExtension(window->display, &ver, &rev,
3013                                   &reqBase, &eventBase, &errorBase)) {
3014                 return -1;
3015         }
3016
3017 // XV adaptors are available
3018         XvQueryAdaptors(window->display,
3019                 DefaultRootWindow(window->display),
3020                 &numAdapt, &info);
3021
3022         if(!numAdapt) {
3023                 return -1;
3024         }
3025
3026 // Get adaptor with desired color model
3027         for(i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3028 /* adaptor supports XvImages */
3029                 if(info[i].type & XvImageMask)
3030                 {
3031                 formats = XvListImageFormats(window->display,
3032                                                         info[i].base_id,
3033                                                         &numFormats);
3034 // for(j = 0; j < numFormats; j++)
3035 //      printf("%08x\n", formats[j].id);
3036
3037                 int numPorts = info[i].num_ports;
3038                 for(j = 0; j < (int)numFormats && xvideo_port_id < 0; j++)
3039                 {
3040 /* this adaptor supports the desired format */
3041                                 if(formats[j].id == x_color_model)
3042                                 {
3043 /* Try to grab a port */
3044                                         for(k = 0; k < numPorts; k++)
3045                                         {
3046 /* Got a port */
3047                                                 if(Success == XvGrabPort(top_level->display,
3048                                                         info[i].base_id + k,
3049                                                         CurrentTime))
3050                                                 {
3051 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3052                                                         xvideo_port_id = info[i].base_id + k;
3053                                                         break;
3054                                                 }
3055                                         }
3056                                 }
3057                         }
3058                 if(formats) XFree(formats);
3059                 }
3060         }
3061
3062         XvFreeAdaptorInfo(info);
3063
3064         return xvideo_port_id;
3065 }
3066
3067
3068 int BC_WindowBase::show_window(int flush)
3069 {
3070         for(int i = 0; i < subwindows->size(); i++)
3071         {
3072                 subwindows->get(i)->show_window(0);
3073         }
3074
3075         XMapWindow(top_level->display, win);
3076         if(flush) XFlush(top_level->display);
3077 //      XSync(top_level->display, 0);
3078         hidden = 0;
3079         return 0;
3080 }
3081
3082 int BC_WindowBase::hide_window(int flush)
3083 {
3084         for(int i = 0; i < subwindows->size(); i++)
3085         {
3086                 subwindows->get(i)->hide_window(0);
3087         }
3088
3089         XUnmapWindow(top_level->display, win);
3090         if(flush) this->flush();
3091         hidden = 1;
3092         return 0;
3093 }
3094
3095 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3096 {
3097         subwindows->append((BC_SubWindow*)menu_bar);
3098
3099         menu_bar->parent_window = this;
3100         menu_bar->top_level = this->top_level;
3101         menu_bar->initialize();
3102         return menu_bar;
3103 }
3104
3105 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3106 {
3107 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3108         if(this != top_level) return top_level->add_popup(window);
3109         popups.append(window);
3110         return window;
3111 }
3112
3113 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3114 {
3115 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3116         if(this != top_level)
3117                 top_level->remove_popup(window);
3118         else
3119                 popups.remove(window);
3120 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3121 }
3122
3123
3124 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3125 {
3126         subwindows->append(subwindow);
3127
3128         if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3129
3130 // parent window must be set before the subwindow initialization
3131         subwindow->parent_window = this;
3132         subwindow->top_level = this->top_level;
3133
3134 // Execute derived initialization
3135         subwindow->initialize();
3136         return subwindow;
3137 }
3138
3139
3140 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3141 {
3142         return add_subwindow(subwindow);
3143 }
3144
3145 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3146 {
3147 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3148         set_opaque();
3149         XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3150         if(x >= 0)
3151         {
3152                 XClearArea(top_level->display, win, x, y, w, h, 0);
3153         }
3154         else
3155         {
3156                 XClearWindow(top_level->display, win);
3157         }
3158
3159         if(flush)
3160                 this->flush();
3161         return 0;
3162 }
3163
3164 int BC_WindowBase::flash(int flush)
3165 {
3166         flash(-1, -1, -1, -1, flush);
3167         return 0;
3168 }
3169
3170 void BC_WindowBase::flush()
3171 {
3172         //if(!get_window_lock())
3173         //      printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3174         // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3175         //   _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3176         int locked  = get_window_lock();
3177         if( !locked ) lock_window("BC_WindowBase::flush");
3178         XFlush(top_level->display);
3179         if( !locked ) unlock_window();
3180 }
3181
3182 void BC_WindowBase::sync_display()
3183 {
3184         int locked  = get_window_lock();
3185         if( !locked ) lock_window("BC_WindowBase::sync_display");
3186         XSync(top_level->display, False);
3187         if( !locked ) unlock_window();
3188 }
3189
3190 int BC_WindowBase::get_window_lock()
3191 {
3192 #ifdef SINGLE_THREAD
3193         return BC_Display::display_global->get_display_locked();
3194 #else
3195         return top_level->window_lock;
3196 #endif
3197 }
3198
3199 int BC_WindowBase::lock_window(const char *location)
3200 {
3201         if(top_level && top_level != this)
3202         {
3203                 top_level->lock_window(location);
3204         }
3205         else
3206         if(top_level)
3207         {
3208                 SET_LOCK(this, title, location);
3209 #ifdef SINGLE_THREAD
3210                 BC_Display::lock_display(location);
3211 #else
3212                 XLockDisplay(top_level->display);
3213                 top_level->display_lock_owner = pthread_self();
3214 #endif
3215                 SET_LOCK2
3216                 ++top_level->window_lock;
3217         }
3218         else
3219         {
3220                 printf("BC_WindowBase::lock_window top_level NULL\n");
3221         }
3222         return 0;
3223 }
3224
3225 int BC_WindowBase::unlock_window()
3226 {
3227         if(top_level && top_level != this)
3228         {
3229                 top_level->unlock_window();
3230         }
3231         else
3232         if(top_level)
3233         {
3234                 UNSET_LOCK(this);
3235
3236 //              if(!top_level->window_lock)
3237 //              {
3238 //                      printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3239 //                              __LINE__,
3240 //                              title);
3241 //              }
3242                 if( top_level->window_lock > 0 )
3243                         if( --top_level->window_lock == 0 )
3244                                 top_level->display_lock_owner = 0;
3245 #ifdef SINGLE_THREAD
3246                 BC_Display::unlock_display();
3247 #else
3248                 XUnlockDisplay(top_level->display);
3249 #endif
3250         }
3251         else
3252         {
3253                 printf("BC_WindowBase::unlock_window top_level NULL\n");
3254         }
3255         return 0;
3256 }
3257
3258 void BC_WindowBase::set_done(int return_value)
3259 {
3260         if(done_set) return;
3261         done_set = 1;
3262         if(window_type != MAIN_WINDOW)
3263                 top_level->set_done(return_value);
3264         else
3265         {
3266 #ifdef SINGLE_THREAD
3267                 this->return_value = return_value;
3268                 BC_Display::display_global->arm_completion(this);
3269                 completion_lock->unlock();
3270 #else // SINGLE_THREAD
3271                 init_wait();
3272                 if( !event_thread ) return;
3273                 XEvent *event = new XEvent;
3274                 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3275                 event->type = ClientMessage;
3276                 ptr->message_type = SetDoneXAtom;
3277                 ptr->format = 32;
3278                 this->return_value = return_value;
3279 // May lock up here because XSendEvent doesn't work too well 
3280 // asynchronous with XNextEvent.
3281 // This causes BC_WindowEvents to forward a copy of the event to run_window where 
3282 // it is deleted.
3283 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3284                 put_event(event);
3285         }
3286 #endif
3287 }
3288
3289 void BC_WindowBase::close(int return_value)
3290 {
3291         hide_window();  flush();
3292         set_done(return_value);
3293 }
3294
3295 int BC_WindowBase::get_w()
3296 {
3297         return w;
3298 }
3299
3300 int BC_WindowBase::get_h()
3301 {
3302         return h;
3303 }
3304
3305 int BC_WindowBase::get_x()
3306 {
3307         return x;
3308 }
3309
3310 int BC_WindowBase::get_y()
3311 {
3312         return y;
3313 }
3314
3315 // for get_root_w/h
3316 //   WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3317 //   this is the bounding box of all the screens
3318
3319 int BC_WindowBase::get_root_w(int lock_display)
3320 {
3321         if(lock_display) lock_window("BC_WindowBase::get_root_w");
3322         Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3323         int result = WidthOfScreen(def_screen);
3324         if(lock_display) unlock_window();
3325         return result;
3326 }
3327
3328 int BC_WindowBase::get_root_h(int lock_display)
3329 {
3330         if(lock_display) lock_window("BC_WindowBase::get_root_h");
3331         Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3332         int result = HeightOfScreen(def_screen);
3333         if(lock_display) unlock_window();
3334         return result;
3335 }
3336
3337 XineramaScreenInfo *
3338 BC_WindowBase::get_xinerama_info(int screen)
3339 {
3340         if( !xinerama_info || !xinerama_screens ) return 0;
3341         if( screen >= 0 ) {
3342                 for( int i=0; i<xinerama_screens; ++i )
3343                         if( xinerama_info[i].screen_number == screen )
3344                                 return &xinerama_info[i];
3345                 return 0;
3346         }
3347         int top_x = get_x(), top_y = get_y();
3348         for( int i=0; i<xinerama_screens; ++i ) {
3349                 int scr_y = top_y - xinerama_info[i].y_org;
3350                 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3351                 int scr_x = top_x - xinerama_info[i].x_org;
3352                 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3353                         return &xinerama_info[i];
3354         }
3355         return 0;
3356 }
3357
3358 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3359 {
3360         int result = -1;
3361         if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3362         XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3363         if( !info ) {
3364                 result = 0;
3365                 int root_w = get_root_w(0);
3366                 int root_h = get_root_h(0);
3367 // Shift X based on position of current window if dual head
3368                 if( (float)root_w/root_h > 1.8 ) {
3369                         root_w = get_screen_w(0, 0);
3370                         if( top_level->get_x() >= root_w )
3371                                 result = root_w;
3372                 }
3373         }
3374         else
3375                 result = info->x_org;
3376         if(lock_display) unlock_window();
3377         return result;
3378 }
3379
3380 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3381 {
3382         if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3383         XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3384         int result = !info ? 0 : info->y_org;
3385         if(lock_display) unlock_window();
3386         return result;
3387 }
3388
3389 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3390 {
3391         int result = -1;
3392         if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3393         XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3394         if( !info ) {
3395                 int width = get_root_w(0);
3396                 int height = get_root_h(0);
3397                 if( (float)width/height > 1.8 ) {
3398                         // If dual head, the screen width is > 16x9
3399                         // but we only want to fill one screen
3400                         // this code assumes the "big" screen is on the right
3401                         int scr_w0 = width / 2;
3402                         switch( height ) {
3403                         case 600:  scr_w0 = 800;   break;
3404                         case 720:  scr_w0 = 1280;  break;
3405                         case 1024: scr_w0 = 1280;  break;
3406                         case 1200: scr_w0 = 1600;  break;
3407                         case 1080: scr_w0 = 1920;  break;
3408                         }
3409                         int scr_w1 = width - scr_w0;
3410                         result = screen > 0 ? scr_w1 :
3411                                 screen == 0 ? scr_w0 :
3412                                 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3413                 }
3414                 else
3415                         result = width;
3416         }
3417         else
3418                 result = info->width;
3419         if(lock_display) unlock_window();
3420         return result;
3421 }
3422
3423 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3424 {
3425         if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3426         XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3427         int result = info ? info->height : get_root_h(0);
3428         if(lock_display) unlock_window();
3429         return result;
3430 }
3431
3432 // Bottom right corner
3433 int BC_WindowBase::get_x2()
3434 {
3435         return w + x;
3436 }
3437
3438 int BC_WindowBase::get_y2()
3439 {
3440         return y + h;
3441 }
3442
3443 int BC_WindowBase::get_video_on()
3444 {
3445         return video_on;
3446 }
3447
3448 int BC_WindowBase::get_hidden()
3449 {
3450         return top_level->hidden;
3451 }
3452
3453 int BC_WindowBase::cursor_inside()
3454 {
3455         return (top_level->cursor_x >= 0 &&
3456                         top_level->cursor_y >= 0 &&
3457                         top_level->cursor_x < w &&
3458                         top_level->cursor_y < h);
3459 }
3460
3461 BC_WindowBase* BC_WindowBase::get_top_level()
3462 {
3463         return top_level;
3464 }
3465
3466 BC_WindowBase* BC_WindowBase::get_parent()
3467 {
3468         return parent_window;
3469 }
3470
3471 int BC_WindowBase::get_color_model()
3472 {
3473         return top_level->color_model;
3474 }
3475
3476 BC_Resources* BC_WindowBase::get_resources()
3477 {
3478         return &BC_WindowBase::resources;
3479 }
3480
3481 BC_Synchronous* BC_WindowBase::get_synchronous()
3482 {
3483         return BC_WindowBase::resources.get_synchronous();
3484 }
3485
3486 int BC_WindowBase::get_bg_color()
3487 {
3488         return bg_color;
3489 }
3490
3491 void BC_WindowBase::set_bg_color(int color)
3492 {
3493         this->bg_color = color;
3494 }
3495
3496 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3497 {
3498         return bg_pixmap;
3499 }
3500
3501 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3502 {
3503         top_level->active_subwindow = subwindow;
3504 }
3505
3506 int BC_WindowBase::activate()
3507 {
3508         return 0;
3509 }
3510
3511 int BC_WindowBase::deactivate()
3512 {
3513         if(window_type == MAIN_WINDOW)
3514         {
3515                 if(top_level->active_menubar) top_level->active_menubar->deactivate();
3516                 if(top_level->active_popup_menu) top_level->active_popup_menu->deactivate();
3517                 if(top_level->active_subwindow) top_level->active_subwindow->deactivate();
3518
3519                 top_level->active_menubar = 0;
3520                 top_level->active_popup_menu = 0;
3521                 top_level->active_subwindow = 0;
3522         }
3523         return 0;
3524 }
3525
3526 int BC_WindowBase::cycle_textboxes(int amount)
3527 {
3528         int result = 0;
3529         BC_WindowBase *new_textbox = 0;
3530
3531         if(amount > 0)
3532         {
3533                 BC_WindowBase *first_textbox = 0;
3534                 find_next_textbox(&first_textbox, &new_textbox, result);
3535                 if(!new_textbox) new_textbox = first_textbox;
3536
3537         }
3538         else
3539         if(amount < 0)
3540         {
3541                 BC_WindowBase *last_textbox = 0;
3542                 find_prev_textbox(&last_textbox, &new_textbox, result);
3543                 if(!new_textbox) new_textbox = last_textbox;
3544
3545         }
3546
3547         if(new_textbox != active_subwindow)
3548         {
3549                 deactivate();
3550                 new_textbox->activate();
3551         }
3552
3553         return 0;
3554 }
3555
3556 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3557 {
3558 // Search subwindows for textbox
3559         for(int i = 0; i < subwindows->total && result < 2; i++)
3560         {
3561                 BC_WindowBase *test_subwindow = subwindows->values[i];
3562                 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3563         }
3564
3565         if(result < 2)
3566         {
3567                 if(uses_text())
3568                 {
3569                         if(!*first_textbox) *first_textbox = this;
3570
3571                         if(result < 1)
3572                         {
3573                                 if(top_level->active_subwindow == this)
3574                                         result++;
3575                         }
3576                         else
3577                         {
3578                                 result++;
3579                                 *next_textbox = this;
3580                         }
3581                 }
3582         }
3583         return 0;
3584 }
3585
3586 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3587 {
3588         if(result < 2)
3589         {
3590                 if(uses_text())
3591                 {
3592                         if(!*last_textbox) *last_textbox = this;
3593
3594                         if(result < 1)
3595                         {
3596                                 if(top_level->active_subwindow == this)
3597                                         result++;
3598                         }
3599                         else
3600                         {
3601                                 result++;
3602                                 *prev_textbox = this;
3603                         }
3604                 }
3605         }
3606
3607 // Search subwindows for textbox
3608         for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3609         {
3610                 BC_WindowBase *test_subwindow = subwindows->values[i];
3611                 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3612         }
3613         return 0;
3614 }
3615
3616 BC_Clipboard* BC_WindowBase::get_clipboard()
3617 {
3618 #ifdef SINGLE_THREAD
3619         return BC_Display::display_global->clipboard;
3620 #else
3621         return top_level->clipboard;
3622 #endif
3623 }
3624
3625 int BC_WindowBase::get_relative_cursor_x()
3626 {
3627         int abs_x, abs_y, x, y, win_x, win_y;
3628         unsigned int temp_mask;
3629         Window temp_win;
3630
3631         XQueryPointer(top_level->display, top_level->win,
3632            &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3633            &temp_mask);
3634
3635         XTranslateCoordinates(top_level->display, top_level->rootwin,
3636            win, abs_x, abs_y, &x, &y, &temp_win);
3637
3638         return x;
3639 }
3640
3641 int BC_WindowBase::get_relative_cursor_y()
3642 {
3643         int abs_x, abs_y, x, y, win_x, win_y;
3644         unsigned int temp_mask;
3645         Window temp_win;
3646
3647         XQueryPointer(top_level->display, top_level->win,
3648            &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3649            &temp_mask);
3650
3651         XTranslateCoordinates(top_level->display,
3652            top_level->rootwin, win, abs_x, abs_y, &x, &y, &temp_win);
3653
3654         return y;
3655 }
3656
3657 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3658 {
3659         int abs_x, abs_y, win_x, win_y;
3660         unsigned int temp_mask;
3661         Window temp_win;
3662
3663         if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
3664         XQueryPointer(top_level->display, top_level->win,
3665                 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3666                 &temp_mask);
3667         if(lock_window) this->unlock_window();
3668         return abs_x;
3669 }
3670
3671 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3672 {
3673         int abs_x, abs_y, win_x, win_y;
3674         unsigned int temp_mask;
3675         Window temp_win;
3676
3677         if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
3678         XQueryPointer(top_level->display, top_level->win,
3679                 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3680                 &temp_mask);
3681         if(lock_window) this->unlock_window();
3682         return abs_y;
3683 }
3684
3685 int BC_WindowBase::match_window(Window win)
3686 {
3687         if (this->win == win) return 1;
3688         int result = 0;
3689         for(int i = 0; i < subwindows->total; i++) {
3690                 result = subwindows->values[i]->match_window(win);
3691                 if (result) return result;
3692         }
3693         return 0;
3694
3695 }
3696
3697 int BC_WindowBase::get_cursor_over_window()
3698 {
3699         if(top_level != this) return top_level->get_cursor_over_window();
3700
3701         int abs_x, abs_y, win_x, win_y;
3702         unsigned int temp_mask;
3703         Window root_return, child_return;
3704
3705         int ret = XQueryPointer(display, win,
3706                 &root_return, &child_return, &abs_x, &abs_y,
3707                 &win_x, &win_y, &temp_mask);
3708 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3709 //  __LINE__, title, win, ret, child_return);
3710
3711         int result = !ret || child_return == None ? 0 :
3712                 match_window(child_return);
3713         return result;
3714 }
3715
3716 int BC_WindowBase::cursor_above()
3717 {
3718         int rx = get_relative_cursor_x();
3719         if( rx < 0 || rx >= get_w() ) return 0;
3720         int ry = get_relative_cursor_y();
3721         if( ry < 0 || ry >= get_h() ) return 0;
3722         return 1;
3723 }
3724
3725 int BC_WindowBase::get_drag_x()
3726 {
3727         return top_level->drag_x;
3728 }
3729
3730 int BC_WindowBase::get_drag_y()
3731 {
3732         return top_level->drag_y;
3733 }
3734
3735 int BC_WindowBase::get_cursor_x()
3736 {
3737         return top_level->cursor_x;
3738 }
3739
3740 int BC_WindowBase::get_cursor_y()
3741 {
3742         return top_level->cursor_y;
3743 }
3744
3745 int BC_WindowBase::dump_windows()
3746 {
3747         printf("\tBC_WindowBase::dump_windows window=%p win=%p\n",
3748                 this, (void*)this->win);
3749         for(int i = 0; i < subwindows->size(); i++)
3750                 subwindows->get(i)->dump_windows();
3751         for(int i = 0; i < popups.size(); i++)
3752                 printf("\tBC_WindowBase::dump_windows popup=%p win=%p\n",
3753                         popups.get(i), (void*)popups.get(i)->win);
3754         return 0;
3755 }
3756
3757 int BC_WindowBase::is_event_win()
3758 {
3759         return this->win == top_level->event_win;
3760 }
3761
3762 void BC_WindowBase::set_dragging(int value)
3763 {
3764         is_dragging = value;
3765 }
3766
3767 int BC_WindowBase::get_dragging()
3768 {
3769         return is_dragging;
3770 }
3771
3772 int BC_WindowBase::get_buttonpress()
3773 {
3774         return top_level->button_number;
3775 }
3776
3777 int BC_WindowBase::get_button_down()
3778 {
3779         return top_level->button_down;
3780 }
3781
3782 int BC_WindowBase::alt_down()
3783 {
3784         return top_level->alt_mask;
3785 }
3786
3787 int BC_WindowBase::shift_down()
3788 {
3789         return top_level->shift_mask;
3790 }
3791
3792 int BC_WindowBase::ctrl_down()
3793 {
3794         return top_level->ctrl_mask;
3795 }
3796
3797 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3798 {
3799         if(length)
3800                 *length = top_level->wkey_string_length;
3801         return top_level->wkey_string;
3802 }
3803
3804 #ifdef X_HAVE_UTF8_STRING
3805 char* BC_WindowBase::get_keypress_utf8()
3806 {
3807         return top_level->key_pressed_utf8;
3808 }
3809 #endif
3810
3811
3812 int BC_WindowBase::get_keypress()
3813 {
3814         return top_level->key_pressed;
3815 }
3816
3817 int BC_WindowBase::get_double_click()
3818 {
3819         return top_level->double_click;
3820 }
3821
3822 int BC_WindowBase::get_triple_click()
3823 {
3824         return top_level->triple_click;
3825 }
3826
3827 int BC_WindowBase::get_bgcolor()
3828 {
3829         return bg_color;
3830 }
3831
3832 int BC_WindowBase::resize_window(int w, int h)
3833 {
3834         if(this->w == w && this->h == h) return 0;
3835
3836         if(window_type == MAIN_WINDOW && !allow_resize)
3837         {
3838                 XSizeHints size_hints;
3839                 size_hints.flags = PSize | PMinSize | PMaxSize;
3840                 size_hints.width = w;
3841                 size_hints.height = h;
3842                 size_hints.min_width = w;
3843                 size_hints.max_width = w;
3844                 size_hints.min_height = h;
3845                 size_hints.max_height = h;
3846                 XSetNormalHints(top_level->display, win, &size_hints);
3847         }
3848         XResizeWindow(top_level->display, win, w, h);
3849
3850         this->w = w;
3851         this->h = h;
3852         delete pixmap;
3853         pixmap = new BC_Pixmap(this, w, h);
3854
3855 // Propagate to menubar
3856         for(int i = 0; i < subwindows->total; i++)
3857         {
3858                 subwindows->values[i]->dispatch_resize_event(w, h);
3859         }
3860
3861         draw_background(0, 0, w, h);
3862         if(top_level == this && get_resources()->recursive_resizing)
3863                 resize_history.append(new BC_ResizeCall(w, h));
3864         return 0;
3865 }
3866
3867 // The only way for resize events to be propagated is by updating the internal w and h
3868 int BC_WindowBase::resize_event(int w, int h)
3869 {
3870         if(window_type == MAIN_WINDOW)
3871         {
3872                 this->w = w;
3873                 this->h = h;
3874         }
3875         return 0;
3876 }
3877
3878 int BC_WindowBase::reposition_window(int x, int y)
3879 {
3880         reposition_window(x, y, -1, -1);
3881         return 0;
3882 }
3883
3884
3885 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
3886 {
3887         int resize = 0;
3888
3889 // Some tools set their own dimensions before calling this, causing the
3890 // resize check to skip.
3891         this->x = x;
3892         this->y = y;
3893
3894         if(w > 0 && w != this->w)
3895         {
3896                 resize = 1;
3897                 this->w = w;
3898         }
3899
3900         if(h > 0 && h != this->h)
3901         {
3902                 resize = 1;
3903                 this->h = h;
3904         }
3905
3906 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
3907
3908         if(this->w <= 0)
3909                 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
3910         if(this->h <= 0)
3911                 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
3912
3913         if(translation_count && window_type == MAIN_WINDOW)
3914         {
3915 // KDE shifts window right and down.
3916 // FVWM leaves window alone and adds border around it.
3917                 XMoveResizeWindow(top_level->display,
3918                         win,
3919                         x - BC_DisplayInfo::auto_reposition_x,
3920                         y - BC_DisplayInfo::auto_reposition_y,
3921                         this->w,
3922                         this->h);
3923         }
3924         else
3925         {
3926                 XMoveResizeWindow(top_level->display,
3927                         win,
3928                         x,
3929                         y,
3930                         this->w,
3931                         this->h);
3932         }
3933
3934         if(resize)
3935         {
3936                 delete pixmap;
3937                 pixmap = new BC_Pixmap(this, this->w, this->h);
3938 // Propagate to menubar
3939                 for(int i = 0; i < subwindows->total; i++)
3940                 {
3941                         subwindows->values[i]->dispatch_resize_event(this->w, this->h);
3942                 }
3943
3944 //              draw_background(0, 0, w, h);
3945         }
3946
3947         return 0;
3948 }
3949
3950 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
3951 {
3952         return reposition_window(get_x()+dx, get_y()+dy, w, h);
3953 }
3954
3955 int BC_WindowBase::reposition_window_relative(int dx, int dy)
3956 {
3957         return reposition_window_relative(dx, dy, -1, -1);
3958 }
3959
3960 void BC_WindowBase::set_tooltips(int v)
3961 {
3962         get_resources()->tooltips_enabled = v;
3963 }
3964
3965 void BC_WindowBase::set_force_tooltip(int v)
3966 {
3967         force_tooltip = v;
3968 }
3969
3970 int BC_WindowBase::raise_window(int do_flush)
3971 {
3972         XRaiseWindow(top_level->display, win);
3973         if(do_flush) XFlush(top_level->display);
3974         return 0;
3975 }
3976
3977 int BC_WindowBase::lower_window(int do_flush)
3978 {
3979         XLowerWindow(top_level->display, win);
3980         if(do_flush) XFlush(top_level->display);
3981         return 0;
3982 }
3983
3984 void BC_WindowBase::set_background(VFrame *bitmap)
3985 {
3986         if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
3987
3988         bg_pixmap = new BC_Pixmap(this,
3989                         bitmap,
3990                         PIXMAP_OPAQUE);
3991         shared_bg_pixmap = 0;
3992         draw_background(0, 0, w, h);
3993 }
3994
3995 void BC_WindowBase::set_title(const char *text)
3996 {
3997         XTextProperty titleprop;
3998         char *txlist[2];
3999
4000         strcpy(this->title, _(text));
4001         txlist[0] = this->title;
4002         txlist[1] = 0;
4003
4004         XmbTextListToTextProperty(top_level->display, txlist, 1,
4005                 XStdICCTextStyle, &titleprop);
4006         XSetWMName(top_level->display, top_level->win, &titleprop);
4007         XSetWMIconName(top_level->display, top_level->win, &titleprop);
4008         XFree(titleprop.value);
4009
4010         flush();
4011 }
4012
4013 void BC_WindowBase::set_utf8title(const char *text)
4014 {
4015         XTextProperty titleprop;
4016         char *txlist[2];
4017
4018         strcpy(this->title, text);
4019         txlist[0] = this->title;
4020         txlist[1] = 0;
4021
4022         Xutf8TextListToTextProperty(top_level->display, txlist,  1,
4023                 XUTF8StringStyle, &titleprop);
4024         XSetWMName(top_level->display, top_level->win, &titleprop);
4025         XSetWMIconName(top_level->display, top_level->win, &titleprop);
4026         XFree(titleprop.value);
4027
4028         flush();
4029 }
4030
4031 const char *BC_WindowBase::get_title()
4032 {
4033         return title;
4034 }
4035
4036 int BC_WindowBase::get_toggle_value()
4037 {
4038         return toggle_value;
4039 }
4040
4041 int BC_WindowBase::get_toggle_drag()
4042 {
4043         return toggle_drag;
4044 }
4045
4046 int BC_WindowBase::set_icon(VFrame *data)
4047 {
4048         if(icon_pixmap) delete icon_pixmap;
4049         icon_pixmap = new BC_Pixmap(top_level,
4050                 data,
4051                 PIXMAP_ALPHA,
4052                 1);
4053
4054         if(icon_window) delete icon_window;
4055         icon_window = new BC_Popup(this,
4056                 (int)BC_INFINITY,
4057                 (int)BC_INFINITY,
4058                 icon_pixmap->get_w(),
4059                 icon_pixmap->get_h(),
4060                 -1,
4061                 1, // All windows are hidden initially
4062                 icon_pixmap);
4063
4064         XWMHints wm_hints;
4065         wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4066         wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4067         wm_hints.icon_mask = icon_pixmap->get_alpha();
4068         wm_hints.icon_window = icon_window->win;
4069         wm_hints.window_group = XGroupLeader;
4070
4071 // for(int i = 0; i < 1000; i++)
4072 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4073 // printf("\n");
4074
4075         XSetWMHints(top_level->display, top_level->win, &wm_hints);
4076         XSync(top_level->display, 0);
4077         return 0;
4078 }
4079
4080 int BC_WindowBase::set_w(int w)
4081 {
4082         this->w = w;
4083         return 0;
4084 }
4085
4086 int BC_WindowBase::set_h(int h)
4087 {
4088         this->h = h;
4089         return 0;
4090 }
4091
4092 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4093 {
4094         BC_Resources *resources = get_resources();
4095         char string[BCTEXTLEN];
4096         int newest_id = - 1;
4097         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4098         {
4099                 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4100                 resources->filebox_history[i].path[0] = 0;
4101                 defaults->get(string, resources->filebox_history[i].path);
4102                 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4103                 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4104                 if(resources->filebox_history[i].id > newest_id)
4105                         newest_id = resources->filebox_history[i].id;
4106         }
4107
4108         resources->filebox_id = newest_id + 1;
4109         resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4110         resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4111         resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4112         resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4113         resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4114         resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4115         resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4116         resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4117         resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4118         resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4119         resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4120         defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4121         return 0;
4122 }
4123
4124 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4125 {
4126         BC_Resources *resources = get_resources();
4127         char string[BCTEXTLEN];
4128         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4129         {
4130                 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4131                 defaults->update(string, resources->filebox_history[i].path);
4132                 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4133                 defaults->update(string, resources->filebox_history[i].id);
4134         }
4135         defaults->update("FILEBOX_MODE", resources->filebox_mode);
4136         defaults->update("FILEBOX_W", resources->filebox_w);
4137         defaults->update("FILEBOX_H", resources->filebox_h);
4138         defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4139         defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4140         defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4141         defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4142         defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4143         defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4144         defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4145         defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4146         defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4147         return 0;
4148 }
4149
4150
4151
4152 // For some reason XTranslateCoordinates can take a long time to return.
4153 // We work around this by only calling it when the event windows are different.
4154 void BC_WindowBase::translate_coordinates(Window src_w,
4155                 Window dest_w,
4156                 int src_x,
4157                 int src_y,
4158                 int *dest_x_return,
4159                 int *dest_y_return)
4160 {
4161         Window tempwin = 0;
4162 //Timer timer;
4163 //timer.update();
4164         if(src_w == dest_w)
4165         {
4166                 *dest_x_return = src_x;
4167                 *dest_y_return = src_y;
4168         }
4169         else
4170         {
4171                 XTranslateCoordinates(top_level->display,
4172                         src_w,
4173                         dest_w,
4174                         src_x,
4175                         src_y,
4176                         dest_x_return,
4177                         dest_y_return,
4178                         &tempwin);
4179 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4180         }
4181 }
4182
4183
4184
4185
4186
4187
4188
4189 #ifdef HAVE_LIBXXF86VM
4190 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4191 {
4192    int foo,bar;
4193    *vm = 0;
4194    if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4195            int vm_count,i;
4196            XF86VidModeModeInfo **vm_modelines;
4197            XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4198            for (i = 0; i < vm_count; i++) {
4199                    if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4200                            *vm = i;
4201            }
4202            display = top_level->display;
4203            if (vm_modelines[*vm]->hdisplay == *width)
4204                    *vm = -1;
4205            else
4206            {
4207                    *width = vm_modelines[*vm]->hdisplay;
4208                    *height = vm_modelines[*vm]->vdisplay;
4209            }
4210    }
4211 }
4212
4213 void BC_WindowBase::scale_vm(int vm)
4214 {
4215    int foo,bar,dotclock;
4216    if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4217    {
4218            int vm_count;
4219            XF86VidModeModeInfo **vm_modelines;
4220            XF86VidModeModeLine vml;
4221            XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4222            XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4223            orig_modeline.dotclock = dotclock;
4224            orig_modeline.hdisplay = vml.hdisplay;
4225            orig_modeline.hsyncstart = vml.hsyncstart;
4226            orig_modeline.hsyncend = vml.hsyncend;
4227            orig_modeline.htotal = vml.htotal;
4228            orig_modeline.vdisplay = vml.vdisplay;
4229            orig_modeline.vsyncstart = vml.vsyncstart;
4230            orig_modeline.vsyncend = vml.vsyncend;
4231            orig_modeline.vtotal = vml.vtotal;
4232            orig_modeline.flags = vml.flags;
4233            orig_modeline.privsize = vml.privsize;
4234            // orig_modeline.private = vml.private;
4235            XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4236            XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4237            XFlush(top_level->display);
4238    }
4239 }
4240
4241 void BC_WindowBase::restore_vm()
4242 {
4243    XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4244    XFlush(top_level->display);
4245 }
4246
4247
4248 #endif
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267 #ifndef SINGLE_THREAD
4268 int BC_WindowBase::get_event_count()
4269 {
4270         event_lock->lock("BC_WindowBase::get_event_count");
4271         int result = common_events.total;
4272         event_lock->unlock();
4273         return result;
4274 }
4275
4276 XEvent* BC_WindowBase::get_event()
4277 {
4278         XEvent *result = 0;
4279         while(!done && !result)
4280         {
4281                 event_condition->lock("BC_WindowBase::get_event");
4282                 event_lock->lock("BC_WindowBase::get_event");
4283
4284                 if(common_events.total && !done)
4285                 {
4286                         result = common_events.values[0];
4287                         common_events.remove_number(0);
4288                 }
4289
4290                 event_lock->unlock();
4291         }
4292         return result;
4293 }
4294
4295 void BC_WindowBase::put_event(XEvent *event)
4296 {
4297         event_lock->lock("BC_WindowBase::put_event");
4298         common_events.append(event);
4299         event_lock->unlock();
4300         event_condition->unlock();
4301 }
4302
4303 #endif // SINGLE_THREAD
4304
4305 int BC_WindowBase::get_id()
4306 {
4307         return id;
4308 }
4309
4310
4311 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4312 {
4313         int w = vframe->get_w(), h = vframe->get_h();
4314         BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4315         icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4316         return icon;
4317 }
4318
4319