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"
57 #include <X11/extensions/Xinerama.h>
58 #include <X11/extensions/Xvlib.h>
59 #include <X11/extensions/shape.h>
60 #include <X11/XF86keysym.h>
61 #include <X11/Sunkeysym.h>
64 BC_ResizeCall::BC_ResizeCall(int w, int h)
76 int BC_WindowBase::shm_completion_event = -1;
80 BC_Resources BC_WindowBase::resources;
82 Window XGroupLeader = 0;
84 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
85 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
87 BC_WindowBase::BC_WindowBase(int opts)
90 //printf("BC_WindowBase::BC_WindowBase 1\n");
91 BC_WindowBase::initialize();
94 BC_WindowBase::~BC_WindowBase()
97 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
99 if(window_type == MAIN_WINDOW)
100 lock_window("BC_WindowBase::~BC_WindowBase");
103 #ifdef HAVE_LIBXXF86VM
104 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
111 if(window_type != MAIN_WINDOW)
114 XSelectInput(top_level->display, this->win, 0);
115 motion_events = resize_events = translation_events = 0;
116 #ifndef SINGLE_THREAD
117 top_level->dequeue_events(win);
119 if(top_level->active_menubar == this) top_level->active_menubar = 0;
120 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
121 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
122 // Remove pointer from parent window to this
123 parent_window->subwindows->remove(this);
126 if(icon_window) delete icon_window;
127 if(window_type == POPUP_WINDOW)
128 parent_window->remove_popup(this);
130 // Delete the subwindows
133 while(subwindows->total)
135 // Subwindow removes its own pointer
136 delete subwindows->values[0];
144 if( get_resources()->get_synchronous() &&
145 (top_level->options & GLX_WINDOW) && glx_win != 0 )
146 get_resources()->get_synchronous()->delete_window(this);
149 XDestroyWindow(top_level->display, win);
151 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
152 if(icon_pixmap) delete icon_pixmap;
153 if(temp_bitmap) delete temp_bitmap;
154 top_level->active_bitmaps.remove_buffers(this);
155 if(_7segment_pixmaps)
157 for(int i = 0; i < TOTAL_7SEGMENT; i++)
158 delete _7segment_pixmaps[i];
160 delete [] _7segment_pixmaps;
165 if(window_type == MAIN_WINDOW)
167 XFreeGC(display, gc);
168 static XFontStruct *BC_WindowBase::*xfont[] = {
169 &BC_WindowBase::smallfont,
170 &BC_WindowBase::mediumfont,
171 &BC_WindowBase::largefont,
172 &BC_WindowBase::bigfont,
174 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
175 XFreeFont(display, this->*xfont[i]);
177 // bug in X causes XRenderExtensionInfo to be damaged if this is done here
178 // left to be done in XCloseDisplay by Xlib.
179 #if defined(HAVE_XFT) && 0
180 static void *BC_WindowBase::*xft_font[] = {
181 &BC_WindowBase::smallfont_xft,
182 &BC_WindowBase::mediumfont_xft,
183 &BC_WindowBase::largefont_xft,
184 &BC_WindowBase::bigfont_xft,
185 &BC_WindowBase::bold_smallfont_xft,
186 &BC_WindowBase::bold_mediumfont_xft,
187 &BC_WindowBase::bold_largefont_xft,
189 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
190 XftFont *xft = (XftFont *)(this->*xft_font[i]);
191 if( xft ) XftFontClose (display, xft);
199 XFree(xinerama_info);
200 xinerama_screens = 0;
202 if( xvideo_port_id >= 0 )
203 XvUngrabPort(display, xvideo_port_id, CurrentTime);
206 // Can't close display if another thread is waiting for events.
207 // Synchronous thread must delete display it owns a GLX_WINDOW
208 // Must be last reference to display.
209 #ifndef SINGLE_THREAD
211 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
212 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
213 "implemented for BC_Pixmap.\n"));
214 get_resources()->get_synchronous()->delete_display(this);
218 XCloseDisplay(display);
219 // clipboard uses a different display connection
220 clipboard->stop_clipboard();
222 #endif // SINGLE_THREAD
225 resize_history.remove_all_objects();
227 #ifndef SINGLE_THREAD
228 common_events.remove_all_objects();
230 delete event_condition;
233 top_level->window_lock = 0;
234 BC_Display::unlock_display();
239 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
240 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
241 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
244 UNSET_ALL_LOCKS(this)
247 int BC_WindowBase::initialize()
267 xinerama_screens = 0;
272 translation_events = 0;
273 ctrl_mask = shift_mask = alt_mask = 0;
274 cursor_x = cursor_y = button_number = 0;
286 active_popup_menu = 0;
287 active_subwindow = 0;
290 _7segment_pixmaps = 0;
293 // next_repeat_id = 0;
296 current_font = MEDIUMFONT;
297 current_color = BLACK;
298 current_cursor = ARROW_CURSOR;
301 shared_bg_pixmap = 0;
304 window_type = MAIN_WINDOW;
305 translation_count = 0;
306 x_correction = y_correction = 0;
315 #ifdef HAVE_LIBXXF86VM
319 bold_largefont_xft = 0;
320 bold_mediumfont_xft = 0;
321 bold_smallfont_xft = 0;
329 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
331 // Need these right away since put_event is called before run_window sometimes.
332 event_lock = new Mutex("BC_WindowBase::event_lock");
333 event_condition = new Condition(0, "BC_WindowBase::event_condition");
334 init_lock = new Condition(0, "BC_WindowBase::init_lock");
337 cursor_timer = new Timer;
340 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
341 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
342 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
356 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
359 ButtonReleaseMask | \
360 PointerMotionMask | \
364 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
376 const char *display_name,
378 BC_Pixmap *bg_pixmap,
381 XSetWindowAttributes attr;
383 XSizeHints size_hints;
386 #ifdef HAVE_LIBXXF86VM
390 id = get_resources()->get_id();
392 if(parent_window) top_level = parent_window->top_level;
393 if( top_level ) { // need this to avoid deadlock with Xlib's locks
395 lock_window("BC_WindowBase::create_window");
397 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
399 #ifdef HAVE_LIBXXF86VM
400 if(window_type == VIDMODE_SCALED_WINDOW)
401 closest_vm(&vm,&w,&h);
408 this->bg_color = bg_color;
409 this->window_type = window_type;
411 this->private_color = private_color;
412 this->parent_window = parent_window;
413 this->bg_pixmap = bg_pixmap;
414 this->allow_resize = allow_resize;
416 strcpy(this->display_name, display_name);
418 this->display_name[0] = 0;
421 if(bg_pixmap) shared_bg_pixmap = 1;
423 subwindows = new BC_SubWindowList;
425 if(window_type == MAIN_WINDOW)
428 parent_window = this;
432 display = BC_Display::get_display(display_name);
433 BC_Display::lock_display("BC_WindowBase::create_window");
434 // BC_Display::display_global->new_window(this);
437 // get the display connection
439 // This function must be the first Xlib
440 // function a multi-threaded program calls
442 display = init_display(display_name);
443 if( shm_completion_event < 0 ) shm_completion_event =
444 ShmCompletion + XShmGetEventBase(display);
447 screen = DefaultScreen(display);
448 rootwin = RootWindow(display, screen);
449 // window placement boundaries
450 if( !xinerama_screens && XineramaIsActive(display) )
451 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
452 root_w = get_root_w(0);
453 root_h = get_root_h(0);
456 vis = get_glx_visual(display);
459 vis = DefaultVisual(display, screen);
461 default_depth = DefaultDepth(display, screen);
463 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
464 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
467 // This must be done before fonts to know if antialiasing is available.
470 if(resources.use_shm < 0) resources.initialize_display(this);
471 x_correction = BC_DisplayInfo::get_left_border();
472 y_correction = BC_DisplayInfo::get_top_border();
474 // clamp window placement
475 if(this->x + this->w + x_correction > root_w)
476 this->x = root_w - this->w - x_correction;
477 if(this->y + this->h + y_correction > root_h)
478 this->y = root_h - this->h - y_correction;
479 if(this->x < 0) this->x = 0;
480 if(this->y < 0) this->y = 0;
482 if(this->bg_color == -1)
483 this->bg_color = resources.get_bg_color();
485 // printf("bcwindowbase 1 %s\n", title);
486 // if(window_type == MAIN_WINDOW) sleep(1);
487 // printf("bcwindowbase 10\n");
493 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
495 attr.event_mask = DEFAULT_EVENT_MASKS |
496 StructureNotifyMask |
500 attr.background_pixel = get_color(this->bg_color);
501 attr.colormap = cmap;
502 attr.cursor = get_cursor_struct(ARROW_CURSOR);
504 win = XCreateWindow(display, rootwin,
505 this->x, this->y, this->w, this->h, 0,
506 top_level->default_depth, InputOutput,
508 XGetNormalHints(display, win, &size_hints);
510 size_hints.flags = PSize | PMinSize | PMaxSize;
511 size_hints.width = this->w;
512 size_hints.height = this->h;
513 size_hints.min_width = allow_resize ? minw : this->w;
514 size_hints.max_width = allow_resize ? 32767 : this->w;
515 size_hints.min_height = allow_resize ? minh : this->h;
516 size_hints.max_height = allow_resize ? 32767 : this->h;
517 if(x > -BC_INFINITY && x < BC_INFINITY)
519 size_hints.flags |= PPosition;
520 size_hints.x = this->x;
521 size_hints.y = this->y;
525 txlist[0] = this->title;
527 XTextProperty titleprop;
528 if(options & WINDOW_UTF8)
529 Xutf8TextListToTextProperty(display, txlist, 1,
530 XUTF8StringStyle, &titleprop);
532 XmbTextListToTextProperty(display, txlist, 1,
533 XStdICCTextStyle, &titleprop);
534 XSetWMProperties(display, win, &titleprop, &titleprop,
535 0, 0, &size_hints, 0, 0);
536 XFree(titleprop.value);
539 #ifndef SINGLE_THREAD
540 clipboard = new BC_Clipboard(display_name);
541 clipboard->start_clipboard();
547 Atom ClientLeaderXAtom;
548 if (XGroupLeader == 0)
550 const char *instance_name = "cinelerra";
551 const char *class_name = "Cinelerra";
552 XClassHint *class_hints = XAllocClassHint();
553 class_hints->res_name = (char*)instance_name;
554 class_hints->res_class = (char*)class_name;
555 XSetClassHint(top_level->display, win, class_hints);
557 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
558 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
559 PropModeReplace, (unsigned char *)&XGroupLeader, true);
564 #ifdef HAVE_LIBXXF86VM
565 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
572 #ifdef HAVE_LIBXXF86VM
573 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
575 if(window_type == POPUP_WINDOW)
578 mask = CWEventMask | CWBackPixel | CWColormap |
579 CWOverrideRedirect | CWSaveUnder | CWCursor;
581 attr.event_mask = DEFAULT_EVENT_MASKS |
585 if(this->bg_color == -1)
586 this->bg_color = resources.get_bg_color();
587 attr.background_pixel = top_level->get_color(bg_color);
588 attr.colormap = top_level->cmap;
589 if(top_level->is_hourglass)
590 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
592 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
593 attr.override_redirect = True;
594 attr.save_under = True;
596 win = XCreateWindow(top_level->display,
597 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
598 top_level->default_depth, InputOutput, top_level->vis, mask,
600 top_level->add_popup(this);
603 if(window_type == SUB_WINDOW)
605 mask = CWEventMask | CWBackPixel | CWCursor;
606 attr.event_mask = DEFAULT_EVENT_MASKS;
607 attr.background_pixel = top_level->get_color(this->bg_color);
608 if(top_level->is_hourglass)
609 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
611 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
612 win = XCreateWindow(top_level->display,
613 parent_window->win, this->x, this->y, this->w, this->h, 0,
614 top_level->default_depth, InputOutput, top_level->vis, mask,
617 if(!hidden) XMapWindow(top_level->display, win);
620 // Create pixmap for all windows
621 pixmap = new BC_Pixmap(this, this->w, this->h);
623 // Set up options for main window
624 if(window_type == MAIN_WINDOW)
626 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
628 this->bg_pixmap = new BC_Pixmap(this,
629 get_resources()->bg_image,
633 if(!hidden) show_window();
640 draw_background(0, 0, this->w, this->h);
642 flash(-1, -1, -1, -1, 0);
644 // Set up options for popup window
645 #ifdef HAVE_LIBXXF86VM
646 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
648 if(window_type == POPUP_WINDOW)
652 if(!hidden) show_window();
654 get_resources()->create_window_lock->unlock();
655 if(need_lock) unlock_window();
660 Display* BC_WindowBase::init_display(const char *display_name)
664 if(display_name && display_name[0] == 0) display_name = NULL;
665 if((display = XOpenDisplay(display_name)) == NULL) {
666 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
668 if(getenv("DISPLAY") == NULL) {
669 printf(_("'DISPLAY' environment variable not set.\n"));
673 // Try again with default display.
674 if((display = XOpenDisplay(0)) == NULL) {
675 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
683 Display* BC_WindowBase::get_display()
685 return top_level->display;
688 int BC_WindowBase::get_screen()
690 return top_level->screen;
693 int BC_WindowBase::run_window()
699 // Events may have been sent before run_window so can't initialize them here.
702 set_repeat(get_resources()->tooltip_delay);
703 BC_Display::display_global->new_window(this);
705 // If the first window created, run the display loop in this thread.
706 if(BC_Display::display_global->is_first(this))
708 BC_Display::unlock_display();
709 BC_Display::display_global->loop();
713 BC_Display::unlock_display();
714 completion_lock->lock("BC_WindowBase::run_window");
717 BC_Display::lock_display("BC_WindowBase::run_window");
718 BC_Display::display_global->delete_window(this);
720 unset_all_repeaters();
722 BC_Display::unlock_display();
724 #else // SINGLE_THREAD
729 set_repeat(get_resources()->tooltip_delay);
731 // Start X server events
732 event_thread = new BC_WindowEvents(this);
733 event_thread->start();
739 // Handle common events
745 unset_all_repeaters();
749 event_condition->reset();
750 common_events.remove_all_objects();
754 #endif // SINGLE_THREAD
759 int BC_WindowBase::get_key_masks(unsigned int key_state)
761 // printf("BC_WindowBase::get_key_masks %llx\n",
762 // event->xkey.state);
763 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
764 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
765 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
770 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
772 BC_KeyboardHandlerLock set;
773 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
776 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
778 BC_KeyboardHandlerLock set;
779 int i = BC_KeyboardHandler::listeners.size();
780 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
781 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
784 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
786 int result = (win->*handler)(wp);
790 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
793 BC_KeyboardHandlerLock set;
794 for( int i=0; !result && i<listeners.size(); ++i ) {
795 BC_KeyboardHandler *listener = listeners[i];
796 result = listener->run_event(wp);
801 void BC_KeyboardHandler::kill_grabs()
803 BC_KeyboardHandlerLock set;
804 for( int i=0; i<listeners.size(); ++i ) {
805 BC_WindowBase *win = listeners[i]->win;
806 if( win->get_window_type() != POPUP_WINDOW ) continue;
807 ((BC_Popup *)win)->ungrab_keyboard();
811 void BC_ActiveBitmaps::reque(XEvent *event)
813 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
814 ShmSeg shmseg = shm_ev->shmseg;
815 Drawable drawable = shm_ev->drawable;
816 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
817 active_lock.lock("BC_BitmapImage::reque");
818 BC_BitmapImage *bfr = first;
819 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
820 if( bfr && bfr->drawable == drawable )
822 active_lock.unlock();
824 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
825 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
826 // (int)shmseg, (int)drawable);
829 if( bfr->drawable != drawable ) return;
830 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
831 bfr->bitmap->reque(bfr);
834 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
836 active_lock.lock("BC_BitmapImage::insert");
837 bfr->drawable = pixmap;
839 active_lock.unlock();
842 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
844 active_lock.lock("BC_ActiveBitmaps::remove");
845 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
847 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
848 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
850 active_lock.unlock();
853 BC_ActiveBitmaps::BC_ActiveBitmaps()
857 BC_ActiveBitmaps::~BC_ActiveBitmaps()
863 int BC_WindowBase::keysym_lookup(XEvent *event)
865 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
866 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
868 if( event->xany.send_event && !event->xany.serial ) {
869 keysym = (KeySym) event->xkey.keycode;
870 keys_return[0] = keysym;
873 wkey_string_length = 0;
875 if( input_context ) {
876 wkey_string_length = XwcLookupString(input_context,
877 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
878 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
879 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
882 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
883 keys_return, KEYPRESSLEN, &keysym, &stat);
884 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
885 if( stat == XLookupBoth ) return ret;
886 if( stat == XLookupKeySym ) return 0;
888 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
889 wkey_string_length = ret;
890 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
894 pthread_t locking_task = (pthread_t)-1L;
895 int locking_event = -1;
896 int locking_message = -1;
898 int BC_WindowBase::dispatch_event(XEvent *event)
902 XClientMessageEvent *ptr;
903 int cancel_resize, cancel_translation;
908 #ifndef SINGLE_THREAD
909 // If an event is waiting get it, otherwise
910 // wait for next event only if there are no compressed events.
911 if(get_event_count() ||
912 (!motion_events && !resize_events && !translation_events))
915 // Lock out window deletions
916 lock_window("BC_WindowBase::dispatch_event 1");
917 locking_event = event->type;
918 locking_task = pthread_self();
919 locking_message = event->xclient.message_type;
922 // Handle compressed events
924 lock_window("BC_WindowBase::dispatch_event 2");
926 dispatch_resize_event(last_resize_w, last_resize_h);
928 dispatch_motion_event();
929 if(translation_events)
930 dispatch_translation_event();
941 //static const char *event_names[] = {
942 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
943 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
944 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
945 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
946 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
947 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
948 // "GenericEvent", "LASTEvent",
952 //if(event->type != ClientMessage) {
953 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
954 //__LINE__, title, event, event->type,
955 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
956 // event_names[event->type] : "Unknown");
962 switch(event->type) {
964 // Clear the resize buffer
965 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
966 // Clear the motion buffer since this can clear the window
969 dispatch_motion_event();
972 ptr = (XClientMessageEvent*)event;
975 if(ptr->message_type == ProtoXAtom &&
976 (Atom)ptr->data.l[0] == DelWinXAtom)
981 if(ptr->message_type == RepeaterXAtom)
983 dispatch_repeat_event(ptr->data.l[0]);
984 // Make sure the repeater still exists.
985 // for(int i = 0; i < repeaters.total; i++)
987 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
989 // dispatch_repeat_event_master(ptr->data.l[0]);
995 if(ptr->message_type == SetDoneXAtom)
999 { // We currently use X marshalling for xatom events, we can switch to something else later
1000 recieve_custom_xatoms((xatom_event *)ptr);
1006 dispatch_focus_in();
1011 dispatch_focus_out();
1023 get_key_masks(event->xbutton.state);
1024 cursor_x = event->xbutton.x;
1025 cursor_y = event->xbutton.y;
1026 button_number = event->xbutton.button;
1028 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1029 event_win = event->xany.window;
1030 if (button_number < 6)
1032 if(button_number < 4)
1034 button_pressed = event->xbutton.button;
1035 button_time1 = button_time2;
1036 button_time2 = button_time3;
1037 button_time3 = event->xbutton.time;
1040 drag_win = event_win;
1041 drag_x1 = cursor_x - get_resources()->drag_radius;
1042 drag_x2 = cursor_x + get_resources()->drag_radius;
1043 drag_y1 = cursor_y - get_resources()->drag_radius;
1044 drag_y2 = cursor_y + get_resources()->drag_radius;
1046 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1049 button_time3 = button_time2 = button_time1 = 0;
1051 if((long)(button_time3 - button_time2) < resources.double_click)
1054 // button_time3 = button_time2 = button_time1 = 0;
1062 dispatch_button_press();
1067 get_key_masks(event->xbutton.state);
1068 button_number = event->xbutton.button;
1069 event_win = event->xany.window;
1070 if (button_number < 6)
1072 if(button_number < 4)
1074 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1076 dispatch_button_release();
1081 event_win = event->xany.window;
1082 dispatch_expose_event();
1086 get_key_masks(event->xmotion.state);
1087 // Dispatch previous motion event if this is a subsequent motion from a different window
1088 if(motion_events && last_motion_win != event->xany.window)
1090 dispatch_motion_event();
1093 // Buffer the current motion
1095 last_motion_state = event->xmotion.state;
1096 last_motion_x = event->xmotion.x;
1097 last_motion_y = event->xmotion.y;
1098 last_motion_win = event->xany.window;
1101 case ConfigureNotify:
1102 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1104 // event->xany.window,
1107 XTranslateCoordinates(top_level->display,
1115 last_resize_w = event->xconfigure.width;
1116 last_resize_h = event->xconfigure.height;
1119 cancel_translation = 0;
1121 // Resize history prevents responses to recursive resize requests
1122 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1124 if(resize_history.values[i]->w == last_resize_w &&
1125 resize_history.values[i]->h == last_resize_h)
1127 delete resize_history.values[i];
1128 resize_history.remove_number(i);
1133 if(last_resize_w == w && last_resize_h == h)
1141 if((last_translate_x == x && last_translate_y == y))
1142 cancel_translation = 1;
1144 if(!cancel_translation)
1146 translation_events = 1;
1149 translation_count++;
1153 get_key_masks(event->xkey.state);
1154 keys_return[0] = 0; keysym = -1;
1155 if(XFilterEvent(event, win)) {
1158 if( keysym_lookup(event) < 0 ) {
1159 printf("keysym %x\n", (uint32_t)keysym);
1163 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1167 // block out control keys
1168 if(keysym > 0xffe0 && keysym < 0xffff) break;
1169 // block out Alt_GR key
1170 if(keysym == 0xfe03) break;
1173 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1175 #ifdef X_HAVE_UTF8_STRING
1176 //It's Ascii or UTF8?
1177 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1178 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1180 if( ((keys_return[1] & 0xff) > 0x80) &&
1181 ((keys_return[0] & 0xff) > 0xC0) ) {
1182 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1183 key_pressed = keysym & 0xff;
1189 // block out extra keys
1199 // Translate key codes
1200 case XK_Return: key_pressed = RETURN; break;
1201 case XK_Up: key_pressed = UP; break;
1202 case XK_Down: key_pressed = DOWN; break;
1203 case XK_Left: key_pressed = LEFT; break;
1204 case XK_Right: key_pressed = RIGHT; break;
1205 case XK_Next: key_pressed = PGDN; break;
1206 case XK_Prior: key_pressed = PGUP; break;
1207 case XK_BackSpace: key_pressed = BACKSPACE; break;
1208 case XK_Escape: key_pressed = ESC; break;
1211 key_pressed = LEFTTAB;
1215 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1216 case XK_underscore: key_pressed = '_'; break;
1217 case XK_asciitilde: key_pressed = '~'; break;
1218 case XK_Delete: key_pressed = DELETE; break;
1219 case XK_Home: key_pressed = HOME; break;
1220 case XK_End: key_pressed = END; break;
1223 case XK_KP_Enter: key_pressed = KPENTER; break;
1224 case XK_KP_Add: key_pressed = KPPLUS; break;
1225 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1226 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1227 case XK_KP_Divide: key_pressed = KPSLASH; break;
1229 case XK_KP_End: key_pressed = KP1; break;
1231 case XK_KP_Down: key_pressed = KP2; break;
1233 case XK_KP_Page_Down: key_pressed = KP3; break;
1235 case XK_KP_Left: key_pressed = KP4; break;
1237 case XK_KP_Begin: key_pressed = KP5; break;
1239 case XK_KP_Right: key_pressed = KP6; break;
1241 case XK_KP_Home: key_pressed = KP7; break;
1243 case XK_KP_Up: key_pressed = KP8; break;
1245 case XK_KP_Page_Up: key_pressed = KP9; break;
1247 case XK_KP_Insert: key_pressed = KPINS; break;
1249 case XK_KP_Delete: key_pressed = KPDEL; break;
1250 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1252 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1253 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1254 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1255 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1256 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1257 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1258 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1259 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1260 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1261 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1262 // above case XK_Left: key_pressed = LEFT; break; /* left */
1263 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1264 // above case XK_Down: key_pressed = DOWN; break; /* down */
1265 // above case XK_Up: key_pressed = UP; break; /* up */
1266 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1267 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1268 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1269 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1270 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1271 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1272 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1273 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1274 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1275 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1276 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1277 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1280 key_pressed = keysym & 0xff;
1281 #ifdef X_HAVE_UTF8_STRING
1282 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1287 #ifdef X_HAVE_UTF8_STRING
1289 key_pressed_utf8 = keys_return;
1294 if( top_level == this )
1295 result = BC_KeyboardHandler::run_listeners(this);
1297 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1299 result = dispatch_keypress_event();
1300 // Handle some default keypresses
1303 if(key_pressed == 'w' ||
1312 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1313 dispatch_keyrelease_event();
1314 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1315 // keysym, event->xkey.state);
1319 event_win = event->xany.window;
1320 dispatch_cursor_leave();
1324 event_win = event->xany.window;
1325 cursor_x = event->xcrossing.x;
1326 cursor_y = event->xcrossing.y;
1327 dispatch_cursor_enter();
1332 //printf("100 %s %p %d\n", title, event, event->type);
1333 //if(event->type != ClientMessage) dump();
1335 #ifndef SINGLE_THREAD
1337 if(event) delete event;
1339 // if(done) completion_lock->unlock();
1342 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1346 int BC_WindowBase::dispatch_expose_event()
1349 for(int i = 0; i < subwindows->total && !result; i++)
1351 result = subwindows->values[i]->dispatch_expose_event();
1354 // Propagate to user
1355 if(!result) expose_event();
1359 int BC_WindowBase::dispatch_resize_event(int w, int h)
1361 // Can't store new w and h until the event is handles
1362 // because bcfilebox depends on the old w and h to
1363 // reposition widgets.
1364 if( window_type == MAIN_WINDOW ) {
1369 pixmap = new BC_Pixmap(this, w, h);
1371 clear_box(0, 0, w, h);
1374 // Propagate to subwindows
1375 for(int i = 0; i < subwindows->total; i++) {
1376 subwindows->values[i]->dispatch_resize_event(w, h);
1379 // Propagate to user
1382 if( window_type == MAIN_WINDOW ) {
1390 int BC_WindowBase::dispatch_flash()
1393 for(int i = 0; i < subwindows->total; i++)
1394 subwindows->values[i]->dispatch_flash();
1398 int BC_WindowBase::dispatch_translation_event()
1400 translation_events = 0;
1401 if(window_type == MAIN_WINDOW)
1405 x = last_translate_x;
1406 y = last_translate_y;
1407 // Correct for window manager offsets
1412 for(int i = 0; i < subwindows->total; i++)
1414 subwindows->values[i]->dispatch_translation_event();
1417 translation_event();
1421 int BC_WindowBase::dispatch_motion_event()
1426 if(top_level == this)
1429 event_win = last_motion_win;
1430 get_key_masks(last_motion_state);
1433 if(get_button_down() && !active_menubar && !active_popup_menu)
1437 cursor_x = last_motion_x;
1438 cursor_y = last_motion_y;
1439 result = dispatch_drag_motion();
1443 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1444 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1449 result = dispatch_drag_start();
1453 cursor_x = last_motion_x;
1454 cursor_y = last_motion_y;
1456 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1459 // active_popup_menu,
1460 // active_subwindow);
1462 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1463 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1464 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1467 // Dispatch in stacking order
1468 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1470 result = subwindows->values[i]->dispatch_motion_event();
1473 if(!result) result = cursor_motion_event(); // give to user
1477 int BC_WindowBase::dispatch_keypress_event()
1480 if(top_level == this)
1482 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1485 for(int i = 0; i < subwindows->total && !result; i++)
1487 result = subwindows->values[i]->dispatch_keypress_event();
1490 if(!result) result = keypress_event();
1495 int BC_WindowBase::dispatch_keyrelease_event()
1498 if(top_level == this)
1500 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1503 for(int i = 0; i < subwindows->total && !result; i++)
1505 result = subwindows->values[i]->dispatch_keyrelease_event();
1508 if(!result) result = keyrelease_event();
1513 int BC_WindowBase::dispatch_focus_in()
1515 for(int i = 0; i < subwindows->total; i++)
1517 subwindows->values[i]->dispatch_focus_in();
1525 int BC_WindowBase::dispatch_focus_out()
1527 for(int i = 0; i < subwindows->total; i++)
1529 subwindows->values[i]->dispatch_focus_out();
1537 int BC_WindowBase::get_has_focus()
1539 return top_level->has_focus;
1542 int BC_WindowBase::get_deleting()
1544 if(is_deleting) return 1;
1545 if(parent_window && parent_window->get_deleting()) return 1;
1549 int BC_WindowBase::dispatch_button_press()
1554 if(top_level == this)
1556 if(active_menubar) result = active_menubar->dispatch_button_press();
1557 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1558 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1561 for(int i = 0; i < subwindows->total && !result; i++)
1563 result = subwindows->values[i]->dispatch_button_press();
1566 if(!result) result = button_press_event();
1572 int BC_WindowBase::dispatch_button_release()
1575 if(top_level == this)
1577 if(active_menubar) result = active_menubar->dispatch_button_release();
1578 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1579 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1580 if(!result && button_number != 4 && button_number != 5)
1581 result = dispatch_drag_stop();
1584 for(int i = 0; i < subwindows->total && !result; i++)
1586 result = subwindows->values[i]->dispatch_button_release();
1591 result = button_release_event();
1598 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1601 // all repeat event handlers get called and decide based on activity and duration
1602 // whether to respond
1603 for(int i = 0; i < subwindows->total; i++)
1605 subwindows->values[i]->dispatch_repeat_event(duration);
1609 repeat_event(duration);
1613 // Unlock next repeat signal
1614 if(window_type == MAIN_WINDOW)
1616 #ifdef SINGLE_THREAD
1617 BC_Display::display_global->unlock_repeaters(duration);
1619 for(int i = 0; i < repeaters.total; i++)
1621 if(repeaters.values[i]->delay == duration)
1623 repeaters.values[i]->repeat_lock->unlock();
1631 void BC_WindowBase::unhide_cursor()
1636 if(top_level->is_hourglass)
1637 set_cursor(HOURGLASS_CURSOR, 1, 0);
1639 set_cursor(current_cursor, 1, 0);
1641 cursor_timer->update();
1645 void BC_WindowBase::update_video_cursor()
1647 if(video_on && !is_transparent)
1649 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1652 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1653 cursor_timer->update();
1658 cursor_timer->update();
1663 int BC_WindowBase::dispatch_cursor_leave()
1667 for(int i = 0; i < subwindows->total; i++)
1669 subwindows->values[i]->dispatch_cursor_leave();
1672 cursor_leave_event();
1676 int BC_WindowBase::dispatch_cursor_enter()
1682 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1683 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1684 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1686 for(int i = 0; !result && i < subwindows->total; i++)
1688 result = subwindows->values[i]->dispatch_cursor_enter();
1691 if(!result) result = cursor_enter_event();
1695 int BC_WindowBase::cursor_enter_event()
1700 int BC_WindowBase::cursor_leave_event()
1705 int BC_WindowBase::close_event()
1711 int BC_WindowBase::dispatch_drag_start()
1714 if(active_menubar) result = active_menubar->dispatch_drag_start();
1715 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1716 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1718 for(int i = 0; i < subwindows->total && !result; i++)
1720 result = subwindows->values[i]->dispatch_drag_start();
1723 if(!result) result = is_dragging = drag_start_event();
1727 int BC_WindowBase::dispatch_drag_stop()
1731 for(int i = 0; i < subwindows->total && !result; i++)
1733 result = subwindows->values[i]->dispatch_drag_stop();
1736 if(is_dragging && !result)
1746 int BC_WindowBase::dispatch_drag_motion()
1749 for(int i = 0; i < subwindows->total && !result; i++)
1751 result = subwindows->values[i]->dispatch_drag_motion();
1754 if(is_dragging && !result)
1756 drag_motion_event();
1767 int BC_WindowBase::show_tooltip(int w, int h)
1771 if(tooltip_text && !tooltip_on &&
1772 (force_tooltip || get_resources()->tooltips_enabled))
1775 top_level->hide_tooltip();
1779 w = get_text_width(MEDIUMFONT, tooltip_text);
1782 h = get_text_height(MEDIUMFONT, tooltip_text);
1784 w += TOOLTIP_MARGIN * 2;
1785 h += TOOLTIP_MARGIN * 2;
1787 XTranslateCoordinates(top_level->display, win,
1788 top_level->rootwin, get_w(), get_h(),
1790 // keep the tip inside the window/display
1791 int top_x = top_level->get_x();
1792 if( x < top_x ) x = top_x;
1793 int top_w = top_x + top_level->get_w();
1794 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1795 if( top_w < lmt_w ) lmt_w = top_w;
1796 if( x+w > lmt_w ) x = lmt_w-w;
1798 int top_y = top_level->get_y();
1799 if( y < top_y ) y = top_y;
1800 int top_h = top_y + top_level->get_h();
1801 int lmt_h = top_level->get_root_h(0);
1802 if( top_h < lmt_h ) lmt_h = top_h;
1803 if( y+h > lmt_h ) y = lmt_h-h;
1805 int abs_x, abs_y, win_x, win_y;
1806 unsigned int temp_mask;
1808 XQueryPointer(top_level->display, top_level->win,
1809 &temp_win, &temp_win, &abs_x, &abs_y,
1810 &win_x, &win_y, &temp_mask);
1811 // check for cursor inside popup
1812 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1814 if( x-abs_x < y-abs_y )
1819 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1820 get_resources()->tooltip_bg_color);
1823 tooltip_popup->set_font(MEDIUMFONT);
1824 tooltip_popup->flash();
1825 tooltip_popup->flush();
1830 int BC_WindowBase::hide_tooltip()
1833 for(int i = 0; i < subwindows->total; i++)
1835 subwindows->values[i]->hide_tooltip();
1841 delete tooltip_popup;
1847 const char *BC_WindowBase::get_tooltip()
1849 return tooltip_text;
1852 int BC_WindowBase::set_tooltip(const char *text)
1854 tooltip_text = text;
1856 // Update existing tooltip if it is visible
1860 tooltip_popup->flash();
1865 // signal the event handler to repeat
1866 int BC_WindowBase::set_repeat(int64_t duration)
1870 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1873 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1875 #ifdef SINGLE_THREAD
1876 BC_Display::display_global->set_repeat(this, duration);
1878 // test repeater database for duplicates
1879 for(int i = 0; i < repeaters.total; i++)
1882 if(repeaters.values[i]->delay == duration)
1884 repeaters.values[i]->start_repeating(this);
1889 BC_Repeater *repeater = new BC_Repeater(this, duration);
1890 repeater->initialize();
1891 repeaters.append(repeater);
1892 repeater->start_repeating();
1897 int BC_WindowBase::unset_repeat(int64_t duration)
1899 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1901 #ifdef SINGLE_THREAD
1902 BC_Display::display_global->unset_repeat(this, duration);
1904 for(int i = 0; i < repeaters.total; i++)
1906 if(repeaters.values[i]->delay == duration)
1908 repeaters.values[i]->stop_repeating();
1916 int BC_WindowBase::unset_all_repeaters()
1918 #ifdef SINGLE_THREAD
1919 BC_Display::display_global->unset_all_repeaters(this);
1921 for(int i = 0; i < repeaters.total; i++)
1923 repeaters.values[i]->stop_repeating();
1925 repeaters.remove_all_objects();
1930 // long BC_WindowBase::get_repeat_id()
1932 // return top_level->next_repeat_id++;
1935 XEvent *BC_WindowBase::new_xevent()
1937 XEvent *event = new XEvent;
1938 memset(event, 0, sizeof(*event));
1942 #ifndef SINGLE_THREAD
1943 int BC_WindowBase::arm_repeat(int64_t duration)
1945 XEvent *event = new_xevent();
1946 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1947 ptr->type = ClientMessage;
1948 ptr->message_type = RepeaterXAtom;
1950 ptr->data.l[0] = duration;
1952 // Couldn't use XSendEvent since it locked up randomly.
1958 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1963 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1965 #ifndef SINGLE_THREAD
1966 XEvent *myevent = new_xevent();
1967 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1968 ptr->type = ClientMessage;
1969 ptr->message_type = event->message_type;
1970 ptr->format = event->format;
1971 ptr->data.l[0] = event->data.l[0];
1972 ptr->data.l[1] = event->data.l[1];
1973 ptr->data.l[2] = event->data.l[2];
1974 ptr->data.l[3] = event->data.l[3];
1975 ptr->data.l[4] = event->data.l[4];
1984 Atom BC_WindowBase::create_xatom(const char *atom_name)
1986 return XInternAtom(display, atom_name, False);
1989 int BC_WindowBase::get_atoms()
1991 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1992 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1993 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
1994 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1995 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
1996 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
1997 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2003 void BC_WindowBase::init_cursors()
2005 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2006 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2007 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2008 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2009 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2010 move_cursor = XCreateFontCursor(display, XC_fleur);
2011 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2012 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2013 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2014 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2015 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2016 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2017 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2018 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2021 char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2022 Colormap colormap = DefaultColormap(display, screen);
2023 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2024 rootwin, cursor_data, 8, 8);
2025 XColor black, dummy;
2026 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2027 transparent_cursor = XCreatePixmapCursor(display,
2028 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2029 // XDefineCursor(display, win, transparent_cursor);
2030 XFreePixmap(display, pixmap_bottom);
2033 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2035 int color_model = BC_TRANSPARENCY;
2039 color_model = BC_RGB8;
2042 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2045 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2048 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2054 int BC_WindowBase::init_colors()
2057 current_color_value = current_color_pixel = 0;
2059 // Get the real depth
2062 ximage = XCreateImage(top_level->display,
2064 top_level->default_depth,
2072 bits_per_pixel = ximage->bits_per_pixel;
2073 XDestroyImage(ximage);
2075 color_model = evaluate_color_model(client_byte_order,
2078 // Get the color model
2084 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2085 create_private_colors();
2089 cmap = DefaultColormap(display, screen);
2090 create_shared_colors();
2093 allocate_color_table();
2097 //cmap = DefaultColormap(display, screen);
2098 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2104 int BC_WindowBase::create_private_colors()
2109 for(int i = 0; i < 255; i++)
2111 color = (i & 0xc0) << 16;
2112 color += (i & 0x38) << 10;
2113 color += (i & 0x7) << 5;
2114 color_table[i][0] = color;
2116 create_shared_colors(); // overwrite the necessary colors on the table
2121 int BC_WindowBase::create_color(int color)
2123 if(total_colors == 256)
2125 // replace the closest match with an exact match
2126 color_table[get_color_rgb8(color)][0] = color;
2130 // add the color to the table
2131 color_table[total_colors][0] = color;
2137 int BC_WindowBase::create_shared_colors()
2139 create_color(BLACK);
2140 create_color(WHITE);
2142 create_color(LTGREY);
2143 create_color(MEGREY);
2144 create_color(MDGREY);
2145 create_color(DKGREY);
2147 create_color(LTCYAN);
2148 create_color(MECYAN);
2149 create_color(MDCYAN);
2150 create_color(DKCYAN);
2152 create_color(LTGREEN);
2153 create_color(GREEN);
2154 create_color(DKGREEN);
2156 create_color(LTPINK);
2160 create_color(LTBLUE);
2162 create_color(DKBLUE);
2164 create_color(LTYELLOW);
2165 create_color(MEYELLOW);
2166 create_color(MDYELLOW);
2167 create_color(DKYELLOW);
2169 create_color(LTPURPLE);
2170 create_color(MEPURPLE);
2171 create_color(MDPURPLE);
2172 create_color(DKPURPLE);
2174 create_color(FGGREY);
2175 create_color(MNBLUE);
2176 create_color(ORANGE);
2177 create_color(FTGREY);
2182 int BC_WindowBase::allocate_color_table()
2184 int red, green, blue, color;
2187 for(int i = 0; i < total_colors; i++)
2189 color = color_table[i][0];
2190 red = (color & 0xFF0000) >> 16;
2191 green = (color & 0x00FF00) >> 8;
2192 blue = color & 0xFF;
2194 col.flags = DoRed | DoGreen | DoBlue;
2195 col.red = red<<8 | red;
2196 col.green = green<<8 | green;
2197 col.blue = blue<<8 | blue;
2199 XAllocColor(display, cmap, &col);
2200 color_table[i][1] = col.pixel;
2203 XInstallColormap(display, cmap);
2207 int BC_WindowBase::init_window_shape()
2209 if(bg_pixmap && bg_pixmap->use_alpha())
2211 XShapeCombineMask(top_level->display,
2212 this->win, ShapeBounding, 0, 0,
2213 bg_pixmap->get_alpha(), ShapeSet);
2219 int BC_WindowBase::init_gc()
2221 unsigned long gcmask;
2222 gcmask = GCFont | GCGraphicsExposures;
2225 gcvalues.font = mediumfont->fid; // set the font
2226 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2227 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2229 // gcmask = GCCapStyle | GCJoinStyle;
2230 // XGetGCValues(display, gc, gcmask, &gcvalues);
2231 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2235 int BC_WindowBase::init_fonts()
2237 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2238 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2239 smallfont = XLoadQueryFont(display, "fixed");
2240 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2241 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2242 mediumfont = XLoadQueryFont(display, "fixed");
2243 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2244 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2245 largefont = XLoadQueryFont(display, "fixed");
2246 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2247 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2248 bigfont = XLoadQueryFont(display, "fixed");
2251 if(get_resources()->use_fontset)
2256 // FIXME: should check the m,d,n values
2257 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2259 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2260 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2261 if( !mediumfontset )
2262 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2263 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2265 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2266 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2268 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2269 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2270 curr_fontset = mediumfontset;
2271 get_resources()->use_fontset = 1;
2275 get_resources()->use_fontset = 0;
2282 void BC_WindowBase::init_xft()
2285 if( !get_resources()->use_xft ) return;
2286 if(!(smallfont_xft =
2287 (resources.small_font_xft[0] == '-' ?
2288 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2289 XftFontOpenName(display, screen, resources.small_font_xft))) )
2290 if(!(smallfont_xft =
2291 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2292 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2293 if(!(mediumfont_xft =
2294 (resources.medium_font_xft[0] == '-' ?
2295 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2296 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2297 if(!(mediumfont_xft =
2298 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2299 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2300 if(!(largefont_xft =
2301 (resources.large_font_xft[0] == '-' ?
2302 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2303 XftFontOpenName(display, screen, resources.large_font_xft))) )
2304 if(!(largefont_xft =
2305 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2306 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2308 (resources.big_font_xft[0] == '-' ?
2309 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2310 XftFontOpenName(display, screen, resources.big_font_xft))) )
2312 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2313 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2315 if(!(bold_smallfont_xft =
2316 (resources.small_b_font_xft[0] == '-' ?
2317 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2318 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2319 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2320 if(!(bold_mediumfont_xft =
2321 (resources.medium_b_font_xft[0] == '-' ?
2322 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2323 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2324 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2325 if(!(bold_largefont_xft =
2326 (resources.large_b_font_xft[0] == '-' ?
2327 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2328 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2329 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2331 // Extension failed to locate fonts
2332 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2333 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2334 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2335 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2336 resources.small_font_xft, smallfont_xft,
2337 resources.medium_font_xft, mediumfont_xft,
2338 resources.large_font_xft, largefont_xft,
2339 resources.big_font_xft, bigfont_xft,
2340 resources.small_b_font_xft, bold_smallfont_xft,
2341 resources.medium_b_font_xft, bold_mediumfont_xft,
2342 resources.large_b_font_xft, bold_largefont_xft);
2343 get_resources()->use_xft = 0;
2349 void BC_WindowBase::init_im()
2351 XIMStyles *xim_styles;
2354 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2356 printf("BC_WindowBase::init_im: Could not open input method.\n");
2359 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2362 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2363 XCloseIM(input_method);
2368 for(int z = 0; z < xim_styles->count_styles; z++)
2370 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2372 xim_style = xim_styles->supported_styles[z];
2380 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2381 XCloseIM(input_method);
2385 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2386 XNClientWindow, win, XNFocusWindow, win, NULL);
2389 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2390 XCloseIM(input_method);
2395 void BC_WindowBase::finit_im()
2397 if( input_context ) {
2398 XDestroyIC(input_context);
2401 if( input_method ) {
2402 XCloseIM(input_method);
2408 int BC_WindowBase::get_color(int64_t color)
2410 // return pixel of color
2411 // use this only for drawing subwindows not for bitmaps
2412 int i, test, difference;
2418 return get_color_rgb8(color);
2419 // test last color looked up
2420 if(current_color_value == color)
2421 return current_color_pixel;
2424 current_color_value = color;
2425 for(i = 0; i < total_colors; i++)
2427 if(color_table[i][0] == color)
2429 current_color_pixel = color_table[i][1];
2430 return current_color_pixel;
2434 // find nearest match
2435 difference = 0xFFFFFF;
2437 for(i = 0; i < total_colors; i++)
2439 test = abs((int)(color_table[i][0] - color));
2441 if(test < difference)
2443 current_color_pixel = color_table[i][1];
2447 return current_color_pixel;
2450 return get_color_rgb16(color);
2453 return get_color_bgr16(color);
2457 return client_byte_order == server_byte_order ?
2458 color : get_color_bgr24(color);
2466 int BC_WindowBase::get_color_rgb8(int color)
2470 pixel = (color & 0xc00000) >> 16;
2471 pixel += (color & 0xe000) >> 10;
2472 pixel += (color & 0xe0) >> 5;
2476 int64_t BC_WindowBase::get_color_rgb16(int color)
2479 result = (color & 0xf80000) >> 8;
2480 result += (color & 0xfc00) >> 5;
2481 result += (color & 0xf8) >> 3;
2486 int64_t BC_WindowBase::get_color_bgr16(int color)
2489 result = (color & 0xf80000) >> 19;
2490 result += (color & 0xfc00) >> 5;
2491 result += (color & 0xf8) << 8;
2496 int64_t BC_WindowBase::get_color_bgr24(int color)
2499 result = (color & 0xff) << 16;
2500 result += (color & 0xff00);
2501 result += (color & 0xff0000) >> 16;
2505 void BC_WindowBase::start_video()
2507 cursor_timer->update();
2509 // set_color(BLACK);
2510 // draw_box(0, 0, get_w(), get_h());
2514 void BC_WindowBase::stop_video()
2522 int64_t BC_WindowBase::get_color()
2524 return top_level->current_color;
2527 void BC_WindowBase::set_color(int64_t color)
2529 top_level->current_color = color;
2530 XSetForeground(top_level->display,
2532 top_level->get_color(color));
2535 void BC_WindowBase::set_opaque()
2537 XSetFunction(top_level->display, top_level->gc, GXcopy);
2540 void BC_WindowBase::set_inverse()
2542 XSetFunction(top_level->display, top_level->gc, GXxor);
2545 void BC_WindowBase::set_line_width(int value)
2547 this->line_width = value;
2548 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2549 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2550 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2551 JoinMiter); /* join_style */
2553 if(line_dashes > 0) {
2554 const char dashes = line_dashes;
2555 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2558 // XGCValues gcvalues;
2559 // unsigned long gcmask;
2560 // gcmask = GCCapStyle | GCJoinStyle;
2561 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2562 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2565 void BC_WindowBase::set_line_dashes(int value)
2567 line_dashes = value;
2568 // call XSetLineAttributes
2569 set_line_width(line_width);
2573 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2577 case ARROW_CURSOR: return top_level->arrow_cursor; break;
2578 case CROSS_CURSOR: return top_level->cross_cursor;
2579 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
2580 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
2581 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
2582 case MOVE_CURSOR: return top_level->move_cursor; break;
2583 case LEFT_CURSOR: return top_level->left_cursor; break;
2584 case RIGHT_CURSOR: return top_level->right_cursor; break;
2585 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
2586 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
2587 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
2588 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
2589 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
2590 case HOURGLASS_CURSOR: return top_level->hourglass_cursor; break;
2591 case TRANSPARENT_CURSOR: return top_level->transparent_cursor; break;
2596 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2598 // inherit cursor from parent
2601 XUndefineCursor(top_level->display, win);
2602 current_cursor = cursor;
2605 // don't change cursor if overridden
2606 if((!top_level->is_hourglass && !is_transparent) ||
2609 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2610 if(flush) this->flush();
2613 if(!override) current_cursor = cursor;
2616 void BC_WindowBase::set_x_cursor(int cursor)
2618 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2619 XDefineCursor(top_level->display, win, temp_cursor);
2620 current_cursor = cursor;
2624 int BC_WindowBase::get_cursor()
2626 return current_cursor;
2629 void BC_WindowBase::start_hourglass()
2631 top_level->start_hourglass_recursive();
2635 void BC_WindowBase::stop_hourglass()
2637 top_level->stop_hourglass_recursive();
2641 void BC_WindowBase::start_hourglass_recursive()
2643 if(this == top_level)
2651 set_cursor(HOURGLASS_CURSOR, 1, 0);
2652 for(int i = 0; i < subwindows->total; i++)
2654 subwindows->values[i]->start_hourglass_recursive();
2659 void BC_WindowBase::stop_hourglass_recursive()
2661 if(this == top_level)
2663 if(hourglass_total == 0) return;
2664 top_level->hourglass_total--;
2667 if(!top_level->hourglass_total)
2669 top_level->is_hourglass = 0;
2671 // Cause set_cursor to perform change
2673 set_cursor(current_cursor, 1, 0);
2675 for(int i = 0; i < subwindows->total; i++)
2677 subwindows->values[i]->stop_hourglass_recursive();
2685 XFontStruct* BC_WindowBase::get_font_struct(int font)
2687 // Clear out unrelated flags
2688 if(font & BOLDFACE) font ^= BOLDFACE;
2691 case SMALLFONT: return top_level->smallfont; break;
2692 case MEDIUMFONT: return top_level->mediumfont; break;
2693 case LARGEFONT: return top_level->largefont; break;
2694 case BIGFONT: return top_level->bigfont; break;
2699 XFontSet BC_WindowBase::get_fontset(int font)
2703 if(get_resources()->use_fontset)
2705 switch(font & 0xff) {
2706 case SMALLFONT: fs = top_level->smallfontset; break;
2707 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2708 case LARGEFONT: fs = top_level->largefontset; break;
2709 case BIGFONT: fs = top_level->bigfontset; break;
2717 XftFont* BC_WindowBase::get_xft_struct(int font)
2720 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2721 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2722 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2723 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2724 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2725 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2726 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2741 int BC_WindowBase::get_current_font()
2743 return top_level->current_font;
2746 void BC_WindowBase::set_font(int font)
2748 top_level->current_font = font;
2751 if(get_resources()->use_xft) {}
2754 if(get_resources()->use_fontset) {
2758 if(get_font_struct(font))
2760 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2766 void BC_WindowBase::set_fontset(int font)
2770 if(get_resources()->use_fontset) {
2772 case SMALLFONT: fs = top_level->smallfontset; break;
2773 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2774 case LARGEFONT: fs = top_level->largefontset; break;
2775 case BIGFONT: fs = top_level->bigfontset; break;
2783 XFontSet BC_WindowBase::get_curr_fontset(void)
2785 if(get_resources()->use_fontset)
2786 return curr_fontset;
2790 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2793 if(get_resources()->use_xft && get_xft_struct(font))
2796 #ifdef X_HAVE_UTF8_STRING
2797 if(get_resources()->locale_utf8)
2799 XftTextExtentsUtf8(top_level->display,
2800 get_xft_struct(font),
2801 (const XftChar8 *)text,
2808 XftTextExtents8(top_level->display,
2809 get_xft_struct(font),
2810 (const XftChar8 *)text,
2814 return extents.xOff;
2818 if(get_resources()->use_fontset && top_level->get_fontset(font))
2819 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2821 if(get_font_struct(font))
2822 return XTextWidth(get_font_struct(font), text, length);
2828 case MEDIUM_7SEGMENT:
2829 return get_resources()->medium_7segment[0]->get_w() * length;
2839 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2842 if(get_resources()->use_xft && get_xft_struct(font)) {
2845 XftTextExtents32(top_level->display, get_xft_struct(font),
2846 (const FcChar32*)text, length, &extents);
2847 return extents.xOff;
2850 if(!get_font_struct(font)) return 0;
2851 XChar2b xtext[length], *xp = xtext;
2852 for( int i=0; i<length; ++i,++xp ) {
2853 xp->byte1 = (unsigned char) (text[i] >> 8);
2854 xp->byte2 = (unsigned char) (text[i] & 0xff);
2856 return XTextWidth16(get_font_struct(font), xtext, length);
2859 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2861 int i, j, w = 0, line_w = 0;
2862 if(length < 0) length = strlen(text);
2864 for(i = 0, j = 0; i <= length; i++)
2869 line_w = get_single_text_width(font, &text[j], i - j);
2875 line_w = get_single_text_width(font, &text[j], length - j);
2877 if(line_w > w) w = line_w;
2880 if(i > length && w == 0)
2882 w = get_single_text_width(font, text, length);
2888 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2890 int i, j, w = 0, line_w = 0;
2892 if(length < 0) length = wcslen(text);
2894 for(i = 0, j = 0; i <= length; i++)
2899 line_w = get_single_text_width(font, &text[j], i - j);
2904 line_w = get_single_text_width(font, &text[j], length - j);
2906 if(line_w > w) w = line_w;
2909 if(i > length && w == 0)
2910 w = get_single_text_width(font, text, length);
2915 int BC_WindowBase::get_text_ascent(int font)
2919 if( (fstruct = get_xft_struct(font)) != 0 )
2920 return fstruct->ascent;
2922 if(get_resources()->use_fontset && top_level->get_fontset(font))
2924 XFontSetExtents *extents;
2926 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2927 return -extents->max_logical_extent.y;
2930 if(get_font_struct(font))
2931 return top_level->get_font_struct(font)->ascent;
2934 case MEDIUM_7SEGMENT:
2935 return get_resources()->medium_7segment[0]->get_h();
2940 int BC_WindowBase::get_text_descent(int font)
2944 if( (fstruct = get_xft_struct(font)) != 0 )
2945 return fstruct->descent;
2947 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2948 XFontSetExtents *extents;
2949 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2950 return (extents->max_logical_extent.height
2951 + extents->max_logical_extent.y);
2954 if(get_font_struct(font))
2955 return top_level->get_font_struct(font)->descent;
2960 int BC_WindowBase::get_text_height(int font, const char *text)
2965 if( (fstruct = get_xft_struct(font)) != 0 )
2966 rowh = fstruct->height;
2969 rowh = get_text_ascent(font) + get_text_descent(font);
2971 if(!text) return rowh;
2973 // Add height of lines
2974 int h = 0, i, length = strlen(text);
2975 for(i = 0; i <= length; i++)
2986 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2988 if(color_model < 0) color_model = top_level->get_color_model();
2989 return new BC_Bitmap(top_level, w, h, color_model);
2992 void BC_WindowBase::init_wait()
2994 #ifndef SINGLE_THREAD
2995 if(window_type != MAIN_WINDOW)
2996 top_level->init_wait();
2997 init_lock->lock("BC_WindowBase::init_wait");
2998 init_lock->unlock();
3002 int BC_WindowBase::accel_available(int color_model, int lock_it)
3004 if( window_type != MAIN_WINDOW )
3005 return top_level->accel_available(color_model, lock_it);
3007 lock_window("BC_WindowBase::accel_available");
3009 switch(color_model) {
3011 grab_port_id(color_model);
3015 grab_port_id(color_model);
3024 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3025 return xvideo_port_id >= 0 ? 1 : 0;
3029 int BC_WindowBase::grab_port_id(int color_model)
3031 if( !get_resources()->use_xvideo || // disabled
3032 !get_resources()->use_shm ) // Only local server is fast enough.
3034 if( xvideo_port_id >= 0 )
3035 return xvideo_port_id;
3037 unsigned int ver, rev, reqBase, eventBase, errorBase;
3038 if( Success != XvQueryExtension(display, // XV extension is available
3039 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3042 // XV adaptors are available
3043 unsigned int numAdapt = 0;
3044 XvAdaptorInfo *info = 0;
3045 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3049 // Translate from color_model to X color model
3050 int x_color_model = BC_CModels::bc_to_x(color_model);
3052 // Get adaptor with desired color model
3053 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3054 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3055 // adaptor supports XvImages
3056 int numFormats = 0, numPorts = info[i].num_ports;
3057 XvImageFormatValues *formats =
3058 XvListImageFormats(display, info[i].base_id, &numFormats);
3059 if( !formats ) continue;
3061 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3062 if( formats[j].id != x_color_model ) continue;
3063 // this adaptor supports the desired format, grab a port
3064 for( int k=0; k<numPorts; ++k ) {
3065 if( Success == XvGrabPort(top_level->display,
3066 info[i].base_id+k, CurrentTime) ) {
3067 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3068 xvideo_port_id = info[i].base_id + k;
3076 XvFreeAdaptorInfo(info);
3078 return xvideo_port_id;
3082 int BC_WindowBase::show_window(int flush)
3084 for(int i = 0; i < subwindows->size(); i++)
3086 subwindows->get(i)->show_window(0);
3089 XMapWindow(top_level->display, win);
3090 if(flush) XFlush(top_level->display);
3091 // XSync(top_level->display, 0);
3096 int BC_WindowBase::hide_window(int flush)
3098 for(int i = 0; i < subwindows->size(); i++)
3100 subwindows->get(i)->hide_window(0);
3103 XUnmapWindow(top_level->display, win);
3104 if(flush) this->flush();
3109 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3111 subwindows->append((BC_SubWindow*)menu_bar);
3113 menu_bar->parent_window = this;
3114 menu_bar->top_level = this->top_level;
3115 menu_bar->initialize();
3119 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3121 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3122 if(this != top_level) return top_level->add_popup(window);
3123 popups.append(window);
3127 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3129 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3130 if(this != top_level)
3131 top_level->remove_popup(window);
3133 popups.remove(window);
3134 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3138 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3140 subwindows->append(subwindow);
3142 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3144 // parent window must be set before the subwindow initialization
3145 subwindow->parent_window = this;
3146 subwindow->top_level = this->top_level;
3148 // Execute derived initialization
3149 subwindow->initialize();
3154 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3156 return add_subwindow(subwindow);
3159 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3161 if( !top_level->flash_enabled ) return 0;
3162 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3164 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3167 XClearArea(top_level->display, win, x, y, w, h, 0);
3171 XClearWindow(top_level->display, win);
3179 int BC_WindowBase::flash(int flush)
3181 flash(-1, -1, -1, -1, flush);
3185 void BC_WindowBase::flush()
3187 //if(!get_window_lock())
3188 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3189 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3190 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3191 int locked = get_window_lock();
3192 if( !locked ) lock_window("BC_WindowBase::flush");
3193 XFlush(top_level->display);
3194 if( !locked ) unlock_window();
3197 void BC_WindowBase::sync_display()
3199 int locked = get_window_lock();
3200 if( !locked ) lock_window("BC_WindowBase::sync_display");
3201 XSync(top_level->display, False);
3202 if( !locked ) unlock_window();
3205 int BC_WindowBase::get_window_lock()
3207 #ifdef SINGLE_THREAD
3208 return BC_Display::display_global->get_display_locked();
3210 return top_level->window_lock;
3214 int BC_WindowBase::lock_window(const char *location)
3216 if(top_level && top_level != this)
3218 top_level->lock_window(location);
3223 SET_LOCK(this, title, location);
3224 #ifdef SINGLE_THREAD
3225 BC_Display::lock_display(location);
3227 XLockDisplay(top_level->display);
3228 top_level->display_lock_owner = pthread_self();
3231 ++top_level->window_lock;
3235 printf("BC_WindowBase::lock_window top_level NULL\n");
3240 int BC_WindowBase::unlock_window()
3242 if(top_level && top_level != this)
3244 top_level->unlock_window();
3251 // if(!top_level->window_lock)
3253 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3257 if( top_level->window_lock > 0 )
3258 if( --top_level->window_lock == 0 )
3259 top_level->display_lock_owner = 0;
3260 #ifdef SINGLE_THREAD
3261 BC_Display::unlock_display();
3263 XUnlockDisplay(top_level->display);
3268 printf("BC_WindowBase::unlock_window top_level NULL\n");
3273 void BC_WindowBase::set_done(int return_value)
3275 if(done_set) return;
3277 if(window_type != MAIN_WINDOW)
3278 top_level->set_done(return_value);
3281 #ifdef SINGLE_THREAD
3282 this->return_value = return_value;
3283 BC_Display::display_global->arm_completion(this);
3284 completion_lock->unlock();
3285 #else // SINGLE_THREAD
3287 if( !event_thread ) return;
3288 XEvent *event = new_xevent();
3289 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3290 event->type = ClientMessage;
3291 ptr->message_type = SetDoneXAtom;
3293 this->return_value = return_value;
3294 // May lock up here because XSendEvent doesn't work too well
3295 // asynchronous with XNextEvent.
3296 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3298 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3304 void BC_WindowBase::close(int return_value)
3306 hide_window(); flush();
3307 set_done(return_value);
3310 int BC_WindowBase::get_w()
3315 int BC_WindowBase::get_h()
3320 int BC_WindowBase::get_x()
3325 int BC_WindowBase::get_y()
3331 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3332 // this is the bounding box of all the screens
3334 int BC_WindowBase::get_root_w(int lock_display)
3336 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3337 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3338 int result = WidthOfScreen(def_screen);
3339 if(lock_display) unlock_window();
3343 int BC_WindowBase::get_root_h(int lock_display)
3345 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3346 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3347 int result = HeightOfScreen(def_screen);
3348 if(lock_display) unlock_window();
3352 XineramaScreenInfo *
3353 BC_WindowBase::get_xinerama_info(int screen)
3355 if( !xinerama_info || !xinerama_screens ) return 0;
3357 for( int i=0; i<xinerama_screens; ++i )
3358 if( xinerama_info[i].screen_number == screen )
3359 return &xinerama_info[i];
3362 int top_x = get_x(), top_y = get_y();
3363 for( int i=0; i<xinerama_screens; ++i ) {
3364 int scr_y = top_y - xinerama_info[i].y_org;
3365 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3366 int scr_x = top_x - xinerama_info[i].x_org;
3367 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3368 return &xinerama_info[i];
3373 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3376 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3377 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3380 int root_w = get_root_w(0);
3381 int root_h = get_root_h(0);
3382 // Shift X based on position of current window if dual head
3383 if( (float)root_w/root_h > 1.8 ) {
3384 root_w = get_screen_w(0, 0);
3385 if( top_level->get_x() >= root_w )
3390 result = info->x_org;
3391 if(lock_display) unlock_window();
3395 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3397 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3398 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3399 int result = !info ? 0 : info->y_org;
3400 if(lock_display) unlock_window();
3404 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3407 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3408 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3410 int width = get_root_w(0);
3411 int height = get_root_h(0);
3412 if( (float)width/height > 1.8 ) {
3413 // If dual head, the screen width is > 16x9
3414 // but we only want to fill one screen
3415 // this code assumes the "big" screen is on the right
3416 int scr_w0 = width / 2;
3418 case 600: scr_w0 = 800; break;
3419 case 720: scr_w0 = 1280; break;
3420 case 1024: scr_w0 = 1280; break;
3421 case 1200: scr_w0 = 1600; break;
3422 case 1080: scr_w0 = 1920; break;
3424 int scr_w1 = width - scr_w0;
3425 result = screen > 0 ? scr_w1 :
3426 screen == 0 ? scr_w0 :
3427 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3433 result = info->width;
3434 if(lock_display) unlock_window();
3438 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3440 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3441 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3442 int result = info ? info->height : get_root_h(0);
3443 if(lock_display) unlock_window();
3447 // Bottom right corner
3448 int BC_WindowBase::get_x2()
3453 int BC_WindowBase::get_y2()
3458 int BC_WindowBase::get_video_on()
3463 int BC_WindowBase::get_hidden()
3465 return top_level->hidden;
3468 int BC_WindowBase::cursor_inside()
3470 return (top_level->cursor_x >= 0 &&
3471 top_level->cursor_y >= 0 &&
3472 top_level->cursor_x < w &&
3473 top_level->cursor_y < h);
3476 BC_WindowBase* BC_WindowBase::get_top_level()
3481 BC_WindowBase* BC_WindowBase::get_parent()
3483 return parent_window;
3486 int BC_WindowBase::get_color_model()
3488 return top_level->color_model;
3491 BC_Resources* BC_WindowBase::get_resources()
3493 return &BC_WindowBase::resources;
3496 BC_Synchronous* BC_WindowBase::get_synchronous()
3498 return BC_WindowBase::resources.get_synchronous();
3501 int BC_WindowBase::get_bg_color()
3506 void BC_WindowBase::set_bg_color(int color)
3508 this->bg_color = color;
3511 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3516 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3518 top_level->active_subwindow = subwindow;
3521 int BC_WindowBase::activate()
3526 int BC_WindowBase::deactivate()
3528 if(window_type == MAIN_WINDOW)
3530 if(top_level->active_menubar) top_level->active_menubar->deactivate();
3531 if(top_level->active_popup_menu) top_level->active_popup_menu->deactivate();
3532 if(top_level->active_subwindow) top_level->active_subwindow->deactivate();
3534 top_level->active_menubar = 0;
3535 top_level->active_popup_menu = 0;
3536 top_level->active_subwindow = 0;
3541 int BC_WindowBase::cycle_textboxes(int amount)
3544 BC_WindowBase *new_textbox = 0;
3548 BC_WindowBase *first_textbox = 0;
3549 find_next_textbox(&first_textbox, &new_textbox, result);
3550 if(!new_textbox) new_textbox = first_textbox;
3556 BC_WindowBase *last_textbox = 0;
3557 find_prev_textbox(&last_textbox, &new_textbox, result);
3558 if(!new_textbox) new_textbox = last_textbox;
3562 if(new_textbox != active_subwindow)
3565 new_textbox->activate();
3571 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3573 // Search subwindows for textbox
3574 for(int i = 0; i < subwindows->total && result < 2; i++)
3576 BC_WindowBase *test_subwindow = subwindows->values[i];
3577 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3584 if(!*first_textbox) *first_textbox = this;
3588 if(top_level->active_subwindow == this)
3594 *next_textbox = this;
3601 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3607 if(!*last_textbox) *last_textbox = this;
3611 if(top_level->active_subwindow == this)
3617 *prev_textbox = this;
3622 // Search subwindows for textbox
3623 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3625 BC_WindowBase *test_subwindow = subwindows->values[i];
3626 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3631 BC_Clipboard* BC_WindowBase::get_clipboard()
3633 #ifdef SINGLE_THREAD
3634 return BC_Display::display_global->clipboard;
3636 return top_level->clipboard;
3640 int BC_WindowBase::get_relative_cursor_x()
3642 int abs_x, abs_y, x, y, win_x, win_y;
3643 unsigned int temp_mask;
3646 XQueryPointer(top_level->display, top_level->win,
3647 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3650 XTranslateCoordinates(top_level->display, top_level->rootwin,
3651 win, abs_x, abs_y, &x, &y, &temp_win);
3656 int BC_WindowBase::get_relative_cursor_y()
3658 int abs_x, abs_y, x, y, win_x, win_y;
3659 unsigned int temp_mask;
3662 XQueryPointer(top_level->display, top_level->win,
3663 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3666 XTranslateCoordinates(top_level->display,
3667 top_level->rootwin, win, abs_x, abs_y, &x, &y, &temp_win);
3672 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3674 int abs_x, abs_y, win_x, win_y;
3675 unsigned int temp_mask;
3678 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
3679 XQueryPointer(top_level->display, top_level->win,
3680 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3682 if(lock_window) this->unlock_window();
3686 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3688 int abs_x, abs_y, win_x, win_y;
3689 unsigned int temp_mask;
3692 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
3693 XQueryPointer(top_level->display, top_level->win,
3694 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3696 if(lock_window) this->unlock_window();
3700 int BC_WindowBase::match_window(Window win)
3702 if (this->win == win) return 1;
3704 for(int i = 0; i < subwindows->total; i++) {
3705 result = subwindows->values[i]->match_window(win);
3706 if (result) return result;
3712 int BC_WindowBase::get_cursor_over_window()
3714 if(top_level != this) return top_level->get_cursor_over_window();
3716 int abs_x, abs_y, win_x, win_y;
3717 unsigned int temp_mask;
3718 Window root_return, child_return;
3720 int ret = XQueryPointer(display, win,
3721 &root_return, &child_return, &abs_x, &abs_y,
3722 &win_x, &win_y, &temp_mask);
3723 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3724 // __LINE__, title, win, ret, child_return);
3726 int result = !ret || child_return == None ? 0 :
3727 match_window(child_return);
3731 int BC_WindowBase::cursor_above()
3733 int rx = get_relative_cursor_x();
3734 if( rx < 0 || rx >= get_w() ) return 0;
3735 int ry = get_relative_cursor_y();
3736 if( ry < 0 || ry >= get_h() ) return 0;
3740 int BC_WindowBase::get_drag_x()
3742 return top_level->drag_x;
3745 int BC_WindowBase::get_drag_y()
3747 return top_level->drag_y;
3750 int BC_WindowBase::get_cursor_x()
3752 return top_level->cursor_x;
3755 int BC_WindowBase::get_cursor_y()
3757 return top_level->cursor_y;
3760 int BC_WindowBase::dump_windows()
3762 printf("\tBC_WindowBase::dump_windows window=%p win=%p\n",
3763 this, (void*)this->win);
3764 for(int i = 0; i < subwindows->size(); i++)
3765 subwindows->get(i)->dump_windows();
3766 for(int i = 0; i < popups.size(); i++)
3767 printf("\tBC_WindowBase::dump_windows popup=%p win=%p\n",
3768 popups.get(i), (void*)popups.get(i)->win);
3772 int BC_WindowBase::is_event_win()
3774 return this->win == top_level->event_win;
3777 void BC_WindowBase::set_dragging(int value)
3779 is_dragging = value;
3782 int BC_WindowBase::get_dragging()
3787 int BC_WindowBase::get_buttonpress()
3789 return top_level->button_number;
3792 int BC_WindowBase::get_button_down()
3794 return top_level->button_down;
3797 int BC_WindowBase::alt_down()
3799 return top_level->alt_mask;
3802 int BC_WindowBase::shift_down()
3804 return top_level->shift_mask;
3807 int BC_WindowBase::ctrl_down()
3809 return top_level->ctrl_mask;
3812 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3815 *length = top_level->wkey_string_length;
3816 return top_level->wkey_string;
3819 #ifdef X_HAVE_UTF8_STRING
3820 char* BC_WindowBase::get_keypress_utf8()
3822 return top_level->key_pressed_utf8;
3827 int BC_WindowBase::get_keypress()
3829 return top_level->key_pressed;
3832 int BC_WindowBase::get_double_click()
3834 return top_level->double_click;
3837 int BC_WindowBase::get_triple_click()
3839 return top_level->triple_click;
3842 int BC_WindowBase::get_bgcolor()
3847 int BC_WindowBase::resize_window(int w, int h)
3849 if(this->w == w && this->h == h) return 0;
3851 if(window_type == MAIN_WINDOW && !allow_resize)
3853 XSizeHints size_hints;
3854 size_hints.flags = PSize | PMinSize | PMaxSize;
3855 size_hints.width = w;
3856 size_hints.height = h;
3857 size_hints.min_width = w;
3858 size_hints.max_width = w;
3859 size_hints.min_height = h;
3860 size_hints.max_height = h;
3861 XSetNormalHints(top_level->display, win, &size_hints);
3863 XResizeWindow(top_level->display, win, w, h);
3868 pixmap = new BC_Pixmap(this, w, h);
3870 // Propagate to menubar
3871 for(int i = 0; i < subwindows->total; i++)
3873 subwindows->values[i]->dispatch_resize_event(w, h);
3876 draw_background(0, 0, w, h);
3877 if(top_level == this && get_resources()->recursive_resizing)
3878 resize_history.append(new BC_ResizeCall(w, h));
3882 // The only way for resize events to be propagated is by updating the internal w and h
3883 int BC_WindowBase::resize_event(int w, int h)
3885 if(window_type == MAIN_WINDOW)
3893 int BC_WindowBase::reposition_window(int x, int y)
3895 reposition_window(x, y, -1, -1);
3900 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
3904 // Some tools set their own dimensions before calling this, causing the
3905 // resize check to skip.
3909 if(w > 0 && w != this->w)
3915 if(h > 0 && h != this->h)
3921 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
3924 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
3926 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
3928 if(translation_count && window_type == MAIN_WINDOW)
3930 // KDE shifts window right and down.
3931 // FVWM leaves window alone and adds border around it.
3932 XMoveResizeWindow(top_level->display,
3934 x - BC_DisplayInfo::auto_reposition_x,
3935 y - BC_DisplayInfo::auto_reposition_y,
3941 XMoveResizeWindow(top_level->display,
3952 pixmap = new BC_Pixmap(this, this->w, this->h);
3953 // Propagate to menubar
3954 for(int i = 0; i < subwindows->total; i++)
3956 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
3959 // draw_background(0, 0, w, h);
3965 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
3967 return reposition_window(get_x()+dx, get_y()+dy, w, h);
3970 int BC_WindowBase::reposition_window_relative(int dx, int dy)
3972 return reposition_window_relative(dx, dy, -1, -1);
3975 void BC_WindowBase::set_tooltips(int v)
3977 get_resources()->tooltips_enabled = v;
3980 void BC_WindowBase::set_force_tooltip(int v)
3985 int BC_WindowBase::raise_window(int do_flush)
3987 XRaiseWindow(top_level->display, win);
3988 if(do_flush) XFlush(top_level->display);
3992 int BC_WindowBase::lower_window(int do_flush)
3994 XLowerWindow(top_level->display, win);
3995 if(do_flush) XFlush(top_level->display);
3999 void BC_WindowBase::set_background(VFrame *bitmap)
4001 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4003 bg_pixmap = new BC_Pixmap(this,
4006 shared_bg_pixmap = 0;
4007 draw_background(0, 0, w, h);
4010 void BC_WindowBase::put_title(const char *text)
4012 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4013 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4014 *cp++ = *bp >= ' ' ? *bp : ' ';
4018 void BC_WindowBase::set_title(const char *text)
4023 txlist[0] = this->title;
4026 XTextProperty titleprop;
4027 XmbTextListToTextProperty(top_level->display, txlist, 1,
4028 XStdICCTextStyle, &titleprop);
4029 XSetWMName(top_level->display, top_level->win, &titleprop);
4030 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4031 XFree(titleprop.value);
4036 void BC_WindowBase::set_utf8title(const char *text)
4038 XTextProperty titleprop;
4041 strcpy(this->title, text);
4042 txlist[0] = this->title;
4045 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4046 XUTF8StringStyle, &titleprop);
4047 XSetWMName(top_level->display, top_level->win, &titleprop);
4048 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4049 XFree(titleprop.value);
4054 const char *BC_WindowBase::get_title()
4059 int BC_WindowBase::get_toggle_value()
4061 return toggle_value;
4064 int BC_WindowBase::get_toggle_drag()
4069 int BC_WindowBase::set_icon(VFrame *data)
4071 if(icon_pixmap) delete icon_pixmap;
4072 icon_pixmap = new BC_Pixmap(top_level,
4077 if(icon_window) delete icon_window;
4078 icon_window = new BC_Popup(this,
4081 icon_pixmap->get_w(),
4082 icon_pixmap->get_h(),
4084 1, // All windows are hidden initially
4088 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4089 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4090 wm_hints.icon_mask = icon_pixmap->get_alpha();
4091 wm_hints.icon_window = icon_window->win;
4092 wm_hints.window_group = XGroupLeader;
4094 // for(int i = 0; i < 1000; i++)
4095 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4098 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4099 XSync(top_level->display, 0);
4103 int BC_WindowBase::set_w(int w)
4109 int BC_WindowBase::set_h(int h)
4115 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4117 BC_Resources *resources = get_resources();
4118 char string[BCTEXTLEN];
4119 int newest_id = - 1;
4120 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4122 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4123 resources->filebox_history[i].path[0] = 0;
4124 defaults->get(string, resources->filebox_history[i].path);
4125 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4126 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4127 if(resources->filebox_history[i].id > newest_id)
4128 newest_id = resources->filebox_history[i].id;
4131 resources->filebox_id = newest_id + 1;
4132 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4133 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4134 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4135 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4136 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4137 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4138 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4139 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4140 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4141 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4142 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4143 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4147 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4149 BC_Resources *resources = get_resources();
4150 char string[BCTEXTLEN];
4151 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4153 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4154 defaults->update(string, resources->filebox_history[i].path);
4155 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4156 defaults->update(string, resources->filebox_history[i].id);
4158 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4159 defaults->update("FILEBOX_W", resources->filebox_w);
4160 defaults->update("FILEBOX_H", resources->filebox_h);
4161 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4162 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4163 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4164 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4165 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4166 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4167 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4168 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4169 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4175 // For some reason XTranslateCoordinates can take a long time to return.
4176 // We work around this by only calling it when the event windows are different.
4177 void BC_WindowBase::translate_coordinates(Window src_w,
4189 *dest_x_return = src_x;
4190 *dest_y_return = src_y;
4194 XTranslateCoordinates(top_level->display,
4202 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4206 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4208 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4211 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4213 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4221 #ifdef HAVE_LIBXXF86VM
4222 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4226 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4228 XF86VidModeModeInfo **vm_modelines;
4229 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4230 for (i = 0; i < vm_count; i++) {
4231 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4234 display = top_level->display;
4235 if (vm_modelines[*vm]->hdisplay == *width)
4239 *width = vm_modelines[*vm]->hdisplay;
4240 *height = vm_modelines[*vm]->vdisplay;
4245 void BC_WindowBase::scale_vm(int vm)
4247 int foo,bar,dotclock;
4248 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4251 XF86VidModeModeInfo **vm_modelines;
4252 XF86VidModeModeLine vml;
4253 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4254 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4255 orig_modeline.dotclock = dotclock;
4256 orig_modeline.hdisplay = vml.hdisplay;
4257 orig_modeline.hsyncstart = vml.hsyncstart;
4258 orig_modeline.hsyncend = vml.hsyncend;
4259 orig_modeline.htotal = vml.htotal;
4260 orig_modeline.vdisplay = vml.vdisplay;
4261 orig_modeline.vsyncstart = vml.vsyncstart;
4262 orig_modeline.vsyncend = vml.vsyncend;
4263 orig_modeline.vtotal = vml.vtotal;
4264 orig_modeline.flags = vml.flags;
4265 orig_modeline.privsize = vml.privsize;
4266 // orig_modeline.private = vml.private;
4267 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4268 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4269 XFlush(top_level->display);
4273 void BC_WindowBase::restore_vm()
4275 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4276 XFlush(top_level->display);
4299 #ifndef SINGLE_THREAD
4300 int BC_WindowBase::get_event_count()
4302 event_lock->lock("BC_WindowBase::get_event_count");
4303 int result = common_events.total;
4304 event_lock->unlock();
4308 XEvent* BC_WindowBase::get_event()
4311 while(!done && !result)
4313 event_condition->lock("BC_WindowBase::get_event");
4314 event_lock->lock("BC_WindowBase::get_event");
4316 if(common_events.total && !done)
4318 result = common_events.values[0];
4319 common_events.remove_number(0);
4322 event_lock->unlock();
4327 void BC_WindowBase::put_event(XEvent *event)
4329 event_lock->lock("BC_WindowBase::put_event");
4330 common_events.append(event);
4331 event_lock->unlock();
4332 event_condition->unlock();
4335 void BC_WindowBase::dequeue_events(Window win)
4337 event_lock->lock("BC_WindowBase::dequeue_events");
4339 int out = 0, total = common_events.size();
4340 for( int in=0; in<total; ++in ) {
4341 if( common_events[in]->xany.window == win ) continue;
4342 common_events[out++] = common_events[in];
4344 common_events.total = out;
4346 event_lock->unlock();
4349 #endif // SINGLE_THREAD
4351 int BC_WindowBase::get_id()
4357 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4359 int w = vframe->get_w(), h = vframe->get_h();
4360 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4361 icon->draw_vframe(vframe, 0,0, w,h, 0,0);