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::AdvanceSelection( bool forward
)
409 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
411 int max
= GetPageCount();
414 // nothing to do with empty notebook
418 int sel
= GetSelection();
421 SetSelection( sel
== max
- 1 ? 0 : sel
+ 1 );
423 SetSelection( sel
== 0 ? max
- 1 : sel
- 1 );
426 void wxNotebook::SetImageList( wxImageList
* imageList
)
428 if (m_ownsImageList
) delete m_imageList
;
429 m_imageList
= imageList
;
430 m_ownsImageList
= FALSE
;
433 void wxNotebook::AssignImageList( wxImageList
* imageList
)
435 SetImageList(imageList
);
436 m_ownsImageList
= TRUE
;
439 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
441 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
443 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
445 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
447 nb_page
->m_text
= text
;
449 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
454 bool wxNotebook::SetPageImage( int page
, int image
)
456 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
458 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
460 if (!nb_page
) return FALSE
;
462 /* Optimization posibility: return immediately if image unchanged.
463 * Not enabled because it may break existing (stupid) code that
464 * manipulates the imagelist to cycle images */
466 /* if (image == nb_page->m_image) return TRUE; */
468 /* For different cases:
469 1) no image -> no image
474 if (image
== -1 && nb_page
->m_image
== -1)
475 return TRUE
; /* Case 1): Nothing to do. */
477 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
479 if (nb_page
->m_image
!= -1)
481 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
483 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
486 if (GTK_IS_PIXMAP(child
->data
))
488 pixmapwid
= GTK_WIDGET(child
->data
);
494 /* We should have the pixmap widget now */
495 wxASSERT(pixmapwid
!= NULL
);
499 /* If there's no new widget, just remove the old from the box */
500 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
501 nb_page
->m_image
= -1;
503 return TRUE
; /* Case 2) */
507 /* Only cases 3) and 4) left */
508 wxASSERT( m_imageList
!= NULL
); /* Just in case */
510 /* Construct the new pixmap */
511 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
512 GdkPixmap
*pixmap
= bmp
->GetPixmap();
513 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
514 if ( bmp
->GetMask() )
516 mask
= bmp
->GetMask()->GetBitmap();
519 if (pixmapwid
== NULL
)
521 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
522 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
524 /* CHECKME: Are these pack flags okay? */
525 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
526 gtk_widget_show(pixmapwid
);
530 /* Case 4) Simply replace the pixmap */
531 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
534 nb_page
->m_image
= image
;
539 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
541 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
544 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
546 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
549 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
551 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
554 bool wxNotebook::DeleteAllPages()
556 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
558 while (m_pages
.GetCount() > 0)
559 DeletePage( m_pages
.GetCount()-1 );
564 bool wxNotebook::DeletePage( int page
)
566 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
567 wxCHECK_MSG( nb_page
, FALSE
, _T("invalid page in wxNotebook::DeletePage") );
569 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
570 // event so we have to store the selection internally
571 if ( m_selection
== -1 )
573 m_selection
= GetSelection();
574 if ( m_selection
== (int)m_pages
.GetCount() - 1 )
576 // the index will become invalid after the page is deleted
581 nb_page
->m_client
->Destroy();
582 m_pages
.DeleteObject( nb_page
);
587 bool wxNotebook::RemovePage( int page
)
589 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
591 wxCHECK_MSG( nb_page
, FALSE
, _T("wxNotebook::RemovePage: invalid page") );
593 gtk_widget_ref( nb_page
->m_client
->m_widget
);
594 gtk_widget_unrealize( nb_page
->m_client
->m_widget
);
595 gtk_widget_unparent( nb_page
->m_client
->m_widget
);
597 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
599 m_pages
.DeleteObject( nb_page
);
604 bool wxNotebook::InsertPage( int position
, wxNotebookPage
* win
, const wxString
& text
,
605 bool select
, int imageId
)
607 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
609 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
610 wxT("Can't add a page whose parent is not the notebook!") );
612 /* don't receive switch page during addition */
613 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
614 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
617 win
->SetThemeEnabled(TRUE
);
619 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
621 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
624 m_pages
.Append( page
);
626 m_pages
.Insert( m_pages
.Nth( position
), page
);
628 page
->m_client
= win
;
630 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
631 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
633 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
634 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
637 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
639 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
641 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
643 /* set the label image */
644 page
->m_image
= imageId
;
648 wxASSERT( m_imageList
!= NULL
);
650 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
651 GdkPixmap
*pixmap
= bmp
->GetPixmap();
652 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
653 if ( bmp
->GetMask() )
655 mask
= bmp
->GetMask()->GetBitmap();
658 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
660 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
662 gtk_widget_show(pixmapwid
);
665 /* set the label text */
667 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
669 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
670 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
673 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
675 if (select
&& (m_pages
.GetCount() > 1))
678 SetSelection( GetPageCount()-1 );
680 SetSelection( position
);
683 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
684 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
689 bool wxNotebook::AddPage(wxNotebookPage
* win
, const wxString
& text
,
690 bool select
, int imageId
)
692 return InsertPage( -1, win
, text
, select
, imageId
);
695 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
697 if (event
.IsWindowChange())
698 AdvanceSelection( event
.GetDirection() );
703 wxNotebookPage
*wxNotebook::GetPage( int page
) const
705 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, wxT("invalid notebook") );
707 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
709 return (wxNotebookPage
*) NULL
;
711 return nb_page
->m_client
;
714 #if wxUSE_CONSTRAINTS
716 // override these 2 functions to do nothing: everything is done in OnSize
717 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
719 // don't set the sizes of the pages - their correct size is not yet known
720 wxControl::SetConstraintSizes(FALSE
);
723 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
730 void wxNotebook::ApplyWidgetStyle()
732 // TODO, font for labels etc
735 gtk_widget_set_style( m_widget
, m_widgetStyle
);
738 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
740 return ((m_widget
->window
== window
) ||
741 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
744 //-----------------------------------------------------------------------------
746 //-----------------------------------------------------------------------------
748 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)