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 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
114 void wxNotebook::Init()
117 m_pages
.DeleteContents( TRUE
);
121 wxNotebook::wxNotebook()
126 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
127 const wxPoint
& pos
, const wxSize
& size
,
128 long style
, const wxString
& name
)
131 Create( parent
, id
, pos
, size
, style
, name
);
134 wxNotebook::~wxNotebook()
136 // don't generate change page events any more
137 if ( m_idHandler
!= 0 )
138 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
143 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
144 const wxPoint
& pos
, const wxSize
& size
,
145 long style
, const wxString
& name
)
149 PreCreation( parent
, id
, pos
, size
, style
, name
);
151 m_widget
= gtk_notebook_new();
153 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
155 m_idHandler
= gtk_signal_connect
157 GTK_OBJECT(m_widget
), "switch_page",
158 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
169 int wxNotebook::GetSelection() const
171 if (m_pages
.Number() == 0)
174 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
176 wxNotebookPage
*page
= NULL
;
178 wxNode
*node
= m_pages
.First();
181 page
= (wxNotebookPage
*)node
->Data();
182 if (page
->m_page
== g_page
)
187 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?");
192 int wxNotebook::GetPageCount() const
194 return m_pages
.Number();
197 int wxNotebook::GetRowCount() const
202 wxString
wxNotebook::GetPageText( int page
) const
204 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
206 return nb_page
->m_text
;
211 int wxNotebook::GetPageImage( int page
) const
213 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
215 return nb_page
->m_image
;
220 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
222 wxNotebookPage
*nb_page
= NULL
;
224 wxNode
*node
= m_pages
.First();
227 nb_page
= (wxNotebookPage
*)node
->Data();
228 if (nb_page
->m_id
== page
)
233 wxLogDebug( "Notebook page %d not found!", page
);
238 int wxNotebook::SetSelection( int page
)
240 int selOld
= GetSelection();
241 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
246 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
249 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
)
255 if (!child
) return -1;
257 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
262 void wxNotebook::AdvanceSelection(bool bForward
)
264 int nSel
= GetSelection(),
265 nMax
= GetPageCount();
268 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
271 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
275 void wxNotebook::SetImageList( wxImageList
* imageList
)
277 m_imageList
= imageList
;
280 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
282 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
286 nb_page
->m_text
= text
;
291 bool wxNotebook::SetPageImage( int page
, int image
)
293 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
297 nb_page
->m_image
= image
;
302 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
304 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
307 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
309 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
312 bool wxNotebook::DeleteAllPages()
314 wxNode
*page_node
= m_pages
.First();
317 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
319 DeletePage( page
->m_id
);
321 page_node
= m_pages
.First();
327 bool wxNotebook::DeletePage( int page
)
329 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
330 if (!nb_page
) return FALSE
;
333 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
336 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
343 delete nb_page
->m_client
;
345 // Amazingly, this is not necessary
346 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
348 m_pages
.DeleteObject( nb_page
);
353 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
354 bool bSelect
, int imageId
)
356 // we've created the notebook page in AddChild(). Now we just have to set
357 // the caption for the page and set the others parameters.
359 // first, find the page
360 wxNotebookPage
*page
= NULL
;
362 wxNode
*node
= m_pages
.First();
365 page
= (wxNotebookPage
*)node
->Data();
366 if ( page
->m_client
== win
)
371 wxCHECK_MSG(page
!= NULL
, FALSE
,
372 "Can't add a page whose parent is not the notebook!");
374 // then set the attributes
376 if ( page
->m_text
.IsEmpty() )
378 page
->m_image
= imageId
;
379 gtk_label_set(page
->m_label
, page
->m_text
);
382 SetSelection(GetPageCount());
388 wxWindow
*wxNotebook::GetPage( int page
) const
390 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
394 return nb_page
->m_client
;
397 void wxNotebook::AddChild( wxWindow
*win
)
399 m_children
.Append(win
);
401 wxNotebookPage
*page
= new wxNotebookPage();
403 page
->m_id
= GetPageCount();
404 page
->m_label
= (GtkLabel
*)gtk_label_new("Handle");
405 page
->m_client
= win
;
406 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
,
407 (GtkWidget
*)page
->m_label
);
408 gtk_misc_set_alignment(GTK_MISC(page
->m_label
), 0.0, 0.5);
411 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
413 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
415 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
416 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
420 wxLogFatalError( "Notebook page creation error" );
424 m_pages
.Append( page
);
427 // override these 2 functions to do nothing: everything is done in OnSize
428 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
430 // don't set the sizes of the pages - their correct size is not yet known
431 wxControl::SetConstraintSizes(FALSE
);
434 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
439 //-----------------------------------------------------------------------------
441 //-----------------------------------------------------------------------------
443 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)