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 if (m_pages
.Number() == 0) return -1;
155 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
157 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
159 wxNode
*node
= m_pages
.First();
162 page
= (wxNotebookPage
*)node
->Data();
163 if (page
->m_page
== g_page
)
168 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
173 int wxNotebook::GetPageCount() const
175 return m_pages
.Number();
178 int wxNotebook::GetRowCount() const
183 wxString
wxNotebook::GetPageText( int page
) const
185 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
187 return nb_page
->m_text
;
192 int wxNotebook::GetPageImage( int page
) const
194 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
196 return nb_page
->m_image
;
201 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
203 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
205 wxNode
*node
= m_pages
.First();
208 nb_page
= (wxNotebookPage
*)node
->Data();
209 if (nb_page
->m_id
== page
)
214 wxLogDebug( "Notebook page %d not found!", page
);
216 return (wxNotebookPage
*) NULL
;
219 int wxNotebook::SetSelection( int page
)
221 int selOld
= GetSelection();
222 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
224 if (!nb_page
) return -1;
227 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
230 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
235 if (!child
) return -1;
237 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
242 void wxNotebook::AdvanceSelection( bool bForward
)
244 int sel
= GetSelection();
245 int max
= GetPageCount();
248 SetSelection( sel
== max
? 0 : sel
+ 1 );
250 SetSelection( sel
== 0 ? max
: sel
- 1 );
253 void wxNotebook::SetImageList( wxImageList
* imageList
)
255 m_imageList
= imageList
;
258 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
260 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
262 if (!nb_page
) return FALSE
;
264 nb_page
->m_text
= text
;
269 bool wxNotebook::SetPageImage( int page
, int image
)
271 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
273 if (!nb_page
) return FALSE
;
275 nb_page
->m_image
= image
;
280 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
282 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
285 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
287 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
290 bool wxNotebook::DeleteAllPages()
292 wxNode
*page_node
= m_pages
.First();
295 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
297 DeletePage( page
->m_id
);
299 page_node
= m_pages
.First();
305 bool wxNotebook::DeletePage( int page
)
307 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
308 if (!nb_page
) return FALSE
;
311 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
314 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
321 delete nb_page
->m_client
;
323 // Amazingly, this is not necessary
324 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
326 m_pages
.DeleteObject( nb_page
);
331 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
332 bool bSelect
, int imageId
)
334 // we've created the notebook page in AddChild(). Now we just have to set
335 // the caption for the page and set the others parameters.
337 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
339 wxNode
*node
= m_pages
.First();
342 page
= (wxNotebookPage
*)node
->Data();
343 if ( page
->m_client
== win
) break;
347 wxCHECK_MSG( page
!= NULL
, FALSE
, "Can't add a page whose parent is not the notebook!" );
351 wxASSERT( m_imageList
!= NULL
);
353 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
354 GdkPixmap
*pixmap
= bmp
->GetPixmap();
355 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
356 if ( bmp
->GetMask() )
358 mask
= bmp
->GetMask()->GetBitmap();
361 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
363 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
365 gtk_widget_show(pixmapwid
);
368 // then set the attributes
370 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
371 page
->m_image
= imageId
;
372 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
373 gtk_box_pack_start( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
375 // @@@: what does this do? do we still need it?
376 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
378 gtk_widget_show((GtkWidget
*)page
->m_label
);
380 if (bSelect
) SetSelection(GetPageCount());
385 wxWindow
*wxNotebook::GetPage( int page
) const
387 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
389 return (wxWindow
*) NULL
;
391 return nb_page
->m_client
;
394 void wxNotebook::AddChild( wxWindow
*win
)
396 m_children
.Append(win
);
398 wxNotebookPage
*page
= new wxNotebookPage();
400 page
->m_id
= GetPageCount();
402 page
->m_box
= gtk_hbox_new (FALSE
, 0);
403 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
405 page
->m_client
= win
;
406 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
, page
->m_box
);
409 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
411 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
413 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
414 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
418 wxLogFatalError( "Notebook page creation error" );
422 m_pages
.Append( page
);
425 // override these 2 functions to do nothing: everything is done in OnSize
426 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
428 // don't set the sizes of the pages - their correct size is not yet known
429 wxControl::SetConstraintSizes(FALSE
);
432 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
437 //-----------------------------------------------------------------------------
439 //-----------------------------------------------------------------------------
441 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)