]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/notebook.cpp
Make wxWindow::HasScrollbar() respect wxScrolled::ShowScrollbars().
[wxWidgets.git] / src / gtk / notebook.cpp
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"
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
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
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" {
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
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
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
141 wxNotebook::wxNotebook()
142 {
143 Init();
144 }
145
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
154 wxNotebook::~wxNotebook()
155 {
156 DeleteAllPages();
157 }
158
159 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
160 const wxPoint& pos, const wxSize& size,
161 long style, const wxString& name )
162 {
163 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
164 style |= wxBK_TOP;
165
166 if (!PreCreation( parent, pos, size ) ||
167 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
168 {
169 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
170 return false;
171 }
172
173
174 m_widget = gtk_notebook_new();
175 g_object_ref(m_widget);
176
177 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
178
179 g_signal_connect (m_widget, "switch_page",
180 G_CALLBACK(switch_page), this);
181
182 g_signal_connect_after (m_widget, "switch_page",
183 G_CALLBACK(switch_page_after), this);
184 g_signal_handlers_block_by_func(m_widget, (void*)switch_page_after, this);
185
186 g_signal_connect(m_widget, "event_after", G_CALLBACK(event_after), this);
187 g_signal_handlers_block_by_func(m_widget, (void*)event_after, this);
188
189 m_parent->DoAddChild( this );
190
191 if (m_windowStyle & wxBK_RIGHT)
192 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
193 if (m_windowStyle & wxBK_LEFT)
194 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
195 if (m_windowStyle & wxBK_BOTTOM)
196 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
197
198 PostCreation(size);
199
200 return true;
201 }
202
203 int wxNotebook::GetSelection() const
204 {
205 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid notebook") );
206
207 return gtk_notebook_get_current_page( GTK_NOTEBOOK(m_widget) );
208 }
209
210 wxString wxNotebook::GetPageText( size_t page ) const
211 {
212 wxCHECK_MSG(page < GetPageCount(), wxEmptyString, "invalid notebook index");
213
214 GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
215 return wxGTK_CONV_BACK(gtk_label_get_text(label));
216 }
217
218 int wxNotebook::GetPageImage( size_t page ) const
219 {
220 wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
221
222 return GetNotebookPage(page)->m_imageIndex;
223 }
224
225 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
226 {
227 return m_pagesData.Item(page)->GetData();
228 }
229
230 int wxNotebook::DoSetSelection( size_t page, int flags )
231 {
232 wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
233
234 int selOld = GetSelection();
235
236 if ( !(flags & SetSelection_SendEvent) )
237 {
238 g_signal_handlers_block_by_func(m_widget, (void*)switch_page, this);
239 }
240
241 gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget), page );
242
243 if ( !(flags & SetSelection_SendEvent) )
244 {
245 g_signal_handlers_unblock_by_func(m_widget, (void*)switch_page, this);
246 }
247
248 m_selection = page;
249
250 wxNotebookPage *client = GetPage(page);
251 if ( client )
252 client->SetFocus();
253
254 return selOld;
255 }
256
257 void wxNotebook::GTKOnPageChanged()
258 {
259 m_selection = gtk_notebook_get_current_page(GTK_NOTEBOOK(m_widget));
260
261 SendPageChangedEvent(m_oldSelection);
262 }
263
264 bool wxNotebook::SetPageText( size_t page, const wxString &text )
265 {
266 wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
267
268 GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
269 gtk_label_set_text(label, wxGTK_CONV(text));
270
271 return true;
272 }
273
274 bool wxNotebook::SetPageImage( size_t page, int image )
275 {
276 wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
277
278 wxGtkNotebookPage* pageData = GetNotebookPage(page);
279 if (image >= 0)
280 {
281 wxCHECK_MSG(HasImageList(), false, "invalid notebook imagelist");
282 const wxBitmap* bitmap = GetImageList()->GetBitmapPtr(image);
283 if (bitmap == NULL)
284 return false;
285 if (pageData->m_image)
286 {
287 gtk_image_set_from_pixbuf(
288 GTK_IMAGE(pageData->m_image), bitmap->GetPixbuf());
289 }
290 else
291 {
292 pageData->m_image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
293 gtk_widget_show(pageData->m_image);
294 gtk_box_pack_start(GTK_BOX(pageData->m_box),
295 pageData->m_image, false, false, m_padding);
296 }
297 }
298 else if (pageData->m_image)
299 {
300 gtk_widget_destroy(pageData->m_image);
301 pageData->m_image = NULL;
302 }
303 pageData->m_imageIndex = image;
304
305 return true;
306 }
307
308 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
309 {
310 // Compute the max size of the tab labels.
311 wxSize sizeTabMax;
312 const size_t pageCount = GetPageCount();
313 for ( size_t n = 0; n < pageCount; n++ )
314 {
315 GtkRequisition req;
316 gtk_widget_get_preferred_size(GetNotebookPage(n)->m_box, NULL, &req);
317 sizeTabMax.IncTo(wxSize(req.width, req.height));
318 }
319
320 // Unfortunately this doesn't account for the real tab size and I don't
321 // know how to find it, e.g. where do the margins below come from.
322 const int PAGE_MARGIN = 3;
323 const int TAB_MARGIN = 4;
324
325 sizeTabMax.IncBy(3*TAB_MARGIN);
326
327 wxSize sizeFull(sizePage);
328 if ( IsVertical() )
329 sizeFull.y += sizeTabMax.y;
330 else
331 sizeFull.x += sizeTabMax.x;
332
333 sizeFull.IncBy(2*PAGE_MARGIN);
334
335 return sizeFull;
336 }
337
338 void wxNotebook::SetPadding( const wxSize &padding )
339 {
340 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
341
342 m_padding = padding.GetWidth();
343
344 for (size_t i = GetPageCount(); i--;)
345 {
346 wxGtkNotebookPage* pageData = GetNotebookPage(i);
347 if (pageData->m_image)
348 {
349 gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
350 pageData->m_image, false, false, m_padding, GTK_PACK_START);
351 }
352 gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
353 pageData->m_label, false, false, m_padding, GTK_PACK_END);
354 }
355 }
356
357 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
358 {
359 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
360 }
361
362 bool wxNotebook::DeleteAllPages()
363 {
364 for (size_t i = GetPageCount(); i--;)
365 DeletePage(i);
366
367 return wxNotebookBase::DeleteAllPages();
368 }
369
370 wxNotebookPage *wxNotebook::DoRemovePage( size_t page )
371 {
372 // We cannot remove the page yet, as GTK sends the "switch_page"
373 // signal before it has removed the notebook-page from its
374 // corresponding list. Thus, if we were to remove the page from
375 // m_pages at this point, the two lists of pages would be out
376 // of sync during the PAGE_CHANGING/PAGE_CHANGED events.
377 wxNotebookPage *client = GetPage(page);
378 if ( !client )
379 return NULL;
380
381 // we don't need to unparent the client->m_widget; GTK+ will do
382 // that for us (and will throw a warning if we do it!)
383 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
384
385 // It's safe to remove the page now.
386 wxASSERT_MSG(GetPage(page) == client, wxT("pages changed during delete"));
387 wxNotebookBase::DoRemovePage(page);
388
389 wxGtkNotebookPage* p = GetNotebookPage(page);
390 m_pagesData.DeleteObject(p);
391 delete p;
392
393 return client;
394 }
395
396 bool wxNotebook::InsertPage( size_t position,
397 wxNotebookPage* win,
398 const wxString& text,
399 bool select,
400 int imageId )
401 {
402 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid notebook") );
403
404 wxCHECK_MSG( win->GetParent() == this, false,
405 wxT("Can't add a page whose parent is not the notebook!") );
406
407 wxCHECK_MSG( position <= GetPageCount(), false,
408 wxT("invalid page index in wxNotebookPage::InsertPage()") );
409
410 // Hack Alert! (Part II): See above in wxNotebook::AddChildGTK
411 // why this has to be done.
412 gtk_widget_unparent(win->m_widget);
413
414 if (m_themeEnabled)
415 win->SetThemeEnabled(true);
416
417 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
418
419 wxGtkNotebookPage* pageData = new wxGtkNotebookPage;
420
421 m_pages.Insert(win, position);
422 m_pagesData.Insert(position, pageData);
423
424 // set the label image and text
425 // this must be done before adding the page, as GetPageText
426 // and GetPageImage will otherwise return wrong values in
427 // the page-changed event that results from inserting the
428 // first page.
429 pageData->m_imageIndex = imageId;
430
431 pageData->m_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
432 gtk_container_set_border_width(GTK_CONTAINER(pageData->m_box), 2);
433
434 pageData->m_image = NULL;
435 if (imageId != -1)
436 {
437 if (HasImageList())
438 {
439 const wxBitmap* bitmap = GetImageList()->GetBitmapPtr(imageId);
440 pageData->m_image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
441 gtk_box_pack_start(GTK_BOX(pageData->m_box),
442 pageData->m_image, false, false, m_padding);
443 }
444 else
445 {
446 wxFAIL_MSG("invalid notebook imagelist");
447 }
448 }
449
450 /* set the label text */
451 pageData->m_label = gtk_label_new(wxGTK_CONV(wxStripMenuCodes(text)));
452 gtk_box_pack_end(GTK_BOX(pageData->m_box),
453 pageData->m_label, false, false, m_padding);
454
455 gtk_widget_show_all(pageData->m_box);
456 gtk_notebook_insert_page(notebook, win->m_widget, pageData->m_box, position);
457
458 /* apply current style */
459 #ifdef __WXGTK3__
460 GTKApplyStyle(pageData->m_label, NULL);
461 #else
462 GtkRcStyle *style = GTKCreateWidgetStyle();
463 if ( style )
464 {
465 gtk_widget_modify_style(pageData->m_label, style);
466 g_object_unref(style);
467 }
468 #endif
469
470 if (select && GetPageCount() > 1)
471 {
472 SetSelection( position );
473 }
474
475 InvalidateBestSize();
476 return true;
477 }
478
479 // helper for HitTest(): check if the point lies inside the given widget which
480 // is the child of the notebook whose position and border size are passed as
481 // parameters
482 static bool
483 IsPointInsideWidget(const wxPoint& pt, GtkWidget *w,
484 gint x, gint y, gint border = 0)
485 {
486 GtkAllocation a;
487 gtk_widget_get_allocation(w, &a);
488 return
489 (pt.x >= a.x - x - border) &&
490 (pt.x <= a.x - x + border + a.width) &&
491 (pt.y >= a.y - y - border) &&
492 (pt.y <= a.y - y + border + a.height);
493 }
494
495 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
496 {
497 GtkAllocation a;
498 gtk_widget_get_allocation(m_widget, &a);
499 const int x = a.x;
500 const int y = a.y;
501
502 const size_t count = GetPageCount();
503 size_t i = 0;
504
505 #ifndef __WXGTK3__
506 GtkNotebook * notebook = GTK_NOTEBOOK(m_widget);
507 if (gtk_notebook_get_scrollable(notebook))
508 i = g_list_position( notebook->children, notebook->first_tab );
509 #endif
510
511 for ( ; i < count; i++ )
512 {
513 wxGtkNotebookPage* pageData = GetNotebookPage(i);
514 GtkWidget* box = pageData->m_box;
515
516 const gint border = gtk_container_get_border_width(GTK_CONTAINER(box));
517
518 if ( IsPointInsideWidget(pt, box, x, y, border) )
519 {
520 // ok, we're inside this tab -- now find out where, if needed
521 if ( flags )
522 {
523 if (pageData->m_image && IsPointInsideWidget(pt, pageData->m_image, x, y))
524 {
525 *flags = wxBK_HITTEST_ONICON;
526 }
527 else if (IsPointInsideWidget(pt, pageData->m_label, x, y))
528 {
529 *flags = wxBK_HITTEST_ONLABEL;
530 }
531 else
532 {
533 *flags = wxBK_HITTEST_ONITEM;
534 }
535 }
536
537 return i;
538 }
539 }
540
541 if ( flags )
542 {
543 *flags = wxBK_HITTEST_NOWHERE;
544 wxWindowBase * page = GetCurrentPage();
545 if ( page )
546 {
547 // rect origin is in notebook's parent coordinates
548 wxRect rect = page->GetRect();
549
550 // adjust it to the notebook's coordinates
551 wxPoint pos = GetPosition();
552 rect.x -= pos.x;
553 rect.y -= pos.y;
554 if ( rect.Contains( pt ) )
555 *flags |= wxBK_HITTEST_ONPAGE;
556 }
557 }
558
559 return wxNOT_FOUND;
560 }
561
562 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
563 {
564 if (event.IsWindowChange())
565 AdvanceSelection( event.GetDirection() );
566 else
567 event.Skip();
568 }
569
570 #if wxUSE_CONSTRAINTS
571
572 // override these 2 functions to do nothing: everything is done in OnSize
573 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
574 {
575 // don't set the sizes of the pages - their correct size is not yet known
576 wxControl::SetConstraintSizes(false);
577 }
578
579 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
580 {
581 return true;
582 }
583
584 #endif
585
586 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style)
587 {
588 GTKApplyStyle(m_widget, style);
589 for (size_t i = GetPageCount(); i--;)
590 GTKApplyStyle(GetNotebookPage(i)->m_label, style);
591 }
592
593 GdkWindow *wxNotebook::GTKGetWindow(wxArrayGdkWindows& windows) const
594 {
595 windows.push_back(gtk_widget_get_window(m_widget));
596 #ifdef __WXGTK3__
597 // no access to internal GdkWindows
598 #else
599 windows.push_back(GTK_NOTEBOOK(m_widget)->event_window);
600 #endif
601
602 return NULL;
603 }
604
605 // static
606 wxVisualAttributes
607 wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
608 {
609 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new());
610 }
611
612 #endif