]> git.saurik.com Git - wxWidgets.git/blame - src/osx/notebook_osx.cpp
Use [DOMRange markupString] to get selection source.
[wxWidgets.git] / src / osx / notebook_osx.cpp
CommitLineData
e53b3d16
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/notebook_osx.cpp
3// Purpose: implementation of wxNotebook
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
b5b208a1 7// RCS-ID: $Id$
e53b3d16
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_NOTEBOOK
15
16#include "wx/notebook.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/string.h"
20 #include "wx/log.h"
21 #include "wx/app.h"
22 #include "wx/image.h"
23#endif
24
25#include "wx/string.h"
26#include "wx/imaglist.h"
27#include "wx/osx/private.h"
28
29
30// check that the page index is valid
31#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
32
33BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
34 EVT_NOTEBOOK_PAGE_CHANGED(wxID_ANY, wxNotebook::OnSelChange)
35
36 EVT_SIZE(wxNotebook::OnSize)
37 EVT_SET_FOCUS(wxNotebook::OnSetFocus)
38 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
39END_EVENT_TABLE()
40
e53b3d16
SC
41bool wxNotebook::Create( wxWindow *parent,
42 wxWindowID id,
43 const wxPoint& pos,
44 const wxSize& size,
45 long style,
46 const wxString& name )
d15694e8
SC
47{
48 DontCreatePeer();
49
e53b3d16
SC
50 if (! (style & wxBK_ALIGN_MASK))
51 style |= wxBK_TOP;
52
53 if ( !wxNotebookBase::Create( parent, id, pos, size, style, name ) )
54 return false;
55
22756322 56 SetPeer(wxWidgetImpl::CreateTabView(this,parent, id, pos, size, style, GetExtraStyle() ));
03647350 57
e53b3d16
SC
58 MacPostControlCreate( pos, size );
59
60 return true ;
61}
62
63// dtor
64wxNotebook::~wxNotebook()
65{
66}
67
68// ----------------------------------------------------------------------------
69// wxNotebook accessors
70// ----------------------------------------------------------------------------
71
72void wxNotebook::SetPadding(const wxSize& WXUNUSED(padding))
73{
74 // unsupported by OS
75}
76
77void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
78{
79 // unsupported by OS
80}
81
82void wxNotebook::SetPageSize(const wxSize& size)
83{
84 SetSize( CalcSizeFromPage( size ) );
85}
86
87wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
88{
89 return DoGetSizeFromClientSize( sizePage );
90}
91
92int wxNotebook::DoSetSelection(size_t nPage, int flags)
93{
94 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("DoSetSelection: invalid notebook page") );
95
681be2ef 96 if ( m_selection == wxNOT_FOUND || nPage != (size_t)m_selection )
e53b3d16
SC
97 {
98 if ( flags & SetSelection_SendEvent )
99 {
100 if ( !SendPageChangingEvent(nPage) )
101 {
102 // vetoed by program
681be2ef 103 return m_selection;
e53b3d16
SC
104 }
105 //else: program allows the page change
106
681be2ef 107 SendPageChangedEvent(m_selection, nPage);
e53b3d16
SC
108 }
109
681be2ef 110 ChangePage(m_selection, nPage);
e53b3d16
SC
111 }
112 //else: no change
113
681be2ef 114 return m_selection;
e53b3d16
SC
115}
116
117bool wxNotebook::SetPageText(size_t nPage, const wxString& strText)
118{
119 wxCHECK_MSG( IS_VALID_PAGE(nPage), false, wxT("SetPageText: invalid notebook page") );
120
121 wxNotebookPage *page = m_pages[nPage];
122 page->SetLabel(wxStripMenuCodes(strText));
123 MacSetupTabs();
124
125 return true;
126}
127
128wxString wxNotebook::GetPageText(size_t nPage) const
129{
130 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxEmptyString, wxT("GetPageText: invalid notebook page") );
131
132 wxNotebookPage *page = m_pages[nPage];
133
134 return page->GetLabel();
135}
136
137int wxNotebook::GetPageImage(size_t nPage) const
138{
139 wxCHECK_MSG( IS_VALID_PAGE(nPage), wxNOT_FOUND, wxT("GetPageImage: invalid notebook page") );
140
141 return m_images[nPage];
142}
143
144bool wxNotebook::SetPageImage(size_t nPage, int nImage)
145{
146 wxCHECK_MSG( IS_VALID_PAGE(nPage), false,
147 wxT("SetPageImage: invalid notebook page") );
abfdefed 148 wxCHECK_MSG( HasImageList() && nImage < GetImageList()->GetImageCount(), false,
e53b3d16
SC
149 wxT("SetPageImage: invalid image index") );
150
151 if ( nImage != m_images[nPage] )
152 {
153 // if the item didn't have an icon before or, on the contrary, did have
154 // it but has lost it now, its size will change - but if the icon just
155 // changes, it won't
156 m_images[nPage] = nImage;
157
158 MacSetupTabs() ;
159 }
160
161 return true;
162}
163
164// ----------------------------------------------------------------------------
165// wxNotebook operations
166// ----------------------------------------------------------------------------
167
168// remove one page from the notebook, without deleting the window
169wxNotebookPage* wxNotebook::DoRemovePage(size_t nPage)
170{
171 wxCHECK_MSG( IS_VALID_PAGE(nPage), NULL,
172 wxT("DoRemovePage: invalid notebook page") );
173
174 wxNotebookPage* page = m_pages[nPage] ;
175 m_pages.RemoveAt(nPage);
f2d8a276 176 m_images.RemoveAt(nPage);
e53b3d16
SC
177
178 MacSetupTabs();
179
f197e652
SC
180 if ( m_selection >= (int)nPage )
181 {
182 if ( GetPageCount() == 0 )
183 m_selection = wxNOT_FOUND;
184 else
185 m_selection = m_selection ? m_selection - 1 : 0;
186
187 GetPeer()->SetValue( m_selection + 1 ) ;
188 }
e53b3d16 189
681be2ef
VZ
190 if (m_selection >= 0)
191 m_pages[m_selection]->Show(true);
e53b3d16
SC
192
193 InvalidateBestSize();
194
195 return page;
196}
197
198// remove all pages
199bool wxNotebook::DeleteAllPages()
200{
f2d8a276
SC
201 WX_CLEAR_ARRAY(m_pages);
202 m_images.clear();
e53b3d16 203 MacSetupTabs();
681be2ef 204 m_selection = wxNOT_FOUND ;
e53b3d16
SC
205 InvalidateBestSize();
206
207 return true;
208}
209
210// same as AddPage() but does it at given position
211bool wxNotebook::InsertPage(size_t nPage,
212 wxNotebookPage *pPage,
213 const wxString& strText,
214 bool bSelect,
215 int imageId )
216{
217 if ( !wxNotebookBase::InsertPage( nPage, pPage, strText, bSelect, imageId ) )
218 return false;
219
220 wxASSERT_MSG( pPage->GetParent() == this, wxT("notebook pages must have notebook as parent") );
221
222 // don't show pages by default (we'll need to adjust their size first)
223 pPage->Show( false ) ;
224
225 pPage->SetLabel( wxStripMenuCodes(strText) );
226
227 m_images.Insert( imageId, nPage );
228
229 MacSetupTabs();
230
231 wxRect rect = GetPageRect() ;
232 pPage->SetSize( rect );
233 if ( pPage->GetAutoLayout() )
234 pPage->Layout();
235
236 // now deal with the selection
237 // ---------------------------
238
239 // if the inserted page is before the selected one, we must update the
240 // index of the selected page
241
681be2ef 242 if ( int(nPage) <= m_selection )
e53b3d16 243 {
681be2ef 244 m_selection++;
e53b3d16
SC
245
246 // while this still is the same page showing, we need to update the tabs
22756322 247 GetPeer()->SetValue( m_selection + 1 ) ;
e53b3d16
SC
248 }
249
60d5c563 250 DoSetSelectionAfterInsertion(nPage, bSelect);
e53b3d16
SC
251
252 InvalidateBestSize();
253
254 return true;
255}
256
ddd7e430 257int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
e53b3d16 258{
dfd0756f 259 return GetPeer()->TabHitTest(pt,flags);
e53b3d16
SC
260}
261
262// Added by Mark Newsam
263// When a page is added or deleted to the notebook this function updates
264// information held in the control so that it matches the order
265// the user would expect.
266//
267void wxNotebook::MacSetupTabs()
268{
22756322 269 GetPeer()->SetupTabs(*this);
e53b3d16
SC
270 Refresh();
271}
272
273wxRect wxNotebook::GetPageRect() const
274{
275 wxSize size = GetClientSize() ;
276
277 return wxRect( 0 , 0 , size.x , size.y ) ;
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)
286void wxNotebook::OnSize(wxSizeEvent& event)
287{
288 unsigned int nCount = m_pages.Count();
289 wxRect rect = GetPageRect() ;
290
291 for ( unsigned int nPage = 0; nPage < nCount; nPage++ )
292 {
293 wxNotebookPage *pPage = m_pages[nPage];
ff94c563 294 pPage->SetSize(rect, wxSIZE_FORCE_EVENT);
e53b3d16
SC
295 }
296
43321b80 297#if 0 // deactivate r65078 for the moment
5bdeabaf
VZ
298 // If the selected page is hidden at this point, the notebook
299 // has become visible for the first time after creation, and
300 // we postponed showing the page in ChangePage().
301 // So show the selected page now.
681be2ef 302 if ( m_selection != wxNOT_FOUND )
5bdeabaf 303 {
681be2ef 304 wxNotebookPage *pPage = m_pages[m_selection];
5bdeabaf
VZ
305 if ( !pPage->IsShown() )
306 {
307 pPage->Show( true );
308 pPage->SetFocus();
309 }
310 }
43321b80 311#endif
5bdeabaf 312
e53b3d16
SC
313 // Processing continues to next OnSize
314 event.Skip();
315}
316
317void wxNotebook::OnSelChange(wxBookCtrlEvent& event)
318{
319 // is it our tab control?
320 if ( event.GetEventObject() == this )
321 ChangePage(event.GetOldSelection(), event.GetSelection());
322
323 // we want to give others a chance to process this message as well
324 event.Skip();
325}
326
327void wxNotebook::OnSetFocus(wxFocusEvent& event)
328{
329 // set focus to the currently selected page if any
681be2ef
VZ
330 if ( m_selection != wxNOT_FOUND )
331 m_pages[m_selection]->SetFocus();
e53b3d16
SC
332
333 event.Skip();
334}
335
336void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
337{
338 if ( event.IsWindowChange() )
339 {
340 // change pages
341 AdvanceSelection( event.GetDirection() );
342 }
343 else
344 {
345 // we get this event in 2 cases
346 //
347 // a) one of our pages might have generated it because the user TABbed
348 // out from it in which case we should propagate the event upwards and
349 // our parent will take care of setting the focus to prev/next sibling
350 //
351 // or
352 //
353 // b) the parent panel wants to give the focus to us so that we
354 // forward it to our selected page. We can't deal with this in
355 // OnSetFocus() because we don't know which direction the focus came
356 // from in this case and so can't choose between setting the focus to
357 // first or last panel child
358 wxWindow *parent = GetParent();
359
360 // the cast is here to fix a GCC ICE
361 if ( ((wxWindow*)event.GetEventObject()) == parent )
362 {
363 // no, it doesn't come from child, case (b): forward to a page
681be2ef 364 if ( m_selection != wxNOT_FOUND )
e53b3d16
SC
365 {
366 // so that the page knows that the event comes from it's parent
367 // and is being propagated downwards
368 event.SetEventObject( this );
369
681be2ef 370 wxWindow *page = m_pages[m_selection];
e53b3d16
SC
371 if ( !page->HandleWindowEvent( event ) )
372 {
373 page->SetFocus();
374 }
375 //else: page manages focus inside it itself
376 }
377 else
378 {
379 // we have no pages - still have to give focus to _something_
380 SetFocus();
381 }
382 }
383 else
384 {
385 // it comes from our child, case (a), pass to the parent
386 if ( parent )
387 {
388 event.SetCurrentFocus( this );
389 parent->HandleWindowEvent( event );
390 }
391 }
392 }
393}
394
395// ----------------------------------------------------------------------------
396// wxNotebook base class virtuals
397// ----------------------------------------------------------------------------
398
399#if wxUSE_CONSTRAINTS
400
401// override these 2 functions to do nothing: everything is done in OnSize
402
403void wxNotebook::SetConstraintSizes(bool WXUNUSED(recurse))
404{
405 // don't set the sizes of the pages - their correct size is not yet known
406 wxControl::SetConstraintSizes( false );
407}
408
409bool wxNotebook::DoPhase(int WXUNUSED(nPhase))
410{
411 return true;
412}
413
414#endif // wxUSE_CONSTRAINTS
415
416void wxNotebook::Command(wxCommandEvent& WXUNUSED(event))
417{
418 wxFAIL_MSG(wxT("wxNotebook::Command not implemented"));
419}
420
421// ----------------------------------------------------------------------------
422// wxNotebook helper functions
423// ----------------------------------------------------------------------------
424
425// hide the currently active panel and show the new one
426void wxNotebook::ChangePage(int nOldSel, int nSel)
427{
428 if (nOldSel == nSel)
429 return;
430
7e837615 431 if ( nOldSel != wxNOT_FOUND )
e53b3d16
SC
432 m_pages[nOldSel]->Show( false );
433
7e837615 434 if ( nSel != wxNOT_FOUND )
e53b3d16
SC
435 {
436 wxNotebookPage *pPage = m_pages[nSel];
43321b80 437#if 0 // deactivate r65078 for the moment
5bdeabaf
VZ
438 if ( IsShownOnScreen() )
439 {
440 pPage->Show( true );
441 pPage->SetFocus();
442 }
443 else
444 {
445 // Postpone Show() until the control is actually shown.
446 // Otherwise this forces the containing toplevel window
447 // to show, even if it's just being created and called
448 // AddPage() without intent to show the window yet.
449 // We Show() the selected page in our OnSize handler,
450 // unless it already is shown.
451 }
43321b80
SC
452#else
453 pPage->Show( true );
454 pPage->SetFocus();
455#endif
e53b3d16
SC
456 }
457
681be2ef 458 m_selection = nSel;
22756322 459 GetPeer()->SetValue( m_selection + 1 ) ;
e53b3d16
SC
460}
461
0faf03bf 462bool wxNotebook::OSXHandleClicked( double WXUNUSED(timestampsec) )
e53b3d16
SC
463{
464 bool status = false ;
465
22756322 466 SInt32 newSel = GetPeer()->GetValue() - 1 ;
681be2ef 467 if ( newSel != m_selection )
e53b3d16
SC
468 {
469 wxBookCtrlEvent changing(
ce7fe42e 470 wxEVT_NOTEBOOK_PAGE_CHANGING, m_windowId,
681be2ef 471 newSel , m_selection );
e53b3d16
SC
472 changing.SetEventObject( this );
473 HandleWindowEvent( changing );
474
475 if ( changing.IsAllowed() )
476 {
477 wxBookCtrlEvent event(
ce7fe42e 478 wxEVT_NOTEBOOK_PAGE_CHANGED, m_windowId,
681be2ef 479 newSel, m_selection );
e53b3d16
SC
480 event.SetEventObject( this );
481 HandleWindowEvent( event );
821073e3
VZ
482
483 m_selection = newSel;
e53b3d16
SC
484 }
485 else
486 {
22756322 487 GetPeer()->SetValue( m_selection + 1 ) ;
e53b3d16
SC
488 }
489
490 status = true ;
491 }
492
493 return status ;
494}
495
496#endif