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
34 m_page
= (GtkNotebookPage
*) NULL
;
35 m_client
= (wxWindow
*) NULL
;
36 m_parent
= (GtkNotebook
*) NULL
;
37 m_box
= (GtkWidget
*) NULL
;
44 GtkNotebookPage
*m_page
;
47 GtkNotebook
*m_parent
;
48 GtkWidget
*m_box
; // in which the label and image are packed
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
56 GtkNotebookPage
*WXUNUSED(page
),
60 wxNotebook
*notebook
= (wxNotebook
*)data
;
62 int old
= notebook
->GetSelection();
64 // TODO: emulate PAGE_CHANGING event
65 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
69 event
.SetEventObject( notebook
);
70 notebook
->ProcessEvent( event
);
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
79 if (win
->GetAutoLayout()) win
->Layout();
81 if ((win
->m_x
== alloc
->x
) &&
82 (win
->m_y
== alloc
->y
) &&
83 (win
->m_width
== alloc
->width
) &&
84 (win
->m_height
== alloc
->height
))
89 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
92 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
96 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
98 void wxNotebook::Init()
100 m_imageList
= (wxImageList
*) NULL
;
101 m_pages
.DeleteContents( TRUE
);
105 wxNotebook::wxNotebook()
110 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
111 const wxPoint
& pos
, const wxSize
& size
,
112 long style
, const wxString
& name
)
115 Create( parent
, id
, pos
, size
, style
, name
);
118 wxNotebook::~wxNotebook()
120 // don't generate change page events any more
121 if (m_idHandler
!= 0)
122 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
127 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
128 const wxPoint
& pos
, const wxSize
& size
,
129 long style
, const wxString
& name
)
133 PreCreation( parent
, id
, pos
, size
, style
, name
);
135 m_widget
= gtk_notebook_new();
137 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
139 m_idHandler
= gtk_signal_connect
141 GTK_OBJECT(m_widget
), "switch_page",
142 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
153 int wxNotebook::GetSelection() const
155 if (m_pages
.Number() == 0) return -1;
157 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
159 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
161 wxNode
*node
= m_pages
.First();
164 page
= (wxNotebookPage
*)node
->Data();
165 if (page
->m_page
== g_page
)
170 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
175 int wxNotebook::GetPageCount() const
177 return m_pages
.Number();
180 int wxNotebook::GetRowCount() const
185 wxString
wxNotebook::GetPageText( int page
) const
187 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
189 return nb_page
->m_text
;
194 int wxNotebook::GetPageImage( int page
) const
196 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
198 return nb_page
->m_image
;
203 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
205 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
207 wxNode
*node
= m_pages
.First();
210 nb_page
= (wxNotebookPage
*)node
->Data();
211 if (nb_page
->m_id
== page
)
216 wxLogDebug( "Notebook page %d not found!", page
);
218 return (wxNotebookPage
*) NULL
;
221 int wxNotebook::SetSelection( int page
)
223 int selOld
= GetSelection();
224 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
226 if (!nb_page
) return -1;
229 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
232 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
237 if (!child
) return -1;
239 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
244 void wxNotebook::AdvanceSelection( bool bForward
)
246 int sel
= GetSelection();
247 int max
= GetPageCount();
250 SetSelection( sel
== max
? 0 : sel
+ 1 );
252 SetSelection( sel
== 0 ? max
: sel
- 1 );
255 void wxNotebook::SetImageList( wxImageList
* imageList
)
257 m_imageList
= imageList
;
260 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
262 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
264 if (!nb_page
) return FALSE
;
266 nb_page
->m_text
= text
;
271 bool wxNotebook::SetPageImage( int page
, int image
)
273 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
275 if (!nb_page
) return FALSE
;
277 nb_page
->m_image
= image
;
282 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
284 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
287 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
289 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
292 bool wxNotebook::DeleteAllPages()
294 wxNode
*page_node
= m_pages
.First();
297 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
299 DeletePage( page
->m_id
);
301 page_node
= m_pages
.First();
307 bool wxNotebook::DeletePage( int page
)
309 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
310 if (!nb_page
) return FALSE
;
313 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
316 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
323 delete nb_page
->m_client
;
325 // Amazingly, this is not necessary
326 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
328 m_pages
.DeleteObject( nb_page
);
333 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
334 bool bSelect
, int imageId
)
336 // we've created the notebook page in AddChild(). Now we just have to set
337 // the caption for the page and set the others parameters.
339 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
341 wxNode
*node
= m_pages
.First();
344 page
= (wxNotebookPage
*)node
->Data();
345 if ( page
->m_client
== win
) break;
349 wxCHECK_MSG( page
!= NULL
, FALSE
, "Can't add a page whose parent is not the notebook!" );
353 wxASSERT( m_imageList
!= NULL
);
355 wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
356 GdkPixmap
*pixmap
= bmp
->GetPixmap();
357 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
358 if (bmp
->GetMask()) mask
= bmp
->GetMask()->GetBitmap();
359 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
361 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
363 gtk_widget_show(pixmapwid
);
366 // then set the attributes
368 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
369 page
->m_image
= imageId
;
370 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
371 gtk_box_pack_start( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
373 // @@@: what does this do? do we still need it?
374 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
376 gtk_widget_show((GtkWidget
*)page
->m_label
);
378 if (bSelect
) SetSelection(GetPageCount());
383 wxWindow
*wxNotebook::GetPage( int page
) const
385 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
387 return (wxWindow
*) NULL
;
389 return nb_page
->m_client
;
392 void wxNotebook::AddChild( wxWindow
*win
)
394 m_children
.Append(win
);
396 wxNotebookPage
*page
= new wxNotebookPage();
398 page
->m_id
= GetPageCount();
400 page
->m_box
= gtk_hbox_new (FALSE
, 0);
401 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
403 page
->m_client
= win
;
404 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
, page
->m_box
);
407 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
409 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
411 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
412 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
416 wxLogFatalError( "Notebook page creation error" );
420 m_pages
.Append( page
);
423 // override these 2 functions to do nothing: everything is done in OnSize
424 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
426 // don't set the sizes of the pages - their correct size is not yet known
427 wxControl::SetConstraintSizes(FALSE
);
430 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
435 //-----------------------------------------------------------------------------
437 //-----------------------------------------------------------------------------
439 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)