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 wxString
wxNotebook::GetPageText( int page
) const
347 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
349 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
351 return nb_page
->m_text
;
356 int wxNotebook::GetPageImage( int page
) const
358 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
360 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
362 return nb_page
->m_image
;
367 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
369 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
371 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
373 wxNode
*node
= m_pages
.Nth( page
);
375 return (wxGtkNotebookPage
*) node
->Data();
378 int wxNotebook::SetSelection( int page
)
380 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
382 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, wxT("invalid notebook index") );
384 int selOld
= GetSelection();
386 // cache the selection
388 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
390 wxGtkNotebookPage
* g_page
= GetNotebookPage( page
);
391 if (g_page
->m_client
)
392 g_page
->m_client
->SetFocus();
397 void wxNotebook::SetImageList( wxImageList
* imageList
)
399 if (m_ownsImageList
) delete m_imageList
;
400 m_imageList
= imageList
;
401 m_ownsImageList
= FALSE
;
404 void wxNotebook::AssignImageList( wxImageList
* imageList
)
406 SetImageList(imageList
);
407 m_ownsImageList
= TRUE
;
410 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
412 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
414 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
416 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
418 nb_page
->m_text
= text
;
420 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
425 bool wxNotebook::SetPageImage( int page
, int image
)
427 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
429 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
431 if (!nb_page
) return FALSE
;
433 /* Optimization posibility: return immediately if image unchanged.
434 * Not enabled because it may break existing (stupid) code that
435 * manipulates the imagelist to cycle images */
437 /* if (image == nb_page->m_image) return TRUE; */
439 /* For different cases:
440 1) no image -> no image
445 if (image
== -1 && nb_page
->m_image
== -1)
446 return TRUE
; /* Case 1): Nothing to do. */
448 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
450 if (nb_page
->m_image
!= -1)
452 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
454 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
457 if (GTK_IS_PIXMAP(child
->data
))
459 pixmapwid
= GTK_WIDGET(child
->data
);
465 /* We should have the pixmap widget now */
466 wxASSERT(pixmapwid
!= NULL
);
470 /* If there's no new widget, just remove the old from the box */
471 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
472 nb_page
->m_image
= -1;
474 return TRUE
; /* Case 2) */
478 /* Only cases 3) and 4) left */
479 wxASSERT( m_imageList
!= NULL
); /* Just in case */
481 /* Construct the new pixmap */
482 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
483 GdkPixmap
*pixmap
= bmp
->GetPixmap();
484 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
485 if ( bmp
->GetMask() )
487 mask
= bmp
->GetMask()->GetBitmap();
490 if (pixmapwid
== NULL
)
492 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
493 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
495 /* CHECKME: Are these pack flags okay? */
496 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
497 gtk_widget_show(pixmapwid
);
501 /* Case 4) Simply replace the pixmap */
502 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
505 nb_page
->m_image
= image
;
510 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
512 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
515 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
517 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
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_pages
.GetCount() > 0)
530 DeletePage( m_pages
.GetCount()-1 );
535 bool wxNotebook::DeletePage( int page
)
537 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
538 wxCHECK_MSG( nb_page
, FALSE
, _T("invalid page in wxNotebook::DeletePage") );
540 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
541 // event so we have to store the selection internally
542 if ( m_selection
== -1 )
544 m_selection
= GetSelection();
545 if ( m_selection
== (int)m_pages
.GetCount() - 1 )
547 // the index will become invalid after the page is deleted
552 nb_page
->m_client
->Destroy();
553 m_pages
.DeleteObject( nb_page
);
558 wxNotebookPage
*wxNotebook::DoRemovePage( int page
)
560 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
562 wxCHECK_MSG( nb_page
, NULL
, _T("wxNotebook::RemovePage: invalid page") );
564 gtk_widget_ref( nb_page
->m_client
->m_widget
);
565 gtk_widget_unrealize( nb_page
->m_client
->m_widget
);
566 gtk_widget_unparent( nb_page
->m_client
->m_widget
);
568 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
570 wxNotebookPage
*pageRemoved
= (wxNotebookPage
*)m_pages
[page
];
571 m_pages
.DeleteObject( nb_page
);
576 bool wxNotebook::InsertPage( int position
, wxNotebookPage
* win
, const wxString
& text
,
577 bool select
, int imageId
)
579 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
581 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
582 wxT("Can't add a page whose parent is not the notebook!") );
584 /* don't receive switch page during addition */
585 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
586 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
589 win
->SetThemeEnabled(TRUE
);
591 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
593 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
596 m_pages
.Append( page
);
598 m_pages
.Insert( m_pages
.Nth( position
), page
);
600 page
->m_client
= win
;
602 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
603 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
605 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
606 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
609 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
611 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
613 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
615 /* set the label image */
616 page
->m_image
= imageId
;
620 wxASSERT( m_imageList
!= NULL
);
622 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
623 GdkPixmap
*pixmap
= bmp
->GetPixmap();
624 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
625 if ( bmp
->GetMask() )
627 mask
= bmp
->GetMask()->GetBitmap();
630 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
632 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
634 gtk_widget_show(pixmapwid
);
637 /* set the label text */
639 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
641 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
642 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
645 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
647 if (select
&& (m_pages
.GetCount() > 1))
650 SetSelection( GetPageCount()-1 );
652 SetSelection( position
);
655 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
656 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
661 bool wxNotebook::AddPage(wxNotebookPage
* win
, const wxString
& text
,
662 bool select
, int imageId
)
664 return InsertPage( -1, win
, text
, select
, imageId
);
667 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
669 if (event
.IsWindowChange())
670 AdvanceSelection( event
.GetDirection() );
675 #if wxUSE_CONSTRAINTS
677 // override these 2 functions to do nothing: everything is done in OnSize
678 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
680 // don't set the sizes of the pages - their correct size is not yet known
681 wxControl::SetConstraintSizes(FALSE
);
684 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
691 void wxNotebook::ApplyWidgetStyle()
693 // TODO, font for labels etc
696 gtk_widget_set_style( m_widget
, m_widgetStyle
);
699 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
701 return ((m_widget
->window
== window
) ||
702 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
705 //-----------------------------------------------------------------------------
707 //-----------------------------------------------------------------------------
709 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)