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"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxNotebookPage
: public wxObject
42 GtkNotebookPage
*m_page
;
45 GtkNotebook
*m_parent
;
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 // page change callback
53 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
54 GtkNotebookPage
*WXUNUSED(page
),
58 wxNotebook
*notebook
= (wxNotebook
*)data
;
60 int nOld
= notebook
->GetSelection();
62 // TODO: emulate PAGE_CHANGING event
63 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
67 event
.SetEventObject(notebook
);
68 notebook
->ProcessEvent(event
);
71 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
73 if ( win
->GetAutoLayout() )
76 if ((win
->m_x
== alloc
->x
) &&
77 (win
->m_y
== alloc
->y
) &&
78 (win
->m_width
== alloc
->width
) &&
79 (win
->m_height
== alloc
->height
))
85 printf( "OnResize from " );
86 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
87 printf( win->GetClassInfo()->GetClassName() );
90 printf( " Old: X: %d Y: %d ", win->m_x, win->m_y );
91 printf( " W: %d H: %d ", win->m_width, win->m_height );
94 printf( " New: X: %d Y: %d ", alloc->x, alloc->y );
95 printf( " W: %d H: %d ", alloc->width, alloc->height );
99 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
102 printf( " Res: X: %d Y: %d ", win->m_x, win->m_y );
103 printf( " W: %d H: %d ", win->m_width, win->m_height );
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
112 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
113 EVT_SIZE(wxNotebook::OnSize
)
116 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
118 void wxNotebook::Init()
121 m_pages
.DeleteContents( TRUE
);
125 wxNotebook::wxNotebook()
130 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
131 const wxPoint
& pos
, const wxSize
& size
,
132 long style
, const wxString
& name
)
135 Create( parent
, id
, pos
, size
, style
, name
);
138 wxNotebook::~wxNotebook()
140 // don't generate change page events any more
141 if ( m_idHandler
!= 0 )
142 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();
157 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
159 m_idHandler
= gtk_signal_connect
161 GTK_OBJECT(m_widget
), "switch_page",
162 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
173 int wxNotebook::GetSelection() const
175 if (m_pages
.Number() == 0)
178 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
180 wxNotebookPage
*page
= NULL
;
182 wxNode
*node
= m_pages
.First();
185 page
= (wxNotebookPage
*)node
->Data();
186 if (page
->m_page
== g_page
)
191 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?");
196 int wxNotebook::GetPageCount() const
198 return m_pages
.Number();
201 int wxNotebook::GetRowCount() const
206 wxString
wxNotebook::GetPageText( int page
) const
208 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
210 return nb_page
->m_text
;
215 int wxNotebook::GetPageImage( int page
) const
217 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
219 return nb_page
->m_image
;
224 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
226 wxNotebookPage
*nb_page
= NULL
;
228 wxNode
*node
= m_pages
.First();
231 nb_page
= (wxNotebookPage
*)node
->Data();
232 if (nb_page
->m_id
== page
)
237 wxLogDebug( "Notebook page %d not found!", page
);
242 int wxNotebook::SetSelection( int page
)
244 int selOld
= GetSelection();
245 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
250 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
253 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
)
259 if (!child
) return -1;
261 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
266 void wxNotebook::AdvanceSelection(bool bForward
)
268 int nSel
= GetSelection(),
269 nMax
= GetPageCount();
272 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
275 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
279 void wxNotebook::SetImageList( wxImageList
* imageList
)
281 m_imageList
= imageList
;
284 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
286 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
290 nb_page
->m_text
= text
;
295 bool wxNotebook::SetPageImage( int page
, int image
)
297 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
301 nb_page
->m_image
= image
;
306 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
308 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
311 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
313 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
316 bool wxNotebook::DeleteAllPages()
318 wxNode
*page_node
= m_pages
.First();
321 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
323 DeletePage( page
->m_id
);
325 page_node
= m_pages
.First();
331 bool wxNotebook::DeletePage( int page
)
333 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
334 if (!nb_page
) return FALSE
;
337 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
340 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
347 delete nb_page
->m_client
;
349 // Amazingly, this is not necessary
350 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
352 m_pages
.DeleteObject( nb_page
);
357 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
358 bool bSelect
, int imageId
)
360 // we've created the notebook page in AddChild(). Now we just have to set
361 // the caption for the page and set the others parameters.
363 // first, find the page
364 wxNotebookPage
*page
= NULL
;
366 wxNode
*node
= m_pages
.First();
369 page
= (wxNotebookPage
*)node
->Data();
370 if ( page
->m_client
== win
)
375 wxCHECK_MSG(page
!= NULL
, FALSE
,
376 "Can't add a page whose parent is not the notebook!");
378 // then set the attributes
380 if ( page
->m_text
.IsEmpty() )
382 page
->m_image
= imageId
;
383 gtk_label_set(page
->m_label
, page
->m_text
);
386 SetSelection(GetPageCount());
392 wxWindow
*wxNotebook::GetPage( int page
) const
394 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
398 return nb_page
->m_client
;
401 void wxNotebook::AddChild( wxWindow
*win
)
403 // @@@ normally done in wxWindow::AddChild but for some reason wxNotebook
404 // case is special there (Robert?)
405 // Robert: Don't you think the code below looks different from the one
406 // in wxWindow::AddChild :-)
408 m_children
.Append(win
);
410 wxNotebookPage
*page
= new wxNotebookPage();
412 page
->m_id
= GetPageCount();
413 page
->m_label
= (GtkLabel
*)gtk_label_new("Handle");
414 page
->m_client
= win
;
415 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
,
416 (GtkWidget
*)page
->m_label
);
417 gtk_misc_set_alignment(GTK_MISC(page
->m_label
), 0.0, 0.5);
420 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
422 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
424 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
425 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
429 wxLogFatalError( "Notebook page creation error" );
433 m_pages
.Append( page
);
436 // override these 2 functions to do nothing: everything is done in OnSize
437 void wxNotebook::SetConstraintSizes(bool /* recurse */)
439 // don't set the sizes of the pages - their correct size is not yet known
440 wxControl::SetConstraintSizes(FALSE
);
443 bool wxNotebook::DoPhase(int /* nPhase */)
448 //-----------------------------------------------------------------------------
450 //-----------------------------------------------------------------------------
452 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)