]> git.saurik.com Git - wxWidgets.git/blob - src/mac/toplevel.cpp
wxFont can now raelly use the native fonts
[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 MacSuperEnabled( inIsActivating ) ;
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 }