]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/notebook.cpp
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"
29 #include "wx/gtk/private.h"
30 #include "wx/gtk/private/gtk2-compat.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 // VZ: this is rather ugly as we keep the pages themselves in an array (it
37 // allows us to have quite a few functions implemented in the base class)
38 // but the page data is kept in a separate list, so we must maintain them
39 // in sync manually... of course, the list had been there before the base
40 // class which explains it but it still would be nice to do something
43 class wxGtkNotebookPage
: public wxObject
53 #include "wx/listimpl.cpp"
54 WX_DEFINE_LIST(wxGtkNotebookPagesList
)
57 static void event_after(GtkNotebook
*, GdkEvent
*, wxNotebook
*);
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
66 switch_page_after(GtkNotebook
* widget
, GtkNotebookPage
*, guint
, wxNotebook
* win
)
68 g_signal_handlers_block_by_func(widget
, (void*)switch_page_after
, win
);
70 win
->GTKOnPageChanged();
76 switch_page(GtkNotebook
* widget
, GtkNotebookPage
*, int page
, wxNotebook
* win
)
78 win
->m_oldSelection
= gtk_notebook_get_current_page(widget
);
80 if (win
->SendPageChangingEvent(page
))
81 // allow change, unblock handler for changed event
82 g_signal_handlers_unblock_by_func(widget
, (void*)switch_page_after
, win
);
84 // change vetoed, unblock handler to set selection back
85 g_signal_handlers_unblock_by_func(widget
, (void*)event_after
, win
);
89 //-----------------------------------------------------------------------------
90 // "event_after" from m_widget
91 //-----------------------------------------------------------------------------
94 static void event_after(GtkNotebook
* widget
, GdkEvent
*, wxNotebook
* win
)
96 g_signal_handlers_block_by_func(widget
, (void*)event_after
, win
);
97 g_signal_handlers_block_by_func(widget
, (void*)switch_page
, win
);
99 // restore previous selection
100 gtk_notebook_set_current_page(widget
, win
->m_oldSelection
);
102 g_signal_handlers_unblock_by_func(widget
, (void*)switch_page
, win
);
106 //-----------------------------------------------------------------------------
107 // InsertChild callback for wxNotebook
108 //-----------------------------------------------------------------------------
110 void wxNotebook::AddChildGTK(wxWindowGTK
* child
)
112 // Hack Alert! (Part I): This sets the notebook as the parent of the child
113 // widget, and takes care of some details such as updating the state and
114 // style of the child to reflect its new location. We do this early
115 // because without it GetBestSize (which is used to set the initial size
116 // of controls if an explicit size is not given) will often report
117 // incorrect sizes since the widget's style context is not fully known.
118 // See bug #901694 for details
119 // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
120 gtk_widget_set_parent(child
->m_widget
, m_widget
);
122 // NOTE: This should be considered a temporary workaround until we can
123 // work out the details and implement delaying the setting of the initial
124 // size of widgets until the size is really needed.
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
132 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
135 void wxNotebook::Init()
139 m_themeEnabled
= true;
142 wxNotebook::wxNotebook()
147 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
148 const wxPoint
& pos
, const wxSize
& size
,
149 long style
, const wxString
& name
)
152 Create( parent
, id
, pos
, size
, style
, name
);
155 wxNotebook::~wxNotebook()
160 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
161 const wxPoint
& pos
, const wxSize
& size
,
162 long style
, const wxString
& name
)
164 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
167 if (!PreCreation( parent
, pos
, size
) ||
168 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
170 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
175 m_widget
= gtk_notebook_new();
176 g_object_ref(m_widget
);
178 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
180 g_signal_connect (m_widget
, "switch_page",
181 G_CALLBACK(switch_page
), this);
183 g_signal_connect_after (m_widget
, "switch_page",
184 G_CALLBACK(switch_page_after
), this);
185 g_signal_handlers_block_by_func(m_widget
, (void*)switch_page_after
, this);
187 g_signal_connect(m_widget
, "event_after", G_CALLBACK(event_after
), this);
188 g_signal_handlers_block_by_func(m_widget
, (void*)event_after
, this);
190 m_parent
->DoAddChild( this );
192 if (m_windowStyle
& wxBK_RIGHT
)
193 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
194 if (m_windowStyle
& wxBK_LEFT
)
195 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
196 if (m_windowStyle
& wxBK_BOTTOM
)
197 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
204 int wxNotebook::GetSelection() const
206 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid notebook") );
208 return gtk_notebook_get_current_page( GTK_NOTEBOOK(m_widget
) );
211 wxString
wxNotebook::GetPageText( size_t page
) const
213 wxCHECK_MSG(page
< GetPageCount(), wxEmptyString
, "invalid notebook index");
215 GtkLabel
* label
= GTK_LABEL(GetNotebookPage(page
)->m_label
);
216 return wxGTK_CONV_BACK(gtk_label_get_text(label
));
219 int wxNotebook::GetPageImage( size_t page
) const
221 wxCHECK_MSG(page
< GetPageCount(), wxNOT_FOUND
, "invalid notebook index");
223 return GetNotebookPage(page
)->m_imageIndex
;
226 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
228 return m_pagesData
.Item(page
)->GetData();
231 int wxNotebook::DoSetSelection( size_t page
, int flags
)
233 wxCHECK_MSG(page
< GetPageCount(), wxNOT_FOUND
, "invalid notebook index");
235 int selOld
= GetSelection();
237 if ( !(flags
& SetSelection_SendEvent
) )
239 g_signal_handlers_block_by_func(m_widget
, (void*)switch_page
, this);
242 gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget
), page
);
244 if ( !(flags
& SetSelection_SendEvent
) )
246 g_signal_handlers_unblock_by_func(m_widget
, (void*)switch_page
, this);
251 wxNotebookPage
*client
= GetPage(page
);
258 void wxNotebook::GTKOnPageChanged()
260 m_selection
= gtk_notebook_get_current_page(GTK_NOTEBOOK(m_widget
));
262 SendPageChangedEvent(m_oldSelection
);
265 bool wxNotebook::SetPageText( size_t page
, const wxString
&text
)
267 wxCHECK_MSG(page
< GetPageCount(), false, "invalid notebook index");
269 GtkLabel
* label
= GTK_LABEL(GetNotebookPage(page
)->m_label
);
270 gtk_label_set_text(label
, wxGTK_CONV(text
));
275 bool wxNotebook::SetPageImage( size_t page
, int image
)
277 wxCHECK_MSG(page
< GetPageCount(), false, "invalid notebook index");
279 wxGtkNotebookPage
* pageData
= GetNotebookPage(page
);
282 wxCHECK_MSG(HasImageList(), false, "invalid notebook imagelist");
283 const wxBitmap
* bitmap
= GetImageList()->GetBitmapPtr(image
);
286 if (pageData
->m_image
)
288 gtk_image_set_from_pixbuf(
289 GTK_IMAGE(pageData
->m_image
), bitmap
->GetPixbuf());
293 pageData
->m_image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
294 gtk_widget_show(pageData
->m_image
);
295 gtk_box_pack_start(GTK_BOX(pageData
->m_box
),
296 pageData
->m_image
, false, false, m_padding
);
299 else if (pageData
->m_image
)
301 gtk_widget_destroy(pageData
->m_image
);
302 pageData
->m_image
= NULL
;
304 pageData
->m_imageIndex
= image
;
309 wxSize
wxNotebook::CalcSizeFromPage(const wxSize
& sizePage
) const
311 // Compute the max size of the tab labels.
313 const size_t pageCount
= GetPageCount();
314 for ( size_t n
= 0; n
< pageCount
; n
++ )
317 gtk_widget_size_request(GetNotebookPage(n
)->m_box
, &req
);
318 sizeTabMax
.IncTo(wxSize(req
.width
, req
.height
));
321 // Unfortunately this doesn't account for the real tab size and I don't
322 // know how to find it, e.g. where do the margins below come from.
323 const int PAGE_MARGIN
= 3;
324 const int TAB_MARGIN
= 4;
326 sizeTabMax
.IncBy(3*TAB_MARGIN
);
328 wxSize
sizeFull(sizePage
);
330 sizeFull
.y
+= sizeTabMax
.y
;
332 sizeFull
.x
+= sizeTabMax
.x
;
334 sizeFull
.IncBy(2*PAGE_MARGIN
);
339 void wxNotebook::SetPadding( const wxSize
&padding
)
341 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
343 m_padding
= padding
.GetWidth();
345 for (size_t i
= GetPageCount(); i
--;)
347 wxGtkNotebookPage
* pageData
= GetNotebookPage(i
);
348 if (pageData
->m_image
)
350 gtk_box_set_child_packing(GTK_BOX(pageData
->m_box
),
351 pageData
->m_image
, false, false, m_padding
, GTK_PACK_START
);
353 gtk_box_set_child_packing(GTK_BOX(pageData
->m_box
),
354 pageData
->m_label
, false, false, m_padding
, GTK_PACK_END
);
358 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
360 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
363 bool wxNotebook::DeleteAllPages()
365 for (size_t i
= GetPageCount(); i
--;)
368 return wxNotebookBase::DeleteAllPages();
371 wxNotebookPage
*wxNotebook::DoRemovePage( size_t page
)
373 // We cannot remove the page yet, as GTK sends the "switch_page"
374 // signal before it has removed the notebook-page from its
375 // corresponding list. Thus, if we were to remove the page from
376 // m_pages at this point, the two lists of pages would be out
377 // of sync during the PAGE_CHANGING/PAGE_CHANGED events.
378 wxNotebookPage
*client
= GetPage(page
);
382 // we don't need to unparent the client->m_widget; GTK+ will do
383 // that for us (and will throw a warning if we do it!)
384 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
386 // It's safe to remove the page now.
387 wxASSERT_MSG(GetPage(page
) == client
, wxT("pages changed during delete"));
388 wxNotebookBase::DoRemovePage(page
);
390 wxGtkNotebookPage
* p
= GetNotebookPage(page
);
391 m_pagesData
.DeleteObject(p
);
397 bool wxNotebook::InsertPage( size_t position
,
399 const wxString
& text
,
403 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid notebook") );
405 wxCHECK_MSG( win
->GetParent() == this, false,
406 wxT("Can't add a page whose parent is not the notebook!") );
408 wxCHECK_MSG( position
<= GetPageCount(), false,
409 wxT("invalid page index in wxNotebookPage::InsertPage()") );
411 // Hack Alert! (Part II): See above in wxNotebook::AddChildGTK
412 // why this has to be done.
413 gtk_widget_unparent(win
->m_widget
);
416 win
->SetThemeEnabled(true);
418 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
420 wxGtkNotebookPage
* pageData
= new wxGtkNotebookPage
;
422 m_pages
.Insert(win
, position
);
423 m_pagesData
.Insert(position
, pageData
);
425 // set the label image and text
426 // this must be done before adding the page, as GetPageText
427 // and GetPageImage will otherwise return wrong values in
428 // the page-changed event that results from inserting the
430 pageData
->m_imageIndex
= imageId
;
432 pageData
->m_box
= gtk_hbox_new(false, 1);
433 gtk_container_set_border_width(GTK_CONTAINER(pageData
->m_box
), 2);
435 pageData
->m_image
= NULL
;
440 const wxBitmap
* bitmap
= GetImageList()->GetBitmapPtr(imageId
);
441 pageData
->m_image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
442 gtk_box_pack_start(GTK_BOX(pageData
->m_box
),
443 pageData
->m_image
, false, false, m_padding
);
447 wxFAIL_MSG("invalid notebook imagelist");
451 /* set the label text */
452 pageData
->m_label
= gtk_label_new(wxGTK_CONV(wxStripMenuCodes(text
)));
453 gtk_box_pack_end(GTK_BOX(pageData
->m_box
),
454 pageData
->m_label
, false, false, m_padding
);
456 gtk_widget_show_all(pageData
->m_box
);
457 gtk_notebook_insert_page(notebook
, win
->m_widget
, pageData
->m_box
, position
);
459 /* apply current style */
461 GTKApplyStyle(pageData
->m_label
, NULL
);
463 GtkRcStyle
*style
= GTKCreateWidgetStyle();
466 gtk_widget_modify_style(pageData
->m_label
, style
);
467 g_object_unref(style
);
471 if (select
&& GetPageCount() > 1)
473 SetSelection( position
);
476 InvalidateBestSize();
480 // helper for HitTest(): check if the point lies inside the given widget which
481 // is the child of the notebook whose position and border size are passed as
484 IsPointInsideWidget(const wxPoint
& pt
, GtkWidget
*w
,
485 gint x
, gint y
, gint border
= 0)
488 gtk_widget_get_allocation(w
, &a
);
490 (pt
.x
>= a
.x
- x
- border
) &&
491 (pt
.x
<= a
.x
- x
+ border
+ a
.width
) &&
492 (pt
.y
>= a
.y
- y
- border
) &&
493 (pt
.y
<= a
.y
- y
+ border
+ a
.height
);
496 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
499 gtk_widget_get_allocation(m_widget
, &a
);
503 const size_t count
= GetPageCount();
506 #if !GTK_CHECK_VERSION(3,0,0) && !defined(GSEAL_ENABLE)
507 GtkNotebook
* notebook
= GTK_NOTEBOOK(m_widget
);
508 if (gtk_notebook_get_scrollable(notebook
))
509 i
= g_list_position( notebook
->children
, notebook
->first_tab
);
512 for ( ; i
< count
; i
++ )
514 wxGtkNotebookPage
* pageData
= GetNotebookPage(i
);
515 GtkWidget
* box
= pageData
->m_box
;
517 const gint border
= gtk_container_get_border_width(GTK_CONTAINER(box
));
519 if ( IsPointInsideWidget(pt
, box
, x
, y
, border
) )
521 // ok, we're inside this tab -- now find out where, if needed
524 if (pageData
->m_image
&& IsPointInsideWidget(pt
, pageData
->m_image
, x
, y
))
526 *flags
= wxBK_HITTEST_ONICON
;
528 else if (IsPointInsideWidget(pt
, pageData
->m_label
, x
, y
))
530 *flags
= wxBK_HITTEST_ONLABEL
;
534 *flags
= wxBK_HITTEST_ONITEM
;
544 *flags
= wxBK_HITTEST_NOWHERE
;
545 wxWindowBase
* page
= GetCurrentPage();
548 // rect origin is in notebook's parent coordinates
549 wxRect rect
= page
->GetRect();
551 // adjust it to the notebook's coordinates
552 wxPoint pos
= GetPosition();
555 if ( rect
.Contains( pt
) )
556 *flags
|= wxBK_HITTEST_ONPAGE
;
563 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
565 if (event
.IsWindowChange())
566 AdvanceSelection( event
.GetDirection() );
571 #if wxUSE_CONSTRAINTS
573 // override these 2 functions to do nothing: everything is done in OnSize
574 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
576 // don't set the sizes of the pages - their correct size is not yet known
577 wxControl::SetConstraintSizes(false);
580 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
587 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle
*style
)
589 GTKApplyStyle(m_widget
, style
);
590 for (size_t i
= GetPageCount(); i
--;)
591 GTKApplyStyle(GetNotebookPage(i
)->m_label
, style
);
594 GdkWindow
*wxNotebook::GTKGetWindow(wxArrayGdkWindows
& windows
) const
596 windows
.push_back(gtk_widget_get_window(m_widget
));
598 // no access to internal GdkWindows
600 windows
.push_back(GTK_NOTEBOOK(m_widget
)->event_window
);
608 wxNotebook::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
610 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new
);