Make everything compile with GTK_DISABLE_DEPRECATED declared.
[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 gint 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 gint
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 return FALSE;
189 }
190 }
191
192 //-----------------------------------------------------------------------------
193 // "key_press_event"
194 //-----------------------------------------------------------------------------
195
196 extern "C" {
197 static gint gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *notebook )
198 {
199 if (g_isIdle)
200 wxapp_install_idle_handler();
201
202 if (!notebook->m_hasVMT) return FALSE;
203 if (g_blockEventsOnDrag) return FALSE;
204
205 /* win is a control: tab can be propagated up */
206 if ((gdk_event->keyval == GDK_Left) || (gdk_event->keyval == GDK_Right))
207 {
208 int page;
209 int nMax = notebook->GetPageCount();
210 if ( nMax-- ) // decrement it to get the last valid index
211 {
212 int nSel = notebook->GetSelection();
213
214 // change selection wrapping if it becomes invalid
215 page = (gdk_event->keyval != GDK_Left) ? nSel == nMax ? 0
216 : nSel + 1
217 : nSel == 0 ? nMax
218 : nSel - 1;
219 }
220 else // notebook is empty, no next page
221 {
222 return FALSE;
223 }
224
225 // m_selection = page;
226 gtk_notebook_set_current_page( GTK_NOTEBOOK(widget), page );
227
228 g_signal_stop_emission_by_name (widget, "key_press_event");
229 return TRUE;
230 }
231
232 /* win is a control: tab can be propagated up */
233 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
234 {
235 int sel = notebook->GetSelection();
236 if (sel == -1)
237 return TRUE;
238 wxGtkNotebookPage *nb_page = notebook->GetNotebookPage(sel);
239 wxCHECK_MSG( nb_page, FALSE, _T("invalid selection in wxNotebook") );
240
241 wxNavigationKeyEvent event;
242 event.SetEventObject( notebook );
243 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
244 event.SetDirection( (gdk_event->keyval == GDK_Tab) );
245 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
246 event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ||
247 (gdk_event->keyval == GDK_Left) || (gdk_event->keyval == GDK_Right) );
248 event.SetCurrentFocus( notebook );
249
250 wxNotebookPage *client = notebook->GetPage(sel);
251 if ( !client->GetEventHandler()->ProcessEvent( event ) )
252 {
253 client->SetFocus();
254 }
255
256 g_signal_stop_emission_by_name (widget, "key_press_event");
257 return TRUE;
258 }
259
260 return FALSE;
261 }
262 }
263
264 //-----------------------------------------------------------------------------
265 // InsertChild callback for wxNotebook
266 //-----------------------------------------------------------------------------
267
268 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
269 {
270 // Hack Alert! (Part I): This sets the notebook as the parent of the child
271 // widget, and takes care of some details such as updating the state and
272 // style of the child to reflect its new location. We do this early
273 // because without it GetBestSize (which is used to set the initial size
274 // of controls if an explicit size is not given) will often report
275 // incorrect sizes since the widget's style context is not fully known.
276 // See bug #901694 for details
277 // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
278 gtk_widget_set_parent(child->m_widget, parent->m_widget);
279
280 // NOTE: This should be considered a temporary workaround until we can
281 // work out the details and implement delaying the setting of the initial
282 // size of widgets until the size is really needed.
283 }
284
285 //-----------------------------------------------------------------------------
286 // wxNotebook
287 //-----------------------------------------------------------------------------
288
289 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
290
291 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
292 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
293 END_EVENT_TABLE()
294
295 void wxNotebook::Init()
296 {
297 m_padding = 0;
298 m_inSwitchPage = FALSE;
299
300 m_imageList = (wxImageList *) NULL;
301 m_selection = -1;
302 m_themeEnabled = TRUE;
303 }
304
305 wxNotebook::wxNotebook()
306 {
307 Init();
308 }
309
310 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
311 const wxPoint& pos, const wxSize& size,
312 long style, const wxString& name )
313 {
314 Init();
315 Create( parent, id, pos, size, style, name );
316 }
317
318 wxNotebook::~wxNotebook()
319 {
320 DeleteAllPages();
321 }
322
323 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
324 const wxPoint& pos, const wxSize& size,
325 long style, const wxString& name )
326 {
327 m_needParent = TRUE;
328 m_acceptsFocus = TRUE;
329 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
330
331 if (!PreCreation( parent, pos, size ) ||
332 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
333 {
334 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
335 return FALSE;
336 }
337
338
339 m_widget = gtk_notebook_new();
340
341 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
342
343 g_signal_connect (m_widget, "switch_page",
344 G_CALLBACK (gtk_notebook_page_change_callback), this);
345
346 m_parent->DoAddChild( this );
347
348 if (m_windowStyle & wxBK_RIGHT)
349 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
350 if (m_windowStyle & wxBK_LEFT)
351 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
352 if (m_windowStyle & wxBK_BOTTOM)
353 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
354
355 g_signal_connect (m_widget, "key_press_event",
356 G_CALLBACK (gtk_notebook_key_press_callback), this);
357
358 PostCreation(size);
359
360 g_signal_connect (m_widget, "realize",
361 G_CALLBACK (gtk_notebook_realized_callback), this);
362
363 return TRUE;
364 }
365
366 int wxNotebook::GetSelection() const
367 {
368 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
369
370 if ( m_selection == -1 )
371 {
372 GList *nb_pages = GTK_NOTEBOOK(m_widget)->children;
373
374 if (g_list_length(nb_pages) != 0)
375 {
376 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
377
378 gpointer cur = notebook->cur_page;
379 if ( cur != NULL )
380 {
381 wxConstCast(this, wxNotebook)->m_selection =
382 g_list_index( nb_pages, cur );
383 }
384 }
385 }
386
387 return m_selection;
388 }
389
390 wxString wxNotebook::GetPageText( size_t page ) const
391 {
392 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") );
393
394 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
395 if (nb_page)
396 return nb_page->m_text;
397 else
398 return wxT("");
399 }
400
401 int wxNotebook::GetPageImage( size_t page ) const
402 {
403 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
404
405 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
406 if (nb_page)
407 return nb_page->m_image;
408 else
409 return -1;
410 }
411
412 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
413 {
414 wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") );
415
416 wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") );
417
418 return m_pagesData.Item(page)->GetData();
419 }
420
421 int wxNotebook::SetSelection( size_t page )
422 {
423 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
424
425 wxCHECK_MSG( page < m_pagesData.GetCount(), -1, wxT("invalid notebook index") );
426
427 int selOld = GetSelection();
428
429 // cache the selection
430 m_selection = page;
431 gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget), page );
432
433 wxNotebookPage *client = GetPage(page);
434 if ( client )
435 client->SetFocus();
436
437 return selOld;
438 }
439
440 bool wxNotebook::SetPageText( size_t page, const wxString &text )
441 {
442 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
443
444 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
445
446 wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") );
447
448 nb_page->m_text = text;
449
450 gtk_label_set_text( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) );
451
452 return TRUE;
453 }
454
455 bool wxNotebook::SetPageImage( size_t page, int image )
456 {
457 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
458
459 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
460
461 if (!nb_page) return FALSE;
462
463 /* Optimization posibility: return immediately if image unchanged.
464 * Not enabled because it may break existing (stupid) code that
465 * manipulates the imagelist to cycle images */
466
467 /* if (image == nb_page->m_image) return TRUE; */
468
469 /* For different cases:
470 1) no image -> no image
471 2) image -> no image
472 3) no image -> image
473 4) image -> image */
474
475 if (image == -1 && nb_page->m_image == -1)
476 return TRUE; /* Case 1): Nothing to do. */
477
478 GtkWidget *pixmapwid = (GtkWidget*) NULL;
479
480 if (nb_page->m_image != -1)
481 {
482 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
483
484 GList *child = gtk_container_get_children(GTK_CONTAINER(nb_page->m_box));
485 while (child)
486 {
487 if (GTK_IS_PIXMAP(child->data))
488 {
489 pixmapwid = GTK_WIDGET(child->data);
490 break;
491 }
492 child = child->next;
493 }
494
495 /* We should have the pixmap widget now */
496 wxASSERT(pixmapwid != NULL);
497
498 if (image == -1)
499 {
500 /* If there's no new widget, just remove the old from the box */
501 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
502 nb_page->m_image = -1;
503
504 return TRUE; /* Case 2) */
505 }
506 }
507
508 /* Only cases 3) and 4) left */
509 wxASSERT( m_imageList != NULL ); /* Just in case */
510
511 /* Construct the new pixmap */
512 const wxBitmap *bmp = m_imageList->GetBitmapPtr(image);
513 GdkPixmap *pixmap = bmp->GetPixmap();
514 GdkBitmap *mask = (GdkBitmap*) NULL;
515 if ( bmp->GetMask() )
516 {
517 mask = bmp->GetMask()->GetBitmap();
518 }
519
520 if (pixmapwid == NULL)
521 {
522 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
523 pixmapwid = gtk_pixmap_new (pixmap, mask );
524
525 /* CHECKME: Are these pack flags okay? */
526 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
527 gtk_widget_show(pixmapwid);
528 }
529 else
530 {
531 /* Case 4) Simply replace the pixmap */
532 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
533 }
534
535 nb_page->m_image = image;
536
537 return TRUE;
538 }
539
540 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
541 {
542 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
543 }
544
545 void wxNotebook::SetPadding( const wxSize &padding )
546 {
547 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
548
549 m_padding = padding.GetWidth();
550
551 int i;
552 for (i=0; i<int(GetPageCount()); i++)
553 {
554 wxGtkNotebookPage* nb_page = GetNotebookPage(i);
555 wxASSERT(nb_page != NULL);
556
557 if (nb_page->m_image != -1)
558 {
559 // gtk_box_set_child_packing sets padding on BOTH sides
560 // icon provides left padding, label provides center and right
561 int image = nb_page->m_image;
562 SetPageImage(i,-1);
563 SetPageImage(i,image);
564 }
565 wxASSERT(nb_page->m_label);
566 gtk_box_set_child_packing(GTK_BOX(nb_page->m_box),
567 GTK_WIDGET(nb_page->m_label),
568 FALSE, FALSE, m_padding, GTK_PACK_END);
569 }
570 }
571
572 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
573 {
574 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
575 }
576
577 bool wxNotebook::DeleteAllPages()
578 {
579 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
580
581 while (m_pagesData.GetCount() > 0)
582 DeletePage( m_pagesData.GetCount()-1 );
583
584 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
585
586 InvalidateBestSize();
587 return wxNotebookBase::DeleteAllPages();
588 }
589
590 wxNotebookPage *wxNotebook::DoRemovePage( size_t page )
591 {
592 if ( m_selection != -1 && (size_t)m_selection >= page )
593 {
594 // the index will become invalid after the page is deleted
595 m_selection = -1;
596 }
597
598 wxNotebookPage *client = wxNotebookBase::DoRemovePage(page);
599 if ( !client )
600 return NULL;
601
602 gtk_widget_ref( client->m_widget );
603 gtk_widget_unrealize( client->m_widget );
604 gtk_widget_unparent( client->m_widget );
605
606 // gtk_notebook_remove_page() sends "switch_page" signal with some strange
607 // new page index (when deleting selected page 0, new page is 1 although,
608 // clearly, the selection should stay 0), so suppress this
609 g_signal_handlers_disconnect_by_func (m_widget,
610 (gpointer) gtk_notebook_page_change_callback,
611 this);
612
613 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
614
615 g_signal_connect (m_widget, "switch_page",
616 G_CALLBACK (gtk_notebook_page_change_callback), this);
617
618 wxGtkNotebookPage* p = GetNotebookPage(page);
619 m_pagesData.DeleteObject(p);
620 delete p;
621
622 return client;
623 }
624
625 bool wxNotebook::InsertPage( size_t position,
626 wxNotebookPage* win,
627 const wxString& text,
628 bool select,
629 int imageId )
630 {
631 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
632
633 wxCHECK_MSG( win->GetParent() == this, FALSE,
634 wxT("Can't add a page whose parent is not the notebook!") );
635
636 wxCHECK_MSG( position <= GetPageCount(), FALSE,
637 _T("invalid page index in wxNotebookPage::InsertPage()") );
638
639 // Hack Alert! (Part II): See above in wxInsertChildInNotebook callback
640 // why this has to be done. NOTE: using gtk_widget_unparent here does not
641 // work as it seems to undo too much and will cause errors in the
642 // gtk_notebook_insert_page below, so instead just clear the parent by
643 // hand here.
644 win->m_widget->parent = NULL;
645
646 // don't receive switch page during addition
647 g_signal_handlers_disconnect_by_func (m_widget,
648 (gpointer) gtk_notebook_page_change_callback,
649 this);
650
651 if (m_themeEnabled)
652 win->SetThemeEnabled(TRUE);
653
654 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
655
656 wxGtkNotebookPage *nb_page = new wxGtkNotebookPage();
657
658 if ( position == GetPageCount() )
659 m_pagesData.Append( nb_page );
660 else
661 m_pagesData.Insert( position, nb_page );
662
663 m_pages.Insert(win, position);
664
665 nb_page->m_box = gtk_hbox_new( FALSE, 1 );
666 gtk_container_border_width( GTK_CONTAINER(nb_page->m_box), 2 );
667
668 g_signal_connect (win->m_widget, "size_allocate",
669 G_CALLBACK (gtk_page_size_callback), win);
670
671 gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position );
672
673 nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data;
674
675 /* set the label image */
676 nb_page->m_image = imageId;
677
678 if (imageId != -1)
679 {
680 wxASSERT( m_imageList != NULL );
681
682 const wxBitmap *bmp = m_imageList->GetBitmapPtr(imageId);
683 GdkPixmap *pixmap = bmp->GetPixmap();
684 GdkBitmap *mask = (GdkBitmap*) NULL;
685 if ( bmp->GetMask() )
686 {
687 mask = bmp->GetMask()->GetBitmap();
688 }
689
690 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
691
692 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
693
694 gtk_widget_show(pixmapwid);
695 }
696
697 /* set the label text */
698
699 nb_page->m_text = text;
700 if (nb_page->m_text.IsEmpty()) nb_page->m_text = wxT("");
701
702 nb_page->m_label = GTK_LABEL( gtk_label_new(wxGTK_CONV(nb_page->m_text)) );
703 gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding );
704
705 /* apply current style */
706 GtkRcStyle *style = CreateWidgetStyle();
707 if ( style )
708 {
709 gtk_widget_modify_style(GTK_WIDGET(nb_page->m_label), style);
710 gtk_rc_style_unref(style);
711 }
712
713 /* show the label */
714 gtk_widget_show( GTK_WIDGET(nb_page->m_label) );
715 if (select && (m_pagesData.GetCount() > 1))
716 {
717 SetSelection( position );
718 }
719
720 g_signal_connect (m_widget, "switch_page",
721 G_CALLBACK (gtk_notebook_page_change_callback), this);
722
723 InvalidateBestSize();
724 return TRUE;
725 }
726
727 // helper for HitTest(): check if the point lies inside the given widget which
728 // is the child of the notebook whose position and border size are passed as
729 // parameters
730 static bool
731 IsPointInsideWidget(const wxPoint& pt, GtkWidget *w,
732 gint x, gint y, gint border = 0)
733 {
734 return
735 (pt.x >= w->allocation.x - x - border) &&
736 (pt.x <= w->allocation.x - x + border + w->allocation.width) &&
737 (pt.y >= w->allocation.y - y - border) &&
738 (pt.y <= w->allocation.y - y + border + w->allocation.height);
739 }
740
741 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
742 {
743 const gint x = m_widget->allocation.x;
744 const gint y = m_widget->allocation.y;
745
746 const size_t count = GetPageCount();
747 size_t i = 0;
748
749 GtkNotebook * notebook = GTK_NOTEBOOK(m_widget);
750 if (gtk_notebook_get_scrollable(notebook));
751 i = g_list_position( notebook->children, notebook->first_tab );
752
753 for ( ; i < count; i++ )
754 {
755 wxGtkNotebookPage* nb_page = GetNotebookPage(i);
756 GtkWidget *box = nb_page->m_box;
757
758 const gint border = gtk_container_get_border_width(GTK_CONTAINER(box));
759
760 if ( IsPointInsideWidget(pt, box, x, y, border) )
761 {
762 // ok, we're inside this tab -- now find out where, if needed
763 if ( flags )
764 {
765 GtkWidget *pixmap = NULL;
766
767 GList *children = gtk_container_get_children(GTK_CONTAINER(box));
768 for ( GList *child = children; child; child = child->next )
769 {
770 if ( GTK_IS_PIXMAP(child->data) )
771 {
772 pixmap = GTK_WIDGET(child->data);
773 break;
774 }
775 }
776
777 if ( children )
778 g_list_free(children);
779
780 if ( pixmap && IsPointInsideWidget(pt, pixmap, x, y) )
781 {
782 *flags = wxNB_HITTEST_ONICON;
783 }
784 else if ( IsPointInsideWidget(pt, GTK_WIDGET(nb_page->m_label), x, y) )
785 {
786 *flags = wxNB_HITTEST_ONLABEL;
787 }
788 else
789 {
790 *flags = wxNB_HITTEST_ONITEM;
791 }
792 }
793
794 return i;
795 }
796 }
797
798 if ( flags )
799 *flags = wxNB_HITTEST_NOWHERE;
800
801 return wxNOT_FOUND;
802 }
803
804 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
805 {
806 if (event.IsWindowChange())
807 AdvanceSelection( event.GetDirection() );
808 else
809 event.Skip();
810 }
811
812 #if wxUSE_CONSTRAINTS
813
814 // override these 2 functions to do nothing: everything is done in OnSize
815 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
816 {
817 // don't set the sizes of the pages - their correct size is not yet known
818 wxControl::SetConstraintSizes(FALSE);
819 }
820
821 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
822 {
823 return TRUE;
824 }
825
826 #endif
827
828 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style)
829 {
830 gtk_widget_modify_style(m_widget, style);
831 size_t cnt = m_pagesData.GetCount();
832 for (size_t i = 0; i < cnt; i++)
833 gtk_widget_modify_style(GTK_WIDGET(GetNotebookPage(i)->m_label), style);
834 }
835
836 bool wxNotebook::IsOwnGtkWindow( GdkWindow *window )
837 {
838 return ((m_widget->window == window) ||
839 GTK_NOTEBOOK(m_widget)->event_window == window);
840 }
841
842 // static
843 wxVisualAttributes
844 wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
845 {
846 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new);
847 }
848
849 //-----------------------------------------------------------------------------
850 // wxNotebookEvent
851 //-----------------------------------------------------------------------------
852
853 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
854
855 #endif