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