1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "notebook.h"
14 #include "wx/notebook.h"
17 #include "wx/imaglist.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxNotebookPage
: public wxObject
33 m_page
= (GtkNotebookPage
*) NULL
;
34 m_client
= (wxWindow
*) NULL
;
35 m_parent
= (GtkNotebook
*) NULL
;
36 m_box
= (GtkWidget
*) NULL
;
43 GtkNotebookPage
*m_page
;
46 GtkNotebook
*m_parent
;
47 GtkWidget
*m_box
; // in which the label and image are packed
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
55 GtkNotebookPage
*WXUNUSED(page
),
59 wxNotebook
*notebook
= (wxNotebook
*)data
;
61 int old
= notebook
->GetSelection();
63 // TODO: emulate PAGE_CHANGING event
65 wxNotebookEvent
event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
66 notebook
->GetId(), nPage
, old
);
67 event
.SetEventObject( notebook
);
68 notebook
->GetEventHandler()->ProcessEvent( event
);
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
75 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
77 if (win
->GetAutoLayout()) win
->Layout();
79 if ((win
->m_x
== alloc
->x
) &&
80 (win
->m_y
== alloc
->y
) &&
81 (win
->m_width
== alloc
->width
) &&
82 (win
->m_height
== alloc
->height
))
87 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
96 void wxNotebook::Init()
98 m_imageList
= (wxImageList
*) NULL
;
99 m_pages
.DeleteContents( TRUE
);
103 wxNotebook::wxNotebook()
108 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
109 const wxPoint
& pos
, const wxSize
& size
,
110 long style
, const wxString
& name
)
113 Create( parent
, id
, pos
, size
, style
, name
);
116 wxNotebook::~wxNotebook()
118 // don't generate change page events any more
119 if (m_idHandler
!= 0)
120 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
125 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
126 const wxPoint
& pos
, const wxSize
& size
,
127 long style
, const wxString
& name
)
131 PreCreation( parent
, id
, pos
, size
, style
, name
);
133 m_widget
= gtk_notebook_new();
135 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
137 m_idHandler
= gtk_signal_connect
139 GTK_OBJECT(m_widget
), "switch_page",
140 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
151 int wxNotebook::GetSelection() const
153 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
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 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid notebook" );
189 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
191 return nb_page
->m_text
;
196 int wxNotebook::GetPageImage( int page
) const
198 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid notebook" );
200 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
202 return nb_page
->m_image
;
207 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
209 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*)NULL
, "invalid notebook" );
211 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
213 wxNode
*node
= m_pages
.First();
216 nb_page
= (wxNotebookPage
*)node
->Data();
217 if (nb_page
->m_id
== page
)
222 wxLogDebug( "Notebook page %d not found!", page
);
224 return (wxNotebookPage
*) NULL
;
227 int wxNotebook::SetSelection( int page
)
229 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
231 int selOld
= GetSelection();
232 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
234 if (!nb_page
) return -1;
237 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
240 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
245 if (!child
) return -1;
247 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
252 void wxNotebook::AdvanceSelection( bool bForward
)
254 wxCHECK_RET( m_widget
!= NULL
, "invalid notebook" );
256 int sel
= GetSelection();
257 int max
= GetPageCount();
260 SetSelection( sel
== max
? 0 : sel
+ 1 );
262 SetSelection( sel
== 0 ? max
: sel
- 1 );
265 void wxNotebook::SetImageList( wxImageList
* imageList
)
267 m_imageList
= imageList
;
270 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
272 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
274 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
276 if (!nb_page
) return FALSE
;
278 nb_page
->m_text
= text
;
283 bool wxNotebook::SetPageImage( int page
, int image
)
285 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
287 if (!nb_page
) return FALSE
;
289 nb_page
->m_image
= image
;
294 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
296 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
299 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
301 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
304 bool wxNotebook::DeleteAllPages()
306 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
308 wxNode
*page_node
= m_pages
.First();
311 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
313 DeletePage( page
->m_id
);
315 page_node
= m_pages
.First();
321 bool wxNotebook::DeletePage( int page
)
323 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
324 if (!nb_page
) return FALSE
;
327 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
330 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
337 delete nb_page
->m_client
;
339 // Amazingly, this is not necessary
340 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
342 m_pages
.DeleteObject( nb_page
);
347 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
348 bool bSelect
, int imageId
)
350 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
352 // we've created the notebook page in AddChild(). Now we just have to set
353 // the caption for the page and set the others parameters.
355 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
357 wxNode
*node
= m_pages
.First();
360 page
= (wxNotebookPage
*)node
->Data();
361 if ( page
->m_client
== win
) break;
365 wxCHECK_MSG( page
!= NULL
, FALSE
, "Can't add a page whose parent is not the notebook!" );
369 wxASSERT( m_imageList
!= NULL
);
371 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
372 GdkPixmap
*pixmap
= bmp
->GetPixmap();
373 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
374 if ( bmp
->GetMask() )
376 mask
= bmp
->GetMask()->GetBitmap();
379 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
381 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
383 gtk_widget_show(pixmapwid
);
386 // then set the attributes
388 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
389 page
->m_image
= imageId
;
390 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
391 gtk_box_pack_start( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
393 // @@@: what does this do? do we still need it?
394 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
396 gtk_widget_show((GtkWidget
*)page
->m_label
);
398 if (bSelect
) SetSelection(GetPageCount());
403 wxWindow
*wxNotebook::GetPage( int page
) const
405 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, "invalid notebook" );
407 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
409 return (wxWindow
*) NULL
;
411 return nb_page
->m_client
;
414 void wxNotebook::AddChild( wxWindow
*win
)
416 wxCHECK_RET( m_widget
!= NULL
, "invalid notebook" );
418 m_children
.Append(win
);
420 wxNotebookPage
*page
= new wxNotebookPage();
422 page
->m_id
= GetPageCount();
424 page
->m_box
= gtk_hbox_new (FALSE
, 0);
425 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
427 page
->m_client
= win
;
428 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
, page
->m_box
);
431 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
433 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
435 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
436 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
440 wxLogFatalError( "Notebook page creation error" );
444 m_pages
.Append( page
);
447 // override these 2 functions to do nothing: everything is done in OnSize
448 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
450 // don't set the sizes of the pages - their correct size is not yet known
451 wxControl::SetConstraintSizes(FALSE
);
454 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
459 void wxNotebook::ApplyWidgetStyle()
462 gtk_widget_set_style( m_widget
, m_widgetStyle
);
465 //-----------------------------------------------------------------------------
467 //-----------------------------------------------------------------------------
469 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)