4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcclipboard.h"
24 #include "bcdisplay.h"
25 #include "bcdisplayinfo.h"
26 #include "bcmenubar.h"
29 #include "bcpopupmenu.h"
30 #include "bcrepeater.h"
31 #include "bcresources.h"
32 #include "bcsignals.h"
33 #include "bcsubwindow.h"
34 #include "bcsynchronous.h"
36 #include "bcwindowbase.h"
37 #include "bcwindowevents.h"
38 #include "bccmodels.h"
40 #include "condition.h"
58 #include <X11/extensions/Xinerama.h>
59 #include <X11/extensions/Xvlib.h>
60 #include <X11/extensions/shape.h>
61 #include <X11/XF86keysym.h>
62 #include <X11/Sunkeysym.h>
65 BC_ResizeCall::BC_ResizeCall(int w, int h)
77 int BC_WindowBase::shm_completion_event = -1;
81 BC_Resources BC_WindowBase::resources;
83 Window XGroupLeader = 0;
85 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
86 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
88 BC_WindowBase::BC_WindowBase(int opts)
91 //printf("BC_WindowBase::BC_WindowBase 1\n");
92 BC_WindowBase::initialize();
95 BC_WindowBase::~BC_WindowBase()
98 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
100 if(window_type == MAIN_WINDOW)
101 lock_window("BC_WindowBase::~BC_WindowBase");
104 #ifdef HAVE_LIBXXF86VM
105 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
112 if(window_type != MAIN_WINDOW)
115 XSelectInput(top_level->display, this->win, 0);
116 XSync(top_level->display,0);
117 #ifndef SINGLE_THREAD
118 top_level->dequeue_events(win);
120 // drop active window refs to this
121 if(top_level->active_menubar == this) top_level->active_menubar = 0;
122 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
123 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
124 // drop motion window refs to this
125 if(top_level->motion_events && top_level->last_motion_win == this->win)
126 top_level->motion_events = 0;
128 // Remove pointer from parent window to this
129 parent_window->subwindows->remove(this);
132 if(grab_active) grab_active->active_grab = 0;
133 if(icon_window) delete icon_window;
134 if(window_type == POPUP_WINDOW)
135 parent_window->remove_popup(this);
137 // Delete the subwindows
140 while(subwindows->total)
142 // Subwindow removes its own pointer
143 delete subwindows->values[0];
151 if( get_resources()->get_synchronous() &&
152 (top_level->options & GLX_WINDOW) && glx_win != 0 )
153 get_resources()->get_synchronous()->delete_window(this);
156 XDestroyWindow(top_level->display, win);
158 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
159 if(icon_pixmap) delete icon_pixmap;
160 if(temp_bitmap) delete temp_bitmap;
161 top_level->active_bitmaps.remove_buffers(this);
162 if(_7segment_pixmaps)
164 for(int i = 0; i < TOTAL_7SEGMENT; i++)
165 delete _7segment_pixmaps[i];
167 delete [] _7segment_pixmaps;
172 if(window_type == MAIN_WINDOW)
174 XFreeGC(display, gc);
175 static XFontStruct *BC_WindowBase::*xfont[] = {
176 &BC_WindowBase::smallfont,
177 &BC_WindowBase::mediumfont,
178 &BC_WindowBase::largefont,
179 &BC_WindowBase::bigfont,
181 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
182 XFreeFont(display, this->*xfont[i]);
184 // bug in X causes XRenderExtensionInfo to be damaged if this is done here
185 // left to be done in XCloseDisplay by Xlib.
186 #if defined(HAVE_XFT) && 0
187 static void *BC_WindowBase::*xft_font[] = {
188 &BC_WindowBase::smallfont_xft,
189 &BC_WindowBase::mediumfont_xft,
190 &BC_WindowBase::largefont_xft,
191 &BC_WindowBase::bigfont_xft,
192 &BC_WindowBase::bold_smallfont_xft,
193 &BC_WindowBase::bold_mediumfont_xft,
194 &BC_WindowBase::bold_largefont_xft,
196 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
197 XftFont *xft = (XftFont *)(this->*xft_font[i]);
198 if( xft ) XftFontClose (display, xft);
206 XFree(xinerama_info);
207 xinerama_screens = 0;
209 if( xvideo_port_id >= 0 )
210 XvUngrabPort(display, xvideo_port_id, CurrentTime);
213 // Can't close display if another thread is waiting for events.
214 // Synchronous thread must delete display it owns a GLX_WINDOW
215 // Must be last reference to display.
216 #ifndef SINGLE_THREAD
218 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
219 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
220 "implemented for BC_Pixmap.\n"));
221 get_resources()->get_synchronous()->delete_display(this);
225 XCloseDisplay(display);
226 // clipboard uses a different display connection
227 clipboard->stop_clipboard();
229 #endif // SINGLE_THREAD
232 resize_history.remove_all_objects();
234 #ifndef SINGLE_THREAD
235 common_events.remove_all_objects();
237 delete event_condition;
240 top_level->window_lock = 0;
241 BC_Display::unlock_display();
246 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
247 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
248 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
251 UNSET_ALL_LOCKS(this)
254 int BC_WindowBase::initialize()
259 display_lock_owner = 0;
275 xinerama_screens = 0;
280 translation_events = 0;
281 ctrl_mask = shift_mask = alt_mask = 0;
282 cursor_x = cursor_y = button_number = 0;
296 active_popup_menu = 0;
297 active_subwindow = 0;
300 _7segment_pixmaps = 0;
303 // next_repeat_id = 0;
306 current_font = MEDIUMFONT;
307 current_color = BLACK;
308 current_cursor = ARROW_CURSOR;
311 shared_bg_pixmap = 0;
314 window_type = MAIN_WINDOW;
315 translation_count = 0;
316 x_correction = y_correction = 0;
325 #ifdef HAVE_LIBXXF86VM
329 bold_largefont_xft = 0;
330 bold_mediumfont_xft = 0;
331 bold_smallfont_xft = 0;
339 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
341 // Need these right away since put_event is called before run_window sometimes.
342 event_lock = new Mutex("BC_WindowBase::event_lock");
343 event_condition = new Condition(0, "BC_WindowBase::event_condition");
344 init_lock = new Condition(0, "BC_WindowBase::init_lock");
347 cursor_timer = new Timer;
350 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
351 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
352 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
366 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
369 ButtonReleaseMask | \
370 PointerMotionMask | \
374 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
386 const char *display_name,
388 BC_Pixmap *bg_pixmap,
391 XSetWindowAttributes attr;
393 XSizeHints size_hints;
396 #ifdef HAVE_LIBXXF86VM
400 id = get_resources()->get_id();
402 if(parent_window) top_level = parent_window->top_level;
403 if( top_level ) { // need this to avoid deadlock with Xlib's locks
405 lock_window("BC_WindowBase::create_window");
407 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
409 #ifdef HAVE_LIBXXF86VM
410 if(window_type == VIDMODE_SCALED_WINDOW)
411 closest_vm(&vm,&w,&h);
418 this->bg_color = bg_color;
419 this->window_type = window_type;
421 this->private_color = private_color;
422 this->parent_window = parent_window;
423 this->bg_pixmap = bg_pixmap;
424 this->allow_resize = allow_resize;
426 strcpy(this->display_name, display_name);
428 this->display_name[0] = 0;
431 if(bg_pixmap) shared_bg_pixmap = 1;
433 subwindows = new BC_SubWindowList;
435 if(window_type == MAIN_WINDOW)
438 parent_window = this;
442 display = BC_Display::get_display(display_name);
443 BC_Display::lock_display("BC_WindowBase::create_window");
444 // BC_Display::display_global->new_window(this);
447 // get the display connection
449 // This function must be the first Xlib
450 // function a multi-threaded program calls
452 display = init_display(display_name);
453 if( shm_completion_event < 0 ) shm_completion_event =
454 ShmCompletion + XShmGetEventBase(display);
457 screen = DefaultScreen(display);
458 rootwin = RootWindow(display, screen);
459 // window placement boundaries
460 if( !xinerama_screens && XineramaIsActive(display) )
461 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
462 root_w = get_root_w(0);
463 root_h = get_root_h(0);
466 vis = get_glx_visual(display);
469 vis = DefaultVisual(display, screen);
471 default_depth = DefaultDepth(display, screen);
473 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
474 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
477 // This must be done before fonts to know if antialiasing is available.
480 if(resources.use_shm < 0) resources.initialize_display(this);
481 x_correction = BC_DisplayInfo::get_left_border();
482 y_correction = BC_DisplayInfo::get_top_border();
484 // clamp window placement
485 if(this->x + this->w + x_correction > root_w)
486 this->x = root_w - this->w - x_correction;
487 if(this->y + this->h + y_correction > root_h)
488 this->y = root_h - this->h - y_correction;
489 if(this->x < 0) this->x = 0;
490 if(this->y < 0) this->y = 0;
492 if(this->bg_color == -1)
493 this->bg_color = resources.get_bg_color();
495 // printf("bcwindowbase 1 %s\n", title);
496 // if(window_type == MAIN_WINDOW) sleep(1);
497 // printf("bcwindowbase 10\n");
503 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
505 attr.event_mask = DEFAULT_EVENT_MASKS |
506 StructureNotifyMask |
510 attr.background_pixel = get_color(this->bg_color);
511 attr.colormap = cmap;
512 attr.cursor = get_cursor_struct(ARROW_CURSOR);
514 win = XCreateWindow(display, rootwin,
515 this->x, this->y, this->w, this->h, 0,
516 top_level->default_depth, InputOutput,
518 XGetNormalHints(display, win, &size_hints);
520 size_hints.flags = PSize | PMinSize | PMaxSize;
521 size_hints.width = this->w;
522 size_hints.height = this->h;
523 size_hints.min_width = allow_resize ? minw : this->w;
524 size_hints.max_width = allow_resize ? 32767 : this->w;
525 size_hints.min_height = allow_resize ? minh : this->h;
526 size_hints.max_height = allow_resize ? 32767 : this->h;
527 if(x > -BC_INFINITY && x < BC_INFINITY)
529 size_hints.flags |= PPosition;
530 size_hints.x = this->x;
531 size_hints.y = this->y;
535 txlist[0] = this->title;
537 XTextProperty titleprop;
538 if(options & WINDOW_UTF8)
539 Xutf8TextListToTextProperty(display, txlist, 1,
540 XUTF8StringStyle, &titleprop);
542 XmbTextListToTextProperty(display, txlist, 1,
543 XStdICCTextStyle, &titleprop);
544 XSetWMProperties(display, win, &titleprop, &titleprop,
545 0, 0, &size_hints, 0, 0);
546 XFree(titleprop.value);
549 #ifndef SINGLE_THREAD
550 clipboard = new BC_Clipboard(display_name);
551 clipboard->start_clipboard();
557 Atom ClientLeaderXAtom;
558 if (XGroupLeader == 0)
560 const char *instance_name = "cinelerra";
561 const char *class_name = "Cinelerra";
562 XClassHint *class_hints = XAllocClassHint();
563 class_hints->res_name = (char*)instance_name;
564 class_hints->res_class = (char*)class_name;
565 XSetClassHint(top_level->display, win, class_hints);
567 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
568 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
569 PropModeReplace, (unsigned char *)&XGroupLeader, true);
574 #ifdef HAVE_LIBXXF86VM
575 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
582 #ifdef HAVE_LIBXXF86VM
583 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
585 if(window_type == POPUP_WINDOW)
588 mask = CWEventMask | CWBackPixel | CWColormap |
589 CWOverrideRedirect | CWSaveUnder | CWCursor;
591 attr.event_mask = DEFAULT_EVENT_MASKS |
595 if(this->bg_color == -1)
596 this->bg_color = resources.get_bg_color();
597 attr.background_pixel = top_level->get_color(bg_color);
598 attr.colormap = top_level->cmap;
599 if(top_level->is_hourglass)
600 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
602 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
603 attr.override_redirect = True;
604 attr.save_under = True;
606 win = XCreateWindow(top_level->display,
607 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
608 top_level->default_depth, InputOutput, top_level->vis, mask,
610 top_level->add_popup(this);
613 if(window_type == SUB_WINDOW)
615 mask = CWEventMask | CWBackPixel | CWCursor;
616 attr.event_mask = DEFAULT_EVENT_MASKS;
617 attr.background_pixel = top_level->get_color(this->bg_color);
618 if(top_level->is_hourglass)
619 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
621 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
622 win = XCreateWindow(top_level->display,
623 parent_window->win, this->x, this->y, this->w, this->h, 0,
624 top_level->default_depth, InputOutput, top_level->vis, mask,
627 if(!hidden) XMapWindow(top_level->display, win);
630 // Create pixmap for all windows
631 pixmap = new BC_Pixmap(this, this->w, this->h);
633 // Set up options for main window
634 if(window_type == MAIN_WINDOW)
636 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
638 this->bg_pixmap = new BC_Pixmap(this,
639 get_resources()->bg_image,
643 if(!hidden) show_window();
647 draw_background(0, 0, this->w, this->h);
649 flash(-1, -1, -1, -1, 0);
651 // Set up options for popup window
652 #ifdef HAVE_LIBXXF86VM
653 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
655 if(window_type == POPUP_WINDOW)
659 if(!hidden) show_window();
661 get_resources()->create_window_lock->unlock();
662 if(need_lock) unlock_window();
667 Display* BC_WindowBase::init_display(const char *display_name)
671 if(display_name && display_name[0] == 0) display_name = NULL;
672 if((display = XOpenDisplay(display_name)) == NULL) {
673 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
675 if(getenv("DISPLAY") == NULL) {
676 printf(_("'DISPLAY' environment variable not set.\n"));
680 // Try again with default display.
681 if((display = XOpenDisplay(0)) == NULL) {
682 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
690 Display* BC_WindowBase::get_display()
692 return top_level->display;
695 int BC_WindowBase::get_screen()
697 return top_level->screen;
700 int BC_WindowBase::run_window()
706 // Events may have been sent before run_window so can't initialize them here.
709 set_repeat(get_resources()->tooltip_delay);
710 BC_Display::display_global->new_window(this);
712 // If the first window created, run the display loop in this thread.
713 if(BC_Display::display_global->is_first(this))
715 BC_Display::unlock_display();
716 BC_Display::display_global->loop();
720 BC_Display::unlock_display();
721 completion_lock->lock("BC_WindowBase::run_window");
724 BC_Display::lock_display("BC_WindowBase::run_window");
725 BC_Display::display_global->delete_window(this);
727 unset_all_repeaters();
729 BC_Display::unlock_display();
731 #else // SINGLE_THREAD
736 set_repeat(get_resources()->tooltip_delay);
738 // Start X server events
739 event_thread = new BC_WindowEvents(this);
740 event_thread->start();
746 // Handle common events
752 unset_all_repeaters();
756 event_condition->reset();
757 common_events.remove_all_objects();
761 #endif // SINGLE_THREAD
766 int BC_WindowBase::get_key_masks(unsigned int key_state)
768 // printf("BC_WindowBase::get_key_masks %llx\n",
769 // event->xkey.state);
770 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
771 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
772 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
777 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
779 BC_KeyboardHandlerLock set;
780 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
783 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
785 BC_KeyboardHandlerLock set;
786 int i = BC_KeyboardHandler::listeners.size();
787 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
788 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
791 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
793 int result = (win->*handler)(wp);
797 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
800 BC_KeyboardHandlerLock set;
801 for( int i=0; !result && i<listeners.size(); ++i ) {
802 BC_KeyboardHandler *listener = listeners[i];
803 result = listener->run_event(wp);
808 void BC_KeyboardHandler::kill_grabs()
810 BC_KeyboardHandlerLock set;
811 for( int i=0; i<listeners.size(); ++i ) {
812 BC_WindowBase *win = listeners[i]->win;
813 if( win->get_window_type() != POPUP_WINDOW ) continue;
814 ((BC_Popup *)win)->ungrab_keyboard();
818 void BC_ActiveBitmaps::reque(XEvent *event)
820 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
821 ShmSeg shmseg = shm_ev->shmseg;
822 Drawable drawable = shm_ev->drawable;
823 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
824 active_lock.lock("BC_BitmapImage::reque");
825 BC_BitmapImage *bfr = first;
826 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
827 if( bfr && bfr->drawable == drawable )
829 active_lock.unlock();
831 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
832 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
833 // (int)shmseg, (int)drawable);
836 if( bfr->drawable != drawable ) return;
837 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
838 bfr->bitmap->reque(bfr);
841 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
843 active_lock.lock("BC_BitmapImage::insert");
844 bfr->drawable = pixmap;
846 active_lock.unlock();
849 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
851 active_lock.lock("BC_ActiveBitmaps::remove");
852 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
854 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
855 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
857 active_lock.unlock();
860 BC_ActiveBitmaps::BC_ActiveBitmaps()
864 BC_ActiveBitmaps::~BC_ActiveBitmaps()
870 int BC_WindowBase::keysym_lookup(XEvent *event)
872 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
873 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
875 if( event->xany.send_event && !event->xany.serial ) {
876 keysym = (KeySym) event->xkey.keycode;
877 keys_return[0] = keysym;
880 wkey_string_length = 0;
882 if( input_context ) {
883 wkey_string_length = XwcLookupString(input_context,
884 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
885 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
886 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
889 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
890 keys_return, KEYPRESSLEN, &keysym, &stat);
891 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
892 if( stat == XLookupBoth ) return ret;
893 if( stat == XLookupKeySym ) return 0;
895 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
896 wkey_string_length = ret;
897 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
901 pthread_t locking_task = (pthread_t)-1L;
902 int locking_event = -1;
903 int locking_message = -1;
905 int BC_WindowBase::dispatch_event(XEvent *event)
909 XClientMessageEvent *ptr;
910 int cancel_resize, cancel_translation;
915 #ifndef SINGLE_THREAD
916 // If an event is waiting get it, otherwise
917 // wait for next event only if there are no compressed events.
918 if(get_event_count() ||
919 (!motion_events && !resize_events && !translation_events))
922 // Lock out window deletions
923 lock_window("BC_WindowBase::dispatch_event 1");
924 locking_event = event->type;
925 locking_task = pthread_self();
926 locking_message = event->xclient.message_type;
929 // Handle compressed events
931 lock_window("BC_WindowBase::dispatch_event 2");
933 dispatch_resize_event(last_resize_w, last_resize_h);
935 dispatch_motion_event();
936 if(translation_events)
937 dispatch_translation_event();
948 //static const char *event_names[] = {
949 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
950 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
951 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
952 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
953 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
954 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
955 // "GenericEvent", "LASTEvent",
959 //if(event->type != ClientMessage) {
960 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
961 //__LINE__, title, event, event->type,
962 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
963 // event_names[event->type] : "Unknown");
966 if( active_grab && active_grab->grab_event(event) ) {
972 switch(event->type) {
974 // Clear the resize buffer
975 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
976 // Clear the motion buffer since this can clear the window
979 dispatch_motion_event();
982 ptr = (XClientMessageEvent*)event;
985 if(ptr->message_type == ProtoXAtom &&
986 (Atom)ptr->data.l[0] == DelWinXAtom)
991 if(ptr->message_type == RepeaterXAtom)
993 dispatch_repeat_event(ptr->data.l[0]);
994 // Make sure the repeater still exists.
995 // for(int i = 0; i < repeaters.total; i++)
997 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
999 // dispatch_repeat_event_master(ptr->data.l[0]);
1005 if(ptr->message_type == SetDoneXAtom)
1009 { // We currently use X marshalling for xatom events, we can switch to something else later
1010 recieve_custom_xatoms((xatom_event *)ptr);
1016 dispatch_focus_in();
1021 dispatch_focus_out();
1033 get_key_masks(event->xbutton.state);
1034 cursor_x = event->xbutton.x;
1035 cursor_y = event->xbutton.y;
1036 button_number = event->xbutton.button;
1038 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1039 event_win = event->xany.window;
1040 if (button_number < 6)
1042 if(button_number < 4)
1044 button_pressed = event->xbutton.button;
1045 button_time1 = button_time2;
1046 button_time2 = button_time3;
1047 button_time3 = event->xbutton.time;
1050 drag_win = event_win;
1051 drag_x1 = cursor_x - get_resources()->drag_radius;
1052 drag_x2 = cursor_x + get_resources()->drag_radius;
1053 drag_y1 = cursor_y - get_resources()->drag_radius;
1054 drag_y2 = cursor_y + get_resources()->drag_radius;
1056 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1059 button_time3 = button_time2 = button_time1 = 0;
1061 if((long)(button_time3 - button_time2) < resources.double_click)
1064 // button_time3 = button_time2 = button_time1 = 0;
1072 dispatch_button_press();
1077 get_key_masks(event->xbutton.state);
1078 button_number = event->xbutton.button;
1079 event_win = event->xany.window;
1080 if (button_number < 6)
1082 if(button_number < 4)
1084 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1086 dispatch_button_release();
1091 event_win = event->xany.window;
1092 dispatch_expose_event();
1096 get_key_masks(event->xmotion.state);
1097 // Dispatch previous motion event if this is a subsequent motion from a different window
1098 if(motion_events && last_motion_win != event->xany.window)
1100 dispatch_motion_event();
1103 // Buffer the current motion
1105 last_motion_state = event->xmotion.state;
1106 last_motion_x = event->xmotion.x;
1107 last_motion_y = event->xmotion.y;
1108 last_motion_win = event->xany.window;
1111 case ConfigureNotify:
1112 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1114 // event->xany.window,
1117 XTranslateCoordinates(top_level->display,
1125 last_resize_w = event->xconfigure.width;
1126 last_resize_h = event->xconfigure.height;
1129 cancel_translation = 0;
1131 // Resize history prevents responses to recursive resize requests
1132 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1134 if(resize_history.values[i]->w == last_resize_w &&
1135 resize_history.values[i]->h == last_resize_h)
1137 delete resize_history.values[i];
1138 resize_history.remove_number(i);
1143 if(last_resize_w == w && last_resize_h == h)
1151 if((last_translate_x == x && last_translate_y == y))
1152 cancel_translation = 1;
1154 if(!cancel_translation)
1156 translation_events = 1;
1159 translation_count++;
1163 get_key_masks(event->xkey.state);
1164 keys_return[0] = 0; keysym = -1;
1165 if(XFilterEvent(event, win)) {
1168 if( keysym_lookup(event) < 0 ) {
1169 printf("keysym %x\n", (uint32_t)keysym);
1173 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1177 // block out control keys
1178 if(keysym > 0xffe0 && keysym < 0xffff) break;
1179 // block out Alt_GR key
1180 if(keysym == 0xfe03) break;
1183 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1185 #ifdef X_HAVE_UTF8_STRING
1186 //It's Ascii or UTF8?
1187 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1188 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1190 if( ((keys_return[1] & 0xff) > 0x80) &&
1191 ((keys_return[0] & 0xff) > 0xC0) ) {
1192 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1193 key_pressed = keysym & 0xff;
1199 // block out extra keys
1209 // Translate key codes
1210 case XK_Return: key_pressed = RETURN; break;
1211 case XK_Up: key_pressed = UP; break;
1212 case XK_Down: key_pressed = DOWN; break;
1213 case XK_Left: key_pressed = LEFT; break;
1214 case XK_Right: key_pressed = RIGHT; break;
1215 case XK_Next: key_pressed = PGDN; break;
1216 case XK_Prior: key_pressed = PGUP; break;
1217 case XK_BackSpace: key_pressed = BACKSPACE; break;
1218 case XK_Escape: key_pressed = ESC; break;
1221 key_pressed = LEFTTAB;
1225 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1226 case XK_underscore: key_pressed = '_'; break;
1227 case XK_asciitilde: key_pressed = '~'; break;
1228 case XK_Delete: key_pressed = DELETE; break;
1229 case XK_Home: key_pressed = HOME; break;
1230 case XK_End: key_pressed = END; break;
1233 case XK_KP_Enter: key_pressed = KPENTER; break;
1234 case XK_KP_Add: key_pressed = KPPLUS; break;
1235 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1236 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1237 case XK_KP_Divide: key_pressed = KPSLASH; break;
1239 case XK_KP_End: key_pressed = KP1; break;
1241 case XK_KP_Down: key_pressed = KP2; break;
1243 case XK_KP_Page_Down: key_pressed = KP3; break;
1245 case XK_KP_Left: key_pressed = KP4; break;
1247 case XK_KP_Begin: key_pressed = KP5; break;
1249 case XK_KP_Right: key_pressed = KP6; break;
1251 case XK_KP_Home: key_pressed = KP7; break;
1253 case XK_KP_Up: key_pressed = KP8; break;
1255 case XK_KP_Page_Up: key_pressed = KP9; break;
1257 case XK_KP_Insert: key_pressed = KPINS; break;
1259 case XK_KP_Delete: key_pressed = KPDEL; break;
1260 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1262 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1263 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1264 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1265 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1266 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1267 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1268 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1269 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1270 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1271 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1272 // above case XK_Left: key_pressed = LEFT; break; /* left */
1273 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1274 // above case XK_Down: key_pressed = DOWN; break; /* down */
1275 // above case XK_Up: key_pressed = UP; break; /* up */
1276 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1277 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1278 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1279 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1280 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1281 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1282 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1283 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1284 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1285 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1286 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1287 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1290 key_pressed = keysym & 0xff;
1291 #ifdef X_HAVE_UTF8_STRING
1292 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1297 #ifdef X_HAVE_UTF8_STRING
1299 key_pressed_utf8 = keys_return;
1304 if( top_level == this )
1305 result = BC_KeyboardHandler::run_listeners(this);
1307 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1309 result = dispatch_keypress_event();
1310 // Handle some default keypresses
1313 if(key_pressed == 'w' ||
1322 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1323 dispatch_keyrelease_event();
1324 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1325 // keysym, event->xkey.state);
1329 event_win = event->xany.window;
1330 dispatch_cursor_leave();
1334 event_win = event->xany.window;
1335 cursor_x = event->xcrossing.x;
1336 cursor_y = event->xcrossing.y;
1337 dispatch_cursor_enter();
1342 //printf("100 %s %p %d\n", title, event, event->type);
1343 //if(event->type != ClientMessage) dump();
1345 #ifndef SINGLE_THREAD
1347 if(event) delete event;
1349 // if(done) completion_lock->unlock();
1352 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1356 int BC_WindowBase::dispatch_expose_event()
1359 for(int i = 0; i < subwindows->total && !result; i++)
1361 result = subwindows->values[i]->dispatch_expose_event();
1364 // Propagate to user
1365 if(!result) expose_event();
1369 int BC_WindowBase::dispatch_resize_event(int w, int h)
1371 // Can't store new w and h until the event is handles
1372 // because bcfilebox depends on the old w and h to
1373 // reposition widgets.
1374 if( window_type == MAIN_WINDOW ) {
1379 pixmap = new BC_Pixmap(this, w, h);
1380 clear_box(0, 0, w, h);
1383 // Propagate to subwindows
1384 for(int i = 0; i < subwindows->total; i++) {
1385 subwindows->values[i]->dispatch_resize_event(w, h);
1388 // Propagate to user
1391 if( window_type == MAIN_WINDOW ) {
1400 int BC_WindowBase::dispatch_flash()
1403 for(int i = 0; i < subwindows->total; i++)
1404 subwindows->values[i]->dispatch_flash();
1408 int BC_WindowBase::dispatch_translation_event()
1410 translation_events = 0;
1411 if(window_type == MAIN_WINDOW)
1415 x = last_translate_x;
1416 y = last_translate_y;
1417 // Correct for window manager offsets
1422 for(int i = 0; i < subwindows->total; i++)
1424 subwindows->values[i]->dispatch_translation_event();
1427 translation_event();
1431 int BC_WindowBase::dispatch_motion_event()
1436 if(top_level == this)
1439 event_win = last_motion_win;
1440 get_key_masks(last_motion_state);
1443 if(get_button_down() && !active_menubar && !active_popup_menu)
1447 cursor_x = last_motion_x;
1448 cursor_y = last_motion_y;
1449 result = dispatch_drag_motion();
1453 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1454 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1459 result = dispatch_drag_start();
1463 cursor_x = last_motion_x;
1464 cursor_y = last_motion_y;
1466 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1469 // active_popup_menu,
1470 // active_subwindow);
1472 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1473 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1474 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1477 // Dispatch in stacking order
1478 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1480 result = subwindows->values[i]->dispatch_motion_event();
1483 if(!result) result = cursor_motion_event(); // give to user
1487 int BC_WindowBase::dispatch_keypress_event()
1490 if(top_level == this)
1492 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1495 for(int i = 0; i < subwindows->total && !result; i++)
1497 result = subwindows->values[i]->dispatch_keypress_event();
1500 if(!result) result = keypress_event();
1505 int BC_WindowBase::dispatch_keyrelease_event()
1508 if(top_level == this)
1510 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1513 for(int i = 0; i < subwindows->total && !result; i++)
1515 result = subwindows->values[i]->dispatch_keyrelease_event();
1518 if(!result) result = keyrelease_event();
1523 int BC_WindowBase::dispatch_focus_in()
1525 for(int i = 0; i < subwindows->total; i++)
1527 subwindows->values[i]->dispatch_focus_in();
1535 int BC_WindowBase::dispatch_focus_out()
1537 for(int i = 0; i < subwindows->total; i++)
1539 subwindows->values[i]->dispatch_focus_out();
1547 int BC_WindowBase::get_has_focus()
1549 return top_level->has_focus;
1552 int BC_WindowBase::get_deleting()
1554 if(is_deleting) return 1;
1555 if(parent_window && parent_window->get_deleting()) return 1;
1559 int BC_WindowBase::dispatch_button_press()
1564 if(top_level == this)
1566 if(active_menubar) result = active_menubar->dispatch_button_press();
1567 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1568 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1571 for(int i = 0; i < subwindows->total && !result; i++)
1573 result = subwindows->values[i]->dispatch_button_press();
1576 if(!result) result = button_press_event();
1582 int BC_WindowBase::dispatch_button_release()
1585 if(top_level == this)
1587 if(active_menubar) result = active_menubar->dispatch_button_release();
1588 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1589 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1590 if(!result && button_number != 4 && button_number != 5)
1591 result = dispatch_drag_stop();
1594 for(int i = 0; i < subwindows->total && !result; i++)
1596 result = subwindows->values[i]->dispatch_button_release();
1601 result = button_release_event();
1608 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1611 // all repeat event handlers get called and decide based on activity and duration
1612 // whether to respond
1613 for(int i = 0; i < subwindows->total; i++)
1615 subwindows->values[i]->dispatch_repeat_event(duration);
1619 repeat_event(duration);
1623 // Unlock next repeat signal
1624 if(window_type == MAIN_WINDOW)
1626 #ifdef SINGLE_THREAD
1627 BC_Display::display_global->unlock_repeaters(duration);
1629 for(int i = 0; i < repeaters.total; i++)
1631 if(repeaters.values[i]->delay == duration)
1633 repeaters.values[i]->repeat_lock->unlock();
1641 void BC_WindowBase::unhide_cursor()
1646 if(top_level->is_hourglass)
1647 set_cursor(HOURGLASS_CURSOR, 1, 0);
1649 set_cursor(current_cursor, 1, 0);
1651 cursor_timer->update();
1655 void BC_WindowBase::update_video_cursor()
1657 if(video_on && !is_transparent)
1659 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1662 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1663 cursor_timer->update();
1668 cursor_timer->update();
1673 int BC_WindowBase::dispatch_cursor_leave()
1677 for(int i = 0; i < subwindows->total; i++)
1679 subwindows->values[i]->dispatch_cursor_leave();
1682 cursor_leave_event();
1686 int BC_WindowBase::dispatch_cursor_enter()
1692 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1693 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1694 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1696 for(int i = 0; !result && i < subwindows->total; i++)
1698 result = subwindows->values[i]->dispatch_cursor_enter();
1701 if(!result) result = cursor_enter_event();
1705 int BC_WindowBase::cursor_enter_event()
1710 int BC_WindowBase::cursor_leave_event()
1715 int BC_WindowBase::close_event()
1721 int BC_WindowBase::dispatch_drag_start()
1724 if(active_menubar) result = active_menubar->dispatch_drag_start();
1725 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1726 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1728 for(int i = 0; i < subwindows->total && !result; i++)
1730 result = subwindows->values[i]->dispatch_drag_start();
1733 if(!result) result = is_dragging = drag_start_event();
1737 int BC_WindowBase::dispatch_drag_stop()
1741 for(int i = 0; i < subwindows->total && !result; i++)
1743 result = subwindows->values[i]->dispatch_drag_stop();
1746 if(is_dragging && !result)
1756 int BC_WindowBase::dispatch_drag_motion()
1759 for(int i = 0; i < subwindows->total && !result; i++)
1761 result = subwindows->values[i]->dispatch_drag_motion();
1764 if(is_dragging && !result)
1766 drag_motion_event();
1777 int BC_WindowBase::show_tooltip(int w, int h)
1781 if(tooltip_text && !tooltip_on &&
1782 (force_tooltip || get_resources()->tooltips_enabled))
1785 top_level->hide_tooltip();
1789 w = get_text_width(MEDIUMFONT, tooltip_text);
1792 h = get_text_height(MEDIUMFONT, tooltip_text);
1794 w += TOOLTIP_MARGIN * 2;
1795 h += TOOLTIP_MARGIN * 2;
1797 XTranslateCoordinates(top_level->display, win,
1798 top_level->rootwin, get_w(), get_h(),
1800 // keep the tip inside the window/display
1801 int top_x = top_level->get_x();
1802 if( x < top_x ) x = top_x;
1803 int top_w = top_x + top_level->get_w();
1804 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1805 if( top_w < lmt_w ) lmt_w = top_w;
1806 if( x+w > lmt_w ) x = lmt_w-w;
1808 int top_y = top_level->get_y();
1809 if( y < top_y ) y = top_y;
1810 int top_h = top_y + top_level->get_h();
1811 int lmt_h = top_level->get_root_h(0);
1812 if( top_h < lmt_h ) lmt_h = top_h;
1813 if( y+h > lmt_h ) y = lmt_h-h;
1815 int abs_x, abs_y, win_x, win_y;
1816 unsigned int temp_mask;
1818 XQueryPointer(top_level->display, top_level->win,
1819 &temp_win, &temp_win, &abs_x, &abs_y,
1820 &win_x, &win_y, &temp_mask);
1821 // check for cursor inside popup
1822 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1824 if( x-abs_x < y-abs_y )
1829 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1830 get_resources()->tooltip_bg_color);
1833 tooltip_popup->set_font(MEDIUMFONT);
1834 tooltip_popup->flash();
1835 tooltip_popup->flush();
1840 int BC_WindowBase::hide_tooltip()
1843 for(int i = 0; i < subwindows->total; i++)
1845 subwindows->values[i]->hide_tooltip();
1851 delete tooltip_popup;
1857 const char *BC_WindowBase::get_tooltip()
1859 return tooltip_text;
1862 int BC_WindowBase::set_tooltip(const char *text)
1864 tooltip_text = text;
1866 // Update existing tooltip if it is visible
1870 tooltip_popup->flash();
1874 void BC_WindowBase::set_tooltip_done(int v)
1879 // signal the event handler to repeat
1880 int BC_WindowBase::set_repeat(int64_t duration)
1884 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1887 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1889 #ifdef SINGLE_THREAD
1890 BC_Display::display_global->set_repeat(this, duration);
1892 // test repeater database for duplicates
1893 for(int i = 0; i < repeaters.total; i++)
1896 if(repeaters.values[i]->delay == duration)
1898 repeaters.values[i]->start_repeating(this);
1903 BC_Repeater *repeater = new BC_Repeater(this, duration);
1904 repeater->initialize();
1905 repeaters.append(repeater);
1906 repeater->start_repeating();
1911 int BC_WindowBase::unset_repeat(int64_t duration)
1913 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1915 #ifdef SINGLE_THREAD
1916 BC_Display::display_global->unset_repeat(this, duration);
1918 for(int i = 0; i < repeaters.total; i++)
1920 if(repeaters.values[i]->delay == duration)
1922 repeaters.values[i]->stop_repeating();
1930 int BC_WindowBase::unset_all_repeaters()
1932 #ifdef SINGLE_THREAD
1933 BC_Display::display_global->unset_all_repeaters(this);
1935 for(int i = 0; i < repeaters.total; i++)
1937 repeaters.values[i]->stop_repeating();
1939 repeaters.remove_all_objects();
1944 // long BC_WindowBase::get_repeat_id()
1946 // return top_level->next_repeat_id++;
1949 XEvent *BC_WindowBase::new_xevent()
1951 XEvent *event = new XEvent;
1952 memset(event, 0, sizeof(*event));
1956 #ifndef SINGLE_THREAD
1957 int BC_WindowBase::arm_repeat(int64_t duration)
1959 XEvent *event = new_xevent();
1960 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1961 ptr->type = ClientMessage;
1962 ptr->message_type = RepeaterXAtom;
1964 ptr->data.l[0] = duration;
1966 // Couldn't use XSendEvent since it locked up randomly.
1972 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1977 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1979 #ifndef SINGLE_THREAD
1980 XEvent *myevent = new_xevent();
1981 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1982 ptr->type = ClientMessage;
1983 ptr->message_type = event->message_type;
1984 ptr->format = event->format;
1985 ptr->data.l[0] = event->data.l[0];
1986 ptr->data.l[1] = event->data.l[1];
1987 ptr->data.l[2] = event->data.l[2];
1988 ptr->data.l[3] = event->data.l[3];
1989 ptr->data.l[4] = event->data.l[4];
1998 Atom BC_WindowBase::create_xatom(const char *atom_name)
2000 return XInternAtom(display, atom_name, False);
2003 int BC_WindowBase::get_atoms()
2005 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
2006 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
2007 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
2008 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
2009 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
2010 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
2011 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2017 void BC_WindowBase::init_cursors()
2019 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2020 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2021 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2022 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2023 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2024 move_cursor = XCreateFontCursor(display, XC_fleur);
2025 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2026 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2027 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2028 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2029 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2030 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2031 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2032 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2035 static char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2036 Colormap colormap = DefaultColormap(display, screen);
2037 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2038 rootwin, cursor_data, 8, 8);
2039 XColor black, dummy;
2040 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2041 transparent_cursor = XCreatePixmapCursor(display,
2042 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2043 // XDefineCursor(display, win, transparent_cursor);
2044 XFreePixmap(display, pixmap_bottom);
2046 int iw = 23, iw1 = iw-1, iw2 = iw/2;
2047 int ih = 23, ih1 = ih-1, ih2 = ih/2;
2048 VFrame grab(iw,ih,BC_RGB888);
2050 grab.set_pixel_color(RED); // fg
2051 grab.draw_smooth(iw2,0, iw1,0, iw1,ih2);
2052 grab.draw_smooth(iw1,ih2, iw1,ih1, iw2,ih1);
2053 grab.draw_smooth(iw2,ih1, 0,ih1, 0,ih2);
2054 grab.draw_smooth(0,ih2, 0,0, iw2,0);
2055 grab.set_pixel_color(WHITE); // bg
2056 grab.draw_line(0,ih2, iw2-2,ih2);
2057 grab.draw_line(iw2+2,ih2, iw1,ih2);
2058 grab.draw_line(iw2,0, iw2,ih2-2);
2059 grab.draw_line(iw2,ih2+2, iw2,ih1);
2061 int bpl = (iw+7)/8, isz = bpl * ih;
2062 char img[isz]; memset(img, 0, isz);
2063 char msk[isz]; memset(msk, 0, isz);
2064 unsigned char **rows = grab.get_rows();
2065 for( int iy=0; iy<ih; ++iy ) {
2066 char *op = img + iy*bpl;
2067 char *mp = msk + iy*bpl;
2068 unsigned char *ip = rows[iy];
2069 for( int ix=0; ix<iw; ++ix,ip+=3 ) {
2070 if( ip[0] ) mp[ix>>3] |= (1<<(ix&7));
2071 if( !ip[1] ) op[ix>>3] |= (1<<(ix&7));
2074 unsigned long white_pix = WhitePixel(display, screen);
2075 unsigned long black_pix = BlackPixel(display, screen);
2076 Pixmap img_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2077 img, iw,ih, white_pix,black_pix, 1);
2078 Pixmap msk_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2079 msk, iw,ih, white_pix,black_pix, 1);
2082 fc.flags = bc.flags = DoRed | DoGreen | DoBlue;
2083 fc.red = 0xffff; fc.green = fc.blue = 0; // fg
2084 bc.red = bc.green = bc.blue = 0x0000; // bg
2085 grabbed_cursor = XCreatePixmapCursor(display, img_xpm,msk_xpm, &fc,&bc, iw2,ih2);
2086 XFreePixmap(display, img_xpm);
2087 XFreePixmap(display, msk_xpm);
2090 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2092 int color_model = BC_TRANSPARENCY;
2096 color_model = BC_RGB8;
2099 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2102 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2105 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2111 int BC_WindowBase::init_colors()
2114 current_color_value = current_color_pixel = 0;
2116 // Get the real depth
2119 ximage = XCreateImage(top_level->display,
2121 top_level->default_depth,
2129 bits_per_pixel = ximage->bits_per_pixel;
2130 XDestroyImage(ximage);
2132 color_model = evaluate_color_model(client_byte_order,
2135 // Get the color model
2141 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2142 create_private_colors();
2146 cmap = DefaultColormap(display, screen);
2147 create_shared_colors();
2150 allocate_color_table();
2154 //cmap = DefaultColormap(display, screen);
2155 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2161 int BC_WindowBase::create_private_colors()
2166 for(int i = 0; i < 255; i++)
2168 color = (i & 0xc0) << 16;
2169 color += (i & 0x38) << 10;
2170 color += (i & 0x7) << 5;
2171 color_table[i][0] = color;
2173 create_shared_colors(); // overwrite the necessary colors on the table
2178 int BC_WindowBase::create_color(int color)
2180 if(total_colors == 256)
2182 // replace the closest match with an exact match
2183 color_table[get_color_rgb8(color)][0] = color;
2187 // add the color to the table
2188 color_table[total_colors][0] = color;
2194 int BC_WindowBase::create_shared_colors()
2196 create_color(BLACK);
2197 create_color(WHITE);
2199 create_color(LTGREY);
2200 create_color(MEGREY);
2201 create_color(MDGREY);
2202 create_color(DKGREY);
2204 create_color(LTCYAN);
2205 create_color(MECYAN);
2206 create_color(MDCYAN);
2207 create_color(DKCYAN);
2209 create_color(LTGREEN);
2210 create_color(GREEN);
2211 create_color(DKGREEN);
2213 create_color(LTPINK);
2217 create_color(LTBLUE);
2219 create_color(DKBLUE);
2221 create_color(LTYELLOW);
2222 create_color(MEYELLOW);
2223 create_color(MDYELLOW);
2224 create_color(DKYELLOW);
2226 create_color(LTPURPLE);
2227 create_color(MEPURPLE);
2228 create_color(MDPURPLE);
2229 create_color(DKPURPLE);
2231 create_color(FGGREY);
2232 create_color(MNBLUE);
2233 create_color(ORANGE);
2234 create_color(FTGREY);
2239 int BC_WindowBase::allocate_color_table()
2241 int red, green, blue, color;
2244 for(int i = 0; i < total_colors; i++)
2246 color = color_table[i][0];
2247 red = (color & 0xFF0000) >> 16;
2248 green = (color & 0x00FF00) >> 8;
2249 blue = color & 0xFF;
2251 col.flags = DoRed | DoGreen | DoBlue;
2252 col.red = red<<8 | red;
2253 col.green = green<<8 | green;
2254 col.blue = blue<<8 | blue;
2256 XAllocColor(display, cmap, &col);
2257 color_table[i][1] = col.pixel;
2260 XInstallColormap(display, cmap);
2264 int BC_WindowBase::init_window_shape()
2266 if(bg_pixmap && bg_pixmap->use_alpha())
2268 XShapeCombineMask(top_level->display,
2269 this->win, ShapeBounding, 0, 0,
2270 bg_pixmap->get_alpha(), ShapeSet);
2276 int BC_WindowBase::init_gc()
2278 unsigned long gcmask;
2279 gcmask = GCFont | GCGraphicsExposures;
2282 gcvalues.font = mediumfont->fid; // set the font
2283 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2284 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2286 // gcmask = GCCapStyle | GCJoinStyle;
2287 // XGetGCValues(display, gc, gcmask, &gcvalues);
2288 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2292 int BC_WindowBase::init_fonts()
2294 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2295 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2296 smallfont = XLoadQueryFont(display, "fixed");
2297 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2298 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2299 mediumfont = XLoadQueryFont(display, "fixed");
2300 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2301 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2302 largefont = XLoadQueryFont(display, "fixed");
2303 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2304 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2305 bigfont = XLoadQueryFont(display, "fixed");
2308 if(get_resources()->use_fontset)
2313 // FIXME: should check the m,d,n values
2314 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2316 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2317 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2318 if( !mediumfontset )
2319 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2320 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2322 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2323 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2325 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2326 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2327 curr_fontset = mediumfontset;
2328 get_resources()->use_fontset = 1;
2332 get_resources()->use_fontset = 0;
2339 void BC_WindowBase::init_xft()
2342 if( !get_resources()->use_xft ) return;
2343 if(!(smallfont_xft =
2344 (resources.small_font_xft[0] == '-' ?
2345 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2346 XftFontOpenName(display, screen, resources.small_font_xft))) )
2347 if(!(smallfont_xft =
2348 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2349 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2350 if(!(mediumfont_xft =
2351 (resources.medium_font_xft[0] == '-' ?
2352 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2353 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2354 if(!(mediumfont_xft =
2355 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2356 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2357 if(!(largefont_xft =
2358 (resources.large_font_xft[0] == '-' ?
2359 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2360 XftFontOpenName(display, screen, resources.large_font_xft))) )
2361 if(!(largefont_xft =
2362 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2363 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2365 (resources.big_font_xft[0] == '-' ?
2366 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2367 XftFontOpenName(display, screen, resources.big_font_xft))) )
2369 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2370 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2372 if(!(bold_smallfont_xft =
2373 (resources.small_b_font_xft[0] == '-' ?
2374 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2375 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2376 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2377 if(!(bold_mediumfont_xft =
2378 (resources.medium_b_font_xft[0] == '-' ?
2379 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2380 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2381 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2382 if(!(bold_largefont_xft =
2383 (resources.large_b_font_xft[0] == '-' ?
2384 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2385 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2386 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2388 // Extension failed to locate fonts
2389 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2390 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2391 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2392 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2393 resources.small_font_xft, smallfont_xft,
2394 resources.medium_font_xft, mediumfont_xft,
2395 resources.large_font_xft, largefont_xft,
2396 resources.big_font_xft, bigfont_xft,
2397 resources.small_b_font_xft, bold_smallfont_xft,
2398 resources.medium_b_font_xft, bold_mediumfont_xft,
2399 resources.large_b_font_xft, bold_largefont_xft);
2400 get_resources()->use_xft = 0;
2406 void BC_WindowBase::init_im()
2408 XIMStyles *xim_styles;
2411 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2413 printf("BC_WindowBase::init_im: Could not open input method.\n");
2416 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2419 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2420 XCloseIM(input_method);
2425 for(int z = 0; z < xim_styles->count_styles; z++)
2427 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2429 xim_style = xim_styles->supported_styles[z];
2437 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2438 XCloseIM(input_method);
2442 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2443 XNClientWindow, win, XNFocusWindow, win, NULL);
2446 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2447 XCloseIM(input_method);
2452 void BC_WindowBase::finit_im()
2454 if( input_context ) {
2455 XDestroyIC(input_context);
2458 if( input_method ) {
2459 XCloseIM(input_method);
2465 int BC_WindowBase::get_color(int64_t color)
2467 // return pixel of color
2468 // use this only for drawing subwindows not for bitmaps
2469 int i, test, difference;
2475 return get_color_rgb8(color);
2476 // test last color looked up
2477 if(current_color_value == color)
2478 return current_color_pixel;
2481 current_color_value = color;
2482 for(i = 0; i < total_colors; i++)
2484 if(color_table[i][0] == color)
2486 current_color_pixel = color_table[i][1];
2487 return current_color_pixel;
2491 // find nearest match
2492 difference = 0xFFFFFF;
2494 for(i = 0; i < total_colors; i++)
2496 test = abs((int)(color_table[i][0] - color));
2498 if(test < difference)
2500 current_color_pixel = color_table[i][1];
2504 return current_color_pixel;
2507 return get_color_rgb16(color);
2510 return get_color_bgr16(color);
2514 return client_byte_order == server_byte_order ?
2515 color : get_color_bgr24(color);
2523 int BC_WindowBase::get_color_rgb8(int color)
2527 pixel = (color & 0xc00000) >> 16;
2528 pixel += (color & 0xe000) >> 10;
2529 pixel += (color & 0xe0) >> 5;
2533 int64_t BC_WindowBase::get_color_rgb16(int color)
2536 result = (color & 0xf80000) >> 8;
2537 result += (color & 0xfc00) >> 5;
2538 result += (color & 0xf8) >> 3;
2543 int64_t BC_WindowBase::get_color_bgr16(int color)
2546 result = (color & 0xf80000) >> 19;
2547 result += (color & 0xfc00) >> 5;
2548 result += (color & 0xf8) << 8;
2553 int64_t BC_WindowBase::get_color_bgr24(int color)
2556 result = (color & 0xff) << 16;
2557 result += (color & 0xff00);
2558 result += (color & 0xff0000) >> 16;
2562 void BC_WindowBase::start_video()
2564 cursor_timer->update();
2566 // set_color(BLACK);
2567 // draw_box(0, 0, get_w(), get_h());
2571 void BC_WindowBase::stop_video()
2579 int64_t BC_WindowBase::get_color()
2581 return top_level->current_color;
2584 void BC_WindowBase::set_color(int64_t color)
2586 top_level->current_color = color;
2587 XSetForeground(top_level->display,
2589 top_level->get_color(color));
2592 void BC_WindowBase::set_opaque()
2594 XSetFunction(top_level->display, top_level->gc, GXcopy);
2597 void BC_WindowBase::set_inverse()
2599 XSetFunction(top_level->display, top_level->gc, GXxor);
2602 void BC_WindowBase::set_line_width(int value)
2604 this->line_width = value;
2605 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2606 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2607 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2608 JoinMiter); /* join_style */
2610 if(line_dashes > 0) {
2611 const char dashes = line_dashes;
2612 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2615 // XGCValues gcvalues;
2616 // unsigned long gcmask;
2617 // gcmask = GCCapStyle | GCJoinStyle;
2618 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2619 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2622 void BC_WindowBase::set_line_dashes(int value)
2624 line_dashes = value;
2625 // call XSetLineAttributes
2626 set_line_width(line_width);
2630 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2634 case ARROW_CURSOR: return top_level->arrow_cursor; break;
2635 case CROSS_CURSOR: return top_level->cross_cursor;
2636 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
2637 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
2638 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
2639 case MOVE_CURSOR: return top_level->move_cursor; break;
2640 case LEFT_CURSOR: return top_level->left_cursor; break;
2641 case RIGHT_CURSOR: return top_level->right_cursor; break;
2642 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
2643 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
2644 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
2645 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
2646 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
2647 case HOURGLASS_CURSOR: return top_level->hourglass_cursor; break;
2648 case TRANSPARENT_CURSOR: return top_level->transparent_cursor; break;
2653 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2655 // inherit cursor from parent
2658 XUndefineCursor(top_level->display, win);
2659 current_cursor = cursor;
2662 // don't change cursor if overridden
2663 if((!top_level->is_hourglass && !is_transparent) ||
2666 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2667 if(flush) this->flush();
2670 if(!override) current_cursor = cursor;
2673 void BC_WindowBase::set_x_cursor(int cursor)
2675 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2676 XDefineCursor(top_level->display, win, temp_cursor);
2677 current_cursor = cursor;
2681 int BC_WindowBase::get_cursor()
2683 return current_cursor;
2686 void BC_WindowBase::start_hourglass()
2688 top_level->start_hourglass_recursive();
2692 void BC_WindowBase::stop_hourglass()
2694 top_level->stop_hourglass_recursive();
2698 void BC_WindowBase::start_hourglass_recursive()
2700 if(this == top_level)
2708 set_cursor(HOURGLASS_CURSOR, 1, 0);
2709 for(int i = 0; i < subwindows->total; i++)
2711 subwindows->values[i]->start_hourglass_recursive();
2716 void BC_WindowBase::stop_hourglass_recursive()
2718 if(this == top_level)
2720 if(hourglass_total == 0) return;
2721 top_level->hourglass_total--;
2724 if(!top_level->hourglass_total)
2726 top_level->is_hourglass = 0;
2728 // Cause set_cursor to perform change
2730 set_cursor(current_cursor, 1, 0);
2732 for(int i = 0; i < subwindows->total; i++)
2734 subwindows->values[i]->stop_hourglass_recursive();
2742 XFontStruct* BC_WindowBase::get_font_struct(int font)
2744 // Clear out unrelated flags
2745 if(font & BOLDFACE) font ^= BOLDFACE;
2748 case SMALLFONT: return top_level->smallfont; break;
2749 case MEDIUMFONT: return top_level->mediumfont; break;
2750 case LARGEFONT: return top_level->largefont; break;
2751 case BIGFONT: return top_level->bigfont; break;
2756 XFontSet BC_WindowBase::get_fontset(int font)
2760 if(get_resources()->use_fontset)
2762 switch(font & 0xff) {
2763 case SMALLFONT: fs = top_level->smallfontset; break;
2764 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2765 case LARGEFONT: fs = top_level->largefontset; break;
2766 case BIGFONT: fs = top_level->bigfontset; break;
2774 XftFont* BC_WindowBase::get_xft_struct(int font)
2777 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2778 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2779 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2780 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2781 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2782 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2783 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2798 int BC_WindowBase::get_current_font()
2800 return top_level->current_font;
2803 void BC_WindowBase::set_font(int font)
2805 top_level->current_font = font;
2808 if(get_resources()->use_xft) {}
2811 if(get_resources()->use_fontset) {
2815 if(get_font_struct(font))
2817 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2823 void BC_WindowBase::set_fontset(int font)
2827 if(get_resources()->use_fontset) {
2829 case SMALLFONT: fs = top_level->smallfontset; break;
2830 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2831 case LARGEFONT: fs = top_level->largefontset; break;
2832 case BIGFONT: fs = top_level->bigfontset; break;
2840 XFontSet BC_WindowBase::get_curr_fontset(void)
2842 if(get_resources()->use_fontset)
2843 return curr_fontset;
2847 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2850 if(get_resources()->use_xft && get_xft_struct(font))
2853 #ifdef X_HAVE_UTF8_STRING
2854 if(get_resources()->locale_utf8)
2856 XftTextExtentsUtf8(top_level->display,
2857 get_xft_struct(font),
2858 (const XftChar8 *)text,
2865 XftTextExtents8(top_level->display,
2866 get_xft_struct(font),
2867 (const XftChar8 *)text,
2871 return extents.xOff;
2875 if(get_resources()->use_fontset && top_level->get_fontset(font))
2876 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2878 if(get_font_struct(font))
2879 return XTextWidth(get_font_struct(font), text, length);
2885 case MEDIUM_7SEGMENT:
2886 return get_resources()->medium_7segment[0]->get_w() * length;
2896 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2899 if(get_resources()->use_xft && get_xft_struct(font)) {
2902 XftTextExtents32(top_level->display, get_xft_struct(font),
2903 (const FcChar32*)text, length, &extents);
2904 return extents.xOff;
2907 if(!get_font_struct(font)) return 0;
2908 XChar2b xtext[length], *xp = xtext;
2909 for( int i=0; i<length; ++i,++xp ) {
2910 xp->byte1 = (unsigned char) (text[i] >> 8);
2911 xp->byte2 = (unsigned char) (text[i] & 0xff);
2913 return XTextWidth16(get_font_struct(font), xtext, length);
2916 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2918 int i, j, w = 0, line_w = 0;
2919 if(length < 0) length = strlen(text);
2921 for(i = 0, j = 0; i <= length; i++)
2926 line_w = get_single_text_width(font, &text[j], i - j);
2932 line_w = get_single_text_width(font, &text[j], length - j);
2934 if(line_w > w) w = line_w;
2937 if(i > length && w == 0)
2939 w = get_single_text_width(font, text, length);
2945 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2948 if( length < 0 ) length = wcslen(text);
2950 for( i=j=0; i<length && text[i]; ++i ) {
2951 if( text[i] != '\n' ) continue;
2953 int lw = get_single_text_width(font, &text[j], i-j);
2954 if( w < lw ) w = lw;
2959 int lw = get_single_text_width(font, &text[j], length-j);
2960 if( w < lw ) w = lw;
2966 int BC_WindowBase::get_text_ascent(int font)
2970 if( (fstruct = get_xft_struct(font)) != 0 )
2971 return fstruct->ascent;
2973 if(get_resources()->use_fontset && top_level->get_fontset(font))
2975 XFontSetExtents *extents;
2977 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2978 return -extents->max_logical_extent.y;
2981 if(get_font_struct(font))
2982 return top_level->get_font_struct(font)->ascent;
2985 case MEDIUM_7SEGMENT:
2986 return get_resources()->medium_7segment[0]->get_h();
2991 int BC_WindowBase::get_text_descent(int font)
2995 if( (fstruct = get_xft_struct(font)) != 0 )
2996 return fstruct->descent;
2998 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2999 XFontSetExtents *extents;
3000 extents = XExtentsOfFontSet(top_level->get_fontset(font));
3001 return (extents->max_logical_extent.height
3002 + extents->max_logical_extent.y);
3005 if(get_font_struct(font))
3006 return top_level->get_font_struct(font)->descent;
3011 int BC_WindowBase::get_text_height(int font, const char *text)
3016 if( (fstruct = get_xft_struct(font)) != 0 )
3017 rowh = fstruct->height;
3020 rowh = get_text_ascent(font) + get_text_descent(font);
3022 if(!text) return rowh;
3024 // Add height of lines
3025 int h = 0, i, length = strlen(text);
3026 for(i = 0; i <= length; i++)
3037 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
3039 if(color_model < 0) color_model = top_level->get_color_model();
3040 return new BC_Bitmap(top_level, w, h, color_model);
3043 void BC_WindowBase::init_wait()
3045 #ifndef SINGLE_THREAD
3046 if(window_type != MAIN_WINDOW)
3047 top_level->init_wait();
3048 init_lock->lock("BC_WindowBase::init_wait");
3049 init_lock->unlock();
3053 int BC_WindowBase::accel_available(int color_model, int lock_it)
3055 if( window_type != MAIN_WINDOW )
3056 return top_level->accel_available(color_model, lock_it);
3058 lock_window("BC_WindowBase::accel_available");
3060 switch(color_model) {
3062 grab_port_id(color_model);
3066 grab_port_id(color_model);
3075 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3076 return xvideo_port_id >= 0 ? 1 : 0;
3080 int BC_WindowBase::grab_port_id(int color_model)
3082 if( !get_resources()->use_xvideo || // disabled
3083 !get_resources()->use_shm ) // Only local server is fast enough.
3085 if( xvideo_port_id >= 0 )
3086 return xvideo_port_id;
3088 unsigned int ver, rev, reqBase, eventBase, errorBase;
3089 if( Success != XvQueryExtension(display, // XV extension is available
3090 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3093 // XV adaptors are available
3094 unsigned int numAdapt = 0;
3095 XvAdaptorInfo *info = 0;
3096 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3100 // Translate from color_model to X color model
3101 int x_color_model = BC_CModels::bc_to_x(color_model);
3103 // Get adaptor with desired color model
3104 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3105 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3106 // adaptor supports XvImages
3107 int numFormats = 0, numPorts = info[i].num_ports;
3108 XvImageFormatValues *formats =
3109 XvListImageFormats(display, info[i].base_id, &numFormats);
3110 if( !formats ) continue;
3112 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3113 if( formats[j].id != x_color_model ) continue;
3114 // this adaptor supports the desired format, grab a port
3115 for( int k=0; k<numPorts; ++k ) {
3116 if( Success == XvGrabPort(top_level->display,
3117 info[i].base_id+k, CurrentTime) ) {
3118 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3119 xvideo_port_id = info[i].base_id + k;
3127 XvFreeAdaptorInfo(info);
3129 return xvideo_port_id;
3133 int BC_WindowBase::show_window(int flush)
3135 for(int i = 0; i < subwindows->size(); i++)
3137 subwindows->get(i)->show_window(0);
3140 XMapWindow(top_level->display, win);
3141 if(flush) XFlush(top_level->display);
3142 // XSync(top_level->display, 0);
3147 int BC_WindowBase::hide_window(int flush)
3149 for(int i = 0; i < subwindows->size(); i++)
3151 subwindows->get(i)->hide_window(0);
3154 XUnmapWindow(top_level->display, win);
3155 if(flush) this->flush();
3160 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3162 subwindows->append((BC_SubWindow*)menu_bar);
3164 menu_bar->parent_window = this;
3165 menu_bar->top_level = this->top_level;
3166 menu_bar->initialize();
3170 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3172 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3173 if(this != top_level) return top_level->add_popup(window);
3174 popups.append(window);
3178 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3180 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3181 if(this != top_level)
3182 top_level->remove_popup(window);
3184 popups.remove(window);
3185 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3189 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3191 subwindows->append(subwindow);
3193 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3195 // parent window must be set before the subwindow initialization
3196 subwindow->parent_window = this;
3197 subwindow->top_level = this->top_level;
3199 // Execute derived initialization
3200 subwindow->initialize();
3205 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3207 return add_subwindow(subwindow);
3210 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3212 if( !top_level->flash_enabled ) return 0;
3213 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3215 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3218 XClearArea(top_level->display, win, x, y, w, h, 0);
3222 XClearWindow(top_level->display, win);
3230 int BC_WindowBase::flash(int flush)
3232 flash(-1, -1, -1, -1, flush);
3236 void BC_WindowBase::flush()
3238 //if(!get_window_lock())
3239 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3240 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3241 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3242 int locked = get_window_lock();
3243 if( !locked ) lock_window("BC_WindowBase::flush");
3244 XFlush(top_level->display);
3245 if( !locked ) unlock_window();
3248 void BC_WindowBase::sync_display()
3250 int locked = get_window_lock();
3251 if( !locked ) lock_window("BC_WindowBase::sync_display");
3252 XSync(top_level->display, False);
3253 if( !locked ) unlock_window();
3256 int BC_WindowBase::get_window_lock()
3258 #ifdef SINGLE_THREAD
3259 return BC_Display::display_global->get_display_locked();
3261 return top_level->window_lock;
3265 int BC_WindowBase::lock_window(const char *location)
3267 if(top_level && top_level != this)
3269 top_level->lock_window(location);
3274 SET_LOCK(this, title, location);
3275 #ifdef SINGLE_THREAD
3276 BC_Display::lock_display(location);
3278 XLockDisplay(top_level->display);
3279 top_level->display_lock_owner = pthread_self();
3282 ++top_level->window_lock;
3286 printf("BC_WindowBase::lock_window top_level NULL\n");
3291 int BC_WindowBase::unlock_window()
3293 if(top_level && top_level != this)
3295 top_level->unlock_window();
3302 // if(!top_level->window_lock)
3304 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3308 if( top_level->window_lock > 0 )
3309 if( --top_level->window_lock == 0 )
3310 top_level->display_lock_owner = 0;
3311 #ifdef SINGLE_THREAD
3312 BC_Display::unlock_display();
3314 XUnlockDisplay(top_level->display);
3319 printf("BC_WindowBase::unlock_window top_level NULL\n");
3324 void BC_WindowBase::set_done(int return_value)
3326 if(done_set) return;
3328 if(window_type != MAIN_WINDOW)
3329 top_level->set_done(return_value);
3332 #ifdef SINGLE_THREAD
3333 this->return_value = return_value;
3334 BC_Display::display_global->arm_completion(this);
3335 completion_lock->unlock();
3336 #else // SINGLE_THREAD
3338 if( !event_thread ) return;
3339 XEvent *event = new_xevent();
3340 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3341 event->type = ClientMessage;
3342 ptr->message_type = SetDoneXAtom;
3344 this->return_value = return_value;
3345 // May lock up here because XSendEvent doesn't work too well
3346 // asynchronous with XNextEvent.
3347 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3349 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3355 void BC_WindowBase::close(int return_value)
3357 hide_window(); flush();
3358 set_done(return_value);
3361 int BC_WindowBase::grab(BC_WindowBase *window)
3363 if( window->active_grab && this != window->active_grab ) return 0;
3364 window->active_grab = this;
3365 this->grab_active = window;
3368 int BC_WindowBase::ungrab(BC_WindowBase *window)
3370 if( window->active_grab && this != window->active_grab ) return 0;
3371 window->active_grab = 0;
3372 this->grab_active = 0;
3375 int BC_WindowBase::grab_buttons()
3377 XSync(top_level->display, False);
3378 if( XGrabButton(top_level->display, AnyButton, AnyModifier,
3379 top_level->rootwin, True, ButtonPressMask | ButtonReleaseMask,
3380 GrabModeAsync, GrabModeSync, None, grabbed_cursor) == GrabSuccess ) {
3381 set_active_subwindow(this);
3386 void BC_WindowBase::ungrab_buttons()
3388 XUngrabButton(top_level->display, AnyButton, AnyModifier, top_level->rootwin);
3389 set_active_subwindow(0);
3392 void BC_WindowBase::grab_cursor()
3394 XDefineCursor(top_level->display, top_level->rootwin, grabbed_cursor);
3396 void BC_WindowBase::ungrab_cursor()
3398 XUndefineCursor(top_level->display, top_level->rootwin);
3401 int BC_WindowBase::get_w()
3406 int BC_WindowBase::get_h()
3411 int BC_WindowBase::get_x()
3416 int BC_WindowBase::get_y()
3422 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3423 // this is the bounding box of all the screens
3425 int BC_WindowBase::get_root_w(int lock_display)
3427 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3428 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3429 int result = WidthOfScreen(def_screen);
3430 if(lock_display) unlock_window();
3434 int BC_WindowBase::get_root_h(int lock_display)
3436 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3437 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3438 int result = HeightOfScreen(def_screen);
3439 if(lock_display) unlock_window();
3443 XineramaScreenInfo *
3444 BC_WindowBase::get_xinerama_info(int screen)
3446 if( !xinerama_info || !xinerama_screens ) return 0;
3448 for( int i=0; i<xinerama_screens; ++i )
3449 if( xinerama_info[i].screen_number == screen )
3450 return &xinerama_info[i];
3453 int top_x = get_x(), top_y = get_y();
3454 for( int i=0; i<xinerama_screens; ++i ) {
3455 int scr_y = top_y - xinerama_info[i].y_org;
3456 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3457 int scr_x = top_x - xinerama_info[i].x_org;
3458 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3459 return &xinerama_info[i];
3464 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3467 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3468 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3471 int root_w = get_root_w(0);
3472 int root_h = get_root_h(0);
3473 // Shift X based on position of current window if dual head
3474 if( (float)root_w/root_h > 1.8 ) {
3475 root_w = get_screen_w(0, 0);
3476 if( top_level->get_x() >= root_w )
3481 result = info->x_org;
3482 if(lock_display) unlock_window();
3486 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3488 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3489 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3490 int result = !info ? 0 : info->y_org;
3491 if(lock_display) unlock_window();
3495 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3498 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3499 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3501 int width = get_root_w(0);
3502 int height = get_root_h(0);
3503 if( (float)width/height > 1.8 ) {
3504 // If dual head, the screen width is > 16x9
3505 // but we only want to fill one screen
3506 // this code assumes the "big" screen is on the right
3507 int scr_w0 = width / 2;
3509 case 600: scr_w0 = 800; break;
3510 case 720: scr_w0 = 1280; break;
3511 case 1024: scr_w0 = 1280; break;
3512 case 1200: scr_w0 = 1600; break;
3513 case 1080: scr_w0 = 1920; break;
3515 int scr_w1 = width - scr_w0;
3516 result = screen > 0 ? scr_w1 :
3517 screen == 0 ? scr_w0 :
3518 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3524 result = info->width;
3525 if(lock_display) unlock_window();
3529 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3531 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3532 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3533 int result = info ? info->height : get_root_h(0);
3534 if(lock_display) unlock_window();
3538 // Bottom right corner
3539 int BC_WindowBase::get_x2()
3544 int BC_WindowBase::get_y2()
3549 int BC_WindowBase::get_video_on()
3554 int BC_WindowBase::get_hidden()
3556 return top_level->hidden;
3559 int BC_WindowBase::cursor_inside()
3561 return (top_level->cursor_x >= 0 &&
3562 top_level->cursor_y >= 0 &&
3563 top_level->cursor_x < w &&
3564 top_level->cursor_y < h);
3567 BC_WindowBase* BC_WindowBase::get_top_level()
3572 BC_WindowBase* BC_WindowBase::get_parent()
3574 return parent_window;
3577 int BC_WindowBase::get_color_model()
3579 return top_level->color_model;
3582 BC_Resources* BC_WindowBase::get_resources()
3584 return &BC_WindowBase::resources;
3587 BC_Synchronous* BC_WindowBase::get_synchronous()
3589 return BC_WindowBase::resources.get_synchronous();
3592 int BC_WindowBase::get_bg_color()
3597 void BC_WindowBase::set_bg_color(int color)
3599 this->bg_color = color;
3602 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3607 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3609 top_level->active_subwindow = subwindow;
3612 int BC_WindowBase::activate()
3617 int BC_WindowBase::deactivate()
3619 if(window_type == MAIN_WINDOW)
3621 if( top_level->active_menubar ) {
3622 top_level->active_menubar->deactivate();
3623 top_level->active_menubar = 0;
3625 if( top_level->active_popup_menu ) {
3626 top_level->active_popup_menu->deactivate();
3627 top_level->active_popup_menu = 0;
3629 if( top_level->active_subwindow ) {
3630 top_level->active_subwindow->deactivate();
3631 top_level->active_subwindow = 0;
3633 if( top_level->motion_events && top_level->last_motion_win == this->win )
3634 top_level->motion_events = 0;
3640 int BC_WindowBase::cycle_textboxes(int amount)
3643 BC_WindowBase *new_textbox = 0;
3647 BC_WindowBase *first_textbox = 0;
3648 find_next_textbox(&first_textbox, &new_textbox, result);
3649 if(!new_textbox) new_textbox = first_textbox;
3655 BC_WindowBase *last_textbox = 0;
3656 find_prev_textbox(&last_textbox, &new_textbox, result);
3657 if(!new_textbox) new_textbox = last_textbox;
3661 if(new_textbox != active_subwindow)
3664 new_textbox->activate();
3670 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3672 // Search subwindows for textbox
3673 for(int i = 0; i < subwindows->total && result < 2; i++)
3675 BC_WindowBase *test_subwindow = subwindows->values[i];
3676 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3683 if(!*first_textbox) *first_textbox = this;
3687 if(top_level->active_subwindow == this)
3693 *next_textbox = this;
3700 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3706 if(!*last_textbox) *last_textbox = this;
3710 if(top_level->active_subwindow == this)
3716 *prev_textbox = this;
3721 // Search subwindows for textbox
3722 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3724 BC_WindowBase *test_subwindow = subwindows->values[i];
3725 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3730 BC_Clipboard* BC_WindowBase::get_clipboard()
3732 #ifdef SINGLE_THREAD
3733 return BC_Display::display_global->clipboard;
3735 return top_level->clipboard;
3739 void BC_WindowBase::get_relative_cursor_xy(int &x, int &y, int lock_window)
3741 int abs_x, abs_y, win_x, win_y;
3742 unsigned int temp_mask;
3745 if(lock_window) this->lock_window("BC_WindowBase::get_relative_cursor_xy");
3746 XQueryPointer(top_level->display, top_level->win,
3747 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3750 XTranslateCoordinates(top_level->display, top_level->rootwin,
3751 win, abs_x, abs_y, &x, &y, &temp_win);
3752 if(lock_window) this->unlock_window();
3754 int BC_WindowBase::get_relative_cursor_x(int lock_window)
3757 get_relative_cursor_xy(x, y, lock_window);
3760 int BC_WindowBase::get_relative_cursor_y(int lock_window)
3763 get_relative_cursor_xy(x, y, lock_window);
3767 void BC_WindowBase::get_abs_cursor_xy(int &abs_x, int &abs_y, int lock_window)
3770 unsigned int temp_mask;
3773 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_xy");
3774 XQueryPointer(top_level->display, top_level->win,
3775 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3777 if(lock_window) this->unlock_window();
3779 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3782 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3785 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3788 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3792 void BC_WindowBase::get_pop_cursor_xy(int &px, int &py, int lock_window)
3795 get_abs_cursor_xy(px, py, lock_window);
3796 if( px < margin ) px = margin;
3797 if( py < margin ) py = margin;
3798 int wd = get_screen_w(lock_window,-1) - margin;
3799 if( px > wd ) px = wd;
3800 int ht = get_screen_h(lock_window,-1) - margin;
3801 if( py > ht ) py = ht;
3803 int BC_WindowBase::get_pop_cursor_x(int lock_window)
3806 get_pop_cursor_xy(px, py, lock_window);
3809 int BC_WindowBase::get_pop_cursor_y(int lock_window)
3812 get_pop_cursor_xy(px, py, lock_window);
3816 int BC_WindowBase::match_window(Window win)
3818 if (this->win == win) return 1;
3820 for(int i = 0; i < subwindows->total; i++) {
3821 result = subwindows->values[i]->match_window(win);
3822 if (result) return result;
3828 int BC_WindowBase::get_cursor_over_window()
3830 if(top_level != this) return top_level->get_cursor_over_window();
3832 int abs_x, abs_y, win_x, win_y;
3833 unsigned int temp_mask;
3834 Window root_return, child_return;
3836 int ret = XQueryPointer(display, win,
3837 &root_return, &child_return, &abs_x, &abs_y,
3838 &win_x, &win_y, &temp_mask);
3839 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3840 // __LINE__, title, win, ret, child_return);
3842 int result = !ret || child_return == None ? 0 :
3843 match_window(child_return);
3847 int BC_WindowBase::cursor_above()
3850 get_relative_cursor_xy(rx, ry);
3851 return rx < 0 || rx >= get_w() ||
3852 ry < 0 || ry >= get_h() ? 0 : 1;
3855 int BC_WindowBase::get_drag_x()
3857 return top_level->drag_x;
3860 int BC_WindowBase::get_drag_y()
3862 return top_level->drag_y;
3865 int BC_WindowBase::get_cursor_x()
3867 return top_level->cursor_x;
3870 int BC_WindowBase::get_cursor_y()
3872 return top_level->cursor_y;
3875 int BC_WindowBase::dump_windows()
3877 printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
3878 this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
3879 for(int i = 0; i < subwindows->size(); i++)
3880 subwindows->get(i)->dump_windows();
3881 for(int i = 0; i < popups.size(); i++) {
3882 BC_WindowBase *p = popups[i];
3883 printf("\tBC_WindowBase::dump_windows popup=%p win=%p '%s', %dx%d+%d+%d %s\n",
3884 p, (void*)p->win, p->title, p->w,p->h,p->x,p->y, typeid(*p).name());
3889 int BC_WindowBase::is_event_win()
3891 return this->win == top_level->event_win;
3894 void BC_WindowBase::set_dragging(int value)
3896 is_dragging = value;
3899 int BC_WindowBase::get_dragging()
3904 int BC_WindowBase::get_buttonpress()
3906 return top_level->button_number;
3909 int BC_WindowBase::get_button_down()
3911 return top_level->button_down;
3914 int BC_WindowBase::alt_down()
3916 return top_level->alt_mask;
3919 int BC_WindowBase::shift_down()
3921 return top_level->shift_mask;
3924 int BC_WindowBase::ctrl_down()
3926 return top_level->ctrl_mask;
3929 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3932 *length = top_level->wkey_string_length;
3933 return top_level->wkey_string;
3936 #ifdef X_HAVE_UTF8_STRING
3937 char* BC_WindowBase::get_keypress_utf8()
3939 return top_level->key_pressed_utf8;
3944 int BC_WindowBase::get_keypress()
3946 return top_level->key_pressed;
3949 int BC_WindowBase::get_double_click()
3951 return top_level->double_click;
3954 int BC_WindowBase::get_triple_click()
3956 return top_level->triple_click;
3959 int BC_WindowBase::get_bgcolor()
3964 int BC_WindowBase::resize_window(int w, int h)
3966 if(this->w == w && this->h == h) return 0;
3968 if(window_type == MAIN_WINDOW && !allow_resize)
3970 XSizeHints size_hints;
3971 size_hints.flags = PSize | PMinSize | PMaxSize;
3972 size_hints.width = w;
3973 size_hints.height = h;
3974 size_hints.min_width = w;
3975 size_hints.max_width = w;
3976 size_hints.min_height = h;
3977 size_hints.max_height = h;
3978 XSetNormalHints(top_level->display, win, &size_hints);
3980 XResizeWindow(top_level->display, win, w, h);
3985 pixmap = new BC_Pixmap(this, w, h);
3987 // Propagate to menubar
3988 for(int i = 0; i < subwindows->total; i++)
3990 subwindows->values[i]->dispatch_resize_event(w, h);
3993 draw_background(0, 0, w, h);
3994 if(top_level == this && get_resources()->recursive_resizing)
3995 resize_history.append(new BC_ResizeCall(w, h));
3999 // The only way for resize events to be propagated is by updating the internal w and h
4000 int BC_WindowBase::resize_event(int w, int h)
4002 if(window_type == MAIN_WINDOW)
4010 int BC_WindowBase::reposition_window(int x, int y)
4012 reposition_window(x, y, -1, -1);
4017 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
4021 // Some tools set their own dimensions before calling this, causing the
4022 // resize check to skip.
4026 if(w > 0 && w != this->w)
4032 if(h > 0 && h != this->h)
4038 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
4041 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
4043 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
4045 if(translation_count && window_type == MAIN_WINDOW)
4047 // KDE shifts window right and down.
4048 // FVWM leaves window alone and adds border around it.
4049 XMoveResizeWindow(top_level->display,
4051 x - BC_DisplayInfo::auto_reposition_x,
4052 y - BC_DisplayInfo::auto_reposition_y,
4058 XMoveResizeWindow(top_level->display,
4069 pixmap = new BC_Pixmap(this, this->w, this->h);
4070 clear_box(0,0, this->w, this->h);
4071 // Propagate to menubar
4072 for(int i = 0; i < subwindows->total; i++)
4074 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
4077 // draw_background(0, 0, w, h);
4083 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
4085 return reposition_window(get_x()+dx, get_y()+dy, w, h);
4088 int BC_WindowBase::reposition_window_relative(int dx, int dy)
4090 return reposition_window_relative(dx, dy, -1, -1);
4093 void BC_WindowBase::set_tooltips(int v)
4095 get_resources()->tooltips_enabled = v;
4098 void BC_WindowBase::set_force_tooltip(int v)
4103 int BC_WindowBase::raise_window(int do_flush)
4105 XRaiseWindow(top_level->display, win);
4106 if(do_flush) XFlush(top_level->display);
4110 int BC_WindowBase::lower_window(int do_flush)
4112 XLowerWindow(top_level->display, win);
4113 if(do_flush) XFlush(top_level->display);
4117 void BC_WindowBase::set_background(VFrame *bitmap)
4119 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4121 bg_pixmap = new BC_Pixmap(this, bitmap, PIXMAP_OPAQUE);
4122 shared_bg_pixmap = 0;
4123 draw_background(0, 0, w, h);
4126 void BC_WindowBase::put_title(const char *text)
4128 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4129 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4130 *cp++ = *bp >= ' ' ? *bp : ' ';
4134 void BC_WindowBase::set_title(const char *text)
4139 txlist[0] = this->title;
4142 XTextProperty titleprop;
4143 XmbTextListToTextProperty(top_level->display, txlist, 1,
4144 XStdICCTextStyle, &titleprop);
4145 XSetWMName(top_level->display, top_level->win, &titleprop);
4146 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4147 XFree(titleprop.value);
4152 void BC_WindowBase::set_utf8title(const char *text)
4154 XTextProperty titleprop;
4157 strcpy(this->title, text);
4158 txlist[0] = this->title;
4161 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4162 XUTF8StringStyle, &titleprop);
4163 XSetWMName(top_level->display, top_level->win, &titleprop);
4164 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4165 XFree(titleprop.value);
4170 const char *BC_WindowBase::get_title()
4175 int BC_WindowBase::get_toggle_value()
4177 return toggle_value;
4180 int BC_WindowBase::get_toggle_drag()
4185 int BC_WindowBase::set_icon(VFrame *data)
4187 if(icon_pixmap) delete icon_pixmap;
4188 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4190 if(icon_window) delete icon_window;
4191 icon_window = new BC_Popup(this,
4194 icon_pixmap->get_w(),
4195 icon_pixmap->get_h(),
4197 1, // All windows are hidden initially
4201 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4202 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4203 wm_hints.icon_mask = icon_pixmap->get_alpha();
4204 wm_hints.icon_window = icon_window->win;
4205 wm_hints.window_group = XGroupLeader;
4207 // for(int i = 0; i < 1000; i++)
4208 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4211 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4212 XSync(top_level->display, 0);
4216 int BC_WindowBase::set_w(int w)
4222 int BC_WindowBase::set_h(int h)
4228 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4230 BC_Resources *resources = get_resources();
4231 char string[BCTEXTLEN];
4232 int newest_id = - 1;
4233 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4235 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4236 resources->filebox_history[i].path[0] = 0;
4237 defaults->get(string, resources->filebox_history[i].path);
4238 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4239 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4240 if(resources->filebox_history[i].id > newest_id)
4241 newest_id = resources->filebox_history[i].id;
4244 resources->filebox_id = newest_id + 1;
4245 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4246 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4247 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4248 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4249 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4250 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4251 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4252 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4253 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4254 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4255 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4256 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4260 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4262 BC_Resources *resources = get_resources();
4263 char string[BCTEXTLEN];
4264 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4266 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4267 defaults->update(string, resources->filebox_history[i].path);
4268 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4269 defaults->update(string, resources->filebox_history[i].id);
4271 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4272 defaults->update("FILEBOX_W", resources->filebox_w);
4273 defaults->update("FILEBOX_H", resources->filebox_h);
4274 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4275 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4276 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4277 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4278 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4279 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4280 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4281 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4282 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4288 // For some reason XTranslateCoordinates can take a long time to return.
4289 // We work around this by only calling it when the event windows are different.
4290 void BC_WindowBase::translate_coordinates(Window src_w,
4302 *dest_x_return = src_x;
4303 *dest_y_return = src_y;
4307 XTranslateCoordinates(top_level->display,
4315 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4319 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4321 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4324 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4326 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4334 #ifdef HAVE_LIBXXF86VM
4335 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4339 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4341 XF86VidModeModeInfo **vm_modelines;
4342 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4343 for (i = 0; i < vm_count; i++) {
4344 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4347 display = top_level->display;
4348 if (vm_modelines[*vm]->hdisplay == *width)
4352 *width = vm_modelines[*vm]->hdisplay;
4353 *height = vm_modelines[*vm]->vdisplay;
4358 void BC_WindowBase::scale_vm(int vm)
4360 int foo,bar,dotclock;
4361 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4364 XF86VidModeModeInfo **vm_modelines;
4365 XF86VidModeModeLine vml;
4366 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4367 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4368 orig_modeline.dotclock = dotclock;
4369 orig_modeline.hdisplay = vml.hdisplay;
4370 orig_modeline.hsyncstart = vml.hsyncstart;
4371 orig_modeline.hsyncend = vml.hsyncend;
4372 orig_modeline.htotal = vml.htotal;
4373 orig_modeline.vdisplay = vml.vdisplay;
4374 orig_modeline.vsyncstart = vml.vsyncstart;
4375 orig_modeline.vsyncend = vml.vsyncend;
4376 orig_modeline.vtotal = vml.vtotal;
4377 orig_modeline.flags = vml.flags;
4378 orig_modeline.privsize = vml.privsize;
4379 // orig_modeline.private = vml.private;
4380 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4381 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4382 XFlush(top_level->display);
4386 void BC_WindowBase::restore_vm()
4388 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4389 XFlush(top_level->display);
4412 #ifndef SINGLE_THREAD
4413 int BC_WindowBase::get_event_count()
4415 event_lock->lock("BC_WindowBase::get_event_count");
4416 int result = common_events.total;
4417 event_lock->unlock();
4421 XEvent* BC_WindowBase::get_event()
4424 while(!done && !result)
4426 event_condition->lock("BC_WindowBase::get_event");
4427 event_lock->lock("BC_WindowBase::get_event");
4429 if(common_events.total && !done)
4431 result = common_events.values[0];
4432 common_events.remove_number(0);
4435 event_lock->unlock();
4440 void BC_WindowBase::put_event(XEvent *event)
4442 event_lock->lock("BC_WindowBase::put_event");
4443 common_events.append(event);
4444 event_lock->unlock();
4445 event_condition->unlock();
4448 void BC_WindowBase::dequeue_events(Window win)
4450 event_lock->lock("BC_WindowBase::dequeue_events");
4452 int out = 0, total = common_events.size();
4453 for( int in=0; in<total; ++in ) {
4454 if( common_events[in]->xany.window == win ) continue;
4455 common_events[out++] = common_events[in];
4457 common_events.total = out;
4459 event_lock->unlock();
4462 #endif // SINGLE_THREAD
4464 int BC_WindowBase::get_id()
4470 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4472 int w = vframe->get_w(), h = vframe->get_h();
4473 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4474 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4479 void BC_WindowBase::flicker(int n, int ms)
4481 int color = get_bg_color();
4482 for( int i=2*n; --i>=0; ) {
4483 set_inverse(); set_bg_color(WHITE);
4484 clear_box(0,0, w,h); flash(1);
4485 sync_display(); Timer::delay(ms);
4487 set_bg_color(color);