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