]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/notebmac.cpp
Make mouse capture checking asserts stronger and more detailed.
[wxWidgets.git] / src / osx / carbon / notebmac.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/notebmac.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_NOTEBOOK
14
15 #include "wx/notebook.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/string.h"
19 #include "wx/log.h"
20 #include "wx/app.h"
21 #include "wx/image.h"
22 #endif
23
24 #include "wx/string.h"
25 #include "wx/imaglist.h"
26 #include "wx/osx/private.h"
27
28
29 // check that the page index is valid
30 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
31
32 class wxCarbonTabView : public wxMacControl
33 {
34 public:
35 wxCarbonTabView( wxWindowMac* peer ) : wxMacControl(peer )
36 {
37 }
38
39 // Added by Mark Newsam
40 // When a page is added or deleted to the notebook this function updates
41 // information held in the control so that it matches the order
42 // the user would expect.
43 //
44 void SetupTabs( const wxNotebook& notebook)
45 {
46 const size_t countPages = notebook.GetPageCount();
47 SetMaximum( countPages ) ;
48
49 wxNotebookPage *page;
50 ControlTabInfoRecV1 info;
51
52 for (size_t ii = 0; ii < countPages; ii++)
53 {
54 page = (wxNotebookPage*) notebook.GetPage(ii);
55 info.version = kControlTabInfoVersionOne;
56 info.iconSuiteID = 0;
57 wxCFStringRef cflabel( page->GetLabel(), page->GetFont().GetEncoding() ) ;
58 info.name = cflabel ;
59 SetData<ControlTabInfoRecV1>( ii + 1, kControlTabInfoTag, &info ) ;
60
61 if ( notebook.GetImageList() && notebook.GetPageImage(ii) >= 0 )
62 {
63 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( ii ) ) ;
64 if ( bmap.IsOk() )
65 {
66 ControlButtonContentInfo info ;
67
68 wxMacCreateBitmapButton( &info, bmap ) ;
69
70 OSStatus err = SetData<ControlButtonContentInfo>( ii + 1, kControlTabImageContentTag, &info );
71 if ( err != noErr )
72 {
73 wxFAIL_MSG("Error when setting icon on tab");
74 }
75
76 wxMacReleaseBitmapButton( &info ) ;
77 }
78 }
79 SetTabEnabled( ii + 1, true ) ;
80 }
81 }
82
83 int TabHitTest(const wxPoint & pt, long* flags)
84 {
85 int resultV = wxNOT_FOUND;
86
87 wxNotebook *notebookpeer = wxDynamicCast( GetWXPeer() , wxNotebook );
88 if ( NULL == notebookpeer )
89 return wxNOT_FOUND;
90
91 const int countPages = notebookpeer->GetPageCount();
92
93 // we have to convert from Client to Window relative coordinates
94 wxPoint adjustedPt = pt + GetWXPeer()->GetClientAreaOrigin();
95 // and now to HIView native ones
96 adjustedPt.x -= GetWXPeer()->MacGetLeftBorderSize() ;
97 adjustedPt.y -= GetWXPeer()->MacGetTopBorderSize() ;
98
99 HIPoint hipoint= { adjustedPt.x , adjustedPt.y } ;
100 HIViewPartCode outPart = 0 ;
101 OSStatus err = HIViewGetPartHit( GetControlRef(), &hipoint, &outPart );
102
103 int max = GetMaximum() ;
104 if ( outPart == 0 && max > 0 )
105 {
106 // this is a hack, as unfortunately a hit on an already selected tab returns 0,
107 // so we have to go some extra miles to make sure we select something different
108 // and try again ..
109 int val = GetValue() ;
110 int maxval = max ;
111 if ( max == 1 )
112 {
113 SetMaximum( 2 ) ;
114 maxval = 2 ;
115 }
116
117 if ( val == 1 )
118 SetValue( maxval ) ;
119 else
120 SetValue( 1 ) ;
121
122 err = HIViewGetPartHit( GetControlRef(), &hipoint, &outPart );
123
124 SetValue( val ) ;
125 if ( max == 1 )
126 SetMaximum( 1 ) ;
127 }
128
129 if ( outPart >= 1 && outPart <= countPages )
130 resultV = outPart - 1 ;
131
132 if (flags != NULL)
133 {
134 *flags = 0;
135
136 // we cannot differentiate better
137 if (resultV >= 0)
138 *flags |= wxBK_HITTEST_ONLABEL;
139 else
140 *flags |= wxBK_HITTEST_NOWHERE;
141 }
142
143 return resultV;
144
145 }
146
147 };
148
149 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
150 wxWindowMac* parent,
151 wxWindowID WXUNUSED(id),
152 const wxPoint& pos,
153 const wxSize& size,
154 long style,
155 long WXUNUSED(extraStyle))
156 {
157 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
158
159 if ( bounds.right <= bounds.left )
160 bounds.right = bounds.left + 100;
161 if ( bounds.bottom <= bounds.top )
162 bounds.bottom = bounds.top + 100;
163
164 UInt16 tabstyle = kControlTabDirectionNorth;
165 if ( style & wxBK_LEFT )
166 tabstyle = kControlTabDirectionWest;
167 else if ( style & wxBK_RIGHT )
168 tabstyle = kControlTabDirectionEast;
169 else if ( style & wxBK_BOTTOM )
170 tabstyle = kControlTabDirectionSouth;
171
172 ControlTabSize tabsize;
173 switch (wxpeer->GetWindowVariant())
174 {
175 case wxWINDOW_VARIANT_MINI:
176 tabsize = 3 ;
177 break;
178
179 case wxWINDOW_VARIANT_SMALL:
180 tabsize = kControlTabSizeSmall;
181 break;
182
183 default:
184 tabsize = kControlTabSizeLarge;
185 break;
186 }
187
188 wxMacControl* peer = new wxCarbonTabView( wxpeer );
189
190 OSStatus err = CreateTabsControl(
191 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
192 tabsize, tabstyle, 0, NULL, peer->GetControlRefAddr() );
193 verify_noerr( err );
194
195 return peer;
196 }
197
198 #endif