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