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
);
103 if (win
->GetAutoLayout()) win
->Layout();
106 //-----------------------------------------------------------------------------
107 // InsertChild callback for wxNotebook
108 //-----------------------------------------------------------------------------
110 static void wxInsertChildInNotebook( wxNotebook
* parent
, wxWindow
* child
)
112 wxNotebookPage
*page
= new wxNotebookPage();
114 page
->m_id
= parent
->GetPageCount();
116 page
->m_box
= gtk_hbox_new (FALSE
, 0);
117 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
119 GtkNotebook
*notebook
= GTK_NOTEBOOK(parent
->m_widget
);
121 page
->m_client
= child
;
122 gtk_notebook_append_page( notebook
, child
->m_widget
, page
->m_box
);
124 page
->m_page
= (GtkNotebookPage
*) (g_list_last(notebook
->children
)->data
);
126 page
->m_parent
= notebook
;
128 gtk_signal_connect( GTK_OBJECT(child
->m_widget
), "size_allocate",
129 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)child
);
133 wxLogFatalError( "Notebook page creation error" );
137 parent
->m_pages
.Append( page
);
140 //-----------------------------------------------------------------------------
142 //-----------------------------------------------------------------------------
144 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
146 void wxNotebook::Init()
148 m_imageList
= (wxImageList
*) NULL
;
149 m_pages
.DeleteContents( TRUE
);
153 wxNotebook::wxNotebook()
158 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
159 const wxPoint
& pos
, const wxSize
& size
,
160 long style
, const wxString
& name
)
163 Create( parent
, id
, pos
, size
, style
, name
);
166 wxNotebook::~wxNotebook()
168 // don't generate change page events any more
169 if (m_idHandler
!= 0)
170 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
175 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
176 const wxPoint
& pos
, const wxSize
& size
,
177 long style
, const wxString
& name
)
180 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
182 PreCreation( parent
, id
, pos
, size
, style
, name
);
184 m_widget
= gtk_notebook_new();
186 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
188 m_idHandler
= gtk_signal_connect (
189 GTK_OBJECT(m_widget
), "switch_page",
190 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
193 m_parent
->AddChild( this );
195 (m_parent
->m_insertCallback
)( m_parent
, this );
204 int wxNotebook::GetSelection() const
206 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
208 if (m_pages
.Number() == 0) return -1;
210 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
211 if (!g_page
) return -1;
213 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
215 wxNode
*node
= m_pages
.First();
218 page
= (wxNotebookPage
*)node
->Data();
220 if ((page
->m_page
== g_page
) || (page
->m_page
== (GtkNotebookPage
*)NULL
))
222 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
223 // "switch_page" then and we ask for GetSelection() in the handler for
224 // "switch_page". otherwise m_page should never be NULL. all this
225 // might also be wrong.
231 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
236 int wxNotebook::GetPageCount() const
238 // count only the pages which were already added to the notebook for MSW
239 // compatibility (and, in fact, this behaviour makes more sense anyhow
240 // because only the added pages are shown)
242 for ( wxNode
*node
= m_pages
.First(); node
; node
= node
->Next() )
244 wxNotebookPage
*page
= (wxNotebookPage
*)node
->Data();
245 if ( page
->WasAdded() )
252 int wxNotebook::GetRowCount() const
257 wxString
wxNotebook::GetPageText( int page
) const
259 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid notebook" );
261 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
263 return nb_page
->m_text
;
268 int wxNotebook::GetPageImage( int page
) const
270 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid notebook" );
272 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
274 return nb_page
->m_image
;
279 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
281 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*)NULL
, "invalid notebook" );
283 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
285 wxNode
*node
= m_pages
.First();
288 nb_page
= (wxNotebookPage
*)node
->Data();
289 if (nb_page
->m_id
== page
)
294 wxLogDebug( "Notebook page %d not found!", page
);
296 return (wxNotebookPage
*) NULL
;
299 int wxNotebook::SetSelection( int page
)
301 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
303 int selOld
= GetSelection();
304 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
306 if (!nb_page
) return -1;
309 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
312 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
317 if (!child
) return -1;
319 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
324 void wxNotebook::AdvanceSelection( bool bForward
)
326 wxCHECK_RET( m_widget
!= NULL
, "invalid notebook" );
328 int sel
= GetSelection();
329 int max
= GetPageCount();
332 SetSelection( sel
== max
? 0 : sel
+ 1 );
334 SetSelection( sel
== 0 ? max
: sel
- 1 );
337 void wxNotebook::SetImageList( wxImageList
* imageList
)
339 m_imageList
= imageList
;
342 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
344 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
346 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
348 if (!nb_page
) return FALSE
;
350 nb_page
->m_text
= text
;
355 bool wxNotebook::SetPageImage( int page
, int image
)
357 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
359 if (!nb_page
) return FALSE
;
361 nb_page
->m_image
= image
;
366 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
368 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
371 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
373 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
376 bool wxNotebook::DeleteAllPages()
378 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
380 wxNode
*page_node
= m_pages
.First();
383 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
385 DeletePage( page
->m_id
);
387 page_node
= m_pages
.First();
393 bool wxNotebook::DeletePage( int page
)
395 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
396 if (!nb_page
) return FALSE
;
399 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
402 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
407 wxCHECK_MSG( child
!= NULL
, FALSE
, "illegal notebook index" );
409 delete nb_page
->m_client
;
411 m_pages
.DeleteObject( nb_page
);
416 bool wxNotebook::RemovePage( int page
)
418 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
419 if (!nb_page
) return FALSE
;
422 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
425 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
430 wxCHECK_MSG( child
!= NULL
, FALSE
, "illegal notebook index" );
432 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page_num
);
434 m_pages
.DeleteObject( nb_page
);
439 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
440 bool bSelect
, int imageId
)
442 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
444 // we've created the notebook page in AddChild(). Now we just have to set
445 // the caption for the page and set the others parameters.
447 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
449 wxNode
*node
= m_pages
.First();
452 page
= (wxNotebookPage
*)node
->Data();
453 if ( page
->m_client
== win
) break;
457 wxCHECK_MSG( page
!= NULL
, FALSE
,
458 "Can't add a page whose parent is not the notebook!" );
460 wxCHECK_MSG( page
->Add(), FALSE
,
461 "Can't add the same page twice to a notebook." );
465 wxASSERT( m_imageList
!= NULL
);
467 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
468 GdkPixmap
*pixmap
= bmp
->GetPixmap();
469 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
470 if ( bmp
->GetMask() )
472 mask
= bmp
->GetMask()->GetBitmap();
475 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
477 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
479 gtk_widget_show(pixmapwid
);
482 // then set the attributes
484 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
485 page
->m_image
= imageId
;
486 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
487 gtk_box_pack_start( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
489 // @@@: what does this do? do we still need it?
490 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
492 gtk_widget_show((GtkWidget
*)page
->m_label
);
494 if (bSelect
) SetSelection(GetPageCount());
499 wxWindow
*wxNotebook::GetPage( int page
) const
501 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, "invalid notebook" );
503 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
505 return (wxWindow
*) NULL
;
507 return nb_page
->m_client
;
510 // override these 2 functions to do nothing: everything is done in OnSize
511 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
513 // don't set the sizes of the pages - their correct size is not yet known
514 wxControl::SetConstraintSizes(FALSE
);
517 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
522 void wxNotebook::ApplyWidgetStyle()
525 gtk_widget_set_style( m_widget
, m_widgetStyle
);
528 //-----------------------------------------------------------------------------
530 //-----------------------------------------------------------------------------
532 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)