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