1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "notebook.h"
14 #include "wx/notebook.h"
20 #include "wx/imaglist.h"
26 #include "wx/gtk/win_gtk.h"
27 #include <gdk/gdkkeysyms.h>
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern void wxapp_install_idle_handler();
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern bool g_blockEventsOnDrag
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
55 extern void debug_focus_in( GtkWidget
* widget
, const wxChar
* name
, const wxChar
*window
);
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 class wxGtkNotebookPage
: public wxObject
70 m_page
= (GtkNotebookPage
*) NULL
;
71 m_client
= (wxNotebookPage
*) NULL
;
72 m_box
= (GtkWidget
*) NULL
;
77 GtkNotebookPage
*m_page
;
79 wxNotebookPage
*m_client
;
80 GtkWidget
*m_box
; // in which the label and image are packed
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
88 GtkNotebookPage
*WXUNUSED(page
),
90 wxNotebook
*notebook
)
92 static bool s_inPageChange
= FALSE
;
94 // are you trying to call SetSelection() from a notebook event handler?
96 wxCHECK_RET( !s_inPageChange
,
97 _T("gtk_notebook_page_change_callback reentered") );
99 s_inPageChange
= TRUE
;
101 wxapp_install_idle_handler();
103 int old
= notebook
->GetSelection();
105 wxNotebookEvent
eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
106 notebook
->GetId(), page
, old
);
107 eventChanging
.SetEventObject( notebook
);
109 if ( (notebook
->GetEventHandler()->ProcessEvent(eventChanging
)) &&
110 !eventChanging
.IsAllowed() )
112 /* program doesn't allow the page change */
113 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
),
116 else // change allowed
118 // make wxNotebook::GetSelection() return the correct (i.e. consistent
119 // with wxNotebookEvent::GetSelection()) value even though the page is
120 // not really changed in GTK+
121 notebook
->m_selection
= page
;
123 wxNotebookEvent
eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
124 notebook
->GetId(), page
, old
);
125 eventChanged
.SetEventObject( notebook
);
126 notebook
->GetEventHandler()->ProcessEvent( eventChanged
);
129 s_inPageChange
= FALSE
;
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
136 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
139 wxapp_install_idle_handler();
141 if ((win
->m_x
== alloc
->x
) &&
142 (win
->m_y
== alloc
->y
) &&
143 (win
->m_width
== alloc
->width
) &&
144 (win
->m_height
== alloc
->height
))
149 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
151 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
152 here in order to make repositioning after resizing to take effect. */
153 if ((gtk_major_version
== 1) &&
154 (gtk_minor_version
== 2) &&
155 (gtk_micro_version
< 6) &&
157 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
159 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
163 //-----------------------------------------------------------------------------
164 // "realize" from m_widget
165 //-----------------------------------------------------------------------------
168 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
171 wxapp_install_idle_handler();
173 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
174 here in order to make repositioning before showing to take effect. */
175 gtk_widget_queue_resize( win
->m_widget
);
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
187 wxapp_install_idle_handler();
189 if (!win
->m_hasVMT
) return FALSE
;
190 if (g_blockEventsOnDrag
) return FALSE
;
192 /* win is a control: tab can be propagated up */
193 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
195 wxNode
*node
= win
->m_pages
.Nth( win
->GetSelection() );
196 if (!node
) return FALSE
;
198 wxGtkNotebookPage
*page
= (wxGtkNotebookPage
*) node
->Data();
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
);
207 if (!page
->m_client
->GetEventHandler()->ProcessEvent( event
))
209 page
->m_client
->SetFocus();
212 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
219 //-----------------------------------------------------------------------------
220 // InsertChild callback for wxNotebook
221 //-----------------------------------------------------------------------------
223 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
225 /* we don't do anything here but pray */
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
232 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
234 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
235 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
238 void wxNotebook::Init()
240 m_imageList
= (wxImageList
*) NULL
;
241 m_ownsImageList
= FALSE
;
242 m_pages
.DeleteContents( TRUE
);
244 m_themeEnabled
= TRUE
;
247 wxNotebook::wxNotebook()
252 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
253 const wxPoint
& pos
, const wxSize
& size
,
254 long style
, const wxString
& name
)
257 Create( parent
, id
, pos
, size
, style
, name
);
260 wxNotebook::~wxNotebook()
262 /* don't generate change page events any more */
263 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
264 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
267 if (m_ownsImageList
) delete m_imageList
;
270 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
271 const wxPoint
& pos
, const wxSize
& size
,
272 long style
, const wxString
& name
)
275 m_acceptsFocus
= TRUE
;
276 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
278 if (!PreCreation( parent
, pos
, size
) ||
279 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
281 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
286 m_widget
= gtk_notebook_new();
289 debug_focus_in( m_widget
, wxT("wxNotebook::m_widget"), name
);
292 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
294 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
295 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
297 m_parent
->DoAddChild( this );
299 if (m_windowStyle
& wxNB_RIGHT
)
300 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
301 if (m_windowStyle
& wxNB_LEFT
)
302 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
303 if (m_windowStyle
& wxNB_BOTTOM
)
304 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
306 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
307 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
311 SetFont( parent
->GetFont() );
313 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
314 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
321 int wxNotebook::GetSelection() const
323 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
325 if ( m_selection
== -1 )
327 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
329 if (g_list_length(pages
) != 0)
331 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
333 gpointer cur
= notebook
->cur_page
;
336 wxConstCast(this, wxNotebook
)->m_selection
=
337 g_list_index( pages
, cur
);
345 int wxNotebook::GetPageCount() const
347 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
350 int wxNotebook::GetRowCount() const
355 wxString
wxNotebook::GetPageText( int page
) const
357 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
359 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
361 return nb_page
->m_text
;
366 int wxNotebook::GetPageImage( int page
) const
368 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
370 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
372 return nb_page
->m_image
;
377 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
379 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
381 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
383 wxNode
*node
= m_pages
.Nth( page
);
385 return (wxGtkNotebookPage
*) node
->Data();
388 int wxNotebook::SetSelection( int page
)
390 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
392 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, wxT("invalid notebook index") );
394 int selOld
= GetSelection();
396 // cache the selection
398 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
400 wxGtkNotebookPage
* g_page
= GetNotebookPage( page
);
401 if (g_page
->m_client
)
402 g_page
->m_client
->SetFocus();
407 void wxNotebook::SetImageList( wxImageList
* imageList
)
409 if (m_ownsImageList
) delete m_imageList
;
410 m_imageList
= imageList
;
411 m_ownsImageList
= FALSE
;
414 void wxNotebook::AssignImageList( wxImageList
* imageList
)
416 SetImageList(imageList
);
417 m_ownsImageList
= TRUE
;
420 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
422 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
424 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
426 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
428 nb_page
->m_text
= text
;
430 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
435 bool wxNotebook::SetPageImage( int page
, int image
)
437 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
439 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
441 if (!nb_page
) return FALSE
;
443 /* Optimization posibility: return immediately if image unchanged.
444 * Not enabled because it may break existing (stupid) code that
445 * manipulates the imagelist to cycle images */
447 /* if (image == nb_page->m_image) return TRUE; */
449 /* For different cases:
450 1) no image -> no image
455 if (image
== -1 && nb_page
->m_image
== -1)
456 return TRUE
; /* Case 1): Nothing to do. */
458 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
460 if (nb_page
->m_image
!= -1)
462 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
464 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
467 if (GTK_IS_PIXMAP(child
->data
))
469 pixmapwid
= GTK_WIDGET(child
->data
);
475 /* We should have the pixmap widget now */
476 wxASSERT(pixmapwid
!= NULL
);
480 /* If there's no new widget, just remove the old from the box */
481 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
482 nb_page
->m_image
= -1;
484 return TRUE
; /* Case 2) */
488 /* Only cases 3) and 4) left */
489 wxASSERT( m_imageList
!= NULL
); /* Just in case */
491 /* Construct the new pixmap */
492 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
493 GdkPixmap
*pixmap
= bmp
->GetPixmap();
494 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
495 if ( bmp
->GetMask() )
497 mask
= bmp
->GetMask()->GetBitmap();
500 if (pixmapwid
== NULL
)
502 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
503 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
505 /* CHECKME: Are these pack flags okay? */
506 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
507 gtk_widget_show(pixmapwid
);
511 /* Case 4) Simply replace the pixmap */
512 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
515 nb_page
->m_image
= image
;
520 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
522 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
525 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
527 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
530 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
532 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
535 bool wxNotebook::DeleteAllPages()
537 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
539 while (m_pages
.GetCount() > 0)
540 DeletePage( m_pages
.GetCount()-1 );
545 bool wxNotebook::DeletePage( int page
)
547 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
548 wxCHECK_MSG( nb_page
, FALSE
, _T("invalid page in wxNotebook::DeletePage") );
550 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
551 // event so we have to store the selection internally
552 if ( m_selection
== -1 )
554 m_selection
= GetSelection();
555 if ( m_selection
== (int)m_pages
.GetCount() - 1 )
557 // the index will become invalid after the page is deleted
562 nb_page
->m_client
->Destroy();
563 m_pages
.DeleteObject( nb_page
);
568 wxNotebookPage
*wxNotebook::DoRemovePage( int page
)
570 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
572 wxCHECK_MSG( nb_page
, NULL
, _T("wxNotebook::RemovePage: invalid page") );
574 gtk_widget_ref( nb_page
->m_client
->m_widget
);
575 gtk_widget_unrealize( nb_page
->m_client
->m_widget
);
576 gtk_widget_unparent( nb_page
->m_client
->m_widget
);
578 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
580 wxNotebookPage
*pageRemoved
= (wxNotebookPage
*)m_pages
[page
];
581 m_pages
.DeleteObject( nb_page
);
586 bool wxNotebook::InsertPage( int position
, wxNotebookPage
* win
, const wxString
& text
,
587 bool select
, int imageId
)
589 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
591 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
592 wxT("Can't add a page whose parent is not the notebook!") );
594 /* don't receive switch page during addition */
595 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
596 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
599 win
->SetThemeEnabled(TRUE
);
601 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
603 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
606 m_pages
.Append( page
);
608 m_pages
.Insert( m_pages
.Nth( position
), page
);
610 page
->m_client
= win
;
612 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
613 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
615 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
616 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
619 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
621 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
623 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
625 /* set the label image */
626 page
->m_image
= imageId
;
630 wxASSERT( m_imageList
!= NULL
);
632 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
633 GdkPixmap
*pixmap
= bmp
->GetPixmap();
634 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
635 if ( bmp
->GetMask() )
637 mask
= bmp
->GetMask()->GetBitmap();
640 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
642 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
644 gtk_widget_show(pixmapwid
);
647 /* set the label text */
649 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
651 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
652 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
655 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
657 if (select
&& (m_pages
.GetCount() > 1))
660 SetSelection( GetPageCount()-1 );
662 SetSelection( position
);
665 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
666 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
671 bool wxNotebook::AddPage(wxNotebookPage
* win
, const wxString
& text
,
672 bool select
, int imageId
)
674 return InsertPage( -1, win
, text
, select
, imageId
);
677 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
679 if (event
.IsWindowChange())
680 AdvanceSelection( event
.GetDirection() );
685 #if wxUSE_CONSTRAINTS
687 // override these 2 functions to do nothing: everything is done in OnSize
688 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
690 // don't set the sizes of the pages - their correct size is not yet known
691 wxControl::SetConstraintSizes(FALSE
);
694 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
701 void wxNotebook::ApplyWidgetStyle()
703 // TODO, font for labels etc
706 gtk_widget_set_style( m_widget
, m_widgetStyle
);
709 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
711 return ((m_widget
->window
== window
) ||
712 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
715 //-----------------------------------------------------------------------------
717 //-----------------------------------------------------------------------------
719 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)