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