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"
23 #include "wx/gtk/win_gtk.h"
24 #include "gdk/gdkkeysyms.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern bool g_blockEventsOnDrag
;
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 class wxNotebookPage
: public wxObject
44 m_page
= (GtkNotebookPage
*) NULL
;
45 m_client
= (wxWindow
*) NULL
;
46 m_parent
= (GtkNotebook
*) NULL
;
47 m_box
= (GtkWidget
*) NULL
;
52 mark page as "added' to the notebook, return FALSE if the page was
65 bool WasAdded() const { return m_added
; }
70 GtkNotebookPage
*m_page
;
73 GtkNotebook
*m_parent
;
74 GtkWidget
*m_box
; // in which the label and image are packed
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
85 GtkNotebookPage
*WXUNUSED(page
),
89 wxNotebook
*notebook
= (wxNotebook
*)data
;
91 int old
= notebook
->GetSelection();
93 // TODO: emulate PAGE_CHANGING event
95 wxNotebookEvent
event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
96 notebook
->GetId(), nPage
, old
);
97 event
.SetEventObject( notebook
);
98 notebook
->GetEventHandler()->ProcessEvent( event
);
101 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
105 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
107 if ((win
->m_x
== alloc
->x
) &&
108 (win
->m_y
== alloc
->y
) &&
109 (win
->m_width
== alloc
->width
) &&
110 (win
->m_height
== alloc
->height
))
115 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
117 if (win
->GetAutoLayout()) win
->Layout();
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
125 gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*notebook
)
127 if (g_blockEventsOnDrag
) return FALSE
;
129 if (!notebook
->HasVMT()) return FALSE
;
131 /* this code makes jumping down from the handles of the notebooks
132 to the actual items in the visible notebook page possible with
133 the down-arrow key */
135 if (gdk_event
->keyval
!= GDK_Down
) return FALSE
;
137 if (notebook
!= notebook
->FindFocus()) return FALSE
;
139 if (notebook
->m_pages
.GetCount() == 0) return FALSE
;
141 wxNode
*node
= notebook
->m_pages
.Nth( notebook
->GetSelection() );
143 if (!node
) return FALSE
;
145 wxNotebookPage
*page
= (wxNotebookPage
*) node
->Data();
147 // don't let others the key event
148 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
150 page
->m_client
->SetFocus();
155 //-----------------------------------------------------------------------------
156 // InsertChild callback for wxNotebook
157 //-----------------------------------------------------------------------------
159 static void wxInsertChildInNotebook( wxNotebook
* parent
, wxWindow
* child
)
161 wxNotebookPage
*page
= new wxNotebookPage();
163 page
->m_id
= parent
->GetPageCount();
165 page
->m_box
= gtk_hbox_new (FALSE
, 0);
166 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
168 GtkNotebook
*notebook
= GTK_NOTEBOOK(parent
->m_widget
);
170 page
->m_client
= child
;
171 gtk_notebook_append_page( notebook
, child
->m_widget
, page
->m_box
);
173 page
->m_page
= (GtkNotebookPage
*) (g_list_last(notebook
->children
)->data
);
175 page
->m_parent
= notebook
;
177 gtk_signal_connect( GTK_OBJECT(child
->m_widget
), "size_allocate",
178 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)child
);
180 wxASSERT_MSG( page
->m_page
, _T("Notebook page creation error") );
182 parent
->m_pages
.Append( page
);
185 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
189 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
191 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
192 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
195 void wxNotebook::Init()
197 m_imageList
= (wxImageList
*) NULL
;
198 m_pages
.DeleteContents( TRUE
);
202 wxNotebook::wxNotebook()
207 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
208 const wxPoint
& pos
, const wxSize
& size
,
209 long style
, const wxString
& name
)
212 Create( parent
, id
, pos
, size
, style
, name
);
215 wxNotebook::~wxNotebook()
217 // don't generate change page events any more
218 if (m_idHandler
!= 0)
219 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
224 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
225 const wxPoint
& pos
, const wxSize
& size
,
226 long style
, const wxString
& name
)
229 m_acceptsFocus
= TRUE
;
230 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
232 PreCreation( parent
, id
, pos
, size
, style
, name
);
234 m_widget
= gtk_notebook_new();
236 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
238 m_idHandler
= gtk_signal_connect (
239 GTK_OBJECT(m_widget
), "switch_page",
240 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
243 m_parent
->AddChild( this );
245 (m_parent
->m_insertCallback
)( m_parent
, this );
247 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
248 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
257 int wxNotebook::GetSelection() const
259 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
261 if (m_pages
.Number() == 0) return -1;
263 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
264 if (!g_page
) return -1;
266 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
268 wxNode
*node
= m_pages
.First();
271 page
= (wxNotebookPage
*)node
->Data();
273 if ((page
->m_page
== g_page
) || (page
->m_page
== (GtkNotebookPage
*)NULL
))
275 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
276 // "switch_page" then and we ask for GetSelection() in the handler for
277 // "switch_page". otherwise m_page should never be NULL. all this
278 // might also be wrong.
284 wxCHECK_MSG( node
!= NULL
, -1, _T("wxNotebook: no selection?") );
289 int wxNotebook::GetPageCount() const
291 // count only the pages which were already added to the notebook for MSW
292 // compatibility (and, in fact, this behaviour makes more sense anyhow
293 // because only the added pages are shown)
296 for ( wxNode
*node
= m_pages
.First(); node
; node
= node
->Next() )
298 wxNotebookPage
*page
= (wxNotebookPage
*)node
->Data();
300 if (page
->WasAdded()) n
++;
306 int wxNotebook::GetRowCount() const
311 wxString
wxNotebook::GetPageText( int page
) const
313 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid notebook") );
315 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
317 return nb_page
->m_text
;
322 int wxNotebook::GetPageImage( int page
) const
324 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid notebook") );
326 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
328 return nb_page
->m_image
;
333 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
335 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*)NULL
, _T("invalid notebook") );
337 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
339 wxNode
*node
= m_pages
.First();
342 nb_page
= (wxNotebookPage
*)node
->Data();
343 if (nb_page
->m_id
== page
)
348 wxFAIL_MSG( _T("Notebook page not found!") );
350 return (wxNotebookPage
*) NULL
;
353 int wxNotebook::SetSelection( int page
)
355 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
357 int selOld
= GetSelection();
358 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
360 if (!nb_page
) return -1;
363 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
366 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
371 if (!child
) return -1;
373 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
378 void wxNotebook::AdvanceSelection( bool bForward
)
380 wxCHECK_RET( m_widget
!= NULL
, _T("invalid notebook") );
382 int sel
= GetSelection();
383 int max
= GetPageCount();
386 SetSelection( sel
== max
? 0 : sel
+ 1 );
388 SetSelection( sel
== 0 ? max
-1 : sel
- 1 );
391 void wxNotebook::SetImageList( wxImageList
* imageList
)
393 m_imageList
= imageList
;
396 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
398 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
400 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
402 wxCHECK_MSG( nb_page
, FALSE
, _T("SetPageText: invalid page index") );
404 nb_page
->m_text
= text
;
406 gtk_label_set(nb_page
->m_label
, nb_page
->m_text
.mbc_str());
411 bool wxNotebook::SetPageImage( int page
, int image
)
413 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
415 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
417 if (!nb_page
) return FALSE
;
419 /* Optimization posibility: return immediately if image unchanged.
420 * Not enabled because it may break existing (stupid) code that
421 * manipulates the imagelist to cycle images */
423 /* if (image == nb_page->m_image) return TRUE; */
425 /* For different cases:
426 1) no image -> no image
431 if (image
== -1 && nb_page
->m_image
== -1)
432 return TRUE
; /* Case 1): Nothing to do. */
434 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
436 if (nb_page
->m_image
!= -1)
438 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
440 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
443 if (GTK_IS_PIXMAP(child
->data
))
445 pixmapwid
= GTK_WIDGET(child
->data
);
451 /* We should have the pixmap widget now */
452 wxASSERT(pixmapwid
!= NULL
);
456 /* If there's no new widget, just remove the old from the box */
457 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
458 nb_page
->m_image
= -1;
460 return TRUE
; /* Case 2) */
464 /* Only cases 3) and 4) left */
465 wxASSERT( m_imageList
!= NULL
); /* Just in case */
467 /* Construct the new pixmap */
468 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
469 GdkPixmap
*pixmap
= bmp
->GetPixmap();
470 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
471 if ( bmp
->GetMask() )
473 mask
= bmp
->GetMask()->GetBitmap();
476 if (pixmapwid
== NULL
)
478 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
479 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
481 /* CHECKME: Are these pack flags okay? */
482 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
483 gtk_widget_show(pixmapwid
);
487 /* Case 4) Simply replace the pixmap */
488 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
491 nb_page
->m_image
= image
;
496 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
498 wxFAIL_MSG( _T("wxNotebook::SetPageSize not implemented") );
501 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
503 wxFAIL_MSG( _T("wxNotebook::SetPadding not implemented") );
506 void wxNotebook::SetTabSize(const wxSize
& sz
)
508 wxFAIL_MSG( _T("wxNotebook::SetTabSize not implemented") );
511 bool wxNotebook::DeleteAllPages()
513 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
515 wxNode
*page_node
= m_pages
.First();
518 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
520 DeletePage( page
->m_id
);
522 page_node
= m_pages
.First();
528 bool wxNotebook::DeletePage( int page
)
530 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
531 if (!nb_page
) return FALSE
;
534 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
537 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
542 wxCHECK_MSG( child
!= NULL
, FALSE
, _T("illegal notebook index") );
544 delete nb_page
->m_client
;
546 m_pages
.DeleteObject( nb_page
);
551 bool wxNotebook::RemovePage( int page
)
553 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
554 if (!nb_page
) return FALSE
;
557 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
560 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
565 wxCHECK_MSG( child
!= NULL
, FALSE
, _T("illegal notebook index") );
567 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page_num
);
569 m_pages
.DeleteObject( nb_page
);
574 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
575 bool select
, int imageId
)
577 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
579 /* we've created the notebook page in AddChild(). Now we just have to set
580 the caption for the page and set the others parameters. */
582 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
584 wxNode
*node
= m_pages
.First();
587 page
= (wxNotebookPage
*)node
->Data();
588 if ( page
->m_client
== win
) break;
592 wxCHECK_MSG( page
!= NULL
, FALSE
,
593 _T("Can't add a page whose parent is not the notebook!") );
595 wxCHECK_MSG( page
->Add(), FALSE
,
596 _T("Can't add the same page twice to a notebook.") );
600 wxASSERT( m_imageList
!= NULL
);
602 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
603 GdkPixmap
*pixmap
= bmp
->GetPixmap();
604 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
605 if ( bmp
->GetMask() )
607 mask
= bmp
->GetMask()->GetBitmap();
610 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
612 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
614 gtk_widget_show(pixmapwid
);
617 /* then set the attributes */
619 if (page
->m_text
.IsEmpty()) page
->m_text
= _T("");
620 page
->m_image
= imageId
;
621 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
.mbc_str());
622 gtk_box_pack_end( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
624 /* @@@: what does this do? do we still need it?
625 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); */
627 gtk_widget_show((GtkWidget
*)page
->m_label
);
629 if (select
) SetSelection( GetPageCount()-1 );
634 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
636 if (event
.IsWindowChange())
637 AdvanceSelection( event
.GetDirection() );
642 wxWindow
*wxNotebook::GetPage( int page
) const
644 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, _T("invalid notebook") );
646 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
648 return (wxWindow
*) NULL
;
650 return nb_page
->m_client
;
653 // override these 2 functions to do nothing: everything is done in OnSize
654 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
656 // don't set the sizes of the pages - their correct size is not yet known
657 wxControl::SetConstraintSizes(FALSE
);
660 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
665 void wxNotebook::ApplyWidgetStyle()
668 gtk_widget_set_style( m_widget
, m_widgetStyle
);
671 //-----------------------------------------------------------------------------
673 //-----------------------------------------------------------------------------
675 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)