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