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