]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
use UNIX wxDir if available
[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(WXWindow 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(WXWindow 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 WXHWND 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( (WindowRef) 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( (RgnHandle) 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 , (WindowRef*)&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( (WindowRef)m_macWindow , label ) ;
278 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
279
280 m_macFocus = NULL ;
281 }
282
283 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXHWND *window , wxWindowMac** rootwin)
284 {
285 ((Point*)localOrigin)->h = 0;
286 ((Point*)localOrigin)->v = 0;
287 ((Rect*)clipRect)->left = 0;
288 ((Rect*)clipRect)->top = 0;
289 ((Rect*)clipRect)->right = m_width;
290 ((Rect*)clipRect)->bottom = m_height;
291 *window = m_macWindow ;
292 *rootwin = this ;
293 }
294
295 void wxTopLevelWindowMac::Clear()
296 {
297 wxWindow::Clear() ;
298 }
299
300 WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
301 {
302 return m_macRootControl ;
303 }
304
305
306 void wxTopLevelWindowMac::MacUpdate( long timestamp)
307 {
308
309 wxMacPortStateHelper help( GetWindowPort( (WindowRef) m_macWindow) ) ;
310
311 BeginUpdate( (WindowRef)m_macWindow ) ;
312
313 RgnHandle updateRgn = NewRgn();
314 RgnHandle diffRgn = NewRgn() ;
315 if ( updateRgn && diffRgn )
316 {
317 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
318 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
319 if ( !EmptyRgn( updateRgn ) )
320 {
321 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
322 }
323 }
324 if ( updateRgn )
325 DisposeRgn( updateRgn );
326 if ( diffRgn )
327 DisposeRgn( diffRgn );
328 EndUpdate( (WindowRef)m_macWindow ) ;
329 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
330 m_macNeedsErasing = false ;
331 }
332
333
334 // Raise the window to the top of the Z order
335 void wxTopLevelWindowMac::Raise()
336 {
337 ::BringToFront( (WindowRef)m_macWindow ) ;
338 }
339
340 // Lower the window to the bottom of the Z order
341 void wxTopLevelWindowMac::Lower()
342 {
343 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
344 }
345
346 Point lastWhere ;
347 long lastWhen = 0 ;
348 extern int wxBusyCursorCount ;
349
350 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr )
351 {
352 EventRecord *ev = (EventRecord*) evr ;
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( (WindowRef)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( WXEVENTREF ev , short part)
452 {
453 MacFireMouseEvent( ev ) ;
454 }
455
456 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF 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( WXEVENTREF ev , short part)
469 {
470 switch (part)
471 {
472 case inContent:
473 {
474 MacFireMouseEvent( ev ) ;
475 }
476 break ;
477 }
478 }
479 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev , bool inIsActivating )
480 {
481 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
482 event.m_timeStamp = ((EventRecord*)ev)->when ;
483 event.SetEventObject(this);
484
485 GetEventHandler()->ProcessEvent(event);
486
487 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
488
489 MacSuperEnabled( inIsActivating ) ;
490 }
491
492 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF 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( (WindowRef)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( (WindowRef)m_macWindow ) ;
518 ::SelectWindow( (WindowRef)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( (WindowRef)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((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
584
585 if ( doResize )
586 ::SizeWindow((WindowRef)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 WXRECTPTR rect, bool eraseBackground )
635 {
636 GrafPtr formerPort ;
637 GetPort( &formerPort ) ;
638 SetPortWindowPort( (WindowRef)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( (WindowRef)m_macWindow , updateRgn );
654 Point pt = {0,0} ;
655 LocalToGlobal( &pt ) ;
656 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
657 DiffRgn( updateRgn , (RgnHandle) 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*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
672 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
673 DisposeRgn( rectRgn ) ;
674 }
675 }
676 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
677 // turn this on to debug the refreshing cycle
678 #if wxMAC_DEBUG_REDRAW
679 PaintRect( rect ) ;
680 #endif
681 SetPort( formerPort ) ;
682 }
683