Test the commit announcement bot some more by fixing more gtk signal callback handler...
[wxWidgets.git] / src / gtk / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/notebook.h"
14
15 #if wxUSE_NOTEBOOK
16
17 #include "wx/panel.h"
18 #include "wx/utils.h"
19 #include "wx/imaglist.h"
20 #include "wx/intl.h"
21 #include "wx/log.h"
22 #include "wx/bitmap.h"
23 #include "wx/fontutil.h"
24
25 // FIXME: Use GtkImage instead of GtkPixmap. Don't use gtk_container_border_width
26 #include <gtk/gtkversion.h>
27 #ifdef GTK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
29 #endif
30
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/win_gtk.h"
33
34 #include <gdk/gdkkeysyms.h>
35
36 #include "wx/msgdlg.h"
37
38 // ----------------------------------------------------------------------------
39 // events
40 // ----------------------------------------------------------------------------
41
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
44
45 //-----------------------------------------------------------------------------
46 // idle system
47 //-----------------------------------------------------------------------------
48
49 extern void wxapp_install_idle_handler();
50 extern bool g_isIdle;
51
52 //-----------------------------------------------------------------------------
53 // data
54 //-----------------------------------------------------------------------------
55
56 extern bool g_blockEventsOnDrag;
57
58 //-----------------------------------------------------------------------------
59 // wxGtkNotebookPage
60 //-----------------------------------------------------------------------------
61
62 // VZ: this is rather ugly as we keep the pages themselves in an array (it
63 // allows us to have quite a few functions implemented in the base class)
64 // but the page data is kept in a separate list, so we must maintain them
65 // in sync manually... of course, the list had been there before the base
66 // class which explains it but it still would be nice to do something
67 // about this one day
68
69 class wxGtkNotebookPage: public wxObject
70 {
71 public:
72 wxGtkNotebookPage()
73 {
74 m_image = -1;
75 m_page = (GtkNotebookPage *) NULL;
76 m_box = (GtkWidget *) NULL;
77 }
78
79 wxString m_text;
80 int m_image;
81 GtkNotebookPage *m_page;
82 GtkLabel *m_label;
83 GtkWidget *m_box; // in which the label and image are packed
84 };
85
86
87 #include "wx/listimpl.cpp"
88 WX_DEFINE_LIST(wxGtkNotebookPagesList)
89
90
91 //-----------------------------------------------------------------------------
92 // "switch_page"
93 //-----------------------------------------------------------------------------
94
95 extern "C" {
96 static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
97 GtkNotebookPage *WXUNUSED(page),
98 guint page,
99 wxNotebook *notebook )
100 {
101 // are you trying to call SetSelection() from a notebook event handler?
102 // you shouldn't!
103 wxCHECK_RET( !notebook->m_inSwitchPage,
104 _T("gtk_notebook_page_change_callback reentered") );
105
106 notebook->m_inSwitchPage = TRUE;
107 if (g_isIdle)
108 wxapp_install_idle_handler();
109
110 int old = notebook->GetSelection();
111
112 wxNotebookEvent eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,
113 notebook->GetId(), page, old );
114 eventChanging.SetEventObject( notebook );
115
116 if ( (notebook->GetEventHandler()->ProcessEvent(eventChanging)) &&
117 !eventChanging.IsAllowed() )
118 {
119 /* program doesn't allow the page change */
120 g_signal_stop_emission_by_name (notebook->m_widget,
121 "switch_page");
122 }
123 else // change allowed
124 {
125 // make wxNotebook::GetSelection() return the correct (i.e. consistent
126 // with wxNotebookEvent::GetSelection()) value even though the page is
127 // not really changed in GTK+
128 notebook->m_selection = page;
129
130 wxNotebookEvent eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
131 notebook->GetId(), page, old );
132 eventChanged.SetEventObject( notebook );
133 notebook->GetEventHandler()->ProcessEvent( eventChanged );
134 }
135
136 notebook->m_inSwitchPage = FALSE;
137 }
138 }
139
140 //-----------------------------------------------------------------------------
141 // "size_allocate"
142 //-----------------------------------------------------------------------------
143
144 extern "C" {
145 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
146 {
147 if (g_isIdle)
148 wxapp_install_idle_handler();
149
150 if ((win->m_x == alloc->x) &&
151 (win->m_y == alloc->y) &&
152 (win->m_width == alloc->width) &&
153 (win->m_height == alloc->height))
154 {
155 return;
156 }
157
158 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
159
160 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
161 here in order to make repositioning after resizing to take effect. */
162 if ((gtk_major_version == 1) &&
163 (gtk_minor_version == 2) &&
164 (gtk_micro_version < 6) &&
165 (win->m_wxwindow) &&
166 (GTK_WIDGET_REALIZED(win->m_wxwindow)))
167 {
168 gtk_widget_size_allocate( win->m_wxwindow, alloc );
169 }
170 }
171 }
172
173 //-----------------------------------------------------------------------------
174 // "realize" from m_widget
175 //-----------------------------------------------------------------------------
176
177 extern "C" {
178 static void
179 gtk_notebook_realized_callback( GtkWidget * WXUNUSED(widget), wxWindow *win )
180 {
181 if (g_isIdle)
182 wxapp_install_idle_handler();
183
184 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
185 here in order to make repositioning before showing to take effect. */
186 gtk_widget_queue_resize( win->m_widget );
187 }
188 }
189
190 //-----------------------------------------------------------------------------
191 // "key_press_event"
192 //-----------------------------------------------------------------------------
193
194 extern "C" {
195 static gboolean
196 gtk_notebook_key_press_callback( GtkWidget *widget,
197 GdkEventKey *gdk_event,
198 wxNotebook *notebook )
199 {
200 if (g_isIdle)
201 wxapp_install_idle_handler();
202
203 if (!notebook->m_hasVMT) return FALSE;
204 if (g_blockEventsOnDrag) return FALSE;
205
206 /* win is a control: tab can be propagated up */
207 if ((gdk_event->keyval == GDK_Left) || (gdk_event->keyval == GDK_Right))
208 {
209 int page;
210 int nMax = notebook->GetPageCount();
211 if ( nMax-- ) // decrement it to get the last valid index
212 {
213 int nSel = notebook->GetSelection();
214
215 // change selection wrapping if it becomes invalid
216 page = (gdk_event->keyval != GDK_Left) ? nSel == nMax ? 0
217 : nSel + 1
218 : nSel == 0 ? nMax
219 : nSel - 1;
220 }
221 else // notebook is empty, no next page
222 {
223 return FALSE;
224 }
225
226 // m_selection = page;
227 gtk_notebook_set_current_page( GTK_NOTEBOOK(widget), page );
228
229 g_signal_stop_emission_by_name (widget, "key_press_event");
230 return TRUE;
231 }
232
233 /* win is a control: tab can be propagated up */
234 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
235 {
236 int sel = notebook->GetSelection();
237 if (sel == -1)
238 return TRUE;
239 wxGtkNotebookPage *nb_page = notebook->GetNotebookPage(sel);
240 wxCHECK_MSG( nb_page, FALSE, _T("invalid selection in wxNotebook") );
241
242 wxNavigationKeyEvent event;
243 event.SetEventObject( notebook );
244 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
245 event.SetDirection( (gdk_event->keyval == GDK_Tab) );
246 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
247 event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ||
248 (gdk_event->keyval == GDK_Left) || (gdk_event->keyval == GDK_Right) );
249 event.SetCurrentFocus( notebook );
250
251 wxNotebookPage *client = notebook->GetPage(sel);
252 if ( !client->GetEventHandler()->ProcessEvent( event ) )
253 {
254 client->SetFocus();
255 }
256
257 g_signal_stop_emission_by_name (widget, "key_press_event");
258 return TRUE;
259 }
260
261 return FALSE;
262 }
263 }
264
265 //-----------------------------------------------------------------------------
266 // InsertChild callback for wxNotebook
267 //-----------------------------------------------------------------------------
268
269 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
270 {
271 // Hack Alert! (Part I): This sets the notebook as the parent of the child
272 // widget, and takes care of some details such as updating the state and
273 // style of the child to reflect its new location. We do this early
274 // because without it GetBestSize (which is used to set the initial size
275 // of controls if an explicit size is not given) will often report
276 // incorrect sizes since the widget's style context is not fully known.
277 // See bug #901694 for details
278 // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
279 gtk_widget_set_parent(child->m_widget, parent->m_widget);
280
281 // NOTE: This should be considered a temporary workaround until we can
282 // work out the details and implement delaying the setting of the initial
283 // size of widgets until the size is really needed.
284 }
285
286 //-----------------------------------------------------------------------------
287 // wxNotebook
288 //-----------------------------------------------------------------------------
289
290 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
291
292 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
293 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
294 END_EVENT_TABLE()
295
296 void wxNotebook::Init()
297 {
298 m_padding = 0;
299 m_inSwitchPage = FALSE;
300
301 m_imageList = (wxImageList *) NULL;
302 m_selection = -1;
303 m_themeEnabled = TRUE;
304 }
305
306 wxNotebook::wxNotebook()
307 {
308 Init();
309 }
310
311 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
312 const wxPoint& pos, const wxSize& size,
313 long style, const wxString& name )
314 {
315 Init();
316 Create( parent, id, pos, size, style, name );
317 }
318
319 wxNotebook::~wxNotebook()
320 {
321 DeleteAllPages();
322 }
323
324 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
325 const wxPoint& pos, const wxSize& size,
326 long style, const wxString& name )
327 {
328 m_needParent = TRUE;
329 m_acceptsFocus = TRUE;
330 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
331
332 if (!PreCreation( parent, pos, size ) ||
333 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
334 {
335 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
336 return FALSE;
337 }
338
339
340 m_widget = gtk_notebook_new();
341
342 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
343
344 g_signal_connect (m_widget, "switch_page",
345 G_CALLBACK (gtk_notebook_page_change_callback), this);
346
347 m_parent->DoAddChild( this );
348
349 if (m_windowStyle & wxBK_RIGHT)
350 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
351 if (m_windowStyle & wxBK_LEFT)
352 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
353 if (m_windowStyle & wxBK_BOTTOM)
354 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
355
356 g_signal_connect (m_widget, "key_press_event",
357 G_CALLBACK (gtk_notebook_key_press_callback), this);
358
359 PostCreation(size);
360
361 g_signal_connect (m_widget, "realize",
362 G_CALLBACK (gtk_notebook_realized_callback), this);
363
364 return TRUE;
365 }
366
367 int wxNotebook::GetSelection() const
368 {
369 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
370
371 if ( m_selection == -1 )
372 {
373 GList *nb_pages = GTK_NOTEBOOK(m_widget)->children;
374
375 if (g_list_length(nb_pages) != 0)
376 {
377 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
378
379 gpointer cur = notebook->cur_page;
380 if ( cur != NULL )
381 {
382 wxConstCast(this, wxNotebook)->m_selection =
383 g_list_index( nb_pages, cur );
384 }
385 }
386 }
387
388 return m_selection;
389 }
390
391 wxString wxNotebook::GetPageText( size_t page ) const
392 {
393 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") );
394
395 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
396 if (nb_page)
397 return nb_page->m_text;
398 else
399 return wxT("");
400 }
401
402 int wxNotebook::GetPageImage( size_t page ) const
403 {
404 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
405
406 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
407 if (nb_page)
408 return nb_page->m_image;
409 else
410 return -1;
411 }
412
413 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
414 {
415 wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") );
416
417 wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") );
418
419 return m_pagesData.Item(page)->GetData();
420 }
421
422 int wxNotebook::SetSelection( size_t page )
423 {
424 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
425
426 wxCHECK_MSG( page < m_pagesData.GetCount(), -1, wxT("invalid notebook index") );
427
428 int selOld = GetSelection();
429
430 // cache the selection
431 m_selection = page;
432 gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget), page );
433
434 wxNotebookPage *client = GetPage(page);
435 if ( client )
436 client->SetFocus();
437
438 return selOld;
439 }
440
441 bool wxNotebook::SetPageText( size_t page, const wxString &text )
442 {
443 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
444
445 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
446
447 wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") );
448
449 nb_page->m_text = text;
450
451 gtk_label_set_text( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) );
452
453 return TRUE;
454 }
455
456 bool wxNotebook::SetPageImage( size_t page, int image )
457 {
458 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
459
460 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
461
462 if (!nb_page) return FALSE;
463
464 /* Optimization posibility: return immediately if image unchanged.
465 * Not enabled because it may break existing (stupid) code that
466 * manipulates the imagelist to cycle images */
467
468 /* if (image == nb_page->m_image) return TRUE; */
469
470 /* For different cases:
471 1) no image -> no image
472 2) image -> no image
473 3) no image -> image
474 4) image -> image */
475
476 if (image == -1 && nb_page->m_image == -1)
477 return TRUE; /* Case 1): Nothing to do. */
478
479 GtkWidget *pixmapwid = (GtkWidget*) NULL;
480
481 if (nb_page->m_image != -1)
482 {
483 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
484
485 GList *child = gtk_container_get_children(GTK_CONTAINER(nb_page->m_box));
486 while (child)
487 {
488 if (GTK_IS_PIXMAP(child->data))
489 {
490 pixmapwid = GTK_WIDGET(child->data);
491 break;
492 }
493 child = child->next;
494 }
495
496 /* We should have the pixmap widget now */
497 wxASSERT(pixmapwid != NULL);
498
499 if (image == -1)
500 {
501 /* If there's no new widget, just remove the old from the box */
502 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
503 nb_page->m_image = -1;
504
505 return TRUE; /* Case 2) */
506 }
507 }
508
509 /* Only cases 3) and 4) left */
510 wxASSERT( m_imageList != NULL ); /* Just in case */
511
512 /* Construct the new pixmap */
513 const wxBitmap *bmp = m_imageList->GetBitmapPtr(image);
514 GdkPixmap *pixmap = bmp->GetPixmap();
515 GdkBitmap *mask = (GdkBitmap*) NULL;
516 if ( bmp->GetMask() )
517 {
518 mask = bmp->GetMask()->GetBitmap();
519 }
520
521 if (pixmapwid == NULL)
522 {
523 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
524 pixmapwid = gtk_pixmap_new (pixmap, mask );
525
526 /* CHECKME: Are these pack flags okay? */
527 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
528 gtk_widget_show(pixmapwid);
529 }
530 else
531 {
532 /* Case 4) Simply replace the pixmap */
533 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
534 }
535
536 nb_page->m_image = image;
537
538 return TRUE;
539 }
540
541 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
542 {
543 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
544 }
545
546 void wxNotebook::SetPadding( const wxSize &padding )
547 {
548 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
549
550 m_padding = padding.GetWidth();
551
552 int i;
553 for (i=0; i<int(GetPageCount()); i++)
554 {
555 wxGtkNotebookPage* nb_page = GetNotebookPage(i);
556 wxASSERT(nb_page != NULL);
557
558 if (nb_page->m_image != -1)
559 {
560 // gtk_box_set_child_packing sets padding on BOTH sides
561 // icon provides left padding, label provides center and right
562 int image = nb_page->m_image;
563 SetPageImage(i,-1);
564 SetPageImage(i,image);
565 }
566 wxASSERT(nb_page->m_label);
567 gtk_box_set_child_packing(GTK_BOX(nb_page->m_box),
568 GTK_WIDGET(nb_page->m_label),
569 FALSE, FALSE, m_padding, GTK_PACK_END);
570 }
571 }
572
573 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
574 {
575 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
576 }
577
578 bool wxNotebook::DeleteAllPages()
579 {
580 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
581
582 while (m_pagesData.GetCount() > 0)
583 DeletePage( m_pagesData.GetCount()-1 );
584
585 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
586
587 InvalidateBestSize();
588 return wxNotebookBase::DeleteAllPages();
589 }
590
591 wxNotebookPage *wxNotebook::DoRemovePage( size_t page )
592 {
593 if ( m_selection != -1 && (size_t)m_selection >= page )
594 {
595 // the index will become invalid after the page is deleted
596 m_selection = -1;
597 }
598
599 wxNotebookPage *client = wxNotebookBase::DoRemovePage(page);
600 if ( !client )
601 return NULL;
602
603 gtk_widget_ref( client->m_widget );
604 gtk_widget_unrealize( client->m_widget );
605 gtk_widget_unparent( client->m_widget );
606
607 // gtk_notebook_remove_page() sends "switch_page" signal with some strange
608 // new page index (when deleting selected page 0, new page is 1 although,
609 // clearly, the selection should stay 0), so suppress this
610 g_signal_handlers_disconnect_by_func (m_widget,
611 (gpointer) gtk_notebook_page_change_callback,
612 this);
613
614 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
615
616 g_signal_connect (m_widget, "switch_page",
617 G_CALLBACK (gtk_notebook_page_change_callback), this);
618
619 wxGtkNotebookPage* p = GetNotebookPage(page);
620 m_pagesData.DeleteObject(p);
621 delete p;
622
623 return client;
624 }
625
626 bool wxNotebook::InsertPage( size_t position,
627 wxNotebookPage* win,
628 const wxString& text,
629 bool select,
630 int imageId )
631 {
632 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
633
634 wxCHECK_MSG( win->GetParent() == this, FALSE,
635 wxT("Can't add a page whose parent is not the notebook!") );
636
637 wxCHECK_MSG( position <= GetPageCount(), FALSE,
638 _T("invalid page index in wxNotebookPage::InsertPage()") );
639
640 // Hack Alert! (Part II): See above in wxInsertChildInNotebook callback
641 // why this has to be done. NOTE: using gtk_widget_unparent here does not
642 // work as it seems to undo too much and will cause errors in the
643 // gtk_notebook_insert_page below, so instead just clear the parent by
644 // hand here.
645 win->m_widget->parent = NULL;
646
647 // don't receive switch page during addition
648 g_signal_handlers_disconnect_by_func (m_widget,
649 (gpointer) gtk_notebook_page_change_callback,
650 this);
651
652 if (m_themeEnabled)
653 win->SetThemeEnabled(TRUE);
654
655 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
656
657 wxGtkNotebookPage *nb_page = new wxGtkNotebookPage();
658
659 if ( position == GetPageCount() )
660 m_pagesData.Append( nb_page );
661 else
662 m_pagesData.Insert( position, nb_page );
663
664 m_pages.Insert(win, position);
665
666 nb_page->m_box = gtk_hbox_new( FALSE, 1 );
667 gtk_container_border_width( GTK_CONTAINER(nb_page->m_box), 2 );
668
669 g_signal_connect (win->m_widget, "size_allocate",
670 G_CALLBACK (gtk_page_size_callback), win);
671
672 gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position );
673
674 nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data;
675
676 /* set the label image */
677 nb_page->m_image = imageId;
678
679 if (imageId != -1)
680 {
681 wxASSERT( m_imageList != NULL );
682
683 const wxBitmap *bmp = m_imageList->GetBitmapPtr(imageId);
684 GdkPixmap *pixmap = bmp->GetPixmap();
685 GdkBitmap *mask = (GdkBitmap*) NULL;
686 if ( bmp->GetMask() )
687 {
688 mask = bmp->GetMask()->GetBitmap();
689 }
690
691 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
692
693 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
694
695 gtk_widget_show(pixmapwid);
696 }
697
698 /* set the label text */
699
700 nb_page->m_text = text;
701 if (nb_page->m_text.IsEmpty()) nb_page->m_text = wxT("");
702
703 nb_page->m_label = GTK_LABEL( gtk_label_new(wxGTK_CONV(nb_page->m_text)) );
704 gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding );
705
706 /* apply current style */
707 GtkRcStyle *style = CreateWidgetStyle();
708 if ( style )
709 {
710 gtk_widget_modify_style(GTK_WIDGET(nb_page->m_label), style);
711 gtk_rc_style_unref(style);
712 }
713
714 /* show the label */
715 gtk_widget_show( GTK_WIDGET(nb_page->m_label) );
716 if (select && (m_pagesData.GetCount() > 1))
717 {
718 SetSelection( position );
719 }
720
721 g_signal_connect (m_widget, "switch_page",
722 G_CALLBACK (gtk_notebook_page_change_callback), this);
723
724 InvalidateBestSize();
725 return TRUE;
726 }
727
728 // helper for HitTest(): check if the point lies inside the given widget which
729 // is the child of the notebook whose position and border size are passed as
730 // parameters
731 static bool
732 IsPointInsideWidget(const wxPoint& pt, GtkWidget *w,
733 gint x, gint y, gint border = 0)
734 {
735 return
736 (pt.x >= w->allocation.x - x - border) &&
737 (pt.x <= w->allocation.x - x + border + w->allocation.width) &&
738 (pt.y >= w->allocation.y - y - border) &&
739 (pt.y <= w->allocation.y - y + border + w->allocation.height);
740 }
741
742 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
743 {
744 const gint x = m_widget->allocation.x;
745 const gint y = m_widget->allocation.y;
746
747 const size_t count = GetPageCount();
748 size_t i = 0;
749
750 GtkNotebook * notebook = GTK_NOTEBOOK(m_widget);
751 if (gtk_notebook_get_scrollable(notebook));
752 i = g_list_position( notebook->children, notebook->first_tab );
753
754 for ( ; i < count; i++ )
755 {
756 wxGtkNotebookPage* nb_page = GetNotebookPage(i);
757 GtkWidget *box = nb_page->m_box;
758
759 const gint border = gtk_container_get_border_width(GTK_CONTAINER(box));
760
761 if ( IsPointInsideWidget(pt, box, x, y, border) )
762 {
763 // ok, we're inside this tab -- now find out where, if needed
764 if ( flags )
765 {
766 GtkWidget *pixmap = NULL;
767
768 GList *children = gtk_container_get_children(GTK_CONTAINER(box));
769 for ( GList *child = children; child; child = child->next )
770 {
771 if ( GTK_IS_PIXMAP(child->data) )
772 {
773 pixmap = GTK_WIDGET(child->data);
774 break;
775 }
776 }
777
778 if ( children )
779 g_list_free(children);
780
781 if ( pixmap && IsPointInsideWidget(pt, pixmap, x, y) )
782 {
783 *flags = wxNB_HITTEST_ONICON;
784 }
785 else if ( IsPointInsideWidget(pt, GTK_WIDGET(nb_page->m_label), x, y) )
786 {
787 *flags = wxNB_HITTEST_ONLABEL;
788 }
789 else
790 {
791 *flags = wxNB_HITTEST_ONITEM;
792 }
793 }
794
795 return i;
796 }
797 }
798
799 if ( flags )
800 *flags = wxNB_HITTEST_NOWHERE;
801
802 return wxNOT_FOUND;
803 }
804
805 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
806 {
807 if (event.IsWindowChange())
808 AdvanceSelection( event.GetDirection() );
809 else
810 event.Skip();
811 }
812
813 #if wxUSE_CONSTRAINTS
814
815 // override these 2 functions to do nothing: everything is done in OnSize
816 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
817 {
818 // don't set the sizes of the pages - their correct size is not yet known
819 wxControl::SetConstraintSizes(FALSE);
820 }
821
822 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
823 {
824 return TRUE;
825 }
826
827 #endif
828
829 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style)
830 {
831 gtk_widget_modify_style(m_widget, style);
832 size_t cnt = m_pagesData.GetCount();
833 for (size_t i = 0; i < cnt; i++)
834 gtk_widget_modify_style(GTK_WIDGET(GetNotebookPage(i)->m_label), style);
835 }
836
837 bool wxNotebook::IsOwnGtkWindow( GdkWindow *window )
838 {
839 return ((m_widget->window == window) ||
840 GTK_NOTEBOOK(m_widget)->event_window == window);
841 }
842
843 // static
844 wxVisualAttributes
845 wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
846 {
847 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new);
848 }
849
850 //-----------------------------------------------------------------------------
851 // wxNotebookEvent
852 //-----------------------------------------------------------------------------
853
854 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
855
856 #endif