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