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_pages
.DeleteContents( TRUE
);
218 m_lastSelection
= -1;
219 m_themeEnabled
= TRUE
;
222 wxNotebook::wxNotebook()
227 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
228 const wxPoint
& pos
, const wxSize
& size
,
229 long style
, const wxString
& name
)
232 Create( parent
, id
, pos
, size
, style
, name
);
235 wxNotebook::~wxNotebook()
237 /* don't generate change page events any more */
238 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
239 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
244 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
245 const wxPoint
& pos
, const wxSize
& size
,
246 long style
, const wxString
& name
)
249 m_acceptsFocus
= TRUE
;
250 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
252 if (!PreCreation( parent
, pos
, size
) ||
253 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
255 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
260 m_widget
= gtk_notebook_new();
263 debug_focus_in( m_widget
, wxT("wxNotebook::m_widget"), name
);
266 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
268 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
269 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
271 m_parent
->DoAddChild( this );
273 if (m_windowStyle
& wxNB_RIGHT
)
274 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
275 if (m_windowStyle
& wxNB_LEFT
)
276 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
277 if (m_windowStyle
& wxNB_BOTTOM
)
278 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
280 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
281 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
285 SetFont( parent
->GetFont() );
287 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
288 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
295 int wxNotebook::GetSelection() const
297 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
299 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
301 if (g_list_length(pages
) == 0) return -1;
303 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
305 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
307 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
310 int wxNotebook::GetPageCount() const
312 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
315 int wxNotebook::GetRowCount() const
320 wxString
wxNotebook::GetPageText( int page
) const
322 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
324 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
326 return nb_page
->m_text
;
331 int wxNotebook::GetPageImage( int page
) const
333 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
335 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
337 return nb_page
->m_image
;
342 wxGtkNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
344 wxCHECK_MSG( m_widget
!= NULL
, (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook") );
346 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxGtkNotebookPage
*) NULL
, wxT("invalid notebook index") );
348 wxNode
*node
= m_pages
.Nth( page
);
350 return (wxGtkNotebookPage
*) node
->Data();
353 int wxNotebook::SetSelection( int page
)
355 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
357 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, wxT("invalid notebook index") );
359 int selOld
= GetSelection();
361 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
363 wxGtkNotebookPage
* g_page
= GetNotebookPage( page
);
364 if (g_page
->m_client
)
365 g_page
->m_client
->SetFocus();
370 void wxNotebook::AdvanceSelection( bool forward
)
372 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
374 int max
= GetPageCount();
377 // nothing to do with empty notebook
381 int sel
= GetSelection();
384 SetSelection( sel
== max
- 1 ? 0 : sel
+ 1 );
386 SetSelection( sel
== 0 ? max
- 1 : sel
- 1 );
389 void wxNotebook::SetImageList( wxImageList
* imageList
)
391 m_imageList
= imageList
;
394 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
396 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
398 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
400 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
402 nb_page
->m_text
= text
;
404 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
409 bool wxNotebook::SetPageImage( int page
, int image
)
411 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
413 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
415 if (!nb_page
) return FALSE
;
417 /* Optimization posibility: return immediately if image unchanged.
418 * Not enabled because it may break existing (stupid) code that
419 * manipulates the imagelist to cycle images */
421 /* if (image == nb_page->m_image) return TRUE; */
423 /* For different cases:
424 1) no image -> no image
429 if (image
== -1 && nb_page
->m_image
== -1)
430 return TRUE
; /* Case 1): Nothing to do. */
432 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
434 if (nb_page
->m_image
!= -1)
436 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
438 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
441 if (GTK_IS_PIXMAP(child
->data
))
443 pixmapwid
= GTK_WIDGET(child
->data
);
449 /* We should have the pixmap widget now */
450 wxASSERT(pixmapwid
!= NULL
);
454 /* If there's no new widget, just remove the old from the box */
455 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
456 nb_page
->m_image
= -1;
458 return TRUE
; /* Case 2) */
462 /* Only cases 3) and 4) left */
463 wxASSERT( m_imageList
!= NULL
); /* Just in case */
465 /* Construct the new pixmap */
466 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
467 GdkPixmap
*pixmap
= bmp
->GetPixmap();
468 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
469 if ( bmp
->GetMask() )
471 mask
= bmp
->GetMask()->GetBitmap();
474 if (pixmapwid
== NULL
)
476 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
477 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
479 /* CHECKME: Are these pack flags okay? */
480 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
481 gtk_widget_show(pixmapwid
);
485 /* Case 4) Simply replace the pixmap */
486 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
489 nb_page
->m_image
= image
;
494 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
496 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
499 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
501 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
504 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
506 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
509 bool wxNotebook::DeleteAllPages()
511 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
513 while (m_pages
.GetCount() > 0)
514 DeletePage( m_pages
.GetCount()-1 );
519 bool wxNotebook::DeletePage( int page
)
521 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
522 if (!nb_page
) return FALSE
;
524 /* GTK sets GtkNotebook.cur_page to NULL before sending
525 the switch page event */
526 m_lastSelection
= GetSelection();
528 nb_page
->m_client
->Destroy();
529 m_pages
.DeleteObject( nb_page
);
531 m_lastSelection
= -1;
536 bool wxNotebook::RemovePage( int page
)
538 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
540 wxCHECK_MSG( nb_page
, FALSE
, _T("wxNotebook::RemovePage: invalid page") );
542 gtk_widget_ref( nb_page
->m_client
->m_widget
);
543 gtk_widget_unrealize( nb_page
->m_client
->m_widget
);
544 gtk_widget_unparent( nb_page
->m_client
->m_widget
);
546 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
548 m_pages
.DeleteObject( nb_page
);
553 bool wxNotebook::InsertPage( int position
, wxNotebookPage
* win
, const wxString
& text
,
554 bool select
, int imageId
)
556 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
558 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
559 wxT("Can't add a page whose parent is not the notebook!") );
561 /* don't receive switch page during addition */
562 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
563 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
566 win
->SetThemeEnabled(TRUE
);
568 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
570 wxGtkNotebookPage
*page
= new wxGtkNotebookPage();
573 m_pages
.Append( page
);
575 m_pages
.Insert( m_pages
.Nth( position
), page
);
577 page
->m_client
= win
;
579 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
580 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
582 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
583 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
586 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
588 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
590 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
592 /* set the label image */
593 page
->m_image
= imageId
;
597 wxASSERT( m_imageList
!= NULL
);
599 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
600 GdkPixmap
*pixmap
= bmp
->GetPixmap();
601 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
602 if ( bmp
->GetMask() )
604 mask
= bmp
->GetMask()->GetBitmap();
607 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
609 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
611 gtk_widget_show(pixmapwid
);
614 /* set the label text */
616 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
618 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
619 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
622 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
624 if (select
&& (m_pages
.GetCount() > 1))
627 SetSelection( GetPageCount()-1 );
629 SetSelection( position
);
632 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
633 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
638 bool wxNotebook::AddPage(wxNotebookPage
* win
, const wxString
& text
,
639 bool select
, int imageId
)
641 return InsertPage( -1, win
, text
, select
, imageId
);
644 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
646 if (event
.IsWindowChange())
647 AdvanceSelection( event
.GetDirection() );
652 wxNotebookPage
*wxNotebook::GetPage( int page
) const
654 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, wxT("invalid notebook") );
656 wxGtkNotebookPage
* nb_page
= GetNotebookPage(page
);
658 return (wxNotebookPage
*) NULL
;
660 return nb_page
->m_client
;
663 #if wxUSE_CONSTRAINTS
665 // override these 2 functions to do nothing: everything is done in OnSize
666 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
668 // don't set the sizes of the pages - their correct size is not yet known
669 wxControl::SetConstraintSizes(FALSE
);
672 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
679 void wxNotebook::ApplyWidgetStyle()
681 // TODO, font for labels etc
684 gtk_widget_set_style( m_widget
, m_widgetStyle
);
687 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
689 return ((m_widget
->window
== window
) ||
690 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
693 //-----------------------------------------------------------------------------
695 //-----------------------------------------------------------------------------
697 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)