4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcclipboard.h"
24 #include "bcdisplay.h"
25 #include "bcdisplayinfo.h"
26 #include "bcmenubar.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"
36 #include "bcwindowbase.h"
37 #include "bcwindowevents.h"
38 #include "bccmodels.h"
40 #include "condition.h"
58 #include <X11/extensions/Xinerama.h>
59 #include <X11/extensions/Xvlib.h>
60 #include <X11/extensions/shape.h>
61 #include <X11/XF86keysym.h>
62 #include <X11/Sunkeysym.h>
65 BC_ResizeCall::BC_ResizeCall(int w, int h)
77 int BC_WindowBase::shm_completion_event = -1;
81 BC_Resources BC_WindowBase::resources;
83 Window XGroupLeader = 0;
85 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
86 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
88 BC_WindowBase::BC_WindowBase(int opts)
91 //printf("BC_WindowBase::BC_WindowBase 1\n");
92 BC_WindowBase::initialize();
95 BC_WindowBase::~BC_WindowBase()
98 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
100 if(window_type == MAIN_WINDOW)
101 lock_window("BC_WindowBase::~BC_WindowBase");
104 #ifdef HAVE_LIBXXF86VM
105 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
112 if(window_type != MAIN_WINDOW)
115 XSelectInput(top_level->display, this->win, 0);
116 XSync(top_level->display,0);
117 #ifndef SINGLE_THREAD
118 top_level->dequeue_events(win);
120 // drop active window refs to this
121 if(top_level->active_menubar == this) top_level->active_menubar = 0;
122 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
123 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
124 // drop motion window refs to this
125 if(top_level->motion_events && top_level->last_motion_win == this->win)
126 top_level->motion_events = 0;
128 // Remove pointer from parent window to this
129 parent_window->subwindows->remove(this);
132 if(grab_active) grab_active->active_grab = 0;
133 if(icon_window) delete icon_window;
134 if(window_type == POPUP_WINDOW)
135 parent_window->remove_popup(this);
137 // Delete the subwindows
140 while(subwindows->total)
142 // Subwindow removes its own pointer
143 delete subwindows->values[0];
151 if( get_resources()->get_synchronous() &&
152 (top_level->options & GLX_WINDOW) && glx_win != 0 )
153 get_resources()->get_synchronous()->delete_window(this);
156 XDestroyWindow(top_level->display, win);
158 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
159 if(icon_pixmap) delete icon_pixmap;
160 if(temp_bitmap) delete temp_bitmap;
161 top_level->active_bitmaps.remove_buffers(this);
162 if(_7segment_pixmaps)
164 for(int i = 0; i < TOTAL_7SEGMENT; i++)
165 delete _7segment_pixmaps[i];
167 delete [] _7segment_pixmaps;
172 if(window_type == MAIN_WINDOW)
174 XFreeGC(display, gc);
175 static XFontStruct *BC_WindowBase::*xfont[] = {
176 &BC_WindowBase::smallfont,
177 &BC_WindowBase::mediumfont,
178 &BC_WindowBase::largefont,
179 &BC_WindowBase::bigfont,
181 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
182 XFreeFont(display, this->*xfont[i]);
184 // past bug in X caused XRenderExtensionInfo to be damaged
185 // if this is done here. left to be done in XCloseDisplay by Xlib.
186 // works in more modern systems, and needed for leak testing.
187 #if defined(HAVE_XFT) && defined(VALGRIND)
188 static void *BC_WindowBase::*xft_font[] = {
189 &BC_WindowBase::smallfont_xft,
190 &BC_WindowBase::mediumfont_xft,
191 &BC_WindowBase::largefont_xft,
192 &BC_WindowBase::bigfont_xft,
193 &BC_WindowBase::bold_smallfont_xft,
194 &BC_WindowBase::bold_mediumfont_xft,
195 &BC_WindowBase::bold_largefont_xft,
197 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
198 XftFont *xft = (XftFont *)(this->*xft_font[i]);
199 if( xft ) XftFontClose (display, xft);
207 XFree(xinerama_info);
208 xinerama_screens = 0;
210 if( xvideo_port_id >= 0 )
211 XvUngrabPort(display, xvideo_port_id, CurrentTime);
214 // Can't close display if another thread is waiting for events.
215 // Synchronous thread must delete display it owns a GLX_WINDOW
216 // Must be last reference to display.
217 #ifndef SINGLE_THREAD
219 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
220 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
221 "implemented for BC_Pixmap.\n"));
222 get_resources()->get_synchronous()->delete_display(this);
226 XCloseDisplay(display);
227 // clipboard uses a different display connection
228 clipboard->stop_clipboard();
230 #endif // SINGLE_THREAD
233 resize_history.remove_all_objects();
235 #ifndef SINGLE_THREAD
236 common_events.remove_all_objects();
238 delete event_condition;
241 top_level->window_lock = 0;
242 BC_Display::unlock_display();
247 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
248 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
249 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
252 UNSET_ALL_LOCKS(this)
255 int BC_WindowBase::initialize()
260 display_lock_owner = 0;
276 xinerama_screens = 0;
281 translation_events = 0;
282 ctrl_mask = shift_mask = alt_mask = 0;
283 cursor_x = cursor_y = button_number = 0;
297 active_popup_menu = 0;
298 active_subwindow = 0;
301 _7segment_pixmaps = 0;
304 // next_repeat_id = 0;
306 current_font = MEDIUMFONT;
307 current_color = BLACK;
308 current_cursor = ARROW_CURSOR;
311 shared_bg_pixmap = 0;
314 window_type = MAIN_WINDOW;
315 translation_count = 0;
316 x_correction = y_correction = 0;
325 #ifdef HAVE_LIBXXF86VM
329 bold_largefont_xft = 0;
330 bold_mediumfont_xft = 0;
331 bold_smallfont_xft = 0;
339 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
341 // Need these right away since put_event is called before run_window sometimes.
342 event_lock = new Mutex("BC_WindowBase::event_lock");
343 event_condition = new Condition(0, "BC_WindowBase::event_condition");
344 init_lock = new Condition(0, "BC_WindowBase::init_lock");
347 cursor_timer = new Timer;
350 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
351 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
352 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
366 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
369 ButtonReleaseMask | \
370 PointerMotionMask | \
374 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
386 const char *display_name,
388 BC_Pixmap *bg_pixmap,
391 XSetWindowAttributes attr;
393 XSizeHints size_hints;
396 #ifdef HAVE_LIBXXF86VM
400 id = get_resources()->get_id();
402 if(parent_window) top_level = parent_window->top_level;
403 if( top_level ) { // need this to avoid deadlock with Xlib's locks
405 lock_window("BC_WindowBase::create_window");
407 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
409 #ifdef HAVE_LIBXXF86VM
410 if(window_type == VIDMODE_SCALED_WINDOW)
411 closest_vm(&vm,&w,&h);
418 this->bg_color = bg_color;
419 this->window_type = window_type;
421 this->private_color = private_color;
422 this->parent_window = parent_window;
423 this->bg_pixmap = bg_pixmap;
424 this->allow_resize = allow_resize;
426 strcpy(this->display_name, display_name);
428 this->display_name[0] = 0;
431 if(bg_pixmap) shared_bg_pixmap = 1;
433 subwindows = new BC_SubWindowList;
435 if(window_type == MAIN_WINDOW)
438 parent_window = this;
442 display = BC_Display::get_display(display_name);
443 BC_Display::lock_display("BC_WindowBase::create_window");
444 // BC_Display::display_global->new_window(this);
447 // get the display connection
449 // This function must be the first Xlib
450 // function a multi-threaded program calls
452 display = init_display(display_name);
453 if( shm_completion_event < 0 ) shm_completion_event =
454 ShmCompletion + XShmGetEventBase(display);
457 screen = DefaultScreen(display);
458 rootwin = RootWindow(display, screen);
459 // window placement boundaries
460 if( !xinerama_screens && XineramaIsActive(display) )
461 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
462 root_w = get_root_w(0);
463 root_h = get_root_h(0);
466 vis = get_glx_visual(display);
469 vis = DefaultVisual(display, screen);
471 default_depth = DefaultDepth(display, screen);
473 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
474 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
477 // This must be done before fonts to know if antialiasing is available.
480 if(resources.use_shm < 0) resources.initialize_display(this);
481 x_correction = BC_DisplayInfo::get_left_border();
482 y_correction = BC_DisplayInfo::get_top_border();
484 // clamp window placement
485 if(this->x + this->w + x_correction > root_w)
486 this->x = root_w - this->w - x_correction;
487 if(this->y + this->h + y_correction > root_h)
488 this->y = root_h - this->h - y_correction;
489 if(this->x < 0) this->x = 0;
490 if(this->y < 0) this->y = 0;
492 if(this->bg_color == -1)
493 this->bg_color = resources.get_bg_color();
495 // printf("bcwindowbase 1 %s\n", title);
496 // if(window_type == MAIN_WINDOW) sleep(1);
497 // printf("bcwindowbase 10\n");
503 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
505 attr.event_mask = DEFAULT_EVENT_MASKS |
506 StructureNotifyMask |
510 attr.background_pixel = get_color(this->bg_color);
511 attr.colormap = cmap;
512 attr.cursor = get_cursor_struct(ARROW_CURSOR);
514 win = XCreateWindow(display, rootwin,
515 this->x, this->y, this->w, this->h, 0,
516 top_level->default_depth, InputOutput,
518 XGetNormalHints(display, win, &size_hints);
520 size_hints.flags = PSize | PMinSize | PMaxSize;
521 size_hints.width = this->w;
522 size_hints.height = this->h;
523 size_hints.min_width = allow_resize ? minw : this->w;
524 size_hints.max_width = allow_resize ? 32767 : this->w;
525 size_hints.min_height = allow_resize ? minh : this->h;
526 size_hints.max_height = allow_resize ? 32767 : this->h;
527 if(x > -BC_INFINITY && x < BC_INFINITY)
529 size_hints.flags |= PPosition;
530 size_hints.x = this->x;
531 size_hints.y = this->y;
535 txlist[0] = this->title;
537 XTextProperty titleprop;
538 if(options & WINDOW_UTF8)
539 Xutf8TextListToTextProperty(display, txlist, 1,
540 XUTF8StringStyle, &titleprop);
542 XmbTextListToTextProperty(display, txlist, 1,
543 XStdICCTextStyle, &titleprop);
544 XSetWMProperties(display, win, &titleprop, &titleprop,
545 0, 0, &size_hints, 0, 0);
546 XFree(titleprop.value);
549 #ifndef SINGLE_THREAD
550 clipboard = new BC_Clipboard(this);
551 clipboard->start_clipboard();
557 Atom ClientLeaderXAtom;
558 if (XGroupLeader == 0)
560 const char *instance_name = "cinelerra";
561 const char *class_name = "Cinelerra";
562 XClassHint *class_hints = XAllocClassHint();
563 class_hints->res_name = (char*)instance_name;
564 class_hints->res_class = (char*)class_name;
565 XSetClassHint(top_level->display, win, class_hints);
567 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
568 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
569 PropModeReplace, (unsigned char *)&XGroupLeader, true);
574 #ifdef HAVE_LIBXXF86VM
575 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
582 #ifdef HAVE_LIBXXF86VM
583 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
585 if(window_type == POPUP_WINDOW)
588 mask = CWEventMask | CWBackPixel | CWColormap |
589 CWOverrideRedirect | CWSaveUnder | CWCursor;
591 attr.event_mask = DEFAULT_EVENT_MASKS |
595 if(this->bg_color == -1)
596 this->bg_color = resources.get_bg_color();
597 attr.background_pixel = top_level->get_color(bg_color);
598 attr.colormap = top_level->cmap;
599 if(top_level->is_hourglass)
600 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
602 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
603 attr.override_redirect = True;
604 attr.save_under = True;
606 win = XCreateWindow(top_level->display,
607 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
608 top_level->default_depth, InputOutput, top_level->vis, mask,
610 top_level->add_popup(this);
613 if(window_type == SUB_WINDOW)
615 mask = CWEventMask | CWBackPixel | CWCursor;
616 attr.event_mask = DEFAULT_EVENT_MASKS;
617 attr.background_pixel = top_level->get_color(this->bg_color);
618 if(top_level->is_hourglass)
619 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
621 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
622 win = XCreateWindow(top_level->display,
623 parent_window->win, this->x, this->y, this->w, this->h, 0,
624 top_level->default_depth, InputOutput, top_level->vis, mask,
627 if(!hidden) XMapWindow(top_level->display, win);
630 // Create pixmap for all windows
631 pixmap = new BC_Pixmap(this, this->w, this->h);
633 // Set up options for main window
634 if(window_type == MAIN_WINDOW)
636 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
638 this->bg_pixmap = new BC_Pixmap(this,
639 get_resources()->bg_image,
643 if(!hidden) show_window();
647 draw_background(0, 0, this->w, this->h);
649 flash(-1, -1, -1, -1, 0);
651 // Set up options for popup window
652 #ifdef HAVE_LIBXXF86VM
653 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
655 if(window_type == POPUP_WINDOW)
659 if(!hidden) show_window();
661 get_resources()->create_window_lock->unlock();
662 if(need_lock) unlock_window();
667 Display* BC_WindowBase::init_display(const char *display_name)
671 if(display_name && display_name[0] == 0) display_name = NULL;
672 if((display = XOpenDisplay(display_name)) == NULL) {
673 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
675 if(getenv("DISPLAY") == NULL) {
676 printf(_("'DISPLAY' environment variable not set.\n"));
680 // Try again with default display.
681 if((display = XOpenDisplay(0)) == NULL) {
682 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
688 static int xsynch = -1;
690 const char *cp = getenv("CIN_XSYNCH");
691 xsynch = !cp ? 0 : atoi(cp);
694 XSynchronize(display, True);
699 Display* BC_WindowBase::get_display()
701 return top_level->display;
704 int BC_WindowBase::get_screen()
706 return top_level->screen;
709 int BC_WindowBase::run_window()
715 // Events may have been sent before run_window so can't initialize them here.
718 set_repeat(get_resources()->tooltip_delay);
719 BC_Display::display_global->new_window(this);
721 // If the first window created, run the display loop in this thread.
722 if(BC_Display::display_global->is_first(this))
724 BC_Display::unlock_display();
725 BC_Display::display_global->loop();
729 BC_Display::unlock_display();
730 completion_lock->lock("BC_WindowBase::run_window");
733 BC_Display::lock_display("BC_WindowBase::run_window");
734 BC_Display::display_global->delete_window(this);
736 unset_all_repeaters();
738 BC_Display::unlock_display();
740 #else // SINGLE_THREAD
745 set_repeat(get_resources()->tooltip_delay);
747 // Start X server events
748 event_thread = new BC_WindowEvents(this);
749 event_thread->start();
755 // Handle common events
761 unset_all_repeaters();
765 event_condition->reset();
766 common_events.remove_all_objects();
770 #endif // SINGLE_THREAD
775 int BC_WindowBase::get_key_masks(unsigned int key_state)
777 // printf("BC_WindowBase::get_key_masks %llx\n",
778 // event->xkey.state);
779 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
780 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
781 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
786 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
788 BC_KeyboardHandlerLock set;
789 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
792 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
794 BC_KeyboardHandlerLock set;
795 int i = BC_KeyboardHandler::listeners.size();
796 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
797 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
800 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
802 int result = (win->*handler)(wp);
806 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
809 BC_KeyboardHandlerLock set;
810 for( int i=0; !result && i<listeners.size(); ++i ) {
811 BC_KeyboardHandler *listener = listeners[i];
812 result = listener->run_event(wp);
817 void BC_KeyboardHandler::kill_grabs()
819 BC_KeyboardHandlerLock set;
820 for( int i=0; i<listeners.size(); ++i ) {
821 BC_WindowBase *win = listeners[i]->win;
822 if( win->get_window_type() != POPUP_WINDOW ) continue;
823 ((BC_Popup *)win)->ungrab_keyboard();
827 void BC_ActiveBitmaps::reque(XEvent *event)
829 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
830 ShmSeg shmseg = shm_ev->shmseg;
831 Drawable drawable = shm_ev->drawable;
832 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
833 active_lock.lock("BC_BitmapImage::reque");
834 BC_BitmapImage *bfr = first;
835 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
836 if( bfr && bfr->drawable == drawable )
838 active_lock.unlock();
840 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
841 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
842 // (int)shmseg, (int)drawable);
845 if( bfr->drawable != drawable ) return;
846 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
847 bfr->bitmap->reque(bfr);
850 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
852 active_lock.lock("BC_BitmapImage::insert");
853 bfr->drawable = pixmap;
855 active_lock.unlock();
858 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
860 active_lock.lock("BC_ActiveBitmaps::remove");
861 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
863 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
864 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
866 active_lock.unlock();
869 BC_ActiveBitmaps::BC_ActiveBitmaps()
873 BC_ActiveBitmaps::~BC_ActiveBitmaps()
879 int BC_WindowBase::keysym_lookup(XEvent *event)
881 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
882 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
884 if( event->xany.send_event && !event->xany.serial ) {
885 keysym = (KeySym) event->xkey.keycode;
886 keys_return[0] = keysym;
889 wkey_string_length = 0;
891 if( input_context ) {
892 wkey_string_length = XwcLookupString(input_context,
893 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
894 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
895 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
898 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
899 keys_return, KEYPRESSLEN, &keysym, &stat);
900 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
901 if( stat == XLookupBoth ) return ret;
902 if( stat == XLookupKeySym ) return 0;
904 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
905 wkey_string_length = ret;
906 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
910 pthread_t locking_task = (pthread_t)-1L;
911 int locking_event = -1;
912 int locking_message = -1;
914 int BC_WindowBase::dispatch_event(XEvent *event)
918 XClientMessageEvent *ptr;
919 int cancel_resize, cancel_translation;
924 #ifndef SINGLE_THREAD
925 // If an event is waiting get it, otherwise
926 // wait for next event only if there are no compressed events.
927 if(get_event_count() ||
928 (!motion_events && !resize_events && !translation_events))
931 // Lock out window deletions
932 lock_window("BC_WindowBase::dispatch_event 1");
933 locking_event = event->type;
934 locking_task = pthread_self();
935 locking_message = event->xclient.message_type;
938 // Handle compressed events
940 lock_window("BC_WindowBase::dispatch_event 2");
942 dispatch_resize_event(last_resize_w, last_resize_h);
944 dispatch_motion_event();
945 if(translation_events)
946 dispatch_translation_event();
957 //static const char *event_names[] = {
958 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
959 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
960 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
961 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
962 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
963 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
964 // "GenericEvent", "LASTEvent",
968 //if(event->type != ClientMessage) {
969 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
970 //__LINE__, title, event, event->type,
971 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
972 // event_names[event->type] : "Unknown");
977 active_grab->lock_window("BC_WindowBase::dispatch_event 3");
978 result = active_grab->grab_event(event);
979 active_grab->unlock_window();
980 if( result ) return result;
981 lock_window("BC_WindowBase::dispatch_event 4");
984 switch(event->type) {
986 // Clear the resize buffer
987 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
988 // Clear the motion buffer since this can clear the window
991 dispatch_motion_event();
994 ptr = (XClientMessageEvent*)event;
997 if(ptr->message_type == ProtoXAtom &&
998 (Atom)ptr->data.l[0] == DelWinXAtom)
1003 if(ptr->message_type == RepeaterXAtom)
1005 dispatch_repeat_event(ptr->data.l[0]);
1006 // Make sure the repeater still exists.
1007 // for(int i = 0; i < repeaters.total; i++)
1009 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
1011 // dispatch_repeat_event_master(ptr->data.l[0]);
1017 if(ptr->message_type == SetDoneXAtom)
1021 { // We currently use X marshalling for xatom events, we can switch to something else later
1022 recieve_custom_xatoms((xatom_event *)ptr);
1028 dispatch_focus_in();
1033 dispatch_focus_out();
1045 get_key_masks(event->xbutton.state);
1046 cursor_x = event->xbutton.x;
1047 cursor_y = event->xbutton.y;
1048 button_number = event->xbutton.button;
1050 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1051 event_win = event->xany.window;
1052 if (button_number < 6)
1054 if(button_number < 4)
1056 button_pressed = event->xbutton.button;
1057 button_time1 = button_time2;
1058 button_time2 = button_time3;
1059 button_time3 = event->xbutton.time;
1062 drag_win = event_win;
1063 drag_x1 = cursor_x - get_resources()->drag_radius;
1064 drag_x2 = cursor_x + get_resources()->drag_radius;
1065 drag_y1 = cursor_y - get_resources()->drag_radius;
1066 drag_y2 = cursor_y + get_resources()->drag_radius;
1068 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1071 button_time3 = button_time2 = button_time1 = 0;
1073 if((long)(button_time3 - button_time2) < resources.double_click)
1076 // button_time3 = button_time2 = button_time1 = 0;
1084 dispatch_button_press();
1089 get_key_masks(event->xbutton.state);
1090 button_number = event->xbutton.button;
1091 event_win = event->xany.window;
1092 if (button_number < 6)
1094 if(button_number < 4)
1096 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1098 dispatch_button_release();
1103 event_win = event->xany.window;
1104 dispatch_expose_event();
1108 get_key_masks(event->xmotion.state);
1109 // Dispatch previous motion event if this is a subsequent motion from a different window
1110 if(motion_events && last_motion_win != event->xany.window)
1112 dispatch_motion_event();
1115 // Buffer the current motion
1117 last_motion_state = event->xmotion.state;
1118 last_motion_x = event->xmotion.x;
1119 last_motion_y = event->xmotion.y;
1120 last_motion_win = event->xany.window;
1123 case ConfigureNotify:
1124 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1126 // event->xany.window,
1129 XTranslateCoordinates(top_level->display,
1137 last_resize_w = event->xconfigure.width;
1138 last_resize_h = event->xconfigure.height;
1141 cancel_translation = 0;
1143 // Resize history prevents responses to recursive resize requests
1144 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1146 if(resize_history.values[i]->w == last_resize_w &&
1147 resize_history.values[i]->h == last_resize_h)
1149 delete resize_history.values[i];
1150 resize_history.remove_number(i);
1155 if(last_resize_w == w && last_resize_h == h)
1163 if((last_translate_x == x && last_translate_y == y))
1164 cancel_translation = 1;
1166 if(!cancel_translation)
1168 translation_events = 1;
1171 translation_count++;
1175 get_key_masks(event->xkey.state);
1176 keys_return[0] = 0; keysym = -1;
1177 if(XFilterEvent(event, win)) {
1180 if( keysym_lookup(event) < 0 ) {
1181 printf("keysym %x\n", (uint32_t)keysym);
1185 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1189 // block out control keys
1190 if(keysym > 0xffe0 && keysym < 0xffff) break;
1191 // block out Alt_GR key
1192 if(keysym == 0xfe03) break;
1195 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1197 #ifdef X_HAVE_UTF8_STRING
1198 //It's Ascii or UTF8?
1199 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1200 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1202 if( ((keys_return[1] & 0xff) > 0x80) &&
1203 ((keys_return[0] & 0xff) > 0xC0) ) {
1204 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1205 key_pressed = keysym & 0xff;
1211 // block out extra keys
1221 // Translate key codes
1222 case XK_Return: key_pressed = RETURN; break;
1223 case XK_Up: key_pressed = UP; break;
1224 case XK_Down: key_pressed = DOWN; break;
1225 case XK_Left: key_pressed = LEFT; break;
1226 case XK_Right: key_pressed = RIGHT; break;
1227 case XK_Next: key_pressed = PGDN; break;
1228 case XK_Prior: key_pressed = PGUP; break;
1229 case XK_BackSpace: key_pressed = BACKSPACE; break;
1230 case XK_Escape: key_pressed = ESC; break;
1233 key_pressed = LEFTTAB;
1237 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1238 case XK_underscore: key_pressed = '_'; break;
1239 case XK_asciitilde: key_pressed = '~'; break;
1240 case XK_Delete: key_pressed = DELETE; break;
1241 case XK_Home: key_pressed = HOME; break;
1242 case XK_End: key_pressed = END; break;
1245 case XK_KP_Enter: key_pressed = KPENTER; break;
1246 case XK_KP_Add: key_pressed = KPPLUS; break;
1247 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1248 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1249 case XK_KP_Divide: key_pressed = KPSLASH; break;
1251 case XK_KP_End: key_pressed = KP1; break;
1253 case XK_KP_Down: key_pressed = KP2; break;
1255 case XK_KP_Page_Down: key_pressed = KP3; break;
1257 case XK_KP_Left: key_pressed = KP4; break;
1259 case XK_KP_Begin: key_pressed = KP5; break;
1261 case XK_KP_Right: key_pressed = KP6; break;
1263 case XK_KP_Home: key_pressed = KP7; break;
1265 case XK_KP_Up: key_pressed = KP8; break;
1267 case XK_KP_Page_Up: key_pressed = KP9; break;
1269 case XK_KP_Insert: key_pressed = KPINS; break;
1271 case XK_KP_Delete: key_pressed = KPDEL; break;
1272 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1274 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1275 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1276 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1277 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1278 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1279 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1280 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1281 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1282 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1283 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1284 // above case XK_Left: key_pressed = LEFT; break; /* left */
1285 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1286 // above case XK_Down: key_pressed = DOWN; break; /* down */
1287 // above case XK_Up: key_pressed = UP; break; /* up */
1288 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1289 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1290 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1291 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1292 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1293 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1294 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1295 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1296 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1297 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1298 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1299 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1302 key_pressed = keysym & 0xff;
1303 #ifdef X_HAVE_UTF8_STRING
1304 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1309 #ifdef X_HAVE_UTF8_STRING
1311 key_pressed_utf8 = keys_return;
1316 if( top_level == this )
1317 result = BC_KeyboardHandler::run_listeners(this);
1319 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1321 result = dispatch_keypress_event();
1322 // Handle some default keypresses
1325 if(key_pressed == 'w' ||
1334 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1335 dispatch_keyrelease_event();
1336 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1337 // keysym, event->xkey.state);
1341 event_win = event->xany.window;
1342 dispatch_cursor_leave();
1346 event_win = event->xany.window;
1347 cursor_x = event->xcrossing.x;
1348 cursor_y = event->xcrossing.y;
1349 dispatch_cursor_enter();
1355 //printf("100 %s %p %d\n", title, event, event->type);
1356 //if(event->type != ClientMessage) dump();
1358 #ifndef SINGLE_THREAD
1360 if(event) delete event;
1362 // if(done) completion_lock->unlock();
1365 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1369 int BC_WindowBase::dispatch_expose_event()
1372 for(int i = 0; i < subwindows->total && !result; i++)
1374 result = subwindows->values[i]->dispatch_expose_event();
1377 // Propagate to user
1378 if(!result) expose_event();
1382 int BC_WindowBase::dispatch_resize_event(int w, int h)
1384 // Can't store new w and h until the event is handles
1385 // because bcfilebox depends on the old w and h to
1386 // reposition widgets.
1387 if( window_type == MAIN_WINDOW ) {
1392 pixmap = new BC_Pixmap(this, w, h);
1393 clear_box(0, 0, w, h);
1396 // Propagate to subwindows
1397 for(int i = 0; i < subwindows->total; i++) {
1398 subwindows->values[i]->dispatch_resize_event(w, h);
1401 // Propagate to user
1404 if( window_type == MAIN_WINDOW ) {
1413 int BC_WindowBase::dispatch_flash()
1416 for(int i = 0; i < subwindows->total; i++)
1417 subwindows->values[i]->dispatch_flash();
1421 int BC_WindowBase::dispatch_translation_event()
1423 translation_events = 0;
1424 if(window_type == MAIN_WINDOW)
1428 x = last_translate_x;
1429 y = last_translate_y;
1430 // Correct for window manager offsets
1435 for(int i = 0; i < subwindows->total; i++)
1437 subwindows->values[i]->dispatch_translation_event();
1440 translation_event();
1444 int BC_WindowBase::dispatch_motion_event()
1449 if(top_level == this)
1452 event_win = last_motion_win;
1453 get_key_masks(last_motion_state);
1456 if(get_button_down() && !active_menubar && !active_popup_menu)
1460 cursor_x = last_motion_x;
1461 cursor_y = last_motion_y;
1462 result = dispatch_drag_motion();
1466 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1467 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1472 result = dispatch_drag_start();
1476 cursor_x = last_motion_x;
1477 cursor_y = last_motion_y;
1479 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1482 // active_popup_menu,
1483 // active_subwindow);
1485 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1486 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1487 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1490 // Dispatch in stacking order
1491 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1493 result = subwindows->values[i]->dispatch_motion_event();
1496 if(!result) result = cursor_motion_event(); // give to user
1500 int BC_WindowBase::dispatch_keypress_event()
1503 if(top_level == this)
1505 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1508 for(int i = 0; i < subwindows->total && !result; i++)
1510 result = subwindows->values[i]->dispatch_keypress_event();
1513 if(!result) result = keypress_event();
1518 int BC_WindowBase::dispatch_keyrelease_event()
1521 if(top_level == this)
1523 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1526 for(int i = 0; i < subwindows->total && !result; i++)
1528 result = subwindows->values[i]->dispatch_keyrelease_event();
1531 if(!result) result = keyrelease_event();
1536 int BC_WindowBase::dispatch_focus_in()
1538 for(int i = 0; i < subwindows->total; i++)
1540 subwindows->values[i]->dispatch_focus_in();
1548 int BC_WindowBase::dispatch_focus_out()
1550 for(int i = 0; i < subwindows->total; i++)
1552 subwindows->values[i]->dispatch_focus_out();
1560 int BC_WindowBase::get_has_focus()
1562 return top_level->has_focus;
1565 int BC_WindowBase::get_deleting()
1567 if(is_deleting) return 1;
1568 if(parent_window && parent_window->get_deleting()) return 1;
1572 int BC_WindowBase::dispatch_button_press()
1577 if(top_level == this)
1579 if(active_menubar) result = active_menubar->dispatch_button_press();
1580 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1581 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1584 for(int i = 0; i < subwindows->total && !result; i++)
1586 result = subwindows->values[i]->dispatch_button_press();
1589 if(!result) result = button_press_event();
1595 int BC_WindowBase::dispatch_button_release()
1598 if(top_level == this)
1600 if(active_menubar) result = active_menubar->dispatch_button_release();
1601 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1602 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1603 if(!result && button_number != 4 && button_number != 5)
1604 result = dispatch_drag_stop();
1607 for(int i = 0; i < subwindows->total && !result; i++)
1609 result = subwindows->values[i]->dispatch_button_release();
1614 result = button_release_event();
1621 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1624 // all repeat event handlers get called and decide based on activity and duration
1625 // whether to respond
1626 for(int i = 0; i < subwindows->total; i++)
1628 subwindows->values[i]->dispatch_repeat_event(duration);
1632 repeat_event(duration);
1636 // Unlock next repeat signal
1637 if(window_type == MAIN_WINDOW)
1639 #ifdef SINGLE_THREAD
1640 BC_Display::display_global->unlock_repeaters(duration);
1642 for(int i = 0; i < repeaters.total; i++)
1644 if(repeaters.values[i]->delay == duration)
1646 repeaters.values[i]->repeat_lock->unlock();
1654 void BC_WindowBase::unhide_cursor()
1659 if(top_level->is_hourglass)
1660 set_cursor(HOURGLASS_CURSOR, 1, 0);
1662 set_cursor(current_cursor, 1, 0);
1664 cursor_timer->update();
1668 void BC_WindowBase::update_video_cursor()
1670 if(video_on && !is_transparent)
1672 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1675 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1676 cursor_timer->update();
1681 cursor_timer->update();
1686 int BC_WindowBase::dispatch_cursor_leave()
1690 for(int i = 0; i < subwindows->total; i++)
1692 subwindows->values[i]->dispatch_cursor_leave();
1695 cursor_leave_event();
1699 int BC_WindowBase::dispatch_cursor_enter()
1705 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1706 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1707 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1709 for(int i = 0; !result && i < subwindows->total; i++)
1711 result = subwindows->values[i]->dispatch_cursor_enter();
1714 if(!result) result = cursor_enter_event();
1718 int BC_WindowBase::cursor_enter_event()
1723 int BC_WindowBase::cursor_leave_event()
1728 int BC_WindowBase::close_event()
1734 int BC_WindowBase::dispatch_drag_start()
1737 if(active_menubar) result = active_menubar->dispatch_drag_start();
1738 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1739 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1741 for(int i = 0; i < subwindows->total && !result; i++)
1743 result = subwindows->values[i]->dispatch_drag_start();
1746 if(!result) result = is_dragging = drag_start_event();
1750 int BC_WindowBase::dispatch_drag_stop()
1754 for(int i = 0; i < subwindows->total && !result; i++)
1756 result = subwindows->values[i]->dispatch_drag_stop();
1759 if(is_dragging && !result)
1769 int BC_WindowBase::dispatch_drag_motion()
1772 for(int i = 0; i < subwindows->total && !result; i++)
1774 result = subwindows->values[i]->dispatch_drag_motion();
1777 if(is_dragging && !result)
1779 drag_motion_event();
1787 int BC_WindowBase::show_tooltip(const char *text, int x, int y, int w, int h)
1790 int forced = !text ? force_tooltip : 1;
1791 if( !text ) text = tooltip_text;
1792 if( !text || (!forced && !get_resources()->tooltips_enabled) ) {
1793 top_level->hide_tooltip();
1797 if(w < 0) w = get_text_width(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1798 if(h < 0) h = get_text_height(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1799 // default x,y (win relative)
1800 if( x < 0 ) x = get_w();
1801 if( y < 0 ) y = get_h();
1803 get_root_coordinates(x, y, &wx, &wy);
1804 // keep the tip inside the window/display
1805 int x0 = top_level->get_x(), x1 = x0 + top_level->get_w();
1806 int x2 = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1807 if( x1 > x2 ) x1 = x2;
1808 if( wx < x0 ) wx = x0;
1809 if( wx >= (x1-=w) ) wx = x1;
1810 int y0 = top_level->get_y(), y1 = y0 + top_level->get_h();
1811 int y2 = top_level->get_root_h(0);
1812 if( y1 > y2 ) y1 = y2;
1813 if( wy < y0 ) wy = y0;
1814 if( wy >= (y1-=h) ) wy = y1;
1815 // avoid tip under cursor (flickers)
1817 get_abs_cursor_xy(abs_x,abs_y, 0);
1818 if( wx < abs_x && abs_x < wx+w && wy < abs_y && abs_y < wy+h ) {
1819 if( wx-abs_x < wy-abs_y )
1826 tooltip_popup = new BC_Popup(top_level, wx, wy, w, h,
1827 get_resources()->tooltip_bg_color);
1830 tooltip_popup->reposition_window(wx, wy, w, h);
1833 tooltip_popup->flash();
1834 tooltip_popup->flush();
1838 int BC_WindowBase::hide_tooltip()
1841 for(int i = 0; i < subwindows->total; i++)
1843 subwindows->values[i]->hide_tooltip();
1849 delete tooltip_popup;
1855 const char *BC_WindowBase::get_tooltip()
1857 return tooltip_text;
1860 int BC_WindowBase::set_tooltip(const char *text)
1862 tooltip_text = text;
1864 // Update existing tooltip if it is visible
1868 tooltip_popup->flash();
1872 // signal the event handler to repeat
1873 int BC_WindowBase::set_repeat(int64_t duration)
1877 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1880 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1882 #ifdef SINGLE_THREAD
1883 BC_Display::display_global->set_repeat(this, duration);
1885 // test repeater database for duplicates
1886 for(int i = 0; i < repeaters.total; i++)
1889 if(repeaters.values[i]->delay == duration)
1891 repeaters.values[i]->start_repeating(this);
1896 BC_Repeater *repeater = new BC_Repeater(this, duration);
1897 repeater->initialize();
1898 repeaters.append(repeater);
1899 repeater->start_repeating();
1904 int BC_WindowBase::unset_repeat(int64_t duration)
1906 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1908 #ifdef SINGLE_THREAD
1909 BC_Display::display_global->unset_repeat(this, duration);
1911 for(int i = 0; i < repeaters.total; i++)
1913 if(repeaters.values[i]->delay == duration)
1915 repeaters.values[i]->stop_repeating();
1923 int BC_WindowBase::unset_all_repeaters()
1925 #ifdef SINGLE_THREAD
1926 BC_Display::display_global->unset_all_repeaters(this);
1928 for(int i = 0; i < repeaters.total; i++)
1930 repeaters.values[i]->stop_repeating();
1932 repeaters.remove_all_objects();
1937 // long BC_WindowBase::get_repeat_id()
1939 // return top_level->next_repeat_id++;
1942 XEvent *BC_WindowBase::new_xevent()
1944 XEvent *event = new XEvent;
1945 memset(event, 0, sizeof(*event));
1949 #ifndef SINGLE_THREAD
1950 int BC_WindowBase::arm_repeat(int64_t duration)
1952 XEvent *event = new_xevent();
1953 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1954 ptr->type = ClientMessage;
1955 ptr->message_type = RepeaterXAtom;
1957 ptr->data.l[0] = duration;
1959 // Couldn't use XSendEvent since it locked up randomly.
1965 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1970 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1972 #ifndef SINGLE_THREAD
1973 XEvent *myevent = new_xevent();
1974 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1975 ptr->type = ClientMessage;
1976 ptr->message_type = event->message_type;
1977 ptr->format = event->format;
1978 ptr->data.l[0] = event->data.l[0];
1979 ptr->data.l[1] = event->data.l[1];
1980 ptr->data.l[2] = event->data.l[2];
1981 ptr->data.l[3] = event->data.l[3];
1982 ptr->data.l[4] = event->data.l[4];
1991 Atom BC_WindowBase::create_xatom(const char *atom_name)
1993 return XInternAtom(display, atom_name, False);
1996 int BC_WindowBase::get_atoms()
1998 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1999 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
2000 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
2001 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
2002 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
2003 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
2004 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2010 void BC_WindowBase::init_cursors()
2012 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2013 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2014 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2015 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2016 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2017 move_cursor = XCreateFontCursor(display, XC_fleur);
2018 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2019 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2020 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2021 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2022 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2023 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2024 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2025 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2026 grabbed_cursor = create_grab_cursor();
2028 static char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2029 Colormap colormap = DefaultColormap(display, screen);
2030 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2031 rootwin, cursor_data, 8, 8);
2032 XColor black, dummy;
2033 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2034 transparent_cursor = XCreatePixmapCursor(display,
2035 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2036 // XDefineCursor(display, win, transparent_cursor);
2037 XFreePixmap(display, pixmap_bottom);
2040 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2042 int color_model = BC_TRANSPARENCY;
2046 color_model = BC_RGB8;
2049 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2052 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2055 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2061 int BC_WindowBase::init_colors()
2064 current_color_value = current_color_pixel = 0;
2066 // Get the real depth
2069 ximage = XCreateImage(top_level->display,
2071 top_level->default_depth,
2079 bits_per_pixel = ximage->bits_per_pixel;
2080 XDestroyImage(ximage);
2082 color_model = evaluate_color_model(client_byte_order,
2085 // Get the color model
2091 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2092 create_private_colors();
2096 cmap = DefaultColormap(display, screen);
2097 create_shared_colors();
2100 allocate_color_table();
2104 //cmap = DefaultColormap(display, screen);
2105 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2111 int BC_WindowBase::create_private_colors()
2116 for(int i = 0; i < 255; i++)
2118 color = (i & 0xc0) << 16;
2119 color += (i & 0x38) << 10;
2120 color += (i & 0x7) << 5;
2121 color_table[i][0] = color;
2123 create_shared_colors(); // overwrite the necessary colors on the table
2128 int BC_WindowBase::create_color(int color)
2130 if(total_colors == 256)
2132 // replace the closest match with an exact match
2133 color_table[get_color_rgb8(color)][0] = color;
2137 // add the color to the table
2138 color_table[total_colors][0] = color;
2144 int BC_WindowBase::create_shared_colors()
2146 create_color(BLACK);
2147 create_color(WHITE);
2149 create_color(LTGREY);
2150 create_color(MEGREY);
2151 create_color(MDGREY);
2152 create_color(DKGREY);
2154 create_color(LTCYAN);
2155 create_color(MECYAN);
2156 create_color(MDCYAN);
2157 create_color(DKCYAN);
2159 create_color(LTGREEN);
2160 create_color(GREEN);
2161 create_color(DKGREEN);
2163 create_color(LTPINK);
2167 create_color(LTBLUE);
2169 create_color(DKBLUE);
2171 create_color(LTYELLOW);
2172 create_color(MEYELLOW);
2173 create_color(MDYELLOW);
2174 create_color(DKYELLOW);
2176 create_color(LTPURPLE);
2177 create_color(MEPURPLE);
2178 create_color(MDPURPLE);
2179 create_color(DKPURPLE);
2181 create_color(FGGREY);
2182 create_color(MNBLUE);
2183 create_color(ORANGE);
2184 create_color(FTGREY);
2189 Cursor BC_WindowBase::create_grab_cursor()
2191 int iw = 23, iw1 = iw-1, iw2 = iw/2;
2192 int ih = 23, ih1 = ih-1, ih2 = ih/2;
2193 VFrame grab(iw,ih,BC_RGB888);
2195 grab.set_pixel_color(RED); // fg
2196 grab.draw_smooth(iw2,0, iw1,0, iw1,ih2);
2197 grab.draw_smooth(iw1,ih2, iw1,ih1, iw2,ih1);
2198 grab.draw_smooth(iw2,ih1, 0,ih1, 0,ih2);
2199 grab.draw_smooth(0,ih2, 0,0, iw2,0);
2200 grab.set_pixel_color(WHITE); // bg
2201 grab.draw_line(0,ih2, iw2-2,ih2);
2202 grab.draw_line(iw2+2,ih2, iw1,ih2);
2203 grab.draw_line(iw2,0, iw2,ih2-2);
2204 grab.draw_line(iw2,ih2+2, iw2,ih1);
2206 int bpl = (iw+7)/8, isz = bpl * ih;
2207 char img[isz]; memset(img, 0, isz);
2208 char msk[isz]; memset(msk, 0, isz);
2209 unsigned char **rows = grab.get_rows();
2210 for( int iy=0; iy<ih; ++iy ) {
2211 char *op = img + iy*bpl;
2212 char *mp = msk + iy*bpl;
2213 unsigned char *ip = rows[iy];
2214 for( int ix=0; ix<iw; ++ix,ip+=3 ) {
2215 if( ip[0] ) mp[ix>>3] |= (1<<(ix&7));
2216 if( !ip[1] ) op[ix>>3] |= (1<<(ix&7));
2219 unsigned long white_pix = WhitePixel(display, screen);
2220 unsigned long black_pix = BlackPixel(display, screen);
2221 Pixmap img_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2222 img, iw,ih, white_pix,black_pix, 1);
2223 Pixmap msk_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2224 msk, iw,ih, white_pix,black_pix, 1);
2227 fc.flags = bc.flags = DoRed | DoGreen | DoBlue;
2228 fc.red = 0xffff; fc.green = fc.blue = 0; // fg
2229 bc.red = bc.green = bc.blue = 0x0000; // bg
2230 Cursor cursor = XCreatePixmapCursor(display, img_xpm,msk_xpm, &fc,&bc, iw2,ih2);
2231 XFreePixmap(display, img_xpm);
2232 XFreePixmap(display, msk_xpm);
2236 int BC_WindowBase::allocate_color_table()
2238 int red, green, blue, color;
2241 for(int i = 0; i < total_colors; i++)
2243 color = color_table[i][0];
2244 red = (color & 0xFF0000) >> 16;
2245 green = (color & 0x00FF00) >> 8;
2246 blue = color & 0xFF;
2248 col.flags = DoRed | DoGreen | DoBlue;
2249 col.red = red<<8 | red;
2250 col.green = green<<8 | green;
2251 col.blue = blue<<8 | blue;
2253 XAllocColor(display, cmap, &col);
2254 color_table[i][1] = col.pixel;
2257 XInstallColormap(display, cmap);
2261 int BC_WindowBase::init_window_shape()
2263 if(bg_pixmap && bg_pixmap->use_alpha())
2265 XShapeCombineMask(top_level->display,
2266 this->win, ShapeBounding, 0, 0,
2267 bg_pixmap->get_alpha(), ShapeSet);
2273 int BC_WindowBase::init_gc()
2275 unsigned long gcmask;
2276 gcmask = GCFont | GCGraphicsExposures;
2279 gcvalues.font = mediumfont->fid; // set the font
2280 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2281 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2283 // gcmask = GCCapStyle | GCJoinStyle;
2284 // XGetGCValues(display, gc, gcmask, &gcvalues);
2285 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2289 int BC_WindowBase::init_fonts()
2291 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2292 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2293 smallfont = XLoadQueryFont(display, "fixed");
2294 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2295 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2296 mediumfont = XLoadQueryFont(display, "fixed");
2297 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2298 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2299 largefont = XLoadQueryFont(display, "fixed");
2300 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2301 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2302 bigfont = XLoadQueryFont(display, "fixed");
2305 if(get_resources()->use_fontset)
2310 // FIXME: should check the m,d,n values
2311 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2313 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2314 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2315 if( !mediumfontset )
2316 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2317 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2319 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2320 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2322 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2323 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2324 curr_fontset = mediumfontset;
2325 get_resources()->use_fontset = 1;
2329 get_resources()->use_fontset = 0;
2336 void BC_WindowBase::init_xft()
2339 if( !get_resources()->use_xft ) return;
2340 if(!(smallfont_xft =
2341 (resources.small_font_xft[0] == '-' ?
2342 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2343 XftFontOpenName(display, screen, resources.small_font_xft))) )
2344 if(!(smallfont_xft =
2345 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2346 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2347 if(!(mediumfont_xft =
2348 (resources.medium_font_xft[0] == '-' ?
2349 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2350 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2351 if(!(mediumfont_xft =
2352 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2353 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2354 if(!(largefont_xft =
2355 (resources.large_font_xft[0] == '-' ?
2356 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2357 XftFontOpenName(display, screen, resources.large_font_xft))) )
2358 if(!(largefont_xft =
2359 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2360 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2362 (resources.big_font_xft[0] == '-' ?
2363 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2364 XftFontOpenName(display, screen, resources.big_font_xft))) )
2366 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2367 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2369 if(!(bold_smallfont_xft =
2370 (resources.small_b_font_xft[0] == '-' ?
2371 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2372 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2373 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2374 if(!(bold_mediumfont_xft =
2375 (resources.medium_b_font_xft[0] == '-' ?
2376 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2377 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2378 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2379 if(!(bold_largefont_xft =
2380 (resources.large_b_font_xft[0] == '-' ?
2381 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2382 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2383 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2385 // Extension failed to locate fonts
2386 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2387 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2388 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2389 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2390 resources.small_font_xft, smallfont_xft,
2391 resources.medium_font_xft, mediumfont_xft,
2392 resources.large_font_xft, largefont_xft,
2393 resources.big_font_xft, bigfont_xft,
2394 resources.small_b_font_xft, bold_smallfont_xft,
2395 resources.medium_b_font_xft, bold_mediumfont_xft,
2396 resources.large_b_font_xft, bold_largefont_xft);
2397 get_resources()->use_xft = 0;
2403 void BC_WindowBase::init_im()
2405 XIMStyles *xim_styles;
2408 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2410 printf("BC_WindowBase::init_im: Could not open input method.\n");
2413 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2416 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2417 XCloseIM(input_method);
2422 for(int z = 0; z < xim_styles->count_styles; z++)
2424 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2426 xim_style = xim_styles->supported_styles[z];
2434 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2435 XCloseIM(input_method);
2439 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2440 XNClientWindow, win, XNFocusWindow, win, NULL);
2443 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2444 XCloseIM(input_method);
2449 void BC_WindowBase::finit_im()
2451 if( input_context ) {
2452 XDestroyIC(input_context);
2455 if( input_method ) {
2456 XCloseIM(input_method);
2462 int BC_WindowBase::get_color(int64_t color)
2464 // return pixel of color
2465 // use this only for drawing subwindows not for bitmaps
2466 int i, test, difference;
2472 return get_color_rgb8(color);
2473 // test last color looked up
2474 if(current_color_value == color)
2475 return current_color_pixel;
2478 current_color_value = color;
2479 for(i = 0; i < total_colors; i++)
2481 if(color_table[i][0] == color)
2483 current_color_pixel = color_table[i][1];
2484 return current_color_pixel;
2488 // find nearest match
2489 difference = 0xFFFFFF;
2491 for(i = 0; i < total_colors; i++)
2493 test = abs((int)(color_table[i][0] - color));
2495 if(test < difference)
2497 current_color_pixel = color_table[i][1];
2501 return current_color_pixel;
2504 return get_color_rgb16(color);
2507 return get_color_bgr16(color);
2511 return client_byte_order == server_byte_order ?
2512 color : get_color_bgr24(color);
2520 int BC_WindowBase::get_color_rgb8(int color)
2524 pixel = (color & 0xc00000) >> 16;
2525 pixel += (color & 0xe000) >> 10;
2526 pixel += (color & 0xe0) >> 5;
2530 int64_t BC_WindowBase::get_color_rgb16(int color)
2533 result = (color & 0xf80000) >> 8;
2534 result += (color & 0xfc00) >> 5;
2535 result += (color & 0xf8) >> 3;
2540 int64_t BC_WindowBase::get_color_bgr16(int color)
2543 result = (color & 0xf80000) >> 19;
2544 result += (color & 0xfc00) >> 5;
2545 result += (color & 0xf8) << 8;
2550 int64_t BC_WindowBase::get_color_bgr24(int color)
2553 result = (color & 0xff) << 16;
2554 result += (color & 0xff00);
2555 result += (color & 0xff0000) >> 16;
2559 void BC_WindowBase::start_video()
2561 cursor_timer->update();
2563 // set_color(BLACK);
2564 // draw_box(0, 0, get_w(), get_h());
2568 void BC_WindowBase::stop_video()
2576 int64_t BC_WindowBase::get_color()
2578 return top_level->current_color;
2581 void BC_WindowBase::set_color(int64_t color)
2583 top_level->current_color = color;
2584 XSetForeground(top_level->display,
2586 top_level->get_color(color));
2589 void BC_WindowBase::set_opaque()
2591 XSetFunction(top_level->display, top_level->gc, GXcopy);
2594 void BC_WindowBase::set_inverse()
2596 XSetFunction(top_level->display, top_level->gc, GXxor);
2599 void BC_WindowBase::set_line_width(int value)
2601 this->line_width = value;
2602 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2603 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2604 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2605 JoinMiter); /* join_style */
2607 if(line_dashes > 0) {
2608 const char dashes = line_dashes;
2609 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2612 // XGCValues gcvalues;
2613 // unsigned long gcmask;
2614 // gcmask = GCCapStyle | GCJoinStyle;
2615 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2616 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2619 void BC_WindowBase::set_line_dashes(int value)
2621 line_dashes = value;
2622 // call XSetLineAttributes
2623 set_line_width(line_width);
2627 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2631 case ARROW_CURSOR: return top_level->arrow_cursor;
2632 case CROSS_CURSOR: return top_level->cross_cursor;
2633 case IBEAM_CURSOR: return top_level->ibeam_cursor;
2634 case VSEPARATE_CURSOR: return top_level->vseparate_cursor;
2635 case HSEPARATE_CURSOR: return top_level->hseparate_cursor;
2636 case MOVE_CURSOR: return top_level->move_cursor;
2637 case LEFT_CURSOR: return top_level->left_cursor;
2638 case RIGHT_CURSOR: return top_level->right_cursor;
2639 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor;
2640 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor;
2641 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor;
2642 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor;
2643 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor;
2644 case HOURGLASS_CURSOR: return top_level->hourglass_cursor;
2645 case TRANSPARENT_CURSOR: return top_level->transparent_cursor;
2646 case GRABBED_CURSOR: return top_level->grabbed_cursor;
2651 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2653 // inherit cursor from parent
2656 XUndefineCursor(top_level->display, win);
2657 current_cursor = cursor;
2660 // don't change cursor if overridden
2661 if((!top_level->is_hourglass && !is_transparent) ||
2664 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2665 if(flush) this->flush();
2668 if(!override) current_cursor = cursor;
2671 void BC_WindowBase::set_x_cursor(int cursor)
2673 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2674 XDefineCursor(top_level->display, win, temp_cursor);
2675 current_cursor = cursor;
2679 int BC_WindowBase::get_cursor()
2681 return current_cursor;
2684 void BC_WindowBase::start_hourglass()
2686 top_level->start_hourglass_recursive();
2690 void BC_WindowBase::stop_hourglass()
2692 top_level->stop_hourglass_recursive();
2696 void BC_WindowBase::start_hourglass_recursive()
2698 if(this == top_level)
2706 set_cursor(HOURGLASS_CURSOR, 1, 0);
2707 for(int i = 0; i < subwindows->total; i++)
2709 subwindows->values[i]->start_hourglass_recursive();
2714 void BC_WindowBase::stop_hourglass_recursive()
2716 if(this == top_level)
2718 if(hourglass_total == 0) return;
2719 top_level->hourglass_total--;
2722 if(!top_level->hourglass_total)
2724 top_level->is_hourglass = 0;
2726 // Cause set_cursor to perform change
2728 set_cursor(current_cursor, 1, 0);
2730 for(int i = 0; i < subwindows->total; i++)
2732 subwindows->values[i]->stop_hourglass_recursive();
2740 XFontStruct* BC_WindowBase::get_font_struct(int font)
2742 // Clear out unrelated flags
2743 if(font & BOLDFACE) font ^= BOLDFACE;
2746 case SMALLFONT: return top_level->smallfont; break;
2747 case MEDIUMFONT: return top_level->mediumfont; break;
2748 case LARGEFONT: return top_level->largefont; break;
2749 case BIGFONT: return top_level->bigfont; break;
2754 XFontSet BC_WindowBase::get_fontset(int font)
2758 if(get_resources()->use_fontset)
2760 switch(font & 0xff) {
2761 case SMALLFONT: fs = top_level->smallfontset; break;
2762 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2763 case LARGEFONT: fs = top_level->largefontset; break;
2764 case BIGFONT: fs = top_level->bigfontset; break;
2772 XftFont* BC_WindowBase::get_xft_struct(int font)
2775 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2776 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2777 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2778 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2779 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2780 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2781 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2796 int BC_WindowBase::get_current_font()
2798 return top_level->current_font;
2801 void BC_WindowBase::set_font(int font)
2803 top_level->current_font = font;
2806 if(get_resources()->use_xft) {}
2809 if(get_resources()->use_fontset) {
2813 if(get_font_struct(font))
2815 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2821 void BC_WindowBase::set_fontset(int font)
2825 if(get_resources()->use_fontset) {
2827 case SMALLFONT: fs = top_level->smallfontset; break;
2828 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2829 case LARGEFONT: fs = top_level->largefontset; break;
2830 case BIGFONT: fs = top_level->bigfontset; break;
2838 XFontSet BC_WindowBase::get_curr_fontset(void)
2840 if(get_resources()->use_fontset)
2841 return curr_fontset;
2845 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2848 if(get_resources()->use_xft && get_xft_struct(font))
2851 #ifdef X_HAVE_UTF8_STRING
2852 if(get_resources()->locale_utf8)
2854 XftTextExtentsUtf8(top_level->display,
2855 get_xft_struct(font),
2856 (const XftChar8 *)text,
2863 XftTextExtents8(top_level->display,
2864 get_xft_struct(font),
2865 (const XftChar8 *)text,
2869 return extents.xOff;
2873 if(get_resources()->use_fontset && top_level->get_fontset(font))
2874 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2876 if(get_font_struct(font))
2877 return XTextWidth(get_font_struct(font), text, length);
2883 case MEDIUM_7SEGMENT:
2884 return get_resources()->medium_7segment[0]->get_w() * length;
2894 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2896 int i, j, w = 0, line_w = 0;
2897 if(length < 0) length = strlen(text);
2899 for(i = 0, j = 0; i <= length; i++)
2904 line_w = get_single_text_width(font, &text[j], i - j);
2910 line_w = get_single_text_width(font, &text[j], length - j);
2912 if(line_w > w) w = line_w;
2915 if(i > length && w == 0)
2917 w = get_single_text_width(font, text, length);
2923 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2926 if( length < 0 ) length = wcslen(text);
2928 for( i=j=0; i<length && text[i]; ++i ) {
2929 if( text[i] != '\n' ) continue;
2931 int lw = get_single_text_width(font, &text[j], i-j);
2932 if( w < lw ) w = lw;
2937 int lw = get_single_text_width(font, &text[j], length-j);
2938 if( w < lw ) w = lw;
2944 int BC_WindowBase::get_text_ascent(int font)
2948 if( (fstruct = get_xft_struct(font)) != 0 )
2949 return fstruct->ascent;
2951 if(get_resources()->use_fontset && top_level->get_fontset(font))
2953 XFontSetExtents *extents;
2955 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2956 return -extents->max_logical_extent.y;
2959 if(get_font_struct(font))
2960 return top_level->get_font_struct(font)->ascent;
2963 case MEDIUM_7SEGMENT:
2964 return get_resources()->medium_7segment[0]->get_h();
2969 int BC_WindowBase::get_text_descent(int font)
2973 if( (fstruct = get_xft_struct(font)) != 0 )
2974 return fstruct->descent;
2976 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2977 XFontSetExtents *extents;
2978 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2979 return (extents->max_logical_extent.height
2980 + extents->max_logical_extent.y);
2983 if(get_font_struct(font))
2984 return top_level->get_font_struct(font)->descent;
2989 int BC_WindowBase::get_text_height(int font, const char *text)
2994 if( (fstruct = get_xft_struct(font)) != 0 )
2995 rowh = fstruct->height;
2998 rowh = get_text_ascent(font) + get_text_descent(font);
3000 if(!text) return rowh;
3002 // Add height of lines
3003 int h = 0, i, length = strlen(text);
3004 for(i = 0; i <= length; i++)
3015 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
3017 if(color_model < 0) color_model = top_level->get_color_model();
3018 return new BC_Bitmap(top_level, w, h, color_model);
3021 void BC_WindowBase::init_wait()
3023 #ifndef SINGLE_THREAD
3024 if(window_type != MAIN_WINDOW)
3025 top_level->init_wait();
3026 init_lock->lock("BC_WindowBase::init_wait");
3027 init_lock->unlock();
3031 int BC_WindowBase::accel_available(int color_model, int lock_it)
3033 if( window_type != MAIN_WINDOW )
3034 return top_level->accel_available(color_model, lock_it);
3036 lock_window("BC_WindowBase::accel_available");
3038 switch(color_model) {
3040 grab_port_id(color_model);
3044 grab_port_id(color_model);
3053 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3054 return xvideo_port_id >= 0 ? 1 : 0;
3058 int BC_WindowBase::grab_port_id(int color_model)
3060 if( !get_resources()->use_xvideo || // disabled
3061 !get_resources()->use_shm ) // Only local server is fast enough.
3063 if( xvideo_port_id >= 0 )
3064 return xvideo_port_id;
3066 unsigned int ver, rev, reqBase, eventBase, errorBase;
3067 if( Success != XvQueryExtension(display, // XV extension is available
3068 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3071 // XV adaptors are available
3072 unsigned int numAdapt = 0;
3073 XvAdaptorInfo *info = 0;
3074 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3078 // Translate from color_model to X color model
3079 int x_color_model = BC_CModels::bc_to_x(color_model);
3081 // Get adaptor with desired color model
3082 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3083 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3084 // adaptor supports XvImages
3085 int numFormats = 0, numPorts = info[i].num_ports;
3086 XvImageFormatValues *formats =
3087 XvListImageFormats(display, info[i].base_id, &numFormats);
3088 if( !formats ) continue;
3090 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3091 if( formats[j].id != x_color_model ) continue;
3092 // this adaptor supports the desired format, grab a port
3093 for( int k=0; k<numPorts; ++k ) {
3094 if( Success == XvGrabPort(top_level->display,
3095 info[i].base_id+k, CurrentTime) ) {
3096 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3097 xvideo_port_id = info[i].base_id + k;
3105 XvFreeAdaptorInfo(info);
3107 return xvideo_port_id;
3111 int BC_WindowBase::show_window(int flush)
3113 for(int i = 0; i < subwindows->size(); i++)
3115 subwindows->get(i)->show_window(0);
3118 XMapWindow(top_level->display, win);
3119 if(flush) XFlush(top_level->display);
3120 // XSync(top_level->display, 0);
3125 int BC_WindowBase::hide_window(int flush)
3127 for(int i = 0; i < subwindows->size(); i++)
3129 subwindows->get(i)->hide_window(0);
3132 XUnmapWindow(top_level->display, win);
3133 if(flush) this->flush();
3138 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3140 subwindows->append((BC_SubWindow*)menu_bar);
3142 menu_bar->parent_window = this;
3143 menu_bar->top_level = this->top_level;
3144 menu_bar->initialize();
3148 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3150 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3151 if(this != top_level) return top_level->add_popup(window);
3152 popups.append(window);
3156 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3158 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3159 if(this != top_level)
3160 top_level->remove_popup(window);
3162 popups.remove(window);
3163 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3167 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3169 subwindows->append(subwindow);
3171 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3173 // parent window must be set before the subwindow initialization
3174 subwindow->parent_window = this;
3175 subwindow->top_level = this->top_level;
3177 // Execute derived initialization
3178 subwindow->initialize();
3183 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3185 return add_subwindow(subwindow);
3188 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3190 if( !top_level->flash_enabled ) return 0;
3191 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3193 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3196 XClearArea(top_level->display, win, x, y, w, h, 0);
3200 XClearWindow(top_level->display, win);
3208 int BC_WindowBase::flash(int flush)
3210 flash(-1, -1, -1, -1, flush);
3214 void BC_WindowBase::flush()
3216 //if(!get_window_lock())
3217 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3218 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3219 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3220 int locked = get_window_lock();
3221 if( !locked ) lock_window("BC_WindowBase::flush");
3222 XFlush(top_level->display);
3223 if( !locked ) unlock_window();
3226 void BC_WindowBase::sync_display()
3228 int locked = get_window_lock();
3229 if( !locked ) lock_window("BC_WindowBase::sync_display");
3230 XSync(top_level->display, False);
3231 if( !locked ) unlock_window();
3234 int BC_WindowBase::get_window_lock()
3236 #ifdef SINGLE_THREAD
3237 return BC_Display::display_global->get_display_locked();
3239 return top_level->window_lock;
3243 int BC_WindowBase::lock_window(const char *location)
3245 if(top_level && top_level != this)
3247 top_level->lock_window(location);
3252 SET_LOCK(this, title, location);
3253 #ifdef SINGLE_THREAD
3254 BC_Display::lock_display(location);
3256 XLockDisplay(top_level->display);
3257 top_level->display_lock_owner = pthread_self();
3260 ++top_level->window_lock;
3264 printf("BC_WindowBase::lock_window top_level NULL\n");
3269 int BC_WindowBase::unlock_window()
3271 if(top_level && top_level != this)
3273 top_level->unlock_window();
3280 // if(!top_level->window_lock)
3282 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3286 if( top_level->window_lock > 0 )
3287 if( --top_level->window_lock == 0 )
3288 top_level->display_lock_owner = 0;
3289 #ifdef SINGLE_THREAD
3290 BC_Display::unlock_display();
3292 XUnlockDisplay(top_level->display);
3297 printf("BC_WindowBase::unlock_window top_level NULL\n");
3302 void BC_WindowBase::set_done(int return_value)
3304 if(done_set) return;
3306 if(window_type != MAIN_WINDOW)
3307 top_level->set_done(return_value);
3310 #ifdef SINGLE_THREAD
3311 this->return_value = return_value;
3312 BC_Display::display_global->arm_completion(this);
3313 completion_lock->unlock();
3314 #else // SINGLE_THREAD
3316 if( !event_thread ) return;
3317 XEvent *event = new_xevent();
3318 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3319 event->type = ClientMessage;
3320 ptr->message_type = SetDoneXAtom;
3322 this->return_value = return_value;
3323 // May lock up here because XSendEvent doesn't work too well
3324 // asynchronous with XNextEvent.
3325 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3327 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3333 void BC_WindowBase::close(int return_value)
3335 hide_window(); flush();
3336 set_done(return_value);
3339 int BC_WindowBase::grab(BC_WindowBase *window)
3341 if( window->active_grab && this != window->active_grab ) return 0;
3342 window->active_grab = this;
3343 this->grab_active = window;
3346 int BC_WindowBase::ungrab(BC_WindowBase *window)
3348 if( window->active_grab && this != window->active_grab ) return 0;
3349 window->active_grab = 0;
3350 this->grab_active = 0;
3353 int BC_WindowBase::grab_buttons()
3355 XSync(top_level->display, False);
3356 if( XGrabButton(top_level->display, AnyButton, AnyModifier,
3357 top_level->rootwin, True, ButtonPressMask | ButtonReleaseMask,
3358 GrabModeAsync, GrabModeSync, None, None) == GrabSuccess ) {
3359 set_active_subwindow(this);
3364 void BC_WindowBase::ungrab_buttons()
3366 XUngrabButton(top_level->display, AnyButton, AnyModifier, top_level->rootwin);
3367 set_active_subwindow(0);
3370 void BC_WindowBase::grab_cursor()
3372 Cursor cursor_grab = get_cursor_struct(GRABBED_CURSOR);
3373 XGrabPointer(top_level->display, top_level->rootwin, True,
3374 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
3375 GrabModeAsync, GrabModeAsync, None, cursor_grab, CurrentTime);
3377 void BC_WindowBase::ungrab_cursor()
3379 XUngrabPointer(top_level->display, CurrentTime);
3383 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3384 // this is the bounding box of all the screens
3386 int BC_WindowBase::get_root_w(int lock_display)
3388 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3389 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3390 int result = WidthOfScreen(def_screen);
3391 if(lock_display) unlock_window();
3395 int BC_WindowBase::get_root_h(int lock_display)
3397 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3398 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3399 int result = HeightOfScreen(def_screen);
3400 if(lock_display) unlock_window();
3404 XineramaScreenInfo *
3405 BC_WindowBase::get_xinerama_info(int screen)
3407 if( !xinerama_info || !xinerama_screens ) return 0;
3409 for( int i=0; i<xinerama_screens; ++i )
3410 if( xinerama_info[i].screen_number == screen )
3411 return &xinerama_info[i];
3414 int top_x = get_x(), top_y = get_y();
3415 for( int i=0; i<xinerama_screens; ++i ) {
3416 int scr_y = top_y - xinerama_info[i].y_org;
3417 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3418 int scr_x = top_x - xinerama_info[i].x_org;
3419 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3420 return &xinerama_info[i];
3425 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3428 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3429 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3432 int root_w = get_root_w(0);
3433 int root_h = get_root_h(0);
3434 // Shift X based on position of current window if dual head
3435 if( (float)root_w/root_h > 1.8 ) {
3436 root_w = get_screen_w(0, 0);
3437 if( top_level->get_x() >= root_w )
3442 result = info->x_org;
3443 if(lock_display) unlock_window();
3447 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3449 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3450 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3451 int result = !info ? 0 : info->y_org;
3452 if(lock_display) unlock_window();
3456 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3459 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3460 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3462 int width = get_root_w(0);
3463 int height = get_root_h(0);
3464 if( (float)width/height > 1.8 ) {
3465 // If dual head, the screen width is > 16x9
3466 // but we only want to fill one screen
3467 // this code assumes the "big" screen is on the right
3468 int scr_w0 = width / 2;
3470 case 600: scr_w0 = 800; break;
3471 case 720: scr_w0 = 1280; break;
3472 case 1024: scr_w0 = 1280; break;
3473 case 1200: scr_w0 = 1600; break;
3474 case 1080: scr_w0 = 1920; break;
3476 int scr_w1 = width - scr_w0;
3477 result = screen > 0 ? scr_w1 :
3478 screen == 0 ? scr_w0 :
3479 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3485 result = info->width;
3486 if(lock_display) unlock_window();
3490 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3492 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3493 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3494 int result = info ? info->height : get_root_h(0);
3495 if(lock_display) unlock_window();
3499 // Bottom right corner
3500 int BC_WindowBase::get_x2()
3505 int BC_WindowBase::get_y2()
3510 int BC_WindowBase::get_video_on()
3515 int BC_WindowBase::get_hidden()
3517 return top_level->hidden;
3520 int BC_WindowBase::cursor_inside()
3522 return (top_level->cursor_x >= 0 &&
3523 top_level->cursor_y >= 0 &&
3524 top_level->cursor_x < w &&
3525 top_level->cursor_y < h);
3528 BC_WindowBase* BC_WindowBase::get_top_level()
3533 BC_WindowBase* BC_WindowBase::get_parent()
3535 return parent_window;
3538 int BC_WindowBase::get_color_model()
3540 return top_level->color_model;
3543 BC_Resources* BC_WindowBase::get_resources()
3545 return &BC_WindowBase::resources;
3548 BC_Synchronous* BC_WindowBase::get_synchronous()
3550 return BC_WindowBase::resources.get_synchronous();
3553 int BC_WindowBase::get_bg_color()
3558 void BC_WindowBase::set_bg_color(int color)
3560 this->bg_color = color;
3563 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3568 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3570 top_level->active_subwindow = subwindow;
3573 int BC_WindowBase::activate()
3578 int BC_WindowBase::deactivate()
3580 if(window_type == MAIN_WINDOW)
3582 if( top_level->active_menubar ) {
3583 top_level->active_menubar->deactivate();
3584 top_level->active_menubar = 0;
3586 if( top_level->active_popup_menu ) {
3587 top_level->active_popup_menu->deactivate();
3588 top_level->active_popup_menu = 0;
3590 if( top_level->active_subwindow ) {
3591 top_level->active_subwindow->deactivate();
3592 top_level->active_subwindow = 0;
3594 if( top_level->motion_events && top_level->last_motion_win == this->win )
3595 top_level->motion_events = 0;
3601 int BC_WindowBase::cycle_textboxes(int amount)
3604 BC_WindowBase *new_textbox = 0;
3608 BC_WindowBase *first_textbox = 0;
3609 find_next_textbox(&first_textbox, &new_textbox, result);
3610 if(!new_textbox) new_textbox = first_textbox;
3616 BC_WindowBase *last_textbox = 0;
3617 find_prev_textbox(&last_textbox, &new_textbox, result);
3618 if(!new_textbox) new_textbox = last_textbox;
3622 if(new_textbox != active_subwindow)
3625 new_textbox->activate();
3631 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3633 // Search subwindows for textbox
3634 for(int i = 0; i < subwindows->total && result < 2; i++)
3636 BC_WindowBase *test_subwindow = subwindows->values[i];
3637 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3644 if(!*first_textbox) *first_textbox = this;
3648 if(top_level->active_subwindow == this)
3654 *next_textbox = this;
3661 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3667 if(!*last_textbox) *last_textbox = this;
3671 if(top_level->active_subwindow == this)
3677 *prev_textbox = this;
3682 // Search subwindows for textbox
3683 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3685 BC_WindowBase *test_subwindow = subwindows->values[i];
3686 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3691 BC_Clipboard* BC_WindowBase::get_clipboard()
3693 #ifdef SINGLE_THREAD
3694 return BC_Display::display_global->clipboard;
3696 return top_level->clipboard;
3700 Atom BC_WindowBase::to_clipboard(const char *data, long len, int clipboard_num)
3702 return get_clipboard()->to_clipboard(this, data, len, clipboard_num);
3705 long BC_WindowBase::from_clipboard(char *data, long maxlen, int clipboard_num)
3707 return get_clipboard()->from_clipboard(data, maxlen, clipboard_num);
3710 long BC_WindowBase::clipboard_len(int clipboard_num)
3712 return get_clipboard()->clipboard_len(clipboard_num);
3715 int BC_WindowBase::do_selection_clear(Window win)
3717 top_level->event_win = win;
3718 return dispatch_selection_clear();
3721 int BC_WindowBase::dispatch_selection_clear()
3724 for( int i=0; i<subwindows->total && !result; ++i )
3725 result = subwindows->values[i]->dispatch_selection_clear();
3727 result = selection_clear_event();
3732 void BC_WindowBase::get_relative_cursor_xy(int &x, int &y, int lock_window)
3734 int abs_x, abs_y, win_x, win_y;
3735 unsigned int temp_mask;
3738 if(lock_window) this->lock_window("BC_WindowBase::get_relative_cursor_xy");
3739 XQueryPointer(top_level->display, top_level->win,
3740 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3743 XTranslateCoordinates(top_level->display, top_level->rootwin,
3744 win, abs_x, abs_y, &x, &y, &temp_win);
3745 if(lock_window) this->unlock_window();
3747 int BC_WindowBase::get_relative_cursor_x(int lock_window)
3750 get_relative_cursor_xy(x, y, lock_window);
3753 int BC_WindowBase::get_relative_cursor_y(int lock_window)
3756 get_relative_cursor_xy(x, y, lock_window);
3760 void BC_WindowBase::get_abs_cursor_xy(int &abs_x, int &abs_y, int lock_window)
3763 unsigned int temp_mask;
3766 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_xy");
3767 XQueryPointer(top_level->display, top_level->win,
3768 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3770 if(lock_window) this->unlock_window();
3772 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3775 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3778 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3781 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3785 void BC_WindowBase::get_pop_cursor_xy(int &px, int &py, int lock_window)
3788 get_abs_cursor_xy(px, py, lock_window);
3789 if( px < margin ) px = margin;
3790 if( py < margin ) py = margin;
3791 int wd = get_screen_w(lock_window,-1) - margin;
3792 if( px > wd ) px = wd;
3793 int ht = get_screen_h(lock_window,-1) - margin;
3794 if( py > ht ) py = ht;
3796 int BC_WindowBase::get_pop_cursor_x(int lock_window)
3799 get_pop_cursor_xy(px, py, lock_window);
3802 int BC_WindowBase::get_pop_cursor_y(int lock_window)
3805 get_pop_cursor_xy(px, py, lock_window);
3809 int BC_WindowBase::match_window(Window win)
3811 if (this->win == win) return 1;
3813 for(int i = 0; i < subwindows->total; i++) {
3814 result = subwindows->values[i]->match_window(win);
3815 if (result) return result;
3821 int BC_WindowBase::get_cursor_over_window()
3823 if(top_level != this) return top_level->get_cursor_over_window();
3825 int abs_x, abs_y, win_x, win_y;
3826 unsigned int temp_mask;
3827 Window root_return, child_return;
3829 int ret = XQueryPointer(display, win,
3830 &root_return, &child_return, &abs_x, &abs_y,
3831 &win_x, &win_y, &temp_mask);
3832 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3833 // __LINE__, title, win, ret, child_return);
3835 int result = !ret || child_return == None ? 0 :
3836 match_window(child_return);
3840 int BC_WindowBase::cursor_above()
3843 get_relative_cursor_xy(rx, ry);
3844 return rx < 0 || rx >= get_w() ||
3845 ry < 0 || ry >= get_h() ? 0 : 1;
3848 int BC_WindowBase::get_drag_x()
3850 return top_level->drag_x;
3853 int BC_WindowBase::get_drag_y()
3855 return top_level->drag_y;
3858 int BC_WindowBase::get_cursor_x()
3860 return top_level->cursor_x;
3863 int BC_WindowBase::get_cursor_y()
3865 return top_level->cursor_y;
3868 int BC_WindowBase::dump_windows()
3870 printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
3871 this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
3872 for(int i = 0; i < subwindows->size(); i++)
3873 subwindows->get(i)->dump_windows();
3874 for(int i = 0; i < popups.size(); i++) {
3875 BC_WindowBase *p = popups[i];
3876 printf("\tBC_WindowBase::dump_windows popup=%p win=%p '%s', %dx%d+%d+%d %s\n",
3877 p, (void*)p->win, p->title, p->w,p->h,p->x,p->y, typeid(*p).name());
3882 int BC_WindowBase::is_event_win()
3884 return this->win == top_level->event_win;
3887 void BC_WindowBase::set_dragging(int value)
3889 is_dragging = value;
3892 int BC_WindowBase::get_dragging()
3897 int BC_WindowBase::get_buttonpress()
3899 return top_level->button_number;
3902 int BC_WindowBase::get_button_down()
3904 return top_level->button_down;
3907 int BC_WindowBase::alt_down()
3909 return top_level->alt_mask;
3912 int BC_WindowBase::shift_down()
3914 return top_level->shift_mask;
3917 int BC_WindowBase::ctrl_down()
3919 return top_level->ctrl_mask;
3922 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3925 *length = top_level->wkey_string_length;
3926 return top_level->wkey_string;
3929 #ifdef X_HAVE_UTF8_STRING
3930 char* BC_WindowBase::get_keypress_utf8()
3932 return top_level->key_pressed_utf8;
3937 int BC_WindowBase::get_keypress()
3939 return top_level->key_pressed;
3942 int BC_WindowBase::get_double_click()
3944 return top_level->double_click;
3947 int BC_WindowBase::get_triple_click()
3949 return top_level->triple_click;
3952 int BC_WindowBase::get_bgcolor()
3957 int BC_WindowBase::resize_window(int w, int h)
3959 if(this->w == w && this->h == h) return 0;
3961 if(window_type == MAIN_WINDOW && !allow_resize)
3963 XSizeHints size_hints;
3964 size_hints.flags = PSize | PMinSize | PMaxSize;
3965 size_hints.width = w;
3966 size_hints.height = h;
3967 size_hints.min_width = w;
3968 size_hints.max_width = w;
3969 size_hints.min_height = h;
3970 size_hints.max_height = h;
3971 XSetNormalHints(top_level->display, win, &size_hints);
3973 XResizeWindow(top_level->display, win, w, h);
3978 pixmap = new BC_Pixmap(this, w, h);
3980 // Propagate to menubar
3981 for(int i = 0; i < subwindows->total; i++)
3983 subwindows->values[i]->dispatch_resize_event(w, h);
3986 draw_background(0, 0, w, h);
3987 if(top_level == this && get_resources()->recursive_resizing)
3988 resize_history.append(new BC_ResizeCall(w, h));
3992 // The only way for resize events to be propagated is by updating the internal w and h
3993 int BC_WindowBase::resize_event(int w, int h)
3995 if(window_type == MAIN_WINDOW)
4003 int BC_WindowBase::reposition_window(int x, int y)
4005 reposition_window(x, y, -1, -1);
4010 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
4014 // Some tools set their own dimensions before calling this, causing the
4015 // resize check to skip.
4019 if(w > 0 && w != this->w)
4025 if(h > 0 && h != this->h)
4031 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
4034 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
4036 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
4038 if(translation_count && window_type == MAIN_WINDOW)
4040 // KDE shifts window right and down.
4041 // FVWM leaves window alone and adds border around it.
4042 XMoveResizeWindow(top_level->display,
4044 x - BC_DisplayInfo::auto_reposition_x,
4045 y - BC_DisplayInfo::auto_reposition_y,
4051 XMoveResizeWindow(top_level->display,
4062 pixmap = new BC_Pixmap(this, this->w, this->h);
4063 clear_box(0,0, this->w, this->h);
4064 // Propagate to menubar
4065 for(int i = 0; i < subwindows->total; i++)
4067 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
4070 // draw_background(0, 0, w, h);
4076 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
4078 return reposition_window(get_x()+dx, get_y()+dy, w, h);
4081 int BC_WindowBase::reposition_window_relative(int dx, int dy)
4083 return reposition_window_relative(dx, dy, -1, -1);
4086 void BC_WindowBase::set_tooltips(int v)
4088 get_resources()->tooltips_enabled = v;
4091 void BC_WindowBase::set_force_tooltip(int v)
4096 int BC_WindowBase::raise_window(int do_flush)
4098 XRaiseWindow(top_level->display, win);
4099 if(do_flush) XFlush(top_level->display);
4103 int BC_WindowBase::lower_window(int do_flush)
4105 XLowerWindow(top_level->display, win);
4106 if(do_flush) XFlush(top_level->display);
4110 void BC_WindowBase::set_background(VFrame *bitmap)
4112 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4114 bg_pixmap = new BC_Pixmap(this, bitmap, PIXMAP_OPAQUE);
4115 shared_bg_pixmap = 0;
4116 draw_background(0, 0, w, h);
4119 void BC_WindowBase::put_title(const char *text)
4121 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4122 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4123 *cp++ = *bp >= ' ' ? *bp : ' ';
4127 void BC_WindowBase::set_title(const char *text)
4132 txlist[0] = this->title;
4135 XTextProperty titleprop;
4136 XmbTextListToTextProperty(top_level->display, txlist, 1,
4137 XStdICCTextStyle, &titleprop);
4138 XSetWMName(top_level->display, top_level->win, &titleprop);
4139 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4140 XFree(titleprop.value);
4145 void BC_WindowBase::set_utf8title(const char *text)
4147 XTextProperty titleprop;
4150 strcpy(this->title, text);
4151 txlist[0] = this->title;
4154 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4155 XUTF8StringStyle, &titleprop);
4156 XSetWMName(top_level->display, top_level->win, &titleprop);
4157 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4158 XFree(titleprop.value);
4163 const char *BC_WindowBase::get_title()
4168 int BC_WindowBase::get_toggle_value()
4170 return toggle_value;
4173 int BC_WindowBase::get_toggle_drag()
4178 int BC_WindowBase::set_icon(VFrame *data)
4180 if(icon_pixmap) delete icon_pixmap;
4181 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4183 if(icon_window) delete icon_window;
4184 icon_window = new BC_Popup(this,
4187 icon_pixmap->get_w(),
4188 icon_pixmap->get_h(),
4190 1, // All windows are hidden initially
4194 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4195 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4196 wm_hints.icon_mask = icon_pixmap->get_alpha();
4197 wm_hints.icon_window = icon_window->win;
4198 wm_hints.window_group = XGroupLeader;
4200 // for(int i = 0; i < 1000; i++)
4201 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4204 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4205 XSync(top_level->display, 0);
4209 int BC_WindowBase::set_w(int w)
4215 int BC_WindowBase::set_h(int h)
4221 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4223 BC_Resources *resources = get_resources();
4224 char string[BCTEXTLEN];
4225 int newest_id = - 1;
4226 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4228 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4229 resources->filebox_history[i].path[0] = 0;
4230 defaults->get(string, resources->filebox_history[i].path);
4231 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4232 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4233 if(resources->filebox_history[i].id > newest_id)
4234 newest_id = resources->filebox_history[i].id;
4237 resources->filebox_id = newest_id + 1;
4238 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4239 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4240 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4241 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4242 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4243 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4244 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4245 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4246 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4247 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4248 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4249 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4253 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4255 BC_Resources *resources = get_resources();
4256 char string[BCTEXTLEN];
4257 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4259 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4260 defaults->update(string, resources->filebox_history[i].path);
4261 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4262 defaults->update(string, resources->filebox_history[i].id);
4264 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4265 defaults->update("FILEBOX_W", resources->filebox_w);
4266 defaults->update("FILEBOX_H", resources->filebox_h);
4267 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4268 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4269 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4270 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4271 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4272 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4273 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4274 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4275 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4281 // For some reason XTranslateCoordinates can take a long time to return.
4282 // We work around this by only calling it when the event windows are different.
4283 void BC_WindowBase::translate_coordinates(Window src_w,
4295 *dest_x_return = src_x;
4296 *dest_y_return = src_y;
4300 XTranslateCoordinates(top_level->display,
4308 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4312 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4314 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4317 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4319 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4327 #ifdef HAVE_LIBXXF86VM
4328 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4332 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4334 XF86VidModeModeInfo **vm_modelines;
4335 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4336 for (i = 0; i < vm_count; i++) {
4337 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4340 display = top_level->display;
4341 if (vm_modelines[*vm]->hdisplay == *width)
4345 *width = vm_modelines[*vm]->hdisplay;
4346 *height = vm_modelines[*vm]->vdisplay;
4351 void BC_WindowBase::scale_vm(int vm)
4353 int foo,bar,dotclock;
4354 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4357 XF86VidModeModeInfo **vm_modelines;
4358 XF86VidModeModeLine vml;
4359 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4360 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4361 orig_modeline.dotclock = dotclock;
4362 orig_modeline.hdisplay = vml.hdisplay;
4363 orig_modeline.hsyncstart = vml.hsyncstart;
4364 orig_modeline.hsyncend = vml.hsyncend;
4365 orig_modeline.htotal = vml.htotal;
4366 orig_modeline.vdisplay = vml.vdisplay;
4367 orig_modeline.vsyncstart = vml.vsyncstart;
4368 orig_modeline.vsyncend = vml.vsyncend;
4369 orig_modeline.vtotal = vml.vtotal;
4370 orig_modeline.flags = vml.flags;
4371 orig_modeline.privsize = vml.privsize;
4372 // orig_modeline.private = vml.private;
4373 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4374 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4375 XFlush(top_level->display);
4379 void BC_WindowBase::restore_vm()
4381 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4382 XFlush(top_level->display);
4405 #ifndef SINGLE_THREAD
4406 int BC_WindowBase::get_event_count()
4408 event_lock->lock("BC_WindowBase::get_event_count");
4409 int result = common_events.total;
4410 event_lock->unlock();
4414 XEvent* BC_WindowBase::get_event()
4417 while(!done && !result)
4419 event_condition->lock("BC_WindowBase::get_event");
4420 event_lock->lock("BC_WindowBase::get_event");
4422 if(common_events.total && !done)
4424 result = common_events.values[0];
4425 common_events.remove_number(0);
4428 event_lock->unlock();
4433 void BC_WindowBase::put_event(XEvent *event)
4435 event_lock->lock("BC_WindowBase::put_event");
4436 common_events.append(event);
4437 event_lock->unlock();
4438 event_condition->unlock();
4441 void BC_WindowBase::dequeue_events(Window win)
4443 event_lock->lock("BC_WindowBase::dequeue_events");
4445 int out = 0, total = common_events.size();
4446 for( int in=0; in<total; ++in ) {
4447 if( common_events[in]->xany.window == win ) continue;
4448 common_events[out++] = common_events[in];
4450 common_events.total = out;
4452 event_lock->unlock();
4455 #endif // SINGLE_THREAD
4457 int BC_WindowBase::get_id()
4463 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4465 int w = vframe->get_w(), h = vframe->get_h();
4466 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4467 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4472 void BC_WindowBase::flicker(int n, int ms)
4474 int color = get_bg_color();
4475 for( int i=2*n; --i>=0; ) {
4476 set_inverse(); set_bg_color(WHITE);
4477 clear_box(0,0, w,h); flash(1);
4478 sync_display(); Timer::delay(ms);
4480 set_bg_color(color);