]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/window.cpp
1360b6545090597fb7c2053e09551557e8b16300
[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
536 if (gs_activeFrame == this)
537 {
538 gs_activeFrame = NULL;
539 // activate next frame in Z-order:
540 if ( m_wnd->prev )
541 {
542 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
543 win->SetFocus();
544 }
545 }
546
547 if ( gs_focusedWindow == this )
548 KillFocus();
549
550 if ( gs_windowUnderMouse == this )
551 gs_windowUnderMouse = NULL;
552
553 // VS: destroy children first and _then_ detach *this from its parent.
554 // If we'd do it the other way around, children wouldn't be able
555 // find their parent frame (see above).
556 DestroyChildren();
557
558 if ( m_parent )
559 m_parent->RemoveChild(this);
560
561 if ( m_wnd )
562 MGL_wmDestroyWindow(m_wnd);
563 }
564
565 // real construction (Init() must have been called before!)
566 bool wxWindowMGL::Create(wxWindow *parent,
567 wxWindowID id,
568 const wxPoint& pos,
569 const wxSize& size,
570 long style,
571 const wxString& name)
572 {
573 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
574 return FALSE;
575
576 if ( parent )
577 parent->AddChild(this);
578
579 int x, y, w, h;
580 x = pos.x, y = pos.y;
581 if ( x == -1 )
582 x = 0; // FIXME_MGL, something better, see GTK+
583 if ( y == -1 )
584 y = 0; // FIXME_MGL, something better, see GTK+
585 w = WidthDefault(size.x);
586 h = HeightDefault(size.y);
587
588 long mgl_style = 0;
589 window_t *wnd_parent = parent ? parent->GetHandle() : NULL;
590
591 if ( !(style & wxNO_FULL_REPAINT_ON_RESIZE) )
592 {
593 mgl_style |= MGL_WM_FULL_REPAINT_ON_RESIZE;
594 }
595 if ( style & wxSTAY_ON_TOP )
596 {
597 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
598 }
599 if ( style & wxPOPUP_WINDOW )
600 {
601 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
602 // it is created hidden as other top level windows
603 m_isShown = FALSE;
604 wnd_parent = NULL;
605 }
606
607 m_wnd = MGL_wmCreateWindow(g_winMng, wnd_parent, x, y, w, h);
608
609 MGL_wmSetWindowFlags(m_wnd, mgl_style);
610 MGL_wmSetWindowUserData(m_wnd, (void*) this);
611 MGL_wmSetWindowPainter(m_wnd, wxWindowPainter);
612 MGL_wmShowWindow(m_wnd, m_isShown);
613 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
614
615 MGL_wmPushWindowEventHandler(m_wnd, wxWindowMouseHandler, EVT_MOUSEEVT, 0);
616 MGL_wmPushWindowEventHandler(m_wnd, wxWindowKeybHandler, EVT_KEYEVT, 0);
617
618 return TRUE;
619 }
620
621 // ---------------------------------------------------------------------------
622 // basic operations
623 // ---------------------------------------------------------------------------
624
625 void wxWindowMGL::SetFocus()
626 {
627 if ( gs_focusedWindow )
628 gs_focusedWindow->KillFocus();
629
630 gs_focusedWindow = this;
631
632 MGL_wmCaptureEvents(GetHandle(), EVT_KEYEVT, wxMGL_CAPTURE_KEYB);
633
634 #if wxUSE_CARET
635 // caret needs to be informed about focus change
636 wxCaret *caret = GetCaret();
637 if ( caret )
638 caret->OnSetFocus();
639 #endif // wxUSE_CARET
640
641 wxWindowMGL *active = wxGetTopLevelParent(this);
642 if ( active != gs_activeFrame )
643 {
644 if ( gs_activeFrame )
645 {
646 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, gs_activeFrame->GetId());
647 event.SetEventObject(gs_activeFrame);
648 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
649 }
650
651 gs_activeFrame = active;
652 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, gs_activeFrame->GetId());
653 event.SetEventObject(gs_activeFrame);
654 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
655 }
656
657 wxFocusEvent event(wxEVT_SET_FOCUS, GetId());
658 event.SetEventObject(this);
659 GetEventHandler()->ProcessEvent(event);
660 }
661
662 void wxWindowMGL::KillFocus()
663 {
664 if ( gs_focusedWindow != this ) return;
665 gs_focusedWindow = NULL;
666
667 if ( m_isBeingDeleted ) return;
668
669 MGL_wmUncaptureEvents(GetHandle(), wxMGL_CAPTURE_KEYB);
670
671 #if wxUSE_CARET
672 // caret needs to be informed about focus change
673 wxCaret *caret = GetCaret();
674 if ( caret )
675 caret->OnKillFocus();
676 #endif // wxUSE_CARET
677
678 if ( IsTopLevel() )
679 {
680 // FIXME_MGL - this is wrong, see wxGTK!
681 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, GetId());
682 event.SetEventObject(this);
683 GetEventHandler()->ProcessEvent(event);
684 }
685
686 wxFocusEvent event(wxEVT_KILL_FOCUS, GetId());
687 event.SetEventObject(this);
688 GetEventHandler()->ProcessEvent(event);
689 }
690
691 // ----------------------------------------------------------------------------
692 // this wxWindowBase function is implemented here (in platform-specific file)
693 // because it is static and so couldn't be made virtual
694 // ----------------------------------------------------------------------------
695 wxWindow *wxWindowBase::FindFocus()
696 {
697 return (wxWindow*)gs_focusedWindow;
698 }
699
700 bool wxWindowMGL::Show(bool show)
701 {
702 if ( !wxWindowBase::Show(show) )
703 return FALSE;
704
705 MGL_wmShowWindow(m_wnd, show);
706 return TRUE;
707 }
708
709 // Raise the window to the top of the Z order
710 void wxWindowMGL::Raise()
711 {
712 MGL_wmRaiseWindow(m_wnd);
713 }
714
715 // Lower the window to the bottom of the Z order
716 void wxWindowMGL::Lower()
717 {
718 MGL_wmLowerWindow(m_wnd);
719 }
720
721 void wxWindowMGL::CaptureMouse()
722 {
723 if ( gs_mouseCapture )
724 MGL_wmUncaptureEvents(gs_mouseCapture->m_wnd, wxMGL_CAPTURE_MOUSE);
725
726 gs_mouseCapture = this;
727 MGL_wmCaptureEvents(m_wnd, EVT_MOUSEEVT, wxMGL_CAPTURE_MOUSE);
728 }
729
730 void wxWindowMGL::ReleaseMouse()
731 {
732 wxASSERT_MSG( gs_mouseCapture == this, wxT("attempt to release mouse, but this window hasn't captured it") )
733
734 MGL_wmUncaptureEvents(m_wnd, wxMGL_CAPTURE_MOUSE);
735 gs_mouseCapture = NULL;
736 }
737
738 /* static */ wxWindow *wxWindowBase::GetCapture()
739 {
740 return (wxWindow*)gs_mouseCapture;
741 }
742
743 bool wxWindowMGL::SetCursor(const wxCursor& cursor)
744 {
745 if ( !wxWindowBase::SetCursor(cursor) )
746 {
747 // no change
748 return FALSE;
749 }
750
751 if ( m_cursor.Ok() )
752 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
753 else
754 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
755
756 return TRUE;
757 }
758
759 void wxWindowMGL::WarpPointer(int x, int y)
760 {
761 ClientToScreen(&x, &y);
762 EVT_setMousePos(x, y);
763 }
764
765 #if WXWIN_COMPATIBILITY
766 // If nothing defined for this, try the parent.
767 // E.g. we may be a button loaded from a resource, with no callback function
768 // defined.
769 void wxWindowMGL::OnCommand(wxWindow& win, wxCommandEvent& event)
770 {
771 if ( GetEventHandler()->ProcessEvent(event) )
772 return;
773 if ( m_parent )
774 m_parent->GetEventHandler()->OnCommand(win, event);
775 }
776 #endif // WXWIN_COMPATIBILITY_2
777
778 #if WXWIN_COMPATIBILITY
779 wxObject* wxWindowMGL::GetChild(int number) const
780 {
781 // Return a pointer to the Nth object in the Panel
782 wxNode *node = GetChildren().First();
783 int n = number;
784 while (node && n--)
785 node = node->Next();
786 if ( node )
787 {
788 wxObject *obj = (wxObject *)node->Data();
789 return(obj);
790 }
791 else
792 return NULL;
793 }
794 #endif // WXWIN_COMPATIBILITY
795
796 // Set this window to be the child of 'parent'.
797 bool wxWindowMGL::Reparent(wxWindowBase *parent)
798 {
799 if ( !wxWindowBase::Reparent(parent) )
800 return FALSE;
801
802 MGL_wmReparentWindow(m_wnd, parent->GetHandle());
803
804 return TRUE;
805 }
806
807
808 // ---------------------------------------------------------------------------
809 // drag and drop
810 // ---------------------------------------------------------------------------
811
812 #if wxUSE_DRAG_AND_DROP
813
814 void wxWindowMGL::SetDropTarget(wxDropTarget *pDropTarget)
815 {
816 if ( m_dropTarget != 0 ) {
817 m_dropTarget->Revoke(m_hWnd);
818 delete m_dropTarget;
819 }
820
821 m_dropTarget = pDropTarget;
822 if ( m_dropTarget != 0 )
823 m_dropTarget->Register(m_hWnd);
824 }
825 // FIXME_MGL
826 #endif // wxUSE_DRAG_AND_DROP
827
828 // old style file-manager drag&drop support: we retain the old-style
829 // DragAcceptFiles in parallel with SetDropTarget.
830 void wxWindowMGL::DragAcceptFiles(bool accept)
831 {
832 #if 0 // FIXME_MGL
833 HWND hWnd = GetHwnd();
834 if ( hWnd )
835 ::DragAcceptFiles(hWnd, (BOOL)accept);
836 #endif
837 }
838
839 // ---------------------------------------------------------------------------
840 // moving and resizing
841 // ---------------------------------------------------------------------------
842
843 // Get total size
844 void wxWindowMGL::DoGetSize(int *x, int *y) const
845 {
846 if (x) *x = m_wnd->width;
847 if (y) *y = m_wnd->height;
848 }
849
850 void wxWindowMGL::DoGetPosition(int *x, int *y) const
851 {
852 if (x) *x = m_wnd->x;
853 if (y) *y = m_wnd->y;
854 }
855
856 void wxWindowMGL::DoScreenToClient(int *x, int *y) const
857 {
858 int ax, ay;
859 MGL_wmCoordGlobalToLocal(m_wnd, 0, 0, &ax, &ay);
860 if (x)
861 (*x) += ax;
862 if (y)
863 (*y) += ay;
864 }
865
866 void wxWindowMGL::DoClientToScreen(int *x, int *y) const
867 {
868 int ax, ay;
869 MGL_wmCoordLocalToGlobal(m_wnd, 0, 0, &ax, &ay);
870 if (x)
871 (*x) += ax;
872 if (y)
873 (*y) += ay;
874 }
875
876 // Get size *available for subwindows* i.e. excluding menu bar etc.
877 void wxWindowMGL::DoGetClientSize(int *x, int *y) const
878 {
879 DoGetSize(x, y);
880 }
881
882 void wxWindowMGL::DoMoveWindow(int x, int y, int width, int height)
883 {
884 MGL_wmSetWindowPosition(GetHandle(), x, y, width, height);
885 }
886
887 // set the size of the window: if the dimensions are positive, just use them,
888 // but if any of them is equal to -1, it means that we must find the value for
889 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
890 // which case -1 is a valid value for x and y)
891 //
892 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
893 // the width/height to best suit our contents, otherwise we reuse the current
894 // width/height
895 void wxWindowMGL::DoSetSize(int x, int y, int width, int height, int sizeFlags)
896 {
897 // get the current size and position...
898 int currentX, currentY;
899 GetPosition(&currentX, &currentY);
900 int currentW,currentH;
901 GetSize(&currentW, &currentH);
902
903 // ... and don't do anything (avoiding flicker) if it's already ok
904 if ( x == currentX && y == currentY &&
905 width == currentW && height == currentH )
906 {
907 return;
908 }
909
910 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
911 x = currentX;
912 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
913 y = currentY;
914
915 AdjustForParentClientOrigin(x, y, sizeFlags);
916
917 wxSize size(-1, -1);
918 if ( width == -1 )
919 {
920 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
921 {
922 size = DoGetBestSize();
923 width = size.x;
924 }
925 else
926 {
927 // just take the current one
928 width = currentW;
929 }
930 }
931
932 if ( height == -1 )
933 {
934 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
935 {
936 if ( size.x == -1 )
937 {
938 size = DoGetBestSize();
939 }
940 //else: already called DoGetBestSize() above
941
942 height = size.y;
943 }
944 else
945 {
946 // just take the current one
947 height = currentH;
948 }
949 }
950
951 DoMoveWindow(x, y, width, height);
952
953 wxSizeEvent event(wxSize(width, height), GetId());
954 event.SetEventObject(this);
955 GetEventHandler()->ProcessEvent(event);
956 }
957
958 void wxWindowMGL::DoSetClientSize(int width, int height)
959 {
960 SetSize(width, height);
961 }
962
963 // ---------------------------------------------------------------------------
964 // text metrics
965 // ---------------------------------------------------------------------------
966
967 int wxWindowMGL::GetCharHeight() const
968 {
969 wxScreenDC dc;
970 dc.SetFont(m_font);
971 return dc.GetCharHeight();
972 }
973
974 int wxWindowMGL::GetCharWidth() const
975 {
976 wxScreenDC dc;
977 dc.SetFont(m_font);
978 return dc.GetCharWidth();
979 }
980
981 void wxWindowMGL::GetTextExtent(const wxString& string,
982 int *x, int *y,
983 int *descent, int *externalLeading,
984 const wxFont *theFont) const
985 {
986 wxScreenDC dc;
987 if (!theFont)
988 theFont = &m_font;
989 dc.GetTextExtent(string, x, y, descent, externalLeading, (wxFont*)theFont);
990 }
991
992 #if wxUSE_CARET && WXWIN_COMPATIBILITY
993 // ---------------------------------------------------------------------------
994 // Caret manipulation
995 // ---------------------------------------------------------------------------
996
997 void wxWindowMGL::CreateCaret(int w, int h)
998 {
999 SetCaret(new wxCaret(this, w, h));
1000 }
1001
1002 void wxWindowMGL::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
1003 {
1004 wxFAIL_MSG("not implemented");
1005 }
1006
1007 void wxWindowMGL::ShowCaret(bool show)
1008 {
1009 wxCHECK_RET( m_caret, "no caret to show" );
1010
1011 m_caret->Show(show);
1012 }
1013
1014 void wxWindowMGL::DestroyCaret()
1015 {
1016 SetCaret(NULL);
1017 }
1018
1019 void wxWindowMGL::SetCaretPos(int x, int y)
1020 {
1021 wxCHECK_RET( m_caret, "no caret to move" );
1022
1023 m_caret->Move(x, y);
1024 }
1025
1026 void wxWindowMGL::GetCaretPos(int *x, int *y) const
1027 {
1028 wxCHECK_RET( m_caret, "no caret to get position of" );
1029
1030 m_caret->GetPosition(x, y);
1031 }
1032 #endif // wxUSE_CARET
1033
1034
1035 // ---------------------------------------------------------------------------
1036 // painting
1037 // ---------------------------------------------------------------------------
1038
1039 void wxWindowMGL::Clear()
1040 {
1041 wxClientDC dc((wxWindow *)this);
1042 wxBrush brush(GetBackgroundColour(), wxSOLID);
1043 dc.SetBackground(brush);
1044 dc.Clear();
1045 }
1046
1047 void wxWindowMGL::Refresh(bool eraseBack, const wxRect *rect)
1048 {
1049 if ( m_eraseBackground == -1 )
1050 m_eraseBackground = eraseBack;
1051 else
1052 m_eraseBackground |= eraseBack;
1053
1054 if ( rect )
1055 {
1056 rect_t r;
1057 r.left = rect->GetLeft(), r.right = rect->GetRight();
1058 r.top = rect->GetTop(), r.bottom = rect->GetBottom();
1059 MGL_wmInvalidateWindowRect(GetHandle(), &r);
1060 }
1061 else
1062 MGL_wmInvalidateWindow(GetHandle());
1063 }
1064
1065 void wxWindowMGL::Update()
1066 {
1067 if ( !m_frozen )
1068 MGL_wmUpdateDC(g_winMng);
1069 }
1070
1071 void wxWindowMGL::Freeze()
1072 {
1073 m_frozen = TRUE;
1074 m_refreshAfterThaw = FALSE;
1075 }
1076
1077 void wxWindowMGL::Thaw()
1078 {
1079 m_frozen = FALSE;
1080 if ( m_refreshAfterThaw )
1081 Refresh();
1082 }
1083
1084 void wxWindowMGL::HandlePaint(MGLDevCtx *dc)
1085 {
1086 if ( m_frozen )
1087 {
1088 // Don't paint anything if the window is frozen.
1089 m_refreshAfterThaw = TRUE;
1090 return;
1091 }
1092
1093 MGLRegion clip;
1094 dc->getClipRegion(clip);
1095 m_updateRegion = wxRegion(clip);
1096 m_paintMGLDC = dc;
1097
1098 if ( m_eraseBackground != 0 )
1099 {
1100 wxWindowDC dc((wxWindow*)this);
1101 wxEraseEvent eventEr(m_windowId, &dc);
1102 eventEr.SetEventObject(this);
1103 GetEventHandler()->ProcessEvent(eventEr);
1104 }
1105 m_eraseBackground = -1;
1106
1107 wxNcPaintEvent eventNc(GetId());
1108 eventNc.SetEventObject(this);
1109 GetEventHandler()->ProcessEvent(eventNc);
1110
1111 wxPaintEvent eventPt(GetId());
1112 eventPt.SetEventObject(this);
1113 GetEventHandler()->ProcessEvent(eventPt);
1114
1115 m_paintMGLDC = NULL;
1116 m_updateRegion.Clear();
1117 }
1118
1119
1120 // Find the wxWindow at the current mouse position, returning the mouse
1121 // position.
1122 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1123 {
1124 return wxFindWindowAtPoint(pt = wxGetMousePosition());
1125 }
1126
1127 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
1128 {
1129 window_t *wnd = MGL_wmGetWindowAtPosition(g_winMng, pt.x, pt.y);
1130 return (wxWindow*)wnd->userData;
1131 }