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()
90 //printf("BC_WindowBase::BC_WindowBase 1\n");
91 BC_WindowBase::initialize();
94 BC_WindowBase::~BC_WindowBase()
97 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
99 if(window_type == MAIN_WINDOW)
100 lock_window("BC_WindowBase::~BC_WindowBase");
103 #ifdef HAVE_LIBXXF86VM
104 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
111 if(window_type != MAIN_WINDOW)
114 XSelectInput(top_level->display, this->win, 0);
115 XSync(top_level->display,0);
116 #ifndef SINGLE_THREAD
117 top_level->dequeue_events(win);
119 // drop active window refs to this
120 if(top_level->active_menubar == this) top_level->active_menubar = 0;
121 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
122 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
123 // drop motion window refs to this
124 if(top_level->motion_events && top_level->last_motion_win == this->win)
125 top_level->motion_events = 0;
127 // Remove pointer from parent window to this
128 parent_window->subwindows->remove(this);
131 if(grab_active) grab_active->active_grab = 0;
132 if(icon_window) delete icon_window;
133 if(window_type == POPUP_WINDOW)
134 parent_window->remove_popup(this);
136 // Delete the subwindows
139 while(subwindows->total)
141 // Subwindow removes its own pointer
142 delete subwindows->values[0];
149 //printf("delete glx=%08x, win=%08x %s\n", (unsigned)glx_win, (unsigned)win, title);
151 if( get_resources()->get_synchronous() && glx_win != 0 ) {
152 get_resources()->get_synchronous()->delete_window(this);
155 XDestroyWindow(top_level->display, win);
157 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
158 if(icon_pixmap) delete icon_pixmap;
159 if(temp_bitmap) delete temp_bitmap;
160 top_level->active_bitmaps.remove_buffers(this);
161 if(_7segment_pixmaps)
163 for(int i = 0; i < TOTAL_7SEGMENT; i++)
164 delete _7segment_pixmaps[i];
166 delete [] _7segment_pixmaps;
171 if(window_type == MAIN_WINDOW)
173 XFreeGC(display, gc);
174 static XFontStruct *BC_WindowBase::*xfont[] = {
175 &BC_WindowBase::smallfont,
176 &BC_WindowBase::mediumfont,
177 &BC_WindowBase::largefont,
178 &BC_WindowBase::bigfont,
179 &BC_WindowBase::clockfont,
181 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
182 XFreeFont(display, this->*xfont[i]);
184 // past bug in X caused XRenderExtensionInfo to be damaged
185 // if this is done here. left to be done in XCloseDisplay by Xlib.
186 // works in more modern systems, and needed for leak testing.
187 #if defined(HAVE_XFT) && defined(VALGRIND)
188 static void *BC_WindowBase::*xft_font[] = {
189 &BC_WindowBase::smallfont_xft,
190 &BC_WindowBase::mediumfont_xft,
191 &BC_WindowBase::largefont_xft,
192 &BC_WindowBase::bigfont_xft,
193 &BC_WindowBase::bold_smallfont_xft,
194 &BC_WindowBase::bold_mediumfont_xft,
195 &BC_WindowBase::bold_largefont_xft,
196 &BC_WindowBase::clockfont_xft,
198 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
199 XftFont *xft = (XftFont *)(this->*xft_font[i]);
200 if( xft ) XftFontClose (display, xft);
208 XFree(xinerama_info);
209 xinerama_screens = 0;
211 if( xvideo_port_id >= 0 )
212 XvUngrabPort(display, xvideo_port_id, CurrentTime);
215 // Must be last reference to display.
216 // _XftDisplayInfo needs a lock.
217 get_resources()->create_window_lock->lock("BC_WindowBase::~BC_WindowBase");
218 XCloseDisplay(display);
219 get_resources()->create_window_lock->unlock();
221 // clipboard uses a different display connection
222 clipboard->stop_clipboard();
226 resize_history.remove_all_objects();
228 #ifndef SINGLE_THREAD
229 common_events.remove_all_objects();
231 delete event_condition;
234 top_level->window_lock = 0;
235 BC_Display::unlock_display();
240 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
241 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
242 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
245 UNSET_ALL_LOCKS(this)
248 int BC_WindowBase::initialize()
253 display_lock_owner = 0;
269 xinerama_screens = 0;
274 translation_events = 0;
275 ctrl_mask = shift_mask = alt_mask = 0;
276 cursor_x = cursor_y = button_number = 0;
290 active_popup_menu = 0;
291 active_subwindow = 0;
295 _7segment_pixmaps = 0;
298 // next_repeat_id = 0;
300 current_font = MEDIUMFONT;
301 current_color = BLACK;
302 current_cursor = ARROW_CURSOR;
305 shared_bg_pixmap = 0;
308 window_type = MAIN_WINDOW;
309 translation_count = 0;
310 x_correction = y_correction = 0;
319 #ifdef HAVE_LIBXXF86VM
337 bold_smallfont_xft = 0;
338 bold_mediumfont_xft = 0;
339 bold_largefont_xft = 0;
341 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
343 // Need these right away since put_event is called before run_window sometimes.
344 event_lock = new Mutex("BC_WindowBase::event_lock");
345 event_condition = new Condition(0, "BC_WindowBase::event_condition");
346 init_lock = new Condition(0, "BC_WindowBase::init_lock");
349 cursor_timer = new Timer;
352 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
353 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
354 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
368 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
371 ButtonReleaseMask | \
372 PointerMotionMask | \
376 int BC_WindowBase::create_window(BC_WindowBase *parent_window, const char *title,
377 int x, int y, int w, int h, int minw, int minh, int allow_resize,
378 int private_color, int hide, int bg_color, const char *display_name,
379 int window_type, BC_Pixmap *bg_pixmap, int group_it)
381 XSetWindowAttributes attr;
383 XSizeHints size_hints;
386 #ifdef HAVE_LIBXXF86VM
390 id = get_resources()->get_id();
392 if(parent_window) top_level = parent_window->top_level;
393 if( top_level ) { // need this to avoid deadlock with Xlib's locks
395 lock_window("BC_WindowBase::create_window");
397 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
399 #ifdef HAVE_LIBXXF86VM
400 if(window_type == VIDMODE_SCALED_WINDOW)
401 closest_vm(&vm,&w,&h);
408 this->bg_color = bg_color;
409 this->window_type = window_type;
411 this->private_color = private_color;
412 this->parent_window = parent_window;
413 this->bg_pixmap = bg_pixmap;
414 this->allow_resize = allow_resize;
416 strcpy(this->display_name, display_name);
418 this->display_name[0] = 0;
421 if(bg_pixmap) shared_bg_pixmap = 1;
423 subwindows = new BC_SubWindowList;
425 if(window_type == MAIN_WINDOW)
428 parent_window = this;
432 display = BC_Display::get_display(display_name);
433 BC_Display::lock_display("BC_WindowBase::create_window");
434 // BC_Display::display_global->new_window(this);
437 // get the display connection
439 // This function must be the first Xlib
440 // function a multi-threaded program calls
442 display = init_display(display_name);
443 if( shm_completion_event < 0 ) shm_completion_event =
444 ShmCompletion + XShmGetEventBase(display);
447 screen = DefaultScreen(display);
448 rootwin = RootWindow(display, screen);
449 // window placement boundaries
450 if( !xinerama_screens && XineramaIsActive(display) )
451 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
452 root_w = get_root_w(0);
453 root_h = get_root_h(0);
456 vis = get_glx_visual(display);
459 vis = DefaultVisual(display, screen);
461 default_depth = DefaultDepth(display, screen);
463 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
464 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
467 // This must be done before fonts to know if antialiasing is available.
470 if(resources.use_shm < 0) resources.initialize_display(this);
471 x_correction = BC_DisplayInfo::get_left_border();
472 y_correction = BC_DisplayInfo::get_top_border();
474 // clamp window placement
475 if(this->x + this->w + x_correction > root_w)
476 this->x = root_w - this->w - x_correction;
477 if(this->y + this->h + y_correction > root_h)
478 this->y = root_h - this->h - y_correction;
479 if(this->x < 0) this->x = 0;
480 if(this->y < 0) this->y = 0;
482 if(this->bg_color == -1)
483 this->bg_color = resources.get_bg_color();
485 // printf("bcwindowbase 1 %s\n", title);
486 // if(window_type == MAIN_WINDOW) sleep(1);
487 // printf("bcwindowbase 10\n");
493 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
495 attr.event_mask = DEFAULT_EVENT_MASKS |
496 StructureNotifyMask |
500 attr.background_pixel = get_color(this->bg_color);
501 attr.colormap = cmap;
502 attr.cursor = get_cursor_struct(ARROW_CURSOR);
504 win = XCreateWindow(display, rootwin,
505 this->x, this->y, this->w, this->h, 0,
506 top_level->default_depth, InputOutput,
508 XGetNormalHints(display, win, &size_hints);
510 size_hints.flags = PSize | PMinSize | PMaxSize;
511 size_hints.width = this->w;
512 size_hints.height = this->h;
513 size_hints.min_width = allow_resize ? minw : this->w;
514 size_hints.max_width = allow_resize ? 32767 : this->w;
515 size_hints.min_height = allow_resize ? minh : this->h;
516 size_hints.max_height = allow_resize ? 32767 : this->h;
517 if(x > -BC_INFINITY && x < BC_INFINITY)
519 size_hints.flags |= PPosition;
520 size_hints.x = this->x;
521 size_hints.y = this->y;
523 XSetWMProperties(display, win, 0, 0, 0, 0, &size_hints, 0, 0);
526 #ifndef SINGLE_THREAD
527 clipboard = new BC_Clipboard(this);
528 clipboard->start_clipboard();
534 Atom ClientLeaderXAtom;
535 if (XGroupLeader == 0)
537 const char *instance_name = "cinelerra";
538 const char *class_name = "Cinelerra";
539 XClassHint *class_hints = XAllocClassHint();
540 class_hints->res_name = (char*)instance_name;
541 class_hints->res_class = (char*)class_name;
542 XSetClassHint(top_level->display, win, class_hints);
544 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
545 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
546 PropModeReplace, (unsigned char *)&XGroupLeader, true);
551 #ifdef HAVE_LIBXXF86VM
552 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
559 #ifdef HAVE_LIBXXF86VM
560 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
562 if(window_type == POPUP_WINDOW)
565 mask = CWEventMask | CWBackPixel | CWColormap |
566 CWOverrideRedirect | CWSaveUnder | CWCursor;
568 attr.event_mask = DEFAULT_EVENT_MASKS |
572 if(this->bg_color == -1)
573 this->bg_color = resources.get_bg_color();
574 attr.background_pixel = top_level->get_color(bg_color);
575 attr.colormap = top_level->cmap;
576 if(top_level->is_hourglass)
577 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
579 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
580 attr.override_redirect = True;
581 attr.save_under = True;
583 win = XCreateWindow(top_level->display,
584 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
585 top_level->default_depth, InputOutput, top_level->vis, mask,
587 top_level->add_popup(this);
590 if(window_type == SUB_WINDOW)
592 mask = CWEventMask | CWBackPixel | CWCursor;
593 attr.event_mask = DEFAULT_EVENT_MASKS;
594 attr.background_pixel = top_level->get_color(this->bg_color);
595 if(top_level->is_hourglass)
596 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
598 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
599 win = XCreateWindow(top_level->display,
600 parent_window->win, this->x, this->y, this->w, this->h, 0,
601 top_level->default_depth, InputOutput, top_level->vis, mask,
604 if(!hidden) XMapWindow(top_level->display, win);
607 // Create pixmap for all windows
608 pixmap = new BC_Pixmap(this, this->w, this->h);
610 // Set up options for main window
611 if(window_type == MAIN_WINDOW)
613 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
615 this->bg_pixmap = new BC_Pixmap(this,
616 get_resources()->bg_image,
620 if(!hidden) show_window();
624 draw_background(0, 0, this->w, this->h);
626 flash(-1, -1, -1, -1, 0);
628 // Set up options for popup window
629 #ifdef HAVE_LIBXXF86VM
630 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
632 if(window_type == POPUP_WINDOW)
636 if(!hidden) show_window();
638 get_resources()->create_window_lock->unlock();
639 if(need_lock) unlock_window();
644 Display* BC_WindowBase::init_display(const char *display_name)
648 if(display_name && display_name[0] == 0) display_name = NULL;
649 if((display = XOpenDisplay(display_name)) == NULL) {
650 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
652 if(getenv("DISPLAY") == NULL) {
653 printf(_("'DISPLAY' environment variable not set.\n"));
657 // Try again with default display.
658 if((display = XOpenDisplay(0)) == NULL) {
659 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
665 static int xsynch = -1;
667 const char *cp = getenv("CIN_XSYNCH");
668 xsynch = !cp ? 0 : atoi(cp);
671 XSynchronize(display, True);
676 Display* BC_WindowBase::get_display()
678 return top_level->display;
681 int BC_WindowBase::get_screen()
683 return top_level->screen;
686 int BC_WindowBase::run_window()
692 // Events may have been sent before run_window so can't initialize them here.
695 set_repeat(get_resources()->tooltip_delay);
696 BC_Display::display_global->new_window(this);
698 // If the first window created, run the display loop in this thread.
699 if(BC_Display::display_global->is_first(this))
701 BC_Display::unlock_display();
702 BC_Display::display_global->loop();
706 BC_Display::unlock_display();
707 completion_lock->lock("BC_WindowBase::run_window");
710 BC_Display::lock_display("BC_WindowBase::run_window");
711 BC_Display::display_global->delete_window(this);
713 unset_all_repeaters();
715 BC_Display::unlock_display();
717 #else // SINGLE_THREAD
722 set_repeat(get_resources()->tooltip_delay);
724 // Start X server events
725 event_thread = new BC_WindowEvents(this);
726 event_thread->start();
732 // Handle common events
738 unset_all_repeaters();
742 event_condition->reset();
743 common_events.remove_all_objects();
747 #endif // SINGLE_THREAD
752 int BC_WindowBase::get_key_masks(unsigned int key_state)
754 // printf("BC_WindowBase::get_key_masks %llx\n",
755 // event->xkey.state);
756 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
757 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
758 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
763 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
765 BC_KeyboardHandlerLock set;
766 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
769 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
771 BC_KeyboardHandlerLock set;
772 int i = BC_KeyboardHandler::listeners.size();
773 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
774 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
777 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
779 int result = (win->*handler)(wp);
783 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
786 BC_KeyboardHandlerLock set;
787 for( int i=0; !result && i<listeners.size(); ++i ) {
788 BC_KeyboardHandler *listener = listeners[i];
789 result = listener->run_event(wp);
794 void BC_KeyboardHandler::kill_grabs()
796 BC_KeyboardHandlerLock set;
797 for( int i=0; i<listeners.size(); ++i ) {
798 BC_WindowBase *win = listeners[i]->win;
799 if( win->get_window_type() != POPUP_WINDOW ) continue;
800 ((BC_Popup *)win)->ungrab_keyboard();
804 void BC_ActiveBitmaps::reque(XEvent *event)
806 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
807 ShmSeg shmseg = shm_ev->shmseg;
808 Drawable drawable = shm_ev->drawable;
809 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
810 active_lock.lock("BC_BitmapImage::reque");
811 BC_BitmapImage *bfr = first;
812 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
813 if( bfr && bfr->drawable == drawable )
815 active_lock.unlock();
817 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
818 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
819 // (int)shmseg, (int)drawable);
822 if( bfr->drawable != drawable ) return;
823 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
824 bfr->bitmap->reque(bfr);
827 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
829 active_lock.lock("BC_BitmapImage::insert");
830 bfr->drawable = pixmap;
832 active_lock.unlock();
835 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
837 active_lock.lock("BC_ActiveBitmaps::remove");
838 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
840 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
841 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
843 active_lock.unlock();
846 BC_ActiveBitmaps::BC_ActiveBitmaps()
850 BC_ActiveBitmaps::~BC_ActiveBitmaps()
856 int BC_WindowBase::keysym_lookup(XEvent *event)
858 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
859 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
861 if( event->xany.send_event && !event->xany.serial ) {
862 keysym = (KeySym) event->xkey.keycode;
863 keys_return[0] = keysym;
866 wkey_string_length = 0;
868 if( input_context ) {
869 wkey_string_length = XwcLookupString(input_context,
870 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
871 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
872 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
875 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
876 keys_return, KEYPRESSLEN, &keysym, &stat);
877 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
878 if( stat == XLookupBoth ) return ret;
879 if( stat == XLookupKeySym ) return 0;
881 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
882 wkey_string_length = ret;
883 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
887 pthread_t locking_task = (pthread_t)-1L;
888 int locking_event = -1;
889 int locking_message = -1;
891 int BC_WindowBase::dispatch_event(XEvent *event)
895 XClientMessageEvent *ptr;
896 int cancel_resize, cancel_translation;
901 #ifndef SINGLE_THREAD
902 // If an event is waiting get it, otherwise
903 // wait for next event only if there are no compressed events.
904 if(get_event_count() ||
905 (!motion_events && !resize_events && !translation_events))
908 // Lock out window deletions
909 lock_window("BC_WindowBase::dispatch_event 1");
910 locking_event = event->type;
911 locking_task = pthread_self();
912 locking_message = event->xclient.message_type;
915 // Handle compressed events
917 lock_window("BC_WindowBase::dispatch_event 2");
919 dispatch_resize_event(last_resize_w, last_resize_h);
921 dispatch_motion_event();
922 if(translation_events)
923 dispatch_translation_event();
934 //static const char *event_names[] = {
935 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
936 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
937 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
938 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
939 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
940 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
941 // "GenericEvent", "LASTEvent",
945 //if(event->type != ClientMessage) {
946 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
947 //__LINE__, title, event, event->type,
948 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
949 // event_names[event->type] : "Unknown");
954 active_grab->lock_window("BC_WindowBase::dispatch_event 3");
955 result = active_grab->grab_event(event);
956 active_grab->unlock_window();
957 if( result ) return result;
958 lock_window("BC_WindowBase::dispatch_event 4");
961 switch(event->type) {
963 // Clear the resize buffer
964 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
965 // Clear the motion buffer since this can clear the window
968 dispatch_motion_event();
971 ptr = (XClientMessageEvent*)event;
974 if(ptr->message_type == ProtoXAtom &&
975 (Atom)ptr->data.l[0] == DelWinXAtom)
980 if(ptr->message_type == RepeaterXAtom)
982 dispatch_repeat_event(ptr->data.l[0]);
983 // Make sure the repeater still exists.
984 // for(int i = 0; i < repeaters.total; i++)
986 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
988 // dispatch_repeat_event_master(ptr->data.l[0]);
994 if(ptr->message_type == SetDoneXAtom)
998 { // We currently use X marshalling for xatom events, we can switch to something else later
999 recieve_custom_xatoms((xatom_event *)ptr);
1005 dispatch_focus_in();
1010 dispatch_focus_out();
1024 dispatch_motion_event();
1026 get_key_masks(event->xbutton.state);
1027 cursor_x = event->xbutton.x;
1028 cursor_y = event->xbutton.y;
1029 button_number = event->xbutton.button;
1031 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1032 event_win = event->xany.window;
1033 if (button_number < 6)
1035 if(button_number < 4)
1037 button_pressed = event->xbutton.button;
1038 button_time1 = button_time2;
1039 button_time2 = button_time3;
1040 button_time3 = event->xbutton.time;
1043 drag_win = event_win;
1044 drag_x1 = cursor_x - get_resources()->drag_radius;
1045 drag_x2 = cursor_x + get_resources()->drag_radius;
1046 drag_y1 = cursor_y - get_resources()->drag_radius;
1047 drag_y2 = cursor_y + get_resources()->drag_radius;
1049 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1052 button_time3 = button_time2 = button_time1 = 0;
1054 if((long)(button_time3 - button_time2) < resources.double_click)
1057 // button_time3 = button_time2 = button_time1 = 0;
1065 dispatch_button_press();
1072 dispatch_motion_event();
1074 get_key_masks(event->xbutton.state);
1075 button_number = event->xbutton.button;
1076 event_win = event->xany.window;
1077 if (button_number < 6)
1079 if(button_number < 4)
1081 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1083 dispatch_button_release();
1088 event_win = event->xany.window;
1089 dispatch_expose_event();
1093 get_key_masks(event->xmotion.state);
1094 // Dispatch previous motion event if this is a subsequent motion from a different window
1095 if(motion_events && last_motion_win != event->xany.window)
1097 dispatch_motion_event();
1100 // Buffer the current motion
1102 last_motion_state = event->xmotion.state;
1103 last_motion_x = event->xmotion.x;
1104 last_motion_y = event->xmotion.y;
1105 last_motion_win = event->xany.window;
1108 case ConfigureNotify:
1109 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1111 // event->xany.window,
1114 XTranslateCoordinates(top_level->display,
1122 last_resize_w = event->xconfigure.width;
1123 last_resize_h = event->xconfigure.height;
1126 cancel_translation = 0;
1128 // Resize history prevents responses to recursive resize requests
1129 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1131 if(resize_history.values[i]->w == last_resize_w &&
1132 resize_history.values[i]->h == last_resize_h)
1134 delete resize_history.values[i];
1135 resize_history.remove_number(i);
1140 if(last_resize_w == w && last_resize_h == h)
1148 if((last_translate_x == x && last_translate_y == y))
1149 cancel_translation = 1;
1151 if(!cancel_translation)
1153 translation_events = 1;
1156 translation_count++;
1160 get_key_masks(event->xkey.state);
1161 keys_return[0] = 0; keysym = -1;
1162 if(XFilterEvent(event, win)) {
1165 if( keysym_lookup(event) < 0 ) {
1166 printf("keysym %x\n", (uint32_t)keysym);
1170 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1174 // block out control keys
1175 if(keysym > 0xffe0 && keysym < 0xffff) break;
1176 // block out Alt_GR key
1177 if(keysym == 0xfe03) break;
1180 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1182 #ifdef X_HAVE_UTF8_STRING
1183 //It's Ascii or UTF8?
1184 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1185 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1187 if( ((keys_return[1] & 0xff) > 0x80) &&
1188 ((keys_return[0] & 0xff) > 0xC0) ) {
1189 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1190 key_pressed = keysym & 0xff;
1196 // block out extra keys
1206 // Translate key codes
1207 case XK_Return: key_pressed = RETURN; break;
1208 case XK_Up: key_pressed = UP; break;
1209 case XK_Down: key_pressed = DOWN; break;
1210 case XK_Left: key_pressed = LEFT; break;
1211 case XK_Right: key_pressed = RIGHT; break;
1212 case XK_Next: key_pressed = PGDN; break;
1213 case XK_Prior: key_pressed = PGUP; break;
1214 case XK_BackSpace: key_pressed = BACKSPACE; break;
1215 case XK_Escape: key_pressed = ESC; break;
1218 key_pressed = LEFTTAB;
1222 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1223 case XK_underscore: key_pressed = '_'; break;
1224 case XK_asciitilde: key_pressed = '~'; break;
1225 case XK_Delete: key_pressed = DELETE; break;
1226 case XK_Home: key_pressed = HOME; break;
1227 case XK_End: key_pressed = END; break;
1230 case XK_KP_Enter: key_pressed = KPENTER; break;
1231 case XK_KP_Add: key_pressed = KPPLUS; break;
1232 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1233 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1234 case XK_KP_Divide: key_pressed = KPSLASH; break;
1236 case XK_KP_End: key_pressed = KP1; break;
1238 case XK_KP_Down: key_pressed = KP2; break;
1240 case XK_KP_Page_Down: key_pressed = KP3; break;
1242 case XK_KP_Left: key_pressed = KP4; break;
1244 case XK_KP_Begin: key_pressed = KP5; break;
1246 case XK_KP_Right: key_pressed = KP6; break;
1248 case XK_KP_Home: key_pressed = KP7; break;
1250 case XK_KP_Up: key_pressed = KP8; break;
1252 case XK_KP_Page_Up: key_pressed = KP9; break;
1254 case XK_KP_Insert: key_pressed = KPINS; break;
1256 case XK_KP_Delete: key_pressed = KPDEL; break;
1257 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1259 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1260 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1261 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1262 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1263 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1264 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1265 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1266 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1267 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1268 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1269 // above case XK_Left: key_pressed = LEFT; break; /* left */
1270 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1271 // above case XK_Down: key_pressed = DOWN; break; /* down */
1272 // above case XK_Up: key_pressed = UP; break; /* up */
1273 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1274 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1275 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1276 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1277 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1278 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1279 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1280 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1281 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1282 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1283 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1284 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1287 key_pressed = keysym & 0xff;
1288 #ifdef X_HAVE_UTF8_STRING
1289 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1294 #ifdef X_HAVE_UTF8_STRING
1296 key_pressed_utf8 = keys_return;
1301 if( top_level == this )
1302 result = BC_KeyboardHandler::run_listeners(this);
1304 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1306 result = dispatch_keypress_event();
1307 // Handle some default keypresses
1310 if(key_pressed == 'w' ||
1319 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1320 dispatch_keyrelease_event();
1321 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1322 // keysym, event->xkey.state);
1326 if( cursor_entered && event->xcrossing.window == win ) {
1329 event_win = event->xany.window;
1330 dispatch_cursor_leave();
1334 if( !cursor_entered && event->xcrossing.window == win ) {
1335 if( !event->xcrossing.focus && get_resources()->grab_input_focus ) {
1336 XSetInputFocus(display, win, RevertToParent, CurrentTime);
1340 event_win = event->xany.window;
1341 cursor_x = event->xcrossing.x;
1342 cursor_y = event->xcrossing.y;
1343 dispatch_cursor_enter();
1349 //printf("100 %s %p %d\n", title, event, event->type);
1350 //if(event->type != ClientMessage) dump();
1352 #ifndef SINGLE_THREAD
1354 if(event) delete event;
1356 // if(done) completion_lock->unlock();
1359 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1363 int BC_WindowBase::dispatch_expose_event()
1366 for(int i = 0; i < subwindows->total && !result; i++)
1368 result = subwindows->values[i]->dispatch_expose_event();
1371 // Propagate to user
1372 if(!result) expose_event();
1376 int BC_WindowBase::dispatch_resize_event(int w, int h)
1378 // Can't store new w and h until the event is handles
1379 // because bcfilebox depends on the old w and h to
1380 // reposition widgets.
1381 if( window_type == MAIN_WINDOW ) {
1386 pixmap = new BC_Pixmap(this, w, h);
1387 clear_box(0, 0, w, h);
1390 // Propagate to subwindows
1391 for(int i = 0; i < subwindows->total; i++) {
1392 subwindows->values[i]->dispatch_resize_event(w, h);
1395 // Propagate to user
1398 if( window_type == MAIN_WINDOW ) {
1407 int BC_WindowBase::dispatch_flash()
1410 for(int i = 0; i < subwindows->total; i++)
1411 subwindows->values[i]->dispatch_flash();
1415 int BC_WindowBase::dispatch_translation_event()
1417 translation_events = 0;
1418 if(window_type == MAIN_WINDOW)
1422 x = last_translate_x;
1423 y = last_translate_y;
1424 // Correct for window manager offsets
1429 for(int i = 0; i < subwindows->total; i++)
1431 subwindows->values[i]->dispatch_translation_event();
1434 translation_event();
1438 int BC_WindowBase::dispatch_motion_event()
1443 if(top_level == this)
1446 event_win = last_motion_win;
1447 get_key_masks(last_motion_state);
1450 if(get_button_down() && !active_menubar && !active_popup_menu)
1454 cursor_x = last_motion_x;
1455 cursor_y = last_motion_y;
1456 result = dispatch_drag_motion();
1460 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1461 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1466 result = dispatch_drag_start();
1470 cursor_x = last_motion_x;
1471 cursor_y = last_motion_y;
1473 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1476 // active_popup_menu,
1477 // active_subwindow);
1479 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1480 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1481 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1484 // Dispatch in stacking order
1485 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1487 result = subwindows->values[i]->dispatch_motion_event();
1490 if(!result) result = cursor_motion_event(); // give to user
1494 int BC_WindowBase::dispatch_keypress_event()
1497 if(top_level == this)
1499 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1502 for(int i = 0; i < subwindows->total && !result; i++)
1504 result = subwindows->values[i]->dispatch_keypress_event();
1507 if(!result) result = keypress_event();
1512 int BC_WindowBase::dispatch_keyrelease_event()
1515 if(top_level == this)
1517 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1520 for(int i = 0; i < subwindows->total && !result; i++)
1522 result = subwindows->values[i]->dispatch_keyrelease_event();
1525 if(!result) result = keyrelease_event();
1530 int BC_WindowBase::dispatch_focus_in()
1532 for(int i = 0; i < subwindows->total; i++)
1534 subwindows->values[i]->dispatch_focus_in();
1542 int BC_WindowBase::dispatch_focus_out()
1544 for(int i = 0; i < subwindows->total; i++)
1546 subwindows->values[i]->dispatch_focus_out();
1554 int BC_WindowBase::get_has_focus()
1556 return top_level->has_focus;
1559 int BC_WindowBase::get_deleting()
1561 if(is_deleting) return 1;
1562 if(parent_window && parent_window->get_deleting()) return 1;
1566 int BC_WindowBase::dispatch_button_press()
1571 if(top_level == this)
1573 if(active_menubar) result = active_menubar->dispatch_button_press();
1574 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1575 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1578 for(int i = 0; i < subwindows->total && !result; i++)
1580 result = subwindows->values[i]->dispatch_button_press();
1583 if(!result) result = button_press_event();
1589 int BC_WindowBase::dispatch_button_release()
1592 if(top_level == this)
1594 if(active_menubar) result = active_menubar->dispatch_button_release();
1595 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1596 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1597 if(!result && button_number != 4 && button_number != 5)
1598 result = dispatch_drag_stop();
1601 for(int i = 0; i < subwindows->total && !result; i++)
1603 result = subwindows->values[i]->dispatch_button_release();
1608 result = button_release_event();
1615 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1618 // all repeat event handlers get called and decide based on activity and duration
1619 // whether to respond
1620 for(int i = 0; i < subwindows->total; i++)
1622 subwindows->values[i]->dispatch_repeat_event(duration);
1626 repeat_event(duration);
1630 // Unlock next repeat signal
1631 if(window_type == MAIN_WINDOW)
1633 #ifdef SINGLE_THREAD
1634 BC_Display::display_global->unlock_repeaters(duration);
1636 for(int i = 0; i < repeaters.total; i++)
1638 if(repeaters.values[i]->delay == duration)
1640 repeaters.values[i]->repeat_lock->unlock();
1648 void BC_WindowBase::unhide_cursor()
1653 if(top_level->is_hourglass)
1654 set_cursor(HOURGLASS_CURSOR, 1, 0);
1656 set_cursor(current_cursor, 1, 0);
1658 cursor_timer->update();
1662 void BC_WindowBase::update_video_cursor()
1664 if(video_on && !is_transparent)
1666 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1669 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1670 cursor_timer->update();
1675 cursor_timer->update();
1680 int BC_WindowBase::dispatch_cursor_leave()
1684 for(int i = 0; i < subwindows->total; i++)
1686 subwindows->values[i]->dispatch_cursor_leave();
1689 cursor_leave_event();
1693 int BC_WindowBase::dispatch_cursor_enter()
1699 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1700 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1701 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1703 for(int i = 0; !result && i < subwindows->total; i++)
1705 result = subwindows->values[i]->dispatch_cursor_enter();
1708 if(!result) result = cursor_enter_event();
1712 int BC_WindowBase::cursor_enter_event()
1717 int BC_WindowBase::cursor_leave_event()
1722 int BC_WindowBase::close_event()
1728 int BC_WindowBase::dispatch_drag_start()
1731 if(active_menubar) result = active_menubar->dispatch_drag_start();
1732 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1733 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1735 for(int i = 0; i < subwindows->total && !result; i++)
1737 result = subwindows->values[i]->dispatch_drag_start();
1740 if(!result) result = is_dragging = drag_start_event();
1744 int BC_WindowBase::dispatch_drag_stop()
1748 for(int i = 0; i < subwindows->total && !result; i++)
1750 result = subwindows->values[i]->dispatch_drag_stop();
1753 if(is_dragging && !result)
1763 int BC_WindowBase::dispatch_drag_motion()
1766 for(int i = 0; i < subwindows->total && !result; i++)
1768 result = subwindows->values[i]->dispatch_drag_motion();
1771 if(is_dragging && !result)
1773 drag_motion_event();
1781 int BC_WindowBase::show_tooltip(const char *text, int x, int y, int w, int h)
1784 int forced = !text ? force_tooltip : 1;
1785 if( !text ) text = tooltip_text;
1786 if( !text || (!forced && !get_resources()->tooltips_enabled) ) {
1787 top_level->hide_tooltip();
1791 if(w < 0) w = get_text_width(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1792 if(h < 0) h = get_text_height(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1793 // default x,y (win relative)
1794 if( x < 0 ) x = get_w();
1795 if( y < 0 ) y = get_h();
1797 get_root_coordinates(x, y, &wx, &wy);
1798 // keep the tip inside the window/display
1799 int x0 = top_level->get_x(), x1 = x0 + top_level->get_w();
1800 int x2 = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1801 if( x1 > x2 ) x1 = x2;
1802 if( wx < x0 ) wx = x0;
1803 if( wx >= (x1-=w) ) wx = x1;
1804 int y0 = top_level->get_y(), y1 = y0 + top_level->get_h();
1805 int y2 = top_level->get_root_h(0);
1806 if( y1 > y2 ) y1 = y2;
1807 if( wy < y0 ) wy = y0;
1808 if( wy >= (y1-=h) ) wy = y1;
1809 // avoid tip under cursor (flickers)
1811 get_abs_cursor(abs_x,abs_y, 0);
1812 if( wx < abs_x && abs_x < wx+w && wy < abs_y && abs_y < wy+h ) {
1813 if( wx-abs_x < wy-abs_y )
1820 tooltip_popup = new BC_Popup(top_level, wx, wy, w, h,
1821 get_resources()->tooltip_bg_color);
1824 tooltip_popup->reposition_window(wx, wy, w, h);
1827 tooltip_popup->flash();
1828 tooltip_popup->flush();
1832 int BC_WindowBase::hide_tooltip()
1835 for(int i = 0; i < subwindows->total; i++)
1837 subwindows->values[i]->hide_tooltip();
1843 delete tooltip_popup;
1849 const char *BC_WindowBase::get_tooltip()
1851 return tooltip_text;
1854 int BC_WindowBase::set_tooltip(const char *text)
1856 tooltip_text = text;
1858 // Update existing tooltip if it is visible
1862 tooltip_popup->flash();
1866 // signal the event handler to repeat
1867 int BC_WindowBase::set_repeat(int64_t duration)
1871 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1874 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1876 #ifdef SINGLE_THREAD
1877 BC_Display::display_global->set_repeat(this, duration);
1879 // test repeater database for duplicates
1880 for(int i = 0; i < repeaters.total; i++)
1883 if(repeaters.values[i]->delay == duration)
1885 repeaters.values[i]->start_repeating(this);
1890 BC_Repeater *repeater = new BC_Repeater(this, duration);
1891 repeater->initialize();
1892 repeaters.append(repeater);
1893 repeater->start_repeating();
1898 int BC_WindowBase::unset_repeat(int64_t duration)
1900 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1902 #ifdef SINGLE_THREAD
1903 BC_Display::display_global->unset_repeat(this, duration);
1905 for(int i = 0; i < repeaters.total; i++)
1907 if(repeaters.values[i]->delay == duration)
1909 repeaters.values[i]->stop_repeating();
1917 int BC_WindowBase::unset_all_repeaters()
1919 #ifdef SINGLE_THREAD
1920 BC_Display::display_global->unset_all_repeaters(this);
1922 for(int i = 0; i < repeaters.total; i++)
1924 repeaters.values[i]->stop_repeating();
1926 repeaters.remove_all_objects();
1931 // long BC_WindowBase::get_repeat_id()
1933 // return top_level->next_repeat_id++;
1936 XEvent *BC_WindowBase::new_xevent()
1938 XEvent *event = new XEvent;
1939 memset(event, 0, sizeof(*event));
1943 #ifndef SINGLE_THREAD
1944 int BC_WindowBase::arm_repeat(int64_t duration)
1946 XEvent *event = new_xevent();
1947 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1948 ptr->type = ClientMessage;
1949 ptr->message_type = RepeaterXAtom;
1951 ptr->data.l[0] = duration;
1953 // Couldn't use XSendEvent since it locked up randomly.
1959 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1964 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1966 #ifndef SINGLE_THREAD
1967 XEvent *myevent = new_xevent();
1968 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1969 ptr->type = ClientMessage;
1970 ptr->message_type = event->message_type;
1971 ptr->format = event->format;
1972 ptr->data.l[0] = event->data.l[0];
1973 ptr->data.l[1] = event->data.l[1];
1974 ptr->data.l[2] = event->data.l[2];
1975 ptr->data.l[3] = event->data.l[3];
1976 ptr->data.l[4] = event->data.l[4];
1985 Atom BC_WindowBase::create_xatom(const char *atom_name)
1987 return XInternAtom(display, atom_name, False);
1990 int BC_WindowBase::get_atoms()
1992 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1993 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1994 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
1995 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1996 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
1997 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
1998 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2004 void BC_WindowBase::init_cursors()
2006 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2007 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2008 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2009 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2010 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2011 move_cursor = XCreateFontCursor(display, XC_fleur);
2012 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2013 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2014 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2015 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2016 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2017 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2018 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2019 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2020 grabbed_cursor = create_grab_cursor();
2022 static char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2023 Colormap colormap = DefaultColormap(display, screen);
2024 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2025 rootwin, cursor_data, 8, 8);
2026 XColor black, dummy;
2027 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2028 transparent_cursor = XCreatePixmapCursor(display,
2029 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2030 // XDefineCursor(display, win, transparent_cursor);
2031 XFreePixmap(display, pixmap_bottom);
2034 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2036 int color_model = BC_TRANSPARENCY;
2040 color_model = BC_RGB8;
2043 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2046 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2049 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2055 int BC_WindowBase::init_colors()
2058 current_color_value = current_color_pixel = 0;
2060 // Get the real depth
2063 ximage = XCreateImage(top_level->display,
2065 top_level->default_depth,
2073 bits_per_pixel = ximage->bits_per_pixel;
2074 XDestroyImage(ximage);
2076 color_model = evaluate_color_model(client_byte_order,
2079 // Get the color model
2085 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2086 create_private_colors();
2090 cmap = DefaultColormap(display, screen);
2091 create_shared_colors();
2094 allocate_color_table();
2098 //cmap = DefaultColormap(display, screen);
2099 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2105 int BC_WindowBase::create_private_colors()
2110 for(int i = 0; i < 255; i++)
2112 color = (i & 0xc0) << 16;
2113 color += (i & 0x38) << 10;
2114 color += (i & 0x7) << 5;
2115 color_table[i][0] = color;
2117 create_shared_colors(); // overwrite the necessary colors on the table
2122 int BC_WindowBase::create_color(int color)
2124 if(total_colors == 256)
2126 // replace the closest match with an exact match
2127 color_table[get_color_rgb8(color)][0] = color;
2131 // add the color to the table
2132 color_table[total_colors][0] = color;
2138 int BC_WindowBase::create_shared_colors()
2140 create_color(BLACK);
2141 create_color(WHITE);
2143 create_color(LTGREY);
2144 create_color(MEGREY);
2145 create_color(MDGREY);
2146 create_color(DKGREY);
2148 create_color(LTCYAN);
2149 create_color(MECYAN);
2150 create_color(MDCYAN);
2151 create_color(DKCYAN);
2153 create_color(LTGREEN);
2154 create_color(GREEN);
2155 create_color(DKGREEN);
2157 create_color(LTPINK);
2161 create_color(LTBLUE);
2163 create_color(DKBLUE);
2165 create_color(LTYELLOW);
2166 create_color(MEYELLOW);
2167 create_color(MDYELLOW);
2168 create_color(DKYELLOW);
2170 create_color(LTPURPLE);
2171 create_color(MEPURPLE);
2172 create_color(MDPURPLE);
2173 create_color(DKPURPLE);
2175 create_color(FGGREY);
2176 create_color(MNBLUE);
2177 create_color(ORANGE);
2178 create_color(FTGREY);
2183 Cursor BC_WindowBase::create_grab_cursor()
2185 int iw = 23, iw1 = iw-1, iw2 = iw/2;
2186 int ih = 23, ih1 = ih-1, ih2 = ih/2;
2187 VFrame grab(iw,ih,BC_RGB888);
2189 grab.set_pixel_color(RED); // fg
2190 grab.draw_smooth(iw2,0, iw1,0, iw1,ih2);
2191 grab.draw_smooth(iw1,ih2, iw1,ih1, iw2,ih1);
2192 grab.draw_smooth(iw2,ih1, 0,ih1, 0,ih2);
2193 grab.draw_smooth(0,ih2, 0,0, iw2,0);
2194 grab.set_pixel_color(WHITE); // bg
2195 grab.draw_line(0,ih2, iw2-2,ih2);
2196 grab.draw_line(iw2+2,ih2, iw1,ih2);
2197 grab.draw_line(iw2,0, iw2,ih2-2);
2198 grab.draw_line(iw2,ih2+2, iw2,ih1);
2200 int bpl = (iw+7)/8, isz = bpl * ih;
2201 char img[isz]; memset(img, 0, isz);
2202 char msk[isz]; memset(msk, 0, isz);
2203 unsigned char **rows = grab.get_rows();
2204 for( int iy=0; iy<ih; ++iy ) {
2205 char *op = img + iy*bpl;
2206 char *mp = msk + iy*bpl;
2207 unsigned char *ip = rows[iy];
2208 for( int ix=0; ix<iw; ++ix,ip+=3 ) {
2209 if( ip[0] ) mp[ix>>3] |= (1<<(ix&7));
2210 if( !ip[1] ) op[ix>>3] |= (1<<(ix&7));
2213 unsigned long white_pix = WhitePixel(display, screen);
2214 unsigned long black_pix = BlackPixel(display, screen);
2215 Pixmap img_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2216 img, iw,ih, white_pix,black_pix, 1);
2217 Pixmap msk_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2218 msk, iw,ih, white_pix,black_pix, 1);
2221 fc.flags = bc.flags = DoRed | DoGreen | DoBlue;
2222 fc.red = 0xffff; fc.green = fc.blue = 0; // fg
2223 bc.red = bc.green = bc.blue = 0x0000; // bg
2224 Cursor cursor = XCreatePixmapCursor(display, img_xpm,msk_xpm, &fc,&bc, iw2,ih2);
2225 XFreePixmap(display, img_xpm);
2226 XFreePixmap(display, msk_xpm);
2230 int BC_WindowBase::allocate_color_table()
2232 int red, green, blue, color;
2235 for(int i = 0; i < total_colors; i++)
2237 color = color_table[i][0];
2238 red = (color & 0xFF0000) >> 16;
2239 green = (color & 0x00FF00) >> 8;
2240 blue = color & 0xFF;
2242 col.flags = DoRed | DoGreen | DoBlue;
2243 col.red = red<<8 | red;
2244 col.green = green<<8 | green;
2245 col.blue = blue<<8 | blue;
2247 XAllocColor(display, cmap, &col);
2248 color_table[i][1] = col.pixel;
2251 XInstallColormap(display, cmap);
2255 int BC_WindowBase::init_window_shape()
2257 if(bg_pixmap && bg_pixmap->use_alpha())
2259 XShapeCombineMask(top_level->display,
2260 this->win, ShapeBounding, 0, 0,
2261 bg_pixmap->get_alpha(), ShapeSet);
2267 int BC_WindowBase::init_gc()
2269 unsigned long gcmask;
2270 gcmask = GCFont | GCGraphicsExposures;
2273 gcvalues.font = mediumfont->fid; // set the font
2274 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2275 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2277 // gcmask = GCCapStyle | GCJoinStyle;
2278 // XGetGCValues(display, gc, gcmask, &gcvalues);
2279 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2283 int BC_WindowBase::init_fonts()
2285 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2286 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2287 smallfont = XLoadQueryFont(display, "fixed");
2288 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2289 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2290 mediumfont = XLoadQueryFont(display, "fixed");
2291 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2292 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2293 largefont = XLoadQueryFont(display, "fixed");
2294 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2295 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2296 bigfont = XLoadQueryFont(display, "fixed");
2298 if((clockfont = XLoadQueryFont(display, _(resources.clock_font))) == NULL)
2299 if((clockfont = XLoadQueryFont(display, _(resources.clock_font2))) == NULL)
2300 clockfont = XLoadQueryFont(display, "fixed");
2303 if(get_resources()->use_fontset)
2308 // FIXME: should check the m,d,n values
2309 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2311 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2312 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2313 if( !mediumfontset )
2314 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2315 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2317 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2318 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2320 bigfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2321 clockfontset = XCreateFontSet(display, resources.clock_fontset, &m, &n, &d);
2323 clockfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2324 if(clockfontset && bigfontset && largefontset && mediumfontset && smallfontset) {
2325 curr_fontset = mediumfontset;
2326 get_resources()->use_fontset = 1;
2330 get_resources()->use_fontset = 0;
2337 void BC_WindowBase::init_xft()
2340 if( !get_resources()->use_xft ) return;
2341 if(!(smallfont_xft =
2342 (resources.small_font_xft[0] == '-' ?
2343 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2344 XftFontOpenName(display, screen, resources.small_font_xft))) )
2345 if(!(smallfont_xft =
2346 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2347 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2348 if(!(mediumfont_xft =
2349 (resources.medium_font_xft[0] == '-' ?
2350 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2351 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2352 if(!(mediumfont_xft =
2353 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2354 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2355 if(!(largefont_xft =
2356 (resources.large_font_xft[0] == '-' ?
2357 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2358 XftFontOpenName(display, screen, resources.large_font_xft))) )
2359 if(!(largefont_xft =
2360 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2361 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2363 (resources.big_font_xft[0] == '-' ?
2364 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2365 XftFontOpenName(display, screen, resources.big_font_xft))) )
2367 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2368 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2369 if(!(clockfont_xft =
2370 (resources.clock_font_xft[0] == '-' ?
2371 XftFontOpenXlfd(display, screen, resources.clock_font_xft) :
2372 XftFontOpenName(display, screen, resources.clock_font_xft))) )
2373 clockfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2376 if(!(bold_smallfont_xft =
2377 (resources.small_b_font_xft[0] == '-' ?
2378 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2379 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2380 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2381 if(!(bold_mediumfont_xft =
2382 (resources.medium_b_font_xft[0] == '-' ?
2383 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2384 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2385 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2386 if(!(bold_largefont_xft =
2387 (resources.large_b_font_xft[0] == '-' ?
2388 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2389 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2390 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2392 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2393 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ||
2395 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2396 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2397 resources.small_font_xft, smallfont_xft,
2398 resources.medium_font_xft, mediumfont_xft,
2399 resources.large_font_xft, largefont_xft,
2400 resources.big_font_xft, bigfont_xft,
2401 resources.clock_font_xft, clockfont_xft,
2402 resources.small_b_font_xft, bold_smallfont_xft,
2403 resources.medium_b_font_xft, bold_mediumfont_xft,
2404 resources.large_b_font_xft, bold_largefont_xft);
2405 get_resources()->use_xft = 0;
2408 // _XftDisplayInfo needs a lock.
2409 XftDefaultHasRender(display);
2413 void BC_WindowBase::init_im()
2415 XIMStyles *xim_styles;
2418 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2420 printf("BC_WindowBase::init_im: Could not open input method.\n");
2423 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2426 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2427 XCloseIM(input_method);
2432 for(int z = 0; z < xim_styles->count_styles; z++)
2434 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2436 xim_style = xim_styles->supported_styles[z];
2444 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2445 XCloseIM(input_method);
2449 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2450 XNClientWindow, win, XNFocusWindow, win, NULL);
2453 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2454 XCloseIM(input_method);
2459 void BC_WindowBase::finit_im()
2461 if( input_context ) {
2462 XDestroyIC(input_context);
2465 if( input_method ) {
2466 XCloseIM(input_method);
2472 int BC_WindowBase::get_color(int64_t color)
2474 // return pixel of color
2475 // use this only for drawing subwindows not for bitmaps
2476 int i, test, difference;
2482 return get_color_rgb8(color);
2483 // test last color looked up
2484 if(current_color_value == color)
2485 return current_color_pixel;
2488 current_color_value = color;
2489 for(i = 0; i < total_colors; i++)
2491 if(color_table[i][0] == color)
2493 current_color_pixel = color_table[i][1];
2494 return current_color_pixel;
2498 // find nearest match
2499 difference = 0xFFFFFF;
2501 for(i = 0; i < total_colors; i++)
2503 test = abs((int)(color_table[i][0] - color));
2505 if(test < difference)
2507 current_color_pixel = color_table[i][1];
2511 return current_color_pixel;
2514 return get_color_rgb16(color);
2517 return get_color_bgr16(color);
2521 return client_byte_order == server_byte_order ?
2522 color : get_color_bgr24(color);
2530 int BC_WindowBase::get_color_rgb8(int color)
2534 pixel = (color & 0xc00000) >> 16;
2535 pixel += (color & 0xe000) >> 10;
2536 pixel += (color & 0xe0) >> 5;
2540 int64_t BC_WindowBase::get_color_rgb16(int color)
2543 result = (color & 0xf80000) >> 8;
2544 result += (color & 0xfc00) >> 5;
2545 result += (color & 0xf8) >> 3;
2550 int64_t BC_WindowBase::get_color_bgr16(int color)
2553 result = (color & 0xf80000) >> 19;
2554 result += (color & 0xfc00) >> 5;
2555 result += (color & 0xf8) << 8;
2560 int64_t BC_WindowBase::get_color_bgr24(int color)
2563 result = (color & 0xff) << 16;
2564 result += (color & 0xff00);
2565 result += (color & 0xff0000) >> 16;
2569 void BC_WindowBase::start_video()
2571 cursor_timer->update();
2573 // set_color(BLACK);
2574 // draw_box(0, 0, get_w(), get_h());
2578 void BC_WindowBase::stop_video()
2586 int64_t BC_WindowBase::get_color()
2588 return top_level->current_color;
2591 void BC_WindowBase::set_color(int64_t color)
2593 top_level->current_color = color;
2594 XSetForeground(top_level->display,
2596 top_level->get_color(color));
2599 void BC_WindowBase::set_opaque()
2601 XSetFunction(top_level->display, top_level->gc, GXcopy);
2604 void BC_WindowBase::set_inverse()
2606 XSetFunction(top_level->display, top_level->gc, GXxor);
2609 void BC_WindowBase::set_line_width(int value)
2611 this->line_width = value;
2612 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2613 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2614 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2615 JoinMiter); /* join_style */
2617 if(line_dashes > 0) {
2618 const char dashes = line_dashes;
2619 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2622 // XGCValues gcvalues;
2623 // unsigned long gcmask;
2624 // gcmask = GCCapStyle | GCJoinStyle;
2625 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2626 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2629 void BC_WindowBase::set_line_dashes(int value)
2631 line_dashes = value;
2632 // call XSetLineAttributes
2633 set_line_width(line_width);
2637 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2641 case ARROW_CURSOR: return top_level->arrow_cursor;
2642 case CROSS_CURSOR: return top_level->cross_cursor;
2643 case IBEAM_CURSOR: return top_level->ibeam_cursor;
2644 case VSEPARATE_CURSOR: return top_level->vseparate_cursor;
2645 case HSEPARATE_CURSOR: return top_level->hseparate_cursor;
2646 case MOVE_CURSOR: return top_level->move_cursor;
2647 case LEFT_CURSOR: return top_level->left_cursor;
2648 case RIGHT_CURSOR: return top_level->right_cursor;
2649 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor;
2650 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor;
2651 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor;
2652 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor;
2653 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor;
2654 case HOURGLASS_CURSOR: return top_level->hourglass_cursor;
2655 case TRANSPARENT_CURSOR: return top_level->transparent_cursor;
2656 case GRABBED_CURSOR: return top_level->grabbed_cursor;
2661 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2663 // inherit cursor from parent
2666 XUndefineCursor(top_level->display, win);
2667 current_cursor = cursor;
2670 // don't change cursor if overridden
2671 if((!top_level->is_hourglass && !is_transparent) ||
2674 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2675 if(flush) this->flush();
2678 if(!override) current_cursor = cursor;
2681 void BC_WindowBase::set_x_cursor(int cursor)
2683 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2684 XDefineCursor(top_level->display, win, temp_cursor);
2685 current_cursor = cursor;
2689 int BC_WindowBase::get_cursor()
2691 return current_cursor;
2694 void BC_WindowBase::start_hourglass()
2696 top_level->start_hourglass_recursive();
2700 void BC_WindowBase::stop_hourglass()
2702 top_level->stop_hourglass_recursive();
2706 void BC_WindowBase::start_hourglass_recursive()
2708 if(this == top_level)
2716 set_cursor(HOURGLASS_CURSOR, 1, 0);
2717 for(int i = 0; i < subwindows->total; i++)
2719 subwindows->values[i]->start_hourglass_recursive();
2724 void BC_WindowBase::stop_hourglass_recursive()
2726 if(this == top_level)
2728 if(hourglass_total == 0) return;
2729 top_level->hourglass_total--;
2732 if(!top_level->hourglass_total)
2734 top_level->is_hourglass = 0;
2736 // Cause set_cursor to perform change
2738 set_cursor(current_cursor, 1, 0);
2740 for(int i = 0; i < subwindows->total; i++)
2742 subwindows->values[i]->stop_hourglass_recursive();
2750 XFontStruct* BC_WindowBase::get_font_struct(int font)
2752 // Clear out unrelated flags
2753 if(font & BOLDFACE) font ^= BOLDFACE;
2756 case SMALLFONT: return top_level->smallfont; break;
2757 case MEDIUMFONT: return top_level->mediumfont; break;
2758 case LARGEFONT: return top_level->largefont; break;
2759 case BIGFONT: return top_level->bigfont; break;
2760 case CLOCKFONT: return top_level->clockfont; break;
2765 XFontSet BC_WindowBase::get_fontset(int font)
2769 if(get_resources()->use_fontset)
2771 switch(font & 0xff) {
2772 case SMALLFONT: fs = top_level->smallfontset; break;
2773 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2774 case LARGEFONT: fs = top_level->largefontset; break;
2775 case BIGFONT: fs = top_level->bigfontset; break;
2776 case CLOCKFONT: fs = top_level->clockfontset; break;
2784 XftFont* BC_WindowBase::get_xft_struct(int font)
2787 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2788 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2789 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2790 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2791 case CLOCKFONT: return (XftFont*)top_level->clockfont_xft;
2792 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2793 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2794 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2802 int BC_WindowBase::get_current_font()
2804 return top_level->current_font;
2807 void BC_WindowBase::set_font(int font)
2809 top_level->current_font = font;
2812 if(get_resources()->use_xft) {}
2815 if(get_resources()->use_fontset) {
2819 if(get_font_struct(font))
2821 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2827 void BC_WindowBase::set_fontset(int font)
2831 if(get_resources()->use_fontset) {
2833 case SMALLFONT: fs = top_level->smallfontset; break;
2834 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2835 case LARGEFONT: fs = top_level->largefontset; break;
2836 case BIGFONT: fs = top_level->bigfontset; break;
2837 case CLOCKFONT: fs = top_level->clockfontset; break;
2845 XFontSet BC_WindowBase::get_curr_fontset(void)
2847 if(get_resources()->use_fontset)
2848 return curr_fontset;
2852 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2855 if(get_resources()->use_xft && get_xft_struct(font))
2858 #ifdef X_HAVE_UTF8_STRING
2859 if(get_resources()->locale_utf8)
2861 XftTextExtentsUtf8(top_level->display,
2862 get_xft_struct(font),
2863 (const XftChar8 *)text,
2870 XftTextExtents8(top_level->display,
2871 get_xft_struct(font),
2872 (const XftChar8 *)text,
2876 return extents.xOff;
2880 if(get_resources()->use_fontset && top_level->get_fontset(font))
2881 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2883 if(get_font_struct(font))
2884 return XTextWidth(get_font_struct(font), text, length);
2890 case MEDIUM_7SEGMENT:
2891 return get_resources()->medium_7segment[0]->get_w() * length;
2901 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2903 int i, j, w = 0, line_w = 0;
2904 if(length < 0) length = strlen(text);
2906 for(i = 0, j = 0; i <= length; i++)
2911 line_w = get_single_text_width(font, &text[j], i - j);
2917 line_w = get_single_text_width(font, &text[j], length - j);
2919 if(line_w > w) w = line_w;
2922 if(i > length && w == 0)
2924 w = get_single_text_width(font, text, length);
2930 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2933 if( length < 0 ) length = wcslen(text);
2935 for( i=j=0; i<length && text[i]; ++i ) {
2936 if( text[i] != '\n' ) continue;
2938 int lw = get_single_text_width(font, &text[j], i-j);
2939 if( w < lw ) w = lw;
2944 int lw = get_single_text_width(font, &text[j], length-j);
2945 if( w < lw ) w = lw;
2951 int BC_WindowBase::get_text_ascent(int font)
2955 if( (fstruct = get_xft_struct(font)) != 0 )
2956 return fstruct->ascent;
2958 if(get_resources()->use_fontset && top_level->get_fontset(font))
2960 XFontSetExtents *extents;
2962 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2963 return -extents->max_logical_extent.y;
2966 if(get_font_struct(font))
2967 return top_level->get_font_struct(font)->ascent;
2970 case MEDIUM_7SEGMENT:
2971 return get_resources()->medium_7segment[0]->get_h();
2976 int BC_WindowBase::get_text_descent(int font)
2980 if( (fstruct = get_xft_struct(font)) != 0 )
2981 return fstruct->descent;
2983 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2984 XFontSetExtents *extents;
2985 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2986 return (extents->max_logical_extent.height
2987 + extents->max_logical_extent.y);
2990 if(get_font_struct(font))
2991 return top_level->get_font_struct(font)->descent;
2996 int BC_WindowBase::get_text_height(int font, const char *text)
3001 if( (fstruct = get_xft_struct(font)) != 0 )
3002 rowh = fstruct->height;
3005 rowh = get_text_ascent(font) + get_text_descent(font);
3007 if(!text) return rowh;
3009 // Add height of lines
3010 int h = 0, i, length = strlen(text);
3011 for(i = 0; i <= length; i++)
3022 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
3024 if(color_model < 0) color_model = top_level->get_color_model();
3025 return new BC_Bitmap(top_level, w, h, color_model);
3028 void BC_WindowBase::init_wait()
3030 #ifndef SINGLE_THREAD
3031 if(window_type != MAIN_WINDOW)
3032 top_level->init_wait();
3033 init_lock->lock("BC_WindowBase::init_wait");
3034 init_lock->unlock();
3038 int BC_WindowBase::accel_available(int color_model, int lock_it)
3040 if( window_type != MAIN_WINDOW )
3041 return top_level->accel_available(color_model, lock_it);
3043 lock_window("BC_WindowBase::accel_available");
3045 switch(color_model) {
3047 grab_port_id(color_model);
3051 grab_port_id(color_model);
3060 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3061 return xvideo_port_id >= 0 ? 1 : 0;
3065 int BC_WindowBase::grab_port_id(int color_model)
3067 if( !get_resources()->use_xvideo || // disabled
3068 !get_resources()->use_shm ) // Only local server is fast enough.
3070 if( xvideo_port_id >= 0 )
3071 return xvideo_port_id;
3073 unsigned int ver, rev, reqBase, eventBase, errorBase;
3074 if( Success != XvQueryExtension(display, // XV extension is available
3075 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3078 // XV adaptors are available
3079 unsigned int numAdapt = 0;
3080 XvAdaptorInfo *info = 0;
3081 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3085 // Translate from color_model to X color model
3086 int x_color_model = BC_CModels::bc_to_x(color_model);
3088 // Get adaptor with desired color model
3089 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3090 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3091 // adaptor supports XvImages
3092 int numFormats = 0, numPorts = info[i].num_ports;
3093 XvImageFormatValues *formats =
3094 XvListImageFormats(display, info[i].base_id, &numFormats);
3095 if( !formats ) continue;
3097 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3098 if( formats[j].id != x_color_model ) continue;
3099 // this adaptor supports the desired format, grab a port
3100 for( int k=0; k<numPorts; ++k ) {
3101 if( Success == XvGrabPort(top_level->display,
3102 info[i].base_id+k, CurrentTime) ) {
3103 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3104 xvideo_port_id = info[i].base_id + k;
3112 XvFreeAdaptorInfo(info);
3114 return xvideo_port_id;
3118 int BC_WindowBase::show_window(int flush)
3120 for(int i = 0; i < subwindows->size(); i++)
3122 subwindows->get(i)->show_window(0);
3125 XMapWindow(top_level->display, win);
3126 if(flush) XFlush(top_level->display);
3127 // XSync(top_level->display, 0);
3132 int BC_WindowBase::hide_window(int flush)
3134 for(int i = 0; i < subwindows->size(); i++)
3136 subwindows->get(i)->hide_window(0);
3139 XUnmapWindow(top_level->display, win);
3140 if(flush) this->flush();
3145 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3147 subwindows->append((BC_SubWindow*)menu_bar);
3149 menu_bar->parent_window = this;
3150 menu_bar->top_level = this->top_level;
3151 menu_bar->initialize();
3155 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3157 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3158 if(this != top_level) return top_level->add_popup(window);
3159 popups.append(window);
3163 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3165 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3166 if(this != top_level)
3167 top_level->remove_popup(window);
3169 popups.remove(window);
3170 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3174 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3176 subwindows->append(subwindow);
3178 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3180 // parent window must be set before the subwindow initialization
3181 subwindow->parent_window = this;
3182 subwindow->top_level = this->top_level;
3184 // Execute derived initialization
3185 subwindow->initialize();
3190 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3192 return add_subwindow(subwindow);
3195 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3197 if( !top_level->flash_enabled ) return 0;
3198 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3200 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3203 XClearArea(top_level->display, win, x, y, w, h, 0);
3207 XClearWindow(top_level->display, win);
3215 int BC_WindowBase::flash(int flush)
3217 flash(-1, -1, -1, -1, flush);
3221 void BC_WindowBase::flush()
3223 //if(!get_window_lock())
3224 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3225 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3226 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3227 int locked = get_window_lock();
3228 if( !locked ) lock_window("BC_WindowBase::flush");
3229 XFlush(top_level->display);
3230 if( !locked ) unlock_window();
3233 void BC_WindowBase::sync_display()
3235 int locked = get_window_lock();
3236 if( !locked ) lock_window("BC_WindowBase::sync_display");
3237 XSync(top_level->display, False);
3238 if( !locked ) unlock_window();
3241 int BC_WindowBase::get_window_lock()
3243 #ifdef SINGLE_THREAD
3244 return BC_Display::display_global->get_display_locked();
3246 return top_level->window_lock;
3250 int BC_WindowBase::lock_window(const char *location)
3252 if(top_level && top_level != this)
3254 top_level->lock_window(location);
3259 SET_LOCK(this, title, location);
3260 #ifdef SINGLE_THREAD
3261 BC_Display::lock_display(location);
3263 XLockDisplay(top_level->display);
3264 top_level->display_lock_owner = pthread_self();
3267 ++top_level->window_lock;
3271 printf("BC_WindowBase::lock_window top_level NULL\n");
3276 int BC_WindowBase::unlock_window()
3278 if(top_level && top_level != this)
3280 top_level->unlock_window();
3287 // if(!top_level->window_lock)
3289 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3293 if( top_level->window_lock > 0 )
3294 if( --top_level->window_lock == 0 )
3295 top_level->display_lock_owner = 0;
3296 #ifdef SINGLE_THREAD
3297 BC_Display::unlock_display();
3299 XUnlockDisplay(top_level->display);
3304 printf("BC_WindowBase::unlock_window top_level NULL\n");
3309 void BC_WindowBase::set_done(int return_value)
3311 if(done_set) return;
3313 if(window_type != MAIN_WINDOW)
3314 top_level->set_done(return_value);
3317 #ifdef SINGLE_THREAD
3318 this->return_value = return_value;
3319 BC_Display::display_global->arm_completion(this);
3320 completion_lock->unlock();
3321 #else // SINGLE_THREAD
3323 if( !event_thread ) return;
3324 XEvent *event = new_xevent();
3325 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3326 event->type = ClientMessage;
3327 ptr->message_type = SetDoneXAtom;
3329 this->return_value = return_value;
3330 // May lock up here because XSendEvent doesn't work too well
3331 // asynchronous with XNextEvent.
3332 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3334 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3340 void BC_WindowBase::close(int return_value)
3342 hide_window(); flush();
3343 set_done(return_value);
3346 int BC_WindowBase::grab(BC_WindowBase *window)
3348 if( window->active_grab && this != window->active_grab ) return 0;
3349 window->active_grab = this;
3350 this->grab_active = window;
3353 int BC_WindowBase::ungrab(BC_WindowBase *window)
3355 if( window->active_grab && this != window->active_grab ) return 0;
3356 window->active_grab = 0;
3357 this->grab_active = 0;
3360 int BC_WindowBase::grab_buttons()
3362 XSync(top_level->display, False);
3363 if( XGrabButton(top_level->display, AnyButton, AnyModifier,
3364 top_level->rootwin, True, ButtonPressMask | ButtonReleaseMask,
3365 GrabModeAsync, GrabModeSync, None, None) == GrabSuccess ) {
3366 set_active_subwindow(this);
3371 void BC_WindowBase::ungrab_buttons()
3373 XUngrabButton(top_level->display, AnyButton, AnyModifier, top_level->rootwin);
3374 set_active_subwindow(0);
3377 void BC_WindowBase::grab_cursor()
3379 Cursor cursor_grab = get_cursor_struct(GRABBED_CURSOR);
3380 XGrabPointer(top_level->display, top_level->rootwin, True,
3381 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
3382 GrabModeAsync, GrabModeAsync, None, cursor_grab, CurrentTime);
3384 void BC_WindowBase::ungrab_cursor()
3386 XUngrabPointer(top_level->display, CurrentTime);
3390 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3391 // this is the bounding box of all the screens
3393 int BC_WindowBase::get_root_w(int lock_display)
3395 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3396 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3397 int result = WidthOfScreen(def_screen);
3398 if(lock_display) unlock_window();
3402 int BC_WindowBase::get_root_h(int lock_display)
3404 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3405 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3406 int result = HeightOfScreen(def_screen);
3407 if(lock_display) unlock_window();
3411 XineramaScreenInfo *
3412 BC_WindowBase::get_xinerama_info(int screen)
3414 if( !xinerama_info || !xinerama_screens ) return 0;
3416 for( int i=0; i<xinerama_screens; ++i )
3417 if( xinerama_info[i].screen_number == screen )
3418 return &xinerama_info[i];
3421 int top_x = get_x(), top_y = get_y();
3422 for( int i=0; i<xinerama_screens; ++i ) {
3423 int scr_y = top_y - xinerama_info[i].y_org;
3424 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3425 int scr_x = top_x - xinerama_info[i].x_org;
3426 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3427 return &xinerama_info[i];
3432 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3435 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3436 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3439 int root_w = get_root_w(0);
3440 int root_h = get_root_h(0);
3441 // Shift X based on position of current window if dual head
3442 if( (float)root_w/root_h > 1.8 ) {
3443 root_w = get_screen_w(0, 0);
3444 if( top_level->get_x() >= root_w )
3449 result = info->x_org;
3450 if(lock_display) unlock_window();
3454 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3456 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3457 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3458 int result = !info ? 0 : info->y_org;
3459 if(lock_display) unlock_window();
3463 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3466 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3467 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3469 int width = get_root_w(0);
3470 int height = get_root_h(0);
3471 if( (float)width/height > 1.8 ) {
3472 // If dual head, the screen width is > 16x9
3473 // but we only want to fill one screen
3474 // this code assumes the "big" screen is on the right
3475 int scr_w0 = width / 2;
3477 case 600: scr_w0 = 800; break;
3478 case 720: scr_w0 = 1280; break;
3479 case 1024: scr_w0 = 1280; break;
3480 case 1200: scr_w0 = 1600; break;
3481 case 1080: scr_w0 = 1920; break;
3483 int scr_w1 = width - scr_w0;
3484 result = screen > 0 ? scr_w1 :
3485 screen == 0 ? scr_w0 :
3486 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3492 result = info->width;
3493 if(lock_display) unlock_window();
3497 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3499 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3500 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3501 int result = info ? info->height : get_root_h(0);
3502 if(lock_display) unlock_window();
3506 // Bottom right corner
3507 int BC_WindowBase::get_x2()
3512 int BC_WindowBase::get_y2()
3517 int BC_WindowBase::get_video_on()
3522 int BC_WindowBase::get_hidden()
3524 return top_level->hidden;
3527 int BC_WindowBase::cursor_inside()
3529 return (top_level->cursor_x >= 0 &&
3530 top_level->cursor_y >= 0 &&
3531 top_level->cursor_x < w &&
3532 top_level->cursor_y < h);
3535 BC_WindowBase* BC_WindowBase::get_top_level()
3540 BC_WindowBase* BC_WindowBase::get_parent()
3542 return parent_window;
3545 int BC_WindowBase::get_color_model()
3547 return top_level->color_model;
3550 BC_Resources* BC_WindowBase::get_resources()
3552 return &BC_WindowBase::resources;
3555 BC_Synchronous* BC_WindowBase::get_synchronous()
3557 return BC_WindowBase::resources.get_synchronous();
3560 int BC_WindowBase::get_bg_color()
3565 void BC_WindowBase::set_bg_color(int color)
3567 this->bg_color = color;
3570 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3575 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3577 top_level->active_subwindow = subwindow;
3580 int BC_WindowBase::activate()
3585 int BC_WindowBase::deactivate()
3587 if(window_type == MAIN_WINDOW)
3589 if( top_level->active_menubar ) {
3590 top_level->active_menubar->deactivate();
3591 top_level->active_menubar = 0;
3593 if( top_level->active_popup_menu ) {
3594 top_level->active_popup_menu->deactivate();
3595 top_level->active_popup_menu = 0;
3597 if( top_level->active_subwindow ) {
3598 top_level->active_subwindow->deactivate();
3599 top_level->active_subwindow = 0;
3601 if( top_level->motion_events && top_level->last_motion_win == this->win )
3602 top_level->motion_events = 0;
3608 int BC_WindowBase::cycle_textboxes(int amount)
3611 BC_WindowBase *new_textbox = 0;
3615 BC_WindowBase *first_textbox = 0;
3616 find_next_textbox(&first_textbox, &new_textbox, result);
3617 if(!new_textbox) new_textbox = first_textbox;
3623 BC_WindowBase *last_textbox = 0;
3624 find_prev_textbox(&last_textbox, &new_textbox, result);
3625 if(!new_textbox) new_textbox = last_textbox;
3629 if(new_textbox != active_subwindow)
3632 new_textbox->activate();
3638 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3640 // Search subwindows for textbox
3641 for(int i = 0; i < subwindows->total && result < 2; i++)
3643 BC_WindowBase *test_subwindow = subwindows->values[i];
3644 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3651 if(!*first_textbox) *first_textbox = this;
3655 if(top_level->active_subwindow == this)
3661 *next_textbox = this;
3668 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3674 if(!*last_textbox) *last_textbox = this;
3678 if(top_level->active_subwindow == this)
3684 *prev_textbox = this;
3689 // Search subwindows for textbox
3690 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3692 BC_WindowBase *test_subwindow = subwindows->values[i];
3693 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3698 BC_Clipboard* BC_WindowBase::get_clipboard()
3700 #ifdef SINGLE_THREAD
3701 return BC_Display::display_global->clipboard;
3703 return top_level->clipboard;
3707 Atom BC_WindowBase::to_clipboard(const char *data, long len, int clipboard_num)
3709 return get_clipboard()->to_clipboard(this, data, len, clipboard_num);
3712 long BC_WindowBase::from_clipboard(char *data, long maxlen, int clipboard_num)
3714 return get_clipboard()->from_clipboard(data, maxlen, clipboard_num);
3717 long BC_WindowBase::clipboard_len(int clipboard_num)
3719 return get_clipboard()->clipboard_len(clipboard_num);
3722 int BC_WindowBase::do_selection_clear(Window win)
3724 top_level->event_win = win;
3725 return dispatch_selection_clear();
3728 int BC_WindowBase::dispatch_selection_clear()
3731 for( int i=0; i<subwindows->total && !result; ++i )
3732 result = subwindows->values[i]->dispatch_selection_clear();
3734 result = selection_clear_event();
3739 void BC_WindowBase::get_relative_cursor(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");
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(x, y, lock_window);
3760 int BC_WindowBase::get_relative_cursor_y(int lock_window)
3763 get_relative_cursor(x, y, lock_window);
3767 void BC_WindowBase::get_abs_cursor(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");
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(abs_x, abs_y, lock_window);
3785 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3788 get_abs_cursor(abs_x, abs_y, lock_window);
3792 void BC_WindowBase::get_pop_cursor(int &px, int &py, int lock_window)
3795 get_abs_cursor(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(px, py, lock_window);
3809 int BC_WindowBase::get_pop_cursor_y(int lock_window)
3812 get_pop_cursor(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(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, int utf8)
4136 // utf8>0: wm + net_wm, utf8=0: wm only, utf<0: net_wm only
4138 const unsigned char *wm_title = (const unsigned char *)title;
4139 int title_len = strlen((const char *)title);
4141 Atom xa_wm_name = XA_WM_NAME;
4142 Atom xa_icon_name = XA_WM_ICON_NAME;
4143 Atom xa_string = XA_STRING;
4144 XChangeProperty(display, win, xa_wm_name, xa_string, 8,
4145 PropModeReplace, wm_title, title_len);
4146 XChangeProperty(display, win, xa_icon_name, xa_string, 8,
4147 PropModeReplace, wm_title, title_len);
4150 Atom xa_net_wm_name = XInternAtom(display, "_NET_WM_NAME", True);
4151 Atom xa_net_icon_name = XInternAtom(display, "_NET_WM_ICON_NAME", True);
4152 Atom xa_utf8_string = XInternAtom(display, "UTF8_STRING", True);
4153 XChangeProperty(display, win, xa_net_wm_name, xa_utf8_string, 8,
4154 PropModeReplace, wm_title, title_len);
4155 XChangeProperty(display, win, xa_net_icon_name, xa_utf8_string, 8,
4156 PropModeReplace, wm_title, title_len);
4161 const char *BC_WindowBase::get_title()
4166 int BC_WindowBase::get_toggle_value()
4168 return toggle_value;
4171 int BC_WindowBase::get_toggle_drag()
4176 int BC_WindowBase::set_icon(VFrame *data)
4178 if(icon_pixmap) delete icon_pixmap;
4179 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4181 if(icon_window) delete icon_window;
4182 icon_window = new BC_Popup(this,
4185 icon_pixmap->get_w(),
4186 icon_pixmap->get_h(),
4188 1, // All windows are hidden initially
4192 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4193 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4194 wm_hints.icon_mask = icon_pixmap->get_alpha();
4195 wm_hints.icon_window = icon_window->win;
4196 wm_hints.window_group = XGroupLeader;
4198 // for(int i = 0; i < 1000; i++)
4199 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4202 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4203 XSync(top_level->display, 0);
4207 int BC_WindowBase::set_w(int w)
4213 int BC_WindowBase::set_h(int h)
4219 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4221 BC_Resources *resources = get_resources();
4222 char string[BCTEXTLEN];
4223 int newest_id = - 1;
4224 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4226 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4227 resources->filebox_history[i].path[0] = 0;
4228 defaults->get(string, resources->filebox_history[i].path);
4229 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4230 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4231 if(resources->filebox_history[i].id > newest_id)
4232 newest_id = resources->filebox_history[i].id;
4235 resources->filebox_id = newest_id + 1;
4236 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4237 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4238 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4239 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4240 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4241 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4242 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4243 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4244 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4245 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4246 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4247 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4251 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4253 BC_Resources *resources = get_resources();
4254 char string[BCTEXTLEN];
4255 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4257 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4258 defaults->update(string, resources->filebox_history[i].path);
4259 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4260 defaults->update(string, resources->filebox_history[i].id);
4262 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4263 defaults->update("FILEBOX_W", resources->filebox_w);
4264 defaults->update("FILEBOX_H", resources->filebox_h);
4265 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4266 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4267 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4268 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4269 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4270 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4271 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4272 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4273 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4279 // For some reason XTranslateCoordinates can take a long time to return.
4280 // We work around this by only calling it when the event windows are different.
4281 void BC_WindowBase::translate_coordinates(Window src_w,
4293 *dest_x_return = src_x;
4294 *dest_y_return = src_y;
4298 XTranslateCoordinates(top_level->display,
4306 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4310 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4312 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4315 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4317 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4325 #ifdef HAVE_LIBXXF86VM
4326 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4330 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4332 XF86VidModeModeInfo **vm_modelines;
4333 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4334 for (i = 0; i < vm_count; i++) {
4335 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4338 display = top_level->display;
4339 if (vm_modelines[*vm]->hdisplay == *width)
4343 *width = vm_modelines[*vm]->hdisplay;
4344 *height = vm_modelines[*vm]->vdisplay;
4349 void BC_WindowBase::scale_vm(int vm)
4351 int foo,bar,dotclock;
4352 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4355 XF86VidModeModeInfo **vm_modelines;
4356 XF86VidModeModeLine vml;
4357 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4358 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4359 orig_modeline.dotclock = dotclock;
4360 orig_modeline.hdisplay = vml.hdisplay;
4361 orig_modeline.hsyncstart = vml.hsyncstart;
4362 orig_modeline.hsyncend = vml.hsyncend;
4363 orig_modeline.htotal = vml.htotal;
4364 orig_modeline.vdisplay = vml.vdisplay;
4365 orig_modeline.vsyncstart = vml.vsyncstart;
4366 orig_modeline.vsyncend = vml.vsyncend;
4367 orig_modeline.vtotal = vml.vtotal;
4368 orig_modeline.flags = vml.flags;
4369 orig_modeline.privsize = vml.privsize;
4370 // orig_modeline.private = vml.private;
4371 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4372 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4373 XFlush(top_level->display);
4377 void BC_WindowBase::restore_vm()
4379 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4380 XFlush(top_level->display);
4403 #ifndef SINGLE_THREAD
4404 int BC_WindowBase::get_event_count()
4406 event_lock->lock("BC_WindowBase::get_event_count");
4407 int result = common_events.total;
4408 event_lock->unlock();
4412 XEvent* BC_WindowBase::get_event()
4415 while(!done && !result)
4417 event_condition->lock("BC_WindowBase::get_event");
4418 event_lock->lock("BC_WindowBase::get_event");
4420 if(common_events.total && !done)
4422 result = common_events.values[0];
4423 common_events.remove_number(0);
4426 event_lock->unlock();
4431 void BC_WindowBase::put_event(XEvent *event)
4433 event_lock->lock("BC_WindowBase::put_event");
4434 common_events.append(event);
4435 event_lock->unlock();
4436 event_condition->unlock();
4439 void BC_WindowBase::dequeue_events(Window win)
4441 event_lock->lock("BC_WindowBase::dequeue_events");
4443 int out = 0, total = common_events.size();
4444 for( int in=0; in<total; ++in ) {
4445 if( common_events[in]->xany.window == win ) continue;
4446 common_events[out++] = common_events[in];
4448 common_events.total = out;
4450 event_lock->unlock();
4453 #endif // SINGLE_THREAD
4455 int BC_WindowBase::get_id()
4461 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4463 int w = vframe->get_w(), h = vframe->get_h();
4464 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4465 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4470 void BC_WindowBase::flicker(int n, int ms)
4472 int color = get_bg_color();
4473 for( int i=2*n; --i>=0; ) {
4474 set_inverse(); set_bg_color(WHITE);
4475 clear_box(0,0, w,h); flash(1);
4476 sync_display(); Timer::delay(ms);
4478 set_bg_color(color);