2 #include "bcclipboard.h"
3 #include "bcdisplayinfo.h"
7 #include "bcpopupmenu.h"
8 #include "bcrepeater.h"
9 #include "bcresources.h"
10 #include "bcsignals.h"
11 #include "bcsubwindow.h"
12 #include "bcwindowbase.h"
13 #include "bcwindowevents.h"
14 #include "colormodels.h"
16 #include "condition.h"
31 #include <X11/extensions/Xvlib.h>
32 #include <X11/extensions/shape.h>
35 BC_ResizeCall::BC_ResizeCall(int w, int h)
50 Mutex BC_WindowBase::opengl_lock;
52 BC_Resources BC_WindowBase::resources;
54 BC_WindowBase::BC_WindowBase()
56 //printf("BC_WindowBase::BC_WindowBase 1\n");
57 BC_WindowBase::initialize();
60 BC_WindowBase::~BC_WindowBase()
63 #ifdef HAVE_LIBXXF86VM
64 if(window_type == VIDMODE_SCALED_WINDOW && vm_switched)
71 if(window_type != MAIN_WINDOW)
73 if(top_level->active_menubar == this) top_level->active_menubar = 0;
74 if(top_level->active_popup_menu == this) top_level->active_popup_menu = 0;
75 if(top_level->active_subwindow == this) top_level->active_subwindow = 0;
76 parent_window->subwindows->remove(this);
81 for(int i = 0; i < subwindows->total; i++)
83 delete subwindows->values[i];
88 XFreePixmap(top_level->display, pixmap);
89 XDestroyWindow(top_level->display, win);
91 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
92 if(icon_pixmap) delete icon_pixmap;
93 if(temp_bitmap) delete temp_bitmap;
96 if(window_type == MAIN_WINDOW)
102 XftFontClose (display, (XftFont*)largefont_xft);
104 XftFontClose (display, (XftFont*)mediumfont_xft);
106 XftFontClose (display, (XftFont*)smallfont_xft);
109 // Can't close display if another thread is waiting for events
110 XCloseDisplay(display);
111 // XCloseDisplay(event_display);
112 clipboard->stop_clipboard();
120 resize_history.remove_all_objects();
121 common_events.remove_all_objects();
123 delete event_condition;
124 UNSET_ALL_LOCKS(this)
127 int BC_WindowBase::initialize()
142 translation_events = 0;
143 ctrl_mask = shift_mask = alt_mask = 0;
144 cursor_x = cursor_y = button_number = 0;
147 button_time1 = button_time2 = 0;
152 active_popup_menu = 0;
153 active_subwindow = 0;
156 persistant_tooltip = 0;
157 // next_repeat_id = 0;
160 current_font = MEDIUMFONT;
161 current_cursor = ARROW_CURSOR;
162 current_color = BLACK;
164 shared_bg_pixmap = 0;
166 window_type = MAIN_WINDOW;
167 translation_count = 0;
168 x_correction = y_correction = 0;
175 #ifdef HAVE_LIBXXF86VM
182 // Need these right away since put_event is called before run_window sometimes.
183 event_lock = new Mutex("BC_WindowBase::event_lock");
184 event_condition = new Condition(0, "BC_WindowBase::event_condition");
190 #define DEFAULT_EVENT_MASKS EnterWindowMask | \
193 ButtonReleaseMask | \
194 PointerMotionMask | \
198 int BC_WindowBase::create_window(BC_WindowBase *parent_window,
212 BC_Pixmap *bg_pixmap)
214 XSetWindowAttributes attr;
216 XSizeHints size_hints;
219 #ifdef HAVE_LIBXXF86VM
223 if(parent_window) top_level = parent_window->top_level;
225 #ifdef HAVE_LIBXXF86VM
226 if(window_type == VIDMODE_SCALED_WINDOW)
227 closest_vm(&vm,&w,&h);
234 this->bg_color = bg_color;
235 this->window_type = window_type;
237 this->private_color = private_color;
238 this->parent_window = parent_window;
239 this->bg_pixmap = bg_pixmap;
240 this->allow_resize = allow_resize;
241 strcpy(this->title, _(title));
242 if(bg_pixmap) shared_bg_pixmap = 1;
244 if(parent_window) top_level = parent_window->top_level;
246 subwindows = new BC_SubWindowList;
249 if(window_type == MAIN_WINDOW)
252 parent_window = this;
254 // This function must be the first Xlib
255 // function a multi-threaded program calls
259 // get the display connection
260 display = init_display(display_name);
261 // event_display = init_display(display_name);
263 // Fudge window placement
264 root_w = get_root_w(1, 0);
265 root_h = get_root_h(0);
266 if(this->x + this->w > root_w) this->x = root_w - this->w;
267 if(this->y + this->h > root_h) this->y = root_h - this->h;
268 if(this->x < 0) this->x = 0;
269 if(this->y < 0) this->y = 0;
270 screen = DefaultScreen(display);
272 rootwin = RootWindow(display, screen);
273 vis = DefaultVisual(display, screen);
274 default_depth = DefaultDepth(display, screen);
275 client_byte_order = (*(u_int32_t*)"a ") & 0x00000001;
276 server_byte_order = (XImageByteOrder(display) == MSBFirst) ? 0 : 1;
280 // This must be done before fonts to know if antialiasing is available.
283 if(resources.use_shm < 0) resources.initialize_display(this);
284 x_correction = get_resources()->get_left_border();
285 y_correction = get_resources()->get_top_border();
287 if(this->bg_color == -1)
288 this->bg_color = resources.get_bg_color();
299 attr.event_mask = DEFAULT_EVENT_MASKS |
300 StructureNotifyMask |
303 attr.background_pixel = get_color(this->bg_color);
304 attr.colormap = cmap;
305 attr.cursor = get_cursor_struct(ARROW_CURSOR);
307 win = XCreateWindow(display,
314 top_level->default_depth,
320 XGetNormalHints(display, win, &size_hints);
322 size_hints.flags = PSize | PMinSize | PMaxSize;
323 size_hints.width = this->w;
324 size_hints.height = this->h;
325 size_hints.min_width = allow_resize ? minw : this->w;
326 size_hints.max_width = allow_resize ? 32767 : this->w;
327 size_hints.min_height = allow_resize ? minh : this->h;
328 size_hints.max_height = allow_resize ? 32767 : this->h;
329 if(x > -BC_INFINITY && x < BC_INFINITY)
331 size_hints.flags |= PPosition;
332 size_hints.x = this->x;
333 size_hints.y = this->y;
336 XSetStandardProperties(display,
346 clipboard = new BC_Clipboard(display_name);
347 clipboard->start_clipboard();
350 #ifdef HAVE_LIBXXF86VM
351 if(window_type == VIDMODE_SCALED_WINDOW && vm != -1)
358 #ifdef HAVE_LIBXXF86VM
359 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
361 if(window_type == POPUP_WINDOW)
371 attr.event_mask = DEFAULT_EVENT_MASKS;
373 if(this->bg_color == -1)
374 this->bg_color = resources.get_bg_color();
375 attr.background_pixel = top_level->get_color(bg_color);
376 attr.colormap = top_level->cmap;
377 attr.cursor = top_level->get_cursor_struct(ARROW_CURSOR);
378 attr.override_redirect = True;
379 attr.save_under = True;
381 win = XCreateWindow(top_level->display,
388 top_level->default_depth,
395 if(window_type == SUB_WINDOW)
397 mask = CWBackPixel | CWEventMask;
398 attr.event_mask = DEFAULT_EVENT_MASKS;
399 attr.background_pixel = top_level->get_color(this->bg_color);
400 win = XCreateWindow(top_level->display,
407 top_level->default_depth,
413 XMapWindow(top_level->display, win);
416 // Create pixmap for all windows
417 pixmap = XCreatePixmap(top_level->display,
421 top_level->default_depth);
423 // Create truetype rendering surface
425 if(get_resources()->use_xft)
427 // printf("BC_WindowBase::create_window 1 %p %p %p %p\n",
428 // top_level->display,
432 xft_drawable = XftDrawCreate(top_level->display,
436 // printf("BC_WindowBase::create_window 10 %p %p %p %p %p\n",
438 // top_level->display,
445 // Set up options for main window
446 if(window_type == MAIN_WINDOW)
448 if(get_resources()->bg_image && !bg_pixmap && bg_color < 0)
450 this->bg_pixmap = new BC_Pixmap(this,
451 get_resources()->bg_image,
455 if(!hidden) show_window();
462 draw_background(0, 0, this->w, this->h);
465 // Set up options for popup window
466 #ifdef HAVE_LIBXXF86VM
467 if(window_type == POPUP_WINDOW || window_type == VIDMODE_SCALED_WINDOW)
469 if(window_type == POPUP_WINDOW)
473 if(!hidden) show_window();
478 Display* BC_WindowBase::init_display(char *display_name)
482 if(display_name && display_name[0] == 0) display_name = NULL;
483 if((display = XOpenDisplay(display_name)) == NULL)
485 printf("BC_WindowBase::init_display: cannot connect to X server %s\n",
487 if(getenv("DISPLAY") == NULL)
489 printf("'DISPLAY' environment variable not set.\n");
493 // Try again with default display.
495 if((display = XOpenDisplay(0)) == NULL)
497 printf("BC_WindowBase::init_display: cannot connect to default X server.\n");
505 int BC_WindowBase::run_window()
511 // Events may have been sent before run_window so can't initialize them here.
514 if(window_type == MAIN_WINDOW)
516 // tooltip_id = get_repeat_id();
517 set_repeat(get_resources()->tooltip_delay);
520 // Start X server events
521 event_thread = new BC_WindowEvents(this);
522 event_thread->start();
524 // Start common events
530 unset_all_repeaters();
534 event_condition->reset();
535 common_events.remove_all_objects();
541 int BC_WindowBase::get_key_masks(XEvent *event)
544 ctrl_mask = (event->xkey.state & ControlMask) ? 1 : 0;
546 shift_mask = (event->xkey.state & ShiftMask) ? 1 : 0;
547 alt_mask = (event->xkey.state & Mod1Mask) ? 1 : 0;
557 int BC_WindowBase::dispatch_event()
564 XClientMessageEvent *ptr;
566 int cancel_resize, cancel_translation;
570 // If an event is waiting get it, otherwise
571 // wait for next event only if there are no compressed events.
572 if(/* XPending(display) */
574 (!motion_events && !resize_events && !translation_events))
576 // XNextEvent(display, event);
578 // Lock out window deletions
579 lock_window("BC_WindowBase::dispatch_event 1");
580 get_key_masks(event);
583 // Handle compressed events
585 lock_window("BC_WindowBase::dispatch_event 2");
587 dispatch_resize_event(last_resize_w, last_resize_h);
590 dispatch_motion_event();
592 if(translation_events)
593 dispatch_translation_event();
599 //printf("1 %s %p %d\n", title, event, event->type);
603 // Clear the resize buffer
604 if(resize_events) dispatch_resize_event(last_resize_w, last_resize_h);
605 // Clear the motion buffer since this can clear the window
606 if(motion_events) dispatch_motion_event();
608 ptr = (XClientMessageEvent*)event;
611 if(ptr->message_type == ProtoXAtom &&
612 ptr->data.l[0] == DelWinXAtom)
617 if(ptr->message_type == RepeaterXAtom)
619 dispatch_repeat_event(ptr->data.l[0]);
620 // Make sure the repeater still exists.
621 // for(int i = 0; i < repeaters.total; i++)
623 // if(repeaters.values[i]->repeat_id == ptr->data.l[0])
625 // dispatch_repeat_event_master(ptr->data.l[0]);
631 if(ptr->message_type == SetDoneXAtom)
644 dispatch_focus_out();
648 cursor_x = event->xbutton.x;
649 cursor_y = event->xbutton.y;
650 button_number = event->xbutton.button;
651 event_win = event->xany.window;
653 button_pressed = event->xbutton.button;
654 button_time1 = button_time2;
655 button_time2 = event->xbutton.time;
658 drag_win = event_win;
659 drag_x1 = cursor_x - get_resources()->drag_radius;
660 drag_x2 = cursor_x + get_resources()->drag_radius;
661 drag_y1 = cursor_y - get_resources()->drag_radius;
662 drag_y2 = cursor_y + get_resources()->drag_radius;
664 if(button_time2 - button_time1 < resources.double_click)
666 // Ignore triple clicks
668 button_time2 = button_time1 = 0;
673 dispatch_button_press();
677 button_number = event->xbutton.button;
678 event_win = event->xany.window;
681 dispatch_button_release();
685 event_win = event->xany.window;
686 dispatch_expose_event();
690 // Dispatch previous motion event if this is a subsequent motion from a different window
691 if(motion_events && last_motion_win != event->xany.window)
693 dispatch_motion_event();
696 // Buffer the current motion
698 last_motion_x = event->xmotion.x;
699 last_motion_y = event->xmotion.y;
700 last_motion_win = event->xany.window;
703 case ConfigureNotify:
704 XTranslateCoordinates(top_level->display,
712 last_resize_w = event->xconfigure.width;
713 last_resize_h = event->xconfigure.height;
716 cancel_translation = 0;
718 // Resize history prevents responses to recursive resize requests
719 for(int i = 0; i < resize_history.total && !cancel_resize; i++)
721 if(resize_history.values[i]->w == last_resize_w &&
722 resize_history.values[i]->h == last_resize_h)
724 delete resize_history.values[i];
725 resize_history.remove_number(i);
730 if(last_resize_w == w && last_resize_h == h)
738 if((last_translate_x == x && last_translate_y == y))
739 cancel_translation = 1;
741 if(!cancel_translation)
743 translation_events = 1;
751 XLookupString((XKeyEvent*)event, keys_return, 1, &keysym, 0);
752 //printf("BC_WindowBase::dispatch_event %08x\n", keys_return[0]);
753 // block out control keys
754 if(keysym > 0xffe0 && keysym < 0xffff) break;
757 // block out extra keys
767 // Translate key codes
768 case XK_Return: key_pressed = RETURN; break;
769 case XK_Up: key_pressed = UP; break;
770 case XK_Down: key_pressed = DOWN; break;
771 case XK_Left: key_pressed = LEFT; break;
772 case XK_Right: key_pressed = RIGHT; break;
773 case XK_Next: key_pressed = PGDN; break;
774 case XK_Prior: key_pressed = PGUP; break;
775 case XK_BackSpace: key_pressed = BACKSPACE; break;
776 case XK_Escape: key_pressed = ESC; break;
779 key_pressed = LEFTTAB;
783 case XK_ISO_Left_Tab: key_pressed = LEFTTAB; break;
784 case XK_underscore: key_pressed = '_'; break;
785 case XK_asciitilde: key_pressed = '~'; break;
786 case XK_Delete: key_pressed = DELETE; break;
787 case XK_Home: key_pressed = HOME; break;
788 case XK_End: key_pressed = END; break;
791 case XK_KP_Enter: key_pressed = KPENTER; break;
792 case XK_KP_Add: key_pressed = KPPLUS; break;
794 case XK_KP_End: key_pressed = KP1; break;
796 case XK_KP_Down: key_pressed = KP2; break;
798 case XK_KP_Page_Down: key_pressed = KP3; break;
800 case XK_KP_Left: key_pressed = KP4; break;
802 case XK_KP_Begin: key_pressed = KP5; break;
804 case XK_KP_Right: key_pressed = KP6; break;
806 case XK_KP_Insert: key_pressed = KPINS; break;
808 case XK_KP_Delete: key_pressed = KPDEL; break;
810 //key_pressed = keys_return[0];
811 key_pressed = keysym & 0xff;
815 //printf("BC_WindowBase::dispatch_event %d %d %x\n", shift_down(), alt_down(), key_pressed);
816 result = dispatch_keypress_event();
817 // Handle some default keypresses
820 if(key_pressed == 'w' ||
829 event_win = event->xany.window;
830 dispatch_cursor_leave();
834 event_win = event->xany.window;
835 cursor_x = event->xcrossing.x;
836 cursor_y = event->xcrossing.y;
837 dispatch_cursor_enter();
840 //printf("100 %s %p %d\n", title, event, event->type);
843 if(event) delete event;
847 int BC_WindowBase::dispatch_expose_event()
850 for(int i = 0; i < subwindows->total && !result; i++)
852 result = subwindows->values[i]->dispatch_expose_event();
856 if(!result) expose_event();
860 int BC_WindowBase::dispatch_resize_event(int w, int h)
862 if(window_type == MAIN_WINDOW)
865 // Can't store w and h here because bcfilebox depends on the old w and h to
866 // reposition widgets.
867 XFreePixmap(top_level->display, pixmap);
868 pixmap = XCreatePixmap(top_level->display,
872 top_level->default_depth);
873 clear_box(0, 0, w, h);
876 // Propagate to subwindows
877 for(int i = 0; i < subwindows->total; i++)
879 subwindows->values[i]->dispatch_resize_event(w, h);
885 if(window_type == MAIN_WINDOW)
893 int BC_WindowBase::dispatch_translation_event()
895 translation_events = 0;
896 if(window_type == MAIN_WINDOW)
900 x = last_translate_x;
901 y = last_translate_y;
902 // Correct for window manager offsets
907 for(int i = 0; i < subwindows->total; i++)
909 subwindows->values[i]->dispatch_translation_event();
916 int BC_WindowBase::dispatch_motion_event()
920 if(top_level == this)
922 event_win = last_motion_win;
926 if(get_button_down() && !active_menubar && !active_popup_menu)
930 cursor_x = last_motion_x;
931 cursor_y = last_motion_y;
932 result = dispatch_drag_motion();
936 (last_motion_x < drag_x1 || last_motion_x >= drag_x2 ||
937 last_motion_y < drag_y1 || last_motion_y >= drag_y2))
942 result = dispatch_drag_start();
945 cursor_x = last_motion_x;
946 cursor_y = last_motion_y;
948 if(active_menubar && !result) result = active_menubar->dispatch_motion_event();
949 if(active_popup_menu && !result) result = active_popup_menu->dispatch_motion_event();
950 if(active_subwindow && !result) result = active_subwindow->dispatch_motion_event();
953 for(int i = 0; i < subwindows->total && !result; i++)
955 result = subwindows->values[i]->dispatch_motion_event();
958 if(!result) result = cursor_motion_event(); // give to user
962 int BC_WindowBase::dispatch_keypress_event()
965 if(top_level == this)
967 if(active_subwindow) result = active_subwindow->dispatch_keypress_event();
970 for(int i = 0; i < subwindows->total && !result; i++)
972 result = subwindows->values[i]->dispatch_keypress_event();
975 if(!result) result = keypress_event();
980 int BC_WindowBase::dispatch_focus_in()
982 for(int i = 0; i < subwindows->total; i++)
984 subwindows->values[i]->dispatch_focus_in();
992 int BC_WindowBase::dispatch_focus_out()
994 for(int i = 0; i < subwindows->total; i++)
996 subwindows->values[i]->dispatch_focus_out();
1004 int BC_WindowBase::get_has_focus()
1006 return top_level->has_focus;
1009 int BC_WindowBase::dispatch_button_press()
1012 if(top_level == this)
1014 if(active_menubar) result = active_menubar->dispatch_button_press();
1015 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_press();
1016 if(active_subwindow && !result) result = active_subwindow->dispatch_button_press();
1019 for(int i = 0; i < subwindows->total && !result; i++)
1021 result = subwindows->values[i]->dispatch_button_press();
1024 if(!result) result = button_press_event();
1029 int BC_WindowBase::dispatch_button_release()
1032 if(top_level == this)
1034 if(active_menubar) result = active_menubar->dispatch_button_release();
1035 //printf("BC_WindowBase::dispatch_button_release 1 %d\n", result);
1036 if(active_popup_menu && !result) result = active_popup_menu->dispatch_button_release();
1037 //printf("BC_WindowBase::dispatch_button_release 2 %p %d\n", active_subwindow, result);
1038 if(active_subwindow && !result) result = active_subwindow->dispatch_button_release();
1039 //printf("BC_WindowBase::dispatch_button_release 3 %d\n", result);
1040 if(!result) result = dispatch_drag_stop();
1042 //printf("BC_WindowBase::dispatch_button_release 4 %d\n", result);
1044 for(int i = 0; i < subwindows->total && !result; i++)
1046 result = subwindows->values[i]->dispatch_button_release();
1048 //printf("BC_WindowBase::dispatch_button_release 5 %d\n", result);
1052 result = button_release_event();
1054 //printf("BC_WindowBase::dispatch_button_release 6 %d\n", result);
1059 // int BC_WindowBase::dispatch_repeat_event_master(long duration)
1062 // BC_Repeater *repeater;
1064 // // Unlock the repeater if it still exists.
1065 // for(int i = 0; i < repeaters.total; i++)
1067 // if(repeaters.values[i]->repeat_id == repeat_id)
1069 // repeater = repeaters.values[i];
1070 // if(repeater->interrupted)
1073 // if(interrupt_now)
1076 // repeater->join();
1077 // repeaters.remove(repeater);
1083 // // Propogate to subwindows
1084 // if(active_menubar) result = active_menubar->dispatch_repeat_event(repeat_id);
1085 // if(!result && active_subwindow) result = active_subwindow->dispatch_repeat_event(repeat_id);
1086 // if(!result) result = dispatch_repeat_event(repeat_id);
1087 // repeater->repeat_mutex.unlock();
1089 // i = repeaters.total;
1096 int BC_WindowBase::dispatch_repeat_event(int64_t duration)
1098 // all repeat event handlers get called and decide based on activity and duration
1099 // whether to respond
1100 for(int i = 0; i < subwindows->total; i++)
1102 subwindows->values[i]->dispatch_repeat_event(duration);
1104 repeat_event(duration);
1106 // Unlock next signal
1107 if(window_type == MAIN_WINDOW)
1109 for(int i = 0; i < repeaters.total; i++)
1111 if(repeaters.values[i]->delay == duration)
1113 repeaters.values[i]->repeat_lock->unlock();
1121 int BC_WindowBase::dispatch_cursor_leave()
1123 for(int i = 0; i < subwindows->total; i++)
1125 subwindows->values[i]->dispatch_cursor_leave();
1128 cursor_leave_event();
1132 int BC_WindowBase::dispatch_cursor_enter()
1136 if(active_menubar) result = active_menubar->dispatch_cursor_enter();
1137 if(!result && active_popup_menu) result = active_popup_menu->dispatch_cursor_enter();
1138 if(!result && active_subwindow) result = active_subwindow->dispatch_cursor_enter();
1140 for(int i = 0; !result && i < subwindows->total; i++)
1142 result = subwindows->values[i]->dispatch_cursor_enter();
1145 if(!result) result = cursor_enter_event();
1149 int BC_WindowBase::cursor_enter_event()
1154 int BC_WindowBase::cursor_leave_event()
1159 int BC_WindowBase::close_event()
1165 int BC_WindowBase::dispatch_drag_start()
1168 if(active_menubar) result = active_menubar->dispatch_drag_start();
1169 if(!result && active_popup_menu) result = active_popup_menu->dispatch_drag_start();
1170 if(!result && active_subwindow) result = active_subwindow->dispatch_drag_start();
1172 for(int i = 0; i < subwindows->total && !result; i++)
1174 result = subwindows->values[i]->dispatch_drag_start();
1177 if(!result) result = is_dragging = drag_start_event();
1181 int BC_WindowBase::dispatch_drag_stop()
1185 for(int i = 0; i < subwindows->total && !result; i++)
1187 result = subwindows->values[i]->dispatch_drag_stop();
1190 if(is_dragging && !result)
1200 int BC_WindowBase::dispatch_drag_motion()
1203 for(int i = 0; i < subwindows->total && !result; i++)
1205 result = subwindows->values[i]->dispatch_drag_motion();
1208 if(is_dragging && !result)
1210 drag_motion_event();
1221 int BC_WindowBase::show_tooltip(int w, int h)
1225 if(!tooltip_on && get_resources()->tooltips_enabled)
1228 top_level->hide_tooltip();
1232 w = get_text_width(MEDIUMFONT, tooltip_text);
1235 h = get_text_height(MEDIUMFONT, tooltip_text);
1237 w += TOOLTIP_MARGIN * 2;
1238 h += TOOLTIP_MARGIN * 2;
1240 XTranslateCoordinates(top_level->display,
1248 tooltip_popup = new BC_Popup(top_level,
1253 get_resources()->tooltip_bg_color);
1256 tooltip_popup->set_font(MEDIUMFONT);
1257 tooltip_popup->flash();
1258 tooltip_popup->flush();
1263 int BC_WindowBase::hide_tooltip()
1266 for(int i = 0; i < subwindows->total; i++)
1268 subwindows->values[i]->hide_tooltip();
1274 delete tooltip_popup;
1280 int BC_WindowBase::set_tooltip(char *text)
1282 strcpy(this->tooltip_text, text);
1283 // Update existing tooltip if it is visible
1287 tooltip_popup->flash();
1292 // signal the event handler to repeat
1293 int BC_WindowBase::set_repeat(int64_t duration)
1297 printf("BC_WindowBase::set_repeat duration=%d\n", duration);
1300 if(window_type != MAIN_WINDOW) return top_level->set_repeat(duration);
1302 // test repeater database for duplicates
1303 for(int i = 0; i < repeaters.total; i++)
1306 if(repeaters.values[i]->delay == duration)
1308 repeaters.values[i]->start_repeating();
1313 BC_Repeater *repeater = new BC_Repeater(this, duration);
1314 repeater->initialize();
1315 repeaters.append(repeater);
1316 repeater->start_repeating();
1320 int BC_WindowBase::unset_repeat(int64_t duration)
1322 if(window_type != MAIN_WINDOW) return top_level->unset_repeat(duration);
1324 BC_Repeater *repeater = 0;
1325 for(int i = 0; i < repeaters.total; i++)
1327 if(repeaters.values[i]->delay == duration)
1329 repeaters.values[i]->stop_repeating();
1336 int BC_WindowBase::unset_all_repeaters()
1338 for(int i = 0; i < repeaters.total; i++)
1340 repeaters.values[i]->stop_repeating();
1342 repeaters.remove_all_objects();
1346 // long BC_WindowBase::get_repeat_id()
1348 // return top_level->next_repeat_id++;
1351 int BC_WindowBase::arm_repeat(int64_t duration)
1353 XEvent *event = new XEvent;
1354 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
1355 ptr->type = ClientMessage;
1356 ptr->message_type = RepeaterXAtom;
1358 ptr->data.l[0] = duration;
1360 // Couldn't use XSendEvent since it locked up randomly.
1362 // XSendEvent(top_level->event_display,
1371 int BC_WindowBase::get_atoms()
1373 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
1374 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
1375 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
1376 if(ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False))
1377 XChangeProperty(display, win, ProtoXAtom, XA_ATOM, 32, PropModeReplace, (unsigned char *)&DelWinXAtom, True);
1381 void BC_WindowBase::init_cursors()
1383 arrow_cursor = XCreateFontCursor(display, XC_top_left_arrow);
1384 cross_cursor = XCreateFontCursor(display, XC_crosshair);
1385 ibeam_cursor = XCreateFontCursor(display, XC_xterm);
1386 vseparate_cursor = XCreateFontCursor(display, XC_sb_v_double_arrow);
1387 hseparate_cursor = XCreateFontCursor(display, XC_sb_h_double_arrow);
1388 move_cursor = XCreateFontCursor(display, XC_fleur);
1389 left_cursor = XCreateFontCursor(display, XC_sb_left_arrow);
1390 right_cursor = XCreateFontCursor(display, XC_sb_right_arrow);
1391 upright_arrow_cursor = XCreateFontCursor(display, XC_arrow);
1392 upleft_resize_cursor = XCreateFontCursor(display, XC_top_left_corner);
1393 upright_resize_cursor = XCreateFontCursor(display, XC_top_right_corner);
1394 downleft_resize_cursor = XCreateFontCursor(display, XC_bottom_left_corner);
1395 downright_resize_cursor = XCreateFontCursor(display, XC_bottom_right_corner);
1398 int BC_WindowBase::evaluate_color_model(int client_byte_order, int server_byte_order, int depth)
1404 color_model = BC_RGB8;
1407 color_model = (server_byte_order == client_byte_order) ? BC_RGB565 : BC_BGR565;
1410 color_model = server_byte_order ? BC_BGR888 : BC_RGB888;
1413 color_model = server_byte_order ? BC_BGR8888 : BC_RGBA8888;
1419 int BC_WindowBase::init_colors()
1422 current_color_value = current_color_pixel = 0;
1424 // Get the real depth
1427 ximage = XCreateImage(top_level->display,
1429 top_level->default_depth,
1437 bits_per_pixel = ximage->bits_per_pixel;
1438 XDestroyImage(ximage);
1440 color_model = evaluate_color_model(client_byte_order,
1443 // Get the color model
1449 cmap = XCreateColormap(display, rootwin, vis, AllocNone);
1450 create_private_colors();
1454 cmap = DefaultColormap(display, screen);
1455 create_shared_colors();
1458 allocate_color_table();
1460 get_resources()->use_xft = 0;
1464 cmap = DefaultColormap(display, screen);
1470 int BC_WindowBase::create_private_colors()
1475 for(int i = 0; i < 255; i++)
1477 color = (i & 0xc0) << 16;
1478 color += (i & 0x38) << 10;
1479 color += (i & 0x7) << 5;
1480 color_table[i][0] = color;
1482 create_shared_colors(); // overwrite the necessary colors on the table
1487 int BC_WindowBase::create_color(int color)
1489 if(total_colors == 256)
1491 // replace the closest match with an exact match
1492 color_table[get_color_rgb8(color)][0] = color;
1496 // add the color to the table
1497 color_table[total_colors][0] = color;
1503 int BC_WindowBase::create_shared_colors()
1505 create_color(BLACK);
1506 create_color(WHITE);
1508 create_color(LTGREY);
1509 create_color(MEGREY);
1510 create_color(MDGREY);
1511 create_color(DKGREY);
1513 create_color(LTCYAN);
1514 create_color(MECYAN);
1515 create_color(MDCYAN);
1516 create_color(DKCYAN);
1518 create_color(LTGREEN);
1519 create_color(GREEN);
1520 create_color(DKGREEN);
1522 create_color(LTPINK);
1526 create_color(LTBLUE);
1528 create_color(DKBLUE);
1530 create_color(LTYELLOW);
1531 create_color(MEYELLOW);
1532 create_color(MDYELLOW);
1533 create_color(DKYELLOW);
1535 create_color(LTPURPLE);
1536 create_color(MEPURPLE);
1537 create_color(MDPURPLE);
1538 create_color(DKPURPLE);
1542 int BC_WindowBase::allocate_color_table()
1544 int red, green, blue, color;
1548 for(int i = 0; i < total_colors; i++)
1550 color = color_table[i][0];
1551 red = (color & 0xFF0000) >> 16;
1552 green = (color & 0x00FF00) >> 8;
1553 blue = color & 0xFF;
1555 col.flags = DoRed | DoGreen | DoBlue;
1556 col.red = red<<8 | red;
1557 col.green = green<<8 | green;
1558 col.blue = blue<<8 | blue;
1560 XAllocColor(display, cmap, &col);
1561 color_table[i][1] = col.pixel;
1564 XInstallColormap(display, cmap);
1568 int BC_WindowBase::init_window_shape()
1570 if(bg_pixmap && bg_pixmap->use_alpha())
1572 XShapeCombineMask(top_level->display,
1577 bg_pixmap->get_alpha(),
1584 int BC_WindowBase::init_gc()
1586 unsigned long gcmask;
1587 gcmask = GCFont | GCGraphicsExposures;
1590 gcvalues.font = mediumfont->fid; // set the font
1591 gcvalues.graphics_exposures = 0; // prevent expose events for every redraw
1592 gc = XCreateGC(display, rootwin, gcmask, &gcvalues);
1596 int BC_WindowBase::init_fonts()
1598 if((largefont = XLoadQueryFont(display, _(resources.large_font))) == NULL &&
1599 (largefont = XLoadQueryFont(display, _(resources.large_font2))) == NULL)
1600 largefont = XLoadQueryFont(display, "fixed");
1602 if((mediumfont = XLoadQueryFont(display, _(resources.medium_font))) == NULL &&
1603 (mediumfont = XLoadQueryFont(display, _(resources.medium_font2))) == NULL)
1604 mediumfont = XLoadQueryFont(display, "fixed");
1606 if((smallfont = XLoadQueryFont(display, _(resources.small_font))) == NULL &&
1607 (smallfont = XLoadQueryFont(display, _(resources.small_font2))) == NULL)
1608 smallfont = XLoadQueryFont(display, "fixed");
1611 if(get_resources()->use_xft)
1615 //printf("BC_WindowBase::init_fonts 1 %p %p %s\n", display, screen, resources.large_font_xft);
1617 if(!(largefont_xft = XftFontOpenXlfd(display,
1619 resources.large_font_xft)))
1621 largefont_xft = XftFontOpenXlfd(display,
1625 //printf("BC_WindowBase::init_fonts 1 %p\n", largefont_xft);
1626 if(!(largefont_xft = XftFontOpenXlfd(display,
1628 resources.large_font_xft)))
1630 largefont_xft = XftFontOpenXlfd(display,
1634 //printf("BC_WindowBase::init_fonts 2 %p\n", largefont_xft);
1637 if(!(mediumfont_xft = XftFontOpenXlfd(display,
1639 resources.medium_font_xft)))
1641 mediumfont_xft = XftFontOpenXlfd(display,
1647 if(!(smallfont_xft = XftFontOpenXlfd(display,
1649 resources.small_font_xft)))
1651 smallfont_xft = XftFontOpenXlfd(display,
1656 //printf("BC_WindowBase::init_fonts 100 %s %p\n",
1657 //resources.medium_font,
1660 printf("BC_WindowBase::init_fonts: %s=%p %s=%p %s=%p\n",
1661 resources.large_font_xft,
1663 resources.medium_font_xft,
1665 resources.small_font_xft,
1668 // Extension failed to locate fonts
1669 if(!largefontset || !mediumfontset || !smallfontset)
1671 printf("BC_WindowBase::init_fonts: no xft fonts found %s=%p %s=%p %s=%p\n",
1672 resources.large_font_xft,
1674 resources.medium_font_xft,
1676 resources.small_font_xft,
1678 get_resources()->use_xft = 0;
1683 if(get_resources()->use_fontset)
1688 // FIXME: should check the m,d,n values
1689 if((largefontset = XCreateFontSet(display,
1690 resources.large_fontset,
1694 largefontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
1695 if((mediumfontset = XCreateFontSet(display,
1696 resources.medium_fontset,
1700 mediumfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
1701 if((smallfontset = XCreateFontSet(display,
1702 resources.small_fontset,
1706 smallfontset = XCreateFontSet(display, "fixed,*", &m, &n, &d);
1708 if(largefontset && mediumfontset && smallfontset)
1710 curr_fontset = mediumfontset;
1711 get_resources()->use_fontset = 1;
1716 get_resources()->use_fontset = 0;
1723 int BC_WindowBase::get_color(int64_t color)
1725 // return pixel of color
1726 // use this only for drawing subwindows not for bitmaps
1727 int i, test, difference, result;
1734 return get_color_rgb8(color);
1738 // test last color looked up
1739 if(current_color_value == color) return current_color_pixel;
1742 current_color_value = color;
1743 for(i = 0; i < total_colors; i++)
1745 if(color_table[i][0] == color)
1747 current_color_pixel = color_table[i][1];
1748 return current_color_pixel;
1752 // find nearest match
1753 difference = 0xFFFFFF;
1755 for(i = 0, result = 0; i < total_colors; i++)
1757 test = abs((int)(color_table[i][0] - color));
1759 if(test < difference)
1761 current_color_pixel = color_table[i][1];
1766 return current_color_pixel;
1771 return get_color_rgb16(color);
1775 return get_color_bgr16(color);
1780 if(client_byte_order == server_byte_order)
1783 get_color_bgr24(color);
1793 int BC_WindowBase::get_color_rgb8(int color)
1797 pixel = (color & 0xc00000) >> 16;
1798 pixel += (color & 0xe000) >> 10;
1799 pixel += (color & 0xe0) >> 5;
1803 int64_t BC_WindowBase::get_color_rgb16(int color)
1806 result = (color & 0xf80000) >> 8;
1807 result += (color & 0xfc00) >> 5;
1808 result += (color & 0xf8) >> 3;
1813 int64_t BC_WindowBase::get_color_bgr16(int color)
1816 result = (color & 0xf80000) >> 19;
1817 result += (color & 0xfc00) >> 5;
1818 result += (color & 0xf8) << 8;
1823 int64_t BC_WindowBase::get_color_bgr24(int color)
1826 result = (color & 0xff) << 16;
1827 result += (color & 0xff00);
1828 result += (color & 0xff0000) >> 16;
1832 int BC_WindowBase::video_is_on()
1837 void BC_WindowBase::start_video()
1841 draw_box(0, 0, get_w(), get_h());
1845 void BC_WindowBase::stop_video()
1852 int64_t BC_WindowBase::get_color()
1854 return top_level->current_color;
1857 void BC_WindowBase::set_color(int64_t color)
1859 top_level->current_color = color;
1860 XSetForeground(top_level->display,
1862 top_level->get_color(color));
1865 void BC_WindowBase::set_opaque()
1867 XSetFunction(top_level->display, top_level->gc, GXcopy);
1870 void BC_WindowBase::set_inverse()
1872 XSetFunction(top_level->display, top_level->gc, GXxor);
1875 Cursor BC_WindowBase::get_cursor_struct(int cursor)
1879 case ARROW_CURSOR: return top_level->arrow_cursor; break;
1880 case CROSS_CURSOR: return top_level->cross_cursor;
1881 case IBEAM_CURSOR: return top_level->ibeam_cursor; break;
1882 case VSEPARATE_CURSOR: return top_level->vseparate_cursor; break;
1883 case HSEPARATE_CURSOR: return top_level->hseparate_cursor; break;
1884 case MOVE_CURSOR: return top_level->move_cursor; break;
1885 case LEFT_CURSOR: return top_level->left_cursor; break;
1886 case RIGHT_CURSOR: return top_level->right_cursor; break;
1887 case UPRIGHT_ARROW_CURSOR: return top_level->upright_arrow_cursor; break;
1888 case UPLEFT_RESIZE: return top_level->upleft_resize_cursor; break;
1889 case UPRIGHT_RESIZE: return top_level->upright_resize_cursor; break;
1890 case DOWNLEFT_RESIZE: return top_level->downleft_resize_cursor; break;
1891 case DOWNRIGHT_RESIZE: return top_level->downright_resize_cursor; break;
1896 void BC_WindowBase::set_cursor(int cursor)
1898 XDefineCursor(top_level->display, win, get_cursor_struct(cursor));
1899 current_cursor = cursor;
1903 void BC_WindowBase::set_x_cursor(int cursor)
1905 temp_cursor = XCreateFontCursor(top_level->display, cursor);
1906 XDefineCursor(top_level->display, win, temp_cursor);
1907 current_cursor = cursor;
1911 int BC_WindowBase::get_cursor()
1913 return current_cursor;
1922 XFontStruct* BC_WindowBase::get_font_struct(int font)
1924 // Clear out unrelated flags
1925 if(font & BOLDFACE) font ^= BOLDFACE;
1929 case MEDIUMFONT: return top_level->mediumfont; break;
1930 case SMALLFONT: return top_level->smallfont; break;
1931 case LARGEFONT: return top_level->largefont; break;
1936 XFontSet BC_WindowBase::get_fontset(int font)
1940 if(get_resources()->use_fontset)
1944 case SMALLFONT: fs = top_level->smallfontset; break;
1945 case LARGEFONT: fs = top_level->largefontset; break;
1946 case MEDIUMFONT: fs = top_level->mediumfontset; break;
1954 XftFont* BC_WindowBase::get_xft_struct(int font)
1956 // Clear out unrelated flags
1957 if(font & BOLDFACE) font ^= BOLDFACE;
1961 case MEDIUMFONT: return (XftFont*)top_level->mediumfont_xft; break;
1962 case SMALLFONT: return (XftFont*)top_level->smallfont_xft; break;
1963 case LARGEFONT: return (XftFont*)top_level->largefont_xft; break;
1980 void BC_WindowBase::set_font(int font)
1982 top_level->current_font = font;
1986 if(get_resources()->use_xft)
1992 if(get_resources()->use_fontset)
1997 if(get_font_struct(font))
1999 XSetFont(top_level->display, top_level->gc, get_font_struct(font)->fid);
2005 void BC_WindowBase::set_fontset(int font)
2009 if(get_resources()->use_fontset)
2013 case SMALLFONT: fs = top_level->smallfontset; break;
2014 case LARGEFONT: fs = top_level->largefontset; break;
2015 case MEDIUMFONT: fs = top_level->mediumfontset; break;
2023 XFontSet BC_WindowBase::get_curr_fontset(void)
2025 if(get_resources()->use_fontset)
2026 return curr_fontset;
2030 int BC_WindowBase::get_single_text_width(int font, char *text, int length)
2033 if(get_resources()->use_xft && get_xft_struct(font))
2036 XftTextExtents8(top_level->display,
2037 get_xft_struct(font),
2041 return extents.xOff;
2045 if(get_resources()->use_fontset && top_level->get_fontset(font))
2046 return XmbTextEscapement(top_level->get_fontset(font), text, length);
2048 if(get_font_struct(font))
2049 return XTextWidth(get_font_struct(font), text, length);
2055 case MEDIUM_7SEGMENT:
2056 return get_resources()->medium_7segment[0]->get_w() * length;
2066 int BC_WindowBase::get_text_width(int font, char *text, int length)
2068 int i, j, w = 0, line_w = 0;
2069 if(length < 0) length = strlen(text);
2071 for(i = 0, j = 0; i <= length; i++)
2076 line_w = get_single_text_width(font, &text[j], i - j);
2082 line_w = get_single_text_width(font, &text[j], length - j);
2084 if(line_w > w) w = line_w;
2087 if(i > length && w == 0)
2089 w = get_single_text_width(font, text, length);
2095 int BC_WindowBase::get_text_ascent(int font)
2098 if(get_resources()->use_xft && get_xft_struct(font))
2101 XftTextExtents8(top_level->display,
2102 get_xft_struct(font),
2110 if(get_resources()->use_fontset && top_level->get_fontset(font))
2112 XFontSetExtents *extents;
2114 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2115 return -extents->max_logical_extent.y;
2118 if(get_font_struct(font))
2119 return top_level->get_font_struct(font)->ascent;
2123 case MEDIUM_7SEGMENT:
2124 return get_resources()->medium_7segment[0]->get_h();
2132 int BC_WindowBase::get_text_descent(int font)
2135 if(get_resources()->use_xft && get_xft_struct(font))
2138 XftTextExtents8(top_level->display,
2139 get_xft_struct(font),
2143 return extents.height - extents.y;
2147 if(get_resources()->use_fontset && top_level->get_fontset(font))
2149 XFontSetExtents *extents;
2151 extents = XExtentsOfFontSet(top_level->get_fontset(font));
2152 return (extents->max_logical_extent.height
2153 + extents->max_logical_extent.y);
2156 if(get_font_struct(font))
2157 return top_level->get_font_struct(font)->descent;
2166 int BC_WindowBase::get_text_height(int font, char *text)
2168 if(!text) return get_text_ascent(font) + get_text_descent(font);
2170 // Add height of lines
2171 int h = 0, i, length = strlen(text);
2172 for(i = 0; i <= length; i++)
2180 return h * (get_text_ascent(font) + get_text_descent(font));
2183 BC_Bitmap* BC_WindowBase::new_bitmap(int w, int h, int color_model)
2185 if(color_model < 0) color_model = top_level->get_color_model();
2186 return new BC_Bitmap(top_level, w, h, color_model);
2189 int BC_WindowBase::accel_available(int color_model)
2191 if(window_type != MAIN_WINDOW)
2192 return top_level->accel_available(color_model);
2199 result = grab_port_id(this, color_model);
2202 xvideo_port_id = result;
2214 //printf("BC_WindowBase::accel_available 1\n");
2215 result = grab_port_id(this, color_model);
2216 //printf("BC_WindowBase::accel_available 2 %d\n", result);
2219 xvideo_port_id = result;
2224 //printf("BC_WindowBase::accel_available 3 %d\n", xvideo_port_id);
2231 //printf("BC_WindowBase::accel_available %d %d\n", color_model, result);
2236 int BC_WindowBase::grab_port_id(BC_WindowBase *window, int color_model)
2238 int numFormats, i, j, k;
2239 unsigned int ver, rev, numAdapt, reqBase, eventBase, errorBase;
2241 XvAdaptorInfo *info;
2242 XvImageFormatValues *formats;
2245 if(!get_resources()->use_xvideo) return -1;
2247 // Translate from color_model to X color model
2248 x_color_model = cmodel_bc_to_x(color_model);
2250 // Only local server is fast enough.
2251 if(!resources.use_shm) return -1;
2253 // XV extension is available
2254 if(Success != XvQueryExtension(window->display,
2264 // XV adaptors are available
2265 XvQueryAdaptors(window->display,
2266 DefaultRootWindow(window->display),
2275 // Get adaptor with desired color model
2276 for(i = 0; i < numAdapt && xvideo_port_id == -1; i++)
2278 /* adaptor supports XvImages */
2279 if(info[i].type & XvImageMask)
2281 formats = XvListImageFormats(window->display,
2284 // for(j = 0; j < numFormats; j++)
2285 // printf("%08x\n", formats[j].id);
2287 for(j = 0; j < numFormats && xvideo_port_id < 0; j++)
2289 /* this adaptor supports the desired format */
2290 if(formats[j].id == x_color_model)
2292 /* Try to grab a port */
2293 for(k = 0; k < info[i].num_ports; k++)
2296 if(Success == XvGrabPort(top_level->display,
2297 info[i].base_id + k,
2300 //printf("BC_WindowBase::grab_port_id %llx\n", info[i].base_id);
2301 xvideo_port_id = info[i].base_id + k;
2307 if(formats) XFree(formats);
2311 XvFreeAdaptorInfo(info);
2313 return xvideo_port_id;
2317 int BC_WindowBase::show_window(int flush)
2319 XMapWindow(top_level->display, win);
2320 if(flush) XFlush(top_level->display);
2321 // XSync(top_level->display, 0);
2326 int BC_WindowBase::hide_window(int flush)
2328 XUnmapWindow(top_level->display, win);
2329 if(flush) XFlush(top_level->display);
2334 BC_MenuBar* BC_WindowBase::add_menubar(BC_MenuBar *menu_bar)
2336 subwindows->append((BC_SubWindow*)menu_bar);
2338 menu_bar->parent_window = this;
2339 menu_bar->top_level = this->top_level;
2340 menu_bar->initialize();
2344 BC_WindowBase* BC_WindowBase::add_subwindow(BC_WindowBase *subwindow)
2346 subwindows->append(subwindow);
2348 if(subwindow->bg_color == -1) subwindow->bg_color = this->bg_color;
2350 // parent window must be set before the subwindow initialization
2351 subwindow->parent_window = this;
2352 subwindow->top_level = this->top_level;
2354 // Execute derived initialization
2355 subwindow->initialize();
2360 BC_WindowBase* BC_WindowBase::add_tool(BC_WindowBase *subwindow)
2362 return add_subwindow(subwindow);
2365 int BC_WindowBase::flash(int x, int y, int w, int h, int flush)
2368 XSetWindowBackgroundPixmap(top_level->display, win, pixmap);
2371 XClearArea(top_level->display, win, x, y, w, h, 0);
2375 XClearWindow(top_level->display, win);
2383 int BC_WindowBase::flash(int flush)
2385 flash(-1, -1, -1, -1, flush);
2388 void BC_WindowBase::flush()
2390 XFlush(top_level->display);
2393 void BC_WindowBase::sync_display()
2395 XSync(top_level->display, False);
2398 int BC_WindowBase::get_window_lock()
2400 return top_level->window_lock;
2403 int BC_WindowBase::lock_window(char *location)
2405 if(top_level && top_level != this)
2407 top_level->lock_window(location);
2412 SET_LOCK(this, title, location);
2413 XLockDisplay(top_level->display);
2415 top_level->window_lock = 1;
2419 printf("BC_WindowBase::lock_window top_level NULL\n");
2424 int BC_WindowBase::unlock_window()
2426 if(top_level && top_level != this)
2428 top_level->unlock_window();
2434 top_level->window_lock = 0;
2435 XUnlockDisplay(top_level->display);
2439 printf("BC_WindowBase::unlock_window top_level NULL\n");
2444 void BC_WindowBase::set_done(int return_value)
2446 if(window_type != MAIN_WINDOW)
2447 top_level->set_done(return_value);
2451 XEvent *event = new XEvent;
2452 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
2454 event->type = ClientMessage;
2455 ptr->message_type = SetDoneXAtom;
2457 this->return_value = return_value;
2459 // May lock up here because XSendEvent doesn't work too well
2460 // asynchronous with XNextEvent.
2461 // This causes BC_WindowEvents to forward a copy of the event to run_window where
2463 event_thread->done = 1;
2474 int BC_WindowBase::get_w()
2479 int BC_WindowBase::get_h()
2484 int BC_WindowBase::get_x()
2489 int BC_WindowBase::get_y()
2494 int BC_WindowBase::get_root_w(int ignore_dualhead, int lock_display)
2496 if(lock_display) lock_window("BC_WindowBase::get_root_w");
2497 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
2498 int result = WidthOfScreen(screen_ptr);
2499 // Wider than 16:9, narrower than dual head
2500 if(!ignore_dualhead) if((float)result / HeightOfScreen(screen_ptr) > 1.8) result /= 2;
2502 if(lock_display) unlock_window();
2506 int BC_WindowBase::get_root_h(int lock_display)
2508 if(lock_display) lock_window("BC_WindowBase::get_root_h");
2509 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
2510 int result = HeightOfScreen(screen_ptr);
2511 if(lock_display) unlock_window();
2515 // Bottom right corner
2516 int BC_WindowBase::get_x2()
2521 int BC_WindowBase::get_y2()
2526 int BC_WindowBase::get_hidden()
2528 return top_level->hidden;
2531 int BC_WindowBase::cursor_inside()
2533 return (top_level->cursor_x >= 0 &&
2534 top_level->cursor_y >= 0 &&
2535 top_level->cursor_x < w &&
2536 top_level->cursor_y < h);
2539 BC_WindowBase* BC_WindowBase::get_top_level()
2544 BC_WindowBase* BC_WindowBase::get_parent()
2546 return parent_window;
2549 int BC_WindowBase::get_color_model()
2551 return top_level->color_model;
2554 BC_Resources* BC_WindowBase::get_resources()
2556 return &BC_WindowBase::resources;
2559 int BC_WindowBase::get_bg_color()
2564 BC_Pixmap* BC_WindowBase::get_bg_pixmap()
2569 void BC_WindowBase::set_active_subwindow(BC_WindowBase *subwindow)
2571 top_level->active_subwindow = subwindow;
2574 int BC_WindowBase::activate()
2579 int BC_WindowBase::deactivate()
2581 if(window_type == MAIN_WINDOW)
2583 if(top_level->active_menubar) top_level->active_menubar->deactivate();
2584 if(top_level->active_popup_menu) top_level->active_popup_menu->deactivate();
2585 if(top_level->active_subwindow) top_level->active_subwindow->deactivate();
2587 top_level->active_menubar = 0;
2588 top_level->active_popup_menu = 0;
2589 top_level->active_subwindow = 0;
2594 int BC_WindowBase::cycle_textboxes(int amount)
2597 BC_WindowBase *new_textbox = 0;
2601 BC_WindowBase *first_textbox = 0;
2602 find_next_textbox(&first_textbox, &new_textbox, result);
2603 if(!new_textbox) new_textbox = first_textbox;
2609 BC_WindowBase *last_textbox = 0;
2610 find_prev_textbox(&last_textbox, &new_textbox, result);
2611 if(!new_textbox) new_textbox = last_textbox;
2615 if(new_textbox != active_subwindow)
2618 new_textbox->activate();
2624 int BC_WindowBase::find_next_textbox(BC_WindowBase **first_textbox, BC_WindowBase **next_textbox, int &result)
2626 // Search subwindows for textbox
2627 for(int i = 0; i < subwindows->total && result < 2; i++)
2629 BC_WindowBase *test_subwindow = subwindows->values[i];
2630 test_subwindow->find_next_textbox(first_textbox, next_textbox, result);
2637 if(!*first_textbox) *first_textbox = this;
2641 if(top_level->active_subwindow == this)
2647 *next_textbox = this;
2654 int BC_WindowBase::find_prev_textbox(BC_WindowBase **last_textbox, BC_WindowBase **prev_textbox, int &result)
2660 if(!*last_textbox) *last_textbox = this;
2664 if(top_level->active_subwindow == this)
2670 *prev_textbox = this;
2675 // Search subwindows for textbox
2676 for(int i = subwindows->total - 1; i >= 0 && result < 2; i--)
2678 BC_WindowBase *test_subwindow = subwindows->values[i];
2679 test_subwindow->find_prev_textbox(last_textbox, prev_textbox, result);
2684 BC_Clipboard* BC_WindowBase::get_clipboard()
2686 return top_level->clipboard;
2689 int BC_WindowBase::get_relative_cursor_x()
2691 int abs_x, abs_y, x, y, win_x, win_y;
2692 unsigned int temp_mask;
2695 XQueryPointer(top_level->display,
2705 XTranslateCoordinates(top_level->display,
2717 int BC_WindowBase::get_relative_cursor_y()
2719 int abs_x, abs_y, x, y, win_x, win_y;
2720 unsigned int temp_mask;
2723 XQueryPointer(top_level->display,
2733 XTranslateCoordinates(top_level->display,
2745 int BC_WindowBase::get_abs_cursor_x(int lock_window)
2747 int abs_x, abs_y, win_x, win_y;
2748 unsigned int temp_mask;
2751 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_x");
2752 XQueryPointer(top_level->display,
2761 if(lock_window) this->unlock_window();
2765 int BC_WindowBase::get_abs_cursor_y(int lock_window)
2767 int abs_x, abs_y, win_x, win_y;
2768 unsigned int temp_mask;
2771 if(lock_window) this->lock_window("BC_WindowBase::get_abs_cursor_y");
2772 XQueryPointer(top_level->display,
2781 if(lock_window) this->unlock_window();
2785 int BC_WindowBase::match_window(Window win)
2787 if (this->win == win) return 1;
2789 for(int i = 0; i < subwindows->total; i++)
2791 result = subwindows->values[i]->match_window(win);
2792 if (result) return result;
2798 int BC_WindowBase::get_cursor_over_window()
2800 if(top_level != this) return top_level->get_cursor_over_window();
2802 int abs_x, abs_y, win_x, win_y;
2803 unsigned int temp_mask;
2804 Window temp_win1, temp_win2;
2806 if (!XQueryPointer(display,
2817 int result = match_window(temp_win2) ;
2821 int BC_WindowBase::relative_cursor_x(BC_WindowBase *pov)
2826 translate_coordinates(top_level->event_win,
2828 top_level->cursor_x,
2829 top_level->cursor_y,
2835 int BC_WindowBase::relative_cursor_y(BC_WindowBase *pov)
2840 translate_coordinates(top_level->event_win,
2842 top_level->cursor_x,
2843 top_level->cursor_y,
2849 int BC_WindowBase::get_drag_x()
2851 return top_level->drag_x;
2854 int BC_WindowBase::get_drag_y()
2856 return top_level->drag_y;
2859 int BC_WindowBase::get_cursor_x()
2861 return top_level->cursor_x;
2864 int BC_WindowBase::get_cursor_y()
2866 return top_level->cursor_y;
2869 int BC_WindowBase::is_event_win()
2871 return this->win == top_level->event_win;
2874 void BC_WindowBase::set_dragging(int value)
2876 is_dragging = value;
2879 int BC_WindowBase::get_dragging()
2884 int BC_WindowBase::get_buttonpress()
2886 return top_level->button_number;
2889 int BC_WindowBase::get_button_down()
2891 return top_level->button_down;
2894 int BC_WindowBase::alt_down()
2896 return top_level->alt_mask;
2899 int BC_WindowBase::shift_down()
2901 return top_level->shift_mask;
2904 int BC_WindowBase::ctrl_down()
2906 return top_level->ctrl_mask;
2910 int BC_WindowBase::get_keypress()
2912 return top_level->key_pressed;
2915 int BC_WindowBase::get_double_click()
2917 return top_level->double_click;
2920 int BC_WindowBase::get_bgcolor()
2925 int BC_WindowBase::resize_window(int w, int h)
2927 if(window_type == MAIN_WINDOW && !allow_resize)
2929 XSizeHints size_hints;
2930 size_hints.flags = PSize | PMinSize | PMaxSize;
2931 size_hints.width = w;
2932 size_hints.height = h;
2933 size_hints.min_width = w;
2934 size_hints.max_width = w;
2935 size_hints.min_height = h;
2936 size_hints.max_height = h;
2937 XSetNormalHints(top_level->display, win, &size_hints);
2939 XResizeWindow(top_level->display, win, w, h);
2943 XFreePixmap(top_level->display, pixmap);
2944 pixmap = XCreatePixmap(top_level->display, win, w, h, top_level->default_depth);
2946 // Propagate to menubar
2947 for(int i = 0; i < subwindows->total; i++)
2949 subwindows->values[i]->dispatch_resize_event(w, h);
2952 draw_background(0, 0, w, h);
2953 if(top_level == this && get_resources()->recursive_resizing)
2954 resize_history.append(new BC_ResizeCall(w, h));
2958 // The only way for resize events to be propagated is by updating the internal w and h
2959 int BC_WindowBase::resize_event(int w, int h)
2961 if(window_type == MAIN_WINDOW)
2969 int BC_WindowBase::reposition_window(int x, int y, int w, int h)
2973 // Some tools set their own dimensions before calling this, causing the
2974 // resize check to skip.
2978 if(w > 0 && w != this->w)
2984 if(h > 0 && h != this->h)
2990 //printf("BC_WindowBase::reposition_window %d %d %d\n", translation_count, x_correction, y_correction);
2993 printf("BC_WindowBase::reposition_window this->w == %d\n", this->w);
2995 printf("BC_WindowBase::reposition_window this->h == %d\n", this->h);
2997 if(translation_count && window_type == MAIN_WINDOW)
2999 // KDE shifts window right and down.
3000 // FVWM leaves window alone and adds border around it.
3001 XMoveResizeWindow(top_level->display,
3003 x + BC_DisplayInfo::left_border - BC_DisplayInfo::auto_reposition_x,
3004 y + BC_DisplayInfo::top_border - BC_DisplayInfo::auto_reposition_y,
3010 XMoveResizeWindow(top_level->display,
3020 XFreePixmap(top_level->display, pixmap);
3021 pixmap = XCreatePixmap(top_level->display,
3025 top_level->default_depth);
3027 // Propagate to menubar
3028 for(int i = 0; i < subwindows->total; i++)
3030 subwindows->values[i]->dispatch_resize_event(this->w, this->h);
3033 // draw_background(0, 0, w, h);
3039 int BC_WindowBase::set_tooltips(int tooltips_enabled)
3041 get_resources()->tooltips_enabled = tooltips_enabled;
3045 int BC_WindowBase::raise_window(int do_flush)
3047 XRaiseWindow(top_level->display, win);
3048 if(do_flush) XFlush(top_level->display);
3052 void BC_WindowBase::set_background(VFrame *bitmap)
3054 if(bg_pixmap && !shared_bg_pixmap) delete bg_pixmap;
3056 bg_pixmap = new BC_Pixmap(this,
3059 shared_bg_pixmap = 0;
3060 draw_background(0, 0, w, h);
3063 void BC_WindowBase::set_title(char *text)
3065 XSetStandardProperties(top_level->display, top_level->win, text, text, None, 0, 0, 0);
3066 strcpy(this->title, _(text));
3070 char* BC_WindowBase::get_title()
3075 int BC_WindowBase::get_toggle_value()
3077 return toggle_value;
3080 int BC_WindowBase::get_toggle_drag()
3085 int BC_WindowBase::set_icon(VFrame *data)
3087 if(icon_pixmap) delete icon_pixmap;
3088 icon_pixmap = new BC_Pixmap(top_level,
3093 icon_window = new BC_Popup(this,
3096 icon_pixmap->get_w(),
3097 icon_pixmap->get_h(),
3099 1, // All windows are hidden initially
3103 wm_hints.flags = IconPixmapHint | IconMaskHint | IconWindowHint;
3104 wm_hints.icon_pixmap = icon_pixmap->get_pixmap();
3105 wm_hints.icon_mask = icon_pixmap->get_alpha();
3106 wm_hints.icon_window = icon_window->win;
3107 // for(int i = 0; i < 1000; i++)
3108 // printf("02x ", icon_pixmap->get_alpha()->get_row_pointers()[0][i]);
3111 XSetWMHints(top_level->display, top_level->win, &wm_hints);
3112 XSync(top_level->display, 0);
3116 int BC_WindowBase::set_w(int w)
3122 int BC_WindowBase::set_h(int h)
3128 int BC_WindowBase::load_defaults(Defaults *defaults)
3130 get_resources()->filebox_mode = defaults->get("FILEBOX_MODE", get_resources()->filebox_mode);
3131 get_resources()->filebox_w = defaults->get("FILEBOX_W", get_resources()->filebox_w);
3132 get_resources()->filebox_h = defaults->get("FILEBOX_H", get_resources()->filebox_h);
3133 defaults->get("FILEBOX_FILTER", get_resources()->filebox_filter);
3137 int BC_WindowBase::save_defaults(Defaults *defaults)
3139 defaults->update("FILEBOX_MODE", get_resources()->filebox_mode);
3140 defaults->update("FILEBOX_W", get_resources()->filebox_w);
3141 //printf("BC_ListBox::cursor_motion_event 1 %lld\n", timer.get_difference());
3142 defaults->update("FILEBOX_H", get_resources()->filebox_h);
3143 defaults->update("FILEBOX_FILTER", get_resources()->filebox_filter);
3149 // For some reason XTranslateCoordinates can take a long time to return.
3150 // We work around this by only calling it when the event windows are different.
3151 void BC_WindowBase::translate_coordinates(Window src_w,
3163 *dest_x_return = src_x;
3164 *dest_y_return = src_y;
3168 XTranslateCoordinates(top_level->display,
3176 //printf("BC_WindowBase::translate_coordinates 1 %lld\n", timer.get_difference());
3186 #ifdef HAVE_LIBXXF86VM
3187 void BC_WindowBase::closest_vm(int *vm, int *width, int *height)
3191 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar)) {
3193 XF86VidModeModeInfo **vm_modelines;
3194 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
3195 for (i = 0; i < vm_count; i++) {
3196 if (vm_modelines[i]->hdisplay < vm_modelines[*vm]->hdisplay && vm_modelines[i]->hdisplay >= *width)
3199 display = top_level->display;
3200 if (vm_modelines[*vm]->hdisplay == *width)
3204 *width = vm_modelines[*vm]->hdisplay;
3205 *height = vm_modelines[*vm]->vdisplay;
3210 void BC_WindowBase::scale_vm(int vm)
3212 int foo,bar,dotclock;
3213 if(XF86VidModeQueryExtension(top_level->display,&foo,&bar))
3216 XF86VidModeModeInfo **vm_modelines;
3217 XF86VidModeModeLine vml;
3218 XF86VidModeGetAllModeLines(top_level->display,XDefaultScreen(top_level->display),&vm_count,&vm_modelines);
3219 XF86VidModeGetModeLine(top_level->display,XDefaultScreen(top_level->display),&dotclock,&vml);
3220 orig_modeline.dotclock = dotclock;
3221 orig_modeline.hdisplay = vml.hdisplay;
3222 orig_modeline.hsyncstart = vml.hsyncstart;
3223 orig_modeline.hsyncend = vml.hsyncend;
3224 orig_modeline.htotal = vml.htotal;
3225 orig_modeline.vdisplay = vml.vdisplay;
3226 orig_modeline.vsyncstart = vml.vsyncstart;
3227 orig_modeline.vsyncend = vml.vsyncend;
3228 orig_modeline.vtotal = vml.vtotal;
3229 orig_modeline.flags = vml.flags;
3230 orig_modeline.privsize = vml.privsize;
3231 // orig_modeline.private = vml.private;
3232 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),vm_modelines[vm]);
3233 XF86VidModeSetViewPort(top_level->display,XDefaultScreen(top_level->display),0,0);
3234 XFlush(top_level->display);
3238 void BC_WindowBase::restore_vm()
3240 XF86VidModeSwitchToMode(top_level->display,XDefaultScreen(top_level->display),&orig_modeline);
3241 XFlush(top_level->display);
3252 GLXContext glXCreateContext(Display *dpy,
3254 GLXContext shareList,
3257 int glXMakeCurrent(Display *dpy,
3261 void glXSwapBuffers(Display *dpy,
3267 void BC_WindowBase::enable_opengl()
3269 lock_window("BC_WindowBase::enable_opengl");
3272 XVisualInfo viproto;
3273 XVisualInfo *visinfo;
3276 //printf("BC_WindowBase::enable_opengl 1\n");
3277 viproto.screen = top_level->screen;
3278 visinfo = XGetVisualInfo(top_level->display,
3282 //printf("BC_WindowBase::enable_opengl 1 %p\n", visinfo);
3284 gl_context = glXCreateContext(top_level->display,
3288 //printf("BC_WindowBase::enable_opengl 1\n");
3290 glXMakeCurrent(top_level->display,
3294 unsigned long valuemask = CWEventMask;
3295 XSetWindowAttributes attributes;
3296 attributes.event_mask = DEFAULT_EVENT_MASKS |
3298 XChangeWindowAttributes(top_level->display, win, valuemask, &attributes);
3300 opengl_lock.unlock();
3302 //printf("BC_WindowBase::enable_opengl 2\n");
3305 void BC_WindowBase::disable_opengl()
3307 unsigned long valuemask = CWEventMask;
3308 XSetWindowAttributes attributes;
3309 attributes.event_mask = DEFAULT_EVENT_MASKS;
3310 XChangeWindowAttributes(top_level->display, win, valuemask, &attributes);
3313 void BC_WindowBase::lock_opengl()
3315 lock_window("BC_WindowBase::lock_opengl");
3317 glXMakeCurrent(top_level->display,
3322 void BC_WindowBase::unlock_opengl()
3324 opengl_lock.unlock();
3328 void BC_WindowBase::flip_opengl()
3330 glXSwapBuffers(top_level->display, win);
3334 int BC_WindowBase::get_event_count()
3336 event_lock->lock("BC_WindowBase::get_event_count");
3337 int result = common_events.total;
3338 event_lock->unlock();
3342 XEvent* BC_WindowBase::get_event()
3345 while(!done && !result)
3347 event_condition->lock("BC_WindowBase::get_event");
3348 event_lock->lock("BC_WindowBase::get_event");
3349 if(common_events.total && !done)
3351 result = common_events.values[0];
3352 common_events.remove_number(0);
3354 event_lock->unlock();
3359 void BC_WindowBase::put_event(XEvent *event)
3361 event_lock->lock("BC_WindowBase::put_event");
3362 common_events.append(event);
3363 event_lock->unlock();
3364 event_condition->unlock();