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