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