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
->GetAutoLayout() )
77 if ((win
->m_x
== alloc
->x
) &&
78 (win
->m_y
== alloc
->y
) &&
79 (win
->m_width
== alloc
->width
) &&
80 (win
->m_height
== alloc
->height
))
86 printf( "OnResize from " );
87 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
88 printf( win->GetClassInfo()->GetClassName() );
91 printf( " Old: X: %d Y: %d ", win->m_x, win->m_y );
92 printf( " W: %d H: %d ", win->m_width, win->m_height );
95 printf( " New: X: %d Y: %d ", alloc->x, alloc->y );
96 printf( " W: %d H: %d ", alloc->width, alloc->height );
100 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
103 printf( " Res: X: %d Y: %d ", win->m_x, win->m_y );
104 printf( " W: %d H: %d ", win->m_width, win->m_height );
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
115 void wxNotebook::Init()
118 m_pages
.DeleteContents( TRUE
);
122 wxNotebook::wxNotebook()
127 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
128 const wxPoint
& pos
, const wxSize
& size
,
129 long style
, const wxString
& name
)
132 Create( parent
, id
, pos
, size
, style
, name
);
135 wxNotebook::~wxNotebook()
137 // don't generate change page events any more
138 if ( m_idHandler
!= 0 )
139 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
144 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
145 const wxPoint
& pos
, const wxSize
& size
,
146 long style
, const wxString
& name
)
150 PreCreation( parent
, id
, pos
, size
, style
, name
);
152 m_widget
= gtk_notebook_new();
154 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
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 m_children
.Append(win
);
402 wxNotebookPage
*page
= new wxNotebookPage();
404 page
->m_id
= GetPageCount();
405 page
->m_label
= (GtkLabel
*)gtk_label_new(_("Handle"));
406 page
->m_client
= win
;
407 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
,
408 (GtkWidget
*)page
->m_label
);
409 gtk_misc_set_alignment(GTK_MISC(page
->m_label
), 0.0, 0.5);
412 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
414 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
416 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
417 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
421 wxLogFatalError( _("Notebook page creation error") );
425 m_pages
.Append( page
);
428 // override these 2 functions to do nothing: everything is done in OnSize
429 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
431 // don't set the sizes of the pages - their correct size is not yet known
432 wxControl::SetConstraintSizes(FALSE
);
435 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
440 //-----------------------------------------------------------------------------
442 //-----------------------------------------------------------------------------
444 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)