]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/notebmac.cpp
corrected event handling according to current msw/gtk implementation
[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 #if TARGET_CARBON
264 c2pstrcpy( (StringPtr) tie.name , strText ) ;
265 #else
266 strcpy( (char *) tie.name , strText ) ;
267 c2pstr( (char *) tie.name ) ;
268 #endif
269 SetControlData( m_macControl, nPage + 1, kControlTabInfoTag , sizeof( ControlTabInfoRec) , (char*) &tie ) ;
270 SetControlData( m_macControl, m_nSelection + 1, kControlTabEnabledFlagTag, sizeof( Boolean ), (Ptr)&enabled );
271
272 // save the pointer to the page
273 m_aPages.Insert(pPage, nPage);
274
275 // some page must be selected: either this one or the first one if there is
276 // still no selection
277 if ( bSelect )
278 m_nSelection = nPage;
279 else if ( m_nSelection == -1 )
280 m_nSelection = 0;
281
282 // don't show pages by default (we'll need to adjust their size first)
283 pPage->Show( FALSE ) ;
284
285 return TRUE;
286 }
287
288 // ----------------------------------------------------------------------------
289 // wxNotebook callbacks
290 // ----------------------------------------------------------------------------
291
292 // @@@ OnSize() is used for setting the font when it's called for the first
293 // time because doing it in ::Create() doesn't work (for unknown reasons)
294 void wxNotebook::OnSize(wxSizeEvent& event)
295 {
296 static bool s_bFirstTime = TRUE;
297 if ( s_bFirstTime ) {
298 // TODO: any first-time-size processing.
299 s_bFirstTime = FALSE;
300 }
301
302 // TODO: all this may or may not be necessary for your platform
303
304 // emulate page change (it's esp. important to do it first time because
305 // otherwise our page would stay invisible)
306 int nSel = m_nSelection;
307 m_nSelection = -1;
308 SetSelection(nSel);
309
310 // fit the notebook page to the tab control's display area
311 int w, h;
312 GetSize(&w, &h);
313
314 unsigned int nCount = m_aPages.Count();
315 for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) {
316 wxNotebookPage *pPage = m_aPages[nPage];
317 pPage->SetSize(kwxMacTabLeftMargin, kwxMacTabTopMargin, w - kwxMacTabLeftMargin - kwxMacTabRightMargin,
318 h - kwxMacTabTopMargin - kwxMacTabBottomMargin );
319 // pPage->SetSize(0, 0, w, h);
320 if ( pPage->GetAutoLayout() )
321 pPage->Layout();
322 }
323
324 // Processing continues to next OnSize
325 event.Skip();
326 }
327
328 void wxNotebook::OnSelChange(wxNotebookEvent& event)
329 {
330 // is it our tab control?
331 if ( event.GetEventObject() == this )
332 ChangePage(event.GetOldSelection(), event.GetSelection());
333
334 // we want to give others a chance to process this message as well
335 event.Skip();
336 }
337
338 void wxNotebook::OnSetFocus(wxFocusEvent& event)
339 {
340 // set focus to the currently selected page if any
341 if ( m_nSelection != -1 )
342 m_aPages[m_nSelection]->SetFocus();
343
344 event.Skip();
345 }
346
347 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
348 {
349 if ( event.IsWindowChange() ) {
350 // change pages
351 AdvanceSelection(event.GetDirection());
352 }
353 else {
354 // pass to the parent
355 if ( GetParent() ) {
356 event.SetCurrentFocus(this);
357 GetParent()->ProcessEvent(event);
358 }
359 }
360 }
361
362 // ----------------------------------------------------------------------------
363 // wxNotebook base class virtuals
364 // ----------------------------------------------------------------------------
365
366 // override these 2 functions to do nothing: everything is done in OnSize
367
368 void wxNotebook::SetConstraintSizes(bool /* recurse */)
369 {
370 // don't set the sizes of the pages - their correct size is not yet known
371 wxControl::SetConstraintSizes(FALSE);
372 }
373
374 bool wxNotebook::DoPhase(int /* nPhase */)
375 {
376 return TRUE;
377 }
378
379 void wxNotebook::Command(wxCommandEvent& event)
380 {
381 wxFAIL_MSG("wxNotebook::Command not implemented");
382 }
383
384 // ----------------------------------------------------------------------------
385 // wxNotebook helper functions
386 // ----------------------------------------------------------------------------
387
388 // hide the currently active panel and show the new one
389 void wxNotebook::ChangePage(int nOldSel, int nSel)
390 {
391 // it's not an error (the message may be generated by the tab control itself)
392 // and it may happen - just do nothing
393 if ( nSel == nOldSel )
394 {
395 wxNotebookPage *pPage = m_aPages[nSel];
396 pPage->Show(FALSE);
397 pPage->Show(TRUE);
398 pPage->SetFocus();
399 return;
400 }
401
402 if ( nOldSel != -1 ) {
403 m_aPages[nOldSel]->Show(FALSE);
404 }
405
406 wxNotebookPage *pPage = m_aPages[nSel];
407 pPage->Show(TRUE);
408 pPage->SetFocus();
409
410 m_nSelection = nSel;
411 }
412
413 void wxNotebook::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
414 {
415 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId , ::GetControlValue(m_macControl) - 1, m_nSelection);
416 event.SetEventObject(this);
417
418 ProcessEvent(event);
419 }
420