1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/notebook.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Copyright:   (c) 1998 Robert Roebling, Vadim Zeitlin
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 #if wxUSE_NOTEBOOK
13 
14 #include "wx/notebook.h"
15 
16 #ifndef WX_PRECOMP
17     #include "wx/intl.h"
18     #include "wx/log.h"
19     #include "wx/utils.h"
20     #include "wx/msgdlg.h"
21     #include "wx/bitmap.h"
22 #endif
23 
24 #include "wx/imaglist.h"
25 #include "wx/fontutil.h"
26 
27 #include <gtk/gtk.h>
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/private/gtk2-compat.h"
30 
31 //-----------------------------------------------------------------------------
32 // wxGtkNotebookPage
33 //-----------------------------------------------------------------------------
34 
35 // VZ: this is rather ugly as we keep the pages themselves in an array (it
36 //     allows us to have quite a few functions implemented in the base class)
37 //     but the page data is kept in a separate list, so we must maintain them
38 //     in sync manually... of course, the list had been there before the base
39 //     class which explains it but it still would be nice to do something
40 //     about this one day
41 
42 class wxGtkNotebookPage: public wxObject
43 {
44 public:
45     GtkWidget* m_box;
46     GtkWidget* m_label;
47     GtkWidget* m_image;
48     int m_imageIndex;
49 };
50 
51 
52 #include "wx/listimpl.cpp"
WX_DEFINE_LIST(wxGtkNotebookPagesList)53 WX_DEFINE_LIST(wxGtkNotebookPagesList)
54 
55 extern "C" {
56 static void event_after(GtkNotebook*, GdkEvent*, wxNotebook*);
57 }
58 
59 //-----------------------------------------------------------------------------
60 // "switch_page"
61 //-----------------------------------------------------------------------------
62 
63 extern "C" {
64 static void
switch_page_after(GtkNotebook * widget,GtkNotebookPage *,guint,wxNotebook * win)65 switch_page_after(GtkNotebook* widget, GtkNotebookPage*, guint, wxNotebook* win)
66 {
67     g_signal_handlers_block_by_func(widget, (void*)switch_page_after, win);
68 
69     win->GTKOnPageChanged();
70 }
71 }
72 
73 extern "C" {
74 static void
switch_page(GtkNotebook * widget,GtkNotebookPage *,int page,wxNotebook * win)75 switch_page(GtkNotebook* widget, GtkNotebookPage*, int page, wxNotebook* win)
76 {
77     win->m_oldSelection = gtk_notebook_get_current_page(widget);
78 
79     if (win->SendPageChangingEvent(page))
80         // allow change, unblock handler for changed event
81         g_signal_handlers_unblock_by_func(widget, (void*)switch_page_after, win);
82     else
83         // change vetoed, unblock handler to set selection back
84         g_signal_handlers_unblock_by_func(widget, (void*)event_after, win);
85 }
86 }
87 
88 //-----------------------------------------------------------------------------
89 // "event_after" from m_widget
90 //-----------------------------------------------------------------------------
91 
92 extern "C" {
event_after(GtkNotebook * widget,GdkEvent *,wxNotebook * win)93 static void event_after(GtkNotebook* widget, GdkEvent*, wxNotebook* win)
94 {
95     g_signal_handlers_block_by_func(widget, (void*)event_after, win);
96     g_signal_handlers_block_by_func(widget, (void*)switch_page, win);
97 
98     // restore previous selection
99     gtk_notebook_set_current_page(widget, win->m_oldSelection);
100 
101     g_signal_handlers_unblock_by_func(widget, (void*)switch_page, win);
102 }
103 }
104 
105 //-----------------------------------------------------------------------------
106 // InsertChild callback for wxNotebook
107 //-----------------------------------------------------------------------------
108 
AddChildGTK(wxWindowGTK * child)109 void wxNotebook::AddChildGTK(wxWindowGTK* child)
110 {
111     // Hack Alert! (Part I): This sets the notebook as the parent of the child
112     // widget, and takes care of some details such as updating the state and
113     // style of the child to reflect its new location.  We do this early
114     // because without it GetBestSize (which is used to set the initial size
115     // of controls if an explicit size is not given) will often report
116     // incorrect sizes since the widget's style context is not fully known.
117     // See bug #901694 for details
118     // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
119     gtk_widget_set_parent(child->m_widget, m_widget);
120 
121     // NOTE: This should be considered a temporary workaround until we can
122     // work out the details and implement delaying the setting of the initial
123     // size of widgets until the size is really needed.
124 }
125 
126 //-----------------------------------------------------------------------------
127 // wxNotebook
128 //-----------------------------------------------------------------------------
129 
BEGIN_EVENT_TABLE(wxNotebook,wxBookCtrlBase)130 BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
131     EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
132 END_EVENT_TABLE()
133 
134 void wxNotebook::Init()
135 {
136     m_padding = 0;
137     m_oldSelection = -1;
138     m_themeEnabled = true;
139 }
140 
wxNotebook()141 wxNotebook::wxNotebook()
142 {
143     Init();
144 }
145 
wxNotebook(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxString & name)146 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
147       const wxPoint& pos, const wxSize& size,
148       long style, const wxString& name )
149 {
150     Init();
151     Create( parent, id, pos, size, style, name );
152 }
153 
~wxNotebook()154 wxNotebook::~wxNotebook()
155 {
156     // Ensure that we don't generate page changing events during the
157     // destruction, this is unexpected and may reference the already (half)
158     // destroyed parent window, for example. So make sure our switch_page
159     // callback is not called from inside DeleteAllPages() by disconnecting all
160     // the callbacks associated with this widget.
161     GTKDisconnect(m_widget);
162 
163     DeleteAllPages();
164 }
165 
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxString & name)166 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
167                         const wxPoint& pos, const wxSize& size,
168                         long style, const wxString& name )
169 {
170     if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
171         style |= wxBK_TOP;
172 
173     if (!PreCreation( parent, pos, size ) ||
174         !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
175     {
176         wxFAIL_MSG( wxT("wxNoteBook creation failed") );
177         return false;
178     }
179 
180 
181     m_widget = gtk_notebook_new();
182     g_object_ref(m_widget);
183 
184     gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
185 
186     g_signal_connect (m_widget, "switch_page",
187                       G_CALLBACK(switch_page), this);
188 
189     g_signal_connect_after (m_widget, "switch_page",
190                       G_CALLBACK(switch_page_after), this);
191     g_signal_handlers_block_by_func(m_widget, (void*)switch_page_after, this);
192 
193     g_signal_connect(m_widget, "event_after", G_CALLBACK(event_after), this);
194     g_signal_handlers_block_by_func(m_widget, (void*)event_after, this);
195 
196     m_parent->DoAddChild( this );
197 
198     if (m_windowStyle & wxBK_RIGHT)
199         gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
200     if (m_windowStyle & wxBK_LEFT)
201         gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
202     if (m_windowStyle & wxBK_BOTTOM)
203         gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
204 
205     PostCreation(size);
206 
207     return true;
208 }
209 
GetSelection() const210 int wxNotebook::GetSelection() const
211 {
212     wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid notebook") );
213 
214     return gtk_notebook_get_current_page( GTK_NOTEBOOK(m_widget) );
215 }
216 
GetPageText(size_t page) const217 wxString wxNotebook::GetPageText( size_t page ) const
218 {
219     wxCHECK_MSG(page < GetPageCount(), wxEmptyString, "invalid notebook index");
220 
221     GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
222     return wxGTK_CONV_BACK(gtk_label_get_text(label));
223 }
224 
GetPageImage(size_t page) const225 int wxNotebook::GetPageImage( size_t page ) const
226 {
227     wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
228 
229     return GetNotebookPage(page)->m_imageIndex;
230 }
231 
GetNotebookPage(int page) const232 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
233 {
234     return m_pagesData.Item(page)->GetData();
235 }
236 
DoSetSelection(size_t page,int flags)237 int wxNotebook::DoSetSelection( size_t page, int flags )
238 {
239     wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
240 
241     int selOld = GetSelection();
242 
243     if ( !(flags & SetSelection_SendEvent) )
244     {
245         g_signal_handlers_block_by_func(m_widget, (void*)switch_page, this);
246     }
247 
248     gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget), page );
249 
250     if ( !(flags & SetSelection_SendEvent) )
251     {
252         g_signal_handlers_unblock_by_func(m_widget, (void*)switch_page, this);
253     }
254 
255     m_selection = page;
256 
257     wxNotebookPage *client = GetPage(page);
258     if ( client )
259         client->SetFocus();
260 
261     return selOld;
262 }
263 
GTKOnPageChanged()264 void wxNotebook::GTKOnPageChanged()
265 {
266     m_selection = gtk_notebook_get_current_page(GTK_NOTEBOOK(m_widget));
267 
268     SendPageChangedEvent(m_oldSelection);
269 }
270 
SetPageText(size_t page,const wxString & text)271 bool wxNotebook::SetPageText( size_t page, const wxString &text )
272 {
273     wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
274 
275     GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
276     gtk_label_set_text(label, wxGTK_CONV(text));
277 
278     return true;
279 }
280 
SetPageImage(size_t page,int image)281 bool wxNotebook::SetPageImage( size_t page, int image )
282 {
283     wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
284 
285     wxGtkNotebookPage* pageData = GetNotebookPage(page);
286     if (image >= 0)
287     {
288         wxCHECK_MSG(HasImageList(), false, "invalid notebook imagelist");
289         const wxBitmap* bitmap = GetImageList()->GetBitmapPtr(image);
290         if (bitmap == NULL)
291             return false;
292         if (pageData->m_image)
293         {
294             gtk_image_set_from_pixbuf(
295                 GTK_IMAGE(pageData->m_image), bitmap->GetPixbuf());
296         }
297         else
298         {
299             pageData->m_image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
300             gtk_widget_show(pageData->m_image);
301             gtk_box_pack_start(GTK_BOX(pageData->m_box),
302                 pageData->m_image, false, false, m_padding);
303         }
304     }
305     else if (pageData->m_image)
306     {
307         gtk_widget_destroy(pageData->m_image);
308         pageData->m_image = NULL;
309     }
310     pageData->m_imageIndex = image;
311 
312     return true;
313 }
314 
CalcSizeFromPage(const wxSize & sizePage) const315 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
316 {
317     // Compute the max size of the tab labels.
318     wxSize sizeTabMax;
319     const size_t pageCount = GetPageCount();
320     for ( size_t n = 0; n < pageCount; n++ )
321     {
322         GtkRequisition req;
323         gtk_widget_get_preferred_size(GetNotebookPage(n)->m_box, NULL, &req);
324         sizeTabMax.IncTo(wxSize(req.width, req.height));
325     }
326 
327     // Unfortunately this doesn't account for the real tab size and I don't
328     // know how to find it, e.g. where do the margins below come from.
329     const int PAGE_MARGIN = 3;
330     const int TAB_MARGIN = 4;
331 
332     sizeTabMax.IncBy(3*TAB_MARGIN);
333 
334     wxSize sizeFull(sizePage);
335     if ( IsVertical() )
336         sizeFull.y += sizeTabMax.y;
337     else
338         sizeFull.x += sizeTabMax.x;
339 
340     sizeFull.IncBy(2*PAGE_MARGIN);
341 
342     return sizeFull;
343 }
344 
SetPadding(const wxSize & padding)345 void wxNotebook::SetPadding( const wxSize &padding )
346 {
347     wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
348 
349     m_padding = padding.GetWidth();
350 
351     for (size_t i = GetPageCount(); i--;)
352     {
353         wxGtkNotebookPage* pageData = GetNotebookPage(i);
354         if (pageData->m_image)
355         {
356             gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
357                 pageData->m_image, false, false, m_padding, GTK_PACK_START);
358         }
359         gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
360             pageData->m_label, false, false, m_padding, GTK_PACK_END);
361     }
362 }
363 
SetTabSize(const wxSize & WXUNUSED (sz))364 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
365 {
366     wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
367 }
368 
DeleteAllPages()369 bool wxNotebook::DeleteAllPages()
370 {
371     for (size_t i = GetPageCount(); i--;)
372         DeletePage(i);
373 
374     return wxNotebookBase::DeleteAllPages();
375 }
376 
DoRemovePage(size_t page)377 wxNotebookPage *wxNotebook::DoRemovePage( size_t page )
378 {
379     // We cannot remove the page yet, as GTK sends the "switch_page"
380     // signal before it has removed the notebook-page from its
381     // corresponding list. Thus, if we were to remove the page from
382     // m_pages at this point, the two lists of pages would be out
383     // of sync during the PAGE_CHANGING/PAGE_CHANGED events.
384     wxNotebookPage *client = GetPage(page);
385     if ( !client )
386         return NULL;
387 
388     // we don't need to unparent the client->m_widget; GTK+ will do
389     // that for us (and will throw a warning if we do it!)
390     gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
391 
392     // It's safe to remove the page now.
393     wxASSERT_MSG(GetPage(page) == client, wxT("pages changed during delete"));
394     wxNotebookBase::DoRemovePage(page);
395 
396     wxGtkNotebookPage* p = GetNotebookPage(page);
397     m_pagesData.DeleteObject(p);
398     delete p;
399 
400     return client;
401 }
402 
InsertPage(size_t position,wxNotebookPage * win,const wxString & text,bool select,int imageId)403 bool wxNotebook::InsertPage( size_t position,
404                              wxNotebookPage* win,
405                              const wxString& text,
406                              bool select,
407                              int imageId )
408 {
409     wxCHECK_MSG( m_widget != NULL, false, wxT("invalid notebook") );
410 
411     wxCHECK_MSG( win->GetParent() == this, false,
412                wxT("Can't add a page whose parent is not the notebook!") );
413 
414     wxCHECK_MSG( position <= GetPageCount(), false,
415                  wxT("invalid page index in wxNotebookPage::InsertPage()") );
416 
417     // Hack Alert! (Part II): See above in wxNotebook::AddChildGTK
418     // why this has to be done.
419     gtk_widget_unparent(win->m_widget);
420 
421     if (m_themeEnabled)
422         win->SetThemeEnabled(true);
423 
424     GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
425 
426     wxGtkNotebookPage* pageData = new wxGtkNotebookPage;
427 
428     m_pages.Insert(win, position);
429     m_pagesData.Insert(position, pageData);
430 
431     // set the label image and text
432     // this must be done before adding the page, as GetPageText
433     // and GetPageImage will otherwise return wrong values in
434     // the page-changed event that results from inserting the
435     // first page.
436     pageData->m_imageIndex = imageId;
437 
438     pageData->m_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
439 #ifndef __WXGTK3__
440     gtk_container_set_border_width(GTK_CONTAINER(pageData->m_box), 2);
441 #endif
442 
443     pageData->m_image = NULL;
444     if (imageId != -1)
445     {
446         if (HasImageList())
447         {
448             const wxBitmap* bitmap = GetImageList()->GetBitmapPtr(imageId);
449             pageData->m_image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
450             gtk_box_pack_start(GTK_BOX(pageData->m_box),
451                 pageData->m_image, false, false, m_padding);
452         }
453         else
454         {
455             wxFAIL_MSG("invalid notebook imagelist");
456         }
457     }
458 
459     /* set the label text */
460     pageData->m_label = gtk_label_new(wxGTK_CONV(wxStripMenuCodes(text)));
461     gtk_box_pack_end(GTK_BOX(pageData->m_box),
462         pageData->m_label, false, false, m_padding);
463 
464     gtk_widget_show_all(pageData->m_box);
465     gtk_notebook_insert_page(notebook, win->m_widget, pageData->m_box, position);
466 
467     /* apply current style */
468 #ifdef __WXGTK3__
469     GTKApplyStyle(pageData->m_label, NULL);
470 #else
471     GtkRcStyle *style = GTKCreateWidgetStyle();
472     if ( style )
473     {
474         gtk_widget_modify_style(pageData->m_label, style);
475         g_object_unref(style);
476     }
477 #endif
478 
479     if (select && GetPageCount() > 1)
480     {
481         SetSelection( position );
482     }
483 
484     InvalidateBestSize();
485     return true;
486 }
487 
488 // helper for HitTest(): check if the point lies inside the given widget which
489 // is the child of the notebook whose position and border size are passed as
490 // parameters
491 static bool
IsPointInsideWidget(const wxPoint & pt,GtkWidget * w,gint x,gint y,gint border=0)492 IsPointInsideWidget(const wxPoint& pt, GtkWidget *w,
493                     gint x, gint y, gint border = 0)
494 {
495     GtkAllocation a;
496     gtk_widget_get_allocation(w, &a);
497     return
498         (pt.x >= a.x - x - border) &&
499         (pt.x <= a.x - x + border + a.width) &&
500         (pt.y >= a.y - y - border) &&
501         (pt.y <= a.y - y + border + a.height);
502 }
503 
HitTest(const wxPoint & pt,long * flags) const504 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
505 {
506     GtkAllocation a;
507     gtk_widget_get_allocation(m_widget, &a);
508     const int x = a.x;
509     const int y = a.y;
510 
511     const size_t count = GetPageCount();
512     size_t i = 0;
513 
514 #ifndef __WXGTK3__
515     GtkNotebook * notebook = GTK_NOTEBOOK(m_widget);
516     if (gtk_notebook_get_scrollable(notebook))
517         i = g_list_position( notebook->children, notebook->first_tab );
518 #endif
519 
520     for ( ; i < count; i++ )
521     {
522         wxGtkNotebookPage* pageData = GetNotebookPage(i);
523         GtkWidget* box = pageData->m_box;
524 
525         const gint border = gtk_container_get_border_width(GTK_CONTAINER(box));
526 
527         if ( IsPointInsideWidget(pt, box, x, y, border) )
528         {
529             // ok, we're inside this tab -- now find out where, if needed
530             if ( flags )
531             {
532                 if (pageData->m_image && IsPointInsideWidget(pt, pageData->m_image, x, y))
533                 {
534                     *flags = wxBK_HITTEST_ONICON;
535                 }
536                 else if (IsPointInsideWidget(pt, pageData->m_label, x, y))
537                 {
538                     *flags = wxBK_HITTEST_ONLABEL;
539                 }
540                 else
541                 {
542                     *flags = wxBK_HITTEST_ONITEM;
543                 }
544             }
545 
546             return i;
547         }
548     }
549 
550     if ( flags )
551     {
552         *flags = wxBK_HITTEST_NOWHERE;
553         wxWindowBase * page = GetCurrentPage();
554         if ( page )
555         {
556             // rect origin is in notebook's parent coordinates
557             wxRect rect = page->GetRect();
558 
559             // adjust it to the notebook's coordinates
560             wxPoint pos = GetPosition();
561             rect.x -= pos.x;
562             rect.y -= pos.y;
563             if ( rect.Contains( pt ) )
564                 *flags |= wxBK_HITTEST_ONPAGE;
565         }
566     }
567 
568     return wxNOT_FOUND;
569 }
570 
OnNavigationKey(wxNavigationKeyEvent & event)571 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
572 {
573     if (event.IsWindowChange())
574         AdvanceSelection( event.GetDirection() );
575     else
576         event.Skip();
577 }
578 
579 #if wxUSE_CONSTRAINTS
580 
581 // override these 2 functions to do nothing: everything is done in OnSize
SetConstraintSizes(bool WXUNUSED (recurse))582 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
583 {
584     // don't set the sizes of the pages - their correct size is not yet known
585     wxControl::SetConstraintSizes(false);
586 }
587 
DoPhase(int WXUNUSED (nPhase))588 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
589 {
590     return true;
591 }
592 
593 #endif
594 
DoApplyWidgetStyle(GtkRcStyle * style)595 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style)
596 {
597     GTKApplyStyle(m_widget, style);
598     for (size_t i = GetPageCount(); i--;)
599         GTKApplyStyle(GetNotebookPage(i)->m_label, style);
600 }
601 
GTKGetWindow(wxArrayGdkWindows & windows) const602 GdkWindow *wxNotebook::GTKGetWindow(wxArrayGdkWindows& windows) const
603 {
604     windows.push_back(gtk_widget_get_window(m_widget));
605 #ifdef __WXGTK3__
606     GdkWindow* wxGTKFindWindow(GtkWidget* widget);
607     GdkWindow* window = wxGTKFindWindow(m_widget);
608     if (window)
609         windows.push_back(window);
610 #else
611     windows.push_back(GTK_NOTEBOOK(m_widget)->event_window);
612 #endif
613 
614     return NULL;
615 }
616 
617 // static
618 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))619 wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
620 {
621     return GetDefaultAttributesFromGTKWidget(gtk_notebook_new());
622 }
623 
624 #endif
625