]> git.saurik.com Git - wxWidgets.git/blob - src/mac/notebook.cpp
ANSI 'C' fix (decs in "for" clauses no longer have any scope outside clause itself)
[wxWidgets.git] / src / mac / notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: notebook.cpp
3 // Purpose: implementation of wxNotebook
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "notebook.h"
21 #endif
22
23 #include <wx/string.h>
24 #include <wx/log.h>
25 #include <wx/imaglist.h>
26 #include <wx/notebook.h>
27 #include <wx/mac/uma.h>
28 // ----------------------------------------------------------------------------
29 // macros
30 // ----------------------------------------------------------------------------
31
32 // check that the page index is valid
33 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
34
35 const short kwxMacTabLeftMargin = 16 ;
36 const short kwxMacTabTopMargin = 30 ;
37 const short kwxMacTabRightMargin = 16 ;
38 const short kwxMacTabBottomMargin = 16 ;
39
40 // ----------------------------------------------------------------------------
41 // event table
42 // ----------------------------------------------------------------------------
43
44 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
45 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
46
47 EVT_SIZE(wxNotebook::OnSize)
48 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
49 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
50 END_EVENT_TABLE()
51
52 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
53 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
54
55 // ============================================================================
56 // implementation
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // wxNotebook construction
61 // ----------------------------------------------------------------------------
62
63 // common part of all ctors
64 void wxNotebook::Init()
65 {
66 m_pImageList = NULL;
67 m_nSelection = -1;
68 }
69
70 // default for dynamic class
71 wxNotebook::wxNotebook()
72 {
73 Init();
74 }
75
76 // the same arguments as for wxControl
77 wxNotebook::wxNotebook(wxWindow *parent,
78 wxWindowID id,
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxString& name)
83 {
84 Init();
85
86 Create(parent, id, pos, size, style, name);
87 }
88
89 // Create() function
90 bool wxNotebook::Create(wxWindow *parent,
91 wxWindowID id,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 Rect bounds ;
98 Str255 title ;
99
100 MacPreControlCreate( parent , id , "" , pos , size ,style, *((wxValidator*)NULL) , name , &bounds , title ) ;
101
102 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , true , 0 , 0 , 1,
103 kControlTabSmallProc , (long) this ) ;
104
105 MacPostControlCreate() ;
106 return TRUE ;
107 }
108
109 // dtor
110 wxNotebook::~wxNotebook()
111 {
112 }
113
114 // ----------------------------------------------------------------------------
115 // wxNotebook accessors
116 // ----------------------------------------------------------------------------
117 int wxNotebook::GetPageCount() const
118 {
119 return m_aPages.Count();
120 }
121
122 int wxNotebook::GetRowCount() const
123 {
124 return 1;
125 }
126
127 int wxNotebook::SetSelection(int nPage)
128 {
129 wxASSERT( IS_VALID_PAGE(nPage) );
130
131 ChangePage(m_nSelection, nPage);
132 SetControlValue( m_macControl , m_nSelection + 1 ) ;
133 // Boolean enabled = true ;
134
135 // SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
136 return m_nSelection;
137 }
138
139 void wxNotebook::AdvanceSelection(bool bForward)
140 {
141 int nSel = GetSelection();
142 int nMax = GetPageCount() - 1;
143 if ( bForward )
144 SetSelection(nSel == nMax ? 0 : nSel + 1);
145 else
146 SetSelection(nSel == 0 ? nMax : nSel - 1);
147 }
148
149 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
150 {
151 wxASSERT( IS_VALID_PAGE(nPage) );
152
153 // TODO
154 return FALSE;
155 }
156
157 wxString wxNotebook::GetPageText(int nPage) const
158 {
159 wxASSERT( IS_VALID_PAGE(nPage) );
160
161 // TODO
162 return wxString("");
163 }
164
165 int wxNotebook::GetPageImage(int nPage) const
166 {
167 wxASSERT( IS_VALID_PAGE(nPage) );
168
169 // TODO
170 return 0;
171 }
172
173 bool wxNotebook::SetPageImage(int nPage, int nImage)
174 {
175 wxASSERT( IS_VALID_PAGE(nPage) );
176
177 // TODO
178 return FALSE;
179 }
180
181 void wxNotebook::SetImageList(wxImageList* imageList)
182 {
183 m_pImageList = imageList;
184 // TODO
185 }
186
187 // ----------------------------------------------------------------------------
188 // wxNotebook operations
189 // ----------------------------------------------------------------------------
190
191 // remove one page from the notebook
192 bool wxNotebook::DeletePage(int nPage)
193 {
194 wxCHECK( IS_VALID_PAGE(nPage), FALSE );
195
196 // TODO: delete native widget page
197
198 delete m_aPages[nPage];
199 m_aPages.Remove(nPage);
200
201 return TRUE;
202 }
203
204 // remove one page from the notebook, without deleting the window
205 bool wxNotebook::RemovePage(int nPage)
206 {
207 wxCHECK( IS_VALID_PAGE(nPage), FALSE );
208
209 m_aPages.Remove(nPage);
210
211 return TRUE;
212 }
213
214 // remove all pages
215 bool wxNotebook::DeleteAllPages()
216 {
217 // TODO: delete native widget pages
218
219 int nPageCount = GetPageCount();
220 int nPage;
221 for ( nPage = 0; nPage < nPageCount; nPage++ )
222 delete m_aPages[nPage];
223
224 m_aPages.Clear();
225
226 return TRUE;
227 }
228
229 // add a page to the notebook
230 bool wxNotebook::AddPage(wxNotebookPage *pPage,
231 const wxString& strText,
232 bool bSelect,
233 int imageId)
234 {
235 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
236 }
237
238 // same as AddPage() but does it at given position
239 bool wxNotebook::InsertPage(int nPage,
240 wxNotebookPage *pPage,
241 const wxString& strText,
242 bool bSelect,
243 int imageId)
244 {
245 wxASSERT( pPage != NULL );
246 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
247
248 ControlTabInfoRec tie ;
249 Boolean enabled = true ;
250 if ( nPage + 1 > GetControlMaximum( m_macControl ) )
251 {
252 SetControlMaximum( m_macControl , nPage + 1 ) ;
253 }
254
255 tie.version = 0 ;
256 tie.iconSuiteID = 0 ;
257 strcpy( (char*) tie.name , strText ) ;
258 c2pstr( (char*) tie.name ) ;
259 SetControlData( m_macControl, nPage + 1, kControlTabInfoTag , sizeof( ControlTabInfoRec) , (char*) &tie ) ;
260 SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
261
262 // save the pointer to the page
263 m_aPages.Insert(pPage, nPage);
264
265 // some page must be selected: either this one or the first one if there is
266 // still no selection
267 if ( bSelect )
268 m_nSelection = nPage;
269 else if ( m_nSelection == -1 )
270 m_nSelection = 0;
271
272 // don't show pages by default (we'll need to adjust their size first)
273 pPage->Show( FALSE ) ;
274
275 return TRUE;
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxNotebook callbacks
280 // ----------------------------------------------------------------------------
281
282 // @@@ OnSize() is used for setting the font when it's called for the first
283 // time because doing it in ::Create() doesn't work (for unknown reasons)
284 void wxNotebook::OnSize(wxSizeEvent& event)
285 {
286 static bool s_bFirstTime = TRUE;
287 if ( s_bFirstTime ) {
288 // TODO: any first-time-size processing.
289 s_bFirstTime = FALSE;
290 }
291
292 // TODO: all this may or may not be necessary for your platform
293
294 // emulate page change (it's esp. important to do it first time because
295 // otherwise our page would stay invisible)
296 int nSel = m_nSelection;
297 m_nSelection = -1;
298 SetSelection(nSel);
299
300 // fit the notebook page to the tab control's display area
301 int w, h;
302 GetSize(&w, &h);
303
304 unsigned int nCount = m_aPages.Count();
305 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
306 wxNotebookPage *pPage = m_aPages[nPage];
307 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin, w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
308 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
309 // pPage->SetSize(0, 0, w, h);
310 if ( pPage->GetAutoLayout() )
311 pPage->Layout();
312 }
313
314 // Processing continues to next OnSize
315 event.Skip();
316 }
317
318 void wxNotebook::OnSelChange(wxNotebookEvent& event)
319 {
320 // is it our tab control?
321 if ( event.GetEventObject() == this )
322 ChangePage(event.GetOldSelection(), event.GetSelection());
323
324 // we want to give others a chance to process this message as well
325 event.Skip();
326 }
327
328 void wxNotebook::OnSetFocus(wxFocusEvent& event)
329 {
330 // set focus to the currently selected page if any
331 if ( m_nSelection != -1 )
332 m_aPages[m_nSelection]->SetFocus();
333
334 event.Skip();
335 }
336
337 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
338 {
339 if ( event.IsWindowChange() ) {
340 // change pages
341 AdvanceSelection(event.GetDirection());
342 }
343 else {
344 // pass to the parent
345 if ( GetParent() ) {
346 event.SetCurrentFocus(this);
347 GetParent()->ProcessEvent(event);
348 }
349 }
350 }
351
352 // ----------------------------------------------------------------------------
353 // wxNotebook base class virtuals
354 // ----------------------------------------------------------------------------
355
356 // override these 2 functions to do nothing: everything is done in OnSize
357
358 void wxNotebook::SetConstraintSizes(bool /* recurse */)
359 {
360 // don't set the sizes of the pages - their correct size is not yet known
361 wxControl::SetConstraintSizes(FALSE);
362 }
363
364 bool wxNotebook::DoPhase(int /* nPhase */)
365 {
366 return TRUE;
367 }
368
369 void wxNotebook::Command(wxCommandEvent& event)
370 {
371 wxFAIL_MSG("wxNotebook::Command not implemented");
372 }
373
374 // ----------------------------------------------------------------------------
375 // wxNotebook helper functions
376 // ----------------------------------------------------------------------------
377
378 // hide the currently active panel and show the new one
379 void wxNotebook::ChangePage(int nOldSel, int nSel)
380 {
381 // it's not an error (the message may be generated by the tab control itself)
382 // and it may happen - just do nothing
383 if ( nSel == nOldSel )
384 {
385 wxNotebookPage *pPage = m_aPages[nSel];
386 pPage->Show(FALSE);
387 pPage->Show(TRUE);
388 pPage->SetFocus();
389 return;
390 }
391
392 if ( nOldSel != -1 ) {
393 m_aPages[nOldSel]->Show(FALSE);
394 }
395
396 wxNotebookPage *pPage = m_aPages[nSel];
397 pPage->Show(TRUE);
398 pPage->SetFocus();
399
400 m_nSelection = nSel;
401 }
402
403 void wxNotebook::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
404 {
405 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControlValue(m_macControl) - 1, m_nSelection);
406 event.SetEventObject(this);
407
408 ProcessEvent(event);
409 }
410