4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcclipboard.h"
24 #include "bcdisplay.h"
25 #include "bcdisplayinfo.h"
26 #include "bcmenubar.h"
29 #include "bcpopupmenu.h"
30 #include "bcrepeater.h"
31 #include "bcresources.h"
32 #include "bcsignals.h"
33 #include "bcsubwindow.h"
34 #include "bcsynchronous.h"
36 #include "bcwindowbase.h"
37 #include "bcwindowevents.h"
38 #include "bccmodels.h"
40 #include "condition.h"
58 #include <X11/extensions/Xinerama.h>
59 #include <X11/extensions/Xvlib.h>
60 #include <X11/extensions/shape.h>
61 #include <X11/XF86keysym.h>
62 #include <X11/Sunkeysym.h>
65 BC_ResizeCall::BC_ResizeCall(int w, int h)
77 int BC_WindowBase::shm_completion_event = -1;
81 BC_Resources BC_WindowBase::resources;
83 Window XGroupLeader = 0;
85 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
86 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
88 BC_WindowBase::BC_WindowBase(int opts)
91 //printf("BC_WindowBase::BC_WindowBase 1\n");
92 BC_WindowBase::initialize();
95 BC_WindowBase::~BC_WindowBase()
98 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
100 if(window_type == MAIN_WINDOW)
101 lock_window("BC_WindowBase::~BC_WindowBase");
104 #ifdef HAVE_LIBXXF86VM
105 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
112 if(window_type != MAIN_WINDOW)
115 XSelectInput(top_level->display, this->win, 0);
116 motion_events = resize_events = translation_events = 0;
117 #ifndef SINGLE_THREAD
118 top_level->dequeue_events(win);
120 if(top_level->active_menubar == this) top_level->active_menubar = 0;
121 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
122 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
123 // Remove pointer from parent window to this
124 parent_window->subwindows->remove(this);
127 if(icon_window) delete icon_window;
128 if(window_type == POPUP_WINDOW)
129 parent_window->remove_popup(this);
131 // Delete the subwindows
134 while(subwindows->total)
136 // Subwindow removes its own pointer
137 delete subwindows->values[0];
145 if( get_resources()->get_synchronous() &&
146 (top_level->options & GLX_WINDOW) && glx_win != 0 )
147 get_resources()->get_synchronous()->delete_window(this);
150 XDestroyWindow(top_level->display, win);
152 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
153 if(icon_pixmap) delete icon_pixmap;
154 if(temp_bitmap) delete temp_bitmap;
155 top_level->active_bitmaps.remove_buffers(this);
156 if(_7segment_pixmaps)
158 for(int i = 0; i < TOTAL_7SEGMENT; i++)
159 delete _7segment_pixmaps[i];
161 delete [] _7segment_pixmaps;
166 if(window_type == MAIN_WINDOW)
168 XFreeGC(display, gc);
169 static XFontStruct *BC_WindowBase::*xfont[] = {
170 &BC_WindowBase::smallfont,
171 &BC_WindowBase::mediumfont,
172 &BC_WindowBase::largefont,
173 &BC_WindowBase::bigfont,
175 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
176 XFreeFont(display, this->*xfont[i]);
178 // bug in X causes XRenderExtensionInfo to be damaged if this is done here
179 // left to be done in XCloseDisplay by Xlib.
180 #if defined(HAVE_XFT) && 0
181 static void *BC_WindowBase::*xft_font[] = {
182 &BC_WindowBase::smallfont_xft,
183 &BC_WindowBase::mediumfont_xft,
184 &BC_WindowBase::largefont_xft,
185 &BC_WindowBase::bigfont_xft,
186 &BC_WindowBase::bold_smallfont_xft,
187 &BC_WindowBase::bold_mediumfont_xft,
188 &BC_WindowBase::bold_largefont_xft,
190 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
191 XftFont *xft = (XftFont *)(this->*xft_font[i]);
192 if( xft ) XftFontClose (display, xft);
200 XFree(xinerama_info);
201 xinerama_screens = 0;
203 if( xvideo_port_id >= 0 )
204 XvUngrabPort(display, xvideo_port_id, CurrentTime);
207 // Can't close display if another thread is waiting for events.
208 // Synchronous thread must delete display it owns a GLX_WINDOW
209 // Must be last reference to display.
210 #ifndef SINGLE_THREAD
212 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
213 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
214 "implemented for BC_Pixmap.\n"));
215 get_resources()->get_synchronous()->delete_display(this);
219 XCloseDisplay(display);
220 // clipboard uses a different display connection
221 clipboard->stop_clipboard();
223 #endif // SINGLE_THREAD
226 resize_history.remove_all_objects();
228 #ifndef SINGLE_THREAD
229 common_events.remove_all_objects();
231 delete event_condition;
234 top_level->window_lock = 0;
235 BC_Display::unlock_display();
240 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
241 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
242 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
245 UNSET_ALL_LOCKS(this)
248 int BC_WindowBase::initialize()
268 xinerama_screens = 0;
273 translation_events = 0;
274 ctrl_mask = shift_mask = alt_mask = 0;
275 cursor_x = cursor_y = button_number = 0;
287 active_popup_menu = 0;
288 active_subwindow = 0;
291 _7segment_pixmaps = 0;
294 // next_repeat_id = 0;
297 current_font = MEDIUMFONT;
298 current_color = BLACK;
299 current_cursor = ARROW_CURSOR;
302 shared_bg_pixmap = 0;
305 window_type = MAIN_WINDOW;
306 translation_count = 0;
307 x_correction = y_correction = 0;
316 #ifdef HAVE_LIBXXF86VM
320 bold_largefont_xft = 0;
321 bold_mediumfont_xft = 0;
322 bold_smallfont_xft = 0;
330 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
332 // Need these right away since put_event is called before run_window sometimes.
333 event_lock = new Mutex("BC_WindowBase::event_lock");
334 event_condition = new Condition(0, "BC_WindowBase::event_condition");
335 init_lock = new Condition(0, "BC_WindowBase::init_lock");
338 cursor_timer = new Timer;
341 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
342 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
343 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
357 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
360 ButtonReleaseMask | \
361 PointerMotionMask | \
365 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
377 const char *display_name,
379 BC_Pixmap *bg_pixmap,
382 XSetWindowAttributes attr;
384 XSizeHints size_hints;
387 #ifdef HAVE_LIBXXF86VM
391 id = get_resources()->get_id();
393 if(parent_window) top_level = parent_window->top_level;
394 if( top_level ) { // need this to avoid deadlock with Xlib's locks
396 lock_window("BC_WindowBase::create_window");
398 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
400 #ifdef HAVE_LIBXXF86VM
401 if(window_type == VIDMODE_SCALED_WINDOW)
402 closest_vm(&vm,&w,&h);
409 this->bg_color = bg_color;
410 this->window_type = window_type;
412 this->private_color = private_color;
413 this->parent_window = parent_window;
414 this->bg_pixmap = bg_pixmap;
415 this->allow_resize = allow_resize;
417 strcpy(this->display_name, display_name);
419 this->display_name[0] = 0;
422 if(bg_pixmap) shared_bg_pixmap = 1;
424 subwindows = new BC_SubWindowList;
426 if(window_type == MAIN_WINDOW)
429 parent_window = this;
433 display = BC_Display::get_display(display_name);
434 BC_Display::lock_display("BC_WindowBase::create_window");
435 // BC_Display::display_global->new_window(this);
438 // get the display connection
440 // This function must be the first Xlib
441 // function a multi-threaded program calls
443 display = init_display(display_name);
444 if( shm_completion_event < 0 ) shm_completion_event =
445 ShmCompletion + XShmGetEventBase(display);
448 screen = DefaultScreen(display);
449 rootwin = RootWindow(display, screen);
450 // window placement boundaries
451 if( !xinerama_screens && XineramaIsActive(display) )
452 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
453 root_w = get_root_w(0);
454 root_h = get_root_h(0);
457 vis = get_glx_visual(display);
460 vis = DefaultVisual(display, screen);
462 default_depth = DefaultDepth(display, screen);
464 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
465 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
468 // This must be done before fonts to know if antialiasing is available.
471 if(resources.use_shm < 0) resources.initialize_display(this);
472 x_correction = BC_DisplayInfo::get_left_border();
473 y_correction = BC_DisplayInfo::get_top_border();
475 // clamp window placement
476 if(this->x + this->w + x_correction > root_w)
477 this->x = root_w - this->w - x_correction;
478 if(this->y + this->h + y_correction > root_h)
479 this->y = root_h - this->h - y_correction;
480 if(this->x < 0) this->x = 0;
481 if(this->y < 0) this->y = 0;
483 if(this->bg_color == -1)
484 this->bg_color = resources.get_bg_color();
486 // printf("bcwindowbase 1 %s\n", title);
487 // if(window_type == MAIN_WINDOW) sleep(1);
488 // printf("bcwindowbase 10\n");
494 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
496 attr.event_mask = DEFAULT_EVENT_MASKS |
497 StructureNotifyMask |
501 attr.background_pixel = get_color(this->bg_color);
502 attr.colormap = cmap;
503 attr.cursor = get_cursor_struct(ARROW_CURSOR);
505 win = XCreateWindow(display, rootwin,
506 this->x, this->y, this->w, this->h, 0,
507 top_level->default_depth, InputOutput,
509 XGetNormalHints(display, win, &size_hints);
511 size_hints.flags = PSize | PMinSize | PMaxSize;
512 size_hints.width = this->w;
513 size_hints.height = this->h;
514 size_hints.min_width = allow_resize ? minw : this->w;
515 size_hints.max_width = allow_resize ? 32767 : this->w;
516 size_hints.min_height = allow_resize ? minh : this->h;
517 size_hints.max_height = allow_resize ? 32767 : this->h;
518 if(x > -BC_INFINITY && x < BC_INFINITY)
520 size_hints.flags |= PPosition;
521 size_hints.x = this->x;
522 size_hints.y = this->y;
526 txlist[0] = this->title;
528 XTextProperty titleprop;
529 if(options & WINDOW_UTF8)
530 Xutf8TextListToTextProperty(display, txlist, 1,
531 XUTF8StringStyle, &titleprop);
533 XmbTextListToTextProperty(display, txlist, 1,
534 XStdICCTextStyle, &titleprop);
535 XSetWMProperties(display, win, &titleprop, &titleprop,
536 0, 0, &size_hints, 0, 0);
537 XFree(titleprop.value);
540 #ifndef SINGLE_THREAD
541 clipboard = new BC_Clipboard(display_name);
542 clipboard->start_clipboard();
548 Atom ClientLeaderXAtom;
549 if (XGroupLeader == 0)
551 const char *instance_name = "cinelerra";
552 const char *class_name = "Cinelerra";
553 XClassHint *class_hints = XAllocClassHint();
554 class_hints->res_name = (char*)instance_name;
555 class_hints->res_class = (char*)class_name;
556 XSetClassHint(top_level->display, win, class_hints);
558 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
559 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
560 PropModeReplace, (unsigned char *)&XGroupLeader, true);
565 #ifdef HAVE_LIBXXF86VM
566 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
573 #ifdef HAVE_LIBXXF86VM
574 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
576 if(window_type == POPUP_WINDOW)
579 mask = CWEventMask | CWBackPixel | CWColormap |
580 CWOverrideRedirect | CWSaveUnder | CWCursor;
582 attr.event_mask = DEFAULT_EVENT_MASKS |
586 if(this->bg_color == -1)
587 this->bg_color = resources.get_bg_color();
588 attr.background_pixel = top_level->get_color(bg_color);
589 attr.colormap = top_level->cmap;
590 if(top_level->is_hourglass)
591 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
593 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
594 attr.override_redirect = True;
595 attr.save_under = True;
597 win = XCreateWindow(top_level->display,
598 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
599 top_level->default_depth, InputOutput, top_level->vis, mask,
601 top_level->add_popup(this);
604 if(window_type == SUB_WINDOW)
606 mask = CWEventMask | CWBackPixel | CWCursor;
607 attr.event_mask = DEFAULT_EVENT_MASKS;
608 attr.background_pixel = top_level->get_color(this->bg_color);
609 if(top_level->is_hourglass)
610 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
612 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
613 win = XCreateWindow(top_level->display,
614 parent_window->win, this->x, this->y, this->w, this->h, 0,
615 top_level->default_depth, InputOutput, top_level->vis, mask,
618 if(!hidden) XMapWindow(top_level->display, win);
621 // Create pixmap for all windows
622 pixmap = new BC_Pixmap(this, this->w, this->h);
624 // Set up options for main window
625 if(window_type == MAIN_WINDOW)
627 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
629 this->bg_pixmap = new BC_Pixmap(this,
630 get_resources()->bg_image,
634 if(!hidden) show_window();
638 draw_background(0, 0, this->w, this->h);
640 flash(-1, -1, -1, -1, 0);
642 // Set up options for popup window
643 #ifdef HAVE_LIBXXF86VM
644 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
646 if(window_type == POPUP_WINDOW)
650 if(!hidden) show_window();
652 get_resources()->create_window_lock->unlock();
653 if(need_lock) unlock_window();
658 Display* BC_WindowBase::init_display(const char *display_name)
662 if(display_name && display_name[0] == 0) display_name = NULL;
663 if((display = XOpenDisplay(display_name)) == NULL) {
664 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
666 if(getenv("DISPLAY") == NULL) {
667 printf(_("'DISPLAY' environment variable not set.\n"));
671 // Try again with default display.
672 if((display = XOpenDisplay(0)) == NULL) {
673 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
681 Display* BC_WindowBase::get_display()
683 return top_level->display;
686 int BC_WindowBase::get_screen()
688 return top_level->screen;
691 int BC_WindowBase::run_window()
697 // Events may have been sent before run_window so can't initialize them here.
700 set_repeat(get_resources()->tooltip_delay);
701 BC_Display::display_global->new_window(this);
703 // If the first window created, run the display loop in this thread.
704 if(BC_Display::display_global->is_first(this))
706 BC_Display::unlock_display();
707 BC_Display::display_global->loop();
711 BC_Display::unlock_display();
712 completion_lock->lock("BC_WindowBase::run_window");
715 BC_Display::lock_display("BC_WindowBase::run_window");
716 BC_Display::display_global->delete_window(this);
718 unset_all_repeaters();
720 BC_Display::unlock_display();
722 #else // SINGLE_THREAD
727 set_repeat(get_resources()->tooltip_delay);
729 // Start X server events
730 event_thread = new BC_WindowEvents(this);
731 event_thread->start();
737 // Handle common events
743 unset_all_repeaters();
747 event_condition->reset();
748 common_events.remove_all_objects();
752 #endif // SINGLE_THREAD
757 int BC_WindowBase::get_key_masks(unsigned int key_state)
759 // printf("BC_WindowBase::get_key_masks %llx\n",
760 // event->xkey.state);
761 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
762 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
763 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
768 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
770 BC_KeyboardHandlerLock set;
771 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
774 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
776 BC_KeyboardHandlerLock set;
777 int i = BC_KeyboardHandler::listeners.size();
778 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
779 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
782 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
784 int result = (win->*handler)(wp);
788 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
791 BC_KeyboardHandlerLock set;
792 for( int i=0; !result && i<listeners.size(); ++i ) {
793 BC_KeyboardHandler *listener = listeners[i];
794 result = listener->run_event(wp);
799 void BC_KeyboardHandler::kill_grabs()
801 BC_KeyboardHandlerLock set;
802 for( int i=0; i<listeners.size(); ++i ) {
803 BC_WindowBase *win = listeners[i]->win;
804 if( win->get_window_type() != POPUP_WINDOW ) continue;
805 ((BC_Popup *)win)->ungrab_keyboard();
809 void BC_ActiveBitmaps::reque(XEvent *event)
811 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
812 ShmSeg shmseg = shm_ev->shmseg;
813 Drawable drawable = shm_ev->drawable;
814 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
815 active_lock.lock("BC_BitmapImage::reque");
816 BC_BitmapImage *bfr = first;
817 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
818 if( bfr && bfr->drawable == drawable )
820 active_lock.unlock();
822 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
823 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
824 // (int)shmseg, (int)drawable);
827 if( bfr->drawable != drawable ) return;
828 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
829 bfr->bitmap->reque(bfr);
832 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
834 active_lock.lock("BC_BitmapImage::insert");
835 bfr->drawable = pixmap;
837 active_lock.unlock();
840 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
842 active_lock.lock("BC_ActiveBitmaps::remove");
843 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
845 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
846 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
848 active_lock.unlock();
851 BC_ActiveBitmaps::BC_ActiveBitmaps()
855 BC_ActiveBitmaps::~BC_ActiveBitmaps()
861 int BC_WindowBase::keysym_lookup(XEvent *event)
863 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
864 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
866 if( event->xany.send_event && !event->xany.serial ) {
867 keysym = (KeySym) event->xkey.keycode;
868 keys_return[0] = keysym;
871 wkey_string_length = 0;
873 if( input_context ) {
874 wkey_string_length = XwcLookupString(input_context,
875 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
876 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
877 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
880 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
881 keys_return, KEYPRESSLEN, &keysym, &stat);
882 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
883 if( stat == XLookupBoth ) return ret;
884 if( stat == XLookupKeySym ) return 0;
886 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
887 wkey_string_length = ret;
888 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
892 pthread_t locking_task = (pthread_t)-1L;
893 int locking_event = -1;
894 int locking_message = -1;
896 int BC_WindowBase::dispatch_event(XEvent *event)
900 XClientMessageEvent *ptr;
901 int cancel_resize, cancel_translation;
906 #ifndef SINGLE_THREAD
907 // If an event is waiting get it, otherwise
908 // wait for next event only if there are no compressed events.
909 if(get_event_count() ||
910 (!motion_events && !resize_events && !translation_events))
913 // Lock out window deletions
914 lock_window("BC_WindowBase::dispatch_event 1");
915 locking_event = event->type;
916 locking_task = pthread_self();
917 locking_message = event->xclient.message_type;
920 // Handle compressed events
922 lock_window("BC_WindowBase::dispatch_event 2");
924 dispatch_resize_event(last_resize_w, last_resize_h);
926 dispatch_motion_event();
927 if(translation_events)
928 dispatch_translation_event();
939 //static const char *event_names[] = {
940 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
941 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
942 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
943 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
944 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
945 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
946 // "GenericEvent", "LASTEvent",
950 //if(event->type != ClientMessage) {
951 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
952 //__LINE__, title, event, event->type,
953 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
954 // event_names[event->type] : "Unknown");
960 switch(event->type) {
962 // Clear the resize buffer
963 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
964 // Clear the motion buffer since this can clear the window
967 dispatch_motion_event();
970 ptr = (XClientMessageEvent*)event;
973 if(ptr->message_type == ProtoXAtom &&
974 (Atom)ptr->data.l[0] == DelWinXAtom)
979 if(ptr->message_type == RepeaterXAtom)
981 dispatch_repeat_event(ptr->data.l[0]);
982 // Make sure the repeater still exists.
983 // for(int i = 0; i < repeaters.total; i++)
985 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
987 // dispatch_repeat_event_master(ptr->data.l[0]);
993 if(ptr->message_type == SetDoneXAtom)
997 { // We currently use X marshalling for xatom events, we can switch to something else later
998 recieve_custom_xatoms((xatom_event *)ptr);
1004 dispatch_focus_in();
1009 dispatch_focus_out();
1021 get_key_masks(event->xbutton.state);
1022 cursor_x = event->xbutton.x;
1023 cursor_y = event->xbutton.y;
1024 button_number = event->xbutton.button;
1026 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1027 event_win = event->xany.window;
1028 if (button_number < 6)
1030 if(button_number < 4)
1032 button_pressed = event->xbutton.button;
1033 button_time1 = button_time2;
1034 button_time2 = button_time3;
1035 button_time3 = event->xbutton.time;
1038 drag_win = event_win;
1039 drag_x1 = cursor_x - get_resources()->drag_radius;
1040 drag_x2 = cursor_x + get_resources()->drag_radius;
1041 drag_y1 = cursor_y - get_resources()->drag_radius;
1042 drag_y2 = cursor_y + get_resources()->drag_radius;
1044 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1047 button_time3 = button_time2 = button_time1 = 0;
1049 if((long)(button_time3 - button_time2) < resources.double_click)
1052 // button_time3 = button_time2 = button_time1 = 0;
1060 dispatch_button_press();
1065 get_key_masks(event->xbutton.state);
1066 button_number = event->xbutton.button;
1067 event_win = event->xany.window;
1068 if (button_number < 6)
1070 if(button_number < 4)
1072 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1074 dispatch_button_release();
1079 event_win = event->xany.window;
1080 dispatch_expose_event();
1084 get_key_masks(event->xmotion.state);
1085 // Dispatch previous motion event if this is a subsequent motion from a different window
1086 if(motion_events && last_motion_win != event->xany.window)
1088 dispatch_motion_event();
1091 // Buffer the current motion
1093 last_motion_state = event->xmotion.state;
1094 last_motion_x = event->xmotion.x;
1095 last_motion_y = event->xmotion.y;
1096 last_motion_win = event->xany.window;
1099 case ConfigureNotify:
1100 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1102 // event->xany.window,
1105 XTranslateCoordinates(top_level->display,
1113 last_resize_w = event->xconfigure.width;
1114 last_resize_h = event->xconfigure.height;
1117 cancel_translation = 0;
1119 // Resize history prevents responses to recursive resize requests
1120 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1122 if(resize_history.values[i]->w == last_resize_w &&
1123 resize_history.values[i]->h == last_resize_h)
1125 delete resize_history.values[i];
1126 resize_history.remove_number(i);
1131 if(last_resize_w == w && last_resize_h == h)
1139 if((last_translate_x == x && last_translate_y == y))
1140 cancel_translation = 1;
1142 if(!cancel_translation)
1144 translation_events = 1;
1147 translation_count++;
1151 get_key_masks(event->xkey.state);
1152 keys_return[0] = 0; keysym = -1;
1153 if(XFilterEvent(event, win)) {
1156 if( keysym_lookup(event) < 0 ) {
1157 printf("keysym %x\n", (uint32_t)keysym);
1161 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1165 // block out control keys
1166 if(keysym > 0xffe0 && keysym < 0xffff) break;
1167 // block out Alt_GR key
1168 if(keysym == 0xfe03) break;
1171 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1173 #ifdef X_HAVE_UTF8_STRING
1174 //It's Ascii or UTF8?
1175 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1176 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1178 if( ((keys_return[1] & 0xff) > 0x80) &&
1179 ((keys_return[0] & 0xff) > 0xC0) ) {
1180 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1181 key_pressed = keysym & 0xff;
1187 // block out extra keys
1197 // Translate key codes
1198 case XK_Return: key_pressed = RETURN; break;
1199 case XK_Up: key_pressed = UP; break;
1200 case XK_Down: key_pressed = DOWN; break;
1201 case XK_Left: key_pressed = LEFT; break;
1202 case XK_Right: key_pressed = RIGHT; break;
1203 case XK_Next: key_pressed = PGDN; break;
1204 case XK_Prior: key_pressed = PGUP; break;
1205 case XK_BackSpace: key_pressed = BACKSPACE; break;
1206 case XK_Escape: key_pressed = ESC; break;
1209 key_pressed = LEFTTAB;
1213 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1214 case XK_underscore: key_pressed = '_'; break;
1215 case XK_asciitilde: key_pressed = '~'; break;
1216 case XK_Delete: key_pressed = DELETE; break;
1217 case XK_Home: key_pressed = HOME; break;
1218 case XK_End: key_pressed = END; break;
1221 case XK_KP_Enter: key_pressed = KPENTER; break;
1222 case XK_KP_Add: key_pressed = KPPLUS; break;
1223 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1224 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1225 case XK_KP_Divide: key_pressed = KPSLASH; break;
1227 case XK_KP_End: key_pressed = KP1; break;
1229 case XK_KP_Down: key_pressed = KP2; break;
1231 case XK_KP_Page_Down: key_pressed = KP3; break;
1233 case XK_KP_Left: key_pressed = KP4; break;
1235 case XK_KP_Begin: key_pressed = KP5; break;
1237 case XK_KP_Right: key_pressed = KP6; break;
1239 case XK_KP_Home: key_pressed = KP7; break;
1241 case XK_KP_Up: key_pressed = KP8; break;
1243 case XK_KP_Page_Up: key_pressed = KP9; break;
1245 case XK_KP_Insert: key_pressed = KPINS; break;
1247 case XK_KP_Delete: key_pressed = KPDEL; break;
1248 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1250 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1251 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1252 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1253 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1254 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1255 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1256 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1257 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1258 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1259 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1260 // above case XK_Left: key_pressed = LEFT; break; /* left */
1261 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1262 // above case XK_Down: key_pressed = DOWN; break; /* down */
1263 // above case XK_Up: key_pressed = UP; break; /* up */
1264 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1265 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1266 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1267 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1268 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1269 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1270 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1271 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1272 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1273 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1274 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1275 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1278 key_pressed = keysym & 0xff;
1279 #ifdef X_HAVE_UTF8_STRING
1280 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1285 #ifdef X_HAVE_UTF8_STRING
1287 key_pressed_utf8 = keys_return;
1292 if( top_level == this )
1293 result = BC_KeyboardHandler::run_listeners(this);
1295 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1297 result = dispatch_keypress_event();
1298 // Handle some default keypresses
1301 if(key_pressed == 'w' ||
1310 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1311 dispatch_keyrelease_event();
1312 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1313 // keysym, event->xkey.state);
1317 event_win = event->xany.window;
1318 dispatch_cursor_leave();
1322 event_win = event->xany.window;
1323 cursor_x = event->xcrossing.x;
1324 cursor_y = event->xcrossing.y;
1325 dispatch_cursor_enter();
1330 //printf("100 %s %p %d\n", title, event, event->type);
1331 //if(event->type != ClientMessage) dump();
1333 #ifndef SINGLE_THREAD
1335 if(event) delete event;
1337 // if(done) completion_lock->unlock();
1340 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1344 int BC_WindowBase::dispatch_expose_event()
1347 for(int i = 0; i < subwindows->total && !result; i++)
1349 result = subwindows->values[i]->dispatch_expose_event();
1352 // Propagate to user
1353 if(!result) expose_event();
1357 int BC_WindowBase::dispatch_resize_event(int w, int h)
1359 // Can't store new w and h until the event is handles
1360 // because bcfilebox depends on the old w and h to
1361 // reposition widgets.
1362 if( window_type == MAIN_WINDOW ) {
1367 pixmap = new BC_Pixmap(this, w, h);
1368 clear_box(0, 0, w, h);
1371 // Propagate to subwindows
1372 for(int i = 0; i < subwindows->total; i++) {
1373 subwindows->values[i]->dispatch_resize_event(w, h);
1376 // Propagate to user
1379 if( window_type == MAIN_WINDOW ) {
1388 int BC_WindowBase::dispatch_flash()
1391 for(int i = 0; i < subwindows->total; i++)
1392 subwindows->values[i]->dispatch_flash();
1396 int BC_WindowBase::dispatch_translation_event()
1398 translation_events = 0;
1399 if(window_type == MAIN_WINDOW)
1403 x = last_translate_x;
1404 y = last_translate_y;
1405 // Correct for window manager offsets
1410 for(int i = 0; i < subwindows->total; i++)
1412 subwindows->values[i]->dispatch_translation_event();
1415 translation_event();
1419 int BC_WindowBase::dispatch_motion_event()
1424 if(top_level == this)
1427 event_win = last_motion_win;
1428 get_key_masks(last_motion_state);
1431 if(get_button_down() && !active_menubar && !active_popup_menu)
1435 cursor_x = last_motion_x;
1436 cursor_y = last_motion_y;
1437 result = dispatch_drag_motion();
1441 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1442 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1447 result = dispatch_drag_start();
1451 cursor_x = last_motion_x;
1452 cursor_y = last_motion_y;
1454 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1457 // active_popup_menu,
1458 // active_subwindow);
1460 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1461 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1462 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1465 // Dispatch in stacking order
1466 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1468 result = subwindows->values[i]->dispatch_motion_event();
1471 if(!result) result = cursor_motion_event(); // give to user
1475 int BC_WindowBase::dispatch_keypress_event()
1478 if(top_level == this)
1480 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1483 for(int i = 0; i < subwindows->total && !result; i++)
1485 result = subwindows->values[i]->dispatch_keypress_event();
1488 if(!result) result = keypress_event();
1493 int BC_WindowBase::dispatch_keyrelease_event()
1496 if(top_level == this)
1498 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1501 for(int i = 0; i < subwindows->total && !result; i++)
1503 result = subwindows->values[i]->dispatch_keyrelease_event();
1506 if(!result) result = keyrelease_event();
1511 int BC_WindowBase::dispatch_focus_in()
1513 for(int i = 0; i < subwindows->total; i++)
1515 subwindows->values[i]->dispatch_focus_in();
1523 int BC_WindowBase::dispatch_focus_out()
1525 for(int i = 0; i < subwindows->total; i++)
1527 subwindows->values[i]->dispatch_focus_out();
1535 int BC_WindowBase::get_has_focus()
1537 return top_level->has_focus;
1540 int BC_WindowBase::get_deleting()
1542 if(is_deleting) return 1;
1543 if(parent_window && parent_window->get_deleting()) return 1;
1547 int BC_WindowBase::dispatch_button_press()
1552 if(top_level == this)
1554 if(active_menubar) result = active_menubar->dispatch_button_press();
1555 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1556 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1559 for(int i = 0; i < subwindows->total && !result; i++)
1561 result = subwindows->values[i]->dispatch_button_press();
1564 if(!result) result = button_press_event();
1570 int BC_WindowBase::dispatch_button_release()
1573 if(top_level == this)
1575 if(active_menubar) result = active_menubar->dispatch_button_release();
1576 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1577 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1578 if(!result && button_number != 4 && button_number != 5)
1579 result = dispatch_drag_stop();
1582 for(int i = 0; i < subwindows->total && !result; i++)
1584 result = subwindows->values[i]->dispatch_button_release();
1589 result = button_release_event();
1596 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1599 // all repeat event handlers get called and decide based on activity and duration
1600 // whether to respond
1601 for(int i = 0; i < subwindows->total; i++)
1603 subwindows->values[i]->dispatch_repeat_event(duration);
1607 repeat_event(duration);
1611 // Unlock next repeat signal
1612 if(window_type == MAIN_WINDOW)
1614 #ifdef SINGLE_THREAD
1615 BC_Display::display_global->unlock_repeaters(duration);
1617 for(int i = 0; i < repeaters.total; i++)
1619 if(repeaters.values[i]->delay == duration)
1621 repeaters.values[i]->repeat_lock->unlock();
1629 void BC_WindowBase::unhide_cursor()
1634 if(top_level->is_hourglass)
1635 set_cursor(HOURGLASS_CURSOR, 1, 0);
1637 set_cursor(current_cursor, 1, 0);
1639 cursor_timer->update();
1643 void BC_WindowBase::update_video_cursor()
1645 if(video_on && !is_transparent)
1647 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1650 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1651 cursor_timer->update();
1656 cursor_timer->update();
1661 int BC_WindowBase::dispatch_cursor_leave()
1665 for(int i = 0; i < subwindows->total; i++)
1667 subwindows->values[i]->dispatch_cursor_leave();
1670 cursor_leave_event();
1674 int BC_WindowBase::dispatch_cursor_enter()
1680 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1681 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1682 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1684 for(int i = 0; !result && i < subwindows->total; i++)
1686 result = subwindows->values[i]->dispatch_cursor_enter();
1689 if(!result) result = cursor_enter_event();
1693 int BC_WindowBase::cursor_enter_event()
1698 int BC_WindowBase::cursor_leave_event()
1703 int BC_WindowBase::close_event()
1709 int BC_WindowBase::dispatch_drag_start()
1712 if(active_menubar) result = active_menubar->dispatch_drag_start();
1713 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1714 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1716 for(int i = 0; i < subwindows->total && !result; i++)
1718 result = subwindows->values[i]->dispatch_drag_start();
1721 if(!result) result = is_dragging = drag_start_event();
1725 int BC_WindowBase::dispatch_drag_stop()
1729 for(int i = 0; i < subwindows->total && !result; i++)
1731 result = subwindows->values[i]->dispatch_drag_stop();
1734 if(is_dragging && !result)
1744 int BC_WindowBase::dispatch_drag_motion()
1747 for(int i = 0; i < subwindows->total && !result; i++)
1749 result = subwindows->values[i]->dispatch_drag_motion();
1752 if(is_dragging && !result)
1754 drag_motion_event();
1765 int BC_WindowBase::show_tooltip(int w, int h)
1769 if(tooltip_text && !tooltip_on &&
1770 (force_tooltip || get_resources()->tooltips_enabled))
1773 top_level->hide_tooltip();
1777 w = get_text_width(MEDIUMFONT, tooltip_text);
1780 h = get_text_height(MEDIUMFONT, tooltip_text);
1782 w += TOOLTIP_MARGIN * 2;
1783 h += TOOLTIP_MARGIN * 2;
1785 XTranslateCoordinates(top_level->display, win,
1786 top_level->rootwin, get_w(), get_h(),
1788 // keep the tip inside the window/display
1789 int top_x = top_level->get_x();
1790 if( x < top_x ) x = top_x;
1791 int top_w = top_x + top_level->get_w();
1792 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1793 if( top_w < lmt_w ) lmt_w = top_w;
1794 if( x+w > lmt_w ) x = lmt_w-w;
1796 int top_y = top_level->get_y();
1797 if( y < top_y ) y = top_y;
1798 int top_h = top_y + top_level->get_h();
1799 int lmt_h = top_level->get_root_h(0);
1800 if( top_h < lmt_h ) lmt_h = top_h;
1801 if( y+h > lmt_h ) y = lmt_h-h;
1803 int abs_x, abs_y, win_x, win_y;
1804 unsigned int temp_mask;
1806 XQueryPointer(top_level->display, top_level->win,
1807 &temp_win, &temp_win, &abs_x, &abs_y,
1808 &win_x, &win_y, &temp_mask);
1809 // check for cursor inside popup
1810 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1812 if( x-abs_x < y-abs_y )
1817 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1818 get_resources()->tooltip_bg_color);
1821 tooltip_popup->set_font(MEDIUMFONT);
1822 tooltip_popup->flash();
1823 tooltip_popup->flush();
1828 int BC_WindowBase::hide_tooltip()
1831 for(int i = 0; i < subwindows->total; i++)
1833 subwindows->values[i]->hide_tooltip();
1839 delete tooltip_popup;
1845 const char *BC_WindowBase::get_tooltip()
1847 return tooltip_text;
1850 int BC_WindowBase::set_tooltip(const char *text)
1852 tooltip_text = text;
1854 // Update existing tooltip if it is visible
1858 tooltip_popup->flash();
1863 // signal the event handler to repeat
1864 int BC_WindowBase::set_repeat(int64_t duration)
1868 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1871 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1873 #ifdef SINGLE_THREAD
1874 BC_Display::display_global->set_repeat(this, duration);
1876 // test repeater database for duplicates
1877 for(int i = 0; i < repeaters.total; i++)
1880 if(repeaters.values[i]->delay == duration)
1882 repeaters.values[i]->start_repeating(this);
1887 BC_Repeater *repeater = new BC_Repeater(this, duration);
1888 repeater->initialize();
1889 repeaters.append(repeater);
1890 repeater->start_repeating();
1895 int BC_WindowBase::unset_repeat(int64_t duration)
1897 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1899 #ifdef SINGLE_THREAD
1900 BC_Display::display_global->unset_repeat(this, duration);
1902 for(int i = 0; i < repeaters.total; i++)
1904 if(repeaters.values[i]->delay == duration)
1906 repeaters.values[i]->stop_repeating();
1914 int BC_WindowBase::unset_all_repeaters()
1916 #ifdef SINGLE_THREAD
1917 BC_Display::display_global->unset_all_repeaters(this);
1919 for(int i = 0; i < repeaters.total; i++)
1921 repeaters.values[i]->stop_repeating();
1923 repeaters.remove_all_objects();
1928 // long BC_WindowBase::get_repeat_id()
1930 // return top_level->next_repeat_id++;
1933 XEvent *BC_WindowBase::new_xevent()
1935 XEvent *event = new XEvent;
1936 memset(event, 0, sizeof(*event));
1940 #ifndef SINGLE_THREAD
1941 int BC_WindowBase::arm_repeat(int64_t duration)
1943 XEvent *event = new_xevent();
1944 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1945 ptr->type = ClientMessage;
1946 ptr->message_type = RepeaterXAtom;
1948 ptr->data.l[0] = duration;
1950 // Couldn't use XSendEvent since it locked up randomly.
1956 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1961 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1963 #ifndef SINGLE_THREAD
1964 XEvent *myevent = new_xevent();
1965 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1966 ptr->type = ClientMessage;
1967 ptr->message_type = event->message_type;
1968 ptr->format = event->format;
1969 ptr->data.l[0] = event->data.l[0];
1970 ptr->data.l[1] = event->data.l[1];
1971 ptr->data.l[2] = event->data.l[2];
1972 ptr->data.l[3] = event->data.l[3];
1973 ptr->data.l[4] = event->data.l[4];
1982 Atom BC_WindowBase::create_xatom(const char *atom_name)
1984 return XInternAtom(display, atom_name, False);
1987 int BC_WindowBase::get_atoms()
1989 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1990 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1991 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
1992 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1993 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
1994 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
1995 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2001 void BC_WindowBase::init_cursors()
2003 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2004 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2005 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2006 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2007 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2008 move_cursor = XCreateFontCursor(display, XC_fleur);
2009 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2010 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2011 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2012 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2013 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2014 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2015 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2016 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2019 char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2020 Colormap colormap = DefaultColormap(display, screen);
2021 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2022 rootwin, cursor_data, 8, 8);
2023 XColor black, dummy;
2024 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2025 transparent_cursor = XCreatePixmapCursor(display,
2026 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2027 // XDefineCursor(display, win, transparent_cursor);
2028 XFreePixmap(display, pixmap_bottom);
2031 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2033 int color_model = BC_TRANSPARENCY;
2037 color_model = BC_RGB8;
2040 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2043 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2046 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2052 int BC_WindowBase::init_colors()
2055 current_color_value = current_color_pixel = 0;
2057 // Get the real depth
2060 ximage = XCreateImage(top_level->display,
2062 top_level->default_depth,
2070 bits_per_pixel = ximage->bits_per_pixel;
2071 XDestroyImage(ximage);
2073 color_model = evaluate_color_model(client_byte_order,
2076 // Get the color model
2082 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2083 create_private_colors();
2087 cmap = DefaultColormap(display, screen);
2088 create_shared_colors();
2091 allocate_color_table();
2095 //cmap = DefaultColormap(display, screen);
2096 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2102 int BC_WindowBase::create_private_colors()
2107 for(int i = 0; i < 255; i++)
2109 color = (i & 0xc0) << 16;
2110 color += (i & 0x38) << 10;
2111 color += (i & 0x7) << 5;
2112 color_table[i][0] = color;
2114 create_shared_colors(); // overwrite the necessary colors on the table
2119 int BC_WindowBase::create_color(int color)
2121 if(total_colors == 256)
2123 // replace the closest match with an exact match
2124 color_table[get_color_rgb8(color)][0] = color;
2128 // add the color to the table
2129 color_table[total_colors][0] = color;
2135 int BC_WindowBase::create_shared_colors()
2137 create_color(BLACK);
2138 create_color(WHITE);
2140 create_color(LTGREY);
2141 create_color(MEGREY);
2142 create_color(MDGREY);
2143 create_color(DKGREY);
2145 create_color(LTCYAN);
2146 create_color(MECYAN);
2147 create_color(MDCYAN);
2148 create_color(DKCYAN);
2150 create_color(LTGREEN);
2151 create_color(GREEN);
2152 create_color(DKGREEN);
2154 create_color(LTPINK);
2158 create_color(LTBLUE);
2160 create_color(DKBLUE);
2162 create_color(LTYELLOW);
2163 create_color(MEYELLOW);
2164 create_color(MDYELLOW);
2165 create_color(DKYELLOW);
2167 create_color(LTPURPLE);
2168 create_color(MEPURPLE);
2169 create_color(MDPURPLE);
2170 create_color(DKPURPLE);
2172 create_color(FGGREY);
2173 create_color(MNBLUE);
2174 create_color(ORANGE);
2175 create_color(FTGREY);
2180 int BC_WindowBase::allocate_color_table()
2182 int red, green, blue, color;
2185 for(int i = 0; i < total_colors; i++)
2187 color = color_table[i][0];
2188 red = (color & 0xFF0000) >> 16;
2189 green = (color & 0x00FF00) >> 8;
2190 blue = color & 0xFF;
2192 col.flags = DoRed | DoGreen | DoBlue;
2193 col.red = red<<8 | red;
2194 col.green = green<<8 | green;
2195 col.blue = blue<<8 | blue;
2197 XAllocColor(display, cmap, &col);
2198 color_table[i][1] = col.pixel;
2201 XInstallColormap(display, cmap);
2205 int BC_WindowBase::init_window_shape()
2207 if(bg_pixmap && bg_pixmap->use_alpha())
2209 XShapeCombineMask(top_level->display,
2210 this->win, ShapeBounding, 0, 0,
2211 bg_pixmap->get_alpha(), ShapeSet);
2217 int BC_WindowBase::init_gc()
2219 unsigned long gcmask;
2220 gcmask = GCFont | GCGraphicsExposures;
2223 gcvalues.font = mediumfont->fid; // set the font
2224 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2225 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2227 // gcmask = GCCapStyle | GCJoinStyle;
2228 // XGetGCValues(display, gc, gcmask, &gcvalues);
2229 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2233 int BC_WindowBase::init_fonts()
2235 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2236 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2237 smallfont = XLoadQueryFont(display, "fixed");
2238 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2239 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2240 mediumfont = XLoadQueryFont(display, "fixed");
2241 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2242 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2243 largefont = XLoadQueryFont(display, "fixed");
2244 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2245 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2246 bigfont = XLoadQueryFont(display, "fixed");
2249 if(get_resources()->use_fontset)
2254 // FIXME: should check the m,d,n values
2255 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2257 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2258 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2259 if( !mediumfontset )
2260 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2261 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2263 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2264 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2266 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2267 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2268 curr_fontset = mediumfontset;
2269 get_resources()->use_fontset = 1;
2273 get_resources()->use_fontset = 0;
2280 void BC_WindowBase::init_xft()
2283 if( !get_resources()->use_xft ) return;
2284 if(!(smallfont_xft =
2285 (resources.small_font_xft[0] == '-' ?
2286 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2287 XftFontOpenName(display, screen, resources.small_font_xft))) )
2288 if(!(smallfont_xft =
2289 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2290 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2291 if(!(mediumfont_xft =
2292 (resources.medium_font_xft[0] == '-' ?
2293 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2294 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2295 if(!(mediumfont_xft =
2296 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2297 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2298 if(!(largefont_xft =
2299 (resources.large_font_xft[0] == '-' ?
2300 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2301 XftFontOpenName(display, screen, resources.large_font_xft))) )
2302 if(!(largefont_xft =
2303 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2304 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2306 (resources.big_font_xft[0] == '-' ?
2307 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2308 XftFontOpenName(display, screen, resources.big_font_xft))) )
2310 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2311 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2313 if(!(bold_smallfont_xft =
2314 (resources.small_b_font_xft[0] == '-' ?
2315 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2316 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2317 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2318 if(!(bold_mediumfont_xft =
2319 (resources.medium_b_font_xft[0] == '-' ?
2320 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2321 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2322 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2323 if(!(bold_largefont_xft =
2324 (resources.large_b_font_xft[0] == '-' ?
2325 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2326 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2327 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2329 // Extension failed to locate fonts
2330 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2331 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2332 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2333 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2334 resources.small_font_xft, smallfont_xft,
2335 resources.medium_font_xft, mediumfont_xft,
2336 resources.large_font_xft, largefont_xft,
2337 resources.big_font_xft, bigfont_xft,
2338 resources.small_b_font_xft, bold_smallfont_xft,
2339 resources.medium_b_font_xft, bold_mediumfont_xft,
2340 resources.large_b_font_xft, bold_largefont_xft);
2341 get_resources()->use_xft = 0;
2347 void BC_WindowBase::init_im()
2349 XIMStyles *xim_styles;
2352 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2354 printf("BC_WindowBase::init_im: Could not open input method.\n");
2357 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2360 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2361 XCloseIM(input_method);
2366 for(int z = 0; z < xim_styles->count_styles; z++)
2368 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2370 xim_style = xim_styles->supported_styles[z];
2378 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2379 XCloseIM(input_method);
2383 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2384 XNClientWindow, win, XNFocusWindow, win, NULL);
2387 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2388 XCloseIM(input_method);
2393 void BC_WindowBase::finit_im()
2395 if( input_context ) {
2396 XDestroyIC(input_context);
2399 if( input_method ) {
2400 XCloseIM(input_method);
2406 int BC_WindowBase::get_color(int64_t color)
2408 // return pixel of color
2409 // use this only for drawing subwindows not for bitmaps
2410 int i, test, difference;
2416 return get_color_rgb8(color);
2417 // test last color looked up
2418 if(current_color_value == color)
2419 return current_color_pixel;
2422 current_color_value = color;
2423 for(i = 0; i < total_colors; i++)
2425 if(color_table[i][0] == color)
2427 current_color_pixel = color_table[i][1];
2428 return current_color_pixel;
2432 // find nearest match
2433 difference = 0xFFFFFF;
2435 for(i = 0; i < total_colors; i++)
2437 test = abs((int)(color_table[i][0] - color));
2439 if(test < difference)
2441 current_color_pixel = color_table[i][1];
2445 return current_color_pixel;
2448 return get_color_rgb16(color);
2451 return get_color_bgr16(color);
2455 return client_byte_order == server_byte_order ?
2456 color : get_color_bgr24(color);
2464 int BC_WindowBase::get_color_rgb8(int color)
2468 pixel = (color & 0xc00000) >> 16;
2469 pixel += (color & 0xe000) >> 10;
2470 pixel += (color & 0xe0) >> 5;
2474 int64_t BC_WindowBase::get_color_rgb16(int color)
2477 result = (color & 0xf80000) >> 8;
2478 result += (color & 0xfc00) >> 5;
2479 result += (color & 0xf8) >> 3;
2484 int64_t BC_WindowBase::get_color_bgr16(int color)
2487 result = (color & 0xf80000) >> 19;
2488 result += (color & 0xfc00) >> 5;
2489 result += (color & 0xf8) << 8;
2494 int64_t BC_WindowBase::get_color_bgr24(int color)
2497 result = (color & 0xff) << 16;
2498 result += (color & 0xff00);
2499 result += (color & 0xff0000) >> 16;
2503 void BC_WindowBase::start_video()
2505 cursor_timer->update();
2507 // set_color(BLACK);
2508 // draw_box(0, 0, get_w(), get_h());
2512 void BC_WindowBase::stop_video()
2520 int64_t BC_WindowBase::get_color()
2522 return top_level->current_color;
2525 void BC_WindowBase::set_color(int64_t color)
2527 top_level->current_color = color;
2528 XSetForeground(top_level->display,
2530 top_level->get_color(color));
2533 void BC_WindowBase::set_opaque()
2535 XSetFunction(top_level->display, top_level->gc, GXcopy);
2538 void BC_WindowBase::set_inverse()
2540 XSetFunction(top_level->display, top_level->gc, GXxor);
2543 void BC_WindowBase::set_line_width(int value)
2545 this->line_width = value;
2546 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2547 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2548 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2549 JoinMiter); /* join_style */
2551 if(line_dashes > 0) {
2552 const char dashes = line_dashes;
2553 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2556 // XGCValues gcvalues;
2557 // unsigned long gcmask;
2558 // gcmask = GCCapStyle | GCJoinStyle;
2559 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2560 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2563 void BC_WindowBase::set_line_dashes(int value)
2565 line_dashes = value;
2566 // call XSetLineAttributes
2567 set_line_width(line_width);
2571 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2575 case ARROW_CURSOR: return top_level->arrow_cursor; break;
2576 case CROSS_CURSOR: return top_level->cross_cursor;
2577 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
2578 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
2579 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
2580 case MOVE_CURSOR: return top_level->move_cursor; break;
2581 case LEFT_CURSOR: return top_level->left_cursor; break;
2582 case RIGHT_CURSOR: return top_level->right_cursor; break;
2583 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
2584 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
2585 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
2586 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
2587 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
2588 case HOURGLASS_CURSOR: return top_level->hourglass_cursor; break;
2589 case TRANSPARENT_CURSOR: return top_level->transparent_cursor; break;
2594 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2596 // inherit cursor from parent
2599 XUndefineCursor(top_level->display, win);
2600 current_cursor = cursor;
2603 // don't change cursor if overridden
2604 if((!top_level->is_hourglass && !is_transparent) ||
2607 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2608 if(flush) this->flush();
2611 if(!override) current_cursor = cursor;
2614 void BC_WindowBase::set_x_cursor(int cursor)
2616 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2617 XDefineCursor(top_level->display, win, temp_cursor);
2618 current_cursor = cursor;
2622 int BC_WindowBase::get_cursor()
2624 return current_cursor;
2627 void BC_WindowBase::start_hourglass()
2629 top_level->start_hourglass_recursive();
2633 void BC_WindowBase::stop_hourglass()
2635 top_level->stop_hourglass_recursive();
2639 void BC_WindowBase::start_hourglass_recursive()
2641 if(this == top_level)
2649 set_cursor(HOURGLASS_CURSOR, 1, 0);
2650 for(int i = 0; i < subwindows->total; i++)
2652 subwindows->values[i]->start_hourglass_recursive();
2657 void BC_WindowBase::stop_hourglass_recursive()
2659 if(this == top_level)
2661 if(hourglass_total == 0) return;
2662 top_level->hourglass_total--;
2665 if(!top_level->hourglass_total)
2667 top_level->is_hourglass = 0;
2669 // Cause set_cursor to perform change
2671 set_cursor(current_cursor, 1, 0);
2673 for(int i = 0; i < subwindows->total; i++)
2675 subwindows->values[i]->stop_hourglass_recursive();
2683 XFontStruct* BC_WindowBase::get_font_struct(int font)
2685 // Clear out unrelated flags
2686 if(font & BOLDFACE) font ^= BOLDFACE;
2689 case SMALLFONT: return top_level->smallfont; break;
2690 case MEDIUMFONT: return top_level->mediumfont; break;
2691 case LARGEFONT: return top_level->largefont; break;
2692 case BIGFONT: return top_level->bigfont; break;
2697 XFontSet BC_WindowBase::get_fontset(int font)
2701 if(get_resources()->use_fontset)
2703 switch(font & 0xff) {
2704 case SMALLFONT: fs = top_level->smallfontset; break;
2705 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2706 case LARGEFONT: fs = top_level->largefontset; break;
2707 case BIGFONT: fs = top_level->bigfontset; break;
2715 XftFont* BC_WindowBase::get_xft_struct(int font)
2718 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2719 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2720 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2721 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2722 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2723 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2724 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2739 int BC_WindowBase::get_current_font()
2741 return top_level->current_font;
2744 void BC_WindowBase::set_font(int font)
2746 top_level->current_font = font;
2749 if(get_resources()->use_xft) {}
2752 if(get_resources()->use_fontset) {
2756 if(get_font_struct(font))
2758 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2764 void BC_WindowBase::set_fontset(int font)
2768 if(get_resources()->use_fontset) {
2770 case SMALLFONT: fs = top_level->smallfontset; break;
2771 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2772 case LARGEFONT: fs = top_level->largefontset; break;
2773 case BIGFONT: fs = top_level->bigfontset; break;
2781 XFontSet BC_WindowBase::get_curr_fontset(void)
2783 if(get_resources()->use_fontset)
2784 return curr_fontset;
2788 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2791 if(get_resources()->use_xft && get_xft_struct(font))
2794 #ifdef X_HAVE_UTF8_STRING
2795 if(get_resources()->locale_utf8)
2797 XftTextExtentsUtf8(top_level->display,
2798 get_xft_struct(font),
2799 (const XftChar8 *)text,
2806 XftTextExtents8(top_level->display,
2807 get_xft_struct(font),
2808 (const XftChar8 *)text,
2812 return extents.xOff;
2816 if(get_resources()->use_fontset && top_level->get_fontset(font))
2817 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2819 if(get_font_struct(font))
2820 return XTextWidth(get_font_struct(font), text, length);
2826 case MEDIUM_7SEGMENT:
2827 return get_resources()->medium_7segment[0]->get_w() * length;
2837 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2840 if(get_resources()->use_xft && get_xft_struct(font)) {
2843 XftTextExtents32(top_level->display, get_xft_struct(font),
2844 (const FcChar32*)text, length, &extents);
2845 return extents.xOff;
2848 if(!get_font_struct(font)) return 0;
2849 XChar2b xtext[length], *xp = xtext;
2850 for( int i=0; i<length; ++i,++xp ) {
2851 xp->byte1 = (unsigned char) (text[i] >> 8);
2852 xp->byte2 = (unsigned char) (text[i] & 0xff);
2854 return XTextWidth16(get_font_struct(font), xtext, length);
2857 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2859 int i, j, w = 0, line_w = 0;
2860 if(length < 0) length = strlen(text);
2862 for(i = 0, j = 0; i <= length; i++)
2867 line_w = get_single_text_width(font, &text[j], i - j);
2873 line_w = get_single_text_width(font, &text[j], length - j);
2875 if(line_w > w) w = line_w;
2878 if(i > length && w == 0)
2880 w = get_single_text_width(font, text, length);
2886 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2888 int i, j, w = 0, line_w = 0;
2890 if(length < 0) length = wcslen(text);
2892 for(i = 0, j = 0; i <= length; i++)
2897 line_w = get_single_text_width(font, &text[j], i - j);
2902 line_w = get_single_text_width(font, &text[j], length - j);
2904 if(line_w > w) w = line_w;
2907 if(i > length && w == 0)
2908 w = get_single_text_width(font, text, length);
2913 int BC_WindowBase::get_text_ascent(int font)
2917 if( (fstruct = get_xft_struct(font)) != 0 )
2918 return fstruct->ascent;
2920 if(get_resources()->use_fontset && top_level->get_fontset(font))
2922 XFontSetExtents *extents;
2924 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2925 return -extents->max_logical_extent.y;
2928 if(get_font_struct(font))
2929 return top_level->get_font_struct(font)->ascent;
2932 case MEDIUM_7SEGMENT:
2933 return get_resources()->medium_7segment[0]->get_h();
2938 int BC_WindowBase::get_text_descent(int font)
2942 if( (fstruct = get_xft_struct(font)) != 0 )
2943 return fstruct->descent;
2945 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2946 XFontSetExtents *extents;
2947 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2948 return (extents->max_logical_extent.height
2949 + extents->max_logical_extent.y);
2952 if(get_font_struct(font))
2953 return top_level->get_font_struct(font)->descent;
2958 int BC_WindowBase::get_text_height(int font, const char *text)
2963 if( (fstruct = get_xft_struct(font)) != 0 )
2964 rowh = fstruct->height;
2967 rowh = get_text_ascent(font) + get_text_descent(font);
2969 if(!text) return rowh;
2971 // Add height of lines
2972 int h = 0, i, length = strlen(text);
2973 for(i = 0; i <= length; i++)
2984 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2986 if(color_model < 0) color_model = top_level->get_color_model();
2987 return new BC_Bitmap(top_level, w, h, color_model);
2990 void BC_WindowBase::init_wait()
2992 #ifndef SINGLE_THREAD
2993 if(window_type != MAIN_WINDOW)
2994 top_level->init_wait();
2995 init_lock->lock("BC_WindowBase::init_wait");
2996 init_lock->unlock();
3000 int BC_WindowBase::accel_available(int color_model, int lock_it)
3002 if( window_type != MAIN_WINDOW )
3003 return top_level->accel_available(color_model, lock_it);
3005 lock_window("BC_WindowBase::accel_available");
3007 switch(color_model) {
3009 grab_port_id(color_model);
3013 grab_port_id(color_model);
3022 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3023 return xvideo_port_id >= 0 ? 1 : 0;
3027 int BC_WindowBase::grab_port_id(int color_model)
3029 if( !get_resources()->use_xvideo || // disabled
3030 !get_resources()->use_shm ) // Only local server is fast enough.
3032 if( xvideo_port_id >= 0 )
3033 return xvideo_port_id;
3035 unsigned int ver, rev, reqBase, eventBase, errorBase;
3036 if( Success != XvQueryExtension(display, // XV extension is available
3037 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3040 // XV adaptors are available
3041 unsigned int numAdapt = 0;
3042 XvAdaptorInfo *info = 0;
3043 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3047 // Translate from color_model to X color model
3048 int x_color_model = BC_CModels::bc_to_x(color_model);
3050 // Get adaptor with desired color model
3051 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3052 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3053 // adaptor supports XvImages
3054 int numFormats = 0, numPorts = info[i].num_ports;
3055 XvImageFormatValues *formats =
3056 XvListImageFormats(display, info[i].base_id, &numFormats);
3057 if( !formats ) continue;
3059 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3060 if( formats[j].id != x_color_model ) continue;
3061 // this adaptor supports the desired format, grab a port
3062 for( int k=0; k<numPorts; ++k ) {
3063 if( Success == XvGrabPort(top_level->display,
3064 info[i].base_id+k, CurrentTime) ) {
3065 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3066 xvideo_port_id = info[i].base_id + k;
3074 XvFreeAdaptorInfo(info);
3076 return xvideo_port_id;
3080 int BC_WindowBase::show_window(int flush)
3082 for(int i = 0; i < subwindows->size(); i++)
3084 subwindows->get(i)->show_window(0);
3087 XMapWindow(top_level->display, win);
3088 if(flush) XFlush(top_level->display);
3089 // XSync(top_level->display, 0);
3094 int BC_WindowBase::hide_window(int flush)
3096 for(int i = 0; i < subwindows->size(); i++)
3098 subwindows->get(i)->hide_window(0);
3101 XUnmapWindow(top_level->display, win);
3102 if(flush) this->flush();
3107 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3109 subwindows->append((BC_SubWindow*)menu_bar);
3111 menu_bar->parent_window = this;
3112 menu_bar->top_level = this->top_level;
3113 menu_bar->initialize();
3117 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3119 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3120 if(this != top_level) return top_level->add_popup(window);
3121 popups.append(window);
3125 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3127 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3128 if(this != top_level)
3129 top_level->remove_popup(window);
3131 popups.remove(window);
3132 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3136 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3138 subwindows->append(subwindow);
3140 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3142 // parent window must be set before the subwindow initialization
3143 subwindow->parent_window = this;
3144 subwindow->top_level = this->top_level;
3146 // Execute derived initialization
3147 subwindow->initialize();
3152 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3154 return add_subwindow(subwindow);
3157 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3159 if( !top_level->flash_enabled ) return 0;
3160 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3162 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3165 XClearArea(top_level->display, win, x, y, w, h, 0);
3169 XClearWindow(top_level->display, win);
3177 int BC_WindowBase::flash(int flush)
3179 flash(-1, -1, -1, -1, flush);
3183 void BC_WindowBase::flush()
3185 //if(!get_window_lock())
3186 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3187 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3188 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3189 int locked = get_window_lock();
3190 if( !locked ) lock_window("BC_WindowBase::flush");
3191 XFlush(top_level->display);
3192 if( !locked ) unlock_window();
3195 void BC_WindowBase::sync_display()
3197 int locked = get_window_lock();
3198 if( !locked ) lock_window("BC_WindowBase::sync_display");
3199 XSync(top_level->display, False);
3200 if( !locked ) unlock_window();
3203 int BC_WindowBase::get_window_lock()
3205 #ifdef SINGLE_THREAD
3206 return BC_Display::display_global->get_display_locked();
3208 return top_level->window_lock;
3212 int BC_WindowBase::lock_window(const char *location)
3214 if(top_level && top_level != this)
3216 top_level->lock_window(location);
3221 SET_LOCK(this, title, location);
3222 #ifdef SINGLE_THREAD
3223 BC_Display::lock_display(location);
3225 XLockDisplay(top_level->display);
3226 top_level->display_lock_owner = pthread_self();
3229 ++top_level->window_lock;
3233 printf("BC_WindowBase::lock_window top_level NULL\n");
3238 int BC_WindowBase::unlock_window()
3240 if(top_level && top_level != this)
3242 top_level->unlock_window();
3249 // if(!top_level->window_lock)
3251 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3255 if( top_level->window_lock > 0 )
3256 if( --top_level->window_lock == 0 )
3257 top_level->display_lock_owner = 0;
3258 #ifdef SINGLE_THREAD
3259 BC_Display::unlock_display();
3261 XUnlockDisplay(top_level->display);
3266 printf("BC_WindowBase::unlock_window top_level NULL\n");
3271 void BC_WindowBase::set_done(int return_value)
3273 if(done_set) return;
3275 if(window_type != MAIN_WINDOW)
3276 top_level->set_done(return_value);
3279 #ifdef SINGLE_THREAD
3280 this->return_value = return_value;
3281 BC_Display::display_global->arm_completion(this);
3282 completion_lock->unlock();
3283 #else // SINGLE_THREAD
3285 if( !event_thread ) return;
3286 XEvent *event = new_xevent();
3287 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3288 event->type = ClientMessage;
3289 ptr->message_type = SetDoneXAtom;
3291 this->return_value = return_value;
3292 // May lock up here because XSendEvent doesn't work too well
3293 // asynchronous with XNextEvent.
3294 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3296 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3302 void BC_WindowBase::close(int return_value)
3304 hide_window(); flush();
3305 set_done(return_value);
3308 int BC_WindowBase::get_w()
3313 int BC_WindowBase::get_h()
3318 int BC_WindowBase::get_x()
3323 int BC_WindowBase::get_y()
3329 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3330 // this is the bounding box of all the screens
3332 int BC_WindowBase::get_root_w(int lock_display)
3334 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3335 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3336 int result = WidthOfScreen(def_screen);
3337 if(lock_display) unlock_window();
3341 int BC_WindowBase::get_root_h(int lock_display)
3343 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3344 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3345 int result = HeightOfScreen(def_screen);
3346 if(lock_display) unlock_window();
3350 XineramaScreenInfo *
3351 BC_WindowBase::get_xinerama_info(int screen)
3353 if( !xinerama_info || !xinerama_screens ) return 0;
3355 for( int i=0; i<xinerama_screens; ++i )
3356 if( xinerama_info[i].screen_number == screen )
3357 return &xinerama_info[i];
3360 int top_x = get_x(), top_y = get_y();
3361 for( int i=0; i<xinerama_screens; ++i ) {
3362 int scr_y = top_y - xinerama_info[i].y_org;
3363 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3364 int scr_x = top_x - xinerama_info[i].x_org;
3365 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3366 return &xinerama_info[i];
3371 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3374 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3375 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3378 int root_w = get_root_w(0);
3379 int root_h = get_root_h(0);
3380 // Shift X based on position of current window if dual head
3381 if( (float)root_w/root_h > 1.8 ) {
3382 root_w = get_screen_w(0, 0);
3383 if( top_level->get_x() >= root_w )
3388 result = info->x_org;
3389 if(lock_display) unlock_window();
3393 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3395 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3396 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3397 int result = !info ? 0 : info->y_org;
3398 if(lock_display) unlock_window();
3402 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3405 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3406 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3408 int width = get_root_w(0);
3409 int height = get_root_h(0);
3410 if( (float)width/height > 1.8 ) {
3411 // If dual head, the screen width is > 16x9
3412 // but we only want to fill one screen
3413 // this code assumes the "big" screen is on the right
3414 int scr_w0 = width / 2;
3416 case 600: scr_w0 = 800; break;
3417 case 720: scr_w0 = 1280; break;
3418 case 1024: scr_w0 = 1280; break;
3419 case 1200: scr_w0 = 1600; break;
3420 case 1080: scr_w0 = 1920; break;
3422 int scr_w1 = width - scr_w0;
3423 result = screen > 0 ? scr_w1 :
3424 screen == 0 ? scr_w0 :
3425 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3431 result = info->width;
3432 if(lock_display) unlock_window();
3436 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3438 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3439 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3440 int result = info ? info->height : get_root_h(0);
3441 if(lock_display) unlock_window();
3445 // Bottom right corner
3446 int BC_WindowBase::get_x2()
3451 int BC_WindowBase::get_y2()
3456 int BC_WindowBase::get_video_on()
3461 int BC_WindowBase::get_hidden()
3463 return top_level->hidden;
3466 int BC_WindowBase::cursor_inside()
3468 return (top_level->cursor_x >= 0 &&
3469 top_level->cursor_y >= 0 &&
3470 top_level->cursor_x < w &&
3471 top_level->cursor_y < h);
3474 BC_WindowBase* BC_WindowBase::get_top_level()
3479 BC_WindowBase* BC_WindowBase::get_parent()
3481 return parent_window;
3484 int BC_WindowBase::get_color_model()
3486 return top_level->color_model;
3489 BC_Resources* BC_WindowBase::get_resources()
3491 return &BC_WindowBase::resources;
3494 BC_Synchronous* BC_WindowBase::get_synchronous()
3496 return BC_WindowBase::resources.get_synchronous();
3499 int BC_WindowBase::get_bg_color()
3504 void BC_WindowBase::set_bg_color(int color)
3506 this->bg_color = color;
3509 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3514 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3516 top_level->active_subwindow = subwindow;
3519 int BC_WindowBase::activate()
3524 int BC_WindowBase::deactivate()
3526 if(window_type == MAIN_WINDOW)
3528 if(top_level->active_menubar) top_level->active_menubar->deactivate();
3529 if(top_level->active_popup_menu) top_level->active_popup_menu->deactivate();
3530 if(top_level->active_subwindow) top_level->active_subwindow->deactivate();
3532 top_level->active_menubar = 0;
3533 top_level->active_popup_menu = 0;
3534 top_level->active_subwindow = 0;
3539 int BC_WindowBase::cycle_textboxes(int amount)
3542 BC_WindowBase *new_textbox = 0;
3546 BC_WindowBase *first_textbox = 0;
3547 find_next_textbox(&first_textbox, &new_textbox, result);
3548 if(!new_textbox) new_textbox = first_textbox;
3554 BC_WindowBase *last_textbox = 0;
3555 find_prev_textbox(&last_textbox, &new_textbox, result);
3556 if(!new_textbox) new_textbox = last_textbox;
3560 if(new_textbox != active_subwindow)
3563 new_textbox->activate();
3569 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3571 // Search subwindows for textbox
3572 for(int i = 0; i < subwindows->total && result < 2; i++)
3574 BC_WindowBase *test_subwindow = subwindows->values[i];
3575 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3582 if(!*first_textbox) *first_textbox = this;
3586 if(top_level->active_subwindow == this)
3592 *next_textbox = this;
3599 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3605 if(!*last_textbox) *last_textbox = this;
3609 if(top_level->active_subwindow == this)
3615 *prev_textbox = this;
3620 // Search subwindows for textbox
3621 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3623 BC_WindowBase *test_subwindow = subwindows->values[i];
3624 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3629 BC_Clipboard* BC_WindowBase::get_clipboard()
3631 #ifdef SINGLE_THREAD
3632 return BC_Display::display_global->clipboard;
3634 return top_level->clipboard;
3638 int BC_WindowBase::get_relative_cursor_x()
3640 int abs_x, abs_y, x, y, win_x, win_y;
3641 unsigned int temp_mask;
3644 XQueryPointer(top_level->display, top_level->win,
3645 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3648 XTranslateCoordinates(top_level->display, top_level->rootwin,
3649 win, abs_x, abs_y, &x, &y, &temp_win);
3654 int BC_WindowBase::get_relative_cursor_y()
3656 int abs_x, abs_y, x, y, win_x, win_y;
3657 unsigned int temp_mask;
3660 XQueryPointer(top_level->display, top_level->win,
3661 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3664 XTranslateCoordinates(top_level->display,
3665 top_level->rootwin, win, abs_x, abs_y, &x, &y, &temp_win);
3670 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3672 int abs_x, abs_y, win_x, win_y;
3673 unsigned int temp_mask;
3676 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
3677 XQueryPointer(top_level->display, top_level->win,
3678 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3680 if(lock_window) this->unlock_window();
3684 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3686 int abs_x, abs_y, win_x, win_y;
3687 unsigned int temp_mask;
3690 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
3691 XQueryPointer(top_level->display, top_level->win,
3692 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3694 if(lock_window) this->unlock_window();
3698 int BC_WindowBase::match_window(Window win)
3700 if (this->win == win) return 1;
3702 for(int i = 0; i < subwindows->total; i++) {
3703 result = subwindows->values[i]->match_window(win);
3704 if (result) return result;
3710 int BC_WindowBase::get_cursor_over_window()
3712 if(top_level != this) return top_level->get_cursor_over_window();
3714 int abs_x, abs_y, win_x, win_y;
3715 unsigned int temp_mask;
3716 Window root_return, child_return;
3718 int ret = XQueryPointer(display, win,
3719 &root_return, &child_return, &abs_x, &abs_y,
3720 &win_x, &win_y, &temp_mask);
3721 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3722 // __LINE__, title, win, ret, child_return);
3724 int result = !ret || child_return == None ? 0 :
3725 match_window(child_return);
3729 int BC_WindowBase::cursor_above()
3731 int rx = get_relative_cursor_x();
3732 if( rx < 0 || rx >= get_w() ) return 0;
3733 int ry = get_relative_cursor_y();
3734 if( ry < 0 || ry >= get_h() ) return 0;
3738 int BC_WindowBase::get_drag_x()
3740 return top_level->drag_x;
3743 int BC_WindowBase::get_drag_y()
3745 return top_level->drag_y;
3748 int BC_WindowBase::get_cursor_x()
3750 return top_level->cursor_x;
3753 int BC_WindowBase::get_cursor_y()
3755 return top_level->cursor_y;
3758 int BC_WindowBase::dump_windows()
3760 printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
3761 this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
3762 for(int i = 0; i < subwindows->size(); i++)
3763 subwindows->get(i)->dump_windows();
3764 for(int i = 0; i < popups.size(); i++)
3765 printf("\tBC_WindowBase::dump_windows popup=%p win=%p\n",
3766 popups.get(i), (void*)popups.get(i)->win);
3770 int BC_WindowBase::is_event_win()
3772 return this->win == top_level->event_win;
3775 void BC_WindowBase::set_dragging(int value)
3777 is_dragging = value;
3780 int BC_WindowBase::get_dragging()
3785 int BC_WindowBase::get_buttonpress()
3787 return top_level->button_number;
3790 int BC_WindowBase::get_button_down()
3792 return top_level->button_down;
3795 int BC_WindowBase::alt_down()
3797 return top_level->alt_mask;
3800 int BC_WindowBase::shift_down()
3802 return top_level->shift_mask;
3805 int BC_WindowBase::ctrl_down()
3807 return top_level->ctrl_mask;
3810 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3813 *length = top_level->wkey_string_length;
3814 return top_level->wkey_string;
3817 #ifdef X_HAVE_UTF8_STRING
3818 char* BC_WindowBase::get_keypress_utf8()
3820 return top_level->key_pressed_utf8;
3825 int BC_WindowBase::get_keypress()
3827 return top_level->key_pressed;
3830 int BC_WindowBase::get_double_click()
3832 return top_level->double_click;
3835 int BC_WindowBase::get_triple_click()
3837 return top_level->triple_click;
3840 int BC_WindowBase::get_bgcolor()
3845 int BC_WindowBase::resize_window(int w, int h)
3847 if(this->w == w && this->h == h) return 0;
3849 if(window_type == MAIN_WINDOW && !allow_resize)
3851 XSizeHints size_hints;
3852 size_hints.flags = PSize | PMinSize | PMaxSize;
3853 size_hints.width = w;
3854 size_hints.height = h;
3855 size_hints.min_width = w;
3856 size_hints.max_width = w;
3857 size_hints.min_height = h;
3858 size_hints.max_height = h;
3859 XSetNormalHints(top_level->display, win, &size_hints);
3861 XResizeWindow(top_level->display, win, w, h);
3866 pixmap = new BC_Pixmap(this, w, h);
3868 // Propagate to menubar
3869 for(int i = 0; i < subwindows->total; i++)
3871 subwindows->values[i]->dispatch_resize_event(w, h);
3874 draw_background(0, 0, w, h);
3875 if(top_level == this && get_resources()->recursive_resizing)
3876 resize_history.append(new BC_ResizeCall(w, h));
3880 // The only way for resize events to be propagated is by updating the internal w and h
3881 int BC_WindowBase::resize_event(int w, int h)
3883 if(window_type == MAIN_WINDOW)
3891 int BC_WindowBase::reposition_window(int x, int y)
3893 reposition_window(x, y, -1, -1);
3898 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
3902 // Some tools set their own dimensions before calling this, causing the
3903 // resize check to skip.
3907 if(w > 0 && w != this->w)
3913 if(h > 0 && h != this->h)
3919 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
3922 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
3924 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
3926 if(translation_count && window_type == MAIN_WINDOW)
3928 // KDE shifts window right and down.
3929 // FVWM leaves window alone and adds border around it.
3930 XMoveResizeWindow(top_level->display,
3932 x - BC_DisplayInfo::auto_reposition_x,
3933 y - BC_DisplayInfo::auto_reposition_y,
3939 XMoveResizeWindow(top_level->display,
3950 pixmap = new BC_Pixmap(this, this->w, this->h);
3951 clear_box(0,0, this->w, this->h);
3952 // Propagate to menubar
3953 for(int i = 0; i < subwindows->total; i++)
3955 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
3958 // draw_background(0, 0, w, h);
3964 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
3966 return reposition_window(get_x()+dx, get_y()+dy, w, h);
3969 int BC_WindowBase::reposition_window_relative(int dx, int dy)
3971 return reposition_window_relative(dx, dy, -1, -1);
3974 void BC_WindowBase::set_tooltips(int v)
3976 get_resources()->tooltips_enabled = v;
3979 void BC_WindowBase::set_force_tooltip(int v)
3984 int BC_WindowBase::raise_window(int do_flush)
3986 XRaiseWindow(top_level->display, win);
3987 if(do_flush) XFlush(top_level->display);
3991 int BC_WindowBase::lower_window(int do_flush)
3993 XLowerWindow(top_level->display, win);
3994 if(do_flush) XFlush(top_level->display);
3998 void BC_WindowBase::set_background(VFrame *bitmap)
4000 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4002 bg_pixmap = new BC_Pixmap(this, bitmap, PIXMAP_OPAQUE);
4003 shared_bg_pixmap = 0;
4004 draw_background(0, 0, w, h);
4007 void BC_WindowBase::put_title(const char *text)
4009 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4010 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4011 *cp++ = *bp >= ' ' ? *bp : ' ';
4015 void BC_WindowBase::set_title(const char *text)
4020 txlist[0] = this->title;
4023 XTextProperty titleprop;
4024 XmbTextListToTextProperty(top_level->display, txlist, 1,
4025 XStdICCTextStyle, &titleprop);
4026 XSetWMName(top_level->display, top_level->win, &titleprop);
4027 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4028 XFree(titleprop.value);
4033 void BC_WindowBase::set_utf8title(const char *text)
4035 XTextProperty titleprop;
4038 strcpy(this->title, text);
4039 txlist[0] = this->title;
4042 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4043 XUTF8StringStyle, &titleprop);
4044 XSetWMName(top_level->display, top_level->win, &titleprop);
4045 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4046 XFree(titleprop.value);
4051 const char *BC_WindowBase::get_title()
4056 int BC_WindowBase::get_toggle_value()
4058 return toggle_value;
4061 int BC_WindowBase::get_toggle_drag()
4066 int BC_WindowBase::set_icon(VFrame *data)
4068 if(icon_pixmap) delete icon_pixmap;
4069 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4071 if(icon_window) delete icon_window;
4072 icon_window = new BC_Popup(this,
4075 icon_pixmap->get_w(),
4076 icon_pixmap->get_h(),
4078 1, // All windows are hidden initially
4082 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4083 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4084 wm_hints.icon_mask = icon_pixmap->get_alpha();
4085 wm_hints.icon_window = icon_window->win;
4086 wm_hints.window_group = XGroupLeader;
4088 // for(int i = 0; i < 1000; i++)
4089 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4092 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4093 XSync(top_level->display, 0);
4097 int BC_WindowBase::set_w(int w)
4103 int BC_WindowBase::set_h(int h)
4109 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4111 BC_Resources *resources = get_resources();
4112 char string[BCTEXTLEN];
4113 int newest_id = - 1;
4114 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4116 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4117 resources->filebox_history[i].path[0] = 0;
4118 defaults->get(string, resources->filebox_history[i].path);
4119 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4120 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4121 if(resources->filebox_history[i].id > newest_id)
4122 newest_id = resources->filebox_history[i].id;
4125 resources->filebox_id = newest_id + 1;
4126 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4127 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4128 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4129 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4130 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4131 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4132 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4133 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4134 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4135 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4136 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4137 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4141 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4143 BC_Resources *resources = get_resources();
4144 char string[BCTEXTLEN];
4145 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4147 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4148 defaults->update(string, resources->filebox_history[i].path);
4149 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4150 defaults->update(string, resources->filebox_history[i].id);
4152 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4153 defaults->update("FILEBOX_W", resources->filebox_w);
4154 defaults->update("FILEBOX_H", resources->filebox_h);
4155 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4156 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4157 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4158 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4159 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4160 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4161 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4162 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4163 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4169 // For some reason XTranslateCoordinates can take a long time to return.
4170 // We work around this by only calling it when the event windows are different.
4171 void BC_WindowBase::translate_coordinates(Window src_w,
4183 *dest_x_return = src_x;
4184 *dest_y_return = src_y;
4188 XTranslateCoordinates(top_level->display,
4196 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4200 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4202 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4205 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4207 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4215 #ifdef HAVE_LIBXXF86VM
4216 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4220 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4222 XF86VidModeModeInfo **vm_modelines;
4223 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4224 for (i = 0; i < vm_count; i++) {
4225 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4228 display = top_level->display;
4229 if (vm_modelines[*vm]->hdisplay == *width)
4233 *width = vm_modelines[*vm]->hdisplay;
4234 *height = vm_modelines[*vm]->vdisplay;
4239 void BC_WindowBase::scale_vm(int vm)
4241 int foo,bar,dotclock;
4242 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4245 XF86VidModeModeInfo **vm_modelines;
4246 XF86VidModeModeLine vml;
4247 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4248 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4249 orig_modeline.dotclock = dotclock;
4250 orig_modeline.hdisplay = vml.hdisplay;
4251 orig_modeline.hsyncstart = vml.hsyncstart;
4252 orig_modeline.hsyncend = vml.hsyncend;
4253 orig_modeline.htotal = vml.htotal;
4254 orig_modeline.vdisplay = vml.vdisplay;
4255 orig_modeline.vsyncstart = vml.vsyncstart;
4256 orig_modeline.vsyncend = vml.vsyncend;
4257 orig_modeline.vtotal = vml.vtotal;
4258 orig_modeline.flags = vml.flags;
4259 orig_modeline.privsize = vml.privsize;
4260 // orig_modeline.private = vml.private;
4261 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4262 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4263 XFlush(top_level->display);
4267 void BC_WindowBase::restore_vm()
4269 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4270 XFlush(top_level->display);
4293 #ifndef SINGLE_THREAD
4294 int BC_WindowBase::get_event_count()
4296 event_lock->lock("BC_WindowBase::get_event_count");
4297 int result = common_events.total;
4298 event_lock->unlock();
4302 XEvent* BC_WindowBase::get_event()
4305 while(!done && !result)
4307 event_condition->lock("BC_WindowBase::get_event");
4308 event_lock->lock("BC_WindowBase::get_event");
4310 if(common_events.total && !done)
4312 result = common_events.values[0];
4313 common_events.remove_number(0);
4316 event_lock->unlock();
4321 void BC_WindowBase::put_event(XEvent *event)
4323 event_lock->lock("BC_WindowBase::put_event");
4324 common_events.append(event);
4325 event_lock->unlock();
4326 event_condition->unlock();
4329 void BC_WindowBase::dequeue_events(Window win)
4331 event_lock->lock("BC_WindowBase::dequeue_events");
4333 int out = 0, total = common_events.size();
4334 for( int in=0; in<total; ++in ) {
4335 if( common_events[in]->xany.window == win ) continue;
4336 common_events[out++] = common_events[in];
4338 common_events.total = out;
4340 event_lock->unlock();
4343 #endif // SINGLE_THREAD
4345 int BC_WindowBase::get_id()
4351 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4353 int w = vframe->get_w(), h = vframe->get_h();
4354 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4355 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4360 void BC_WindowBase::flicker(int n, int ms)
4362 int color = get_bg_color();
4363 for( int i=2*n; --i>=0; ) {
4364 set_inverse(); set_bg_color(WHITE);
4365 clear_box(0,0, w,h); flash(1);
4366 sync_display(); Timer::delay(ms);
4368 set_bg_color(color);