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)
113 if(top_level->active_menubar == this) top_level->active_menubar = 0;
114 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
115 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
116 // Remove pointer from parent window to this
117 parent_window->subwindows->remove(this);
120 if(icon_window) delete icon_window;
121 if(window_type == POPUP_WINDOW) parent_window->remove_popup(this);
123 // Delete the subwindows
127 while(subwindows->total)
129 // Subwindow removes its own pointer
130 delete subwindows->values[0];
138 if( get_resources()->get_synchronous() &&
139 (top_level->options & GLX_WINDOW) && glx_win != 0 )
140 get_resources()->get_synchronous()->delete_window(this);
143 XDestroyWindow(top_level->display, win);
145 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
146 if(icon_pixmap) delete icon_pixmap;
147 if(temp_bitmap) delete temp_bitmap;
148 top_level->active_bitmaps.remove_buffers(this);
149 if(_7segment_pixmaps)
151 for(int i = 0; i < TOTAL_7SEGMENT; i++)
152 delete _7segment_pixmaps[i];
154 delete [] _7segment_pixmaps;
159 if(window_type == MAIN_WINDOW)
161 XFreeGC(display, gc);
162 static XFontStruct *BC_WindowBase::*xfont[] = {
163 &BC_WindowBase::smallfont,
164 &BC_WindowBase::mediumfont,
165 &BC_WindowBase::largefont,
166 &BC_WindowBase::bigfont,
168 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
169 XFreeFont(display, this->*xfont[i]);
171 // bug in X causes XRenderExtensionInfo to be damaged if this is done here
172 // left to be done in XCloseDisplay by Xlib.
173 #if defined(HAVE_XFT) && 0
174 static void *BC_WindowBase::*xft_font[] = {
175 &BC_WindowBase::smallfont_xft,
176 &BC_WindowBase::mediumfont_xft,
177 &BC_WindowBase::largefont_xft,
178 &BC_WindowBase::bigfont_xft,
179 &BC_WindowBase::bold_smallfont_xft,
180 &BC_WindowBase::bold_mediumfont_xft,
181 &BC_WindowBase::bold_largefont_xft,
183 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
184 XftFont *xft = (XftFont *)(this->*xft_font[i]);
185 if( xft ) XftFontClose (display, xft);
194 XFree(xinerama_info);
195 xinerama_screens = 0;
199 // Can't close display if another thread is waiting for events.
200 // Synchronous thread must delete display it owns a GLX_WINDOW
201 // Must be last reference to display.
202 #ifndef SINGLE_THREAD
204 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
205 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
206 "implemented for BC_Pixmap.\n"));
207 get_resources()->get_synchronous()->delete_display(this);
211 XCloseDisplay(display);
212 // clipboard uses a different display connection
213 clipboard->stop_clipboard();
215 #endif // SINGLE_THREAD
218 resize_history.remove_all_objects();
220 #ifndef SINGLE_THREAD
221 common_events.remove_all_objects();
223 delete event_condition;
226 top_level->window_lock = 0;
227 BC_Display::unlock_display();
232 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
233 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
234 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
237 UNSET_ALL_LOCKS(this)
240 int BC_WindowBase::initialize()
260 xinerama_screens = 0;
265 translation_events = 0;
266 ctrl_mask = shift_mask = alt_mask = 0;
267 cursor_x = cursor_y = button_number = 0;
279 active_popup_menu = 0;
280 active_subwindow = 0;
283 _7segment_pixmaps = 0;
286 // next_repeat_id = 0;
289 current_font = MEDIUMFONT;
290 current_color = BLACK;
291 current_cursor = ARROW_CURSOR;
294 shared_bg_pixmap = 0;
297 window_type = MAIN_WINDOW;
298 translation_count = 0;
299 x_correction = y_correction = 0;
308 #ifdef HAVE_LIBXXF86VM
312 bold_largefont_xft = 0;
313 bold_mediumfont_xft = 0;
314 bold_smallfont_xft = 0;
322 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
324 // Need these right away since put_event is called before run_window sometimes.
325 event_lock = new Mutex("BC_WindowBase::event_lock");
326 event_condition = new Condition(0, "BC_WindowBase::event_condition");
327 init_lock = new Condition(0, "BC_WindowBase::init_lock");
330 cursor_timer = new Timer;
333 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
334 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
335 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
347 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
350 ButtonReleaseMask | \
351 PointerMotionMask | \
355 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
367 const char *display_name,
369 BC_Pixmap *bg_pixmap,
372 XSetWindowAttributes attr;
374 XSizeHints size_hints;
377 #ifdef HAVE_LIBXXF86VM
381 id = get_resources()->get_id();
383 if(parent_window) top_level = parent_window->top_level;
384 if( top_level ) { // need this to avoid deadlock with Xlib's locks
386 lock_window("BC_WindowBase::create_window");
388 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
390 #ifdef HAVE_LIBXXF86VM
391 if(window_type == VIDMODE_SCALED_WINDOW)
392 closest_vm(&vm,&w,&h);
399 this->bg_color = bg_color;
400 this->window_type = window_type;
402 this->private_color = private_color;
403 this->parent_window = parent_window;
404 this->bg_pixmap = bg_pixmap;
405 this->allow_resize = allow_resize;
407 strcpy(this->display_name, display_name);
409 this->display_name[0] = 0;
412 if(bg_pixmap) shared_bg_pixmap = 1;
414 subwindows = new BC_SubWindowList;
416 if(window_type == MAIN_WINDOW)
419 parent_window = this;
423 display = BC_Display::get_display(display_name);
424 BC_Display::lock_display("BC_WindowBase::create_window");
425 // BC_Display::display_global->new_window(this);
428 // get the display connection
430 // This function must be the first Xlib
431 // function a multi-threaded program calls
433 display = init_display(display_name);
434 if( shm_completion_event < 0 ) shm_completion_event =
435 ShmCompletion + XShmGetEventBase(display);
438 screen = DefaultScreen(display);
439 rootwin = RootWindow(display, screen);
440 // window placement boundaries
441 if( !xinerama_screens && XineramaIsActive(display) )
442 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
443 root_w = get_root_w(0);
444 root_h = get_root_h(0);
447 vis = get_glx_visual(display);
450 vis = DefaultVisual(display, screen);
452 default_depth = DefaultDepth(display, screen);
454 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
455 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
458 // This must be done before fonts to know if antialiasing is available.
461 if(resources.use_shm < 0) resources.initialize_display(this);
462 x_correction = BC_DisplayInfo::get_left_border();
463 y_correction = BC_DisplayInfo::get_top_border();
465 // clamp window placement
466 if(this->x + this->w + x_correction > root_w)
467 this->x = root_w - this->w - x_correction;
468 if(this->y + this->h + y_correction > root_h)
469 this->y = root_h - this->h - y_correction;
470 if(this->x < 0) this->x = 0;
471 if(this->y < 0) this->y = 0;
473 if(this->bg_color == -1)
474 this->bg_color = resources.get_bg_color();
476 // printf("bcwindowbase 1 %s\n", title);
477 // if(window_type == MAIN_WINDOW) sleep(1);
478 // printf("bcwindowbase 10\n");
484 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
486 attr.event_mask = DEFAULT_EVENT_MASKS |
487 StructureNotifyMask |
491 attr.background_pixel = get_color(this->bg_color);
492 attr.colormap = cmap;
493 attr.cursor = get_cursor_struct(ARROW_CURSOR);
495 win = XCreateWindow(display, rootwin,
496 this->x, this->y, this->w, this->h, 0,
497 top_level->default_depth, InputOutput,
499 XGetNormalHints(display, win, &size_hints);
501 size_hints.flags = PSize | PMinSize | PMaxSize;
502 size_hints.width = this->w;
503 size_hints.height = this->h;
504 size_hints.min_width = allow_resize ? minw : this->w;
505 size_hints.max_width = allow_resize ? 32767 : this->w;
506 size_hints.min_height = allow_resize ? minh : this->h;
507 size_hints.max_height = allow_resize ? 32767 : this->h;
508 if(x > -BC_INFINITY && x < BC_INFINITY)
510 size_hints.flags |= PPosition;
511 size_hints.x = this->x;
512 size_hints.y = this->y;
516 txlist[0] = this->title;
518 XTextProperty titleprop;
519 if(options & WINDOW_UTF8)
520 Xutf8TextListToTextProperty(display, txlist, 1,
521 XUTF8StringStyle, &titleprop);
523 XmbTextListToTextProperty(display, txlist, 1,
524 XStdICCTextStyle, &titleprop);
525 XSetWMProperties(display, win, &titleprop, &titleprop,
526 0, 0, &size_hints, 0, 0);
527 XFree(titleprop.value);
530 #ifndef SINGLE_THREAD
531 clipboard = new BC_Clipboard(display_name);
532 clipboard->start_clipboard();
538 Atom ClientLeaderXAtom;
539 if (XGroupLeader == 0)
541 const char *instance_name = "cinelerra";
542 const char *class_name = "Cinelerra";
543 XClassHint *class_hints = XAllocClassHint();
544 class_hints->res_name = (char*)instance_name;
545 class_hints->res_class = (char*)class_name;
546 XSetClassHint(top_level->display, win, class_hints);
548 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
549 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
550 PropModeReplace, (unsigned char *)&XGroupLeader, true);
555 #ifdef HAVE_LIBXXF86VM
556 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
563 #ifdef HAVE_LIBXXF86VM
564 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
566 if(window_type == POPUP_WINDOW)
569 mask = CWEventMask | CWBackPixel | CWColormap |
570 CWOverrideRedirect | CWSaveUnder | CWCursor;
572 attr.event_mask = DEFAULT_EVENT_MASKS |
576 if(this->bg_color == -1)
577 this->bg_color = resources.get_bg_color();
578 attr.background_pixel = top_level->get_color(bg_color);
579 attr.colormap = top_level->cmap;
580 if(top_level->is_hourglass)
581 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
583 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
584 attr.override_redirect = True;
585 attr.save_under = True;
587 win = XCreateWindow(top_level->display,
588 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
589 top_level->default_depth, InputOutput, top_level->vis, mask,
591 top_level->add_popup(this);
594 if(window_type == SUB_WINDOW)
596 mask = CWEventMask | CWBackPixel | CWCursor;
597 attr.event_mask = DEFAULT_EVENT_MASKS;
598 attr.background_pixel = top_level->get_color(this->bg_color);
599 if(top_level->is_hourglass)
600 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
602 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
603 win = XCreateWindow(top_level->display,
604 parent_window->win, this->x, this->y, this->w, this->h, 0,
605 top_level->default_depth, InputOutput, top_level->vis, mask,
608 if(!hidden) XMapWindow(top_level->display, win);
611 // Create pixmap for all windows
612 pixmap = new BC_Pixmap(this, this->w, this->h);
614 // Set up options for main window
615 if(window_type == MAIN_WINDOW)
617 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
619 this->bg_pixmap = new BC_Pixmap(this,
620 get_resources()->bg_image,
624 if(!hidden) show_window();
631 draw_background(0, 0, this->w, this->h);
633 flash(-1, -1, -1, -1, 0);
635 // Set up options for popup window
636 #ifdef HAVE_LIBXXF86VM
637 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
639 if(window_type == POPUP_WINDOW)
643 if(!hidden) show_window();
645 get_resources()->create_window_lock->unlock();
646 if(need_lock) unlock_window();
651 Display* BC_WindowBase::init_display(const char *display_name)
655 if(display_name && display_name[0] == 0) display_name = NULL;
656 if((display = XOpenDisplay(display_name)) == NULL) {
657 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
659 if(getenv("DISPLAY") == NULL) {
660 printf(_("'DISPLAY' environment variable not set.\n"));
664 // Try again with default display.
665 if((display = XOpenDisplay(0)) == NULL) {
666 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
674 Display* BC_WindowBase::get_display()
676 return top_level->display;
679 int BC_WindowBase::get_screen()
681 return top_level->screen;
684 int BC_WindowBase::run_window()
690 // Events may have been sent before run_window so can't initialize them here.
693 set_repeat(get_resources()->tooltip_delay);
694 BC_Display::display_global->new_window(this);
696 // If the first window created, run the display loop in this thread.
697 if(BC_Display::display_global->is_first(this))
699 BC_Display::unlock_display();
700 BC_Display::display_global->loop();
704 BC_Display::unlock_display();
705 completion_lock->lock("BC_WindowBase::run_window");
708 BC_Display::lock_display("BC_WindowBase::run_window");
709 BC_Display::display_global->delete_window(this);
711 unset_all_repeaters();
713 BC_Display::unlock_display();
715 #else // SINGLE_THREAD
720 set_repeat(get_resources()->tooltip_delay);
722 // Start X server events
723 event_thread = new BC_WindowEvents(this);
724 event_thread->start();
730 // Handle common events
736 unset_all_repeaters();
740 event_condition->reset();
741 common_events.remove_all_objects();
745 #endif // SINGLE_THREAD
750 int BC_WindowBase::get_key_masks(unsigned int key_state)
752 // printf("BC_WindowBase::get_key_masks %llx\n",
753 // event->xkey.state);
754 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
755 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
756 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
761 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
763 BC_KeyboardHandlerLock set;
764 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
767 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
769 BC_KeyboardHandlerLock set;
770 int i = BC_KeyboardHandler::listeners.size();
771 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
772 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
775 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
777 int result = (win->*handler)(wp);
781 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
784 BC_KeyboardHandlerLock set;
785 for( int i=0; !result && i<listeners.size(); ++i ) {
786 BC_KeyboardHandler *listener = listeners[i];
787 result = listener->run_event(wp);
792 void BC_KeyboardHandler::kill_grabs()
794 BC_KeyboardHandlerLock set;
795 for( int i=0; i<listeners.size(); ++i ) {
796 BC_WindowBase *win = listeners[i]->win;
797 if( win->get_window_type() != POPUP_WINDOW ) continue;
798 ((BC_Popup *)win)->ungrab_keyboard();
802 void BC_ActiveBitmaps::reque(XEvent *event)
804 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
805 ShmSeg shmseg = shm_ev->shmseg;
806 Drawable drawable = shm_ev->drawable;
807 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
808 active_lock.lock("BC_BitmapImage::reque");
809 BC_BitmapImage *bfr = first;
810 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
811 if( bfr && bfr->drawable == drawable )
813 active_lock.unlock();
815 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
816 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
817 // (int)shmseg, (int)drawable);
820 if( bfr->drawable != drawable ) return;
821 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
822 bfr->bitmap->reque(bfr);
825 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
827 active_lock.lock("BC_BitmapImage::insert");
828 bfr->drawable = pixmap;
830 active_lock.unlock();
833 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
835 active_lock.lock("BC_ActiveBitmaps::remove");
836 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
838 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
839 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
841 active_lock.unlock();
844 BC_ActiveBitmaps::BC_ActiveBitmaps()
848 BC_ActiveBitmaps::~BC_ActiveBitmaps()
854 int BC_WindowBase::keysym_lookup(XEvent *event)
856 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
857 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
859 if( event->xany.send_event && !event->xany.serial ) {
860 keysym = (KeySym) event->xkey.keycode;
861 keys_return[0] = keysym;
864 wkey_string_length = 0;
866 if( input_context ) {
867 wkey_string_length = XwcLookupString(input_context,
868 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
869 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
870 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
873 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
874 keys_return, KEYPRESSLEN, &keysym, &stat);
875 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
876 if( stat == XLookupBoth ) return ret;
877 if( stat == XLookupKeySym ) return 0;
879 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
880 wkey_string_length = ret;
881 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
885 pthread_t locking_task = -1;
886 int locking_event = -1;
887 int locking_message = -1;
889 int BC_WindowBase::dispatch_event(XEvent *event)
893 XClientMessageEvent *ptr;
894 int cancel_resize, cancel_translation;
899 #ifndef SINGLE_THREAD
900 // If an event is waiting get it, otherwise
901 // wait for next event only if there are no compressed events.
902 if(get_event_count() ||
903 (!motion_events && !resize_events && !translation_events))
906 // Lock out window deletions
907 lock_window("BC_WindowBase::dispatch_event 1");
908 locking_event = event->type;
909 locking_task = pthread_self();
910 locking_message = event->xclient.message_type;
913 // Handle compressed events
915 lock_window("BC_WindowBase::dispatch_event 2");
917 dispatch_resize_event(last_resize_w, last_resize_h);
919 dispatch_motion_event();
920 if(translation_events)
921 dispatch_translation_event();
932 //static const char *event_names[] = {
933 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
934 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
935 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
936 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
937 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
938 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
939 // "GenericEvent", "LASTEvent",
943 //if(event->type != ClientMessage) {
944 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
945 //__LINE__, title, event, event->type,
946 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
947 // event_names[event->type] : "Unknown");
953 switch(event->type) {
955 // Clear the resize buffer
956 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
957 // Clear the motion buffer since this can clear the window
960 dispatch_motion_event();
963 ptr = (XClientMessageEvent*)event;
966 if(ptr->message_type == ProtoXAtom &&
967 (Atom)ptr->data.l[0] == DelWinXAtom)
972 if(ptr->message_type == RepeaterXAtom)
974 dispatch_repeat_event(ptr->data.l[0]);
975 // Make sure the repeater still exists.
976 // for(int i = 0; i < repeaters.total; i++)
978 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
980 // dispatch_repeat_event_master(ptr->data.l[0]);
986 if(ptr->message_type == SetDoneXAtom)
990 { // We currently use X marshalling for xatom events, we can switch to something else later
991 recieve_custom_xatoms((xatom_event *)ptr);
1002 dispatch_focus_out();
1014 get_key_masks(event->xbutton.state);
1015 cursor_x = event->xbutton.x;
1016 cursor_y = event->xbutton.y;
1017 button_number = event->xbutton.button;
1019 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1020 event_win = event->xany.window;
1021 if (button_number < 6)
1023 if(button_number < 4)
1025 button_pressed = event->xbutton.button;
1026 button_time1 = button_time2;
1027 button_time2 = button_time3;
1028 button_time3 = event->xbutton.time;
1031 drag_win = event_win;
1032 drag_x1 = cursor_x - get_resources()->drag_radius;
1033 drag_x2 = cursor_x + get_resources()->drag_radius;
1034 drag_y1 = cursor_y - get_resources()->drag_radius;
1035 drag_y2 = cursor_y + get_resources()->drag_radius;
1037 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1040 button_time3 = button_time2 = button_time1 = 0;
1042 if((long)(button_time3 - button_time2) < resources.double_click)
1045 // button_time3 = button_time2 = button_time1 = 0;
1053 dispatch_button_press();
1058 get_key_masks(event->xbutton.state);
1059 button_number = event->xbutton.button;
1060 event_win = event->xany.window;
1061 if (button_number < 6)
1063 if(button_number < 4)
1065 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1067 dispatch_button_release();
1072 event_win = event->xany.window;
1073 dispatch_expose_event();
1077 get_key_masks(event->xmotion.state);
1078 // Dispatch previous motion event if this is a subsequent motion from a different window
1079 if(motion_events && last_motion_win != event->xany.window)
1081 dispatch_motion_event();
1084 // Buffer the current motion
1086 last_motion_state = event->xmotion.state;
1087 last_motion_x = event->xmotion.x;
1088 last_motion_y = event->xmotion.y;
1089 last_motion_win = event->xany.window;
1092 case ConfigureNotify:
1093 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1095 // event->xany.window,
1098 XTranslateCoordinates(top_level->display,
1106 last_resize_w = event->xconfigure.width;
1107 last_resize_h = event->xconfigure.height;
1110 cancel_translation = 0;
1112 // Resize history prevents responses to recursive resize requests
1113 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1115 if(resize_history.values[i]->w == last_resize_w &&
1116 resize_history.values[i]->h == last_resize_h)
1118 delete resize_history.values[i];
1119 resize_history.remove_number(i);
1124 if(last_resize_w == w && last_resize_h == h)
1132 if((last_translate_x == x && last_translate_y == y))
1133 cancel_translation = 1;
1135 if(!cancel_translation)
1137 translation_events = 1;
1140 translation_count++;
1144 get_key_masks(event->xkey.state);
1145 keys_return[0] = 0; keysym = -1;
1146 if(XFilterEvent(event, win)) {
1149 if( keysym_lookup(event) < 0 ) {
1150 printf("keysym %x\n", (uint32_t)keysym);
1154 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1158 // block out control keys
1159 if(keysym > 0xffe0 && keysym < 0xffff) break;
1160 // block out Alt_GR key
1161 if(keysym == 0xfe03) break;
1164 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1166 #ifdef X_HAVE_UTF8_STRING
1167 //It's Ascii or UTF8?
1168 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1169 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1171 if( ((keys_return[1] & 0xff) > 0x80) &&
1172 ((keys_return[0] & 0xff) > 0xC0) ) {
1173 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1174 key_pressed = keysym & 0xff;
1180 // block out extra keys
1190 // Translate key codes
1191 case XK_Return: key_pressed = RETURN; break;
1192 case XK_Up: key_pressed = UP; break;
1193 case XK_Down: key_pressed = DOWN; break;
1194 case XK_Left: key_pressed = LEFT; break;
1195 case XK_Right: key_pressed = RIGHT; break;
1196 case XK_Next: key_pressed = PGDN; break;
1197 case XK_Prior: key_pressed = PGUP; break;
1198 case XK_BackSpace: key_pressed = BACKSPACE; break;
1199 case XK_Escape: key_pressed = ESC; break;
1202 key_pressed = LEFTTAB;
1206 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1207 case XK_underscore: key_pressed = '_'; break;
1208 case XK_asciitilde: key_pressed = '~'; break;
1209 case XK_Delete: key_pressed = DELETE; break;
1210 case XK_Home: key_pressed = HOME; break;
1211 case XK_End: key_pressed = END; break;
1214 case XK_KP_Enter: key_pressed = KPENTER; break;
1215 case XK_KP_Add: key_pressed = KPPLUS; break;
1216 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1217 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1218 case XK_KP_Divide: key_pressed = KPSLASH; break;
1220 case XK_KP_End: key_pressed = KP1; break;
1222 case XK_KP_Down: key_pressed = KP2; break;
1224 case XK_KP_Page_Down: key_pressed = KP3; break;
1226 case XK_KP_Left: key_pressed = KP4; break;
1228 case XK_KP_Begin: key_pressed = KP5; break;
1230 case XK_KP_Right: key_pressed = KP6; break;
1232 case XK_KP_Home: key_pressed = KP7; break;
1234 case XK_KP_Up: key_pressed = KP8; break;
1236 case XK_KP_Page_Up: key_pressed = KP9; break;
1238 case XK_KP_Insert: key_pressed = KPINS; break;
1240 case XK_KP_Delete: key_pressed = KPDEL; break;
1241 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1243 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1244 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1245 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1246 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1247 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1248 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1249 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1250 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1251 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1252 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1253 // above case XK_Left: key_pressed = LEFT; break; /* left */
1254 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1255 // above case XK_Down: key_pressed = DOWN; break; /* down */
1256 // above case XK_Up: key_pressed = UP; break; /* up */
1257 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1258 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1259 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1260 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1261 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1262 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1263 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1264 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1265 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1266 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1267 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1268 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1271 key_pressed = keysym & 0xff;
1272 #ifdef X_HAVE_UTF8_STRING
1273 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1278 #ifdef X_HAVE_UTF8_STRING
1280 key_pressed_utf8 = keys_return;
1285 if( top_level == this )
1286 result = BC_KeyboardHandler::run_listeners(this);
1288 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1290 result = dispatch_keypress_event();
1291 // Handle some default keypresses
1294 if(key_pressed == 'w' ||
1303 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1304 dispatch_keyrelease_event();
1305 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1306 // keysym, event->xkey.state);
1310 event_win = event->xany.window;
1311 dispatch_cursor_leave();
1315 event_win = event->xany.window;
1316 cursor_x = event->xcrossing.x;
1317 cursor_y = event->xcrossing.y;
1318 dispatch_cursor_enter();
1323 //printf("100 %s %p %d\n", title, event, event->type);
1324 //if(event->type != ClientMessage) dump();
1326 #ifndef SINGLE_THREAD
1328 if(event) delete event;
1330 // if(done) completion_lock->unlock();
1333 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1337 int BC_WindowBase::dispatch_expose_event()
1340 for(int i = 0; i < subwindows->total && !result; i++)
1342 result = subwindows->values[i]->dispatch_expose_event();
1345 // Propagate to user
1346 if(!result) expose_event();
1350 int BC_WindowBase::dispatch_resize_event(int w, int h)
1352 // Can't store new w and h until the event is handles
1353 // because bcfilebox depends on the old w and h to
1354 // reposition widgets.
1355 if(window_type == MAIN_WINDOW)
1359 pixmap = new BC_Pixmap(this, w, h);
1361 clear_box(0, 0, w, h);
1364 // Propagate to subwindows
1365 for(int i = 0; i < subwindows->total; i++)
1367 subwindows->values[i]->dispatch_resize_event(w, h);
1370 // Propagate to user
1373 if(window_type == MAIN_WINDOW)
1381 int BC_WindowBase::dispatch_translation_event()
1383 translation_events = 0;
1384 if(window_type == MAIN_WINDOW)
1388 x = last_translate_x;
1389 y = last_translate_y;
1390 // Correct for window manager offsets
1395 for(int i = 0; i < subwindows->total; i++)
1397 subwindows->values[i]->dispatch_translation_event();
1400 translation_event();
1404 int BC_WindowBase::dispatch_motion_event()
1409 if(top_level == this)
1412 event_win = last_motion_win;
1413 get_key_masks(last_motion_state);
1416 if(get_button_down() && !active_menubar && !active_popup_menu)
1420 cursor_x = last_motion_x;
1421 cursor_y = last_motion_y;
1422 result = dispatch_drag_motion();
1426 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1427 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1432 result = dispatch_drag_start();
1436 cursor_x = last_motion_x;
1437 cursor_y = last_motion_y;
1439 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1442 // active_popup_menu,
1443 // active_subwindow);
1445 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1446 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1447 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1450 // Dispatch in stacking order
1451 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1453 result = subwindows->values[i]->dispatch_motion_event();
1456 if(!result) result = cursor_motion_event(); // give to user
1460 int BC_WindowBase::dispatch_keypress_event()
1463 if(top_level == this)
1465 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1468 for(int i = 0; i < subwindows->total && !result; i++)
1470 result = subwindows->values[i]->dispatch_keypress_event();
1473 if(!result) result = keypress_event();
1478 int BC_WindowBase::dispatch_keyrelease_event()
1481 if(top_level == this)
1483 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1486 for(int i = 0; i < subwindows->total && !result; i++)
1488 result = subwindows->values[i]->dispatch_keyrelease_event();
1491 if(!result) result = keyrelease_event();
1496 int BC_WindowBase::dispatch_focus_in()
1498 for(int i = 0; i < subwindows->total; i++)
1500 subwindows->values[i]->dispatch_focus_in();
1508 int BC_WindowBase::dispatch_focus_out()
1510 for(int i = 0; i < subwindows->total; i++)
1512 subwindows->values[i]->dispatch_focus_out();
1520 int BC_WindowBase::get_has_focus()
1522 return top_level->has_focus;
1525 int BC_WindowBase::get_deleting()
1527 if(is_deleting) return 1;
1528 if(parent_window && parent_window->get_deleting()) return 1;
1532 int BC_WindowBase::dispatch_button_press()
1537 if(top_level == this)
1539 if(active_menubar) result = active_menubar->dispatch_button_press();
1540 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1541 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1544 for(int i = 0; i < subwindows->total && !result; i++)
1546 result = subwindows->values[i]->dispatch_button_press();
1549 if(!result) result = button_press_event();
1555 int BC_WindowBase::dispatch_button_release()
1558 if(top_level == this)
1560 if(active_menubar) result = active_menubar->dispatch_button_release();
1561 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1562 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1563 if(!result && button_number != 4 && button_number != 5)
1564 result = dispatch_drag_stop();
1567 for(int i = 0; i < subwindows->total && !result; i++)
1569 result = subwindows->values[i]->dispatch_button_release();
1574 result = button_release_event();
1581 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1584 // all repeat event handlers get called and decide based on activity and duration
1585 // whether to respond
1586 for(int i = 0; i < subwindows->total; i++)
1588 subwindows->values[i]->dispatch_repeat_event(duration);
1592 repeat_event(duration);
1596 // Unlock next repeat signal
1597 if(window_type == MAIN_WINDOW)
1599 #ifdef SINGLE_THREAD
1600 BC_Display::display_global->unlock_repeaters(duration);
1602 for(int i = 0; i < repeaters.total; i++)
1604 if(repeaters.values[i]->delay == duration)
1606 repeaters.values[i]->repeat_lock->unlock();
1614 void BC_WindowBase::unhide_cursor()
1619 if(top_level->is_hourglass)
1620 set_cursor(HOURGLASS_CURSOR, 1, 0);
1622 set_cursor(current_cursor, 1, 0);
1624 cursor_timer->update();
1628 void BC_WindowBase::update_video_cursor()
1630 if(video_on && !is_transparent)
1632 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1635 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1636 cursor_timer->update();
1641 cursor_timer->update();
1646 int BC_WindowBase::dispatch_cursor_leave()
1650 for(int i = 0; i < subwindows->total; i++)
1652 subwindows->values[i]->dispatch_cursor_leave();
1655 cursor_leave_event();
1659 int BC_WindowBase::dispatch_cursor_enter()
1665 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1666 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1667 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1669 for(int i = 0; !result && i < subwindows->total; i++)
1671 result = subwindows->values[i]->dispatch_cursor_enter();
1674 if(!result) result = cursor_enter_event();
1678 int BC_WindowBase::cursor_enter_event()
1683 int BC_WindowBase::cursor_leave_event()
1688 int BC_WindowBase::close_event()
1694 int BC_WindowBase::dispatch_drag_start()
1697 if(active_menubar) result = active_menubar->dispatch_drag_start();
1698 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1699 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1701 for(int i = 0; i < subwindows->total && !result; i++)
1703 result = subwindows->values[i]->dispatch_drag_start();
1706 if(!result) result = is_dragging = drag_start_event();
1710 int BC_WindowBase::dispatch_drag_stop()
1714 for(int i = 0; i < subwindows->total && !result; i++)
1716 result = subwindows->values[i]->dispatch_drag_stop();
1719 if(is_dragging && !result)
1729 int BC_WindowBase::dispatch_drag_motion()
1732 for(int i = 0; i < subwindows->total && !result; i++)
1734 result = subwindows->values[i]->dispatch_drag_motion();
1737 if(is_dragging && !result)
1739 drag_motion_event();
1750 int BC_WindowBase::show_tooltip(int w, int h)
1754 if(tooltip_text && !tooltip_on &&
1755 (force_tooltip || get_resources()->tooltips_enabled))
1758 top_level->hide_tooltip();
1762 w = get_text_width(MEDIUMFONT, tooltip_text);
1765 h = get_text_height(MEDIUMFONT, tooltip_text);
1767 w += TOOLTIP_MARGIN * 2;
1768 h += TOOLTIP_MARGIN * 2;
1770 XTranslateCoordinates(top_level->display, win,
1771 top_level->rootwin, get_w(), get_h(),
1773 // keep the tip inside the window/display
1774 int top_x = top_level->get_x();
1775 if( x < top_x ) x = top_x;
1776 int top_w = top_x + top_level->get_w();
1777 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1778 if( top_w < lmt_w ) lmt_w = top_w;
1779 if( x+w > lmt_w ) x = lmt_w-w;
1781 int top_y = top_level->get_y();
1782 if( y < top_y ) y = top_y;
1783 int top_h = top_y + top_level->get_h();
1784 int lmt_h = top_level->get_root_h(0);
1785 if( top_h < lmt_h ) lmt_h = top_h;
1786 if( y+h > lmt_h ) y = lmt_h-h;
1788 int abs_x, abs_y, win_x, win_y;
1789 unsigned int temp_mask;
1791 XQueryPointer(top_level->display, top_level->win,
1792 &temp_win, &temp_win, &abs_x, &abs_y,
1793 &win_x, &win_y, &temp_mask);
1794 // check for cursor inside popup
1795 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1797 if( x-abs_x < y-abs_y )
1802 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1803 get_resources()->tooltip_bg_color);
1806 tooltip_popup->set_font(MEDIUMFONT);
1807 tooltip_popup->flash();
1808 tooltip_popup->flush();
1813 int BC_WindowBase::hide_tooltip()
1816 for(int i = 0; i < subwindows->total; i++)
1818 subwindows->values[i]->hide_tooltip();
1824 delete tooltip_popup;
1830 const char *BC_WindowBase::get_tooltip()
1832 return tooltip_text;
1835 int BC_WindowBase::set_tooltip(const char *text)
1837 tooltip_text = text;
1839 // Update existing tooltip if it is visible
1843 tooltip_popup->flash();
1848 // signal the event handler to repeat
1849 int BC_WindowBase::set_repeat(int64_t duration)
1853 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1856 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1858 #ifdef SINGLE_THREAD
1859 BC_Display::display_global->set_repeat(this, duration);
1861 // test repeater database for duplicates
1862 for(int i = 0; i < repeaters.total; i++)
1865 if(repeaters.values[i]->delay == duration)
1867 repeaters.values[i]->start_repeating(this);
1872 BC_Repeater *repeater = new BC_Repeater(this, duration);
1873 repeater->initialize();
1874 repeaters.append(repeater);
1875 repeater->start_repeating();
1880 int BC_WindowBase::unset_repeat(int64_t duration)
1882 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1884 #ifdef SINGLE_THREAD
1885 BC_Display::display_global->unset_repeat(this, duration);
1887 for(int i = 0; i < repeaters.total; i++)
1889 if(repeaters.values[i]->delay == duration)
1891 repeaters.values[i]->stop_repeating();
1899 int BC_WindowBase::unset_all_repeaters()
1901 #ifdef SINGLE_THREAD
1902 BC_Display::display_global->unset_all_repeaters(this);
1904 for(int i = 0; i < repeaters.total; i++)
1906 repeaters.values[i]->stop_repeating();
1908 repeaters.remove_all_objects();
1913 // long BC_WindowBase::get_repeat_id()
1915 // return top_level->next_repeat_id++;
1919 #ifndef SINGLE_THREAD
1920 int BC_WindowBase::arm_repeat(int64_t duration)
1922 XEvent *event = new XEvent;
1923 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1924 ptr->type = ClientMessage;
1925 ptr->message_type = RepeaterXAtom;
1927 ptr->data.l[0] = duration;
1929 // Couldn't use XSendEvent since it locked up randomly.
1935 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1940 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1942 XEvent *myevent = new XEvent;
1943 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1944 ptr->type = ClientMessage;
1945 ptr->message_type = event->message_type;
1946 ptr->format = event->format;
1947 ptr->data.l[0] = event->data.l[0];
1948 ptr->data.l[1] = event->data.l[1];
1949 ptr->data.l[2] = event->data.l[2];
1950 ptr->data.l[3] = event->data.l[3];
1951 ptr->data.l[4] = event->data.l[4];
1959 Atom BC_WindowBase::create_xatom(const char *atom_name)
1961 return XInternAtom(display, atom_name, False);
1964 int BC_WindowBase::get_atoms()
1966 SetDoneXAtom = create_xatom("BC_REPEAT_EVENT");
1967 RepeaterXAtom = create_xatom("BC_CLOSE_EVENT");
1968 DelWinXAtom = create_xatom("WM_DELETE_WINDOW");
1969 ProtoXAtom = create_xatom("WM_PROTOCOLS");
1970 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1971 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1972 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1973 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
1974 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
1975 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
1981 void BC_WindowBase::init_cursors()
1983 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
1984 cross_cursor = XCreateFontCursor(display, XC_crosshair);
1985 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
1986 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
1987 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
1988 move_cursor = XCreateFontCursor(display, XC_fleur);
1989 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
1990 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
1991 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
1992 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
1993 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
1994 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
1995 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
1996 hourglass_cursor = XCreateFontCursor(display, XC_watch);
1999 char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2000 Colormap colormap = DefaultColormap(display, screen);
2001 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2002 rootwin, cursor_data, 8, 8);
2003 XColor black, dummy;
2004 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2005 transparent_cursor = XCreatePixmapCursor(display,
2006 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2007 // XDefineCursor(display, win, transparent_cursor);
2008 XFreePixmap(display, pixmap_bottom);
2011 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2013 int color_model = BC_TRANSPARENCY;
2017 color_model = BC_RGB8;
2020 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2023 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2026 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2032 int BC_WindowBase::init_colors()
2035 current_color_value = current_color_pixel = 0;
2037 // Get the real depth
2040 ximage = XCreateImage(top_level->display,
2042 top_level->default_depth,
2050 bits_per_pixel = ximage->bits_per_pixel;
2051 XDestroyImage(ximage);
2053 color_model = evaluate_color_model(client_byte_order,
2056 // Get the color model
2062 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2063 create_private_colors();
2067 cmap = DefaultColormap(display, screen);
2068 create_shared_colors();
2071 allocate_color_table();
2075 //cmap = DefaultColormap(display, screen);
2076 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2082 int BC_WindowBase::create_private_colors()
2087 for(int i = 0; i < 255; i++)
2089 color = (i & 0xc0) << 16;
2090 color += (i & 0x38) << 10;
2091 color += (i & 0x7) << 5;
2092 color_table[i][0] = color;
2094 create_shared_colors(); // overwrite the necessary colors on the table
2099 int BC_WindowBase::create_color(int color)
2101 if(total_colors == 256)
2103 // replace the closest match with an exact match
2104 color_table[get_color_rgb8(color)][0] = color;
2108 // add the color to the table
2109 color_table[total_colors][0] = color;
2115 int BC_WindowBase::create_shared_colors()
2117 create_color(BLACK);
2118 create_color(WHITE);
2120 create_color(LTGREY);
2121 create_color(MEGREY);
2122 create_color(MDGREY);
2123 create_color(DKGREY);
2125 create_color(LTCYAN);
2126 create_color(MECYAN);
2127 create_color(MDCYAN);
2128 create_color(DKCYAN);
2130 create_color(LTGREEN);
2131 create_color(GREEN);
2132 create_color(DKGREEN);
2134 create_color(LTPINK);
2138 create_color(LTBLUE);
2140 create_color(DKBLUE);
2142 create_color(LTYELLOW);
2143 create_color(MEYELLOW);
2144 create_color(MDYELLOW);
2145 create_color(DKYELLOW);
2147 create_color(LTPURPLE);
2148 create_color(MEPURPLE);
2149 create_color(MDPURPLE);
2150 create_color(DKPURPLE);
2152 create_color(FGGREY);
2153 create_color(MNBLUE);
2154 create_color(ORANGE);
2155 create_color(FTGREY);
2160 int BC_WindowBase::allocate_color_table()
2162 int red, green, blue, color;
2165 for(int i = 0; i < total_colors; i++)
2167 color = color_table[i][0];
2168 red = (color & 0xFF0000) >> 16;
2169 green = (color & 0x00FF00) >> 8;
2170 blue = color & 0xFF;
2172 col.flags = DoRed | DoGreen | DoBlue;
2173 col.red = red<<8 | red;
2174 col.green = green<<8 | green;
2175 col.blue = blue<<8 | blue;
2177 XAllocColor(display, cmap, &col);
2178 color_table[i][1] = col.pixel;
2181 XInstallColormap(display, cmap);
2185 int BC_WindowBase::init_window_shape()
2187 if(bg_pixmap && bg_pixmap->use_alpha())
2189 XShapeCombineMask(top_level->display,
2190 this->win, ShapeBounding, 0, 0,
2191 bg_pixmap->get_alpha(), ShapeSet);
2197 int BC_WindowBase::init_gc()
2199 unsigned long gcmask;
2200 gcmask = GCFont | GCGraphicsExposures;
2203 gcvalues.font = mediumfont->fid; // set the font
2204 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2205 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2207 // gcmask = GCCapStyle | GCJoinStyle;
2208 // XGetGCValues(display, gc, gcmask, &gcvalues);
2209 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2213 int BC_WindowBase::init_fonts()
2215 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2216 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2217 smallfont = XLoadQueryFont(display, "fixed");
2218 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2219 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2220 mediumfont = XLoadQueryFont(display, "fixed");
2221 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2222 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2223 largefont = XLoadQueryFont(display, "fixed");
2224 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2225 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2226 bigfont = XLoadQueryFont(display, "fixed");
2229 if(get_resources()->use_fontset)
2234 // FIXME: should check the m,d,n values
2235 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2237 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2238 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2239 if( !mediumfontset )
2240 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2241 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2243 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2244 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2246 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2247 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2248 curr_fontset = mediumfontset;
2249 get_resources()->use_fontset = 1;
2253 get_resources()->use_fontset = 0;
2260 void BC_WindowBase::init_xft()
2263 if( !get_resources()->use_xft ) return;
2264 if(!(smallfont_xft =
2265 (resources.small_font_xft[0] == '-' ?
2266 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2267 XftFontOpenName(display, screen, resources.small_font_xft))) )
2268 if(!(smallfont_xft =
2269 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2270 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2271 if(!(mediumfont_xft =
2272 (resources.medium_font_xft[0] == '-' ?
2273 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2274 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2275 if(!(mediumfont_xft =
2276 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2277 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2278 if(!(largefont_xft =
2279 (resources.large_font_xft[0] == '-' ?
2280 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2281 XftFontOpenName(display, screen, resources.large_font_xft))) )
2282 if(!(largefont_xft =
2283 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2284 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2286 (resources.big_font_xft[0] == '-' ?
2287 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2288 XftFontOpenName(display, screen, resources.big_font_xft))) )
2290 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2291 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2293 if(!(bold_smallfont_xft =
2294 (resources.small_b_font_xft[0] == '-' ?
2295 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2296 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2297 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2298 if(!(bold_mediumfont_xft =
2299 (resources.medium_b_font_xft[0] == '-' ?
2300 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2301 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2302 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2303 if(!(bold_largefont_xft =
2304 (resources.large_b_font_xft[0] == '-' ?
2305 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2306 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2307 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2309 // Extension failed to locate fonts
2310 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2311 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2312 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2313 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2314 resources.small_font_xft, smallfont_xft,
2315 resources.medium_font_xft, mediumfont_xft,
2316 resources.large_font_xft, largefont_xft,
2317 resources.big_font_xft, bigfont_xft,
2318 resources.small_b_font_xft, bold_smallfont_xft,
2319 resources.medium_b_font_xft, bold_mediumfont_xft,
2320 resources.large_b_font_xft, bold_largefont_xft);
2321 get_resources()->use_xft = 0;
2327 void BC_WindowBase::init_im()
2329 XIMStyles *xim_styles;
2332 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2334 printf("BC_WindowBase::init_im: Could not open input method.\n");
2337 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2340 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2341 XCloseIM(input_method);
2346 for(int z = 0; z < xim_styles->count_styles; z++)
2348 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2350 xim_style = xim_styles->supported_styles[z];
2358 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2359 XCloseIM(input_method);
2363 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2364 XNClientWindow, win, XNFocusWindow, win, NULL);
2367 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2368 XCloseIM(input_method);
2373 void BC_WindowBase::finit_im()
2375 if( input_context ) {
2376 XDestroyIC(input_context);
2379 if( input_method ) {
2380 XCloseIM(input_method);
2386 int BC_WindowBase::get_color(int64_t color)
2388 // return pixel of color
2389 // use this only for drawing subwindows not for bitmaps
2390 int i, test, difference;
2396 return get_color_rgb8(color);
2397 // test last color looked up
2398 if(current_color_value == color)
2399 return current_color_pixel;
2402 current_color_value = color;
2403 for(i = 0; i < total_colors; i++)
2405 if(color_table[i][0] == color)
2407 current_color_pixel = color_table[i][1];
2408 return current_color_pixel;
2412 // find nearest match
2413 difference = 0xFFFFFF;
2415 for(i = 0; i < total_colors; i++)
2417 test = abs((int)(color_table[i][0] - color));
2419 if(test < difference)
2421 current_color_pixel = color_table[i][1];
2425 return current_color_pixel;
2428 return get_color_rgb16(color);
2431 return get_color_bgr16(color);
2435 return client_byte_order == server_byte_order ?
2436 color : get_color_bgr24(color);
2444 int BC_WindowBase::get_color_rgb8(int color)
2448 pixel = (color & 0xc00000) >> 16;
2449 pixel += (color & 0xe000) >> 10;
2450 pixel += (color & 0xe0) >> 5;
2454 int64_t BC_WindowBase::get_color_rgb16(int color)
2457 result = (color & 0xf80000) >> 8;
2458 result += (color & 0xfc00) >> 5;
2459 result += (color & 0xf8) >> 3;
2464 int64_t BC_WindowBase::get_color_bgr16(int color)
2467 result = (color & 0xf80000) >> 19;
2468 result += (color & 0xfc00) >> 5;
2469 result += (color & 0xf8) << 8;
2474 int64_t BC_WindowBase::get_color_bgr24(int color)
2477 result = (color & 0xff) << 16;
2478 result += (color & 0xff00);
2479 result += (color & 0xff0000) >> 16;
2483 void BC_WindowBase::start_video()
2485 cursor_timer->update();
2487 // set_color(BLACK);
2488 // draw_box(0, 0, get_w(), get_h());
2492 void BC_WindowBase::stop_video()
2500 int64_t BC_WindowBase::get_color()
2502 return top_level->current_color;
2505 void BC_WindowBase::set_color(int64_t color)
2507 top_level->current_color = color;
2508 XSetForeground(top_level->display,
2510 top_level->get_color(color));
2513 void BC_WindowBase::set_opaque()
2515 XSetFunction(top_level->display, top_level->gc, GXcopy);
2518 void BC_WindowBase::set_inverse()
2520 XSetFunction(top_level->display, top_level->gc, GXxor);
2523 void BC_WindowBase::set_line_width(int value)
2525 this->line_width = value;
2526 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2527 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2528 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2529 JoinMiter); /* join_style */
2531 if(line_dashes > 0) {
2532 const char dashes = line_dashes;
2533 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2536 // XGCValues gcvalues;
2537 // unsigned long gcmask;
2538 // gcmask = GCCapStyle | GCJoinStyle;
2539 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2540 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2543 void BC_WindowBase::set_line_dashes(int value)
2545 line_dashes = value;
2546 // call XSetLineAttributes
2547 set_line_width(line_width);
2551 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2555 case ARROW_CURSOR: return top_level->arrow_cursor; break;
2556 case CROSS_CURSOR: return top_level->cross_cursor;
2557 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
2558 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
2559 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
2560 case MOVE_CURSOR: return top_level->move_cursor; break;
2561 case LEFT_CURSOR: return top_level->left_cursor; break;
2562 case RIGHT_CURSOR: return top_level->right_cursor; break;
2563 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
2564 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
2565 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
2566 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
2567 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
2568 case HOURGLASS_CURSOR: return top_level->hourglass_cursor; break;
2569 case TRANSPARENT_CURSOR: return top_level->transparent_cursor; break;
2574 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2576 // inherit cursor from parent
2579 XUndefineCursor(top_level->display, win);
2580 current_cursor = cursor;
2583 // don't change cursor if overridden
2584 if((!top_level->is_hourglass && !is_transparent) ||
2587 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2588 if(flush) this->flush();
2591 if(!override) current_cursor = cursor;
2594 void BC_WindowBase::set_x_cursor(int cursor)
2596 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2597 XDefineCursor(top_level->display, win, temp_cursor);
2598 current_cursor = cursor;
2602 int BC_WindowBase::get_cursor()
2604 return current_cursor;
2607 void BC_WindowBase::start_hourglass()
2609 top_level->start_hourglass_recursive();
2613 void BC_WindowBase::stop_hourglass()
2615 top_level->stop_hourglass_recursive();
2619 void BC_WindowBase::start_hourglass_recursive()
2621 if(this == top_level)
2629 set_cursor(HOURGLASS_CURSOR, 1, 0);
2630 for(int i = 0; i < subwindows->total; i++)
2632 subwindows->values[i]->start_hourglass_recursive();
2637 void BC_WindowBase::stop_hourglass_recursive()
2639 if(this == top_level)
2641 if(hourglass_total == 0) return;
2642 top_level->hourglass_total--;
2645 if(!top_level->hourglass_total)
2647 top_level->is_hourglass = 0;
2649 // Cause set_cursor to perform change
2651 set_cursor(current_cursor, 1, 0);
2653 for(int i = 0; i < subwindows->total; i++)
2655 subwindows->values[i]->stop_hourglass_recursive();
2663 XFontStruct* BC_WindowBase::get_font_struct(int font)
2665 // Clear out unrelated flags
2666 if(font & BOLDFACE) font ^= BOLDFACE;
2669 case SMALLFONT: return top_level->smallfont; break;
2670 case MEDIUMFONT: return top_level->mediumfont; break;
2671 case LARGEFONT: return top_level->largefont; break;
2672 case BIGFONT: return top_level->bigfont; break;
2677 XFontSet BC_WindowBase::get_fontset(int font)
2681 if(get_resources()->use_fontset)
2683 switch(font & 0xff) {
2684 case SMALLFONT: fs = top_level->smallfontset; break;
2685 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2686 case LARGEFONT: fs = top_level->largefontset; break;
2687 case BIGFONT: fs = top_level->bigfontset; break;
2695 XftFont* BC_WindowBase::get_xft_struct(int font)
2698 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2699 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2700 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2701 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2702 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2703 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2704 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2719 int BC_WindowBase::get_current_font()
2721 return top_level->current_font;
2724 void BC_WindowBase::set_font(int font)
2726 top_level->current_font = font;
2729 if(get_resources()->use_xft) {}
2732 if(get_resources()->use_fontset) {
2736 if(get_font_struct(font))
2738 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2744 void BC_WindowBase::set_fontset(int font)
2748 if(get_resources()->use_fontset) {
2750 case SMALLFONT: fs = top_level->smallfontset; break;
2751 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2752 case LARGEFONT: fs = top_level->largefontset; break;
2753 case BIGFONT: fs = top_level->bigfontset; break;
2761 XFontSet BC_WindowBase::get_curr_fontset(void)
2763 if(get_resources()->use_fontset)
2764 return curr_fontset;
2768 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2771 if(get_resources()->use_xft && get_xft_struct(font))
2774 #ifdef X_HAVE_UTF8_STRING
2775 if(get_resources()->locale_utf8)
2777 XftTextExtentsUtf8(top_level->display,
2778 get_xft_struct(font),
2779 (const XftChar8 *)text,
2786 XftTextExtents8(top_level->display,
2787 get_xft_struct(font),
2788 (const XftChar8 *)text,
2792 return extents.xOff;
2796 if(get_resources()->use_fontset && top_level->get_fontset(font))
2797 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2799 if(get_font_struct(font))
2800 return XTextWidth(get_font_struct(font), text, length);
2806 case MEDIUM_7SEGMENT:
2807 return get_resources()->medium_7segment[0]->get_w() * length;
2817 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2820 if(get_resources()->use_xft && get_xft_struct(font)) {
2823 XftTextExtents32(top_level->display, get_xft_struct(font),
2824 (const FcChar32*)text, length, &extents);
2825 return extents.xOff;
2828 if(!get_font_struct(font)) return 0;
2829 XChar2b xtext[length], *xp = xtext;
2830 for( int i=0; i<length; ++i,++xp ) {
2831 xp->byte1 = (unsigned char) (text[i] >> 8);
2832 xp->byte2 = (unsigned char) (text[i] & 0xff);
2834 return XTextWidth16(get_font_struct(font), xtext, length);
2837 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2839 int i, j, w = 0, line_w = 0;
2840 if(length < 0) length = strlen(text);
2842 for(i = 0, j = 0; i <= length; i++)
2847 line_w = get_single_text_width(font, &text[j], i - j);
2853 line_w = get_single_text_width(font, &text[j], length - j);
2855 if(line_w > w) w = line_w;
2858 if(i > length && w == 0)
2860 w = get_single_text_width(font, text, length);
2866 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2868 int i, j, w = 0, line_w = 0;
2870 if(length < 0) length = wcslen(text);
2872 for(i = 0, j = 0; i <= length; i++)
2877 line_w = get_single_text_width(font, &text[j], i - j);
2882 line_w = get_single_text_width(font, &text[j], length - j);
2884 if(line_w > w) w = line_w;
2887 if(i > length && w == 0)
2888 w = get_single_text_width(font, text, length);
2893 int BC_WindowBase::get_text_ascent(int font)
2897 if( (fstruct = get_xft_struct(font)) != 0 )
2898 return fstruct->ascent;
2900 if(get_resources()->use_fontset && top_level->get_fontset(font))
2902 XFontSetExtents *extents;
2904 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2905 return -extents->max_logical_extent.y;
2908 if(get_font_struct(font))
2909 return top_level->get_font_struct(font)->ascent;
2912 case MEDIUM_7SEGMENT:
2913 return get_resources()->medium_7segment[0]->get_h();
2918 int BC_WindowBase::get_text_descent(int font)
2922 if( (fstruct = get_xft_struct(font)) != 0 )
2923 return fstruct->descent;
2925 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2926 XFontSetExtents *extents;
2927 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2928 return (extents->max_logical_extent.height
2929 + extents->max_logical_extent.y);
2932 if(get_font_struct(font))
2933 return top_level->get_font_struct(font)->descent;
2938 int BC_WindowBase::get_text_height(int font, const char *text)
2943 if( (fstruct = get_xft_struct(font)) != 0 )
2944 rowh = fstruct->height;
2947 rowh = get_text_ascent(font) + get_text_descent(font);
2949 if(!text) return rowh;
2951 // Add height of lines
2952 int h = 0, i, length = strlen(text);
2953 for(i = 0; i <= length; i++)
2964 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2966 if(color_model < 0) color_model = top_level->get_color_model();
2967 return new BC_Bitmap(top_level, w, h, color_model);
2970 void BC_WindowBase::init_wait()
2972 #ifndef SINGLE_THREAD
2973 if(window_type != MAIN_WINDOW)
2974 top_level->init_wait();
2975 init_lock->lock("BC_WindowBase::init_wait");
2976 init_lock->unlock();
2980 int BC_WindowBase::accel_available(int color_model, int lock_it)
2982 if(window_type != MAIN_WINDOW)
2983 return top_level->accel_available(color_model, lock_it);
2987 if(lock_it) lock_window("BC_WindowBase::accel_available");
2991 result = grab_port_id(this, color_model);
2994 xvideo_port_id = result;
3006 //printf("BC_WindowBase::accel_available 1\n");
3007 result = grab_port_id(this, color_model);
3008 //printf("BC_WindowBase::accel_available 2 %d\n", result);
3011 xvideo_port_id = result;
3016 //printf("BC_WindowBase::accel_available 3 %d\n", xvideo_port_id);
3024 if(lock_it) unlock_window();
3025 //printf("BC_WindowBase::accel_available %d %d\n", color_model, result);
3030 int BC_WindowBase::grab_port_id(BC_WindowBase *window, int color_model)
3032 int numFormats, i, j, k;
3033 unsigned int ver, rev, numAdapt, reqBase, eventBase, errorBase;
3034 XvAdaptorInfo *info;
3035 XvImageFormatValues *formats;
3038 if(!get_resources()->use_xvideo) return -1;
3040 // Translate from color_model to X color model
3041 x_color_model = BC_CModels::bc_to_x(color_model);
3043 // Only local server is fast enough.
3044 if(!resources.use_shm) return -1;
3046 // XV extension is available
3047 if(Success != XvQueryExtension(window->display, &ver, &rev,
3048 &reqBase, &eventBase, &errorBase)) {
3052 // XV adaptors are available
3053 XvQueryAdaptors(window->display,
3054 DefaultRootWindow(window->display),
3061 // Get adaptor with desired color model
3062 for(i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3063 /* adaptor supports XvImages */
3064 if(info[i].type & XvImageMask)
3066 formats = XvListImageFormats(window->display,
3069 // for(j = 0; j < numFormats; j++)
3070 // printf("%08x\n", formats[j].id);
3072 int numPorts = info[i].num_ports;
3073 for(j = 0; j < (int)numFormats && xvideo_port_id < 0; j++)
3075 /* this adaptor supports the desired format */
3076 if(formats[j].id == x_color_model)
3078 /* Try to grab a port */
3079 for(k = 0; k < numPorts; k++)
3082 if(Success == XvGrabPort(top_level->display,
3083 info[i].base_id + k,
3086 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3087 xvideo_port_id = info[i].base_id + k;
3093 if(formats) XFree(formats);
3097 XvFreeAdaptorInfo(info);
3099 return xvideo_port_id;
3103 int BC_WindowBase::show_window(int flush)
3105 for(int i = 0; i < subwindows->size(); i++)
3107 subwindows->get(i)->show_window(0);
3110 XMapWindow(top_level->display, win);
3111 if(flush) XFlush(top_level->display);
3112 // XSync(top_level->display, 0);
3117 int BC_WindowBase::hide_window(int flush)
3119 for(int i = 0; i < subwindows->size(); i++)
3121 subwindows->get(i)->hide_window(0);
3124 XUnmapWindow(top_level->display, win);
3125 if(flush) this->flush();
3130 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3132 subwindows->append((BC_SubWindow*)menu_bar);
3134 menu_bar->parent_window = this;
3135 menu_bar->top_level = this->top_level;
3136 menu_bar->initialize();
3140 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3142 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3143 if(this != top_level) return top_level->add_popup(window);
3144 popups.append(window);
3148 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3150 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3151 if(this != top_level)
3152 top_level->remove_popup(window);
3154 popups.remove(window);
3155 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3159 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3161 subwindows->append(subwindow);
3163 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3165 // parent window must be set before the subwindow initialization
3166 subwindow->parent_window = this;
3167 subwindow->top_level = this->top_level;
3169 // Execute derived initialization
3170 subwindow->initialize();
3175 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3177 return add_subwindow(subwindow);
3180 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3182 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3184 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3187 XClearArea(top_level->display, win, x, y, w, h, 0);
3191 XClearWindow(top_level->display, win);
3199 int BC_WindowBase::flash(int flush)
3201 flash(-1, -1, -1, -1, flush);
3205 void BC_WindowBase::flush()
3207 //if(!get_window_lock())
3208 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3209 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3210 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3211 int locked = get_window_lock();
3212 if( !locked ) lock_window("BC_WindowBase::flush");
3213 XFlush(top_level->display);
3214 if( !locked ) unlock_window();
3217 void BC_WindowBase::sync_display()
3219 int locked = get_window_lock();
3220 if( !locked ) lock_window("BC_WindowBase::sync_display");
3221 XSync(top_level->display, False);
3222 if( !locked ) unlock_window();
3225 int BC_WindowBase::get_window_lock()
3227 #ifdef SINGLE_THREAD
3228 return BC_Display::display_global->get_display_locked();
3230 return top_level->window_lock;
3234 int BC_WindowBase::lock_window(const char *location)
3236 if(top_level && top_level != this)
3238 top_level->lock_window(location);
3243 SET_LOCK(this, title, location);
3244 #ifdef SINGLE_THREAD
3245 BC_Display::lock_display(location);
3247 XLockDisplay(top_level->display);
3248 top_level->display_lock_owner = pthread_self();
3251 ++top_level->window_lock;
3255 printf("BC_WindowBase::lock_window top_level NULL\n");
3260 int BC_WindowBase::unlock_window()
3262 if(top_level && top_level != this)
3264 top_level->unlock_window();
3271 // if(!top_level->window_lock)
3273 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3277 if( top_level->window_lock > 0 )
3278 if( --top_level->window_lock == 0 )
3279 top_level->display_lock_owner = 0;
3280 #ifdef SINGLE_THREAD
3281 BC_Display::unlock_display();
3283 XUnlockDisplay(top_level->display);
3288 printf("BC_WindowBase::unlock_window top_level NULL\n");
3293 void BC_WindowBase::set_done(int return_value)
3295 if(done_set) return;
3297 if(window_type != MAIN_WINDOW)
3298 top_level->set_done(return_value);
3301 #ifdef SINGLE_THREAD
3302 this->return_value = return_value;
3303 BC_Display::display_global->arm_completion(this);
3304 completion_lock->unlock();
3305 #else // SINGLE_THREAD
3307 if( !event_thread ) return;
3308 XEvent *event = new XEvent;
3309 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3310 event->type = ClientMessage;
3311 ptr->message_type = SetDoneXAtom;
3313 this->return_value = return_value;
3314 // May lock up here because XSendEvent doesn't work too well
3315 // asynchronous with XNextEvent.
3316 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3318 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3324 void BC_WindowBase::close(int return_value)
3326 hide_window(); flush();
3327 set_done(return_value);
3330 int BC_WindowBase::get_w()
3335 int BC_WindowBase::get_h()
3340 int BC_WindowBase::get_x()
3345 int BC_WindowBase::get_y()
3351 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3352 // this is the bounding box of all the screens
3354 int BC_WindowBase::get_root_w(int lock_display)
3356 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3357 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3358 int result = WidthOfScreen(def_screen);
3359 if(lock_display) unlock_window();
3363 int BC_WindowBase::get_root_h(int lock_display)
3365 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3366 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3367 int result = HeightOfScreen(def_screen);
3368 if(lock_display) unlock_window();
3372 XineramaScreenInfo *
3373 BC_WindowBase::get_xinerama_info(int screen)
3375 if( !xinerama_info || !xinerama_screens ) return 0;
3377 for( int i=0; i<xinerama_screens; ++i )
3378 if( xinerama_info[i].screen_number == screen )
3379 return &xinerama_info[i];
3382 int top_x = get_x(), top_y = get_y();
3383 for( int i=0; i<xinerama_screens; ++i ) {
3384 int scr_y = top_y - xinerama_info[i].y_org;
3385 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3386 int scr_x = top_x - xinerama_info[i].x_org;
3387 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3388 return &xinerama_info[i];
3393 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3396 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3397 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3400 int root_w = get_root_w(0);
3401 int root_h = get_root_h(0);
3402 // Shift X based on position of current window if dual head
3403 if( (float)root_w/root_h > 1.8 ) {
3404 root_w = get_screen_w(0, 0);
3405 if( top_level->get_x() >= root_w )
3410 result = info->x_org;
3411 if(lock_display) unlock_window();
3415 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3417 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3418 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3419 int result = !info ? 0 : info->y_org;
3420 if(lock_display) unlock_window();
3424 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3427 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3428 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3430 int width = get_root_w(0);
3431 int height = get_root_h(0);
3432 if( (float)width/height > 1.8 ) {
3433 // If dual head, the screen width is > 16x9
3434 // but we only want to fill one screen
3435 // this code assumes the "big" screen is on the right
3436 int scr_w0 = width / 2;
3438 case 600: scr_w0 = 800; break;
3439 case 720: scr_w0 = 1280; break;
3440 case 1024: scr_w0 = 1280; break;
3441 case 1200: scr_w0 = 1600; break;
3442 case 1080: scr_w0 = 1920; break;
3444 int scr_w1 = width - scr_w0;
3445 result = screen > 0 ? scr_w1 :
3446 screen == 0 ? scr_w0 :
3447 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3453 result = info->width;
3454 if(lock_display) unlock_window();
3458 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3460 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3461 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3462 int result = info ? info->height : get_root_h(0);
3463 if(lock_display) unlock_window();
3467 // Bottom right corner
3468 int BC_WindowBase::get_x2()
3473 int BC_WindowBase::get_y2()
3478 int BC_WindowBase::get_video_on()
3483 int BC_WindowBase::get_hidden()
3485 return top_level->hidden;
3488 int BC_WindowBase::cursor_inside()
3490 return (top_level->cursor_x >= 0 &&
3491 top_level->cursor_y >= 0 &&
3492 top_level->cursor_x < w &&
3493 top_level->cursor_y < h);
3496 BC_WindowBase* BC_WindowBase::get_top_level()
3501 BC_WindowBase* BC_WindowBase::get_parent()
3503 return parent_window;
3506 int BC_WindowBase::get_color_model()
3508 return top_level->color_model;
3511 BC_Resources* BC_WindowBase::get_resources()
3513 return &BC_WindowBase::resources;
3516 BC_Synchronous* BC_WindowBase::get_synchronous()
3518 return BC_WindowBase::resources.get_synchronous();
3521 int BC_WindowBase::get_bg_color()
3526 void BC_WindowBase::set_bg_color(int color)
3528 this->bg_color = color;
3531 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3536 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3538 top_level->active_subwindow = subwindow;
3541 int BC_WindowBase::activate()
3546 int BC_WindowBase::deactivate()
3548 if(window_type == MAIN_WINDOW)
3550 if(top_level->active_menubar) top_level->active_menubar->deactivate();
3551 if(top_level->active_popup_menu) top_level->active_popup_menu->deactivate();
3552 if(top_level->active_subwindow) top_level->active_subwindow->deactivate();
3554 top_level->active_menubar = 0;
3555 top_level->active_popup_menu = 0;
3556 top_level->active_subwindow = 0;
3561 int BC_WindowBase::cycle_textboxes(int amount)
3564 BC_WindowBase *new_textbox = 0;
3568 BC_WindowBase *first_textbox = 0;
3569 find_next_textbox(&first_textbox, &new_textbox, result);
3570 if(!new_textbox) new_textbox = first_textbox;
3576 BC_WindowBase *last_textbox = 0;
3577 find_prev_textbox(&last_textbox, &new_textbox, result);
3578 if(!new_textbox) new_textbox = last_textbox;
3582 if(new_textbox != active_subwindow)
3585 new_textbox->activate();
3591 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3593 // Search subwindows for textbox
3594 for(int i = 0; i < subwindows->total && result < 2; i++)
3596 BC_WindowBase *test_subwindow = subwindows->values[i];
3597 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3604 if(!*first_textbox) *first_textbox = this;
3608 if(top_level->active_subwindow == this)
3614 *next_textbox = this;
3621 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3627 if(!*last_textbox) *last_textbox = this;
3631 if(top_level->active_subwindow == this)
3637 *prev_textbox = this;
3642 // Search subwindows for textbox
3643 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3645 BC_WindowBase *test_subwindow = subwindows->values[i];
3646 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3651 BC_Clipboard* BC_WindowBase::get_clipboard()
3653 #ifdef SINGLE_THREAD
3654 return BC_Display::display_global->clipboard;
3656 return top_level->clipboard;
3660 int BC_WindowBase::get_relative_cursor_x()
3662 int abs_x, abs_y, x, y, win_x, win_y;
3663 unsigned int temp_mask;
3666 XQueryPointer(top_level->display, top_level->win,
3667 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3670 XTranslateCoordinates(top_level->display, top_level->rootwin,
3671 win, abs_x, abs_y, &x, &y, &temp_win);
3676 int BC_WindowBase::get_relative_cursor_y()
3678 int abs_x, abs_y, x, y, win_x, win_y;
3679 unsigned int temp_mask;
3682 XQueryPointer(top_level->display, top_level->win,
3683 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3686 XTranslateCoordinates(top_level->display,
3687 top_level->rootwin, win, abs_x, abs_y, &x, &y, &temp_win);
3692 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3694 int abs_x, abs_y, win_x, win_y;
3695 unsigned int temp_mask;
3698 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
3699 XQueryPointer(top_level->display, top_level->win,
3700 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3702 if(lock_window) this->unlock_window();
3706 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3708 int abs_x, abs_y, win_x, win_y;
3709 unsigned int temp_mask;
3712 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
3713 XQueryPointer(top_level->display, top_level->win,
3714 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3716 if(lock_window) this->unlock_window();
3720 int BC_WindowBase::match_window(Window win)
3722 if (this->win == win) return 1;
3724 for(int i = 0; i < subwindows->total; i++) {
3725 result = subwindows->values[i]->match_window(win);
3726 if (result) return result;
3732 int BC_WindowBase::get_cursor_over_window()
3734 if(top_level != this) return top_level->get_cursor_over_window();
3736 int abs_x, abs_y, win_x, win_y;
3737 unsigned int temp_mask;
3738 Window root_return, child_return;
3740 int ret = XQueryPointer(display, win,
3741 &root_return, &child_return, &abs_x, &abs_y,
3742 &win_x, &win_y, &temp_mask);
3743 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3744 // __LINE__, title, win, ret, child_return);
3746 int result = !ret || child_return == None ? 0 :
3747 match_window(child_return);
3751 int BC_WindowBase::cursor_above()
3753 int rx = get_relative_cursor_x();
3754 if( rx < 0 || rx >= get_w() ) return 0;
3755 int ry = get_relative_cursor_y();
3756 if( ry < 0 || ry >= get_h() ) return 0;
3760 int BC_WindowBase::get_drag_x()
3762 return top_level->drag_x;
3765 int BC_WindowBase::get_drag_y()
3767 return top_level->drag_y;
3770 int BC_WindowBase::get_cursor_x()
3772 return top_level->cursor_x;
3775 int BC_WindowBase::get_cursor_y()
3777 return top_level->cursor_y;
3780 int BC_WindowBase::dump_windows()
3782 printf("\tBC_WindowBase::dump_windows window=%p win=%p\n",
3783 this, (void*)this->win);
3784 for(int i = 0; i < subwindows->size(); i++)
3785 subwindows->get(i)->dump_windows();
3786 for(int i = 0; i < popups.size(); i++)
3787 printf("\tBC_WindowBase::dump_windows popup=%p win=%p\n",
3788 popups.get(i), (void*)popups.get(i)->win);
3792 int BC_WindowBase::is_event_win()
3794 return this->win == top_level->event_win;
3797 void BC_WindowBase::set_dragging(int value)
3799 is_dragging = value;
3802 int BC_WindowBase::get_dragging()
3807 int BC_WindowBase::get_buttonpress()
3809 return top_level->button_number;
3812 int BC_WindowBase::get_button_down()
3814 return top_level->button_down;
3817 int BC_WindowBase::alt_down()
3819 return top_level->alt_mask;
3822 int BC_WindowBase::shift_down()
3824 return top_level->shift_mask;
3827 int BC_WindowBase::ctrl_down()
3829 return top_level->ctrl_mask;
3832 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3835 *length = top_level->wkey_string_length;
3836 return top_level->wkey_string;
3839 #ifdef X_HAVE_UTF8_STRING
3840 char* BC_WindowBase::get_keypress_utf8()
3842 return top_level->key_pressed_utf8;
3847 int BC_WindowBase::get_keypress()
3849 return top_level->key_pressed;
3852 int BC_WindowBase::get_double_click()
3854 return top_level->double_click;
3857 int BC_WindowBase::get_triple_click()
3859 return top_level->triple_click;
3862 int BC_WindowBase::get_bgcolor()
3867 int BC_WindowBase::resize_window(int w, int h)
3869 if(this->w == w && this->h == h) return 0;
3871 if(window_type == MAIN_WINDOW && !allow_resize)
3873 XSizeHints size_hints;
3874 size_hints.flags = PSize | PMinSize | PMaxSize;
3875 size_hints.width = w;
3876 size_hints.height = h;
3877 size_hints.min_width = w;
3878 size_hints.max_width = w;
3879 size_hints.min_height = h;
3880 size_hints.max_height = h;
3881 XSetNormalHints(top_level->display, win, &size_hints);
3883 XResizeWindow(top_level->display, win, w, h);
3888 pixmap = new BC_Pixmap(this, w, h);
3890 // Propagate to menubar
3891 for(int i = 0; i < subwindows->total; i++)
3893 subwindows->values[i]->dispatch_resize_event(w, h);
3896 draw_background(0, 0, w, h);
3897 if(top_level == this && get_resources()->recursive_resizing)
3898 resize_history.append(new BC_ResizeCall(w, h));
3902 // The only way for resize events to be propagated is by updating the internal w and h
3903 int BC_WindowBase::resize_event(int w, int h)
3905 if(window_type == MAIN_WINDOW)
3913 int BC_WindowBase::reposition_window(int x, int y)
3915 reposition_window(x, y, -1, -1);
3920 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
3924 // Some tools set their own dimensions before calling this, causing the
3925 // resize check to skip.
3929 if(w > 0 && w != this->w)
3935 if(h > 0 && h != this->h)
3941 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
3944 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
3946 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
3948 if(translation_count && window_type == MAIN_WINDOW)
3950 // KDE shifts window right and down.
3951 // FVWM leaves window alone and adds border around it.
3952 XMoveResizeWindow(top_level->display,
3954 x - BC_DisplayInfo::auto_reposition_x,
3955 y - BC_DisplayInfo::auto_reposition_y,
3961 XMoveResizeWindow(top_level->display,
3972 pixmap = new BC_Pixmap(this, this->w, this->h);
3973 // Propagate to menubar
3974 for(int i = 0; i < subwindows->total; i++)
3976 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
3979 // draw_background(0, 0, w, h);
3985 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
3987 return reposition_window(get_x()+dx, get_y()+dy, w, h);
3990 int BC_WindowBase::reposition_window_relative(int dx, int dy)
3992 return reposition_window_relative(dx, dy, -1, -1);
3995 void BC_WindowBase::set_tooltips(int v)
3997 get_resources()->tooltips_enabled = v;
4000 void BC_WindowBase::set_force_tooltip(int v)
4005 int BC_WindowBase::raise_window(int do_flush)
4007 XRaiseWindow(top_level->display, win);
4008 if(do_flush) XFlush(top_level->display);
4012 int BC_WindowBase::lower_window(int do_flush)
4014 XLowerWindow(top_level->display, win);
4015 if(do_flush) XFlush(top_level->display);
4019 void BC_WindowBase::set_background(VFrame *bitmap)
4021 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4023 bg_pixmap = new BC_Pixmap(this,
4026 shared_bg_pixmap = 0;
4027 draw_background(0, 0, w, h);
4030 void BC_WindowBase::put_title(const char *text)
4032 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4033 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4034 *cp++ = *bp >= ' ' ? *bp : ' ';
4038 void BC_WindowBase::set_title(const char *text)
4043 txlist[0] = this->title;
4046 XTextProperty titleprop;
4047 XmbTextListToTextProperty(top_level->display, txlist, 1,
4048 XStdICCTextStyle, &titleprop);
4049 XSetWMName(top_level->display, top_level->win, &titleprop);
4050 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4051 XFree(titleprop.value);
4056 void BC_WindowBase::set_utf8title(const char *text)
4058 XTextProperty titleprop;
4061 strcpy(this->title, text);
4062 txlist[0] = this->title;
4065 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4066 XUTF8StringStyle, &titleprop);
4067 XSetWMName(top_level->display, top_level->win, &titleprop);
4068 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4069 XFree(titleprop.value);
4074 const char *BC_WindowBase::get_title()
4079 int BC_WindowBase::get_toggle_value()
4081 return toggle_value;
4084 int BC_WindowBase::get_toggle_drag()
4089 int BC_WindowBase::set_icon(VFrame *data)
4091 if(icon_pixmap) delete icon_pixmap;
4092 icon_pixmap = new BC_Pixmap(top_level,
4097 if(icon_window) delete icon_window;
4098 icon_window = new BC_Popup(this,
4101 icon_pixmap->get_w(),
4102 icon_pixmap->get_h(),
4104 1, // All windows are hidden initially
4108 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4109 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4110 wm_hints.icon_mask = icon_pixmap->get_alpha();
4111 wm_hints.icon_window = icon_window->win;
4112 wm_hints.window_group = XGroupLeader;
4114 // for(int i = 0; i < 1000; i++)
4115 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4118 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4119 XSync(top_level->display, 0);
4123 int BC_WindowBase::set_w(int w)
4129 int BC_WindowBase::set_h(int h)
4135 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4137 BC_Resources *resources = get_resources();
4138 char string[BCTEXTLEN];
4139 int newest_id = - 1;
4140 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4142 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4143 resources->filebox_history[i].path[0] = 0;
4144 defaults->get(string, resources->filebox_history[i].path);
4145 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4146 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4147 if(resources->filebox_history[i].id > newest_id)
4148 newest_id = resources->filebox_history[i].id;
4151 resources->filebox_id = newest_id + 1;
4152 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4153 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4154 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4155 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4156 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4157 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4158 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4159 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4160 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4161 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4162 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4163 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4167 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4169 BC_Resources *resources = get_resources();
4170 char string[BCTEXTLEN];
4171 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4173 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4174 defaults->update(string, resources->filebox_history[i].path);
4175 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4176 defaults->update(string, resources->filebox_history[i].id);
4178 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4179 defaults->update("FILEBOX_W", resources->filebox_w);
4180 defaults->update("FILEBOX_H", resources->filebox_h);
4181 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4182 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4183 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4184 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4185 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4186 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4187 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4188 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4189 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4195 // For some reason XTranslateCoordinates can take a long time to return.
4196 // We work around this by only calling it when the event windows are different.
4197 void BC_WindowBase::translate_coordinates(Window src_w,
4209 *dest_x_return = src_x;
4210 *dest_y_return = src_y;
4214 XTranslateCoordinates(top_level->display,
4222 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4226 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4228 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4231 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4233 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4241 #ifdef HAVE_LIBXXF86VM
4242 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4246 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4248 XF86VidModeModeInfo **vm_modelines;
4249 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4250 for (i = 0; i < vm_count; i++) {
4251 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4254 display = top_level->display;
4255 if (vm_modelines[*vm]->hdisplay == *width)
4259 *width = vm_modelines[*vm]->hdisplay;
4260 *height = vm_modelines[*vm]->vdisplay;
4265 void BC_WindowBase::scale_vm(int vm)
4267 int foo,bar,dotclock;
4268 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4271 XF86VidModeModeInfo **vm_modelines;
4272 XF86VidModeModeLine vml;
4273 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4274 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4275 orig_modeline.dotclock = dotclock;
4276 orig_modeline.hdisplay = vml.hdisplay;
4277 orig_modeline.hsyncstart = vml.hsyncstart;
4278 orig_modeline.hsyncend = vml.hsyncend;
4279 orig_modeline.htotal = vml.htotal;
4280 orig_modeline.vdisplay = vml.vdisplay;
4281 orig_modeline.vsyncstart = vml.vsyncstart;
4282 orig_modeline.vsyncend = vml.vsyncend;
4283 orig_modeline.vtotal = vml.vtotal;
4284 orig_modeline.flags = vml.flags;
4285 orig_modeline.privsize = vml.privsize;
4286 // orig_modeline.private = vml.private;
4287 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4288 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4289 XFlush(top_level->display);
4293 void BC_WindowBase::restore_vm()
4295 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4296 XFlush(top_level->display);
4319 #ifndef SINGLE_THREAD
4320 int BC_WindowBase::get_event_count()
4322 event_lock->lock("BC_WindowBase::get_event_count");
4323 int result = common_events.total;
4324 event_lock->unlock();
4328 XEvent* BC_WindowBase::get_event()
4331 while(!done && !result)
4333 event_condition->lock("BC_WindowBase::get_event");
4334 event_lock->lock("BC_WindowBase::get_event");
4336 if(common_events.total && !done)
4338 result = common_events.values[0];
4339 common_events.remove_number(0);
4342 event_lock->unlock();
4347 void BC_WindowBase::put_event(XEvent *event)
4349 event_lock->lock("BC_WindowBase::put_event");
4350 common_events.append(event);
4351 event_lock->unlock();
4352 event_condition->unlock();
4355 #endif // SINGLE_THREAD
4357 int BC_WindowBase::get_id()
4363 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4365 int w = vframe->get_w(), h = vframe->get_h();
4366 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4367 icon->draw_vframe(vframe, 0,0, w,h, 0,0);