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 motion_events = resize_events = translation_events = 0;
117 #ifndef SINGLE_THREAD
118 top_level->dequeue_events(win);
120 if(top_level->active_menubar == this) top_level->active_menubar = 0;
121 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
122 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
123 // Remove pointer from parent window to this
124 parent_window->subwindows->remove(this);
127 if(icon_window) delete icon_window;
128 if(window_type == POPUP_WINDOW)
129 parent_window->remove_popup(this);
131 // Delete the subwindows
134 while(subwindows->total)
136 // Subwindow removes its own pointer
137 delete subwindows->values[0];
145 if( get_resources()->get_synchronous() &&
146 (top_level->options & GLX_WINDOW) && glx_win != 0 )
147 get_resources()->get_synchronous()->delete_window(this);
150 XDestroyWindow(top_level->display, win);
152 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
153 if(icon_pixmap) delete icon_pixmap;
154 if(temp_bitmap) delete temp_bitmap;
155 top_level->active_bitmaps.remove_buffers(this);
156 if(_7segment_pixmaps)
158 for(int i = 0; i < TOTAL_7SEGMENT; i++)
159 delete _7segment_pixmaps[i];
161 delete [] _7segment_pixmaps;
166 if(window_type == MAIN_WINDOW)
168 XFreeGC(display, gc);
169 static XFontStruct *BC_WindowBase::*xfont[] = {
170 &BC_WindowBase::smallfont,
171 &BC_WindowBase::mediumfont,
172 &BC_WindowBase::largefont,
173 &BC_WindowBase::bigfont,
175 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
176 XFreeFont(display, this->*xfont[i]);
178 // bug in X causes XRenderExtensionInfo to be damaged if this is done here
179 // left to be done in XCloseDisplay by Xlib.
180 #if defined(HAVE_XFT) && 0
181 static void *BC_WindowBase::*xft_font[] = {
182 &BC_WindowBase::smallfont_xft,
183 &BC_WindowBase::mediumfont_xft,
184 &BC_WindowBase::largefont_xft,
185 &BC_WindowBase::bigfont_xft,
186 &BC_WindowBase::bold_smallfont_xft,
187 &BC_WindowBase::bold_mediumfont_xft,
188 &BC_WindowBase::bold_largefont_xft,
190 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
191 XftFont *xft = (XftFont *)(this->*xft_font[i]);
192 if( xft ) XftFontClose (display, xft);
200 XFree(xinerama_info);
201 xinerama_screens = 0;
203 if( xvideo_port_id >= 0 )
204 XvUngrabPort(display, xvideo_port_id, CurrentTime);
207 // Can't close display if another thread is waiting for events.
208 // Synchronous thread must delete display it owns a GLX_WINDOW
209 // Must be last reference to display.
210 #ifndef SINGLE_THREAD
212 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
213 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
214 "implemented for BC_Pixmap.\n"));
215 get_resources()->get_synchronous()->delete_display(this);
219 XCloseDisplay(display);
220 // clipboard uses a different display connection
221 clipboard->stop_clipboard();
223 #endif // SINGLE_THREAD
226 resize_history.remove_all_objects();
228 #ifndef SINGLE_THREAD
229 common_events.remove_all_objects();
231 delete event_condition;
234 top_level->window_lock = 0;
235 BC_Display::unlock_display();
240 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
241 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
242 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
245 UNSET_ALL_LOCKS(this)
248 int BC_WindowBase::initialize()
253 display_lock_owner = 0;
269 xinerama_screens = 0;
274 translation_events = 0;
275 ctrl_mask = shift_mask = alt_mask = 0;
276 cursor_x = cursor_y = button_number = 0;
288 active_popup_menu = 0;
289 active_subwindow = 0;
292 _7segment_pixmaps = 0;
295 // next_repeat_id = 0;
298 current_font = MEDIUMFONT;
299 current_color = BLACK;
300 current_cursor = ARROW_CURSOR;
303 shared_bg_pixmap = 0;
306 window_type = MAIN_WINDOW;
307 translation_count = 0;
308 x_correction = y_correction = 0;
317 #ifdef HAVE_LIBXXF86VM
321 bold_largefont_xft = 0;
322 bold_mediumfont_xft = 0;
323 bold_smallfont_xft = 0;
331 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
333 // Need these right away since put_event is called before run_window sometimes.
334 event_lock = new Mutex("BC_WindowBase::event_lock");
335 event_condition = new Condition(0, "BC_WindowBase::event_condition");
336 init_lock = new Condition(0, "BC_WindowBase::init_lock");
339 cursor_timer = new Timer;
342 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
343 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
344 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
358 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
361 ButtonReleaseMask | \
362 PointerMotionMask | \
366 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
378 const char *display_name,
380 BC_Pixmap *bg_pixmap,
383 XSetWindowAttributes attr;
385 XSizeHints size_hints;
388 #ifdef HAVE_LIBXXF86VM
392 id = get_resources()->get_id();
394 if(parent_window) top_level = parent_window->top_level;
395 if( top_level ) { // need this to avoid deadlock with Xlib's locks
397 lock_window("BC_WindowBase::create_window");
399 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
401 #ifdef HAVE_LIBXXF86VM
402 if(window_type == VIDMODE_SCALED_WINDOW)
403 closest_vm(&vm,&w,&h);
410 this->bg_color = bg_color;
411 this->window_type = window_type;
413 this->private_color = private_color;
414 this->parent_window = parent_window;
415 this->bg_pixmap = bg_pixmap;
416 this->allow_resize = allow_resize;
418 strcpy(this->display_name, display_name);
420 this->display_name[0] = 0;
423 if(bg_pixmap) shared_bg_pixmap = 1;
425 subwindows = new BC_SubWindowList;
427 if(window_type == MAIN_WINDOW)
430 parent_window = this;
434 display = BC_Display::get_display(display_name);
435 BC_Display::lock_display("BC_WindowBase::create_window");
436 // BC_Display::display_global->new_window(this);
439 // get the display connection
441 // This function must be the first Xlib
442 // function a multi-threaded program calls
444 display = init_display(display_name);
445 if( shm_completion_event < 0 ) shm_completion_event =
446 ShmCompletion + XShmGetEventBase(display);
449 screen = DefaultScreen(display);
450 rootwin = RootWindow(display, screen);
451 // window placement boundaries
452 if( !xinerama_screens && XineramaIsActive(display) )
453 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
454 root_w = get_root_w(0);
455 root_h = get_root_h(0);
458 vis = get_glx_visual(display);
461 vis = DefaultVisual(display, screen);
463 default_depth = DefaultDepth(display, screen);
465 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
466 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
469 // This must be done before fonts to know if antialiasing is available.
472 if(resources.use_shm < 0) resources.initialize_display(this);
473 x_correction = BC_DisplayInfo::get_left_border();
474 y_correction = BC_DisplayInfo::get_top_border();
476 // clamp window placement
477 if(this->x + this->w + x_correction > root_w)
478 this->x = root_w - this->w - x_correction;
479 if(this->y + this->h + y_correction > root_h)
480 this->y = root_h - this->h - y_correction;
481 if(this->x < 0) this->x = 0;
482 if(this->y < 0) this->y = 0;
484 if(this->bg_color == -1)
485 this->bg_color = resources.get_bg_color();
487 // printf("bcwindowbase 1 %s\n", title);
488 // if(window_type == MAIN_WINDOW) sleep(1);
489 // printf("bcwindowbase 10\n");
495 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
497 attr.event_mask = DEFAULT_EVENT_MASKS |
498 StructureNotifyMask |
502 attr.background_pixel = get_color(this->bg_color);
503 attr.colormap = cmap;
504 attr.cursor = get_cursor_struct(ARROW_CURSOR);
506 win = XCreateWindow(display, rootwin,
507 this->x, this->y, this->w, this->h, 0,
508 top_level->default_depth, InputOutput,
510 XGetNormalHints(display, win, &size_hints);
512 size_hints.flags = PSize | PMinSize | PMaxSize;
513 size_hints.width = this->w;
514 size_hints.height = this->h;
515 size_hints.min_width = allow_resize ? minw : this->w;
516 size_hints.max_width = allow_resize ? 32767 : this->w;
517 size_hints.min_height = allow_resize ? minh : this->h;
518 size_hints.max_height = allow_resize ? 32767 : this->h;
519 if(x > -BC_INFINITY && x < BC_INFINITY)
521 size_hints.flags |= PPosition;
522 size_hints.x = this->x;
523 size_hints.y = this->y;
527 txlist[0] = this->title;
529 XTextProperty titleprop;
530 if(options & WINDOW_UTF8)
531 Xutf8TextListToTextProperty(display, txlist, 1,
532 XUTF8StringStyle, &titleprop);
534 XmbTextListToTextProperty(display, txlist, 1,
535 XStdICCTextStyle, &titleprop);
536 XSetWMProperties(display, win, &titleprop, &titleprop,
537 0, 0, &size_hints, 0, 0);
538 XFree(titleprop.value);
541 #ifndef SINGLE_THREAD
542 clipboard = new BC_Clipboard(display_name);
543 clipboard->start_clipboard();
549 Atom ClientLeaderXAtom;
550 if (XGroupLeader == 0)
552 const char *instance_name = "cinelerra";
553 const char *class_name = "Cinelerra";
554 XClassHint *class_hints = XAllocClassHint();
555 class_hints->res_name = (char*)instance_name;
556 class_hints->res_class = (char*)class_name;
557 XSetClassHint(top_level->display, win, class_hints);
559 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
560 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
561 PropModeReplace, (unsigned char *)&XGroupLeader, true);
566 #ifdef HAVE_LIBXXF86VM
567 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
574 #ifdef HAVE_LIBXXF86VM
575 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
577 if(window_type == POPUP_WINDOW)
580 mask = CWEventMask | CWBackPixel | CWColormap |
581 CWOverrideRedirect | CWSaveUnder | CWCursor;
583 attr.event_mask = DEFAULT_EVENT_MASKS |
587 if(this->bg_color == -1)
588 this->bg_color = resources.get_bg_color();
589 attr.background_pixel = top_level->get_color(bg_color);
590 attr.colormap = top_level->cmap;
591 if(top_level->is_hourglass)
592 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
594 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
595 attr.override_redirect = True;
596 attr.save_under = True;
598 win = XCreateWindow(top_level->display,
599 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
600 top_level->default_depth, InputOutput, top_level->vis, mask,
602 top_level->add_popup(this);
605 if(window_type == SUB_WINDOW)
607 mask = CWEventMask | CWBackPixel | CWCursor;
608 attr.event_mask = DEFAULT_EVENT_MASKS;
609 attr.background_pixel = top_level->get_color(this->bg_color);
610 if(top_level->is_hourglass)
611 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
613 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
614 win = XCreateWindow(top_level->display,
615 parent_window->win, this->x, this->y, this->w, this->h, 0,
616 top_level->default_depth, InputOutput, top_level->vis, mask,
619 if(!hidden) XMapWindow(top_level->display, win);
622 // Create pixmap for all windows
623 pixmap = new BC_Pixmap(this, this->w, this->h);
625 // Set up options for main window
626 if(window_type == MAIN_WINDOW)
628 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
630 this->bg_pixmap = new BC_Pixmap(this,
631 get_resources()->bg_image,
635 if(!hidden) show_window();
639 draw_background(0, 0, this->w, this->h);
641 flash(-1, -1, -1, -1, 0);
643 // Set up options for popup window
644 #ifdef HAVE_LIBXXF86VM
645 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
647 if(window_type == POPUP_WINDOW)
651 if(!hidden) show_window();
653 get_resources()->create_window_lock->unlock();
654 if(need_lock) unlock_window();
659 Display* BC_WindowBase::init_display(const char *display_name)
663 if(display_name && display_name[0] == 0) display_name = NULL;
664 if((display = XOpenDisplay(display_name)) == NULL) {
665 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
667 if(getenv("DISPLAY") == NULL) {
668 printf(_("'DISPLAY' environment variable not set.\n"));
672 // Try again with default display.
673 if((display = XOpenDisplay(0)) == NULL) {
674 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
682 Display* BC_WindowBase::get_display()
684 return top_level->display;
687 int BC_WindowBase::get_screen()
689 return top_level->screen;
692 int BC_WindowBase::run_window()
698 // Events may have been sent before run_window so can't initialize them here.
701 set_repeat(get_resources()->tooltip_delay);
702 BC_Display::display_global->new_window(this);
704 // If the first window created, run the display loop in this thread.
705 if(BC_Display::display_global->is_first(this))
707 BC_Display::unlock_display();
708 BC_Display::display_global->loop();
712 BC_Display::unlock_display();
713 completion_lock->lock("BC_WindowBase::run_window");
716 BC_Display::lock_display("BC_WindowBase::run_window");
717 BC_Display::display_global->delete_window(this);
719 unset_all_repeaters();
721 BC_Display::unlock_display();
723 #else // SINGLE_THREAD
728 set_repeat(get_resources()->tooltip_delay);
730 // Start X server events
731 event_thread = new BC_WindowEvents(this);
732 event_thread->start();
738 // Handle common events
744 unset_all_repeaters();
748 event_condition->reset();
749 common_events.remove_all_objects();
753 #endif // SINGLE_THREAD
758 int BC_WindowBase::get_key_masks(unsigned int key_state)
760 // printf("BC_WindowBase::get_key_masks %llx\n",
761 // event->xkey.state);
762 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
763 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
764 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
769 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
771 BC_KeyboardHandlerLock set;
772 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
775 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
777 BC_KeyboardHandlerLock set;
778 int i = BC_KeyboardHandler::listeners.size();
779 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
780 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
783 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
785 int result = (win->*handler)(wp);
789 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
792 BC_KeyboardHandlerLock set;
793 for( int i=0; !result && i<listeners.size(); ++i ) {
794 BC_KeyboardHandler *listener = listeners[i];
795 result = listener->run_event(wp);
800 void BC_KeyboardHandler::kill_grabs()
802 BC_KeyboardHandlerLock set;
803 for( int i=0; i<listeners.size(); ++i ) {
804 BC_WindowBase *win = listeners[i]->win;
805 if( win->get_window_type() != POPUP_WINDOW ) continue;
806 ((BC_Popup *)win)->ungrab_keyboard();
810 void BC_ActiveBitmaps::reque(XEvent *event)
812 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
813 ShmSeg shmseg = shm_ev->shmseg;
814 Drawable drawable = shm_ev->drawable;
815 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
816 active_lock.lock("BC_BitmapImage::reque");
817 BC_BitmapImage *bfr = first;
818 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
819 if( bfr && bfr->drawable == drawable )
821 active_lock.unlock();
823 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
824 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
825 // (int)shmseg, (int)drawable);
828 if( bfr->drawable != drawable ) return;
829 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
830 bfr->bitmap->reque(bfr);
833 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
835 active_lock.lock("BC_BitmapImage::insert");
836 bfr->drawable = pixmap;
838 active_lock.unlock();
841 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
843 active_lock.lock("BC_ActiveBitmaps::remove");
844 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
846 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
847 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
849 active_lock.unlock();
852 BC_ActiveBitmaps::BC_ActiveBitmaps()
856 BC_ActiveBitmaps::~BC_ActiveBitmaps()
862 int BC_WindowBase::keysym_lookup(XEvent *event)
864 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
865 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
867 if( event->xany.send_event && !event->xany.serial ) {
868 keysym = (KeySym) event->xkey.keycode;
869 keys_return[0] = keysym;
872 wkey_string_length = 0;
874 if( input_context ) {
875 wkey_string_length = XwcLookupString(input_context,
876 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
877 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
878 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
881 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
882 keys_return, KEYPRESSLEN, &keysym, &stat);
883 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
884 if( stat == XLookupBoth ) return ret;
885 if( stat == XLookupKeySym ) return 0;
887 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
888 wkey_string_length = ret;
889 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
893 pthread_t locking_task = (pthread_t)-1L;
894 int locking_event = -1;
895 int locking_message = -1;
897 int BC_WindowBase::dispatch_event(XEvent *event)
901 XClientMessageEvent *ptr;
902 int cancel_resize, cancel_translation;
907 #ifndef SINGLE_THREAD
908 // If an event is waiting get it, otherwise
909 // wait for next event only if there are no compressed events.
910 if(get_event_count() ||
911 (!motion_events && !resize_events && !translation_events))
914 // Lock out window deletions
915 lock_window("BC_WindowBase::dispatch_event 1");
916 locking_event = event->type;
917 locking_task = pthread_self();
918 locking_message = event->xclient.message_type;
921 // Handle compressed events
923 lock_window("BC_WindowBase::dispatch_event 2");
925 dispatch_resize_event(last_resize_w, last_resize_h);
927 dispatch_motion_event();
928 if(translation_events)
929 dispatch_translation_event();
940 //static const char *event_names[] = {
941 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
942 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
943 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
944 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
945 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
946 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
947 // "GenericEvent", "LASTEvent",
951 //if(event->type != ClientMessage) {
952 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
953 //__LINE__, title, event, event->type,
954 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
955 // event_names[event->type] : "Unknown");
961 switch(event->type) {
963 // Clear the resize buffer
964 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
965 // Clear the motion buffer since this can clear the window
968 dispatch_motion_event();
971 ptr = (XClientMessageEvent*)event;
974 if(ptr->message_type == ProtoXAtom &&
975 (Atom)ptr->data.l[0] == DelWinXAtom)
980 if(ptr->message_type == RepeaterXAtom)
982 dispatch_repeat_event(ptr->data.l[0]);
983 // Make sure the repeater still exists.
984 // for(int i = 0; i < repeaters.total; i++)
986 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
988 // dispatch_repeat_event_master(ptr->data.l[0]);
994 if(ptr->message_type == SetDoneXAtom)
998 { // We currently use X marshalling for xatom events, we can switch to something else later
999 recieve_custom_xatoms((xatom_event *)ptr);
1005 dispatch_focus_in();
1010 dispatch_focus_out();
1022 get_key_masks(event->xbutton.state);
1023 cursor_x = event->xbutton.x;
1024 cursor_y = event->xbutton.y;
1025 button_number = event->xbutton.button;
1027 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1028 event_win = event->xany.window;
1029 if (button_number < 6)
1031 if(button_number < 4)
1033 button_pressed = event->xbutton.button;
1034 button_time1 = button_time2;
1035 button_time2 = button_time3;
1036 button_time3 = event->xbutton.time;
1039 drag_win = event_win;
1040 drag_x1 = cursor_x - get_resources()->drag_radius;
1041 drag_x2 = cursor_x + get_resources()->drag_radius;
1042 drag_y1 = cursor_y - get_resources()->drag_radius;
1043 drag_y2 = cursor_y + get_resources()->drag_radius;
1045 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1048 button_time3 = button_time2 = button_time1 = 0;
1050 if((long)(button_time3 - button_time2) < resources.double_click)
1053 // button_time3 = button_time2 = button_time1 = 0;
1061 dispatch_button_press();
1066 get_key_masks(event->xbutton.state);
1067 button_number = event->xbutton.button;
1068 event_win = event->xany.window;
1069 if (button_number < 6)
1071 if(button_number < 4)
1073 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1075 dispatch_button_release();
1080 event_win = event->xany.window;
1081 dispatch_expose_event();
1085 get_key_masks(event->xmotion.state);
1086 // Dispatch previous motion event if this is a subsequent motion from a different window
1087 if(motion_events && last_motion_win != event->xany.window)
1089 dispatch_motion_event();
1092 // Buffer the current motion
1094 last_motion_state = event->xmotion.state;
1095 last_motion_x = event->xmotion.x;
1096 last_motion_y = event->xmotion.y;
1097 last_motion_win = event->xany.window;
1100 case ConfigureNotify:
1101 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1103 // event->xany.window,
1106 XTranslateCoordinates(top_level->display,
1114 last_resize_w = event->xconfigure.width;
1115 last_resize_h = event->xconfigure.height;
1118 cancel_translation = 0;
1120 // Resize history prevents responses to recursive resize requests
1121 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1123 if(resize_history.values[i]->w == last_resize_w &&
1124 resize_history.values[i]->h == last_resize_h)
1126 delete resize_history.values[i];
1127 resize_history.remove_number(i);
1132 if(last_resize_w == w && last_resize_h == h)
1140 if((last_translate_x == x && last_translate_y == y))
1141 cancel_translation = 1;
1143 if(!cancel_translation)
1145 translation_events = 1;
1148 translation_count++;
1152 get_key_masks(event->xkey.state);
1153 keys_return[0] = 0; keysym = -1;
1154 if(XFilterEvent(event, win)) {
1157 if( keysym_lookup(event) < 0 ) {
1158 printf("keysym %x\n", (uint32_t)keysym);
1162 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1166 // block out control keys
1167 if(keysym > 0xffe0 && keysym < 0xffff) break;
1168 // block out Alt_GR key
1169 if(keysym == 0xfe03) break;
1172 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1174 #ifdef X_HAVE_UTF8_STRING
1175 //It's Ascii or UTF8?
1176 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1177 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1179 if( ((keys_return[1] & 0xff) > 0x80) &&
1180 ((keys_return[0] & 0xff) > 0xC0) ) {
1181 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1182 key_pressed = keysym & 0xff;
1188 // block out extra keys
1198 // Translate key codes
1199 case XK_Return: key_pressed = RETURN; break;
1200 case XK_Up: key_pressed = UP; break;
1201 case XK_Down: key_pressed = DOWN; break;
1202 case XK_Left: key_pressed = LEFT; break;
1203 case XK_Right: key_pressed = RIGHT; break;
1204 case XK_Next: key_pressed = PGDN; break;
1205 case XK_Prior: key_pressed = PGUP; break;
1206 case XK_BackSpace: key_pressed = BACKSPACE; break;
1207 case XK_Escape: key_pressed = ESC; break;
1210 key_pressed = LEFTTAB;
1214 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1215 case XK_underscore: key_pressed = '_'; break;
1216 case XK_asciitilde: key_pressed = '~'; break;
1217 case XK_Delete: key_pressed = DELETE; break;
1218 case XK_Home: key_pressed = HOME; break;
1219 case XK_End: key_pressed = END; break;
1222 case XK_KP_Enter: key_pressed = KPENTER; break;
1223 case XK_KP_Add: key_pressed = KPPLUS; break;
1224 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1225 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1226 case XK_KP_Divide: key_pressed = KPSLASH; break;
1228 case XK_KP_End: key_pressed = KP1; break;
1230 case XK_KP_Down: key_pressed = KP2; break;
1232 case XK_KP_Page_Down: key_pressed = KP3; break;
1234 case XK_KP_Left: key_pressed = KP4; break;
1236 case XK_KP_Begin: key_pressed = KP5; break;
1238 case XK_KP_Right: key_pressed = KP6; break;
1240 case XK_KP_Home: key_pressed = KP7; break;
1242 case XK_KP_Up: key_pressed = KP8; break;
1244 case XK_KP_Page_Up: key_pressed = KP9; break;
1246 case XK_KP_Insert: key_pressed = KPINS; break;
1248 case XK_KP_Delete: key_pressed = KPDEL; break;
1249 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1251 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1252 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1253 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1254 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1255 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1256 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1257 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1258 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1259 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1260 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1261 // above case XK_Left: key_pressed = LEFT; break; /* left */
1262 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1263 // above case XK_Down: key_pressed = DOWN; break; /* down */
1264 // above case XK_Up: key_pressed = UP; break; /* up */
1265 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1266 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1267 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1268 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1269 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1270 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1271 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1272 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1273 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1274 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1275 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1276 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1279 key_pressed = keysym & 0xff;
1280 #ifdef X_HAVE_UTF8_STRING
1281 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1286 #ifdef X_HAVE_UTF8_STRING
1288 key_pressed_utf8 = keys_return;
1293 if( top_level == this )
1294 result = BC_KeyboardHandler::run_listeners(this);
1296 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1298 result = dispatch_keypress_event();
1299 // Handle some default keypresses
1302 if(key_pressed == 'w' ||
1311 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1312 dispatch_keyrelease_event();
1313 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1314 // keysym, event->xkey.state);
1318 event_win = event->xany.window;
1319 dispatch_cursor_leave();
1323 event_win = event->xany.window;
1324 cursor_x = event->xcrossing.x;
1325 cursor_y = event->xcrossing.y;
1326 dispatch_cursor_enter();
1331 //printf("100 %s %p %d\n", title, event, event->type);
1332 //if(event->type != ClientMessage) dump();
1334 #ifndef SINGLE_THREAD
1336 if(event) delete event;
1338 // if(done) completion_lock->unlock();
1341 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1345 int BC_WindowBase::dispatch_expose_event()
1348 for(int i = 0; i < subwindows->total && !result; i++)
1350 result = subwindows->values[i]->dispatch_expose_event();
1353 // Propagate to user
1354 if(!result) expose_event();
1358 int BC_WindowBase::dispatch_resize_event(int w, int h)
1360 // Can't store new w and h until the event is handles
1361 // because bcfilebox depends on the old w and h to
1362 // reposition widgets.
1363 if( window_type == MAIN_WINDOW ) {
1368 pixmap = new BC_Pixmap(this, w, h);
1369 clear_box(0, 0, w, h);
1372 // Propagate to subwindows
1373 for(int i = 0; i < subwindows->total; i++) {
1374 subwindows->values[i]->dispatch_resize_event(w, h);
1377 // Propagate to user
1380 if( window_type == MAIN_WINDOW ) {
1389 int BC_WindowBase::dispatch_flash()
1392 for(int i = 0; i < subwindows->total; i++)
1393 subwindows->values[i]->dispatch_flash();
1397 int BC_WindowBase::dispatch_translation_event()
1399 translation_events = 0;
1400 if(window_type == MAIN_WINDOW)
1404 x = last_translate_x;
1405 y = last_translate_y;
1406 // Correct for window manager offsets
1411 for(int i = 0; i < subwindows->total; i++)
1413 subwindows->values[i]->dispatch_translation_event();
1416 translation_event();
1420 int BC_WindowBase::dispatch_motion_event()
1425 if(top_level == this)
1428 event_win = last_motion_win;
1429 get_key_masks(last_motion_state);
1432 if(get_button_down() && !active_menubar && !active_popup_menu)
1436 cursor_x = last_motion_x;
1437 cursor_y = last_motion_y;
1438 result = dispatch_drag_motion();
1442 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1443 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1448 result = dispatch_drag_start();
1452 cursor_x = last_motion_x;
1453 cursor_y = last_motion_y;
1455 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1458 // active_popup_menu,
1459 // active_subwindow);
1461 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1462 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1463 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1466 // Dispatch in stacking order
1467 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1469 result = subwindows->values[i]->dispatch_motion_event();
1472 if(!result) result = cursor_motion_event(); // give to user
1476 int BC_WindowBase::dispatch_keypress_event()
1479 if(top_level == this)
1481 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1484 for(int i = 0; i < subwindows->total && !result; i++)
1486 result = subwindows->values[i]->dispatch_keypress_event();
1489 if(!result) result = keypress_event();
1494 int BC_WindowBase::dispatch_keyrelease_event()
1497 if(top_level == this)
1499 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1502 for(int i = 0; i < subwindows->total && !result; i++)
1504 result = subwindows->values[i]->dispatch_keyrelease_event();
1507 if(!result) result = keyrelease_event();
1512 int BC_WindowBase::dispatch_focus_in()
1514 for(int i = 0; i < subwindows->total; i++)
1516 subwindows->values[i]->dispatch_focus_in();
1524 int BC_WindowBase::dispatch_focus_out()
1526 for(int i = 0; i < subwindows->total; i++)
1528 subwindows->values[i]->dispatch_focus_out();
1536 int BC_WindowBase::get_has_focus()
1538 return top_level->has_focus;
1541 int BC_WindowBase::get_deleting()
1543 if(is_deleting) return 1;
1544 if(parent_window && parent_window->get_deleting()) return 1;
1548 int BC_WindowBase::dispatch_button_press()
1553 if(top_level == this)
1555 if(active_menubar) result = active_menubar->dispatch_button_press();
1556 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1557 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1560 for(int i = 0; i < subwindows->total && !result; i++)
1562 result = subwindows->values[i]->dispatch_button_press();
1565 if(!result) result = button_press_event();
1571 int BC_WindowBase::dispatch_button_release()
1574 if(top_level == this)
1576 if(active_menubar) result = active_menubar->dispatch_button_release();
1577 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1578 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1579 if(!result && button_number != 4 && button_number != 5)
1580 result = dispatch_drag_stop();
1583 for(int i = 0; i < subwindows->total && !result; i++)
1585 result = subwindows->values[i]->dispatch_button_release();
1590 result = button_release_event();
1597 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1600 // all repeat event handlers get called and decide based on activity and duration
1601 // whether to respond
1602 for(int i = 0; i < subwindows->total; i++)
1604 subwindows->values[i]->dispatch_repeat_event(duration);
1608 repeat_event(duration);
1612 // Unlock next repeat signal
1613 if(window_type == MAIN_WINDOW)
1615 #ifdef SINGLE_THREAD
1616 BC_Display::display_global->unlock_repeaters(duration);
1618 for(int i = 0; i < repeaters.total; i++)
1620 if(repeaters.values[i]->delay == duration)
1622 repeaters.values[i]->repeat_lock->unlock();
1630 void BC_WindowBase::unhide_cursor()
1635 if(top_level->is_hourglass)
1636 set_cursor(HOURGLASS_CURSOR, 1, 0);
1638 set_cursor(current_cursor, 1, 0);
1640 cursor_timer->update();
1644 void BC_WindowBase::update_video_cursor()
1646 if(video_on && !is_transparent)
1648 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1651 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1652 cursor_timer->update();
1657 cursor_timer->update();
1662 int BC_WindowBase::dispatch_cursor_leave()
1666 for(int i = 0; i < subwindows->total; i++)
1668 subwindows->values[i]->dispatch_cursor_leave();
1671 cursor_leave_event();
1675 int BC_WindowBase::dispatch_cursor_enter()
1681 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1682 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1683 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1685 for(int i = 0; !result && i < subwindows->total; i++)
1687 result = subwindows->values[i]->dispatch_cursor_enter();
1690 if(!result) result = cursor_enter_event();
1694 int BC_WindowBase::cursor_enter_event()
1699 int BC_WindowBase::cursor_leave_event()
1704 int BC_WindowBase::close_event()
1710 int BC_WindowBase::dispatch_drag_start()
1713 if(active_menubar) result = active_menubar->dispatch_drag_start();
1714 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1715 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1717 for(int i = 0; i < subwindows->total && !result; i++)
1719 result = subwindows->values[i]->dispatch_drag_start();
1722 if(!result) result = is_dragging = drag_start_event();
1726 int BC_WindowBase::dispatch_drag_stop()
1730 for(int i = 0; i < subwindows->total && !result; i++)
1732 result = subwindows->values[i]->dispatch_drag_stop();
1735 if(is_dragging && !result)
1745 int BC_WindowBase::dispatch_drag_motion()
1748 for(int i = 0; i < subwindows->total && !result; i++)
1750 result = subwindows->values[i]->dispatch_drag_motion();
1753 if(is_dragging && !result)
1755 drag_motion_event();
1766 int BC_WindowBase::show_tooltip(int w, int h)
1770 if(tooltip_text && !tooltip_on &&
1771 (force_tooltip || get_resources()->tooltips_enabled))
1774 top_level->hide_tooltip();
1778 w = get_text_width(MEDIUMFONT, tooltip_text);
1781 h = get_text_height(MEDIUMFONT, tooltip_text);
1783 w += TOOLTIP_MARGIN * 2;
1784 h += TOOLTIP_MARGIN * 2;
1786 XTranslateCoordinates(top_level->display, win,
1787 top_level->rootwin, get_w(), get_h(),
1789 // keep the tip inside the window/display
1790 int top_x = top_level->get_x();
1791 if( x < top_x ) x = top_x;
1792 int top_w = top_x + top_level->get_w();
1793 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1794 if( top_w < lmt_w ) lmt_w = top_w;
1795 if( x+w > lmt_w ) x = lmt_w-w;
1797 int top_y = top_level->get_y();
1798 if( y < top_y ) y = top_y;
1799 int top_h = top_y + top_level->get_h();
1800 int lmt_h = top_level->get_root_h(0);
1801 if( top_h < lmt_h ) lmt_h = top_h;
1802 if( y+h > lmt_h ) y = lmt_h-h;
1804 int abs_x, abs_y, win_x, win_y;
1805 unsigned int temp_mask;
1807 XQueryPointer(top_level->display, top_level->win,
1808 &temp_win, &temp_win, &abs_x, &abs_y,
1809 &win_x, &win_y, &temp_mask);
1810 // check for cursor inside popup
1811 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1813 if( x-abs_x < y-abs_y )
1818 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1819 get_resources()->tooltip_bg_color);
1822 tooltip_popup->set_font(MEDIUMFONT);
1823 tooltip_popup->flash();
1824 tooltip_popup->flush();
1829 int BC_WindowBase::hide_tooltip()
1832 for(int i = 0; i < subwindows->total; i++)
1834 subwindows->values[i]->hide_tooltip();
1840 delete tooltip_popup;
1846 const char *BC_WindowBase::get_tooltip()
1848 return tooltip_text;
1851 int BC_WindowBase::set_tooltip(const char *text)
1853 tooltip_text = text;
1855 // Update existing tooltip if it is visible
1859 tooltip_popup->flash();
1864 // signal the event handler to repeat
1865 int BC_WindowBase::set_repeat(int64_t duration)
1869 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1872 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1874 #ifdef SINGLE_THREAD
1875 BC_Display::display_global->set_repeat(this, duration);
1877 // test repeater database for duplicates
1878 for(int i = 0; i < repeaters.total; i++)
1881 if(repeaters.values[i]->delay == duration)
1883 repeaters.values[i]->start_repeating(this);
1888 BC_Repeater *repeater = new BC_Repeater(this, duration);
1889 repeater->initialize();
1890 repeaters.append(repeater);
1891 repeater->start_repeating();
1896 int BC_WindowBase::unset_repeat(int64_t duration)
1898 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1900 #ifdef SINGLE_THREAD
1901 BC_Display::display_global->unset_repeat(this, duration);
1903 for(int i = 0; i < repeaters.total; i++)
1905 if(repeaters.values[i]->delay == duration)
1907 repeaters.values[i]->stop_repeating();
1915 int BC_WindowBase::unset_all_repeaters()
1917 #ifdef SINGLE_THREAD
1918 BC_Display::display_global->unset_all_repeaters(this);
1920 for(int i = 0; i < repeaters.total; i++)
1922 repeaters.values[i]->stop_repeating();
1924 repeaters.remove_all_objects();
1929 // long BC_WindowBase::get_repeat_id()
1931 // return top_level->next_repeat_id++;
1934 XEvent *BC_WindowBase::new_xevent()
1936 XEvent *event = new XEvent;
1937 memset(event, 0, sizeof(*event));
1941 #ifndef SINGLE_THREAD
1942 int BC_WindowBase::arm_repeat(int64_t duration)
1944 XEvent *event = new_xevent();
1945 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1946 ptr->type = ClientMessage;
1947 ptr->message_type = RepeaterXAtom;
1949 ptr->data.l[0] = duration;
1951 // Couldn't use XSendEvent since it locked up randomly.
1957 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1962 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1964 #ifndef SINGLE_THREAD
1965 XEvent *myevent = new_xevent();
1966 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1967 ptr->type = ClientMessage;
1968 ptr->message_type = event->message_type;
1969 ptr->format = event->format;
1970 ptr->data.l[0] = event->data.l[0];
1971 ptr->data.l[1] = event->data.l[1];
1972 ptr->data.l[2] = event->data.l[2];
1973 ptr->data.l[3] = event->data.l[3];
1974 ptr->data.l[4] = event->data.l[4];
1983 Atom BC_WindowBase::create_xatom(const char *atom_name)
1985 return XInternAtom(display, atom_name, False);
1988 int BC_WindowBase::get_atoms()
1990 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1991 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1992 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
1993 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1994 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
1995 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
1996 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2002 void BC_WindowBase::init_cursors()
2004 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2005 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2006 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2007 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2008 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2009 move_cursor = XCreateFontCursor(display, XC_fleur);
2010 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2011 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2012 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2013 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2014 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2015 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2016 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2017 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2020 char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2021 Colormap colormap = DefaultColormap(display, screen);
2022 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2023 rootwin, cursor_data, 8, 8);
2024 XColor black, dummy;
2025 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2026 transparent_cursor = XCreatePixmapCursor(display,
2027 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2028 // XDefineCursor(display, win, transparent_cursor);
2029 XFreePixmap(display, pixmap_bottom);
2032 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2034 int color_model = BC_TRANSPARENCY;
2038 color_model = BC_RGB8;
2041 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2044 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2047 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2053 int BC_WindowBase::init_colors()
2056 current_color_value = current_color_pixel = 0;
2058 // Get the real depth
2061 ximage = XCreateImage(top_level->display,
2063 top_level->default_depth,
2071 bits_per_pixel = ximage->bits_per_pixel;
2072 XDestroyImage(ximage);
2074 color_model = evaluate_color_model(client_byte_order,
2077 // Get the color model
2083 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2084 create_private_colors();
2088 cmap = DefaultColormap(display, screen);
2089 create_shared_colors();
2092 allocate_color_table();
2096 //cmap = DefaultColormap(display, screen);
2097 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2103 int BC_WindowBase::create_private_colors()
2108 for(int i = 0; i < 255; i++)
2110 color = (i & 0xc0) << 16;
2111 color += (i & 0x38) << 10;
2112 color += (i & 0x7) << 5;
2113 color_table[i][0] = color;
2115 create_shared_colors(); // overwrite the necessary colors on the table
2120 int BC_WindowBase::create_color(int color)
2122 if(total_colors == 256)
2124 // replace the closest match with an exact match
2125 color_table[get_color_rgb8(color)][0] = color;
2129 // add the color to the table
2130 color_table[total_colors][0] = color;
2136 int BC_WindowBase::create_shared_colors()
2138 create_color(BLACK);
2139 create_color(WHITE);
2141 create_color(LTGREY);
2142 create_color(MEGREY);
2143 create_color(MDGREY);
2144 create_color(DKGREY);
2146 create_color(LTCYAN);
2147 create_color(MECYAN);
2148 create_color(MDCYAN);
2149 create_color(DKCYAN);
2151 create_color(LTGREEN);
2152 create_color(GREEN);
2153 create_color(DKGREEN);
2155 create_color(LTPINK);
2159 create_color(LTBLUE);
2161 create_color(DKBLUE);
2163 create_color(LTYELLOW);
2164 create_color(MEYELLOW);
2165 create_color(MDYELLOW);
2166 create_color(DKYELLOW);
2168 create_color(LTPURPLE);
2169 create_color(MEPURPLE);
2170 create_color(MDPURPLE);
2171 create_color(DKPURPLE);
2173 create_color(FGGREY);
2174 create_color(MNBLUE);
2175 create_color(ORANGE);
2176 create_color(FTGREY);
2181 int BC_WindowBase::allocate_color_table()
2183 int red, green, blue, color;
2186 for(int i = 0; i < total_colors; i++)
2188 color = color_table[i][0];
2189 red = (color & 0xFF0000) >> 16;
2190 green = (color & 0x00FF00) >> 8;
2191 blue = color & 0xFF;
2193 col.flags = DoRed | DoGreen | DoBlue;
2194 col.red = red<<8 | red;
2195 col.green = green<<8 | green;
2196 col.blue = blue<<8 | blue;
2198 XAllocColor(display, cmap, &col);
2199 color_table[i][1] = col.pixel;
2202 XInstallColormap(display, cmap);
2206 int BC_WindowBase::init_window_shape()
2208 if(bg_pixmap && bg_pixmap->use_alpha())
2210 XShapeCombineMask(top_level->display,
2211 this->win, ShapeBounding, 0, 0,
2212 bg_pixmap->get_alpha(), ShapeSet);
2218 int BC_WindowBase::init_gc()
2220 unsigned long gcmask;
2221 gcmask = GCFont | GCGraphicsExposures;
2224 gcvalues.font = mediumfont->fid; // set the font
2225 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2226 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2228 // gcmask = GCCapStyle | GCJoinStyle;
2229 // XGetGCValues(display, gc, gcmask, &gcvalues);
2230 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2234 int BC_WindowBase::init_fonts()
2236 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2237 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2238 smallfont = XLoadQueryFont(display, "fixed");
2239 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2240 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2241 mediumfont = XLoadQueryFont(display, "fixed");
2242 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2243 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2244 largefont = XLoadQueryFont(display, "fixed");
2245 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2246 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2247 bigfont = XLoadQueryFont(display, "fixed");
2250 if(get_resources()->use_fontset)
2255 // FIXME: should check the m,d,n values
2256 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2258 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2259 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2260 if( !mediumfontset )
2261 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2262 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2264 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2265 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2267 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2268 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2269 curr_fontset = mediumfontset;
2270 get_resources()->use_fontset = 1;
2274 get_resources()->use_fontset = 0;
2281 void BC_WindowBase::init_xft()
2284 if( !get_resources()->use_xft ) return;
2285 if(!(smallfont_xft =
2286 (resources.small_font_xft[0] == '-' ?
2287 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2288 XftFontOpenName(display, screen, resources.small_font_xft))) )
2289 if(!(smallfont_xft =
2290 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2291 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2292 if(!(mediumfont_xft =
2293 (resources.medium_font_xft[0] == '-' ?
2294 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2295 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2296 if(!(mediumfont_xft =
2297 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2298 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2299 if(!(largefont_xft =
2300 (resources.large_font_xft[0] == '-' ?
2301 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2302 XftFontOpenName(display, screen, resources.large_font_xft))) )
2303 if(!(largefont_xft =
2304 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2305 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2307 (resources.big_font_xft[0] == '-' ?
2308 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2309 XftFontOpenName(display, screen, resources.big_font_xft))) )
2311 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2312 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2314 if(!(bold_smallfont_xft =
2315 (resources.small_b_font_xft[0] == '-' ?
2316 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2317 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2318 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2319 if(!(bold_mediumfont_xft =
2320 (resources.medium_b_font_xft[0] == '-' ?
2321 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2322 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2323 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2324 if(!(bold_largefont_xft =
2325 (resources.large_b_font_xft[0] == '-' ?
2326 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2327 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2328 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2330 // Extension failed to locate fonts
2331 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2332 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2333 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2334 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2335 resources.small_font_xft, smallfont_xft,
2336 resources.medium_font_xft, mediumfont_xft,
2337 resources.large_font_xft, largefont_xft,
2338 resources.big_font_xft, bigfont_xft,
2339 resources.small_b_font_xft, bold_smallfont_xft,
2340 resources.medium_b_font_xft, bold_mediumfont_xft,
2341 resources.large_b_font_xft, bold_largefont_xft);
2342 get_resources()->use_xft = 0;
2348 void BC_WindowBase::init_im()
2350 XIMStyles *xim_styles;
2353 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2355 printf("BC_WindowBase::init_im: Could not open input method.\n");
2358 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2361 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2362 XCloseIM(input_method);
2367 for(int z = 0; z < xim_styles->count_styles; z++)
2369 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2371 xim_style = xim_styles->supported_styles[z];
2379 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2380 XCloseIM(input_method);
2384 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2385 XNClientWindow, win, XNFocusWindow, win, NULL);
2388 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2389 XCloseIM(input_method);
2394 void BC_WindowBase::finit_im()
2396 if( input_context ) {
2397 XDestroyIC(input_context);
2400 if( input_method ) {
2401 XCloseIM(input_method);
2407 int BC_WindowBase::get_color(int64_t color)
2409 // return pixel of color
2410 // use this only for drawing subwindows not for bitmaps
2411 int i, test, difference;
2417 return get_color_rgb8(color);
2418 // test last color looked up
2419 if(current_color_value == color)
2420 return current_color_pixel;
2423 current_color_value = color;
2424 for(i = 0; i < total_colors; i++)
2426 if(color_table[i][0] == color)
2428 current_color_pixel = color_table[i][1];
2429 return current_color_pixel;
2433 // find nearest match
2434 difference = 0xFFFFFF;
2436 for(i = 0; i < total_colors; i++)
2438 test = abs((int)(color_table[i][0] - color));
2440 if(test < difference)
2442 current_color_pixel = color_table[i][1];
2446 return current_color_pixel;
2449 return get_color_rgb16(color);
2452 return get_color_bgr16(color);
2456 return client_byte_order == server_byte_order ?
2457 color : get_color_bgr24(color);
2465 int BC_WindowBase::get_color_rgb8(int color)
2469 pixel = (color & 0xc00000) >> 16;
2470 pixel += (color & 0xe000) >> 10;
2471 pixel += (color & 0xe0) >> 5;
2475 int64_t BC_WindowBase::get_color_rgb16(int color)
2478 result = (color & 0xf80000) >> 8;
2479 result += (color & 0xfc00) >> 5;
2480 result += (color & 0xf8) >> 3;
2485 int64_t BC_WindowBase::get_color_bgr16(int color)
2488 result = (color & 0xf80000) >> 19;
2489 result += (color & 0xfc00) >> 5;
2490 result += (color & 0xf8) << 8;
2495 int64_t BC_WindowBase::get_color_bgr24(int color)
2498 result = (color & 0xff) << 16;
2499 result += (color & 0xff00);
2500 result += (color & 0xff0000) >> 16;
2504 void BC_WindowBase::start_video()
2506 cursor_timer->update();
2508 // set_color(BLACK);
2509 // draw_box(0, 0, get_w(), get_h());
2513 void BC_WindowBase::stop_video()
2521 int64_t BC_WindowBase::get_color()
2523 return top_level->current_color;
2526 void BC_WindowBase::set_color(int64_t color)
2528 top_level->current_color = color;
2529 XSetForeground(top_level->display,
2531 top_level->get_color(color));
2534 void BC_WindowBase::set_opaque()
2536 XSetFunction(top_level->display, top_level->gc, GXcopy);
2539 void BC_WindowBase::set_inverse()
2541 XSetFunction(top_level->display, top_level->gc, GXxor);
2544 void BC_WindowBase::set_line_width(int value)
2546 this->line_width = value;
2547 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2548 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2549 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2550 JoinMiter); /* join_style */
2552 if(line_dashes > 0) {
2553 const char dashes = line_dashes;
2554 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2557 // XGCValues gcvalues;
2558 // unsigned long gcmask;
2559 // gcmask = GCCapStyle | GCJoinStyle;
2560 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2561 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2564 void BC_WindowBase::set_line_dashes(int value)
2566 line_dashes = value;
2567 // call XSetLineAttributes
2568 set_line_width(line_width);
2572 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2576 case ARROW_CURSOR: return top_level->arrow_cursor; break;
2577 case CROSS_CURSOR: return top_level->cross_cursor;
2578 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
2579 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
2580 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
2581 case MOVE_CURSOR: return top_level->move_cursor; break;
2582 case LEFT_CURSOR: return top_level->left_cursor; break;
2583 case RIGHT_CURSOR: return top_level->right_cursor; break;
2584 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
2585 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
2586 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
2587 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
2588 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
2589 case HOURGLASS_CURSOR: return top_level->hourglass_cursor; break;
2590 case TRANSPARENT_CURSOR: return top_level->transparent_cursor; break;
2595 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2597 // inherit cursor from parent
2600 XUndefineCursor(top_level->display, win);
2601 current_cursor = cursor;
2604 // don't change cursor if overridden
2605 if((!top_level->is_hourglass && !is_transparent) ||
2608 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2609 if(flush) this->flush();
2612 if(!override) current_cursor = cursor;
2615 void BC_WindowBase::set_x_cursor(int cursor)
2617 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2618 XDefineCursor(top_level->display, win, temp_cursor);
2619 current_cursor = cursor;
2623 int BC_WindowBase::get_cursor()
2625 return current_cursor;
2628 void BC_WindowBase::start_hourglass()
2630 top_level->start_hourglass_recursive();
2634 void BC_WindowBase::stop_hourglass()
2636 top_level->stop_hourglass_recursive();
2640 void BC_WindowBase::start_hourglass_recursive()
2642 if(this == top_level)
2650 set_cursor(HOURGLASS_CURSOR, 1, 0);
2651 for(int i = 0; i < subwindows->total; i++)
2653 subwindows->values[i]->start_hourglass_recursive();
2658 void BC_WindowBase::stop_hourglass_recursive()
2660 if(this == top_level)
2662 if(hourglass_total == 0) return;
2663 top_level->hourglass_total--;
2666 if(!top_level->hourglass_total)
2668 top_level->is_hourglass = 0;
2670 // Cause set_cursor to perform change
2672 set_cursor(current_cursor, 1, 0);
2674 for(int i = 0; i < subwindows->total; i++)
2676 subwindows->values[i]->stop_hourglass_recursive();
2684 XFontStruct* BC_WindowBase::get_font_struct(int font)
2686 // Clear out unrelated flags
2687 if(font & BOLDFACE) font ^= BOLDFACE;
2690 case SMALLFONT: return top_level->smallfont; break;
2691 case MEDIUMFONT: return top_level->mediumfont; break;
2692 case LARGEFONT: return top_level->largefont; break;
2693 case BIGFONT: return top_level->bigfont; break;
2698 XFontSet BC_WindowBase::get_fontset(int font)
2702 if(get_resources()->use_fontset)
2704 switch(font & 0xff) {
2705 case SMALLFONT: fs = top_level->smallfontset; break;
2706 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2707 case LARGEFONT: fs = top_level->largefontset; break;
2708 case BIGFONT: fs = top_level->bigfontset; break;
2716 XftFont* BC_WindowBase::get_xft_struct(int font)
2719 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2720 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2721 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2722 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2723 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2724 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2725 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2740 int BC_WindowBase::get_current_font()
2742 return top_level->current_font;
2745 void BC_WindowBase::set_font(int font)
2747 top_level->current_font = font;
2750 if(get_resources()->use_xft) {}
2753 if(get_resources()->use_fontset) {
2757 if(get_font_struct(font))
2759 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2765 void BC_WindowBase::set_fontset(int font)
2769 if(get_resources()->use_fontset) {
2771 case SMALLFONT: fs = top_level->smallfontset; break;
2772 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2773 case LARGEFONT: fs = top_level->largefontset; break;
2774 case BIGFONT: fs = top_level->bigfontset; break;
2782 XFontSet BC_WindowBase::get_curr_fontset(void)
2784 if(get_resources()->use_fontset)
2785 return curr_fontset;
2789 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2792 if(get_resources()->use_xft && get_xft_struct(font))
2795 #ifdef X_HAVE_UTF8_STRING
2796 if(get_resources()->locale_utf8)
2798 XftTextExtentsUtf8(top_level->display,
2799 get_xft_struct(font),
2800 (const XftChar8 *)text,
2807 XftTextExtents8(top_level->display,
2808 get_xft_struct(font),
2809 (const XftChar8 *)text,
2813 return extents.xOff;
2817 if(get_resources()->use_fontset && top_level->get_fontset(font))
2818 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2820 if(get_font_struct(font))
2821 return XTextWidth(get_font_struct(font), text, length);
2827 case MEDIUM_7SEGMENT:
2828 return get_resources()->medium_7segment[0]->get_w() * length;
2838 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2841 if(get_resources()->use_xft && get_xft_struct(font)) {
2844 XftTextExtents32(top_level->display, get_xft_struct(font),
2845 (const FcChar32*)text, length, &extents);
2846 return extents.xOff;
2849 if(!get_font_struct(font)) return 0;
2850 XChar2b xtext[length], *xp = xtext;
2851 for( int i=0; i<length; ++i,++xp ) {
2852 xp->byte1 = (unsigned char) (text[i] >> 8);
2853 xp->byte2 = (unsigned char) (text[i] & 0xff);
2855 return XTextWidth16(get_font_struct(font), xtext, length);
2858 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2860 int i, j, w = 0, line_w = 0;
2861 if(length < 0) length = strlen(text);
2863 for(i = 0, j = 0; i <= length; i++)
2868 line_w = get_single_text_width(font, &text[j], i - j);
2874 line_w = get_single_text_width(font, &text[j], length - j);
2876 if(line_w > w) w = line_w;
2879 if(i > length && w == 0)
2881 w = get_single_text_width(font, text, length);
2887 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2889 int i, j, w = 0, line_w = 0;
2891 if(length < 0) length = wcslen(text);
2893 for(i = 0, j = 0; i <= length; i++)
2898 line_w = get_single_text_width(font, &text[j], i - j);
2903 line_w = get_single_text_width(font, &text[j], length - j);
2905 if(line_w > w) w = line_w;
2908 if(i > length && w == 0)
2909 w = get_single_text_width(font, text, length);
2914 int BC_WindowBase::get_text_ascent(int font)
2918 if( (fstruct = get_xft_struct(font)) != 0 )
2919 return fstruct->ascent;
2921 if(get_resources()->use_fontset && top_level->get_fontset(font))
2923 XFontSetExtents *extents;
2925 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2926 return -extents->max_logical_extent.y;
2929 if(get_font_struct(font))
2930 return top_level->get_font_struct(font)->ascent;
2933 case MEDIUM_7SEGMENT:
2934 return get_resources()->medium_7segment[0]->get_h();
2939 int BC_WindowBase::get_text_descent(int font)
2943 if( (fstruct = get_xft_struct(font)) != 0 )
2944 return fstruct->descent;
2946 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2947 XFontSetExtents *extents;
2948 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2949 return (extents->max_logical_extent.height
2950 + extents->max_logical_extent.y);
2953 if(get_font_struct(font))
2954 return top_level->get_font_struct(font)->descent;
2959 int BC_WindowBase::get_text_height(int font, const char *text)
2964 if( (fstruct = get_xft_struct(font)) != 0 )
2965 rowh = fstruct->height;
2968 rowh = get_text_ascent(font) + get_text_descent(font);
2970 if(!text) return rowh;
2972 // Add height of lines
2973 int h = 0, i, length = strlen(text);
2974 for(i = 0; i <= length; i++)
2985 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2987 if(color_model < 0) color_model = top_level->get_color_model();
2988 return new BC_Bitmap(top_level, w, h, color_model);
2991 void BC_WindowBase::init_wait()
2993 #ifndef SINGLE_THREAD
2994 if(window_type != MAIN_WINDOW)
2995 top_level->init_wait();
2996 init_lock->lock("BC_WindowBase::init_wait");
2997 init_lock->unlock();
3001 int BC_WindowBase::accel_available(int color_model, int lock_it)
3003 if( window_type != MAIN_WINDOW )
3004 return top_level->accel_available(color_model, lock_it);
3006 lock_window("BC_WindowBase::accel_available");
3008 switch(color_model) {
3010 grab_port_id(color_model);
3014 grab_port_id(color_model);
3023 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3024 return xvideo_port_id >= 0 ? 1 : 0;
3028 int BC_WindowBase::grab_port_id(int color_model)
3030 if( !get_resources()->use_xvideo || // disabled
3031 !get_resources()->use_shm ) // Only local server is fast enough.
3033 if( xvideo_port_id >= 0 )
3034 return xvideo_port_id;
3036 unsigned int ver, rev, reqBase, eventBase, errorBase;
3037 if( Success != XvQueryExtension(display, // XV extension is available
3038 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3041 // XV adaptors are available
3042 unsigned int numAdapt = 0;
3043 XvAdaptorInfo *info = 0;
3044 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3048 // Translate from color_model to X color model
3049 int x_color_model = BC_CModels::bc_to_x(color_model);
3051 // Get adaptor with desired color model
3052 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3053 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3054 // adaptor supports XvImages
3055 int numFormats = 0, numPorts = info[i].num_ports;
3056 XvImageFormatValues *formats =
3057 XvListImageFormats(display, info[i].base_id, &numFormats);
3058 if( !formats ) continue;
3060 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3061 if( formats[j].id != x_color_model ) continue;
3062 // this adaptor supports the desired format, grab a port
3063 for( int k=0; k<numPorts; ++k ) {
3064 if( Success == XvGrabPort(top_level->display,
3065 info[i].base_id+k, CurrentTime) ) {
3066 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3067 xvideo_port_id = info[i].base_id + k;
3075 XvFreeAdaptorInfo(info);
3077 return xvideo_port_id;
3081 int BC_WindowBase::show_window(int flush)
3083 for(int i = 0; i < subwindows->size(); i++)
3085 subwindows->get(i)->show_window(0);
3088 XMapWindow(top_level->display, win);
3089 if(flush) XFlush(top_level->display);
3090 // XSync(top_level->display, 0);
3095 int BC_WindowBase::hide_window(int flush)
3097 for(int i = 0; i < subwindows->size(); i++)
3099 subwindows->get(i)->hide_window(0);
3102 XUnmapWindow(top_level->display, win);
3103 if(flush) this->flush();
3108 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3110 subwindows->append((BC_SubWindow*)menu_bar);
3112 menu_bar->parent_window = this;
3113 menu_bar->top_level = this->top_level;
3114 menu_bar->initialize();
3118 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3120 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3121 if(this != top_level) return top_level->add_popup(window);
3122 popups.append(window);
3126 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3128 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3129 if(this != top_level)
3130 top_level->remove_popup(window);
3132 popups.remove(window);
3133 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3137 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3139 subwindows->append(subwindow);
3141 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3143 // parent window must be set before the subwindow initialization
3144 subwindow->parent_window = this;
3145 subwindow->top_level = this->top_level;
3147 // Execute derived initialization
3148 subwindow->initialize();
3153 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3155 return add_subwindow(subwindow);
3158 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3160 if( !top_level->flash_enabled ) return 0;
3161 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3163 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3166 XClearArea(top_level->display, win, x, y, w, h, 0);
3170 XClearWindow(top_level->display, win);
3178 int BC_WindowBase::flash(int flush)
3180 flash(-1, -1, -1, -1, flush);
3184 void BC_WindowBase::flush()
3186 //if(!get_window_lock())
3187 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3188 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3189 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3190 int locked = get_window_lock();
3191 if( !locked ) lock_window("BC_WindowBase::flush");
3192 XFlush(top_level->display);
3193 if( !locked ) unlock_window();
3196 void BC_WindowBase::sync_display()
3198 int locked = get_window_lock();
3199 if( !locked ) lock_window("BC_WindowBase::sync_display");
3200 XSync(top_level->display, False);
3201 if( !locked ) unlock_window();
3204 int BC_WindowBase::get_window_lock()
3206 #ifdef SINGLE_THREAD
3207 return BC_Display::display_global->get_display_locked();
3209 return top_level->window_lock;
3213 int BC_WindowBase::lock_window(const char *location)
3215 if(top_level && top_level != this)
3217 top_level->lock_window(location);
3222 SET_LOCK(this, title, location);
3223 #ifdef SINGLE_THREAD
3224 BC_Display::lock_display(location);
3226 XLockDisplay(top_level->display);
3227 top_level->display_lock_owner = pthread_self();
3230 ++top_level->window_lock;
3234 printf("BC_WindowBase::lock_window top_level NULL\n");
3239 int BC_WindowBase::unlock_window()
3241 if(top_level && top_level != this)
3243 top_level->unlock_window();
3250 // if(!top_level->window_lock)
3252 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3256 if( top_level->window_lock > 0 )
3257 if( --top_level->window_lock == 0 )
3258 top_level->display_lock_owner = 0;
3259 #ifdef SINGLE_THREAD
3260 BC_Display::unlock_display();
3262 XUnlockDisplay(top_level->display);
3267 printf("BC_WindowBase::unlock_window top_level NULL\n");
3272 void BC_WindowBase::set_done(int return_value)
3274 if(done_set) return;
3276 if(window_type != MAIN_WINDOW)
3277 top_level->set_done(return_value);
3280 #ifdef SINGLE_THREAD
3281 this->return_value = return_value;
3282 BC_Display::display_global->arm_completion(this);
3283 completion_lock->unlock();
3284 #else // SINGLE_THREAD
3286 if( !event_thread ) return;
3287 XEvent *event = new_xevent();
3288 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3289 event->type = ClientMessage;
3290 ptr->message_type = SetDoneXAtom;
3292 this->return_value = return_value;
3293 // May lock up here because XSendEvent doesn't work too well
3294 // asynchronous with XNextEvent.
3295 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3297 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3303 void BC_WindowBase::close(int return_value)
3305 hide_window(); flush();
3306 set_done(return_value);
3309 int BC_WindowBase::get_w()
3314 int BC_WindowBase::get_h()
3319 int BC_WindowBase::get_x()
3324 int BC_WindowBase::get_y()
3330 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3331 // this is the bounding box of all the screens
3333 int BC_WindowBase::get_root_w(int lock_display)
3335 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3336 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3337 int result = WidthOfScreen(def_screen);
3338 if(lock_display) unlock_window();
3342 int BC_WindowBase::get_root_h(int lock_display)
3344 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3345 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3346 int result = HeightOfScreen(def_screen);
3347 if(lock_display) unlock_window();
3351 XineramaScreenInfo *
3352 BC_WindowBase::get_xinerama_info(int screen)
3354 if( !xinerama_info || !xinerama_screens ) return 0;
3356 for( int i=0; i<xinerama_screens; ++i )
3357 if( xinerama_info[i].screen_number == screen )
3358 return &xinerama_info[i];
3361 int top_x = get_x(), top_y = get_y();
3362 for( int i=0; i<xinerama_screens; ++i ) {
3363 int scr_y = top_y - xinerama_info[i].y_org;
3364 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3365 int scr_x = top_x - xinerama_info[i].x_org;
3366 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3367 return &xinerama_info[i];
3372 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3375 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3376 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3379 int root_w = get_root_w(0);
3380 int root_h = get_root_h(0);
3381 // Shift X based on position of current window if dual head
3382 if( (float)root_w/root_h > 1.8 ) {
3383 root_w = get_screen_w(0, 0);
3384 if( top_level->get_x() >= root_w )
3389 result = info->x_org;
3390 if(lock_display) unlock_window();
3394 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3396 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3397 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3398 int result = !info ? 0 : info->y_org;
3399 if(lock_display) unlock_window();
3403 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3406 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3407 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3409 int width = get_root_w(0);
3410 int height = get_root_h(0);
3411 if( (float)width/height > 1.8 ) {
3412 // If dual head, the screen width is > 16x9
3413 // but we only want to fill one screen
3414 // this code assumes the "big" screen is on the right
3415 int scr_w0 = width / 2;
3417 case 600: scr_w0 = 800; break;
3418 case 720: scr_w0 = 1280; break;
3419 case 1024: scr_w0 = 1280; break;
3420 case 1200: scr_w0 = 1600; break;
3421 case 1080: scr_w0 = 1920; break;
3423 int scr_w1 = width - scr_w0;
3424 result = screen > 0 ? scr_w1 :
3425 screen == 0 ? scr_w0 :
3426 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3432 result = info->width;
3433 if(lock_display) unlock_window();
3437 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3439 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3440 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3441 int result = info ? info->height : get_root_h(0);
3442 if(lock_display) unlock_window();
3446 // Bottom right corner
3447 int BC_WindowBase::get_x2()
3452 int BC_WindowBase::get_y2()
3457 int BC_WindowBase::get_video_on()
3462 int BC_WindowBase::get_hidden()
3464 return top_level->hidden;
3467 int BC_WindowBase::cursor_inside()
3469 return (top_level->cursor_x >= 0 &&
3470 top_level->cursor_y >= 0 &&
3471 top_level->cursor_x < w &&
3472 top_level->cursor_y < h);
3475 BC_WindowBase* BC_WindowBase::get_top_level()
3480 BC_WindowBase* BC_WindowBase::get_parent()
3482 return parent_window;
3485 int BC_WindowBase::get_color_model()
3487 return top_level->color_model;
3490 BC_Resources* BC_WindowBase::get_resources()
3492 return &BC_WindowBase::resources;
3495 BC_Synchronous* BC_WindowBase::get_synchronous()
3497 return BC_WindowBase::resources.get_synchronous();
3500 int BC_WindowBase::get_bg_color()
3505 void BC_WindowBase::set_bg_color(int color)
3507 this->bg_color = color;
3510 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3515 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3517 top_level->active_subwindow = subwindow;
3520 int BC_WindowBase::activate()
3525 int BC_WindowBase::deactivate()
3527 if(window_type == MAIN_WINDOW)
3529 if(top_level->active_menubar) top_level->active_menubar->deactivate();
3530 if(top_level->active_popup_menu) top_level->active_popup_menu->deactivate();
3531 if(top_level->active_subwindow) top_level->active_subwindow->deactivate();
3533 top_level->active_menubar = 0;
3534 top_level->active_popup_menu = 0;
3535 top_level->active_subwindow = 0;
3540 int BC_WindowBase::cycle_textboxes(int amount)
3543 BC_WindowBase *new_textbox = 0;
3547 BC_WindowBase *first_textbox = 0;
3548 find_next_textbox(&first_textbox, &new_textbox, result);
3549 if(!new_textbox) new_textbox = first_textbox;
3555 BC_WindowBase *last_textbox = 0;
3556 find_prev_textbox(&last_textbox, &new_textbox, result);
3557 if(!new_textbox) new_textbox = last_textbox;
3561 if(new_textbox != active_subwindow)
3564 new_textbox->activate();
3570 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3572 // Search subwindows for textbox
3573 for(int i = 0; i < subwindows->total && result < 2; i++)
3575 BC_WindowBase *test_subwindow = subwindows->values[i];
3576 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3583 if(!*first_textbox) *first_textbox = this;
3587 if(top_level->active_subwindow == this)
3593 *next_textbox = this;
3600 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3606 if(!*last_textbox) *last_textbox = this;
3610 if(top_level->active_subwindow == this)
3616 *prev_textbox = this;
3621 // Search subwindows for textbox
3622 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3624 BC_WindowBase *test_subwindow = subwindows->values[i];
3625 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3630 BC_Clipboard* BC_WindowBase::get_clipboard()
3632 #ifdef SINGLE_THREAD
3633 return BC_Display::display_global->clipboard;
3635 return top_level->clipboard;
3639 int BC_WindowBase::get_relative_cursor_x()
3641 int abs_x, abs_y, x, y, win_x, win_y;
3642 unsigned int temp_mask;
3645 XQueryPointer(top_level->display, top_level->win,
3646 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3649 XTranslateCoordinates(top_level->display, top_level->rootwin,
3650 win, abs_x, abs_y, &x, &y, &temp_win);
3655 int BC_WindowBase::get_relative_cursor_y()
3657 int abs_x, abs_y, x, y, win_x, win_y;
3658 unsigned int temp_mask;
3661 XQueryPointer(top_level->display, top_level->win,
3662 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3665 XTranslateCoordinates(top_level->display,
3666 top_level->rootwin, win, abs_x, abs_y, &x, &y, &temp_win);
3671 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3673 int abs_x, abs_y, win_x, win_y;
3674 unsigned int temp_mask;
3677 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
3678 XQueryPointer(top_level->display, top_level->win,
3679 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3681 if(lock_window) this->unlock_window();
3685 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3687 int abs_x, abs_y, win_x, win_y;
3688 unsigned int temp_mask;
3691 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
3692 XQueryPointer(top_level->display, top_level->win,
3693 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3695 if(lock_window) this->unlock_window();
3699 int BC_WindowBase::match_window(Window win)
3701 if (this->win == win) return 1;
3703 for(int i = 0; i < subwindows->total; i++) {
3704 result = subwindows->values[i]->match_window(win);
3705 if (result) return result;
3711 int BC_WindowBase::get_cursor_over_window()
3713 if(top_level != this) return top_level->get_cursor_over_window();
3715 int abs_x, abs_y, win_x, win_y;
3716 unsigned int temp_mask;
3717 Window root_return, child_return;
3719 int ret = XQueryPointer(display, win,
3720 &root_return, &child_return, &abs_x, &abs_y,
3721 &win_x, &win_y, &temp_mask);
3722 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3723 // __LINE__, title, win, ret, child_return);
3725 int result = !ret || child_return == None ? 0 :
3726 match_window(child_return);
3730 int BC_WindowBase::cursor_above()
3732 int rx = get_relative_cursor_x();
3733 if( rx < 0 || rx >= get_w() ) return 0;
3734 int ry = get_relative_cursor_y();
3735 if( ry < 0 || ry >= get_h() ) return 0;
3739 int BC_WindowBase::get_drag_x()
3741 return top_level->drag_x;
3744 int BC_WindowBase::get_drag_y()
3746 return top_level->drag_y;
3749 int BC_WindowBase::get_cursor_x()
3751 return top_level->cursor_x;
3754 int BC_WindowBase::get_cursor_y()
3756 return top_level->cursor_y;
3759 int BC_WindowBase::dump_windows()
3761 printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
3762 this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
3763 for(int i = 0; i < subwindows->size(); i++)
3764 subwindows->get(i)->dump_windows();
3765 for(int i = 0; i < popups.size(); i++) {
3766 BC_WindowBase *p = popups[i];
3767 printf("\tBC_WindowBase::dump_windows popup=%p win=%p '%s', %dx%d+%d+%d %s\n",
3768 p, (void*)p->win, p->title, p->w,p->h,p->x,p->y, typeid(*p).name());
3773 int BC_WindowBase::is_event_win()
3775 return this->win == top_level->event_win;
3778 void BC_WindowBase::set_dragging(int value)
3780 is_dragging = value;
3783 int BC_WindowBase::get_dragging()
3788 int BC_WindowBase::get_buttonpress()
3790 return top_level->button_number;
3793 int BC_WindowBase::get_button_down()
3795 return top_level->button_down;
3798 int BC_WindowBase::alt_down()
3800 return top_level->alt_mask;
3803 int BC_WindowBase::shift_down()
3805 return top_level->shift_mask;
3808 int BC_WindowBase::ctrl_down()
3810 return top_level->ctrl_mask;
3813 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3816 *length = top_level->wkey_string_length;
3817 return top_level->wkey_string;
3820 #ifdef X_HAVE_UTF8_STRING
3821 char* BC_WindowBase::get_keypress_utf8()
3823 return top_level->key_pressed_utf8;
3828 int BC_WindowBase::get_keypress()
3830 return top_level->key_pressed;
3833 int BC_WindowBase::get_double_click()
3835 return top_level->double_click;
3838 int BC_WindowBase::get_triple_click()
3840 return top_level->triple_click;
3843 int BC_WindowBase::get_bgcolor()
3848 int BC_WindowBase::resize_window(int w, int h)
3850 if(this->w == w && this->h == h) return 0;
3852 if(window_type == MAIN_WINDOW && !allow_resize)
3854 XSizeHints size_hints;
3855 size_hints.flags = PSize | PMinSize | PMaxSize;
3856 size_hints.width = w;
3857 size_hints.height = h;
3858 size_hints.min_width = w;
3859 size_hints.max_width = w;
3860 size_hints.min_height = h;
3861 size_hints.max_height = h;
3862 XSetNormalHints(top_level->display, win, &size_hints);
3864 XResizeWindow(top_level->display, win, w, h);
3869 pixmap = new BC_Pixmap(this, w, h);
3871 // Propagate to menubar
3872 for(int i = 0; i < subwindows->total; i++)
3874 subwindows->values[i]->dispatch_resize_event(w, h);
3877 draw_background(0, 0, w, h);
3878 if(top_level == this && get_resources()->recursive_resizing)
3879 resize_history.append(new BC_ResizeCall(w, h));
3883 // The only way for resize events to be propagated is by updating the internal w and h
3884 int BC_WindowBase::resize_event(int w, int h)
3886 if(window_type == MAIN_WINDOW)
3894 int BC_WindowBase::reposition_window(int x, int y)
3896 reposition_window(x, y, -1, -1);
3901 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
3905 // Some tools set their own dimensions before calling this, causing the
3906 // resize check to skip.
3910 if(w > 0 && w != this->w)
3916 if(h > 0 && h != this->h)
3922 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
3925 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
3927 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
3929 if(translation_count && window_type == MAIN_WINDOW)
3931 // KDE shifts window right and down.
3932 // FVWM leaves window alone and adds border around it.
3933 XMoveResizeWindow(top_level->display,
3935 x - BC_DisplayInfo::auto_reposition_x,
3936 y - BC_DisplayInfo::auto_reposition_y,
3942 XMoveResizeWindow(top_level->display,
3953 pixmap = new BC_Pixmap(this, this->w, this->h);
3954 clear_box(0,0, this->w, this->h);
3955 // Propagate to menubar
3956 for(int i = 0; i < subwindows->total; i++)
3958 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
3961 // draw_background(0, 0, w, h);
3967 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
3969 return reposition_window(get_x()+dx, get_y()+dy, w, h);
3972 int BC_WindowBase::reposition_window_relative(int dx, int dy)
3974 return reposition_window_relative(dx, dy, -1, -1);
3977 void BC_WindowBase::set_tooltips(int v)
3979 get_resources()->tooltips_enabled = v;
3982 void BC_WindowBase::set_force_tooltip(int v)
3987 int BC_WindowBase::raise_window(int do_flush)
3989 XRaiseWindow(top_level->display, win);
3990 if(do_flush) XFlush(top_level->display);
3994 int BC_WindowBase::lower_window(int do_flush)
3996 XLowerWindow(top_level->display, win);
3997 if(do_flush) XFlush(top_level->display);
4001 void BC_WindowBase::set_background(VFrame *bitmap)
4003 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4005 bg_pixmap = new BC_Pixmap(this, bitmap, PIXMAP_OPAQUE);
4006 shared_bg_pixmap = 0;
4007 draw_background(0, 0, w, h);
4010 void BC_WindowBase::put_title(const char *text)
4012 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4013 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4014 *cp++ = *bp >= ' ' ? *bp : ' ';
4018 void BC_WindowBase::set_title(const char *text)
4023 txlist[0] = this->title;
4026 XTextProperty titleprop;
4027 XmbTextListToTextProperty(top_level->display, txlist, 1,
4028 XStdICCTextStyle, &titleprop);
4029 XSetWMName(top_level->display, top_level->win, &titleprop);
4030 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4031 XFree(titleprop.value);
4036 void BC_WindowBase::set_utf8title(const char *text)
4038 XTextProperty titleprop;
4041 strcpy(this->title, text);
4042 txlist[0] = this->title;
4045 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4046 XUTF8StringStyle, &titleprop);
4047 XSetWMName(top_level->display, top_level->win, &titleprop);
4048 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4049 XFree(titleprop.value);
4054 const char *BC_WindowBase::get_title()
4059 int BC_WindowBase::get_toggle_value()
4061 return toggle_value;
4064 int BC_WindowBase::get_toggle_drag()
4069 int BC_WindowBase::set_icon(VFrame *data)
4071 if(icon_pixmap) delete icon_pixmap;
4072 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4074 if(icon_window) delete icon_window;
4075 icon_window = new BC_Popup(this,
4078 icon_pixmap->get_w(),
4079 icon_pixmap->get_h(),
4081 1, // All windows are hidden initially
4085 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4086 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4087 wm_hints.icon_mask = icon_pixmap->get_alpha();
4088 wm_hints.icon_window = icon_window->win;
4089 wm_hints.window_group = XGroupLeader;
4091 // for(int i = 0; i < 1000; i++)
4092 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4095 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4096 XSync(top_level->display, 0);
4100 int BC_WindowBase::set_w(int w)
4106 int BC_WindowBase::set_h(int h)
4112 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4114 BC_Resources *resources = get_resources();
4115 char string[BCTEXTLEN];
4116 int newest_id = - 1;
4117 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4119 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4120 resources->filebox_history[i].path[0] = 0;
4121 defaults->get(string, resources->filebox_history[i].path);
4122 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4123 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4124 if(resources->filebox_history[i].id > newest_id)
4125 newest_id = resources->filebox_history[i].id;
4128 resources->filebox_id = newest_id + 1;
4129 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4130 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4131 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4132 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4133 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4134 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4135 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4136 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4137 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4138 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4139 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4140 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4144 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4146 BC_Resources *resources = get_resources();
4147 char string[BCTEXTLEN];
4148 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4150 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4151 defaults->update(string, resources->filebox_history[i].path);
4152 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4153 defaults->update(string, resources->filebox_history[i].id);
4155 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4156 defaults->update("FILEBOX_W", resources->filebox_w);
4157 defaults->update("FILEBOX_H", resources->filebox_h);
4158 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4159 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4160 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4161 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4162 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4163 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4164 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4165 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4166 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4172 // For some reason XTranslateCoordinates can take a long time to return.
4173 // We work around this by only calling it when the event windows are different.
4174 void BC_WindowBase::translate_coordinates(Window src_w,
4186 *dest_x_return = src_x;
4187 *dest_y_return = src_y;
4191 XTranslateCoordinates(top_level->display,
4199 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4203 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4205 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4208 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4210 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4218 #ifdef HAVE_LIBXXF86VM
4219 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4223 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4225 XF86VidModeModeInfo **vm_modelines;
4226 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4227 for (i = 0; i < vm_count; i++) {
4228 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4231 display = top_level->display;
4232 if (vm_modelines[*vm]->hdisplay == *width)
4236 *width = vm_modelines[*vm]->hdisplay;
4237 *height = vm_modelines[*vm]->vdisplay;
4242 void BC_WindowBase::scale_vm(int vm)
4244 int foo,bar,dotclock;
4245 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4248 XF86VidModeModeInfo **vm_modelines;
4249 XF86VidModeModeLine vml;
4250 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4251 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4252 orig_modeline.dotclock = dotclock;
4253 orig_modeline.hdisplay = vml.hdisplay;
4254 orig_modeline.hsyncstart = vml.hsyncstart;
4255 orig_modeline.hsyncend = vml.hsyncend;
4256 orig_modeline.htotal = vml.htotal;
4257 orig_modeline.vdisplay = vml.vdisplay;
4258 orig_modeline.vsyncstart = vml.vsyncstart;
4259 orig_modeline.vsyncend = vml.vsyncend;
4260 orig_modeline.vtotal = vml.vtotal;
4261 orig_modeline.flags = vml.flags;
4262 orig_modeline.privsize = vml.privsize;
4263 // orig_modeline.private = vml.private;
4264 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4265 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4266 XFlush(top_level->display);
4270 void BC_WindowBase::restore_vm()
4272 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4273 XFlush(top_level->display);
4296 #ifndef SINGLE_THREAD
4297 int BC_WindowBase::get_event_count()
4299 event_lock->lock("BC_WindowBase::get_event_count");
4300 int result = common_events.total;
4301 event_lock->unlock();
4305 XEvent* BC_WindowBase::get_event()
4308 while(!done && !result)
4310 event_condition->lock("BC_WindowBase::get_event");
4311 event_lock->lock("BC_WindowBase::get_event");
4313 if(common_events.total && !done)
4315 result = common_events.values[0];
4316 common_events.remove_number(0);
4319 event_lock->unlock();
4324 void BC_WindowBase::put_event(XEvent *event)
4326 event_lock->lock("BC_WindowBase::put_event");
4327 common_events.append(event);
4328 event_lock->unlock();
4329 event_condition->unlock();
4332 void BC_WindowBase::dequeue_events(Window win)
4334 event_lock->lock("BC_WindowBase::dequeue_events");
4336 int out = 0, total = common_events.size();
4337 for( int in=0; in<total; ++in ) {
4338 if( common_events[in]->xany.window == win ) continue;
4339 common_events[out++] = common_events[in];
4341 common_events.total = out;
4343 event_lock->unlock();
4346 #endif // SINGLE_THREAD
4348 int BC_WindowBase::get_id()
4354 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4356 int w = vframe->get_w(), h = vframe->get_h();
4357 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4358 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4363 void BC_WindowBase::flicker(int n, int ms)
4365 int color = get_bg_color();
4366 for( int i=2*n; --i>=0; ) {
4367 set_inverse(); set_bg_color(WHITE);
4368 clear_box(0,0, w,h); flash(1);
4369 sync_display(); Timer::delay(ms);
4371 set_bg_color(color);