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