1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "notebook.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #include "wx/notebook.h"
23 #include "wx/imaglist.h"
26 #include "wx/bitmap.h"
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/win_gtk.h"
31 #include <gdk/gdkkeysyms.h>
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 extern void wxapp_install_idle_handler();
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 extern bool g_blockEventsOnDrag
;
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
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
64 class wxGtkNotebookPage
: public wxObject
70 m_page
= (GtkNotebookPage
*) NULL
;
71 m_box
= (GtkWidget
*) NULL
;
76 GtkNotebookPage
*m_page
;
78 GtkWidget
*m_box
; // in which the label and image are packed
81 #include "wx/listimpl.cpp"
82 WX_DEFINE_LIST(wxGtkNotebookPagesList
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
89 GtkNotebookPage
*WXUNUSED(page
),
91 wxNotebook
*notebook
)
93 // are you trying to call SetSelection() from a notebook event handler?
95 wxCHECK_RET( !notebook
->m_inSwitchPage
,
96 _T("gtk_notebook_page_change_callback reentered") );
98 notebook
->m_inSwitchPage
= TRUE
;
100 wxapp_install_idle_handler();
102 int old
= notebook
->GetSelection();
104 wxNotebookEvent
eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
105 notebook
->GetId(), page
, old
);
106 eventChanging
.SetEventObject( notebook
);
108 if ( (notebook
->GetEventHandler()->ProcessEvent(eventChanging
)) &&
109 !eventChanging
.IsAllowed() )
111 /* program doesn't allow the page change */
112 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
),
115 else // change allowed
117 // make wxNotebook::GetSelection() return the correct (i.e. consistent
118 // with wxNotebookEvent::GetSelection()) value even though the page is
119 // not really changed in GTK+
120 notebook
->m_selection
= page
;
122 wxNotebookEvent
eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
123 notebook
->GetId(), page
, old
);
124 eventChanged
.SetEventObject( notebook
);
125 notebook
->GetEventHandler()->ProcessEvent( eventChanged
);
128 notebook
->m_inSwitchPage
= FALSE
;
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
135 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
138 wxapp_install_idle_handler();
140 if ((win
->m_x
== alloc
->x
) &&
141 (win
->m_y
== alloc
->y
) &&
142 (win
->m_width
== alloc
->width
) &&
143 (win
->m_height
== alloc
->height
))
148 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
150 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
151 here in order to make repositioning after resizing to take effect. */
152 if ((gtk_major_version
== 1) &&
153 (gtk_minor_version
== 2) &&
154 (gtk_micro_version
< 6) &&
156 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
158 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
162 //-----------------------------------------------------------------------------
163 // "realize" from m_widget
164 //-----------------------------------------------------------------------------
167 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
170 wxapp_install_idle_handler();
172 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
173 here in order to make repositioning before showing to take effect. */
174 gtk_widget_queue_resize( win
->m_widget
);
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
186 wxapp_install_idle_handler();
188 if (!win
->m_hasVMT
) return FALSE
;
189 if (g_blockEventsOnDrag
) return FALSE
;
191 /* win is a control: tab can be propagated up */
192 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
194 int sel
= win
->GetSelection();
197 wxGtkNotebookPage
*nb_page
= win
->GetNotebookPage(sel
);
198 wxCHECK_MSG( nb_page
, FALSE
, _T("invalid selection in wxNotebook") );
200 wxNavigationKeyEvent event
;
201 event
.SetEventObject( win
);
202 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
203 event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
204 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
205 event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
206 event
.SetCurrentFocus( win
);
208 wxNotebookPage
*client
= win
->GetPage(sel
);
209 if ( !client
->GetEventHandler()->ProcessEvent( event
) )
214 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
221 //-----------------------------------------------------------------------------
222 // InsertChild callback for wxNotebook
223 //-----------------------------------------------------------------------------
225 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
227 /* we don't do anything here but pray */
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
236 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
237 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
240 void wxNotebook::Init()
243 m_inSwitchPage
= FALSE
;
245 m_imageList
= (wxImageList
*) NULL
;
247 m_themeEnabled
= TRUE
;
250 wxNotebook::wxNotebook()
255 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
256 const wxPoint
& pos
, const wxSize
& size
,
257 long style
, const wxString
& name
)
260 Create( parent
, id
, pos
, size
, style
, name
);
263 wxNotebook::~wxNotebook()
265 /* don't generate change page events any more */
266 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
267 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
272 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
273 const wxPoint
& pos
, const wxSize
& size
,
274 long style
, const wxString
& name
)
277 m_acceptsFocus
= TRUE
;
278 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
280 if (!PreCreation( parent
, pos
, size
) ||
281 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
283 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
288 m_widget
= gtk_notebook_new();
290 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
292 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
293 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
295 m_parent
->DoAddChild( this );
297 if (m_windowStyle
& wxNB_RIGHT
)
298 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
299 if (m_windowStyle
& wxNB_LEFT
)
300 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
301 if (m_windowStyle
& wxNB_BOTTOM
)
302 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
304 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
305 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
309 SetFont( parent
->GetFont() );
311 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
312 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
319 int wxNotebook::GetSelection() const
321 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
323 if ( m_selection
== -1 )
325 GList
*nb_pages
= GTK_NOTEBOOK(m_widget
)->children
;
327 if (g_list_length(nb_pages
) != 0)
329 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
331 gpointer cur
= notebook
->cur_page
;
334 wxConstCast(this, wxNotebook
)->m_selection
=
335 g_list_index( nb_pages
, cur
);
343 wxString
wxNotebook::GetPageText( size_t page
) const
345 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
347 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
349 return nb_page
->m_text
;
354 int wxNotebook::GetPageImage( size_t page
) const
356 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
358 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
360 return nb_page
->m_image
;
365 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
367 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
369 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
371 return m_pagesData
.Item(page
)->GetData();
374 int wxNotebook::SetSelection( size_t page
)
376 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
378 wxCHECK_MSG( page
< m_pagesData
.GetCount(), -1, wxT("invalid notebook index") );
380 int selOld
= GetSelection();
382 // cache the selection
384 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
386 wxNotebookPage
*client
= GetPage(page
);
393 bool wxNotebook::SetPageText( size_t page
, const wxString
&text
)
395 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
397 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
399 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
401 nb_page
->m_text
= text
;
403 gtk_label_set( nb_page
->m_label
, wxGTK_CONV( nb_page
->m_text
) );
408 bool wxNotebook::SetPageImage( size_t page
, int image
)
410 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
412 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
414 if (!nb_page
) return FALSE
;
416 /* Optimization posibility: return immediately if image unchanged.
417 * Not enabled because it may break existing (stupid) code that
418 * manipulates the imagelist to cycle images */
420 /* if (image == nb_page->m_image) return TRUE; */
422 /* For different cases:
423 1) no image -> no image
428 if (image
== -1 && nb_page
->m_image
== -1)
429 return TRUE
; /* Case 1): Nothing to do. */
431 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
433 if (nb_page
->m_image
!= -1)
435 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
437 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
440 if (GTK_IS_PIXMAP(child
->data
))
442 pixmapwid
= GTK_WIDGET(child
->data
);
448 /* We should have the pixmap widget now */
449 wxASSERT(pixmapwid
!= NULL
);
453 /* If there's no new widget, just remove the old from the box */
454 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
455 nb_page
->m_image
= -1;
457 return TRUE
; /* Case 2) */
461 /* Only cases 3) and 4) left */
462 wxASSERT( m_imageList
!= NULL
); /* Just in case */
464 /* Construct the new pixmap */
465 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
466 GdkPixmap
*pixmap
= bmp
->GetPixmap();
467 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
468 if ( bmp
->GetMask() )
470 mask
= bmp
->GetMask()->GetBitmap();
473 if (pixmapwid
== NULL
)
475 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
476 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
478 /* CHECKME: Are these pack flags okay? */
479 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
480 gtk_widget_show(pixmapwid
);
484 /* Case 4) Simply replace the pixmap */
485 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
488 nb_page
->m_image
= image
;
493 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
495 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
498 void wxNotebook::SetPadding( const wxSize
&padding
)
500 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
502 m_padding
= padding
.GetWidth();
505 for (i
=0; i
<int(GetPageCount()); i
++)
507 wxGtkNotebookPage
* nb_page
= GetNotebookPage(i
);
508 wxASSERT(nb_page
!= NULL
);
510 if (nb_page
->m_image
!= -1)
512 // gtk_box_set_child_packing sets padding on BOTH sides
513 // icon provides left padding, label provides center and right
514 int image
= nb_page
->m_image
;
516 SetPageImage(i
,image
);
518 wxASSERT(nb_page
->m_label
);
519 gtk_box_set_child_packing(GTK_BOX(nb_page
->m_box
),
520 GTK_WIDGET(nb_page
->m_label
),
521 FALSE
, FALSE
, m_padding
, GTK_PACK_END
);
525 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
527 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
530 bool wxNotebook::DeleteAllPages()
532 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
534 while (m_pagesData
.GetCount() > 0)
535 DeletePage( m_pagesData
.GetCount()-1 );
537 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
539 return wxNotebookBase::DeleteAllPages();
542 bool wxNotebook::DeletePage( size_t page
)
544 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
545 // event so we have to store the selection internally
546 if ( m_selection
== -1 )
548 m_selection
= GetSelection();
549 if ( m_selection
== (int)m_pagesData
.GetCount() - 1 )
551 // the index will become invalid after the page is deleted
556 // it will call our DoRemovePage() to do the real work
557 return wxNotebookBase::DeletePage(page
);
560 wxNotebookPage
*wxNotebook::DoRemovePage( size_t page
)
562 wxNotebookPage
*client
= wxNotebookBase::DoRemovePage(page
);
566 gtk_widget_ref( client
->m_widget
);
567 gtk_widget_unrealize( client
->m_widget
);
568 gtk_widget_unparent( client
->m_widget
);
570 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
572 wxGtkNotebookPage
* p
= GetNotebookPage(page
);
573 m_pagesData
.DeleteObject(p
);
579 bool wxNotebook::InsertPage( size_t position
,
581 const wxString
& text
,
585 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
587 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
588 wxT("Can't add a page whose parent is not the notebook!") );
590 wxCHECK_MSG( position
<= GetPageCount(), FALSE
,
591 _T("invalid page index in wxNotebookPage::InsertPage()") );
593 /* don't receive switch page during addition */
594 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
595 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
598 win
->SetThemeEnabled(TRUE
);
600 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
602 wxGtkNotebookPage
*nb_page
= new wxGtkNotebookPage();
604 if ( position
== GetPageCount() )
605 m_pagesData
.Append( nb_page
);
607 m_pagesData
.Insert( m_pagesData
.Item( position
), nb_page
);
609 m_pages
.Insert(win
, position
);
611 nb_page
->m_box
= gtk_hbox_new( FALSE
, 1 );
612 gtk_container_border_width( GTK_CONTAINER(nb_page
->m_box
), 2 );
614 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
615 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
618 // On VMS position is unsigned and thus always positive
620 gtk_notebook_append_page( notebook
, win
->m_widget
, nb_page
->m_box
);
623 gtk_notebook_insert_page( notebook
, win
->m_widget
, nb_page
->m_box
, position
);
625 nb_page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
627 /* set the label image */
628 nb_page
->m_image
= imageId
;
632 wxASSERT( m_imageList
!= NULL
);
634 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
635 GdkPixmap
*pixmap
= bmp
->GetPixmap();
636 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
637 if ( bmp
->GetMask() )
639 mask
= bmp
->GetMask()->GetBitmap();
642 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
644 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
646 gtk_widget_show(pixmapwid
);
649 /* set the label text */
650 nb_page
->m_text
= text
;
651 if (nb_page
->m_text
.IsEmpty()) nb_page
->m_text
= wxT("");
653 nb_page
->m_label
= GTK_LABEL( gtk_label_new(nb_page
->m_text
.mbc_str()) );
654 gtk_box_pack_end( GTK_BOX(nb_page
->m_box
), GTK_WIDGET(nb_page
->m_label
), FALSE
, FALSE
, m_padding
);
657 gtk_widget_show( GTK_WIDGET(nb_page
->m_label
) );
658 if (select
&& (m_pagesData
.GetCount() > 1))
661 // On VMS position is unsigned and thus always positive
663 SetSelection( GetPageCount()-1 );
666 SetSelection( position
);
669 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
670 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
675 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
677 if (event
.IsWindowChange())
678 AdvanceSelection( event
.GetDirection() );
683 #if wxUSE_CONSTRAINTS
685 // override these 2 functions to do nothing: everything is done in OnSize
686 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
688 // don't set the sizes of the pages - their correct size is not yet known
689 wxControl::SetConstraintSizes(FALSE
);
692 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
699 void wxNotebook::ApplyWidgetStyle()
701 // TODO, font for labels etc
704 gtk_widget_set_style( m_widget
, m_widgetStyle
);
707 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
709 return ((m_widget
->window
== window
) ||
710 (NOTEBOOK_PANEL(m_widget
) == window
));
713 //-----------------------------------------------------------------------------
715 //-----------------------------------------------------------------------------
717 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)