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 // VZ: this is rather ugly as we keep the pages themselves in an array (it
64 // allows us to have quite a few functions implemented in the base class)
65 // but the page data is kept in a separate list, so we must maintain them
66 // in sync manually... of course, the list had been there before the base
67 // class which explains it but it still would be nice to do something
70 class wxGtkNotebookPage
: public wxObject
76 m_page
= (GtkNotebookPage
*) NULL
;
77 m_box
= (GtkWidget
*) NULL
;
82 GtkNotebookPage
*m_page
;
84 GtkWidget
*m_box
; // in which the label and image are packed
87 #include "wx/listimpl.cpp"
88 WX_DEFINE_LIST(wxGtkNotebookPagesList
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
95 GtkNotebookPage
*WXUNUSED(page
),
97 wxNotebook
*notebook
)
99 static bool s_inPageChange
= FALSE
;
101 // are you trying to call SetSelection() from a notebook event handler?
103 wxCHECK_RET( !s_inPageChange
,
104 _T("gtk_notebook_page_change_callback reentered") );
106 s_inPageChange
= TRUE
;
108 wxapp_install_idle_handler();
110 int old
= notebook
->GetSelection();
112 wxNotebookEvent
eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
113 notebook
->GetId(), page
, old
);
114 eventChanging
.SetEventObject( notebook
);
116 if ( (notebook
->GetEventHandler()->ProcessEvent(eventChanging
)) &&
117 !eventChanging
.IsAllowed() )
119 /* program doesn't allow the page change */
120 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
),
123 else // change allowed
125 // make wxNotebook::GetSelection() return the correct (i.e. consistent
126 // with wxNotebookEvent::GetSelection()) value even though the page is
127 // not really changed in GTK+
128 notebook
->m_selection
= page
;
130 wxNotebookEvent
eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
131 notebook
->GetId(), page
, old
);
132 eventChanged
.SetEventObject( notebook
);
133 notebook
->GetEventHandler()->ProcessEvent( eventChanged
);
136 s_inPageChange
= FALSE
;
139 //-----------------------------------------------------------------------------
141 //-----------------------------------------------------------------------------
143 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
146 wxapp_install_idle_handler();
148 if ((win
->m_x
== alloc
->x
) &&
149 (win
->m_y
== alloc
->y
) &&
150 (win
->m_width
== alloc
->width
) &&
151 (win
->m_height
== alloc
->height
))
156 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
158 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
159 here in order to make repositioning after resizing to take effect. */
160 if ((gtk_major_version
== 1) &&
161 (gtk_minor_version
== 2) &&
162 (gtk_micro_version
< 6) &&
164 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
166 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
170 //-----------------------------------------------------------------------------
171 // "realize" from m_widget
172 //-----------------------------------------------------------------------------
175 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
178 wxapp_install_idle_handler();
180 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
181 here in order to make repositioning before showing to take effect. */
182 gtk_widget_queue_resize( win
->m_widget
);
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
194 wxapp_install_idle_handler();
196 if (!win
->m_hasVMT
) return FALSE
;
197 if (g_blockEventsOnDrag
) return FALSE
;
199 /* win is a control: tab can be propagated up */
200 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
202 int sel
= win
->GetSelection();
203 wxGtkNotebookPage
*page
= win
->GetNotebookPage(sel
);
204 wxCHECK_MSG( page
, FALSE
, _T("invalid selection in wxNotebook") );
206 wxNavigationKeyEvent event
;
207 event
.SetEventObject( win
);
208 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
209 event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
210 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
211 event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
212 event
.SetCurrentFocus( win
);
214 wxNotebookPage
*client
= win
->GetPage(sel
);
215 if ( !client
->GetEventHandler()->ProcessEvent( event
) )
220 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
227 //-----------------------------------------------------------------------------
228 // InsertChild callback for wxNotebook
229 //-----------------------------------------------------------------------------
231 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
233 /* we don't do anything here but pray */
236 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
240 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
242 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
243 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
246 void wxNotebook::Init()
248 m_imageList
= (wxImageList
*) NULL
;
249 m_pagesData
.DeleteContents( TRUE
);
251 m_themeEnabled
= TRUE
;
254 wxNotebook::wxNotebook()
259 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
260 const wxPoint
& pos
, const wxSize
& size
,
261 long style
, const wxString
& name
)
264 Create( parent
, id
, pos
, size
, style
, name
);
267 wxNotebook::~wxNotebook()
269 /* don't generate change page events any more */
270 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
271 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
276 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
277 const wxPoint
& pos
, const wxSize
& size
,
278 long style
, const wxString
& name
)
281 m_acceptsFocus
= TRUE
;
282 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
284 if (!PreCreation( parent
, pos
, size
) ||
285 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
287 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
292 m_widget
= gtk_notebook_new();
295 debug_focus_in( m_widget
, wxT("wxNotebook::m_widget"), name
);
298 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
300 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
301 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
303 m_parent
->DoAddChild( this );
305 if (m_windowStyle
& wxNB_RIGHT
)
306 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
307 if (m_windowStyle
& wxNB_LEFT
)
308 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
309 if (m_windowStyle
& wxNB_BOTTOM
)
310 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
312 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
313 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
317 SetFont( parent
->GetFont() );
319 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
320 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
327 int wxNotebook::GetSelection() const
329 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
331 if ( m_selection
== -1 )
333 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
335 if (g_list_length(pages
) != 0)
337 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
339 gpointer cur
= notebook
->cur_page
;
342 wxConstCast(this, wxNotebook
)->m_selection
=
343 g_list_index( pages
, cur
);
351 wxString
wxNotebook::GetPageText( int page
) const
353 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
355 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
357 return nb_page
->m_text
;
362 int wxNotebook::GetPageImage( int page
) const
364 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
366 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
368 return nb_page
->m_image
;
373 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
375 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
377 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
379 return m_pagesData
.Item(page
)->GetData();
382 int wxNotebook::SetSelection( int page
)
384 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
386 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), -1, wxT("invalid notebook index") );
388 int selOld
= GetSelection();
390 // cache the selection
392 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
394 wxNotebookPage
*client
= GetPage(page
);
401 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
403 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
405 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
407 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
409 nb_page
->m_text
= text
;
411 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
416 bool wxNotebook::SetPageImage( int page
, int image
)
418 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
420 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
422 if (!nb_page
) return FALSE
;
424 /* Optimization posibility: return immediately if image unchanged.
425 * Not enabled because it may break existing (stupid) code that
426 * manipulates the imagelist to cycle images */
428 /* if (image == nb_page->m_image) return TRUE; */
430 /* For different cases:
431 1) no image -> no image
436 if (image
== -1 && nb_page
->m_image
== -1)
437 return TRUE
; /* Case 1): Nothing to do. */
439 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
441 if (nb_page
->m_image
!= -1)
443 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
445 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
448 if (GTK_IS_PIXMAP(child
->data
))
450 pixmapwid
= GTK_WIDGET(child
->data
);
456 /* We should have the pixmap widget now */
457 wxASSERT(pixmapwid
!= NULL
);
461 /* If there's no new widget, just remove the old from the box */
462 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
463 nb_page
->m_image
= -1;
465 return TRUE
; /* Case 2) */
469 /* Only cases 3) and 4) left */
470 wxASSERT( m_imageList
!= NULL
); /* Just in case */
472 /* Construct the new pixmap */
473 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
474 GdkPixmap
*pixmap
= bmp
->GetPixmap();
475 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
476 if ( bmp
->GetMask() )
478 mask
= bmp
->GetMask()->GetBitmap();
481 if (pixmapwid
== NULL
)
483 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
484 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
486 /* CHECKME: Are these pack flags okay? */
487 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
488 gtk_widget_show(pixmapwid
);
492 /* Case 4) Simply replace the pixmap */
493 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
496 nb_page
->m_image
= image
;
501 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
503 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
506 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
508 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
511 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
513 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
516 bool wxNotebook::DeleteAllPages()
518 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
520 while (m_pagesData
.GetCount() > 0)
521 DeletePage( m_pagesData
.GetCount()-1 );
523 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
525 return wxNotebookBase::DeleteAllPages();
528 bool wxNotebook::DeletePage( int page
)
530 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
531 // event so we have to store the selection internally
532 if ( m_selection
== -1 )
534 m_selection
= GetSelection();
535 if ( m_selection
== (int)m_pagesData
.GetCount() - 1 )
537 // the index will become invalid after the page is deleted
542 // it will call our DoRemovePage() to do the real work
543 return wxNotebookBase::DeletePage(page
);
546 wxNotebookPage
*wxNotebook::DoRemovePage( int page
)
548 wxNotebookPage
*client
= wxNotebookBase::DoRemovePage(page
);
552 gtk_widget_ref( client
->m_widget
);
553 gtk_widget_unrealize( client
->m_widget
);
554 gtk_widget_unparent( client
->m_widget
);
556 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
558 m_pagesData
.DeleteObject(GetNotebookPage(page
));
563 bool wxNotebook::InsertPage( int position
,
565 const wxString
& text
,
569 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
571 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
572 wxT("Can't add a page whose parent is not the notebook!") );
574 wxCHECK_MSG( position
>= 0 && position
<= GetPageCount(), FALSE
,
575 _T("invalid page index in wxNotebookPage::InsertPage()") );
577 /* don't receive switch page during addition */
578 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
579 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
582 win
->SetThemeEnabled(TRUE
);
584 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
586 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
588 if ( position
== GetPageCount() )
589 m_pagesData
.Append( page
);
591 m_pagesData
.Insert( m_pagesData
.Item( position
), page
);
593 m_pages
.Insert(win
, position
);
595 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
596 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
598 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
599 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
602 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
604 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
606 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
608 /* set the label image */
609 page
->m_image
= imageId
;
613 wxASSERT( m_imageList
!= NULL
);
615 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
616 GdkPixmap
*pixmap
= bmp
->GetPixmap();
617 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
618 if ( bmp
->GetMask() )
620 mask
= bmp
->GetMask()->GetBitmap();
623 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
625 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
627 gtk_widget_show(pixmapwid
);
630 /* set the label text */
632 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
634 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
635 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
638 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
640 if (select
&& (m_pagesData
.GetCount() > 1))
643 SetSelection( GetPageCount()-1 );
645 SetSelection( position
);
648 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
649 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
654 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
656 if (event
.IsWindowChange())
657 AdvanceSelection( event
.GetDirection() );
662 #if wxUSE_CONSTRAINTS
664 // override these 2 functions to do nothing: everything is done in OnSize
665 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
667 // don't set the sizes of the pages - their correct size is not yet known
668 wxControl::SetConstraintSizes(FALSE
);
671 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
678 void wxNotebook::ApplyWidgetStyle()
680 // TODO, font for labels etc
683 gtk_widget_set_style( m_widget
, m_widgetStyle
);
686 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
688 return ((m_widget
->window
== window
) ||
689 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
692 //-----------------------------------------------------------------------------
694 //-----------------------------------------------------------------------------
696 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)