]> git.saurik.com Git - wxWidgets.git/blob - src/mac/window.cpp
Added wxMessageOutput as per the discussion on wx-dev.
[wxWidgets.git] / src / mac / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: windows.cpp
3 // Purpose: wxWindowMac
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "window.h"
14 #endif
15
16 #include "wx/setup.h"
17 #include "wx/menu.h"
18 #include "wx/window.h"
19 #include "wx/dc.h"
20 #include "wx/dcclient.h"
21 #include "wx/utils.h"
22 #include "wx/app.h"
23 #include "wx/panel.h"
24 #include "wx/layout.h"
25 #include "wx/dialog.h"
26 #include "wx/listbox.h"
27 #include "wx/scrolbar.h"
28 #include "wx/statbox.h"
29 #include "wx/button.h"
30 #include "wx/settings.h"
31 #include "wx/msgdlg.h"
32 #include "wx/frame.h"
33 #include "wx/notebook.h"
34 #include "wx/tabctrl.h"
35 #include "wx/tooltip.h"
36 #include "wx/statusbr.h"
37 #include "wx/menuitem.h"
38 #include "wx/spinctrl.h"
39 #include "wx/log.h"
40
41 #if wxUSE_CARET
42 #include "wx/caret.h"
43 #endif // wxUSE_CARET
44
45 #define wxWINDOW_HSCROLL 5998
46 #define wxWINDOW_VSCROLL 5997
47 #define MAC_SCROLLBAR_SIZE 16
48
49 #include "wx/mac/uma.h"
50 #ifndef __DARWIN__
51 #include <Windows.h>
52 #include <ToolUtils.h>
53 #endif
54
55 #if wxUSE_DRAG_AND_DROP
56 #include "wx/dnd.h"
57 #endif
58
59 #include <string.h>
60
61 extern wxList wxPendingDelete;
62 wxWindowMac* gFocusWindow = NULL ;
63
64 #ifdef __WXUNIVERSAL__
65 IMPLEMENT_ABSTRACT_CLASS(wxWindowMac, wxWindowBase)
66 #else // __WXMAC__
67 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
68 #endif // __WXUNIVERSAL__/__WXMAC__
69
70 #if !USE_SHARED_LIBRARY
71
72 BEGIN_EVENT_TABLE(wxWindowMac, wxWindowBase)
73 EVT_NC_PAINT(wxWindowMac::OnNcPaint)
74 EVT_ERASE_BACKGROUND(wxWindowMac::OnEraseBackground)
75 EVT_SYS_COLOUR_CHANGED(wxWindowMac::OnSysColourChanged)
76 EVT_INIT_DIALOG(wxWindowMac::OnInitDialog)
77 EVT_IDLE(wxWindowMac::OnIdle)
78 EVT_SET_FOCUS(wxWindowMac::OnSetFocus)
79 END_EVENT_TABLE()
80
81 #endif
82
83 #define wxMAC_DEBUG_REDRAW 0
84 #ifndef wxMAC_DEBUG_REDRAW
85 #define wxMAC_DEBUG_REDRAW 0
86 #endif
87
88 #define wxMAC_USE_THEME_BORDER 0
89
90
91 // ===========================================================================
92 // implementation
93 // ===========================================================================
94
95
96 // ----------------------------------------------------------------------------
97 // constructors and such
98 // ----------------------------------------------------------------------------
99
100 void wxWindowMac::Init()
101 {
102 // generic
103 InitBase();
104
105 // MSW specific
106 m_doubleClickAllowed = 0;
107 m_winCaptured = FALSE;
108
109 m_isBeingDeleted = FALSE;
110
111 m_useCtl3D = FALSE;
112 m_mouseInWindow = FALSE;
113
114 m_xThumbSize = 0;
115 m_yThumbSize = 0;
116 m_backgroundTransparent = FALSE;
117
118 // as all windows are created with WS_VISIBLE style...
119 m_isShown = TRUE;
120
121 m_x = 0;
122 m_y = 0 ;
123 m_width = 0 ;
124 m_height = 0 ;
125
126 m_hScrollBar = NULL ;
127 m_vScrollBar = NULL ;
128
129 m_label = wxEmptyString;
130 }
131
132 // Destructor
133 wxWindowMac::~wxWindowMac()
134 {
135 // deleting a window while it is shown invalidates the region
136 if ( IsShown() ) {
137 wxWindowMac* iter = this ;
138 while( iter ) {
139 if ( iter->IsTopLevel() )
140 {
141 Refresh() ;
142 break ;
143 }
144 iter = iter->GetParent() ;
145
146 }
147 }
148
149 m_isBeingDeleted = TRUE;
150
151 #ifndef __WXUNIVERSAL__
152 // VS: make sure there's no wxFrame with last focus set to us:
153 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
154 {
155 wxFrame *frame = wxDynamicCast(win, wxFrame);
156 if ( frame )
157 {
158 if ( frame->GetLastFocus() == this )
159 {
160 frame->SetLastFocus((wxWindow*)NULL);
161 }
162 break;
163 }
164 }
165 #endif // __WXUNIVERSAL__
166
167 if ( s_lastMouseWindow == this )
168 {
169 s_lastMouseWindow = NULL ;
170 }
171
172 if ( gFocusWindow == this )
173 {
174 gFocusWindow = NULL ;
175 }
176
177 if ( m_parent )
178 m_parent->RemoveChild(this);
179
180 DestroyChildren();
181 }
182
183 // Constructor
184 bool wxWindowMac::Create(wxWindowMac *parent, wxWindowID id,
185 const wxPoint& pos,
186 const wxSize& size,
187 long style,
188 const wxString& name)
189 {
190 wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindowMac without parent") );
191
192 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
193 return FALSE;
194
195 parent->AddChild(this);
196
197 m_x = (int)pos.x;
198 m_y = (int)pos.y;
199 AdjustForParentClientOrigin(m_x, m_y, wxSIZE_USE_EXISTING);
200 m_width = WidthDefault( size.x );
201 m_height = HeightDefault( size.y ) ;
202 #ifndef __WXUNIVERSAL__
203 // Don't give scrollbars to wxControls unless they ask for them
204 if ( (! IsKindOf(CLASSINFO(wxControl)) && ! IsKindOf(CLASSINFO(wxStatusBar))) ||
205 (IsKindOf(CLASSINFO(wxControl)) && ( style & wxHSCROLL || style & wxVSCROLL)))
206 {
207 MacCreateScrollBars( style ) ;
208 }
209 #endif
210 return TRUE;
211 }
212
213 void wxWindowMac::SetFocus()
214 {
215 if ( gFocusWindow == this )
216 return ;
217
218 if ( AcceptsFocus() )
219 {
220 if (gFocusWindow )
221 {
222 #if wxUSE_CARET
223 // Deal with caret
224 if ( gFocusWindow->m_caret )
225 {
226 gFocusWindow->m_caret->OnKillFocus();
227 }
228 #endif // wxUSE_CARET
229 #ifndef __WXUNIVERSAL__
230 wxControl* control = wxDynamicCast( gFocusWindow , wxControl ) ;
231 if ( control && control->GetMacControl() )
232 {
233 UMASetKeyboardFocus( (WindowRef) gFocusWindow->MacGetRootWindow() , (ControlHandle) control->GetMacControl() , kControlFocusNoPart ) ;
234 control->MacRedrawControl() ;
235 }
236 #endif
237 wxFocusEvent event(wxEVT_KILL_FOCUS, gFocusWindow->m_windowId);
238 event.SetEventObject(gFocusWindow);
239 gFocusWindow->GetEventHandler()->ProcessEvent(event) ;
240 }
241 gFocusWindow = this ;
242 {
243 #if wxUSE_CARET
244 // Deal with caret
245 if ( m_caret )
246 {
247 m_caret->OnSetFocus();
248 }
249 #endif // wxUSE_CARET
250 // panel wants to track the window which was the last to have focus in it
251 wxChildFocusEvent eventFocus(this);
252 GetEventHandler()->ProcessEvent(eventFocus);
253
254 #ifndef __WXUNIVERSAL__
255 wxControl* control = wxDynamicCast( gFocusWindow , wxControl ) ;
256 if ( control && control->GetMacControl() )
257 {
258 UMASetKeyboardFocus( (WindowRef) gFocusWindow->MacGetRootWindow() , (ControlHandle) control->GetMacControl() , kControlFocusNextPart ) ;
259 }
260 #endif
261 wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
262 event.SetEventObject(this);
263 GetEventHandler()->ProcessEvent(event) ;
264 }
265 }
266 }
267
268 bool wxWindowMac::Enable(bool enable)
269 {
270 if ( !wxWindowBase::Enable(enable) )
271 return FALSE;
272
273 MacSuperEnabled( enable ) ;
274
275 return TRUE;
276 }
277
278 void wxWindowMac::DoCaptureMouse()
279 {
280 wxTheApp->s_captureWindow = this ;
281 }
282
283 wxWindow* wxWindowBase::GetCapture()
284 {
285 return wxTheApp->s_captureWindow ;
286 }
287
288 void wxWindowMac::DoReleaseMouse()
289 {
290 wxTheApp->s_captureWindow = NULL ;
291 }
292
293 #if wxUSE_DRAG_AND_DROP
294
295 void wxWindowMac::SetDropTarget(wxDropTarget *pDropTarget)
296 {
297 if ( m_dropTarget != 0 ) {
298 delete m_dropTarget;
299 }
300
301 m_dropTarget = pDropTarget;
302 if ( m_dropTarget != 0 )
303 {
304 // TODO
305 }
306 }
307
308 #endif
309
310 // Old style file-manager drag&drop
311 void wxWindowMac::DragAcceptFiles(bool accept)
312 {
313 // TODO
314 }
315
316 // Get total size
317 void wxWindowMac::DoGetSize(int *x, int *y) const
318 {
319 if(x) *x = m_width ;
320 if(y) *y = m_height ;
321 }
322
323 void wxWindowMac::DoGetPosition(int *x, int *y) const
324 {
325 int xx,yy;
326
327 xx = m_x ;
328 yy = m_y ;
329 if ( !IsTopLevel() && GetParent())
330 {
331 wxPoint pt(GetParent()->GetClientAreaOrigin());
332 xx -= pt.x;
333 yy -= pt.y;
334 }
335 if(x) *x = xx;
336 if(y) *y = yy;
337 }
338
339 #if wxUSE_MENUS
340 bool wxWindowMac::DoPopupMenu(wxMenu *menu, int x, int y)
341 {
342 menu->SetInvokingWindow(this);
343 menu->UpdateUI();
344 ClientToScreen( &x , &y ) ;
345
346 ::InsertMenu( (MenuHandle) menu->GetHMenu() , -1 ) ;
347 long menuResult = ::PopUpMenuSelect((MenuHandle) menu->GetHMenu() ,y,x, 0) ;
348 menu->MacMenuSelect( this , TickCount() , HiWord(menuResult) , LoWord(menuResult) ) ;
349 ::DeleteMenu( menu->MacGetMenuId() ) ;
350 menu->SetInvokingWindow(NULL);
351
352 return TRUE;
353 }
354 #endif
355
356 void wxWindowMac::DoScreenToClient(int *x, int *y) const
357 {
358 WindowRef window = (WindowRef) MacGetRootWindow() ;
359
360 Point localwhere = {0,0} ;
361
362 if(x) localwhere.h = * x ;
363 if(y) localwhere.v = * y ;
364
365 GrafPtr port ;
366 ::GetPort( &port ) ;
367 ::SetPort( UMAGetWindowPort( window ) ) ;
368 ::GlobalToLocal( &localwhere ) ;
369 ::SetPort( port ) ;
370
371 if(x) *x = localwhere.h ;
372 if(y) *y = localwhere.v ;
373
374 MacRootWindowToWindow( x , y ) ;
375 if ( x )
376 x -= MacGetLeftBorderSize() ;
377 if ( y )
378 y -= MacGetTopBorderSize() ;
379 }
380
381 void wxWindowMac::DoClientToScreen(int *x, int *y) const
382 {
383 WindowRef window = (WindowRef) MacGetRootWindow() ;
384
385 if ( x )
386 x += MacGetLeftBorderSize() ;
387 if ( y )
388 y += MacGetTopBorderSize() ;
389
390 MacWindowToRootWindow( x , y ) ;
391
392 Point localwhere = { 0,0 };
393 if(x) localwhere.h = * x ;
394 if(y) localwhere.v = * y ;
395
396 GrafPtr port ;
397 ::GetPort( &port ) ;
398 ::SetPort( UMAGetWindowPort( window ) ) ;
399
400 ::LocalToGlobal( &localwhere ) ;
401 ::SetPort( port ) ;
402 if(x) *x = localwhere.h ;
403 if(y) *y = localwhere.v ;
404 }
405
406 void wxWindowMac::MacClientToRootWindow( int *x , int *y ) const
407 {
408 wxPoint origin = GetClientAreaOrigin() ;
409 if(x) *x += origin.x ;
410 if(y) *y += origin.y ;
411
412 MacWindowToRootWindow( x , y ) ;
413 }
414
415 void wxWindowMac::MacRootWindowToClient( int *x , int *y ) const
416 {
417 wxPoint origin = GetClientAreaOrigin() ;
418 MacRootWindowToWindow( x , y ) ;
419 if(x) *x -= origin.x ;
420 if(y) *y -= origin.y ;
421 }
422
423 void wxWindowMac::MacWindowToRootWindow( int *x , int *y ) const
424 {
425 if ( !IsTopLevel() )
426 {
427 if(x) *x += m_x ;
428 if(y) *y += m_y ;
429 GetParent()->MacWindowToRootWindow( x , y ) ;
430 }
431 }
432
433 void wxWindowMac::MacRootWindowToWindow( int *x , int *y ) const
434 {
435 if ( !IsTopLevel() )
436 {
437 if(x) *x -= m_x ;
438 if(y) *y -= m_y ;
439 GetParent()->MacRootWindowToWindow( x , y ) ;
440 }
441 }
442
443 bool wxWindowMac::SetCursor(const wxCursor& cursor)
444 {
445 if (m_cursor == cursor)
446 return FALSE;
447
448 if (wxNullCursor == cursor)
449 {
450 if ( ! wxWindowBase::SetCursor( *wxSTANDARD_CURSOR ) )
451 return FALSE ;
452 }
453 else
454 {
455 if ( ! wxWindowBase::SetCursor( cursor ) )
456 return FALSE ;
457 }
458
459 wxASSERT_MSG( m_cursor.Ok(),
460 wxT("cursor must be valid after call to the base version"));
461
462 Point pt ;
463 wxWindowMac *mouseWin ;
464 GetMouse( &pt ) ;
465
466 // Change the cursor NOW if we're within the correct window
467
468 if ( MacGetWindowFromPoint( wxPoint( pt.h , pt.v ) , &mouseWin ) )
469 {
470 if ( mouseWin == this && !wxIsBusy() )
471 {
472 m_cursor.MacInstall() ;
473 }
474 }
475
476 return TRUE ;
477 }
478
479
480 // Get size *available for subwindows* i.e. excluding menu bar etc.
481 void wxWindowMac::DoGetClientSize(int *x, int *y) const
482 {
483 int ww, hh;
484 ww = m_width ;
485 hh = m_height ;
486
487 ww -= MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
488 hh -= MacGetTopBorderSize( ) + MacGetBottomBorderSize( );
489
490 if ( (m_vScrollBar && m_vScrollBar->IsShown()) || (m_hScrollBar && m_hScrollBar->IsShown()) )
491 {
492 int x1 = 0 ;
493 int y1 = 0 ;
494 int w = m_width ;
495 int h = m_height ;
496
497 MacClientToRootWindow( &x1 , &y1 ) ;
498 MacClientToRootWindow( &w , &h ) ;
499
500 wxWindowMac *iter = (wxWindowMac*)this ;
501
502 int totW = 10000 , totH = 10000;
503 while( iter )
504 {
505 if ( iter->IsTopLevel() )
506 {
507 totW = iter->m_width ;
508 totH = iter->m_height ;
509 break ;
510 }
511
512 iter = iter->GetParent() ;
513 }
514
515 if (m_hScrollBar && m_hScrollBar->IsShown() )
516 {
517 hh -= MAC_SCROLLBAR_SIZE;
518 if ( h-y1 >= totH )
519 {
520 hh += 1 ;
521 }
522 }
523 if (m_vScrollBar && m_vScrollBar->IsShown() )
524 {
525 ww -= MAC_SCROLLBAR_SIZE;
526 if ( w-x1 >= totW )
527 {
528 ww += 1 ;
529 }
530 }
531 }
532 if(x) *x = ww;
533 if(y) *y = hh;
534 }
535
536
537 // ----------------------------------------------------------------------------
538 // tooltips
539 // ----------------------------------------------------------------------------
540
541 #if wxUSE_TOOLTIPS
542
543 void wxWindowMac::DoSetToolTip(wxToolTip *tooltip)
544 {
545 wxWindowBase::DoSetToolTip(tooltip);
546
547 if ( m_tooltip )
548 m_tooltip->SetWindow(this);
549 }
550
551 #endif // wxUSE_TOOLTIPS
552
553 void wxWindowMac::DoMoveWindow(int x, int y, int width, int height)
554 {
555 int former_x = m_x ;
556 int former_y = m_y ;
557 int former_w = m_width ;
558 int former_h = m_height ;
559
560 int actualWidth = width;
561 int actualHeight = height;
562 int actualX = x;
563 int actualY = y;
564
565 if ((m_minWidth != -1) && (actualWidth < m_minWidth))
566 actualWidth = m_minWidth;
567 if ((m_minHeight != -1) && (actualHeight < m_minHeight))
568 actualHeight = m_minHeight;
569 if ((m_maxWidth != -1) && (actualWidth > m_maxWidth))
570 actualWidth = m_maxWidth;
571 if ((m_maxHeight != -1) && (actualHeight > m_maxHeight))
572 actualHeight = m_maxHeight;
573
574 bool doMove = false ;
575 bool doResize = false ;
576
577 if ( actualX != former_x || actualY != former_y )
578 {
579 doMove = true ;
580 }
581 if ( actualWidth != former_w || actualHeight != former_h )
582 {
583 doResize = true ;
584 }
585
586 if ( doMove || doResize )
587 {
588 // erase former position
589
590 Refresh() ;
591
592 m_x = actualX ;
593 m_y = actualY ;
594 m_width = actualWidth ;
595 m_height = actualHeight ;
596
597 // erase new position
598
599 Refresh() ;
600 if ( doMove )
601 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
602
603 MacRepositionScrollBars() ;
604 if ( doMove )
605 {
606 wxPoint point(m_x, m_y);
607 wxMoveEvent event(point, m_windowId);
608 event.SetEventObject(this);
609 GetEventHandler()->ProcessEvent(event) ;
610 }
611 if ( doResize )
612 {
613 MacRepositionScrollBars() ;
614 wxSize size(m_width, m_height);
615 wxSizeEvent event(size, m_windowId);
616 event.SetEventObject(this);
617 GetEventHandler()->ProcessEvent(event);
618 }
619 }
620
621 }
622
623 // set the size of the window: if the dimensions are positive, just use them,
624 // but if any of them is equal to -1, it means that we must find the value for
625 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
626 // which case -1 is a valid value for x and y)
627 //
628 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
629 // the width/height to best suit our contents, otherwise we reuse the current
630 // width/height
631 void wxWindowMac::DoSetSize(int x, int y, int width, int height, int sizeFlags)
632 {
633 // get the current size and position...
634 int currentX, currentY;
635 GetPosition(&currentX, &currentY);
636
637 int currentW,currentH;
638 GetSize(&currentW, &currentH);
639
640 // ... and don't do anything (avoiding flicker) if it's already ok
641 if ( x == currentX && y == currentY &&
642 width == currentW && height == currentH )
643 {
644 MacRepositionScrollBars() ; // we might have a real position shift
645 return;
646 }
647
648 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
649 x = currentX;
650 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
651 y = currentY;
652
653 AdjustForParentClientOrigin(x, y, sizeFlags);
654
655 wxSize size(-1, -1);
656 if ( width == -1 )
657 {
658 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
659 {
660 size = DoGetBestSize();
661 width = size.x;
662 }
663 else
664 {
665 // just take the current one
666 width = currentW;
667 }
668 }
669
670 if ( height == -1 )
671 {
672 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
673 {
674 if ( size.x == -1 )
675 {
676 size = DoGetBestSize();
677 }
678 //else: already called DoGetBestSize() above
679
680 height = size.y;
681 }
682 else
683 {
684 // just take the current one
685 height = currentH;
686 }
687 }
688
689 DoMoveWindow(x, y, width, height);
690
691 }
692 // For implementation purposes - sometimes decorations make the client area
693 // smaller
694
695 wxPoint wxWindowMac::GetClientAreaOrigin() const
696 {
697 return wxPoint(MacGetLeftBorderSize( ) , MacGetTopBorderSize( ) );
698 }
699
700 void wxWindowMac::SetTitle(const wxString& title)
701 {
702 m_label = title ;
703 }
704
705 wxString wxWindowMac::GetTitle() const
706 {
707 return m_label ;
708 }
709
710 bool wxWindowMac::Show(bool show)
711 {
712 if ( !wxWindowBase::Show(show) )
713 return FALSE;
714
715 /*
716 WindowRef window = (WindowRef) MacGetRootWindow() ;
717 wxWindowMac* win = wxFindWinFromMacWindow( window ) ;
718 if ( win == NULL && win->m_isBeingDeleted )
719 return FALSE ;
720 */
721 MacSuperShown( show ) ;
722 Refresh() ;
723 /*
724 if ( !show )
725 {
726 if ( win && !win->m_isBeingDeleted )
727 Refresh() ;
728 }
729 else
730 {
731 Refresh() ;
732 }
733 */
734 return TRUE;
735 }
736
737 void wxWindowMac::MacSuperShown( bool show )
738 {
739 wxNode *node = GetChildren().First();
740 while ( node )
741 {
742 wxWindowMac *child = (wxWindowMac *)node->Data();
743 if ( child->m_isShown )
744 child->MacSuperShown( show ) ;
745 node = node->Next();
746 }
747 }
748
749 void wxWindowMac::MacSuperEnabled( bool enabled )
750 {
751 if ( !IsTopLevel() )
752 {
753 // to be absolutely correct we'd have to invalidate (with eraseBkground
754 // because unter MacOSX the frames are drawn with an addXXX mode)
755 // the borders area
756 }
757 wxNode *node = GetChildren().First();
758 while ( node )
759 {
760 wxWindowMac *child = (wxWindowMac *)node->Data();
761 if ( child->m_isShown )
762 child->MacSuperEnabled( enabled ) ;
763 node = node->Next();
764 }
765 }
766
767 bool wxWindowMac::MacIsReallyShown() const
768 {
769 if ( m_isShown && (m_parent != NULL) ) {
770 return m_parent->MacIsReallyShown();
771 }
772 return m_isShown;
773 /*
774 bool status = m_isShown ;
775 wxWindowMac * win = this ;
776 while ( status && win->m_parent != NULL )
777 {
778 win = win->m_parent ;
779 status = win->m_isShown ;
780 }
781 return status ;
782 */
783 }
784
785 int wxWindowMac::GetCharHeight() const
786 {
787 wxClientDC dc ( (wxWindowMac*)this ) ;
788 return dc.GetCharHeight() ;
789 }
790
791 int wxWindowMac::GetCharWidth() const
792 {
793 wxClientDC dc ( (wxWindowMac*)this ) ;
794 return dc.GetCharWidth() ;
795 }
796
797 void wxWindowMac::GetTextExtent(const wxString& string, int *x, int *y,
798 int *descent, int *externalLeading, const wxFont *theFont ) const
799 {
800 const wxFont *fontToUse = theFont;
801 if ( !fontToUse )
802 fontToUse = &m_font;
803
804 wxClientDC dc( (wxWindowMac*) this ) ;
805 long lx,ly,ld,le ;
806 dc.GetTextExtent( string , &lx , &ly , &ld, &le, (wxFont *)fontToUse ) ;
807 if ( externalLeading )
808 *externalLeading = le ;
809 if ( descent )
810 *descent = ld ;
811 if ( x )
812 *x = lx ;
813 if ( y )
814 *y = ly ;
815 }
816
817 /*
818 * Rect is given in client coordinates, for further reading, read wxTopLevelWindowMac::InvalidateRect
819 * we always intersect with the entire window, not only with the client area
820 */
821
822 void wxWindowMac::Refresh(bool eraseBack, const wxRect *rect)
823 {
824 if ( MacGetTopLevelWindow() == NULL )
825 return ;
826
827 wxPoint client = GetClientAreaOrigin();
828 int x1 = -client.x;
829 int y1 = -client.y;
830 int x2 = m_width - client.x;
831 int y2 = m_height - client.y;
832
833 if (IsKindOf( CLASSINFO(wxButton)))
834 {
835 // buttons have an "aura"
836 y1 -= 5;
837 x1 -= 5;
838 y2 += 5;
839 x2 += 5;
840 }
841
842 Rect clientrect = { y1, x1, y2, x2 };
843
844 if ( rect )
845 {
846 Rect r = { rect->y , rect->x , rect->y + rect->height , rect->x + rect->width } ;
847 SectRect( &clientrect , &r , &clientrect ) ;
848 }
849
850 if ( !EmptyRect( &clientrect ) )
851 {
852 int top = 0 , left = 0 ;
853
854 MacClientToRootWindow( &left , &top ) ;
855 OffsetRect( &clientrect , left , top ) ;
856
857 MacGetTopLevelWindow()->MacInvalidate( &clientrect , eraseBack ) ;
858 }
859 }
860
861 #if wxUSE_CARET && WXWIN_COMPATIBILITY
862 // ---------------------------------------------------------------------------
863 // Caret manipulation
864 // ---------------------------------------------------------------------------
865
866 void wxWindowMac::CreateCaret(int w, int h)
867 {
868 SetCaret(new wxCaret(this, w, h));
869 }
870
871 void wxWindowMac::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
872 {
873 wxFAIL_MSG("not implemented");
874 }
875
876 void wxWindowMac::ShowCaret(bool show)
877 {
878 wxCHECK_RET( m_caret, "no caret to show" );
879
880 m_caret->Show(show);
881 }
882
883 void wxWindowMac::DestroyCaret()
884 {
885 SetCaret(NULL);
886 }
887
888 void wxWindowMac::SetCaretPos(int x, int y)
889 {
890 wxCHECK_RET( m_caret, "no caret to move" );
891
892 m_caret->Move(x, y);
893 }
894
895 void wxWindowMac::GetCaretPos(int *x, int *y) const
896 {
897 wxCHECK_RET( m_caret, "no caret to get position of" );
898
899 m_caret->GetPosition(x, y);
900 }
901 #endif // wxUSE_CARET
902
903 wxWindowMac *wxGetActiveWindow()
904 {
905 // actually this is a windows-only concept
906 return NULL;
907 }
908
909 // Coordinates relative to the window
910 void wxWindowMac::WarpPointer (int x_pos, int y_pos)
911 {
912 // We really dont move the mouse programmatically under mac
913 }
914
915 const wxBrush& wxWindowMac::MacGetBackgroundBrush()
916 {
917 if ( m_backgroundColour == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
918 {
919 m_macBackgroundBrush.SetMacTheme( kThemeBrushDocumentWindowBackground ) ;
920 }
921 else if ( m_backgroundColour == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) )
922 {
923 // on mac we have the difficult situation, that 3dface gray can be different colours, depending whether
924 // it is on a notebook panel or not, in order to take care of that we walk up the hierarchy until we have
925 // either a non gray background color or a non control window
926
927 WindowRef window = (WindowRef) MacGetRootWindow() ;
928
929 wxWindowMac* parent = GetParent() ;
930 while( parent )
931 {
932 if ( parent->MacGetRootWindow() != window )
933 {
934 // we are in a different window on the mac system
935 parent = NULL ;
936 break ;
937 }
938
939 {
940 if ( parent->m_backgroundColour != wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE )
941 && parent->m_backgroundColour != wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) )
942 {
943 // if we have any other colours in the hierarchy
944 m_macBackgroundBrush.SetColour( parent->m_backgroundColour ) ;
945 break ;
946 }
947 // if we have the normal colours in the hierarchy but another control etc. -> use it's background
948 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
949 {
950 Rect extent = { 0 , 0 , 0 , 0 } ;
951 int x , y ;
952 x = y = 0 ;
953 wxSize size = parent->GetSize() ;
954 parent->MacClientToRootWindow( &x , &y ) ;
955 extent.left = x ;
956 extent.top = y ;
957 extent.top-- ;
958 extent.right = x + size.x ;
959 extent.bottom = y + size.y ;
960 m_macBackgroundBrush.SetMacThemeBackground( kThemeBackgroundTabPane , (WXRECTPTR) &extent ) ; // todo eventually change for inactive
961 break ;
962 }
963 }
964 parent = parent->GetParent() ;
965 }
966 if ( !parent )
967 {
968 m_macBackgroundBrush.SetMacTheme( kThemeBrushDialogBackgroundActive ) ; // todo eventually change for inactive
969 }
970 }
971 else
972 {
973 m_macBackgroundBrush.SetColour( m_backgroundColour ) ;
974 }
975
976 return m_macBackgroundBrush ;
977
978 }
979
980 void wxWindowMac::OnEraseBackground(wxEraseEvent& event)
981 {
982 event.GetDC()->Clear() ;
983 }
984
985 void wxWindowMac::OnNcPaint( wxNcPaintEvent& event )
986 {
987 wxWindowDC dc(this) ;
988 wxMacPortSetter helper(&dc) ;
989
990 MacPaintBorders( dc.m_macLocalOrigin.x , dc.m_macLocalOrigin.y) ;
991 }
992
993 int wxWindowMac::GetScrollPos(int orient) const
994 {
995 if ( orient == wxHORIZONTAL )
996 {
997 if ( m_hScrollBar )
998 return m_hScrollBar->GetThumbPosition() ;
999 }
1000 else
1001 {
1002 if ( m_vScrollBar )
1003 return m_vScrollBar->GetThumbPosition() ;
1004 }
1005 return 0;
1006 }
1007
1008 // This now returns the whole range, not just the number
1009 // of positions that we can scroll.
1010 int wxWindowMac::GetScrollRange(int orient) const
1011 {
1012 if ( orient == wxHORIZONTAL )
1013 {
1014 if ( m_hScrollBar )
1015 return m_hScrollBar->GetRange() ;
1016 }
1017 else
1018 {
1019 if ( m_vScrollBar )
1020 return m_vScrollBar->GetRange() ;
1021 }
1022 return 0;
1023 }
1024
1025 int wxWindowMac::GetScrollThumb(int orient) const
1026 {
1027 if ( orient == wxHORIZONTAL )
1028 {
1029 if ( m_hScrollBar )
1030 return m_hScrollBar->GetThumbSize() ;
1031 }
1032 else
1033 {
1034 if ( m_vScrollBar )
1035 return m_vScrollBar->GetThumbSize() ;
1036 }
1037 return 0;
1038 }
1039
1040 void wxWindowMac::SetScrollPos(int orient, int pos, bool refresh)
1041 {
1042 if ( orient == wxHORIZONTAL )
1043 {
1044 if ( m_hScrollBar )
1045 m_hScrollBar->SetThumbPosition( pos ) ;
1046 }
1047 else
1048 {
1049 if ( m_vScrollBar )
1050 m_vScrollBar->SetThumbPosition( pos ) ;
1051 }
1052 }
1053
1054 void wxWindowMac::MacPaintBorders( int left , int top )
1055 {
1056 if( IsTopLevel() )
1057 return ;
1058
1059 RGBColor white = { 0xFFFF, 0xFFFF , 0xFFFF } ;
1060 RGBColor black = { 0x0000, 0x0000 , 0x0000 } ;
1061 RGBColor face = { 0xDDDD, 0xDDDD , 0xDDDD } ;
1062 RGBColor shadow = { 0x4444, 0x4444 , 0x4444 } ;
1063 PenNormal() ;
1064
1065 if (HasFlag(wxRAISED_BORDER) || HasFlag( wxSUNKEN_BORDER) || HasFlag(wxDOUBLE_BORDER) )
1066 {
1067 #if wxMAC_USE_THEME_BORDER
1068 Rect rect = { top , left , m_height + top , m_width + left } ;
1069 SInt32 border = 0 ;
1070 /*
1071 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
1072 InsetRect( &rect , border , border );
1073 DrawThemeListBoxFrame(&rect,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1074 */
1075
1076 DrawThemePrimaryGroup(&rect ,IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1077 #else
1078 bool sunken = HasFlag( wxSUNKEN_BORDER ) ;
1079 RGBForeColor( &face );
1080 MoveTo( left + 0 , top + m_height - 2 );
1081 LineTo( left + 0 , top + 0 );
1082 LineTo( left + m_width - 2 , top + 0 );
1083
1084 MoveTo( left + 2 , top + m_height - 3 );
1085 LineTo( left + m_width - 3 , top + m_height - 3 );
1086 LineTo( left + m_width - 3 , top + 2 );
1087
1088 RGBForeColor( sunken ? &face : &black );
1089 MoveTo( left + 0 , top + m_height - 1 );
1090 LineTo( left + m_width - 1 , top + m_height - 1 );
1091 LineTo( left + m_width - 1 , top + 0 );
1092
1093 RGBForeColor( sunken ? &shadow : &white );
1094 MoveTo( left + 1 , top + m_height - 3 );
1095 LineTo( left + 1, top + 1 );
1096 LineTo( left + m_width - 3 , top + 1 );
1097
1098 RGBForeColor( sunken ? &white : &shadow );
1099 MoveTo( left + 1 , top + m_height - 2 );
1100 LineTo( left + m_width - 2 , top + m_height - 2 );
1101 LineTo( left + m_width - 2 , top + 1 );
1102
1103 RGBForeColor( sunken ? &black : &face );
1104 MoveTo( left + 2 , top + m_height - 4 );
1105 LineTo( left + 2 , top + 2 );
1106 LineTo( left + m_width - 4 , top + 2 );
1107 #endif
1108 }
1109 else if (HasFlag(wxSIMPLE_BORDER))
1110 {
1111 Rect rect = { top , left , m_height + top , m_width + left } ;
1112 RGBForeColor( &black ) ;
1113 FrameRect( &rect ) ;
1114 }
1115 }
1116
1117 void wxWindowMac::RemoveChild( wxWindowBase *child )
1118 {
1119 if ( child == m_hScrollBar )
1120 m_hScrollBar = NULL ;
1121 if ( child == m_vScrollBar )
1122 m_vScrollBar = NULL ;
1123
1124 wxWindowBase::RemoveChild( child ) ;
1125 }
1126
1127 // New function that will replace some of the above.
1128 void wxWindowMac::SetScrollbar(int orient, int pos, int thumbVisible,
1129 int range, bool refresh)
1130 {
1131 if ( orient == wxHORIZONTAL )
1132 {
1133 if ( m_hScrollBar )
1134 {
1135 if ( range == 0 || thumbVisible >= range )
1136 {
1137 if ( m_hScrollBar->IsShown() )
1138 m_hScrollBar->Show(false) ;
1139 }
1140 else
1141 {
1142 if ( !m_hScrollBar->IsShown() )
1143 m_hScrollBar->Show(true) ;
1144 m_hScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1145 }
1146 }
1147 }
1148 else
1149 {
1150 if ( m_vScrollBar )
1151 {
1152 if ( range == 0 || thumbVisible >= range )
1153 {
1154 if ( m_vScrollBar->IsShown() )
1155 m_vScrollBar->Show(false) ;
1156 }
1157 else
1158 {
1159 if ( !m_vScrollBar->IsShown() )
1160 m_vScrollBar->Show(true) ;
1161 m_vScrollBar->SetScrollbar( pos , thumbVisible , range , thumbVisible , refresh ) ;
1162 }
1163 }
1164 }
1165 MacRepositionScrollBars() ;
1166 }
1167
1168 // Does a physical scroll
1169 void wxWindowMac::ScrollWindow(int dx, int dy, const wxRect *rect)
1170 {
1171 wxClientDC dc(this) ;
1172 wxMacPortSetter helper(&dc) ;
1173
1174 {
1175 int width , height ;
1176 GetClientSize( &width , &height ) ;
1177
1178 Rect scrollrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) , dc.YLOG2DEVMAC(height) , dc.XLOG2DEVMAC(width) } ;
1179 RgnHandle updateRgn = NewRgn() ;
1180 ClipRect( &scrollrect ) ;
1181 if ( rect )
1182 {
1183 Rect r = { dc.YLOG2DEVMAC(rect->y) , dc.XLOG2DEVMAC(rect->x) , dc.YLOG2DEVMAC(rect->y + rect->height) ,
1184 dc.XLOG2DEVMAC(rect->x + rect->width) } ;
1185 SectRect( &scrollrect , &r , &scrollrect ) ;
1186 }
1187 ScrollRect( &scrollrect , dx , dy , updateRgn ) ;
1188 InvalWindowRgn( (WindowRef) MacGetRootWindow() , updateRgn ) ;
1189 DisposeRgn( updateRgn ) ;
1190 }
1191
1192 for (wxNode *node = GetChildren().First(); node; node = node->Next())
1193 {
1194 wxWindowMac *child = (wxWindowMac*)node->Data();
1195 if (child == m_vScrollBar) continue;
1196 if (child == m_hScrollBar) continue;
1197 if (child->IsTopLevel()) continue;
1198 int x,y;
1199 child->GetPosition( &x, &y );
1200 int w,h;
1201 child->GetSize( &w, &h );
1202 child->SetSize( x+dx, y+dy, w, h );
1203 }
1204
1205 }
1206
1207 void wxWindowMac::MacOnScroll(wxScrollEvent &event )
1208 {
1209 if ( event.m_eventObject == m_vScrollBar || event.m_eventObject == m_hScrollBar )
1210 {
1211 wxScrollWinEvent wevent;
1212 wevent.SetPosition(event.GetPosition());
1213 wevent.SetOrientation(event.GetOrientation());
1214 wevent.m_eventObject = this;
1215
1216 if (event.m_eventType == wxEVT_SCROLL_TOP) {
1217 wevent.m_eventType = wxEVT_SCROLLWIN_TOP;
1218 } else
1219 if (event.m_eventType == wxEVT_SCROLL_BOTTOM) {
1220 wevent.m_eventType = wxEVT_SCROLLWIN_BOTTOM;
1221 } else
1222 if (event.m_eventType == wxEVT_SCROLL_LINEUP) {
1223 wevent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
1224 } else
1225 if (event.m_eventType == wxEVT_SCROLL_LINEDOWN) {
1226 wevent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
1227 } else
1228 if (event.m_eventType == wxEVT_SCROLL_PAGEUP) {
1229 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
1230 } else
1231 if (event.m_eventType == wxEVT_SCROLL_PAGEDOWN) {
1232 wevent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
1233 } else
1234 if (event.m_eventType == wxEVT_SCROLL_THUMBTRACK) {
1235 wevent.m_eventType = wxEVT_SCROLLWIN_THUMBTRACK;
1236 }
1237
1238 GetEventHandler()->ProcessEvent(wevent);
1239 }
1240 }
1241
1242 // Get the window with the focus
1243 wxWindowMac *wxWindowBase::FindFocus()
1244 {
1245 return gFocusWindow ;
1246 }
1247
1248 #if WXWIN_COMPATIBILITY
1249 // If nothing defined for this, try the parent.
1250 // E.g. we may be a button loaded from a resource, with no callback function
1251 // defined.
1252 void wxWindowMac::OnCommand(wxWindowMac& win, wxCommandEvent& event)
1253 {
1254 if ( GetEventHandler()->ProcessEvent(event) )
1255 return;
1256 if ( m_parent )
1257 m_parent->GetEventHandler()->OnCommand(win, event);
1258 }
1259 #endif // WXWIN_COMPATIBILITY_2
1260
1261 #if WXWIN_COMPATIBILITY
1262 wxObject* wxWindowMac::GetChild(int number) const
1263 {
1264 // Return a pointer to the Nth object in the Panel
1265 wxNode *node = GetChildren().First();
1266 int n = number;
1267 while (node && n--)
1268 node = node->Next();
1269 if ( node )
1270 {
1271 wxObject *obj = (wxObject *)node->Data();
1272 return(obj);
1273 }
1274 else
1275 return NULL;
1276 }
1277 #endif // WXWIN_COMPATIBILITY
1278
1279 void wxWindowMac::OnSetFocus(wxFocusEvent& event)
1280 {
1281 // panel wants to track the window which was the last to have focus in it,
1282 // so we want to set ourselves as the window which last had focus
1283 //
1284 // notice that it's also important to do it upwards the tree becaus
1285 // otherwise when the top level panel gets focus, it won't set it back to
1286 // us, but to some other sibling
1287
1288 // CS:don't know if this is still needed:
1289 //wxChildFocusEvent eventFocus(this);
1290 //(void)GetEventHandler()->ProcessEvent(eventFocus);
1291
1292 event.Skip();
1293 }
1294
1295 void wxWindowMac::Clear()
1296 {
1297 wxClientDC dc(this);
1298 wxBrush brush(GetBackgroundColour(), wxSOLID);
1299 dc.SetBackground(brush);
1300 dc.Clear();
1301 }
1302
1303 // Setup background and foreground colours correctly
1304 void wxWindowMac::SetupColours()
1305 {
1306 if ( GetParent() )
1307 SetBackgroundColour(GetParent()->GetBackgroundColour());
1308 }
1309
1310 void wxWindowMac::OnIdle(wxIdleEvent& event)
1311 {
1312 /*
1313 // Check if we need to send a LEAVE event
1314 if (m_mouseInWindow)
1315 {
1316 POINT pt;
1317 ::GetCursorPos(&pt);
1318 if (::WindowFromPoint(pt) != (HWND) GetHWND())
1319 {
1320 // Generate a LEAVE event
1321 m_mouseInWindow = FALSE;
1322 MSWOnMouseLeave(pt.x, pt.y, 0);
1323 }
1324 }
1325 */
1326
1327 // This calls the UI-update mechanism (querying windows for
1328 // menu/toolbar/control state information)
1329 UpdateWindowUI();
1330 }
1331
1332 // Raise the window to the top of the Z order
1333 void wxWindowMac::Raise()
1334 {
1335 }
1336
1337 // Lower the window to the bottom of the Z order
1338 void wxWindowMac::Lower()
1339 {
1340 }
1341
1342 void wxWindowMac::DoSetClientSize(int width, int height)
1343 {
1344 if ( width != -1 || height != -1 )
1345 {
1346
1347 if ( width != -1 && m_vScrollBar )
1348 width += MAC_SCROLLBAR_SIZE ;
1349 if ( height != -1 && m_vScrollBar )
1350 height += MAC_SCROLLBAR_SIZE ;
1351
1352 width += MacGetLeftBorderSize( ) + MacGetRightBorderSize( ) ;
1353 height += MacGetTopBorderSize( ) + MacGetBottomBorderSize( ) ;
1354
1355 DoSetSize( -1 , -1 , width , height ) ;
1356 }
1357 }
1358
1359
1360 wxWindowMac* wxWindowMac::s_lastMouseWindow = NULL ;
1361
1362 bool wxWindowMac::MacGetWindowFromPointSub( const wxPoint &point , wxWindowMac** outWin )
1363 {
1364 if ( IsTopLevel() )
1365 {
1366 if ((point.x < 0) || (point.y < 0) ||
1367 (point.x > (m_width)) || (point.y > (m_height)))
1368 return FALSE;
1369 }
1370 else
1371 {
1372 if ((point.x < m_x) || (point.y < m_y) ||
1373 (point.x > (m_x + m_width)) || (point.y > (m_y + m_height)))
1374 return FALSE;
1375 }
1376
1377 WindowRef window = (WindowRef) MacGetRootWindow() ;
1378
1379 wxPoint newPoint( point ) ;
1380
1381 if ( !IsTopLevel() )
1382 {
1383 newPoint.x -= m_x;
1384 newPoint.y -= m_y;
1385 }
1386
1387 for (wxNode *node = GetChildren().First(); node; node = node->Next())
1388 {
1389 wxWindowMac *child = (wxWindowMac*)node->Data();
1390 // added the m_isShown test --dmazzoni
1391 if ( child->MacGetRootWindow() == window && child->m_isShown )
1392 {
1393 if (child->MacGetWindowFromPointSub(newPoint , outWin ))
1394 return TRUE;
1395 }
1396 }
1397
1398 *outWin = this ;
1399 return TRUE;
1400 }
1401
1402 bool wxWindowMac::MacGetWindowFromPoint( const wxPoint &screenpoint , wxWindowMac** outWin )
1403 {
1404 WindowRef window ;
1405
1406 Point pt = { screenpoint.y , screenpoint.x } ;
1407 if ( ::FindWindow( pt , &window ) == 3 )
1408 {
1409
1410 wxWindowMac* win = wxFindWinFromMacWindow( window ) ;
1411 if ( win )
1412 {
1413 // No, this yields the CLIENT are, we need the whole frame. RR.
1414 // point = win->ScreenToClient( point ) ;
1415
1416 GrafPtr port;
1417 ::GetPort( &port ) ;
1418 ::SetPort( UMAGetWindowPort( window ) ) ;
1419 ::GlobalToLocal( &pt ) ;
1420 ::SetPort( port ) ;
1421
1422 wxPoint point( pt.h, pt.v ) ;
1423
1424 return win->MacGetWindowFromPointSub( point , outWin ) ;
1425 }
1426 }
1427 return FALSE ;
1428 }
1429
1430 extern int wxBusyCursorCount ;
1431 static wxWindow *gs_lastWhich = NULL;
1432
1433 bool wxWindowMac::MacDispatchMouseEvent(wxMouseEvent& event)
1434 {
1435 if ((event.m_x < m_x) || (event.m_y < m_y) ||
1436 (event.m_x > (m_x + m_width)) || (event.m_y > (m_y + m_height)))
1437 return FALSE;
1438
1439
1440 if ( IsKindOf( CLASSINFO ( wxStaticBox ) ) || IsKindOf( CLASSINFO( wxSpinCtrl ) ))
1441 return FALSE ;
1442
1443 WindowRef window = (WindowRef) MacGetRootWindow() ;
1444
1445 event.m_x -= m_x;
1446 event.m_y -= m_y;
1447
1448 int x = event.m_x ;
1449 int y = event.m_y ;
1450
1451 for (wxNode *node = GetChildren().First(); node; node = node->Next())
1452 {
1453 wxWindowMac *child = (wxWindowMac*)node->Data();
1454 if ( child->MacGetRootWindow() == window && child->IsShown() && child->IsEnabled() )
1455 {
1456 if (child->MacDispatchMouseEvent(event))
1457 return TRUE;
1458 }
1459 }
1460
1461 event.m_x = x ;
1462 event.m_y = y ;
1463 event.SetEventObject( this ) ;
1464
1465 if ( wxBusyCursorCount == 0 )
1466 {
1467 m_cursor.MacInstall() ;
1468 }
1469
1470 if ( event.GetEventType() == wxEVT_LEFT_DOWN )
1471 {
1472 // set focus to this window
1473 if (AcceptsFocus() && FindFocus()!=this)
1474 SetFocus();
1475 }
1476
1477 #if wxUSE_TOOLTIPS
1478 if ( event.GetEventType() == wxEVT_MOTION
1479 || event.GetEventType() == wxEVT_ENTER_WINDOW
1480 || event.GetEventType() == wxEVT_LEAVE_WINDOW )
1481 wxToolTip::RelayEvent( this , event);
1482 #endif // wxUSE_TOOLTIPS
1483
1484 if (gs_lastWhich != this)
1485 {
1486 gs_lastWhich = this;
1487
1488 // Double clicks must always occur on the same window
1489 if (event.GetEventType() == wxEVT_LEFT_DCLICK)
1490 event.SetEventType( wxEVT_LEFT_DOWN );
1491 if (event.GetEventType() == wxEVT_RIGHT_DCLICK)
1492 event.SetEventType( wxEVT_RIGHT_DOWN );
1493
1494 // Same for mouse up events
1495 if (event.GetEventType() == wxEVT_LEFT_UP)
1496 return TRUE;
1497 if (event.GetEventType() == wxEVT_RIGHT_UP)
1498 return TRUE;
1499 }
1500
1501 GetEventHandler()->ProcessEvent( event ) ;
1502
1503 return TRUE;
1504 }
1505
1506 wxString wxWindowMac::MacGetToolTipString( wxPoint &pt )
1507 {
1508 if ( m_tooltip )
1509 {
1510 return m_tooltip->GetTip() ;
1511 }
1512 return "" ;
1513 }
1514
1515 void wxWindowMac::Update()
1516 {
1517 wxTopLevelWindowMac* win = MacGetTopLevelWindow( ) ;
1518 if ( win )
1519 {
1520 win->MacUpdate( 0 ) ;
1521 #if TARGET_API_MAC_CARBON
1522 if ( QDIsPortBuffered( GetWindowPort( (WindowRef) win->MacGetWindowRef() ) ) )
1523 {
1524 QDFlushPortBuffer( GetWindowPort( (WindowRef) win->MacGetWindowRef() ) , NULL ) ;
1525 }
1526 #endif
1527 }
1528 }
1529
1530 wxTopLevelWindowMac* wxWindowMac::MacGetTopLevelWindow() const
1531 {
1532 wxTopLevelWindowMac* win = NULL ;
1533 WindowRef window = (WindowRef) MacGetRootWindow() ;
1534 if ( window )
1535 {
1536 win = wxFindWinFromMacWindow( window ) ;
1537 }
1538 return win ;
1539 }
1540
1541 const wxRegion& wxWindowMac::MacGetVisibleRegion()
1542 {
1543 RgnHandle visRgn = NewRgn() ;
1544 RgnHandle tempRgn = NewRgn() ;
1545
1546 SetRectRgn( visRgn , 0 , 0 , m_width , m_height ) ;
1547
1548 //TODO : as soon as the new scheme has proven to work correctly, move this to wxStaticBox
1549 if ( IsKindOf( CLASSINFO( wxStaticBox ) ) )
1550 {
1551 int borderTop = 14 ;
1552 int borderOther = 4 ;
1553
1554 SetRectRgn( tempRgn , borderOther , borderTop , m_width - borderOther , m_height - borderOther ) ;
1555 DiffRgn( visRgn , tempRgn , visRgn ) ;
1556 }
1557
1558 if ( !IsTopLevel() )
1559 {
1560 wxWindow* parent = GetParent() ;
1561 while( parent )
1562 {
1563 wxSize size = parent->GetSize() ;
1564 int x , y ;
1565 x = y = 0 ;
1566 parent->MacWindowToRootWindow( &x, &y ) ;
1567 MacRootWindowToWindow( &x , &y ) ;
1568 SetRectRgn( tempRgn , x , y , x + size.x , y + size.y ) ;
1569 SectRgn( visRgn , tempRgn , visRgn ) ;
1570 if ( parent->IsTopLevel() )
1571 break ;
1572 parent = parent->GetParent() ;
1573 }
1574 }
1575 if ( GetWindowStyle() & wxCLIP_CHILDREN )
1576 {
1577 for (wxNode *node = GetChildren().First(); node; node = node->Next())
1578 {
1579 wxWindowMac *child = (wxWindowMac*)node->Data();
1580
1581 if ( !child->IsTopLevel() && child->IsShown() )
1582 {
1583 SetRectRgn( tempRgn , child->m_x , child->m_y , child->m_x + child->m_width , child->m_y + child->m_height ) ;
1584 DiffRgn( visRgn , tempRgn , visRgn ) ;
1585 }
1586 }
1587 }
1588
1589 if ( (GetWindowStyle() & wxCLIP_SIBLINGS) && GetParent() )
1590 {
1591 bool thisWindowThrough = false ;
1592 for (wxNode *node = GetParent()->GetChildren().First(); node; node = node->Next())
1593 {
1594 wxWindowMac *sibling = (wxWindowMac*)node->Data();
1595 if ( sibling == this )
1596 {
1597 thisWindowThrough = true ;
1598 continue ;
1599 }
1600 if( !thisWindowThrough )
1601 {
1602 continue ;
1603 }
1604
1605 if ( !sibling->IsTopLevel() && sibling->IsShown() )
1606 {
1607 SetRectRgn( tempRgn , sibling->m_x - m_x , sibling->m_y - m_y , sibling->m_x + sibling->m_width - m_x , sibling->m_y + sibling->m_height - m_y ) ;
1608 DiffRgn( visRgn , tempRgn , visRgn ) ;
1609 }
1610 }
1611 }
1612 m_macVisibleRegion = visRgn ;
1613 DisposeRgn( visRgn ) ;
1614 DisposeRgn( tempRgn ) ;
1615 return m_macVisibleRegion ;
1616 }
1617
1618 void wxWindowMac::MacRedraw( WXHRGN updatergnr , long time, bool erase)
1619 {
1620 RgnHandle updatergn = (RgnHandle) updatergnr ;
1621 // updatergn is always already clipped to our boundaries
1622 // it is in window coordinates, not in client coordinates
1623
1624 WindowRef window = (WindowRef) MacGetRootWindow() ;
1625
1626 {
1627 // ownUpdateRgn is the area that this window has to repaint, it is in window coordinates
1628 RgnHandle ownUpdateRgn = NewRgn() ;
1629 CopyRgn( updatergn , ownUpdateRgn ) ;
1630
1631 SectRgn( ownUpdateRgn , (RgnHandle) MacGetVisibleRegion().GetWXHRGN() , ownUpdateRgn ) ;
1632
1633 // newupdate is the update region in client coordinates
1634 RgnHandle newupdate = NewRgn() ;
1635 wxSize point = GetClientSize() ;
1636 wxPoint origin = GetClientAreaOrigin() ;
1637 SetRectRgn( newupdate , origin.x , origin.y , origin.x + point.x , origin.y+point.y ) ;
1638 SectRgn( newupdate , ownUpdateRgn , newupdate ) ;
1639 OffsetRgn( newupdate , -origin.x , -origin.y ) ;
1640 m_updateRegion = newupdate ;
1641 DisposeRgn( newupdate ) ; // it's been cloned to m_updateRegion
1642
1643 if ( erase && !EmptyRgn(ownUpdateRgn) )
1644 {
1645 wxWindowDC dc(this);
1646 dc.SetClippingRegion(wxRegion(ownUpdateRgn));
1647 wxEraseEvent eevent( GetId(), &dc );
1648 eevent.SetEventObject( this );
1649 GetEventHandler()->ProcessEvent( eevent );
1650
1651 wxNcPaintEvent eventNc( GetId() );
1652 eventNc.SetEventObject( this );
1653 GetEventHandler()->ProcessEvent( eventNc );
1654 }
1655 DisposeRgn( ownUpdateRgn ) ;
1656 if ( !m_updateRegion.Empty() )
1657 {
1658 wxPaintEvent event;
1659 event.m_timeStamp = time ;
1660 event.SetEventObject(this);
1661 GetEventHandler()->ProcessEvent(event);
1662 }
1663 }
1664
1665 // now intersect for each of the children their rect with the updateRgn and call MacRedraw recursively
1666
1667 RgnHandle childupdate = NewRgn() ;
1668 for (wxNode *node = GetChildren().First(); node; node = node->Next())
1669 {
1670 // calculate the update region for the child windows by intersecting the window rectangle with our own
1671 // passed in update region and then offset it to be client-wise window coordinates again
1672 wxWindowMac *child = (wxWindowMac*)node->Data();
1673 SetRectRgn( childupdate , child->m_x , child->m_y , child->m_x + child->m_width , child->m_y + child->m_height ) ;
1674 SectRgn( childupdate , updatergn , childupdate ) ;
1675 OffsetRgn( childupdate , -child->m_x , -child->m_y ) ;
1676 if ( child->MacGetRootWindow() == window && child->IsShown() && !EmptyRgn( childupdate ) )
1677 {
1678 // because dialogs may also be children
1679 child->MacRedraw( childupdate , time , erase ) ;
1680 }
1681 }
1682 DisposeRgn( childupdate ) ;
1683 // eventually a draw grow box here
1684
1685 }
1686
1687 WXHWND wxWindowMac::MacGetRootWindow() const
1688 {
1689 wxWindowMac *iter = (wxWindowMac*)this ;
1690
1691 while( iter )
1692 {
1693 if ( iter->IsTopLevel() )
1694 return ((wxTopLevelWindow*)iter)->MacGetWindowRef() ;
1695
1696 iter = iter->GetParent() ;
1697 }
1698 wxASSERT_MSG( 1 , "No valid mac root window" ) ;
1699 return NULL ;
1700 }
1701
1702 void wxWindowMac::MacCreateScrollBars( long style )
1703 {
1704 wxASSERT_MSG( m_vScrollBar == NULL && m_hScrollBar == NULL , "attempt to create window twice" ) ;
1705
1706 bool hasBoth = ( style & wxVSCROLL ) && ( style & wxHSCROLL ) ;
1707 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1: 0 ;
1708 int width, height ;
1709 GetClientSize( &width , &height ) ;
1710
1711 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
1712 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
1713 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
1714 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
1715
1716 m_vScrollBar = new wxScrollBar(this, wxWINDOW_VSCROLL, vPoint,
1717 vSize , wxVERTICAL);
1718
1719 if ( style & wxVSCROLL )
1720 {
1721
1722 }
1723 else
1724 {
1725 m_vScrollBar->Show(false) ;
1726 }
1727 m_hScrollBar = new wxScrollBar(this, wxWINDOW_HSCROLL, hPoint,
1728 hSize , wxHORIZONTAL);
1729 if ( style & wxHSCROLL )
1730 {
1731 }
1732 else
1733 {
1734 m_hScrollBar->Show(false) ;
1735 }
1736
1737 // because the create does not take into account the client area origin
1738 MacRepositionScrollBars() ; // we might have a real position shift
1739 }
1740
1741 void wxWindowMac::MacRepositionScrollBars()
1742 {
1743 bool hasBoth = ( m_hScrollBar && m_hScrollBar->IsShown()) && ( m_vScrollBar && m_vScrollBar->IsShown()) ;
1744 int adjust = hasBoth ? MAC_SCROLLBAR_SIZE - 1 : 0 ;
1745
1746 // get real client area
1747
1748 int width = m_width ;
1749 int height = m_height ;
1750
1751 width -= MacGetLeftBorderSize() + MacGetRightBorderSize();
1752 height -= MacGetTopBorderSize() + MacGetBottomBorderSize();
1753
1754 wxPoint vPoint(width-MAC_SCROLLBAR_SIZE, 0) ;
1755 wxSize vSize(MAC_SCROLLBAR_SIZE, height - adjust) ;
1756 wxPoint hPoint(0 , height-MAC_SCROLLBAR_SIZE ) ;
1757 wxSize hSize( width - adjust, MAC_SCROLLBAR_SIZE) ;
1758
1759 int x = 0 ;
1760 int y = 0 ;
1761 int w = m_width ;
1762 int h = m_height ;
1763
1764 MacClientToRootWindow( &x , &y ) ;
1765 MacClientToRootWindow( &w , &h ) ;
1766
1767 wxWindowMac *iter = (wxWindowMac*)this ;
1768
1769 int totW = 10000 , totH = 10000;
1770 while( iter )
1771 {
1772 if ( iter->IsTopLevel() )
1773 {
1774 totW = iter->m_width ;
1775 totH = iter->m_height ;
1776 break ;
1777 }
1778
1779 iter = iter->GetParent() ;
1780 }
1781
1782 if ( x == 0 )
1783 {
1784 hPoint.x = -1 ;
1785 hSize.x += 1 ;
1786 }
1787 if ( y == 0 )
1788 {
1789 vPoint.y = -1 ;
1790 vSize.y += 1 ;
1791 }
1792
1793 if ( w-x >= totW )
1794 {
1795 hSize.x += 1 ;
1796 vPoint.x += 1 ;
1797 }
1798
1799 if ( h-y >= totH )
1800 {
1801 vSize.y += 1 ;
1802 hPoint.y += 1 ;
1803 }
1804
1805 if ( m_vScrollBar )
1806 {
1807 m_vScrollBar->SetSize( vPoint.x , vPoint.y, vSize.x, vSize.y , wxSIZE_ALLOW_MINUS_ONE);
1808 }
1809 if ( m_hScrollBar )
1810 {
1811 m_hScrollBar->SetSize( hPoint.x , hPoint.y, hSize.x, hSize.y, wxSIZE_ALLOW_MINUS_ONE);
1812 }
1813 }
1814
1815 bool wxWindowMac::AcceptsFocus() const
1816 {
1817 return MacCanFocus() && wxWindowBase::AcceptsFocus();
1818 }
1819
1820 WXWidget wxWindowMac::MacGetContainerForEmbedding()
1821 {
1822 return GetParent()->MacGetContainerForEmbedding() ;
1823 }
1824
1825 void wxWindowMac::MacSuperChangedPosition()
1826 {
1827 // only window-absolute structures have to be moved i.e. controls
1828
1829 wxNode *node = GetChildren().First();
1830 while ( node )
1831 {
1832 wxWindowMac *child = (wxWindowMac *)node->Data();
1833 child->MacSuperChangedPosition() ;
1834 node = node->Next();
1835 }
1836 }
1837
1838 void wxWindowMac::MacTopLevelWindowChangedPosition()
1839 {
1840 // only screen-absolute structures have to be moved i.e. glcanvas
1841
1842 wxNode *node = GetChildren().First();
1843 while ( node )
1844 {
1845 wxWindowMac *child = (wxWindowMac *)node->Data();
1846 child->MacTopLevelWindowChangedPosition() ;
1847 node = node->Next();
1848 }
1849 }
1850 long wxWindowMac::MacGetLeftBorderSize( ) const
1851 {
1852 if( IsTopLevel() )
1853 return 0 ;
1854
1855 if (m_windowStyle & wxRAISED_BORDER || m_windowStyle & wxSUNKEN_BORDER )
1856 {
1857 SInt32 border = 3 ;
1858 #if wxMAC_USE_THEME_BORDER
1859 #if TARGET_CARBON
1860 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
1861 #endif
1862 #endif
1863 return border ;
1864 }
1865 else if ( m_windowStyle &wxDOUBLE_BORDER)
1866 {
1867 SInt32 border = 3 ;
1868 #if wxMAC_USE_THEME_BORDER
1869 #if TARGET_CARBON
1870 GetThemeMetric( kThemeMetricListBoxFrameOutset , &border ) ;
1871 #endif
1872 #endif
1873 return border ;
1874 }
1875 else if (m_windowStyle &wxSIMPLE_BORDER)
1876 {
1877 return 1 ;
1878 }
1879 return 0 ;
1880 }
1881
1882 long wxWindowMac::MacGetRightBorderSize( ) const
1883 {
1884 // they are all symmetric in mac themes
1885 return MacGetLeftBorderSize() ;
1886 }
1887
1888 long wxWindowMac::MacGetTopBorderSize( ) const
1889 {
1890 // they are all symmetric in mac themes
1891 return MacGetLeftBorderSize() ;
1892 }
1893
1894 long wxWindowMac::MacGetBottomBorderSize( ) const
1895 {
1896 // they are all symmetric in mac themes
1897 return MacGetLeftBorderSize() ;
1898 }
1899
1900 long wxWindowMac::MacRemoveBordersFromStyle( long style )
1901 {
1902 return style & ~( wxDOUBLE_BORDER | wxSUNKEN_BORDER | wxRAISED_BORDER | wxBORDER | wxSTATIC_BORDER ) ;
1903 }
1904
1905 // Find the wxWindowMac at the current mouse position, returning the mouse
1906 // position.
1907 wxWindowMac* wxFindWindowAtPointer(wxPoint& pt)
1908 {
1909 pt = wxGetMousePosition();
1910 wxWindowMac* found = wxFindWindowAtPoint(pt);
1911 return found;
1912 }
1913
1914 // Get the current mouse position.
1915 wxPoint wxGetMousePosition()
1916 {
1917 int x, y;
1918 wxGetMousePosition(& x, & y);
1919 return wxPoint(x, y);
1920 }
1921