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