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"
49 #include "workarounds.h"
59 #include <X11/extensions/Xinerama.h>
60 #include <X11/extensions/Xvlib.h>
61 #include <X11/extensions/shape.h>
62 #include <X11/XF86keysym.h>
63 #include <X11/Sunkeysym.h>
66 BC_ResizeCall::BC_ResizeCall(int w, int h)
78 int BC_WindowBase::shm_completion_event = -1;
82 BC_Resources BC_WindowBase::resources;
84 Window XGroupLeader = 0;
86 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
87 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
89 BC_WindowBase::BC_WindowBase()
91 //printf("BC_WindowBase::BC_WindowBase 1\n");
92 BC_WindowBase::initialize();
95 BC_WindowBase::~BC_WindowBase()
98 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
100 if(window_type == MAIN_WINDOW)
101 lock_window("BC_WindowBase::~BC_WindowBase");
104 #ifdef HAVE_LIBXXF86VM
105 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
112 if(window_type != MAIN_WINDOW)
115 XSelectInput(top_level->display, this->win, 0);
116 XSync(top_level->display,0);
117 #ifndef SINGLE_THREAD
118 top_level->dequeue_events(win);
120 // drop active window refs to this
121 if(top_level->active_menubar == this) top_level->active_menubar = 0;
122 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
123 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
124 // drop motion window refs to this
125 if(top_level->motion_events && top_level->last_motion_win == this->win)
126 top_level->motion_events = 0;
128 // Remove pointer from parent window to this
129 parent_window->subwindows->remove(this);
132 if(grab_active) grab_active->active_grab = 0;
133 if(icon_window) delete icon_window;
134 if(window_type == POPUP_WINDOW)
135 parent_window->remove_popup(this);
137 // Delete the subwindows
140 while(subwindows->total)
142 // Subwindow removes its own pointer
143 delete subwindows->values[0];
150 //printf("delete glx=%08x, win=%08x %s\n", (unsigned)glx_win, (unsigned)win, title);
152 if( get_resources()->get_synchronous() && glx_win != 0 ) {
153 get_resources()->get_synchronous()->delete_window(this);
156 XDestroyWindow(top_level->display, win);
158 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
159 if(icon_pixmap) delete icon_pixmap;
160 if(temp_bitmap) delete temp_bitmap;
161 top_level->active_bitmaps.remove_buffers(this);
162 if(_7segment_pixmaps)
164 for(int i = 0; i < TOTAL_7SEGMENT; i++)
165 delete _7segment_pixmaps[i];
167 delete [] _7segment_pixmaps;
172 if(window_type == MAIN_WINDOW)
174 XFreeGC(display, gc);
175 static XFontStruct *BC_WindowBase::*xfont[] = {
176 &BC_WindowBase::smallfont,
177 &BC_WindowBase::mediumfont,
178 &BC_WindowBase::largefont,
179 &BC_WindowBase::bigfont,
180 &BC_WindowBase::clockfont,
182 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
183 XFreeFont(display, this->*xfont[i]);
186 // prevents a bug when Xft closes with unrefd fonts
187 FcPattern *defaults = FcPatternCreate();
188 FcPatternAddInteger(defaults, "maxunreffonts", 0);
189 XftDefaultSet(display, defaults);
191 static void *BC_WindowBase::*xft_font[] = {
192 &BC_WindowBase::smallfont_xft,
193 &BC_WindowBase::mediumfont_xft,
194 &BC_WindowBase::largefont_xft,
195 &BC_WindowBase::bigfont_xft,
196 &BC_WindowBase::bold_smallfont_xft,
197 &BC_WindowBase::bold_mediumfont_xft,
198 &BC_WindowBase::bold_largefont_xft,
199 &BC_WindowBase::clockfont_xft,
201 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
202 XftFont *xft = (XftFont *)(this->*xft_font[i]);
203 if( xft ) xftFontClose (display, xft);
211 XFree(xinerama_info);
212 xinerama_screens = 0;
214 if( xvideo_port_id >= 0 )
215 XvUngrabPort(display, xvideo_port_id, CurrentTime);
218 // Must be last reference to display.
219 // _XftDisplayInfo needs a lock.
220 get_resources()->create_window_lock->lock("BC_WindowBase::~BC_WindowBase");
221 XCloseDisplay(display);
222 get_resources()->create_window_lock->unlock();
224 // clipboard uses a different display connection
225 clipboard->stop_clipboard();
229 resize_history.remove_all_objects();
231 #ifndef SINGLE_THREAD
232 common_events.remove_all_objects();
234 delete event_condition;
237 top_level->window_lock = 0;
238 BC_Display::unlock_display();
243 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
244 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
245 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
248 UNSET_ALL_LOCKS(this)
251 int BC_WindowBase::initialize()
256 display_lock_owner = 0;
261 resend_event_window = 0;
273 xinerama_screens = 0;
278 translation_events = 0;
279 ctrl_mask = shift_mask = alt_mask = 0;
280 cursor_x = cursor_y = button_number = 0;
294 active_popup_menu = 0;
295 active_subwindow = 0;
299 _7segment_pixmaps = 0;
302 // next_repeat_id = 0;
304 current_font = MEDIUMFONT;
305 current_color = BLACK;
306 current_cursor = ARROW_CURSOR;
309 shared_bg_pixmap = 0;
312 window_type = MAIN_WINDOW;
313 translation_count = 0;
314 x_correction = y_correction = 0;
323 #ifdef HAVE_LIBXXF86VM
341 bold_smallfont_xft = 0;
342 bold_mediumfont_xft = 0;
343 bold_largefont_xft = 0;
345 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
347 // Need these right away since put_event is called before run_window sometimes.
348 event_lock = new Mutex("BC_WindowBase::event_lock");
349 event_condition = new Condition(0, "BC_WindowBase::event_condition");
350 init_lock = new Condition(0, "BC_WindowBase::init_lock");
353 cursor_timer = new Timer;
356 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
357 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
358 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
372 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
375 ButtonReleaseMask | \
376 PointerMotionMask | \
380 int BC_WindowBase::create_window(BC_WindowBase *parent_window, const char *title,
381 int x, int y, int w, int h, int minw, int minh, int allow_resize,
382 int private_color, int hide, int bg_color, const char *display_name,
383 int window_type, BC_Pixmap *bg_pixmap, int group_it)
385 XSetWindowAttributes attr;
387 XSizeHints size_hints;
390 #ifdef HAVE_LIBXXF86VM
394 id = get_resources()->get_id();
395 if(parent_window) top_level = parent_window->top_level;
396 if( top_level ) 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);
446 lock_window("BC_WindowBase::create_window 1");
448 screen = DefaultScreen(display);
449 rootwin = RootWindow(display, screen);
450 // window placement boundaries
451 if( !xinerama_screens && XineramaIsActive(display) )
452 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
453 root_w = get_root_w(0);
454 root_h = get_root_h(0);
457 vis = get_glx_visual(display);
460 vis = DefaultVisual(display, screen);
462 default_depth = DefaultDepth(display, screen);
464 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
465 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
468 // This must be done before fonts to know if antialiasing is available.
471 if(resources.use_shm < 0) resources.initialize_display(this);
472 x_correction = BC_DisplayInfo::get_left_border();
473 y_correction = BC_DisplayInfo::get_top_border();
475 // clamp window placement
476 if(this->x + this->w + x_correction > root_w)
477 this->x = root_w - this->w - x_correction;
478 if(this->y + this->h + y_correction > root_h)
479 this->y = root_h - this->h - y_correction;
480 if(this->x < 0) this->x = 0;
481 if(this->y < 0) this->y = 0;
483 if(this->bg_color == -1)
484 this->bg_color = resources.get_bg_color();
486 // printf("bcwindowbase 1 %s\n", title);
487 // if(window_type == MAIN_WINDOW) sleep(1);
488 // printf("bcwindowbase 10\n");
494 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
496 attr.event_mask = DEFAULT_EVENT_MASKS |
497 StructureNotifyMask |
501 attr.background_pixel = get_color(this->bg_color);
502 attr.colormap = cmap;
503 attr.cursor = get_cursor_struct(ARROW_CURSOR);
505 win = XCreateWindow(display, rootwin,
506 this->x, this->y, this->w, this->h, 0,
507 top_level->default_depth, InputOutput,
509 XGetNormalHints(display, win, &size_hints);
511 size_hints.flags = PSize | PMinSize | PMaxSize;
512 size_hints.width = this->w;
513 size_hints.height = this->h;
514 size_hints.min_width = allow_resize ? minw : this->w;
515 size_hints.max_width = allow_resize ? 32767 : this->w;
516 size_hints.min_height = allow_resize ? minh : this->h;
517 size_hints.max_height = allow_resize ? 32767 : this->h;
518 if(x > -BC_INFINITY && x < BC_INFINITY)
520 size_hints.flags |= PPosition;
521 size_hints.x = this->x;
522 size_hints.y = this->y;
524 XSetWMProperties(display, win, 0, 0, 0, 0, &size_hints, 0, 0);
527 #ifndef SINGLE_THREAD
528 clipboard = new BC_Clipboard(this);
529 clipboard->start_clipboard();
535 Atom ClientLeaderXAtom;
536 if (XGroupLeader == 0)
538 const char *instance_name = "cinelerra";
539 const char *class_name = "Cinelerra";
540 XClassHint *class_hints = XAllocClassHint();
541 class_hints->res_name = (char*)instance_name;
542 class_hints->res_class = (char*)class_name;
543 XSetClassHint(top_level->display, win, class_hints);
545 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
546 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
547 PropModeReplace, (unsigned char *)&XGroupLeader, true);
552 #ifdef HAVE_LIBXXF86VM
553 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
560 #ifdef HAVE_LIBXXF86VM
561 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
563 if(window_type == POPUP_WINDOW)
566 mask = CWEventMask | CWBackPixel | CWColormap |
567 CWOverrideRedirect | CWSaveUnder | CWCursor;
569 attr.event_mask = DEFAULT_EVENT_MASKS |
573 if(this->bg_color == -1)
574 this->bg_color = resources.get_bg_color();
575 attr.background_pixel = top_level->get_color(bg_color);
576 attr.colormap = top_level->cmap;
577 if(top_level->is_hourglass)
578 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
580 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
581 attr.override_redirect = True;
582 attr.save_under = True;
584 win = XCreateWindow(top_level->display,
585 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
586 top_level->default_depth, InputOutput, top_level->vis, mask,
588 top_level->add_popup(this);
591 if(window_type == SUB_WINDOW)
593 mask = CWEventMask | CWBackPixel | CWCursor;
594 attr.event_mask = DEFAULT_EVENT_MASKS;
595 attr.background_pixel = top_level->get_color(this->bg_color);
596 if(top_level->is_hourglass)
597 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
599 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
600 win = XCreateWindow(top_level->display,
601 parent_window->win, this->x, this->y, this->w, this->h, 0,
602 top_level->default_depth, InputOutput, top_level->vis, mask,
605 if(!hidden) XMapWindow(top_level->display, win);
608 // Create pixmap for all windows
609 pixmap = new BC_Pixmap(this, this->w, this->h);
611 // Set up options for main window
612 if(window_type == MAIN_WINDOW)
614 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
616 this->bg_pixmap = new BC_Pixmap(this,
617 get_resources()->bg_image,
621 if(!hidden) show_window();
625 draw_background(0, 0, this->w, this->h);
627 flash(-1, -1, -1, -1, 0);
629 // Set up options for popup window
630 #ifdef HAVE_LIBXXF86VM
631 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
633 if(window_type == POPUP_WINDOW)
637 if(!hidden) show_window();
639 get_resources()->create_window_lock->unlock();
645 Display* BC_WindowBase::init_display(const char *display_name)
649 if(display_name && display_name[0] == 0) display_name = NULL;
650 if((display = XOpenDisplay(display_name)) == NULL) {
651 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
653 if(getenv("DISPLAY") == NULL) {
654 printf(_("'DISPLAY' environment variable not set.\n"));
658 // Try again with default display.
659 if((display = XOpenDisplay(0)) == NULL) {
660 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
666 static int xsynch = -1;
668 const char *cp = getenv("CIN_XSYNCH");
669 xsynch = !cp ? 0 : atoi(cp);
672 XSynchronize(display, True);
677 Display* BC_WindowBase::get_display()
679 return top_level->display;
682 int BC_WindowBase::get_screen()
684 return top_level->screen;
687 int BC_WindowBase::run_window()
693 // Events may have been sent before run_window so can't initialize them here.
696 set_repeat(get_resources()->tooltip_delay);
697 BC_Display::display_global->new_window(this);
699 // If the first window created, run the display loop in this thread.
700 if(BC_Display::display_global->is_first(this))
702 BC_Display::unlock_display();
703 BC_Display::display_global->loop();
707 BC_Display::unlock_display();
708 completion_lock->lock("BC_WindowBase::run_window");
711 BC_Display::lock_display("BC_WindowBase::run_window");
712 BC_Display::display_global->delete_window(this);
714 unset_all_repeaters();
716 BC_Display::unlock_display();
718 #else // SINGLE_THREAD
723 set_repeat(get_resources()->tooltip_delay);
725 // Start X server events
726 event_thread = new BC_WindowEvents(this);
727 event_thread->start();
733 // Handle common events
739 unset_all_repeaters();
743 event_condition->reset();
744 common_events.remove_all_objects();
748 #endif // SINGLE_THREAD
753 int BC_WindowBase::get_key_masks(unsigned int key_state)
755 // printf("BC_WindowBase::get_key_masks %llx\n",
756 // event->xkey.state);
757 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
758 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
759 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
764 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
766 BC_KeyboardHandlerLock set;
767 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
770 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
772 BC_KeyboardHandlerLock set;
773 int i = BC_KeyboardHandler::listeners.size();
774 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
775 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
778 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
780 int result = (win->*handler)(wp);
784 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
787 BC_KeyboardHandlerLock set;
788 for( int i=0; !result && i<listeners.size(); ++i ) {
789 BC_KeyboardHandler *listener = listeners[i];
790 result = listener->run_event(wp);
795 void BC_KeyboardHandler::kill_grabs()
797 BC_KeyboardHandlerLock set;
798 for( int i=0; i<listeners.size(); ++i ) {
799 BC_WindowBase *win = listeners[i]->win;
800 if( win->get_window_type() != POPUP_WINDOW ) continue;
801 ((BC_Popup *)win)->ungrab_keyboard();
805 void BC_ActiveBitmaps::reque(XEvent *event)
807 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
808 ShmSeg shmseg = shm_ev->shmseg;
809 Drawable drawable = shm_ev->drawable;
810 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
811 active_lock.lock("BC_BitmapImage::reque");
812 BC_BitmapImage *bfr = first;
813 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
814 if( bfr && bfr->drawable == drawable )
816 active_lock.unlock();
818 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
819 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
820 // (int)shmseg, (int)drawable);
823 if( bfr->drawable != drawable ) return;
824 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
825 bfr->bitmap->reque(bfr);
828 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
830 active_lock.lock("BC_BitmapImage::insert");
831 bfr->drawable = pixmap;
833 active_lock.unlock();
836 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
838 active_lock.lock("BC_ActiveBitmaps::remove");
839 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
841 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
842 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
844 active_lock.unlock();
847 BC_ActiveBitmaps::BC_ActiveBitmaps()
851 BC_ActiveBitmaps::~BC_ActiveBitmaps()
857 int BC_WindowBase::keysym_lookup(XEvent *event)
859 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
860 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
862 if( event->xany.send_event && !event->xany.serial ) {
863 keysym = (KeySym) event->xkey.keycode;
864 keys_return[0] = keysym;
867 wkey_string_length = 0;
869 if( input_context ) {
870 wkey_string_length = XwcLookupString(input_context,
871 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
872 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
873 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
876 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
877 keys_return, KEYPRESSLEN, &keysym, &stat);
878 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
879 if( stat == XLookupBoth ) return ret;
880 if( stat == XLookupKeySym ) return 0;
882 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
883 wkey_string_length = ret;
884 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
888 pthread_t locking_task = (pthread_t)-1L;
889 int locking_event = -1;
890 int locking_message = -1;
892 int BC_WindowBase::dispatch_event(XEvent *event)
896 XClientMessageEvent *ptr;
897 int cancel_resize, cancel_translation;
902 #ifndef SINGLE_THREAD
903 // If an event is waiting get it, otherwise
904 // wait for next event only if there are no compressed events.
905 if(get_event_count() ||
906 (!motion_events && !resize_events && !translation_events))
909 // Lock out window deletions
910 lock_window("BC_WindowBase::dispatch_event 1");
911 locking_event = event->type;
912 locking_task = pthread_self();
913 locking_message = event->xclient.message_type;
916 // Handle compressed events
918 lock_window("BC_WindowBase::dispatch_event 2");
920 dispatch_resize_event(last_resize_w, last_resize_h);
922 dispatch_motion_event();
923 if(translation_events)
924 dispatch_translation_event();
935 //static const char *event_names[] = {
936 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
937 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
938 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
939 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
940 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
941 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
942 // "GenericEvent", "LASTEvent",
946 //if(event->type != ClientMessage) {
947 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
948 //__LINE__, title, event, event->type,
949 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
950 // event_names[event->type] : "Unknown");
955 active_grab->lock_window("BC_WindowBase::dispatch_event 3");
956 result = active_grab->grab_event(event);
957 active_grab->unlock_window();
958 if( result ) return result;
959 lock_window("BC_WindowBase::dispatch_event 4");
962 switch(event->type) {
964 // Clear the resize buffer
965 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
966 // Clear the motion buffer since this can clear the window
969 dispatch_motion_event();
972 ptr = (XClientMessageEvent*)event;
975 if(ptr->message_type == ProtoXAtom &&
976 (Atom)ptr->data.l[0] == DelWinXAtom)
981 if(ptr->message_type == RepeaterXAtom)
983 dispatch_repeat_event(ptr->data.l[0]);
984 // Make sure the repeater still exists.
985 // for(int i = 0; i < repeaters.total; i++)
987 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
989 // dispatch_repeat_event_master(ptr->data.l[0]);
995 if(ptr->message_type == SetDoneXAtom)
999 { // We currently use X marshalling for xatom events, we can switch to something else later
1000 receive_custom_xatoms((xatom_event *)ptr);
1006 dispatch_focus_in();
1011 dispatch_focus_out();
1025 dispatch_motion_event();
1027 get_key_masks(event->xbutton.state);
1028 cursor_x = event->xbutton.x;
1029 cursor_y = event->xbutton.y;
1030 button_number = event->xbutton.button;
1032 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1033 event_win = event->xany.window;
1034 if (button_number < 6)
1036 if(button_number < 4)
1038 button_pressed = event->xbutton.button;
1039 button_time1 = button_time2;
1040 button_time2 = button_time3;
1041 button_time3 = event->xbutton.time;
1044 drag_win = event_win;
1045 drag_x1 = cursor_x - get_resources()->drag_radius;
1046 drag_x2 = cursor_x + get_resources()->drag_radius;
1047 drag_y1 = cursor_y - get_resources()->drag_radius;
1048 drag_y2 = cursor_y + get_resources()->drag_radius;
1050 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1053 button_time3 = button_time2 = button_time1 = 0;
1055 if((long)(button_time3 - button_time2) < resources.double_click)
1058 // button_time3 = button_time2 = button_time1 = 0;
1066 dispatch_button_press();
1073 dispatch_motion_event();
1075 get_key_masks(event->xbutton.state);
1076 button_number = event->xbutton.button;
1077 event_win = event->xany.window;
1078 if (button_number < 6)
1080 if(button_number < 4)
1082 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1084 dispatch_button_release();
1089 event_win = event->xany.window;
1090 dispatch_expose_event();
1094 get_key_masks(event->xmotion.state);
1095 // Dispatch previous motion event if this is a subsequent motion from a different window
1096 if(motion_events && last_motion_win != event->xany.window)
1098 dispatch_motion_event();
1101 // Buffer the current motion
1103 last_motion_state = event->xmotion.state;
1104 last_motion_x = event->xmotion.x;
1105 last_motion_y = event->xmotion.y;
1106 last_motion_win = event->xany.window;
1109 case ConfigureNotify:
1110 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1112 // event->xany.window,
1115 XTranslateCoordinates(top_level->display,
1123 last_resize_w = event->xconfigure.width;
1124 last_resize_h = event->xconfigure.height;
1127 cancel_translation = 0;
1129 // Resize history prevents responses to recursive resize requests
1130 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1132 if(resize_history.values[i]->w == last_resize_w &&
1133 resize_history.values[i]->h == last_resize_h)
1135 delete resize_history.values[i];
1136 resize_history.remove_number(i);
1141 if(last_resize_w == w && last_resize_h == h)
1149 if((last_translate_x == x && last_translate_y == y))
1150 cancel_translation = 1;
1152 if(!cancel_translation)
1154 translation_events = 1;
1157 translation_count++;
1161 get_key_masks(event->xkey.state);
1162 keys_return[0] = 0; keysym = -1;
1163 if(XFilterEvent(event, win)) {
1166 if( keysym_lookup(event) < 0 ) {
1167 printf("keysym %x\n", (uint32_t)keysym);
1171 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1175 // block out control keys
1176 if(keysym > 0xffe0 && keysym < 0xffff) break;
1177 // block out Alt_GR key
1178 if(keysym == 0xfe03) break;
1181 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1183 #ifdef X_HAVE_UTF8_STRING
1184 //It's Ascii or UTF8?
1185 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1186 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1188 if( ((keys_return[1] & 0xff) > 0x80) &&
1189 ((keys_return[0] & 0xff) > 0xC0) ) {
1190 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1191 key_pressed = keysym & 0xff;
1197 // block out extra keys
1207 // Translate key codes
1208 case XK_Return: key_pressed = RETURN; break;
1209 case XK_Up: key_pressed = UP; break;
1210 case XK_Down: key_pressed = DOWN; break;
1211 case XK_Left: key_pressed = LEFT; break;
1212 case XK_Right: key_pressed = RIGHT; break;
1213 case XK_Next: key_pressed = PGDN; break;
1214 case XK_Prior: key_pressed = PGUP; break;
1215 case XK_BackSpace: key_pressed = BACKSPACE; break;
1216 case XK_Escape: key_pressed = ESC; break;
1219 key_pressed = LEFTTAB;
1223 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1224 case XK_underscore: key_pressed = '_'; break;
1225 case XK_asciitilde: key_pressed = '~'; break;
1226 case XK_Delete: key_pressed = DELETE; break;
1227 case XK_Home: key_pressed = HOME; break;
1228 case XK_End: key_pressed = END; break;
1231 case XK_KP_Enter: key_pressed = KPENTER; break;
1232 case XK_KP_Add: key_pressed = KPPLUS; break;
1233 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1234 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1235 case XK_KP_Divide: key_pressed = KPSLASH; break;
1237 case XK_KP_End: key_pressed = KP1; break;
1239 case XK_KP_Down: key_pressed = KP2; break;
1241 case XK_KP_Page_Down: key_pressed = KP3; break;
1243 case XK_KP_Left: key_pressed = KP4; break;
1245 case XK_KP_Begin: key_pressed = KP5; break;
1247 case XK_KP_Right: key_pressed = KP6; break;
1249 case XK_KP_Home: key_pressed = KP7; break;
1251 case XK_KP_Up: key_pressed = KP8; break;
1253 case XK_KP_Page_Up: key_pressed = KP9; break;
1255 case XK_KP_Insert: key_pressed = KPINS; break;
1257 case XK_KP_Delete: key_pressed = KPDEL; break;
1259 case XK_F1: key_pressed = KEY_F1; break;
1260 case XK_F2: key_pressed = KEY_F2; break;
1261 case XK_F3: key_pressed = KEY_F3; break;
1262 case XK_F4: key_pressed = KEY_F4; break;
1263 case XK_F5: key_pressed = KEY_F5; break;
1264 case XK_F6: key_pressed = KEY_F6; break;
1265 case XK_F7: key_pressed = KEY_F7; break;
1266 case XK_F8: key_pressed = KEY_F8; break;
1267 case XK_F9: key_pressed = KEY_F9; break;
1268 case XK_F10: key_pressed = KEY_F10; break;
1269 case XK_F11: key_pressed = KEY_F11; break;
1270 case XK_F12: key_pressed = KEY_F12; break;
1272 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1274 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1275 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1276 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1277 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1278 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1279 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1280 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1281 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1282 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1283 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1284 // above case XK_Left: key_pressed = LEFT; break; /* left */
1285 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1286 // above case XK_Down: key_pressed = DOWN; break; /* down */
1287 // above case XK_Up: key_pressed = UP; break; /* up */
1288 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1289 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1290 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1291 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1292 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1293 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1294 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1295 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1296 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1297 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1298 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1299 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1302 key_pressed = keysym & 0xff;
1303 #ifdef X_HAVE_UTF8_STRING
1304 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1309 #ifdef X_HAVE_UTF8_STRING
1311 key_pressed_utf8 = keys_return;
1316 if( top_level == this )
1317 result = BC_KeyboardHandler::run_listeners(this);
1319 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1321 result = dispatch_keypress_event();
1322 // Handle some default keypresses
1325 if(key_pressed == 'w' ||
1334 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1335 dispatch_keyrelease_event();
1336 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1337 // keysym, event->xkey.state);
1341 if( event->xcrossing.mode != NotifyNormal ) break;
1342 if( cursor_entered && event->xcrossing.window == win ) {
1345 event_win = event->xany.window;
1346 dispatch_cursor_leave();
1350 if( event->xcrossing.mode != NotifyNormal ) break;
1351 if( !cursor_entered && event->xcrossing.window == win ) {
1352 if( !event->xcrossing.focus && get_resources()->grab_input_focus ) {
1353 XSetInputFocus(display, win, RevertToParent, CurrentTime);
1357 event_win = event->xany.window;
1358 cursor_x = event->xcrossing.x;
1359 cursor_y = event->xcrossing.y;
1360 dispatch_cursor_enter();
1366 //printf("100 %s %p %d\n", title, event, event->type);
1367 //if(event->type != ClientMessage) dump();
1369 #ifndef SINGLE_THREAD
1372 if( resend_event_window ) {
1373 resend_event_window->put_event(event);
1374 resend_event_window = 0;
1380 // if(done) completion_lock->unlock();
1383 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1387 int BC_WindowBase::dispatch_expose_event()
1390 for(int i = 0; i < subwindows->total && !result; i++)
1392 result = subwindows->values[i]->dispatch_expose_event();
1395 // Propagate to user
1396 if(!result) expose_event();
1400 int BC_WindowBase::dispatch_resize_event(int w, int h)
1402 // Can't store new w and h until the event is handles
1403 // because bcfilebox depends on the old w and h to
1404 // reposition widgets.
1405 if( window_type == MAIN_WINDOW ) {
1410 pixmap = new BC_Pixmap(this, w, h);
1411 clear_box(0, 0, w, h);
1414 // Propagate to subwindows
1415 for(int i = 0; i < subwindows->total; i++) {
1416 subwindows->values[i]->dispatch_resize_event(w, h);
1419 // Propagate to user
1422 if( window_type == MAIN_WINDOW ) {
1431 int BC_WindowBase::dispatch_flash()
1434 for(int i = 0; i < subwindows->total; i++)
1435 subwindows->values[i]->dispatch_flash();
1439 int BC_WindowBase::dispatch_translation_event()
1441 translation_events = 0;
1442 if(window_type == MAIN_WINDOW)
1446 x = last_translate_x;
1447 y = last_translate_y;
1448 // Correct for window manager offsets
1453 for(int i = 0; i < subwindows->total; i++)
1455 subwindows->values[i]->dispatch_translation_event();
1458 translation_event();
1462 int BC_WindowBase::dispatch_motion_event()
1467 if(top_level == this)
1470 event_win = last_motion_win;
1471 get_key_masks(last_motion_state);
1474 if(get_button_down() && !active_menubar && !active_popup_menu)
1478 cursor_x = last_motion_x;
1479 cursor_y = last_motion_y;
1480 result = dispatch_drag_motion();
1484 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1485 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1490 result = dispatch_drag_start();
1494 cursor_x = last_motion_x;
1495 cursor_y = last_motion_y;
1497 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1500 // active_popup_menu,
1501 // active_subwindow);
1503 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1504 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1505 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1508 // Dispatch in stacking order
1509 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1511 result = subwindows->values[i]->dispatch_motion_event();
1514 if(!result) result = cursor_motion_event(); // give to user
1518 int BC_WindowBase::dispatch_keypress_event()
1521 if(top_level == this)
1523 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1526 for(int i = 0; i < subwindows->total && !result; i++)
1528 result = subwindows->values[i]->dispatch_keypress_event();
1531 if(!result) result = keypress_event();
1536 int BC_WindowBase::dispatch_keyrelease_event()
1539 if(top_level == this)
1541 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1544 for(int i = 0; i < subwindows->total && !result; i++)
1546 result = subwindows->values[i]->dispatch_keyrelease_event();
1549 if(!result) result = keyrelease_event();
1554 int BC_WindowBase::dispatch_focus_in()
1556 for(int i = 0; i < subwindows->total; i++)
1558 subwindows->values[i]->dispatch_focus_in();
1566 int BC_WindowBase::dispatch_focus_out()
1568 for(int i = 0; i < subwindows->total; i++)
1570 subwindows->values[i]->dispatch_focus_out();
1578 int BC_WindowBase::get_has_focus()
1580 return top_level->has_focus;
1583 int BC_WindowBase::get_deleting()
1585 if(is_deleting) return 1;
1586 if(parent_window && parent_window->get_deleting()) return 1;
1590 int BC_WindowBase::dispatch_button_press()
1595 if(top_level == this)
1597 if(active_menubar) result = active_menubar->dispatch_button_press();
1598 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1599 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1602 for(int i = 0; i < subwindows->total && !result; i++)
1604 result = subwindows->values[i]->dispatch_button_press();
1607 if(!result) result = button_press_event();
1613 int BC_WindowBase::dispatch_button_release()
1616 if(top_level == this)
1618 if(active_menubar) result = active_menubar->dispatch_button_release();
1619 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1620 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1621 if(!result && button_number != 4 && button_number != 5)
1622 result = dispatch_drag_stop();
1625 for(int i = 0; i < subwindows->total && !result; i++)
1627 result = subwindows->values[i]->dispatch_button_release();
1632 result = button_release_event();
1639 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1642 // all repeat event handlers get called and decide based on activity and duration
1643 // whether to respond
1644 for(int i = 0; i < subwindows->total; i++)
1646 subwindows->values[i]->dispatch_repeat_event(duration);
1650 repeat_event(duration);
1654 // Unlock next repeat signal
1655 if(window_type == MAIN_WINDOW)
1657 #ifdef SINGLE_THREAD
1658 BC_Display::display_global->unlock_repeaters(duration);
1660 for(int i = 0; i < repeaters.total; i++)
1662 if(repeaters.values[i]->delay == duration)
1664 repeaters.values[i]->repeat_lock->unlock();
1672 void BC_WindowBase::unhide_cursor()
1677 if(top_level->is_hourglass)
1678 set_cursor(HOURGLASS_CURSOR, 1, 0);
1680 set_cursor(current_cursor, 1, 0);
1682 cursor_timer->update();
1686 void BC_WindowBase::update_video_cursor()
1688 if(video_on && !is_transparent)
1690 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1693 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1694 cursor_timer->update();
1699 cursor_timer->update();
1704 int BC_WindowBase::dispatch_cursor_leave()
1708 for(int i = 0; i < subwindows->total; i++)
1710 subwindows->values[i]->dispatch_cursor_leave();
1713 cursor_leave_event();
1717 int BC_WindowBase::dispatch_cursor_enter()
1723 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1724 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1725 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1727 for(int i = 0; !result && i < subwindows->total; i++)
1729 result = subwindows->values[i]->dispatch_cursor_enter();
1732 if(!result) result = cursor_enter_event();
1736 int BC_WindowBase::cursor_enter_event()
1741 int BC_WindowBase::cursor_leave_event()
1746 int BC_WindowBase::close_event()
1752 int BC_WindowBase::dispatch_drag_start()
1755 if(active_menubar) result = active_menubar->dispatch_drag_start();
1756 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1757 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1759 for(int i = 0; i < subwindows->total && !result; i++)
1761 result = subwindows->values[i]->dispatch_drag_start();
1764 if(!result) result = is_dragging = drag_start_event();
1768 int BC_WindowBase::dispatch_drag_stop()
1772 for(int i = 0; i < subwindows->total && !result; i++)
1774 result = subwindows->values[i]->dispatch_drag_stop();
1777 if(is_dragging && !result)
1787 int BC_WindowBase::dispatch_drag_motion()
1790 for(int i = 0; i < subwindows->total && !result; i++)
1792 result = subwindows->values[i]->dispatch_drag_motion();
1795 if(is_dragging && !result)
1797 drag_motion_event();
1805 int BC_WindowBase::show_tooltip(const char *text, int x, int y, int w, int h)
1808 int forced = !text ? force_tooltip : 1;
1809 if( !text ) text = tooltip_text;
1810 if( !text || (!forced && !get_resources()->tooltips_enabled) ) {
1811 top_level->hide_tooltip();
1815 if(w < 0) w = get_text_width(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1816 if(h < 0) h = get_text_height(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1817 // default x,y (win relative)
1818 if( x < 0 ) x = get_w();
1819 if( y < 0 ) y = get_h();
1821 get_root_coordinates(x, y, &wx, &wy);
1822 // keep the tip inside the window/display
1823 int x0 = top_level->get_x(), x1 = x0 + top_level->get_w();
1824 int x2 = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1825 if( x1 > x2 ) x1 = x2;
1826 if( wx < x0 ) wx = x0;
1827 if( wx >= (x1-=w) ) wx = x1;
1828 int y0 = top_level->get_y(), y1 = y0 + top_level->get_h();
1829 int y2 = top_level->get_root_h(0);
1830 if( y1 > y2 ) y1 = y2;
1831 if( wy < y0 ) wy = y0;
1832 if( wy >= (y1-=h) ) wy = y1;
1833 // avoid tip under cursor (flickers)
1835 get_abs_cursor(abs_x,abs_y, 0);
1836 if( wx < abs_x && abs_x < wx+w && wy < abs_y && abs_y < wy+h ) {
1837 if( wx-abs_x < wy-abs_y )
1844 tooltip_popup = new BC_Popup(top_level, wx, wy, w, h,
1845 get_resources()->tooltip_bg_color);
1848 tooltip_popup->reposition_window(wx, wy, w, h);
1851 tooltip_popup->flash();
1852 tooltip_popup->flush();
1856 int BC_WindowBase::hide_tooltip()
1859 for(int i = 0; i < subwindows->total; i++)
1861 subwindows->values[i]->hide_tooltip();
1867 delete tooltip_popup;
1873 const char *BC_WindowBase::get_tooltip()
1875 return tooltip_text;
1878 int BC_WindowBase::set_tooltip(const char *text)
1880 tooltip_text = text;
1882 // Update existing tooltip if it is visible
1886 tooltip_popup->flash();
1890 // signal the event handler to repeat
1891 int BC_WindowBase::set_repeat(int64_t duration)
1895 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1898 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1900 #ifdef SINGLE_THREAD
1901 BC_Display::display_global->set_repeat(this, duration);
1903 // test repeater database for duplicates
1904 for(int i = 0; i < repeaters.total; i++)
1907 if(repeaters.values[i]->delay == duration)
1909 repeaters.values[i]->start_repeating(this);
1914 BC_Repeater *repeater = new BC_Repeater(this, duration);
1915 repeater->initialize();
1916 repeaters.append(repeater);
1917 repeater->start_repeating();
1922 int BC_WindowBase::unset_repeat(int64_t duration)
1924 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1926 #ifdef SINGLE_THREAD
1927 BC_Display::display_global->unset_repeat(this, duration);
1929 for(int i = 0; i < repeaters.total; i++)
1931 if(repeaters.values[i]->delay == duration)
1933 repeaters.values[i]->stop_repeating();
1941 int BC_WindowBase::unset_all_repeaters()
1943 #ifdef SINGLE_THREAD
1944 BC_Display::display_global->unset_all_repeaters(this);
1946 for(int i = 0; i < repeaters.total; i++)
1948 repeaters.values[i]->stop_repeating();
1950 repeaters.remove_all_objects();
1955 // long BC_WindowBase::get_repeat_id()
1957 // return top_level->next_repeat_id++;
1960 XEvent *BC_WindowBase::new_xevent()
1962 XEvent *event = new XEvent;
1963 memset(event, 0, sizeof(*event));
1967 #ifndef SINGLE_THREAD
1968 int BC_WindowBase::arm_repeat(int64_t duration)
1970 XEvent *event = new_xevent();
1971 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1972 ptr->type = ClientMessage;
1973 ptr->message_type = RepeaterXAtom;
1975 ptr->data.l[0] = duration;
1977 // Couldn't use XSendEvent since it locked up randomly.
1983 int BC_WindowBase::receive_custom_xatoms(xatom_event *event)
1988 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1990 #ifndef SINGLE_THREAD
1991 XEvent *myevent = new_xevent();
1992 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1993 ptr->type = ClientMessage;
1994 ptr->message_type = event->message_type;
1995 ptr->format = event->format;
1996 ptr->data.l[0] = event->data.l[0];
1997 ptr->data.l[1] = event->data.l[1];
1998 ptr->data.l[2] = event->data.l[2];
1999 ptr->data.l[3] = event->data.l[3];
2000 ptr->data.l[4] = event->data.l[4];
2009 Atom BC_WindowBase::create_xatom(const char *atom_name)
2011 return XInternAtom(display, atom_name, False);
2014 int BC_WindowBase::get_atoms()
2016 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
2017 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
2018 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
2019 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
2020 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
2021 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
2022 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2028 void BC_WindowBase::init_cursors()
2030 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2031 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2032 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2033 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2034 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2035 move_cursor = XCreateFontCursor(display, XC_fleur);
2036 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2037 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2038 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2039 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2040 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2041 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2042 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2043 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2044 grabbed_cursor = create_grab_cursor();
2046 static char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2047 Colormap colormap = DefaultColormap(display, screen);
2048 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2049 rootwin, cursor_data, 8, 8);
2050 XColor black, dummy;
2051 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2052 transparent_cursor = XCreatePixmapCursor(display,
2053 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2054 // XDefineCursor(display, win, transparent_cursor);
2055 XFreePixmap(display, pixmap_bottom);
2058 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2060 int color_model = BC_TRANSPARENCY;
2064 color_model = BC_RGB8;
2067 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2070 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2073 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2079 int BC_WindowBase::init_colors()
2082 current_color_value = current_color_pixel = 0;
2084 // Get the real depth
2087 ximage = XCreateImage(top_level->display,
2089 top_level->default_depth,
2097 bits_per_pixel = ximage->bits_per_pixel;
2098 XDestroyImage(ximage);
2100 color_model = evaluate_color_model(client_byte_order,
2103 // Get the color model
2109 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2110 create_private_colors();
2114 cmap = DefaultColormap(display, screen);
2115 create_shared_colors();
2118 allocate_color_table();
2122 //cmap = DefaultColormap(display, screen);
2123 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2129 int BC_WindowBase::create_private_colors()
2134 for(int i = 0; i < 255; i++)
2136 color = (i & 0xc0) << 16;
2137 color += (i & 0x38) << 10;
2138 color += (i & 0x7) << 5;
2139 color_table[i][0] = color;
2141 create_shared_colors(); // overwrite the necessary colors on the table
2146 int BC_WindowBase::create_color(int color)
2148 if(total_colors == 256)
2150 // replace the closest match with an exact match
2151 color_table[get_color_rgb8(color)][0] = color;
2155 // add the color to the table
2156 color_table[total_colors][0] = color;
2162 int BC_WindowBase::create_shared_colors()
2164 create_color(BLACK);
2165 create_color(WHITE);
2167 create_color(LTGREY);
2168 create_color(MEGREY);
2169 create_color(MDGREY);
2170 create_color(DKGREY);
2172 create_color(LTCYAN);
2173 create_color(MECYAN);
2174 create_color(MDCYAN);
2175 create_color(DKCYAN);
2177 create_color(LTGREEN);
2178 create_color(GREEN);
2179 create_color(DKGREEN);
2181 create_color(LTPINK);
2185 create_color(LTBLUE);
2187 create_color(DKBLUE);
2189 create_color(LTYELLOW);
2190 create_color(MEYELLOW);
2191 create_color(MDYELLOW);
2192 create_color(DKYELLOW);
2194 create_color(LTPURPLE);
2195 create_color(MEPURPLE);
2196 create_color(MDPURPLE);
2197 create_color(DKPURPLE);
2199 create_color(FGGREY);
2200 create_color(MNBLUE);
2201 create_color(ORANGE);
2202 create_color(FTGREY);
2207 Cursor BC_WindowBase::create_grab_cursor()
2209 int iw = 23, iw1 = iw-1, iw2 = iw/2;
2210 int ih = 23, ih1 = ih-1, ih2 = ih/2;
2211 VFrame grab(iw,ih,BC_RGB888);
2213 grab.set_pixel_color(RED); // fg
2214 grab.draw_smooth(iw2,0, iw1,0, iw1,ih2);
2215 grab.draw_smooth(iw1,ih2, iw1,ih1, iw2,ih1);
2216 grab.draw_smooth(iw2,ih1, 0,ih1, 0,ih2);
2217 grab.draw_smooth(0,ih2, 0,0, iw2,0);
2218 grab.set_pixel_color(WHITE); // bg
2219 grab.draw_line(0,ih2, iw2-2,ih2);
2220 grab.draw_line(iw2+2,ih2, iw1,ih2);
2221 grab.draw_line(iw2,0, iw2,ih2-2);
2222 grab.draw_line(iw2,ih2+2, iw2,ih1);
2224 int bpl = (iw+7)/8, isz = bpl * ih;
2225 char img[isz]; memset(img, 0, isz);
2226 char msk[isz]; memset(msk, 0, isz);
2227 unsigned char **rows = grab.get_rows();
2228 for( int iy=0; iy<ih; ++iy ) {
2229 char *op = img + iy*bpl;
2230 char *mp = msk + iy*bpl;
2231 unsigned char *ip = rows[iy];
2232 for( int ix=0; ix<iw; ++ix,ip+=3 ) {
2233 if( ip[0] ) mp[ix>>3] |= (1<<(ix&7));
2234 if( !ip[1] ) op[ix>>3] |= (1<<(ix&7));
2237 unsigned long white_pix = WhitePixel(display, screen);
2238 unsigned long black_pix = BlackPixel(display, screen);
2239 Pixmap img_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2240 img, iw,ih, white_pix,black_pix, 1);
2241 Pixmap msk_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2242 msk, iw,ih, white_pix,black_pix, 1);
2245 fc.flags = bc.flags = DoRed | DoGreen | DoBlue;
2246 fc.red = 0xffff; fc.green = fc.blue = 0; // fg
2247 bc.red = bc.green = bc.blue = 0x0000; // bg
2248 Cursor cursor = XCreatePixmapCursor(display, img_xpm,msk_xpm, &fc,&bc, iw2,ih2);
2249 XFreePixmap(display, img_xpm);
2250 XFreePixmap(display, msk_xpm);
2254 int BC_WindowBase::allocate_color_table()
2256 int red, green, blue, color;
2259 for(int i = 0; i < total_colors; i++)
2261 color = color_table[i][0];
2262 red = (color & 0xFF0000) >> 16;
2263 green = (color & 0x00FF00) >> 8;
2264 blue = color & 0xFF;
2266 col.flags = DoRed | DoGreen | DoBlue;
2267 col.red = red<<8 | red;
2268 col.green = green<<8 | green;
2269 col.blue = blue<<8 | blue;
2271 XAllocColor(display, cmap, &col);
2272 color_table[i][1] = col.pixel;
2275 XInstallColormap(display, cmap);
2279 int BC_WindowBase::init_window_shape()
2281 if(bg_pixmap && bg_pixmap->use_alpha())
2283 XShapeCombineMask(top_level->display,
2284 this->win, ShapeBounding, 0, 0,
2285 bg_pixmap->get_alpha(), ShapeSet);
2291 int BC_WindowBase::init_gc()
2293 unsigned long gcmask;
2294 gcmask = GCFont | GCGraphicsExposures;
2297 gcvalues.font = mediumfont->fid; // set the font
2298 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2299 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2301 // gcmask = GCCapStyle | GCJoinStyle;
2302 // XGetGCValues(display, gc, gcmask, &gcvalues);
2303 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2307 int BC_WindowBase::init_fonts()
2309 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2310 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2311 smallfont = XLoadQueryFont(display, "fixed");
2312 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2313 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2314 mediumfont = XLoadQueryFont(display, "fixed");
2315 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2316 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2317 largefont = XLoadQueryFont(display, "fixed");
2318 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2319 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2320 bigfont = XLoadQueryFont(display, "fixed");
2322 if((clockfont = XLoadQueryFont(display, _(resources.clock_font))) == NULL)
2323 if((clockfont = XLoadQueryFont(display, _(resources.clock_font2))) == NULL)
2324 clockfont = XLoadQueryFont(display, "fixed");
2327 if(get_resources()->use_fontset)
2332 // FIXME: should check the m,d,n values
2333 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2335 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2336 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2337 if( !mediumfontset )
2338 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2339 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2341 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2342 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2344 bigfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2345 clockfontset = XCreateFontSet(display, resources.clock_fontset, &m, &n, &d);
2347 clockfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2348 if(clockfontset && bigfontset && largefontset && mediumfontset && smallfontset) {
2349 curr_fontset = mediumfontset;
2350 get_resources()->use_fontset = 1;
2354 get_resources()->use_fontset = 0;
2361 void BC_WindowBase::init_xft()
2364 if( !get_resources()->use_xft ) return;
2365 // apparently, xft is not reentrant, more than this is needed
2366 static Mutex xft_init_lock("BC_WindowBase::xft_init_lock", 0);
2367 xft_init_lock.lock("BC_WindowBase::init_xft");
2368 if(!(smallfont_xft =
2369 (resources.small_font_xft[0] == '-' ?
2370 xftFontOpenXlfd(display, screen, resources.small_font_xft) :
2371 xftFontOpenName(display, screen, resources.small_font_xft))) )
2372 if(!(smallfont_xft =
2373 xftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2374 smallfont_xft = xftFontOpenXlfd(display, screen, "fixed");
2375 if(!(mediumfont_xft =
2376 (resources.medium_font_xft[0] == '-' ?
2377 xftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2378 xftFontOpenName(display, screen, resources.medium_font_xft))) )
2379 if(!(mediumfont_xft =
2380 xftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2381 mediumfont_xft = xftFontOpenXlfd(display, screen, "fixed");
2382 if(!(largefont_xft =
2383 (resources.large_font_xft[0] == '-' ?
2384 xftFontOpenXlfd(display, screen, resources.large_font_xft) :
2385 xftFontOpenName(display, screen, resources.large_font_xft))) )
2386 if(!(largefont_xft =
2387 xftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2388 largefont_xft = xftFontOpenXlfd(display, screen, "fixed");
2390 (resources.big_font_xft[0] == '-' ?
2391 xftFontOpenXlfd(display, screen, resources.big_font_xft) :
2392 xftFontOpenName(display, screen, resources.big_font_xft))) )
2394 xftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2395 bigfont_xft = xftFontOpenXlfd(display, screen, "fixed");
2396 if(!(clockfont_xft =
2397 (resources.clock_font_xft[0] == '-' ?
2398 xftFontOpenXlfd(display, screen, resources.clock_font_xft) :
2399 xftFontOpenName(display, screen, resources.clock_font_xft))) )
2400 clockfont_xft = xftFontOpenXlfd(display, screen, "fixed");
2403 if(!(bold_smallfont_xft =
2404 (resources.small_b_font_xft[0] == '-' ?
2405 xftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2406 xftFontOpenName(display, screen, resources.small_b_font_xft))) )
2407 bold_smallfont_xft = xftFontOpenXlfd(display, screen, "fixed");
2408 if(!(bold_mediumfont_xft =
2409 (resources.medium_b_font_xft[0] == '-' ?
2410 xftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2411 xftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2412 bold_mediumfont_xft = xftFontOpenXlfd(display, screen, "fixed");
2413 if(!(bold_largefont_xft =
2414 (resources.large_b_font_xft[0] == '-' ?
2415 xftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2416 xftFontOpenName(display, screen, resources.large_b_font_xft))) )
2417 bold_largefont_xft = xftFontOpenXlfd(display, screen, "fixed");
2419 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2420 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ||
2422 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2423 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2424 resources.small_font_xft, smallfont_xft,
2425 resources.medium_font_xft, mediumfont_xft,
2426 resources.large_font_xft, largefont_xft,
2427 resources.big_font_xft, bigfont_xft,
2428 resources.clock_font_xft, clockfont_xft,
2429 resources.small_b_font_xft, bold_smallfont_xft,
2430 resources.medium_b_font_xft, bold_mediumfont_xft,
2431 resources.large_b_font_xft, bold_largefont_xft);
2432 get_resources()->use_xft = 0;
2435 // _XftDisplayInfo needs a lock.
2436 xftDefaultHasRender(display);
2437 xft_init_lock.unlock();
2441 void BC_WindowBase::init_im()
2443 XIMStyles *xim_styles;
2446 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2448 printf("BC_WindowBase::init_im: Could not open input method.\n");
2451 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2454 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2455 XCloseIM(input_method);
2460 for(int z = 0; z < xim_styles->count_styles; z++)
2462 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2464 xim_style = xim_styles->supported_styles[z];
2472 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2473 XCloseIM(input_method);
2477 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2478 XNClientWindow, win, XNFocusWindow, win, NULL);
2481 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2482 XCloseIM(input_method);
2487 void BC_WindowBase::finit_im()
2489 if( input_context ) {
2490 XDestroyIC(input_context);
2493 if( input_method ) {
2494 XCloseIM(input_method);
2500 int BC_WindowBase::get_color(int64_t color)
2502 // return pixel of color
2503 // use this only for drawing subwindows not for bitmaps
2504 int i, test, difference;
2510 return get_color_rgb8(color);
2511 // test last color looked up
2512 if(current_color_value == color)
2513 return current_color_pixel;
2516 current_color_value = color;
2517 for(i = 0; i < total_colors; i++)
2519 if(color_table[i][0] == color)
2521 current_color_pixel = color_table[i][1];
2522 return current_color_pixel;
2526 // find nearest match
2527 difference = 0xFFFFFF;
2529 for(i = 0; i < total_colors; i++)
2531 test = abs((int)(color_table[i][0] - color));
2533 if(test < difference)
2535 current_color_pixel = color_table[i][1];
2539 return current_color_pixel;
2542 return get_color_rgb16(color);
2545 return get_color_bgr16(color);
2549 return client_byte_order == server_byte_order ?
2550 color : get_color_bgr24(color);
2558 int BC_WindowBase::get_color_rgb8(int color)
2562 pixel = (color & 0xc00000) >> 16;
2563 pixel += (color & 0xe000) >> 10;
2564 pixel += (color & 0xe0) >> 5;
2568 int64_t BC_WindowBase::get_color_rgb16(int color)
2571 result = (color & 0xf80000) >> 8;
2572 result += (color & 0xfc00) >> 5;
2573 result += (color & 0xf8) >> 3;
2578 int64_t BC_WindowBase::get_color_bgr16(int color)
2581 result = (color & 0xf80000) >> 19;
2582 result += (color & 0xfc00) >> 5;
2583 result += (color & 0xf8) << 8;
2588 int64_t BC_WindowBase::get_color_bgr24(int color)
2591 result = (color & 0xff) << 16;
2592 result += (color & 0xff00);
2593 result += (color & 0xff0000) >> 16;
2597 void BC_WindowBase::start_video()
2599 cursor_timer->update();
2601 // set_color(BLACK);
2602 // draw_box(0, 0, get_w(), get_h());
2606 void BC_WindowBase::stop_video()
2614 int64_t BC_WindowBase::get_color()
2616 return top_level->current_color;
2619 void BC_WindowBase::set_color(int64_t color)
2621 top_level->current_color = color;
2622 XSetForeground(top_level->display,
2624 top_level->get_color(color));
2627 void BC_WindowBase::set_opaque()
2629 XSetFunction(top_level->display, top_level->gc, GXcopy);
2632 void BC_WindowBase::set_inverse()
2634 XSetFunction(top_level->display, top_level->gc, GXxor);
2637 void BC_WindowBase::set_line_width(int value)
2639 this->line_width = value;
2640 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2641 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2642 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2643 JoinMiter); /* join_style */
2645 if(line_dashes > 0) {
2646 const char dashes = line_dashes;
2647 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2650 // XGCValues gcvalues;
2651 // unsigned long gcmask;
2652 // gcmask = GCCapStyle | GCJoinStyle;
2653 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2654 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2657 void BC_WindowBase::set_line_dashes(int value)
2659 line_dashes = value;
2660 // call XSetLineAttributes
2661 set_line_width(line_width);
2665 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2669 case ARROW_CURSOR: return top_level->arrow_cursor;
2670 case CROSS_CURSOR: return top_level->cross_cursor;
2671 case IBEAM_CURSOR: return top_level->ibeam_cursor;
2672 case VSEPARATE_CURSOR: return top_level->vseparate_cursor;
2673 case HSEPARATE_CURSOR: return top_level->hseparate_cursor;
2674 case MOVE_CURSOR: return top_level->move_cursor;
2675 case LEFT_CURSOR: return top_level->left_cursor;
2676 case RIGHT_CURSOR: return top_level->right_cursor;
2677 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor;
2678 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor;
2679 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor;
2680 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor;
2681 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor;
2682 case HOURGLASS_CURSOR: return top_level->hourglass_cursor;
2683 case TRANSPARENT_CURSOR: return top_level->transparent_cursor;
2684 case GRABBED_CURSOR: return top_level->grabbed_cursor;
2689 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2691 // inherit cursor from parent
2694 XUndefineCursor(top_level->display, win);
2695 current_cursor = cursor;
2698 // don't change cursor if overridden
2699 if((!top_level->is_hourglass && !is_transparent) ||
2702 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2703 if(flush) this->flush();
2706 if(!override) current_cursor = cursor;
2709 void BC_WindowBase::set_x_cursor(int cursor)
2711 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2712 XDefineCursor(top_level->display, win, temp_cursor);
2713 current_cursor = cursor;
2717 int BC_WindowBase::get_cursor()
2719 return current_cursor;
2722 void BC_WindowBase::start_hourglass()
2724 top_level->start_hourglass_recursive();
2728 void BC_WindowBase::stop_hourglass()
2730 top_level->stop_hourglass_recursive();
2734 void BC_WindowBase::start_hourglass_recursive()
2736 if(this == top_level)
2744 set_cursor(HOURGLASS_CURSOR, 1, 0);
2745 for(int i = 0; i < subwindows->total; i++)
2747 subwindows->values[i]->start_hourglass_recursive();
2752 void BC_WindowBase::stop_hourglass_recursive()
2754 if(this == top_level)
2756 if(hourglass_total == 0) return;
2757 top_level->hourglass_total--;
2760 if(!top_level->hourglass_total)
2762 top_level->is_hourglass = 0;
2764 // Cause set_cursor to perform change
2766 set_cursor(current_cursor, 1, 0);
2768 for(int i = 0; i < subwindows->total; i++)
2770 subwindows->values[i]->stop_hourglass_recursive();
2778 XFontStruct* BC_WindowBase::get_font_struct(int font)
2780 // Clear out unrelated flags
2781 if(font & BOLDFACE) font ^= BOLDFACE;
2784 case SMALLFONT: return top_level->smallfont; break;
2785 case MEDIUMFONT: return top_level->mediumfont; break;
2786 case LARGEFONT: return top_level->largefont; break;
2787 case BIGFONT: return top_level->bigfont; break;
2788 case CLOCKFONT: return top_level->clockfont; break;
2793 XFontSet BC_WindowBase::get_fontset(int font)
2797 if(get_resources()->use_fontset)
2799 switch(font & 0xff) {
2800 case SMALLFONT: fs = top_level->smallfontset; break;
2801 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2802 case LARGEFONT: fs = top_level->largefontset; break;
2803 case BIGFONT: fs = top_level->bigfontset; break;
2804 case CLOCKFONT: fs = top_level->clockfontset; break;
2812 XftFont* BC_WindowBase::get_xft_struct(int font)
2815 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2816 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2817 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2818 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2819 case CLOCKFONT: return (XftFont*)top_level->clockfont_xft;
2820 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2821 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2822 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2830 int BC_WindowBase::get_current_font()
2832 return top_level->current_font;
2835 void BC_WindowBase::set_font(int font)
2837 top_level->current_font = font;
2840 if(get_resources()->use_xft) {}
2843 if(get_resources()->use_fontset) {
2847 if(get_font_struct(font))
2849 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2855 void BC_WindowBase::set_fontset(int font)
2859 if(get_resources()->use_fontset) {
2861 case SMALLFONT: fs = top_level->smallfontset; break;
2862 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2863 case LARGEFONT: fs = top_level->largefontset; break;
2864 case BIGFONT: fs = top_level->bigfontset; break;
2865 case CLOCKFONT: fs = top_level->clockfontset; break;
2873 XFontSet BC_WindowBase::get_curr_fontset(void)
2875 if(get_resources()->use_fontset)
2876 return curr_fontset;
2880 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2883 if(get_resources()->use_xft && get_xft_struct(font))
2886 #ifdef X_HAVE_UTF8_STRING
2887 if(get_resources()->locale_utf8)
2889 xftTextExtentsUtf8(top_level->display,
2890 get_xft_struct(font),
2891 (const XftChar8 *)text,
2898 xftTextExtents8(top_level->display,
2899 get_xft_struct(font),
2900 (const XftChar8 *)text,
2904 return extents.xOff;
2908 if(get_resources()->use_fontset && top_level->get_fontset(font))
2909 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2911 if(get_font_struct(font))
2912 return XTextWidth(get_font_struct(font), text, length);
2918 case MEDIUM_7SEGMENT:
2919 return get_resources()->medium_7segment[0]->get_w() * length;
2929 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2931 int i, j, w = 0, line_w = 0;
2932 if(length < 0) length = strlen(text);
2934 for(i = 0, j = 0; i <= length; i++)
2939 line_w = get_single_text_width(font, &text[j], i - j);
2945 line_w = get_single_text_width(font, &text[j], length - j);
2947 if(line_w > w) w = line_w;
2950 if(i > length && w == 0)
2952 w = get_single_text_width(font, text, length);
2958 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2961 if( length < 0 ) length = wcslen(text);
2963 for( i=j=0; i<length && text[i]; ++i ) {
2964 if( text[i] != '\n' ) continue;
2966 int lw = get_single_text_width(font, &text[j], i-j);
2967 if( w < lw ) w = lw;
2972 int lw = get_single_text_width(font, &text[j], length-j);
2973 if( w < lw ) w = lw;
2979 int BC_WindowBase::get_text_ascent(int font)
2983 if( (fstruct = get_xft_struct(font)) != 0 )
2984 return fstruct->ascent;
2986 if(get_resources()->use_fontset && top_level->get_fontset(font))
2988 XFontSetExtents *extents;
2990 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2991 return -extents->max_logical_extent.y;
2994 if(get_font_struct(font))
2995 return top_level->get_font_struct(font)->ascent;
2998 case MEDIUM_7SEGMENT:
2999 return get_resources()->medium_7segment[0]->get_h();
3004 int BC_WindowBase::get_text_descent(int font)
3008 if( (fstruct = get_xft_struct(font)) != 0 )
3009 return fstruct->descent;
3011 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
3012 XFontSetExtents *extents;
3013 extents = XExtentsOfFontSet(top_level->get_fontset(font));
3014 return (extents->max_logical_extent.height
3015 + extents->max_logical_extent.y);
3018 if(get_font_struct(font))
3019 return top_level->get_font_struct(font)->descent;
3024 int BC_WindowBase::get_text_height(int font, const char *text)
3029 if( (fstruct = get_xft_struct(font)) != 0 )
3030 rowh = fstruct->height;
3033 rowh = get_text_ascent(font) + get_text_descent(font);
3035 if(!text) return rowh;
3037 // Add height of lines
3038 int h = 0, i, length = strlen(text);
3039 for(i = 0; i <= length; i++)
3050 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
3052 if(color_model < 0) color_model = top_level->get_color_model();
3053 return new BC_Bitmap(top_level, w, h, color_model);
3056 void BC_WindowBase::init_wait()
3058 #ifndef SINGLE_THREAD
3059 if(window_type != MAIN_WINDOW)
3060 top_level->init_wait();
3061 init_lock->lock("BC_WindowBase::init_wait");
3062 init_lock->unlock();
3066 int BC_WindowBase::accel_available(int color_model, int lock_it)
3068 if( window_type != MAIN_WINDOW )
3069 return top_level->accel_available(color_model, lock_it);
3071 lock_window("BC_WindowBase::accel_available");
3073 switch(color_model) {
3075 grab_port_id(color_model);
3079 grab_port_id(color_model);
3088 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3089 return xvideo_port_id >= 0 ? 1 : 0;
3093 int BC_WindowBase::grab_port_id(int color_model)
3095 if( !get_resources()->use_xvideo || // disabled
3096 !get_resources()->use_shm ) // Only local server is fast enough.
3098 if( xvideo_port_id >= 0 )
3099 return xvideo_port_id;
3101 unsigned int ver, rev, reqBase, eventBase, errorBase;
3102 if( Success != XvQueryExtension(display, // XV extension is available
3103 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3106 // XV adaptors are available
3107 unsigned int numAdapt = 0;
3108 XvAdaptorInfo *info = 0;
3109 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3113 // Translate from color_model to X color model
3114 int x_color_model = BC_CModels::bc_to_x(color_model);
3116 // Get adaptor with desired color model
3117 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3118 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3119 // adaptor supports XvImages
3120 int numFormats = 0, numPorts = info[i].num_ports;
3121 XvImageFormatValues *formats =
3122 XvListImageFormats(display, info[i].base_id, &numFormats);
3123 if( !formats ) continue;
3125 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3126 if( formats[j].id != x_color_model ) continue;
3127 // this adaptor supports the desired format, grab a port
3128 for( int k=0; k<numPorts; ++k ) {
3129 if( Success == XvGrabPort(top_level->display,
3130 info[i].base_id+k, CurrentTime) ) {
3131 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3132 xvideo_port_id = info[i].base_id + k;
3140 XvFreeAdaptorInfo(info);
3142 return xvideo_port_id;
3146 int BC_WindowBase::show_window(int flush)
3148 for(int i = 0; i < subwindows->size(); i++)
3150 subwindows->get(i)->show_window(0);
3153 XMapWindow(top_level->display, win);
3154 if(flush) XFlush(top_level->display);
3155 // XSync(top_level->display, 0);
3160 int BC_WindowBase::hide_window(int flush)
3162 for(int i = 0; i < subwindows->size(); i++)
3164 subwindows->get(i)->hide_window(0);
3167 XUnmapWindow(top_level->display, win);
3168 if(flush) this->flush();
3173 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3175 subwindows->append((BC_SubWindow*)menu_bar);
3177 menu_bar->parent_window = this;
3178 menu_bar->top_level = this->top_level;
3179 menu_bar->initialize();
3183 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3185 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3186 if(this != top_level) return top_level->add_popup(window);
3187 popups.append(window);
3191 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3193 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3194 if(this != top_level)
3195 top_level->remove_popup(window);
3197 popups.remove(window);
3198 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3202 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3204 subwindows->append(subwindow);
3206 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3208 // parent window must be set before the subwindow initialization
3209 subwindow->parent_window = this;
3210 subwindow->top_level = this->top_level;
3212 // Execute derived initialization
3213 subwindow->initialize();
3218 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3220 return add_subwindow(subwindow);
3223 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3225 if( !top_level->flash_enabled ) return 0;
3226 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3228 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3231 XClearArea(top_level->display, win, x, y, w, h, 0);
3235 XClearWindow(top_level->display, win);
3243 int BC_WindowBase::flash(int flush)
3245 flash(-1, -1, -1, -1, flush);
3249 void BC_WindowBase::flush()
3251 //if(!get_window_lock())
3252 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3253 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3254 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3255 int locked = get_window_lock();
3256 if( !locked ) lock_window("BC_WindowBase::flush");
3257 XFlush(top_level->display);
3258 if( !locked ) unlock_window();
3261 void BC_WindowBase::sync_display()
3263 int locked = get_window_lock();
3264 if( !locked ) lock_window("BC_WindowBase::sync_display");
3265 XSync(top_level->display, False);
3266 if( !locked ) unlock_window();
3269 int BC_WindowBase::get_window_lock()
3271 #ifdef SINGLE_THREAD
3272 return BC_Display::display_global->get_display_locked();
3274 return top_level->window_lock;
3278 int BC_WindowBase::lock_window(const char *location)
3280 if(top_level && top_level != this)
3282 top_level->lock_window(location);
3287 SET_LOCK(this, title, location);
3288 #ifdef SINGLE_THREAD
3289 BC_Display::lock_display(location);
3291 XLockDisplay(top_level->display);
3292 top_level->display_lock_owner = pthread_self();
3295 ++top_level->window_lock;
3299 printf("BC_WindowBase::lock_window top_level NULL\n");
3304 int BC_WindowBase::unlock_window()
3306 if(top_level && top_level != this)
3308 top_level->unlock_window();
3315 // if(!top_level->window_lock)
3317 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3321 if( top_level->window_lock > 0 )
3322 if( --top_level->window_lock == 0 )
3323 top_level->display_lock_owner = 0;
3324 #ifdef SINGLE_THREAD
3325 BC_Display::unlock_display();
3327 XUnlockDisplay(top_level->display);
3332 printf("BC_WindowBase::unlock_window top_level NULL\n");
3337 void BC_WindowBase::set_done(int return_value)
3339 if(done_set) return;
3341 if(window_type != MAIN_WINDOW)
3342 top_level->set_done(return_value);
3345 #ifdef SINGLE_THREAD
3346 this->return_value = return_value;
3347 BC_Display::display_global->arm_completion(this);
3348 completion_lock->unlock();
3349 #else // SINGLE_THREAD
3351 if( !event_thread ) return;
3352 XEvent *event = new_xevent();
3353 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3354 event->type = ClientMessage;
3355 ptr->message_type = SetDoneXAtom;
3357 this->return_value = return_value;
3358 // May lock up here because XSendEvent doesn't work too well
3359 // asynchronous with XNextEvent.
3360 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3362 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3368 void BC_WindowBase::close(int return_value)
3370 hide_window(); flush();
3371 set_done(return_value);
3374 int BC_WindowBase::grab(BC_WindowBase *window)
3376 if( window->active_grab && this != window->active_grab ) return 0;
3377 window->active_grab = this;
3378 this->grab_active = window;
3381 int BC_WindowBase::ungrab(BC_WindowBase *window)
3383 if( window->active_grab && this != window->active_grab ) return 0;
3384 window->active_grab = 0;
3385 this->grab_active = 0;
3388 int BC_WindowBase::grab_event_count()
3391 #ifndef SINGLE_THREAD
3392 result = grab_active->get_event_count();
3396 int BC_WindowBase::grab_buttons()
3398 XSync(top_level->display, False);
3399 if( XGrabButton(top_level->display, AnyButton, AnyModifier,
3400 top_level->rootwin, True, ButtonPressMask | ButtonReleaseMask,
3401 GrabModeAsync, GrabModeSync, None, None) == GrabSuccess ) {
3402 set_active_subwindow(this);
3407 void BC_WindowBase::ungrab_buttons()
3409 XUngrabButton(top_level->display, AnyButton, AnyModifier, top_level->rootwin);
3410 set_active_subwindow(0);
3413 void BC_WindowBase::grab_cursor()
3415 Cursor cursor_grab = get_cursor_struct(GRABBED_CURSOR);
3416 XGrabPointer(top_level->display, top_level->rootwin, True,
3417 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
3418 GrabModeAsync, GrabModeAsync, None, cursor_grab, CurrentTime);
3420 void BC_WindowBase::ungrab_cursor()
3422 XUngrabPointer(top_level->display, CurrentTime);
3426 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3427 // this is the bounding box of all the screens
3429 int BC_WindowBase::get_root_w(int lock_display)
3431 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3432 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3433 int result = WidthOfScreen(def_screen);
3434 if(lock_display) unlock_window();
3438 int BC_WindowBase::get_root_h(int lock_display)
3440 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3441 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3442 int result = HeightOfScreen(def_screen);
3443 if(lock_display) unlock_window();
3447 XineramaScreenInfo *
3448 BC_WindowBase::get_xinerama_info(int screen)
3450 if( !xinerama_info || !xinerama_screens ) return 0;
3452 for( int i=0; i<xinerama_screens; ++i )
3453 if( xinerama_info[i].screen_number == screen )
3454 return &xinerama_info[i];
3457 int top_x = get_x(), top_y = get_y();
3458 if( BC_DisplayInfo::left_border >= 0 ) top_x += BC_DisplayInfo::left_border;
3459 if( BC_DisplayInfo::top_border >= 0 ) top_y += BC_DisplayInfo::top_border;
3460 for( int i=0; i<xinerama_screens; ++i ) {
3461 int scr_y = top_y - xinerama_info[i].y_org;
3462 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3463 int scr_x = top_x - xinerama_info[i].x_org;
3464 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3465 return &xinerama_info[i];
3470 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3473 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3474 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3477 int root_w = get_root_w(0);
3478 int root_h = get_root_h(0);
3479 // Shift X based on position of current window if dual head
3480 if( (float)root_w/root_h > 1.8 ) {
3481 root_w = get_screen_w(0, 0);
3482 if( top_level->get_x() >= root_w )
3487 result = info->x_org;
3488 if(lock_display) unlock_window();
3492 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3494 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3495 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3496 int result = !info ? 0 : info->y_org;
3497 if(lock_display) unlock_window();
3501 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3504 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3505 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3507 int width = get_root_w(0);
3508 int height = get_root_h(0);
3509 if( (float)width/height > 1.8 ) {
3510 // If dual head, the screen width is > 16x9
3511 // but we only want to fill one screen
3512 // this code assumes the "big" screen is on the right
3513 int scr_w0 = width / 2;
3515 case 600: scr_w0 = 800; break;
3516 case 720: scr_w0 = 1280; break;
3517 case 1024: scr_w0 = 1280; break;
3518 case 1200: scr_w0 = 1600; break;
3519 case 1080: scr_w0 = 1920; break;
3521 int scr_w1 = width - scr_w0;
3522 result = screen > 0 ? scr_w1 :
3523 screen == 0 ? scr_w0 :
3524 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3530 result = info->width;
3531 if(lock_display) unlock_window();
3535 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3537 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3538 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3539 int result = info ? info->height : get_root_h(0);
3540 if(lock_display) unlock_window();
3544 // Bottom right corner
3545 int BC_WindowBase::get_x2()
3550 int BC_WindowBase::get_y2()
3555 int BC_WindowBase::get_video_on()
3560 int BC_WindowBase::get_hidden()
3562 return top_level->hidden;
3565 int BC_WindowBase::cursor_inside()
3567 return (top_level->cursor_x >= 0 &&
3568 top_level->cursor_y >= 0 &&
3569 top_level->cursor_x < w &&
3570 top_level->cursor_y < h);
3573 BC_WindowBase* BC_WindowBase::get_top_level()
3578 BC_WindowBase* BC_WindowBase::get_parent()
3580 return parent_window;
3583 int BC_WindowBase::get_color_model()
3585 return top_level->color_model;
3588 BC_Resources* BC_WindowBase::get_resources()
3590 return &BC_WindowBase::resources;
3593 BC_Synchronous* BC_WindowBase::get_synchronous()
3595 return BC_WindowBase::resources.get_synchronous();
3598 int BC_WindowBase::get_bg_color()
3603 void BC_WindowBase::set_bg_color(int color)
3605 this->bg_color = color;
3608 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3613 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3615 top_level->active_subwindow = subwindow;
3618 int BC_WindowBase::activate()
3623 int BC_WindowBase::deactivate()
3625 if(window_type == MAIN_WINDOW)
3627 if( top_level->active_menubar ) {
3628 top_level->active_menubar->deactivate();
3629 top_level->active_menubar = 0;
3631 if( top_level->active_popup_menu ) {
3632 top_level->active_popup_menu->deactivate();
3633 top_level->active_popup_menu = 0;
3635 if( top_level->active_subwindow ) {
3636 top_level->active_subwindow->deactivate();
3637 top_level->active_subwindow = 0;
3639 if( top_level->motion_events && top_level->last_motion_win == this->win )
3640 top_level->motion_events = 0;
3646 int BC_WindowBase::cycle_textboxes(int amount)
3649 BC_WindowBase *new_textbox = 0;
3653 BC_WindowBase *first_textbox = 0;
3654 find_next_textbox(&first_textbox, &new_textbox, result);
3655 if(!new_textbox) new_textbox = first_textbox;
3661 BC_WindowBase *last_textbox = 0;
3662 find_prev_textbox(&last_textbox, &new_textbox, result);
3663 if(!new_textbox) new_textbox = last_textbox;
3667 if(new_textbox != active_subwindow)
3670 new_textbox->activate();
3676 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3678 // Search subwindows for textbox
3679 for(int i = 0; i < subwindows->total && result < 2; i++)
3681 BC_WindowBase *test_subwindow = subwindows->values[i];
3682 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3689 if(!*first_textbox) *first_textbox = this;
3693 if(top_level->active_subwindow == this)
3699 *next_textbox = this;
3706 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3712 if(!*last_textbox) *last_textbox = this;
3716 if(top_level->active_subwindow == this)
3722 *prev_textbox = this;
3727 // Search subwindows for textbox
3728 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3730 BC_WindowBase *test_subwindow = subwindows->values[i];
3731 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3736 BC_Clipboard* BC_WindowBase::get_clipboard()
3738 #ifdef SINGLE_THREAD
3739 return BC_Display::display_global->clipboard;
3741 return top_level->clipboard;
3745 Atom BC_WindowBase::to_clipboard(const char *data, long len, int clipboard_num)
3747 return get_clipboard()->to_clipboard(this, data, len, clipboard_num);
3750 long BC_WindowBase::from_clipboard(char *data, long maxlen, int clipboard_num)
3752 return get_clipboard()->from_clipboard(data, maxlen, clipboard_num);
3755 long BC_WindowBase::clipboard_len(int clipboard_num)
3757 return get_clipboard()->clipboard_len(clipboard_num);
3760 int BC_WindowBase::do_selection_clear(Window win)
3762 top_level->event_win = win;
3763 return dispatch_selection_clear();
3766 int BC_WindowBase::dispatch_selection_clear()
3769 for( int i=0; i<subwindows->total && !result; ++i )
3770 result = subwindows->values[i]->dispatch_selection_clear();
3772 result = selection_clear_event();
3777 void BC_WindowBase::get_relative_cursor(int &x, int &y, int lock_window)
3779 int abs_x, abs_y, win_x, win_y;
3780 unsigned int temp_mask;
3783 if(lock_window) this->lock_window("BC_WindowBase::get_relative_cursor");
3784 XQueryPointer(top_level->display, top_level->win,
3785 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3788 XTranslateCoordinates(top_level->display, top_level->rootwin,
3789 win, abs_x, abs_y, &x, &y, &temp_win);
3790 if(lock_window) this->unlock_window();
3792 int BC_WindowBase::get_relative_cursor_x(int lock_window)
3795 get_relative_cursor(x, y, lock_window);
3798 int BC_WindowBase::get_relative_cursor_y(int lock_window)
3801 get_relative_cursor(x, y, lock_window);
3805 void BC_WindowBase::get_abs_cursor(int &abs_x, int &abs_y, int lock_window)
3808 unsigned int temp_mask;
3811 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor");
3812 XQueryPointer(top_level->display, top_level->win,
3813 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3815 if(lock_window) this->unlock_window();
3817 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3820 get_abs_cursor(abs_x, abs_y, lock_window);
3823 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3826 get_abs_cursor(abs_x, abs_y, lock_window);
3830 void BC_WindowBase::get_pop_cursor(int &px, int &py, int lock_window)
3833 get_abs_cursor(px, py, lock_window);
3834 if( px < margin ) px = margin;
3835 if( py < margin ) py = margin;
3836 int wd = get_screen_w(lock_window,-1) - margin;
3837 if( px > wd ) px = wd;
3838 int ht = get_screen_h(lock_window,-1) - margin;
3839 if( py > ht ) py = ht;
3841 int BC_WindowBase::get_pop_cursor_x(int lock_window)
3844 get_pop_cursor(px, py, lock_window);
3847 int BC_WindowBase::get_pop_cursor_y(int lock_window)
3850 get_pop_cursor(px, py, lock_window);
3854 int BC_WindowBase::match_window(Window win)
3856 if (this->win == win) return 1;
3858 for(int i = 0; i < subwindows->total; i++) {
3859 result = subwindows->values[i]->match_window(win);
3860 if (result) return result;
3866 int BC_WindowBase::get_cursor_over_window()
3868 int abs_x, abs_y, win_x, win_y;
3869 unsigned int mask_return;
3870 Window root_return, child_return;
3872 int ret = XQueryPointer(top_level->display, top_level->rootwin,
3873 &root_return, &child_return, &abs_x, &abs_y,
3874 &win_x, &win_y, &mask_return);
3875 if( ret && child_return == None ) ret = 0;
3876 if( ret && win != child_return )
3877 ret = top_level->match_window(child_return);
3878 // query pointer can return a window manager window with this top_level as a child
3879 // for kde this can be two levels deep
3880 unsigned int nchildren_return = 0;
3881 Window parent_return, *children_return = 0;
3882 Window top_win = top_level->win;
3883 while( !ret && top_win != top_level->rootwin && top_win != root_return &&
3884 XQueryTree(top_level->display, top_win, &root_return,
3885 &parent_return, &children_return, &nchildren_return) ) {
3886 if( children_return ) XFree(children_return);
3887 if( (top_win=parent_return) == child_return ) ret = 1;
3892 int BC_WindowBase::cursor_above()
3895 get_relative_cursor(rx, ry);
3896 return rx < 0 || rx >= get_w() ||
3897 ry < 0 || ry >= get_h() ? 0 : 1;
3900 int BC_WindowBase::get_drag_x()
3902 return top_level->drag_x;
3905 int BC_WindowBase::get_drag_y()
3907 return top_level->drag_y;
3910 int BC_WindowBase::get_cursor_x()
3912 return top_level->cursor_x;
3915 int BC_WindowBase::get_cursor_y()
3917 return top_level->cursor_y;
3920 int BC_WindowBase::dump_windows()
3922 printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
3923 this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
3924 for(int i = 0; i < subwindows->size(); i++)
3925 subwindows->get(i)->dump_windows();
3926 for(int i = 0; i < popups.size(); i++) {
3927 BC_WindowBase *p = popups[i];
3928 printf("\tBC_WindowBase::dump_windows popup=%p win=%p '%s', %dx%d+%d+%d %s\n",
3929 p, (void*)p->win, p->title, p->w,p->h,p->x,p->y, typeid(*p).name());
3934 int BC_WindowBase::is_event_win()
3936 return this->win == top_level->event_win;
3939 void BC_WindowBase::set_dragging(int value)
3941 is_dragging = value;
3944 int BC_WindowBase::get_dragging()
3949 int BC_WindowBase::get_buttonpress()
3951 return top_level->button_number;
3954 int BC_WindowBase::get_button_down()
3956 return top_level->button_down;
3959 int BC_WindowBase::alt_down()
3961 return top_level->alt_mask;
3964 int BC_WindowBase::shift_down()
3966 return top_level->shift_mask;
3969 int BC_WindowBase::ctrl_down()
3971 return top_level->ctrl_mask;
3974 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3977 *length = top_level->wkey_string_length;
3978 return top_level->wkey_string;
3981 #ifdef X_HAVE_UTF8_STRING
3982 char* BC_WindowBase::get_keypress_utf8()
3984 return top_level->key_pressed_utf8;
3989 int BC_WindowBase::get_keypress()
3991 return top_level->key_pressed;
3994 int BC_WindowBase::get_double_click()
3996 return top_level->double_click;
3999 int BC_WindowBase::get_triple_click()
4001 return top_level->triple_click;
4004 int BC_WindowBase::get_bgcolor()
4009 int BC_WindowBase::resize_window(int w, int h)
4011 if(this->w == w && this->h == h) return 0;
4013 if(window_type == MAIN_WINDOW && !allow_resize)
4015 XSizeHints size_hints;
4016 size_hints.flags = PSize | PMinSize | PMaxSize;
4017 size_hints.width = w;
4018 size_hints.height = h;
4019 size_hints.min_width = w;
4020 size_hints.max_width = w;
4021 size_hints.min_height = h;
4022 size_hints.max_height = h;
4023 XSetNormalHints(top_level->display, win, &size_hints);
4025 XResizeWindow(top_level->display, win, w, h);
4030 pixmap = new BC_Pixmap(this, w, h);
4032 // Propagate to menubar
4033 for(int i = 0; i < subwindows->total; i++)
4035 subwindows->values[i]->dispatch_resize_event(w, h);
4038 draw_background(0, 0, w, h);
4039 if(top_level == this && get_resources()->recursive_resizing)
4040 resize_history.append(new BC_ResizeCall(w, h));
4044 // The only way for resize events to be propagated is by updating the internal w and h
4045 int BC_WindowBase::resize_event(int w, int h)
4047 if(window_type == MAIN_WINDOW)
4055 int BC_WindowBase::reposition_window(int x, int y)
4057 reposition_window(x, y, -1, -1);
4062 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
4066 // Some tools set their own dimensions before calling this, causing the
4067 // resize check to skip.
4071 if(w > 0 && w != this->w)
4077 if(h > 0 && h != this->h)
4083 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
4086 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
4088 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
4090 if(translation_count && window_type == MAIN_WINDOW)
4092 // KDE shifts window right and down.
4093 // FVWM leaves window alone and adds border around it.
4094 XMoveResizeWindow(top_level->display,
4096 x - BC_DisplayInfo::auto_reposition_x,
4097 y - BC_DisplayInfo::auto_reposition_y,
4103 XMoveResizeWindow(top_level->display,
4114 pixmap = new BC_Pixmap(this, this->w, this->h);
4115 clear_box(0,0, this->w, this->h);
4116 // Propagate to menubar
4117 for(int i = 0; i < subwindows->total; i++)
4119 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
4122 // draw_background(0, 0, w, h);
4128 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
4130 return reposition_window(get_x()+dx, get_y()+dy, w, h);
4133 int BC_WindowBase::reposition_window_relative(int dx, int dy)
4135 return reposition_window_relative(dx, dy, -1, -1);
4138 void BC_WindowBase::set_tooltips(int v)
4140 get_resources()->tooltips_enabled = v;
4143 void BC_WindowBase::set_force_tooltip(int v)
4148 int BC_WindowBase::raise_window(int do_flush)
4150 XRaiseWindow(top_level->display, win);
4151 if(do_flush) XFlush(top_level->display);
4155 int BC_WindowBase::lower_window(int do_flush)
4157 XLowerWindow(top_level->display, win);
4158 if(do_flush) XFlush(top_level->display);
4162 void BC_WindowBase::set_background(VFrame *bitmap)
4164 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4166 bg_pixmap = new BC_Pixmap(this, bitmap, PIXMAP_OPAQUE);
4167 shared_bg_pixmap = 0;
4168 draw_background(0, 0, w, h);
4171 void BC_WindowBase::put_title(const char *text)
4173 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4174 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4175 *cp++ = *bp >= ' ' ? *bp : ' ';
4179 void BC_WindowBase::set_title(const char *text, int utf8)
4181 // utf8>0: wm + net_wm, utf8=0: wm only, utf<0: net_wm only
4183 const unsigned char *wm_title = (const unsigned char *)title;
4184 int title_len = strlen((const char *)title);
4186 Atom xa_wm_name = XA_WM_NAME;
4187 Atom xa_icon_name = XA_WM_ICON_NAME;
4188 Atom xa_string = XA_STRING;
4189 XChangeProperty(display, win, xa_wm_name, xa_string, 8,
4190 PropModeReplace, wm_title, title_len);
4191 XChangeProperty(display, win, xa_icon_name, xa_string, 8,
4192 PropModeReplace, wm_title, title_len);
4195 Atom xa_net_wm_name = XInternAtom(display, "_NET_WM_NAME", True);
4196 Atom xa_net_icon_name = XInternAtom(display, "_NET_WM_ICON_NAME", True);
4197 Atom xa_utf8_string = XInternAtom(display, "UTF8_STRING", True);
4198 XChangeProperty(display, win, xa_net_wm_name, xa_utf8_string, 8,
4199 PropModeReplace, wm_title, title_len);
4200 XChangeProperty(display, win, xa_net_icon_name, xa_utf8_string, 8,
4201 PropModeReplace, wm_title, title_len);
4206 const char *BC_WindowBase::get_title()
4211 int BC_WindowBase::get_toggle_value()
4213 return toggle_value;
4216 int BC_WindowBase::get_toggle_drag()
4221 int BC_WindowBase::set_icon(VFrame *data)
4223 if(icon_pixmap) delete icon_pixmap;
4224 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4226 if(icon_window) delete icon_window;
4227 icon_window = new BC_Popup(this,
4230 icon_pixmap->get_w(),
4231 icon_pixmap->get_h(),
4233 1, // All windows are hidden initially
4237 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4238 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4239 wm_hints.icon_mask = icon_pixmap->get_alpha();
4240 wm_hints.icon_window = icon_window->win;
4241 wm_hints.window_group = XGroupLeader;
4243 // for(int i = 0; i < 1000; i++)
4244 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4247 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4248 XSync(top_level->display, 0);
4252 int BC_WindowBase::set_w(int w)
4258 int BC_WindowBase::set_h(int h)
4264 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4266 BC_Resources *resources = get_resources();
4267 char string[BCTEXTLEN];
4268 int newest_id = - 1;
4269 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4271 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4272 resources->filebox_history[i].path[0] = 0;
4273 defaults->get(string, resources->filebox_history[i].path);
4274 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4275 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4276 if(resources->filebox_history[i].id > newest_id)
4277 newest_id = resources->filebox_history[i].id;
4280 resources->filebox_id = newest_id + 1;
4281 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4282 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4283 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4284 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4285 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4286 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4287 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4288 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4289 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4290 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4291 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4292 resources->filebox_size_format = defaults->get("FILEBOX_SIZE_FORMAT", get_resources()->filebox_size_format);
4293 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4297 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4299 BC_Resources *resources = get_resources();
4300 char string[BCTEXTLEN];
4301 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4303 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4304 defaults->update(string, resources->filebox_history[i].path);
4305 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4306 defaults->update(string, resources->filebox_history[i].id);
4308 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4309 defaults->update("FILEBOX_W", resources->filebox_w);
4310 defaults->update("FILEBOX_H", resources->filebox_h);
4311 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4312 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4313 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4314 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4315 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4316 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4317 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4318 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4319 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4320 defaults->update("FILEBOX_SIZE_FORMAT", get_resources()->filebox_size_format);
4326 // For some reason XTranslateCoordinates can take a long time to return.
4327 // We work around this by only calling it when the event windows are different.
4328 void BC_WindowBase::translate_coordinates(Window src_w,
4340 *dest_x_return = src_x;
4341 *dest_y_return = src_y;
4345 XTranslateCoordinates(top_level->display,
4353 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4357 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4359 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4362 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4364 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4372 #ifdef HAVE_LIBXXF86VM
4373 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4377 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4379 XF86VidModeModeInfo **vm_modelines;
4380 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4381 for (i = 0; i < vm_count; i++) {
4382 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4385 display = top_level->display;
4386 if (vm_modelines[*vm]->hdisplay == *width)
4390 *width = vm_modelines[*vm]->hdisplay;
4391 *height = vm_modelines[*vm]->vdisplay;
4396 void BC_WindowBase::scale_vm(int vm)
4398 int foo,bar,dotclock;
4399 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4402 XF86VidModeModeInfo **vm_modelines;
4403 XF86VidModeModeLine vml;
4404 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4405 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4406 orig_modeline.dotclock = dotclock;
4407 orig_modeline.hdisplay = vml.hdisplay;
4408 orig_modeline.hsyncstart = vml.hsyncstart;
4409 orig_modeline.hsyncend = vml.hsyncend;
4410 orig_modeline.htotal = vml.htotal;
4411 orig_modeline.vdisplay = vml.vdisplay;
4412 orig_modeline.vsyncstart = vml.vsyncstart;
4413 orig_modeline.vsyncend = vml.vsyncend;
4414 orig_modeline.vtotal = vml.vtotal;
4415 orig_modeline.flags = vml.flags;
4416 orig_modeline.privsize = vml.privsize;
4417 // orig_modeline.private = vml.private;
4418 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4419 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4420 XFlush(top_level->display);
4424 void BC_WindowBase::restore_vm()
4426 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4427 XFlush(top_level->display);
4450 #ifndef SINGLE_THREAD
4451 int BC_WindowBase::get_event_count()
4453 event_lock->lock("BC_WindowBase::get_event_count");
4454 int result = common_events.total;
4455 event_lock->unlock();
4459 XEvent* BC_WindowBase::get_event()
4462 while(!done && !result)
4464 event_condition->lock("BC_WindowBase::get_event");
4465 event_lock->lock("BC_WindowBase::get_event");
4467 if(common_events.total && !done)
4469 result = common_events.values[0];
4470 common_events.remove_number(0);
4473 event_lock->unlock();
4478 void BC_WindowBase::put_event(XEvent *event)
4480 event_lock->lock("BC_WindowBase::put_event");
4481 common_events.append(event);
4482 event_lock->unlock();
4483 event_condition->unlock();
4486 void BC_WindowBase::dequeue_events(Window win)
4488 event_lock->lock("BC_WindowBase::dequeue_events");
4490 int out = 0, total = common_events.size();
4491 for( int in=0; in<total; ++in ) {
4492 if( common_events[in]->xany.window == win ) continue;
4493 common_events[out++] = common_events[in];
4495 common_events.total = out;
4497 event_lock->unlock();
4500 int BC_WindowBase::resend_event(BC_WindowBase *window)
4502 if( resend_event_window ) return 1;
4503 resend_event_window = window;
4509 int BC_WindowBase::resend_event(BC_WindowBase *window)
4514 #endif // SINGLE_THREAD
4516 int BC_WindowBase::get_id()
4522 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4524 int w = vframe->get_w(), h = vframe->get_h();
4525 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4526 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4531 void BC_WindowBase::flicker(int n, int ms)
4533 int color = get_bg_color();
4534 for( int i=2*n; --i>=0; ) {
4535 set_inverse(); set_bg_color(WHITE);
4536 clear_box(0,0, w,h); flash(1);
4537 sync_display(); Timer::delay(ms);
4539 set_bg_color(color);