]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/notebook.cpp
Invalid conversion compile error fix (GTK+ 2.12.9)
[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 <gtk/gtk.h>
29 #include "wx/gtk/private.h"
30 #include "wx/gtk/private/gtk2-compat.h"
31
32 //-----------------------------------------------------------------------------
33 // wxGtkNotebookPage
34 //-----------------------------------------------------------------------------
35
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
41 // about this one day
42
43 class wxGtkNotebookPage: public wxObject
44 {
45 public:
46 GtkWidget* m_box;
47 GtkWidget* m_label;
48 GtkWidget* m_image;
49 int m_imageIndex;
50 };
51
52
53 #include "wx/listimpl.cpp"
54 WX_DEFINE_LIST(wxGtkNotebookPagesList)
55
56 extern "C" {
57 static void event_after(GtkNotebook*, GdkEvent*, wxNotebook*);
58 }
59
60 //-----------------------------------------------------------------------------
61 // "switch_page"
62 //-----------------------------------------------------------------------------
63
64 extern "C" {
65 static void
66 switch_page_after(GtkNotebook* widget, GtkNotebookPage*, guint, wxNotebook* win)
67 {
68 g_signal_handlers_block_by_func(widget, (void*)switch_page_after, win);
69
70 win->GTKOnPageChanged();
71 }
72 }
73
74 extern "C" {
75 static void
76 switch_page(GtkNotebook* widget, GtkNotebookPage*, int page, wxNotebook* win)
77 {
78 win->m_oldSelection = gtk_notebook_get_current_page(widget);
79
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);
83 else
84 // change vetoed, unblock handler to set selection back
85 g_signal_handlers_unblock_by_func(widget, (void*)event_after, win);
86 }
87 }
88
89 //-----------------------------------------------------------------------------
90 // "event_after" from m_widget
91 //-----------------------------------------------------------------------------
92
93 extern "C" {
94 static void event_after(GtkNotebook* widget, GdkEvent*, wxNotebook* win)
95 {
96 g_signal_handlers_block_by_func(widget, (void*)event_after, win);
97 g_signal_handlers_block_by_func(widget, (void*)switch_page, win);
98
99 // restore previous selection
100 gtk_notebook_set_current_page(widget, win->m_oldSelection);
101
102 g_signal_handlers_unblock_by_func(widget, (void*)switch_page, win);
103 }
104 }
105
106 //-----------------------------------------------------------------------------
107 // InsertChild callback for wxNotebook
108 //-----------------------------------------------------------------------------
109
110 void wxNotebook::AddChildGTK(wxWindowGTK* child)
111 {
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);
121
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.
125 }
126
127 //-----------------------------------------------------------------------------
128 // wxNotebook
129 //-----------------------------------------------------------------------------
130
131 BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
132 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
133 END_EVENT_TABLE()
134
135 void wxNotebook::Init()
136 {
137 m_padding = 0;
138 m_oldSelection = -1;
139 m_themeEnabled = true;
140 }
141
142 wxNotebook::wxNotebook()
143 {
144 Init();
145 }
146
147 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
148 const wxPoint& pos, const wxSize& size,
149 long style, const wxString& name )
150 {
151 Init();
152 Create( parent, id, pos, size, style, name );
153 }
154
155 wxNotebook::~wxNotebook()
156 {
157 DeleteAllPages();
158 }
159
160 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
161 const wxPoint& pos, const wxSize& size,
162 long style, const wxString& name )
163 {
164 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
165 style |= wxBK_TOP;
166
167 if (!PreCreation( parent, pos, size ) ||
168 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
169 {
170 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
171 return false;
172 }
173
174
175 m_widget = gtk_notebook_new();
176 g_object_ref(m_widget);
177
178 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
179
180 g_signal_connect (m_widget, "switch_page",
181 G_CALLBACK(switch_page), this);
182
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);
186
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);
189
190 m_parent->DoAddChild( this );
191
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 );
198
199 PostCreation(size);
200
201 return true;
202 }
203
204 int wxNotebook::GetSelection() const
205 {
206 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid notebook") );
207
208 return gtk_notebook_get_current_page( GTK_NOTEBOOK(m_widget) );
209 }
210
211 wxString wxNotebook::GetPageText( size_t page ) const
212 {
213 wxCHECK_MSG(page < GetPageCount(), wxEmptyString, "invalid notebook index");
214
215 GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
216 return wxGTK_CONV_BACK(gtk_label_get_text(label));
217 }
218
219 int wxNotebook::GetPageImage( size_t page ) const
220 {
221 wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
222
223 return GetNotebookPage(page)->m_imageIndex;
224 }
225
226 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
227 {
228 return m_pagesData.Item(page)->GetData();
229 }
230
231 int wxNotebook::DoSetSelection( size_t page, int flags )
232 {
233 wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
234
235 int selOld = GetSelection();
236
237 if ( !(flags & SetSelection_SendEvent) )
238 {
239 g_signal_handlers_block_by_func(m_widget, (void*)switch_page, this);
240 }
241
242 gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget), page );
243
244 if ( !(flags & SetSelection_SendEvent) )
245 {
246 g_signal_handlers_unblock_by_func(m_widget, (void*)switch_page, this);
247 }
248
249 m_selection = page;
250
251 wxNotebookPage *client = GetPage(page);
252 if ( client )
253 client->SetFocus();
254
255 return selOld;
256 }
257
258 void wxNotebook::GTKOnPageChanged()
259 {
260 m_selection = gtk_notebook_get_current_page(GTK_NOTEBOOK(m_widget));
261
262 SendPageChangedEvent(m_oldSelection);
263 }
264
265 bool wxNotebook::SetPageText( size_t page, const wxString &text )
266 {
267 wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
268
269 GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
270 gtk_label_set_text(label, wxGTK_CONV(text));
271
272 return true;
273 }
274
275 bool wxNotebook::SetPageImage( size_t page, int image )
276 {
277 wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
278
279 wxGtkNotebookPage* pageData = GetNotebookPage(page);
280 if (image >= 0)
281 {
282 wxCHECK_MSG(HasImageList(), false, "invalid notebook imagelist");
283 const wxBitmap* bitmap = GetImageList()->GetBitmapPtr(image);
284 if (bitmap == NULL)
285 return false;
286 if (pageData->m_image)
287 {
288 gtk_image_set_from_pixbuf(
289 GTK_IMAGE(pageData->m_image), bitmap->GetPixbuf());
290 }
291 else
292 {
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);
297 }
298 }
299 else if (pageData->m_image)
300 {
301 gtk_widget_destroy(pageData->m_image);
302 pageData->m_image = NULL;
303 }
304 pageData->m_imageIndex = image;
305
306 return true;
307 }
308
309 wxSize wxNotebook::CalcSizeFromPage(const wxSize& sizePage) const
310 {
311 // Compute the max size of the tab labels.
312 wxSize sizeTabMax;
313 const size_t pageCount = GetPageCount();
314 for ( size_t n = 0; n < pageCount; n++ )
315 {
316 GtkRequisition req;
317 gtk_widget_get_preferred_size(GetNotebookPage(n)->m_box, NULL, &req);
318 sizeTabMax.IncTo(wxSize(req.width, req.height));
319 }
320
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;
325
326 sizeTabMax.IncBy(3*TAB_MARGIN);
327
328 wxSize sizeFull(sizePage);
329 if ( IsVertical() )
330 sizeFull.y += sizeTabMax.y;
331 else
332 sizeFull.x += sizeTabMax.x;
333
334 sizeFull.IncBy(2*PAGE_MARGIN);
335
336 return sizeFull;
337 }
338
339 void wxNotebook::SetPadding( const wxSize &padding )
340 {
341 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
342
343 m_padding = padding.GetWidth();
344
345 for (size_t i = GetPageCount(); i--;)
346 {
347 wxGtkNotebookPage* pageData = GetNotebookPage(i);
348 if (pageData->m_image)
349 {
350 gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
351 pageData->m_image, false, false, m_padding, GTK_PACK_START);
352 }
353 gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
354 pageData->m_label, false, false, m_padding, GTK_PACK_END);
355 }
356 }
357
358 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
359 {
360 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
361 }
362
363 bool wxNotebook::DeleteAllPages()
364 {
365 for (size_t i = GetPageCount(); i--;)
366 DeletePage(i);
367
368 return wxNotebookBase::DeleteAllPages();
369 }
370
371 wxNotebookPage *wxNotebook::DoRemovePage( size_t page )
372 {
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);
379 if ( !client )
380 return NULL;
381
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 );
385
386 // It's safe to remove the page now.
387 wxASSERT_MSG(GetPage(page) == client, wxT("pages changed during delete"));
388 wxNotebookBase::DoRemovePage(page);
389
390 wxGtkNotebookPage* p = GetNotebookPage(page);
391 m_pagesData.DeleteObject(p);
392 delete p;
393
394 return client;
395 }
396
397 bool wxNotebook::InsertPage( size_t position,
398 wxNotebookPage* win,
399 const wxString& text,
400 bool select,
401 int imageId )
402 {
403 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid notebook") );
404
405 wxCHECK_MSG( win->GetParent() == this, false,
406 wxT("Can't add a page whose parent is not the notebook!") );
407
408 wxCHECK_MSG( position <= GetPageCount(), false,
409 wxT("invalid page index in wxNotebookPage::InsertPage()") );
410
411 // Hack Alert! (Part II): See above in wxNotebook::AddChildGTK
412 // why this has to be done.
413 gtk_widget_unparent(win->m_widget);
414
415 if (m_themeEnabled)
416 win->SetThemeEnabled(true);
417
418 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
419
420 wxGtkNotebookPage* pageData = new wxGtkNotebookPage;
421
422 m_pages.Insert(win, position);
423 m_pagesData.Insert(position, pageData);
424
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
429 // first page.
430 pageData->m_imageIndex = imageId;
431
432 pageData->m_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
433 gtk_container_set_border_width(GTK_CONTAINER(pageData->m_box), 2);
434
435 pageData->m_image = NULL;
436 if (imageId != -1)
437 {
438 if (HasImageList())
439 {
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);
444 }
445 else
446 {
447 wxFAIL_MSG("invalid notebook imagelist");
448 }
449 }
450
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);
455
456 gtk_widget_show_all(pageData->m_box);
457 gtk_notebook_insert_page(notebook, win->m_widget, pageData->m_box, position);
458
459 /* apply current style */
460 #ifdef __WXGTK3__
461 GTKApplyStyle(pageData->m_label, NULL);
462 #else
463 GtkRcStyle *style = GTKCreateWidgetStyle();
464 if ( style )
465 {
466 gtk_widget_modify_style(pageData->m_label, style);
467 g_object_unref(style);
468 }
469 #endif
470
471 if (select && GetPageCount() > 1)
472 {
473 SetSelection( position );
474 }
475
476 InvalidateBestSize();
477 return true;
478 }
479
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
482 // parameters
483 static bool
484 IsPointInsideWidget(const wxPoint& pt, GtkWidget *w,
485 gint x, gint y, gint border = 0)
486 {
487 GtkAllocation a;
488 gtk_widget_get_allocation(w, &a);
489 return
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);
494 }
495
496 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
497 {
498 GtkAllocation a;
499 gtk_widget_get_allocation(m_widget, &a);
500 const int x = a.x;
501 const int y = a.y;
502
503 const size_t count = GetPageCount();
504 size_t i = 0;
505
506 #ifndef __WXGTK3__
507 GtkNotebook * notebook = GTK_NOTEBOOK(m_widget);
508 if (gtk_notebook_get_scrollable(notebook))
509 i = g_list_position( notebook->children, notebook->first_tab );
510 #endif
511
512 for ( ; i < count; i++ )
513 {
514 wxGtkNotebookPage* pageData = GetNotebookPage(i);
515 GtkWidget* box = pageData->m_box;
516
517 const gint border = gtk_container_get_border_width(GTK_CONTAINER(box));
518
519 if ( IsPointInsideWidget(pt, box, x, y, border) )
520 {
521 // ok, we're inside this tab -- now find out where, if needed
522 if ( flags )
523 {
524 if (pageData->m_image && IsPointInsideWidget(pt, pageData->m_image, x, y))
525 {
526 *flags = wxBK_HITTEST_ONICON;
527 }
528 else if (IsPointInsideWidget(pt, pageData->m_label, x, y))
529 {
530 *flags = wxBK_HITTEST_ONLABEL;
531 }
532 else
533 {
534 *flags = wxBK_HITTEST_ONITEM;
535 }
536 }
537
538 return i;
539 }
540 }
541
542 if ( flags )
543 {
544 *flags = wxBK_HITTEST_NOWHERE;
545 wxWindowBase * page = GetCurrentPage();
546 if ( page )
547 {
548 // rect origin is in notebook's parent coordinates
549 wxRect rect = page->GetRect();
550
551 // adjust it to the notebook's coordinates
552 wxPoint pos = GetPosition();
553 rect.x -= pos.x;
554 rect.y -= pos.y;
555 if ( rect.Contains( pt ) )
556 *flags |= wxBK_HITTEST_ONPAGE;
557 }
558 }
559
560 return wxNOT_FOUND;
561 }
562
563 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
564 {
565 if (event.IsWindowChange())
566 AdvanceSelection( event.GetDirection() );
567 else
568 event.Skip();
569 }
570
571 #if wxUSE_CONSTRAINTS
572
573 // override these 2 functions to do nothing: everything is done in OnSize
574 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
575 {
576 // don't set the sizes of the pages - their correct size is not yet known
577 wxControl::SetConstraintSizes(false);
578 }
579
580 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
581 {
582 return true;
583 }
584
585 #endif
586
587 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style)
588 {
589 GTKApplyStyle(m_widget, style);
590 for (size_t i = GetPageCount(); i--;)
591 GTKApplyStyle(GetNotebookPage(i)->m_label, style);
592 }
593
594 GdkWindow *wxNotebook::GTKGetWindow(wxArrayGdkWindows& windows) const
595 {
596 windows.push_back(gtk_widget_get_window(m_widget));
597 #ifdef __WXGTK3__
598 // no access to internal GdkWindows
599 #else
600 windows.push_back(GTK_NOTEBOOK(m_widget)->event_window);
601 #endif
602
603 return NULL;
604 }
605
606 // static
607 wxVisualAttributes
608 wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
609 {
610 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new());
611 }
612
613 #endif