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"
23 #include "wx/bitmap.h"
25 #include "wx/gtk/private.h"
26 #include "wx/gtk/win_gtk.h"
28 #include <gdk/gdkkeysyms.h>
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
)
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
)
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern void wxapp_install_idle_handler();
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 extern bool g_blockEventsOnDrag
;
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 // VZ: this is rather ugly as we keep the pages themselves in an array (it
55 // allows us to have quite a few functions implemented in the base class)
56 // but the page data is kept in a separate list, so we must maintain them
57 // in sync manually... of course, the list had been there before the base
58 // class which explains it but it still would be nice to do something
61 class wxGtkNotebookPage
: public wxObject
67 m_page
= (GtkNotebookPage
*) NULL
;
68 m_box
= (GtkWidget
*) NULL
;
73 GtkNotebookPage
*m_page
;
75 GtkWidget
*m_box
; // in which the label and image are packed
78 #include "wx/listimpl.cpp"
79 WX_DEFINE_LIST(wxGtkNotebookPagesList
);
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
86 GtkNotebookPage
*WXUNUSED(page
),
88 wxNotebook
*notebook
)
90 // are you trying to call SetSelection() from a notebook event handler?
92 wxCHECK_RET( !notebook
->m_inSwitchPage
,
93 _T("gtk_notebook_page_change_callback reentered") );
95 notebook
->m_inSwitchPage
= TRUE
;
97 wxapp_install_idle_handler();
99 int old
= notebook
->GetSelection();
101 wxNotebookEvent
eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
102 notebook
->GetId(), page
, old
);
103 eventChanging
.SetEventObject( notebook
);
105 if ( (notebook
->GetEventHandler()->ProcessEvent(eventChanging
)) &&
106 !eventChanging
.IsAllowed() )
108 /* program doesn't allow the page change */
109 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
),
112 else // change allowed
114 // make wxNotebook::GetSelection() return the correct (i.e. consistent
115 // with wxNotebookEvent::GetSelection()) value even though the page is
116 // not really changed in GTK+
117 notebook
->m_selection
= page
;
119 wxNotebookEvent
eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
120 notebook
->GetId(), page
, old
);
121 eventChanged
.SetEventObject( notebook
);
122 notebook
->GetEventHandler()->ProcessEvent( eventChanged
);
125 notebook
->m_inSwitchPage
= FALSE
;
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
135 wxapp_install_idle_handler();
137 if ((win
->m_x
== alloc
->x
) &&
138 (win
->m_y
== alloc
->y
) &&
139 (win
->m_width
== alloc
->width
) &&
140 (win
->m_height
== alloc
->height
))
145 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
147 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
148 here in order to make repositioning after resizing to take effect. */
149 if ((gtk_major_version
== 1) &&
150 (gtk_minor_version
== 2) &&
151 (gtk_micro_version
< 6) &&
153 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
155 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
159 //-----------------------------------------------------------------------------
160 // "realize" from m_widget
161 //-----------------------------------------------------------------------------
164 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
167 wxapp_install_idle_handler();
169 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
170 here in order to make repositioning before showing to take effect. */
171 gtk_widget_queue_resize( win
->m_widget
);
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
180 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
183 wxapp_install_idle_handler();
185 if (!win
->m_hasVMT
) return FALSE
;
186 if (g_blockEventsOnDrag
) return FALSE
;
188 /* win is a control: tab can be propagated up */
189 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
191 int sel
= win
->GetSelection();
194 wxGtkNotebookPage
*nb_page
= win
->GetNotebookPage(sel
);
195 wxCHECK_MSG( nb_page
, FALSE
, _T("invalid selection in wxNotebook") );
197 wxNavigationKeyEvent event
;
198 event
.SetEventObject( win
);
199 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
200 event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
201 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
202 event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
203 event
.SetCurrentFocus( win
);
205 wxNotebookPage
*client
= win
->GetPage(sel
);
206 if ( !client
->GetEventHandler()->ProcessEvent( event
) )
211 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
218 //-----------------------------------------------------------------------------
219 // InsertChild callback for wxNotebook
220 //-----------------------------------------------------------------------------
222 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
224 /* we don't do anything here but pray */
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
233 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
234 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
237 void wxNotebook::Init()
240 m_inSwitchPage
= FALSE
;
242 m_imageList
= (wxImageList
*) NULL
;
243 m_pagesData
.DeleteContents( TRUE
);
245 m_themeEnabled
= TRUE
;
248 wxNotebook::wxNotebook()
253 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
254 const wxPoint
& pos
, const wxSize
& size
,
255 long style
, const wxString
& name
)
258 Create( parent
, id
, pos
, size
, style
, name
);
261 wxNotebook::~wxNotebook()
263 /* don't generate change page events any more */
264 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
265 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
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();
288 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
290 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
291 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
293 m_parent
->DoAddChild( this );
295 if (m_windowStyle
& wxNB_RIGHT
)
296 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
297 if (m_windowStyle
& wxNB_LEFT
)
298 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
299 if (m_windowStyle
& wxNB_BOTTOM
)
300 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
302 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
303 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
307 SetFont( parent
->GetFont() );
309 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
310 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
317 int wxNotebook::GetSelection() const
319 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
321 if ( m_selection
== -1 )
323 GList
*nb_pages
= GTK_NOTEBOOK(m_widget
)->children
;
325 if (g_list_length(nb_pages
) != 0)
327 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
329 gpointer cur
= notebook
->cur_page
;
332 wxConstCast(this, wxNotebook
)->m_selection
=
333 g_list_index( nb_pages
, cur
);
341 wxString
wxNotebook::GetPageText( int page
) const
343 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
345 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
347 return nb_page
->m_text
;
352 int wxNotebook::GetPageImage( int page
) const
354 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
356 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
358 return nb_page
->m_image
;
363 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
365 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
367 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
369 return m_pagesData
.Item(page
)->GetData();
372 int wxNotebook::SetSelection( int page
)
374 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
376 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), -1, wxT("invalid notebook index") );
378 int selOld
= GetSelection();
380 // cache the selection
382 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
384 wxNotebookPage
*client
= GetPage(page
);
391 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
393 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
395 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
397 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
399 nb_page
->m_text
= text
;
401 gtk_label_set( nb_page
->m_label
, wxGTK_CONV( nb_page
->m_text
) );
406 bool wxNotebook::SetPageImage( int page
, int image
)
408 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
410 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
412 if (!nb_page
) return FALSE
;
414 /* Optimization posibility: return immediately if image unchanged.
415 * Not enabled because it may break existing (stupid) code that
416 * manipulates the imagelist to cycle images */
418 /* if (image == nb_page->m_image) return TRUE; */
420 /* For different cases:
421 1) no image -> no image
426 if (image
== -1 && nb_page
->m_image
== -1)
427 return TRUE
; /* Case 1): Nothing to do. */
429 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
431 if (nb_page
->m_image
!= -1)
433 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
435 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
438 if (GTK_IS_PIXMAP(child
->data
))
440 pixmapwid
= GTK_WIDGET(child
->data
);
446 /* We should have the pixmap widget now */
447 wxASSERT(pixmapwid
!= NULL
);
451 /* If there's no new widget, just remove the old from the box */
452 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
453 nb_page
->m_image
= -1;
455 return TRUE
; /* Case 2) */
459 /* Only cases 3) and 4) left */
460 wxASSERT( m_imageList
!= NULL
); /* Just in case */
462 /* Construct the new pixmap */
463 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
464 GdkPixmap
*pixmap
= bmp
->GetPixmap();
465 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
466 if ( bmp
->GetMask() )
468 mask
= bmp
->GetMask()->GetBitmap();
471 if (pixmapwid
== NULL
)
473 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
474 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
476 /* CHECKME: Are these pack flags okay? */
477 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
478 gtk_widget_show(pixmapwid
);
482 /* Case 4) Simply replace the pixmap */
483 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
486 nb_page
->m_image
= image
;
491 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
493 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
496 void wxNotebook::SetPadding( const wxSize
&padding
)
498 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
500 m_padding
= padding
.GetWidth();
503 for (i
=0; i
<int(GetPageCount()); i
++)
505 wxGtkNotebookPage
* nb_page
= GetNotebookPage(i
);
506 wxASSERT(nb_page
!= NULL
);
508 if (nb_page
->m_image
!= -1)
510 // gtk_box_set_child_packing sets padding on BOTH sides
511 // icon provides left padding, label provides center and right
512 int image
= nb_page
->m_image
;
514 SetPageImage(i
,image
);
516 wxASSERT(nb_page
->m_label
);
517 gtk_box_set_child_packing(GTK_BOX(nb_page
->m_box
),
518 GTK_WIDGET(nb_page
->m_label
),
519 FALSE
, FALSE
, m_padding
, GTK_PACK_END
);
523 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
525 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
528 bool wxNotebook::DeleteAllPages()
530 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
532 while (m_pagesData
.GetCount() > 0)
533 DeletePage( m_pagesData
.GetCount()-1 );
535 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
537 return wxNotebookBase::DeleteAllPages();
540 bool wxNotebook::DeletePage( int page
)
542 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
543 // event so we have to store the selection internally
544 if ( m_selection
== -1 )
546 m_selection
= GetSelection();
547 if ( m_selection
== (int)m_pagesData
.GetCount() - 1 )
549 // the index will become invalid after the page is deleted
554 // it will call our DoRemovePage() to do the real work
555 return wxNotebookBase::DeletePage(page
);
558 wxNotebookPage
*wxNotebook::DoRemovePage( int page
)
560 wxNotebookPage
*client
= wxNotebookBase::DoRemovePage(page
);
564 gtk_widget_ref( client
->m_widget
);
565 gtk_widget_unrealize( client
->m_widget
);
566 gtk_widget_unparent( client
->m_widget
);
568 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
570 m_pagesData
.DeleteObject(GetNotebookPage(page
));
575 bool wxNotebook::InsertPage( int position
,
577 const wxString
& text
,
581 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
583 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
584 wxT("Can't add a page whose parent is not the notebook!") );
586 wxCHECK_MSG( position
>= 0 && position
<= GetPageCount(), FALSE
,
587 _T("invalid page index in wxNotebookPage::InsertPage()") );
589 /* don't receive switch page during addition */
590 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
591 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
594 win
->SetThemeEnabled(TRUE
);
596 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
598 wxGtkNotebookPage
*nb_page
= new wxGtkNotebookPage();
600 if ( position
== GetPageCount() )
601 m_pagesData
.Append( nb_page
);
603 m_pagesData
.Insert( m_pagesData
.Item( position
), nb_page
);
605 m_pages
.Insert(win
, position
);
607 nb_page
->m_box
= gtk_hbox_new( FALSE
, 1 );
608 gtk_container_border_width( GTK_CONTAINER(nb_page
->m_box
), 2 );
610 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
611 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
614 gtk_notebook_append_page( notebook
, win
->m_widget
, nb_page
->m_box
);
616 gtk_notebook_insert_page( notebook
, win
->m_widget
, nb_page
->m_box
, position
);
618 nb_page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
620 /* set the label image */
621 nb_page
->m_image
= imageId
;
625 wxASSERT( m_imageList
!= NULL
);
627 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
628 GdkPixmap
*pixmap
= bmp
->GetPixmap();
629 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
630 if ( bmp
->GetMask() )
632 mask
= bmp
->GetMask()->GetBitmap();
635 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
637 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
639 gtk_widget_show(pixmapwid
);
642 /* set the label text */
643 nb_page
->m_text
= text
;
644 if (nb_page
->m_text
.IsEmpty()) nb_page
->m_text
= wxT("");
646 nb_page
->m_label
= GTK_LABEL( gtk_label_new(nb_page
->m_text
.mbc_str()) );
647 gtk_box_pack_end( GTK_BOX(nb_page
->m_box
), GTK_WIDGET(nb_page
->m_label
), FALSE
, FALSE
, m_padding
);
650 gtk_widget_show( GTK_WIDGET(nb_page
->m_label
) );
651 if (select
&& (m_pagesData
.GetCount() > 1))
654 SetSelection( GetPageCount()-1 );
656 SetSelection( position
);
659 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
660 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
665 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
667 if (event
.IsWindowChange())
668 AdvanceSelection( event
.GetDirection() );
673 #if wxUSE_CONSTRAINTS
675 // override these 2 functions to do nothing: everything is done in OnSize
676 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
678 // don't set the sizes of the pages - their correct size is not yet known
679 wxControl::SetConstraintSizes(FALSE
);
682 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
689 void wxNotebook::ApplyWidgetStyle()
691 // TODO, font for labels etc
694 gtk_widget_set_style( m_widget
, m_widgetStyle
);
697 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
699 return ((m_widget
->window
== window
) ||
700 (NOTEBOOK_PANEL(m_widget
) == window
));
703 //-----------------------------------------------------------------------------
705 //-----------------------------------------------------------------------------
707 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)