]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/window.cpp
Committing in .
[wxWidgets.git] / src / mgl / window.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
a4bbc9f7
VS
2// Name: src/mgl/window.cpp
3// Purpose: wxWindow
4// Author: Vaclav Slavik
5// (based on GTK & MSW implementations)
6// RCS-ID: $Id$
7// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
8// Licence: wxWindows license
32b8ec41
VZ
9/////////////////////////////////////////////////////////////////////////////
10
a4bbc9f7
VS
11// ===========================================================================
12// declarations
13// ===========================================================================
14
15// ---------------------------------------------------------------------------
16// headers
17// ---------------------------------------------------------------------------
32b8ec41
VZ
18
19#ifdef __GNUG__
20 #pragma implementation "window.h"
21#endif
22
a4bbc9f7
VS
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#ifndef WX_PRECOMP
31 #include "wx/window.h"
32 #include "wx/accel.h"
33 #include "wx/setup.h"
34 #include "wx/dc.h"
35 #include "wx/dcclient.h"
36 #include "wx/utils.h"
37 #include "wx/app.h"
38 #include "wx/panel.h"
39 #include "wx/caret.h"
40#endif
41
42#if wxUSE_DRAG_AND_DROP
43 #include "wx/dnd.h"
44#endif
45
46#include "wx/log.h"
47#include "wx/sysopt.h"
48#include "wx/mgl/private.h"
49#include "wx/intl.h"
50#include "wx/dcscreen.h"
51
52#include <mgraph.hpp>
53
54#if wxUSE_TOOLTIPS
55 #include "wx/tooltip.h"
56#endif
57
58// ---------------------------------------------------------------------------
59// global variables
60// ---------------------------------------------------------------------------
61
62// MGL window manager and associated DC.
63winmng_t *g_winMng = NULL;
64MGLDevCtx *g_displayDC = NULL;
65
2b260bc3 66// the window that has keyboard focus:
7cdbf02c 67static wxWindowMGL *gs_focusedWindow = NULL;
7bdc1879 68// the window that is currently under mouse cursor:
7cdbf02c
VS
69static wxWindowMGL *gs_windowUnderMouse = NULL;
70// the window that has mouse capture
71static wxWindowMGL *gs_mouseCapture = NULL;
72// the frame that is currently active (i.e. its child has focus). It is
73// used to generate wxActivateEvents
74static wxWindowMGL *gs_activeFrame = NULL;
a4bbc9f7
VS
75
76// ---------------------------------------------------------------------------
77// constants
78// ---------------------------------------------------------------------------
79
80// Custom identifiers used to distinguish between various event handlers
81// and capture handlers passed to MGL_wm
82enum
83{
84 wxMGL_CAPTURE_MOUSE = 1,
85 wxMGL_CAPTURE_KEYB = 2
86};
87
88
89// ---------------------------------------------------------------------------
90// private functions
91// ---------------------------------------------------------------------------
92
7cdbf02c
VS
93// Returns toplevel grandparent of given window:
94static wxWindowMGL* wxGetTopLevelParent(wxWindowMGL *win)
95{
96 wxWindowMGL *p = win;
97 while (p && !p->IsTopLevel())
98 p = p->GetParent();
99 return p;
100}
101
e6daf008
VS
102#ifdef __WXDEBUG__
103// Add an easy way to capture screenshots:
104static void CaptureScreenshot()
105{
106 wxBusyCursor bcur;
107
108 static int screenshot_num = 0;
109 char screenshot[128];
110 sprintf(screenshot, "screenshot-%03i.png", screenshot_num++);
111 g_displayDC->savePNGFromDC(screenshot, 0, 0,
112 g_displayDC->sizex(),
113 g_displayDC->sizey());
114}
115#endif
116
7bdc1879
VS
117// ---------------------------------------------------------------------------
118// MGL_WM hooks:
119// ---------------------------------------------------------------------------
120
b43a9886 121static void MGLAPI wxWindowPainter(window_t *wnd, MGLDC *dc)
7bdc1879
VS
122{
123 wxWindowMGL *w = (wxWindow*) wnd->userData;
61bd618f 124
544c782d 125 if ( w && !(w->GetWindowStyle() & wxTRANSPARENT_WINDOW) )
7bdc1879
VS
126 {
127 MGLDevCtx ctx(dc);
128 w->HandlePaint(&ctx);
129 }
ef344ff8
VS
130}
131
b43a9886 132static ibool MGLAPI wxWindowMouseHandler(window_t *wnd, event_t *e)
ef344ff8
VS
133{
134 wxWindowMGL *win = (wxWindowMGL*)MGL_wmGetWindowUserData(wnd);
d76048f5 135 wxPoint orig(win->GetClientAreaOrigin());
985520bc 136 wxPoint where;
ef344ff8 137
985520bc
VS
138 MGL_wmCoordGlobalToLocal(win->GetHandle(),
139 e->where_x, e->where_y, &where.x, &where.y);
140
0a89ac9c
VS
141 for (wxWindowMGL *w = win; w; w = w->GetParent())
142 {
143 if ( !w->IsEnabled() )
144 return FALSE;
145 if ( w->IsTopLevel() )
146 break;
147 }
a200c35e 148
ef344ff8
VS
149 wxEventType type = wxEVT_NULL;
150 wxMouseEvent event;
151 event.SetEventObject(win);
152 event.SetTimestamp(e->when);
d76048f5
VS
153 event.m_x = where.x - orig.x;
154 event.m_y = where.y - orig.y;
ef344ff8
VS
155 event.m_shiftDown = e->modifiers & EVT_SHIFTKEY;
156 event.m_controlDown = e->modifiers & EVT_CTRLSTATE;
157 event.m_altDown = e->modifiers & EVT_LEFTALT;
158 event.m_metaDown = e->modifiers & EVT_RIGHTALT;
159 event.m_leftDown = e->modifiers & EVT_LEFTBUT;
160 event.m_middleDown = e->modifiers & EVT_MIDDLEBUT;
161 event.m_rightDown = e->modifiers & EVT_RIGHTBUT;
162
163 switch (e->what)
164 {
165 case EVT_MOUSEDOWN:
166 if ( e->message & EVT_LEFTBMASK )
167 type = (e->message & EVT_DBLCLICK) ?
168 wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN;
169 else if ( e->message & EVT_MIDDLEBMASK )
170 type = (e->message & EVT_DBLCLICK) ?
171 wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN;
172 else if ( e->message & EVT_RIGHTBMASK )
173 type = (e->message & EVT_DBLCLICK) ?
174 wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN;
175 break;
176
177 case EVT_MOUSEUP:
178 if ( e->message & EVT_LEFTBMASK )
179 type = wxEVT_LEFT_UP;
180 else if ( e->message & EVT_MIDDLEBMASK )
181 type = wxEVT_MIDDLE_UP;
182 else if ( e->message & EVT_RIGHTBMASK )
183 type = wxEVT_RIGHT_UP;
184 break;
185
186 case EVT_MOUSEMOVE:
7cdbf02c
VS
187 if ( !gs_mouseCapture )
188 {
189 if ( win != gs_windowUnderMouse )
190 {
191 if ( gs_windowUnderMouse )
192 {
193 wxMouseEvent event2(event);
194 MGL_wmCoordGlobalToLocal(gs_windowUnderMouse->GetHandle(),
195 e->where_x, e->where_y,
196 &event2.m_x, &event2.m_y);
197
d76048f5
VS
198 wxPoint orig(gs_windowUnderMouse->GetClientAreaOrigin());
199 event2.m_x -= orig.x;
200 event2.m_y -= orig.y;
201
7cdbf02c
VS
202 event2.SetEventObject(gs_windowUnderMouse);
203 event2.SetEventType(wxEVT_LEAVE_WINDOW);
204 gs_windowUnderMouse->GetEventHandler()->ProcessEvent(event2);
205 }
206
207 wxMouseEvent event3(event);
208 event3.SetEventType(wxEVT_ENTER_WINDOW);
209 win->GetEventHandler()->ProcessEvent(event3);
210
211 gs_windowUnderMouse = win;
212 }
213 }
214 else // gs_mouseCapture
ef344ff8 215 {
7cdbf02c
VS
216 bool inside = (where.x >= 0 &&
217 where.y >= 0 &&
218 where.x < win->GetSize().x &&
219 where.y < win->GetSize().y);
220 if ( (inside && gs_windowUnderMouse != win) ||
221 (!inside && gs_windowUnderMouse == win) )
ef344ff8 222 {
7cdbf02c
VS
223 wxMouseEvent evt(inside ?
224 wxEVT_ENTER_WINDOW : wxEVT_LEAVE_WINDOW);
225 evt.SetEventObject(win);
226 win->GetEventHandler()->ProcessEvent(evt);
227 gs_windowUnderMouse = inside ? win : NULL;
ef344ff8 228 }
ef344ff8
VS
229 }
230
231 type = wxEVT_MOTION;
232 break;
233
234 default:
235 break;
236 }
237
238 if ( type == wxEVT_NULL )
239 {
240 return FALSE;
241 }
242 else
243 {
244 event.SetEventType(type);
245 return win->GetEventHandler()->ProcessEvent(event);
246 }
247}
248
2b260bc3 249static long wxScanToKeyCode(event_t *event)
ef344ff8 250{
2b260bc3
VS
251 #define KEY(mgl,wx) case mgl: key = wx; break;
252
253 long key = 0;
254 switch ( EVT_scanCode(event->message) )
255 {
256 KEY (KB_padEnter, WXK_NUMPAD_ENTER)
257 KEY (KB_padMinus, WXK_NUMPAD_SUBTRACT)
258 KEY (KB_padPlus, WXK_NUMPAD_ADD)
259 KEY (KB_padTimes, WXK_NUMPAD_MULTIPLY)
260 KEY (KB_padDivide, WXK_NUMPAD_DIVIDE)
261 KEY (KB_padLeft, WXK_NUMPAD_LEFT)
262 KEY (KB_padRight, WXK_NUMPAD_RIGHT)
263 KEY (KB_padUp, WXK_NUMPAD_UP)
264 KEY (KB_padDown, WXK_NUMPAD_DOWN)
265 KEY (KB_padInsert, WXK_NUMPAD_INSERT)
266 KEY (KB_padDelete, WXK_NUMPAD_DELETE)
267 KEY (KB_padHome, WXK_NUMPAD_HOME)
268 KEY (KB_padEnd, WXK_NUMPAD_END)
269 KEY (KB_padPageUp, WXK_NUMPAD_PAGEUP)
270 //KEY (KB_padPageUp, WXK_NUMPAD_PRIOR)
271 KEY (KB_padPageDown, WXK_NUMPAD_PAGEDOWN)
272 //KEY (KB_padPageDown, WXK_NUMPAD_NEXT)
273 KEY (KB_padCenter, WXK_NUMPAD_SEPARATOR) // ?
274 KEY (KB_F1, WXK_F1)
275 KEY (KB_F2, WXK_F2)
276 KEY (KB_F3, WXK_F3)
277 KEY (KB_F4, WXK_F4)
278 KEY (KB_F5, WXK_F5)
279 KEY (KB_F6, WXK_F6)
280 KEY (KB_F7, WXK_F7)
281 KEY (KB_F8, WXK_F8)
282 KEY (KB_F9, WXK_F9)
283 KEY (KB_F10, WXK_F10)
284 KEY (KB_F11, WXK_F11)
285 KEY (KB_F12, WXK_F12)
286 KEY (KB_left, WXK_LEFT)
287 KEY (KB_right, WXK_RIGHT)
288 KEY (KB_up, WXK_UP)
289 KEY (KB_down, WXK_DOWN)
290 KEY (KB_insert, WXK_INSERT)
291 KEY (KB_delete, WXK_DELETE)
292 KEY (KB_home, WXK_HOME)
293 KEY (KB_end, WXK_END)
294 KEY (KB_pageUp, WXK_PAGEUP)
295 KEY (KB_pageDown, WXK_PAGEDOWN)
296 KEY (KB_capsLock, WXK_CAPITAL)
297 KEY (KB_numLock, WXK_NUMLOCK)
298 KEY (KB_scrollLock, WXK_SCROLL)
299 KEY (KB_leftShift, WXK_SHIFT)
300 KEY (KB_rightShift, WXK_SHIFT)
301 KEY (KB_leftCtrl, WXK_CONTROL)
302 KEY (KB_rightCtrl, WXK_CONTROL)
303 KEY (KB_leftAlt, WXK_ALT)
304 KEY (KB_rightAlt, WXK_ALT)
305 KEY (KB_leftWindows, WXK_START)
306 KEY (KB_rightWindows, WXK_START)
307 KEY (KB_menu, WXK_MENU)
308 KEY (KB_sysReq, WXK_SNAPSHOT)
309 KEY (KB_esc, WXK_ESCAPE)
310 KEY (KB_1, '1')
311 KEY (KB_2, '2')
312 KEY (KB_3, '3')
313 KEY (KB_4, '4')
314 KEY (KB_5, '5')
315 KEY (KB_6, '6')
316 KEY (KB_7, '7')
317 KEY (KB_8, '8')
318 KEY (KB_9, '9')
319 KEY (KB_0, '0')
320 KEY (KB_minus, WXK_SUBTRACT)
321 KEY (KB_equals, WXK_ADD)
322 KEY (KB_backSlash, '\\')
323 KEY (KB_backspace, WXK_BACK)
324 KEY (KB_tab, WXK_TAB)
325 KEY (KB_Q, 'Q')
326 KEY (KB_W, 'W')
327 KEY (KB_E, 'E')
328 KEY (KB_R, 'R')
329 KEY (KB_T, 'T')
330 KEY (KB_Y, 'Y')
331 KEY (KB_U, 'U')
332 KEY (KB_I, 'I')
333 KEY (KB_O, 'O')
334 KEY (KB_P, 'P')
335 KEY (KB_leftSquareBrace,'[')
336 KEY (KB_rightSquareBrace,']')
337 KEY (KB_enter, WXK_RETURN)
338 KEY (KB_A, 'A')
339 KEY (KB_S, 'S')
340 KEY (KB_D, 'D')
341 KEY (KB_F, 'F')
342 KEY (KB_G, 'G')
343 KEY (KB_H, 'H')
344 KEY (KB_J, 'J')
345 KEY (KB_K, 'K')
346 KEY (KB_L, 'L')
347 KEY (KB_semicolon, ';')
348 KEY (KB_apostrophe, '\'')
349 KEY (KB_Z, 'Z')
350 KEY (KB_X, 'X')
351 KEY (KB_C, 'C')
352 KEY (KB_V, 'V')
353 KEY (KB_B, 'B')
354 KEY (KB_N, 'N')
355 KEY (KB_M, 'M')
356 KEY (KB_comma, ',')
357 KEY (KB_period, '.')
358 KEY (KB_divide, WXK_DIVIDE)
359 KEY (KB_space, WXK_SPACE)
360 KEY (KB_tilde, '~')
361
362 default:
363 key = EVT_asciiCode(event->message);
364 break;
365 }
366
544c782d
VS
367 #undef KEY
368
2b260bc3 369 return key;
ef344ff8
VS
370}
371
2b260bc3 372static long wxAsciiToKeyCode(event_t *event)
ef344ff8 373{
2b260bc3
VS
374 return (long)EVT_asciiCode(event->message);
375}
376
b43a9886 377static ibool MGLAPI wxWindowKeybHandler(window_t *wnd, event_t *e)
2b260bc3 378{
2b260bc3 379 wxWindowMGL *win = (wxWindowMGL*)MGL_wmGetWindowUserData(wnd);
a200c35e
VS
380
381 if ( !win->IsEnabled() ) return FALSE;
382
985520bc
VS
383 wxPoint where;
384 MGL_wmCoordGlobalToLocal(win->GetHandle(),
385 e->where_x, e->where_y, &where.x, &where.y);
2b260bc3
VS
386
387 wxKeyEvent event;
388 event.SetEventObject(win);
389 event.SetTimestamp(e->when);
390 event.m_keyCode = wxScanToKeyCode(e);
391 event.m_scanCode = 0; // not used by wx at all
392 event.m_x = where.x;
393 event.m_y = where.y;
2b260bc3
VS
394 event.m_shiftDown = e->modifiers & EVT_SHIFTKEY;
395 event.m_controlDown = e->modifiers & EVT_CTRLSTATE;
396 event.m_altDown = e->modifiers & EVT_LEFTALT;
397 event.m_metaDown = e->modifiers & EVT_RIGHTALT;
398
399 if ( e->what == EVT_KEYUP )
400 {
401 event.SetEventType(wxEVT_KEY_UP);
402 return win->GetEventHandler()->ProcessEvent(event);
403 }
404 else
405 {
406 bool ret;
407 wxKeyEvent event2;
408
409 event.SetEventType(wxEVT_KEY_DOWN);
410 event2 = event;
411
412 ret = win->GetEventHandler()->ProcessEvent(event);
413
e6daf008
VS
414
415#ifdef __WXDEBUG__
416 // Add an easy way to capture screenshots:
417 if ( event.m_keyCode == WXK_F1 &&
418 event.m_shiftDown && event.m_controlDown )
419 CaptureScreenshot();
420#endif
421
2b260bc3
VS
422#if wxUSE_ACCEL
423 if ( !ret )
424 {
425 for (wxWindowMGL *w = win; w; w = w->GetParent())
426 {
427 int command = w->GetAcceleratorTable()->GetCommand(event);
428 if ( command != -1 )
429 {
430 wxCommandEvent eventc(wxEVT_COMMAND_MENU_SELECTED, command);
431 ret = w->GetEventHandler()->ProcessEvent(eventc);
432 break;
433 }
434 if ( w->IsTopLevel() )
435 break;
436 }
437 }
438#endif // wxUSE_ACCEL
439
440 /* wxMSW doesn't send char events with Alt pressed */
441 /* Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
442 will only be sent if it is not in an accelerator table. */
443 event2.m_keyCode = wxAsciiToKeyCode(e);
444 if ( !ret && event2.m_keyCode != 0 )
445 {
446 event2.SetEventType(wxEVT_CHAR);
447 ret = win->GetEventHandler()->ProcessEvent(event2);
448 }
449
450 return ret;
451 }
7bdc1879 452}
a4bbc9f7
VS
453
454// ---------------------------------------------------------------------------
455// event tables
456// ---------------------------------------------------------------------------
457
ef344ff8 458// in wxUniv this class is abstract because it doesn't have DoPopupMenu()
a4bbc9f7
VS
459IMPLEMENT_ABSTRACT_CLASS(wxWindowMGL, wxWindowBase)
460
461BEGIN_EVENT_TABLE(wxWindowMGL, wxWindowBase)
a4bbc9f7
VS
462END_EVENT_TABLE()
463
464// ===========================================================================
465// implementation
466// ===========================================================================
467
468// ----------------------------------------------------------------------------
469// constructors and such
470// ----------------------------------------------------------------------------
471
a3e76614
VS
472extern wxDisplayModeInfo wxGetDefaultDisplayMode();
473
a4bbc9f7
VS
474void wxWindowMGL::Init()
475{
1f43b5c9
VS
476 // First of all, make sure window manager is up and running. If it is
477 // not the case, initialize it in default display mode
478 if ( !g_winMng )
a3e76614 479 wxTheApp->SetDisplayMode(wxGetDefaultDisplayMode());
1f43b5c9 480
a4bbc9f7
VS
481 // generic:
482 InitBase();
483
484 // mgl specific:
a4bbc9f7
VS
485 m_wnd = NULL;
486 m_isShown = TRUE;
487 m_isBeingDeleted = FALSE;
488 m_isEnabled = TRUE;
489 m_frozen = FALSE;
490 m_paintMGLDC = NULL;
2b260bc3 491 m_eraseBackground = -1;
a4bbc9f7
VS
492}
493
494// Destructor
495wxWindowMGL::~wxWindowMGL()
496{
497 m_isBeingDeleted = TRUE;
498
7cdbf02c
VS
499 if ( gs_mouseCapture == this )
500 ReleaseMouse();
d46330c5 501
7cdbf02c 502 if (gs_activeFrame == this)
d46330c5 503 {
31ae546e 504 gs_activeFrame = NULL;
d46330c5
VS
505 // activate next frame in Z-order:
506 if ( m_wnd->prev )
507 {
508 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
509 win->SetFocus();
510 }
d46330c5
VS
511 }
512
7cdbf02c 513 if ( gs_focusedWindow == this )
7bdc1879 514 KillFocus();
d46330c5 515
7cdbf02c
VS
516 if ( gs_windowUnderMouse == this )
517 gs_windowUnderMouse = NULL;
a4bbc9f7
VS
518
519 // VS: destroy children first and _then_ detach *this from its parent.
520 // If we'd do it the other way around, children wouldn't be able
521 // find their parent frame (see above).
522 DestroyChildren();
523
524 if ( m_parent )
525 m_parent->RemoveChild(this);
526
527 if ( m_wnd )
528 MGL_wmDestroyWindow(m_wnd);
529}
530
531// real construction (Init() must have been called before!)
532bool wxWindowMGL::Create(wxWindow *parent,
533 wxWindowID id,
534 const wxPoint& pos,
535 const wxSize& size,
536 long style,
537 const wxString& name)
538{
a4bbc9f7
VS
539 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
540 return FALSE;
541
544c782d 542 if ( parent )
7bdc1879 543 parent->AddChild(this);
a4bbc9f7 544
ef344ff8
VS
545 int x, y, w, h;
546 x = pos.x, y = pos.y;
547 if ( x == -1 )
548 x = 0; // FIXME_MGL, something better, see GTK+
549 if ( y == -1 )
550 y = 0; // FIXME_MGL, something better, see GTK+
551 w = WidthDefault(size.x);
552 h = HeightDefault(size.y);
72fa862b
VS
553
554 long mgl_style = 0;
7cdbf02c 555 window_t *wnd_parent = parent ? parent->GetHandle() : NULL;
544c782d 556
72fa862b 557 if ( !(style & wxNO_FULL_REPAINT_ON_RESIZE) )
544c782d 558 {
72fa862b 559 mgl_style |= MGL_WM_FULL_REPAINT_ON_RESIZE;
544c782d 560 }
72fa862b 561 if ( style & wxSTAY_ON_TOP )
544c782d 562 {
72fa862b 563 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
544c782d
VS
564 }
565 if ( style & wxPOPUP_WINDOW )
566 {
7cdbf02c 567 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
544c782d
VS
568 // it is created hidden as other top level windows
569 m_isShown = FALSE;
7cdbf02c 570 wnd_parent = NULL;
544c782d 571 }
ef344ff8 572
58061670
VS
573 window_t *wnd = MGL_wmCreateWindow(g_winMng, wnd_parent, x, y, w, h);
574
575 MGL_wmSetWindowFlags(wnd, mgl_style);
576 MGL_wmShowWindow(wnd, m_isShown);
577
578 SetMGLwindow_t(wnd);
579
580 return TRUE;
581}
582
583void wxWindowMGL::SetMGLwindow_t(struct window_t *wnd)
584{
585 if ( m_wnd )
586 MGL_wmDestroyWindow(m_wnd);
587
588 m_wnd = wnd;
589 if ( !m_wnd ) return;
590
591 m_isShown = m_wnd->visible;
ef344ff8 592
a4bbc9f7
VS
593 MGL_wmSetWindowUserData(m_wnd, (void*) this);
594 MGL_wmSetWindowPainter(m_wnd, wxWindowPainter);
ef344ff8
VS
595 MGL_wmPushWindowEventHandler(m_wnd, wxWindowMouseHandler, EVT_MOUSEEVT, 0);
596 MGL_wmPushWindowEventHandler(m_wnd, wxWindowKeybHandler, EVT_KEYEVT, 0);
58061670
VS
597
598 if ( m_cursor.Ok() )
599 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
600 else
601 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
a4bbc9f7
VS
602}
603
604// ---------------------------------------------------------------------------
605// basic operations
606// ---------------------------------------------------------------------------
607
608void wxWindowMGL::SetFocus()
609{
a46935cb
VS
610 if ( gs_focusedWindow == this ) return;
611
7cdbf02c
VS
612 if ( gs_focusedWindow )
613 gs_focusedWindow->KillFocus();
a4bbc9f7 614
7cdbf02c 615 gs_focusedWindow = this;
a4bbc9f7 616
2b260bc3 617 MGL_wmCaptureEvents(GetHandle(), EVT_KEYEVT, wxMGL_CAPTURE_KEYB);
a4bbc9f7 618
a4bbc9f7
VS
619#if wxUSE_CARET
620 // caret needs to be informed about focus change
621 wxCaret *caret = GetCaret();
7cdbf02c 622 if ( caret )
a4bbc9f7
VS
623 caret->OnSetFocus();
624#endif // wxUSE_CARET
32b8ec41 625
7cdbf02c 626 wxWindowMGL *active = wxGetTopLevelParent(this);
61bd618f 627 if ( !(m_windowStyle & wxPOPUP_WINDOW) && active != gs_activeFrame )
a4bbc9f7 628 {
7cdbf02c
VS
629 if ( gs_activeFrame )
630 {
631 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, gs_activeFrame->GetId());
632 event.SetEventObject(gs_activeFrame);
633 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
634 }
635
636 gs_activeFrame = active;
637 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, gs_activeFrame->GetId());
638 event.SetEventObject(gs_activeFrame);
639 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
a4bbc9f7
VS
640 }
641
642 wxFocusEvent event(wxEVT_SET_FOCUS, GetId());
643 event.SetEventObject(this);
a46935cb 644 GetEventHandler()->ProcessEvent(event);
a4bbc9f7 645}
32b8ec41 646
a4bbc9f7 647void wxWindowMGL::KillFocus()
32b8ec41 648{
7cdbf02c
VS
649 if ( gs_focusedWindow != this ) return;
650 gs_focusedWindow = NULL;
a4bbc9f7 651
7bdc1879
VS
652 if ( m_isBeingDeleted ) return;
653
a4bbc9f7
VS
654 MGL_wmUncaptureEvents(GetHandle(), wxMGL_CAPTURE_KEYB);
655
656#if wxUSE_CARET
657 // caret needs to be informed about focus change
658 wxCaret *caret = GetCaret();
7bdc1879 659 if ( caret )
a4bbc9f7
VS
660 caret->OnKillFocus();
661#endif // wxUSE_CARET
662
a4bbc9f7
VS
663 wxFocusEvent event(wxEVT_KILL_FOCUS, GetId());
664 event.SetEventObject(this);
a46935cb 665 GetEventHandler()->ProcessEvent(event);
32b8ec41
VZ
666}
667
a4bbc9f7
VS
668// ----------------------------------------------------------------------------
669// this wxWindowBase function is implemented here (in platform-specific file)
670// because it is static and so couldn't be made virtual
671// ----------------------------------------------------------------------------
32b8ec41
VZ
672wxWindow *wxWindowBase::FindFocus()
673{
7cdbf02c 674 return (wxWindow*)gs_focusedWindow;
a4bbc9f7
VS
675}
676
677bool wxWindowMGL::Show(bool show)
678{
679 if ( !wxWindowBase::Show(show) )
680 return FALSE;
681
682 MGL_wmShowWindow(m_wnd, show);
ead60ec5
VS
683
684 if (!show && gs_activeFrame == this)
685 {
686 // activate next frame in Z-order:
687 if ( m_wnd->prev )
688 {
689 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
690 win->SetFocus();
691 }
692 }
693
a4bbc9f7
VS
694 return TRUE;
695}
696
697// Raise the window to the top of the Z order
698void wxWindowMGL::Raise()
699{
700 MGL_wmRaiseWindow(m_wnd);
701}
702
703// Lower the window to the bottom of the Z order
704void wxWindowMGL::Lower()
705{
706 MGL_wmLowerWindow(m_wnd);
707}
708
709void wxWindowMGL::CaptureMouse()
710{
7cdbf02c
VS
711 if ( gs_mouseCapture )
712 MGL_wmUncaptureEvents(gs_mouseCapture->m_wnd, wxMGL_CAPTURE_MOUSE);
713
714 gs_mouseCapture = this;
a4bbc9f7
VS
715 MGL_wmCaptureEvents(m_wnd, EVT_MOUSEEVT, wxMGL_CAPTURE_MOUSE);
716}
717
718void wxWindowMGL::ReleaseMouse()
719{
7cdbf02c
VS
720 wxASSERT_MSG( gs_mouseCapture == this, wxT("attempt to release mouse, but this window hasn't captured it") )
721
a4bbc9f7 722 MGL_wmUncaptureEvents(m_wnd, wxMGL_CAPTURE_MOUSE);
7cdbf02c 723 gs_mouseCapture = NULL;
a4bbc9f7
VS
724}
725
726/* static */ wxWindow *wxWindowBase::GetCapture()
727{
7cdbf02c 728 return (wxWindow*)gs_mouseCapture;
a4bbc9f7
VS
729}
730
731bool wxWindowMGL::SetCursor(const wxCursor& cursor)
732{
733 if ( !wxWindowBase::SetCursor(cursor) )
734 {
735 // no change
736 return FALSE;
737 }
738
739 if ( m_cursor.Ok() )
740 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
ef344ff8
VS
741 else
742 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
a4bbc9f7
VS
743
744 return TRUE;
745}
746
747void wxWindowMGL::WarpPointer(int x, int y)
748{
749 ClientToScreen(&x, &y);
750 EVT_setMousePos(x, y);
751}
752
753#if WXWIN_COMPATIBILITY
754// If nothing defined for this, try the parent.
755// E.g. we may be a button loaded from a resource, with no callback function
756// defined.
757void wxWindowMGL::OnCommand(wxWindow& win, wxCommandEvent& event)
758{
759 if ( GetEventHandler()->ProcessEvent(event) )
760 return;
761 if ( m_parent )
762 m_parent->GetEventHandler()->OnCommand(win, event);
763}
764#endif // WXWIN_COMPATIBILITY_2
765
766#if WXWIN_COMPATIBILITY
767wxObject* wxWindowMGL::GetChild(int number) const
768{
769 // Return a pointer to the Nth object in the Panel
770 wxNode *node = GetChildren().First();
771 int n = number;
772 while (node && n--)
773 node = node->Next();
774 if ( node )
775 {
776 wxObject *obj = (wxObject *)node->Data();
777 return(obj);
778 }
779 else
780 return NULL;
781}
782#endif // WXWIN_COMPATIBILITY
783
784// Set this window to be the child of 'parent'.
785bool wxWindowMGL::Reparent(wxWindowBase *parent)
786{
787 if ( !wxWindowBase::Reparent(parent) )
788 return FALSE;
789
790 MGL_wmReparentWindow(m_wnd, parent->GetHandle());
791
792 return TRUE;
793}
794
795
796// ---------------------------------------------------------------------------
797// drag and drop
798// ---------------------------------------------------------------------------
799
800#if wxUSE_DRAG_AND_DROP
801
802void wxWindowMGL::SetDropTarget(wxDropTarget *pDropTarget)
803{
804 if ( m_dropTarget != 0 ) {
805 m_dropTarget->Revoke(m_hWnd);
806 delete m_dropTarget;
807 }
808
809 m_dropTarget = pDropTarget;
810 if ( m_dropTarget != 0 )
811 m_dropTarget->Register(m_hWnd);
812}
813// FIXME_MGL
814#endif // wxUSE_DRAG_AND_DROP
815
816// old style file-manager drag&drop support: we retain the old-style
817// DragAcceptFiles in parallel with SetDropTarget.
818void wxWindowMGL::DragAcceptFiles(bool accept)
819{
820#if 0 // FIXME_MGL
821 HWND hWnd = GetHwnd();
822 if ( hWnd )
823 ::DragAcceptFiles(hWnd, (BOOL)accept);
824#endif
825}
826
827// ---------------------------------------------------------------------------
828// moving and resizing
829// ---------------------------------------------------------------------------
830
831// Get total size
832void wxWindowMGL::DoGetSize(int *x, int *y) const
833{
834 if (x) *x = m_wnd->width;
835 if (y) *y = m_wnd->height;
836}
837
838void wxWindowMGL::DoGetPosition(int *x, int *y) const
839{
840 if (x) *x = m_wnd->x;
841 if (y) *y = m_wnd->y;
842}
843
844void wxWindowMGL::DoScreenToClient(int *x, int *y) const
845{
846 int ax, ay;
985520bc 847 MGL_wmCoordGlobalToLocal(m_wnd, 0, 0, &ax, &ay);
a4bbc9f7 848 if (x)
985520bc 849 (*x) += ax;
a4bbc9f7 850 if (y)
985520bc 851 (*y) += ay;
a4bbc9f7
VS
852}
853
854void wxWindowMGL::DoClientToScreen(int *x, int *y) const
855{
856 int ax, ay;
985520bc 857 MGL_wmCoordLocalToGlobal(m_wnd, 0, 0, &ax, &ay);
a4bbc9f7 858 if (x)
985520bc 859 (*x) += ax;
a4bbc9f7 860 if (y)
985520bc 861 (*y) += ay;
a4bbc9f7
VS
862}
863
864// Get size *available for subwindows* i.e. excluding menu bar etc.
865void wxWindowMGL::DoGetClientSize(int *x, int *y) const
866{
867 DoGetSize(x, y);
868}
869
870void wxWindowMGL::DoMoveWindow(int x, int y, int width, int height)
871{
872 MGL_wmSetWindowPosition(GetHandle(), x, y, width, height);
873}
874
875// set the size of the window: if the dimensions are positive, just use them,
876// but if any of them is equal to -1, it means that we must find the value for
877// it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
878// which case -1 is a valid value for x and y)
879//
880// If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
881// the width/height to best suit our contents, otherwise we reuse the current
882// width/height
883void wxWindowMGL::DoSetSize(int x, int y, int width, int height, int sizeFlags)
884{
885 // get the current size and position...
886 int currentX, currentY;
887 GetPosition(&currentX, &currentY);
888 int currentW,currentH;
889 GetSize(&currentW, &currentH);
890
891 // ... and don't do anything (avoiding flicker) if it's already ok
892 if ( x == currentX && y == currentY &&
893 width == currentW && height == currentH )
894 {
895 return;
896 }
897
898 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
899 x = currentX;
900 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
901 y = currentY;
902
a4bbc9f7 903 AdjustForParentClientOrigin(x, y, sizeFlags);
a4bbc9f7
VS
904
905 wxSize size(-1, -1);
906 if ( width == -1 )
907 {
908 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
909 {
910 size = DoGetBestSize();
911 width = size.x;
912 }
913 else
914 {
915 // just take the current one
916 width = currentW;
917 }
918 }
919
920 if ( height == -1 )
921 {
922 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
923 {
924 if ( size.x == -1 )
925 {
926 size = DoGetBestSize();
927 }
928 //else: already called DoGetBestSize() above
929
930 height = size.y;
931 }
932 else
933 {
934 // just take the current one
935 height = currentH;
936 }
937 }
938
88f2a771
VS
939 if ( m_wnd->x != x || m_wnd->y != y ||
940 (int)m_wnd->width != width || (int)m_wnd->height != height )
941 {
942 DoMoveWindow(x, y, width, height);
a4bbc9f7 943
88f2a771
VS
944 wxSizeEvent event(wxSize(width, height), GetId());
945 event.SetEventObject(this);
946 GetEventHandler()->ProcessEvent(event);
947 }
a4bbc9f7
VS
948}
949
950void wxWindowMGL::DoSetClientSize(int width, int height)
951{
952 SetSize(width, height);
953}
954
955// ---------------------------------------------------------------------------
956// text metrics
957// ---------------------------------------------------------------------------
958
959int wxWindowMGL::GetCharHeight() const
960{
961 wxScreenDC dc;
962 dc.SetFont(m_font);
963 return dc.GetCharHeight();
964}
965
966int wxWindowMGL::GetCharWidth() const
967{
968 wxScreenDC dc;
969 dc.SetFont(m_font);
970 return dc.GetCharWidth();
971}
972
973void wxWindowMGL::GetTextExtent(const wxString& string,
974 int *x, int *y,
975 int *descent, int *externalLeading,
976 const wxFont *theFont) const
977{
978 wxScreenDC dc;
979 if (!theFont)
980 theFont = &m_font;
981 dc.GetTextExtent(string, x, y, descent, externalLeading, (wxFont*)theFont);
982}
983
984#if wxUSE_CARET && WXWIN_COMPATIBILITY
985// ---------------------------------------------------------------------------
986// Caret manipulation
987// ---------------------------------------------------------------------------
988
989void wxWindowMGL::CreateCaret(int w, int h)
990{
991 SetCaret(new wxCaret(this, w, h));
992}
993
994void wxWindowMGL::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
995{
996 wxFAIL_MSG("not implemented");
997}
998
999void wxWindowMGL::ShowCaret(bool show)
1000{
1001 wxCHECK_RET( m_caret, "no caret to show" );
1002
1003 m_caret->Show(show);
1004}
1005
1006void wxWindowMGL::DestroyCaret()
1007{
1008 SetCaret(NULL);
1009}
1010
1011void wxWindowMGL::SetCaretPos(int x, int y)
1012{
1013 wxCHECK_RET( m_caret, "no caret to move" );
1014
1015 m_caret->Move(x, y);
1016}
1017
1018void wxWindowMGL::GetCaretPos(int *x, int *y) const
1019{
1020 wxCHECK_RET( m_caret, "no caret to get position of" );
1021
1022 m_caret->GetPosition(x, y);
1023}
1024#endif // wxUSE_CARET
1025
1026
a4bbc9f7
VS
1027// ---------------------------------------------------------------------------
1028// painting
1029// ---------------------------------------------------------------------------
1030
1031void wxWindowMGL::Clear()
1032{
1033 wxClientDC dc((wxWindow *)this);
1034 wxBrush brush(GetBackgroundColour(), wxSOLID);
1035 dc.SetBackground(brush);
1036 dc.Clear();
1037}
1038
61bd618f 1039#include "wx/menu.h"
2b260bc3 1040void wxWindowMGL::Refresh(bool eraseBack, const wxRect *rect)
a4bbc9f7 1041{
2b260bc3
VS
1042 if ( m_eraseBackground == -1 )
1043 m_eraseBackground = eraseBack;
1044 else
1045 m_eraseBackground |= eraseBack;
1046
a4bbc9f7
VS
1047 if ( rect )
1048 {
1049 rect_t r;
1050 r.left = rect->GetLeft(), r.right = rect->GetRight();
1051 r.top = rect->GetTop(), r.bottom = rect->GetBottom();
1052 MGL_wmInvalidateWindowRect(GetHandle(), &r);
1053 }
1054 else
1055 MGL_wmInvalidateWindow(GetHandle());
1056}
1057
1058void wxWindowMGL::Update()
1059{
a4bbc9f7
VS
1060 if ( !m_frozen )
1061 MGL_wmUpdateDC(g_winMng);
1062}
1063
1064void wxWindowMGL::Freeze()
1065{
1066 m_frozen = TRUE;
1067 m_refreshAfterThaw = FALSE;
1068}
1069
1070void wxWindowMGL::Thaw()
1071{
1072 m_frozen = FALSE;
1073 if ( m_refreshAfterThaw )
1074 Refresh();
1075}
1076
a4bbc9f7
VS
1077void wxWindowMGL::HandlePaint(MGLDevCtx *dc)
1078{
1079 if ( m_frozen )
1080 {
1081 // Don't paint anything if the window is frozen.
fd495ab3 1082 m_refreshAfterThaw = TRUE;
a4bbc9f7
VS
1083 return;
1084 }
ef344ff8 1085
b8c0528d
VS
1086#if 0 // FIXME_MGL -- debugging stuff!
1087 dc->setColorRGB(255,0,255);
1088 dc->fillRect(-1000,-1000,2000,2000);
f41ed3c4 1089 wxUsleep(50);
b8c0528d
VS
1090#endif
1091
fd495ab3
VS
1092 MGLRegion clip;
1093 dc->getClipRegion(clip);
1094 m_updateRegion = wxRegion(clip);
a4bbc9f7
VS
1095 m_paintMGLDC = dc;
1096
2b260bc3 1097 if ( m_eraseBackground != 0 )
7bdc1879 1098 {
2b260bc3
VS
1099 wxWindowDC dc((wxWindow*)this);
1100 wxEraseEvent eventEr(m_windowId, &dc);
1101 eventEr.SetEventObject(this);
1102 GetEventHandler()->ProcessEvent(eventEr);
7bdc1879 1103 }
2b260bc3 1104 m_eraseBackground = -1;
a4bbc9f7
VS
1105
1106 wxNcPaintEvent eventNc(GetId());
1107 eventNc.SetEventObject(this);
1108 GetEventHandler()->ProcessEvent(eventNc);
1109
1110 wxPaintEvent eventPt(GetId());
1111 eventPt.SetEventObject(this);
1112 GetEventHandler()->ProcessEvent(eventPt);
a4bbc9f7 1113
7bdc1879 1114 m_paintMGLDC = NULL;
fd495ab3 1115 m_updateRegion.Clear();
a4bbc9f7
VS
1116}
1117
1118
1119// Find the wxWindow at the current mouse position, returning the mouse
1120// position.
1121wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1122{
1123 return wxFindWindowAtPoint(pt = wxGetMousePosition());
1124}
1125
1126wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
1127{
1128 window_t *wnd = MGL_wmGetWindowAtPosition(g_winMng, pt.x, pt.y);
1129 return (wxWindow*)wnd->userData;
32b8ec41 1130}