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