4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcclipboard.h"
24 #include "bcdisplay.h"
25 #include "bcdisplayinfo.h"
26 #include "bcmenubar.h"
29 #include "bcpopupmenu.h"
30 #include "bcrepeater.h"
31 #include "bcresources.h"
32 #include "bcsignals.h"
33 #include "bcsubwindow.h"
34 #include "bcsynchronous.h"
36 #include "bcwindowbase.h"
37 #include "bcwindowevents.h"
38 #include "bccmodels.h"
40 #include "condition.h"
57 #include <X11/extensions/Xinerama.h>
58 #include <X11/extensions/Xvlib.h>
59 #include <X11/extensions/shape.h>
60 #include <X11/XF86keysym.h>
61 #include <X11/Sunkeysym.h>
64 BC_ResizeCall::BC_ResizeCall(int w, int h)
76 int BC_WindowBase::shm_completion_event = -1;
80 BC_Resources BC_WindowBase::resources;
82 Window XGroupLeader = 0;
84 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
85 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
87 BC_WindowBase::BC_WindowBase(int opts)
90 //printf("BC_WindowBase::BC_WindowBase 1\n");
91 BC_WindowBase::initialize();
94 BC_WindowBase::~BC_WindowBase()
97 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
99 if(window_type == MAIN_WINDOW)
100 lock_window("BC_WindowBase::~BC_WindowBase");
103 #ifdef HAVE_LIBXXF86VM
104 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
111 if(window_type != MAIN_WINDOW)
114 XSelectInput(top_level->display, this->win, 0);
115 motion_events = resize_events = translation_events = 0;
116 top_level->dequeue_events(win);
118 if(top_level->active_menubar == this) top_level->active_menubar = 0;
119 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
120 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
121 // Remove pointer from parent window to this
122 parent_window->subwindows->remove(this);
125 if(icon_window) delete icon_window;
126 if(window_type == POPUP_WINDOW)
127 parent_window->remove_popup(this);
129 // Delete the subwindows
132 while(subwindows->total)
134 // Subwindow removes its own pointer
135 delete subwindows->values[0];
143 if( get_resources()->get_synchronous() &&
144 (top_level->options & GLX_WINDOW) && glx_win != 0 )
145 get_resources()->get_synchronous()->delete_window(this);
148 XDestroyWindow(top_level->display, win);
150 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
151 if(icon_pixmap) delete icon_pixmap;
152 if(temp_bitmap) delete temp_bitmap;
153 top_level->active_bitmaps.remove_buffers(this);
154 if(_7segment_pixmaps)
156 for(int i = 0; i < TOTAL_7SEGMENT; i++)
157 delete _7segment_pixmaps[i];
159 delete [] _7segment_pixmaps;
164 if(window_type == MAIN_WINDOW)
166 XFreeGC(display, gc);
167 static XFontStruct *BC_WindowBase::*xfont[] = {
168 &BC_WindowBase::smallfont,
169 &BC_WindowBase::mediumfont,
170 &BC_WindowBase::largefont,
171 &BC_WindowBase::bigfont,
173 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
174 XFreeFont(display, this->*xfont[i]);
176 // bug in X causes XRenderExtensionInfo to be damaged if this is done here
177 // left to be done in XCloseDisplay by Xlib.
178 #if defined(HAVE_XFT) && 0
179 static void *BC_WindowBase::*xft_font[] = {
180 &BC_WindowBase::smallfont_xft,
181 &BC_WindowBase::mediumfont_xft,
182 &BC_WindowBase::largefont_xft,
183 &BC_WindowBase::bigfont_xft,
184 &BC_WindowBase::bold_smallfont_xft,
185 &BC_WindowBase::bold_mediumfont_xft,
186 &BC_WindowBase::bold_largefont_xft,
188 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
189 XftFont *xft = (XftFont *)(this->*xft_font[i]);
190 if( xft ) XftFontClose (display, xft);
198 XFree(xinerama_info);
199 xinerama_screens = 0;
203 // Can't close display if another thread is waiting for events.
204 // Synchronous thread must delete display it owns a GLX_WINDOW
205 // Must be last reference to display.
206 #ifndef SINGLE_THREAD
208 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
209 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
210 "implemented for BC_Pixmap.\n"));
211 get_resources()->get_synchronous()->delete_display(this);
215 XCloseDisplay(display);
216 // clipboard uses a different display connection
217 clipboard->stop_clipboard();
219 #endif // SINGLE_THREAD
222 resize_history.remove_all_objects();
224 #ifndef SINGLE_THREAD
225 common_events.remove_all_objects();
227 delete event_condition;
230 top_level->window_lock = 0;
231 BC_Display::unlock_display();
236 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
237 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
238 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
241 UNSET_ALL_LOCKS(this)
244 int BC_WindowBase::initialize()
264 xinerama_screens = 0;
269 translation_events = 0;
270 ctrl_mask = shift_mask = alt_mask = 0;
271 cursor_x = cursor_y = button_number = 0;
283 active_popup_menu = 0;
284 active_subwindow = 0;
287 _7segment_pixmaps = 0;
290 // next_repeat_id = 0;
293 current_font = MEDIUMFONT;
294 current_color = BLACK;
295 current_cursor = ARROW_CURSOR;
298 shared_bg_pixmap = 0;
301 window_type = MAIN_WINDOW;
302 translation_count = 0;
303 x_correction = y_correction = 0;
312 #ifdef HAVE_LIBXXF86VM
316 bold_largefont_xft = 0;
317 bold_mediumfont_xft = 0;
318 bold_smallfont_xft = 0;
326 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
328 // Need these right away since put_event is called before run_window sometimes.
329 event_lock = new Mutex("BC_WindowBase::event_lock");
330 event_condition = new Condition(0, "BC_WindowBase::event_condition");
331 init_lock = new Condition(0, "BC_WindowBase::init_lock");
334 cursor_timer = new Timer;
337 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
338 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
339 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
352 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
355 ButtonReleaseMask | \
356 PointerMotionMask | \
360 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
372 const char *display_name,
374 BC_Pixmap *bg_pixmap,
377 XSetWindowAttributes attr;
379 XSizeHints size_hints;
382 #ifdef HAVE_LIBXXF86VM
386 id = get_resources()->get_id();
388 if(parent_window) top_level = parent_window->top_level;
389 if( top_level ) { // need this to avoid deadlock with Xlib's locks
391 lock_window("BC_WindowBase::create_window");
393 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
395 #ifdef HAVE_LIBXXF86VM
396 if(window_type == VIDMODE_SCALED_WINDOW)
397 closest_vm(&vm,&w,&h);
404 this->bg_color = bg_color;
405 this->window_type = window_type;
407 this->private_color = private_color;
408 this->parent_window = parent_window;
409 this->bg_pixmap = bg_pixmap;
410 this->allow_resize = allow_resize;
412 strcpy(this->display_name, display_name);
414 this->display_name[0] = 0;
417 if(bg_pixmap) shared_bg_pixmap = 1;
419 subwindows = new BC_SubWindowList;
421 if(window_type == MAIN_WINDOW)
424 parent_window = this;
428 display = BC_Display::get_display(display_name);
429 BC_Display::lock_display("BC_WindowBase::create_window");
430 // BC_Display::display_global->new_window(this);
433 // get the display connection
435 // This function must be the first Xlib
436 // function a multi-threaded program calls
438 display = init_display(display_name);
439 if( shm_completion_event < 0 ) shm_completion_event =
440 ShmCompletion + XShmGetEventBase(display);
443 screen = DefaultScreen(display);
444 rootwin = RootWindow(display, screen);
445 // window placement boundaries
446 if( !xinerama_screens && XineramaIsActive(display) )
447 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
448 root_w = get_root_w(0);
449 root_h = get_root_h(0);
452 vis = get_glx_visual(display);
455 vis = DefaultVisual(display, screen);
457 default_depth = DefaultDepth(display, screen);
459 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
460 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
463 // This must be done before fonts to know if antialiasing is available.
466 if(resources.use_shm < 0) resources.initialize_display(this);
467 x_correction = BC_DisplayInfo::get_left_border();
468 y_correction = BC_DisplayInfo::get_top_border();
470 // clamp window placement
471 if(this->x + this->w + x_correction > root_w)
472 this->x = root_w - this->w - x_correction;
473 if(this->y + this->h + y_correction > root_h)
474 this->y = root_h - this->h - y_correction;
475 if(this->x < 0) this->x = 0;
476 if(this->y < 0) this->y = 0;
478 if(this->bg_color == -1)
479 this->bg_color = resources.get_bg_color();
481 // printf("bcwindowbase 1 %s\n", title);
482 // if(window_type == MAIN_WINDOW) sleep(1);
483 // printf("bcwindowbase 10\n");
489 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
491 attr.event_mask = DEFAULT_EVENT_MASKS |
492 StructureNotifyMask |
496 attr.background_pixel = get_color(this->bg_color);
497 attr.colormap = cmap;
498 attr.cursor = get_cursor_struct(ARROW_CURSOR);
500 win = XCreateWindow(display, rootwin,
501 this->x, this->y, this->w, this->h, 0,
502 top_level->default_depth, InputOutput,
504 XGetNormalHints(display, win, &size_hints);
506 size_hints.flags = PSize | PMinSize | PMaxSize;
507 size_hints.width = this->w;
508 size_hints.height = this->h;
509 size_hints.min_width = allow_resize ? minw : this->w;
510 size_hints.max_width = allow_resize ? 32767 : this->w;
511 size_hints.min_height = allow_resize ? minh : this->h;
512 size_hints.max_height = allow_resize ? 32767 : this->h;
513 if(x > -BC_INFINITY && x < BC_INFINITY)
515 size_hints.flags |= PPosition;
516 size_hints.x = this->x;
517 size_hints.y = this->y;
521 txlist[0] = this->title;
523 XTextProperty titleprop;
524 if(options & WINDOW_UTF8)
525 Xutf8TextListToTextProperty(display, txlist, 1,
526 XUTF8StringStyle, &titleprop);
528 XmbTextListToTextProperty(display, txlist, 1,
529 XStdICCTextStyle, &titleprop);
530 XSetWMProperties(display, win, &titleprop, &titleprop,
531 0, 0, &size_hints, 0, 0);
532 XFree(titleprop.value);
535 #ifndef SINGLE_THREAD
536 clipboard = new BC_Clipboard(display_name);
537 clipboard->start_clipboard();
543 Atom ClientLeaderXAtom;
544 if (XGroupLeader == 0)
546 const char *instance_name = "cinelerra";
547 const char *class_name = "Cinelerra";
548 XClassHint *class_hints = XAllocClassHint();
549 class_hints->res_name = (char*)instance_name;
550 class_hints->res_class = (char*)class_name;
551 XSetClassHint(top_level->display, win, class_hints);
553 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
554 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
555 PropModeReplace, (unsigned char *)&XGroupLeader, true);
560 #ifdef HAVE_LIBXXF86VM
561 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
568 #ifdef HAVE_LIBXXF86VM
569 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
571 if(window_type == POPUP_WINDOW)
574 mask = CWEventMask | CWBackPixel | CWColormap |
575 CWOverrideRedirect | CWSaveUnder | CWCursor;
577 attr.event_mask = DEFAULT_EVENT_MASKS |
581 if(this->bg_color == -1)
582 this->bg_color = resources.get_bg_color();
583 attr.background_pixel = top_level->get_color(bg_color);
584 attr.colormap = top_level->cmap;
585 if(top_level->is_hourglass)
586 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
588 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
589 attr.override_redirect = True;
590 attr.save_under = True;
592 win = XCreateWindow(top_level->display,
593 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
594 top_level->default_depth, InputOutput, top_level->vis, mask,
596 top_level->add_popup(this);
599 if(window_type == SUB_WINDOW)
601 mask = CWEventMask | CWBackPixel | CWCursor;
602 attr.event_mask = DEFAULT_EVENT_MASKS;
603 attr.background_pixel = top_level->get_color(this->bg_color);
604 if(top_level->is_hourglass)
605 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
607 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
608 win = XCreateWindow(top_level->display,
609 parent_window->win, this->x, this->y, this->w, this->h, 0,
610 top_level->default_depth, InputOutput, top_level->vis, mask,
613 if(!hidden) XMapWindow(top_level->display, win);
616 // Create pixmap for all windows
617 pixmap = new BC_Pixmap(this, this->w, this->h);
619 // Set up options for main window
620 if(window_type == MAIN_WINDOW)
622 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
624 this->bg_pixmap = new BC_Pixmap(this,
625 get_resources()->bg_image,
629 if(!hidden) show_window();
636 draw_background(0, 0, this->w, this->h);
638 flash(-1, -1, -1, -1, 0);
640 // Set up options for popup window
641 #ifdef HAVE_LIBXXF86VM
642 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
644 if(window_type == POPUP_WINDOW)
648 if(!hidden) show_window();
650 get_resources()->create_window_lock->unlock();
651 if(need_lock) unlock_window();
656 Display* BC_WindowBase::init_display(const char *display_name)
660 if(display_name && display_name[0] == 0) display_name = NULL;
661 if((display = XOpenDisplay(display_name)) == NULL) {
662 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
664 if(getenv("DISPLAY") == NULL) {
665 printf(_("'DISPLAY' environment variable not set.\n"));
669 // Try again with default display.
670 if((display = XOpenDisplay(0)) == NULL) {
671 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
679 Display* BC_WindowBase::get_display()
681 return top_level->display;
684 int BC_WindowBase::get_screen()
686 return top_level->screen;
689 int BC_WindowBase::run_window()
695 // Events may have been sent before run_window so can't initialize them here.
698 set_repeat(get_resources()->tooltip_delay);
699 BC_Display::display_global->new_window(this);
701 // If the first window created, run the display loop in this thread.
702 if(BC_Display::display_global->is_first(this))
704 BC_Display::unlock_display();
705 BC_Display::display_global->loop();
709 BC_Display::unlock_display();
710 completion_lock->lock("BC_WindowBase::run_window");
713 BC_Display::lock_display("BC_WindowBase::run_window");
714 BC_Display::display_global->delete_window(this);
716 unset_all_repeaters();
718 BC_Display::unlock_display();
720 #else // SINGLE_THREAD
725 set_repeat(get_resources()->tooltip_delay);
727 // Start X server events
728 event_thread = new BC_WindowEvents(this);
729 event_thread->start();
735 // Handle common events
741 unset_all_repeaters();
745 event_condition->reset();
746 common_events.remove_all_objects();
750 #endif // SINGLE_THREAD
755 int BC_WindowBase::get_key_masks(unsigned int key_state)
757 // printf("BC_WindowBase::get_key_masks %llx\n",
758 // event->xkey.state);
759 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
760 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
761 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
766 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
768 BC_KeyboardHandlerLock set;
769 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
772 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
774 BC_KeyboardHandlerLock set;
775 int i = BC_KeyboardHandler::listeners.size();
776 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
777 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
780 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
782 int result = (win->*handler)(wp);
786 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
789 BC_KeyboardHandlerLock set;
790 for( int i=0; !result && i<listeners.size(); ++i ) {
791 BC_KeyboardHandler *listener = listeners[i];
792 result = listener->run_event(wp);
797 void BC_KeyboardHandler::kill_grabs()
799 BC_KeyboardHandlerLock set;
800 for( int i=0; i<listeners.size(); ++i ) {
801 BC_WindowBase *win = listeners[i]->win;
802 if( win->get_window_type() != POPUP_WINDOW ) continue;
803 ((BC_Popup *)win)->ungrab_keyboard();
807 void BC_ActiveBitmaps::reque(XEvent *event)
809 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
810 ShmSeg shmseg = shm_ev->shmseg;
811 Drawable drawable = shm_ev->drawable;
812 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
813 active_lock.lock("BC_BitmapImage::reque");
814 BC_BitmapImage *bfr = first;
815 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
816 if( bfr && bfr->drawable == drawable )
818 active_lock.unlock();
820 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
821 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
822 // (int)shmseg, (int)drawable);
825 if( bfr->drawable != drawable ) return;
826 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
827 bfr->bitmap->reque(bfr);
830 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
832 active_lock.lock("BC_BitmapImage::insert");
833 bfr->drawable = pixmap;
835 active_lock.unlock();
838 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
840 active_lock.lock("BC_ActiveBitmaps::remove");
841 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
843 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
844 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
846 active_lock.unlock();
849 BC_ActiveBitmaps::BC_ActiveBitmaps()
853 BC_ActiveBitmaps::~BC_ActiveBitmaps()
859 int BC_WindowBase::keysym_lookup(XEvent *event)
861 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
862 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
864 if( event->xany.send_event && !event->xany.serial ) {
865 keysym = (KeySym) event->xkey.keycode;
866 keys_return[0] = keysym;
869 wkey_string_length = 0;
871 if( input_context ) {
872 wkey_string_length = XwcLookupString(input_context,
873 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
874 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
875 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
878 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
879 keys_return, KEYPRESSLEN, &keysym, &stat);
880 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
881 if( stat == XLookupBoth ) return ret;
882 if( stat == XLookupKeySym ) return 0;
884 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
885 wkey_string_length = ret;
886 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
890 pthread_t locking_task = -1;
891 int locking_event = -1;
892 int locking_message = -1;
894 int BC_WindowBase::dispatch_event(XEvent *event)
898 XClientMessageEvent *ptr;
899 int cancel_resize, cancel_translation;
904 #ifndef SINGLE_THREAD
905 // If an event is waiting get it, otherwise
906 // wait for next event only if there are no compressed events.
907 if(get_event_count() ||
908 (!motion_events && !resize_events && !translation_events))
911 // Lock out window deletions
912 lock_window("BC_WindowBase::dispatch_event 1");
913 locking_event = event->type;
914 locking_task = pthread_self();
915 locking_message = event->xclient.message_type;
918 // Handle compressed events
920 lock_window("BC_WindowBase::dispatch_event 2");
922 dispatch_resize_event(last_resize_w, last_resize_h);
924 dispatch_motion_event();
925 if(translation_events)
926 dispatch_translation_event();
937 //static const char *event_names[] = {
938 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
939 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
940 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
941 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
942 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
943 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
944 // "GenericEvent", "LASTEvent",
948 //if(event->type != ClientMessage) {
949 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
950 //__LINE__, title, event, event->type,
951 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
952 // event_names[event->type] : "Unknown");
958 switch(event->type) {
960 // Clear the resize buffer
961 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
962 // Clear the motion buffer since this can clear the window
965 dispatch_motion_event();
968 ptr = (XClientMessageEvent*)event;
971 if(ptr->message_type == ProtoXAtom &&
972 (Atom)ptr->data.l[0] == DelWinXAtom)
977 if(ptr->message_type == RepeaterXAtom)
979 dispatch_repeat_event(ptr->data.l[0]);
980 // Make sure the repeater still exists.
981 // for(int i = 0; i < repeaters.total; i++)
983 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
985 // dispatch_repeat_event_master(ptr->data.l[0]);
991 if(ptr->message_type == SetDoneXAtom)
995 { // We currently use X marshalling for xatom events, we can switch to something else later
996 recieve_custom_xatoms((xatom_event *)ptr);
1002 dispatch_focus_in();
1007 dispatch_focus_out();
1019 get_key_masks(event->xbutton.state);
1020 cursor_x = event->xbutton.x;
1021 cursor_y = event->xbutton.y;
1022 button_number = event->xbutton.button;
1024 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1025 event_win = event->xany.window;
1026 if (button_number < 6)
1028 if(button_number < 4)
1030 button_pressed = event->xbutton.button;
1031 button_time1 = button_time2;
1032 button_time2 = button_time3;
1033 button_time3 = event->xbutton.time;
1036 drag_win = event_win;
1037 drag_x1 = cursor_x - get_resources()->drag_radius;
1038 drag_x2 = cursor_x + get_resources()->drag_radius;
1039 drag_y1 = cursor_y - get_resources()->drag_radius;
1040 drag_y2 = cursor_y + get_resources()->drag_radius;
1042 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1045 button_time3 = button_time2 = button_time1 = 0;
1047 if((long)(button_time3 - button_time2) < resources.double_click)
1050 // button_time3 = button_time2 = button_time1 = 0;
1058 dispatch_button_press();
1063 get_key_masks(event->xbutton.state);
1064 button_number = event->xbutton.button;
1065 event_win = event->xany.window;
1066 if (button_number < 6)
1068 if(button_number < 4)
1070 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1072 dispatch_button_release();
1077 event_win = event->xany.window;
1078 dispatch_expose_event();
1082 get_key_masks(event->xmotion.state);
1083 // Dispatch previous motion event if this is a subsequent motion from a different window
1084 if(motion_events && last_motion_win != event->xany.window)
1086 dispatch_motion_event();
1089 // Buffer the current motion
1091 last_motion_state = event->xmotion.state;
1092 last_motion_x = event->xmotion.x;
1093 last_motion_y = event->xmotion.y;
1094 last_motion_win = event->xany.window;
1097 case ConfigureNotify:
1098 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1100 // event->xany.window,
1103 XTranslateCoordinates(top_level->display,
1111 last_resize_w = event->xconfigure.width;
1112 last_resize_h = event->xconfigure.height;
1115 cancel_translation = 0;
1117 // Resize history prevents responses to recursive resize requests
1118 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1120 if(resize_history.values[i]->w == last_resize_w &&
1121 resize_history.values[i]->h == last_resize_h)
1123 delete resize_history.values[i];
1124 resize_history.remove_number(i);
1129 if(last_resize_w == w && last_resize_h == h)
1137 if((last_translate_x == x && last_translate_y == y))
1138 cancel_translation = 1;
1140 if(!cancel_translation)
1142 translation_events = 1;
1145 translation_count++;
1149 get_key_masks(event->xkey.state);
1150 keys_return[0] = 0; keysym = -1;
1151 if(XFilterEvent(event, win)) {
1154 if( keysym_lookup(event) < 0 ) {
1155 printf("keysym %x\n", (uint32_t)keysym);
1159 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1163 // block out control keys
1164 if(keysym > 0xffe0 && keysym < 0xffff) break;
1165 // block out Alt_GR key
1166 if(keysym == 0xfe03) break;
1169 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1171 #ifdef X_HAVE_UTF8_STRING
1172 //It's Ascii or UTF8?
1173 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1174 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1176 if( ((keys_return[1] & 0xff) > 0x80) &&
1177 ((keys_return[0] & 0xff) > 0xC0) ) {
1178 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1179 key_pressed = keysym & 0xff;
1185 // block out extra keys
1195 // Translate key codes
1196 case XK_Return: key_pressed = RETURN; break;
1197 case XK_Up: key_pressed = UP; break;
1198 case XK_Down: key_pressed = DOWN; break;
1199 case XK_Left: key_pressed = LEFT; break;
1200 case XK_Right: key_pressed = RIGHT; break;
1201 case XK_Next: key_pressed = PGDN; break;
1202 case XK_Prior: key_pressed = PGUP; break;
1203 case XK_BackSpace: key_pressed = BACKSPACE; break;
1204 case XK_Escape: key_pressed = ESC; break;
1207 key_pressed = LEFTTAB;
1211 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1212 case XK_underscore: key_pressed = '_'; break;
1213 case XK_asciitilde: key_pressed = '~'; break;
1214 case XK_Delete: key_pressed = DELETE; break;
1215 case XK_Home: key_pressed = HOME; break;
1216 case XK_End: key_pressed = END; break;
1219 case XK_KP_Enter: key_pressed = KPENTER; break;
1220 case XK_KP_Add: key_pressed = KPPLUS; break;
1221 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1222 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1223 case XK_KP_Divide: key_pressed = KPSLASH; break;
1225 case XK_KP_End: key_pressed = KP1; break;
1227 case XK_KP_Down: key_pressed = KP2; break;
1229 case XK_KP_Page_Down: key_pressed = KP3; break;
1231 case XK_KP_Left: key_pressed = KP4; break;
1233 case XK_KP_Begin: key_pressed = KP5; break;
1235 case XK_KP_Right: key_pressed = KP6; break;
1237 case XK_KP_Home: key_pressed = KP7; break;
1239 case XK_KP_Up: key_pressed = KP8; break;
1241 case XK_KP_Page_Up: key_pressed = KP9; break;
1243 case XK_KP_Insert: key_pressed = KPINS; break;
1245 case XK_KP_Delete: key_pressed = KPDEL; break;
1246 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1248 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1249 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1250 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1251 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1252 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1253 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1254 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1255 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1256 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1257 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1258 // above case XK_Left: key_pressed = LEFT; break; /* left */
1259 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1260 // above case XK_Down: key_pressed = DOWN; break; /* down */
1261 // above case XK_Up: key_pressed = UP; break; /* up */
1262 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1263 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1264 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1265 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1266 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1267 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1268 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1269 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1270 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1271 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1272 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1273 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1276 key_pressed = keysym & 0xff;
1277 #ifdef X_HAVE_UTF8_STRING
1278 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1283 #ifdef X_HAVE_UTF8_STRING
1285 key_pressed_utf8 = keys_return;
1290 if( top_level == this )
1291 result = BC_KeyboardHandler::run_listeners(this);
1293 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1295 result = dispatch_keypress_event();
1296 // Handle some default keypresses
1299 if(key_pressed == 'w' ||
1308 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1309 dispatch_keyrelease_event();
1310 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1311 // keysym, event->xkey.state);
1315 event_win = event->xany.window;
1316 dispatch_cursor_leave();
1320 event_win = event->xany.window;
1321 cursor_x = event->xcrossing.x;
1322 cursor_y = event->xcrossing.y;
1323 dispatch_cursor_enter();
1328 //printf("100 %s %p %d\n", title, event, event->type);
1329 //if(event->type != ClientMessage) dump();
1331 #ifndef SINGLE_THREAD
1333 if(event) delete event;
1335 // if(done) completion_lock->unlock();
1338 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1342 int BC_WindowBase::dispatch_expose_event()
1345 for(int i = 0; i < subwindows->total && !result; i++)
1347 result = subwindows->values[i]->dispatch_expose_event();
1350 // Propagate to user
1351 if(!result) expose_event();
1355 int BC_WindowBase::dispatch_resize_event(int w, int h)
1357 // Can't store new w and h until the event is handles
1358 // because bcfilebox depends on the old w and h to
1359 // reposition widgets.
1360 if(window_type == MAIN_WINDOW)
1364 pixmap = new BC_Pixmap(this, w, h);
1366 clear_box(0, 0, w, h);
1369 // Propagate to subwindows
1370 for(int i = 0; i < subwindows->total; i++)
1372 subwindows->values[i]->dispatch_resize_event(w, h);
1375 // Propagate to user
1378 if(window_type == MAIN_WINDOW)
1386 int BC_WindowBase::dispatch_translation_event()
1388 translation_events = 0;
1389 if(window_type == MAIN_WINDOW)
1393 x = last_translate_x;
1394 y = last_translate_y;
1395 // Correct for window manager offsets
1400 for(int i = 0; i < subwindows->total; i++)
1402 subwindows->values[i]->dispatch_translation_event();
1405 translation_event();
1409 int BC_WindowBase::dispatch_motion_event()
1414 if(top_level == this)
1417 event_win = last_motion_win;
1418 get_key_masks(last_motion_state);
1421 if(get_button_down() && !active_menubar && !active_popup_menu)
1425 cursor_x = last_motion_x;
1426 cursor_y = last_motion_y;
1427 result = dispatch_drag_motion();
1431 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1432 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1437 result = dispatch_drag_start();
1441 cursor_x = last_motion_x;
1442 cursor_y = last_motion_y;
1444 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1447 // active_popup_menu,
1448 // active_subwindow);
1450 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1451 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1452 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1455 // Dispatch in stacking order
1456 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1458 result = subwindows->values[i]->dispatch_motion_event();
1461 if(!result) result = cursor_motion_event(); // give to user
1465 int BC_WindowBase::dispatch_keypress_event()
1468 if(top_level == this)
1470 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1473 for(int i = 0; i < subwindows->total && !result; i++)
1475 result = subwindows->values[i]->dispatch_keypress_event();
1478 if(!result) result = keypress_event();
1483 int BC_WindowBase::dispatch_keyrelease_event()
1486 if(top_level == this)
1488 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1491 for(int i = 0; i < subwindows->total && !result; i++)
1493 result = subwindows->values[i]->dispatch_keyrelease_event();
1496 if(!result) result = keyrelease_event();
1501 int BC_WindowBase::dispatch_focus_in()
1503 for(int i = 0; i < subwindows->total; i++)
1505 subwindows->values[i]->dispatch_focus_in();
1513 int BC_WindowBase::dispatch_focus_out()
1515 for(int i = 0; i < subwindows->total; i++)
1517 subwindows->values[i]->dispatch_focus_out();
1525 int BC_WindowBase::get_has_focus()
1527 return top_level->has_focus;
1530 int BC_WindowBase::get_deleting()
1532 if(is_deleting) return 1;
1533 if(parent_window && parent_window->get_deleting()) return 1;
1537 int BC_WindowBase::dispatch_button_press()
1542 if(top_level == this)
1544 if(active_menubar) result = active_menubar->dispatch_button_press();
1545 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1546 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1549 for(int i = 0; i < subwindows->total && !result; i++)
1551 result = subwindows->values[i]->dispatch_button_press();
1554 if(!result) result = button_press_event();
1560 int BC_WindowBase::dispatch_button_release()
1563 if(top_level == this)
1565 if(active_menubar) result = active_menubar->dispatch_button_release();
1566 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1567 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1568 if(!result && button_number != 4 && button_number != 5)
1569 result = dispatch_drag_stop();
1572 for(int i = 0; i < subwindows->total && !result; i++)
1574 result = subwindows->values[i]->dispatch_button_release();
1579 result = button_release_event();
1586 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1589 // all repeat event handlers get called and decide based on activity and duration
1590 // whether to respond
1591 for(int i = 0; i < subwindows->total; i++)
1593 subwindows->values[i]->dispatch_repeat_event(duration);
1597 repeat_event(duration);
1601 // Unlock next repeat signal
1602 if(window_type == MAIN_WINDOW)
1604 #ifdef SINGLE_THREAD
1605 BC_Display::display_global->unlock_repeaters(duration);
1607 for(int i = 0; i < repeaters.total; i++)
1609 if(repeaters.values[i]->delay == duration)
1611 repeaters.values[i]->repeat_lock->unlock();
1619 void BC_WindowBase::unhide_cursor()
1624 if(top_level->is_hourglass)
1625 set_cursor(HOURGLASS_CURSOR, 1, 0);
1627 set_cursor(current_cursor, 1, 0);
1629 cursor_timer->update();
1633 void BC_WindowBase::update_video_cursor()
1635 if(video_on && !is_transparent)
1637 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1640 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1641 cursor_timer->update();
1646 cursor_timer->update();
1651 int BC_WindowBase::dispatch_cursor_leave()
1655 for(int i = 0; i < subwindows->total; i++)
1657 subwindows->values[i]->dispatch_cursor_leave();
1660 cursor_leave_event();
1664 int BC_WindowBase::dispatch_cursor_enter()
1670 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1671 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1672 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1674 for(int i = 0; !result && i < subwindows->total; i++)
1676 result = subwindows->values[i]->dispatch_cursor_enter();
1679 if(!result) result = cursor_enter_event();
1683 int BC_WindowBase::cursor_enter_event()
1688 int BC_WindowBase::cursor_leave_event()
1693 int BC_WindowBase::close_event()
1699 int BC_WindowBase::dispatch_drag_start()
1702 if(active_menubar) result = active_menubar->dispatch_drag_start();
1703 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1704 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1706 for(int i = 0; i < subwindows->total && !result; i++)
1708 result = subwindows->values[i]->dispatch_drag_start();
1711 if(!result) result = is_dragging = drag_start_event();
1715 int BC_WindowBase::dispatch_drag_stop()
1719 for(int i = 0; i < subwindows->total && !result; i++)
1721 result = subwindows->values[i]->dispatch_drag_stop();
1724 if(is_dragging && !result)
1734 int BC_WindowBase::dispatch_drag_motion()
1737 for(int i = 0; i < subwindows->total && !result; i++)
1739 result = subwindows->values[i]->dispatch_drag_motion();
1742 if(is_dragging && !result)
1744 drag_motion_event();
1755 int BC_WindowBase::show_tooltip(int w, int h)
1759 if(tooltip_text && !tooltip_on &&
1760 (force_tooltip || get_resources()->tooltips_enabled))
1763 top_level->hide_tooltip();
1767 w = get_text_width(MEDIUMFONT, tooltip_text);
1770 h = get_text_height(MEDIUMFONT, tooltip_text);
1772 w += TOOLTIP_MARGIN * 2;
1773 h += TOOLTIP_MARGIN * 2;
1775 XTranslateCoordinates(top_level->display, win,
1776 top_level->rootwin, get_w(), get_h(),
1778 // keep the tip inside the window/display
1779 int top_x = top_level->get_x();
1780 if( x < top_x ) x = top_x;
1781 int top_w = top_x + top_level->get_w();
1782 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1783 if( top_w < lmt_w ) lmt_w = top_w;
1784 if( x+w > lmt_w ) x = lmt_w-w;
1786 int top_y = top_level->get_y();
1787 if( y < top_y ) y = top_y;
1788 int top_h = top_y + top_level->get_h();
1789 int lmt_h = top_level->get_root_h(0);
1790 if( top_h < lmt_h ) lmt_h = top_h;
1791 if( y+h > lmt_h ) y = lmt_h-h;
1793 int abs_x, abs_y, win_x, win_y;
1794 unsigned int temp_mask;
1796 XQueryPointer(top_level->display, top_level->win,
1797 &temp_win, &temp_win, &abs_x, &abs_y,
1798 &win_x, &win_y, &temp_mask);
1799 // check for cursor inside popup
1800 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1802 if( x-abs_x < y-abs_y )
1807 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1808 get_resources()->tooltip_bg_color);
1811 tooltip_popup->set_font(MEDIUMFONT);
1812 tooltip_popup->flash();
1813 tooltip_popup->flush();
1818 int BC_WindowBase::hide_tooltip()
1821 for(int i = 0; i < subwindows->total; i++)
1823 subwindows->values[i]->hide_tooltip();
1829 delete tooltip_popup;
1835 const char *BC_WindowBase::get_tooltip()
1837 return tooltip_text;
1840 int BC_WindowBase::set_tooltip(const char *text)
1842 tooltip_text = text;
1844 // Update existing tooltip if it is visible
1848 tooltip_popup->flash();
1853 // signal the event handler to repeat
1854 int BC_WindowBase::set_repeat(int64_t duration)
1858 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1861 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1863 #ifdef SINGLE_THREAD
1864 BC_Display::display_global->set_repeat(this, duration);
1866 // test repeater database for duplicates
1867 for(int i = 0; i < repeaters.total; i++)
1870 if(repeaters.values[i]->delay == duration)
1872 repeaters.values[i]->start_repeating(this);
1877 BC_Repeater *repeater = new BC_Repeater(this, duration);
1878 repeater->initialize();
1879 repeaters.append(repeater);
1880 repeater->start_repeating();
1885 int BC_WindowBase::unset_repeat(int64_t duration)
1887 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1889 #ifdef SINGLE_THREAD
1890 BC_Display::display_global->unset_repeat(this, duration);
1892 for(int i = 0; i < repeaters.total; i++)
1894 if(repeaters.values[i]->delay == duration)
1896 repeaters.values[i]->stop_repeating();
1904 int BC_WindowBase::unset_all_repeaters()
1906 #ifdef SINGLE_THREAD
1907 BC_Display::display_global->unset_all_repeaters(this);
1909 for(int i = 0; i < repeaters.total; i++)
1911 repeaters.values[i]->stop_repeating();
1913 repeaters.remove_all_objects();
1918 // long BC_WindowBase::get_repeat_id()
1920 // return top_level->next_repeat_id++;
1924 #ifndef SINGLE_THREAD
1925 int BC_WindowBase::arm_repeat(int64_t duration)
1927 XEvent *event = new XEvent;
1928 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1929 ptr->type = ClientMessage;
1930 ptr->message_type = RepeaterXAtom;
1932 ptr->data.l[0] = duration;
1934 // Couldn't use XSendEvent since it locked up randomly.
1940 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1945 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1947 XEvent *myevent = new XEvent;
1948 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1949 ptr->type = ClientMessage;
1950 ptr->message_type = event->message_type;
1951 ptr->format = event->format;
1952 ptr->data.l[0] = event->data.l[0];
1953 ptr->data.l[1] = event->data.l[1];
1954 ptr->data.l[2] = event->data.l[2];
1955 ptr->data.l[3] = event->data.l[3];
1956 ptr->data.l[4] = event->data.l[4];
1964 Atom BC_WindowBase::create_xatom(const char *atom_name)
1966 return XInternAtom(display, atom_name, False);
1969 int BC_WindowBase::get_atoms()
1971 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1972 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1973 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
1974 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1975 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
1976 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
1977 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
1983 void BC_WindowBase::init_cursors()
1985 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
1986 cross_cursor = XCreateFontCursor(display, XC_crosshair);
1987 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
1988 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
1989 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
1990 move_cursor = XCreateFontCursor(display, XC_fleur);
1991 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
1992 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
1993 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
1994 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
1995 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
1996 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
1997 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
1998 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2001 char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2002 Colormap colormap = DefaultColormap(display, screen);
2003 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2004 rootwin, cursor_data, 8, 8);
2005 XColor black, dummy;
2006 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2007 transparent_cursor = XCreatePixmapCursor(display,
2008 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2009 // XDefineCursor(display, win, transparent_cursor);
2010 XFreePixmap(display, pixmap_bottom);
2013 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2015 int color_model = BC_TRANSPARENCY;
2019 color_model = BC_RGB8;
2022 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2025 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2028 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2034 int BC_WindowBase::init_colors()
2037 current_color_value = current_color_pixel = 0;
2039 // Get the real depth
2042 ximage = XCreateImage(top_level->display,
2044 top_level->default_depth,
2052 bits_per_pixel = ximage->bits_per_pixel;
2053 XDestroyImage(ximage);
2055 color_model = evaluate_color_model(client_byte_order,
2058 // Get the color model
2064 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2065 create_private_colors();
2069 cmap = DefaultColormap(display, screen);
2070 create_shared_colors();
2073 allocate_color_table();
2077 //cmap = DefaultColormap(display, screen);
2078 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2084 int BC_WindowBase::create_private_colors()
2089 for(int i = 0; i < 255; i++)
2091 color = (i & 0xc0) << 16;
2092 color += (i & 0x38) << 10;
2093 color += (i & 0x7) << 5;
2094 color_table[i][0] = color;
2096 create_shared_colors(); // overwrite the necessary colors on the table
2101 int BC_WindowBase::create_color(int color)
2103 if(total_colors == 256)
2105 // replace the closest match with an exact match
2106 color_table[get_color_rgb8(color)][0] = color;
2110 // add the color to the table
2111 color_table[total_colors][0] = color;
2117 int BC_WindowBase::create_shared_colors()
2119 create_color(BLACK);
2120 create_color(WHITE);
2122 create_color(LTGREY);
2123 create_color(MEGREY);
2124 create_color(MDGREY);
2125 create_color(DKGREY);
2127 create_color(LTCYAN);
2128 create_color(MECYAN);
2129 create_color(MDCYAN);
2130 create_color(DKCYAN);
2132 create_color(LTGREEN);
2133 create_color(GREEN);
2134 create_color(DKGREEN);
2136 create_color(LTPINK);
2140 create_color(LTBLUE);
2142 create_color(DKBLUE);
2144 create_color(LTYELLOW);
2145 create_color(MEYELLOW);
2146 create_color(MDYELLOW);
2147 create_color(DKYELLOW);
2149 create_color(LTPURPLE);
2150 create_color(MEPURPLE);
2151 create_color(MDPURPLE);
2152 create_color(DKPURPLE);
2154 create_color(FGGREY);
2155 create_color(MNBLUE);
2156 create_color(ORANGE);
2157 create_color(FTGREY);
2162 int BC_WindowBase::allocate_color_table()
2164 int red, green, blue, color;
2167 for(int i = 0; i < total_colors; i++)
2169 color = color_table[i][0];
2170 red = (color & 0xFF0000) >> 16;
2171 green = (color & 0x00FF00) >> 8;
2172 blue = color & 0xFF;
2174 col.flags = DoRed | DoGreen | DoBlue;
2175 col.red = red<<8 | red;
2176 col.green = green<<8 | green;
2177 col.blue = blue<<8 | blue;
2179 XAllocColor(display, cmap, &col);
2180 color_table[i][1] = col.pixel;
2183 XInstallColormap(display, cmap);
2187 int BC_WindowBase::init_window_shape()
2189 if(bg_pixmap && bg_pixmap->use_alpha())
2191 XShapeCombineMask(top_level->display,
2192 this->win, ShapeBounding, 0, 0,
2193 bg_pixmap->get_alpha(), ShapeSet);
2199 int BC_WindowBase::init_gc()
2201 unsigned long gcmask;
2202 gcmask = GCFont | GCGraphicsExposures;
2205 gcvalues.font = mediumfont->fid; // set the font
2206 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2207 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2209 // gcmask = GCCapStyle | GCJoinStyle;
2210 // XGetGCValues(display, gc, gcmask, &gcvalues);
2211 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2215 int BC_WindowBase::init_fonts()
2217 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2218 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2219 smallfont = XLoadQueryFont(display, "fixed");
2220 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2221 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2222 mediumfont = XLoadQueryFont(display, "fixed");
2223 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2224 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2225 largefont = XLoadQueryFont(display, "fixed");
2226 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2227 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2228 bigfont = XLoadQueryFont(display, "fixed");
2231 if(get_resources()->use_fontset)
2236 // FIXME: should check the m,d,n values
2237 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2239 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2240 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2241 if( !mediumfontset )
2242 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2243 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2245 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2246 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2248 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2249 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2250 curr_fontset = mediumfontset;
2251 get_resources()->use_fontset = 1;
2255 get_resources()->use_fontset = 0;
2262 void BC_WindowBase::init_xft()
2265 if( !get_resources()->use_xft ) return;
2266 if(!(smallfont_xft =
2267 (resources.small_font_xft[0] == '-' ?
2268 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2269 XftFontOpenName(display, screen, resources.small_font_xft))) )
2270 if(!(smallfont_xft =
2271 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2272 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2273 if(!(mediumfont_xft =
2274 (resources.medium_font_xft[0] == '-' ?
2275 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2276 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2277 if(!(mediumfont_xft =
2278 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2279 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2280 if(!(largefont_xft =
2281 (resources.large_font_xft[0] == '-' ?
2282 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2283 XftFontOpenName(display, screen, resources.large_font_xft))) )
2284 if(!(largefont_xft =
2285 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2286 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2288 (resources.big_font_xft[0] == '-' ?
2289 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2290 XftFontOpenName(display, screen, resources.big_font_xft))) )
2292 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2293 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2295 if(!(bold_smallfont_xft =
2296 (resources.small_b_font_xft[0] == '-' ?
2297 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2298 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2299 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2300 if(!(bold_mediumfont_xft =
2301 (resources.medium_b_font_xft[0] == '-' ?
2302 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2303 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2304 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2305 if(!(bold_largefont_xft =
2306 (resources.large_b_font_xft[0] == '-' ?
2307 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2308 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2309 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2311 // Extension failed to locate fonts
2312 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2313 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2314 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2315 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2316 resources.small_font_xft, smallfont_xft,
2317 resources.medium_font_xft, mediumfont_xft,
2318 resources.large_font_xft, largefont_xft,
2319 resources.big_font_xft, bigfont_xft,
2320 resources.small_b_font_xft, bold_smallfont_xft,
2321 resources.medium_b_font_xft, bold_mediumfont_xft,
2322 resources.large_b_font_xft, bold_largefont_xft);
2323 get_resources()->use_xft = 0;
2329 void BC_WindowBase::init_im()
2331 XIMStyles *xim_styles;
2334 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2336 printf("BC_WindowBase::init_im: Could not open input method.\n");
2339 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2342 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2343 XCloseIM(input_method);
2348 for(int z = 0; z < xim_styles->count_styles; z++)
2350 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2352 xim_style = xim_styles->supported_styles[z];
2360 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2361 XCloseIM(input_method);
2365 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2366 XNClientWindow, win, XNFocusWindow, win, NULL);
2369 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2370 XCloseIM(input_method);
2375 void BC_WindowBase::finit_im()
2377 if( input_context ) {
2378 XDestroyIC(input_context);
2381 if( input_method ) {
2382 XCloseIM(input_method);
2388 int BC_WindowBase::get_color(int64_t color)
2390 // return pixel of color
2391 // use this only for drawing subwindows not for bitmaps
2392 int i, test, difference;
2398 return get_color_rgb8(color);
2399 // test last color looked up
2400 if(current_color_value == color)
2401 return current_color_pixel;
2404 current_color_value = color;
2405 for(i = 0; i < total_colors; i++)
2407 if(color_table[i][0] == color)
2409 current_color_pixel = color_table[i][1];
2410 return current_color_pixel;
2414 // find nearest match
2415 difference = 0xFFFFFF;
2417 for(i = 0; i < total_colors; i++)
2419 test = abs((int)(color_table[i][0] - color));
2421 if(test < difference)
2423 current_color_pixel = color_table[i][1];
2427 return current_color_pixel;
2430 return get_color_rgb16(color);
2433 return get_color_bgr16(color);
2437 return client_byte_order == server_byte_order ?
2438 color : get_color_bgr24(color);
2446 int BC_WindowBase::get_color_rgb8(int color)
2450 pixel = (color & 0xc00000) >> 16;
2451 pixel += (color & 0xe000) >> 10;
2452 pixel += (color & 0xe0) >> 5;
2456 int64_t BC_WindowBase::get_color_rgb16(int color)
2459 result = (color & 0xf80000) >> 8;
2460 result += (color & 0xfc00) >> 5;
2461 result += (color & 0xf8) >> 3;
2466 int64_t BC_WindowBase::get_color_bgr16(int color)
2469 result = (color & 0xf80000) >> 19;
2470 result += (color & 0xfc00) >> 5;
2471 result += (color & 0xf8) << 8;
2476 int64_t BC_WindowBase::get_color_bgr24(int color)
2479 result = (color & 0xff) << 16;
2480 result += (color & 0xff00);
2481 result += (color & 0xff0000) >> 16;
2485 void BC_WindowBase::start_video()
2487 cursor_timer->update();
2489 // set_color(BLACK);
2490 // draw_box(0, 0, get_w(), get_h());
2494 void BC_WindowBase::stop_video()
2502 int64_t BC_WindowBase::get_color()
2504 return top_level->current_color;
2507 void BC_WindowBase::set_color(int64_t color)
2509 top_level->current_color = color;
2510 XSetForeground(top_level->display,
2512 top_level->get_color(color));
2515 void BC_WindowBase::set_opaque()
2517 XSetFunction(top_level->display, top_level->gc, GXcopy);
2520 void BC_WindowBase::set_inverse()
2522 XSetFunction(top_level->display, top_level->gc, GXxor);
2525 void BC_WindowBase::set_line_width(int value)
2527 this->line_width = value;
2528 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2529 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2530 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2531 JoinMiter); /* join_style */
2533 if(line_dashes > 0) {
2534 const char dashes = line_dashes;
2535 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2538 // XGCValues gcvalues;
2539 // unsigned long gcmask;
2540 // gcmask = GCCapStyle | GCJoinStyle;
2541 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2542 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2545 void BC_WindowBase::set_line_dashes(int value)
2547 line_dashes = value;
2548 // call XSetLineAttributes
2549 set_line_width(line_width);
2553 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2557 case ARROW_CURSOR: return top_level->arrow_cursor; break;
2558 case CROSS_CURSOR: return top_level->cross_cursor;
2559 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
2560 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
2561 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
2562 case MOVE_CURSOR: return top_level->move_cursor; break;
2563 case LEFT_CURSOR: return top_level->left_cursor; break;
2564 case RIGHT_CURSOR: return top_level->right_cursor; break;
2565 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
2566 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
2567 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
2568 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
2569 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
2570 case HOURGLASS_CURSOR: return top_level->hourglass_cursor; break;
2571 case TRANSPARENT_CURSOR: return top_level->transparent_cursor; break;
2576 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2578 // inherit cursor from parent
2581 XUndefineCursor(top_level->display, win);
2582 current_cursor = cursor;
2585 // don't change cursor if overridden
2586 if((!top_level->is_hourglass && !is_transparent) ||
2589 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2590 if(flush) this->flush();
2593 if(!override) current_cursor = cursor;
2596 void BC_WindowBase::set_x_cursor(int cursor)
2598 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2599 XDefineCursor(top_level->display, win, temp_cursor);
2600 current_cursor = cursor;
2604 int BC_WindowBase::get_cursor()
2606 return current_cursor;
2609 void BC_WindowBase::start_hourglass()
2611 top_level->start_hourglass_recursive();
2615 void BC_WindowBase::stop_hourglass()
2617 top_level->stop_hourglass_recursive();
2621 void BC_WindowBase::start_hourglass_recursive()
2623 if(this == top_level)
2631 set_cursor(HOURGLASS_CURSOR, 1, 0);
2632 for(int i = 0; i < subwindows->total; i++)
2634 subwindows->values[i]->start_hourglass_recursive();
2639 void BC_WindowBase::stop_hourglass_recursive()
2641 if(this == top_level)
2643 if(hourglass_total == 0) return;
2644 top_level->hourglass_total--;
2647 if(!top_level->hourglass_total)
2649 top_level->is_hourglass = 0;
2651 // Cause set_cursor to perform change
2653 set_cursor(current_cursor, 1, 0);
2655 for(int i = 0; i < subwindows->total; i++)
2657 subwindows->values[i]->stop_hourglass_recursive();
2665 XFontStruct* BC_WindowBase::get_font_struct(int font)
2667 // Clear out unrelated flags
2668 if(font & BOLDFACE) font ^= BOLDFACE;
2671 case SMALLFONT: return top_level->smallfont; break;
2672 case MEDIUMFONT: return top_level->mediumfont; break;
2673 case LARGEFONT: return top_level->largefont; break;
2674 case BIGFONT: return top_level->bigfont; break;
2679 XFontSet BC_WindowBase::get_fontset(int font)
2683 if(get_resources()->use_fontset)
2685 switch(font & 0xff) {
2686 case SMALLFONT: fs = top_level->smallfontset; break;
2687 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2688 case LARGEFONT: fs = top_level->largefontset; break;
2689 case BIGFONT: fs = top_level->bigfontset; break;
2697 XftFont* BC_WindowBase::get_xft_struct(int font)
2700 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2701 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2702 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2703 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2704 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2705 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2706 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2721 int BC_WindowBase::get_current_font()
2723 return top_level->current_font;
2726 void BC_WindowBase::set_font(int font)
2728 top_level->current_font = font;
2731 if(get_resources()->use_xft) {}
2734 if(get_resources()->use_fontset) {
2738 if(get_font_struct(font))
2740 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2746 void BC_WindowBase::set_fontset(int font)
2750 if(get_resources()->use_fontset) {
2752 case SMALLFONT: fs = top_level->smallfontset; break;
2753 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2754 case LARGEFONT: fs = top_level->largefontset; break;
2755 case BIGFONT: fs = top_level->bigfontset; break;
2763 XFontSet BC_WindowBase::get_curr_fontset(void)
2765 if(get_resources()->use_fontset)
2766 return curr_fontset;
2770 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2773 if(get_resources()->use_xft && get_xft_struct(font))
2776 #ifdef X_HAVE_UTF8_STRING
2777 if(get_resources()->locale_utf8)
2779 XftTextExtentsUtf8(top_level->display,
2780 get_xft_struct(font),
2781 (const XftChar8 *)text,
2788 XftTextExtents8(top_level->display,
2789 get_xft_struct(font),
2790 (const XftChar8 *)text,
2794 return extents.xOff;
2798 if(get_resources()->use_fontset && top_level->get_fontset(font))
2799 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2801 if(get_font_struct(font))
2802 return XTextWidth(get_font_struct(font), text, length);
2808 case MEDIUM_7SEGMENT:
2809 return get_resources()->medium_7segment[0]->get_w() * length;
2819 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2822 if(get_resources()->use_xft && get_xft_struct(font)) {
2825 XftTextExtents32(top_level->display, get_xft_struct(font),
2826 (const FcChar32*)text, length, &extents);
2827 return extents.xOff;
2830 if(!get_font_struct(font)) return 0;
2831 XChar2b xtext[length], *xp = xtext;
2832 for( int i=0; i<length; ++i,++xp ) {
2833 xp->byte1 = (unsigned char) (text[i] >> 8);
2834 xp->byte2 = (unsigned char) (text[i] & 0xff);
2836 return XTextWidth16(get_font_struct(font), xtext, length);
2839 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2841 int i, j, w = 0, line_w = 0;
2842 if(length < 0) length = strlen(text);
2844 for(i = 0, j = 0; i <= length; i++)
2849 line_w = get_single_text_width(font, &text[j], i - j);
2855 line_w = get_single_text_width(font, &text[j], length - j);
2857 if(line_w > w) w = line_w;
2860 if(i > length && w == 0)
2862 w = get_single_text_width(font, text, length);
2868 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2870 int i, j, w = 0, line_w = 0;
2872 if(length < 0) length = wcslen(text);
2874 for(i = 0, j = 0; i <= length; i++)
2879 line_w = get_single_text_width(font, &text[j], i - j);
2884 line_w = get_single_text_width(font, &text[j], length - j);
2886 if(line_w > w) w = line_w;
2889 if(i > length && w == 0)
2890 w = get_single_text_width(font, text, length);
2895 int BC_WindowBase::get_text_ascent(int font)
2899 if( (fstruct = get_xft_struct(font)) != 0 )
2900 return fstruct->ascent;
2902 if(get_resources()->use_fontset && top_level->get_fontset(font))
2904 XFontSetExtents *extents;
2906 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2907 return -extents->max_logical_extent.y;
2910 if(get_font_struct(font))
2911 return top_level->get_font_struct(font)->ascent;
2914 case MEDIUM_7SEGMENT:
2915 return get_resources()->medium_7segment[0]->get_h();
2920 int BC_WindowBase::get_text_descent(int font)
2924 if( (fstruct = get_xft_struct(font)) != 0 )
2925 return fstruct->descent;
2927 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2928 XFontSetExtents *extents;
2929 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2930 return (extents->max_logical_extent.height
2931 + extents->max_logical_extent.y);
2934 if(get_font_struct(font))
2935 return top_level->get_font_struct(font)->descent;
2940 int BC_WindowBase::get_text_height(int font, const char *text)
2945 if( (fstruct = get_xft_struct(font)) != 0 )
2946 rowh = fstruct->height;
2949 rowh = get_text_ascent(font) + get_text_descent(font);
2951 if(!text) return rowh;
2953 // Add height of lines
2954 int h = 0, i, length = strlen(text);
2955 for(i = 0; i <= length; i++)
2966 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2968 if(color_model < 0) color_model = top_level->get_color_model();
2969 return new BC_Bitmap(top_level, w, h, color_model);
2972 void BC_WindowBase::init_wait()
2974 #ifndef SINGLE_THREAD
2975 if(window_type != MAIN_WINDOW)
2976 top_level->init_wait();
2977 init_lock->lock("BC_WindowBase::init_wait");
2978 init_lock->unlock();
2982 int BC_WindowBase::accel_available(int color_model, int lock_it)
2984 if(window_type != MAIN_WINDOW)
2985 return top_level->accel_available(color_model, lock_it);
2989 if(lock_it) lock_window("BC_WindowBase::accel_available");
2993 result = grab_port_id(this, color_model);
2996 xvideo_port_id = result;
3008 //printf("BC_WindowBase::accel_available 1\n");
3009 result = grab_port_id(this, color_model);
3010 //printf("BC_WindowBase::accel_available 2 %d\n", result);
3013 xvideo_port_id = result;
3018 //printf("BC_WindowBase::accel_available 3 %d\n", xvideo_port_id);
3026 if(lock_it) unlock_window();
3027 //printf("BC_WindowBase::accel_available %d %d\n", color_model, result);
3032 int BC_WindowBase::grab_port_id(BC_WindowBase *window, int color_model)
3034 int numFormats, i, j, k;
3035 unsigned int ver, rev, numAdapt, reqBase, eventBase, errorBase;
3036 XvAdaptorInfo *info;
3037 XvImageFormatValues *formats;
3040 if(!get_resources()->use_xvideo) return -1;
3042 // Translate from color_model to X color model
3043 x_color_model = BC_CModels::bc_to_x(color_model);
3045 // Only local server is fast enough.
3046 if(!resources.use_shm) return -1;
3048 // XV extension is available
3049 if(Success != XvQueryExtension(window->display, &ver, &rev,
3050 &reqBase, &eventBase, &errorBase)) {
3054 // XV adaptors are available
3055 XvQueryAdaptors(window->display,
3056 DefaultRootWindow(window->display),
3063 // Get adaptor with desired color model
3064 for(i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3065 /* adaptor supports XvImages */
3066 if(info[i].type & XvImageMask)
3068 formats = XvListImageFormats(window->display,
3071 // for(j = 0; j < numFormats; j++)
3072 // printf("%08x\n", formats[j].id);
3074 int numPorts = info[i].num_ports;
3075 for(j = 0; j < (int)numFormats && xvideo_port_id < 0; j++)
3077 /* this adaptor supports the desired format */
3078 if(formats[j].id == x_color_model)
3080 /* Try to grab a port */
3081 for(k = 0; k < numPorts; k++)
3084 if(Success == XvGrabPort(top_level->display,
3085 info[i].base_id + k,
3088 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3089 xvideo_port_id = info[i].base_id + k;
3095 if(formats) XFree(formats);
3099 XvFreeAdaptorInfo(info);
3101 return xvideo_port_id;
3105 int BC_WindowBase::show_window(int flush)
3107 for(int i = 0; i < subwindows->size(); i++)
3109 subwindows->get(i)->show_window(0);
3112 XMapWindow(top_level->display, win);
3113 if(flush) XFlush(top_level->display);
3114 // XSync(top_level->display, 0);
3119 int BC_WindowBase::hide_window(int flush)
3121 for(int i = 0; i < subwindows->size(); i++)
3123 subwindows->get(i)->hide_window(0);
3126 XUnmapWindow(top_level->display, win);
3127 if(flush) this->flush();
3132 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3134 subwindows->append((BC_SubWindow*)menu_bar);
3136 menu_bar->parent_window = this;
3137 menu_bar->top_level = this->top_level;
3138 menu_bar->initialize();
3142 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3144 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3145 if(this != top_level) return top_level->add_popup(window);
3146 popups.append(window);
3150 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3152 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3153 if(this != top_level)
3154 top_level->remove_popup(window);
3156 popups.remove(window);
3157 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3161 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3163 subwindows->append(subwindow);
3165 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3167 // parent window must be set before the subwindow initialization
3168 subwindow->parent_window = this;
3169 subwindow->top_level = this->top_level;
3171 // Execute derived initialization
3172 subwindow->initialize();
3177 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3179 return add_subwindow(subwindow);
3182 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3184 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3186 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3189 XClearArea(top_level->display, win, x, y, w, h, 0);
3193 XClearWindow(top_level->display, win);
3201 int BC_WindowBase::flash(int flush)
3203 flash(-1, -1, -1, -1, flush);
3207 void BC_WindowBase::flush()
3209 //if(!get_window_lock())
3210 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3211 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3212 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3213 int locked = get_window_lock();
3214 if( !locked ) lock_window("BC_WindowBase::flush");
3215 XFlush(top_level->display);
3216 if( !locked ) unlock_window();
3219 void BC_WindowBase::sync_display()
3221 int locked = get_window_lock();
3222 if( !locked ) lock_window("BC_WindowBase::sync_display");
3223 XSync(top_level->display, False);
3224 if( !locked ) unlock_window();
3227 int BC_WindowBase::get_window_lock()
3229 #ifdef SINGLE_THREAD
3230 return BC_Display::display_global->get_display_locked();
3232 return top_level->window_lock;
3236 int BC_WindowBase::lock_window(const char *location)
3238 if(top_level && top_level != this)
3240 top_level->lock_window(location);
3245 SET_LOCK(this, title, location);
3246 #ifdef SINGLE_THREAD
3247 BC_Display::lock_display(location);
3249 XLockDisplay(top_level->display);
3250 top_level->display_lock_owner = pthread_self();
3253 ++top_level->window_lock;
3257 printf("BC_WindowBase::lock_window top_level NULL\n");
3262 int BC_WindowBase::unlock_window()
3264 if(top_level && top_level != this)
3266 top_level->unlock_window();
3273 // if(!top_level->window_lock)
3275 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3279 if( top_level->window_lock > 0 )
3280 if( --top_level->window_lock == 0 )
3281 top_level->display_lock_owner = 0;
3282 #ifdef SINGLE_THREAD
3283 BC_Display::unlock_display();
3285 XUnlockDisplay(top_level->display);
3290 printf("BC_WindowBase::unlock_window top_level NULL\n");
3295 void BC_WindowBase::set_done(int return_value)
3297 if(done_set) return;
3299 if(window_type != MAIN_WINDOW)
3300 top_level->set_done(return_value);
3303 #ifdef SINGLE_THREAD
3304 this->return_value = return_value;
3305 BC_Display::display_global->arm_completion(this);
3306 completion_lock->unlock();
3307 #else // SINGLE_THREAD
3309 if( !event_thread ) return;
3310 XEvent *event = new XEvent;
3311 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3312 event->type = ClientMessage;
3313 ptr->message_type = SetDoneXAtom;
3315 this->return_value = return_value;
3316 // May lock up here because XSendEvent doesn't work too well
3317 // asynchronous with XNextEvent.
3318 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3320 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3326 void BC_WindowBase::close(int return_value)
3328 hide_window(); flush();
3329 set_done(return_value);
3332 int BC_WindowBase::get_w()
3337 int BC_WindowBase::get_h()
3342 int BC_WindowBase::get_x()
3347 int BC_WindowBase::get_y()
3353 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3354 // this is the bounding box of all the screens
3356 int BC_WindowBase::get_root_w(int lock_display)
3358 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3359 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3360 int result = WidthOfScreen(def_screen);
3361 if(lock_display) unlock_window();
3365 int BC_WindowBase::get_root_h(int lock_display)
3367 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3368 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3369 int result = HeightOfScreen(def_screen);
3370 if(lock_display) unlock_window();
3374 XineramaScreenInfo *
3375 BC_WindowBase::get_xinerama_info(int screen)
3377 if( !xinerama_info || !xinerama_screens ) return 0;
3379 for( int i=0; i<xinerama_screens; ++i )
3380 if( xinerama_info[i].screen_number == screen )
3381 return &xinerama_info[i];
3384 int top_x = get_x(), top_y = get_y();
3385 for( int i=0; i<xinerama_screens; ++i ) {
3386 int scr_y = top_y - xinerama_info[i].y_org;
3387 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3388 int scr_x = top_x - xinerama_info[i].x_org;
3389 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3390 return &xinerama_info[i];
3395 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3398 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3399 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3402 int root_w = get_root_w(0);
3403 int root_h = get_root_h(0);
3404 // Shift X based on position of current window if dual head
3405 if( (float)root_w/root_h > 1.8 ) {
3406 root_w = get_screen_w(0, 0);
3407 if( top_level->get_x() >= root_w )
3412 result = info->x_org;
3413 if(lock_display) unlock_window();
3417 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3419 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3420 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3421 int result = !info ? 0 : info->y_org;
3422 if(lock_display) unlock_window();
3426 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3429 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3430 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3432 int width = get_root_w(0);
3433 int height = get_root_h(0);
3434 if( (float)width/height > 1.8 ) {
3435 // If dual head, the screen width is > 16x9
3436 // but we only want to fill one screen
3437 // this code assumes the "big" screen is on the right
3438 int scr_w0 = width / 2;
3440 case 600: scr_w0 = 800; break;
3441 case 720: scr_w0 = 1280; break;
3442 case 1024: scr_w0 = 1280; break;
3443 case 1200: scr_w0 = 1600; break;
3444 case 1080: scr_w0 = 1920; break;
3446 int scr_w1 = width - scr_w0;
3447 result = screen > 0 ? scr_w1 :
3448 screen == 0 ? scr_w0 :
3449 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3455 result = info->width;
3456 if(lock_display) unlock_window();
3460 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3462 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3463 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3464 int result = info ? info->height : get_root_h(0);
3465 if(lock_display) unlock_window();
3469 // Bottom right corner
3470 int BC_WindowBase::get_x2()
3475 int BC_WindowBase::get_y2()
3480 int BC_WindowBase::get_video_on()
3485 int BC_WindowBase::get_hidden()
3487 return top_level->hidden;
3490 int BC_WindowBase::cursor_inside()
3492 return (top_level->cursor_x >= 0 &&
3493 top_level->cursor_y >= 0 &&
3494 top_level->cursor_x < w &&
3495 top_level->cursor_y < h);
3498 BC_WindowBase* BC_WindowBase::get_top_level()
3503 BC_WindowBase* BC_WindowBase::get_parent()
3505 return parent_window;
3508 int BC_WindowBase::get_color_model()
3510 return top_level->color_model;
3513 BC_Resources* BC_WindowBase::get_resources()
3515 return &BC_WindowBase::resources;
3518 BC_Synchronous* BC_WindowBase::get_synchronous()
3520 return BC_WindowBase::resources.get_synchronous();
3523 int BC_WindowBase::get_bg_color()
3528 void BC_WindowBase::set_bg_color(int color)
3530 this->bg_color = color;
3533 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3538 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3540 top_level->active_subwindow = subwindow;
3543 int BC_WindowBase::activate()
3548 int BC_WindowBase::deactivate()
3550 if(window_type == MAIN_WINDOW)
3552 if(top_level->active_menubar) top_level->active_menubar->deactivate();
3553 if(top_level->active_popup_menu) top_level->active_popup_menu->deactivate();
3554 if(top_level->active_subwindow) top_level->active_subwindow->deactivate();
3556 top_level->active_menubar = 0;
3557 top_level->active_popup_menu = 0;
3558 top_level->active_subwindow = 0;
3563 int BC_WindowBase::cycle_textboxes(int amount)
3566 BC_WindowBase *new_textbox = 0;
3570 BC_WindowBase *first_textbox = 0;
3571 find_next_textbox(&first_textbox, &new_textbox, result);
3572 if(!new_textbox) new_textbox = first_textbox;
3578 BC_WindowBase *last_textbox = 0;
3579 find_prev_textbox(&last_textbox, &new_textbox, result);
3580 if(!new_textbox) new_textbox = last_textbox;
3584 if(new_textbox != active_subwindow)
3587 new_textbox->activate();
3593 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3595 // Search subwindows for textbox
3596 for(int i = 0; i < subwindows->total && result < 2; i++)
3598 BC_WindowBase *test_subwindow = subwindows->values[i];
3599 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3606 if(!*first_textbox) *first_textbox = this;
3610 if(top_level->active_subwindow == this)
3616 *next_textbox = this;
3623 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3629 if(!*last_textbox) *last_textbox = this;
3633 if(top_level->active_subwindow == this)
3639 *prev_textbox = this;
3644 // Search subwindows for textbox
3645 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3647 BC_WindowBase *test_subwindow = subwindows->values[i];
3648 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3653 BC_Clipboard* BC_WindowBase::get_clipboard()
3655 #ifdef SINGLE_THREAD
3656 return BC_Display::display_global->clipboard;
3658 return top_level->clipboard;
3662 int BC_WindowBase::get_relative_cursor_x()
3664 int abs_x, abs_y, x, y, win_x, win_y;
3665 unsigned int temp_mask;
3668 XQueryPointer(top_level->display, top_level->win,
3669 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3672 XTranslateCoordinates(top_level->display, top_level->rootwin,
3673 win, abs_x, abs_y, &x, &y, &temp_win);
3678 int BC_WindowBase::get_relative_cursor_y()
3680 int abs_x, abs_y, x, y, win_x, win_y;
3681 unsigned int temp_mask;
3684 XQueryPointer(top_level->display, top_level->win,
3685 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3688 XTranslateCoordinates(top_level->display,
3689 top_level->rootwin, win, abs_x, abs_y, &x, &y, &temp_win);
3694 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3696 int abs_x, abs_y, win_x, win_y;
3697 unsigned int temp_mask;
3700 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
3701 XQueryPointer(top_level->display, top_level->win,
3702 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3704 if(lock_window) this->unlock_window();
3708 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3710 int abs_x, abs_y, win_x, win_y;
3711 unsigned int temp_mask;
3714 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
3715 XQueryPointer(top_level->display, top_level->win,
3716 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3718 if(lock_window) this->unlock_window();
3722 int BC_WindowBase::match_window(Window win)
3724 if (this->win == win) return 1;
3726 for(int i = 0; i < subwindows->total; i++) {
3727 result = subwindows->values[i]->match_window(win);
3728 if (result) return result;
3734 int BC_WindowBase::get_cursor_over_window()
3736 if(top_level != this) return top_level->get_cursor_over_window();
3738 int abs_x, abs_y, win_x, win_y;
3739 unsigned int temp_mask;
3740 Window root_return, child_return;
3742 int ret = XQueryPointer(display, win,
3743 &root_return, &child_return, &abs_x, &abs_y,
3744 &win_x, &win_y, &temp_mask);
3745 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3746 // __LINE__, title, win, ret, child_return);
3748 int result = !ret || child_return == None ? 0 :
3749 match_window(child_return);
3753 int BC_WindowBase::cursor_above()
3755 int rx = get_relative_cursor_x();
3756 if( rx < 0 || rx >= get_w() ) return 0;
3757 int ry = get_relative_cursor_y();
3758 if( ry < 0 || ry >= get_h() ) return 0;
3762 int BC_WindowBase::get_drag_x()
3764 return top_level->drag_x;
3767 int BC_WindowBase::get_drag_y()
3769 return top_level->drag_y;
3772 int BC_WindowBase::get_cursor_x()
3774 return top_level->cursor_x;
3777 int BC_WindowBase::get_cursor_y()
3779 return top_level->cursor_y;
3782 int BC_WindowBase::dump_windows()
3784 printf("\tBC_WindowBase::dump_windows window=%p win=%p\n",
3785 this, (void*)this->win);
3786 for(int i = 0; i < subwindows->size(); i++)
3787 subwindows->get(i)->dump_windows();
3788 for(int i = 0; i < popups.size(); i++)
3789 printf("\tBC_WindowBase::dump_windows popup=%p win=%p\n",
3790 popups.get(i), (void*)popups.get(i)->win);
3794 int BC_WindowBase::is_event_win()
3796 return this->win == top_level->event_win;
3799 void BC_WindowBase::set_dragging(int value)
3801 is_dragging = value;
3804 int BC_WindowBase::get_dragging()
3809 int BC_WindowBase::get_buttonpress()
3811 return top_level->button_number;
3814 int BC_WindowBase::get_button_down()
3816 return top_level->button_down;
3819 int BC_WindowBase::alt_down()
3821 return top_level->alt_mask;
3824 int BC_WindowBase::shift_down()
3826 return top_level->shift_mask;
3829 int BC_WindowBase::ctrl_down()
3831 return top_level->ctrl_mask;
3834 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3837 *length = top_level->wkey_string_length;
3838 return top_level->wkey_string;
3841 #ifdef X_HAVE_UTF8_STRING
3842 char* BC_WindowBase::get_keypress_utf8()
3844 return top_level->key_pressed_utf8;
3849 int BC_WindowBase::get_keypress()
3851 return top_level->key_pressed;
3854 int BC_WindowBase::get_double_click()
3856 return top_level->double_click;
3859 int BC_WindowBase::get_triple_click()
3861 return top_level->triple_click;
3864 int BC_WindowBase::get_bgcolor()
3869 int BC_WindowBase::resize_window(int w, int h)
3871 if(this->w == w && this->h == h) return 0;
3873 if(window_type == MAIN_WINDOW && !allow_resize)
3875 XSizeHints size_hints;
3876 size_hints.flags = PSize | PMinSize | PMaxSize;
3877 size_hints.width = w;
3878 size_hints.height = h;
3879 size_hints.min_width = w;
3880 size_hints.max_width = w;
3881 size_hints.min_height = h;
3882 size_hints.max_height = h;
3883 XSetNormalHints(top_level->display, win, &size_hints);
3885 XResizeWindow(top_level->display, win, w, h);
3890 pixmap = new BC_Pixmap(this, w, h);
3892 // Propagate to menubar
3893 for(int i = 0; i < subwindows->total; i++)
3895 subwindows->values[i]->dispatch_resize_event(w, h);
3898 draw_background(0, 0, w, h);
3899 if(top_level == this && get_resources()->recursive_resizing)
3900 resize_history.append(new BC_ResizeCall(w, h));
3904 // The only way for resize events to be propagated is by updating the internal w and h
3905 int BC_WindowBase::resize_event(int w, int h)
3907 if(window_type == MAIN_WINDOW)
3915 int BC_WindowBase::reposition_window(int x, int y)
3917 reposition_window(x, y, -1, -1);
3922 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
3926 // Some tools set their own dimensions before calling this, causing the
3927 // resize check to skip.
3931 if(w > 0 && w != this->w)
3937 if(h > 0 && h != this->h)
3943 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
3946 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
3948 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
3950 if(translation_count && window_type == MAIN_WINDOW)
3952 // KDE shifts window right and down.
3953 // FVWM leaves window alone and adds border around it.
3954 XMoveResizeWindow(top_level->display,
3956 x - BC_DisplayInfo::auto_reposition_x,
3957 y - BC_DisplayInfo::auto_reposition_y,
3963 XMoveResizeWindow(top_level->display,
3974 pixmap = new BC_Pixmap(this, this->w, this->h);
3975 // Propagate to menubar
3976 for(int i = 0; i < subwindows->total; i++)
3978 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
3981 // draw_background(0, 0, w, h);
3987 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
3989 return reposition_window(get_x()+dx, get_y()+dy, w, h);
3992 int BC_WindowBase::reposition_window_relative(int dx, int dy)
3994 return reposition_window_relative(dx, dy, -1, -1);
3997 void BC_WindowBase::set_tooltips(int v)
3999 get_resources()->tooltips_enabled = v;
4002 void BC_WindowBase::set_force_tooltip(int v)
4007 int BC_WindowBase::raise_window(int do_flush)
4009 XRaiseWindow(top_level->display, win);
4010 if(do_flush) XFlush(top_level->display);
4014 int BC_WindowBase::lower_window(int do_flush)
4016 XLowerWindow(top_level->display, win);
4017 if(do_flush) XFlush(top_level->display);
4021 void BC_WindowBase::set_background(VFrame *bitmap)
4023 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4025 bg_pixmap = new BC_Pixmap(this,
4028 shared_bg_pixmap = 0;
4029 draw_background(0, 0, w, h);
4032 void BC_WindowBase::put_title(const char *text)
4034 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4035 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4036 *cp++ = *bp >= ' ' ? *bp : ' ';
4040 void BC_WindowBase::set_title(const char *text)
4045 txlist[0] = this->title;
4048 XTextProperty titleprop;
4049 XmbTextListToTextProperty(top_level->display, txlist, 1,
4050 XStdICCTextStyle, &titleprop);
4051 XSetWMName(top_level->display, top_level->win, &titleprop);
4052 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4053 XFree(titleprop.value);
4058 void BC_WindowBase::set_utf8title(const char *text)
4060 XTextProperty titleprop;
4063 strcpy(this->title, text);
4064 txlist[0] = this->title;
4067 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4068 XUTF8StringStyle, &titleprop);
4069 XSetWMName(top_level->display, top_level->win, &titleprop);
4070 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4071 XFree(titleprop.value);
4076 const char *BC_WindowBase::get_title()
4081 int BC_WindowBase::get_toggle_value()
4083 return toggle_value;
4086 int BC_WindowBase::get_toggle_drag()
4091 int BC_WindowBase::set_icon(VFrame *data)
4093 if(icon_pixmap) delete icon_pixmap;
4094 icon_pixmap = new BC_Pixmap(top_level,
4099 if(icon_window) delete icon_window;
4100 icon_window = new BC_Popup(this,
4103 icon_pixmap->get_w(),
4104 icon_pixmap->get_h(),
4106 1, // All windows are hidden initially
4110 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4111 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4112 wm_hints.icon_mask = icon_pixmap->get_alpha();
4113 wm_hints.icon_window = icon_window->win;
4114 wm_hints.window_group = XGroupLeader;
4116 // for(int i = 0; i < 1000; i++)
4117 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4120 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4121 XSync(top_level->display, 0);
4125 int BC_WindowBase::set_w(int w)
4131 int BC_WindowBase::set_h(int h)
4137 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4139 BC_Resources *resources = get_resources();
4140 char string[BCTEXTLEN];
4141 int newest_id = - 1;
4142 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4144 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4145 resources->filebox_history[i].path[0] = 0;
4146 defaults->get(string, resources->filebox_history[i].path);
4147 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4148 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4149 if(resources->filebox_history[i].id > newest_id)
4150 newest_id = resources->filebox_history[i].id;
4153 resources->filebox_id = newest_id + 1;
4154 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4155 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4156 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4157 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4158 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4159 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4160 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4161 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4162 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4163 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4164 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4165 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4169 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4171 BC_Resources *resources = get_resources();
4172 char string[BCTEXTLEN];
4173 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4175 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4176 defaults->update(string, resources->filebox_history[i].path);
4177 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4178 defaults->update(string, resources->filebox_history[i].id);
4180 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4181 defaults->update("FILEBOX_W", resources->filebox_w);
4182 defaults->update("FILEBOX_H", resources->filebox_h);
4183 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4184 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4185 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4186 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4187 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4188 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4189 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4190 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4191 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4197 // For some reason XTranslateCoordinates can take a long time to return.
4198 // We work around this by only calling it when the event windows are different.
4199 void BC_WindowBase::translate_coordinates(Window src_w,
4211 *dest_x_return = src_x;
4212 *dest_y_return = src_y;
4216 XTranslateCoordinates(top_level->display,
4224 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4228 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4230 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4233 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4235 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4243 #ifdef HAVE_LIBXXF86VM
4244 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4248 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4250 XF86VidModeModeInfo **vm_modelines;
4251 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4252 for (i = 0; i < vm_count; i++) {
4253 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4256 display = top_level->display;
4257 if (vm_modelines[*vm]->hdisplay == *width)
4261 *width = vm_modelines[*vm]->hdisplay;
4262 *height = vm_modelines[*vm]->vdisplay;
4267 void BC_WindowBase::scale_vm(int vm)
4269 int foo,bar,dotclock;
4270 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4273 XF86VidModeModeInfo **vm_modelines;
4274 XF86VidModeModeLine vml;
4275 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4276 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4277 orig_modeline.dotclock = dotclock;
4278 orig_modeline.hdisplay = vml.hdisplay;
4279 orig_modeline.hsyncstart = vml.hsyncstart;
4280 orig_modeline.hsyncend = vml.hsyncend;
4281 orig_modeline.htotal = vml.htotal;
4282 orig_modeline.vdisplay = vml.vdisplay;
4283 orig_modeline.vsyncstart = vml.vsyncstart;
4284 orig_modeline.vsyncend = vml.vsyncend;
4285 orig_modeline.vtotal = vml.vtotal;
4286 orig_modeline.flags = vml.flags;
4287 orig_modeline.privsize = vml.privsize;
4288 // orig_modeline.private = vml.private;
4289 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4290 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4291 XFlush(top_level->display);
4295 void BC_WindowBase::restore_vm()
4297 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4298 XFlush(top_level->display);
4321 #ifndef SINGLE_THREAD
4322 int BC_WindowBase::get_event_count()
4324 event_lock->lock("BC_WindowBase::get_event_count");
4325 int result = common_events.total;
4326 event_lock->unlock();
4330 XEvent* BC_WindowBase::get_event()
4333 while(!done && !result)
4335 event_condition->lock("BC_WindowBase::get_event");
4336 event_lock->lock("BC_WindowBase::get_event");
4338 if(common_events.total && !done)
4340 result = common_events.values[0];
4341 common_events.remove_number(0);
4344 event_lock->unlock();
4349 void BC_WindowBase::put_event(XEvent *event)
4351 event_lock->lock("BC_WindowBase::put_event");
4352 common_events.append(event);
4353 event_lock->unlock();
4354 event_condition->unlock();
4357 void BC_WindowBase::dequeue_events(Window win)
4359 event_lock->lock("BC_WindowBase::dequeue_events");
4361 int out = 0, total = common_events.size();
4362 for( int in=0; in<total; ++in ) {
4363 if( common_events[in]->xany.window == win ) continue;
4364 common_events[out++] = common_events[in];
4366 common_events.total = out;
4368 event_lock->unlock();
4371 #endif // SINGLE_THREAD
4373 int BC_WindowBase::get_id()
4379 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4381 int w = vframe->get_w(), h = vframe->get_h();
4382 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4383 icon->draw_vframe(vframe, 0,0, w,h, 0,0);