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