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