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