Commented out XSetInputFocus for now
[wxWidgets.git] / src / x11 / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: windows.cpp
3 // Purpose: wxWindow
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "window.h"
22 #endif
23
24 #include "wx/setup.h"
25 #include "wx/menu.h"
26 #include "wx/dc.h"
27 #include "wx/dcclient.h"
28 #include "wx/utils.h"
29 #include "wx/app.h"
30 #include "wx/panel.h"
31 #include "wx/layout.h"
32 #include "wx/dialog.h"
33 #include "wx/listbox.h"
34 #include "wx/button.h"
35 #include "wx/settings.h"
36 #include "wx/msgdlg.h"
37 #include "wx/frame.h"
38 #include "wx/scrolwin.h"
39 #include "wx/module.h"
40 #include "wx/menuitem.h"
41 #include "wx/log.h"
42
43 #if wxUSE_DRAG_AND_DROP
44 #include "wx/dnd.h"
45 #endif
46
47 #include "wx/x11/private.h"
48 #include "X11/Xutil.h"
49
50 #include <string.h>
51
52 // ----------------------------------------------------------------------------
53 // global variables for this module
54 // ----------------------------------------------------------------------------
55
56 extern wxHashTable *wxWidgetHashTable;
57 static wxWindow* g_captureWindow = NULL;
58
59 // ----------------------------------------------------------------------------
60 // macros
61 // ----------------------------------------------------------------------------
62
63 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
64 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
65 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
66
67 // ----------------------------------------------------------------------------
68 // event tables
69 // ----------------------------------------------------------------------------
70
71 IMPLEMENT_ABSTRACT_CLASS(wxWindowX11, wxWindowBase)
72
73 BEGIN_EVENT_TABLE(wxWindowX11, wxWindowBase)
74 EVT_SYS_COLOUR_CHANGED(wxWindowX11::OnSysColourChanged)
75 END_EVENT_TABLE()
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // helper functions
83 // ----------------------------------------------------------------------------
84
85 // ----------------------------------------------------------------------------
86 // constructors
87 // ----------------------------------------------------------------------------
88
89 void wxWindowX11::Init()
90 {
91 // generic initializations first
92 InitBase();
93
94 // X11-specific
95 m_mainWidget = (WXWindow) 0;
96
97 m_winCaptured = FALSE;
98
99 m_isShown = TRUE;
100 m_isBeingDeleted = FALSE;
101
102 m_lastTS = 0;
103 m_lastButton = 0;
104 }
105
106 // real construction (Init() must have been called before!)
107 bool wxWindowX11::Create(wxWindow *parent, wxWindowID id,
108 const wxPoint& pos,
109 const wxSize& size,
110 long style,
111 const wxString& name)
112 {
113 wxCHECK_MSG( parent, FALSE, "can't create wxWindow without parent" );
114
115 CreateBase(parent, id, pos, size, style, wxDefaultValidator, name);
116
117 parent->AddChild(this);
118
119 int w = size.GetWidth();
120 int h = size.GetHeight();
121 int x = size.GetX();
122 int y = size.GetY();
123 if (w == -1) w = 20;
124 if (h == -1) h = 20;
125 if (x == -1) x = 0;
126 if (y == -1) y = 0;
127
128 Display *xdisplay = (Display*) wxGlobalDisplay();
129 int xscreen = DefaultScreen( xdisplay );
130 Colormap cm = DefaultColormap( xdisplay, xscreen );
131
132 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
133 m_backgroundColour.CalcPixel( (WXColormap) cm );
134 m_hasBgCol = TRUE;
135
136 m_foregroundColour = *wxBLACK;
137 m_foregroundColour.CalcPixel( (WXColormap) cm );
138
139
140 Window parentWindow = (Window) parent->GetMainWindow();
141
142 Window window = XCreateSimpleWindow(
143 xdisplay, parentWindow,
144 x, y, w, h, 0,
145 m_backgroundColour.GetPixel(),
146 m_backgroundColour.GetPixel() );
147
148 m_mainWidget = (WXWindow) window;
149
150 // Select event types wanted
151 XSelectInput(wxGlobalDisplay(), window,
152 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
153 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
154 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
155 PropertyChangeMask);
156
157 wxAddWindowToTable(window, (wxWindow*) this);
158
159 // Is a subwindow, so map immediately
160 m_isShown = TRUE;
161 XMapWindow(wxGlobalDisplay(), window);
162
163 // Without this, the cursor may not be restored properly (e.g. in splitter
164 // sample).
165 SetCursor(*wxSTANDARD_CURSOR);
166 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
167 SetSize(pos.x, pos.y, size.x, size.y);
168
169 return TRUE;
170 }
171
172 // Destructor
173 wxWindowX11::~wxWindowX11()
174 {
175 if (g_captureWindow == this)
176 g_captureWindow = NULL;
177
178 m_isBeingDeleted = TRUE;
179
180 // X11-specific actions first
181 Window main = (Window) m_mainWidget;
182 if ( main )
183 {
184 // Removes event handlers
185 //DetachWidget(main);
186 }
187
188 if (m_parent)
189 m_parent->RemoveChild( this );
190
191 DestroyChildren();
192
193 // Destroy the window
194 if (main)
195 {
196 XSelectInput( wxGlobalDisplay(), main, NoEventMask);
197 wxDeleteWindowFromTable( main );
198 XDestroyWindow( wxGlobalDisplay(), main );
199 m_mainWidget = NULL;
200 }
201 }
202
203 // ---------------------------------------------------------------------------
204 // basic operations
205 // ---------------------------------------------------------------------------
206
207 void wxWindowX11::SetFocus()
208 {
209 Window wMain = (Window) GetMainWindow();
210 if (wMain)
211 {
212 // TODO: set a m_needInputFocus flag and do the
213 // the setting in OnIdle or Show, because we can't
214 // set the focus for an unmapped window.
215 // We need to figure out how to find out if the window
216 // is mapped.
217 #if 0
218 XSetInputFocus(wxGlobalDisplay(), wMain, RevertToParent, CurrentTime);
219
220 XWMHints wmhints;
221 wmhints.flags = InputHint;
222 wmhints.input = True;
223 XSetWMHints(wxGlobalDisplay(), wMain, &wmhints);
224 #endif
225 }
226 }
227
228 // Get the window with the focus
229 wxWindow *wxWindowBase::FindFocus()
230 {
231 Window wFocus = (Window) 0;
232 int revert = 0;
233
234 XGetInputFocus(wxGlobalDisplay(), & wFocus, & revert);
235 if (wFocus)
236 {
237 wxWindow *win = NULL;
238 do
239 {
240 win = wxGetWindowFromTable(wFocus);
241 wFocus = wxGetWindowParent(wFocus);
242 } while (wFocus && !win);
243
244 return win;
245 }
246
247 return NULL;
248 }
249
250 // Enabling/disabling handled by event loop, and not sending events
251 // if disabled.
252 bool wxWindowX11::Enable(bool enable)
253 {
254 if ( !wxWindowBase::Enable(enable) )
255 return FALSE;
256
257 return TRUE;
258 }
259
260 bool wxWindowX11::Show(bool show)
261 {
262 wxWindowBase::Show(show);
263
264 Window xwin = (Window) GetXWindow();
265 Display *xdisp = (Display*) GetXDisplay();
266 if (show)
267 {
268 wxString msg;
269 msg.Printf("Mapping window of type %s", GetClassInfo()->GetClassName());
270 wxLogDebug(msg);
271 XMapWindow(xdisp, xwin);
272 XSync(xdisp, False);
273 }
274 else
275 {
276 wxString msg;
277 msg.Printf("Unmapping window of type %s", GetClassInfo()->GetClassName());
278 wxLogDebug(msg);
279 XUnmapWindow(xdisp, xwin);
280 }
281
282 return TRUE;
283 }
284
285 // Raise the window to the top of the Z order
286 void wxWindowX11::Raise()
287 {
288 if (m_mainWidget)
289 XRaiseWindow( wxGlobalDisplay(), (Window) m_mainWidget );
290 }
291
292 // Lower the window to the bottom of the Z order
293 void wxWindowX11::Lower()
294 {
295 if (m_mainWidget)
296 XLowerWindow( wxGlobalDisplay(), (Window) m_mainWidget );
297 }
298
299 void wxWindowX11::DoCaptureMouse()
300 {
301 if ((g_captureWindow != NULL) && (g_captureWindow != this))
302 {
303 wxASSERT_MSG(FALSE, "Trying to capture before mouse released.");
304
305 // Core dump now
306 int *tmp = NULL;
307 (*tmp) = 1;
308 return;
309 }
310
311 if (m_winCaptured)
312 return;
313
314 Window xwindow = (Window) GetMainWindow();
315
316 g_captureWindow = (wxWindow*) this;
317
318 if (xwindow)
319 {
320 int res = XGrabPointer(wxGlobalDisplay(), xwindow,
321 FALSE,
322 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
323 GrabModeAsync,
324 GrabModeAsync,
325 None,
326 None, /* cursor */ // TODO: This may need to be set to the cursor of this window
327 CurrentTime );
328
329 if (res != GrabSuccess)
330 {
331 wxString msg;
332 msg.Printf("Failed to grab pointer for window %s", this->GetClassInfo()->GetClassName());
333 wxLogDebug(msg);
334 if (res == GrabNotViewable)
335 {
336 wxLogDebug("This is not a viewable window - perhaps not shown yet?");
337 }
338 g_captureWindow = NULL;
339 return;
340 }
341
342 wxLogDebug("Grabbed pointer");
343
344 #if 0
345 res = XGrabButton(wxGlobalDisplay(), AnyButton, AnyModifier,
346 (Window) GetMainWindow(),
347 FALSE,
348 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
349 GrabModeAsync,
350 GrabModeAsync,
351 None,
352 None);
353
354 if (res != GrabSuccess)
355 {
356 wxLogDebug("Failed to grab mouse buttons.");
357 XUngrabPointer(wxGlobalDisplay(), CurrentTime);
358 return;
359 }
360 #endif
361
362 #if 0
363 res = XGrabKeyboard(wxGlobalDisplay(), (Window) GetMainWindow(),
364 ShiftMask | LockMask | ControlMask | Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask,
365 FALSE,
366 GrabModeAsync,
367 GrabModeAsync,
368 CurrentTime);
369
370 if (res != GrabSuccess)
371 {
372 wxLogDebug("Failed to grab keyboard.");
373 XUngrabPointer(wxGlobalDisplay(), CurrentTime);
374 XUngrabButton(wxGlobalDisplay(), AnyButton, AnyModifier,
375 (Window) GetMainWindow());
376 return;
377 }
378 #endif
379
380 m_winCaptured = TRUE;
381 }
382 }
383
384 void wxWindowX11::DoReleaseMouse()
385 {
386 g_captureWindow = NULL;
387
388 if ( !m_winCaptured )
389 return;
390
391 Window xwindow = (Window) GetMainWindow();
392
393 if (xwindow)
394 {
395 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
396 #if 0
397 XUngrabButton( wxGlobalDisplay(), AnyButton, AnyModifier, xwindow);
398 XUngrabKeyboard( wxGlobalDisplay(), CurrentTime );
399 #endif
400 }
401 wxLogDebug("Ungrabbed pointer");
402
403 m_winCaptured = FALSE;
404 }
405
406 bool wxWindowX11::SetFont(const wxFont& font)
407 {
408 if ( !wxWindowBase::SetFont(font) )
409 {
410 // nothing to do
411 return FALSE;
412 }
413
414 return TRUE;
415 }
416
417 bool wxWindowX11::SetCursor(const wxCursor& cursor)
418 {
419 if ( !wxWindowBase::SetCursor(cursor) )
420 {
421 // no change
422 return FALSE;
423 }
424
425 wxCursor* cursor2 = NULL;
426 if (m_cursor.Ok())
427 cursor2 = & m_cursor;
428 else
429 cursor2 = wxSTANDARD_CURSOR;
430
431 WXCursor x_cursor = cursor2->GetCursor();
432
433 Window win = (Window) GetMainWindow();
434 XDefineCursor((Display*) wxGlobalDisplay(), win, (Cursor) x_cursor);
435
436 return TRUE;
437 }
438
439 // Coordinates relative to the window
440 void wxWindowX11::WarpPointer (int x, int y)
441 {
442 if (m_mainWidget)
443 XWarpPointer( wxGlobalDisplay(), None, (Window) m_mainWidget, 0, 0, 0, 0, x, y);
444 }
445
446 // Does a physical scroll
447 void wxWindowX11::ScrollWindow(int dx, int dy, const wxRect *rect)
448 {
449 #if 0
450 int x, y, w, h;
451 if (rect)
452 {
453 // Use specified rectangle
454 x = rect->x; y = rect->y; w = rect->width; h = rect->height;
455 }
456 else
457 {
458 // Use whole client area
459 x = 0; y = 0;
460 GetClientSize(& w, & h);
461 }
462
463 wxNode *cnode = m_children.First();
464 while (cnode)
465 {
466 wxWindow *child = (wxWindow*) cnode->Data();
467 int sx = 0;
468 int sy = 0;
469 child->GetSize( &sx, &sy );
470 wxPoint pos( child->GetPosition() );
471 child->SetSize( pos.x + dx, pos.y + dy, sx, sy, wxSIZE_ALLOW_MINUS_ONE );
472 cnode = cnode->Next();
473 }
474
475 int x1 = (dx >= 0) ? x : x - dx;
476 int y1 = (dy >= 0) ? y : y - dy;
477 int w1 = w - abs(dx);
478 int h1 = h - abs(dy);
479 int x2 = (dx >= 0) ? x + dx : x;
480 int y2 = (dy >= 0) ? y + dy : y;
481
482 wxClientDC dc((wxWindow*) this);
483
484 dc.SetLogicalFunction (wxCOPY);
485
486 Window window = (Window) GetMainWindow();
487 Display* display = wxGlobalDisplay();
488
489 XCopyArea(display, window, window, (GC) dc.GetGC(),
490 x1, y1, w1, h1, x2, y2);
491
492 dc.SetAutoSetting(TRUE);
493 wxBrush brush(GetBackgroundColour(), wxSOLID);
494 dc.SetBrush(brush); // FIXME: needed?
495
496 // We'll add rectangles to the list of update rectangles according to which
497 // bits we've exposed.
498 wxList updateRects;
499
500 if (dx > 0)
501 {
502 wxRect *rect = new wxRect;
503 rect->x = x;
504 rect->y = y;
505 rect->width = dx;
506 rect->height = h;
507
508 XFillRectangle(display, window,
509 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
510
511 rect->x = rect->x;
512 rect->y = rect->y;
513 rect->width = rect->width;
514 rect->height = rect->height;
515
516 updateRects.Append((wxObject*) rect);
517 }
518 else if (dx < 0)
519 {
520 wxRect *rect = new wxRect;
521
522 rect->x = x + w + dx;
523 rect->y = y;
524 rect->width = -dx;
525 rect->height = h;
526
527 XFillRectangle(display, window,
528 (GC) dc.GetGC(), rect->x, rect->y, rect->width,
529 rect->height);
530
531 rect->x = rect->x;
532 rect->y = rect->y;
533 rect->width = rect->width;
534 rect->height = rect->height;
535
536 updateRects.Append((wxObject*) rect);
537 }
538 if (dy > 0)
539 {
540 wxRect *rect = new wxRect;
541
542 rect->x = x;
543 rect->y = y;
544 rect->width = w;
545 rect->height = dy;
546
547 XFillRectangle(display, window,
548 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
549
550 rect->x = rect->x;
551 rect->y = rect->y;
552 rect->width = rect->width;
553 rect->height = rect->height;
554
555 updateRects.Append((wxObject*) rect);
556 }
557 else if (dy < 0)
558 {
559 wxRect *rect = new wxRect;
560
561 rect->x = x;
562 rect->y = y + h + dy;
563 rect->width = w;
564 rect->height = -dy;
565
566 XFillRectangle(display, window,
567 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
568
569 rect->x = rect->x;
570 rect->y = rect->y;
571 rect->width = rect->width;
572 rect->height = rect->height;
573
574 updateRects.Append((wxObject*) rect);
575 }
576 dc.SetBrush(wxNullBrush);
577
578 // Now send expose events
579
580 wxNode* node = updateRects.First();
581 while (node)
582 {
583 wxRect* rect = (wxRect*) node->Data();
584 XExposeEvent event;
585
586 event.type = Expose;
587 event.display = display;
588 event.send_event = True;
589 event.window = window;
590
591 event.x = rect->x;
592 event.y = rect->y;
593 event.width = rect->width;
594 event.height = rect->height;
595
596 event.count = 0;
597
598 XSendEvent(display, window, False, ExposureMask, (XEvent *)&event);
599
600 node = node->Next();
601
602 }
603
604 // Delete the update rects
605 node = updateRects.First();
606 while (node)
607 {
608 wxRect* rect = (wxRect*) node->Data();
609 delete rect;
610 node = node->Next();
611 }
612 #endif
613 }
614
615 // ---------------------------------------------------------------------------
616 // drag and drop
617 // ---------------------------------------------------------------------------
618
619 #if wxUSE_DRAG_AND_DROP
620
621 void wxWindowX11::SetDropTarget(wxDropTarget * WXUNUSED(pDropTarget))
622 {
623 // TODO
624 }
625
626 #endif
627
628 // Old style file-manager drag&drop
629 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept))
630 {
631 // TODO
632 }
633
634 // ----------------------------------------------------------------------------
635 // tooltips
636 // ----------------------------------------------------------------------------
637
638 #if wxUSE_TOOLTIPS
639
640 void wxWindowX11::DoSetToolTip(wxToolTip * WXUNUSED(tooltip))
641 {
642 // TODO
643 }
644
645 #endif // wxUSE_TOOLTIPS
646
647 // ---------------------------------------------------------------------------
648 // moving and resizing
649 // ---------------------------------------------------------------------------
650
651 bool wxWindowX11::PreResize()
652 {
653 return TRUE;
654 }
655
656 // Get total size
657 void wxWindowX11::DoGetSize(int *x, int *y) const
658 {
659 Window window = (Window) m_mainWidget;
660 if (window)
661 {
662 XWindowAttributes attr;
663 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
664 wxASSERT(status);
665
666 if (status)
667 {
668 *x = attr.width /* + 2*m_borderSize */ ;
669 *y = attr.height /* + 2*m_borderSize */ ;
670 }
671 }
672 }
673
674 void wxWindowX11::DoGetPosition(int *x, int *y) const
675 {
676 Window window = (Window) m_mainWidget;
677 if (window)
678 {
679 XWindowAttributes attr;
680 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
681 wxASSERT(status);
682
683 if (status)
684 {
685 *x = attr.x;
686 *y = attr.y;
687
688 // We may be faking the client origin. So a window that's really at (0, 30)
689 // may appear (to wxWin apps) to be at (0, 0).
690 if (GetParent())
691 {
692 wxPoint pt(GetParent()->GetClientAreaOrigin());
693 *x -= pt.x;
694 *y -= pt.y;
695 }
696 }
697 }
698 }
699
700 void wxWindowX11::DoScreenToClient(int *x, int *y) const
701 {
702 Display *display = wxGlobalDisplay();
703 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
704 Window thisWindow = (Window) m_mainWidget;
705
706 Window childWindow;
707 int xx = *x;
708 int yy = *y;
709 XTranslateCoordinates(display, rootWindow, thisWindow, xx, yy, x, y, &childWindow);
710 }
711
712 void wxWindowX11::DoClientToScreen(int *x, int *y) const
713 {
714 Display *display = wxGlobalDisplay();
715 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
716 Window thisWindow = (Window) m_mainWidget;
717
718 Window childWindow;
719 int xx = *x;
720 int yy = *y;
721 XTranslateCoordinates(display, thisWindow, rootWindow, xx, yy, x, y, &childWindow);
722 }
723
724
725 // Get size *available for subwindows* i.e. excluding menu bar etc.
726 void wxWindowX11::DoGetClientSize(int *x, int *y) const
727 {
728 Window window = (Window) m_mainWidget;
729
730 if (window)
731 {
732 XWindowAttributes attr;
733 Status status = XGetWindowAttributes( wxGlobalDisplay(), window, &attr );
734 wxASSERT(status);
735
736 if (status)
737 {
738 *x = attr.width ;
739 *y = attr.height ;
740 }
741 }
742 }
743
744 void wxWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
745 {
746 if (!GetMainWindow())
747 return;
748
749 XWindowChanges windowChanges;
750 int valueMask = 0;
751
752 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
753 {
754 int yy = 0;
755 AdjustForParentClientOrigin( x, yy, sizeFlags);
756 windowChanges.x = x;
757 valueMask |= CWX;
758 }
759 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
760 {
761 int xx = 0;
762 AdjustForParentClientOrigin( xx, y, sizeFlags);
763 windowChanges.y = y;
764 valueMask |= CWY;
765 }
766 if (width != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
767 {
768 windowChanges.width = width /* - m_borderSize*2 */;
769 valueMask |= CWWidth;
770 }
771 if (height != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
772 {
773 windowChanges.height = height /* -m_borderSize*2*/;
774 valueMask |= CWHeight;
775 }
776
777 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
778 valueMask, & windowChanges);
779 }
780
781 void wxWindowX11::DoSetClientSize(int width, int height)
782 {
783 if (!GetMainWindow())
784 return;
785
786 XWindowChanges windowChanges;
787 int valueMask = 0;
788
789 if (width != -1)
790 {
791 windowChanges.width = width ;
792 valueMask |= CWWidth;
793 }
794 if (height != -1)
795 {
796 windowChanges.height = height ;
797 valueMask |= CWHeight;
798 }
799 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
800 valueMask, & windowChanges);
801 }
802
803 // For implementation purposes - sometimes decorations make the client area
804 // smaller
805 wxPoint wxWindowX11::GetClientAreaOrigin() const
806 {
807 return wxPoint(0, 0);
808 }
809
810 // Makes an adjustment to the window position (for example, a frame that has
811 // a toolbar that it manages itself).
812 void wxWindowX11::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags)
813 {
814 if (((sizeFlags & wxSIZE_NO_ADJUSTMENTS) == 0) && GetParent())
815 {
816 wxPoint pt(GetParent()->GetClientAreaOrigin());
817 x += pt.x; y += pt.y;
818 }
819 }
820
821 void wxWindowX11::SetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH)
822 {
823 m_minWidth = minW;
824 m_minHeight = minH;
825 m_maxWidth = maxW;
826 m_maxHeight = maxH;
827
828 XSizeHints sizeHints;
829 sizeHints.flags = 0;
830
831 if (minW > -1 && minH > -1)
832 {
833 sizeHints.flags |= PMinSize;
834 sizeHints.min_width = minW;
835 sizeHints.min_height = minH;
836 }
837 if (maxW > -1 && maxH > -1)
838 {
839 sizeHints.flags |= PMaxSize;
840 sizeHints.max_width = maxW;
841 sizeHints.max_height = maxH;
842 }
843 if (incW > -1 && incH > -1)
844 {
845 sizeHints.flags |= PResizeInc;
846 sizeHints.width_inc = incW;
847 sizeHints.height_inc = incH;
848 }
849
850 XSetWMNormalHints(wxGlobalDisplay(), (Window) GetMainWindow(), & sizeHints);
851 }
852
853 void wxWindowX11::DoMoveWindow(int x, int y, int width, int height)
854 {
855 DoSetSize(x, y, width, height);
856 }
857
858 // ---------------------------------------------------------------------------
859 // text metrics
860 // ---------------------------------------------------------------------------
861
862 int wxWindowX11::GetCharHeight() const
863 {
864 wxCHECK_MSG( m_font.Ok(), 0, "valid window font needed" );
865
866 WXFontStructPtr pFontStruct = m_font.GetFontStruct(1.0, GetXDisplay());
867
868 int direction, ascent, descent;
869 XCharStruct overall;
870 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
871 &descent, &overall);
872
873 // return (overall.ascent + overall.descent);
874 return (ascent + descent);
875 }
876
877 int wxWindowX11::GetCharWidth() const
878 {
879 wxCHECK_MSG( m_font.Ok(), 0, "valid window font needed" );
880
881 WXFontStructPtr pFontStruct = m_font.GetFontStruct(1.0, GetXDisplay());
882
883 int direction, ascent, descent;
884 XCharStruct overall;
885 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
886 &descent, &overall);
887
888 return overall.width;
889 }
890
891 void wxWindowX11::GetTextExtent(const wxString& string,
892 int *x, int *y,
893 int *descent, int *externalLeading,
894 const wxFont *theFont) const
895 {
896 wxFont fontToUse = m_font;
897 if (theFont) fontToUse = *theFont;
898
899 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
900
901 WXFontStructPtr pFontStruct = fontToUse.GetFontStruct(1.0, GetXDisplay());
902
903 int direction, ascent, descent2;
904 XCharStruct overall;
905 int slen = string.Len();
906
907 #if 0
908 if (use16)
909 XTextExtents16((XFontStruct*) pFontStruct, (XChar2b *) (char*) (const char*) string, slen, &direction,
910 &ascent, &descent2, &overall);
911 #endif
912
913 XTextExtents((XFontStruct*) pFontStruct, string.c_str(), slen,
914 &direction, &ascent, &descent2, &overall);
915
916 if ( x )
917 *x = (overall.width);
918 if ( y )
919 *y = (ascent + descent2);
920 if (descent)
921 *descent = descent2;
922 if (externalLeading)
923 *externalLeading = 0;
924
925 }
926
927 // ----------------------------------------------------------------------------
928 // painting
929 // ----------------------------------------------------------------------------
930
931 void wxWindowX11::Refresh(bool eraseBack, const wxRect *rect)
932 {
933 if (eraseBack)
934 {
935 if (rect)
936 {
937 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
938 m_clearRegion.Union( rect->x, rect->y, rect->width, rect->height );
939 }
940 else
941 {
942 int height,width;
943 GetSize( &width, &height );
944
945 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
946 m_clearRegion.Clear();
947 m_clearRegion.Union( 0, 0, width, height );
948 }
949 }
950
951 if (rect)
952 {
953 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
954 m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
955 }
956 else
957 {
958 int height,width;
959 GetSize( &width, &height );
960
961 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
962 m_updateRegion.Clear();
963 m_updateRegion.Union( 0, 0, width, height );
964 }
965 }
966
967 void wxWindowX11::Update()
968 {
969 if (!m_updateRegion.IsEmpty())
970 {
971 X11SendPaintEvents();
972 }
973 }
974
975 void wxWindowX11::Clear()
976 {
977 wxClientDC dc((wxWindow*) this);
978 wxBrush brush(GetBackgroundColour(), wxSOLID);
979 dc.SetBackground(brush);
980 dc.Clear();
981 }
982
983 void wxWindowX11::X11SendPaintEvents()
984 {
985 m_clipPaintRegion = TRUE;
986
987 // if (!m_clearRegion.IsEmpty())
988 {
989 wxWindowDC dc( (wxWindow*)this );
990 dc.SetClippingRegion( m_clearRegion );
991
992 wxEraseEvent erase_event( GetId(), &dc );
993 erase_event.SetEventObject( this );
994
995 if (!GetEventHandler()->ProcessEvent(erase_event))
996 {
997 wxRegionIterator upd( m_clearRegion );
998 while (upd)
999 {
1000 XClearArea( wxGlobalDisplay(), (Window) m_mainWidget,
1001 upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight(), False );
1002 upd ++;
1003 }
1004 }
1005 m_clearRegion.Clear();
1006 }
1007
1008 wxNcPaintEvent nc_paint_event( GetId() );
1009 nc_paint_event.SetEventObject( this );
1010 GetEventHandler()->ProcessEvent( nc_paint_event );
1011
1012 wxPaintEvent paint_event( GetId() );
1013 paint_event.SetEventObject( this );
1014 GetEventHandler()->ProcessEvent( paint_event );
1015
1016 m_updateRegion.Clear();
1017
1018 m_clipPaintRegion = FALSE;
1019 }
1020
1021 // ----------------------------------------------------------------------------
1022 // event handlers
1023 // ----------------------------------------------------------------------------
1024
1025 // Responds to colour changes: passes event on to children.
1026 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent& event)
1027 {
1028 wxWindowList::Node *node = GetChildren().GetFirst();
1029 while ( node )
1030 {
1031 // Only propagate to non-top-level windows
1032 wxWindow *win = node->GetData();
1033 if ( win->GetParent() )
1034 {
1035 wxSysColourChangedEvent event2;
1036 event.m_eventObject = win;
1037 win->GetEventHandler()->ProcessEvent(event2);
1038 }
1039
1040 node = node->GetNext();
1041 }
1042 }
1043
1044 void wxWindowX11::OnInternalIdle()
1045 {
1046 // Update invalidated regions.
1047 Update();
1048
1049 // This calls the UI-update mechanism (querying windows for
1050 // menu/toolbar/control state information)
1051 UpdateWindowUI();
1052 }
1053
1054 // ----------------------------------------------------------------------------
1055 // function which maintain the global hash table mapping Widgets to wxWindows
1056 // ----------------------------------------------------------------------------
1057
1058 bool wxAddWindowToTable(Window w, wxWindow *win)
1059 {
1060 wxWindow *oldItem = NULL;
1061 if ((oldItem = (wxWindow *)wxWidgetHashTable->Get ((long) w)))
1062 {
1063 wxLogDebug("Widget table clash: new widget is %ld, %s",
1064 (long)w, win->GetClassInfo()->GetClassName());
1065 return FALSE;
1066 }
1067
1068 wxWidgetHashTable->Put((long) w, win);
1069
1070 wxLogTrace("widget", "XWindow 0x%08x <-> window %p (%s)",
1071 w, win, win->GetClassInfo()->GetClassName());
1072
1073 return TRUE;
1074 }
1075
1076 wxWindow *wxGetWindowFromTable(Window w)
1077 {
1078 return (wxWindow *)wxWidgetHashTable->Get((long) w);
1079 }
1080
1081 void wxDeleteWindowFromTable(Window w)
1082 {
1083 wxWidgetHashTable->Delete((long)w);
1084 }
1085
1086 // ----------------------------------------------------------------------------
1087 // add/remove window from the table
1088 // ----------------------------------------------------------------------------
1089
1090 // ----------------------------------------------------------------------------
1091 // X11-specific accessors
1092 // ----------------------------------------------------------------------------
1093
1094 // Get the underlying X window
1095 WXWindow wxWindowX11::GetXWindow() const
1096 {
1097 return GetMainWindow();
1098 }
1099
1100 // Get the underlying X display
1101 WXDisplay *wxWindowX11::GetXDisplay() const
1102 {
1103 return wxGetDisplay();
1104 }
1105
1106 WXWindow wxWindowX11::GetMainWindow() const
1107 {
1108 return m_mainWidget;
1109 }
1110
1111 // ----------------------------------------------------------------------------
1112 // TranslateXXXEvent() functions
1113 // ----------------------------------------------------------------------------
1114
1115 bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, Window window, XEvent *xevent)
1116 {
1117 switch (xevent->xany.type)
1118 {
1119 case EnterNotify:
1120 case LeaveNotify:
1121 case ButtonPress:
1122 case ButtonRelease:
1123 case MotionNotify:
1124 {
1125 wxEventType eventType = wxEVT_NULL;
1126
1127 if (xevent->xany.type == EnterNotify)
1128 {
1129 //if (local_event.xcrossing.mode!=NotifyNormal)
1130 // return ; // Ignore grab events
1131 eventType = wxEVT_ENTER_WINDOW;
1132 // canvas->GetEventHandler()->OnSetFocus();
1133 }
1134 else if (xevent->xany.type == LeaveNotify)
1135 {
1136 //if (local_event.xcrossingr.mode!=NotifyNormal)
1137 // return ; // Ignore grab events
1138 eventType = wxEVT_LEAVE_WINDOW;
1139 // canvas->GetEventHandler()->OnKillFocus();
1140 }
1141 else if (xevent->xany.type == MotionNotify)
1142 {
1143 eventType = wxEVT_MOTION;
1144 }
1145 else if (xevent->xany.type == ButtonPress)
1146 {
1147 wxevent.SetTimestamp(xevent->xbutton.time);
1148 int button = 0;
1149 if (xevent->xbutton.button == Button1)
1150 {
1151 eventType = wxEVT_LEFT_DOWN;
1152 button = 1;
1153 }
1154 else if (xevent->xbutton.button == Button2)
1155 {
1156 eventType = wxEVT_MIDDLE_DOWN;
1157 button = 2;
1158 }
1159 else if (xevent->xbutton.button == Button3)
1160 {
1161 eventType = wxEVT_RIGHT_DOWN;
1162 button = 3;
1163 }
1164
1165 // check for a double click
1166 // TODO: where can we get this value from?
1167 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1168 long dclickTime = 200;
1169 long ts = wxevent.GetTimestamp();
1170
1171 int buttonLast = win->GetLastClickedButton();
1172 long lastTS = win->GetLastClickTime();
1173 if ( buttonLast && buttonLast == button && (ts - lastTS) < dclickTime )
1174 {
1175 // I have a dclick
1176 win->SetLastClick(0, ts);
1177 if ( eventType == wxEVT_LEFT_DOWN )
1178 eventType = wxEVT_LEFT_DCLICK;
1179 else if ( eventType == wxEVT_MIDDLE_DOWN )
1180 eventType = wxEVT_MIDDLE_DCLICK;
1181 else if ( eventType == wxEVT_RIGHT_DOWN )
1182 eventType = wxEVT_RIGHT_DCLICK;
1183 }
1184 else
1185 {
1186 // not fast enough or different button
1187 win->SetLastClick(button, ts);
1188 }
1189 }
1190 else if (xevent->xany.type == ButtonRelease)
1191 {
1192 if (xevent->xbutton.button == Button1)
1193 {
1194 eventType = wxEVT_LEFT_UP;
1195 }
1196 else if (xevent->xbutton.button == Button2)
1197 {
1198 eventType = wxEVT_MIDDLE_UP;
1199 }
1200 else if (xevent->xbutton.button == Button3)
1201 {
1202 eventType = wxEVT_RIGHT_UP;
1203 }
1204 else return FALSE;
1205 }
1206 else
1207 {
1208 return FALSE;
1209 }
1210
1211 wxevent.SetEventType(eventType);
1212
1213 wxevent.m_x = xevent->xbutton.x;
1214 wxevent.m_y = xevent->xbutton.y;
1215
1216 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
1217 || (event_left_is_down (xevent)
1218 && (eventType != wxEVT_LEFT_UP)));
1219 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
1220 || (event_middle_is_down (xevent)
1221 && (eventType != wxEVT_MIDDLE_UP)));
1222 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
1223 || (event_right_is_down (xevent)
1224 && (eventType != wxEVT_RIGHT_UP)));
1225
1226 wxevent.m_shiftDown = xevent->xbutton.state & ShiftMask;
1227 wxevent.m_controlDown = xevent->xbutton.state & ControlMask;
1228 wxevent.m_altDown = xevent->xbutton.state & Mod3Mask;
1229 wxevent.m_metaDown = xevent->xbutton.state & Mod1Mask;
1230
1231 wxevent.SetId(win->GetId());
1232 wxevent.SetEventObject(win);
1233
1234 return TRUE;
1235 }
1236 }
1237 return FALSE;
1238 }
1239
1240 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Window WXUNUSED(win), XEvent *xevent)
1241 {
1242 switch (xevent->xany.type)
1243 {
1244 case KeyPress:
1245 case KeyRelease:
1246 {
1247 char buf[20];
1248
1249 KeySym keySym;
1250 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
1251 int id = wxCharCodeXToWX (keySym);
1252
1253 if (xevent->xkey.state & ShiftMask)
1254 wxevent.m_shiftDown = TRUE;
1255 if (xevent->xkey.state & ControlMask)
1256 wxevent.m_controlDown = TRUE;
1257 if (xevent->xkey.state & Mod3Mask)
1258 wxevent.m_altDown = TRUE;
1259 if (xevent->xkey.state & Mod1Mask)
1260 wxevent.m_metaDown = TRUE;
1261 wxevent.SetEventObject(win);
1262 wxevent.m_keyCode = id;
1263 wxevent.SetTimestamp(xevent->xkey.time);
1264
1265 wxevent.m_x = xevent->xbutton.x;
1266 wxevent.m_y = xevent->xbutton.y;
1267
1268 if (id > -1)
1269 return TRUE;
1270 else
1271 return FALSE;
1272 break;
1273 }
1274 default:
1275 break;
1276 }
1277 return FALSE;
1278 }
1279
1280 // ----------------------------------------------------------------------------
1281 // Colour stuff
1282 // ----------------------------------------------------------------------------
1283
1284 #if 0
1285
1286 #define YAllocColor XAllocColor
1287 XColor g_itemColors[5];
1288 int wxComputeColours (Display *display, wxColour * back, wxColour * fore)
1289 {
1290 int result;
1291 static XmColorProc colorProc;
1292
1293 result = wxNO_COLORS;
1294
1295 if (back)
1296 {
1297 g_itemColors[0].red = (((long) back->Red ()) << 8);
1298 g_itemColors[0].green = (((long) back->Green ()) << 8);
1299 g_itemColors[0].blue = (((long) back->Blue ()) << 8);
1300 g_itemColors[0].flags = DoRed | DoGreen | DoBlue;
1301 if (colorProc == (XmColorProc) NULL)
1302 {
1303 // Get a ptr to the actual function
1304 colorProc = XmSetColorCalculation ((XmColorProc) NULL);
1305 // And set it back to motif.
1306 XmSetColorCalculation (colorProc);
1307 }
1308 (*colorProc) (&g_itemColors[wxBACK_INDEX],
1309 &g_itemColors[wxFORE_INDEX],
1310 &g_itemColors[wxSELE_INDEX],
1311 &g_itemColors[wxTOPS_INDEX],
1312 &g_itemColors[wxBOTS_INDEX]);
1313 result = wxBACK_COLORS;
1314 }
1315 if (fore)
1316 {
1317 g_itemColors[wxFORE_INDEX].red = (((long) fore->Red ()) << 8);
1318 g_itemColors[wxFORE_INDEX].green = (((long) fore->Green ()) << 8);
1319 g_itemColors[wxFORE_INDEX].blue = (((long) fore->Blue ()) << 8);
1320 g_itemColors[wxFORE_INDEX].flags = DoRed | DoGreen | DoBlue;
1321 if (result == wxNO_COLORS)
1322 result = wxFORE_COLORS;
1323 }
1324
1325 Display *dpy = display;
1326 Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy);
1327
1328 if (back)
1329 {
1330 /* 5 Colours to allocate */
1331 for (int i = 0; i < 5; i++)
1332 if (!YAllocColor (dpy, cmap, &g_itemColors[i]))
1333 result = wxNO_COLORS;
1334 }
1335 else if (fore)
1336 {
1337 /* Only 1 colour to allocate */
1338 if (!YAllocColor (dpy, cmap, &g_itemColors[wxFORE_INDEX]))
1339 result = wxNO_COLORS;
1340 }
1341
1342 return (result);
1343
1344 }
1345 #endif
1346
1347 bool wxWindowX11::SetBackgroundColour(const wxColour& col)
1348 {
1349 wxWindowBase::SetBackgroundColour(col);
1350
1351 if (!GetMainWindow())
1352 return FALSE;
1353
1354 Display *xdisplay = (Display*) wxGlobalDisplay();
1355 int xscreen = DefaultScreen( xdisplay );
1356 Colormap cm = DefaultColormap( xdisplay, xscreen );
1357
1358 wxColour colour( col );
1359 colour.CalcPixel( (WXColormap) cm );
1360
1361 XSetWindowAttributes attrib;
1362 attrib.background_pixel = colour.GetPixel();
1363
1364 XChangeWindowAttributes(wxGlobalDisplay(),
1365 (Window) GetMainWindow(),
1366 CWBackPixel,
1367 & attrib);
1368
1369 return TRUE;
1370 }
1371
1372 bool wxWindowX11::SetForegroundColour(const wxColour& col)
1373 {
1374 if ( !wxWindowBase::SetForegroundColour(col) )
1375 return FALSE;
1376
1377 return TRUE;
1378 }
1379
1380 // ----------------------------------------------------------------------------
1381 // global functions
1382 // ----------------------------------------------------------------------------
1383
1384 wxWindow *wxGetActiveWindow()
1385 {
1386 // TODO
1387 wxFAIL_MSG("Not implemented");
1388 return NULL;
1389 }
1390
1391 /* static */
1392 wxWindow *wxWindowBase::GetCapture()
1393 {
1394 return (wxWindow *)g_captureWindow;
1395 }
1396
1397
1398 // Find the wxWindow at the current mouse position, returning the mouse
1399 // position.
1400 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1401 {
1402 return wxFindWindowAtPoint(wxGetMousePosition());
1403 }
1404
1405 // Get the current mouse position.
1406 wxPoint wxGetMousePosition()
1407 {
1408 Display *display = wxGlobalDisplay();
1409 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
1410 Window rootReturn, childReturn;
1411 int rootX, rootY, winX, winY;
1412 unsigned int maskReturn;
1413
1414 XQueryPointer (display,
1415 rootWindow,
1416 &rootReturn,
1417 &childReturn,
1418 &rootX, &rootY, &winX, &winY, &maskReturn);
1419 return wxPoint(rootX, rootY);
1420 }
1421
1422
1423 // ----------------------------------------------------------------------------
1424 // wxNoOptimize: switch off size optimization
1425 // ----------------------------------------------------------------------------
1426
1427 int wxNoOptimize::ms_count = 0;
1428