]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/notebook.cpp
0b95ed355e690dd68168626678a1742815bb20aa
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 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 // VZ: this is rather ugly as we keep the pages themselves in an array (it
35 // allows us to have quite a few functions implemented in the base class)
36 // but the page data is kept in a separate list, so we must maintain them
37 // in sync manually... of course, the list had been there before the base
38 // class which explains it but it still would be nice to do something
41 class wxGtkNotebookPage
: public wxObject
51 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST(wxGtkNotebookPagesList
)
55 static void event_after(GtkNotebook
*, GdkEvent
*, wxNotebook
*);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
64 switch_page_after(GtkWidget
* widget
, GtkNotebookPage
*, guint
, wxNotebook
* win
)
66 g_signal_handlers_block_by_func(widget
, (void*)switch_page_after
, win
);
67 win
->SendPageChangedEvent(win
->m_oldSelection
);
73 switch_page(GtkNotebook
* widget
, GtkNotebookPage
*, int page
, wxNotebook
* win
)
75 win
->m_oldSelection
= gtk_notebook_get_current_page(widget
);
77 if (win
->SendPageChangingEvent(page
))
78 // allow change, unblock handler for changed event
79 g_signal_handlers_unblock_by_func(widget
, (void*)switch_page_after
, win
);
81 // change vetoed, unblock handler to set selection back
82 g_signal_handlers_unblock_by_func(widget
, (void*)event_after
, win
);
86 //-----------------------------------------------------------------------------
87 // "event_after" from m_widget
88 //-----------------------------------------------------------------------------
91 static void event_after(GtkNotebook
* widget
, GdkEvent
*, wxNotebook
* win
)
93 g_signal_handlers_block_by_func(widget
, (void*)event_after
, win
);
94 g_signal_handlers_block_by_func(widget
, (void*)switch_page
, win
);
96 // restore previous selection
97 gtk_notebook_set_current_page(widget
, win
->m_oldSelection
);
99 g_signal_handlers_unblock_by_func(widget
, (void*)switch_page
, win
);
103 //-----------------------------------------------------------------------------
104 // InsertChild callback for wxNotebook
105 //-----------------------------------------------------------------------------
107 static void wxInsertChildInNotebook(wxWindow
* parent
, wxWindow
* child
)
109 // Hack Alert! (Part I): This sets the notebook as the parent of the child
110 // widget, and takes care of some details such as updating the state and
111 // style of the child to reflect its new location. We do this early
112 // because without it GetBestSize (which is used to set the initial size
113 // of controls if an explicit size is not given) will often report
114 // incorrect sizes since the widget's style context is not fully known.
115 // See bug #901694 for details
116 // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
117 gtk_widget_set_parent(child
->m_widget
, parent
->m_widget
);
119 // NOTE: This should be considered a temporary workaround until we can
120 // work out the details and implement delaying the setting of the initial
121 // size of widgets until the size is really needed.
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxBookCtrlBase
)
130 BEGIN_EVENT_TABLE(wxNotebook
, wxBookCtrlBase
)
131 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
134 void wxNotebook::Init()
138 m_themeEnabled
= true;
141 wxNotebook::wxNotebook()
146 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
147 const wxPoint
& pos
, const wxSize
& size
,
148 long style
, const wxString
& name
)
151 Create( parent
, id
, pos
, size
, style
, name
);
154 wxNotebook::~wxNotebook()
159 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
160 const wxPoint
& pos
, const wxSize
& size
,
161 long style
, const wxString
& name
)
163 m_insertCallback
= wxInsertChildInNotebook
;
165 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
168 if (!PreCreation( parent
, pos
, size
) ||
169 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
171 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
176 m_widget
= gtk_notebook_new();
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
, -1, 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(), -1, "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(), -1, "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);
249 wxNotebookPage
*client
= GetPage(page
);
256 bool wxNotebook::SetPageText( size_t page
, const wxString
&text
)
258 wxCHECK_MSG(page
< GetPageCount(), false, "invalid notebook index");
260 GtkLabel
* label
= GTK_LABEL(GetNotebookPage(page
)->m_label
);
261 gtk_label_set_text(label
, wxGTK_CONV(text
));
266 bool wxNotebook::SetPageImage( size_t page
, int image
)
268 wxCHECK_MSG(page
< GetPageCount(), false, "invalid notebook index");
270 wxGtkNotebookPage
* pageData
= GetNotebookPage(page
);
273 wxCHECK_MSG(m_imageList
, false, "invalid notebook imagelist");
274 const wxBitmap
* bitmap
= m_imageList
->GetBitmapPtr(image
);
277 if (pageData
->m_image
)
279 gtk_image_set_from_pixbuf(
280 GTK_IMAGE(pageData
->m_image
), bitmap
->GetPixbuf());
284 pageData
->m_image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
285 gtk_widget_show(pageData
->m_image
);
286 gtk_box_pack_start(GTK_BOX(pageData
->m_box
),
287 pageData
->m_image
, false, false, m_padding
);
290 else if (pageData
->m_image
)
292 gtk_widget_destroy(pageData
->m_image
);
293 pageData
->m_image
= NULL
;
295 pageData
->m_imageIndex
= image
;
300 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
302 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
305 void wxNotebook::SetPadding( const wxSize
&padding
)
307 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
309 m_padding
= padding
.GetWidth();
311 for (size_t i
= GetPageCount(); i
--;)
313 wxGtkNotebookPage
* pageData
= GetNotebookPage(i
);
314 if (pageData
->m_image
)
316 gtk_box_set_child_packing(GTK_BOX(pageData
->m_box
),
317 pageData
->m_image
, false, false, m_padding
, GTK_PACK_START
);
319 gtk_box_set_child_packing(GTK_BOX(pageData
->m_box
),
320 pageData
->m_label
, false, false, m_padding
, GTK_PACK_END
);
324 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
326 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
329 bool wxNotebook::DeleteAllPages()
331 for (size_t i
= GetPageCount(); i
--;)
334 return wxNotebookBase::DeleteAllPages();
337 wxNotebookPage
*wxNotebook::DoRemovePage( size_t page
)
339 // We cannot remove the page yet, as GTK sends the "switch_page"
340 // signal before it has removed the notebook-page from its
341 // corresponding list. Thus, if we were to remove the page from
342 // m_pages at this point, the two lists of pages would be out
343 // of sync during the PAGE_CHANGING/PAGE_CHANGED events.
344 wxNotebookPage
*client
= GetPage(page
);
348 gtk_widget_ref( client
->m_widget
);
349 gtk_widget_unrealize( client
->m_widget
);
351 // we don't need to unparent the client->m_widget; GTK+ will do
352 // that for us (and will throw a warning if we do it!)
353 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
355 // It's safe to remove the page now.
356 wxASSERT_MSG(GetPage(page
) == client
, wxT("pages changed during delete"));
357 wxNotebookBase::DoRemovePage(page
);
359 wxGtkNotebookPage
* p
= GetNotebookPage(page
);
360 m_pagesData
.DeleteObject(p
);
366 bool wxNotebook::InsertPage( size_t position
,
368 const wxString
& text
,
372 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid notebook") );
374 wxCHECK_MSG( win
->GetParent() == this, false,
375 wxT("Can't add a page whose parent is not the notebook!") );
377 wxCHECK_MSG( position
<= GetPageCount(), false,
378 _T("invalid page index in wxNotebookPage::InsertPage()") );
380 // Hack Alert! (Part II): See above in wxInsertChildInNotebook callback
381 // why this has to be done. NOTE: using gtk_widget_unparent here does not
382 // work as it seems to undo too much and will cause errors in the
383 // gtk_notebook_insert_page below, so instead just clear the parent by
385 win
->m_widget
->parent
= NULL
;
388 win
->SetThemeEnabled(true);
390 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
392 wxGtkNotebookPage
* pageData
= new wxGtkNotebookPage
;
394 m_pages
.Insert(win
, position
);
395 m_pagesData
.Insert(position
, pageData
);
397 // set the label image and text
398 // this must be done before adding the page, as GetPageText
399 // and GetPageImage will otherwise return wrong values in
400 // the page-changed event that results from inserting the
402 pageData
->m_imageIndex
= imageId
;
404 pageData
->m_box
= gtk_hbox_new(false, 1);
405 gtk_container_set_border_width(GTK_CONTAINER(pageData
->m_box
), 2);
407 pageData
->m_image
= NULL
;
412 const wxBitmap
* bitmap
= m_imageList
->GetBitmapPtr(imageId
);
413 pageData
->m_image
= gtk_image_new_from_pixbuf(bitmap
->GetPixbuf());
414 gtk_box_pack_start(GTK_BOX(pageData
->m_box
),
415 pageData
->m_image
, false, false, m_padding
);
418 wxFAIL_MSG("invalid notebook imagelist");
421 /* set the label text */
422 pageData
->m_label
= gtk_label_new(wxGTK_CONV(wxStripMenuCodes(text
)));
423 gtk_box_pack_end(GTK_BOX(pageData
->m_box
),
424 pageData
->m_label
, false, false, m_padding
);
426 gtk_widget_show_all(pageData
->m_box
);
427 gtk_notebook_insert_page(notebook
, win
->m_widget
, pageData
->m_box
, position
);
429 /* apply current style */
430 GtkRcStyle
*style
= CreateWidgetStyle();
433 gtk_widget_modify_style(pageData
->m_label
, style
);
434 gtk_rc_style_unref(style
);
437 if (select
&& GetPageCount() > 1)
439 SetSelection( position
);
442 InvalidateBestSize();
446 // helper for HitTest(): check if the point lies inside the given widget which
447 // is the child of the notebook whose position and border size are passed as
450 IsPointInsideWidget(const wxPoint
& pt
, GtkWidget
*w
,
451 gint x
, gint y
, gint border
= 0)
454 (pt
.x
>= w
->allocation
.x
- x
- border
) &&
455 (pt
.x
<= w
->allocation
.x
- x
+ border
+ w
->allocation
.width
) &&
456 (pt
.y
>= w
->allocation
.y
- y
- border
) &&
457 (pt
.y
<= w
->allocation
.y
- y
+ border
+ w
->allocation
.height
);
460 int wxNotebook::HitTest(const wxPoint
& pt
, long *flags
) const
462 const gint x
= m_widget
->allocation
.x
;
463 const gint y
= m_widget
->allocation
.y
;
465 const size_t count
= GetPageCount();
468 GtkNotebook
* notebook
= GTK_NOTEBOOK(m_widget
);
469 if (gtk_notebook_get_scrollable(notebook
))
470 i
= g_list_position( notebook
->children
, notebook
->first_tab
);
472 for ( ; i
< count
; i
++ )
474 wxGtkNotebookPage
* pageData
= GetNotebookPage(i
);
475 GtkWidget
* box
= pageData
->m_box
;
477 const gint border
= gtk_container_get_border_width(GTK_CONTAINER(box
));
479 if ( IsPointInsideWidget(pt
, box
, x
, y
, border
) )
481 // ok, we're inside this tab -- now find out where, if needed
484 if (pageData
->m_image
&& IsPointInsideWidget(pt
, pageData
->m_image
, x
, y
))
486 *flags
= wxBK_HITTEST_ONICON
;
488 else if (IsPointInsideWidget(pt
, pageData
->m_label
, x
, y
))
490 *flags
= wxBK_HITTEST_ONLABEL
;
494 *flags
= wxBK_HITTEST_ONITEM
;
504 *flags
= wxBK_HITTEST_NOWHERE
;
505 wxWindowBase
* page
= GetCurrentPage();
508 // rect origin is in notebook's parent coordinates
509 wxRect rect
= page
->GetRect();
511 // adjust it to the notebook's coordinates
512 wxPoint pos
= GetPosition();
515 if ( rect
.Contains( pt
) )
516 *flags
|= wxBK_HITTEST_ONPAGE
;
523 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
525 if (event
.IsWindowChange())
526 AdvanceSelection( event
.GetDirection() );
531 #if wxUSE_CONSTRAINTS
533 // override these 2 functions to do nothing: everything is done in OnSize
534 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
536 // don't set the sizes of the pages - their correct size is not yet known
537 wxControl::SetConstraintSizes(false);
540 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
547 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle
*style
)
549 gtk_widget_modify_style(m_widget
, style
);
550 for (size_t i
= GetPageCount(); i
--;)
551 gtk_widget_modify_style(GetNotebookPage(i
)->m_label
, style
);
554 GdkWindow
*wxNotebook::GTKGetWindow(wxArrayGdkWindows
& windows
) const
556 windows
.push_back(m_widget
->window
);
557 windows
.push_back(GTK_NOTEBOOK(m_widget
)->event_window
);
564 wxNotebook::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
566 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new
);