4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcclipboard.h"
24 #include "bcdisplay.h"
25 #include "bcdisplayinfo.h"
26 #include "bcmenubar.h"
29 #include "bcpopupmenu.h"
30 #include "bcrepeater.h"
31 #include "bcresources.h"
32 #include "bcsignals.h"
33 #include "bcsubwindow.h"
34 #include "bcsynchronous.h"
36 #include "bcwindowbase.h"
37 #include "bcwindowevents.h"
38 #include "bccmodels.h"
40 #include "condition.h"
58 #include <X11/extensions/Xinerama.h>
59 #include <X11/extensions/Xvlib.h>
60 #include <X11/extensions/shape.h>
61 #include <X11/XF86keysym.h>
62 #include <X11/Sunkeysym.h>
65 BC_ResizeCall::BC_ResizeCall(int w, int h)
77 int BC_WindowBase::shm_completion_event = -1;
81 BC_Resources BC_WindowBase::resources;
83 Window XGroupLeader = 0;
85 Mutex BC_KeyboardHandlerLock::keyboard_listener_mutex("keyboard_listener",0);
86 ArrayList<BC_KeyboardHandler*> BC_KeyboardHandler::listeners;
88 BC_WindowBase::BC_WindowBase(int opts)
91 //printf("BC_WindowBase::BC_WindowBase 1\n");
92 BC_WindowBase::initialize();
95 BC_WindowBase::~BC_WindowBase()
98 BC_Display::lock_display("BC_WindowBase::~BC_WindowBase");
100 if(window_type == MAIN_WINDOW)
101 lock_window("BC_WindowBase::~BC_WindowBase");
104 #ifdef HAVE_LIBXXF86VM
105 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched) {
112 if(window_type != MAIN_WINDOW)
115 XSelectInput(top_level->display, this->win, 0);
116 XSync(top_level->display,0);
117 #ifndef SINGLE_THREAD
118 top_level->dequeue_events(win);
120 // drop active window refs to this
121 if(top_level->active_grab == this) top_level->active_grab = 0;
122 if(top_level->active_menubar == this) top_level->active_menubar = 0;
123 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
124 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
125 // drop motion window refs to this
126 if(top_level->motion_events && top_level->last_motion_win == this->win)
127 top_level->motion_events = 0;
129 // Remove pointer from parent window to this
130 parent_window->subwindows->remove(this);
133 if(icon_window) delete icon_window;
134 if(window_type == POPUP_WINDOW)
135 parent_window->remove_popup(this);
137 // Delete the subwindows
140 while(subwindows->total)
142 // Subwindow removes its own pointer
143 delete subwindows->values[0];
151 if( get_resources()->get_synchronous() &&
152 (top_level->options & GLX_WINDOW) && glx_win != 0 )
153 get_resources()->get_synchronous()->delete_window(this);
156 XDestroyWindow(top_level->display, win);
158 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
159 if(icon_pixmap) delete icon_pixmap;
160 if(temp_bitmap) delete temp_bitmap;
161 top_level->active_bitmaps.remove_buffers(this);
162 if(_7segment_pixmaps)
164 for(int i = 0; i < TOTAL_7SEGMENT; i++)
165 delete _7segment_pixmaps[i];
167 delete [] _7segment_pixmaps;
172 if(window_type == MAIN_WINDOW)
174 XFreeGC(display, gc);
175 static XFontStruct *BC_WindowBase::*xfont[] = {
176 &BC_WindowBase::smallfont,
177 &BC_WindowBase::mediumfont,
178 &BC_WindowBase::largefont,
179 &BC_WindowBase::bigfont,
181 for( int i=sizeof(xfont)/sizeof(xfont[0]); --i>=0; )
182 XFreeFont(display, this->*xfont[i]);
184 // bug in X causes XRenderExtensionInfo to be damaged if this is done here
185 // left to be done in XCloseDisplay by Xlib.
186 #if defined(HAVE_XFT) && 0
187 static void *BC_WindowBase::*xft_font[] = {
188 &BC_WindowBase::smallfont_xft,
189 &BC_WindowBase::mediumfont_xft,
190 &BC_WindowBase::largefont_xft,
191 &BC_WindowBase::bigfont_xft,
192 &BC_WindowBase::bold_smallfont_xft,
193 &BC_WindowBase::bold_mediumfont_xft,
194 &BC_WindowBase::bold_largefont_xft,
196 for( int i=sizeof(xft_font)/sizeof(xft_font[0]); --i>=0; ) {
197 XftFont *xft = (XftFont *)(this->*xft_font[i]);
198 if( xft ) XftFontClose (display, xft);
206 XFree(xinerama_info);
207 xinerama_screens = 0;
209 if( xvideo_port_id >= 0 )
210 XvUngrabPort(display, xvideo_port_id, CurrentTime);
213 // Can't close display if another thread is waiting for events.
214 // Synchronous thread must delete display it owns a GLX_WINDOW
215 // Must be last reference to display.
216 #ifndef SINGLE_THREAD
218 if( (options & GLX_DISPLAY) != 0 && get_resources()->get_synchronous() ) {
219 printf(_("BC_WindowBase::~BC_WindowBase window deleted but opengl deletion is not\n"
220 "implemented for BC_Pixmap.\n"));
221 get_resources()->get_synchronous()->delete_display(this);
225 XCloseDisplay(display);
226 // clipboard uses a different display connection
227 clipboard->stop_clipboard();
229 #endif // SINGLE_THREAD
232 resize_history.remove_all_objects();
234 #ifndef SINGLE_THREAD
235 common_events.remove_all_objects();
237 delete event_condition;
240 top_level->window_lock = 0;
241 BC_Display::unlock_display();
246 if( glx_fbcfgs_window ) XFree(glx_fbcfgs_window);
247 if( glx_fbcfgs_pbuffer) XFree(glx_fbcfgs_pbuffer);
248 if( glx_fbcfgs_pixmap ) XFree(glx_fbcfgs_pixmap);
251 UNSET_ALL_LOCKS(this)
254 int BC_WindowBase::initialize()
259 display_lock_owner = 0;
275 xinerama_screens = 0;
280 translation_events = 0;
281 ctrl_mask = shift_mask = alt_mask = 0;
282 cursor_x = cursor_y = button_number = 0;
295 active_popup_menu = 0;
296 active_subwindow = 0;
299 _7segment_pixmaps = 0;
302 // next_repeat_id = 0;
305 current_font = MEDIUMFONT;
306 current_color = BLACK;
307 current_cursor = ARROW_CURSOR;
310 shared_bg_pixmap = 0;
313 window_type = MAIN_WINDOW;
314 translation_count = 0;
315 x_correction = y_correction = 0;
324 #ifdef HAVE_LIBXXF86VM
328 bold_largefont_xft = 0;
329 bold_mediumfont_xft = 0;
330 bold_smallfont_xft = 0;
338 completion_lock = new Condition(0, "BC_WindowBase::completion_lock");
340 // Need these right away since put_event is called before run_window sometimes.
341 event_lock = new Mutex("BC_WindowBase::event_lock");
342 event_condition = new Condition(0, "BC_WindowBase::event_condition");
343 init_lock = new Condition(0, "BC_WindowBase::init_lock");
346 cursor_timer = new Timer;
349 glx_fbcfgs_window = 0; n_fbcfgs_window = 0;
350 glx_fbcfgs_pbuffer = 0; n_fbcfgs_pbuffer = 0;
351 glx_fbcfgs_pixmap = 0; n_fbcfgs_pixmap = 0;
365 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
368 ButtonReleaseMask | \
369 PointerMotionMask | \
373 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
385 const char *display_name,
387 BC_Pixmap *bg_pixmap,
390 XSetWindowAttributes attr;
392 XSizeHints size_hints;
395 #ifdef HAVE_LIBXXF86VM
399 id = get_resources()->get_id();
401 if(parent_window) top_level = parent_window->top_level;
402 if( top_level ) { // need this to avoid deadlock with Xlib's locks
404 lock_window("BC_WindowBase::create_window");
406 get_resources()->create_window_lock->lock("BC_WindowBase::create_window");
408 #ifdef HAVE_LIBXXF86VM
409 if(window_type == VIDMODE_SCALED_WINDOW)
410 closest_vm(&vm,&w,&h);
417 this->bg_color = bg_color;
418 this->window_type = window_type;
420 this->private_color = private_color;
421 this->parent_window = parent_window;
422 this->bg_pixmap = bg_pixmap;
423 this->allow_resize = allow_resize;
425 strcpy(this->display_name, display_name);
427 this->display_name[0] = 0;
430 if(bg_pixmap) shared_bg_pixmap = 1;
432 subwindows = new BC_SubWindowList;
434 if(window_type == MAIN_WINDOW)
437 parent_window = this;
441 display = BC_Display::get_display(display_name);
442 BC_Display::lock_display("BC_WindowBase::create_window");
443 // BC_Display::display_global->new_window(this);
446 // get the display connection
448 // This function must be the first Xlib
449 // function a multi-threaded program calls
451 display = init_display(display_name);
452 if( shm_completion_event < 0 ) shm_completion_event =
453 ShmCompletion + XShmGetEventBase(display);
456 screen = DefaultScreen(display);
457 rootwin = RootWindow(display, screen);
458 // window placement boundaries
459 if( !xinerama_screens && XineramaIsActive(display) )
460 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
461 root_w = get_root_w(0);
462 root_h = get_root_h(0);
465 vis = get_glx_visual(display);
468 vis = DefaultVisual(display, screen);
470 default_depth = DefaultDepth(display, screen);
472 client_byte_order = (*(const u_int32_t*)"a ") & 0x00000001;
473 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
476 // This must be done before fonts to know if antialiasing is available.
479 if(resources.use_shm < 0) resources.initialize_display(this);
480 x_correction = BC_DisplayInfo::get_left_border();
481 y_correction = BC_DisplayInfo::get_top_border();
483 // clamp window placement
484 if(this->x + this->w + x_correction > root_w)
485 this->x = root_w - this->w - x_correction;
486 if(this->y + this->h + y_correction > root_h)
487 this->y = root_h - this->h - y_correction;
488 if(this->x < 0) this->x = 0;
489 if(this->y < 0) this->y = 0;
491 if(this->bg_color == -1)
492 this->bg_color = resources.get_bg_color();
494 // printf("bcwindowbase 1 %s\n", title);
495 // if(window_type == MAIN_WINDOW) sleep(1);
496 // printf("bcwindowbase 10\n");
502 mask = CWEventMask | CWBackPixel | CWColormap | CWCursor;
504 attr.event_mask = DEFAULT_EVENT_MASKS |
505 StructureNotifyMask |
509 attr.background_pixel = get_color(this->bg_color);
510 attr.colormap = cmap;
511 attr.cursor = get_cursor_struct(ARROW_CURSOR);
513 win = XCreateWindow(display, rootwin,
514 this->x, this->y, this->w, this->h, 0,
515 top_level->default_depth, InputOutput,
517 XGetNormalHints(display, win, &size_hints);
519 size_hints.flags = PSize | PMinSize | PMaxSize;
520 size_hints.width = this->w;
521 size_hints.height = this->h;
522 size_hints.min_width = allow_resize ? minw : this->w;
523 size_hints.max_width = allow_resize ? 32767 : this->w;
524 size_hints.min_height = allow_resize ? minh : this->h;
525 size_hints.max_height = allow_resize ? 32767 : this->h;
526 if(x > -BC_INFINITY && x < BC_INFINITY)
528 size_hints.flags |= PPosition;
529 size_hints.x = this->x;
530 size_hints.y = this->y;
534 txlist[0] = this->title;
536 XTextProperty titleprop;
537 if(options & WINDOW_UTF8)
538 Xutf8TextListToTextProperty(display, txlist, 1,
539 XUTF8StringStyle, &titleprop);
541 XmbTextListToTextProperty(display, txlist, 1,
542 XStdICCTextStyle, &titleprop);
543 XSetWMProperties(display, win, &titleprop, &titleprop,
544 0, 0, &size_hints, 0, 0);
545 XFree(titleprop.value);
548 #ifndef SINGLE_THREAD
549 clipboard = new BC_Clipboard(display_name);
550 clipboard->start_clipboard();
556 Atom ClientLeaderXAtom;
557 if (XGroupLeader == 0)
559 const char *instance_name = "cinelerra";
560 const char *class_name = "Cinelerra";
561 XClassHint *class_hints = XAllocClassHint();
562 class_hints->res_name = (char*)instance_name;
563 class_hints->res_class = (char*)class_name;
564 XSetClassHint(top_level->display, win, class_hints);
566 ClientLeaderXAtom = XInternAtom(display, "WM_CLIENT_LEADER", True);
567 XChangeProperty(display, win, ClientLeaderXAtom, XA_WINDOW, 32,
568 PropModeReplace, (unsigned char *)&XGroupLeader, true);
573 #ifdef HAVE_LIBXXF86VM
574 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
581 #ifdef HAVE_LIBXXF86VM
582 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
584 if(window_type == POPUP_WINDOW)
587 mask = CWEventMask | CWBackPixel | CWColormap |
588 CWOverrideRedirect | CWSaveUnder | CWCursor;
590 attr.event_mask = DEFAULT_EVENT_MASKS |
594 if(this->bg_color == -1)
595 this->bg_color = resources.get_bg_color();
596 attr.background_pixel = top_level->get_color(bg_color);
597 attr.colormap = top_level->cmap;
598 if(top_level->is_hourglass)
599 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
601 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
602 attr.override_redirect = True;
603 attr.save_under = True;
605 win = XCreateWindow(top_level->display,
606 top_level->rootwin, this->x, this->y, this->w, this->h, 0,
607 top_level->default_depth, InputOutput, top_level->vis, mask,
609 top_level->add_popup(this);
612 if(window_type == SUB_WINDOW)
614 mask = CWEventMask | CWBackPixel | CWCursor;
615 attr.event_mask = DEFAULT_EVENT_MASKS;
616 attr.background_pixel = top_level->get_color(this->bg_color);
617 if(top_level->is_hourglass)
618 attr.cursor = top_level->get_cursor_struct(HOURGLASS_CURSOR);
620 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
621 win = XCreateWindow(top_level->display,
622 parent_window->win, this->x, this->y, this->w, this->h, 0,
623 top_level->default_depth, InputOutput, top_level->vis, mask,
626 if(!hidden) XMapWindow(top_level->display, win);
629 // Create pixmap for all windows
630 pixmap = new BC_Pixmap(this, this->w, this->h);
632 // Set up options for main window
633 if(window_type == MAIN_WINDOW)
635 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
637 this->bg_pixmap = new BC_Pixmap(this,
638 get_resources()->bg_image,
642 if(!hidden) show_window();
646 draw_background(0, 0, this->w, this->h);
648 flash(-1, -1, -1, -1, 0);
650 // Set up options for popup window
651 #ifdef HAVE_LIBXXF86VM
652 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
654 if(window_type == POPUP_WINDOW)
658 if(!hidden) show_window();
660 get_resources()->create_window_lock->unlock();
661 if(need_lock) unlock_window();
666 Display* BC_WindowBase::init_display(const char *display_name)
670 if(display_name && display_name[0] == 0) display_name = NULL;
671 if((display = XOpenDisplay(display_name)) == NULL) {
672 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
674 if(getenv("DISPLAY") == NULL) {
675 printf(_("'DISPLAY' environment variable not set.\n"));
679 // Try again with default display.
680 if((display = XOpenDisplay(0)) == NULL) {
681 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
689 Display* BC_WindowBase::get_display()
691 return top_level->display;
694 int BC_WindowBase::get_screen()
696 return top_level->screen;
699 int BC_WindowBase::run_window()
705 // Events may have been sent before run_window so can't initialize them here.
708 set_repeat(get_resources()->tooltip_delay);
709 BC_Display::display_global->new_window(this);
711 // If the first window created, run the display loop in this thread.
712 if(BC_Display::display_global->is_first(this))
714 BC_Display::unlock_display();
715 BC_Display::display_global->loop();
719 BC_Display::unlock_display();
720 completion_lock->lock("BC_WindowBase::run_window");
723 BC_Display::lock_display("BC_WindowBase::run_window");
724 BC_Display::display_global->delete_window(this);
726 unset_all_repeaters();
728 BC_Display::unlock_display();
730 #else // SINGLE_THREAD
735 set_repeat(get_resources()->tooltip_delay);
737 // Start X server events
738 event_thread = new BC_WindowEvents(this);
739 event_thread->start();
745 // Handle common events
751 unset_all_repeaters();
755 event_condition->reset();
756 common_events.remove_all_objects();
760 #endif // SINGLE_THREAD
765 int BC_WindowBase::get_key_masks(unsigned int key_state)
767 // printf("BC_WindowBase::get_key_masks %llx\n",
768 // event->xkey.state);
769 ctrl_mask = (key_state & ControlMask) ? 1 : 0; // ctrl key down
770 shift_mask = (key_state & ShiftMask) ? 1 : 0; // shift key down
771 alt_mask = (key_state & Mod1Mask) ? 1 : 0; // alt key down
776 void BC_WindowBase::add_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
778 BC_KeyboardHandlerLock set;
779 BC_KeyboardHandler::listeners.append(new BC_KeyboardHandler(handler, this));
782 void BC_WindowBase::del_keyboard_listener(int(BC_WindowBase::*handler)(BC_WindowBase *))
784 BC_KeyboardHandlerLock set;
785 int i = BC_KeyboardHandler::listeners.size();
786 while( --i >= 0 && BC_KeyboardHandler::listeners[i]->handler!=handler );
787 if( i >= 0 ) BC_KeyboardHandler::listeners.remove_object_number(i);
790 int BC_KeyboardHandler::run_event(BC_WindowBase *wp)
792 int result = (win->*handler)(wp);
796 int BC_KeyboardHandler::run_listeners(BC_WindowBase *wp)
799 BC_KeyboardHandlerLock set;
800 for( int i=0; !result && i<listeners.size(); ++i ) {
801 BC_KeyboardHandler *listener = listeners[i];
802 result = listener->run_event(wp);
807 void BC_KeyboardHandler::kill_grabs()
809 BC_KeyboardHandlerLock set;
810 for( int i=0; i<listeners.size(); ++i ) {
811 BC_WindowBase *win = listeners[i]->win;
812 if( win->get_window_type() != POPUP_WINDOW ) continue;
813 ((BC_Popup *)win)->ungrab_keyboard();
817 void BC_ActiveBitmaps::reque(XEvent *event)
819 XShmCompletionEvent *shm_ev = (XShmCompletionEvent *)event;
820 ShmSeg shmseg = shm_ev->shmseg;
821 Drawable drawable = shm_ev->drawable;
822 //printf("BC_BitmapImage::reque %08lx\n",shmseg);
823 active_lock.lock("BC_BitmapImage::reque");
824 BC_BitmapImage *bfr = first;
825 while( bfr && bfr->get_shmseg() != shmseg ) bfr = bfr->next;
826 if( bfr && bfr->drawable == drawable )
828 active_lock.unlock();
830 // sadly, X reports two drawable completions and creates false reporting, so no boobytrap
831 // printf("BC_BitmapImage::reque missed shmseg %08x, drawable %08x\n",
832 // (int)shmseg, (int)drawable);
835 if( bfr->drawable != drawable ) return;
836 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; return; }
837 bfr->bitmap->reque(bfr);
840 void BC_ActiveBitmaps::insert(BC_BitmapImage *bfr, Drawable pixmap)
842 active_lock.lock("BC_BitmapImage::insert");
843 bfr->drawable = pixmap;
845 active_lock.unlock();
848 void BC_ActiveBitmaps::remove_buffers(BC_WindowBase *wdw)
850 active_lock.lock("BC_ActiveBitmaps::remove");
851 for( BC_BitmapImage *nxt=0, *bfr=first; bfr; bfr=nxt ) {
853 if( bfr->is_zombie() ) { --BC_Bitmap::zombies; delete bfr; continue; }
854 if( bfr->bitmap->parent_window == wdw ) remove_pointer(bfr);
856 active_lock.unlock();
859 BC_ActiveBitmaps::BC_ActiveBitmaps()
863 BC_ActiveBitmaps::~BC_ActiveBitmaps()
869 int BC_WindowBase::keysym_lookup(XEvent *event)
871 for( int i = 0; i < KEYPRESSLEN; ++i ) keys_return[i] = 0;
872 for( int i = 0; i < 4; ++i ) wkey_string[i] = 0;
874 if( event->xany.send_event && !event->xany.serial ) {
875 keysym = (KeySym) event->xkey.keycode;
876 keys_return[0] = keysym;
879 wkey_string_length = 0;
881 if( input_context ) {
882 wkey_string_length = XwcLookupString(input_context,
883 (XKeyEvent*)event, wkey_string, 4, &keysym, 0);
884 //printf("keysym_lookup 1 %d %d %lx %x %x %x %x\n", wkey_string_length, keysym,
885 // wkey_string[0], wkey_string[1], wkey_string[2], wkey_string[3]);
888 int ret = Xutf8LookupString(input_context, (XKeyEvent*)event,
889 keys_return, KEYPRESSLEN, &keysym, &stat);
890 //printf("keysym_lookup 2 %d %d %lx %x %x\n", ret, stat, keysym, keys_return[0], keys_return[1]);
891 if( stat == XLookupBoth ) return ret;
892 if( stat == XLookupKeySym ) return 0;
894 int ret = XLookupString((XKeyEvent*)event, keys_return, KEYPRESSLEN, &keysym, 0);
895 wkey_string_length = ret;
896 for( int i=0; i<ret; ++i ) wkey_string[i] = keys_return[i];
900 pthread_t locking_task = (pthread_t)-1L;
901 int locking_event = -1;
902 int locking_message = -1;
904 int BC_WindowBase::dispatch_event(XEvent *event)
908 XClientMessageEvent *ptr;
909 int cancel_resize, cancel_translation;
914 #ifndef SINGLE_THREAD
915 // If an event is waiting get it, otherwise
916 // wait for next event only if there are no compressed events.
917 if(get_event_count() ||
918 (!motion_events && !resize_events && !translation_events))
921 // Lock out window deletions
922 lock_window("BC_WindowBase::dispatch_event 1");
923 locking_event = event->type;
924 locking_task = pthread_self();
925 locking_message = event->xclient.message_type;
928 // Handle compressed events
930 lock_window("BC_WindowBase::dispatch_event 2");
932 dispatch_resize_event(last_resize_w, last_resize_h);
934 dispatch_motion_event();
935 if(translation_events)
936 dispatch_translation_event();
947 //static const char *event_names[] = {
948 // "Reply", "Error", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", "MotionNotify",
949 // "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose",
950 // "NoExpose", "VisibilityNotify", "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify",
951 // "MapRequest", "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
952 // "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify", "SelectionClear",
953 // "SelectionRequest", "SelectionNotify", "ColormapNotify", "ClientMessage", "MappingNotify",
954 // "GenericEvent", "LASTEvent",
958 //if(event->type != ClientMessage) {
959 // printf("BC_WindowBase::dispatch_event %d %s %p %d (%s)\n",
960 //__LINE__, title, event, event->type,
961 // event->type>=0 && event->type<sizeof(event_names)/sizeof(event_names[0]) ?
962 // event_names[event->type] : "Unknown");
965 if( active_grab && active_grab->grab_event(event) ) {
971 switch(event->type) {
973 // Clear the resize buffer
974 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
975 // Clear the motion buffer since this can clear the window
978 dispatch_motion_event();
981 ptr = (XClientMessageEvent*)event;
984 if(ptr->message_type == ProtoXAtom &&
985 (Atom)ptr->data.l[0] == DelWinXAtom)
990 if(ptr->message_type == RepeaterXAtom)
992 dispatch_repeat_event(ptr->data.l[0]);
993 // Make sure the repeater still exists.
994 // for(int i = 0; i < repeaters.total; i++)
996 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
998 // dispatch_repeat_event_master(ptr->data.l[0]);
1004 if(ptr->message_type == SetDoneXAtom)
1008 { // We currently use X marshalling for xatom events, we can switch to something else later
1009 recieve_custom_xatoms((xatom_event *)ptr);
1015 dispatch_focus_in();
1020 dispatch_focus_out();
1032 get_key_masks(event->xbutton.state);
1033 cursor_x = event->xbutton.x;
1034 cursor_y = event->xbutton.y;
1035 button_number = event->xbutton.button;
1037 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1038 event_win = event->xany.window;
1039 if (button_number < 6)
1041 if(button_number < 4)
1043 button_pressed = event->xbutton.button;
1044 button_time1 = button_time2;
1045 button_time2 = button_time3;
1046 button_time3 = event->xbutton.time;
1049 drag_win = event_win;
1050 drag_x1 = cursor_x - get_resources()->drag_radius;
1051 drag_x2 = cursor_x + get_resources()->drag_radius;
1052 drag_y1 = cursor_y - get_resources()->drag_radius;
1053 drag_y2 = cursor_y + get_resources()->drag_radius;
1055 if((long)(button_time3 - button_time1) < resources.double_click * 2)
1058 button_time3 = button_time2 = button_time1 = 0;
1060 if((long)(button_time3 - button_time2) < resources.double_click)
1063 // button_time3 = button_time2 = button_time1 = 0;
1071 dispatch_button_press();
1076 get_key_masks(event->xbutton.state);
1077 button_number = event->xbutton.button;
1078 event_win = event->xany.window;
1079 if (button_number < 6)
1081 if(button_number < 4)
1083 //printf("BC_WindowBase::dispatch_event %d %d\n", __LINE__, button_number);
1085 dispatch_button_release();
1090 event_win = event->xany.window;
1091 dispatch_expose_event();
1095 get_key_masks(event->xmotion.state);
1096 // Dispatch previous motion event if this is a subsequent motion from a different window
1097 if(motion_events && last_motion_win != event->xany.window)
1099 dispatch_motion_event();
1102 // Buffer the current motion
1104 last_motion_state = event->xmotion.state;
1105 last_motion_x = event->xmotion.x;
1106 last_motion_y = event->xmotion.y;
1107 last_motion_win = event->xany.window;
1110 case ConfigureNotify:
1111 // printf("BC_WindowBase::dispatch_event %d win=%p this->win=%p\n",
1113 // event->xany.window,
1116 XTranslateCoordinates(top_level->display,
1124 last_resize_w = event->xconfigure.width;
1125 last_resize_h = event->xconfigure.height;
1128 cancel_translation = 0;
1130 // Resize history prevents responses to recursive resize requests
1131 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
1133 if(resize_history.values[i]->w == last_resize_w &&
1134 resize_history.values[i]->h == last_resize_h)
1136 delete resize_history.values[i];
1137 resize_history.remove_number(i);
1142 if(last_resize_w == w && last_resize_h == h)
1150 if((last_translate_x == x && last_translate_y == y))
1151 cancel_translation = 1;
1153 if(!cancel_translation)
1155 translation_events = 1;
1158 translation_count++;
1162 get_key_masks(event->xkey.state);
1163 keys_return[0] = 0; keysym = -1;
1164 if(XFilterEvent(event, win)) {
1167 if( keysym_lookup(event) < 0 ) {
1168 printf("keysym %x\n", (uint32_t)keysym);
1172 //printf("BC_WindowBase::dispatch_event %d keysym=0x%x\n",
1176 // block out control keys
1177 if(keysym > 0xffe0 && keysym < 0xffff) break;
1178 // block out Alt_GR key
1179 if(keysym == 0xfe03) break;
1182 printf("BC_WindowBase::dispatch_event %x\n", (uint32_t)keysym);
1184 #ifdef X_HAVE_UTF8_STRING
1185 //It's Ascii or UTF8?
1186 // if (keysym != 0xffff && (keys_return[0] & 0xff) >= 0x7f )
1187 //printf("BC_WindowBase::dispatch_event %d %02x%02x\n", __LINE__, keys_return[0], keys_return[1]);
1189 if( ((keys_return[1] & 0xff) > 0x80) &&
1190 ((keys_return[0] & 0xff) > 0xC0) ) {
1191 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1192 key_pressed = keysym & 0xff;
1198 // block out extra keys
1208 // Translate key codes
1209 case XK_Return: key_pressed = RETURN; break;
1210 case XK_Up: key_pressed = UP; break;
1211 case XK_Down: key_pressed = DOWN; break;
1212 case XK_Left: key_pressed = LEFT; break;
1213 case XK_Right: key_pressed = RIGHT; break;
1214 case XK_Next: key_pressed = PGDN; break;
1215 case XK_Prior: key_pressed = PGUP; break;
1216 case XK_BackSpace: key_pressed = BACKSPACE; break;
1217 case XK_Escape: key_pressed = ESC; break;
1220 key_pressed = LEFTTAB;
1224 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
1225 case XK_underscore: key_pressed = '_'; break;
1226 case XK_asciitilde: key_pressed = '~'; break;
1227 case XK_Delete: key_pressed = DELETE; break;
1228 case XK_Home: key_pressed = HOME; break;
1229 case XK_End: key_pressed = END; break;
1232 case XK_KP_Enter: key_pressed = KPENTER; break;
1233 case XK_KP_Add: key_pressed = KPPLUS; break;
1234 case XK_KP_Subtract: key_pressed = KPMINUS; break;
1235 case XK_KP_Multiply: key_pressed = KPSTAR; break;
1236 case XK_KP_Divide: key_pressed = KPSLASH; break;
1238 case XK_KP_End: key_pressed = KP1; break;
1240 case XK_KP_Down: key_pressed = KP2; break;
1242 case XK_KP_Page_Down: key_pressed = KP3; break;
1244 case XK_KP_Left: key_pressed = KP4; break;
1246 case XK_KP_Begin: key_pressed = KP5; break;
1248 case XK_KP_Right: key_pressed = KP6; break;
1250 case XK_KP_Home: key_pressed = KP7; break;
1252 case XK_KP_Up: key_pressed = KP8; break;
1254 case XK_KP_Page_Up: key_pressed = KP9; break;
1256 case XK_KP_Insert: key_pressed = KPINS; break;
1258 case XK_KP_Delete: key_pressed = KPDEL; break;
1259 case XK_Menu: key_pressed = KPMENU; break; /* menu */
1261 // above case XK_KP_Enter: key_pressed = KPENTER; break; /* check */
1262 case XF86XK_MenuKB: key_pressed = KPMENU; break; /* menu */
1263 // intercepted case XF86XK_PowerDown: key_pressed = KPPOWER; break; /* Power */
1264 case XF86XK_Launch1: key_pressed = KPTV; break; /* TV */
1265 case XF86XK_Launch2: key_pressed = KPDVD; break; /* DVD */
1266 // intercepted case XF86XK_WWW: key_pressed = KPWWEB; break; /* WEB */
1267 case XF86XK_Launch3: key_pressed = KPBOOK; break; /* book */
1268 case XF86XK_Launch4: key_pressed = KPHAND; break; /* hand */
1269 case XF86XK_Reply: key_pressed = KPTMR; break; /* timer */
1270 case SunXK_Front: key_pressed = KPMAXW; break; /* max */
1271 // above case XK_Left: key_pressed = LEFT; break; /* left */
1272 // above case XK_Right: key_pressed = RIGHT; break; /* right */
1273 // above case XK_Down: key_pressed = DOWN; break; /* down */
1274 // above case XK_Up: key_pressed = UP; break; /* up */
1275 // above case XK_SPACE: key_pressed = KPSPACE; break; /* ok */
1276 // intercepted case XF86XK_AudioRaiseVolume: key_pressed = KPVOLU; break; /* VOL + */
1277 // intercepted case XF86XK_AudioMute: key_pressed = KPMUTE; break; /* MUTE */
1278 // intercepted case XF86XK_AudioLowerVolume: key_pressed = KPVOLD; break; /* VOL - */
1279 case XF86XK_ScrollUp: key_pressed = KPCHUP; break; /* CH + */
1280 case XF86XK_ScrollDown: key_pressed = KPCHDN; break; /* CH - */
1281 case XF86XK_AudioRecord: key_pressed = KPRECD; break; /* ( o) red */
1282 case XF86XK_Forward: key_pressed = KPPLAY; break; /* ( >) */
1283 case XK_Redo: key_pressed = KPFWRD; break; /* (>>) */
1284 case XF86XK_Back: key_pressed = KPBACK; break; /* (<<) */
1285 case XK_Cancel: key_pressed = KPSTOP; break; /* ([]) */
1286 case XK_Pause: key_pressed = KPAUSE; break; /* ('') */
1289 key_pressed = keysym & 0xff;
1290 #ifdef X_HAVE_UTF8_STRING
1291 //printf("BC_WindowBase::dispatch_event %d\n", __LINE__);
1296 #ifdef X_HAVE_UTF8_STRING
1298 key_pressed_utf8 = keys_return;
1303 if( top_level == this )
1304 result = BC_KeyboardHandler::run_listeners(this);
1306 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
1308 result = dispatch_keypress_event();
1309 // Handle some default keypresses
1312 if(key_pressed == 'w' ||
1321 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
1322 dispatch_keyrelease_event();
1323 // printf("BC_WindowBase::dispatch_event KeyRelease keysym=0x%x keystate=0x%lld\n",
1324 // keysym, event->xkey.state);
1328 event_win = event->xany.window;
1329 dispatch_cursor_leave();
1333 event_win = event->xany.window;
1334 cursor_x = event->xcrossing.x;
1335 cursor_y = event->xcrossing.y;
1336 dispatch_cursor_enter();
1341 //printf("100 %s %p %d\n", title, event, event->type);
1342 //if(event->type != ClientMessage) dump();
1344 #ifndef SINGLE_THREAD
1346 if(event) delete event;
1348 // if(done) completion_lock->unlock();
1351 if(debug) printf("BC_WindowBase::dispatch_event this=%p %d\n", this, __LINE__);
1355 int BC_WindowBase::dispatch_expose_event()
1358 for(int i = 0; i < subwindows->total && !result; i++)
1360 result = subwindows->values[i]->dispatch_expose_event();
1363 // Propagate to user
1364 if(!result) expose_event();
1368 int BC_WindowBase::dispatch_resize_event(int w, int h)
1370 // Can't store new w and h until the event is handles
1371 // because bcfilebox depends on the old w and h to
1372 // reposition widgets.
1373 if( window_type == MAIN_WINDOW ) {
1378 pixmap = new BC_Pixmap(this, w, h);
1379 clear_box(0, 0, w, h);
1382 // Propagate to subwindows
1383 for(int i = 0; i < subwindows->total; i++) {
1384 subwindows->values[i]->dispatch_resize_event(w, h);
1387 // Propagate to user
1390 if( window_type == MAIN_WINDOW ) {
1399 int BC_WindowBase::dispatch_flash()
1402 for(int i = 0; i < subwindows->total; i++)
1403 subwindows->values[i]->dispatch_flash();
1407 int BC_WindowBase::dispatch_translation_event()
1409 translation_events = 0;
1410 if(window_type == MAIN_WINDOW)
1414 x = last_translate_x;
1415 y = last_translate_y;
1416 // Correct for window manager offsets
1421 for(int i = 0; i < subwindows->total; i++)
1423 subwindows->values[i]->dispatch_translation_event();
1426 translation_event();
1430 int BC_WindowBase::dispatch_motion_event()
1435 if(top_level == this)
1438 event_win = last_motion_win;
1439 get_key_masks(last_motion_state);
1442 if(get_button_down() && !active_menubar && !active_popup_menu)
1446 cursor_x = last_motion_x;
1447 cursor_y = last_motion_y;
1448 result = dispatch_drag_motion();
1452 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
1453 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
1458 result = dispatch_drag_start();
1462 cursor_x = last_motion_x;
1463 cursor_y = last_motion_y;
1465 // printf("BC_WindowBase::dispatch_motion_event %d %p %p %p\n",
1468 // active_popup_menu,
1469 // active_subwindow);
1471 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
1472 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
1473 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
1476 // Dispatch in stacking order
1477 for(int i = subwindows->size() - 1; i >= 0 && !result; i--)
1479 result = subwindows->values[i]->dispatch_motion_event();
1482 if(!result) result = cursor_motion_event(); // give to user
1486 int BC_WindowBase::dispatch_keypress_event()
1489 if(top_level == this)
1491 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
1494 for(int i = 0; i < subwindows->total && !result; i++)
1496 result = subwindows->values[i]->dispatch_keypress_event();
1499 if(!result) result = keypress_event();
1504 int BC_WindowBase::dispatch_keyrelease_event()
1507 if(top_level == this)
1509 if(active_subwindow) result = active_subwindow->dispatch_keyrelease_event();
1512 for(int i = 0; i < subwindows->total && !result; i++)
1514 result = subwindows->values[i]->dispatch_keyrelease_event();
1517 if(!result) result = keyrelease_event();
1522 int BC_WindowBase::dispatch_focus_in()
1524 for(int i = 0; i < subwindows->total; i++)
1526 subwindows->values[i]->dispatch_focus_in();
1534 int BC_WindowBase::dispatch_focus_out()
1536 for(int i = 0; i < subwindows->total; i++)
1538 subwindows->values[i]->dispatch_focus_out();
1546 int BC_WindowBase::get_has_focus()
1548 return top_level->has_focus;
1551 int BC_WindowBase::get_deleting()
1553 if(is_deleting) return 1;
1554 if(parent_window && parent_window->get_deleting()) return 1;
1558 int BC_WindowBase::dispatch_button_press()
1563 if(top_level == this)
1565 if(active_menubar) result = active_menubar->dispatch_button_press();
1566 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1567 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1570 for(int i = 0; i < subwindows->total && !result; i++)
1572 result = subwindows->values[i]->dispatch_button_press();
1575 if(!result) result = button_press_event();
1581 int BC_WindowBase::dispatch_button_release()
1584 if(top_level == this)
1586 if(active_menubar) result = active_menubar->dispatch_button_release();
1587 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1588 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1589 if(!result && button_number != 4 && button_number != 5)
1590 result = dispatch_drag_stop();
1593 for(int i = 0; i < subwindows->total && !result; i++)
1595 result = subwindows->values[i]->dispatch_button_release();
1600 result = button_release_event();
1607 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1610 // all repeat event handlers get called and decide based on activity and duration
1611 // whether to respond
1612 for(int i = 0; i < subwindows->total; i++)
1614 subwindows->values[i]->dispatch_repeat_event(duration);
1618 repeat_event(duration);
1622 // Unlock next repeat signal
1623 if(window_type == MAIN_WINDOW)
1625 #ifdef SINGLE_THREAD
1626 BC_Display::display_global->unlock_repeaters(duration);
1628 for(int i = 0; i < repeaters.total; i++)
1630 if(repeaters.values[i]->delay == duration)
1632 repeaters.values[i]->repeat_lock->unlock();
1640 void BC_WindowBase::unhide_cursor()
1645 if(top_level->is_hourglass)
1646 set_cursor(HOURGLASS_CURSOR, 1, 0);
1648 set_cursor(current_cursor, 1, 0);
1650 cursor_timer->update();
1654 void BC_WindowBase::update_video_cursor()
1656 if(video_on && !is_transparent)
1658 if(cursor_timer->get_difference() > VIDEO_CURSOR_TIMEOUT && !is_transparent)
1661 set_cursor(TRANSPARENT_CURSOR, 1, 1);
1662 cursor_timer->update();
1667 cursor_timer->update();
1672 int BC_WindowBase::dispatch_cursor_leave()
1676 for(int i = 0; i < subwindows->total; i++)
1678 subwindows->values[i]->dispatch_cursor_leave();
1681 cursor_leave_event();
1685 int BC_WindowBase::dispatch_cursor_enter()
1691 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1692 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1693 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1695 for(int i = 0; !result && i < subwindows->total; i++)
1697 result = subwindows->values[i]->dispatch_cursor_enter();
1700 if(!result) result = cursor_enter_event();
1704 int BC_WindowBase::cursor_enter_event()
1709 int BC_WindowBase::cursor_leave_event()
1714 int BC_WindowBase::close_event()
1720 int BC_WindowBase::dispatch_drag_start()
1723 if(active_menubar) result = active_menubar->dispatch_drag_start();
1724 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1725 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1727 for(int i = 0; i < subwindows->total && !result; i++)
1729 result = subwindows->values[i]->dispatch_drag_start();
1732 if(!result) result = is_dragging = drag_start_event();
1736 int BC_WindowBase::dispatch_drag_stop()
1740 for(int i = 0; i < subwindows->total && !result; i++)
1742 result = subwindows->values[i]->dispatch_drag_stop();
1745 if(is_dragging && !result)
1755 int BC_WindowBase::dispatch_drag_motion()
1758 for(int i = 0; i < subwindows->total && !result; i++)
1760 result = subwindows->values[i]->dispatch_drag_motion();
1763 if(is_dragging && !result)
1765 drag_motion_event();
1776 int BC_WindowBase::show_tooltip(int w, int h)
1780 if(tooltip_text && !tooltip_on &&
1781 (force_tooltip || get_resources()->tooltips_enabled))
1784 top_level->hide_tooltip();
1788 w = get_text_width(MEDIUMFONT, tooltip_text);
1791 h = get_text_height(MEDIUMFONT, tooltip_text);
1793 w += TOOLTIP_MARGIN * 2;
1794 h += TOOLTIP_MARGIN * 2;
1796 XTranslateCoordinates(top_level->display, win,
1797 top_level->rootwin, get_w(), get_h(),
1799 // keep the tip inside the window/display
1800 int top_x = top_level->get_x();
1801 if( x < top_x ) x = top_x;
1802 int top_w = top_x + top_level->get_w();
1803 int lmt_w = top_level->get_screen_x(0, -1) + top_level->get_screen_w(0, -1);
1804 if( top_w < lmt_w ) lmt_w = top_w;
1805 if( x+w > lmt_w ) x = lmt_w-w;
1807 int top_y = top_level->get_y();
1808 if( y < top_y ) y = top_y;
1809 int top_h = top_y + top_level->get_h();
1810 int lmt_h = top_level->get_root_h(0);
1811 if( top_h < lmt_h ) lmt_h = top_h;
1812 if( y+h > lmt_h ) y = lmt_h-h;
1814 int abs_x, abs_y, win_x, win_y;
1815 unsigned int temp_mask;
1817 XQueryPointer(top_level->display, top_level->win,
1818 &temp_win, &temp_win, &abs_x, &abs_y,
1819 &win_x, &win_y, &temp_mask);
1820 // check for cursor inside popup
1821 if( x < abs_x && abs_x < x+w && y < abs_y && abs_y < y+h )
1823 if( x-abs_x < y-abs_y )
1828 tooltip_popup = new BC_Popup(top_level, x, y, w, h,
1829 get_resources()->tooltip_bg_color);
1832 tooltip_popup->set_font(MEDIUMFONT);
1833 tooltip_popup->flash();
1834 tooltip_popup->flush();
1839 int BC_WindowBase::hide_tooltip()
1842 for(int i = 0; i < subwindows->total; i++)
1844 subwindows->values[i]->hide_tooltip();
1850 delete tooltip_popup;
1856 const char *BC_WindowBase::get_tooltip()
1858 return tooltip_text;
1861 int BC_WindowBase::set_tooltip(const char *text)
1863 tooltip_text = text;
1865 // Update existing tooltip if it is visible
1869 tooltip_popup->flash();
1874 // signal the event handler to repeat
1875 int BC_WindowBase::set_repeat(int64_t duration)
1879 printf("BC_WindowBase::set_repeat duration=%jd\n", duration);
1882 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1884 #ifdef SINGLE_THREAD
1885 BC_Display::display_global->set_repeat(this, duration);
1887 // test repeater database for duplicates
1888 for(int i = 0; i < repeaters.total; i++)
1891 if(repeaters.values[i]->delay == duration)
1893 repeaters.values[i]->start_repeating(this);
1898 BC_Repeater *repeater = new BC_Repeater(this, duration);
1899 repeater->initialize();
1900 repeaters.append(repeater);
1901 repeater->start_repeating();
1906 int BC_WindowBase::unset_repeat(int64_t duration)
1908 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1910 #ifdef SINGLE_THREAD
1911 BC_Display::display_global->unset_repeat(this, duration);
1913 for(int i = 0; i < repeaters.total; i++)
1915 if(repeaters.values[i]->delay == duration)
1917 repeaters.values[i]->stop_repeating();
1925 int BC_WindowBase::unset_all_repeaters()
1927 #ifdef SINGLE_THREAD
1928 BC_Display::display_global->unset_all_repeaters(this);
1930 for(int i = 0; i < repeaters.total; i++)
1932 repeaters.values[i]->stop_repeating();
1934 repeaters.remove_all_objects();
1939 // long BC_WindowBase::get_repeat_id()
1941 // return top_level->next_repeat_id++;
1944 XEvent *BC_WindowBase::new_xevent()
1946 XEvent *event = new XEvent;
1947 memset(event, 0, sizeof(*event));
1951 #ifndef SINGLE_THREAD
1952 int BC_WindowBase::arm_repeat(int64_t duration)
1954 XEvent *event = new_xevent();
1955 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1956 ptr->type = ClientMessage;
1957 ptr->message_type = RepeaterXAtom;
1959 ptr->data.l[0] = duration;
1961 // Couldn't use XSendEvent since it locked up randomly.
1967 int BC_WindowBase::recieve_custom_xatoms(xatom_event *event)
1972 int BC_WindowBase::send_custom_xatom(xatom_event *event)
1974 #ifndef SINGLE_THREAD
1975 XEvent *myevent = new_xevent();
1976 XClientMessageEvent *ptr = (XClientMessageEvent*)myevent;
1977 ptr->type = ClientMessage;
1978 ptr->message_type = event->message_type;
1979 ptr->format = event->format;
1980 ptr->data.l[0] = event->data.l[0];
1981 ptr->data.l[1] = event->data.l[1];
1982 ptr->data.l[2] = event->data.l[2];
1983 ptr->data.l[3] = event->data.l[3];
1984 ptr->data.l[4] = event->data.l[4];
1993 Atom BC_WindowBase::create_xatom(const char *atom_name)
1995 return XInternAtom(display, atom_name, False);
1998 int BC_WindowBase::get_atoms()
2000 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
2001 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
2002 DestroyAtom = XInternAtom(display, "BC_DESTROY_WINDOW", False);
2003 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
2004 if( (ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False)) != 0 )
2005 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32,
2006 PropModeReplace, (unsigned char *)&DelWinXAtom, True);
2012 void BC_WindowBase::init_cursors()
2014 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
2015 cross_cursor = XCreateFontCursor(display, XC_crosshair);
2016 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
2017 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
2018 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
2019 move_cursor = XCreateFontCursor(display, XC_fleur);
2020 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
2021 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
2022 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
2023 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
2024 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
2025 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
2026 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
2027 hourglass_cursor = XCreateFontCursor(display, XC_watch);
2030 char cursor_data[] = { 0,0,0,0, 0,0,0,0 };
2031 Colormap colormap = DefaultColormap(display, screen);
2032 Pixmap pixmap_bottom = XCreateBitmapFromData(display,
2033 rootwin, cursor_data, 8, 8);
2034 XColor black, dummy;
2035 XAllocNamedColor(display, colormap, "black", &black, &dummy);
2036 transparent_cursor = XCreatePixmapCursor(display,
2037 pixmap_bottom, pixmap_bottom, &black, &black, 0, 0);
2038 // XDefineCursor(display, win, transparent_cursor);
2039 XFreePixmap(display, pixmap_bottom);
2042 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
2044 int color_model = BC_TRANSPARENCY;
2048 color_model = BC_RGB8;
2051 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
2054 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
2057 color_model = server_byte_order ? BC_BGR8888 : BC_ARGB8888;
2063 int BC_WindowBase::init_colors()
2066 current_color_value = current_color_pixel = 0;
2068 // Get the real depth
2071 ximage = XCreateImage(top_level->display,
2073 top_level->default_depth,
2081 bits_per_pixel = ximage->bits_per_pixel;
2082 XDestroyImage(ximage);
2084 color_model = evaluate_color_model(client_byte_order,
2087 // Get the color model
2093 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
2094 create_private_colors();
2098 cmap = DefaultColormap(display, screen);
2099 create_shared_colors();
2102 allocate_color_table();
2106 //cmap = DefaultColormap(display, screen);
2107 cmap = XCreateColormap(display, rootwin, vis, AllocNone );
2113 int BC_WindowBase::create_private_colors()
2118 for(int i = 0; i < 255; i++)
2120 color = (i & 0xc0) << 16;
2121 color += (i & 0x38) << 10;
2122 color += (i & 0x7) << 5;
2123 color_table[i][0] = color;
2125 create_shared_colors(); // overwrite the necessary colors on the table
2130 int BC_WindowBase::create_color(int color)
2132 if(total_colors == 256)
2134 // replace the closest match with an exact match
2135 color_table[get_color_rgb8(color)][0] = color;
2139 // add the color to the table
2140 color_table[total_colors][0] = color;
2146 int BC_WindowBase::create_shared_colors()
2148 create_color(BLACK);
2149 create_color(WHITE);
2151 create_color(LTGREY);
2152 create_color(MEGREY);
2153 create_color(MDGREY);
2154 create_color(DKGREY);
2156 create_color(LTCYAN);
2157 create_color(MECYAN);
2158 create_color(MDCYAN);
2159 create_color(DKCYAN);
2161 create_color(LTGREEN);
2162 create_color(GREEN);
2163 create_color(DKGREEN);
2165 create_color(LTPINK);
2169 create_color(LTBLUE);
2171 create_color(DKBLUE);
2173 create_color(LTYELLOW);
2174 create_color(MEYELLOW);
2175 create_color(MDYELLOW);
2176 create_color(DKYELLOW);
2178 create_color(LTPURPLE);
2179 create_color(MEPURPLE);
2180 create_color(MDPURPLE);
2181 create_color(DKPURPLE);
2183 create_color(FGGREY);
2184 create_color(MNBLUE);
2185 create_color(ORANGE);
2186 create_color(FTGREY);
2191 int BC_WindowBase::allocate_color_table()
2193 int red, green, blue, color;
2196 for(int i = 0; i < total_colors; i++)
2198 color = color_table[i][0];
2199 red = (color & 0xFF0000) >> 16;
2200 green = (color & 0x00FF00) >> 8;
2201 blue = color & 0xFF;
2203 col.flags = DoRed | DoGreen | DoBlue;
2204 col.red = red<<8 | red;
2205 col.green = green<<8 | green;
2206 col.blue = blue<<8 | blue;
2208 XAllocColor(display, cmap, &col);
2209 color_table[i][1] = col.pixel;
2212 XInstallColormap(display, cmap);
2216 int BC_WindowBase::init_window_shape()
2218 if(bg_pixmap && bg_pixmap->use_alpha())
2220 XShapeCombineMask(top_level->display,
2221 this->win, ShapeBounding, 0, 0,
2222 bg_pixmap->get_alpha(), ShapeSet);
2228 int BC_WindowBase::init_gc()
2230 unsigned long gcmask;
2231 gcmask = GCFont | GCGraphicsExposures;
2234 gcvalues.font = mediumfont->fid; // set the font
2235 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
2236 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
2238 // gcmask = GCCapStyle | GCJoinStyle;
2239 // XGetGCValues(display, gc, gcmask, &gcvalues);
2240 // printf("BC_WindowBase::init_gc %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2244 int BC_WindowBase::init_fonts()
2246 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font))) )
2247 if( !(smallfont = XLoadQueryFont(display, _(resources.small_font2))) )
2248 smallfont = XLoadQueryFont(display, "fixed");
2249 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font))) )
2250 if( !(mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) )
2251 mediumfont = XLoadQueryFont(display, "fixed");
2252 if( !(largefont = XLoadQueryFont(display, _(resources.large_font))) )
2253 if( !(largefont = XLoadQueryFont(display, _(resources.large_font2))) )
2254 largefont = XLoadQueryFont(display, "fixed");
2255 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font))) )
2256 if( !(bigfont = XLoadQueryFont(display, _(resources.big_font2))) )
2257 bigfont = XLoadQueryFont(display, "fixed");
2260 if(get_resources()->use_fontset)
2265 // FIXME: should check the m,d,n values
2266 smallfontset = XCreateFontSet(display, resources.small_fontset, &m, &n, &d);
2268 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2269 mediumfontset = XCreateFontSet(display, resources.medium_fontset, &m, &n, &d);
2270 if( !mediumfontset )
2271 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2272 largefontset = XCreateFontSet(display, resources.large_fontset, &m, &n, &d);
2274 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2275 bigfontset = XCreateFontSet(display, resources.big_fontset, &m, &n, &d);
2277 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
2278 if(bigfontset && largefontset && mediumfontset && smallfontset) {
2279 curr_fontset = mediumfontset;
2280 get_resources()->use_fontset = 1;
2284 get_resources()->use_fontset = 0;
2291 void BC_WindowBase::init_xft()
2294 if( !get_resources()->use_xft ) return;
2295 if(!(smallfont_xft =
2296 (resources.small_font_xft[0] == '-' ?
2297 XftFontOpenXlfd(display, screen, resources.small_font_xft) :
2298 XftFontOpenName(display, screen, resources.small_font_xft))) )
2299 if(!(smallfont_xft =
2300 XftFontOpenXlfd(display, screen, resources.small_font_xft2)))
2301 smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2302 if(!(mediumfont_xft =
2303 (resources.medium_font_xft[0] == '-' ?
2304 XftFontOpenXlfd(display, screen, resources.medium_font_xft) :
2305 XftFontOpenName(display, screen, resources.medium_font_xft))) )
2306 if(!(mediumfont_xft =
2307 XftFontOpenXlfd(display, screen, resources.medium_font_xft2)))
2308 mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2309 if(!(largefont_xft =
2310 (resources.large_font_xft[0] == '-' ?
2311 XftFontOpenXlfd(display, screen, resources.large_font_xft) :
2312 XftFontOpenName(display, screen, resources.large_font_xft))) )
2313 if(!(largefont_xft =
2314 XftFontOpenXlfd(display, screen, resources.large_font_xft2)))
2315 largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2317 (resources.big_font_xft[0] == '-' ?
2318 XftFontOpenXlfd(display, screen, resources.big_font_xft) :
2319 XftFontOpenName(display, screen, resources.big_font_xft))) )
2321 XftFontOpenXlfd(display, screen, resources.big_font_xft2)))
2322 bigfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2324 if(!(bold_smallfont_xft =
2325 (resources.small_b_font_xft[0] == '-' ?
2326 XftFontOpenXlfd(display, screen, resources.small_b_font_xft) :
2327 XftFontOpenName(display, screen, resources.small_b_font_xft))) )
2328 bold_smallfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2329 if(!(bold_mediumfont_xft =
2330 (resources.medium_b_font_xft[0] == '-' ?
2331 XftFontOpenXlfd(display, screen, resources.medium_b_font_xft) :
2332 XftFontOpenName(display, screen, resources.medium_b_font_xft))) )
2333 bold_mediumfont_xft = XftFontOpenXlfd(display, screen, "fixed");
2334 if(!(bold_largefont_xft =
2335 (resources.large_b_font_xft[0] == '-' ?
2336 XftFontOpenXlfd(display, screen, resources.large_b_font_xft) :
2337 XftFontOpenName(display, screen, resources.large_b_font_xft))) )
2338 bold_largefont_xft = XftFontOpenXlfd(display, screen, "fixed");
2340 // Extension failed to locate fonts
2341 if( !smallfont_xft || !mediumfont_xft || !largefont_xft || !bigfont_xft ||
2342 !bold_largefont_xft || !bold_mediumfont_xft || !bold_largefont_xft ) {
2343 printf("BC_WindowBase::init_fonts: no xft fonts found:"
2344 " %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n %s=%p\n",
2345 resources.small_font_xft, smallfont_xft,
2346 resources.medium_font_xft, mediumfont_xft,
2347 resources.large_font_xft, largefont_xft,
2348 resources.big_font_xft, bigfont_xft,
2349 resources.small_b_font_xft, bold_smallfont_xft,
2350 resources.medium_b_font_xft, bold_mediumfont_xft,
2351 resources.large_b_font_xft, bold_largefont_xft);
2352 get_resources()->use_xft = 0;
2358 void BC_WindowBase::init_im()
2360 XIMStyles *xim_styles;
2363 if(!(input_method = XOpenIM(display, NULL, NULL, NULL)))
2365 printf("BC_WindowBase::init_im: Could not open input method.\n");
2368 if(XGetIMValues(input_method, XNQueryInputStyle, &xim_styles, NULL) ||
2371 printf("BC_WindowBase::init_im: Input method doesn't support any styles.\n");
2372 XCloseIM(input_method);
2377 for(int z = 0; z < xim_styles->count_styles; z++)
2379 if(xim_styles->supported_styles[z] == (XIMPreeditNothing | XIMStatusNothing))
2381 xim_style = xim_styles->supported_styles[z];
2389 printf("BC_WindowBase::init_im: Input method doesn't support the style we need.\n");
2390 XCloseIM(input_method);
2394 input_context = XCreateIC(input_method, XNInputStyle, xim_style,
2395 XNClientWindow, win, XNFocusWindow, win, NULL);
2398 printf("BC_WindowBase::init_im: Failed to create input context.\n");
2399 XCloseIM(input_method);
2404 void BC_WindowBase::finit_im()
2406 if( input_context ) {
2407 XDestroyIC(input_context);
2410 if( input_method ) {
2411 XCloseIM(input_method);
2417 int BC_WindowBase::get_color(int64_t color)
2419 // return pixel of color
2420 // use this only for drawing subwindows not for bitmaps
2421 int i, test, difference;
2427 return get_color_rgb8(color);
2428 // test last color looked up
2429 if(current_color_value == color)
2430 return current_color_pixel;
2433 current_color_value = color;
2434 for(i = 0; i < total_colors; i++)
2436 if(color_table[i][0] == color)
2438 current_color_pixel = color_table[i][1];
2439 return current_color_pixel;
2443 // find nearest match
2444 difference = 0xFFFFFF;
2446 for(i = 0; i < total_colors; i++)
2448 test = abs((int)(color_table[i][0] - color));
2450 if(test < difference)
2452 current_color_pixel = color_table[i][1];
2456 return current_color_pixel;
2459 return get_color_rgb16(color);
2462 return get_color_bgr16(color);
2466 return client_byte_order == server_byte_order ?
2467 color : get_color_bgr24(color);
2475 int BC_WindowBase::get_color_rgb8(int color)
2479 pixel = (color & 0xc00000) >> 16;
2480 pixel += (color & 0xe000) >> 10;
2481 pixel += (color & 0xe0) >> 5;
2485 int64_t BC_WindowBase::get_color_rgb16(int color)
2488 result = (color & 0xf80000) >> 8;
2489 result += (color & 0xfc00) >> 5;
2490 result += (color & 0xf8) >> 3;
2495 int64_t BC_WindowBase::get_color_bgr16(int color)
2498 result = (color & 0xf80000) >> 19;
2499 result += (color & 0xfc00) >> 5;
2500 result += (color & 0xf8) << 8;
2505 int64_t BC_WindowBase::get_color_bgr24(int color)
2508 result = (color & 0xff) << 16;
2509 result += (color & 0xff00);
2510 result += (color & 0xff0000) >> 16;
2514 void BC_WindowBase::start_video()
2516 cursor_timer->update();
2518 // set_color(BLACK);
2519 // draw_box(0, 0, get_w(), get_h());
2523 void BC_WindowBase::stop_video()
2531 int64_t BC_WindowBase::get_color()
2533 return top_level->current_color;
2536 void BC_WindowBase::set_color(int64_t color)
2538 top_level->current_color = color;
2539 XSetForeground(top_level->display,
2541 top_level->get_color(color));
2544 void BC_WindowBase::set_opaque()
2546 XSetFunction(top_level->display, top_level->gc, GXcopy);
2549 void BC_WindowBase::set_inverse()
2551 XSetFunction(top_level->display, top_level->gc, GXxor);
2554 void BC_WindowBase::set_line_width(int value)
2556 this->line_width = value;
2557 XSetLineAttributes(top_level->display, top_level->gc, value, /* line_width */
2558 line_dashes == 0 ? LineSolid : LineOnOffDash, /* line_style */
2559 line_dashes == 0 ? CapRound : CapNotLast, /* cap_style */
2560 JoinMiter); /* join_style */
2562 if(line_dashes > 0) {
2563 const char dashes = line_dashes;
2564 XSetDashes(top_level->display, top_level->gc, 0, &dashes, 1);
2567 // XGCValues gcvalues;
2568 // unsigned long gcmask;
2569 // gcmask = GCCapStyle | GCJoinStyle;
2570 // XGetGCValues(top_level->display, top_level->gc, gcmask, &gcvalues);
2571 // printf("BC_WindowBase::set_line_width %d %d %d\n", __LINE__, gcvalues.cap_style, gcvalues.join_style);
2574 void BC_WindowBase::set_line_dashes(int value)
2576 line_dashes = value;
2577 // call XSetLineAttributes
2578 set_line_width(line_width);
2582 Cursor BC_WindowBase::get_cursor_struct(int cursor)
2586 case ARROW_CURSOR: return top_level->arrow_cursor; break;
2587 case CROSS_CURSOR: return top_level->cross_cursor;
2588 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
2589 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
2590 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
2591 case MOVE_CURSOR: return top_level->move_cursor; break;
2592 case LEFT_CURSOR: return top_level->left_cursor; break;
2593 case RIGHT_CURSOR: return top_level->right_cursor; break;
2594 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
2595 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
2596 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
2597 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
2598 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
2599 case HOURGLASS_CURSOR: return top_level->hourglass_cursor; break;
2600 case TRANSPARENT_CURSOR: return top_level->transparent_cursor; break;
2605 void BC_WindowBase::set_cursor(int cursor, int override, int flush)
2607 // inherit cursor from parent
2610 XUndefineCursor(top_level->display, win);
2611 current_cursor = cursor;
2614 // don't change cursor if overridden
2615 if((!top_level->is_hourglass && !is_transparent) ||
2618 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
2619 if(flush) this->flush();
2622 if(!override) current_cursor = cursor;
2625 void BC_WindowBase::set_x_cursor(int cursor)
2627 temp_cursor = XCreateFontCursor(top_level->display, cursor);
2628 XDefineCursor(top_level->display, win, temp_cursor);
2629 current_cursor = cursor;
2633 int BC_WindowBase::get_cursor()
2635 return current_cursor;
2638 void BC_WindowBase::start_hourglass()
2640 top_level->start_hourglass_recursive();
2644 void BC_WindowBase::stop_hourglass()
2646 top_level->stop_hourglass_recursive();
2650 void BC_WindowBase::start_hourglass_recursive()
2652 if(this == top_level)
2660 set_cursor(HOURGLASS_CURSOR, 1, 0);
2661 for(int i = 0; i < subwindows->total; i++)
2663 subwindows->values[i]->start_hourglass_recursive();
2668 void BC_WindowBase::stop_hourglass_recursive()
2670 if(this == top_level)
2672 if(hourglass_total == 0) return;
2673 top_level->hourglass_total--;
2676 if(!top_level->hourglass_total)
2678 top_level->is_hourglass = 0;
2680 // Cause set_cursor to perform change
2682 set_cursor(current_cursor, 1, 0);
2684 for(int i = 0; i < subwindows->total; i++)
2686 subwindows->values[i]->stop_hourglass_recursive();
2694 XFontStruct* BC_WindowBase::get_font_struct(int font)
2696 // Clear out unrelated flags
2697 if(font & BOLDFACE) font ^= BOLDFACE;
2700 case SMALLFONT: return top_level->smallfont; break;
2701 case MEDIUMFONT: return top_level->mediumfont; break;
2702 case LARGEFONT: return top_level->largefont; break;
2703 case BIGFONT: return top_level->bigfont; break;
2708 XFontSet BC_WindowBase::get_fontset(int font)
2712 if(get_resources()->use_fontset)
2714 switch(font & 0xff) {
2715 case SMALLFONT: fs = top_level->smallfontset; break;
2716 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2717 case LARGEFONT: fs = top_level->largefontset; break;
2718 case BIGFONT: fs = top_level->bigfontset; break;
2726 XftFont* BC_WindowBase::get_xft_struct(int font)
2729 case SMALLFONT: return (XftFont*)top_level->smallfont_xft;
2730 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft;
2731 case LARGEFONT: return (XftFont*)top_level->largefont_xft;
2732 case BIGFONT: return (XftFont*)top_level->bigfont_xft;
2733 case MEDIUMFONT_3D: return (XftFont*)top_level->bold_mediumfont_xft;
2734 case SMALLFONT_3D: return (XftFont*)top_level->bold_smallfont_xft;
2735 case LARGEFONT_3D: return (XftFont*)top_level->bold_largefont_xft;
2750 int BC_WindowBase::get_current_font()
2752 return top_level->current_font;
2755 void BC_WindowBase::set_font(int font)
2757 top_level->current_font = font;
2760 if(get_resources()->use_xft) {}
2763 if(get_resources()->use_fontset) {
2767 if(get_font_struct(font))
2769 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2775 void BC_WindowBase::set_fontset(int font)
2779 if(get_resources()->use_fontset) {
2781 case SMALLFONT: fs = top_level->smallfontset; break;
2782 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2783 case LARGEFONT: fs = top_level->largefontset; break;
2784 case BIGFONT: fs = top_level->bigfontset; break;
2792 XFontSet BC_WindowBase::get_curr_fontset(void)
2794 if(get_resources()->use_fontset)
2795 return curr_fontset;
2799 int BC_WindowBase::get_single_text_width(int font, const char *text, int length)
2802 if(get_resources()->use_xft && get_xft_struct(font))
2805 #ifdef X_HAVE_UTF8_STRING
2806 if(get_resources()->locale_utf8)
2808 XftTextExtentsUtf8(top_level->display,
2809 get_xft_struct(font),
2810 (const XftChar8 *)text,
2817 XftTextExtents8(top_level->display,
2818 get_xft_struct(font),
2819 (const XftChar8 *)text,
2823 return extents.xOff;
2827 if(get_resources()->use_fontset && top_level->get_fontset(font))
2828 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2830 if(get_font_struct(font))
2831 return XTextWidth(get_font_struct(font), text, length);
2837 case MEDIUM_7SEGMENT:
2838 return get_resources()->medium_7segment[0]->get_w() * length;
2848 int BC_WindowBase::get_single_text_width(int font, const wchar_t *text, int length)
2851 if(get_resources()->use_xft && get_xft_struct(font)) {
2854 XftTextExtents32(top_level->display, get_xft_struct(font),
2855 (const FcChar32*)text, length, &extents);
2856 return extents.xOff;
2859 if(!get_font_struct(font)) return 0;
2860 XChar2b xtext[length], *xp = xtext;
2861 for( int i=0; i<length; ++i,++xp ) {
2862 xp->byte1 = (unsigned char) (text[i] >> 8);
2863 xp->byte2 = (unsigned char) (text[i] & 0xff);
2865 return XTextWidth16(get_font_struct(font), xtext, length);
2868 int BC_WindowBase::get_text_width(int font, const char *text, int length)
2870 int i, j, w = 0, line_w = 0;
2871 if(length < 0) length = strlen(text);
2873 for(i = 0, j = 0; i <= length; i++)
2878 line_w = get_single_text_width(font, &text[j], i - j);
2884 line_w = get_single_text_width(font, &text[j], length - j);
2886 if(line_w > w) w = line_w;
2889 if(i > length && w == 0)
2891 w = get_single_text_width(font, text, length);
2897 int BC_WindowBase::get_text_width(int font, const wchar_t *text, int length)
2900 if( length < 0 ) length = wcslen(text);
2902 for( i=j=0; i<length && text[i]; ++i ) {
2903 if( text[i] != '\n' ) continue;
2905 int lw = get_single_text_width(font, &text[j], i-j);
2906 if( w < lw ) w = lw;
2911 int lw = get_single_text_width(font, &text[j], length-j);
2912 if( w < lw ) w = lw;
2918 int BC_WindowBase::get_text_ascent(int font)
2922 if( (fstruct = get_xft_struct(font)) != 0 )
2923 return fstruct->ascent;
2925 if(get_resources()->use_fontset && top_level->get_fontset(font))
2927 XFontSetExtents *extents;
2929 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2930 return -extents->max_logical_extent.y;
2933 if(get_font_struct(font))
2934 return top_level->get_font_struct(font)->ascent;
2937 case MEDIUM_7SEGMENT:
2938 return get_resources()->medium_7segment[0]->get_h();
2943 int BC_WindowBase::get_text_descent(int font)
2947 if( (fstruct = get_xft_struct(font)) != 0 )
2948 return fstruct->descent;
2950 if(get_resources()->use_fontset && top_level->get_fontset(font)) {
2951 XFontSetExtents *extents;
2952 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2953 return (extents->max_logical_extent.height
2954 + extents->max_logical_extent.y);
2957 if(get_font_struct(font))
2958 return top_level->get_font_struct(font)->descent;
2963 int BC_WindowBase::get_text_height(int font, const char *text)
2968 if( (fstruct = get_xft_struct(font)) != 0 )
2969 rowh = fstruct->height;
2972 rowh = get_text_ascent(font) + get_text_descent(font);
2974 if(!text) return rowh;
2976 // Add height of lines
2977 int h = 0, i, length = strlen(text);
2978 for(i = 0; i <= length; i++)
2989 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2991 if(color_model < 0) color_model = top_level->get_color_model();
2992 return new BC_Bitmap(top_level, w, h, color_model);
2995 void BC_WindowBase::init_wait()
2997 #ifndef SINGLE_THREAD
2998 if(window_type != MAIN_WINDOW)
2999 top_level->init_wait();
3000 init_lock->lock("BC_WindowBase::init_wait");
3001 init_lock->unlock();
3005 int BC_WindowBase::accel_available(int color_model, int lock_it)
3007 if( window_type != MAIN_WINDOW )
3008 return top_level->accel_available(color_model, lock_it);
3010 lock_window("BC_WindowBase::accel_available");
3012 switch(color_model) {
3014 grab_port_id(color_model);
3018 grab_port_id(color_model);
3027 //printf("BC_WindowBase::accel_available %d %d\n", color_model, xvideo_port_id);
3028 return xvideo_port_id >= 0 ? 1 : 0;
3032 int BC_WindowBase::grab_port_id(int color_model)
3034 if( !get_resources()->use_xvideo || // disabled
3035 !get_resources()->use_shm ) // Only local server is fast enough.
3037 if( xvideo_port_id >= 0 )
3038 return xvideo_port_id;
3040 unsigned int ver, rev, reqBase, eventBase, errorBase;
3041 if( Success != XvQueryExtension(display, // XV extension is available
3042 &ver, &rev, &reqBase, &eventBase, &errorBase) )
3045 // XV adaptors are available
3046 unsigned int numAdapt = 0;
3047 XvAdaptorInfo *info = 0;
3048 XvQueryAdaptors(display, DefaultRootWindow(display), &numAdapt, &info);
3052 // Translate from color_model to X color model
3053 int x_color_model = BC_CModels::bc_to_x(color_model);
3055 // Get adaptor with desired color model
3056 for( int i = 0; i < (int)numAdapt && xvideo_port_id == -1; i++) {
3057 if( !(info[i].type & XvImageMask) || !info[i].num_ports ) continue;
3058 // adaptor supports XvImages
3059 int numFormats = 0, numPorts = info[i].num_ports;
3060 XvImageFormatValues *formats =
3061 XvListImageFormats(display, info[i].base_id, &numFormats);
3062 if( !formats ) continue;
3064 for( int j=0; j<numFormats && xvideo_port_id<0; ++j ) {
3065 if( formats[j].id != x_color_model ) continue;
3066 // this adaptor supports the desired format, grab a port
3067 for( int k=0; k<numPorts; ++k ) {
3068 if( Success == XvGrabPort(top_level->display,
3069 info[i].base_id+k, CurrentTime) ) {
3070 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
3071 xvideo_port_id = info[i].base_id + k;
3079 XvFreeAdaptorInfo(info);
3081 return xvideo_port_id;
3085 int BC_WindowBase::show_window(int flush)
3087 for(int i = 0; i < subwindows->size(); i++)
3089 subwindows->get(i)->show_window(0);
3092 XMapWindow(top_level->display, win);
3093 if(flush) XFlush(top_level->display);
3094 // XSync(top_level->display, 0);
3099 int BC_WindowBase::hide_window(int flush)
3101 for(int i = 0; i < subwindows->size(); i++)
3103 subwindows->get(i)->hide_window(0);
3106 XUnmapWindow(top_level->display, win);
3107 if(flush) this->flush();
3112 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
3114 subwindows->append((BC_SubWindow*)menu_bar);
3116 menu_bar->parent_window = this;
3117 menu_bar->top_level = this->top_level;
3118 menu_bar->initialize();
3122 BC_WindowBase* BC_WindowBase::add_popup(BC_WindowBase *window)
3124 //printf("BC_WindowBase::add_popup window=%p win=%p\n", window, window->win);
3125 if(this != top_level) return top_level->add_popup(window);
3126 popups.append(window);
3130 void BC_WindowBase::remove_popup(BC_WindowBase *window)
3132 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3133 if(this != top_level)
3134 top_level->remove_popup(window);
3136 popups.remove(window);
3137 //printf("BC_WindowBase::remove_popup %d size=%d window=%p win=%p\n", __LINE__, popups.size(), window, window->win);
3141 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
3143 subwindows->append(subwindow);
3145 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
3147 // parent window must be set before the subwindow initialization
3148 subwindow->parent_window = this;
3149 subwindow->top_level = this->top_level;
3151 // Execute derived initialization
3152 subwindow->initialize();
3157 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
3159 return add_subwindow(subwindow);
3162 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
3164 if( !top_level->flash_enabled ) return 0;
3165 //printf("BC_WindowBase::flash %d %d %d %d %d\n", __LINE__, w, h, this->w, this->h);
3167 XSetWindowBackgroundPixmap(top_level->display, win, pixmap->opaque_pixmap);
3170 XClearArea(top_level->display, win, x, y, w, h, 0);
3174 XClearWindow(top_level->display, win);
3182 int BC_WindowBase::flash(int flush)
3184 flash(-1, -1, -1, -1, flush);
3188 void BC_WindowBase::flush()
3190 //if(!get_window_lock())
3191 // printf("BC_WindowBase::flush %s not locked\n", top_level->title);
3192 // X gets hosed if Flush/Sync are not user locked (at libX11-1.1.5 / libxcb-1.1.91)
3193 // _XReply deadlocks in condition_wait waiting for xlib lock when waiters!=-1
3194 int locked = get_window_lock();
3195 if( !locked ) lock_window("BC_WindowBase::flush");
3196 XFlush(top_level->display);
3197 if( !locked ) unlock_window();
3200 void BC_WindowBase::sync_display()
3202 int locked = get_window_lock();
3203 if( !locked ) lock_window("BC_WindowBase::sync_display");
3204 XSync(top_level->display, False);
3205 if( !locked ) unlock_window();
3208 int BC_WindowBase::get_window_lock()
3210 #ifdef SINGLE_THREAD
3211 return BC_Display::display_global->get_display_locked();
3213 return top_level->window_lock;
3217 int BC_WindowBase::lock_window(const char *location)
3219 if(top_level && top_level != this)
3221 top_level->lock_window(location);
3226 SET_LOCK(this, title, location);
3227 #ifdef SINGLE_THREAD
3228 BC_Display::lock_display(location);
3230 XLockDisplay(top_level->display);
3231 top_level->display_lock_owner = pthread_self();
3234 ++top_level->window_lock;
3238 printf("BC_WindowBase::lock_window top_level NULL\n");
3243 int BC_WindowBase::unlock_window()
3245 if(top_level && top_level != this)
3247 top_level->unlock_window();
3254 // if(!top_level->window_lock)
3256 // printf("BC_WindowBase::unlock_window %d %s already unlocked\n",
3260 if( top_level->window_lock > 0 )
3261 if( --top_level->window_lock == 0 )
3262 top_level->display_lock_owner = 0;
3263 #ifdef SINGLE_THREAD
3264 BC_Display::unlock_display();
3266 XUnlockDisplay(top_level->display);
3271 printf("BC_WindowBase::unlock_window top_level NULL\n");
3276 void BC_WindowBase::set_done(int return_value)
3278 if(done_set) return;
3280 if(window_type != MAIN_WINDOW)
3281 top_level->set_done(return_value);
3284 #ifdef SINGLE_THREAD
3285 this->return_value = return_value;
3286 BC_Display::display_global->arm_completion(this);
3287 completion_lock->unlock();
3288 #else // SINGLE_THREAD
3290 if( !event_thread ) return;
3291 XEvent *event = new_xevent();
3292 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
3293 event->type = ClientMessage;
3294 ptr->message_type = SetDoneXAtom;
3296 this->return_value = return_value;
3297 // May lock up here because XSendEvent doesn't work too well
3298 // asynchronous with XNextEvent.
3299 // This causes BC_WindowEvents to forward a copy of the event to run_window where
3301 // Deletion of event_thread is done at the end of BC_WindowBase::run_window() - by calling the destructor
3307 void BC_WindowBase::close(int return_value)
3309 hide_window(); flush();
3310 set_done(return_value);
3313 int BC_WindowBase::grab(BC_WindowBase *window)
3315 if( window->active_grab && this != window->active_grab ) return 0;
3316 window->active_grab = this;
3319 int BC_WindowBase::ungrab(BC_WindowBase *window)
3321 if( window->active_grab && this != window->active_grab ) return 0;
3322 window->active_grab = 0;
3326 int BC_WindowBase::get_w()
3331 int BC_WindowBase::get_h()
3336 int BC_WindowBase::get_x()
3341 int BC_WindowBase::get_y()
3347 // WidthOfScreen/HeightOfScreen of XDefaultScreenOfDisplay
3348 // this is the bounding box of all the screens
3350 int BC_WindowBase::get_root_w(int lock_display)
3352 if(lock_display) lock_window("BC_WindowBase::get_root_w");
3353 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3354 int result = WidthOfScreen(def_screen);
3355 if(lock_display) unlock_window();
3359 int BC_WindowBase::get_root_h(int lock_display)
3361 if(lock_display) lock_window("BC_WindowBase::get_root_h");
3362 Screen *def_screen = XDefaultScreenOfDisplay(top_level->display);
3363 int result = HeightOfScreen(def_screen);
3364 if(lock_display) unlock_window();
3368 XineramaScreenInfo *
3369 BC_WindowBase::get_xinerama_info(int screen)
3371 if( !xinerama_info || !xinerama_screens ) return 0;
3373 for( int i=0; i<xinerama_screens; ++i )
3374 if( xinerama_info[i].screen_number == screen )
3375 return &xinerama_info[i];
3378 int top_x = get_x(), top_y = get_y();
3379 for( int i=0; i<xinerama_screens; ++i ) {
3380 int scr_y = top_y - xinerama_info[i].y_org;
3381 if( scr_y < 0 || scr_y >= xinerama_info[i].height ) continue;
3382 int scr_x = top_x - xinerama_info[i].x_org;
3383 if( scr_x >= 0 && scr_x < xinerama_info[i].width )
3384 return &xinerama_info[i];
3389 int BC_WindowBase::get_screen_x(int lock_display, int screen)
3392 if(lock_display) lock_window("BC_WindowBase::get_screen_x");
3393 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3396 int root_w = get_root_w(0);
3397 int root_h = get_root_h(0);
3398 // Shift X based on position of current window if dual head
3399 if( (float)root_w/root_h > 1.8 ) {
3400 root_w = get_screen_w(0, 0);
3401 if( top_level->get_x() >= root_w )
3406 result = info->x_org;
3407 if(lock_display) unlock_window();
3411 int BC_WindowBase::get_screen_y(int lock_display, int screen)
3413 if(lock_display) lock_window("BC_WindowBase::get_screen_y");
3414 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3415 int result = !info ? 0 : info->y_org;
3416 if(lock_display) unlock_window();
3420 int BC_WindowBase::get_screen_w(int lock_display, int screen)
3423 if(lock_display) lock_window("BC_WindowBase::get_screen_w");
3424 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3426 int width = get_root_w(0);
3427 int height = get_root_h(0);
3428 if( (float)width/height > 1.8 ) {
3429 // If dual head, the screen width is > 16x9
3430 // but we only want to fill one screen
3431 // this code assumes the "big" screen is on the right
3432 int scr_w0 = width / 2;
3434 case 600: scr_w0 = 800; break;
3435 case 720: scr_w0 = 1280; break;
3436 case 1024: scr_w0 = 1280; break;
3437 case 1200: scr_w0 = 1600; break;
3438 case 1080: scr_w0 = 1920; break;
3440 int scr_w1 = width - scr_w0;
3441 result = screen > 0 ? scr_w1 :
3442 screen == 0 ? scr_w0 :
3443 top_level->get_x() < scr_w0 ? scr_w0 : scr_w1;
3449 result = info->width;
3450 if(lock_display) unlock_window();
3454 int BC_WindowBase::get_screen_h(int lock_display, int screen)
3456 if(lock_display) lock_window("BC_WindowBase::get_screen_h");
3457 XineramaScreenInfo *info = top_level->get_xinerama_info(screen);
3458 int result = info ? info->height : get_root_h(0);
3459 if(lock_display) unlock_window();
3463 // Bottom right corner
3464 int BC_WindowBase::get_x2()
3469 int BC_WindowBase::get_y2()
3474 int BC_WindowBase::get_video_on()
3479 int BC_WindowBase::get_hidden()
3481 return top_level->hidden;
3484 int BC_WindowBase::cursor_inside()
3486 return (top_level->cursor_x >= 0 &&
3487 top_level->cursor_y >= 0 &&
3488 top_level->cursor_x < w &&
3489 top_level->cursor_y < h);
3492 BC_WindowBase* BC_WindowBase::get_top_level()
3497 BC_WindowBase* BC_WindowBase::get_parent()
3499 return parent_window;
3502 int BC_WindowBase::get_color_model()
3504 return top_level->color_model;
3507 BC_Resources* BC_WindowBase::get_resources()
3509 return &BC_WindowBase::resources;
3512 BC_Synchronous* BC_WindowBase::get_synchronous()
3514 return BC_WindowBase::resources.get_synchronous();
3517 int BC_WindowBase::get_bg_color()
3522 void BC_WindowBase::set_bg_color(int color)
3524 this->bg_color = color;
3527 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
3532 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
3534 top_level->active_subwindow = subwindow;
3537 int BC_WindowBase::activate()
3542 int BC_WindowBase::deactivate()
3544 if(window_type == MAIN_WINDOW)
3546 if( top_level->active_menubar ) {
3547 top_level->active_menubar->deactivate();
3548 top_level->active_menubar = 0;
3550 if( top_level->active_popup_menu ) {
3551 top_level->active_popup_menu->deactivate();
3552 top_level->active_popup_menu = 0;
3554 if( top_level->active_subwindow ) {
3555 top_level->active_subwindow->deactivate();
3556 top_level->active_subwindow = 0;
3558 if( top_level->motion_events && top_level->last_motion_win == this->win )
3559 top_level->motion_events = 0;
3565 int BC_WindowBase::cycle_textboxes(int amount)
3568 BC_WindowBase *new_textbox = 0;
3572 BC_WindowBase *first_textbox = 0;
3573 find_next_textbox(&first_textbox, &new_textbox, result);
3574 if(!new_textbox) new_textbox = first_textbox;
3580 BC_WindowBase *last_textbox = 0;
3581 find_prev_textbox(&last_textbox, &new_textbox, result);
3582 if(!new_textbox) new_textbox = last_textbox;
3586 if(new_textbox != active_subwindow)
3589 new_textbox->activate();
3595 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
3597 // Search subwindows for textbox
3598 for(int i = 0; i < subwindows->total && result < 2; i++)
3600 BC_WindowBase *test_subwindow = subwindows->values[i];
3601 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
3608 if(!*first_textbox) *first_textbox = this;
3612 if(top_level->active_subwindow == this)
3618 *next_textbox = this;
3625 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
3631 if(!*last_textbox) *last_textbox = this;
3635 if(top_level->active_subwindow == this)
3641 *prev_textbox = this;
3646 // Search subwindows for textbox
3647 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
3649 BC_WindowBase *test_subwindow = subwindows->values[i];
3650 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
3655 BC_Clipboard* BC_WindowBase::get_clipboard()
3657 #ifdef SINGLE_THREAD
3658 return BC_Display::display_global->clipboard;
3660 return top_level->clipboard;
3664 void BC_WindowBase::get_relative_cursor_xy(int &x, int &y, int lock_window)
3666 int abs_x, abs_y, win_x, win_y;
3667 unsigned int temp_mask;
3670 if(lock_window) this->lock_window("BC_WindowBase::get_relative_cursor_xy");
3671 XQueryPointer(top_level->display, top_level->win,
3672 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3675 XTranslateCoordinates(top_level->display, top_level->rootwin,
3676 win, abs_x, abs_y, &x, &y, &temp_win);
3677 if(lock_window) this->unlock_window();
3679 int BC_WindowBase::get_relative_cursor_x(int lock_window)
3682 get_relative_cursor_xy(x, y, lock_window);
3685 int BC_WindowBase::get_relative_cursor_y(int lock_window)
3688 get_relative_cursor_xy(x, y, lock_window);
3692 void BC_WindowBase::get_abs_cursor_xy(int &abs_x, int &abs_y, int lock_window)
3695 unsigned int temp_mask;
3698 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_xy");
3699 XQueryPointer(top_level->display, top_level->win,
3700 &temp_win, &temp_win, &abs_x, &abs_y, &win_x, &win_y,
3702 if(lock_window) this->unlock_window();
3704 int BC_WindowBase::get_abs_cursor_x(int lock_window)
3707 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3710 int BC_WindowBase::get_abs_cursor_y(int lock_window)
3713 get_abs_cursor_xy(abs_x, abs_y, lock_window);
3717 void BC_WindowBase::get_pop_cursor_xy(int &px, int &py, int lock_window)
3720 get_abs_cursor_xy(px, py, lock_window);
3721 if( px < margin ) px = margin;
3722 if( py < margin ) py = margin;
3723 int wd = get_screen_w(lock_window,-1) - margin;
3724 if( px > wd ) px = wd;
3725 int ht = get_screen_h(lock_window,-1) - margin;
3726 if( py > ht ) py = ht;
3728 int BC_WindowBase::get_pop_cursor_x(int lock_window)
3731 get_pop_cursor_xy(px, py, lock_window);
3734 int BC_WindowBase::get_pop_cursor_y(int lock_window)
3737 get_pop_cursor_xy(px, py, lock_window);
3741 int BC_WindowBase::match_window(Window win)
3743 if (this->win == win) return 1;
3745 for(int i = 0; i < subwindows->total; i++) {
3746 result = subwindows->values[i]->match_window(win);
3747 if (result) return result;
3753 int BC_WindowBase::get_cursor_over_window()
3755 if(top_level != this) return top_level->get_cursor_over_window();
3757 int abs_x, abs_y, win_x, win_y;
3758 unsigned int temp_mask;
3759 Window root_return, child_return;
3761 int ret = XQueryPointer(display, win,
3762 &root_return, &child_return, &abs_x, &abs_y,
3763 &win_x, &win_y, &temp_mask);
3764 //printf("BC_WindowBase::get_cursor_over_window %d %s 0x%x %d 0x%x\n",
3765 // __LINE__, title, win, ret, child_return);
3767 int result = !ret || child_return == None ? 0 :
3768 match_window(child_return);
3772 int BC_WindowBase::cursor_above()
3775 get_relative_cursor_xy(rx, ry);
3776 return rx < 0 || rx >= get_w() ||
3777 ry < 0 || ry >= get_h() ? 0 : 1;
3780 int BC_WindowBase::get_drag_x()
3782 return top_level->drag_x;
3785 int BC_WindowBase::get_drag_y()
3787 return top_level->drag_y;
3790 int BC_WindowBase::get_cursor_x()
3792 return top_level->cursor_x;
3795 int BC_WindowBase::get_cursor_y()
3797 return top_level->cursor_y;
3800 int BC_WindowBase::dump_windows()
3802 printf("\tBC_WindowBase::dump_windows window=%p win=%p '%s', %dx%d+%d+%d %s\n",
3803 this, (void*)this->win, title, w,h,x,y, typeid(*this).name());
3804 for(int i = 0; i < subwindows->size(); i++)
3805 subwindows->get(i)->dump_windows();
3806 for(int i = 0; i < popups.size(); i++) {
3807 BC_WindowBase *p = popups[i];
3808 printf("\tBC_WindowBase::dump_windows popup=%p win=%p '%s', %dx%d+%d+%d %s\n",
3809 p, (void*)p->win, p->title, p->w,p->h,p->x,p->y, typeid(*p).name());
3814 int BC_WindowBase::is_event_win()
3816 return this->win == top_level->event_win;
3819 void BC_WindowBase::set_dragging(int value)
3821 is_dragging = value;
3824 int BC_WindowBase::get_dragging()
3829 int BC_WindowBase::get_buttonpress()
3831 return top_level->button_number;
3834 int BC_WindowBase::get_button_down()
3836 return top_level->button_down;
3839 int BC_WindowBase::alt_down()
3841 return top_level->alt_mask;
3844 int BC_WindowBase::shift_down()
3846 return top_level->shift_mask;
3849 int BC_WindowBase::ctrl_down()
3851 return top_level->ctrl_mask;
3854 wchar_t* BC_WindowBase::get_wkeystring(int *length)
3857 *length = top_level->wkey_string_length;
3858 return top_level->wkey_string;
3861 #ifdef X_HAVE_UTF8_STRING
3862 char* BC_WindowBase::get_keypress_utf8()
3864 return top_level->key_pressed_utf8;
3869 int BC_WindowBase::get_keypress()
3871 return top_level->key_pressed;
3874 int BC_WindowBase::get_double_click()
3876 return top_level->double_click;
3879 int BC_WindowBase::get_triple_click()
3881 return top_level->triple_click;
3884 int BC_WindowBase::get_bgcolor()
3889 int BC_WindowBase::resize_window(int w, int h)
3891 if(this->w == w && this->h == h) return 0;
3893 if(window_type == MAIN_WINDOW && !allow_resize)
3895 XSizeHints size_hints;
3896 size_hints.flags = PSize | PMinSize | PMaxSize;
3897 size_hints.width = w;
3898 size_hints.height = h;
3899 size_hints.min_width = w;
3900 size_hints.max_width = w;
3901 size_hints.min_height = h;
3902 size_hints.max_height = h;
3903 XSetNormalHints(top_level->display, win, &size_hints);
3905 XResizeWindow(top_level->display, win, w, h);
3910 pixmap = new BC_Pixmap(this, w, h);
3912 // Propagate to menubar
3913 for(int i = 0; i < subwindows->total; i++)
3915 subwindows->values[i]->dispatch_resize_event(w, h);
3918 draw_background(0, 0, w, h);
3919 if(top_level == this && get_resources()->recursive_resizing)
3920 resize_history.append(new BC_ResizeCall(w, h));
3924 // The only way for resize events to be propagated is by updating the internal w and h
3925 int BC_WindowBase::resize_event(int w, int h)
3927 if(window_type == MAIN_WINDOW)
3935 int BC_WindowBase::reposition_window(int x, int y)
3937 reposition_window(x, y, -1, -1);
3942 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
3946 // Some tools set their own dimensions before calling this, causing the
3947 // resize check to skip.
3951 if(w > 0 && w != this->w)
3957 if(h > 0 && h != this->h)
3963 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
3966 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
3968 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
3970 if(translation_count && window_type == MAIN_WINDOW)
3972 // KDE shifts window right and down.
3973 // FVWM leaves window alone and adds border around it.
3974 XMoveResizeWindow(top_level->display,
3976 x - BC_DisplayInfo::auto_reposition_x,
3977 y - BC_DisplayInfo::auto_reposition_y,
3983 XMoveResizeWindow(top_level->display,
3994 pixmap = new BC_Pixmap(this, this->w, this->h);
3995 clear_box(0,0, this->w, this->h);
3996 // Propagate to menubar
3997 for(int i = 0; i < subwindows->total; i++)
3999 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
4002 // draw_background(0, 0, w, h);
4008 int BC_WindowBase::reposition_window_relative(int dx, int dy, int w, int h)
4010 return reposition_window(get_x()+dx, get_y()+dy, w, h);
4013 int BC_WindowBase::reposition_window_relative(int dx, int dy)
4015 return reposition_window_relative(dx, dy, -1, -1);
4018 void BC_WindowBase::set_tooltips(int v)
4020 get_resources()->tooltips_enabled = v;
4023 void BC_WindowBase::set_force_tooltip(int v)
4028 int BC_WindowBase::raise_window(int do_flush)
4030 XRaiseWindow(top_level->display, win);
4031 if(do_flush) XFlush(top_level->display);
4035 int BC_WindowBase::lower_window(int do_flush)
4037 XLowerWindow(top_level->display, win);
4038 if(do_flush) XFlush(top_level->display);
4042 void BC_WindowBase::set_background(VFrame *bitmap)
4044 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
4046 bg_pixmap = new BC_Pixmap(this, bitmap, PIXMAP_OPAQUE);
4047 shared_bg_pixmap = 0;
4048 draw_background(0, 0, w, h);
4051 void BC_WindowBase::put_title(const char *text)
4053 char *cp = this->title, *ep = cp+sizeof(this->title)-1;
4054 for( const unsigned char *bp = (const unsigned char *)text; *bp && cp<ep; ++bp )
4055 *cp++ = *bp >= ' ' ? *bp : ' ';
4059 void BC_WindowBase::set_title(const char *text)
4064 txlist[0] = this->title;
4067 XTextProperty titleprop;
4068 XmbTextListToTextProperty(top_level->display, txlist, 1,
4069 XStdICCTextStyle, &titleprop);
4070 XSetWMName(top_level->display, top_level->win, &titleprop);
4071 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4072 XFree(titleprop.value);
4077 void BC_WindowBase::set_utf8title(const char *text)
4079 XTextProperty titleprop;
4082 strcpy(this->title, text);
4083 txlist[0] = this->title;
4086 Xutf8TextListToTextProperty(top_level->display, txlist, 1,
4087 XUTF8StringStyle, &titleprop);
4088 XSetWMName(top_level->display, top_level->win, &titleprop);
4089 XSetWMIconName(top_level->display, top_level->win, &titleprop);
4090 XFree(titleprop.value);
4095 const char *BC_WindowBase::get_title()
4100 int BC_WindowBase::get_toggle_value()
4102 return toggle_value;
4105 int BC_WindowBase::get_toggle_drag()
4110 int BC_WindowBase::set_icon(VFrame *data)
4112 if(icon_pixmap) delete icon_pixmap;
4113 icon_pixmap = new BC_Pixmap(top_level, data, PIXMAP_ALPHA, 1);
4115 if(icon_window) delete icon_window;
4116 icon_window = new BC_Popup(this,
4119 icon_pixmap->get_w(),
4120 icon_pixmap->get_h(),
4122 1, // All windows are hidden initially
4126 wm_hints.flags = WindowGroupHint | IconPixmapHint | IconMaskHint | IconWindowHint;
4127 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
4128 wm_hints.icon_mask = icon_pixmap->get_alpha();
4129 wm_hints.icon_window = icon_window->win;
4130 wm_hints.window_group = XGroupLeader;
4132 // for(int i = 0; i < 1000; i++)
4133 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
4136 XSetWMHints(top_level->display, top_level->win, &wm_hints);
4137 XSync(top_level->display, 0);
4141 int BC_WindowBase::set_w(int w)
4147 int BC_WindowBase::set_h(int h)
4153 int BC_WindowBase::load_defaults(BC_Hash *defaults)
4155 BC_Resources *resources = get_resources();
4156 char string[BCTEXTLEN];
4157 int newest_id = - 1;
4158 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4160 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4161 resources->filebox_history[i].path[0] = 0;
4162 defaults->get(string, resources->filebox_history[i].path);
4163 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4164 resources->filebox_history[i].id = defaults->get(string, resources->get_id());
4165 if(resources->filebox_history[i].id > newest_id)
4166 newest_id = resources->filebox_history[i].id;
4169 resources->filebox_id = newest_id + 1;
4170 resources->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
4171 resources->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
4172 resources->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
4173 resources->filebox_columntype[0] = defaults->get("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4174 resources->filebox_columntype[1] = defaults->get("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4175 resources->filebox_columntype[2] = defaults->get("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4176 resources->filebox_columntype[3] = defaults->get("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4177 resources->filebox_columnwidth[0] = defaults->get("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4178 resources->filebox_columnwidth[1] = defaults->get("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4179 resources->filebox_columnwidth[2] = defaults->get("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4180 resources->filebox_columnwidth[3] = defaults->get("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4181 defaults->get("FILEBOX_FILTER", resources->filebox_filter);
4185 int BC_WindowBase::save_defaults(BC_Hash *defaults)
4187 BC_Resources *resources = get_resources();
4188 char string[BCTEXTLEN];
4189 for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
4191 sprintf(string, "FILEBOX_HISTORY_PATH%d", i);
4192 defaults->update(string, resources->filebox_history[i].path);
4193 sprintf(string, "FILEBOX_HISTORY_ID%d", i);
4194 defaults->update(string, resources->filebox_history[i].id);
4196 defaults->update("FILEBOX_MODE", resources->filebox_mode);
4197 defaults->update("FILEBOX_W", resources->filebox_w);
4198 defaults->update("FILEBOX_H", resources->filebox_h);
4199 defaults->update("FILEBOX_TYPE0", resources->filebox_columntype[0]);
4200 defaults->update("FILEBOX_TYPE1", resources->filebox_columntype[1]);
4201 defaults->update("FILEBOX_TYPE2", resources->filebox_columntype[2]);
4202 defaults->update("FILEBOX_TYPE3", resources->filebox_columntype[3]);
4203 defaults->update("FILEBOX_WIDTH0", resources->filebox_columnwidth[0]);
4204 defaults->update("FILEBOX_WIDTH1", resources->filebox_columnwidth[1]);
4205 defaults->update("FILEBOX_WIDTH2", resources->filebox_columnwidth[2]);
4206 defaults->update("FILEBOX_WIDTH3", resources->filebox_columnwidth[3]);
4207 defaults->update("FILEBOX_FILTER", resources->filebox_filter);
4213 // For some reason XTranslateCoordinates can take a long time to return.
4214 // We work around this by only calling it when the event windows are different.
4215 void BC_WindowBase::translate_coordinates(Window src_w,
4227 *dest_x_return = src_x;
4228 *dest_y_return = src_y;
4232 XTranslateCoordinates(top_level->display,
4240 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
4244 void BC_WindowBase::get_root_coordinates(int x, int y, int *abs_x, int *abs_y)
4246 translate_coordinates(win, top_level->rootwin, x, y, abs_x, abs_y);
4249 void BC_WindowBase::get_win_coordinates(int abs_x, int abs_y, int *x, int *y)
4251 translate_coordinates(top_level->rootwin, win, abs_x, abs_y, x, y);
4259 #ifdef HAVE_LIBXXF86VM
4260 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
4264 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
4266 XF86VidModeModeInfo **vm_modelines;
4267 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4268 for (i = 0; i < vm_count; i++) {
4269 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
4272 display = top_level->display;
4273 if (vm_modelines[*vm]->hdisplay == *width)
4277 *width = vm_modelines[*vm]->hdisplay;
4278 *height = vm_modelines[*vm]->vdisplay;
4283 void BC_WindowBase::scale_vm(int vm)
4285 int foo,bar,dotclock;
4286 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
4289 XF86VidModeModeInfo **vm_modelines;
4290 XF86VidModeModeLine vml;
4291 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
4292 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
4293 orig_modeline.dotclock = dotclock;
4294 orig_modeline.hdisplay = vml.hdisplay;
4295 orig_modeline.hsyncstart = vml.hsyncstart;
4296 orig_modeline.hsyncend = vml.hsyncend;
4297 orig_modeline.htotal = vml.htotal;
4298 orig_modeline.vdisplay = vml.vdisplay;
4299 orig_modeline.vsyncstart = vml.vsyncstart;
4300 orig_modeline.vsyncend = vml.vsyncend;
4301 orig_modeline.vtotal = vml.vtotal;
4302 orig_modeline.flags = vml.flags;
4303 orig_modeline.privsize = vml.privsize;
4304 // orig_modeline.private = vml.private;
4305 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
4306 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
4307 XFlush(top_level->display);
4311 void BC_WindowBase::restore_vm()
4313 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
4314 XFlush(top_level->display);
4337 #ifndef SINGLE_THREAD
4338 int BC_WindowBase::get_event_count()
4340 event_lock->lock("BC_WindowBase::get_event_count");
4341 int result = common_events.total;
4342 event_lock->unlock();
4346 XEvent* BC_WindowBase::get_event()
4349 while(!done && !result)
4351 event_condition->lock("BC_WindowBase::get_event");
4352 event_lock->lock("BC_WindowBase::get_event");
4354 if(common_events.total && !done)
4356 result = common_events.values[0];
4357 common_events.remove_number(0);
4360 event_lock->unlock();
4365 void BC_WindowBase::put_event(XEvent *event)
4367 event_lock->lock("BC_WindowBase::put_event");
4368 common_events.append(event);
4369 event_lock->unlock();
4370 event_condition->unlock();
4373 void BC_WindowBase::dequeue_events(Window win)
4375 event_lock->lock("BC_WindowBase::dequeue_events");
4377 int out = 0, total = common_events.size();
4378 for( int in=0; in<total; ++in ) {
4379 if( common_events[in]->xany.window == win ) continue;
4380 common_events[out++] = common_events[in];
4382 common_events.total = out;
4384 event_lock->unlock();
4387 #endif // SINGLE_THREAD
4389 int BC_WindowBase::get_id()
4395 BC_Pixmap *BC_WindowBase::create_pixmap(VFrame *vframe)
4397 int w = vframe->get_w(), h = vframe->get_h();
4398 BC_Pixmap *icon = new BC_Pixmap(this, w, h);
4399 icon->draw_vframe(vframe, 0,0, w,h, 0,0);
4404 void BC_WindowBase::flicker(int n, int ms)
4406 int color = get_bg_color();
4407 for( int i=2*n; --i>=0; ) {
4408 set_inverse(); set_bg_color(WHITE);
4409 clear_box(0,0, w,h); flash(1);
4410 sync_display(); Timer::delay(ms);
4412 set_bg_color(color);