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