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
;
40 // mark page as "added' to the notebook, return FALSE if the page was
51 bool WasAdded() const { return m_added
; }
56 GtkNotebookPage
*m_page
;
59 GtkNotebook
*m_parent
;
60 GtkWidget
*m_box
; // in which the label and image are packed
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
71 GtkNotebookPage
*WXUNUSED(page
),
75 wxNotebook
*notebook
= (wxNotebook
*)data
;
77 int old
= notebook
->GetSelection();
79 // TODO: emulate PAGE_CHANGING event
81 wxNotebookEvent
event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
82 notebook
->GetId(), nPage
, old
);
83 event
.SetEventObject( notebook
);
84 notebook
->GetEventHandler()->ProcessEvent( event
);
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
91 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
93 if ((win
->m_x
== alloc
->x
) &&
94 (win
->m_y
== alloc
->y
) &&
95 (win
->m_width
== alloc
->width
) &&
96 (win
->m_height
== alloc
->height
))
101 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
104 //-----------------------------------------------------------------------------
105 // InsertChild callback for wxNotebook
106 //-----------------------------------------------------------------------------
108 static void wxInsertChildInNotebook( wxNotebook
* parent
, wxWindow
* child
)
110 wxNotebookPage
*page
= new wxNotebookPage();
112 page
->m_id
= parent
->GetPageCount();
114 page
->m_box
= gtk_hbox_new (FALSE
, 0);
115 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
117 GtkNotebook
*notebook
= GTK_NOTEBOOK(parent
->m_widget
);
119 page
->m_client
= child
;
120 gtk_notebook_append_page( notebook
, child
->m_widget
, page
->m_box
);
122 page
->m_page
= (GtkNotebookPage
*) (g_list_last(notebook
->children
)->data
);
124 page
->m_parent
= notebook
;
126 gtk_signal_connect( GTK_OBJECT(child
->m_widget
), "size_allocate",
127 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)child
);
131 wxLogFatalError( "Notebook page creation error" );
135 parent
->m_pages
.Append( page
);
138 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
142 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
144 void wxNotebook::Init()
146 m_imageList
= (wxImageList
*) NULL
;
147 m_pages
.DeleteContents( TRUE
);
151 wxNotebook::wxNotebook()
156 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
157 const wxPoint
& pos
, const wxSize
& size
,
158 long style
, const wxString
& name
)
161 Create( parent
, id
, pos
, size
, style
, name
);
164 wxNotebook::~wxNotebook()
166 // don't generate change page events any more
167 if (m_idHandler
!= 0)
168 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
173 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
174 const wxPoint
& pos
, const wxSize
& size
,
175 long style
, const wxString
& name
)
178 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
180 PreCreation( parent
, id
, pos
, size
, style
, name
);
182 m_widget
= gtk_notebook_new();
184 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
186 m_idHandler
= gtk_signal_connect (
187 GTK_OBJECT(m_widget
), "switch_page",
188 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
191 m_parent
->AddChild( this );
193 (m_parent
->m_insertCallback
)( m_parent
, this );
202 int wxNotebook::GetSelection() const
204 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
206 if (m_pages
.Number() == 0) return -1;
208 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
209 if (!g_page
) return -1;
211 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
213 wxNode
*node
= m_pages
.First();
216 page
= (wxNotebookPage
*)node
->Data();
218 if ((page
->m_page
== g_page
) || (page
->m_page
== (GtkNotebookPage
*)NULL
))
220 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
221 // "switch_page" then and we ask for GetSelection() in the handler for
222 // "switch_page". otherwise m_page should never be NULL. all this
223 // might also be wrong.
229 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
234 int wxNotebook::GetPageCount() const
236 // count only the pages which were already added to the notebook for MSW
237 // compatibility (and, in fact, this behaviour makes more sense anyhow
238 // because only the added pages are shown)
240 for ( wxNode
*node
= m_pages
.First(); node
; node
= node
->Next() )
242 wxNotebookPage
*page
= (wxNotebookPage
*)node
->Data();
243 if ( page
->WasAdded() )
250 int wxNotebook::GetRowCount() const
255 wxString
wxNotebook::GetPageText( int page
) const
257 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid notebook" );
259 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
261 return nb_page
->m_text
;
266 int wxNotebook::GetPageImage( int page
) const
268 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid notebook" );
270 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
272 return nb_page
->m_image
;
277 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
279 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*)NULL
, "invalid notebook" );
281 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
283 wxNode
*node
= m_pages
.First();
286 nb_page
= (wxNotebookPage
*)node
->Data();
287 if (nb_page
->m_id
== page
)
292 wxLogDebug( "Notebook page %d not found!", page
);
294 return (wxNotebookPage
*) NULL
;
297 int wxNotebook::SetSelection( int page
)
299 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
301 int selOld
= GetSelection();
302 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
304 if (!nb_page
) return -1;
307 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
310 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
315 if (!child
) return -1;
317 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
322 void wxNotebook::AdvanceSelection( bool bForward
)
324 wxCHECK_RET( m_widget
!= NULL
, "invalid notebook" );
326 int sel
= GetSelection();
327 int max
= GetPageCount();
330 SetSelection( sel
== max
? 0 : sel
+ 1 );
332 SetSelection( sel
== 0 ? max
: sel
- 1 );
335 void wxNotebook::SetImageList( wxImageList
* imageList
)
337 m_imageList
= imageList
;
340 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
342 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
344 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
346 if (!nb_page
) return FALSE
;
348 nb_page
->m_text
= text
;
353 bool wxNotebook::SetPageImage( int page
, int image
)
355 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
357 if (!nb_page
) return FALSE
;
359 nb_page
->m_image
= image
;
364 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
366 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
369 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
371 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
374 bool wxNotebook::DeleteAllPages()
376 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
378 wxNode
*page_node
= m_pages
.First();
381 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
383 DeletePage( page
->m_id
);
385 page_node
= m_pages
.First();
391 bool wxNotebook::DeletePage( int page
)
393 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
394 if (!nb_page
) return FALSE
;
397 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
400 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
405 wxCHECK_MSG( child
!= NULL
, FALSE
, "illegal notebook index" );
407 delete nb_page
->m_client
;
409 m_pages
.DeleteObject( nb_page
);
414 bool wxNotebook::RemovePage( int page
)
416 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
417 if (!nb_page
) return FALSE
;
420 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
423 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
428 wxCHECK_MSG( child
!= NULL
, FALSE
, "illegal notebook index" );
430 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page_num
);
432 m_pages
.DeleteObject( nb_page
);
437 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
438 bool bSelect
, int imageId
)
440 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
442 // we've created the notebook page in AddChild(). Now we just have to set
443 // the caption for the page and set the others parameters.
445 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
447 wxNode
*node
= m_pages
.First();
450 page
= (wxNotebookPage
*)node
->Data();
451 if ( page
->m_client
== win
) break;
455 wxCHECK_MSG( page
!= NULL
, FALSE
,
456 "Can't add a page whose parent is not the notebook!" );
458 wxCHECK_MSG( page
->Add(), FALSE
,
459 "Can't add the same page twice to a notebook." );
463 wxASSERT( m_imageList
!= NULL
);
465 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
466 GdkPixmap
*pixmap
= bmp
->GetPixmap();
467 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
468 if ( bmp
->GetMask() )
470 mask
= bmp
->GetMask()->GetBitmap();
473 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
475 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
477 gtk_widget_show(pixmapwid
);
480 // then set the attributes
482 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
483 page
->m_image
= imageId
;
484 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
485 gtk_box_pack_start( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
487 // @@@: what does this do? do we still need it?
488 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
490 gtk_widget_show((GtkWidget
*)page
->m_label
);
492 if (bSelect
) SetSelection(GetPageCount());
497 wxWindow
*wxNotebook::GetPage( int page
) const
499 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, "invalid notebook" );
501 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
503 return (wxWindow
*) NULL
;
505 return nb_page
->m_client
;
508 // override these 2 functions to do nothing: everything is done in OnSize
509 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
511 // don't set the sizes of the pages - their correct size is not yet known
512 wxControl::SetConstraintSizes(FALSE
);
515 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
520 void wxNotebook::ApplyWidgetStyle()
523 gtk_widget_set_style( m_widget
, m_widgetStyle
);
526 //-----------------------------------------------------------------------------
528 //-----------------------------------------------------------------------------
530 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)