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