]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: notebook.cpp | |
3 | // Purpose: implementation of wxNotebook | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "notebook.h" | |
14 | #endif | |
15 | ||
16 | // ============================================================================ | |
17 | // declarations | |
18 | // ============================================================================ | |
19 | ||
20 | // ---------------------------------------------------------------------------- | |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
23 | #include "wx/wxprec.h" | |
24 | ||
25 | #if wxUSE_NOTEBOOK | |
26 | ||
27 | #include "wx/app.h" | |
28 | #include "wx/string.h" | |
29 | #include "wx/log.h" | |
30 | #include "wx/imaglist.h" | |
31 | #include "wx/image.h" | |
32 | #include "wx/notebook.h" | |
33 | #include "wx/mac/uma.h" | |
34 | // ---------------------------------------------------------------------------- | |
35 | // macros | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | // check that the page index is valid | |
39 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
40 | ||
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // event table | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) | |
47 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
48 | ||
49 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) | |
50 | EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange) | |
51 | ||
52 | EVT_SIZE(wxNotebook::OnSize) | |
53 | EVT_SET_FOCUS(wxNotebook::OnSetFocus) | |
54 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
55 | END_EVENT_TABLE() | |
56 | ||
57 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) | |
58 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) | |
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_nSelection = -1; | |
72 | } | |
73 | ||
74 | // default for dynamic class | |
75 | wxNotebook::wxNotebook() | |
76 | { | |
77 | Init(); | |
78 | } | |
79 | ||
80 | // the same arguments as for wxControl | |
81 | wxNotebook::wxNotebook(wxWindow *parent, | |
82 | wxWindowID id, | |
83 | const wxPoint& pos, | |
84 | const wxSize& size, | |
85 | long style, | |
86 | const wxString& name) | |
87 | { | |
88 | Init(); | |
89 | ||
90 | Create(parent, id, pos, size, style, name); | |
91 | } | |
92 | ||
93 | // Create() function | |
94 | bool wxNotebook::Create(wxWindow *parent, | |
95 | wxWindowID id, | |
96 | const wxPoint& pos, | |
97 | const wxSize& size, | |
98 | long style, | |
99 | const wxString& name) | |
100 | { | |
101 | m_macIsUserPane = FALSE ; | |
102 | ||
103 | if ( !wxNotebookBase::Create(parent, id, pos, size, style, name) ) | |
104 | return false; | |
105 | ||
106 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; | |
107 | ||
108 | if( bounds.right <= bounds.left ) | |
109 | bounds.right = bounds.left + 100 ; | |
110 | if ( bounds.bottom <= bounds.top ) | |
111 | bounds.bottom = bounds.top + 100 ; | |
112 | ||
113 | UInt16 tabstyle = kControlTabDirectionNorth ; | |
114 | if ( HasFlag(wxNB_LEFT) ) | |
115 | tabstyle = kControlTabDirectionWest ; | |
116 | else if ( HasFlag( wxNB_RIGHT ) ) | |
117 | tabstyle = kControlTabDirectionEast ; | |
118 | else if ( HasFlag( wxNB_BOTTOM ) ) | |
119 | tabstyle = kControlTabDirectionSouth ; | |
120 | ||
121 | ControlTabSize tabsize = kControlTabSizeLarge ; | |
122 | if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL ) | |
123 | tabsize = kControlTabSizeSmall ; | |
124 | else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI ) | |
125 | { | |
126 | if (UMAGetSystemVersion() >= 0x1030 ) | |
127 | tabsize = 3 ; | |
128 | else | |
129 | tabsize = kControlSizeSmall; | |
130 | } | |
131 | ||
132 | m_peer = new wxMacControl(this) ; | |
133 | verify_noerr ( CreateTabsControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , | |
134 | tabsize , tabstyle, 0, NULL, m_peer->GetControlRefAddr() ) ); | |
135 | ||
136 | ||
137 | MacPostControlCreate(pos,size) ; | |
138 | return TRUE ; | |
139 | } | |
140 | ||
141 | // dtor | |
142 | wxNotebook::~wxNotebook() | |
143 | { | |
144 | } | |
145 | ||
146 | // ---------------------------------------------------------------------------- | |
147 | // wxNotebook accessors | |
148 | // ---------------------------------------------------------------------------- | |
149 | ||
150 | void wxNotebook::SetPadding(const wxSize& padding) | |
151 | { | |
152 | // unsupported by OS | |
153 | } | |
154 | ||
155 | void wxNotebook::SetTabSize(const wxSize& sz) | |
156 | { | |
157 | // unsupported by OS | |
158 | } | |
159 | ||
160 | void wxNotebook::SetPageSize(const wxSize& size) | |
161 | { | |
162 | SetSize( CalcSizeFromPage( size ) ); | |
163 | } | |
164 | ||
165 | wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const | |
166 | { | |
167 | return DoGetSizeFromClientSize( sizePage ) ; | |
168 | } | |
169 | ||
170 | int wxNotebook::SetSelection(size_t nPage) | |
171 | { | |
172 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, wxT("notebook page out of range") ); | |
173 | ||
174 | if ( int(nPage) != m_nSelection ) | |
175 | { | |
176 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId); | |
177 | event.SetSelection(nPage); | |
178 | event.SetOldSelection(m_nSelection); | |
179 | event.SetEventObject(this); | |
180 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
181 | { | |
182 | // program allows the page change | |
183 | event.SetEventType(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED); | |
184 | (void)GetEventHandler()->ProcessEvent(event); | |
185 | ||
186 | ChangePage(m_nSelection, nPage); | |
187 | } | |
188 | } | |
189 | ||
190 | return m_nSelection; | |
191 | } | |
192 | ||
193 | bool wxNotebook::SetPageText(size_t nPage, const wxString& strText) | |
194 | { | |
195 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
196 | ||
197 | wxNotebookPage *page = m_pages[nPage]; | |
198 | page->SetLabel(strText); | |
199 | MacSetupTabs(); | |
200 | ||
201 | return true; | |
202 | } | |
203 | ||
204 | wxString wxNotebook::GetPageText(size_t nPage) const | |
205 | { | |
206 | wxASSERT( IS_VALID_PAGE(nPage) ); | |
207 | ||
208 | wxNotebookPage *page = m_pages[nPage]; | |
209 | return page->GetLabel(); | |
210 | } | |
211 | ||
212 | int wxNotebook::GetPageImage(size_t nPage) const | |
213 | { | |
214 | wxCHECK_MSG( IS_VALID_PAGE(nPage), -1, _T("invalid notebook page") ); | |
215 | ||
216 | return m_images[nPage]; | |
217 | } | |
218 | ||
219 | bool wxNotebook::SetPageImage(size_t nPage, int nImage) | |
220 | { | |
221 | wxCHECK_MSG( IS_VALID_PAGE(nPage), FALSE, _T("invalid notebook page") ); | |
222 | ||
223 | wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), FALSE, | |
224 | _T("invalid image index in SetPageImage()") ); | |
225 | ||
226 | if ( nImage != m_images[nPage] ) | |
227 | { | |
228 | // if the item didn't have an icon before or, on the contrary, did have | |
229 | // it but has lost it now, its size will change - but if the icon just | |
230 | // changes, it won't | |
231 | m_images[nPage] = nImage; | |
232 | ||
233 | MacSetupTabs() ; | |
234 | } | |
235 | ||
236 | return TRUE; | |
237 | } | |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // wxNotebook operations | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
243 | // remove one page from the notebook, without deleting the window | |
244 | wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage) | |
245 | { | |
246 | wxCHECK( IS_VALID_PAGE(nPage), NULL ); | |
247 | wxNotebookPage* page = m_pages[nPage] ; | |
248 | m_pages.RemoveAt(nPage); | |
249 | ||
250 | MacSetupTabs(); | |
251 | ||
252 | if(m_nSelection >= (int)GetPageCount()) { | |
253 | m_nSelection = GetPageCount() - 1; | |
254 | } | |
255 | if(m_nSelection >= 0) { | |
256 | m_pages[m_nSelection]->Show(true); | |
257 | } | |
258 | InvalidateBestSize(); | |
259 | return page; | |
260 | } | |
261 | ||
262 | // remove all pages | |
263 | bool wxNotebook::DeleteAllPages() | |
264 | { | |
265 | WX_CLEAR_ARRAY(m_pages) ; | |
266 | MacSetupTabs(); | |
267 | m_nSelection = -1 ; | |
268 | InvalidateBestSize(); | |
269 | return TRUE; | |
270 | } | |
271 | ||
272 | ||
273 | // same as AddPage() but does it at given position | |
274 | bool wxNotebook::InsertPage(size_t nPage, | |
275 | wxNotebookPage *pPage, | |
276 | const wxString& strText, | |
277 | bool bSelect, | |
278 | int imageId) | |
279 | { | |
280 | if ( !wxNotebookBase::InsertPage(nPage, pPage, strText, bSelect, imageId) ) | |
281 | return false; | |
282 | ||
283 | wxASSERT_MSG( pPage->GetParent() == this, | |
284 | _T("notebook pages must have notebook as parent") ); | |
285 | ||
286 | // don't show pages by default (we'll need to adjust their size first) | |
287 | pPage->Show( false ) ; | |
288 | ||
289 | pPage->SetLabel(strText); | |
290 | ||
291 | m_images.Insert(imageId, nPage); | |
292 | ||
293 | MacSetupTabs(); | |
294 | ||
295 | wxRect rect = GetPageRect() ; | |
296 | pPage->SetSize(rect); | |
297 | if ( pPage->GetAutoLayout() ) { | |
298 | pPage->Layout(); | |
299 | } | |
300 | ||
301 | ||
302 | // now deal with the selection | |
303 | // --------------------------- | |
304 | ||
305 | // if the inserted page is before the selected one, we must update the | |
306 | // index of the selected page | |
307 | ||
308 | if ( int(nPage) <= m_nSelection ) | |
309 | { | |
310 | m_nSelection++; | |
311 | // while this still is the same page showing, we need to update the tabs | |
312 | m_peer->SetValue( m_nSelection + 1 ) ; | |
313 | } | |
314 | ||
315 | // some page should be selected: either this one or the first one if there | |
316 | // is still no selection | |
317 | int selNew = -1; | |
318 | if ( bSelect ) | |
319 | selNew = nPage; | |
320 | else if ( m_nSelection == -1 ) | |
321 | selNew = 0; | |
322 | ||
323 | if ( selNew != -1 ) | |
324 | SetSelection(selNew); | |
325 | ||
326 | InvalidateBestSize(); | |
327 | return true; | |
328 | } | |
329 | ||
330 | /* Added by Mark Newsam | |
331 | * When a page is added or deleted to the notebook this function updates | |
332 | * information held in the control so that it matches the order | |
333 | * the user would expect. | |
334 | */ | |
335 | void wxNotebook::MacSetupTabs() | |
336 | { | |
337 | m_peer->SetMaximum( GetPageCount() ) ; | |
338 | ||
339 | wxNotebookPage *page; | |
340 | ControlTabInfoRecV1 info; | |
341 | ||
342 | const size_t countPages = GetPageCount(); | |
343 | for(size_t ii = 0; ii < countPages; ii++) | |
344 | { | |
345 | page = m_pages[ii]; | |
346 | info.version = kControlTabInfoVersionOne; | |
347 | info.iconSuiteID = 0; | |
348 | wxMacCFStringHolder cflabel( page->GetLabel() , m_font.GetEncoding() ) ; | |
349 | info.name = cflabel ; | |
350 | m_peer->SetData<ControlTabInfoRecV1>( ii+1, kControlTabInfoTag, &info ) ; | |
351 | ||
352 | if ( GetImageList() && GetPageImage(ii) >= 0 && UMAGetSystemVersion() >= 0x1020 ) | |
353 | { | |
354 | const wxBitmap bmap = GetImageList()->GetBitmap( GetPageImage(ii ) ) ; | |
355 | if ( bmap.Ok() ) | |
356 | { | |
357 | ControlButtonContentInfo info ; | |
358 | ||
359 | wxMacCreateBitmapButton( &info , bmap ) ; | |
360 | #ifdef __WXDEBUG__ | |
361 | OSStatus err = | |
362 | #endif // __WXDEBUG__ | |
363 | m_peer->SetData<ControlButtonContentInfo>( ii+1,kControlTabImageContentTag, &info ); | |
364 | wxASSERT_MSG( err == noErr , wxT("Error when setting icon on tab") ) ; | |
365 | wxMacReleaseBitmapButton( &info ) ; | |
366 | } | |
367 | } | |
368 | m_peer->SetTabEnabled( ii + 1 , true ) ; | |
369 | } | |
370 | Rect bounds; | |
371 | m_peer->GetRectInWindowCoords( &bounds ) ; | |
372 | InvalWindowRect((WindowRef)MacGetTopLevelWindowRef(), &bounds); | |
373 | } | |
374 | ||
375 | wxRect wxNotebook::GetPageRect() const | |
376 | { | |
377 | wxSize size = GetClientSize() ; | |
378 | return wxRect( 0 , 0 , size.x , size.y ) ; | |
379 | } | |
380 | // ---------------------------------------------------------------------------- | |
381 | // wxNotebook callbacks | |
382 | // ---------------------------------------------------------------------------- | |
383 | ||
384 | // @@@ OnSize() is used for setting the font when it's called for the first | |
385 | // time because doing it in ::Create() doesn't work (for unknown reasons) | |
386 | void wxNotebook::OnSize(wxSizeEvent& event) | |
387 | { | |
388 | ||
389 | unsigned int nCount = m_pages.Count(); | |
390 | wxRect rect = GetPageRect() ; | |
391 | for ( unsigned int nPage = 0; nPage < nCount; nPage++ ) { | |
392 | wxNotebookPage *pPage = m_pages[nPage]; | |
393 | pPage->SetSize(rect); | |
394 | if ( pPage->GetAutoLayout() ) { | |
395 | pPage->Layout(); | |
396 | } | |
397 | } | |
398 | ||
399 | // Processing continues to next OnSize | |
400 | event.Skip(); | |
401 | } | |
402 | ||
403 | void wxNotebook::OnSelChange(wxNotebookEvent& event) | |
404 | { | |
405 | // is it our tab control? | |
406 | if ( event.GetEventObject() == this ) | |
407 | ChangePage(event.GetOldSelection(), event.GetSelection()); | |
408 | ||
409 | // we want to give others a chance to process this message as well | |
410 | event.Skip(); | |
411 | } | |
412 | ||
413 | void wxNotebook::OnSetFocus(wxFocusEvent& event) | |
414 | { | |
415 | // set focus to the currently selected page if any | |
416 | if ( m_nSelection != -1 ) | |
417 | m_pages[m_nSelection]->SetFocus(); | |
418 | ||
419 | event.Skip(); | |
420 | } | |
421 | ||
422 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
423 | { | |
424 | if ( event.IsWindowChange() ) { | |
425 | // change pages | |
426 | AdvanceSelection(event.GetDirection()); | |
427 | } | |
428 | else { | |
429 | // we get this event in 2 cases | |
430 | // | |
431 | // a) one of our pages might have generated it because the user TABbed | |
432 | // out from it in which case we should propagate the event upwards and | |
433 | // our parent will take care of setting the focus to prev/next sibling | |
434 | // | |
435 | // or | |
436 | // | |
437 | // b) the parent panel wants to give the focus to us so that we | |
438 | // forward it to our selected page. We can't deal with this in | |
439 | // OnSetFocus() because we don't know which direction the focus came | |
440 | // from in this case and so can't choose between setting the focus to | |
441 | // first or last panel child | |
442 | wxWindow *parent = GetParent(); | |
443 | // the cast is here to fic a GCC ICE | |
444 | if ( ((wxWindow*)event.GetEventObject()) == parent ) | |
445 | { | |
446 | // no, it doesn't come from child, case (b): forward to a page | |
447 | if ( m_nSelection != -1 ) | |
448 | { | |
449 | // so that the page knows that the event comes from it's parent | |
450 | // and is being propagated downwards | |
451 | event.SetEventObject(this); | |
452 | ||
453 | wxWindow *page = m_pages[m_nSelection]; | |
454 | if ( !page->GetEventHandler()->ProcessEvent(event) ) | |
455 | { | |
456 | page->SetFocus(); | |
457 | } | |
458 | //else: page manages focus inside it itself | |
459 | } | |
460 | else | |
461 | { | |
462 | // we have no pages - still have to give focus to _something_ | |
463 | SetFocus(); | |
464 | } | |
465 | } | |
466 | else | |
467 | { | |
468 | // it comes from our child, case (a), pass to the parent | |
469 | if ( parent ) { | |
470 | event.SetCurrentFocus(this); | |
471 | parent->GetEventHandler()->ProcessEvent(event); | |
472 | } | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | // ---------------------------------------------------------------------------- | |
478 | // wxNotebook base class virtuals | |
479 | // ---------------------------------------------------------------------------- | |
480 | ||
481 | #if wxUSE_CONSTRAINTS | |
482 | ||
483 | // override these 2 functions to do nothing: everything is done in OnSize | |
484 | ||
485 | void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse)) | |
486 | { | |
487 | // don't set the sizes of the pages - their correct size is not yet known | |
488 | wxControl::SetConstraintSizes(FALSE); | |
489 | } | |
490 | ||
491 | bool wxNotebook::DoPhase(int WXUNUSED(nPhase)) | |
492 | { | |
493 | return TRUE; | |
494 | } | |
495 | ||
496 | #endif // wxUSE_CONSTRAINTS | |
497 | ||
498 | void wxNotebook::Command(wxCommandEvent& event) | |
499 | { | |
500 | wxFAIL_MSG(wxT("wxNotebook::Command not implemented")); | |
501 | } | |
502 | ||
503 | // ---------------------------------------------------------------------------- | |
504 | // wxNotebook helper functions | |
505 | // ---------------------------------------------------------------------------- | |
506 | ||
507 | // hide the currently active panel and show the new one | |
508 | void wxNotebook::ChangePage(int nOldSel, int nSel) | |
509 | { | |
510 | if ( nOldSel != -1 ) | |
511 | { | |
512 | m_pages[nOldSel]->Show(FALSE); | |
513 | } | |
514 | ||
515 | if ( nSel != -1 ) | |
516 | { | |
517 | wxNotebookPage *pPage = m_pages[nSel]; | |
518 | pPage->Show(TRUE); | |
519 | pPage->SetFocus(); | |
520 | } | |
521 | ||
522 | m_nSelection = nSel; | |
523 | m_peer->SetValue( m_nSelection + 1 ) ; | |
524 | } | |
525 | ||
526 | wxInt32 wxNotebook::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) | |
527 | { | |
528 | OSStatus status = eventNotHandledErr ; | |
529 | ||
530 | SInt32 newSel = m_peer->GetValue() - 1 ; | |
531 | if ( newSel != m_nSelection ) | |
532 | { | |
533 | wxNotebookEvent changing(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, m_windowId, | |
534 | newSel , m_nSelection); | |
535 | changing.SetEventObject(this); | |
536 | GetEventHandler()->ProcessEvent(changing); | |
537 | ||
538 | if(changing.IsAllowed()) | |
539 | { | |
540 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, m_windowId, | |
541 | newSel, m_nSelection); | |
542 | event.SetEventObject(this); | |
543 | ||
544 | GetEventHandler()->ProcessEvent(event); | |
545 | } | |
546 | else | |
547 | { | |
548 | m_peer->SetValue( m_nSelection + 1 ) ; | |
549 | } | |
550 | status = noErr ; | |
551 | } | |
552 | return status ; | |
553 | } | |
554 | ||
555 | #endif | |
556 |