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