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