]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
checklist implementation
[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
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::MacGetPortParams(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;
289 clipRect->bottom = m_height;
290 *window = m_macWindow ;
291 *rootwin = this ;
292 }
293
294 void wxTopLevelWindowMac::Clear()
295 {
296 wxWindow::Clear() ;
297 }
298
299 ControlHandle wxTopLevelWindowMac::MacGetContainerForEmbedding()
300 {
301 return m_macRootControl ;
302 }
303
304
305 void wxTopLevelWindowMac::MacUpdate( long timestamp)
306 {
307 #if TARGET_CARBON
308 AGAPortHelper help( GetWindowPort(m_macWindow) ) ;
309 #else
310 AGAPortHelper help( (m_macWindow) ) ;
311 #endif
312 BeginUpdate( m_macWindow ) ;
313
314 RgnHandle updateRgn = NewRgn();
315 RgnHandle diffRgn = NewRgn() ;
316 if ( updateRgn && diffRgn )
317 {
318 GetPortVisibleRegion( GetWindowPort( m_macWindow ), updateRgn );
319 DiffRgn( updateRgn , m_macNoEraseUpdateRgn , diffRgn ) ;
320 if ( !EmptyRgn( updateRgn ) )
321 {
322 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
323 }
324 }
325 if ( updateRgn )
326 DisposeRgn( updateRgn );
327 if ( diffRgn )
328 DisposeRgn( diffRgn );
329 EndUpdate( m_macWindow ) ;
330 SetEmptyRgn( m_macNoEraseUpdateRgn ) ;
331 m_macNeedsErasing = false ;
332 }
333
334
335 // Raise the window to the top of the Z order
336 void wxTopLevelWindowMac::Raise()
337 {
338 ::BringToFront( m_macWindow ) ;
339 }
340
341 // Lower the window to the bottom of the Z order
342 void wxTopLevelWindowMac::Lower()
343 {
344 ::SendBehind( m_macWindow , NULL ) ;
345 }
346
347 Point lastWhere ;
348 long lastWhen = 0 ;
349 extern int wxBusyCursorCount ;
350
351 void wxTopLevelWindowMac::MacFireMouseEvent( EventRecord *ev )
352 {
353 wxMouseEvent event(wxEVT_LEFT_DOWN);
354 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
355 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
356
357 event.m_leftDown = isDown && !controlDown;
358
359 event.m_middleDown = FALSE;
360 event.m_rightDown = isDown && controlDown;
361
362 if ( ev->what == mouseDown )
363 {
364 if ( controlDown )
365 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
366 else
367 event.SetEventType(wxEVT_LEFT_DOWN ) ;
368 }
369 else if ( ev->what == mouseUp )
370 {
371 if ( controlDown )
372 event.SetEventType(wxEVT_RIGHT_UP ) ;
373 else
374 event.SetEventType(wxEVT_LEFT_UP ) ;
375 }
376 else
377 {
378 event.SetEventType(wxEVT_MOTION ) ;
379 }
380
381 event.m_shiftDown = ev->modifiers & shiftKey;
382 event.m_controlDown = ev->modifiers & controlKey;
383 event.m_altDown = ev->modifiers & optionKey;
384 event.m_metaDown = ev->modifiers & cmdKey;
385
386 Point localwhere = ev->where ;
387
388 GrafPtr port ;
389 ::GetPort( &port ) ;
390 ::SetPort( UMAGetWindowPort( m_macWindow ) ) ;
391 ::GlobalToLocal( &localwhere ) ;
392 ::SetPort( port ) ;
393
394 if ( ev->what == mouseDown )
395 {
396 if ( ev->when - lastWhen <= GetDblTime() )
397 {
398 if ( abs( localwhere.h - lastWhere.h ) < 3 || abs( localwhere.v - lastWhere.v ) < 3 )
399 {
400 if ( controlDown )
401 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
402 else
403 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
404 }
405 lastWhen = 0 ;
406 }
407 else
408 {
409 lastWhen = ev->when ;
410 }
411 lastWhere = localwhere ;
412 }
413
414 event.m_x = localwhere.h;
415 event.m_y = localwhere.v;
416 event.m_x += m_x;
417 event.m_y += m_y;
418
419 /*
420 wxPoint origin = GetClientAreaOrigin() ;
421
422 event.m_x += origin.x ;
423 event.m_y += origin.y ;
424 */
425
426 event.m_timeStamp = ev->when;
427 event.SetEventObject(this);
428 if ( wxTheApp->s_captureWindow )
429 {
430 int x = event.m_x ;
431 int y = event.m_y ;
432 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
433 event.m_x = x ;
434 event.m_y = y ;
435 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
436 if ( ev->what == mouseUp )
437 {
438 wxTheApp->s_captureWindow = NULL ;
439 if ( wxBusyCursorCount == 0 )
440 {
441 m_cursor.MacInstall() ;
442 }
443 }
444 }
445 else
446 {
447 MacDispatchMouseEvent( event ) ;
448 }
449 }
450
451 void wxTopLevelWindowMac::MacMouseDown( EventRecord *ev , short part)
452 {
453 MacFireMouseEvent( ev ) ;
454 }
455
456 void wxTopLevelWindowMac::MacMouseUp( EventRecord *ev , short part)
457 {
458 switch (part)
459 {
460 case inContent:
461 {
462 MacFireMouseEvent( ev ) ;
463 }
464 break ;
465 }
466 }
467
468 void wxTopLevelWindowMac::MacMouseMoved( EventRecord *ev , short part)
469 {
470 switch (part)
471 {
472 case inContent:
473 {
474 MacFireMouseEvent( ev ) ;
475 }
476 break ;
477 }
478 }
479 void wxTopLevelWindowMac::MacActivate( EventRecord *ev , bool inIsActivating )
480 {
481 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
482 event.m_timeStamp = ev->when ;
483 event.SetEventObject(this);
484
485 GetEventHandler()->ProcessEvent(event);
486
487 UMAHighlightAndActivateWindow( m_macWindow , inIsActivating ) ;
488
489 MacSuperEnabled( inIsActivating ) ;
490 }
491
492 void wxTopLevelWindowMac::MacKeyDown( EventRecord *ev )
493 {
494 }
495
496 void wxTopLevelWindowMac::SetTitle(const wxString& title)
497 {
498 wxWindow::SetTitle( title ) ;
499
500 wxString label ;
501
502 if( wxApp::s_macDefaultEncodingIsPC )
503 label = wxMacMakeMacStringFromPC( m_label ) ;
504 else
505 label = m_label ;
506
507 UMASetWTitleC( m_macWindow , label ) ;
508 }
509
510 bool wxTopLevelWindowMac::Show(bool show)
511 {
512 if ( !wxWindow::Show(show) )
513 return FALSE;
514
515 if (show)
516 {
517 ::ShowWindow( m_macWindow ) ;
518 ::SelectWindow( m_macWindow ) ;
519 // no need to generate events here, they will get them triggered by macos
520 // actually they should be , but apparently they are not
521 wxSize size(m_width, m_height);
522 wxSizeEvent event(size, m_windowId);
523 event.SetEventObject(this);
524 GetEventHandler()->ProcessEvent(event);
525 }
526 else
527 {
528 ::HideWindow( m_macWindow ) ;
529 }
530
531 if ( !show )
532 {
533 }
534 else
535 {
536 Refresh() ;
537 }
538
539 return TRUE;
540 }
541
542 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
543 {
544 int former_x = m_x ;
545 int former_y = m_y ;
546 int former_w = m_width ;
547 int former_h = m_height ;
548
549 int actualWidth = width;
550 int actualHeight = height;
551 int actualX = x;
552 int actualY = y;
553
554 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
555 actualWidth = m_minWidth;
556 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
557 actualHeight = m_minHeight;
558 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
559 actualWidth = m_maxWidth;
560 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
561 actualHeight = m_maxHeight;
562
563 bool doMove = false ;
564 bool doResize = false ;
565
566 if ( actualX != former_x || actualY != former_y )
567 {
568 doMove = true ;
569 }
570 if ( actualWidth != former_w || actualHeight != former_h )
571 {
572 doResize = true ;
573 }
574
575 if ( doMove || doResize )
576 {
577 m_x = actualX ;
578 m_y = actualY ;
579 m_width = actualWidth ;
580 m_height = actualHeight ;
581
582 if ( doMove )
583 ::MoveWindow(m_macWindow, m_x, m_y , false); // don't make frontmost
584
585 if ( doResize )
586 ::SizeWindow(m_macWindow, m_width, m_height , true);
587
588 // the OS takes care of invalidating and erasing the new area
589 // we have erased the old one
590
591 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
592 {
593 wxFrame* frame = (wxFrame*) this ;
594 frame->PositionStatusBar();
595 frame->PositionToolBar();
596 }
597 if ( doMove )
598 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
599
600 MacRepositionScrollBars() ;
601 if ( doMove )
602 {
603 wxPoint point(m_x, m_y);
604 wxMoveEvent event(point, m_windowId);
605 event.SetEventObject(this);
606 GetEventHandler()->ProcessEvent(event) ;
607 }
608 if ( doResize )
609 {
610 MacRepositionScrollBars() ;
611 wxSize size(m_width, m_height);
612 wxSizeEvent event(size, m_windowId);
613 event.SetEventObject(this);
614 GetEventHandler()->ProcessEvent(event);
615 }
616 }
617
618 }
619
620 /*
621 * Invalidation Mechanism
622 *
623 * The update mechanism reflects exactely the windows mechanism
624 * the rect gets added to the window invalidate region, if the eraseBackground flag
625 * has been true for any part of the update rgn the background is erased in the entire region
626 * not just in the specified rect.
627 *
628 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
629 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
630 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
631 * will get the eraseBackground event first
632 */
633
634 void wxTopLevelWindowMac::MacInvalidate( const Rect * rect, bool eraseBackground )
635 {
636 GrafPtr formerPort ;
637 GetPort( &formerPort ) ;
638 SetPortWindowPort( m_macWindow ) ;
639
640 m_macNeedsErasing |= eraseBackground ;
641
642 // if we already know that we will have to erase, there's no need to track the rest
643 if ( !m_macNeedsErasing)
644 {
645 // we end only here if eraseBackground is false
646 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
647 // we will have to erase anyway
648
649 RgnHandle updateRgn = NewRgn();
650 RgnHandle diffRgn = NewRgn() ;
651 if ( updateRgn && diffRgn )
652 {
653 GetWindowUpdateRgn( m_macWindow , updateRgn );
654 Point pt = {0,0} ;
655 LocalToGlobal( &pt ) ;
656 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
657 DiffRgn( updateRgn , m_macNoEraseUpdateRgn , diffRgn ) ;
658 if ( !EmptyRgn( diffRgn ) )
659 {
660 m_macNeedsErasing = true ;
661 }
662 }
663 if ( updateRgn )
664 DisposeRgn( updateRgn );
665 if ( diffRgn )
666 DisposeRgn( diffRgn );
667
668 if ( !m_macNeedsErasing )
669 {
670 RgnHandle rectRgn = NewRgn() ;
671 SetRectRgn( rectRgn , rect->left , rect->top , rect->right , rect->bottom ) ;
672 UnionRgn( m_macNoEraseUpdateRgn , rectRgn , m_macNoEraseUpdateRgn ) ;
673 DisposeRgn( rectRgn ) ;
674 }
675 }
676 InvalWindowRect( m_macWindow , rect ) ;
677 // turn this on to debug the refreshing cycle
678 #if wxMAC_DEBUG_REDRAW
679 PaintRect( rect ) ;
680 #endif
681 SetPort( formerPort ) ;
682 }