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