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
)
93 wxapp_install_idle_handler();
95 int old
= notebook
->GetSelection();
97 wxNotebookEvent
event1( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
98 notebook
->GetId(), page
, old
);
99 event1
.SetEventObject( notebook
);
101 if ((notebook
->GetEventHandler()->ProcessEvent( event1
)) &&
102 !event1
.IsAllowed() )
104 /* program doesn't allow the page change */
105 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
), "switch_page" );
109 wxNotebookEvent
event2( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
110 notebook
->GetId(), page
, old
);
111 event2
.SetEventObject( notebook
);
112 notebook
->GetEventHandler()->ProcessEvent( event2
);
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
122 wxapp_install_idle_handler();
124 if ((win
->m_x
== alloc
->x
) &&
125 (win
->m_y
== alloc
->y
) &&
126 (win
->m_width
== alloc
->width
) &&
127 (win
->m_height
== alloc
->height
))
132 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
134 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
135 here in order to make repositioning after resizing to take effect. */
136 if ((gtk_major_version
== 1) &&
137 (gtk_minor_version
== 2) &&
138 (gtk_micro_version
< 6) &&
140 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
142 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
146 //-----------------------------------------------------------------------------
147 // "realize" from m_widget
148 //-----------------------------------------------------------------------------
151 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
154 wxapp_install_idle_handler();
156 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
157 here in order to make repositioning before showing to take effect. */
158 gtk_widget_queue_resize( win
->m_widget
);
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
170 wxapp_install_idle_handler();
172 if (!win
->m_hasVMT
) return FALSE
;
173 if (g_blockEventsOnDrag
) return FALSE
;
175 /* win is a control: tab can be propagated up */
176 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
178 wxNode
*node
= win
->m_pages
.Nth( win
->GetSelection() );
179 if (!node
) return FALSE
;
181 wxGtkNotebookPage
*page
= (wxGtkNotebookPage
*) node
->Data();
183 wxNavigationKeyEvent event
;
184 event
.SetEventObject( win
);
185 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
186 event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
187 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
188 event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
189 event
.SetCurrentFocus( win
);
190 if (!page
->m_client
->GetEventHandler()->ProcessEvent( event
))
192 page
->m_client
->SetFocus();
195 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
202 //-----------------------------------------------------------------------------
203 // InsertChild callback for wxNotebook
204 //-----------------------------------------------------------------------------
206 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
208 /* we don't do anything here but pray */
211 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
215 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
217 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
218 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
221 void wxNotebook::Init()
223 m_imageList
= (wxImageList
*) NULL
;
224 m_ownsImageList
= FALSE
;
225 m_pages
.DeleteContents( TRUE
);
226 m_lastSelection
= -1;
227 m_themeEnabled
= TRUE
;
230 wxNotebook::wxNotebook()
235 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
236 const wxPoint
& pos
, const wxSize
& size
,
237 long style
, const wxString
& name
)
240 Create( parent
, id
, pos
, size
, style
, name
);
243 wxNotebook::~wxNotebook()
245 /* don't generate change page events any more */
246 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
247 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
250 if (m_ownsImageList
) delete m_imageList
;
253 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
254 const wxPoint
& pos
, const wxSize
& size
,
255 long style
, const wxString
& name
)
258 m_acceptsFocus
= TRUE
;
259 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
261 if (!PreCreation( parent
, pos
, size
) ||
262 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
264 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
269 m_widget
= gtk_notebook_new();
272 debug_focus_in( m_widget
, wxT("wxNotebook::m_widget"), name
);
275 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
277 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
278 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
280 m_parent
->DoAddChild( this );
282 if (m_windowStyle
& wxNB_RIGHT
)
283 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
284 if (m_windowStyle
& wxNB_LEFT
)
285 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
286 if (m_windowStyle
& wxNB_BOTTOM
)
287 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
289 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
290 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
294 SetFont( parent
->GetFont() );
296 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
297 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
304 int wxNotebook::GetSelection() const
306 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
308 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
310 if (g_list_length(pages
) == 0) return -1;
312 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
314 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
316 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
319 int wxNotebook::GetPageCount() const
321 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
324 int wxNotebook::GetRowCount() const
329 wxString
wxNotebook::GetPageText( int page
) const
331 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
333 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
335 return nb_page
->m_text
;
340 int wxNotebook::GetPageImage( int page
) const
342 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
344 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
346 return nb_page
->m_image
;
351 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
353 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
355 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
357 wxNode
*node
= m_pages
.Nth( page
);
359 return (wxGtkNotebookPage
*) node
->Data();
362 int wxNotebook::SetSelection( int page
)
364 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
366 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, wxT("invalid notebook index") );
368 int selOld
= GetSelection();
370 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
372 wxGtkNotebookPage
* g_page
= GetNotebookPage( page
);
373 if (g_page
->m_client
)
374 g_page
->m_client
->SetFocus();
379 void wxNotebook::AdvanceSelection( bool forward
)
381 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
383 int max
= GetPageCount();
386 // nothing to do with empty notebook
390 int sel
= GetSelection();
393 SetSelection( sel
== max
- 1 ? 0 : sel
+ 1 );
395 SetSelection( sel
== 0 ? max
- 1 : sel
- 1 );
398 void wxNotebook::SetImageList( wxImageList
* imageList
)
400 if (m_ownsImageList
) delete m_imageList
;
401 m_imageList
= imageList
;
402 m_ownsImageList
= FALSE
;
405 void wxNotebook::AssignImageList( wxImageList
* imageList
)
407 SetImageList(imageList
);
408 m_ownsImageList
= TRUE
;
411 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
413 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
415 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
417 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
419 nb_page
->m_text
= text
;
421 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
426 bool wxNotebook::SetPageImage( int page
, int image
)
428 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
430 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
432 if (!nb_page
) return FALSE
;
434 /* Optimization posibility: return immediately if image unchanged.
435 * Not enabled because it may break existing (stupid) code that
436 * manipulates the imagelist to cycle images */
438 /* if (image == nb_page->m_image) return TRUE; */
440 /* For different cases:
441 1) no image -> no image
446 if (image
== -1 && nb_page
->m_image
== -1)
447 return TRUE
; /* Case 1): Nothing to do. */
449 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
451 if (nb_page
->m_image
!= -1)
453 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
455 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
458 if (GTK_IS_PIXMAP(child
->data
))
460 pixmapwid
= GTK_WIDGET(child
->data
);
466 /* We should have the pixmap widget now */
467 wxASSERT(pixmapwid
!= NULL
);
471 /* If there's no new widget, just remove the old from the box */
472 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
473 nb_page
->m_image
= -1;
475 return TRUE
; /* Case 2) */
479 /* Only cases 3) and 4) left */
480 wxASSERT( m_imageList
!= NULL
); /* Just in case */
482 /* Construct the new pixmap */
483 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
484 GdkPixmap
*pixmap
= bmp
->GetPixmap();
485 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
486 if ( bmp
->GetMask() )
488 mask
= bmp
->GetMask()->GetBitmap();
491 if (pixmapwid
== NULL
)
493 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
494 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
496 /* CHECKME: Are these pack flags okay? */
497 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
498 gtk_widget_show(pixmapwid
);
502 /* Case 4) Simply replace the pixmap */
503 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
506 nb_page
->m_image
= image
;
511 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
513 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
516 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
518 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
521 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
523 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
526 bool wxNotebook::DeleteAllPages()
528 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
530 while (m_pages
.GetCount() > 0)
531 DeletePage( m_pages
.GetCount()-1 );
536 bool wxNotebook::DeletePage( int page
)
538 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
539 if (!nb_page
) return FALSE
;
541 /* GTK sets GtkNotebook.cur_page to NULL before sending
542 the switch page event */
543 m_lastSelection
= GetSelection();
545 nb_page
->m_client
->Destroy();
546 m_pages
.DeleteObject( nb_page
);
548 m_lastSelection
= -1;
553 bool wxNotebook::RemovePage( int page
)
555 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
557 wxCHECK_MSG( nb_page
, FALSE
, _T("wxNotebook::RemovePage: invalid page") );
559 gtk_widget_ref( nb_page
->m_client
->m_widget
);
560 gtk_widget_unrealize( nb_page
->m_client
->m_widget
);
561 gtk_widget_unparent( nb_page
->m_client
->m_widget
);
563 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
565 m_pages
.DeleteObject( nb_page
);
570 bool wxNotebook::InsertPage( int position
, wxNotebookPage
* win
, const wxString
& text
,
571 bool select
, int imageId
)
573 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
575 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
576 wxT("Can't add a page whose parent is not the notebook!") );
578 /* don't receive switch page during addition */
579 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
580 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
583 win
->SetThemeEnabled(TRUE
);
585 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
587 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
590 m_pages
.Append( page
);
592 m_pages
.Insert( m_pages
.Nth( position
), page
);
594 page
->m_client
= win
;
596 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
597 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
599 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
600 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
603 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
605 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
607 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
609 /* set the label image */
610 page
->m_image
= imageId
;
614 wxASSERT( m_imageList
!= NULL
);
616 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
617 GdkPixmap
*pixmap
= bmp
->GetPixmap();
618 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
619 if ( bmp
->GetMask() )
621 mask
= bmp
->GetMask()->GetBitmap();
624 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
626 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
628 gtk_widget_show(pixmapwid
);
631 /* set the label text */
633 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
635 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
636 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
639 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
641 if (select
&& (m_pages
.GetCount() > 1))
644 SetSelection( GetPageCount()-1 );
646 SetSelection( position
);
649 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
650 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
655 bool wxNotebook::AddPage(wxNotebookPage
* win
, const wxString
& text
,
656 bool select
, int imageId
)
658 return InsertPage( -1, win
, text
, select
, imageId
);
661 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
663 if (event
.IsWindowChange())
664 AdvanceSelection( event
.GetDirection() );
669 wxNotebookPage
*wxNotebook::GetPage( int page
) const
671 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, wxT("invalid notebook") );
673 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
675 return (wxNotebookPage
*) NULL
;
677 return nb_page
->m_client
;
680 #if wxUSE_CONSTRAINTS
682 // override these 2 functions to do nothing: everything is done in OnSize
683 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
685 // don't set the sizes of the pages - their correct size is not yet known
686 wxControl::SetConstraintSizes(FALSE
);
689 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
696 void wxNotebook::ApplyWidgetStyle()
698 // TODO, font for labels etc
701 gtk_widget_set_style( m_widget
, m_widgetStyle
);
704 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
706 return ((m_widget
->window
== window
) ||
707 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
710 //-----------------------------------------------------------------------------
712 //-----------------------------------------------------------------------------
714 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)