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