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