]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toplevel.cpp
made SetItemText() work for the item which we're about to start editing
[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 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) )
260 {
261 if (
262 HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
263 HasFlag( wxSYSTEM_MENU ) || HasFlag( wxCAPTION ) ||
264 HasFlag(wxTINY_CAPTION_HORIZ) || HasFlag(wxTINY_CAPTION_VERT)
265 )
266 {
267 wclass = kFloatingWindowClass ;
268 if ( HasFlag(wxTINY_CAPTION_VERT) )
269 {
270 attr |= kWindowSideTitlebarAttribute ;
271 }
272 }
273 else
274 {
275 wclass = kPlainWindowClass ;
276 }
277 }
278 else if ( HasFlag( wxCAPTION ) )
279 {
280 if ( HasFlag( wxDIALOG_MODAL ) )
281 {
282 wclass = kDocumentWindowClass ; // kMovableModalWindowClass ;
283 }
284 else
285 {
286 wclass = kDocumentWindowClass ;
287 }
288 }
289 else
290 {
291 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) ||
292 HasFlag( wxSYSTEM_MENU ) )
293 {
294 wclass = kDocumentWindowClass ;
295 }
296 else
297 {
298 wclass = kPlainWindowClass ;
299 }
300 }
301
302 if ( HasFlag( wxMINIMIZE_BOX ) || HasFlag( wxMAXIMIZE_BOX ) )
303 {
304 attr |= kWindowFullZoomAttribute ;
305 attr |= kWindowCollapseBoxAttribute ;
306 }
307 if ( HasFlag( wxRESIZE_BORDER ) )
308 {
309 attr |= kWindowResizableAttribute ;
310 }
311 if ( HasFlag( wxSYSTEM_MENU ) )
312 {
313 attr |= kWindowCloseBoxAttribute ;
314 }
315
316 ::CreateNewWindow( wclass , attr , &theBoundsRect , (WindowRef*)&m_macWindow ) ;
317 wxAssociateWinWithMacWindow( m_macWindow , this ) ;
318 wxString label ;
319 if( wxApp::s_macDefaultEncodingIsPC )
320 label = wxMacMakeMacStringFromPC( title ) ;
321 else
322 label = title ;
323 UMASetWTitleC( (WindowRef)m_macWindow , label ) ;
324 ::CreateRootControl( (WindowRef)m_macWindow , (ControlHandle*)&m_macRootControl ) ;
325
326 m_macFocus = NULL ;
327 }
328
329 void wxTopLevelWindowMac::MacGetPortParams(WXPOINTPTR localOrigin, WXRECTPTR clipRect, WXHWND *window , wxWindowMac** rootwin)
330 {
331 ((Point*)localOrigin)->h = 0;
332 ((Point*)localOrigin)->v = 0;
333 ((Rect*)clipRect)->left = 0;
334 ((Rect*)clipRect)->top = 0;
335 ((Rect*)clipRect)->right = m_width;
336 ((Rect*)clipRect)->bottom = m_height;
337 *window = m_macWindow ;
338 *rootwin = this ;
339 }
340
341 void wxTopLevelWindowMac::Clear()
342 {
343 wxWindow::Clear() ;
344 }
345
346 WXWidget wxTopLevelWindowMac::MacGetContainerForEmbedding()
347 {
348 return m_macRootControl ;
349 }
350
351
352 void wxTopLevelWindowMac::MacUpdate( long timestamp)
353 {
354
355 wxMacPortStateHelper help( (GrafPtr) GetWindowPort( (WindowRef) m_macWindow) ) ;
356
357 BeginUpdate( (WindowRef)m_macWindow ) ;
358
359 RgnHandle updateRgn = NewRgn();
360 RgnHandle diffRgn = NewRgn() ;
361 if ( updateRgn && diffRgn )
362 {
363 GetPortVisibleRegion( GetWindowPort( (WindowRef)m_macWindow ), updateRgn );
364 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
365 if ( !EmptyRgn( updateRgn ) )
366 {
367 MacRedraw( updateRgn , timestamp , m_macNeedsErasing || !EmptyRgn( diffRgn ) ) ;
368 }
369 }
370 if ( updateRgn )
371 DisposeRgn( updateRgn );
372 if ( diffRgn )
373 DisposeRgn( diffRgn );
374 EndUpdate( (WindowRef)m_macWindow ) ;
375 SetEmptyRgn( (RgnHandle) m_macNoEraseUpdateRgn ) ;
376 m_macNeedsErasing = false ;
377 }
378
379
380 // Raise the window to the top of the Z order
381 void wxTopLevelWindowMac::Raise()
382 {
383 ::BringToFront( (WindowRef)m_macWindow ) ;
384 }
385
386 // Lower the window to the bottom of the Z order
387 void wxTopLevelWindowMac::Lower()
388 {
389 ::SendBehind( (WindowRef)m_macWindow , NULL ) ;
390 }
391
392 void wxTopLevelWindowMac::MacFireMouseEvent( WXEVENTREF evr )
393 {
394 EventRecord *ev = (EventRecord*) evr ;
395 wxMouseEvent event(wxEVT_LEFT_DOWN);
396 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
397 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
398
399 event.m_leftDown = isDown && !controlDown;
400
401 event.m_middleDown = FALSE;
402 event.m_rightDown = isDown && controlDown;
403
404 if ( ev->what == mouseDown )
405 {
406 if ( controlDown )
407 event.SetEventType(wxEVT_RIGHT_DOWN ) ;
408 else
409 event.SetEventType(wxEVT_LEFT_DOWN ) ;
410 }
411 else if ( ev->what == mouseUp )
412 {
413 if ( controlDown )
414 event.SetEventType(wxEVT_RIGHT_UP ) ;
415 else
416 event.SetEventType(wxEVT_LEFT_UP ) ;
417 }
418 else
419 {
420 event.SetEventType(wxEVT_MOTION ) ;
421 }
422
423 event.m_shiftDown = ev->modifiers & shiftKey;
424 event.m_controlDown = ev->modifiers & controlKey;
425 event.m_altDown = ev->modifiers & optionKey;
426 event.m_metaDown = ev->modifiers & cmdKey;
427
428 Point localwhere = ev->where ;
429
430 GrafPtr port ;
431 ::GetPort( &port ) ;
432 ::SetPort( UMAGetWindowPort( (WindowRef)m_macWindow ) ) ;
433 ::GlobalToLocal( &localwhere ) ;
434 ::SetPort( port ) ;
435
436 if ( ev->what == mouseDown )
437 {
438 if ( ev->when - gs_lastWhen <= GetDblTime() )
439 {
440 if ( abs( localwhere.h - gs_lastWhere.h ) < 3 && abs( localwhere.v - gs_lastWhere.v ) < 3 )
441 {
442 // This is not right if the second mouse down
443 // event occured in a differen window. We
444 // correct this in MacDispatchMouseEvent.
445 if ( controlDown )
446 event.SetEventType(wxEVT_RIGHT_DCLICK ) ;
447 else
448 event.SetEventType(wxEVT_LEFT_DCLICK ) ;
449 }
450 gs_lastWhen = 0 ;
451 }
452 else
453 {
454 gs_lastWhen = ev->when ;
455 }
456 gs_lastWhere = localwhere ;
457 }
458
459 event.m_x = localwhere.h;
460 event.m_y = localwhere.v;
461 event.m_x += m_x;
462 event.m_y += m_y;
463
464 event.m_timeStamp = ev->when;
465 event.SetEventObject(this);
466 if ( wxTheApp->s_captureWindow )
467 {
468 int x = event.m_x ;
469 int y = event.m_y ;
470 wxTheApp->s_captureWindow->ScreenToClient( &x , &y ) ;
471 event.m_x = x ;
472 event.m_y = y ;
473 event.SetEventObject( wxTheApp->s_captureWindow ) ;
474 wxTheApp->s_captureWindow->GetEventHandler()->ProcessEvent( event ) ;
475
476 if ( ev->what == mouseUp )
477 {
478 wxTheApp->s_captureWindow = NULL ;
479 if ( wxBusyCursorCount == 0 )
480 {
481 m_cursor.MacInstall() ;
482 }
483 }
484 }
485 else
486 {
487 MacDispatchMouseEvent( event ) ;
488 }
489 }
490
491 void wxTopLevelWindowMac::MacMouseDown( WXEVENTREF ev , short part)
492 {
493 MacFireMouseEvent( ev ) ;
494 }
495
496 void wxTopLevelWindowMac::MacMouseUp( WXEVENTREF ev , short part)
497 {
498 switch (part)
499 {
500 case inContent:
501 {
502 MacFireMouseEvent( ev ) ;
503 }
504 break ;
505 }
506 }
507
508 void wxTopLevelWindowMac::MacMouseMoved( WXEVENTREF ev , short part)
509 {
510 switch (part)
511 {
512 case inContent:
513 {
514 MacFireMouseEvent( ev ) ;
515 }
516 break ;
517 }
518 }
519 void wxTopLevelWindowMac::MacActivate( WXEVENTREF ev , bool inIsActivating )
520 {
521 wxActivateEvent event(wxEVT_ACTIVATE, inIsActivating , m_windowId);
522 event.m_timeStamp = ((EventRecord*)ev)->when ;
523 event.SetEventObject(this);
524
525 GetEventHandler()->ProcessEvent(event);
526
527 UMAHighlightAndActivateWindow( (WindowRef)m_macWindow , inIsActivating ) ;
528
529 MacSuperEnabled( inIsActivating ) ;
530 }
531
532 void wxTopLevelWindowMac::MacKeyDown( WXEVENTREF ev )
533 {
534 }
535
536 void wxTopLevelWindowMac::SetTitle(const wxString& title)
537 {
538 wxWindow::SetTitle( title ) ;
539
540 wxString label ;
541
542 if( wxApp::s_macDefaultEncodingIsPC )
543 label = wxMacMakeMacStringFromPC( m_label ) ;
544 else
545 label = m_label ;
546
547 UMASetWTitleC( (WindowRef)m_macWindow , label ) ;
548 }
549
550 bool wxTopLevelWindowMac::Show(bool show)
551 {
552 if ( !wxWindow::Show(show) )
553 return FALSE;
554
555 if (show)
556 {
557 ::ShowWindow( (WindowRef)m_macWindow ) ;
558 ::SelectWindow( (WindowRef)m_macWindow ) ;
559 // no need to generate events here, they will get them triggered by macos
560 // actually they should be , but apparently they are not
561 wxSize size(m_width, m_height);
562 wxSizeEvent event(size, m_windowId);
563 event.SetEventObject(this);
564 GetEventHandler()->ProcessEvent(event);
565 }
566 else
567 {
568 ::HideWindow( (WindowRef)m_macWindow ) ;
569 }
570
571 if ( !show )
572 {
573 }
574 else
575 {
576 Refresh() ;
577 }
578
579 return TRUE;
580 }
581
582 void wxTopLevelWindowMac::DoMoveWindow(int x, int y, int width, int height)
583 {
584 int former_x = m_x ;
585 int former_y = m_y ;
586 int former_w = m_width ;
587 int former_h = m_height ;
588
589 int actualWidth = width;
590 int actualHeight = height;
591 int actualX = x;
592 int actualY = y;
593
594 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
595 actualWidth = m_minWidth;
596 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
597 actualHeight = m_minHeight;
598 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
599 actualWidth = m_maxWidth;
600 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
601 actualHeight = m_maxHeight;
602
603 bool doMove = false ;
604 bool doResize = false ;
605
606 if ( actualX != former_x || actualY != former_y )
607 {
608 doMove = true ;
609 }
610 if ( actualWidth != former_w || actualHeight != former_h )
611 {
612 doResize = true ;
613 }
614
615 if ( doMove || doResize )
616 {
617 m_x = actualX ;
618 m_y = actualY ;
619 m_width = actualWidth ;
620 m_height = actualHeight ;
621
622 if ( doMove )
623 ::MoveWindow((WindowRef)m_macWindow, m_x, m_y , false); // don't make frontmost
624
625 if ( doResize )
626 ::SizeWindow((WindowRef)m_macWindow, m_width, m_height , true);
627
628 // the OS takes care of invalidating and erasing the new area
629 // we have erased the old one
630
631 if ( IsKindOf( CLASSINFO( wxFrame ) ) )
632 {
633 wxFrame* frame = (wxFrame*) this ;
634 frame->PositionStatusBar();
635 frame->PositionToolBar();
636 }
637 if ( doMove )
638 wxWindowMac::MacTopLevelWindowChangedPosition() ; // like this only children will be notified
639
640 MacRepositionScrollBars() ;
641 if ( doMove )
642 {
643 wxPoint point(m_x, m_y);
644 wxMoveEvent event(point, m_windowId);
645 event.SetEventObject(this);
646 GetEventHandler()->ProcessEvent(event) ;
647 }
648 if ( doResize )
649 {
650 MacRepositionScrollBars() ;
651 wxSize size(m_width, m_height);
652 wxSizeEvent event(size, m_windowId);
653 event.SetEventObject(this);
654 GetEventHandler()->ProcessEvent(event);
655 }
656 }
657
658 }
659
660 /*
661 * Invalidation Mechanism
662 *
663 * The update mechanism reflects exactely the windows mechanism
664 * the rect gets added to the window invalidate region, if the eraseBackground flag
665 * has been true for any part of the update rgn the background is erased in the entire region
666 * not just in the specified rect.
667 *
668 * In order to achive this, we also have an internal m_macNoEraseUpdateRgn, all rects that have
669 * the eraseBackground flag set to false are also added to this rgn. upon receiving an update event
670 * the update rgn is compared to the m_macNoEraseUpdateRgn and in case they differ, every window
671 * will get the eraseBackground event first
672 */
673
674 void wxTopLevelWindowMac::MacInvalidate( const WXRECTPTR rect, bool eraseBackground )
675 {
676 GrafPtr formerPort ;
677 GetPort( &formerPort ) ;
678 SetPortWindowPort( (WindowRef)m_macWindow ) ;
679
680 m_macNeedsErasing |= eraseBackground ;
681
682 // if we already know that we will have to erase, there's no need to track the rest
683 if ( !m_macNeedsErasing)
684 {
685 // we end only here if eraseBackground is false
686 // if we already have a difference between m_macNoEraseUpdateRgn and UpdateRgn
687 // we will have to erase anyway
688
689 RgnHandle updateRgn = NewRgn();
690 RgnHandle diffRgn = NewRgn() ;
691 if ( updateRgn && diffRgn )
692 {
693 GetWindowUpdateRgn( (WindowRef)m_macWindow , updateRgn );
694 Point pt = {0,0} ;
695 LocalToGlobal( &pt ) ;
696 OffsetRgn( updateRgn , -pt.h , -pt.v ) ;
697 DiffRgn( updateRgn , (RgnHandle) m_macNoEraseUpdateRgn , diffRgn ) ;
698 if ( !EmptyRgn( diffRgn ) )
699 {
700 m_macNeedsErasing = true ;
701 }
702 }
703 if ( updateRgn )
704 DisposeRgn( updateRgn );
705 if ( diffRgn )
706 DisposeRgn( diffRgn );
707
708 if ( !m_macNeedsErasing )
709 {
710 RgnHandle rectRgn = NewRgn() ;
711 SetRectRgn( rectRgn , ((Rect*)rect)->left , ((Rect*)rect)->top , ((Rect*)rect)->right , ((Rect*)rect)->bottom ) ;
712 UnionRgn( (RgnHandle) m_macNoEraseUpdateRgn , rectRgn , (RgnHandle) m_macNoEraseUpdateRgn ) ;
713 DisposeRgn( rectRgn ) ;
714 }
715 }
716 InvalWindowRect( (WindowRef)m_macWindow , (Rect*)rect ) ;
717 // turn this on to debug the refreshing cycle
718 #if wxMAC_DEBUG_REDRAW
719 PaintRect( rect ) ;
720 #endif
721 SetPort( formerPort ) ;
722 }
723