4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcclipboard.h"
24 #include "bcdisplay.h"
25 #include "bcdisplayinfo.h"
26 #include "bcmenubar.h"
29 #include "bcpopupmenu.h"
30 #include "bcrepeater.h"
31 #include "bcresources.h"
32 #include "bcsignals.h"
33 #include "bcsubwindow.h"
34 #include "bcsynchronous.h"
36 #include "bcwindowbase.h"
37 #include "bcwindowevents.h"
38 #include "bccmodels.h"
40 #include "condition.h"
58 #include <X11/extensions/Xinerama.h>
59 #include <X11/extensions/Xvlib.h>
60 #include <X11/extensions/shape.h>
61 #include <X11/XF86keysym.h>
62 #include <X11/Sunkeysym.h>
65 BC_ResizeCall::BC_ResizeCall(int w, int h)
77 int BC_WindowBase::shm_completion_event = -1;
81 BC_Resources BC_WindowBase::resources;
83 Window XGroupLeader = 0;
85 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
86 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
88 BC_WindowBase::BC_WindowBase(int opts)
91 //printf("BC_WindowBase::BC_WindowBase 1\n");
92 BC_WindowBase::initialize();
95 BC_WindowBase::~BC_WindowBase()
98 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
100 if(window_type == MAIN_WINDOW)
101 lock_window("BC_WindowBase::~BC_WindowBase");
104 #ifdef HAVE_LIBXXF86VM
105 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
112 if(window_type != MAIN_WINDOW)
115 XSelectInput(top_level->display, this->win, 0);
116 XSync(top_level->display,0);
117 #ifndef SINGLE_THREAD
118 top_level->dequeue_events(win);
120 // drop active window refs to this
121 if(top_level->active_menubar == this) top_level->active_menubar = 0;
122 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
123 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
124 // drop motion window refs to this
125 if(top_level->motion_events && top_level->last_motion_win == this->win)
126 top_level->motion_events = 0;
128 // Remove pointer from parent window to this
129 parent_window->subwindows->remove(this);
132 if(grab_active) grab_active->active_grab = 0;
133 if(icon_window) delete icon_window;
134 if(window_type == POPUP_WINDOW)
135 parent_window->remove_popup(this);
137 // Delete the subwindows
140 while(subwindows->total)
142 // Subwindow removes its own pointer
143 delete subwindows->values[0];
151 if( get_resources()->get_synchronous() &&
152 (top_level->options & GLX_WINDOW) && glx_win != 0 )
153 get_resources()->get_synchronous()->delete_window(this);
156 XDestroyWindow(top_level->display, win);
158 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
159 if(icon_pixmap) delete icon_pixmap;
160 if(temp_bitmap) delete temp_bitmap;
161 top_level->active_bitmaps.remove_buffers(this);
162 if(_7segment_pixmaps)
164 for(int i = 0; i < TOTAL_7SEGMENT; i++)
165 delete _7segment_pixmaps[i];
167 delete [] _7segment_pixmaps;
172 if(window_type == MAIN_WINDOW)
174 XFreeGC(display, gc);
175 static XFontStruct *BC_WindowBase::*xfont[] = {
176 &BC_WindowBase::smallfont,
177 &BC_WindowBase::mediumfont,
178 &BC_WindowBase::largefont,
179 &BC_WindowBase::bigfont,
181 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
182 XFreeFont(display, this->*xfont[i]);
184 // past bug in X caused XRenderExtensionInfo to be damaged
185 // if this is done here. left to be done in XCloseDisplay by Xlib.
186 // works in more modern systems, and needed for leak testing.
187 #if defined(HAVE_XFT) && defined(VALGRIND)
188 static void *BC_WindowBase::*xft_font[] = {
189 &BC_WindowBase::smallfont_xft,
190 &BC_WindowBase::mediumfont_xft,
191 &BC_WindowBase::largefont_xft,
192 &BC_WindowBase::bigfont_xft,
193 &BC_WindowBase::bold_smallfont_xft,
194 &BC_WindowBase::bold_mediumfont_xft,
195 &BC_WindowBase::bold_largefont_xft,
197 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
198 XftFont *xft = (XftFont *)(this->*xft_font[i]);
199 if( xft ) XftFontClose (display, xft);
207 XFree(xinerama_info);
208 xinerama_screens = 0;
210 if( xvideo_port_id >= 0 )
211 XvUngrabPort(display, xvideo_port_id, CurrentTime);
214 // Can't close display if another thread is waiting for events.
215 // Synchronous thread must delete display it owns a GLX_WINDOW
216 // Must be last reference to display.
217 #ifndef SINGLE_THREAD
219 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
220 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
221 "implemented for BC_Pixmap.\n"));
222 get_resources()->get_synchronous()->delete_display(this);
226 XCloseDisplay(display);
227 // clipboard uses a different display connection
228 clipboard->stop_clipboard();
230 #endif // SINGLE_THREAD
233 resize_history.remove_all_objects();
235 #ifndef SINGLE_THREAD
236 common_events.remove_all_objects();
238 delete event_condition;
241 top_level->window_lock = 0;
242 BC_Display::unlock_display();
247 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
248 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
249 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
252 UNSET_ALL_LOCKS(this)
255 int BC_WindowBase::initialize()
260 display_lock_owner = 0;
276 xinerama_screens = 0;
281 translation_events = 0;
282 ctrl_mask = shift_mask = alt_mask = 0;
283 cursor_x = cursor_y = button_number = 0;
297 active_popup_menu = 0;
298 active_subwindow = 0;
302 _7segment_pixmaps = 0;
305 // next_repeat_id = 0;
307 current_font = MEDIUMFONT;
308 current_color = BLACK;
309 current_cursor = ARROW_CURSOR;
312 shared_bg_pixmap = 0;
315 window_type = MAIN_WINDOW;
316 translation_count = 0;
317 x_correction = y_correction = 0;
326 #ifdef HAVE_LIBXXF86VM
330 bold_largefont_xft = 0;
331 bold_mediumfont_xft = 0;
332 bold_smallfont_xft = 0;
340 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
342 // Need these right away since put_event is called before run_window sometimes.
343 event_lock = new Mutex("BC_WindowBase::event_lock");
344 event_condition = new Condition(0, "BC_WindowBase::event_condition");
345 init_lock = new Condition(0, "BC_WindowBase::init_lock");
348 cursor_timer = new Timer;
351 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
352 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
353 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
367 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
370 ButtonReleaseMask | \
371 PointerMotionMask | \
375 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
387 const char *display_name,
389 BC_Pixmap *bg_pixmap,
392 XSetWindowAttributes attr;
394 XSizeHints size_hints;
397 #ifdef HAVE_LIBXXF86VM
401 id = get_resources()->get_id();
403 if(parent_window) top_level = parent_window->top_level;
404 if( top_level ) { // need this to avoid deadlock with Xlib's locks
406 lock_window("BC_WindowBase::create_window");
408 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
410 #ifdef HAVE_LIBXXF86VM
411 if(window_type == VIDMODE_SCALED_WINDOW)
412 closest_vm(&vm,&w,&h);
419 this->bg_color = bg_color;
420 this->window_type = window_type;
422 this->private_color = private_color;
423 this->parent_window = parent_window;
424 this->bg_pixmap = bg_pixmap;
425 this->allow_resize = allow_resize;
427 strcpy(this->display_name, display_name);
429 this->display_name[0] = 0;
432 if(bg_pixmap) shared_bg_pixmap = 1;
434 subwindows = new BC_SubWindowList;
436 if(window_type == MAIN_WINDOW)
439 parent_window = this;
443 display = BC_Display::get_display(display_name);
444 BC_Display::lock_display("BC_WindowBase::create_window");
445 // BC_Display::display_global->new_window(this);
448 // get the display connection
450 // This function must be the first Xlib
451 // function a multi-threaded program calls
453 display = init_display(display_name);
454 if( shm_completion_event < 0 ) shm_completion_event =
455 ShmCompletion + XShmGetEventBase(display);
458 screen = DefaultScreen(display);
459 rootwin = RootWindow(display, screen);
460 // window placement boundaries
461 if( !xinerama_screens && XineramaIsActive(display) )
462 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
463 root_w = get_root_w(0);
464 root_h = get_root_h(0);
467 vis = get_glx_visual(display);
470 vis = DefaultVisual(display, screen);
472 default_depth = DefaultDepth(display, screen);
474 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
475 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
478 // This must be done before fonts to know if antialiasing is available.
481 if(resources.use_shm < 0) resources.initialize_display(this);
482 x_correction = BC_DisplayInfo::get_left_border();
483 y_correction = BC_DisplayInfo::get_top_border();
485 // clamp window placement
486 if(this->x + this->w + x_correction > root_w)
487 this->x = root_w - this->w - x_correction;
488 if(this->y + this->h + y_correction > root_h)
489 this->y = root_h - this->h - y_correction;
490 if(this->x < 0) this->x = 0;
491 if(this->y < 0) this->y = 0;
493 if(this->bg_color == -1)
494 this->bg_color = resources.get_bg_color();
496 // printf("bcwindowbase 1 %s\n", title);
497 // if(window_type == MAIN_WINDOW) sleep(1);
498 // printf("bcwindowbase 10\n");
504 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
506 attr.event_mask = DEFAULT_EVENT_MASKS |
507 StructureNotifyMask |
511 attr.background_pixel = get_color(this->bg_color);
512 attr.colormap = cmap;
513 attr.cursor = get_cursor_struct(ARROW_CURSOR);
515 win = XCreateWindow(display, rootwin,
516 this->x, this->y, this->w, this->h, 0,
517 top_level->default_depth, InputOutput,
519 XGetNormalHints(display, win, &size_hints);
521 size_hints.flags = PSize | PMinSize | PMaxSize;
522 size_hints.width = this->w;
523 size_hints.height = this->h;
524 size_hints.min_width = allow_resize ? minw : this->w;
525 size_hints.max_width = allow_resize ? 32767 : this->w;
526 size_hints.min_height = allow_resize ? minh : this->h;
527 size_hints.max_height = allow_resize ? 32767 : this->h;
528 if(x > -BC_INFINITY && x < BC_INFINITY)
530 size_hints.flags |= PPosition;
531 size_hints.x = this->x;
532 size_hints.y = this->y;
534 XSetWMProperties(display, win, 0, 0, 0, 0, &size_hints, 0, 0);
537 #ifndef SINGLE_THREAD
538 clipboard = new BC_Clipboard(this);
539 clipboard->start_clipboard();
545 Atom ClientLeaderXAtom;
546 if (XGroupLeader == 0)
548 const char *instance_name = "cinelerra";
549 const char *class_name = "Cinelerra";
550 XClassHint *class_hints = XAllocClassHint();
551 class_hints->res_name = (char*)instance_name;
552 class_hints->res_class = (char*)class_name;
553 XSetClassHint(top_level->display, win, class_hints);
555 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
556 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
557 PropModeReplace, (unsigned char *)&XGroupLeader, true);
562 #ifdef HAVE_LIBXXF86VM
563 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
570 #ifdef HAVE_LIBXXF86VM
571 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
573 if(window_type == POPUP_WINDOW)
576 mask = CWEventMask | CWBackPixel | CWColormap |
577 CWOverrideRedirect | CWSaveUnder | CWCursor;
579 attr.event_mask = DEFAULT_EVENT_MASKS |
583 if(this->bg_color == -1)
584 this->bg_color = resources.get_bg_color();
585 attr.background_pixel = top_level->get_color(bg_color);
586 attr.colormap = top_level->cmap;
587 if(top_level->is_hourglass)
588 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
590 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
591 attr.override_redirect = True;
592 attr.save_under = True;
594 win = XCreateWindow(top_level->display,
595 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
596 top_level->default_depth, InputOutput, top_level->vis, mask,
598 top_level->add_popup(this);
601 if(window_type == SUB_WINDOW)
603 mask = CWEventMask | CWBackPixel | CWCursor;
604 attr.event_mask = DEFAULT_EVENT_MASKS;
605 attr.background_pixel = top_level->get_color(this->bg_color);
606 if(top_level->is_hourglass)
607 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
609 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
610 win = XCreateWindow(top_level->display,
611 parent_window->win, this->x, this->y, this->w, this->h, 0,
612 top_level->default_depth, InputOutput, top_level->vis, mask,
615 if(!hidden) XMapWindow(top_level->display, win);
618 // Create pixmap for all windows
619 pixmap = new BC_Pixmap(this, this->w, this->h);
621 // Set up options for main window
622 if(window_type == MAIN_WINDOW)
624 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
626 this->bg_pixmap = new BC_Pixmap(this,
627 get_resources()->bg_image,
631 if(!hidden) show_window();
635 draw_background(0, 0, this->w, this->h);
637 flash(-1, -1, -1, -1, 0);
639 // Set up options for popup window
640 #ifdef HAVE_LIBXXF86VM
641 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
643 if(window_type == POPUP_WINDOW)
647 if(!hidden) show_window();
649 get_resources()->create_window_lock->unlock();
650 if(need_lock) unlock_window();
655 Display* BC_WindowBase::init_display(const char *display_name)
659 if(display_name && display_name[0] == 0) display_name = NULL;
660 if((display = XOpenDisplay(display_name)) == NULL) {
661 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
663 if(getenv("DISPLAY") == NULL) {
664 printf(_("'DISPLAY' environment variable not set.\n"));
668 // Try again with default display.
669 if((display = XOpenDisplay(0)) == NULL) {
670 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
676 static int xsynch = -1;
678 const char *cp = getenv("CIN_XSYNCH");
679 xsynch = !cp ? 0 : atoi(cp);
682 XSynchronize(display, True);
687 Display* BC_WindowBase::get_display()
689 return top_level->display;
692 int BC_WindowBase::get_screen()
694 return top_level->screen;
697 int BC_WindowBase::run_window()
703 // Events may have been sent before run_window so can't initialize them here.
706 set_repeat(get_resources()->tooltip_delay);
707 BC_Display::display_global->new_window(this);
709 // If the first window created, run the display loop in this thread.
710 if(BC_Display::display_global->is_first(this))
712 BC_Display::unlock_display();
713 BC_Display::display_global->loop();
717 BC_Display::unlock_display();
718 completion_lock->lock("BC_WindowBase::run_window");
721 BC_Display::lock_display("BC_WindowBase::run_window");
722 BC_Display::display_global->delete_window(this);
724 unset_all_repeaters();
726 BC_Display::unlock_display();
728 #else // SINGLE_THREAD
733 set_repeat(get_resources()->tooltip_delay);
735 // Start X server events
736 event_thread = new BC_WindowEvents(this);
737 event_thread->start();
743 // Handle common events
749 unset_all_repeaters();
753 event_condition->reset();
754 common_events.remove_all_objects();
758 #endif // SINGLE_THREAD
763 int BC_WindowBase::get_key_masks(unsigned int key_state)
765 // printf("BC_WindowBase::get_key_masks %llx\n",
766 // event->xkey.state);
767 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
768 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
769 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
774 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
776 BC_KeyboardHandlerLock set;
777 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
780 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
782 BC_KeyboardHandlerLock set;
783 int i = BC_KeyboardHandler::listeners.size();
784 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
785 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
788 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
790 int result = (win->*handler)(wp);
794 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
797 BC_KeyboardHandlerLock set;
798 for( int i=0; !result && i<listeners.size(); ++i ) {
799 BC_KeyboardHandler *listener = listeners[i];
800 result = listener->run_event(wp);
805 void BC_KeyboardHandler::kill_grabs()
807 BC_KeyboardHandlerLock set;
808 for( int i=0; i<listeners.size(); ++i ) {
809 BC_WindowBase *win = listeners[i]->win;
810 if( win->get_window_type() != POPUP_WINDOW ) continue;
811 ((BC_Popup *)win)->ungrab_keyboard();
815 void BC_ActiveBitmaps::reque(XEvent *event)
817 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
818 ShmSeg shmseg = shm_ev->shmseg;
819 Drawable drawable = shm_ev->drawable;
820 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
821 active_lock.lock("BC_BitmapImage::reque");
822 BC_BitmapImage *bfr = first;
823 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
824 if( bfr && bfr->drawable == drawable )
826 active_lock.unlock();
828 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
829 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
830 // (int)shmseg, (int)drawable);
833 if( bfr->drawable != drawable ) return;
834 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
835 bfr->bitmap->reque(bfr);
838 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
840 active_lock.lock("BC_BitmapImage::insert");
841 bfr->drawable = pixmap;
843 active_lock.unlock();
846 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
848 active_lock.lock("BC_ActiveBitmaps::remove");
849 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
851 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
852 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
854 active_lock.unlock();
857 BC_ActiveBitmaps::BC_ActiveBitmaps()
861 BC_ActiveBitmaps::~BC_ActiveBitmaps()
867 int BC_WindowBase::keysym_lookup(XEvent *event)
869 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
870 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
872 if( event->xany.send_event && !event->xany.serial ) {
873 keysym = (KeySym) event->xkey.keycode;
874 keys_return[0] = keysym;
877 wkey_string_length = 0;
879 if( input_context ) {
880 wkey_string_length = XwcLookupString(input_context,
881 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
882 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
883 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
886 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
887 keys_return, KEYPRESSLEN, &keysym, &stat);
888 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
889 if( stat == XLookupBoth ) return ret;
890 if( stat == XLookupKeySym ) return 0;
892 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
893 wkey_string_length = ret;
894 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
898 pthread_t locking_task = (pthread_t)-1L;
899 int locking_event = -1;
900 int locking_message = -1;
902 int BC_WindowBase::dispatch_event(XEvent *event)
906 XClientMessageEvent *ptr;
907 int cancel_resize, cancel_translation;
912 #ifndef SINGLE_THREAD
913 // If an event is waiting get it, otherwise
914 // wait for next event only if there are no compressed events.
915 if(get_event_count() ||
916 (!motion_events && !resize_events && !translation_events))
919 // Lock out window deletions
920 lock_window("BC_WindowBase::dispatch_event 1");
921 locking_event = event->type;
922 locking_task = pthread_self();
923 locking_message = event->xclient.message_type;
926 // Handle compressed events
928 lock_window("BC_WindowBase::dispatch_event 2");
930 dispatch_resize_event(last_resize_w, last_resize_h);
932 dispatch_motion_event();
933 if(translation_events)
934 dispatch_translation_event();
945 //static const char *event_names[] = {
946 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
947 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
948 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
949 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
950 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
951 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
952 // "GenericEvent", "LASTEvent",
956 //if(event->type != ClientMessage) {
957 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
958 //__LINE__, title, event, event->type,
959 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
960 // event_names[event->type] : "Unknown");
965 active_grab->lock_window("BC_WindowBase::dispatch_event 3");
966 result = active_grab->grab_event(event);
967 active_grab->unlock_window();
968 if( result ) return result;
969 lock_window("BC_WindowBase::dispatch_event 4");
972 switch(event->type) {
974 // Clear the resize buffer
975 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
976 // Clear the motion buffer since this can clear the window
979 dispatch_motion_event();
982 ptr = (XClientMessageEvent*)event;
985 if(ptr->message_type == ProtoXAtom &&
986 (Atom)ptr->data.l[0] == DelWinXAtom)
991 if(ptr->message_type == RepeaterXAtom)
993 dispatch_repeat_event(ptr->data.l[0]);
994 // Make sure the repeater still exists.
995 // for(int i = 0; i < repeaters.total; i++)
997 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
999 // dispatch_repeat_event_master(ptr->data.l[0]);
1005 if(ptr->message_type == SetDoneXAtom)
1009 { // We currently use X marshalling for xatom events, we can switch to something else later
1010 recieve_custom_xatoms((xatom_event *)ptr);
1016 dispatch_focus_in();
1021 dispatch_focus_out();
1033 get_key_masks(event->xbutton.state);
1034 cursor_x = event->xbutton.x;
1035 cursor_y = event->xbutton.y;
1036 button_number = event->xbutton.button;
1038 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1039 event_win = event->xany.window;
1040 if (button_number < 6)
1042 if(button_number < 4)
1044 button_pressed = event->xbutton.button;
1045 button_time1 = button_time2;
1046 button_time2 = button_time3;
1047 button_time3 = event->xbutton.time;
1050 drag_win = event_win;
1051 drag_x1 = cursor_x - get_resources()->drag_radius;
1052 drag_x2 = cursor_x + get_resources()->drag_radius;
1053 drag_y1 = cursor_y - get_resources()->drag_radius;
1054 drag_y2 = cursor_y + get_resources()->drag_radius;
1056 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1059 button_time3 = button_time2 = button_time1 = 0;
1061 if((long)(button_time3 - button_time2) < resources.double_click)
1064 // button_time3 = button_time2 = button_time1 = 0;
1072 dispatch_button_press();
1077 get_key_masks(event->xbutton.state);
1078 button_number = event->xbutton.button;
1079 event_win = event->xany.window;
1080 if (button_number < 6)
1082 if(button_number < 4)
1084 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1086 dispatch_button_release();
1091 event_win = event->xany.window;
1092 dispatch_expose_event();
1096 get_key_masks(event->xmotion.state);
1097 // Dispatch previous motion event if this is a subsequent motion from a different window
1098 if(motion_events && last_motion_win != event->xany.window)
1100 dispatch_motion_event();
1103 // Buffer the current motion
1105 last_motion_state = event->xmotion.state;
1106 last_motion_x = event->xmotion.x;
1107 last_motion_y = event->xmotion.y;
1108 last_motion_win = event->xany.window;
1111 case ConfigureNotify:
1112 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1114 // event->xany.window,
1117 XTranslateCoordinates(top_level->display,
1125 last_resize_w = event->xconfigure.width;
1126 last_resize_h = event->xconfigure.height;
1129 cancel_translation = 0;
1131 // Resize history prevents responses to recursive resize requests
1132 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1134 if(resize_history.values[i]->w == last_resize_w &&
1135 resize_history.values[i]->h == last_resize_h)
1137 delete resize_history.values[i];
1138 resize_history.remove_number(i);
1143 if(last_resize_w == w && last_resize_h == h)
1151 if((last_translate_x == x && last_translate_y == y))
1152 cancel_translation = 1;
1154 if(!cancel_translation)
1156 translation_events = 1;
1159 translation_count++;
1163 get_key_masks(event->xkey.state);
1164 keys_return[0] = 0; keysym = -1;
1165 if(XFilterEvent(event, win)) {
1168 if( keysym_lookup(event) < 0 ) {
1169 printf("keysym %x\n", (uint32_t)keysym);
1173 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1177 // block out control keys
1178 if(keysym > 0xffe0 && keysym < 0xffff) break;
1179 // block out Alt_GR key
1180 if(keysym == 0xfe03) break;
1183 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1185 #ifdef X_HAVE_UTF8_STRING
1186 //It's Ascii or UTF8?
1187 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1188 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1190 if( ((keys_return[1] & 0xff) > 0x80) &&
1191 ((keys_return[0] & 0xff) > 0xC0) ) {
1192 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1193 key_pressed = keysym & 0xff;
1199 // block out extra keys
1209 // Translate key codes
1210 case XK_Return: key_pressed = RETURN; break;
1211 case XK_Up: key_pressed = UP; break;
1212 case XK_Down: key_pressed = DOWN; break;
1213 case XK_Left: key_pressed = LEFT; break;
1214 case XK_Right: key_pressed = RIGHT; break;
1215 case XK_Next: key_pressed = PGDN; break;
1216 case XK_Prior: key_pressed = PGUP; break;
1217 case XK_BackSpace: key_pressed = BACKSPACE; break;
1218 case XK_Escape: key_pressed = ESC; break;
1221 key_pressed = LEFTTAB;
1225 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1226 case XK_underscore: key_pressed = '_'; break;
1227 case XK_asciitilde: key_pressed = '~'; break;
1228 case XK_Delete: key_pressed = DELETE; break;
1229 case XK_Home: key_pressed = HOME; break;
1230 case XK_End: key_pressed = END; break;
1233 case XK_KP_Enter: key_pressed = KPENTER; break;
1234 case XK_KP_Add: key_pressed = KPPLUS; break;
1235 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1236 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1237 case XK_KP_Divide: key_pressed = KPSLASH; break;
1239 case XK_KP_End: key_pressed = KP1; break;
1241 case XK_KP_Down: key_pressed = KP2; break;
1243 case XK_KP_Page_Down: key_pressed = KP3; break;
1245 case XK_KP_Left: key_pressed = KP4; break;
1247 case XK_KP_Begin: key_pressed = KP5; break;
1249 case XK_KP_Right: key_pressed = KP6; break;
1251 case XK_KP_Home: key_pressed = KP7; break;
1253 case XK_KP_Up: key_pressed = KP8; break;
1255 case XK_KP_Page_Up: key_pressed = KP9; break;
1257 case XK_KP_Insert: key_pressed = KPINS; break;
1259 case XK_KP_Delete: key_pressed = KPDEL; break;
1260 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1262 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1263 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1264 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1265 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1266 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1267 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1268 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1269 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1270 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1271 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1272 // above case XK_Left: key_pressed = LEFT; break; /* left */
1273 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1274 // above case XK_Down: key_pressed = DOWN; break; /* down */
1275 // above case XK_Up: key_pressed = UP; break; /* up */
1276 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1277 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1278 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1279 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1280 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1281 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1282 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1283 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1284 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1285 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1286 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1287 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1290 key_pressed = keysym & 0xff;
1291 #ifdef X_HAVE_UTF8_STRING
1292 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1297 #ifdef X_HAVE_UTF8_STRING
1299 key_pressed_utf8 = keys_return;
1304 if( top_level == this )
1305 result = BC_KeyboardHandler::run_listeners(this);
1307 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1309 result = dispatch_keypress_event();
1310 // Handle some default keypresses
1313 if(key_pressed == 'w' ||
1322 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1323 dispatch_keyrelease_event();
1324 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1325 // keysym, event->xkey.state);
1329 if( cursor_entered && event->xcrossing.window == win ) {
1332 event_win = event->xany.window;
1333 dispatch_cursor_leave();
1337 if( !cursor_entered && event->xcrossing.window == win ) {
1338 if( !event->xcrossing.focus && get_resources()->grab_input_focus ) {
1339 XSetInputFocus(display, win, RevertToParent, CurrentTime);
1343 event_win = event->xany.window;
1344 cursor_x = event->xcrossing.x;
1345 cursor_y = event->xcrossing.y;
1346 dispatch_cursor_enter();
1352 //printf("100 %s %p %d\n", title, event, event->type);
1353 //if(event->type != ClientMessage) dump();
1355 #ifndef SINGLE_THREAD
1357 if(event) delete event;
1359 // if(done) completion_lock->unlock();
1362 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1366 int BC_WindowBase::dispatch_expose_event()
1369 for(int i = 0; i < subwindows->total && !result; i++)
1371 result = subwindows->values[i]->dispatch_expose_event();
1374 // Propagate to user
1375 if(!result) expose_event();
1379 int BC_WindowBase::dispatch_resize_event(int w, int h)
1381 // Can't store new w and h until the event is handles
1382 // because bcfilebox depends on the old w and h to
1383 // reposition widgets.
1384 if( window_type == MAIN_WINDOW ) {
1389 pixmap = new BC_Pixmap(this, w, h);
1390 clear_box(0, 0, w, h);
1393 // Propagate to subwindows
1394 for(int i = 0; i < subwindows->total; i++) {
1395 subwindows->values[i]->dispatch_resize_event(w, h);
1398 // Propagate to user
1401 if( window_type == MAIN_WINDOW ) {
1410 int BC_WindowBase::dispatch_flash()
1413 for(int i = 0; i < subwindows->total; i++)
1414 subwindows->values[i]->dispatch_flash();
1418 int BC_WindowBase::dispatch_translation_event()
1420 translation_events = 0;
1421 if(window_type == MAIN_WINDOW)
1425 x = last_translate_x;
1426 y = last_translate_y;
1427 // Correct for window manager offsets
1432 for(int i = 0; i < subwindows->total; i++)
1434 subwindows->values[i]->dispatch_translation_event();
1437 translation_event();
1441 int BC_WindowBase::dispatch_motion_event()
1446 if(top_level == this)
1449 event_win = last_motion_win;
1450 get_key_masks(last_motion_state);
1453 if(get_button_down() && !active_menubar && !active_popup_menu)
1457 cursor_x = last_motion_x;
1458 cursor_y = last_motion_y;
1459 result = dispatch_drag_motion();
1463 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1464 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1469 result = dispatch_drag_start();
1473 cursor_x = last_motion_x;
1474 cursor_y = last_motion_y;
1476 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1479 // active_popup_menu,
1480 // active_subwindow);
1482 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1483 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1484 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1487 // Dispatch in stacking order
1488 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1490 result = subwindows->values[i]->dispatch_motion_event();
1493 if(!result) result = cursor_motion_event(); // give to user
1497 int BC_WindowBase::dispatch_keypress_event()
1500 if(top_level == this)
1502 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1505 for(int i = 0; i < subwindows->total && !result; i++)
1507 result = subwindows->values[i]->dispatch_keypress_event();
1510 if(!result) result = keypress_event();
1515 int BC_WindowBase::dispatch_keyrelease_event()
1518 if(top_level == this)
1520 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1523 for(int i = 0; i < subwindows->total && !result; i++)
1525 result = subwindows->values[i]->dispatch_keyrelease_event();
1528 if(!result) result = keyrelease_event();
1533 int BC_WindowBase::dispatch_focus_in()
1535 for(int i = 0; i < subwindows->total; i++)
1537 subwindows->values[i]->dispatch_focus_in();
1545 int BC_WindowBase::dispatch_focus_out()
1547 for(int i = 0; i < subwindows->total; i++)
1549 subwindows->values[i]->dispatch_focus_out();
1557 int BC_WindowBase::get_has_focus()
1559 return top_level->has_focus;
1562 int BC_WindowBase::get_deleting()
1564 if(is_deleting) return 1;
1565 if(parent_window && parent_window->get_deleting()) return 1;
1569 int BC_WindowBase::dispatch_button_press()
1574 if(top_level == this)
1576 if(active_menubar) result = active_menubar->dispatch_button_press();
1577 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1578 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1581 for(int i = 0; i < subwindows->total && !result; i++)
1583 result = subwindows->values[i]->dispatch_button_press();
1586 if(!result) result = button_press_event();
1592 int BC_WindowBase::dispatch_button_release()
1595 if(top_level == this)
1597 if(active_menubar) result = active_menubar->dispatch_button_release();
1598 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1599 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1600 if(!result && button_number != 4 && button_number != 5)
1601 result = dispatch_drag_stop();
1604 for(int i = 0; i < subwindows->total && !result; i++)
1606 result = subwindows->values[i]->dispatch_button_release();
1611 result = button_release_event();
1618 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1621 // all repeat event handlers get called and decide based on activity and duration
1622 // whether to respond
1623 for(int i = 0; i < subwindows->total; i++)
1625 subwindows->values[i]->dispatch_repeat_event(duration);
1629 repeat_event(duration);
1633 // Unlock next repeat signal
1634 if(window_type == MAIN_WINDOW)
1636 #ifdef SINGLE_THREAD
1637 BC_Display::display_global->unlock_repeaters(duration);
1639 for(int i = 0; i < repeaters.total; i++)
1641 if(repeaters.values[i]->delay == duration)
1643 repeaters.values[i]->repeat_lock->unlock();
1651 void BC_WindowBase::unhide_cursor()
1656 if(top_level->is_hourglass)
1657 set_cursor(HOURGLASS_CURSOR, 1, 0);
1659 set_cursor(current_cursor, 1, 0);
1661 cursor_timer->update();
1665 void BC_WindowBase::update_video_cursor()
1667 if(video_on && !is_transparent)
1669 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1672 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1673 cursor_timer->update();
1678 cursor_timer->update();
1683 int BC_WindowBase::dispatch_cursor_leave()
1687 for(int i = 0; i < subwindows->total; i++)
1689 subwindows->values[i]->dispatch_cursor_leave();
1692 cursor_leave_event();
1696 int BC_WindowBase::dispatch_cursor_enter()
1702 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1703 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1704 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1706 for(int i = 0; !result && i < subwindows->total; i++)
1708 result = subwindows->values[i]->dispatch_cursor_enter();
1711 if(!result) result = cursor_enter_event();
1715 int BC_WindowBase::cursor_enter_event()
1720 int BC_WindowBase::cursor_leave_event()
1725 int BC_WindowBase::close_event()
1731 int BC_WindowBase::dispatch_drag_start()
1734 if(active_menubar) result = active_menubar->dispatch_drag_start();
1735 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1736 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1738 for(int i = 0; i < subwindows->total && !result; i++)
1740 result = subwindows->values[i]->dispatch_drag_start();
1743 if(!result) result = is_dragging = drag_start_event();
1747 int BC_WindowBase::dispatch_drag_stop()
1751 for(int i = 0; i < subwindows->total && !result; i++)
1753 result = subwindows->values[i]->dispatch_drag_stop();
1756 if(is_dragging && !result)
1766 int BC_WindowBase::dispatch_drag_motion()
1769 for(int i = 0; i < subwindows->total && !result; i++)
1771 result = subwindows->values[i]->dispatch_drag_motion();
1774 if(is_dragging && !result)
1776 drag_motion_event();
1784 int BC_WindowBase::show_tooltip(const char *text, int x, int y, int w, int h)
1787 int forced = !text ? force_tooltip : 1;
1788 if( !text ) text = tooltip_text;
1789 if( !text || (!forced && !get_resources()->tooltips_enabled) ) {
1790 top_level->hide_tooltip();
1794 if(w < 0) w = get_text_width(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1795 if(h < 0) h = get_text_height(MEDIUMFONT, text) + TOOLTIP_MARGIN * 2;
1796 // default x,y (win relative)
1797 if( x < 0 ) x = get_w();
1798 if( y < 0 ) y = get_h();
1800 get_root_coordinates(x, y, &wx, &wy);
1801 // keep the tip inside the window/display
1802 int x0 = top_level->get_x(), x1 = x0 + top_level->get_w();
1803 int x2 = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1804 if( x1 > x2 ) x1 = x2;
1805 if( wx < x0 ) wx = x0;
1806 if( wx >= (x1-=w) ) wx = x1;
1807 int y0 = top_level->get_y(), y1 = y0 + top_level->get_h();
1808 int y2 = top_level->get_root_h(0);
1809 if( y1 > y2 ) y1 = y2;
1810 if( wy < y0 ) wy = y0;
1811 if( wy >= (y1-=h) ) wy = y1;
1812 // avoid tip under cursor (flickers)
1814 get_abs_cursor_xy(abs_x,abs_y, 0);
1815 if( wx < abs_x && abs_x < wx+w && wy < abs_y && abs_y < wy+h ) {
1816 if( wx-abs_x < wy-abs_y )
1823 tooltip_popup = new BC_Popup(top_level, wx, wy, w, h,
1824 get_resources()->tooltip_bg_color);
1827 tooltip_popup->reposition_window(wx, wy, w, h);
1830 tooltip_popup->flash();
1831 tooltip_popup->flush();
1835 int BC_WindowBase::hide_tooltip()
1838 for(int i = 0; i < subwindows->total; i++)
1840 subwindows->values[i]->hide_tooltip();
1846 delete tooltip_popup;
1852 const char *BC_WindowBase::get_tooltip()
1854 return tooltip_text;
1857 int BC_WindowBase::set_tooltip(const char *text)
1859 tooltip_text = text;
1861 // Update existing tooltip if it is visible
1865 tooltip_popup->flash();
1869 // signal the event handler to repeat
1870 int BC_WindowBase::set_repeat(int64_t duration)
1874 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1877 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1879 #ifdef SINGLE_THREAD
1880 BC_Display::display_global->set_repeat(this, duration);
1882 // test repeater database for duplicates
1883 for(int i = 0; i < repeaters.total; i++)
1886 if(repeaters.values[i]->delay == duration)
1888 repeaters.values[i]->start_repeating(this);
1893 BC_Repeater *repeater = new BC_Repeater(this, duration);
1894 repeater->initialize();
1895 repeaters.append(repeater);
1896 repeater->start_repeating();
1901 int BC_WindowBase::unset_repeat(int64_t duration)
1903 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1905 #ifdef SINGLE_THREAD
1906 BC_Display::display_global->unset_repeat(this, duration);
1908 for(int i = 0; i < repeaters.total; i++)
1910 if(repeaters.values[i]->delay == duration)
1912 repeaters.values[i]->stop_repeating();
1920 int BC_WindowBase::unset_all_repeaters()
1922 #ifdef SINGLE_THREAD
1923 BC_Display::display_global->unset_all_repeaters(this);
1925 for(int i = 0; i < repeaters.total; i++)
1927 repeaters.values[i]->stop_repeating();
1929 repeaters.remove_all_objects();
1934 // long BC_WindowBase::get_repeat_id()
1936 // return top_level->next_repeat_id++;
1939 XEvent *BC_WindowBase::new_xevent()
1941 XEvent *event = new XEvent;
1942 memset(event, 0, sizeof(*event));
1946 #ifndef SINGLE_THREAD
1947 int BC_WindowBase::arm_repeat(int64_t duration)
1949 XEvent *event = new_xevent();
1950 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1951 ptr->type = ClientMessage;
1952 ptr->message_type = RepeaterXAtom;
1954 ptr->data.l[0] = duration;
1956 // Couldn't use XSendEvent since it locked up randomly.
1962 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1967 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1969 #ifndef SINGLE_THREAD
1970 XEvent *myevent = new_xevent();
1971 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1972 ptr->type = ClientMessage;
1973 ptr->message_type = event->message_type;
1974 ptr->format = event->format;
1975 ptr->data.l[0] = event->data.l[0];
1976 ptr->data.l[1] = event->data.l[1];
1977 ptr->data.l[2] = event->data.l[2];
1978 ptr->data.l[3] = event->data.l[3];
1979 ptr->data.l[4] = event->data.l[4];
1988 Atom BC_WindowBase::create_xatom(const char *atom_name)
1990 return XInternAtom(display, atom_name, False);
1993 int BC_WindowBase::get_atoms()
1995 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1996 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1997 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
1998 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1999 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
2000 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
2001 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2007 void BC_WindowBase::init_cursors()
2009 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2010 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2011 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2012 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2013 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2014 move_cursor = XCreateFontCursor(display, XC_fleur);
2015 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2016 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2017 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2018 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2019 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2020 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2021 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2022 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2023 grabbed_cursor = create_grab_cursor();
2025 static char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2026 Colormap colormap = DefaultColormap(display, screen);
2027 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2028 rootwin, cursor_data, 8, 8);
2029 XColor black, dummy;
2030 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2031 transparent_cursor = XCreatePixmapCursor(display,
2032 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2033 // XDefineCursor(display, win, transparent_cursor);
2034 XFreePixmap(display, pixmap_bottom);
2037 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2039 int color_model = BC_TRANSPARENCY;
2043 color_model = BC_RGB8;
2046 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2049 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2052 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2058 int BC_WindowBase::init_colors()
2061 current_color_value = current_color_pixel = 0;
2063 // Get the real depth
2066 ximage = XCreateImage(top_level->display,
2068 top_level->default_depth,
2076 bits_per_pixel = ximage->bits_per_pixel;
2077 XDestroyImage(ximage);
2079 color_model = evaluate_color_model(client_byte_order,
2082 // Get the color model
2088 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2089 create_private_colors();
2093 cmap = DefaultColormap(display, screen);
2094 create_shared_colors();
2097 allocate_color_table();
2101 //cmap = DefaultColormap(display, screen);
2102 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2108 int BC_WindowBase::create_private_colors()
2113 for(int i = 0; i < 255; i++)
2115 color = (i & 0xc0) << 16;
2116 color += (i & 0x38) << 10;
2117 color += (i & 0x7) << 5;
2118 color_table[i][0] = color;
2120 create_shared_colors(); // overwrite the necessary colors on the table
2125 int BC_WindowBase::create_color(int color)
2127 if(total_colors == 256)
2129 // replace the closest match with an exact match
2130 color_table[get_color_rgb8(color)][0] = color;
2134 // add the color to the table
2135 color_table[total_colors][0] = color;
2141 int BC_WindowBase::create_shared_colors()
2143 create_color(BLACK);
2144 create_color(WHITE);
2146 create_color(LTGREY);
2147 create_color(MEGREY);
2148 create_color(MDGREY);
2149 create_color(DKGREY);
2151 create_color(LTCYAN);
2152 create_color(MECYAN);
2153 create_color(MDCYAN);
2154 create_color(DKCYAN);
2156 create_color(LTGREEN);
2157 create_color(GREEN);
2158 create_color(DKGREEN);
2160 create_color(LTPINK);
2164 create_color(LTBLUE);
2166 create_color(DKBLUE);
2168 create_color(LTYELLOW);
2169 create_color(MEYELLOW);
2170 create_color(MDYELLOW);
2171 create_color(DKYELLOW);
2173 create_color(LTPURPLE);
2174 create_color(MEPURPLE);
2175 create_color(MDPURPLE);
2176 create_color(DKPURPLE);
2178 create_color(FGGREY);
2179 create_color(MNBLUE);
2180 create_color(ORANGE);
2181 create_color(FTGREY);
2186 Cursor BC_WindowBase::create_grab_cursor()
2188 int iw = 23, iw1 = iw-1, iw2 = iw/2;
2189 int ih = 23, ih1 = ih-1, ih2 = ih/2;
2190 VFrame grab(iw,ih,BC_RGB888);
2192 grab.set_pixel_color(RED); // fg
2193 grab.draw_smooth(iw2,0, iw1,0, iw1,ih2);
2194 grab.draw_smooth(iw1,ih2, iw1,ih1, iw2,ih1);
2195 grab.draw_smooth(iw2,ih1, 0,ih1, 0,ih2);
2196 grab.draw_smooth(0,ih2, 0,0, iw2,0);
2197 grab.set_pixel_color(WHITE); // bg
2198 grab.draw_line(0,ih2, iw2-2,ih2);
2199 grab.draw_line(iw2+2,ih2, iw1,ih2);
2200 grab.draw_line(iw2,0, iw2,ih2-2);
2201 grab.draw_line(iw2,ih2+2, iw2,ih1);
2203 int bpl = (iw+7)/8, isz = bpl * ih;
2204 char img[isz]; memset(img, 0, isz);
2205 char msk[isz]; memset(msk, 0, isz);
2206 unsigned char **rows = grab.get_rows();
2207 for( int iy=0; iy<ih; ++iy ) {
2208 char *op = img + iy*bpl;
2209 char *mp = msk + iy*bpl;
2210 unsigned char *ip = rows[iy];
2211 for( int ix=0; ix<iw; ++ix,ip+=3 ) {
2212 if( ip[0] ) mp[ix>>3] |= (1<<(ix&7));
2213 if( !ip[1] ) op[ix>>3] |= (1<<(ix&7));
2216 unsigned long white_pix = WhitePixel(display, screen);
2217 unsigned long black_pix = BlackPixel(display, screen);
2218 Pixmap img_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2219 img, iw,ih, white_pix,black_pix, 1);
2220 Pixmap msk_xpm = XCreatePixmapFromBitmapData(display, rootwin,
2221 msk, iw,ih, white_pix,black_pix, 1);
2224 fc.flags = bc.flags = DoRed | DoGreen | DoBlue;
2225 fc.red = 0xffff; fc.green = fc.blue = 0; // fg
2226 bc.red = bc.green = bc.blue = 0x0000; // bg
2227 Cursor cursor = XCreatePixmapCursor(display, img_xpm,msk_xpm, &fc,&bc, iw2,ih2);
2228 XFreePixmap(display, img_xpm);
2229 XFreePixmap(display, msk_xpm);
2233 int BC_WindowBase::allocate_color_table()
2235 int red, green, blue, color;
2238 for(int i = 0; i < total_colors; i++)
2240 color = color_table[i][0];
2241 red = (color & 0xFF0000) >> 16;
2242 green = (color & 0x00FF00) >> 8;
2243 blue = color & 0xFF;
2245 col.flags = DoRed | DoGreen | DoBlue;
2246 col.red = red<<8 | red;
2247 col.green = green<<8 | green;
2248 col.blue = blue<<8 | blue;
2250 XAllocColor(display, cmap, &col);
2251 color_table[i][1] = col.pixel;
2254 XInstallColormap(display, cmap);
2258 int BC_WindowBase::init_window_shape()
2260 if(bg_pixmap && bg_pixmap->use_alpha())
2262 XShapeCombineMask(top_level->display,
2263 this->win, ShapeBounding, 0, 0,
2264 bg_pixmap->get_alpha(), ShapeSet);
2270 int BC_WindowBase::init_gc()
2272 unsigned long gcmask;
2273 gcmask = GCFont | GCGraphicsExposures;
2276 gcvalues.font = mediumfont->fid; // set the font
2277 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2278 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2280 // gcmask = GCCapStyle | GCJoinStyle;
2281 // XGetGCValues(display, gc, gcmask, &gcvalues);
2282 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2286 int BC_WindowBase::init_fonts()
2288 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2289 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2290 smallfont = XLoadQueryFont(display, "fixed");
2291 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2292 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2293 mediumfont = XLoadQueryFont(display, "fixed");
2294 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2295 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2296 largefont = XLoadQueryFont(display, "fixed");
2297 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2298 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2299 bigfont = XLoadQueryFont(display, "fixed");
2302 if(get_resources()->use_fontset)
2307 // FIXME: should check the m,d,n values
2308 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2310 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2311 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2312 if( !mediumfontset )
2313 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2314 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2316 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2317 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2319 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2320 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2321 curr_fontset = mediumfontset;
2322 get_resources()->use_fontset = 1;
2326 get_resources()->use_fontset = 0;
2333 void BC_WindowBase::init_xft()
2336 if( !get_resources()->use_xft ) return;
2337 if(!(smallfont_xft =
2338 (resources.small_font_xft[0] == '-' ?
2339 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2340 XftFontOpenName(display, screen, resources.small_font_xft))) )
2341 if(!(smallfont_xft =
2342 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2343 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2344 if(!(mediumfont_xft =
2345 (resources.medium_font_xft[0] == '-' ?
2346 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2347 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2348 if(!(mediumfont_xft =
2349 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2350 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2351 if(!(largefont_xft =
2352 (resources.large_font_xft[0] == '-' ?
2353 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2354 XftFontOpenName(display, screen, resources.large_font_xft))) )
2355 if(!(largefont_xft =
2356 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2357 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2359 (resources.big_font_xft[0] == '-' ?
2360 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2361 XftFontOpenName(display, screen, resources.big_font_xft))) )
2363 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2364 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2366 if(!(bold_smallfont_xft =
2367 (resources.small_b_font_xft[0] == '-' ?
2368 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2369 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2370 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2371 if(!(bold_mediumfont_xft =
2372 (resources.medium_b_font_xft[0] == '-' ?
2373 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2374 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2375 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2376 if(!(bold_largefont_xft =
2377 (resources.large_b_font_xft[0] == '-' ?
2378 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2379 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2380 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2382 // Extension failed to locate fonts
2383 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2384 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2385 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2386 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2387 resources.small_font_xft, smallfont_xft,
2388 resources.medium_font_xft, mediumfont_xft,
2389 resources.large_font_xft, largefont_xft,
2390 resources.big_font_xft, bigfont_xft,
2391 resources.small_b_font_xft, bold_smallfont_xft,
2392 resources.medium_b_font_xft, bold_mediumfont_xft,
2393 resources.large_b_font_xft, bold_largefont_xft);
2394 get_resources()->use_xft = 0;
2400 void BC_WindowBase::init_im()
2402 XIMStyles *xim_styles;
2405 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2407 printf("BC_WindowBase::init_im: Could not open input method.\n");
2410 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2413 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2414 XCloseIM(input_method);
2419 for(int z = 0; z < xim_styles->count_styles; z++)
2421 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2423 xim_style = xim_styles->supported_styles[z];
2431 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2432 XCloseIM(input_method);
2436 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2437 XNClientWindow, win, XNFocusWindow, win, NULL);
2440 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2441 XCloseIM(input_method);
2446 void BC_WindowBase::finit_im()
2448 if( input_context ) {
2449 XDestroyIC(input_context);
2452 if( input_method ) {
2453 XCloseIM(input_method);
2459 int BC_WindowBase::get_color(int64_t color)
2461 // return pixel of color
2462 // use this only for drawing subwindows not for bitmaps
2463 int i, test, difference;
2469 return get_color_rgb8(color);
2470 // test last color looked up
2471 if(current_color_value == color)
2472 return current_color_pixel;
2475 current_color_value = color;
2476 for(i = 0; i < total_colors; i++)
2478 if(color_table[i][0] == color)
2480 current_color_pixel = color_table[i][1];
2481 return current_color_pixel;
2485 // find nearest match
2486 difference = 0xFFFFFF;
2488 for(i = 0; i < total_colors; i++)
2490 test = abs((int)(color_table[i][0] - color));
2492 if(test < difference)
2494 current_color_pixel = color_table[i][1];
2498 return current_color_pixel;
2501 return get_color_rgb16(color);
2504 return get_color_bgr16(color);
2508 return client_byte_order == server_byte_order ?
2509 color : get_color_bgr24(color);
2517 int BC_WindowBase::get_color_rgb8(int color)
2521 pixel = (color & 0xc00000) >> 16;
2522 pixel += (color & 0xe000) >> 10;
2523 pixel += (color & 0xe0) >> 5;
2527 int64_t BC_WindowBase::get_color_rgb16(int color)
2530 result = (color & 0xf80000) >> 8;
2531 result += (color & 0xfc00) >> 5;
2532 result += (color & 0xf8) >> 3;
2537 int64_t BC_WindowBase::get_color_bgr16(int color)
2540 result = (color & 0xf80000) >> 19;
2541 result += (color & 0xfc00) >> 5;
2542 result += (color & 0xf8) << 8;
2547 int64_t BC_WindowBase::get_color_bgr24(int color)
2550 result = (color & 0xff) << 16;
2551 result += (color & 0xff00);
2552 result += (color & 0xff0000) >> 16;
2556 void BC_WindowBase::start_video()
2558 cursor_timer->update();
2560 // set_color(BLACK);
2561 // draw_box(0, 0, get_w(), get_h());
2565 void BC_WindowBase::stop_video()
2573 int64_t BC_WindowBase::get_color()
2575 return top_level->current_color;
2578 void BC_WindowBase::set_color(int64_t color)
2580 top_level->current_color = color;
2581 XSetForeground(top_level->display,
2583 top_level->get_color(color));
2586 void BC_WindowBase::set_opaque()
2588 XSetFunction(top_level->display, top_level->gc, GXcopy);
2591 void BC_WindowBase::set_inverse()
2593 XSetFunction(top_level->display, top_level->gc, GXxor);
2596 void BC_WindowBase::set_line_width(int value)
2598 this->line_width = value;
2599 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2600 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2601 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2602 JoinMiter); /* join_style */
2604 if(line_dashes > 0) {
2605 const char dashes = line_dashes;
2606 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2609 // XGCValues gcvalues;
2610 // unsigned long gcmask;
2611 // gcmask = GCCapStyle | GCJoinStyle;
2612 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2613 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2616 void BC_WindowBase::set_line_dashes(int value)
2618 line_dashes = value;
2619 // call XSetLineAttributes
2620 set_line_width(line_width);
2624 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2628 case ARROW_CURSOR: return top_level->arrow_cursor;
2629 case CROSS_CURSOR: return top_level->cross_cursor;
2630 case IBEAM_CURSOR: return top_level->ibeam_cursor;
2631 case VSEPARATE_CURSOR: return top_level->vseparate_cursor;
2632 case HSEPARATE_CURSOR: return top_level->hseparate_cursor;
2633 case MOVE_CURSOR: return top_level->move_cursor;
2634 case LEFT_CURSOR: return top_level->left_cursor;
2635 case RIGHT_CURSOR: return top_level->right_cursor;
2636 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor;
2637 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor;
2638 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor;
2639 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor;
2640 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor;
2641 case HOURGLASS_CURSOR: return top_level->hourglass_cursor;
2642 case TRANSPARENT_CURSOR: return top_level->transparent_cursor;
2643 case GRABBED_CURSOR: return top_level->grabbed_cursor;
2648 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2650 // inherit cursor from parent
2653 XUndefineCursor(top_level->display, win);
2654 current_cursor = cursor;
2657 // don't change cursor if overridden
2658 if((!top_level->is_hourglass && !is_transparent) ||
2661 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2662 if(flush) this->flush();
2665 if(!override) current_cursor = cursor;
2668 void BC_WindowBase::set_x_cursor(int cursor)
2670 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2671 XDefineCursor(top_level->display, win, temp_cursor);
2672 current_cursor = cursor;
2676 int BC_WindowBase::get_cursor()
2678 return current_cursor;
2681 void BC_WindowBase::start_hourglass()
2683 top_level->start_hourglass_recursive();
2687 void BC_WindowBase::stop_hourglass()
2689 top_level->stop_hourglass_recursive();
2693 void BC_WindowBase::start_hourglass_recursive()
2695 if(this == top_level)
2703 set_cursor(HOURGLASS_CURSOR, 1, 0);
2704 for(int i = 0; i < subwindows->total; i++)
2706 subwindows->values[i]->start_hourglass_recursive();
2711 void BC_WindowBase::stop_hourglass_recursive()
2713 if(this == top_level)
2715 if(hourglass_total == 0) return;
2716 top_level->hourglass_total--;
2719 if(!top_level->hourglass_total)
2721 top_level->is_hourglass = 0;
2723 // Cause set_cursor to perform change
2725 set_cursor(current_cursor, 1, 0);
2727 for(int i = 0; i < subwindows->total; i++)
2729 subwindows->values[i]->stop_hourglass_recursive();
2737 XFontStruct* BC_WindowBase::get_font_struct(int font)
2739 // Clear out unrelated flags
2740 if(font & BOLDFACE) font ^= BOLDFACE;
2743 case SMALLFONT: return top_level->smallfont; break;
2744 case MEDIUMFONT: return top_level->mediumfont; break;
2745 case LARGEFONT: return top_level->largefont; break;
2746 case BIGFONT: return top_level->bigfont; break;
2751 XFontSet BC_WindowBase::get_fontset(int font)
2755 if(get_resources()->use_fontset)
2757 switch(font & 0xff) {
2758 case SMALLFONT: fs = top_level->smallfontset; break;
2759 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2760 case LARGEFONT: fs = top_level->largefontset; break;
2761 case BIGFONT: fs = top_level->bigfontset; break;
2769 XftFont* BC_WindowBase::get_xft_struct(int font)
2772 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2773 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2774 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2775 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2776 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2777 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2778 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2793 int BC_WindowBase::get_current_font()
2795 return top_level->current_font;
2798 void BC_WindowBase::set_font(int font)
2800 top_level->current_font = font;
2803 if(get_resources()->use_xft) {}
2806 if(get_resources()->use_fontset) {
2810 if(get_font_struct(font))
2812 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2818 void BC_WindowBase::set_fontset(int font)
2822 if(get_resources()->use_fontset) {
2824 case SMALLFONT: fs = top_level->smallfontset; break;
2825 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2826 case LARGEFONT: fs = top_level->largefontset; break;
2827 case BIGFONT: fs = top_level->bigfontset; break;
2835 XFontSet BC_WindowBase::get_curr_fontset(void)
2837 if(get_resources()->use_fontset)
2838 return curr_fontset;
2842 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2845 if(get_resources()->use_xft && get_xft_struct(font))
2848 #ifdef X_HAVE_UTF8_STRING
2849 if(get_resources()->locale_utf8)
2851 XftTextExtentsUtf8(top_level->display,
2852 get_xft_struct(font),
2853 (const XftChar8 *)text,
2860 XftTextExtents8(top_level->display,
2861 get_xft_struct(font),
2862 (const XftChar8 *)text,
2866 return extents.xOff;
2870 if(get_resources()->use_fontset && top_level->get_fontset(font))
2871 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2873 if(get_font_struct(font))
2874 return XTextWidth(get_font_struct(font), text, length);
2880 case MEDIUM_7SEGMENT:
2881 return get_resources()->medium_7segment[0]->get_w() * length;
2891 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2893 int i, j, w = 0, line_w = 0;
2894 if(length < 0) length = strlen(text);
2896 for(i = 0, j = 0; i <= length; i++)
2901 line_w = get_single_text_width(font, &text[j], i - j);
2907 line_w = get_single_text_width(font, &text[j], length - j);
2909 if(line_w > w) w = line_w;
2912 if(i > length && w == 0)
2914 w = get_single_text_width(font, text, length);
2920 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2923 if( length < 0 ) length = wcslen(text);
2925 for( i=j=0; i<length && text[i]; ++i ) {
2926 if( text[i] != '\n' ) continue;
2928 int lw = get_single_text_width(font, &text[j], i-j);
2929 if( w < lw ) w = lw;
2934 int lw = get_single_text_width(font, &text[j], length-j);
2935 if( w < lw ) w = lw;
2941 int BC_WindowBase::get_text_ascent(int font)
2945 if( (fstruct = get_xft_struct(font)) != 0 )
2946 return fstruct->ascent;
2948 if(get_resources()->use_fontset && top_level->get_fontset(font))
2950 XFontSetExtents *extents;
2952 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2953 return -extents->max_logical_extent.y;
2956 if(get_font_struct(font))
2957 return top_level->get_font_struct(font)->ascent;
2960 case MEDIUM_7SEGMENT:
2961 return get_resources()->medium_7segment[0]->get_h();
2966 int BC_WindowBase::get_text_descent(int font)
2970 if( (fstruct = get_xft_struct(font)) != 0 )
2971 return fstruct->descent;
2973 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2974 XFontSetExtents *extents;
2975 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2976 return (extents->max_logical_extent.height
2977 + extents->max_logical_extent.y);
2980 if(get_font_struct(font))
2981 return top_level->get_font_struct(font)->descent;
2986 int BC_WindowBase::get_text_height(int font, const char *text)
2991 if( (fstruct = get_xft_struct(font)) != 0 )
2992 rowh = fstruct->height;
2995 rowh = get_text_ascent(font) + get_text_descent(font);
2997 if(!text) return rowh;
2999 // Add height of lines
3000 int h = 0, i, length = strlen(text);
3001 for(i = 0; i <= length; i++)
3012 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
3014 if(color_model < 0) color_model = top_level->get_color_model();
3015 return new BC_Bitmap(top_level, w, h, color_model);
3018 void BC_WindowBase::init_wait()
3020 #ifndef SINGLE_THREAD
3021 if(window_type != MAIN_WINDOW)
3022 top_level->init_wait();
3023 init_lock->lock("BC_WindowBase::init_wait");
3024 init_lock->unlock();
3028 int BC_WindowBase::accel_available(int color_model, int lock_it)
3030 if( window_type != MAIN_WINDOW )
3031 return top_level->accel_available(color_model, lock_it);
3033 lock_window("BC_WindowBase::accel_available");
3035 switch(color_model) {
3037 grab_port_id(color_model);
3041 grab_port_id(color_model);
3050 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3051 return xvideo_port_id >= 0 ? 1 : 0;
3055 int BC_WindowBase::grab_port_id(int color_model)
3057 if( !get_resources()->use_xvideo || // disabled
3058 !get_resources()->use_shm ) // Only local server is fast enough.
3060 if( xvideo_port_id >= 0 )
3061 return xvideo_port_id;
3063 unsigned int ver, rev, reqBase, eventBase, errorBase;
3064 if( Success != XvQueryExtension(display, // XV extension is available
3065 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3068 // XV adaptors are available
3069 unsigned int numAdapt = 0;
3070 XvAdaptorInfo *info = 0;
3071 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3075 // Translate from color_model to X color model
3076 int x_color_model = BC_CModels::bc_to_x(color_model);
3078 // Get adaptor with desired color model
3079 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3080 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3081 // adaptor supports XvImages
3082 int numFormats = 0, numPorts = info[i].num_ports;
3083 XvImageFormatValues *formats =
3084 XvListImageFormats(display, info[i].base_id, &numFormats);
3085 if( !formats ) continue;
3087 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3088 if( formats[j].id != x_color_model ) continue;
3089 // this adaptor supports the desired format, grab a port
3090 for( int k=0; k<numPorts; ++k ) {
3091 if( Success == XvGrabPort(top_level->display,
3092 info[i].base_id+k, CurrentTime) ) {
3093 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3094 xvideo_port_id = info[i].base_id + k;
3102 XvFreeAdaptorInfo(info);
3104 return xvideo_port_id;
3108 int BC_WindowBase::show_window(int flush)
3110 for(int i = 0; i < subwindows->size(); i++)
3112 subwindows->get(i)->show_window(0);
3115 XMapWindow(top_level->display, win);
3116 if(flush) XFlush(top_level->display);
3117 // XSync(top_level->display, 0);
3122 int BC_WindowBase::hide_window(int flush)
3124 for(int i = 0; i < subwindows->size(); i++)
3126 subwindows->get(i)->hide_window(0);
3129 XUnmapWindow(top_level->display, win);
3130 if(flush) this->flush();
3135 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3137 subwindows->append((BC_SubWindow*)menu_bar);
3139 menu_bar->parent_window = this;
3140 menu_bar->top_level = this->top_level;
3141 menu_bar->initialize();
3145 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3147 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3148 if(this != top_level) return top_level->add_popup(window);
3149 popups.append(window);
3153 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3155 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3156 if(this != top_level)
3157 top_level->remove_popup(window);
3159 popups.remove(window);
3160 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3164 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3166 subwindows->append(subwindow);
3168 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3170 // parent window must be set before the subwindow initialization
3171 subwindow->parent_window = this;
3172 subwindow->top_level = this->top_level;
3174 // Execute derived initialization
3175 subwindow->initialize();
3180 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3182 return add_subwindow(subwindow);
3185 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3187 if( !top_level->flash_enabled ) return 0;
3188 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3190 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3193 XClearArea(top_level->display, win, x, y, w, h, 0);
3197 XClearWindow(top_level->display, win);
3205 int BC_WindowBase::flash(int flush)
3207 flash(-1, -1, -1, -1, flush);
3211 void BC_WindowBase::flush()
3213 //if(!get_window_lock())
3214 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3215 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3216 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3217 int locked = get_window_lock();
3218 if( !locked ) lock_window("BC_WindowBase::flush");
3219 XFlush(top_level->display);
3220 if( !locked ) unlock_window();
3223 void BC_WindowBase::sync_display()
3225 int locked = get_window_lock();
3226 if( !locked ) lock_window("BC_WindowBase::sync_display");
3227 XSync(top_level->display, False);
3228 if( !locked ) unlock_window();
3231 int BC_WindowBase::get_window_lock()
3233 #ifdef SINGLE_THREAD
3234 return BC_Display::display_global->get_display_locked();
3236 return top_level->window_lock;
3240 int BC_WindowBase::lock_window(const char *location)
3242 if(top_level && top_level != this)
3244 top_level->lock_window(location);
3249 SET_LOCK(this, title, location);
3250 #ifdef SINGLE_THREAD
3251 BC_Display::lock_display(location);
3253 XLockDisplay(top_level->display);
3254 top_level->display_lock_owner = pthread_self();
3257 ++top_level->window_lock;
3261 printf("BC_WindowBase::lock_window top_level NULL\n");
3266 int BC_WindowBase::unlock_window()
3268 if(top_level && top_level != this)
3270 top_level->unlock_window();
3277 // if(!top_level->window_lock)
3279 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3283 if( top_level->window_lock > 0 )
3284 if( --top_level->window_lock == 0 )
3285 top_level->display_lock_owner = 0;
3286 #ifdef SINGLE_THREAD
3287 BC_Display::unlock_display();
3289 XUnlockDisplay(top_level->display);
3294 printf("BC_WindowBase::unlock_window top_level NULL\n");
3299 void BC_WindowBase::set_done(int return_value)
3301 if(done_set) return;
3303 if(window_type != MAIN_WINDOW)
3304 top_level->set_done(return_value);
3307 #ifdef SINGLE_THREAD
3308 this->return_value = return_value;
3309 BC_Display::display_global->arm_completion(this);
3310 completion_lock->unlock();
3311 #else // SINGLE_THREAD
3313 if( !event_thread ) return;
3314 XEvent *event = new_xevent();
3315 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3316 event->type = ClientMessage;
3317 ptr->message_type = SetDoneXAtom;
3319 this->return_value = return_value;
3320 // May lock up here because XSendEvent doesn't work too well
3321 // asynchronous with XNextEvent.
3322 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3324 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3330 void BC_WindowBase::close(int return_value)
3332 hide_window(); flush();
3333 set_done(return_value);
3336 int BC_WindowBase::grab(BC_WindowBase *window)
3338 if( window->active_grab && this != window->active_grab ) return 0;
3339 window->active_grab = this;
3340 this->grab_active = window;
3343 int BC_WindowBase::ungrab(BC_WindowBase *window)
3345 if( window->active_grab && this != window->active_grab ) return 0;
3346 window->active_grab = 0;
3347 this->grab_active = 0;
3350 int BC_WindowBase::grab_buttons()
3352 XSync(top_level->display, False);
3353 if( XGrabButton(top_level->display, AnyButton, AnyModifier,
3354 top_level->rootwin, True, ButtonPressMask | ButtonReleaseMask,
3355 GrabModeAsync, GrabModeSync, None, None) == GrabSuccess ) {
3356 set_active_subwindow(this);
3361 void BC_WindowBase::ungrab_buttons()
3363 XUngrabButton(top_level->display, AnyButton, AnyModifier, top_level->rootwin);
3364 set_active_subwindow(0);
3367 void BC_WindowBase::grab_cursor()
3369 Cursor cursor_grab = get_cursor_struct(GRABBED_CURSOR);
3370 XGrabPointer(top_level->display, top_level->rootwin, True,
3371 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
3372 GrabModeAsync, GrabModeAsync, None, cursor_grab, CurrentTime);
3374 void BC_WindowBase::ungrab_cursor()
3376 XUngrabPointer(top_level->display, CurrentTime);
3380 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3381 // this is the bounding box of all the screens
3383 int BC_WindowBase::get_root_w(int lock_display)
3385 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3386 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3387 int result = WidthOfScreen(def_screen);
3388 if(lock_display) unlock_window();
3392 int BC_WindowBase::get_root_h(int lock_display)
3394 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3395 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3396 int result = HeightOfScreen(def_screen);
3397 if(lock_display) unlock_window();
3401 XineramaScreenInfo *
3402 BC_WindowBase::get_xinerama_info(int screen)
3404 if( !xinerama_info || !xinerama_screens ) return 0;
3406 for( int i=0; i<xinerama_screens; ++i )
3407 if( xinerama_info[i].screen_number == screen )
3408 return &xinerama_info[i];
3411 int top_x = get_x(), top_y = get_y();
3412 for( int i=0; i<xinerama_screens; ++i ) {
3413 int scr_y = top_y - xinerama_info[i].y_org;
3414 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3415 int scr_x = top_x - xinerama_info[i].x_org;
3416 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3417 return &xinerama_info[i];
3422 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3425 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3426 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3429 int root_w = get_root_w(0);
3430 int root_h = get_root_h(0);
3431 // Shift X based on position of current window if dual head
3432 if( (float)root_w/root_h > 1.8 ) {
3433 root_w = get_screen_w(0, 0);
3434 if( top_level->get_x() >= root_w )
3439 result = info->x_org;
3440 if(lock_display) unlock_window();
3444 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3446 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3447 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3448 int result = !info ? 0 : info->y_org;
3449 if(lock_display) unlock_window();
3453 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3456 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3457 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3459 int width = get_root_w(0);
3460 int height = get_root_h(0);
3461 if( (float)width/height > 1.8 ) {
3462 // If dual head, the screen width is > 16x9
3463 // but we only want to fill one screen
3464 // this code assumes the "big" screen is on the right
3465 int scr_w0 = width / 2;
3467 case 600: scr_w0 = 800; break;
3468 case 720: scr_w0 = 1280; break;
3469 case 1024: scr_w0 = 1280; break;
3470 case 1200: scr_w0 = 1600; break;
3471 case 1080: scr_w0 = 1920; break;
3473 int scr_w1 = width - scr_w0;
3474 result = screen > 0 ? scr_w1 :
3475 screen == 0 ? scr_w0 :
3476 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3482 result = info->width;
3483 if(lock_display) unlock_window();
3487 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3489 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3490 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3491 int result = info ? info->height : get_root_h(0);
3492 if(lock_display) unlock_window();
3496 // Bottom right corner
3497 int BC_WindowBase::get_x2()
3502 int BC_WindowBase::get_y2()
3507 int BC_WindowBase::get_video_on()
3512 int BC_WindowBase::get_hidden()
3514 return top_level->hidden;
3517 int BC_WindowBase::cursor_inside()
3519 return (top_level->cursor_x >= 0 &&
3520 top_level->cursor_y >= 0 &&
3521 top_level->cursor_x < w &&
3522 top_level->cursor_y < h);
3525 BC_WindowBase* BC_WindowBase::get_top_level()
3530 BC_WindowBase* BC_WindowBase::get_parent()
3532 return parent_window;
3535 int BC_WindowBase::get_color_model()
3537 return top_level->color_model;
3540 BC_Resources* BC_WindowBase::get_resources()
3542 return &BC_WindowBase::resources;
3545 BC_Synchronous* BC_WindowBase::get_synchronous()
3547 return BC_WindowBase::resources.get_synchronous();
3550 int BC_WindowBase::get_bg_color()
3555 void BC_WindowBase::set_bg_color(int color)
3557 this->bg_color = color;
3560 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3565 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3567 top_level->active_subwindow = subwindow;
3570 int BC_WindowBase::activate()
3575 int BC_WindowBase::deactivate()
3577 if(window_type == MAIN_WINDOW)
3579 if( top_level->active_menubar ) {
3580 top_level->active_menubar->deactivate();
3581 top_level->active_menubar = 0;
3583 if( top_level->active_popup_menu ) {
3584 top_level->active_popup_menu->deactivate();
3585 top_level->active_popup_menu = 0;
3587 if( top_level->active_subwindow ) {
3588 top_level->active_subwindow->deactivate();
3589 top_level->active_subwindow = 0;
3591 if( top_level->motion_events && top_level->last_motion_win == this->win )
3592 top_level->motion_events = 0;
3598 int BC_WindowBase::cycle_textboxes(int amount)
3601 BC_WindowBase *new_textbox = 0;
3605 BC_WindowBase *first_textbox = 0;
3606 find_next_textbox(&first_textbox, &new_textbox, result);
3607 if(!new_textbox) new_textbox = first_textbox;
3613 BC_WindowBase *last_textbox = 0;
3614 find_prev_textbox(&last_textbox, &new_textbox, result);
3615 if(!new_textbox) new_textbox = last_textbox;
3619 if(new_textbox != active_subwindow)
3622 new_textbox->activate();
3628 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3630 // Search subwindows for textbox
3631 for(int i = 0; i < subwindows->total && result < 2; i++)
3633 BC_WindowBase *test_subwindow = subwindows->values[i];
3634 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3641 if(!*first_textbox) *first_textbox = this;
3645 if(top_level->active_subwindow == this)
3651 *next_textbox = this;
3658 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3664 if(!*last_textbox) *last_textbox = this;
3668 if(top_level->active_subwindow == this)
3674 *prev_textbox = this;
3679 // Search subwindows for textbox
3680 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3682 BC_WindowBase *test_subwindow = subwindows->values[i];
3683 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3688 BC_Clipboard* BC_WindowBase::get_clipboard()
3690 #ifdef SINGLE_THREAD
3691 return BC_Display::display_global->clipboard;
3693 return top_level->clipboard;
3697 Atom BC_WindowBase::to_clipboard(const char *data, long len, int clipboard_num)
3699 return get_clipboard()->to_clipboard(this, data, len, clipboard_num);
3702 long BC_WindowBase::from_clipboard(char *data, long maxlen, int clipboard_num)
3704 return get_clipboard()->from_clipboard(data, maxlen, clipboard_num);
3707 long BC_WindowBase::clipboard_len(int clipboard_num)
3709 return get_clipboard()->clipboard_len(clipboard_num);
3712 int BC_WindowBase::do_selection_clear(Window win)
3714 top_level->event_win = win;
3715 return dispatch_selection_clear();
3718 int BC_WindowBase::dispatch_selection_clear()
3721 for( int i=0; i<subwindows->total && !result; ++i )
3722 result = subwindows->values[i]->dispatch_selection_clear();
3724 result = selection_clear_event();
3729 void BC_WindowBase::get_relative_cursor_xy(int &x, int &y, int lock_window)
3731 int abs_x, abs_y, win_x, win_y;
3732 unsigned int temp_mask;
3735 if(lock_window) this->lock_window("BC_WindowBase::get_relative_cursor_xy");
3736 XQueryPointer(top_level->display, top_level->win,
3737 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3740 XTranslateCoordinates(top_level->display, top_level->rootwin,
3741 win, abs_x, abs_y, &x, &y, &temp_win);
3742 if(lock_window) this->unlock_window();
3744 int BC_WindowBase::get_relative_cursor_x(int lock_window)
3747 get_relative_cursor_xy(x, y, lock_window);
3750 int BC_WindowBase::get_relative_cursor_y(int lock_window)
3753 get_relative_cursor_xy(x, y, lock_window);
3757 void BC_WindowBase::get_abs_cursor_xy(int &abs_x, int &abs_y, int lock_window)
3760 unsigned int temp_mask;
3763 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_xy");
3764 XQueryPointer(top_level->display, top_level->win,
3765 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3767 if(lock_window) this->unlock_window();
3769 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3772 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3775 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3778 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3782 void BC_WindowBase::get_pop_cursor_xy(int &px, int &py, int lock_window)
3785 get_abs_cursor_xy(px, py, lock_window);
3786 if( px < margin ) px = margin;
3787 if( py < margin ) py = margin;
3788 int wd = get_screen_w(lock_window,-1) - margin;
3789 if( px > wd ) px = wd;
3790 int ht = get_screen_h(lock_window,-1) - margin;
3791 if( py > ht ) py = ht;
3793 int BC_WindowBase::get_pop_cursor_x(int lock_window)
3796 get_pop_cursor_xy(px, py, lock_window);
3799 int BC_WindowBase::get_pop_cursor_y(int lock_window)
3802 get_pop_cursor_xy(px, py, lock_window);
3806 int BC_WindowBase::match_window(Window win)
3808 if (this->win == win) return 1;
3810 for(int i = 0; i < subwindows->total; i++) {
3811 result = subwindows->values[i]->match_window(win);
3812 if (result) return result;
3818 int BC_WindowBase::get_cursor_over_window()
3820 if(top_level != this) return top_level->get_cursor_over_window();
3822 int abs_x, abs_y, win_x, win_y;
3823 unsigned int temp_mask;
3824 Window root_return, child_return;
3826 int ret = XQueryPointer(display, win,
3827 &root_return, &child_return, &abs_x, &abs_y,
3828 &win_x, &win_y, &temp_mask);
3829 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3830 // __LINE__, title, win, ret, child_return);
3832 int result = !ret || child_return == None ? 0 :
3833 match_window(child_return);
3837 int BC_WindowBase::cursor_above()
3840 get_relative_cursor_xy(rx, ry);
3841 return rx < 0 || rx >= get_w() ||
3842 ry < 0 || ry >= get_h() ? 0 : 1;
3845 int BC_WindowBase::get_drag_x()
3847 return top_level->drag_x;
3850 int BC_WindowBase::get_drag_y()
3852 return top_level->drag_y;
3855 int BC_WindowBase::get_cursor_x()
3857 return top_level->cursor_x;
3860 int BC_WindowBase::get_cursor_y()
3862 return top_level->cursor_y;
3865 int BC_WindowBase::dump_windows()
3867 printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
3868 this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
3869 for(int i = 0; i < subwindows->size(); i++)
3870 subwindows->get(i)->dump_windows();
3871 for(int i = 0; i < popups.size(); i++) {
3872 BC_WindowBase *p = popups[i];
3873 printf("\tBC_WindowBase::dump_windows popup=%p win=%p '%s', %dx%d+%d+%d %s\n",
3874 p, (void*)p->win, p->title, p->w,p->h,p->x,p->y, typeid(*p).name());
3879 int BC_WindowBase::is_event_win()
3881 return this->win == top_level->event_win;
3884 void BC_WindowBase::set_dragging(int value)
3886 is_dragging = value;
3889 int BC_WindowBase::get_dragging()
3894 int BC_WindowBase::get_buttonpress()
3896 return top_level->button_number;
3899 int BC_WindowBase::get_button_down()
3901 return top_level->button_down;
3904 int BC_WindowBase::alt_down()
3906 return top_level->alt_mask;
3909 int BC_WindowBase::shift_down()
3911 return top_level->shift_mask;
3914 int BC_WindowBase::ctrl_down()
3916 return top_level->ctrl_mask;
3919 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3922 *length = top_level->wkey_string_length;
3923 return top_level->wkey_string;
3926 #ifdef X_HAVE_UTF8_STRING
3927 char* BC_WindowBase::get_keypress_utf8()
3929 return top_level->key_pressed_utf8;
3934 int BC_WindowBase::get_keypress()
3936 return top_level->key_pressed;
3939 int BC_WindowBase::get_double_click()
3941 return top_level->double_click;
3944 int BC_WindowBase::get_triple_click()
3946 return top_level->triple_click;
3949 int BC_WindowBase::get_bgcolor()
3954 int BC_WindowBase::resize_window(int w, int h)
3956 if(this->w == w && this->h == h) return 0;
3958 if(window_type == MAIN_WINDOW && !allow_resize)
3960 XSizeHints size_hints;
3961 size_hints.flags = PSize | PMinSize | PMaxSize;
3962 size_hints.width = w;
3963 size_hints.height = h;
3964 size_hints.min_width = w;
3965 size_hints.max_width = w;
3966 size_hints.min_height = h;
3967 size_hints.max_height = h;
3968 XSetNormalHints(top_level->display, win, &size_hints);
3970 XResizeWindow(top_level->display, win, w, h);
3975 pixmap = new BC_Pixmap(this, w, h);
3977 // Propagate to menubar
3978 for(int i = 0; i < subwindows->total; i++)
3980 subwindows->values[i]->dispatch_resize_event(w, h);
3983 draw_background(0, 0, w, h);
3984 if(top_level == this && get_resources()->recursive_resizing)
3985 resize_history.append(new BC_ResizeCall(w, h));
3989 // The only way for resize events to be propagated is by updating the internal w and h
3990 int BC_WindowBase::resize_event(int w, int h)
3992 if(window_type == MAIN_WINDOW)
4000 int BC_WindowBase::reposition_window(int x, int y)
4002 reposition_window(x, y, -1, -1);
4007 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
4011 // Some tools set their own dimensions before calling this, causing the
4012 // resize check to skip.
4016 if(w > 0 && w != this->w)
4022 if(h > 0 && h != this->h)
4028 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
4031 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
4033 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
4035 if(translation_count && window_type == MAIN_WINDOW)
4037 // KDE shifts window right and down.
4038 // FVWM leaves window alone and adds border around it.
4039 XMoveResizeWindow(top_level->display,
4041 x - BC_DisplayInfo::auto_reposition_x,
4042 y - BC_DisplayInfo::auto_reposition_y,
4048 XMoveResizeWindow(top_level->display,
4059 pixmap = new BC_Pixmap(this, this->w, this->h);
4060 clear_box(0,0, this->w, this->h);
4061 // Propagate to menubar
4062 for(int i = 0; i < subwindows->total; i++)
4064 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
4067 // draw_background(0, 0, w, h);
4073 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
4075 return reposition_window(get_x()+dx, get_y()+dy, w, h);
4078 int BC_WindowBase::reposition_window_relative(int dx, int dy)
4080 return reposition_window_relative(dx, dy, -1, -1);
4083 void BC_WindowBase::set_tooltips(int v)
4085 get_resources()->tooltips_enabled = v;
4088 void BC_WindowBase::set_force_tooltip(int v)
4093 int BC_WindowBase::raise_window(int do_flush)
4095 XRaiseWindow(top_level->display, win);
4096 if(do_flush) XFlush(top_level->display);
4100 int BC_WindowBase::lower_window(int do_flush)
4102 XLowerWindow(top_level->display, win);
4103 if(do_flush) XFlush(top_level->display);
4107 void BC_WindowBase::set_background(VFrame *bitmap)
4109 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4111 bg_pixmap = new BC_Pixmap(this, bitmap, PIXMAP_OPAQUE);
4112 shared_bg_pixmap = 0;
4113 draw_background(0, 0, w, h);
4116 void BC_WindowBase::put_title(const char *text)
4118 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4119 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4120 *cp++ = *bp >= ' ' ? *bp : ' ';
4124 void BC_WindowBase::set_title(const char *text, int utf8)
4126 // utf8>0: wm + net_wm, utf8=0: wm only, utf<0: net_wm only
4128 const unsigned char *wm_title = (const unsigned char *)title;
4129 int title_len = strlen((const char *)title);
4131 Atom xa_wm_name = XA_WM_NAME;
4132 Atom xa_icon_name = XA_WM_ICON_NAME;
4133 Atom xa_string = XA_STRING;
4134 XChangeProperty(display, win, xa_wm_name, xa_string, 8,
4135 PropModeReplace, wm_title, title_len);
4136 XChangeProperty(display, win, xa_icon_name, xa_string, 8,
4137 PropModeReplace, wm_title, title_len);
4140 Atom xa_net_wm_name = XInternAtom(display, "_NET_WM_NAME", True);
4141 Atom xa_net_icon_name = XInternAtom(display, "_NET_WM_ICON_NAME", True);
4142 Atom xa_utf8_string = XInternAtom(display, "UTF8_STRING", True);
4143 XChangeProperty(display, win, xa_net_wm_name, xa_utf8_string, 8,
4144 PropModeReplace, wm_title, title_len);
4145 XChangeProperty(display, win, xa_net_icon_name, xa_utf8_string, 8,
4146 PropModeReplace, wm_title, title_len);
4151 const char *BC_WindowBase::get_title()
4156 int BC_WindowBase::get_toggle_value()
4158 return toggle_value;
4161 int BC_WindowBase::get_toggle_drag()
4166 int BC_WindowBase::set_icon(VFrame *data)
4168 if(icon_pixmap) delete icon_pixmap;
4169 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4171 if(icon_window) delete icon_window;
4172 icon_window = new BC_Popup(this,
4175 icon_pixmap->get_w(),
4176 icon_pixmap->get_h(),
4178 1, // All windows are hidden initially
4182 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4183 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4184 wm_hints.icon_mask = icon_pixmap->get_alpha();
4185 wm_hints.icon_window = icon_window->win;
4186 wm_hints.window_group = XGroupLeader;
4188 // for(int i = 0; i < 1000; i++)
4189 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4192 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4193 XSync(top_level->display, 0);
4197 int BC_WindowBase::set_w(int w)
4203 int BC_WindowBase::set_h(int h)
4209 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4211 BC_Resources *resources = get_resources();
4212 char string[BCTEXTLEN];
4213 int newest_id = - 1;
4214 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4216 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4217 resources->filebox_history[i].path[0] = 0;
4218 defaults->get(string, resources->filebox_history[i].path);
4219 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4220 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4221 if(resources->filebox_history[i].id > newest_id)
4222 newest_id = resources->filebox_history[i].id;
4225 resources->filebox_id = newest_id + 1;
4226 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4227 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4228 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4229 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4230 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4231 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4232 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4233 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4234 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4235 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4236 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4237 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4241 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4243 BC_Resources *resources = get_resources();
4244 char string[BCTEXTLEN];
4245 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4247 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4248 defaults->update(string, resources->filebox_history[i].path);
4249 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4250 defaults->update(string, resources->filebox_history[i].id);
4252 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4253 defaults->update("FILEBOX_W", resources->filebox_w);
4254 defaults->update("FILEBOX_H", resources->filebox_h);
4255 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4256 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4257 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4258 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4259 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4260 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4261 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4262 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4263 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4269 // For some reason XTranslateCoordinates can take a long time to return.
4270 // We work around this by only calling it when the event windows are different.
4271 void BC_WindowBase::translate_coordinates(Window src_w,
4283 *dest_x_return = src_x;
4284 *dest_y_return = src_y;
4288 XTranslateCoordinates(top_level->display,
4296 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4300 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4302 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4305 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4307 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4315 #ifdef HAVE_LIBXXF86VM
4316 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4320 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4322 XF86VidModeModeInfo **vm_modelines;
4323 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4324 for (i = 0; i < vm_count; i++) {
4325 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4328 display = top_level->display;
4329 if (vm_modelines[*vm]->hdisplay == *width)
4333 *width = vm_modelines[*vm]->hdisplay;
4334 *height = vm_modelines[*vm]->vdisplay;
4339 void BC_WindowBase::scale_vm(int vm)
4341 int foo,bar,dotclock;
4342 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4345 XF86VidModeModeInfo **vm_modelines;
4346 XF86VidModeModeLine vml;
4347 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4348 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4349 orig_modeline.dotclock = dotclock;
4350 orig_modeline.hdisplay = vml.hdisplay;
4351 orig_modeline.hsyncstart = vml.hsyncstart;
4352 orig_modeline.hsyncend = vml.hsyncend;
4353 orig_modeline.htotal = vml.htotal;
4354 orig_modeline.vdisplay = vml.vdisplay;
4355 orig_modeline.vsyncstart = vml.vsyncstart;
4356 orig_modeline.vsyncend = vml.vsyncend;
4357 orig_modeline.vtotal = vml.vtotal;
4358 orig_modeline.flags = vml.flags;
4359 orig_modeline.privsize = vml.privsize;
4360 // orig_modeline.private = vml.private;
4361 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4362 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4363 XFlush(top_level->display);
4367 void BC_WindowBase::restore_vm()
4369 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4370 XFlush(top_level->display);
4393 #ifndef SINGLE_THREAD
4394 int BC_WindowBase::get_event_count()
4396 event_lock->lock("BC_WindowBase::get_event_count");
4397 int result = common_events.total;
4398 event_lock->unlock();
4402 XEvent* BC_WindowBase::get_event()
4405 while(!done && !result)
4407 event_condition->lock("BC_WindowBase::get_event");
4408 event_lock->lock("BC_WindowBase::get_event");
4410 if(common_events.total && !done)
4412 result = common_events.values[0];
4413 common_events.remove_number(0);
4416 event_lock->unlock();
4421 void BC_WindowBase::put_event(XEvent *event)
4423 event_lock->lock("BC_WindowBase::put_event");
4424 common_events.append(event);
4425 event_lock->unlock();
4426 event_condition->unlock();
4429 void BC_WindowBase::dequeue_events(Window win)
4431 event_lock->lock("BC_WindowBase::dequeue_events");
4433 int out = 0, total = common_events.size();
4434 for( int in=0; in<total; ++in ) {
4435 if( common_events[in]->xany.window == win ) continue;
4436 common_events[out++] = common_events[in];
4438 common_events.total = out;
4440 event_lock->unlock();
4443 #endif // SINGLE_THREAD
4445 int BC_WindowBase::get_id()
4451 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4453 int w = vframe->get_w(), h = vframe->get_h();
4454 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4455 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4460 void BC_WindowBase::flicker(int n, int ms)
4462 int color = get_bg_color();
4463 for( int i=2*n; --i>=0; ) {
4464 set_inverse(); set_bg_color(WHITE);
4465 clear_box(0,0, w,h); flash(1);
4466 sync_display(); Timer::delay(ms);
4468 set_bg_color(color);