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