]> git.saurik.com Git - wxWidgets.git/blob - src/mac/notebook.cpp
wxMac:
[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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
46
47 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
48 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange)
49
50 EVT_SIZE(wxNotebook::OnSize)
51 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
52 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
53 END_EVENT_TABLE()
54
55 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
56 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // wxNotebook construction
64 // ----------------------------------------------------------------------------
65
66 // common part of all ctors
67 void wxNotebook::Init()
68 {
69 m_pImageList = NULL;
70 m_nSelection = -1;
71 }
72
73 // default for dynamic class
74 wxNotebook::wxNotebook()
75 {
76 Init();
77 }
78
79 // the same arguments as for wxControl
80 wxNotebook::wxNotebook(wxWindow *parent,
81 wxWindowID id,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style,
85 const wxString& name)
86 {
87 Init();
88
89 Create(parent, id, pos, size, style, name);
90 }
91
92 // Create() function
93 bool wxNotebook::Create(wxWindow *parent,
94 wxWindowID id,
95 const wxPoint& pos,
96 const wxSize& size,
97 long style,
98 const wxString& name)
99 {
100 Rect bounds ;
101 Str255 title ;
102
103 MacPreControlCreate( parent , id , "" , pos , size ,style, *((wxValidator*)NULL) , name , &bounds , title ) ;
104
105 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , true , 0 , 0 , 1,
106 kControlTabSmallProc , (long) this ) ;
107
108 MacPostControlCreate() ;
109 return TRUE ;
110 }
111
112 // dtor
113 wxNotebook::~wxNotebook()
114 {
115 }
116
117 // ----------------------------------------------------------------------------
118 // wxNotebook accessors
119 // ----------------------------------------------------------------------------
120 int wxNotebook::GetPageCount() const
121 {
122 return m_aPages.Count();
123 }
124
125 int wxNotebook::GetRowCount() const
126 {
127 return 1;
128 }
129
130 int wxNotebook::SetSelection(int nPage)
131 {
132 wxASSERT( IS_VALID_PAGE(nPage) );
133
134 ChangePage(m_nSelection, nPage);
135 SetControlValue( m_macControl , m_nSelection + 1 ) ;
136 // Boolean enabled = true ;
137
138 // SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
139 return m_nSelection;
140 }
141
142 void wxNotebook::AdvanceSelection(bool bForward)
143 {
144 int nSel = GetSelection();
145 int nMax = GetPageCount() - 1;
146 if ( bForward )
147 SetSelection(nSel == nMax ? 0 : nSel + 1);
148 else
149 SetSelection(nSel == 0 ? nMax : nSel - 1);
150 }
151
152 bool wxNotebook::SetPageText(int nPage, const wxString& strText)
153 {
154 wxASSERT( IS_VALID_PAGE(nPage) );
155
156 // TODO
157 return FALSE;
158 }
159
160 wxString wxNotebook::GetPageText(int nPage) const
161 {
162 wxASSERT( IS_VALID_PAGE(nPage) );
163
164 // TODO
165 return wxString("");
166 }
167
168 int wxNotebook::GetPageImage(int nPage) const
169 {
170 wxASSERT( IS_VALID_PAGE(nPage) );
171
172 // TODO
173 return 0;
174 }
175
176 bool wxNotebook::SetPageImage(int nPage, int nImage)
177 {
178 wxASSERT( IS_VALID_PAGE(nPage) );
179
180 // TODO
181 return FALSE;
182 }
183
184 void wxNotebook::SetImageList(wxImageList* imageList)
185 {
186 m_pImageList = imageList;
187 // TODO
188 }
189
190 // ----------------------------------------------------------------------------
191 // wxNotebook operations
192 // ----------------------------------------------------------------------------
193
194 // remove one page from the notebook
195 bool wxNotebook::DeletePage(int nPage)
196 {
197 wxCHECK( IS_VALID_PAGE(nPage), FALSE );
198
199 // TODO: delete native widget page
200
201 delete m_aPages[nPage];
202 m_aPages.Remove(nPage);
203
204 return TRUE;
205 }
206
207 // remove one page from the notebook, without deleting the window
208 bool wxNotebook::RemovePage(int nPage)
209 {
210 wxCHECK( IS_VALID_PAGE(nPage), FALSE );
211
212 m_aPages.Remove(nPage);
213
214 return TRUE;
215 }
216
217 // remove all pages
218 bool wxNotebook::DeleteAllPages()
219 {
220 // TODO: delete native widget pages
221
222 int nPageCount = GetPageCount();
223 int nPage;
224 for ( nPage = 0; nPage < nPageCount; nPage++ )
225 delete m_aPages[nPage];
226
227 m_aPages.Clear();
228
229 return TRUE;
230 }
231
232 // add a page to the notebook
233 bool wxNotebook::AddPage(wxNotebookPage *pPage,
234 const wxString& strText,
235 bool bSelect,
236 int imageId)
237 {
238 return InsertPage(GetPageCount(), pPage, strText, bSelect, imageId);
239 }
240
241 // same as AddPage() but does it at given position
242 bool wxNotebook::InsertPage(int nPage,
243 wxNotebookPage *pPage,
244 const wxString& strText,
245 bool bSelect,
246 int imageId)
247 {
248 wxASSERT( pPage != NULL );
249 wxCHECK( IS_VALID_PAGE(nPage) || nPage == GetPageCount(), FALSE );
250
251 ControlTabInfoRec tie ;
252 Boolean enabled = true ;
253 if ( nPage + 1 > GetControlMaximum( m_macControl ) )
254 {
255 SetControlMaximum( m_macControl , nPage + 1 ) ;
256 }
257
258 tie.version = 0 ;
259 tie.iconSuiteID = 0 ;
260 c2pstrcpy( (StringPtr) tie.name , strText ) ;
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