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