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
;
42 GtkNotebookPage
*m_page
;
45 GtkNotebook
*m_parent
;
46 GtkWidget
*m_box
; // in which the label and image are packed
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
54 GtkNotebookPage
*WXUNUSED(page
),
58 wxNotebook
*notebook
= (wxNotebook
*)data
;
60 int old
= notebook
->GetSelection();
62 // TODO: emulate PAGE_CHANGING event
64 wxNotebookEvent
event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
65 notebook
->GetId(), nPage
, old
);
66 event
.SetEventObject( notebook
);
67 notebook
->GetEventHandler()->ProcessEvent( event
);
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
76 if (win
->GetAutoLayout()) win
->Layout();
78 if ((win
->m_x
== alloc
->x
) &&
79 (win
->m_y
== alloc
->y
) &&
80 (win
->m_width
== alloc
->width
) &&
81 (win
->m_height
== alloc
->height
))
86 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
89 //-----------------------------------------------------------------------------
90 // InsertChild callback for wxNotebook
91 //-----------------------------------------------------------------------------
93 static void wxInsertChildInNotebook( wxNotebook
* parent
, wxWindow
* child
)
95 wxNotebookPage
*page
= new wxNotebookPage();
97 page
->m_id
= parent
->GetPageCount();
99 page
->m_box
= gtk_hbox_new (FALSE
, 0);
100 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
102 GtkNotebook
*notebook
= GTK_NOTEBOOK(parent
->m_widget
);
104 page
->m_client
= child
;
105 gtk_notebook_append_page( notebook
, child
->m_widget
, page
->m_box
);
107 page
->m_page
= (GtkNotebookPage
*) (g_list_last(notebook
->children
)->data
);
109 page
->m_parent
= notebook
;
111 gtk_signal_connect( GTK_OBJECT(child
->m_widget
), "size_allocate",
112 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)child
);
116 wxLogFatalError( "Notebook page creation error" );
120 parent
->m_pages
.Append( page
);
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
129 void wxNotebook::Init()
131 m_imageList
= (wxImageList
*) NULL
;
132 m_pages
.DeleteContents( TRUE
);
136 wxNotebook::wxNotebook()
141 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
142 const wxPoint
& pos
, const wxSize
& size
,
143 long style
, const wxString
& name
)
146 Create( parent
, id
, pos
, size
, style
, name
);
149 wxNotebook::~wxNotebook()
151 // don't generate change page events any more
152 if (m_idHandler
!= 0)
153 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
158 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
159 const wxPoint
& pos
, const wxSize
& size
,
160 long style
, const wxString
& name
)
163 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
165 PreCreation( parent
, id
, pos
, size
, style
, name
);
167 m_widget
= gtk_notebook_new();
169 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
171 m_idHandler
= gtk_signal_connect
173 GTK_OBJECT(m_widget
), "switch_page",
174 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
178 m_parent
->AddChild( this );
180 (m_parent
->m_insertCallback
)( m_parent
, this );
189 int wxNotebook::GetSelection() const
191 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
193 if (m_pages
.Number() == 0) return -1;
195 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
197 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
199 wxNode
*node
= m_pages
.First();
202 page
= (wxNotebookPage
*)node
->Data();
203 if (page
->m_page
== g_page
)
208 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
213 int wxNotebook::GetPageCount() const
215 return m_pages
.Number();
218 int wxNotebook::GetRowCount() const
223 wxString
wxNotebook::GetPageText( int page
) const
225 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid notebook" );
227 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
229 return nb_page
->m_text
;
234 int wxNotebook::GetPageImage( int page
) const
236 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid notebook" );
238 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
240 return nb_page
->m_image
;
245 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
247 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*)NULL
, "invalid notebook" );
249 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
251 wxNode
*node
= m_pages
.First();
254 nb_page
= (wxNotebookPage
*)node
->Data();
255 if (nb_page
->m_id
== page
)
260 wxLogDebug( "Notebook page %d not found!", page
);
262 return (wxNotebookPage
*) NULL
;
265 int wxNotebook::SetSelection( int page
)
267 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
269 int selOld
= GetSelection();
270 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
272 if (!nb_page
) return -1;
275 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
278 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
283 if (!child
) return -1;
285 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
290 void wxNotebook::AdvanceSelection( bool bForward
)
292 wxCHECK_RET( m_widget
!= NULL
, "invalid notebook" );
294 int sel
= GetSelection();
295 int max
= GetPageCount();
298 SetSelection( sel
== max
? 0 : sel
+ 1 );
300 SetSelection( sel
== 0 ? max
: sel
- 1 );
303 void wxNotebook::SetImageList( wxImageList
* imageList
)
305 m_imageList
= imageList
;
308 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
310 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
312 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
314 if (!nb_page
) return FALSE
;
316 nb_page
->m_text
= text
;
321 bool wxNotebook::SetPageImage( int page
, int image
)
323 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
325 if (!nb_page
) return FALSE
;
327 nb_page
->m_image
= image
;
332 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
334 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
337 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
339 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
342 bool wxNotebook::DeleteAllPages()
344 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
346 wxNode
*page_node
= m_pages
.First();
349 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
351 DeletePage( page
->m_id
);
353 page_node
= m_pages
.First();
359 bool wxNotebook::DeletePage( int page
)
361 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
362 if (!nb_page
) return FALSE
;
365 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
368 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
375 delete nb_page
->m_client
;
377 // Amazingly, this is not necessary
378 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
380 m_pages
.DeleteObject( nb_page
);
385 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
386 bool bSelect
, int imageId
)
388 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
390 // we've created the notebook page in AddChild(). Now we just have to set
391 // the caption for the page and set the others parameters.
393 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
395 wxNode
*node
= m_pages
.First();
398 page
= (wxNotebookPage
*)node
->Data();
399 if ( page
->m_client
== win
) break;
403 wxCHECK_MSG( page
!= NULL
, FALSE
, "Can't add a page whose parent is not the notebook!" );
407 wxASSERT( m_imageList
!= NULL
);
409 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
410 GdkPixmap
*pixmap
= bmp
->GetPixmap();
411 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
412 if ( bmp
->GetMask() )
414 mask
= bmp
->GetMask()->GetBitmap();
417 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
419 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
421 gtk_widget_show(pixmapwid
);
424 // then set the attributes
426 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
427 page
->m_image
= imageId
;
428 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
429 gtk_box_pack_start( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
431 // @@@: what does this do? do we still need it?
432 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
434 gtk_widget_show((GtkWidget
*)page
->m_label
);
436 if (bSelect
) SetSelection(GetPageCount());
441 wxWindow
*wxNotebook::GetPage( int page
) const
443 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, "invalid notebook" );
445 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
447 return (wxWindow
*) NULL
;
449 return nb_page
->m_client
;
452 // override these 2 functions to do nothing: everything is done in OnSize
453 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
455 // don't set the sizes of the pages - their correct size is not yet known
456 wxControl::SetConstraintSizes(FALSE
);
459 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
464 void wxNotebook::ApplyWidgetStyle()
467 gtk_widget_set_style( m_widget
, m_widgetStyle
);
470 //-----------------------------------------------------------------------------
472 //-----------------------------------------------------------------------------
474 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)