]> git.saurik.com Git - wxWidgets.git/blob - src/mac/toplevel.cpp
since DoScreenToClient and ClientToScreen in toplvlcmn are implemented by calling...
[wxWidgets.git] / src / mac / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "toplevel.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/frame.h"
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38 #endif //WX_PRECOMP
39
40 #include "wx/mac/uma.h"
41 #include "wx/mac/aga.h"
42 #include "wx/tooltip.h"
43 #include "wx/dnd.h"
44
45 #define wxMAC_DEBUG_REDRAW 0
46 #ifndef wxMAC_DEBUG_REDRAW
47 #define wxMAC_DEBUG_REDRAW 0
48 #endif
49
50 // ----------------------------------------------------------------------------
51 // globals
52 // ----------------------------------------------------------------------------
53
54 // list of all frames and modeless dialogs
55 wxWindowList wxModelessWindows;
56
57 // ============================================================================
58 // wxTopLevelWindowMac implementation
59 // ============================================================================
60
61 // ---------------------------------------------------------------------------
62 // wxWindowMac utility functions
63 // ---------------------------------------------------------------------------
64
65 // Find an item given the Macintosh Window Reference
66
67 wxList *wxWinMacWindowList = NULL;
68 wxTopLevelWindowMac *wxFindWinFromMacWindow(WXWindow inWindowRef)
69 {
70 wxNode *node = wxWinMacWindowList->Find((long)inWindowRef);
71 if (!node)
72 return NULL;
73 return (wxTopLevelWindowMac *)node->Data();
74 }
75
76 void wxAssociateWinWithMacWindow(WXWindow inWindowRef, wxTopLevelWindowMac *win)
77 {
78 // adding NULL WindowRef is (first) surely a result of an error and
79 // (secondly) breaks menu command processing
80 wxCHECK_RET( inWindowRef != (WindowRef) NULL, "attempt to add a NULL WindowRef to window list" );
81
82 if ( !wxWinMacWindowList->Find((long)inWindowRef) )
83 wxWinMacWindowList->Append((long)inWindowRef, win);
84 }
85
86 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
87 {
88 wxWinMacWindowList->DeleteObject(win);
89 }
90
91
92 // ----------------------------------------------------------------------------
93 // wxTopLevelWindowMac creation
94 // ----------------------------------------------------------------------------
95
96 WXHWND wxTopLevelWindowMac::s_macWindowInUpdate = NULL;
97
98 void wxTopLevelWindowMac::Init()
99 {
100 m_iconized =
101 m_maximizeOnShow = FALSE;
102 m_macNoEraseUpdateRgn = NewRgn() ;
103 m_macNeedsErasing = false ;
104 m_macWindow = NULL ;
105 }
106
107 bool wxTopLevelWindowMac::Create(wxWindow *parent,
108 wxWindowID id,
109 const wxString& title,
110 const wxPoint& pos,
111 const wxSize& size,
112 long style,
113 const wxString& name)
114 {
115 // init our fields
116 Init();
117
118 m_windowStyle = style;
119
120 SetName(name);
121
122 m_windowId = id == -1 ? NewControlId() : id;
123
124 wxTopLevelWindows.Append(this);
125
126 if ( parent )
127 parent->AddChild(this);
128
129 return TRUE;
130 }
131
132 wxTopLevelWindowMac::~wxTopLevelWindowMac()
133 {
134 if ( m_macWindow )
135 {
136 wxToolTip::NotifyWindowDelete(m_macWindow) ;
137 UMADisposeWindow( (WindowRef) m_macWindow ) ;
138 }
139
140 wxRemoveMacWindowAssociation( this ) ;
141
142 wxTopLevelWindows.DeleteObject(this);
143
144 if ( wxModelessWindows.Find(this) )
145 wxModelessWindows.DeleteObject(this);
146
147 // If this is the last top-level window, exit.
148 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
149 {
150 wxTheApp->SetTopWindow(NULL);
151
152 if ( wxTheApp->GetExitOnFrameDelete() )
153 {
154 wxTheApp->ExitMainLoop() ;
155 }
156 }
157 DisposeRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
158 }
159
160
161 // ----------------------------------------------------------------------------
162 // wxTopLevelWindowMac maximize/minimize
163 // ----------------------------------------------------------------------------
164
165 void wxTopLevelWindowMac::Maximize(bool maximize)
166 {
167 // not available on mac
168 }
169
170 bool wxTopLevelWindowMac::IsMaximized() const
171 {
172 return false ;
173 }
174
175 void wxTopLevelWindowMac::Iconize(bool iconize)
176 {
177 // not available on mac
178 }
179
180 bool wxTopLevelWindowMac::IsIconized() const
181 {
182 // mac dialogs cannot be iconized
183 return FALSE;
184 }
185
186 void wxTopLevelWindowMac::Restore()
187 {
188 // not available on mac
189 }
190
191 // ----------------------------------------------------------------------------
192 // wxTopLevelWindowMac misc
193 // ----------------------------------------------------------------------------
194
195 void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
196 {
197 // this sets m_icon
198 wxTopLevelWindowBase::SetIcon(icon);
199 }
200
201 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
202 const wxPoint& pos,
203 const wxSize& size,
204 long style,
205 const wxString& name )
206 {
207 SetName(name);
208 m_windowStyle = style;
209 m_isShown = FALSE;
210
211 // create frame.
212
213 Rect theBoundsRect;
214
215 m_x = (int)pos.x;
216 m_y = (int)pos.y;
217 if ( m_y < 50 )
218 m_y = 50 ;
219 if ( m_x < 20 )
220 m_x = 20 ;
221
222 m_width = size.x;
223 if (m_width == -1)
224 m_width = 20;
225 m_height = size.y;
226 if (m_height == -1)
227 m_height = 20;
228
229 ::SetRect(&theBoundsRect, m_x, m_y , m_x + m_width, m_y + m_height);
230
231 // translate the window attributes in the appropriate window class and attributes
232
233 WindowClass wclass = 0;
234 WindowAttributes attr = kWindowNoAttributes ;
235
236 if ( HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT) )
237 {
238 wclass = kFloatingWindowClass ;
239 if ( HasFlag(wxTINY_CAPTION_VERT) )
240 {
241 attr |= kWindowSideTitlebarAttribute ;
242 }
243 }
244 else if ( HasFlag( wxCAPTION ) )
245 {
246 if ( HasFlag( wxDIALOG_MODAL ) )
247 {
248 wclass = kMovableModalWindowClass ;
249 }
250 else
251 {
252 wclass = kDocumentWindowClass ;
253 }
254 }
255 else
256 {
257 wclass = kModalWindowClass ;
258 }
259
260 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) )
261 {
262 attr |= kWindowFullZoomAttribute ;
263 attr |= kWindowCollapseBoxAttribute ;
264 }
265 if ( HasFlag( wxRESIZE_BORDER ) )
266 {
267 attr |= kWindowResizableAttribute ;
268 }
269 if ( HasFlag( wxSYSTEM_MENU ) )
270 {
271 attr |= kWindowCloseBoxAttribute ;
272 }
273
274 ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
275 wxAssociateWinWithMacWindow( m_macWindow , this ) ;
276 wxString label ;
277 if( wxApp::s_macDefaultEncodingIsPC )
278 label = wxMacMakeMacStringFromPC( title ) ;
279 else
280 label = title ;
281 UMASetWTitleC( (WindowRef)m_macWindow , label ) ;
282 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
283
284 m_macFocus = NULL ;
285 }
286
287 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXHWND *window , wxWindowMac** rootwin)
288 {
289 ((Point*)localOrigin)->h = 0;
290 ((Point*)localOrigin)->v = 0;
291 ((Rect*)clipRect)->left = 0;
292 ((Rect*)clipRect)->top = 0;
293 ((Rect*)clipRect)->right = m_width;
294 ((Rect*)clipRect)->bottom = m_height;
295 *window = m_macWindow ;
296 *rootwin = this ;
297 }
298
299 void wxTopLevelWindowMac::Clear()
300 {
301 wxWindow::Clear() ;
302 }
303
304 WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
305 {
306 return m_macRootControl ;
307 }
308
309
310 void wxTopLevelWindowMac::MacUpdate( long timestamp)
311 {
312
313 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
314
315 BeginUpdate( (WindowRef)m_macWindow ) ;
316
317 RgnHandle updateRgn = NewRgn();
318 RgnHandle diffRgn = NewRgn() ;
319 if ( updateRgn && diffRgn )
320 {
321 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
322 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
323 if ( !EmptyRgn( updateRgn ) )
324 {
325 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
326 }
327 }
328 if ( updateRgn )
329 DisposeRgn( updateRgn );
330 if ( diffRgn )
331 DisposeRgn( diffRgn );
332 EndUpdate( (WindowRef)m_macWindow ) ;
333 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
334 m_macNeedsErasing = false ;
335 }
336
337
338 // Raise the window to the top of the Z order
339 void wxTopLevelWindowMac::Raise()
340 {
341 ::BringToFront( (WindowRef)m_macWindow ) ;
342 }
343
344 // Lower the window to the bottom of the Z order
345 void wxTopLevelWindowMac::Lower()
346 {
347 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
348 }
349
350 Point lastWhere ;
351 long lastWhen = 0 ;
352 extern int wxBusyCursorCount ;
353
354 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr )
355 {
356 EventRecord *ev = (EventRecord*) evr ;
357 wxMouseEvent event(wxEVT_LEFT_DOWN);
358 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
359 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
360
361 event.m_leftDown = isDown && !controlDown;
362
363 event.m_middleDown = FALSE;
364 event.m_rightDown = isDown && controlDown;
365
366 if ( ev->what == mouseDown )
367 {
368 if ( controlDown )
369 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
370 else
371 event.SetEventType(wxEVT_LEFT_DOWN ) ;
372 }
373 else if ( ev->what == mouseUp )
374 {
375 if ( controlDown )
376 event.SetEventType(wxEVT_RIGHT_UP ) ;
377 else
378 event.SetEventType(wxEVT_LEFT_UP ) ;
379 }
380 else
381 {
382 event.SetEventType(wxEVT_MOTION ) ;
383 }
384
385 event.m_shiftDown = ev->modifiers & shiftKey;
386 event.m_controlDown = ev->modifiers & controlKey;
387 event.m_altDown = ev->modifiers & optionKey;
388 event.m_metaDown = ev->modifiers & cmdKey;
389
390 Point localwhere = ev->where ;
391
392 GrafPtr port ;
393 ::GetPort( &port ) ;
394 ::SetPort( UMAGetWindowPort( (WindowRef)m_macWindow ) ) ;
395 ::GlobalToLocal( &localwhere ) ;
396 ::SetPort( port ) ;
397
398 if ( ev->what == mouseDown )
399 {
400 if ( ev->when - lastWhen <= GetDblTime() )
401 {
402 if ( abs( localwhere.h - lastWhere.h ) < 3 || abs( localwhere.v - lastWhere.v ) < 3 )
403 {
404 if ( controlDown )
405 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
406 else
407 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
408 }
409 lastWhen = 0 ;
410 }
411 else
412 {
413 lastWhen = ev->when ;
414 }
415 lastWhere = localwhere ;
416 }
417
418 event.m_x = localwhere.h;
419 event.m_y = localwhere.v;
420 event.m_x += m_x;
421 event.m_y += m_y;
422
423 /*
424 wxPoint origin = GetClientAreaOrigin() ;
425
426 event.m_x += origin.x ;
427 event.m_y += origin.y ;
428 */
429
430 event.m_timeStamp = ev->when;
431 event.SetEventObject(this);
432 if ( wxTheApp->s_captureWindow )
433 {
434 int x = event.m_x ;
435 int y = event.m_y ;
436 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
437 event.m_x = x ;
438 event.m_y = y ;
439 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
440 if ( ev->what == mouseUp )
441 {
442 wxTheApp->s_captureWindow = NULL ;
443 if ( wxBusyCursorCount == 0 )
444 {
445 m_cursor.MacInstall() ;
446 }
447 }
448 }
449 else
450 {
451 MacDispatchMouseEvent( event ) ;
452 }
453 }
454
455 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev , short part)
456 {
457 MacFireMouseEvent( ev ) ;
458 }
459
460 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev , short part)
461 {
462 switch (part)
463 {
464 case inContent:
465 {
466 MacFireMouseEvent( ev ) ;
467 }
468 break ;
469 }
470 }
471
472 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev , short part)
473 {
474 switch (part)
475 {
476 case inContent:
477 {
478 MacFireMouseEvent( ev ) ;
479 }
480 break ;
481 }
482 }
483 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev , bool inIsActivating )
484 {
485 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
486 event.m_timeStamp = ((EventRecord*)ev)->when ;
487 event.SetEventObject(this);
488
489 GetEventHandler()->ProcessEvent(event);
490
491 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
492
493 MacSuperEnabled( inIsActivating ) ;
494 }
495
496 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev )
497 {
498 }
499
500 void wxTopLevelWindowMac::SetTitle(const wxString& title)
501 {
502 wxWindow::SetTitle( title ) ;
503
504 wxString label ;
505
506 if( wxApp::s_macDefaultEncodingIsPC )
507 label = wxMacMakeMacStringFromPC( m_label ) ;
508 else
509 label = m_label ;
510
511 UMASetWTitleC( (WindowRef)m_macWindow , label ) ;
512 }
513
514 bool wxTopLevelWindowMac::Show(bool show)
515 {
516 if ( !wxWindow::Show(show) )
517 return FALSE;
518
519 if (show)
520 {
521 ::ShowWindow( (WindowRef)m_macWindow ) ;
522 ::SelectWindow( (WindowRef)m_macWindow ) ;
523 // no need to generate events here, they will get them triggered by macos
524 // actually they should be , but apparently they are not
525 wxSize size(m_width, m_height);
526 wxSizeEvent event(size, m_windowId);
527 event.SetEventObject(this);
528 GetEventHandler()->ProcessEvent(event);
529 }
530 else
531 {
532 ::HideWindow( (WindowRef)m_macWindow ) ;
533 }
534
535 if ( !show )
536 {
537 }
538 else
539 {
540 Refresh() ;
541 }
542
543 return TRUE;
544 }
545
546 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
547 {
548 int former_x = m_x ;
549 int former_y = m_y ;
550 int former_w = m_width ;
551 int former_h = m_height ;
552
553 int actualWidth = width;
554 int actualHeight = height;
555 int actualX = x;
556 int actualY = y;
557
558 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
559 actualWidth = m_minWidth;
560 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
561 actualHeight = m_minHeight;
562 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
563 actualWidth = m_maxWidth;
564 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
565 actualHeight = m_maxHeight;
566
567 bool doMove = false ;
568 bool doResize = false ;
569
570 if ( actualX != former_x || actualY != former_y )
571 {
572 doMove = true ;
573 }
574 if ( actualWidth != former_w || actualHeight != former_h )
575 {
576 doResize = true ;
577 }
578
579 if ( doMove || doResize )
580 {
581 m_x = actualX ;
582 m_y = actualY ;
583 m_width = actualWidth ;
584 m_height = actualHeight ;
585
586 if ( doMove )
587 ::MoveWindow((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
588
589 if ( doResize )
590 ::SizeWindow((WindowRef)m_macWindow, m_width, m_height , true);
591
592 // the OS takes care of invalidating and erasing the new area
593 // we have erased the old one
594
595 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
596 {
597 wxFrame* frame = (wxFrame*) this ;
598 frame->PositionStatusBar();
599 frame->PositionToolBar();
600 }
601 if ( doMove )
602 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
603
604 MacRepositionScrollBars() ;
605 if ( doMove )
606 {
607 wxPoint point(m_x, m_y);
608 wxMoveEvent event(point, m_windowId);
609 event.SetEventObject(this);
610 GetEventHandler()->ProcessEvent(event) ;
611 }
612 if ( doResize )
613 {
614 MacRepositionScrollBars() ;
615 wxSize size(m_width, m_height);
616 wxSizeEvent event(size, m_windowId);
617 event.SetEventObject(this);
618 GetEventHandler()->ProcessEvent(event);
619 }
620 }
621
622 }
623
624 /*
625 * Invalidation Mechanism
626 *
627 * The update mechanism reflects exactely the windows mechanism
628 * the rect gets added to the window invalidate region, if the eraseBackground flag
629 * has been true for any part of the update rgn the background is erased in the entire region
630 * not just in the specified rect.
631 *
632 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
633 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
634 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
635 * will get the eraseBackground event first
636 */
637
638 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect, bool eraseBackground )
639 {
640 GrafPtr formerPort ;
641 GetPort( &formerPort ) ;
642 SetPortWindowPort( (WindowRef)m_macWindow ) ;
643
644 m_macNeedsErasing |= eraseBackground ;
645
646 // if we already know that we will have to erase, there's no need to track the rest
647 if ( !m_macNeedsErasing)
648 {
649 // we end only here if eraseBackground is false
650 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
651 // we will have to erase anyway
652
653 RgnHandle updateRgn = NewRgn();
654 RgnHandle diffRgn = NewRgn() ;
655 if ( updateRgn && diffRgn )
656 {
657 GetWindowUpdateRgn( (WindowRef)m_macWindow , updateRgn );
658 Point pt = {0,0} ;
659 LocalToGlobal( &pt ) ;
660 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
661 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
662 if ( !EmptyRgn( diffRgn ) )
663 {
664 m_macNeedsErasing = true ;
665 }
666 }
667 if ( updateRgn )
668 DisposeRgn( updateRgn );
669 if ( diffRgn )
670 DisposeRgn( diffRgn );
671
672 if ( !m_macNeedsErasing )
673 {
674 RgnHandle rectRgn = NewRgn() ;
675 SetRectRgn( rectRgn , ((Rect*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
676 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
677 DisposeRgn( rectRgn ) ;
678 }
679 }
680 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
681 // turn this on to debug the refreshing cycle
682 #if wxMAC_DEBUG_REDRAW
683 PaintRect( rect ) ;
684 #endif
685 SetPort( formerPort ) ;
686 }
687