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 class wxNotebookPage
: public wxObject
43 GtkNotebookPage
*m_page
;
46 GtkNotebook
*m_parent
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 // page change callback
54 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
55 GtkNotebookPage
*WXUNUSED(page
),
59 wxNotebook
*notebook
= (wxNotebook
*)data
;
61 int nOld
= notebook
->GetSelection();
63 // TODO: emulate PAGE_CHANGING event
64 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
68 event
.SetEventObject(notebook
);
69 notebook
->ProcessEvent(event
);
72 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
74 if ((win
->m_x
== alloc
->x
) &&
75 (win
->m_y
== alloc
->y
) &&
76 (win
->m_width
== alloc
->width
) &&
77 (win
->m_height
== alloc
->height
))
83 printf( "OnResize from " );
84 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
85 printf( win->GetClassInfo()->GetClassName() );
88 printf( " Old: X: %d Y: %d ", win->m_x, win->m_y );
89 printf( " W: %d H: %d ", win->m_width, win->m_height );
92 printf( " New: X: %d Y: %d ", alloc->x, alloc->y );
93 printf( " W: %d H: %d ", alloc->width, alloc->height );
97 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
100 printf( " Res: X: %d Y: %d ", win->m_x, win->m_y );
101 printf( " W: %d H: %d ", win->m_width, win->m_height );
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
110 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
111 EVT_SIZE(wxNotebook::OnSize
)
114 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
116 void wxNotebook::Init()
119 m_pages
.DeleteContents( TRUE
);
123 wxNotebook::wxNotebook()
128 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
129 const wxPoint
& pos
, const wxSize
& size
,
130 long style
, const wxString
& name
)
133 Create( parent
, id
, pos
, size
, style
, name
);
136 wxNotebook::~wxNotebook()
138 // don't generate change page events any more
139 if ( m_idHandler
!= 0 )
140 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
147 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
148 const wxPoint
& pos
, const wxSize
& size
,
149 long style
, const wxString
& name
)
153 PreCreation( parent
, id
, pos
, size
, style
, name
);
155 m_widget
= gtk_notebook_new();
156 m_idHandler
= gtk_signal_connect
158 GTK_OBJECT(m_widget
), "switch_page",
159 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
170 int wxNotebook::GetSelection() const
172 if (m_pages
.Number() == 0)
175 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
177 wxNotebookPage
*page
= NULL
;
179 wxNode
*node
= m_pages
.First();
182 page
= (wxNotebookPage
*)node
->Data();
183 if (page
->m_page
== g_page
)
188 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?");
193 int wxNotebook::GetPageCount() const
195 return m_pages
.Number();
198 int wxNotebook::GetRowCount() const
203 wxString
wxNotebook::GetPageText( int page
) const
205 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
207 return nb_page
->m_text
;
212 int wxNotebook::GetPageImage( int page
) const
214 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
216 return nb_page
->m_image
;
221 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
223 wxNotebookPage
*nb_page
= NULL
;
225 wxNode
*node
= m_pages
.First();
228 nb_page
= (wxNotebookPage
*)node
->Data();
229 if (nb_page
->m_id
== page
)
234 wxLogDebug( "Notebook page %d not found!", page
);
239 int wxNotebook::SetSelection( int page
)
241 int selOld
= GetSelection();
242 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
247 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
250 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
)
256 if (!child
) return -1;
258 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
263 void wxNotebook::AdvanceSelection(bool bForward
)
265 int nSel
= GetSelection(),
266 nMax
= GetPageCount();
269 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
272 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
276 void wxNotebook::SetImageList( wxImageList
* imageList
)
278 m_imageList
= imageList
;
281 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
283 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
287 nb_page
->m_text
= text
;
292 bool wxNotebook::SetPageImage( int page
, int image
)
294 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
298 nb_page
->m_image
= image
;
303 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
305 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
308 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
310 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
313 bool wxNotebook::DeleteAllPages()
315 wxNode
*page_node
= m_pages
.First();
318 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
320 DeletePage( page
->m_id
);
322 page_node
= m_pages
.First();
328 bool wxNotebook::DeletePage( int page
)
330 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
331 if (!nb_page
) return FALSE
;
334 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
337 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
344 delete nb_page
->m_client
;
346 // Amazingly, this is not necessary
347 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
349 m_pages
.DeleteObject( nb_page
);
354 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
355 bool bSelect
, int imageId
)
357 // we've created the notebook page in AddChild(). Now we just have to set
358 // the caption for the page and set the others parameters.
360 // first, find the page
361 wxNotebookPage
*page
= NULL
;
363 wxNode
*node
= m_pages
.First();
366 page
= (wxNotebookPage
*)node
->Data();
367 if ( page
->m_client
== win
)
372 wxCHECK_MSG(page
!= NULL
, FALSE
,
373 "Can't add a page whose parent is not the notebook!");
375 // then set the attributes
377 if ( page
->m_text
.IsEmpty() )
379 page
->m_image
= imageId
;
380 gtk_label_set(page
->m_label
, page
->m_text
);
383 SetSelection(GetPageCount());
389 wxWindow
*wxNotebook::GetPage( int page
) const
391 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
395 return nb_page
->m_client
;
398 void wxNotebook::AddChild( wxWindow
*win
)
400 // @@@ normally done in wxWindow::AddChild but for some reason wxNotebook
401 // case is special there (Robert?)
402 // Robert: Don't you think the code below looks different from the one
403 // in wxWindow::AddChild :-)
405 m_children
.Append(win
);
407 wxNotebookPage
*page
= new wxNotebookPage();
409 page
->m_id
= GetPageCount();
410 page
->m_label
= (GtkLabel
*)gtk_label_new("Handle");
411 page
->m_client
= win
;
412 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
,
413 (GtkWidget
*)page
->m_label
);
414 gtk_misc_set_alignment(GTK_MISC(page
->m_label
), 0.0, 0.5);
417 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
419 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
421 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
422 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
426 wxLogFatalError( "Notebook page creation error" );
430 m_pages
.Append( page
);
433 void wxNotebook::OnSize(wxSizeEvent
& event
)
435 // forward this event to all pages
436 wxNode
*node
= m_pages
.First();
439 wxWindow
*page
= ((wxNotebookPage
*)node
->Data())->m_client
;
440 // @@@@ the numbers I substract here are completely arbitrary, instead we
441 // should somehow calculate the size of the page from the size of the
443 /* page->SetSize(event.GetSize().GetX() - 5,
444 event.GetSize().GetY() - 30);
446 if ( page->GetAutoLayout() )
453 // override these 2 functions to do nothing: everything is done in OnSize
454 void wxNotebook::SetConstraintSizes(bool /* recurse */)
456 // don't set the sizes of the pages - their correct size is not yet known
457 wxControl::SetConstraintSizes(FALSE
);
460 bool wxNotebook::DoPhase(int /* nPhase */)
465 //-----------------------------------------------------------------------------
467 //-----------------------------------------------------------------------------
469 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)