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