added wxBookCtrl::ChangeSelection() which is the same as SetSelection() but doesn...
[wxWidgets.git] / src / gtk1 / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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 #include "wx/gtk1/private.h"
30 #include "wx/gtk1/win_gtk.h"
31
32 #include <gdk/gdkkeysyms.h>
33
34 // ----------------------------------------------------------------------------
35 // events
36 // ----------------------------------------------------------------------------
37
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
40
41 //-----------------------------------------------------------------------------
42 // idle system
43 //-----------------------------------------------------------------------------
44
45 extern void wxapp_install_idle_handler();
46 extern bool g_isIdle;
47
48 //-----------------------------------------------------------------------------
49 // data
50 //-----------------------------------------------------------------------------
51
52 extern bool g_blockEventsOnDrag;
53
54 //-----------------------------------------------------------------------------
55 // wxGtkNotebookPage
56 //-----------------------------------------------------------------------------
57
58 // VZ: this is rather ugly as we keep the pages themselves in an array (it
59 // allows us to have quite a few functions implemented in the base class)
60 // but the page data is kept in a separate list, so we must maintain them
61 // in sync manually... of course, the list had been there before the base
62 // class which explains it but it still would be nice to do something
63 // about this one day
64
65 class wxGtkNotebookPage: public wxObject
66 {
67 public:
68 wxGtkNotebookPage()
69 {
70 m_image = -1;
71 m_page = (GtkNotebookPage *) NULL;
72 m_box = (GtkWidget *) NULL;
73 }
74
75 wxString m_text;
76 int m_image;
77 GtkNotebookPage *m_page;
78 GtkLabel *m_label;
79 GtkWidget *m_box; // in which the label and image are packed
80 };
81
82
83 #include "wx/listimpl.cpp"
84 WX_DEFINE_LIST(wxGtkNotebookPagesList)
85
86
87 //-----------------------------------------------------------------------------
88 // "switch_page"
89 //-----------------------------------------------------------------------------
90
91 extern "C" {
92 static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
93 GtkNotebookPage *WXUNUSED(page),
94 gint page,
95 wxNotebook *notebook )
96 {
97 // are you trying to call SetSelection() from a notebook event handler?
98 // you shouldn't!
99 wxCHECK_RET( !notebook->m_inSwitchPage,
100 _T("gtk_notebook_page_change_callback reentered") );
101
102 notebook->m_inSwitchPage = true;
103 if (g_isIdle)
104 wxapp_install_idle_handler();
105
106 int old = notebook->GetSelection();
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 gint
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 return FALSE;
199 }
200 }
201
202 //-----------------------------------------------------------------------------
203 // "key_press_event"
204 //-----------------------------------------------------------------------------
205
206 extern "C" {
207 static gint gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *notebook )
208 {
209 if (g_isIdle)
210 wxapp_install_idle_handler();
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_page( GTK_NOTEBOOK(widget), page );
237
238 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
239 return TRUE;
240 }
241
242 /* win is a control: tab can be propagated up */
243 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
244 {
245 int sel = notebook->GetSelection();
246 if (sel == -1)
247 return TRUE;
248 wxGtkNotebookPage *nb_page = notebook->GetNotebookPage(sel);
249 wxCHECK_MSG( nb_page, FALSE, _T("invalid selection in wxNotebook") );
250
251 wxNavigationKeyEvent event;
252 event.SetEventObject( notebook );
253 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
254 event.SetDirection( (gdk_event->keyval == GDK_Tab) );
255 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
256 event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ||
257 (gdk_event->keyval == GDK_Left) || (gdk_event->keyval == GDK_Right) );
258 event.SetCurrentFocus( notebook );
259
260 wxNotebookPage *client = notebook->GetPage(sel);
261 if ( !client->GetEventHandler()->ProcessEvent( event ) )
262 {
263 client->SetFocus();
264 }
265
266 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
267 return TRUE;
268 }
269
270 return FALSE;
271 }
272 }
273
274 //-----------------------------------------------------------------------------
275 // InsertChild callback for wxNotebook
276 //-----------------------------------------------------------------------------
277
278 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
279 {
280 // Hack Alert! (Part I): This sets the notebook as the parent of the child
281 // widget, and takes care of some details such as updating the state and
282 // style of the child to reflect its new location. We do this early
283 // because without it GetBestSize (which is used to set the initial size
284 // of controls if an explicit size is not given) will often report
285 // incorrect sizes since the widget's style context is not fully known.
286 // See bug #901694 for details
287 // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
288 gtk_widget_set_parent(child->m_widget, parent->m_widget);
289
290 // NOTE: This should be considered a temporary workaround until we can
291 // work out the details and implement delaying the setting of the initial
292 // size of widgets until the size is really needed.
293 }
294
295 //-----------------------------------------------------------------------------
296 // wxNotebook
297 //-----------------------------------------------------------------------------
298
299 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
300
301 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
302 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
303 END_EVENT_TABLE()
304
305 void wxNotebook::Init()
306 {
307 m_padding = 0;
308 m_inSwitchPage = false;
309
310 m_imageList = (wxImageList *) NULL;
311 m_selection = -1;
312 m_themeEnabled = true;
313 }
314
315 wxNotebook::wxNotebook()
316 {
317 Init();
318 }
319
320 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
321 const wxPoint& pos, const wxSize& size,
322 long style, const wxString& name )
323 {
324 Init();
325 Create( parent, id, pos, size, style, name );
326 }
327
328 wxNotebook::~wxNotebook()
329 {
330 DeleteAllPages();
331 }
332
333 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
334 const wxPoint& pos, const wxSize& size,
335 long style, const wxString& name )
336 {
337 m_needParent = true;
338 m_acceptsFocus = true;
339 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
340
341 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
342 style |= wxBK_TOP;
343
344 if (!PreCreation( parent, pos, size ) ||
345 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
346 {
347 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
348 return false;
349 }
350
351
352 m_widget = gtk_notebook_new();
353
354 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
355
356 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
357 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
358
359 m_parent->DoAddChild( this );
360
361 if (m_windowStyle & wxBK_RIGHT)
362 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
363 if (m_windowStyle & wxBK_LEFT)
364 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
365 if (m_windowStyle & wxBK_BOTTOM)
366 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
367
368 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
369 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
370
371 PostCreation(size);
372
373 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
374 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback), (gpointer) this );
375
376 return true;
377 }
378
379 int wxNotebook::GetSelection() const
380 {
381 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
382
383 if ( m_selection == -1 )
384 {
385 GList *nb_pages = GTK_NOTEBOOK(m_widget)->children;
386
387 if (g_list_length(nb_pages) != 0)
388 {
389 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
390
391 gpointer cur = notebook->cur_page;
392 if ( cur != NULL )
393 {
394 wxConstCast(this, wxNotebook)->m_selection =
395 g_list_index( nb_pages, cur );
396 }
397 }
398 }
399
400 return m_selection;
401 }
402
403 wxString wxNotebook::GetPageText( size_t page ) const
404 {
405 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid notebook") );
406
407 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
408 if (nb_page)
409 return nb_page->m_text;
410 else
411 return wxEmptyString;
412 }
413
414 int wxNotebook::GetPageImage( size_t page ) const
415 {
416 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
417
418 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
419 if (nb_page)
420 return nb_page->m_image;
421 else
422 return -1;
423 }
424
425 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
426 {
427 wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") );
428
429 wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") );
430
431 return m_pagesData.Item(page)->GetData();
432 }
433
434 int wxNotebook::DoSetSelection( size_t page, int flags )
435 {
436 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
437
438 wxCHECK_MSG( page < m_pagesData.GetCount(), -1, wxT("invalid notebook index") );
439
440 int selOld = GetSelection();
441
442 if ( !(flags & SetSelection_SendEvent) )
443 m_skipNextPageChangeEvent = true;
444
445 // cache the selection
446 m_selection = page;
447 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page );
448
449 #ifdef __WXDEBUG__
450 if ( !(flags & SetSelection_SendEvent) )
451 {
452 // gtk_notebook_set_current_page will emit the switch-page signal which will be
453 // caught by our gtk_notebook_page_change_callback which should have reset the
454 // flag to false:
455 wxASSERT(!m_skipNextPageChangeEvent);
456 }
457 #endif // __WXDEBUG__
458
459 wxNotebookPage *client = GetPage(page);
460 if ( client )
461 client->SetFocus();
462
463 return selOld;
464 }
465
466 bool wxNotebook::SetPageText( size_t page, const wxString &text )
467 {
468 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
469
470 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
471
472 wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") );
473
474 nb_page->m_text = text;
475
476 gtk_label_set( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) );
477
478 return true;
479 }
480
481 bool wxNotebook::SetPageImage( size_t page, int image )
482 {
483 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
484
485 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
486
487 if (!nb_page) return FALSE;
488
489 /* Optimization posibility: return immediately if image unchanged.
490 * Not enabled because it may break existing (stupid) code that
491 * manipulates the imagelist to cycle images */
492
493 /* if (image == nb_page->m_image) return true; */
494
495 /* For different cases:
496 1) no image -> no image
497 2) image -> no image
498 3) no image -> image
499 4) image -> image */
500
501 if (image == -1 && nb_page->m_image == -1)
502 return true; /* Case 1): Nothing to do. */
503
504 GtkWidget *pixmapwid = (GtkWidget*) NULL;
505
506 if (nb_page->m_image != -1)
507 {
508 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
509
510 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
511 while (child)
512 {
513 if (GTK_IS_PIXMAP(child->data))
514 {
515 pixmapwid = GTK_WIDGET(child->data);
516 break;
517 }
518 child = child->next;
519 }
520
521 /* We should have the pixmap widget now */
522 wxASSERT(pixmapwid != NULL);
523
524 if (image == -1)
525 {
526 /* If there's no new widget, just remove the old from the box */
527 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
528 nb_page->m_image = -1;
529
530 return true; /* Case 2) */
531 }
532 }
533
534 /* Only cases 3) and 4) left */
535 wxASSERT( m_imageList != NULL ); /* Just in case */
536
537 /* Construct the new pixmap */
538 const wxBitmap *bmp = m_imageList->GetBitmapPtr(image);
539 GdkPixmap *pixmap = bmp->GetPixmap();
540 GdkBitmap *mask = (GdkBitmap*) NULL;
541 if ( bmp->GetMask() )
542 {
543 mask = bmp->GetMask()->GetBitmap();
544 }
545
546 if (pixmapwid == NULL)
547 {
548 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
549 pixmapwid = gtk_pixmap_new (pixmap, mask );
550
551 /* CHECKME: Are these pack flags okay? */
552 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
553 gtk_widget_show(pixmapwid);
554 }
555 else
556 {
557 /* Case 4) Simply replace the pixmap */
558 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
559 }
560
561 nb_page->m_image = image;
562
563 return true;
564 }
565
566 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
567 {
568 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
569 }
570
571 void wxNotebook::SetPadding( const wxSize &padding )
572 {
573 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
574
575 m_padding = padding.GetWidth();
576
577 int i;
578 for (i=0; i<int(GetPageCount()); i++)
579 {
580 wxGtkNotebookPage* nb_page = GetNotebookPage(i);
581 wxASSERT(nb_page != NULL);
582
583 if (nb_page->m_image != -1)
584 {
585 // gtk_box_set_child_packing sets padding on BOTH sides
586 // icon provides left padding, label provides center and right
587 int image = nb_page->m_image;
588 SetPageImage(i,-1);
589 SetPageImage(i,image);
590 }
591 wxASSERT(nb_page->m_label);
592 gtk_box_set_child_packing(GTK_BOX(nb_page->m_box),
593 GTK_WIDGET(nb_page->m_label),
594 FALSE, FALSE, m_padding, GTK_PACK_END);
595 }
596 }
597
598 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
599 {
600 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
601 }
602
603 bool wxNotebook::DeleteAllPages()
604 {
605 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
606
607 while (m_pagesData.GetCount() > 0)
608 DeletePage( m_pagesData.GetCount()-1 );
609
610 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
611
612 InvalidateBestSize();
613 return wxNotebookBase::DeleteAllPages();
614 }
615
616 wxNotebookPage *wxNotebook::DoRemovePage( size_t page )
617 {
618 if ( m_selection != -1 && (size_t)m_selection >= page )
619 {
620 // the index will become invalid after the page is deleted
621 m_selection = -1;
622 }
623
624 wxNotebookPage *client = wxNotebookBase::DoRemovePage(page);
625 if ( !client )
626 return NULL;
627
628 gtk_widget_ref( client->m_widget );
629 gtk_widget_unrealize( client->m_widget );
630 gtk_widget_unparent( client->m_widget );
631
632 // gtk_notebook_remove_page() sends "switch_page" signal with some strange
633 // new page index (when deleting selected page 0, new page is 1 although,
634 // clearly, the selection should stay 0), so suppress this
635 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
636 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
637
638 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
639
640 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
641 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)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 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
673 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
674
675 if (m_themeEnabled)
676 win->SetThemeEnabled(true);
677
678 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
679
680 wxGtkNotebookPage *nb_page = new wxGtkNotebookPage();
681
682 if ( position == GetPageCount() )
683 m_pagesData.Append( nb_page );
684 else
685 m_pagesData.Insert( position, nb_page );
686
687 m_pages.Insert(win, position);
688
689 nb_page->m_box = gtk_hbox_new( FALSE, 1 );
690 gtk_container_border_width( GTK_CONTAINER(nb_page->m_box), 2 );
691
692 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
693 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
694
695 gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position );
696
697 nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data;
698
699 /* set the label image */
700 nb_page->m_image = imageId;
701
702 if (imageId != -1)
703 {
704 wxASSERT( m_imageList != NULL );
705
706 const wxBitmap *bmp = m_imageList->GetBitmapPtr(imageId);
707 GdkPixmap *pixmap = bmp->GetPixmap();
708 GdkBitmap *mask = (GdkBitmap*) NULL;
709 if ( bmp->GetMask() )
710 {
711 mask = bmp->GetMask()->GetBitmap();
712 }
713
714 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
715
716 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
717
718 gtk_widget_show(pixmapwid);
719 }
720
721 /* set the label text */
722
723 nb_page->m_text = text;
724 if (nb_page->m_text.empty()) nb_page->m_text = wxEmptyString;
725
726 nb_page->m_label = GTK_LABEL( gtk_label_new(wxGTK_CONV(nb_page->m_text)) );
727 gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding );
728
729 /* apply current style */
730 GtkRcStyle *style = CreateWidgetStyle();
731 if ( style )
732 {
733 gtk_widget_modify_style(GTK_WIDGET(nb_page->m_label), style);
734 gtk_rc_style_unref(style);
735 }
736
737 /* show the label */
738 gtk_widget_show( GTK_WIDGET(nb_page->m_label) );
739 if (select && (m_pagesData.GetCount() > 1))
740 {
741 SetSelection( position );
742 }
743
744 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
745 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
746
747 InvalidateBestSize();
748 return true;
749 }
750
751 // helper for HitTest(): check if the point lies inside the given widget which
752 // is the child of the notebook whose position and border size are passed as
753 // parameters
754 static bool
755 IsPointInsideWidget(const wxPoint& pt, GtkWidget *w,
756 gint x, gint y, gint border = 0)
757 {
758 return
759 (pt.x >= w->allocation.x - x - border) &&
760 (pt.x <= w->allocation.x - x + border + w->allocation.width) &&
761 (pt.y >= w->allocation.y - y - border) &&
762 (pt.y <= w->allocation.y - y + border + w->allocation.height);
763 }
764
765 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
766 {
767 const gint x = m_widget->allocation.x;
768 const gint y = m_widget->allocation.y;
769
770 const size_t count = GetPageCount();
771 size_t i = 0;
772
773 // MR: Code to fix HitTest index return when tabs are scrolled.
774 // No idea if it would work for GTK1
775 #if 0
776 GtkNotebook * notebook = GTK_NOTEBOOK(m_widget);
777 if (gtk_notebook_get_scrollable(notebook));
778 i = g_list_position( notebook->children, notebook->first_tab );
779 #endif
780
781 for ( ; i < count; i++ )
782 {
783 wxGtkNotebookPage* nb_page = GetNotebookPage(i);
784 GtkWidget *box = nb_page->m_box;
785
786 // VZ: don't know how to find the border width in GTK+ 1.2
787 const gint border = 0;
788 if ( IsPointInsideWidget(pt, box, x, y, border) )
789 {
790 // ok, we're inside this tab -- now find out where, if needed
791 if ( flags )
792 {
793 GtkWidget *pixmap = NULL;
794
795 GList *children = gtk_container_children(GTK_CONTAINER(box));
796 for ( GList *child = children; child; child = child->next )
797 {
798 if ( GTK_IS_PIXMAP(child->data) )
799 {
800 pixmap = GTK_WIDGET(child->data);
801 break;
802 }
803 }
804
805 if ( children )
806 g_list_free(children);
807
808 if ( pixmap && IsPointInsideWidget(pt, pixmap, x, y) )
809 {
810 *flags = wxBK_HITTEST_ONICON;
811 }
812 else if ( IsPointInsideWidget(pt, GTK_WIDGET(nb_page->m_label), x, y) )
813 {
814 *flags = wxBK_HITTEST_ONLABEL;
815 }
816 else
817 {
818 *flags = wxBK_HITTEST_ONITEM;
819 }
820 }
821
822 return i;
823 }
824 }
825
826 if ( flags )
827 *flags = wxBK_HITTEST_NOWHERE;
828
829 return wxNOT_FOUND;
830 }
831
832 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
833 {
834 if (event.IsWindowChange())
835 AdvanceSelection( event.GetDirection() );
836 else
837 event.Skip();
838 }
839
840 #if wxUSE_CONSTRAINTS
841
842 // override these 2 functions to do nothing: everything is done in OnSize
843 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
844 {
845 // don't set the sizes of the pages - their correct size is not yet known
846 wxControl::SetConstraintSizes(FALSE);
847 }
848
849 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
850 {
851 return true;
852 }
853
854 #endif
855
856 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style)
857 {
858 gtk_widget_modify_style(m_widget, style);
859 size_t cnt = m_pagesData.GetCount();
860 for (size_t i = 0; i < cnt; i++)
861 gtk_widget_modify_style(GTK_WIDGET(GetNotebookPage(i)->m_label), style);
862 }
863
864 bool wxNotebook::IsOwnGtkWindow( GdkWindow *window )
865 {
866 return ((m_widget->window == window) ||
867 (NOTEBOOK_PANEL(m_widget) == window));
868 }
869
870 // static
871 wxVisualAttributes
872 wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
873 {
874 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new);
875 }
876
877 //-----------------------------------------------------------------------------
878 // wxNotebookEvent
879 //-----------------------------------------------------------------------------
880
881 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
882
883 #endif