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