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 // page change callback
56 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
57 GtkNotebookPage
*WXUNUSED(page
),
61 wxNotebook
*notebook
= (wxNotebook
*)data
;
63 int nOld
= notebook
->GetSelection();
65 // TODO: emulate PAGE_CHANGING event
66 wxNotebookEvent
event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
70 event
.SetEventObject(notebook
);
71 notebook
->ProcessEvent(event
);
74 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
76 if ( win
->GetAutoLayout() )
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
))
88 printf( "OnResize from " );
89 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
90 printf( win->GetClassInfo()->GetClassName() );
93 printf( " Old: X: %d Y: %d ", win->m_x, win->m_y );
94 printf( " W: %d H: %d ", win->m_width, win->m_height );
97 printf( " New: X: %d Y: %d ", alloc->x, alloc->y );
98 printf( " W: %d H: %d ", alloc->width, alloc->height );
102 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
105 printf( " Res: X: %d Y: %d ", win->m_x, win->m_y );
106 printf( " W: %d H: %d ", win->m_width, win->m_height );
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
115 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
117 void wxNotebook::Init()
119 m_imageList
= (wxImageList
*) NULL
;
120 m_pages
.DeleteContents( TRUE
);
124 wxNotebook::wxNotebook()
129 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
130 const wxPoint
& pos
, const wxSize
& size
,
131 long style
, const wxString
& name
)
134 Create( parent
, id
, pos
, size
, style
, name
);
137 wxNotebook::~wxNotebook()
139 // don't generate change page events any more
140 if ( m_idHandler
!= 0 )
141 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
146 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
147 const wxPoint
& pos
, const wxSize
& size
,
148 long style
, const wxString
& name
)
152 PreCreation( parent
, id
, pos
, size
, style
, name
);
154 m_widget
= gtk_notebook_new();
156 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
158 m_idHandler
= gtk_signal_connect
160 GTK_OBJECT(m_widget
), "switch_page",
161 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
172 int wxNotebook::GetSelection() const
174 if (m_pages
.Number() == 0)
177 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
179 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
181 wxNode
*node
= m_pages
.First();
184 page
= (wxNotebookPage
*)node
->Data();
185 if (page
->m_page
== g_page
)
190 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
195 int wxNotebook::GetPageCount() const
197 return m_pages
.Number();
200 int wxNotebook::GetRowCount() const
205 wxString
wxNotebook::GetPageText( int page
) const
207 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
209 return nb_page
->m_text
;
214 int wxNotebook::GetPageImage( int page
) const
216 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
218 return nb_page
->m_image
;
223 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
225 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
227 wxNode
*node
= m_pages
.First();
230 nb_page
= (wxNotebookPage
*)node
->Data();
231 if (nb_page
->m_id
== page
)
236 wxLogDebug( "Notebook page %d not found!", page
);
238 return (wxNotebookPage
*) NULL
;
241 int wxNotebook::SetSelection( int page
)
243 int selOld
= GetSelection();
244 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
249 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
252 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
)
258 if (!child
) return -1;
260 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
265 void wxNotebook::AdvanceSelection(bool bForward
)
267 int nSel
= GetSelection(),
268 nMax
= GetPageCount();
271 SetSelection(nSel
== nMax
? 0 : nSel
+ 1);
274 SetSelection(nSel
== 0 ? nMax
: nSel
- 1);
278 void wxNotebook::SetImageList( wxImageList
* imageList
)
280 m_imageList
= imageList
;
283 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
285 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
289 nb_page
->m_text
= text
;
294 bool wxNotebook::SetPageImage( int page
, int image
)
296 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
300 nb_page
->m_image
= image
;
305 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
307 wxFAIL_MSG(_("wxNotebook::SetPageSize not implemented"));
310 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
312 wxFAIL_MSG(_("wxNotebook::SetPadding not implemented"));
315 bool wxNotebook::DeleteAllPages()
317 wxNode
*page_node
= m_pages
.First();
320 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
322 DeletePage( page
->m_id
);
324 page_node
= m_pages
.First();
330 bool wxNotebook::DeletePage( int page
)
332 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
333 if (!nb_page
) return FALSE
;
336 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
339 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
346 delete nb_page
->m_client
;
348 // Amazingly, this is not necessary
349 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
351 m_pages
.DeleteObject( nb_page
);
356 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
357 bool bSelect
, int imageId
)
359 // we've created the notebook page in AddChild(). Now we just have to set
360 // the caption for the page and set the others parameters.
362 // first, find the page
363 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
365 wxNode
*node
= m_pages
.First();
368 page
= (wxNotebookPage
*)node
->Data();
369 if ( page
->m_client
== win
)
374 wxCHECK_MSG(page
!= NULL
, FALSE
,
375 "Can't add a page whose parent is not the notebook!");
377 // create the image if any
378 if ( imageId
!= -1 ) {
379 wxASSERT( m_imageList
!= NULL
);
381 wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
382 GdkPixmap
*pixmap
= bmp
->GetPixmap();
383 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, NULL
/* @@@@ */);
385 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
387 gtk_widget_show(pixmapwid
);
390 // then set the attributes
392 if ( page
->m_text
.IsEmpty() )
394 page
->m_image
= imageId
;
395 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
396 gtk_box_pack_start(GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
,
399 // @@@: what does this do? do we still need it?
400 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
402 gtk_widget_show((GtkWidget
*)page
->m_label
);
405 SetSelection(GetPageCount());
411 wxWindow
*wxNotebook::GetPage( int page
) const
413 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
415 return (wxWindow
*) NULL
;
417 return nb_page
->m_client
;
420 void wxNotebook::AddChild( wxWindow
*win
)
422 m_children
.Append(win
);
424 wxNotebookPage
*page
= new wxNotebookPage();
426 page
->m_id
= GetPageCount();
428 page
->m_box
= gtk_hbox_new (FALSE
, 0);
429 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
431 page
->m_client
= win
;
432 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget
), win
->m_widget
,
436 (GtkNotebookPage
*) (g_list_last(GTK_NOTEBOOK(m_widget
)->children
)->data
);
438 page
->m_parent
= GTK_NOTEBOOK(m_widget
);
440 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
441 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
445 wxLogFatalError( _("Notebook page creation error") );
449 m_pages
.Append( page
);
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 //-----------------------------------------------------------------------------
466 //-----------------------------------------------------------------------------
468 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxCommandEvent
)