]> git.saurik.com Git - wxWidgets.git/blob - src/mac/toplevel.cpp
Fixed copyrights and licence spelling
[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 licence
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/app.h"
43 #include "wx/tooltip.h"
44 #include "wx/dnd.h"
45
46 #define wxMAC_DEBUG_REDRAW 0
47 #ifndef wxMAC_DEBUG_REDRAW
48 #define wxMAC_DEBUG_REDRAW 0
49 #endif
50
51 // ----------------------------------------------------------------------------
52 // globals
53 // ----------------------------------------------------------------------------
54
55 // list of all frames and modeless dialogs
56 wxWindowList wxModelessWindows;
57
58 // double click testing
59 static Point gs_lastWhere;
60 static long gs_lastWhen = 0;
61
62 // ============================================================================
63 // wxTopLevelWindowMac implementation
64 // ============================================================================
65
66 // ---------------------------------------------------------------------------
67 // Carbon Events
68 // ---------------------------------------------------------------------------
69
70 #if TARGET_CARBON
71
72 extern long wxMacTranslateKey(unsigned char key, unsigned char code) ;
73
74 static const EventTypeSpec eventList[] =
75 {
76 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
77
78 { kEventClassKeyboard, kEventRawKeyDown } ,
79 { kEventClassKeyboard, kEventRawKeyRepeat } ,
80 { kEventClassKeyboard, kEventRawKeyUp } ,
81 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
82
83 { kEventClassWindow , kEventWindowUpdate } ,
84 { kEventClassWindow , kEventWindowActivated } ,
85 { kEventClassWindow , kEventWindowDeactivated } ,
86 { kEventClassWindow , kEventWindowBoundsChanging } ,
87 { kEventClassWindow , kEventWindowBoundsChanged } ,
88 { kEventClassWindow , kEventWindowClose } ,
89
90 { kEventClassMouse , kEventMouseDown } ,
91 { kEventClassMouse , kEventMouseUp } ,
92 { kEventClassMouse , kEventMouseMoved } ,
93 { kEventClassMouse , kEventMouseDragged } ,
94
95 } ;
96
97 static pascal OSStatus TextInputEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
98 {
99 OSStatus result = eventNotHandledErr ;
100
101 wxWindow* focus = wxWindow::FindFocus() ;
102 char charCode ;
103 UInt32 keyCode ;
104 UInt32 modifiers ;
105 Point point ;
106 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
107
108 EventRef rawEvent ;
109
110 GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
111
112 GetEventParameter( rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
113 GetEventParameter( rawEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
114 GetEventParameter( rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
115 GetEventParameter( rawEvent, kEventParamMouseLocation, typeQDPoint, NULL,
116 sizeof( Point ), NULL, &point );
117
118 UInt32 message = (keyCode << 8) + charCode;
119
120 switch ( GetEventKind( event ) )
121 {
122 case kEventTextInputUnicodeForKeyEvent :
123 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
124 focus , message , modifiers , when , point.h , point.v ) )
125 {
126 result = noErr ;
127 }
128 break ;
129 }
130
131 return result ;
132 }
133
134 static pascal OSStatus KeyboardEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
135 {
136 OSStatus result = eventNotHandledErr ;
137
138 wxWindow* focus = wxWindow::FindFocus() ;
139 char charCode ;
140 UInt32 keyCode ;
141 UInt32 modifiers ;
142 Point point ;
143 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
144
145 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL,sizeof(char), NULL,&charCode );
146 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
147 GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers);
148 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
149 sizeof( Point ), NULL, &point );
150
151 UInt32 message = (keyCode << 8) + charCode;
152 switch( GetEventKind( event ) )
153 {
154 case kEventRawKeyRepeat :
155 case kEventRawKeyDown :
156 if ( (focus != NULL) && wxTheApp->MacSendKeyDownEvent(
157 focus , message , modifiers , when , point.h , point.v ) )
158 {
159 result = noErr ;
160 }
161 break ;
162 case kEventRawKeyUp :
163 if ( (focus != NULL) && wxTheApp->MacSendKeyUpEvent(
164 focus , message , modifiers , when , point.h , point.v ) )
165 {
166 result = noErr ;
167 }
168 break ;
169 case kEventRawKeyModifiersChanged :
170 {
171 wxKeyEvent event(wxEVT_KEY_DOWN);
172
173 event.m_shiftDown = modifiers & shiftKey;
174 event.m_controlDown = modifiers & controlKey;
175 event.m_altDown = modifiers & optionKey;
176 event.m_metaDown = modifiers & cmdKey;
177
178 event.m_x = point.h;
179 event.m_y = point.v;
180 event.m_timeStamp = when;
181 wxWindow* focus = wxWindow::FindFocus() ;
182 event.SetEventObject(focus);
183
184 if ( (modifiers ^ wxTheApp->s_lastModifiers ) & controlKey )
185 {
186 event.m_keyCode = WXK_CONTROL ;
187 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
188 focus->GetEventHandler()->ProcessEvent( event ) ;
189 }
190 if ( (modifiers ^ wxTheApp->s_lastModifiers ) & shiftKey )
191 {
192 event.m_keyCode = WXK_SHIFT ;
193 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
194 focus->GetEventHandler()->ProcessEvent( event ) ;
195 }
196 if ( (modifiers ^ wxTheApp->s_lastModifiers ) & optionKey )
197 {
198 event.m_keyCode = WXK_ALT ;
199 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
200 focus->GetEventHandler()->ProcessEvent( event ) ;
201 }
202 wxTheApp->s_lastModifiers = modifiers ;
203 }
204 break ;
205 }
206
207 return result ;
208 }
209
210 static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
211 {
212 OSStatus result = eventNotHandledErr ;
213
214 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
215 Point point ;
216 UInt32 modifiers = 0;
217 EventMouseButton button = 0 ;
218 UInt32 click = 0 ;
219
220 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
221 sizeof( Point ), NULL, &point );
222 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
223 sizeof( UInt32 ), NULL, &modifiers );
224 GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
225 sizeof( EventMouseButton ), NULL, &button );
226 GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
227 sizeof( UInt32 ), NULL, &click );
228
229 if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
230 modifiers += btnState ;
231
232 WindowRef window ;
233 short windowPart = ::FindWindow(point, &window);
234
235 if ( IsWindowActive(window) && windowPart == inContent )
236 {
237 switch ( GetEventKind( event ) )
238 {
239 case kEventMouseDown :
240 toplevelWindow->MacFireMouseEvent( mouseDown , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
241 result = noErr ;
242 break ;
243 case kEventMouseUp :
244 toplevelWindow->MacFireMouseEvent( mouseUp , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
245 result = noErr ;
246 break ;
247 case kEventMouseMoved :
248 toplevelWindow->MacFireMouseEvent( nullEvent , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
249 result = noErr ;
250 break ;
251 case kEventMouseDragged :
252 toplevelWindow->MacFireMouseEvent( nullEvent , point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
253 result = noErr ;
254 break ;
255 default :
256 break ;
257 }
258 }
259
260 return result ;
261
262
263 }
264 static pascal OSStatus WindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
265 {
266 OSStatus result = eventNotHandledErr ;
267 OSStatus err = noErr ;
268
269 UInt32 attributes;
270 WindowRef windowRef ;
271 wxTopLevelWindowMac* toplevelWindow = (wxTopLevelWindowMac*) data ;
272
273 GetEventParameter( event, kEventParamDirectObject, typeWindowRef, NULL,
274 sizeof( WindowRef ), NULL, &windowRef );
275
276 switch( GetEventKind( event ) )
277 {
278 case kEventWindowUpdate :
279 if ( !wxPendingDelete.Member(toplevelWindow) )
280 toplevelWindow->MacUpdate( EventTimeToTicks( GetEventTime( event ) ) ) ;
281 result = noErr ;
282 break ;
283 case kEventWindowActivated :
284 toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , true) ;
285 result = noErr ;
286 break ;
287 case kEventWindowDeactivated :
288 toplevelWindow->MacActivate( EventTimeToTicks( GetEventTime( event ) ) , false) ;
289 result = noErr ;
290 break ;
291 case kEventWindowClose :
292 toplevelWindow->Close() ;
293 result = noErr ;
294 break ;
295 case kEventWindowBoundsChanged :
296 err = GetEventParameter( event, kEventParamAttributes, typeUInt32,
297 NULL, sizeof( UInt32 ), NULL, &attributes );
298 if ( err == noErr )
299 {
300 Rect newContentRect ;
301
302 GetEventParameter( event, kEventParamCurrentBounds, typeQDRectangle, NULL,
303 sizeof( newContentRect ), NULL, &newContentRect );
304
305 toplevelWindow->SetSize( newContentRect.left , newContentRect.top ,
306 newContentRect.right - newContentRect.left ,
307 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
308
309 result = noErr;
310 }
311 break ;
312 default :
313 break ;
314 }
315 return result ;
316 }
317
318 pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
319 {
320 OSStatus result = eventNotHandledErr ;
321
322 switch ( GetEventClass( event ) )
323 {
324 case kEventClassKeyboard :
325 result = KeyboardEventHandler( handler, event , data ) ;
326 break ;
327 case kEventClassTextInput :
328 result = TextInputEventHandler( handler, event , data ) ;
329 break ;
330 case kEventClassWindow :
331 result = WindowEventHandler( handler, event , data ) ;
332 break ;
333 case kEventClassMouse :
334 result = MouseEventHandler( handler, event , data ) ;
335 break ;
336 default :
337 break ;
338 }
339 return result ;
340 }
341
342 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacWindowEventHandler )
343
344 #endif
345
346 // ---------------------------------------------------------------------------
347 // wxWindowMac utility functions
348 // ---------------------------------------------------------------------------
349
350 // Find an item given the Macintosh Window Reference
351
352 wxList *wxWinMacWindowList = NULL;
353 wxTopLevelWindowMac *wxFindWinFromMacWindow(WXWindow inWindowRef)
354 {
355 wxNode *node = wxWinMacWindowList->Find((long)inWindowRef);
356 if (!node)
357 return NULL;
358 return (wxTopLevelWindowMac *)node->GetData();
359 }
360
361 void wxAssociateWinWithMacWindow(WXWindow inWindowRef, wxTopLevelWindowMac *win)
362 {
363 // adding NULL WindowRef is (first) surely a result of an error and
364 // (secondly) breaks menu command processing
365 wxCHECK_RET( inWindowRef != (WindowRef) NULL, "attempt to add a NULL WindowRef to window list" );
366
367 if ( !wxWinMacWindowList->Find((long)inWindowRef) )
368 wxWinMacWindowList->Append((long)inWindowRef, win);
369 }
370
371 void wxRemoveMacWindowAssociation(wxTopLevelWindowMac *win)
372 {
373 wxWinMacWindowList->DeleteObject(win);
374 }
375
376
377 // ----------------------------------------------------------------------------
378 // wxTopLevelWindowMac creation
379 // ----------------------------------------------------------------------------
380
381 WXHWND wxTopLevelWindowMac::s_macWindowInUpdate = NULL;
382
383 void wxTopLevelWindowMac::Init()
384 {
385 m_iconized =
386 m_maximizeOnShow = FALSE;
387 m_macNoEraseUpdateRgn = NewRgn() ;
388 m_macNeedsErasing = false ;
389 m_macWindow = NULL ;
390 #if TARGET_CARBON
391 m_macEventHandler = NULL ;
392 #endif
393 }
394
395 class wxMacDeferredWindowDeleter : public wxObject
396 {
397 public :
398 wxMacDeferredWindowDeleter( WindowRef windowRef )
399 {
400 m_macWindow = windowRef ;
401 }
402 virtual ~wxMacDeferredWindowDeleter()
403 {
404 UMADisposeWindow( (WindowRef) m_macWindow ) ;
405 }
406 protected :
407 WindowRef m_macWindow ;
408 } ;
409
410 bool wxTopLevelWindowMac::Create(wxWindow *parent,
411 wxWindowID id,
412 const wxString& title,
413 const wxPoint& pos,
414 const wxSize& size,
415 long style,
416 const wxString& name)
417 {
418 // init our fields
419 Init();
420
421 m_windowStyle = style;
422
423 SetName(name);
424
425 m_windowId = id == -1 ? NewControlId() : id;
426
427 wxTopLevelWindows.Append(this);
428
429 if ( parent )
430 parent->AddChild(this);
431
432 return TRUE;
433 }
434
435 wxTopLevelWindowMac::~wxTopLevelWindowMac()
436 {
437 if ( m_macWindow )
438 {
439 wxToolTip::NotifyWindowDelete(m_macWindow) ;
440 wxPendingDelete.Append( new wxMacDeferredWindowDeleter( (WindowRef) m_macWindow ) ) ;
441 }
442
443 #if TARGET_CARBON
444 if ( m_macEventHandler )
445 {
446 ::RemoveEventHandler((EventHandlerRef) m_macEventHandler);
447 m_macEventHandler = NULL ;
448 }
449 #endif
450
451 wxRemoveMacWindowAssociation( this ) ;
452
453 if ( wxModelessWindows.Find(this) )
454 wxModelessWindows.DeleteObject(this);
455
456 DisposeRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
457 }
458
459
460 // ----------------------------------------------------------------------------
461 // wxTopLevelWindowMac maximize/minimize
462 // ----------------------------------------------------------------------------
463
464 void wxTopLevelWindowMac::Maximize(bool maximize)
465 {
466 // not available on mac
467 }
468
469 bool wxTopLevelWindowMac::IsMaximized() const
470 {
471 return false ;
472 }
473
474 void wxTopLevelWindowMac::Iconize(bool iconize)
475 {
476 // not available on mac
477 }
478
479 bool wxTopLevelWindowMac::IsIconized() const
480 {
481 // mac dialogs cannot be iconized
482 return FALSE;
483 }
484
485 void wxTopLevelWindowMac::Restore()
486 {
487 // not available on mac
488 }
489
490 // ----------------------------------------------------------------------------
491 // wxTopLevelWindowMac misc
492 // ----------------------------------------------------------------------------
493
494 void wxTopLevelWindowMac::SetIcon(const wxIcon& icon)
495 {
496 // this sets m_icon
497 wxTopLevelWindowBase::SetIcon(icon);
498 }
499
500 void wxTopLevelWindowMac::MacCreateRealWindow( const wxString& title,
501 const wxPoint& pos,
502 const wxSize& size,
503 long style,
504 const wxString& name )
505 {
506 SetName(name);
507 m_windowStyle = style;
508 m_isShown = FALSE;
509
510 // create frame.
511
512 Rect theBoundsRect;
513
514 m_x = (int)pos.x;
515 m_y = (int)pos.y;
516 if ( m_y < 50 )
517 m_y = 50 ;
518 if ( m_x < 20 )
519 m_x = 20 ;
520
521 m_width = size.x;
522 if (m_width == -1)
523 m_width = 20;
524 m_height = size.y;
525 if (m_height == -1)
526 m_height = 20;
527
528 ::SetRect(&theBoundsRect, m_x, m_y , m_x + m_width, m_y + m_height);
529
530 // translate the window attributes in the appropriate window class and attributes
531
532 WindowClass wclass = 0;
533 WindowAttributes attr = kWindowNoAttributes ;
534
535 if ( HasFlag( wxFRAME_TOOL_WINDOW) )
536 {
537 if (
538 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
539 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
540 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
541 )
542 {
543 wclass = kFloatingWindowClass ;
544 if ( HasFlag(wxTINY_CAPTION_VERT) )
545 {
546 attr |= kWindowSideTitlebarAttribute ;
547 }
548 }
549 else
550 {
551 #if TARGET_CARBON
552 wclass = kPlainWindowClass ;
553 #else
554 wclass = kFloatingWindowClass ;
555 #endif
556 }
557 }
558 else if ( HasFlag( wxCAPTION ) )
559 {
560 wclass = kDocumentWindowClass ;
561 }
562 else
563 {
564 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
565 HasFlag( wxSYSTEM_MENU ) )
566 {
567 wclass = kDocumentWindowClass ;
568 }
569 else
570 {
571 #if TARGET_CARBON
572 wclass = kPlainWindowClass ;
573 #else
574 wclass = kModalWindowClass ;
575 #endif
576 }
577 }
578
579 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) )
580 {
581 attr |= kWindowFullZoomAttribute ;
582 attr |= kWindowCollapseBoxAttribute ;
583 }
584 if ( HasFlag( wxRESIZE_BORDER ) )
585 {
586 attr |= kWindowResizableAttribute ;
587 }
588 if ( HasFlag( wxSYSTEM_MENU ) )
589 {
590 attr |= kWindowCloseBoxAttribute ;
591 }
592
593 ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
594 wxAssociateWinWithMacWindow( m_macWindow , this ) ;
595 wxString label ;
596 if( wxApp::s_macDefaultEncodingIsPC )
597 label = wxMacMakeMacStringFromPC( title ) ;
598 else
599 label = title ;
600 UMASetWTitleC( (WindowRef)m_macWindow , label ) ;
601 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
602 #if TARGET_CARBON
603 InstallStandardEventHandler( GetWindowEventTarget(MAC_WXHWND(m_macWindow)) ) ;
604 InstallWindowEventHandler(MAC_WXHWND(m_macWindow), GetwxMacWindowEventHandlerUPP(),
605 GetEventTypeCount(eventList), eventList, this, &((EventHandlerRef)m_macEventHandler));
606 #endif
607 m_macFocus = NULL ;
608 }
609
610 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXHWND *window , wxWindowMac** rootwin)
611 {
612 ((Point*)localOrigin)->h = 0;
613 ((Point*)localOrigin)->v = 0;
614 ((Rect*)clipRect)->left = 0;
615 ((Rect*)clipRect)->top = 0;
616 ((Rect*)clipRect)->right = m_width;
617 ((Rect*)clipRect)->bottom = m_height;
618 *window = m_macWindow ;
619 *rootwin = this ;
620 }
621
622 void wxTopLevelWindowMac::Clear()
623 {
624 wxWindow::Clear() ;
625 }
626
627 WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
628 {
629 return m_macRootControl ;
630 }
631
632
633 void wxTopLevelWindowMac::MacUpdate( long timestamp)
634 {
635 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
636
637 BeginUpdate( (WindowRef)m_macWindow ) ;
638
639 RgnHandle updateRgn = NewRgn();
640 RgnHandle diffRgn = NewRgn() ;
641 if ( updateRgn && diffRgn )
642 {
643 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
644 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
645 if ( !EmptyRgn( updateRgn ) )
646 {
647 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
648 }
649 }
650 if ( updateRgn )
651 DisposeRgn( updateRgn );
652 if ( diffRgn )
653 DisposeRgn( diffRgn );
654 EndUpdate( (WindowRef)m_macWindow ) ;
655 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
656 m_macNeedsErasing = false ;
657 }
658
659
660 // Raise the window to the top of the Z order
661 void wxTopLevelWindowMac::Raise()
662 {
663 ::SelectWindow( (WindowRef)m_macWindow ) ;
664 }
665
666 // Lower the window to the bottom of the Z order
667 void wxTopLevelWindowMac::Lower()
668 {
669 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
670 }
671
672 void wxTopLevelWindowMac::MacFireMouseEvent(
673 wxUint16 kind , wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp )
674 {
675 wxMouseEvent event(wxEVT_LEFT_DOWN);
676 bool isDown = !(modifiers & btnState) ; // 1 is for up
677 bool controlDown = modifiers & controlKey ; // for simulating right mouse
678
679 event.m_leftDown = isDown && !controlDown;
680
681 event.m_middleDown = FALSE;
682 event.m_rightDown = isDown && controlDown;
683
684 if ( kind == mouseDown )
685 {
686 if ( controlDown )
687 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
688 else
689 event.SetEventType(wxEVT_LEFT_DOWN ) ;
690 }
691 else if ( kind == mouseUp )
692 {
693 if ( controlDown )
694 event.SetEventType(wxEVT_RIGHT_UP ) ;
695 else
696 event.SetEventType(wxEVT_LEFT_UP ) ;
697 }
698 else
699 {
700 event.SetEventType(wxEVT_MOTION ) ;
701 }
702
703 event.m_shiftDown = modifiers & shiftKey;
704 event.m_controlDown = modifiers & controlKey;
705 event.m_altDown = modifiers & optionKey;
706 event.m_metaDown = modifiers & cmdKey;
707
708 Point localwhere ;
709 localwhere.h = x ;
710 localwhere.v = y ;
711
712 GrafPtr port ;
713 ::GetPort( &port ) ;
714 ::SetPort( UMAGetWindowPort( (WindowRef)m_macWindow ) ) ;
715 ::GlobalToLocal( &localwhere ) ;
716 ::SetPort( port ) ;
717
718 if ( kind == mouseDown )
719 {
720 if ( timestamp - gs_lastWhen <= GetDblTime() )
721 {
722 if ( abs( localwhere.h - gs_lastWhere.h ) < 3 && abs( localwhere.v - gs_lastWhere.v ) < 3 )
723 {
724 // This is not right if the second mouse down
725 // event occured in a differen window. We
726 // correct this in MacDispatchMouseEvent.
727 if ( controlDown )
728 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
729 else
730 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
731 }
732 gs_lastWhen = 0 ;
733 }
734 else
735 {
736 gs_lastWhen = timestamp ;
737 }
738 gs_lastWhere = localwhere ;
739 }
740
741 event.m_x = localwhere.h;
742 event.m_y = localwhere.v;
743 event.m_x += m_x;
744 event.m_y += m_y;
745
746 event.m_timeStamp = timestamp;
747 event.SetEventObject(this);
748 if ( wxTheApp->s_captureWindow )
749 {
750 int x = event.m_x ;
751 int y = event.m_y ;
752 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
753 event.m_x = x ;
754 event.m_y = y ;
755 event.SetEventObject( wxTheApp->s_captureWindow ) ;
756 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
757
758 if ( kind == mouseUp )
759 {
760 wxTheApp->s_captureWindow = NULL ;
761 if ( !wxIsBusy() )
762 {
763 m_cursor.MacInstall() ;
764 }
765 }
766 }
767 else
768 {
769 MacDispatchMouseEvent( event ) ;
770 }
771 }
772
773 #if !TARGET_CARBON
774
775 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev , short part)
776 {
777 MacFireMouseEvent( mouseDown , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
778 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
779 }
780
781 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev , short part)
782 {
783 switch (part)
784 {
785 case inContent:
786 {
787 MacFireMouseEvent( mouseUp , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
788 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
789 }
790 break ;
791 }
792 }
793
794 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev , short part)
795 {
796 switch (part)
797 {
798 case inContent:
799 {
800 MacFireMouseEvent( nullEvent /*moved*/ , ((EventRecord*)ev)->where.h , ((EventRecord*)ev)->where.v ,
801 ((EventRecord*)ev)->modifiers , ((EventRecord*)ev)->when ) ;
802 }
803 break ;
804 }
805 }
806
807 #endif
808
809 void wxTopLevelWindowMac::MacActivate( long timestamp , bool inIsActivating )
810 {
811 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
812 event.m_timeStamp = timestamp ;
813 event.SetEventObject(this);
814
815 GetEventHandler()->ProcessEvent(event);
816
817 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
818
819 // Early versions of MacOS X don't refresh backgrounds properly,
820 // so refresh the whole window on activation and deactivation.
821 long osVersion = UMAGetSystemVersion();
822 if (osVersion >= 0x1000 && osVersion < 0x1020)
823 Refresh(TRUE);
824 else
825 MacSuperEnabled( inIsActivating ) ;
826 }
827
828 #if !TARGET_CARBON
829
830 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev )
831 {
832 }
833
834 #endif
835
836 void wxTopLevelWindowMac::SetTitle(const wxString& title)
837 {
838 wxWindow::SetTitle( title ) ;
839
840 wxString label ;
841
842 if( wxApp::s_macDefaultEncodingIsPC )
843 label = wxMacMakeMacStringFromPC( m_label ) ;
844 else
845 label = m_label ;
846
847 UMASetWTitleC( (WindowRef)m_macWindow , label ) ;
848 }
849
850 bool wxTopLevelWindowMac::Show(bool show)
851 {
852 if ( !wxWindow::Show(show) )
853 return FALSE;
854
855 if (show)
856 {
857 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowShowTransitionAction,nil);
858 ::SelectWindow( (WindowRef)m_macWindow ) ;
859 // no need to generate events here, they will get them triggered by macos
860 // actually they should be , but apparently they are not
861 wxSize size(m_width, m_height);
862 wxSizeEvent event(size, m_windowId);
863 event.SetEventObject(this);
864 GetEventHandler()->ProcessEvent(event);
865 }
866 else
867 {
868 ::TransitionWindow((WindowRef)m_macWindow,kWindowZoomTransitionEffect,kWindowHideTransitionAction,nil);
869 }
870
871 if ( !show )
872 {
873 }
874 else
875 {
876 Refresh() ;
877 }
878
879 return TRUE;
880 }
881
882 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
883 {
884 int former_x = m_x ;
885 int former_y = m_y ;
886 int former_w = m_width ;
887 int former_h = m_height ;
888
889 int actualWidth = width;
890 int actualHeight = height;
891 int actualX = x;
892 int actualY = y;
893
894 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
895 actualWidth = m_minWidth;
896 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
897 actualHeight = m_minHeight;
898 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
899 actualWidth = m_maxWidth;
900 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
901 actualHeight = m_maxHeight;
902
903 bool doMove = false ;
904 bool doResize = false ;
905
906 if ( actualX != former_x || actualY != former_y )
907 {
908 doMove = true ;
909 }
910 if ( actualWidth != former_w || actualHeight != former_h )
911 {
912 doResize = true ;
913 }
914
915 if ( doMove || doResize )
916 {
917 m_x = actualX ;
918 m_y = actualY ;
919 m_width = actualWidth ;
920 m_height = actualHeight ;
921
922 if ( doMove )
923 ::MoveWindow((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
924
925 if ( doResize )
926 ::SizeWindow((WindowRef)m_macWindow, m_width, m_height , true);
927
928 // the OS takes care of invalidating and erasing the new area so we only have to
929 // take care of refreshing for full repaints
930
931 if ( doResize && !HasFlag(wxNO_FULL_REPAINT_ON_RESIZE) )
932 Refresh() ;
933
934
935 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
936 {
937 wxFrame* frame = (wxFrame*) this ;
938 frame->PositionStatusBar();
939 frame->PositionToolBar();
940 }
941 if ( doMove )
942 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
943
944 MacRepositionScrollBars() ;
945 if ( doMove )
946 {
947 wxPoint point(m_x, m_y);
948 wxMoveEvent event(point, m_windowId);
949 event.SetEventObject(this);
950 GetEventHandler()->ProcessEvent(event) ;
951 }
952 if ( doResize )
953 {
954 MacRepositionScrollBars() ;
955 wxSize size(m_width, m_height);
956 wxSizeEvent event(size, m_windowId);
957 event.SetEventObject(this);
958 GetEventHandler()->ProcessEvent(event);
959 }
960 }
961
962 }
963
964 /*
965 * Invalidation Mechanism
966 *
967 * The update mechanism reflects exactely the windows mechanism
968 * the rect gets added to the window invalidate region, if the eraseBackground flag
969 * has been true for any part of the update rgn the background is erased in the entire region
970 * not just in the specified rect.
971 *
972 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
973 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
974 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
975 * will get the eraseBackground event first
976 */
977
978 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect, bool eraseBackground )
979 {
980 GrafPtr formerPort ;
981 GetPort( &formerPort ) ;
982 SetPortWindowPort( (WindowRef)m_macWindow ) ;
983
984 m_macNeedsErasing |= eraseBackground ;
985
986 // if we already know that we will have to erase, there's no need to track the rest
987 if ( !m_macNeedsErasing)
988 {
989 // we end only here if eraseBackground is false
990 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
991 // we will have to erase anyway
992
993 RgnHandle updateRgn = NewRgn();
994 RgnHandle diffRgn = NewRgn() ;
995 if ( updateRgn && diffRgn )
996 {
997 GetWindowUpdateRgn( (WindowRef)m_macWindow , updateRgn );
998 Point pt = {0,0} ;
999 LocalToGlobal( &pt ) ;
1000 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
1001 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
1002 if ( !EmptyRgn( diffRgn ) )
1003 {
1004 m_macNeedsErasing = true ;
1005 }
1006 }
1007 if ( updateRgn )
1008 DisposeRgn( updateRgn );
1009 if ( diffRgn )
1010 DisposeRgn( diffRgn );
1011
1012 if ( !m_macNeedsErasing )
1013 {
1014 RgnHandle rectRgn = NewRgn() ;
1015 SetRectRgn( rectRgn , ((Rect*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
1016 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
1017 DisposeRgn( rectRgn ) ;
1018 }
1019 }
1020 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
1021 // turn this on to debug the refreshing cycle
1022 #if wxMAC_DEBUG_REDRAW
1023 PaintRect( rect ) ;
1024 #endif
1025 SetPort( formerPort ) ;
1026 }
1027