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