]> git.saurik.com Git - wxWidgets.git/blame - src/mac/notebmac.cpp
added wxGetMultiChoice() (which refuses to work for some reason - will fix
[wxWidgets.git] / src / mac / notebmac.cpp
CommitLineData
ee6b1d97
SC
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
35const short kwxMacTabLeftMargin = 16 ;
36const short kwxMacTabTopMargin = 30 ;
37const short kwxMacTabRightMargin = 16 ;
38const short kwxMacTabBottomMargin = 16 ;
39
40// ----------------------------------------------------------------------------
41// event table
42// ----------------------------------------------------------------------------
43
44#if !USE_SHARED_LIBRARIES
45BEGIN_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)
51END_EVENT_TABLE()
52
53IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
54IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
55#endif
56
57// ============================================================================
58// implementation
59// ============================================================================
60
61// ----------------------------------------------------------------------------
62// wxNotebook construction
63// ----------------------------------------------------------------------------
64
65// common part of all ctors
66void wxNotebook::Init()
67{
68 m_pImageList = NULL;
69 m_nSelection = -1;
70}
71
72// default for dynamic class
73wxNotebook::wxNotebook()
74{
75 Init();
76}
77
78// the same arguments as for wxControl
79wxNotebook::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
92bool 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, wxDefaultValidator , 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
112wxNotebook::~wxNotebook()
113{
114 m_macControl = NULL ;
115}
116
117// ----------------------------------------------------------------------------
118// wxNotebook accessors
119// ----------------------------------------------------------------------------
120int wxNotebook::GetPageCount() const
121{
122 return m_aPages.Count();
123}
124
125int wxNotebook::GetRowCount() const
126{
127 return 1;
128}
129
130int 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
142void 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
152bool wxNotebook::SetPageText(int nPage, const wxString& strText)
153{
154 wxASSERT( IS_VALID_PAGE(nPage) );
155
156 // TODO
157 return FALSE;
158}
159
160wxString wxNotebook::GetPageText(int nPage) const
161{
162 wxASSERT( IS_VALID_PAGE(nPage) );
163
164 // TODO
165 return wxString("");
166}
167
168int wxNotebook::GetPageImage(int nPage) const
169{
170 wxASSERT( IS_VALID_PAGE(nPage) );
171
172 // TODO
173 return 0;
174}
175
176bool wxNotebook::SetPageImage(int nPage, int nImage)
177{
178 wxASSERT( IS_VALID_PAGE(nPage) );
179
180 // TODO
181 return FALSE;
182}
183
184void 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
195bool 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
208bool 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
218bool 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
233bool 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
242bool 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 strcpy( (char*) tie.name , strText ) ;
261 c2pstr( (char*) tie.name ) ;
262 SetControlData( m_macControl, nPage + 1, kControlTabInfoTag , sizeof( ControlTabInfoRec) , (char*) &tie ) ;
263 SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
264
265 // save the pointer to the page
266 m_aPages.Insert(pPage, nPage);
267
268 // some page must be selected: either this one or the first one if there is
269 // still no selection
270 if ( bSelect )
271 m_nSelection = nPage;
272 else if ( m_nSelection == -1 )
273 m_nSelection = 0;
274
275 // don't show pages by default (we'll need to adjust their size first)
276 pPage->Show( FALSE ) ;
277
278 return TRUE;
279}
280
281// ----------------------------------------------------------------------------
282// wxNotebook callbacks
283// ----------------------------------------------------------------------------
284
285// @@@ OnSize() is used for setting the font when it's called for the first
286// time because doing it in ::Create() doesn't work (for unknown reasons)
287void wxNotebook::OnSize(wxSizeEvent& event)
288{
289 static bool s_bFirstTime = TRUE;
290 if ( s_bFirstTime ) {
291 // TODO: any first-time-size processing.
292 s_bFirstTime = FALSE;
293 }
294
295 // TODO: all this may or may not be necessary for your platform
296
297 // emulate page change (it's esp. important to do it first time because
298 // otherwise our page would stay invisible)
299 int nSel = m_nSelection;
300 m_nSelection = -1;
301 SetSelection(nSel);
302
303 // fit the notebook page to the tab control's display area
304 int w, h;
305 GetSize(&w, &h);
306
307 unsigned int nCount = m_aPages.Count();
308 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
309 wxNotebookPage *pPage = m_aPages[nPage];
310 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin, w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
311 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
312// pPage->SetSize(0, 0, w, h);
313 if ( pPage->GetAutoLayout() )
314 pPage->Layout();
315 }
316
317 // Processing continues to next OnSize
318 event.Skip();
319}
320
321void wxNotebook::OnSelChange(wxNotebookEvent& event)
322{
323 // is it our tab control?
324 if ( event.GetEventObject() == this )
325 ChangePage(event.GetOldSelection(), event.GetSelection());
326
327 // we want to give others a chance to process this message as well
328 event.Skip();
329}
330
331void wxNotebook::OnSetFocus(wxFocusEvent& event)
332{
333 // set focus to the currently selected page if any
334 if ( m_nSelection != -1 )
335 m_aPages[m_nSelection]->SetFocus();
336
337 event.Skip();
338}
339
340void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
341{
342 if ( event.IsWindowChange() ) {
343 // change pages
344 AdvanceSelection(event.GetDirection());
345 }
346 else {
347 // pass to the parent
348 if ( GetParent() ) {
349 event.SetCurrentFocus(this);
350 GetParent()->ProcessEvent(event);
351 }
352 }
353}
354
355// ----------------------------------------------------------------------------
356// wxNotebook base class virtuals
357// ----------------------------------------------------------------------------
358
359// override these 2 functions to do nothing: everything is done in OnSize
360
361void wxNotebook::SetConstraintSizes(bool /* recurse */)
362{
363 // don't set the sizes of the pages - their correct size is not yet known
364 wxControl::SetConstraintSizes(FALSE);
365}
366
367bool wxNotebook::DoPhase(int /* nPhase */)
368{
369 return TRUE;
370}
371
372void wxNotebook::Command(wxCommandEvent& event)
373{
374 wxFAIL_MSG("wxNotebook::Command not implemented");
375}
376
377// ----------------------------------------------------------------------------
378// wxNotebook helper functions
379// ----------------------------------------------------------------------------
380
381// hide the currently active panel and show the new one
382void wxNotebook::ChangePage(int nOldSel, int nSel)
383{
384 // it's not an error (the message may be generated by the tab control itself)
385 // and it may happen - just do nothing
386 if ( nSel == nOldSel )
387 {
388 wxNotebookPage *pPage = m_aPages[nSel];
389 pPage->Show(FALSE);
390 pPage->Show(TRUE);
391 pPage->SetFocus();
392 return;
393 }
394
395 if ( nOldSel != -1 ) {
396 m_aPages[nOldSel]->Show(FALSE);
397 }
398
399 wxNotebookPage *pPage = m_aPages[nSel];
400 pPage->Show(TRUE);
401 pPage->SetFocus();
402
403 m_nSelection = nSel;
404}
405
406void wxNotebook::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
407{
408 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControlValue(m_macControl) - 1, m_nSelection);
409 event.SetEventObject(this);
410
411 ProcessEvent(event);
412}
413