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