1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "notebook.h"
15 #include "wx/notebook.h"
18 #include "wx/imaglist.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 class wxNotebookPage
: public wxObject
34 m_page
= (GtkNotebookPage
*) NULL
;
35 m_client
= (wxWindow
*) NULL
;
36 m_parent
= (GtkNotebook
*) NULL
;
37 m_box
= (GtkWidget
*) NULL
;
44 GtkNotebookPage
*m_page
;
47 GtkNotebook
*m_parent
;
48 GtkWidget
*m_box
; // in which the label and image are packed
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
56 GtkNotebookPage
*WXUNUSED(page
),
60 wxNotebook
*notebook
= (wxNotebook
*)data
;
62 int old
= notebook
->GetSelection();
64 // TODO: emulate PAGE_CHANGING event
66 wxNotebookEvent
event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
67 notebook
->GetId(), nPage
, old
);
68 event
.SetEventObject( notebook
);
69 notebook
->GetEventHandler()->ProcessEvent( event
);
72 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
76 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
78 if (win
->GetAutoLayout()) win
->Layout();
80 if ((win
->m_x
== alloc
->x
) &&
81 (win
->m_y
== alloc
->y
) &&
82 (win
->m_width
== alloc
->width
) &&
83 (win
->m_height
== alloc
->height
))
88 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
97 void wxNotebook::Init()
99 m_imageList
= (wxImageList
*) NULL
;
100 m_pages
.DeleteContents( TRUE
);
104 wxNotebook::wxNotebook()
109 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
110 const wxPoint
& pos
, const wxSize
& size
,
111 long style
, const wxString
& name
)
114 Create( parent
, id
, pos
, size
, style
, name
);
117 wxNotebook::~wxNotebook()
119 // don't generate change page events any more
120 if (m_idHandler
!= 0)
121 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
126 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
127 const wxPoint
& pos
, const wxSize
& size
,
128 long style
, const wxString
& name
)
132 PreCreation( parent
, id
, pos
, size
, style
, name
);
134 m_widget
= gtk_notebook_new();
136 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
138 m_idHandler
= gtk_signal_connect
140 GTK_OBJECT(m_widget
), "switch_page",
141 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
152 int wxNotebook::GetSelection() const
154 if (m_pages
.Number() == 0) return -1;
156 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
158 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
160 wxNode
*node
= m_pages
.First();
163 page
= (wxNotebookPage
*)node
->Data();
164 if (page
->m_page
== g_page
)
169 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
174 int wxNotebook::GetPageCount() const
176 return m_pages
.Number();
179 int wxNotebook::GetRowCount() const
184 wxString
wxNotebook::GetPageText( int page
) const
186 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
188 return nb_page
->m_text
;
193 int wxNotebook::GetPageImage( int page
) const
195 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
197 return nb_page
->m_image
;
202 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
204 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
206 wxNode
*node
= m_pages
.First();
209 nb_page
= (wxNotebookPage
*)node
->Data();
210 if (nb_page
->m_id
== page
)
215 wxLogDebug( "Notebook page %d not found!", page
);
217 return (wxNotebookPage
*) NULL
;
220 int wxNotebook::SetSelection( int page
)
222 int selOld
= GetSelection();
223 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
225 if (!nb_page
) return -1;
228 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
231 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
236 if (!child
) return -1;
238 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
243 void wxNotebook::AdvanceSelection( bool bForward
)
245 int sel
= GetSelection();
246 int max
= GetPageCount();
249 SetSelection( sel
== max
? 0 : sel
+ 1 );
251 SetSelection( sel
== 0 ? max
: sel
- 1 );
254 void wxNotebook::SetImageList( wxImageList
* imageList
)
256 m_imageList
= imageList
;
259 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
261 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
263 if (!nb_page
) return FALSE
;
265 nb_page
->m_text
= text
;
270 bool wxNotebook::SetPageImage( int page
, int image
)
272 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
274 if (!nb_page
) return FALSE
;
276 nb_page
->m_image
= image
;
281 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
283 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
286 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
288 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
291 bool wxNotebook::DeleteAllPages()
293 wxNode
*page_node
= m_pages
.First();
296 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
298 DeletePage( page
->m_id
);
300 page_node
= m_pages
.First();
306 bool wxNotebook::DeletePage( int page
)
308 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
309 if (!nb_page
) return FALSE
;
312 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
315 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
322 delete nb_page
->m_client
;
324 // Amazingly, this is not necessary
325 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
327 m_pages
.DeleteObject( nb_page
);
332 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
333 bool bSelect
, int imageId
)
335 // we've created the notebook page in AddChild(). Now we just have to set
336 // the caption for the page and set the others parameters.
338 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
340 wxNode
*node
= m_pages
.First();
343 page
= (wxNotebookPage
*)node
->Data();
344 if ( page
->m_client
== win
) break;
348 wxCHECK_MSG( page
!= NULL
, FALSE
, "Can't add a page whose parent is not the notebook!" );
352 wxASSERT( m_imageList
!= NULL
);
354 wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
355 GdkPixmap
*pixmap
= bmp
->GetPixmap();
356 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
357 if (bmp
->GetMask()) mask
= bmp
->GetMask()->GetBitmap();
358 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
360 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
362 gtk_widget_show(pixmapwid
);
365 // then set the attributes
367 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
368 page
->m_image
= imageId
;
369 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
370 gtk_box_pack_start( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
372 // @@@: what does this do? do we still need it?
373 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
375 gtk_widget_show((GtkWidget
*)page
->m_label
);
377 if (bSelect
) SetSelection(GetPageCount());
382 wxWindow
*wxNotebook::GetPage( int page
) const
384 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
386 return (wxWindow
*) NULL
;
388 return nb_page
->m_client
;
391 void wxNotebook::AddChild( wxWindow
*win
)
393 m_children
.Append(win
);
395 wxNotebookPage
*page
= new wxNotebookPage();
397 page
->m_id
= GetPageCount();
399 page
->m_box
= gtk_hbox_new (FALSE
, 0);
400 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
402 page
->m_client
= win
;
403 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
, page
->m_box
);
406 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
408 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
410 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
411 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
415 wxLogFatalError( "Notebook page creation error" );
419 m_pages
.Append( page
);
422 // override these 2 functions to do nothing: everything is done in OnSize
423 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
425 // don't set the sizes of the pages - their correct size is not yet known
426 wxControl::SetConstraintSizes(FALSE
);
429 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
434 //-----------------------------------------------------------------------------
436 //-----------------------------------------------------------------------------
438 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)