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 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
48 extern void debug_focus_in( GtkWidget
* widget
, const wxChar
* name
, const wxChar
*window
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 class wxGtkNotebookPage
: public wxObject
63 m_page
= (GtkNotebookPage
*) NULL
;
64 m_client
= (wxNotebookPage
*) NULL
;
65 m_box
= (GtkWidget
*) NULL
;
70 GtkNotebookPage
*m_page
;
72 wxNotebookPage
*m_client
;
73 GtkWidget
*m_box
; // in which the label and image are packed
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
81 GtkNotebookPage
*WXUNUSED(page
),
83 wxNotebook
*notebook
)
86 wxapp_install_idle_handler();
88 int old
= notebook
->GetSelection();
90 wxNotebookEvent
event1( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
91 notebook
->GetId(), page
, old
);
92 event1
.SetEventObject( notebook
);
94 if ((notebook
->GetEventHandler()->ProcessEvent( event1
)) &&
97 /* program doesn't allow the page change */
98 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
), "switch_page" );
102 wxNotebookEvent
event2( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
103 notebook
->GetId(), page
, old
);
104 event2
.SetEventObject( notebook
);
105 notebook
->GetEventHandler()->ProcessEvent( event2
);
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
112 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
115 wxapp_install_idle_handler();
117 if ((win
->m_x
== alloc
->x
) &&
118 (win
->m_y
== alloc
->y
) &&
119 (win
->m_width
== alloc
->width
) &&
120 (win
->m_height
== alloc
->height
))
125 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
127 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
128 here in order to make repositioning after resizing to take effect. */
129 if ((gtk_major_version
== 1) &&
130 (gtk_minor_version
== 2) &&
131 (gtk_micro_version
< 6) &&
133 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
135 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
139 //-----------------------------------------------------------------------------
140 // "realize" from m_widget
141 //-----------------------------------------------------------------------------
144 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
147 wxapp_install_idle_handler();
149 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
150 here in order to make repositioning before showing to take effect. */
151 gtk_widget_queue_resize( win
->m_widget
);
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 static gint
gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*win
)
163 wxapp_install_idle_handler();
165 if (!win
->m_hasVMT
) return FALSE
;
166 if (g_blockEventsOnDrag
) return FALSE
;
168 /* win is a control: tab can be propagated up */
169 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
171 wxNode
*node
= win
->m_pages
.Nth( win
->GetSelection() );
172 if (!node
) return FALSE
;
174 wxGtkNotebookPage
*page
= (wxGtkNotebookPage
*) node
->Data();
176 wxNavigationKeyEvent event
;
177 event
.SetEventObject( win
);
178 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
179 event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
180 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
181 event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
182 event
.SetCurrentFocus( win
);
183 if (!page
->m_client
->GetEventHandler()->ProcessEvent( event
))
185 page
->m_client
->SetFocus();
188 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
195 //-----------------------------------------------------------------------------
196 // InsertChild callback for wxNotebook
197 //-----------------------------------------------------------------------------
199 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
201 /* we don't do anything here but pray */
204 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------
208 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
210 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
211 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
214 void wxNotebook::Init()
216 m_imageList
= (wxImageList
*) NULL
;
217 m_ownsImageList
= FALSE
;
218 m_pages
.DeleteContents( TRUE
);
219 m_lastSelection
= -1;
220 m_themeEnabled
= TRUE
;
223 wxNotebook::wxNotebook()
228 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
229 const wxPoint
& pos
, const wxSize
& size
,
230 long style
, const wxString
& name
)
233 Create( parent
, id
, pos
, size
, style
, name
);
236 wxNotebook::~wxNotebook()
238 /* don't generate change page events any more */
239 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
240 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
243 if (m_ownsImageList
) delete m_imageList
;
246 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
247 const wxPoint
& pos
, const wxSize
& size
,
248 long style
, const wxString
& name
)
251 m_acceptsFocus
= TRUE
;
252 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
254 if (!PreCreation( parent
, pos
, size
) ||
255 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
257 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
262 m_widget
= gtk_notebook_new();
265 debug_focus_in( m_widget
, wxT("wxNotebook::m_widget"), name
);
268 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
270 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
271 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
273 m_parent
->DoAddChild( this );
275 if (m_windowStyle
& wxNB_RIGHT
)
276 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
277 if (m_windowStyle
& wxNB_LEFT
)
278 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
279 if (m_windowStyle
& wxNB_BOTTOM
)
280 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
282 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
283 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
287 SetFont( parent
->GetFont() );
289 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
290 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
297 int wxNotebook::GetSelection() const
299 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
301 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
303 if (g_list_length(pages
) == 0) return -1;
305 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
307 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
309 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
312 int wxNotebook::GetPageCount() const
314 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
317 int wxNotebook::GetRowCount() const
322 wxString
wxNotebook::GetPageText( int page
) const
324 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
326 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
328 return nb_page
->m_text
;
333 int wxNotebook::GetPageImage( int page
) const
335 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
337 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
339 return nb_page
->m_image
;
344 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
346 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
348 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
350 wxNode
*node
= m_pages
.Nth( page
);
352 return (wxGtkNotebookPage
*) node
->Data();
355 int wxNotebook::SetSelection( int page
)
357 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
359 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, wxT("invalid notebook index") );
361 int selOld
= GetSelection();
363 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
365 wxGtkNotebookPage
* g_page
= GetNotebookPage( page
);
366 if (g_page
->m_client
)
367 g_page
->m_client
->SetFocus();
372 void wxNotebook::AdvanceSelection( bool forward
)
374 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
376 int max
= GetPageCount();
379 // nothing to do with empty notebook
383 int sel
= GetSelection();
386 SetSelection( sel
== max
- 1 ? 0 : sel
+ 1 );
388 SetSelection( sel
== 0 ? max
- 1 : sel
- 1 );
391 void wxNotebook::SetImageList( wxImageList
* imageList
)
393 if (m_ownsImageList
) delete m_imageList
;
394 m_imageList
= imageList
;
395 m_ownsImageList
= FALSE
;
398 void wxNotebook::AssignImageList( wxImageList
* imageList
)
400 SetImageList(imageList
);
401 m_ownsImageList
= TRUE
;
404 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
406 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
408 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
410 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
412 nb_page
->m_text
= text
;
414 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
419 bool wxNotebook::SetPageImage( int page
, int image
)
421 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
423 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
425 if (!nb_page
) return FALSE
;
427 /* Optimization posibility: return immediately if image unchanged.
428 * Not enabled because it may break existing (stupid) code that
429 * manipulates the imagelist to cycle images */
431 /* if (image == nb_page->m_image) return TRUE; */
433 /* For different cases:
434 1) no image -> no image
439 if (image
== -1 && nb_page
->m_image
== -1)
440 return TRUE
; /* Case 1): Nothing to do. */
442 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
444 if (nb_page
->m_image
!= -1)
446 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
448 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
451 if (GTK_IS_PIXMAP(child
->data
))
453 pixmapwid
= GTK_WIDGET(child
->data
);
459 /* We should have the pixmap widget now */
460 wxASSERT(pixmapwid
!= NULL
);
464 /* If there's no new widget, just remove the old from the box */
465 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
466 nb_page
->m_image
= -1;
468 return TRUE
; /* Case 2) */
472 /* Only cases 3) and 4) left */
473 wxASSERT( m_imageList
!= NULL
); /* Just in case */
475 /* Construct the new pixmap */
476 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
477 GdkPixmap
*pixmap
= bmp
->GetPixmap();
478 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
479 if ( bmp
->GetMask() )
481 mask
= bmp
->GetMask()->GetBitmap();
484 if (pixmapwid
== NULL
)
486 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
487 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
489 /* CHECKME: Are these pack flags okay? */
490 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
491 gtk_widget_show(pixmapwid
);
495 /* Case 4) Simply replace the pixmap */
496 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
499 nb_page
->m_image
= image
;
504 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
506 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
509 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
511 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
514 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
516 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
519 bool wxNotebook::DeleteAllPages()
521 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
523 while (m_pages
.GetCount() > 0)
524 DeletePage( m_pages
.GetCount()-1 );
529 bool wxNotebook::DeletePage( int page
)
531 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
532 if (!nb_page
) return FALSE
;
534 /* GTK sets GtkNotebook.cur_page to NULL before sending
535 the switch page event */
536 m_lastSelection
= GetSelection();
538 nb_page
->m_client
->Destroy();
539 m_pages
.DeleteObject( nb_page
);
541 m_lastSelection
= -1;
546 bool wxNotebook::RemovePage( int page
)
548 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
550 wxCHECK_MSG( nb_page
, FALSE
, _T("wxNotebook::RemovePage: invalid page") );
552 gtk_widget_ref( nb_page
->m_client
->m_widget
);
553 gtk_widget_unrealize( nb_page
->m_client
->m_widget
);
554 gtk_widget_unparent( nb_page
->m_client
->m_widget
);
556 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
558 m_pages
.DeleteObject( nb_page
);
563 bool wxNotebook::InsertPage( int position
, wxNotebookPage
* win
, const wxString
& text
,
564 bool select
, int imageId
)
566 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
568 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
569 wxT("Can't add a page whose parent is not the notebook!") );
571 /* don't receive switch page during addition */
572 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
573 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
576 win
->SetThemeEnabled(TRUE
);
578 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
580 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
583 m_pages
.Append( page
);
585 m_pages
.Insert( m_pages
.Nth( position
), page
);
587 page
->m_client
= win
;
589 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
590 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
592 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
593 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
596 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
598 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
600 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
602 /* set the label image */
603 page
->m_image
= imageId
;
607 wxASSERT( m_imageList
!= NULL
);
609 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
610 GdkPixmap
*pixmap
= bmp
->GetPixmap();
611 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
612 if ( bmp
->GetMask() )
614 mask
= bmp
->GetMask()->GetBitmap();
617 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
619 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
621 gtk_widget_show(pixmapwid
);
624 /* set the label text */
626 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
628 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
629 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
632 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
634 if (select
&& (m_pages
.GetCount() > 1))
637 SetSelection( GetPageCount()-1 );
639 SetSelection( position
);
642 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
643 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
648 bool wxNotebook::AddPage(wxNotebookPage
* win
, const wxString
& text
,
649 bool select
, int imageId
)
651 return InsertPage( -1, win
, text
, select
, imageId
);
654 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
656 if (event
.IsWindowChange())
657 AdvanceSelection( event
.GetDirection() );
662 wxNotebookPage
*wxNotebook::GetPage( int page
) const
664 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, wxT("invalid notebook") );
666 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
668 return (wxNotebookPage
*) NULL
;
670 return nb_page
->m_client
;
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 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
703 //-----------------------------------------------------------------------------
705 //-----------------------------------------------------------------------------
707 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)