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 // are you trying to call SetSelection() from a notebook event handler?
91 wxCHECK_RET( !notebook
->m_inSwitchPage
,
92 _T("gtk_notebook_page_change_callback reentered") );
94 notebook
->m_inSwitchPage
= TRUE
;
96 wxapp_install_idle_handler();
98 int old
= notebook
->GetSelection();
100 wxNotebookEvent
eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
101 notebook
->GetId(), page
, old
);
102 eventChanging
.SetEventObject( notebook
);
104 if ( (notebook
->GetEventHandler()->ProcessEvent(eventChanging
)) &&
105 !eventChanging
.IsAllowed() )
107 /* program doesn't allow the page change */
108 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
),
111 else // change allowed
113 // make wxNotebook::GetSelection() return the correct (i.e. consistent
114 // with wxNotebookEvent::GetSelection()) value even though the page is
115 // not really changed in GTK+
116 notebook
->m_selection
= page
;
118 wxNotebookEvent
eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
119 notebook
->GetId(), page
, old
);
120 eventChanged
.SetEventObject( notebook
);
121 notebook
->GetEventHandler()->ProcessEvent( eventChanged
);
124 notebook
->m_inSwitchPage
= FALSE
;
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
134 wxapp_install_idle_handler();
136 if ((win
->m_x
== alloc
->x
) &&
137 (win
->m_y
== alloc
->y
) &&
138 (win
->m_width
== alloc
->width
) &&
139 (win
->m_height
== alloc
->height
))
144 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
146 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
147 here in order to make repositioning after resizing to take effect. */
148 if ((gtk_major_version
== 1) &&
149 (gtk_minor_version
== 2) &&
150 (gtk_micro_version
< 6) &&
152 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
154 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
158 //-----------------------------------------------------------------------------
159 // "realize" from m_widget
160 //-----------------------------------------------------------------------------
163 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
166 wxapp_install_idle_handler();
168 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
169 here in order to make repositioning before showing to take effect. */
170 gtk_widget_queue_resize( win
->m_widget
);
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
179 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
182 wxapp_install_idle_handler();
184 if (!win
->m_hasVMT
) return FALSE
;
185 if (g_blockEventsOnDrag
) return FALSE
;
187 /* win is a control: tab can be propagated up */
188 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
190 int sel
= win
->GetSelection();
191 wxGtkNotebookPage
*nb_page
= win
->GetNotebookPage(sel
);
192 wxCHECK_MSG( nb_page
, FALSE
, _T("invalid selection in wxNotebook") );
194 wxNavigationKeyEvent event
;
195 event
.SetEventObject( win
);
196 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
197 event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
198 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
199 event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
200 event
.SetCurrentFocus( win
);
202 wxNotebookPage
*client
= win
->GetPage(sel
);
203 if ( !client
->GetEventHandler()->ProcessEvent( event
) )
208 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
215 //-----------------------------------------------------------------------------
216 // InsertChild callback for wxNotebook
217 //-----------------------------------------------------------------------------
219 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
221 /* we don't do anything here but pray */
224 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
228 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
230 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
231 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
234 void wxNotebook::Init()
237 m_inSwitchPage
= FALSE
;
239 m_imageList
= (wxImageList
*) NULL
;
240 m_pagesData
.DeleteContents( TRUE
);
242 m_themeEnabled
= TRUE
;
245 wxNotebook::wxNotebook()
250 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
251 const wxPoint
& pos
, const wxSize
& size
,
252 long style
, const wxString
& name
)
255 Create( parent
, id
, pos
, size
, style
, name
);
258 wxNotebook::~wxNotebook()
260 /* don't generate change page events any more */
261 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
262 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
267 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
268 const wxPoint
& pos
, const wxSize
& size
,
269 long style
, const wxString
& name
)
272 m_acceptsFocus
= TRUE
;
273 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
275 if (!PreCreation( parent
, pos
, size
) ||
276 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
278 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
283 m_widget
= gtk_notebook_new();
285 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
287 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
288 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
290 m_parent
->DoAddChild( this );
292 if (m_windowStyle
& wxNB_RIGHT
)
293 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
294 if (m_windowStyle
& wxNB_LEFT
)
295 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
296 if (m_windowStyle
& wxNB_BOTTOM
)
297 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
299 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
300 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
304 SetFont( parent
->GetFont() );
306 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
307 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
314 int wxNotebook::GetSelection() const
316 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
318 if ( m_selection
== -1 )
320 GList
*nb_pages
= GTK_NOTEBOOK(m_widget
)->children
;
322 if (g_list_length(nb_pages
) != 0)
324 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
326 gpointer cur
= notebook
->cur_page
;
329 wxConstCast(this, wxNotebook
)->m_selection
=
330 g_list_index( nb_pages
, cur
);
338 wxString
wxNotebook::GetPageText( int page
) const
340 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
342 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
344 return nb_page
->m_text
;
349 int wxNotebook::GetPageImage( int page
) const
351 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
353 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
355 return nb_page
->m_image
;
360 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
362 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
364 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
366 return m_pagesData
.Item(page
)->GetData();
369 int wxNotebook::SetSelection( int page
)
371 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
373 wxCHECK_MSG( page
< (int)m_pagesData
.GetCount(), -1, wxT("invalid notebook index") );
375 int selOld
= GetSelection();
377 // cache the selection
379 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
381 wxNotebookPage
*client
= GetPage(page
);
388 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
390 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
392 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
394 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
396 nb_page
->m_text
= text
;
398 gtk_label_set( nb_page
->m_label
, wxGTK_CONV( nb_page
->m_text
) );
403 bool wxNotebook::SetPageImage( int page
, int image
)
405 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
407 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
409 if (!nb_page
) return FALSE
;
411 /* Optimization posibility: return immediately if image unchanged.
412 * Not enabled because it may break existing (stupid) code that
413 * manipulates the imagelist to cycle images */
415 /* if (image == nb_page->m_image) return TRUE; */
417 /* For different cases:
418 1) no image -> no image
423 if (image
== -1 && nb_page
->m_image
== -1)
424 return TRUE
; /* Case 1): Nothing to do. */
426 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
428 if (nb_page
->m_image
!= -1)
430 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
432 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
435 if (GTK_IS_PIXMAP(child
->data
))
437 pixmapwid
= GTK_WIDGET(child
->data
);
443 /* We should have the pixmap widget now */
444 wxASSERT(pixmapwid
!= NULL
);
448 /* If there's no new widget, just remove the old from the box */
449 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
450 nb_page
->m_image
= -1;
452 return TRUE
; /* Case 2) */
456 /* Only cases 3) and 4) left */
457 wxASSERT( m_imageList
!= NULL
); /* Just in case */
459 /* Construct the new pixmap */
460 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
461 GdkPixmap
*pixmap
= bmp
->GetPixmap();
462 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
463 if ( bmp
->GetMask() )
465 mask
= bmp
->GetMask()->GetBitmap();
468 if (pixmapwid
== NULL
)
470 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
471 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
473 /* CHECKME: Are these pack flags okay? */
474 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
475 gtk_widget_show(pixmapwid
);
479 /* Case 4) Simply replace the pixmap */
480 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
483 nb_page
->m_image
= image
;
488 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
490 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
493 void wxNotebook::SetPadding( const wxSize
&padding
)
495 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
497 m_padding
= padding
.GetWidth();
500 for (i
=0; i
<int(GetPageCount()); i
++)
502 wxGtkNotebookPage
* nb_page
= GetNotebookPage(i
);
503 wxASSERT(nb_page
!= NULL
);
505 if (nb_page
->m_image
!= -1)
507 // gtk_box_set_child_packing sets padding on BOTH sides
508 // icon provides left padding, label provides center and right
509 int image
= nb_page
->m_image
;
511 SetPageImage(i
,image
);
513 wxASSERT(nb_page
->m_label
);
514 gtk_box_set_child_packing(GTK_BOX(nb_page
->m_box
),
515 GTK_WIDGET(nb_page
->m_label
),
516 FALSE
, FALSE
, m_padding
, GTK_PACK_END
);
520 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
522 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
525 bool wxNotebook::DeleteAllPages()
527 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
529 while (m_pagesData
.GetCount() > 0)
530 DeletePage( m_pagesData
.GetCount()-1 );
532 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
534 return wxNotebookBase::DeleteAllPages();
537 bool wxNotebook::DeletePage( int page
)
539 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
540 // event so we have to store the selection internally
541 if ( m_selection
== -1 )
543 m_selection
= GetSelection();
544 if ( m_selection
== (int)m_pagesData
.GetCount() - 1 )
546 // the index will become invalid after the page is deleted
551 // it will call our DoRemovePage() to do the real work
552 return wxNotebookBase::DeletePage(page
);
555 wxNotebookPage
*wxNotebook::DoRemovePage( int page
)
557 wxNotebookPage
*client
= wxNotebookBase::DoRemovePage(page
);
561 gtk_widget_ref( client
->m_widget
);
562 gtk_widget_unrealize( client
->m_widget
);
563 gtk_widget_unparent( client
->m_widget
);
565 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
567 m_pagesData
.DeleteObject(GetNotebookPage(page
));
572 bool wxNotebook::InsertPage( int position
,
574 const wxString
& text
,
578 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
580 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
581 wxT("Can't add a page whose parent is not the notebook!") );
583 wxCHECK_MSG( position
>= 0 && position
<= GetPageCount(), FALSE
,
584 _T("invalid page index in wxNotebookPage::InsertPage()") );
586 /* don't receive switch page during addition */
587 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
588 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
591 win
->SetThemeEnabled(TRUE
);
593 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
595 wxGtkNotebookPage
*nb_page
= new wxGtkNotebookPage();
597 if ( position
== GetPageCount() )
598 m_pagesData
.Append( nb_page
);
600 m_pagesData
.Insert( m_pagesData
.Item( position
), nb_page
);
602 m_pages
.Insert(win
, position
);
604 nb_page
->m_box
= gtk_hbox_new( FALSE
, 1 );
605 gtk_container_border_width( GTK_CONTAINER(nb_page
->m_box
), 2 );
607 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
608 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
611 gtk_notebook_append_page( notebook
, win
->m_widget
, nb_page
->m_box
);
613 gtk_notebook_insert_page( notebook
, win
->m_widget
, nb_page
->m_box
, position
);
615 nb_page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
617 /* set the label image */
618 nb_page
->m_image
= imageId
;
622 wxASSERT( m_imageList
!= NULL
);
624 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
625 GdkPixmap
*pixmap
= bmp
->GetPixmap();
626 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
627 if ( bmp
->GetMask() )
629 mask
= bmp
->GetMask()->GetBitmap();
632 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
634 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, m_padding
);
636 gtk_widget_show(pixmapwid
);
639 /* set the label text */
640 nb_page
->m_text
= text
;
641 if (nb_page
->m_text
.IsEmpty()) nb_page
->m_text
= wxT("");
643 nb_page
->m_label
= GTK_LABEL( gtk_label_new(nb_page
->m_text
.mbc_str()) );
644 gtk_box_pack_end( GTK_BOX(nb_page
->m_box
), GTK_WIDGET(nb_page
->m_label
), FALSE
, FALSE
, m_padding
);
647 gtk_widget_show( GTK_WIDGET(nb_page
->m_label
) );
648 if (select
&& (m_pagesData
.GetCount() > 1))
651 SetSelection( GetPageCount()-1 );
653 SetSelection( position
);
656 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
657 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
662 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
664 if (event
.IsWindowChange())
665 AdvanceSelection( event
.GetDirection() );
670 #if wxUSE_CONSTRAINTS
672 // override these 2 functions to do nothing: everything is done in OnSize
673 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
675 // don't set the sizes of the pages - their correct size is not yet known
676 wxControl::SetConstraintSizes(FALSE
);
679 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
686 void wxNotebook::ApplyWidgetStyle()
688 // TODO, font for labels etc
691 gtk_widget_set_style( m_widget
, m_widgetStyle
);
694 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
696 return ((m_widget
->window
== window
) ||
697 (NOTEBOOK_PANEL(m_widget
) == window
));
700 //-----------------------------------------------------------------------------
702 //-----------------------------------------------------------------------------
704 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)