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