]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/notebook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxNotebook
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "notebook.h"
23 #include <wx/string.h>
25 #include <wx/imaglist.h>
26 #include <wx/notebook.h>
27 #include <wx/dcclient.h>
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // check that the page index is valid
34 #define IS_VALID_PAGE(nPage) (((nPage) >= 0) && ((nPage) < GetPageCount()))
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 #if !USE_SHARED_LIBRARIES
41 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
42 EVT_NOTEBOOK_PAGE_CHANGED(-1, wxNotebook::OnSelChange
)
43 EVT_SIZE(wxNotebook::OnSize
)
44 EVT_PAINT(wxNotebook::OnPaint
)
45 EVT_MOUSE_EVENTS(wxNotebook::OnMouseEvent
)
46 EVT_SET_FOCUS(wxNotebook::OnSetFocus
)
47 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
50 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
, wxControl
)
51 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
59 // wxNotebook construction
60 // ----------------------------------------------------------------------------
62 // common part of all ctors
63 void wxNotebook::Init()
65 m_tabView
= (wxNotebookTabView
*) NULL
;
70 // default for dynamic class
71 wxNotebook::wxNotebook()
76 // the same arguments as for wxControl
77 wxNotebook::wxNotebook(wxWindow
*parent
,
86 Create(parent
, id
, pos
, size
, style
, name
);
90 bool wxNotebook::Create(wxWindow
*parent
,
101 m_windowId
= id
== -1 ? NewControlId() : id
;
104 m_windowStyle
= style
;
106 if ( parent
!= NULL
)
107 parent
->AddChild(this);
109 // It's like a normal window...
110 if (!wxWindow::Create(parent
, id
, pos
, size
, style
, name
))
113 SetTabView(new wxNotebookTabView(this));
119 wxNotebook::~wxNotebook()
124 // ----------------------------------------------------------------------------
125 // wxNotebook accessors
126 // ----------------------------------------------------------------------------
127 int wxNotebook::GetPageCount() const
129 return m_aPages
.Count();
132 int wxNotebook::GetRowCount() const
138 int wxNotebook::SetSelection(int nPage
)
140 wxASSERT( IS_VALID_PAGE(nPage
) );
142 ChangePage(m_nSelection
, nPage
);
148 void wxNotebook::AdvanceSelection(bool bForward
)
150 int nSel
= GetSelection();
151 int nMax
= GetPageCount() - 1;
153 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
155 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
158 bool wxNotebook::SetPageText(int nPage
, const wxString
& strText
)
160 wxASSERT( IS_VALID_PAGE(nPage
) );
166 wxString
wxNotebook::GetPageText(int nPage
) const
168 wxASSERT( IS_VALID_PAGE(nPage
) );
174 int wxNotebook::GetPageImage(int nPage
) const
176 wxASSERT( IS_VALID_PAGE(nPage
) );
182 bool wxNotebook::SetPageImage(int nPage
, int nImage
)
184 wxASSERT( IS_VALID_PAGE(nPage
) );
190 void wxNotebook::SetImageList(wxImageList
* imageList
)
192 m_pImageList
= imageList
;
196 // ----------------------------------------------------------------------------
197 // wxNotebook operations
198 // ----------------------------------------------------------------------------
200 // remove one page from the notebook
201 bool wxNotebook::DeletePage(int nPage
)
203 wxFAIL_MSG("Sorry, DeletePage not implemented for Motif wxNotebook because wxTabView doesn't support it.");
207 wxCHECK( IS_VALID_PAGE(nPage), FALSE );
209 // TODO: delete native widget page
211 delete m_aPages[nPage];
212 m_aPages.Remove(nPage);
219 bool wxNotebook::DeleteAllPages()
221 m_tabView
->ClearTabs(TRUE
);
223 int nPageCount
= GetPageCount();
225 for ( nPage
= 0; nPage
< nPageCount
; nPage
++ )
226 delete m_aPages
[nPage
];
233 // add a page to the notebook
234 bool wxNotebook::AddPage(wxNotebookPage
*pPage
,
235 const wxString
& strText
,
239 return InsertPage(GetPageCount(), pPage
, strText
, bSelect
, imageId
);
242 // same as AddPage() but does it at given position
243 bool wxNotebook::InsertPage(int nPage
,
244 wxNotebookPage
*pPage
,
245 const wxString
& strText
,
249 wxASSERT( pPage
!= NULL
);
250 wxCHECK( IS_VALID_PAGE(nPage
) || nPage
== GetPageCount(), FALSE
);
252 m_tabView
->AddTab(nPage
, strText
);
256 m_tabView->SetTabSelection(nPage, TRUE);
259 // save the pointer to the page
260 m_aPages
.Insert(pPage
, nPage
);
262 // some page must be selected: either this one or the first one if there is
263 // still no selection
265 m_nSelection
= nPage
;
266 else if ( m_nSelection
== -1 )
272 // ----------------------------------------------------------------------------
273 // wxNotebook callbacks
274 // ----------------------------------------------------------------------------
276 // @@@ OnSize() is used for setting the font when it's called for the first
277 // time because doing it in ::Create() doesn't work (for unknown reasons)
278 void wxNotebook::OnSize(wxSizeEvent
& event
)
280 static bool s_bFirstTime
= TRUE
;
281 if ( s_bFirstTime
) {
282 // TODO: any first-time-size processing.
283 s_bFirstTime
= FALSE
;
289 GetClientSize(& cw
, & ch
);
291 int tabHeight
= m_tabView
->GetTotalTabHeight();
294 rect
.y
= tabHeight
+ 4;
296 rect
.height
= ch
- 4 - rect
.y
;
298 m_tabView
->SetViewRect(rect
);
302 // Need to do it a 2nd time to get the tab height with
303 // the new view width, since changing the view width changes the
305 tabHeight
= m_tabView
->GetTotalTabHeight();
307 rect
.y
= tabHeight
+ 4;
309 rect
.height
= ch
- 4 - rect
.y
;
311 m_tabView
->SetViewRect(rect
);
315 // emulate page change (it's esp. important to do it first time because
316 // otherwise our page would stay invisible)
317 int nSel
= m_nSelection
;
321 // fit the notebook page to the tab control's display area
323 unsigned int nCount
= m_aPages
.Count();
324 for ( unsigned int nPage
= 0; nPage
< nCount
; nPage
++ ) {
325 wxNotebookPage
*pPage
= m_aPages
[nPage
];
326 pPage
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 2, rect
.height
- 2);
327 if ( pPage
->GetAutoLayout() )
333 // Processing continues to next OnSize
337 void wxNotebook::OnSelChange(wxNotebookEvent
& event
)
339 // is it our tab control?
340 if ( event
.GetEventObject() == this )
341 ChangePage(event
.GetOldSelection(), event
.GetSelection());
343 // we want to give others a chance to process this message as well
347 void wxNotebook::OnSetFocus(wxFocusEvent
& event
)
349 // set focus to the currently selected page if any
350 if ( m_nSelection
!= -1 )
351 m_aPages
[m_nSelection
]->SetFocus();
356 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
358 if ( event
.IsWindowChange() ) {
360 AdvanceSelection(event
.GetDirection());
363 // pass to the parent
365 event
.SetCurrentFocus(this);
366 GetParent()->ProcessEvent(event
);
371 // ----------------------------------------------------------------------------
372 // wxNotebook base class virtuals
373 // ----------------------------------------------------------------------------
375 // override these 2 functions to do nothing: everything is done in OnSize
377 void wxNotebook::SetConstraintSizes(bool /* recurse */)
379 // don't set the sizes of the pages - their correct size is not yet known
380 wxControl::SetConstraintSizes(FALSE
);
383 bool wxNotebook::DoPhase(int /* nPhase */)
388 void wxNotebook::Command(wxCommandEvent
& event
)
390 wxFAIL_MSG("wxNotebook::Command not implemented");
393 // ----------------------------------------------------------------------------
394 // wxNotebook helper functions
395 // ----------------------------------------------------------------------------
397 // hide the currently active panel and show the new one
398 void wxNotebook::ChangePage(int nOldSel
, int nSel
)
400 wxASSERT( nOldSel
!= nSel
); // impossible
402 if ( nOldSel
!= -1 ) {
403 m_aPages
[nOldSel
]->Show(FALSE
);
406 wxNotebookPage
*pPage
= m_aPages
[nSel
];
413 void wxNotebook::ChangeFont(bool keepOriginalSize
)
415 wxWindow::ChangeFont(keepOriginalSize
);
418 void wxNotebook::ChangeBackgroundColour()
420 wxWindow::ChangeBackgroundColour();
423 void wxNotebook::ChangeForegroundColour()
425 wxWindow::ChangeForegroundColour();
428 void wxNotebook::OnMouseEvent(wxMouseEvent
& event
)
431 m_tabView
->OnEvent(event
);
434 void wxNotebook::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
445 IMPLEMENT_CLASS(wxNotebookTabView
, wxTabView
)
447 wxNotebookTabView::wxNotebookTabView(wxNotebook
*notebook
, long style
): wxTabView(style
)
449 m_notebook
= notebook
;
451 // m_currentWindow = (wxWindow *) NULL;
453 m_notebook
->SetTabView(this);
455 SetWindow(m_notebook
);
458 wxNotebookTabView::~wxNotebookTabView(void)
460 // ClearWindows(TRUE);
463 // Called when a tab is activated
464 void wxNotebookTabView::OnTabActivate(int activateId
, int deactivateId
)
469 wxWindow
*oldWindow
= ((deactivateId
== -1) ? 0 : m_notebook
->GetPage(deactivateId
));
470 wxWindow
*newWindow
= m_notebook
->GetPage(activateId
);
473 oldWindow
->Show(FALSE
);
475 newWindow
->Show(TRUE
);
477 m_notebook
->Refresh();
481 void wxNotebookTabView::AddTabWindow(int id
, wxWindow
*window
)
483 m_tabWindows
.Append((long)id
, window
);
487 wxWindow
*wxNotebookTabView::GetTabWindow(int id
) const
489 wxNode
*node
= m_tabWindows
.Find((long)id
);
491 return (wxWindow
*) NULL
;
492 return (wxWindow
*)node
->Data();
495 void wxNotebookTabView::ClearWindows(bool deleteWindows
)
498 m_tabWindows
.DeleteContents(TRUE
);
499 m_tabWindows
.Clear();
500 m_tabWindows
.DeleteContents(FALSE
);
503 void wxNotebookTabView::ShowWindowForTab(int id
)
505 wxWindow
*newWindow
= GetTabWindow(id
);
506 if (newWindow
== m_currentWindow
)
509 m_currentWindow
->Show(FALSE
);
510 newWindow
->Show(TRUE
);
511 newWindow
->Refresh();