1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "notebook.h"
15 #include "wx/notebook.h"
18 #include "wx/imaglist.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 // page change callback
27 static void gtk_notebook_page_change_callback(GtkNotebook
*widget
,
28 GtkNotebookPage
*page
,
32 wxNotebook
*notebook
= (wxNotebook
*)data
;
34 int nOld
= notebook
->GetSelection();
36 // TODO: emulate PAGE_CHANGING event
37 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
41 event
.SetEventObject(notebook
);
42 notebook
->ProcessEvent(event
);
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 class wxNotebookPage
: public wxObject
65 GtkNotebookPage
*m_page
;
67 wxWindow
*m_clientPanel
;
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
75 EVT_SIZE(wxNotebook::OnSize
)
78 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
80 void wxNotebook::Init()
83 m_pages
.DeleteContents( TRUE
);
87 wxNotebook::wxNotebook()
92 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
93 const wxPoint
& pos
, const wxSize
& size
,
94 long style
, const wxString
& name
)
97 Create( parent
, id
, pos
, size
, style
, name
);
100 wxNotebook::~wxNotebook()
102 // don't generate change page events any more
103 if ( m_idHandler
!= 0 )
104 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
111 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
112 const wxPoint
& pos
, const wxSize
& size
,
113 long style
, const wxString
& name
)
117 PreCreation( parent
, id
, pos
, size
, style
, name
);
119 m_widget
= gtk_notebook_new();
120 m_idHandler
= gtk_signal_connect
122 GTK_OBJECT(m_widget
), "switch_page",
123 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
134 int wxNotebook::GetSelection() const
136 if (m_pages
.Number() == 0)
139 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
141 wxNotebookPage
*page
= NULL
;
143 wxNode
*node
= m_pages
.First();
146 page
= (wxNotebookPage
*)node
->Data();
147 if (page
->m_page
== g_page
)
152 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?");
157 int wxNotebook::GetPageCount() const
159 return m_pages
.Number();
162 int wxNotebook::GetRowCount() const
167 wxString
wxNotebook::GetPageText( int page
) const
169 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
171 return nb_page
->m_text
;
176 int wxNotebook::GetPageImage( int page
) const
178 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
180 return nb_page
->m_image
;
185 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
187 wxNotebookPage
*nb_page
= NULL
;
189 wxNode
*node
= m_pages
.First();
192 nb_page
= (wxNotebookPage
*)node
->Data();
193 if (nb_page
->m_id
== page
)
198 wxLogDebug("Notebook page %d not found!", page
);
203 int wxNotebook::SetSelection( int page
)
205 int selOld
= GetSelection();
206 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
211 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
214 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
)
220 if (!child
) return -1;
222 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
227 void wxNotebook::AdvanceSelection(bool bForward
)
229 int nSel
= GetSelection(),
230 nMax
= GetPageCount();
233 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
236 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
240 void wxNotebook::SetImageList( wxImageList
* imageList
)
242 m_imageList
= imageList
;
245 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
247 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
251 nb_page
->m_text
= text
;
256 bool wxNotebook::SetPageImage( int page
, int image
)
258 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
262 nb_page
->m_image
= image
;
267 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
269 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
272 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
274 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
277 bool wxNotebook::DeleteAllPages()
279 wxNode
*page_node
= m_pages
.First();
282 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
284 DeletePage( page
->m_id
);
286 page_node
= m_pages
.First();
292 bool wxNotebook::DeletePage( int page
)
294 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
295 if (!nb_page
) return FALSE
;
298 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
301 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
308 delete nb_page
->m_clientPanel
;
310 // Amazingly, this is not necessary
311 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
313 m_pages
.DeleteObject( nb_page
);
318 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
319 bool bSelect
, int imageId
)
321 // we've created the notebook page in AddChild(). Now we just have to set
322 // the caption for the page and set the others parameters.
324 // first, find the page
325 wxNotebookPage
*page
= NULL
;
327 wxNode
*node
= m_pages
.First();
330 page
= (wxNotebookPage
*)node
->Data();
331 if ( page
->m_clientPanel
== win
)
336 wxCHECK_MSG(page
!= NULL
, FALSE
,
337 "Can't add a page whose parent is not the notebook!");
339 // then set the attributes
341 if ( page
->m_text
.IsEmpty() )
343 page
->m_image
= imageId
;
344 gtk_label_set(page
->m_label
, page
->m_text
);
347 SetSelection(GetPageCount());
353 wxWindow
*wxNotebook::GetPage( int page
) const
355 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
359 return nb_page
->m_clientPanel
;
362 void wxNotebook::AddChild( wxWindow
*win
)
364 // @@@ normally done in wxWindow::AddChild but for some reason wxNotebook
365 // case is special there (Robert?)
366 m_children
.Append(win
);
368 wxNotebookPage
*page
= new wxNotebookPage();
370 page
->m_id
= GetPageCount();
371 page
->m_label
= (GtkLabel
*)gtk_label_new("no caption");
372 page
->m_clientPanel
= win
;
373 gtk_notebook_append_page(GTK_NOTEBOOK(m_widget
), win
->m_widget
,
374 (GtkWidget
*)page
->m_label
);
375 gtk_misc_set_alignment(GTK_MISC(page
->m_label
), 0.0, 0.5);
377 page
->m_page
= (GtkNotebookPage
*)
379 g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
384 wxLogFatalError( "Notebook page creation error" );
388 m_pages
.Append( page
);
391 void wxNotebook::OnSize(wxSizeEvent
& event
)
393 // forward this event to all pages
394 wxNode
*node
= m_pages
.First();
397 wxNotebookPage
*page
= (wxNotebookPage
*)node
->Data();
398 // @@@@ This -50 is completely wrong - instead, we should substract
399 // the height of the tabs
400 page
->m_clientPanel
->SetSize(event
.GetSize().GetX(),
401 event
.GetSize().GetY() - 50);
407 //-----------------------------------------------------------------------------
409 //-----------------------------------------------------------------------------
411 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)