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