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