1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/notebook.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/notebook.h"
21 #include "wx/msgdlg.h"
22 #include "wx/bitmap.h"
25 #include "wx/imaglist.h"
26 #include "wx/fontutil.h"
28 #include "wx/gtk/private.h"
30 #include <gdk/gdkkeysyms.h>
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern bool g_blockEventsOnDrag
;
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 // VZ: this is rather ugly as we keep the pages themselves in an array (it
50 // allows us to have quite a few functions implemented in the base class)
51 // but the page data is kept in a separate list, so we must maintain them
52 // in sync manually... of course, the list had been there before the base
53 // class which explains it but it still would be nice to do something
56 class wxGtkNotebookPage
: public wxObject
68 GtkWidget
*m_box
; // in which the label and image are packed
72 #include "wx/listimpl.cpp"
73 WX_DEFINE_LIST(wxGtkNotebookPagesList
)
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
81 static void gtk_notebook_page_changing_callback( GtkNotebook
*widget
,
82 GtkNotebookPage
*WXUNUSED(gpage
),
84 wxNotebook
*notebook
)
86 int old
= gtk_notebook_get_current_page( widget
);
88 if ( !notebook
->SendPageChangingEvent(page
) )
90 // program doesn't allow the page change
91 g_signal_stop_emission_by_name( widget
, "switch_page" );
95 // the page change event also reports the old page
96 notebook
->m_oldSelection
= old
;
102 static void gtk_notebook_page_changed_callback( GtkNotebook
* WXUNUSED(widget
),
103 GtkNotebookPage
*WXUNUSED(gpage
),
104 guint
WXUNUSED(page
),
105 wxNotebook
*notebook
)
107 int old
= notebook
->m_oldSelection
;
108 notebook
->SendPageChangedEvent( old
);
112 //-----------------------------------------------------------------------------
113 // InsertChild callback for wxNotebook
114 //-----------------------------------------------------------------------------
116 static void wxInsertChildInNotebook(wxWindow
* parent
, wxWindow
* child
)
118 // Hack Alert! (Part I): This sets the notebook as the parent of the child
119 // widget, and takes care of some details such as updating the state and
120 // style of the child to reflect its new location. We do this early
121 // because without it GetBestSize (which is used to set the initial size
122 // of controls if an explicit size is not given) will often report
123 // incorrect sizes since the widget's style context is not fully known.
124 // See bug #901694 for details
125 // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
126 gtk_widget_set_parent(child
->m_widget
, parent
->m_widget
);
128 // NOTE: This should be considered a temporary workaround until we can
129 // work out the details and implement delaying the setting of the initial
130 // size of widgets until the size is really needed.
133 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
137 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxBookCtrlBase
)
139 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
140 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
143 void wxNotebook::Init()
147 m_imageList
= (wxImageList
*) NULL
;
149 m_themeEnabled
= true;
152 wxNotebook::wxNotebook()
157 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
158 const wxPoint
& pos
, const wxSize
& size
,
159 long style
, const wxString
& name
)
162 Create( parent
, id
, pos
, size
, style
, name
);
165 wxNotebook::~wxNotebook()
170 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
171 const wxPoint
& pos
, const wxSize
& size
,
172 long style
, const wxString
& name
)
174 m_insertCallback
= wxInsertChildInNotebook
;
176 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
179 if (!PreCreation( parent
, pos
, size
) ||
180 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
182 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
187 m_widget
= gtk_notebook_new();
189 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
191 g_signal_connect (m_widget
, "switch_page",
192 G_CALLBACK (gtk_notebook_page_changing_callback
), this);
194 g_signal_connect_after (m_widget
, "switch_page",
195 G_CALLBACK (gtk_notebook_page_changed_callback
), this);
197 m_parent
->DoAddChild( this );
199 if (m_windowStyle
& wxBK_RIGHT
)
200 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
201 if (m_windowStyle
& wxBK_LEFT
)
202 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
203 if (m_windowStyle
& wxBK_BOTTOM
)
204 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
211 int wxNotebook::GetSelection() const
213 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
215 return gtk_notebook_get_current_page( GTK_NOTEBOOK(m_widget
) );
218 wxString
wxNotebook::GetPageText( size_t page
) const
220 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid notebook") );
222 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
224 return nb_page
->m_text
;
226 return wxEmptyString
;
229 int wxNotebook::GetPageImage( size_t page
) const
231 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
233 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
235 return nb_page
->m_image
;
240 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
242 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
244 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
246 return m_pagesData
.Item(page
)->GetData();
249 int wxNotebook::DoSetSelection( size_t page
, int flags
)
251 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
253 wxCHECK_MSG( page
< m_pagesData
.GetCount(), -1, wxT("invalid notebook index") );
255 int selOld
= GetSelection();
257 if ( !(flags
& SetSelection_SendEvent
) )
259 g_signal_handlers_block_by_func(m_widget
,
260 (gpointer
)gtk_notebook_page_changing_callback
, this);
262 g_signal_handlers_block_by_func(m_widget
,
263 (gpointer
)gtk_notebook_page_changed_callback
, this);
266 gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget
), page
);
268 if ( !(flags
& SetSelection_SendEvent
) )
270 g_signal_handlers_unblock_by_func(m_widget
,
271 (gpointer
)gtk_notebook_page_changing_callback
, this);
273 g_signal_handlers_unblock_by_func(m_widget
,
274 (gpointer
)gtk_notebook_page_changed_callback
, this);
277 wxNotebookPage
*client
= GetPage(page
);
284 bool wxNotebook::SetPageText( size_t page
, const wxString
&text
)
286 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid notebook") );
288 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
290 wxCHECK_MSG( nb_page
, false, wxT("SetPageText: invalid page index") );
292 nb_page
->m_text
= text
;
294 gtk_label_set_text( nb_page
->m_label
, wxGTK_CONV( nb_page
->m_text
) );
299 bool wxNotebook::SetPageImage( size_t page
, int image
)
301 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
303 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
305 if (!nb_page
) return false;
307 /* Optimization posibility: return immediately if image unchanged.
308 * Not enabled because it may break existing (stupid) code that
309 * manipulates the imagelist to cycle images */
311 /* if (image == nb_page->m_image) return true; */
313 /* For different cases:
314 1) no image -> no image
319 if (image
== -1 && nb_page
->m_image
== -1)
320 return true; /* Case 1): Nothing to do. */
322 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
324 if (nb_page
->m_image
!= -1)
326 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
328 GList
*child
= gtk_container_get_children(GTK_CONTAINER(nb_page
->m_box
));
331 if (GTK_IS_IMAGE(child
->data
))
333 pixmapwid
= GTK_WIDGET(child
->data
);
339 /* We should have the pixmap widget now */
340 wxASSERT(pixmapwid
!= NULL
);
344 /* If there's no new widget, just remove the old from the box */
345 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
346 nb_page
->m_image
= -1;
348 return true; /* Case 2) */
352 /* Only cases 3) and 4) left */
353 wxASSERT( m_imageList
!= NULL
); /* Just in case */
355 /* Construct the new pixmap */
356 const wxBitmap
*bmp
= m_imageList
->GetBitmapPtr(image
);
358 if (pixmapwid
== NULL
)
360 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
361 pixmapwid
= gtk_image_new_from_pixbuf(bmp
->GetPixbuf());
363 /* CHECKME: Are these pack flags okay? */
364 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
365 gtk_widget_show(pixmapwid
);
369 /* Case 4) Simply replace the pixmap */
370 gtk_image_set_from_pixbuf((GtkImage
*)pixmapwid
, bmp
->GetPixbuf());
373 nb_page
->m_image
= image
;
378 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
380 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
383 void wxNotebook::SetPadding( const wxSize
&padding
)
385 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
387 m_padding
= padding
.GetWidth();
390 for (i
=0; i
<int(GetPageCount()); i
++)
392 wxGtkNotebookPage
* nb_page
= GetNotebookPage(i
);
393 wxASSERT(nb_page
!= NULL
);
395 if (nb_page
->m_image
!= -1)
397 // gtk_box_set_child_packing sets padding on BOTH sides
398 // icon provides left padding, label provides center and right
399 int image
= nb_page
->m_image
;
401 SetPageImage(i
,image
);
403 wxASSERT(nb_page
->m_label
);
404 gtk_box_set_child_packing(GTK_BOX(nb_page
->m_box
),
405 GTK_WIDGET(nb_page
->m_label
),
406 FALSE
, FALSE
, m_padding
, GTK_PACK_END
);
410 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
412 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
415 bool wxNotebook::DeleteAllPages()
417 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid notebook") );
419 while (m_pagesData
.GetCount() > 0)
420 DeletePage( m_pagesData
.GetCount()-1 );
422 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
424 InvalidateBestSize();
425 return wxNotebookBase::DeleteAllPages();
428 wxNotebookPage
*wxNotebook::DoRemovePage( size_t page
)
430 // We cannot remove the page yet, as GTK sends the "switch_page"
431 // signal before it has removed the notebook-page from its
432 // corresponding list. Thus, if we were to remove the page from
433 // m_pages at this point, the two lists of pages would be out
434 // of sync during the PAGE_CHANGING/PAGE_CHANGED events.
435 wxNotebookPage
*client
= GetPage(page
);
439 gtk_widget_ref( client
->m_widget
);
440 gtk_widget_unrealize( client
->m_widget
);
442 // we don't need to unparent the client->m_widget; GTK+ will do
443 // that for us (and will throw a warning if we do it!)
444 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
446 // It's safe to remove the page now.
447 wxASSERT_MSG(GetPage(page
) == client
, wxT("pages changed during delete"));
448 wxNotebookBase::DoRemovePage(page
);
450 wxGtkNotebookPage
* p
= GetNotebookPage(page
);
451 m_pagesData
.DeleteObject(p
);
457 bool wxNotebook::InsertPage( size_t position
,
459 const wxString
& text
,
463 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid notebook") );
465 wxCHECK_MSG( win
->GetParent() == this, false,
466 wxT("Can't add a page whose parent is not the notebook!") );
468 wxCHECK_MSG( position
<= GetPageCount(), false,
469 _T("invalid page index in wxNotebookPage::InsertPage()") );
471 // Hack Alert! (Part II): See above in wxInsertChildInNotebook callback
472 // why this has to be done. NOTE: using gtk_widget_unparent here does not
473 // work as it seems to undo too much and will cause errors in the
474 // gtk_notebook_insert_page below, so instead just clear the parent by
476 win
->m_widget
->parent
= NULL
;
479 win
->SetThemeEnabled(true);
481 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
483 wxGtkNotebookPage
*nb_page
= new wxGtkNotebookPage();
485 if ( position
== GetPageCount() )
486 m_pagesData
.Append( nb_page
);
488 m_pagesData
.Insert( position
, nb_page
);
490 m_pages
.Insert(win
, position
);
492 // set the label image and text
493 // this must be done before adding the page, as GetPageText
494 // and GetPageImage will otherwise return wrong values in
495 // the page-changed event that results from inserting the
497 nb_page
->m_image
= imageId
;
498 nb_page
->m_text
= wxStripMenuCodes(text
);
500 nb_page
->m_box
= gtk_hbox_new( FALSE
, 1 );
501 gtk_container_set_border_width((GtkContainer
*)nb_page
->m_box
, 2);
503 gtk_notebook_insert_page(notebook
, win
->m_widget
, nb_page
->m_box
, position
);
507 wxASSERT( m_imageList
!= NULL
);
509 const wxBitmap
*bmp
= m_imageList
->GetBitmapPtr(imageId
);
510 GtkWidget
* pixmapwid
= gtk_image_new_from_pixbuf(bmp
->GetPixbuf());
511 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
512 gtk_widget_show(pixmapwid
);
515 /* set the label text */
516 nb_page
->m_label
= GTK_LABEL( gtk_label_new(wxGTK_CONV(nb_page
->m_text
)) );
517 gtk_box_pack_end( GTK_BOX(nb_page
->m_box
), GTK_WIDGET(nb_page
->m_label
), FALSE
, FALSE
, m_padding
);
519 /* apply current style */
520 GtkRcStyle
*style
= CreateWidgetStyle();
523 gtk_widget_modify_style(GTK_WIDGET(nb_page
->m_label
), style
);
524 gtk_rc_style_unref(style
);
528 gtk_widget_show( GTK_WIDGET(nb_page
->m_label
) );
530 if (select
&& (m_pagesData
.GetCount() > 1))
532 SetSelection( position
);
535 InvalidateBestSize();
539 // helper for HitTest(): check if the point lies inside the given widget which
540 // is the child of the notebook whose position and border size are passed as
543 IsPointInsideWidget(const wxPoint
& pt
, GtkWidget
*w
,
544 gint x
, gint y
, gint border
= 0)
547 (pt
.x
>= w
->allocation
.x
- x
- border
) &&
548 (pt
.x
<= w
->allocation
.x
- x
+ border
+ w
->allocation
.width
) &&
549 (pt
.y
>= w
->allocation
.y
- y
- border
) &&
550 (pt
.y
<= w
->allocation
.y
- y
+ border
+ w
->allocation
.height
);
553 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
555 const gint x
= m_widget
->allocation
.x
;
556 const gint y
= m_widget
->allocation
.y
;
558 const size_t count
= GetPageCount();
561 GtkNotebook
* notebook
= GTK_NOTEBOOK(m_widget
);
562 if (gtk_notebook_get_scrollable(notebook
))
563 i
= g_list_position( notebook
->children
, notebook
->first_tab
);
565 for ( ; i
< count
; i
++ )
567 wxGtkNotebookPage
* nb_page
= GetNotebookPage(i
);
568 GtkWidget
*box
= nb_page
->m_box
;
570 const gint border
= gtk_container_get_border_width(GTK_CONTAINER(box
));
572 if ( IsPointInsideWidget(pt
, box
, x
, y
, border
) )
574 // ok, we're inside this tab -- now find out where, if needed
577 GtkWidget
*pixmap
= NULL
;
579 GList
*children
= gtk_container_get_children(GTK_CONTAINER(box
));
580 for ( GList
*child
= children
; child
; child
= child
->next
)
582 if (GTK_IS_IMAGE(child
->data
))
584 pixmap
= GTK_WIDGET(child
->data
);
590 g_list_free(children
);
592 if ( pixmap
&& IsPointInsideWidget(pt
, pixmap
, x
, y
) )
594 *flags
= wxBK_HITTEST_ONICON
;
596 else if ( IsPointInsideWidget(pt
, GTK_WIDGET(nb_page
->m_label
), x
, y
) )
598 *flags
= wxBK_HITTEST_ONLABEL
;
602 *flags
= wxBK_HITTEST_ONITEM
;
612 *flags
= wxBK_HITTEST_NOWHERE
;
613 wxWindowBase
* page
= GetCurrentPage();
616 // rect origin is in notebook's parent coordinates
617 wxRect rect
= page
->GetRect();
619 // adjust it to the notebook's coordinates
620 wxPoint pos
= GetPosition();
623 if ( rect
.Contains( pt
) )
624 *flags
|= wxBK_HITTEST_ONPAGE
;
631 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
633 if (event
.IsWindowChange())
634 AdvanceSelection( event
.GetDirection() );
639 #if wxUSE_CONSTRAINTS
641 // override these 2 functions to do nothing: everything is done in OnSize
642 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
644 // don't set the sizes of the pages - their correct size is not yet known
645 wxControl::SetConstraintSizes(false);
648 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
655 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle
*style
)
657 gtk_widget_modify_style(m_widget
, style
);
658 size_t cnt
= m_pagesData
.GetCount();
659 for (size_t i
= 0; i
< cnt
; i
++)
660 gtk_widget_modify_style(GTK_WIDGET(GetNotebookPage(i
)->m_label
), style
);
663 GdkWindow
*wxNotebook::GTKGetWindow(wxArrayGdkWindows
& windows
) const
665 windows
.push_back(m_widget
->window
);
666 windows
.push_back(GTK_NOTEBOOK(m_widget
)->event_window
);
673 wxNotebook::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
675 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new
);
678 //-----------------------------------------------------------------------------
680 //-----------------------------------------------------------------------------
682 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)