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"
24 #include "wx/gtk/private.h"
25 #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 //-----------------------------------------------------------------------------
53 // VZ: this is rather ugly as we keep the pages themselves in an array (it
54 // allows us to have quite a few functions implemented in the base class)
55 // but the page data is kept in a separate list, so we must maintain them
56 // in sync manually... of course, the list had been there before the base
57 // class which explains it but it still would be nice to do something
60 class wxGtkNotebookPage
: public wxObject
66 m_page
= (GtkNotebookPage
*) NULL
;
67 m_box
= (GtkWidget
*) NULL
;
72 GtkNotebookPage
*m_page
;
74 GtkWidget
*m_box
; // in which the label and image are packed
77 #include "wx/listimpl.cpp"
78 WX_DEFINE_LIST(wxGtkNotebookPagesList
);
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
85 GtkNotebookPage
*WXUNUSED(page
),
87 wxNotebook
*notebook
)
89 static bool s_inPageChange
= FALSE
;
91 // are you trying to call SetSelection() from a notebook event handler?
93 wxCHECK_RET( !s_inPageChange
,
94 _T("gtk_notebook_page_change_callback reentered") );
96 s_inPageChange
= TRUE
;
98 wxapp_install_idle_handler();
100 int old
= notebook
->GetSelection();
102 wxNotebookEvent
eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
103 notebook
->GetId(), page
, old
);
104 eventChanging
.SetEventObject( notebook
);
106 if ( (notebook
->GetEventHandler()->ProcessEvent(eventChanging
)) &&
107 !eventChanging
.IsAllowed() )
109 /* program doesn't allow the page change */
110 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
),
113 else // change allowed
115 // make wxNotebook::GetSelection() return the correct (i.e. consistent
116 // with wxNotebookEvent::GetSelection()) value even though the page is
117 // not really changed in GTK+
118 notebook
->m_selection
= page
;
120 wxNotebookEvent
eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
121 notebook
->GetId(), page
, old
);
122 eventChanged
.SetEventObject( notebook
);
123 notebook
->GetEventHandler()->ProcessEvent( eventChanged
);
126 s_inPageChange
= FALSE
;
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
133 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
136 wxapp_install_idle_handler();
138 if ((win
->m_x
== alloc
->x
) &&
139 (win
->m_y
== alloc
->y
) &&
140 (win
->m_width
== alloc
->width
) &&
141 (win
->m_height
== alloc
->height
))
146 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
148 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
149 here in order to make repositioning after resizing to take effect. */
150 if ((gtk_major_version
== 1) &&
151 (gtk_minor_version
== 2) &&
152 (gtk_micro_version
< 6) &&
154 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
156 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
160 //-----------------------------------------------------------------------------
161 // "realize" from m_widget
162 //-----------------------------------------------------------------------------
165 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
168 wxapp_install_idle_handler();
170 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
171 here in order to make repositioning before showing to take effect. */
172 gtk_widget_queue_resize( win
->m_widget
);
177 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
181 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
184 wxapp_install_idle_handler();
186 if (!win
->m_hasVMT
) return FALSE
;
187 if (g_blockEventsOnDrag
) return FALSE
;
189 /* win is a control: tab can be propagated up */
190 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
192 int sel
= win
->GetSelection();
193 wxGtkNotebookPage
*page
= win
->GetNotebookPage(sel
);
194 wxCHECK_MSG( page
, FALSE
, _T("invalid selection in wxNotebook") );
196 wxNavigationKeyEvent event
;
197 event
.SetEventObject( win
);
198 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
199 event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
200 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
201 event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
202 event
.SetCurrentFocus( win
);
204 wxNotebookPage
*client
= win
->GetPage(sel
);
205 if ( !client
->GetEventHandler()->ProcessEvent( event
) )
210 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
217 //-----------------------------------------------------------------------------
218 // InsertChild callback for wxNotebook
219 //-----------------------------------------------------------------------------
221 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
223 /* we don't do anything here but pray */
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
230 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
232 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
233 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
236 void wxNotebook::Init()
238 m_imageList
= (wxImageList
*) NULL
;
239 m_pagesData
.DeleteContents( TRUE
);
241 m_themeEnabled
= TRUE
;
244 wxNotebook::wxNotebook()
249 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
250 const wxPoint
& pos
, const wxSize
& size
,
251 long style
, const wxString
& name
)
254 Create( parent
, id
, pos
, size
, style
, name
);
257 wxNotebook::~wxNotebook()
259 /* don't generate change page events any more */
260 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
261 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
266 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
267 const wxPoint
& pos
, const wxSize
& size
,
268 long style
, const wxString
& name
)
271 m_acceptsFocus
= TRUE
;
272 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
274 if (!PreCreation( parent
, pos
, size
) ||
275 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
277 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
282 m_widget
= gtk_notebook_new();
284 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
286 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
287 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
289 m_parent
->DoAddChild( this );
291 if (m_windowStyle
& wxNB_RIGHT
)
292 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
293 if (m_windowStyle
& wxNB_LEFT
)
294 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
295 if (m_windowStyle
& wxNB_BOTTOM
)
296 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
298 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
299 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
303 SetFont( parent
->GetFont() );
305 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
306 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
313 int wxNotebook::GetSelection() const
315 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
317 if ( m_selection
== -1 )
319 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
321 if (g_list_length(pages
) != 0)
323 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
325 gpointer cur
= notebook
->cur_page
;
328 wxConstCast(this, wxNotebook
)->m_selection
=
329 g_list_index( pages
, cur
);
337 wxString
wxNotebook::GetPageText( int page
) const
339 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
341 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
343 return nb_page
->m_text
;
348 int wxNotebook::GetPageImage( int page
) const
350 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
352 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
354 return nb_page
->m_image
;
359 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
361 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
363 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
365 return m_pagesData
.Item(page
)->GetData();
368 int wxNotebook::SetSelection( int page
)
370 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
372 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), -1, wxT("invalid notebook index") );
374 int selOld
= GetSelection();
376 // cache the selection
378 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
380 wxNotebookPage
*client
= GetPage(page
);
387 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
389 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
391 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
393 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
395 nb_page
->m_text
= text
;
397 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
402 bool wxNotebook::SetPageImage( int page
, int image
)
404 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
406 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
408 if (!nb_page
) return FALSE
;
410 /* Optimization posibility: return immediately if image unchanged.
411 * Not enabled because it may break existing (stupid) code that
412 * manipulates the imagelist to cycle images */
414 /* if (image == nb_page->m_image) return TRUE; */
416 /* For different cases:
417 1) no image -> no image
422 if (image
== -1 && nb_page
->m_image
== -1)
423 return TRUE
; /* Case 1): Nothing to do. */
425 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
427 if (nb_page
->m_image
!= -1)
429 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
431 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
434 if (GTK_IS_PIXMAP(child
->data
))
436 pixmapwid
= GTK_WIDGET(child
->data
);
442 /* We should have the pixmap widget now */
443 wxASSERT(pixmapwid
!= NULL
);
447 /* If there's no new widget, just remove the old from the box */
448 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
449 nb_page
->m_image
= -1;
451 return TRUE
; /* Case 2) */
455 /* Only cases 3) and 4) left */
456 wxASSERT( m_imageList
!= NULL
); /* Just in case */
458 /* Construct the new pixmap */
459 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
460 GdkPixmap
*pixmap
= bmp
->GetPixmap();
461 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
462 if ( bmp
->GetMask() )
464 mask
= bmp
->GetMask()->GetBitmap();
467 if (pixmapwid
== NULL
)
469 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
470 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
472 /* CHECKME: Are these pack flags okay? */
473 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
474 gtk_widget_show(pixmapwid
);
478 /* Case 4) Simply replace the pixmap */
479 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
482 nb_page
->m_image
= image
;
487 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
489 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
492 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
494 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
497 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
499 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
502 bool wxNotebook::DeleteAllPages()
504 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
506 while (m_pagesData
.GetCount() > 0)
507 DeletePage( m_pagesData
.GetCount()-1 );
509 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
511 return wxNotebookBase::DeleteAllPages();
514 bool wxNotebook::DeletePage( int page
)
516 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
517 // event so we have to store the selection internally
518 if ( m_selection
== -1 )
520 m_selection
= GetSelection();
521 if ( m_selection
== (int)m_pagesData
.GetCount() - 1 )
523 // the index will become invalid after the page is deleted
528 // it will call our DoRemovePage() to do the real work
529 return wxNotebookBase::DeletePage(page
);
532 wxNotebookPage
*wxNotebook::DoRemovePage( int page
)
534 wxNotebookPage
*client
= wxNotebookBase::DoRemovePage(page
);
538 gtk_widget_ref( client
->m_widget
);
539 gtk_widget_unrealize( client
->m_widget
);
540 gtk_widget_unparent( client
->m_widget
);
542 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
544 m_pagesData
.DeleteObject(GetNotebookPage(page
));
549 bool wxNotebook::InsertPage( int position
,
551 const wxString
& text
,
555 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
557 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
558 wxT("Can't add a page whose parent is not the notebook!") );
560 wxCHECK_MSG( position
>= 0 && position
<= GetPageCount(), FALSE
,
561 _T("invalid page index in wxNotebookPage::InsertPage()") );
563 /* don't receive switch page during addition */
564 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
565 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
568 win
->SetThemeEnabled(TRUE
);
570 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
572 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
574 if ( position
== GetPageCount() )
575 m_pagesData
.Append( page
);
577 m_pagesData
.Insert( m_pagesData
.Item( position
), page
);
579 m_pages
.Insert(win
, position
);
581 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
582 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
584 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
585 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
588 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
590 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
592 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
594 /* set the label image */
595 page
->m_image
= imageId
;
599 wxASSERT( m_imageList
!= NULL
);
601 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
602 GdkPixmap
*pixmap
= bmp
->GetPixmap();
603 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
604 if ( bmp
->GetMask() )
606 mask
= bmp
->GetMask()->GetBitmap();
609 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
611 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
613 gtk_widget_show(pixmapwid
);
616 /* set the label text */
618 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
620 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
621 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
624 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
626 if (select
&& (m_pagesData
.GetCount() > 1))
629 SetSelection( GetPageCount()-1 );
631 SetSelection( position
);
634 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
635 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
640 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
642 if (event
.IsWindowChange())
643 AdvanceSelection( event
.GetDirection() );
648 #if wxUSE_CONSTRAINTS
650 // override these 2 functions to do nothing: everything is done in OnSize
651 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
653 // don't set the sizes of the pages - their correct size is not yet known
654 wxControl::SetConstraintSizes(FALSE
);
657 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
664 void wxNotebook::ApplyWidgetStyle()
666 // TODO, font for labels etc
669 gtk_widget_set_style( m_widget
, m_widgetStyle
);
672 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
674 return ((m_widget
->window
== window
) ||
675 (NOTEBOOK_PANEL(m_widget
) == window
));
678 //-----------------------------------------------------------------------------
680 //-----------------------------------------------------------------------------
682 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)