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