]> git.saurik.com Git - wxWidgets.git/blob - src/x11/window.cpp
pane buttons are now vertically centered
[wxWidgets.git] / src / x11 / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/window.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 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 // ============================================================================
20 // declarations
21 // ============================================================================
22
23 // ----------------------------------------------------------------------------
24 // headers
25 // ----------------------------------------------------------------------------
26
27 #include "wx/window.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/hash.h"
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/utils.h"
34 #include "wx/panel.h"
35 #include "wx/frame.h"
36 #include "wx/dc.h"
37 #include "wx/dcclient.h"
38 #include "wx/button.h"
39 #include "wx/menu.h"
40 #include "wx/dialog.h"
41 #include "wx/timer.h"
42 #include "wx/settings.h"
43 #include "wx/msgdlg.h"
44 #include "wx/scrolbar.h"
45 #include "wx/listbox.h"
46 #include "wx/scrolwin.h"
47 #include "wx/layout.h"
48 #include "wx/menuitem.h"
49 #include "wx/module.h"
50 #endif
51
52 #include "wx/fontutil.h"
53 #include "wx/univ/renderer.h"
54
55 #if wxUSE_DRAG_AND_DROP
56 #include "wx/dnd.h"
57 #endif
58
59 #include "wx/x11/private.h"
60 #include "X11/Xutil.h"
61
62 #include <string.h>
63
64 // ----------------------------------------------------------------------------
65 // global variables for this module
66 // ----------------------------------------------------------------------------
67
68 static wxWindow* g_captureWindow = NULL;
69 static GC g_eraseGC;
70
71 // ----------------------------------------------------------------------------
72 // macros
73 // ----------------------------------------------------------------------------
74
75 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
76 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
77 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
78
79 // ----------------------------------------------------------------------------
80 // event tables
81 // ----------------------------------------------------------------------------
82
83 IMPLEMENT_ABSTRACT_CLASS(wxWindowX11, wxWindowBase)
84
85 BEGIN_EVENT_TABLE(wxWindowX11, wxWindowBase)
86 EVT_SYS_COLOUR_CHANGED(wxWindowX11::OnSysColourChanged)
87 END_EVENT_TABLE()
88
89 // ============================================================================
90 // implementation
91 // ============================================================================
92
93 // ----------------------------------------------------------------------------
94 // helper functions
95 // ----------------------------------------------------------------------------
96
97 // ----------------------------------------------------------------------------
98 // constructors
99 // ----------------------------------------------------------------------------
100
101 void wxWindowX11::Init()
102 {
103 // X11-specific
104 m_mainWindow = (WXWindow) 0;
105 m_clientWindow = (WXWindow) 0;
106 m_insertIntoMain = false;
107 m_updateNcArea = false;
108
109 m_winCaptured = false;
110 m_needsInputFocus = false;
111 m_isShown = true;
112 m_lastTS = 0;
113 m_lastButton = 0;
114 }
115
116 // real construction (Init() must have been called before!)
117 bool wxWindowX11::Create(wxWindow *parent, wxWindowID id,
118 const wxPoint& pos,
119 const wxSize& size,
120 long style,
121 const wxString& name)
122 {
123 wxCHECK_MSG( parent, false, wxT("can't create wxWindow without parent") );
124
125 CreateBase(parent, id, pos, size, style, wxDefaultValidator, name);
126
127 parent->AddChild(this);
128
129 Display *xdisplay = (Display*) wxGlobalDisplay();
130 int xscreen = DefaultScreen( xdisplay );
131 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
132 Colormap cm = DefaultColormap( xdisplay, xscreen );
133
134 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
135 m_backgroundColour.CalcPixel( (WXColormap) cm );
136
137 m_foregroundColour = *wxBLACK;
138 m_foregroundColour.CalcPixel( (WXColormap) cm );
139
140 Window xparent = (Window) parent->GetClientAreaWindow();
141
142 // Add window's own scrollbars to main window, not to client window
143 if (parent->GetInsertIntoMain())
144 {
145 // wxLogDebug( "Inserted into main: %s", GetName().c_str() );
146 xparent = (Window) parent->GetMainWindow();
147 }
148
149 // Size (not including the border) must be nonzero (or a Value error results)!
150 // Note: The Xlib manual doesn't mention this restriction of XCreateWindow.
151 wxSize size2(size);
152 if (size2.x <= 0)
153 size2.x = 20;
154 if (size2.y <= 0)
155 size2.y = 20;
156
157 wxPoint pos2(pos);
158 if (pos2.x == wxDefaultCoord)
159 pos2.x = 0;
160 if (pos2.y == wxDefaultCoord)
161 pos2.y = 0;
162
163 #if wxUSE_TWO_WINDOWS
164 bool need_two_windows =
165 ((( wxSUNKEN_BORDER | wxRAISED_BORDER | wxSIMPLE_BORDER | wxHSCROLL | wxVSCROLL ) & m_windowStyle) != 0);
166 #else
167 bool need_two_windows = false;
168 #endif
169
170 #if wxUSE_NANOX
171 long xattributes = 0;
172 #else
173 XSetWindowAttributes xattributes;
174 long xattributes_mask = 0;
175
176 xattributes_mask |= CWBackPixel;
177 xattributes.background_pixel = m_backgroundColour.GetPixel();
178
179 xattributes_mask |= CWBorderPixel;
180 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
181
182 xattributes_mask |= CWEventMask;
183 #endif
184
185 if (need_two_windows)
186 {
187 #if wxUSE_NANOX
188 long backColor, foreColor;
189 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
190 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
191
192 Window xwindow = XCreateWindowWithColor( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
193 0, 0, InputOutput, xvisual, backColor, foreColor);
194 XSelectInput( xdisplay, xwindow,
195 GR_EVENT_MASK_CLOSE_REQ | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
196 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
197 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
198 PropertyChangeMask );
199
200 #else
201 // Normal X11
202 xattributes.event_mask =
203 ExposureMask | StructureNotifyMask | ColormapChangeMask;
204
205 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
206 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
207
208 #endif
209
210 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
211
212 m_mainWindow = (WXWindow) xwindow;
213 wxAddWindowToTable( xwindow, (wxWindow*) this );
214
215 XMapWindow( xdisplay, xwindow );
216
217 #if !wxUSE_NANOX
218 xattributes.event_mask =
219 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
220 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
221 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
222 PropertyChangeMask | VisibilityChangeMask ;
223
224 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE ))
225 {
226 xattributes_mask |= CWBitGravity;
227 xattributes.bit_gravity = StaticGravity;
228 }
229 #endif
230
231 if (HasFlag( wxSUNKEN_BORDER) || HasFlag( wxRAISED_BORDER))
232 {
233 pos2.x = 2;
234 pos2.y = 2;
235 size2.x -= 4;
236 size2.y -= 4;
237 }
238 else if (HasFlag( wxSIMPLE_BORDER ))
239 {
240 pos2.x = 1;
241 pos2.y = 1;
242 size2.x -= 2;
243 size2.y -= 2;
244 }
245 else
246 {
247 pos2.x = 0;
248 pos2.y = 0;
249 }
250
251 // Make again sure the size is nonzero.
252 if (size2.x <= 0)
253 size2.x = 1;
254 if (size2.y <= 0)
255 size2.y = 1;
256
257 #if wxUSE_NANOX
258 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
259 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
260
261 xwindow = XCreateWindowWithColor( xdisplay, xwindow, pos2.x, pos2.y, size2.x, size2.y,
262 0, 0, InputOutput, xvisual, backColor, foreColor);
263 XSelectInput( xdisplay, xwindow,
264 GR_EVENT_MASK_CLOSE_REQ | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
265 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
266 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
267 PropertyChangeMask );
268
269 #else
270 xwindow = XCreateWindow( xdisplay, xwindow, pos2.x, pos2.y, size2.x, size2.y,
271 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
272 #endif
273
274 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
275
276 m_clientWindow = (WXWindow) xwindow;
277 wxAddClientWindowToTable( xwindow, (wxWindow*) this );
278
279 XMapWindow( xdisplay, xwindow );
280 }
281 else
282 {
283 // wxLogDebug( "No two windows needed %s", GetName().c_str() );
284 #if wxUSE_NANOX
285 long backColor, foreColor;
286 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
287 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
288
289 Window xwindow = XCreateWindowWithColor( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
290 0, 0, InputOutput, xvisual, backColor, foreColor);
291 XSelectInput( xdisplay, xwindow,
292 GR_EVENT_MASK_CLOSE_REQ | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
293 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
294 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
295 PropertyChangeMask );
296
297 #else
298 xattributes.event_mask =
299 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
300 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
301 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
302 PropertyChangeMask | VisibilityChangeMask ;
303
304 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE ))
305 {
306 xattributes_mask |= CWBitGravity;
307 xattributes.bit_gravity = NorthWestGravity;
308 }
309
310 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
311 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
312 #endif
313
314 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
315
316 m_mainWindow = (WXWindow) xwindow;
317 m_clientWindow = m_mainWindow;
318 wxAddWindowToTable( xwindow, (wxWindow*) this );
319
320 XMapWindow( xdisplay, xwindow );
321 }
322
323 // Is a subwindow, so map immediately
324 m_isShown = true;
325
326 // Without this, the cursor may not be restored properly (e.g. in splitter
327 // sample).
328 SetCursor(*wxSTANDARD_CURSOR);
329 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
330
331 // Don't call this, it can have nasty repercussions for composite controls,
332 // for example
333 // SetSize(pos.x, pos.y, size.x, size.y);
334
335 return true;
336 }
337
338 // Destructor
339 wxWindowX11::~wxWindowX11()
340 {
341 SendDestroyEvent();
342
343 if (g_captureWindow == this)
344 g_captureWindow = NULL;
345
346 m_isBeingDeleted = true;
347
348 DestroyChildren();
349
350 if (m_clientWindow != m_mainWindow)
351 {
352 // Destroy the cleint window
353 Window xwindow = (Window) m_clientWindow;
354 wxDeleteClientWindowFromTable( xwindow );
355 XDestroyWindow( wxGlobalDisplay(), xwindow );
356 m_clientWindow = NULL;
357 }
358
359 // Destroy the window
360 Window xwindow = (Window) m_mainWindow;
361 wxDeleteWindowFromTable( xwindow );
362 XDestroyWindow( wxGlobalDisplay(), xwindow );
363 m_mainWindow = NULL;
364 }
365
366 // ---------------------------------------------------------------------------
367 // basic operations
368 // ---------------------------------------------------------------------------
369
370 void wxWindowX11::SetFocus()
371 {
372 Window xwindow = (Window) m_clientWindow;
373
374 wxCHECK_RET( xwindow, wxT("invalid window") );
375
376 // Don't assert; we might be trying to set the focus for a panel
377 // with only static controls, so the panel returns false from AcceptsFocus.
378 // The app should be not be expected to deal with this.
379 if (!AcceptsFocus())
380 return;
381
382 #if 0
383 if (GetName() == "scrollBar")
384 {
385 char *crash = NULL;
386 *crash = 0;
387 }
388 #endif
389
390 if (wxWindowIsVisible(xwindow))
391 {
392 wxLogTrace( _T("focus"), _T("wxWindowX11::SetFocus: %s"), GetClassInfo()->GetClassName());
393 // XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToParent, CurrentTime );
394 XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToNone, CurrentTime );
395 m_needsInputFocus = false;
396 }
397 else
398 {
399 m_needsInputFocus = true;
400 }
401 }
402
403 // Get the window with the focus
404 wxWindow *wxWindowBase::DoFindFocus()
405 {
406 Window xfocus = (Window) 0;
407 int revert = 0;
408
409 XGetInputFocus( wxGlobalDisplay(), &xfocus, &revert);
410 if (xfocus)
411 {
412 wxWindow *win = wxGetWindowFromTable( xfocus );
413 if (!win)
414 {
415 win = wxGetClientWindowFromTable( xfocus );
416 }
417
418 return win;
419 }
420
421 return NULL;
422 }
423
424 // Enabling/disabling handled by event loop, and not sending events
425 // if disabled.
426 bool wxWindowX11::Enable(bool enable)
427 {
428 if ( !wxWindowBase::Enable(enable) )
429 return false;
430
431 return true;
432 }
433
434 bool wxWindowX11::Show(bool show)
435 {
436 wxWindowBase::Show(show);
437
438 Window xwindow = (Window) m_mainWindow;
439 Display *xdisp = wxGlobalDisplay();
440 if (show)
441 {
442 // wxLogDebug( "Mapping window of type %s", GetName().c_str() );
443 XMapWindow(xdisp, xwindow);
444 }
445 else
446 {
447 // wxLogDebug( "Unmapping window of type %s", GetName().c_str() );
448 XUnmapWindow(xdisp, xwindow);
449 }
450
451 return true;
452 }
453
454 // Raise the window to the top of the Z order
455 void wxWindowX11::Raise()
456 {
457 if (m_mainWindow)
458 XRaiseWindow( wxGlobalDisplay(), (Window) m_mainWindow );
459 }
460
461 // Lower the window to the bottom of the Z order
462 void wxWindowX11::Lower()
463 {
464 if (m_mainWindow)
465 XLowerWindow( wxGlobalDisplay(), (Window) m_mainWindow );
466 }
467
468 void wxWindowX11::SetLabel(const wxString& WXUNUSED(label))
469 {
470 // TODO
471 }
472
473 wxString wxWindowX11::GetLabel() const
474 {
475 // TODO
476 return wxEmptyString;
477 }
478
479 void wxWindowX11::DoCaptureMouse()
480 {
481 if ((g_captureWindow != NULL) && (g_captureWindow != this))
482 {
483 wxFAIL_MSG(wxT("Trying to capture before mouse released."));
484
485 // Core dump now
486 int *tmp = NULL;
487 (*tmp) = 1;
488 return;
489 }
490
491 if (m_winCaptured)
492 return;
493
494 Window xwindow = (Window) m_clientWindow;
495
496 wxCHECK_RET( xwindow, wxT("invalid window") );
497
498 g_captureWindow = (wxWindow*) this;
499
500 if (xwindow)
501 {
502 int res = XGrabPointer(wxGlobalDisplay(), xwindow,
503 FALSE,
504 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
505 GrabModeAsync,
506 GrabModeAsync,
507 None,
508 None, /* cursor */ // TODO: This may need to be set to the cursor of this window
509 CurrentTime );
510
511 if (res != GrabSuccess)
512 {
513 wxString msg;
514 msg.Printf(wxT("Failed to grab pointer for window %s"), this->GetClassInfo()->GetClassName());
515 wxLogDebug(msg);
516 if (res == GrabNotViewable)
517 wxLogDebug( wxT("This is not a viewable window - perhaps not shown yet?") );
518
519 g_captureWindow = NULL;
520 return;
521 }
522
523 m_winCaptured = true;
524 }
525 }
526
527 void wxWindowX11::DoReleaseMouse()
528 {
529 g_captureWindow = NULL;
530
531 if ( !m_winCaptured )
532 return;
533
534 Window xwindow = (Window) m_clientWindow;
535
536 if (xwindow)
537 {
538 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
539 }
540
541 // wxLogDebug( "Ungrabbed pointer in %s", GetName().c_str() );
542
543 m_winCaptured = false;
544 }
545
546 bool wxWindowX11::SetFont(const wxFont& font)
547 {
548 if ( !wxWindowBase::SetFont(font) )
549 {
550 // nothing to do
551 return false;
552 }
553
554 return true;
555 }
556
557 bool wxWindowX11::SetCursor(const wxCursor& cursor)
558 {
559 if ( !wxWindowBase::SetCursor(cursor) )
560 {
561 // no change
562 return false;
563 }
564
565 Window xwindow = (Window) m_clientWindow;
566
567 wxCHECK_MSG( xwindow, false, wxT("invalid window") );
568
569 wxCursor cursorToUse;
570 if (m_cursor.Ok())
571 cursorToUse = m_cursor;
572 else
573 cursorToUse = *wxSTANDARD_CURSOR;
574
575 Cursor xcursor = (Cursor) cursorToUse.GetCursor();
576
577 XDefineCursor( wxGlobalDisplay(), xwindow, xcursor );
578
579 return true;
580 }
581
582 // Coordinates relative to the window
583 void wxWindowX11::WarpPointer (int x, int y)
584 {
585 Window xwindow = (Window) m_clientWindow;
586
587 wxCHECK_RET( xwindow, wxT("invalid window") );
588
589 XWarpPointer( wxGlobalDisplay(), None, xwindow, 0, 0, 0, 0, x, y);
590 }
591
592 // Does a physical scroll
593 void wxWindowX11::ScrollWindow(int dx, int dy, const wxRect *rect)
594 {
595 // No scrolling requested.
596 if ((dx == 0) && (dy == 0)) return;
597
598 if (!m_updateRegion.IsEmpty())
599 {
600 m_updateRegion.Offset( dx, dy );
601
602 int cw = 0;
603 int ch = 0;
604 GetSize( &cw, &ch ); // GetClientSize() ??
605 m_updateRegion.Intersect( 0, 0, cw, ch );
606 }
607
608 if (!m_clearRegion.IsEmpty())
609 {
610 m_clearRegion.Offset( dx, dy );
611
612 int cw = 0;
613 int ch = 0;
614 GetSize( &cw, &ch ); // GetClientSize() ??
615 m_clearRegion.Intersect( 0, 0, cw, ch );
616 }
617
618 Window xwindow = (Window) GetClientAreaWindow();
619
620 wxCHECK_RET( xwindow, wxT("invalid window") );
621
622 Display *xdisplay = wxGlobalDisplay();
623
624 GC xgc = XCreateGC( xdisplay, xwindow, 0, NULL );
625 XSetGraphicsExposures( xdisplay, xgc, True );
626
627 int s_x = 0;
628 int s_y = 0;
629 int cw;
630 int ch;
631 if (rect)
632 {
633 s_x = rect->x;
634 s_y = rect->y;
635
636 cw = rect->width;
637 ch = rect->height;
638 }
639 else
640 {
641 s_x = 0;
642 s_y = 0;
643 GetClientSize( &cw, &ch );
644 }
645
646 #if wxUSE_TWO_WINDOWS
647 wxPoint offset( 0,0 );
648 #else
649 wxPoint offset = GetClientAreaOrigin();
650 s_x += offset.x;
651 s_y += offset.y;
652 #endif
653
654 int w = cw - abs(dx);
655 int h = ch - abs(dy);
656
657 if ((h < 0) || (w < 0))
658 {
659 Refresh();
660 }
661 else
662 {
663 wxRect rect;
664 if (dx < 0) rect.x = cw+dx + offset.x; else rect.x = s_x;
665 if (dy < 0) rect.y = ch+dy + offset.y; else rect.y = s_y;
666 if (dy != 0) rect.width = cw; else rect.width = abs(dx);
667 if (dx != 0) rect.height = ch; else rect.height = abs(dy);
668
669 int d_x = s_x;
670 int d_y = s_y;
671
672 if (dx < 0) s_x += -dx;
673 if (dy < 0) s_y += -dy;
674 if (dx > 0) d_x = dx + offset.x;
675 if (dy > 0) d_y = dy + offset.y;
676
677 XCopyArea( xdisplay, xwindow, xwindow, xgc, s_x, s_y, w, h, d_x, d_y );
678
679 // 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 );
680
681 // wxLogDebug( "Update: %d %d %d %d", rect.x, rect.y, rect.width, rect.height );
682
683 m_updateRegion.Union( rect );
684 m_clearRegion.Union( rect );
685 }
686
687 XFreeGC( xdisplay, xgc );
688
689 // Move Clients, but not the scrollbars
690 // FIXME: There may be a better method to move a lot of Windows within X11
691 wxScrollBar *sbH = ((wxWindow *) this)->GetScrollbar( wxHORIZONTAL );
692 wxScrollBar *sbV = ((wxWindow *) this)->GetScrollbar( wxVERTICAL );
693 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
694 while ( node )
695 {
696 // Only propagate to non-top-level windows
697 wxWindow *win = node->GetData();
698 if ( win->GetParent() && win != sbH && win != sbV )
699 {
700 wxPoint pos = win->GetPosition();
701 // Add the delta to the old Position
702 pos.x += dx;
703 pos.y += dy;
704 win->SetPosition(pos);
705 }
706 node = node->GetNext();
707 }
708 }
709
710 // ---------------------------------------------------------------------------
711 // drag and drop
712 // ---------------------------------------------------------------------------
713
714 #if wxUSE_DRAG_AND_DROP
715
716 void wxWindowX11::SetDropTarget(wxDropTarget * WXUNUSED(pDropTarget))
717 {
718 // TODO
719 }
720
721 #endif
722
723 // Old style file-manager drag&drop
724 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept))
725 {
726 // TODO
727 }
728
729 // ----------------------------------------------------------------------------
730 // tooltips
731 // ----------------------------------------------------------------------------
732
733 #if wxUSE_TOOLTIPS
734
735 void wxWindowX11::DoSetToolTip(wxToolTip * WXUNUSED(tooltip))
736 {
737 // TODO
738 }
739
740 #endif // wxUSE_TOOLTIPS
741
742 // ---------------------------------------------------------------------------
743 // moving and resizing
744 // ---------------------------------------------------------------------------
745
746 bool wxWindowX11::PreResize()
747 {
748 return true;
749 }
750
751 // Get total size
752 void wxWindowX11::DoGetSize(int *x, int *y) const
753 {
754 Window xwindow = (Window) m_mainWindow;
755
756 wxCHECK_RET( xwindow, wxT("invalid window") );
757
758 //XSync(wxGlobalDisplay(), False);
759
760 XWindowAttributes attr;
761 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
762 wxASSERT(status);
763
764 if (status)
765 {
766 *x = attr.width /* + 2*m_borderSize */ ;
767 *y = attr.height /* + 2*m_borderSize */ ;
768 }
769 }
770
771 void wxWindowX11::DoGetPosition(int *x, int *y) const
772 {
773 Window window = (Window) m_mainWindow;
774 if (window)
775 {
776 //XSync(wxGlobalDisplay(), False);
777 XWindowAttributes attr;
778 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
779 wxASSERT(status);
780
781 if (status)
782 {
783 *x = attr.x;
784 *y = attr.y;
785
786 // We may be faking the client origin. So a window that's really at (0, 30)
787 // may appear (to wxWin apps) to be at (0, 0).
788 if (GetParent())
789 {
790 wxPoint pt(GetParent()->GetClientAreaOrigin());
791 *x -= pt.x;
792 *y -= pt.y;
793 }
794 }
795 }
796 }
797
798 void wxWindowX11::DoScreenToClient(int *x, int *y) const
799 {
800 Display *display = wxGlobalDisplay();
801 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
802 Window thisWindow = (Window) m_clientWindow;
803
804 Window childWindow;
805 int xx = *x;
806 int yy = *y;
807 XTranslateCoordinates(display, rootWindow, thisWindow, xx, yy, x, y, &childWindow);
808 }
809
810 void wxWindowX11::DoClientToScreen(int *x, int *y) const
811 {
812 Display *display = wxGlobalDisplay();
813 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
814 Window thisWindow = (Window) m_clientWindow;
815
816 Window childWindow;
817 int xx = *x;
818 int yy = *y;
819 XTranslateCoordinates(display, thisWindow, rootWindow, xx, yy, x, y, &childWindow);
820 }
821
822
823 // Get size *available for subwindows* i.e. excluding menu bar etc.
824 void wxWindowX11::DoGetClientSize(int *x, int *y) const
825 {
826 Window window = (Window) m_mainWindow;
827
828 if (window)
829 {
830 XWindowAttributes attr;
831 Status status = XGetWindowAttributes( wxGlobalDisplay(), window, &attr );
832 wxASSERT(status);
833
834 if (status)
835 {
836 *x = attr.width ;
837 *y = attr.height ;
838 }
839 }
840 }
841
842 void wxWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
843 {
844 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
845
846 Window xwindow = (Window) m_mainWindow;
847
848 wxCHECK_RET( xwindow, wxT("invalid window") );
849
850 XWindowAttributes attr;
851 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
852 wxCHECK_RET( status, wxT("invalid window attributes") );
853
854 int new_x = attr.x;
855 int new_y = attr.y;
856 int new_w = attr.width;
857 int new_h = attr.height;
858
859 if (x != wxDefaultCoord || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
860 {
861 int yy = 0;
862 AdjustForParentClientOrigin( x, yy, sizeFlags);
863 new_x = x;
864 }
865 if (y != wxDefaultCoord || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
866 {
867 int xx = 0;
868 AdjustForParentClientOrigin( xx, y, sizeFlags);
869 new_y = y;
870 }
871 if (width != wxDefaultCoord)
872 {
873 new_w = width;
874 if (new_w <= 0)
875 new_w = 20;
876 }
877 if (height != wxDefaultCoord)
878 {
879 new_h = height;
880 if (new_h <= 0)
881 new_h = 20;
882 }
883
884 DoMoveWindow( new_x, new_y, new_w, new_h );
885 }
886
887 void wxWindowX11::DoSetClientSize(int width, int height)
888 {
889 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
890
891 Window xwindow = (Window) m_mainWindow;
892
893 wxCHECK_RET( xwindow, wxT("invalid window") );
894
895 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
896
897 if (m_mainWindow != m_clientWindow)
898 {
899 xwindow = (Window) m_clientWindow;
900
901 wxWindow *window = (wxWindow*) this;
902 wxRenderer *renderer = window->GetRenderer();
903 if (renderer)
904 {
905 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
906 width -= border.x + border.width;
907 height -= border.y + border.height;
908 }
909
910 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
911 }
912 }
913
914 void wxWindowX11::DoMoveWindow(int x, int y, int width, int height)
915 {
916 Window xwindow = (Window) m_mainWindow;
917
918 wxCHECK_RET( xwindow, wxT("invalid window") );
919
920 #if !wxUSE_NANOX
921
922 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, width, height );
923 if (m_mainWindow != m_clientWindow)
924 {
925 xwindow = (Window) m_clientWindow;
926
927 wxWindow *window = (wxWindow*) this;
928 wxRenderer *renderer = window->GetRenderer();
929 if (renderer)
930 {
931 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
932 x = border.x;
933 y = border.y;
934 width -= border.x + border.width;
935 height -= border.y + border.height;
936 }
937 else
938 {
939 x = 0;
940 y = 0;
941 }
942
943 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
944 if (sb && sb->IsShown())
945 {
946 wxSize size = sb->GetSize();
947 height -= size.y;
948 }
949 sb = window->GetScrollbar( wxVERTICAL );
950 if (sb && sb->IsShown())
951 {
952 wxSize size = sb->GetSize();
953 width -= size.x;
954 }
955
956 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, wxMax(1, width), wxMax(1, height) );
957 }
958
959 #else
960
961 XWindowChanges windowChanges;
962 windowChanges.x = x;
963 windowChanges.y = y;
964 windowChanges.width = width;
965 windowChanges.height = height;
966 windowChanges.stack_mode = 0;
967 int valueMask = CWX | CWY | CWWidth | CWHeight;
968
969 XConfigureWindow( wxGlobalDisplay(), xwindow, valueMask, &windowChanges );
970
971 #endif
972 }
973
974 void wxWindowX11::DoSetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH)
975 {
976 m_minWidth = minW;
977 m_minHeight = minH;
978 m_maxWidth = maxW;
979 m_maxHeight = maxH;
980
981 #if !wxUSE_NANOX
982 XSizeHints sizeHints;
983 sizeHints.flags = 0;
984
985 if (minW > -1 && minH > -1)
986 {
987 sizeHints.flags |= PMinSize;
988 sizeHints.min_width = minW;
989 sizeHints.min_height = minH;
990 }
991 if (maxW > -1 && maxH > -1)
992 {
993 sizeHints.flags |= PMaxSize;
994 sizeHints.max_width = maxW;
995 sizeHints.max_height = maxH;
996 }
997 if (incW > -1 && incH > -1)
998 {
999 sizeHints.flags |= PResizeInc;
1000 sizeHints.width_inc = incW;
1001 sizeHints.height_inc = incH;
1002 }
1003
1004 XSetWMNormalHints(wxGlobalDisplay(), (Window) m_mainWindow, &sizeHints );
1005 #endif
1006 }
1007
1008 // ---------------------------------------------------------------------------
1009 // text metrics
1010 // ---------------------------------------------------------------------------
1011
1012 int wxWindowX11::GetCharHeight() const
1013 {
1014 wxFont font(GetFont());
1015 wxCHECK_MSG( font.Ok(), 0, wxT("valid window font needed") );
1016
1017 #if wxUSE_UNICODE
1018 // There should be an easier way.
1019 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1020 pango_layout_set_font_description( layout, font.GetNativeFontInfo()->description );
1021 pango_layout_set_text(layout, "H", 1 );
1022 int w,h;
1023 pango_layout_get_pixel_size(layout, &w, &h);
1024 g_object_unref( G_OBJECT( layout ) );
1025
1026 return h;
1027 #else
1028 WXFontStructPtr pFontStruct = font.GetFontStruct(1.0, wxGlobalDisplay());
1029
1030 int direction, ascent, descent;
1031 XCharStruct overall;
1032 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1033 &descent, &overall);
1034
1035 // return (overall.ascent + overall.descent);
1036 return (ascent + descent);
1037 #endif
1038 }
1039
1040 int wxWindowX11::GetCharWidth() const
1041 {
1042 wxFont font(GetFont());
1043 wxCHECK_MSG( font.Ok(), 0, wxT("valid window font needed") );
1044
1045 #if wxUSE_UNICODE
1046 // There should be an easier way.
1047 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1048 pango_layout_set_font_description( layout, font.GetNativeFontInfo()->description );
1049 pango_layout_set_text(layout, "H", 1 );
1050 int w,h;
1051 pango_layout_get_pixel_size(layout, &w, &h);
1052 g_object_unref( G_OBJECT( layout ) );
1053
1054 return w;
1055 #else
1056 WXFontStructPtr pFontStruct = font.GetFontStruct(1.0, wxGlobalDisplay());
1057
1058 int direction, ascent, descent;
1059 XCharStruct overall;
1060 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1061 &descent, &overall);
1062
1063 return overall.width;
1064 #endif
1065 }
1066
1067 void wxWindowX11::GetTextExtent(const wxString& string,
1068 int *x, int *y,
1069 int *descent, int *externalLeading,
1070 const wxFont *theFont) const
1071 {
1072 wxFont fontToUse = GetFont();
1073 if (theFont) fontToUse = *theFont;
1074
1075 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
1076
1077 if (string.empty())
1078 {
1079 if (x) (*x) = 0;
1080 if (y) (*y) = 0;
1081 return;
1082 }
1083
1084 #if wxUSE_UNICODE
1085 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1086
1087 PangoFontDescription *desc = fontToUse.GetNativeFontInfo()->description;
1088 pango_layout_set_font_description(layout, desc);
1089
1090 const wxCharBuffer data = wxConvUTF8.cWC2MB( string );
1091 pango_layout_set_text(layout, (const char*) data, strlen( (const char*) data ));
1092
1093 PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data;
1094
1095
1096 PangoRectangle rect;
1097 pango_layout_line_get_extents(line, NULL, &rect);
1098
1099 if (x) (*x) = (wxCoord) (rect.width / PANGO_SCALE);
1100 if (y) (*y) = (wxCoord) (rect.height / PANGO_SCALE);
1101 if (descent)
1102 {
1103 // Do something about metrics here
1104 (*descent) = 0;
1105 }
1106 if (externalLeading) (*externalLeading) = 0; // ??
1107
1108 g_object_unref( G_OBJECT( layout ) );
1109 #else
1110 WXFontStructPtr pFontStruct = fontToUse.GetFontStruct(1.0, wxGlobalDisplay());
1111
1112 int direction, ascent, descent2;
1113 XCharStruct overall;
1114 int slen = string.length();
1115
1116 XTextExtents((XFontStruct*) pFontStruct, (char*) string.c_str(), slen,
1117 &direction, &ascent, &descent2, &overall);
1118
1119 if ( x )
1120 *x = (overall.width);
1121 if ( y )
1122 *y = (ascent + descent2);
1123 if (descent)
1124 *descent = descent2;
1125 if (externalLeading)
1126 *externalLeading = 0;
1127 #endif
1128 }
1129
1130 // ----------------------------------------------------------------------------
1131 // painting
1132 // ----------------------------------------------------------------------------
1133
1134 void wxWindowX11::Refresh(bool eraseBack, const wxRect *rect)
1135 {
1136 if (eraseBack)
1137 {
1138 if (rect)
1139 {
1140 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1141 m_clearRegion.Union( rect->x, rect->y, rect->width, rect->height );
1142 }
1143 else
1144 {
1145 int height,width;
1146 GetSize( &width, &height );
1147
1148 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1149 m_clearRegion.Clear();
1150 m_clearRegion.Union( 0, 0, width, height );
1151 }
1152 }
1153
1154 if (rect)
1155 {
1156 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1157 m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
1158 }
1159 else
1160 {
1161 int height,width;
1162 GetSize( &width, &height );
1163
1164 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1165 m_updateRegion.Clear();
1166 m_updateRegion.Union( 0, 0, width, height );
1167 }
1168 }
1169
1170 void wxWindowX11::Update()
1171 {
1172 if (m_updateNcArea)
1173 {
1174 // wxLogDebug("wxWindowX11::UpdateNC: %s", GetClassInfo()->GetClassName());
1175 // Send nc paint events.
1176 SendNcPaintEvents();
1177 }
1178
1179 if (!m_updateRegion.IsEmpty())
1180 {
1181 // wxLogDebug("wxWindowX11::Update: %s", GetClassInfo()->GetClassName());
1182 // Actually send erase events.
1183 SendEraseEvents();
1184
1185 // Actually send paint events.
1186 SendPaintEvents();
1187 }
1188 }
1189
1190 void wxWindowX11::SendEraseEvents()
1191 {
1192 if (m_clearRegion.IsEmpty()) return;
1193
1194 wxClientDC dc( (wxWindow*)this );
1195 dc.SetClippingRegion( m_clearRegion );
1196
1197 wxEraseEvent erase_event( GetId(), &dc );
1198 erase_event.SetEventObject( this );
1199
1200 if (!GetEventHandler()->ProcessEvent(erase_event) )
1201 {
1202 Display *xdisplay = wxGlobalDisplay();
1203 Window xwindow = (Window) GetClientAreaWindow();
1204 XSetForeground( xdisplay, g_eraseGC, m_backgroundColour.GetPixel() );
1205
1206 wxRegionIterator upd( m_clearRegion );
1207 while (upd)
1208 {
1209 XFillRectangle( xdisplay, xwindow, g_eraseGC,
1210 upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
1211 upd ++;
1212 }
1213 }
1214
1215 m_clearRegion.Clear();
1216 }
1217
1218 void wxWindowX11::SendPaintEvents()
1219 {
1220 // wxLogDebug("SendPaintEvents: %s (%ld)", GetClassInfo()->GetClassName(), GetId());
1221
1222 m_clipPaintRegion = true;
1223
1224 wxPaintEvent paint_event( GetId() );
1225 paint_event.SetEventObject( this );
1226 GetEventHandler()->ProcessEvent( paint_event );
1227
1228 m_updateRegion.Clear();
1229
1230 m_clipPaintRegion = false;
1231 }
1232
1233 void wxWindowX11::SendNcPaintEvents()
1234 {
1235 wxWindow *window = (wxWindow*) this;
1236
1237 // All this for drawing the small square between the scrollbars.
1238 int width = 0;
1239 int height = 0;
1240 int x = 0;
1241 int y = 0;
1242 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
1243 if (sb && sb->IsShown())
1244 {
1245 height = sb->GetSize().y;
1246 y = sb->GetPosition().y;
1247
1248 sb = window->GetScrollbar( wxVERTICAL );
1249 if (sb && sb->IsShown())
1250 {
1251 width = sb->GetSize().x;
1252 x = sb->GetPosition().x;
1253
1254 Display *xdisplay = wxGlobalDisplay();
1255 Window xwindow = (Window) GetMainWindow();
1256 Colormap cm = (Colormap) wxTheApp->GetMainColormap( wxGetDisplay() );
1257 wxColour colour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1258 colour.CalcPixel( (WXColormap) cm );
1259
1260 XSetForeground( xdisplay, g_eraseGC, colour.GetPixel() );
1261
1262 XFillRectangle( xdisplay, xwindow, g_eraseGC, x, y, width, height );
1263 }
1264 }
1265
1266 wxNcPaintEvent nc_paint_event( GetId() );
1267 nc_paint_event.SetEventObject( this );
1268 GetEventHandler()->ProcessEvent( nc_paint_event );
1269
1270 m_updateNcArea = false;
1271 }
1272
1273 // ----------------------------------------------------------------------------
1274 // event handlers
1275 // ----------------------------------------------------------------------------
1276
1277 // Responds to colour changes: passes event on to children.
1278 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent& event)
1279 {
1280 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1281 while ( node )
1282 {
1283 // Only propagate to non-top-level windows
1284 wxWindow *win = node->GetData();
1285 if ( win->GetParent() )
1286 {
1287 wxSysColourChangedEvent event2;
1288 event.SetEventObject(win);
1289 win->GetEventHandler()->ProcessEvent(event2);
1290 }
1291
1292 node = node->GetNext();
1293 }
1294 }
1295
1296 // See handler for InFocus case in app.cpp for details.
1297 wxWindow* g_GettingFocus = NULL;
1298
1299 void wxWindowX11::OnInternalIdle()
1300 {
1301 // Update invalidated regions.
1302 Update();
1303
1304 // This calls the UI-update mechanism (querying windows for
1305 // menu/toolbar/control state information)
1306 if (wxUpdateUIEvent::CanUpdate((wxWindow*) this))
1307 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1308
1309 // Set the input focus if couldn't do it before
1310 if (m_needsInputFocus)
1311 {
1312 #if 0
1313 wxString msg;
1314 msg.Printf("Setting focus for %s from OnInternalIdle\n", GetClassInfo()->GetClassName());
1315 printf(msg.c_str());
1316 #endif
1317 SetFocus();
1318
1319 // If it couldn't set the focus now, there's
1320 // no point in trying again.
1321 m_needsInputFocus = false;
1322 }
1323 g_GettingFocus = NULL;
1324 }
1325
1326 // ----------------------------------------------------------------------------
1327 // function which maintain the global hash table mapping Widgets to wxWidgets
1328 // ----------------------------------------------------------------------------
1329
1330 static bool DoAddWindowToTable(wxWindowHash *hash, Window w, wxWindow *win)
1331 {
1332 if ( !hash->insert(wxWindowHash::value_type(w, win)).second )
1333 {
1334 wxLogDebug( wxT("Widget table clash: new widget is 0x%08x, %s"),
1335 (unsigned int)w, win->GetClassInfo()->GetClassName());
1336 return false;
1337 }
1338
1339 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x <-> window %p (%s)"),
1340 (unsigned int) w, win, win->GetClassInfo()->GetClassName());
1341
1342 return true;
1343 }
1344
1345 static inline wxWindow *DoGetWindowFromTable(wxWindowHash *hash, Window w)
1346 {
1347 wxWindowHash::iterator i = hash->find(w);
1348 return i == hash->end() ? NULL : i->second;
1349 }
1350
1351 static inline void DoDeleteWindowFromTable(wxWindowHash *hash, Window w)
1352 {
1353 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x deleted"), (unsigned int) w);
1354
1355 hash->erase(w);
1356 }
1357
1358 // ----------------------------------------------------------------------------
1359 // public wrappers
1360 // ----------------------------------------------------------------------------
1361
1362 bool wxAddWindowToTable(Window w, wxWindow *win)
1363 {
1364 return DoAddWindowToTable(wxWidgetHashTable, w, win);
1365 }
1366
1367 wxWindow *wxGetWindowFromTable(Window w)
1368 {
1369 return DoGetWindowFromTable(wxWidgetHashTable, w);
1370 }
1371
1372 void wxDeleteWindowFromTable(Window w)
1373 {
1374 DoDeleteWindowFromTable(wxWidgetHashTable, w);
1375 }
1376
1377 bool wxAddClientWindowToTable(Window w, wxWindow *win)
1378 {
1379 return DoAddWindowToTable(wxClientWidgetHashTable, w, win);
1380 }
1381
1382 wxWindow *wxGetClientWindowFromTable(Window w)
1383 {
1384 return DoGetWindowFromTable(wxClientWidgetHashTable, w);
1385 }
1386
1387 void wxDeleteClientWindowFromTable(Window w)
1388 {
1389 DoDeleteWindowFromTable(wxClientWidgetHashTable, w);
1390 }
1391
1392 // ----------------------------------------------------------------------------
1393 // X11-specific accessors
1394 // ----------------------------------------------------------------------------
1395
1396 WXWindow wxWindowX11::GetMainWindow() const
1397 {
1398 return m_mainWindow;
1399 }
1400
1401 WXWindow wxWindowX11::GetClientAreaWindow() const
1402 {
1403 return m_clientWindow;
1404 }
1405
1406 // ----------------------------------------------------------------------------
1407 // TranslateXXXEvent() functions
1408 // ----------------------------------------------------------------------------
1409
1410 bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, Window window, XEvent *xevent)
1411 {
1412 switch (XEventGetType(xevent))
1413 {
1414 case EnterNotify:
1415 case LeaveNotify:
1416 case ButtonPress:
1417 case ButtonRelease:
1418 case MotionNotify:
1419 {
1420 wxEventType eventType = wxEVT_NULL;
1421
1422 if (XEventGetType(xevent) == EnterNotify)
1423 {
1424 //if (local_event.xcrossing.mode!=NotifyNormal)
1425 // return ; // Ignore grab events
1426 eventType = wxEVT_ENTER_WINDOW;
1427 // canvas->GetEventHandler()->OnSetFocus();
1428 }
1429 else if (XEventGetType(xevent) == LeaveNotify)
1430 {
1431 //if (local_event.xcrossingr.mode!=NotifyNormal)
1432 // return ; // Ignore grab events
1433 eventType = wxEVT_LEAVE_WINDOW;
1434 // canvas->GetEventHandler()->OnKillFocus();
1435 }
1436 else if (XEventGetType(xevent) == MotionNotify)
1437 {
1438 eventType = wxEVT_MOTION;
1439 }
1440 else if (XEventGetType(xevent) == ButtonPress)
1441 {
1442 wxevent.SetTimestamp(XButtonEventGetTime(xevent));
1443 int button = 0;
1444 if (XButtonEventLChanged(xevent))
1445 {
1446 eventType = wxEVT_LEFT_DOWN;
1447 button = 1;
1448 }
1449 else if (XButtonEventMChanged(xevent))
1450 {
1451 eventType = wxEVT_MIDDLE_DOWN;
1452 button = 2;
1453 }
1454 else if (XButtonEventRChanged(xevent))
1455 {
1456 eventType = wxEVT_RIGHT_DOWN;
1457 button = 3;
1458 }
1459
1460 // check for a double click
1461 // TODO: where can we get this value from?
1462 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1463 long dclickTime = 200;
1464 long ts = wxevent.GetTimestamp();
1465
1466 int buttonLast = win->GetLastClickedButton();
1467 long lastTS = win->GetLastClickTime();
1468 if ( buttonLast && buttonLast == button && (ts - lastTS) < dclickTime )
1469 {
1470 // I have a dclick
1471 win->SetLastClick(0, ts);
1472 if ( eventType == wxEVT_LEFT_DOWN )
1473 eventType = wxEVT_LEFT_DCLICK;
1474 else if ( eventType == wxEVT_MIDDLE_DOWN )
1475 eventType = wxEVT_MIDDLE_DCLICK;
1476 else if ( eventType == wxEVT_RIGHT_DOWN )
1477 eventType = wxEVT_RIGHT_DCLICK;
1478 }
1479 else
1480 {
1481 // not fast enough or different button
1482 win->SetLastClick(button, ts);
1483 }
1484 }
1485 else if (XEventGetType(xevent) == ButtonRelease)
1486 {
1487 if (XButtonEventLChanged(xevent))
1488 {
1489 eventType = wxEVT_LEFT_UP;
1490 }
1491 else if (XButtonEventMChanged(xevent))
1492 {
1493 eventType = wxEVT_MIDDLE_UP;
1494 }
1495 else if (XButtonEventRChanged(xevent))
1496 {
1497 eventType = wxEVT_RIGHT_UP;
1498 }
1499 else return false;
1500 }
1501 else
1502 {
1503 return false;
1504 }
1505
1506 wxevent.SetEventType(eventType);
1507
1508 wxevent.m_x = XButtonEventGetX(xevent);
1509 wxevent.m_y = XButtonEventGetY(xevent);
1510
1511 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
1512 || (XButtonEventLIsDown(xevent)
1513 && (eventType != wxEVT_LEFT_UP)));
1514 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
1515 || (XButtonEventMIsDown(xevent)
1516 && (eventType != wxEVT_MIDDLE_UP)));
1517 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
1518 || (XButtonEventRIsDown (xevent)
1519 && (eventType != wxEVT_RIGHT_UP)));
1520
1521 wxevent.m_shiftDown = XButtonEventShiftIsDown(xevent);
1522 wxevent.m_controlDown = XButtonEventCtrlIsDown(xevent);
1523 wxevent.m_altDown = XButtonEventAltIsDown(xevent);
1524 wxevent.m_metaDown = XButtonEventMetaIsDown(xevent);
1525
1526 wxevent.SetId(win->GetId());
1527 wxevent.SetEventObject(win);
1528
1529 return true;
1530 }
1531 }
1532 return false;
1533 }
1534
1535 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Window WXUNUSED(win), XEvent *xevent, bool isAscii)
1536 {
1537 switch (XEventGetType(xevent))
1538 {
1539 case KeyPress:
1540 case KeyRelease:
1541 {
1542 char buf[20];
1543
1544 KeySym keySym;
1545 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
1546 int id = wxCharCodeXToWX (keySym);
1547 // id may be WXK_xxx code - these are outside ASCII range, so we
1548 // can't just use toupper() on id.
1549 // Only change this if we want the raw key that was pressed,
1550 // and don't change it if we want an ASCII value.
1551 if (!isAscii && (id >= 'a' && id <= 'z'))
1552 {
1553 id = id + 'A' - 'a';
1554 }
1555
1556 wxevent.m_shiftDown = XKeyEventShiftIsDown(xevent);
1557 wxevent.m_controlDown = XKeyEventCtrlIsDown(xevent);
1558 wxevent.m_altDown = XKeyEventAltIsDown(xevent);
1559 wxevent.m_metaDown = XKeyEventMetaIsDown(xevent);
1560 wxevent.SetEventObject(win);
1561 wxevent.m_keyCode = id;
1562 wxevent.SetTimestamp(XKeyEventGetTime(xevent));
1563
1564 wxevent.m_x = XKeyEventGetX(xevent);
1565 wxevent.m_y = XKeyEventGetY(xevent);
1566
1567 return id > -1;
1568 }
1569 default:
1570 break;
1571 }
1572 return false;
1573 }
1574
1575 // ----------------------------------------------------------------------------
1576 // Colour stuff
1577 // ----------------------------------------------------------------------------
1578
1579 bool wxWindowX11::SetBackgroundColour(const wxColour& col)
1580 {
1581 wxWindowBase::SetBackgroundColour(col);
1582
1583 Display *xdisplay = (Display*) wxGlobalDisplay();
1584 int xscreen = DefaultScreen( xdisplay );
1585 Colormap cm = DefaultColormap( xdisplay, xscreen );
1586
1587 m_backgroundColour.CalcPixel( (WXColormap) cm );
1588
1589 // We don't set the background colour as we paint
1590 // the background ourselves.
1591 // XSetWindowBackground( xdisplay, (Window) m_clientWindow, m_backgroundColour.GetPixel() );
1592
1593 return true;
1594 }
1595
1596 bool wxWindowX11::SetForegroundColour(const wxColour& col)
1597 {
1598 if ( !wxWindowBase::SetForegroundColour(col) )
1599 return false;
1600
1601 return true;
1602 }
1603
1604 // ----------------------------------------------------------------------------
1605 // global functions
1606 // ----------------------------------------------------------------------------
1607
1608 wxWindow *wxGetActiveWindow()
1609 {
1610 // TODO
1611 wxFAIL_MSG(wxT("Not implemented"));
1612 return NULL;
1613 }
1614
1615 /* static */
1616 wxWindow *wxWindowBase::GetCapture()
1617 {
1618 return (wxWindow *)g_captureWindow;
1619 }
1620
1621
1622 // Find the wxWindow at the current mouse position, returning the mouse
1623 // position.
1624 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1625 {
1626 return wxFindWindowAtPoint(wxGetMousePosition());
1627 }
1628
1629 void wxGetMouseState(int& rootX, int& rootY, unsigned& maskReturn)
1630 {
1631 #if wxUSE_NANOX
1632 /* TODO */
1633 rootX = rootY = 0;
1634 maskReturn = 0;
1635 #else
1636 Display *display = wxGlobalDisplay();
1637 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
1638 Window rootReturn, childReturn;
1639 int winX, winY;
1640
1641 XQueryPointer (display,
1642 rootWindow,
1643 &rootReturn,
1644 &childReturn,
1645 &rootX, &rootY, &winX, &winY, &maskReturn);
1646 #endif
1647 }
1648
1649 // Get the current mouse position.
1650 wxPoint wxGetMousePosition()
1651 {
1652 int x, y;
1653 unsigned mask;
1654
1655 wxGetMouseState(x, y, mask);
1656 return wxPoint(x, y);
1657 }
1658
1659 wxMouseState wxGetMouseState()
1660 {
1661 wxMouseState ms;
1662 int x, y;
1663 unsigned mask;
1664
1665 wxGetMouseState(x, y, mask);
1666
1667 ms.SetX(x);
1668 ms.SetY(y);
1669
1670 ms.SetLeftDown(mask & Button1Mask);
1671 ms.SetMiddleDown(mask & Button2Mask);
1672 ms.SetRightDown(mask & Button3Mask);
1673
1674 ms.SetControlDown(mask & ControlMask);
1675 ms.SetShiftDown(mask & ShiftMask);
1676 ms.SetAltDown(mask & Mod3Mask);
1677 ms.SetMetaDown(mask & Mod1Mask);
1678
1679 return ms;
1680 }
1681
1682
1683 // ----------------------------------------------------------------------------
1684 // wxNoOptimize: switch off size optimization
1685 // ----------------------------------------------------------------------------
1686
1687 int wxNoOptimize::ms_count = 0;
1688
1689
1690 // ----------------------------------------------------------------------------
1691 // wxDCModule
1692 // ----------------------------------------------------------------------------
1693
1694 class wxWinModule : public wxModule
1695 {
1696 public:
1697 wxWinModule()
1698 {
1699 // we must be cleaned up before the display is closed
1700 AddDependency(wxClassInfo::FindClass(_T("wxX11DisplayModule")));
1701 }
1702
1703 virtual bool OnInit();
1704 virtual void OnExit();
1705
1706 private:
1707 DECLARE_DYNAMIC_CLASS(wxWinModule)
1708 };
1709
1710 IMPLEMENT_DYNAMIC_CLASS(wxWinModule, wxModule)
1711
1712 bool wxWinModule::OnInit()
1713 {
1714 Display *xdisplay = wxGlobalDisplay();
1715 int xscreen = DefaultScreen( xdisplay );
1716 Window xroot = RootWindow( xdisplay, xscreen );
1717 g_eraseGC = XCreateGC( xdisplay, xroot, 0, NULL );
1718 XSetFillStyle( xdisplay, g_eraseGC, FillSolid );
1719
1720 return true;
1721 }
1722
1723 void wxWinModule::OnExit()
1724 {
1725 Display *xdisplay = wxGlobalDisplay();
1726 XFreeGC( xdisplay, g_eraseGC );
1727 }