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 wxNotebookPage
: public wxObject
63 m_page
= (GtkNotebookPage
*) NULL
;
64 m_client
= (wxWindow
*) NULL
;
65 m_box
= (GtkWidget
*) NULL
;
70 GtkNotebookPage
*m_page
;
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 wxNotebookPage
*page
= (wxNotebookPage
*) 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
.SetCurrentFocus( win
);
182 if (!page
->m_client
->GetEventHandler()->ProcessEvent( event
))
184 page
->m_client
->SetFocus();
187 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
194 //-----------------------------------------------------------------------------
195 // InsertChild callback for wxNotebook
196 //-----------------------------------------------------------------------------
198 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
200 /* we don't do anything here but pray */
203 //-----------------------------------------------------------------------------
205 //-----------------------------------------------------------------------------
207 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
209 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
210 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
213 void wxNotebook::Init()
215 m_imageList
= (wxImageList
*) NULL
;
216 m_pages
.DeleteContents( TRUE
);
217 m_lastSelection
= -1;
220 wxNotebook::wxNotebook()
225 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
226 const wxPoint
& pos
, const wxSize
& size
,
227 long style
, const wxString
& name
)
230 Create( parent
, id
, pos
, size
, style
, name
);
233 wxNotebook::~wxNotebook()
235 /* don't generate change page events any more */
236 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
237 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
242 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
243 const wxPoint
& pos
, const wxSize
& size
,
244 long style
, const wxString
& name
)
247 m_acceptsFocus
= TRUE
;
248 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
250 if (!PreCreation( parent
, pos
, size
) ||
251 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
253 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
258 m_widget
= gtk_notebook_new();
261 debug_focus_in( m_widget
, wxT("wxNotebook::m_widget"), name
);
264 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
266 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
267 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
269 m_parent
->DoAddChild( this );
271 if (m_windowStyle
& wxNB_RIGHT
)
272 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
273 if (m_windowStyle
& wxNB_LEFT
)
274 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
275 if (m_windowStyle
& wxNB_BOTTOM
)
276 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
278 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
279 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
283 SetFont( parent
->GetFont() );
285 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
286 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
293 int wxNotebook::GetSelection() const
295 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
297 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
299 if (g_list_length(pages
) == 0) return -1;
301 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
303 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
305 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
308 int wxNotebook::GetPageCount() const
310 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
313 int wxNotebook::GetRowCount() const
318 wxString
wxNotebook::GetPageText( int page
) const
320 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
322 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
324 return nb_page
->m_text
;
329 int wxNotebook::GetPageImage( int page
) const
331 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
333 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
335 return nb_page
->m_image
;
340 wxNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
342 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*) NULL
, wxT("invalid notebook") );
344 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxNotebookPage
*) NULL
, wxT("invalid notebook index") );
346 wxNode
*node
= m_pages
.Nth( page
);
348 return (wxNotebookPage
*) node
->Data();
351 int wxNotebook::SetSelection( int page
)
353 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
355 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, wxT("invalid notebook index") );
357 int selOld
= GetSelection();
359 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
364 void wxNotebook::AdvanceSelection( bool forward
)
366 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
368 int sel
= GetSelection();
369 int max
= GetPageCount();
372 SetSelection( sel
== max
? 0 : sel
+ 1 );
374 SetSelection( sel
== 0 ? max
-1 : sel
- 1 );
377 void wxNotebook::SetImageList( wxImageList
* imageList
)
379 m_imageList
= imageList
;
382 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
384 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
386 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
388 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
390 nb_page
->m_text
= text
;
392 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
397 bool wxNotebook::SetPageImage( int page
, int image
)
399 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
401 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
403 if (!nb_page
) return FALSE
;
405 /* Optimization posibility: return immediately if image unchanged.
406 * Not enabled because it may break existing (stupid) code that
407 * manipulates the imagelist to cycle images */
409 /* if (image == nb_page->m_image) return TRUE; */
411 /* For different cases:
412 1) no image -> no image
417 if (image
== -1 && nb_page
->m_image
== -1)
418 return TRUE
; /* Case 1): Nothing to do. */
420 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
422 if (nb_page
->m_image
!= -1)
424 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
426 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
429 if (GTK_IS_PIXMAP(child
->data
))
431 pixmapwid
= GTK_WIDGET(child
->data
);
437 /* We should have the pixmap widget now */
438 wxASSERT(pixmapwid
!= NULL
);
442 /* If there's no new widget, just remove the old from the box */
443 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
444 nb_page
->m_image
= -1;
446 return TRUE
; /* Case 2) */
450 /* Only cases 3) and 4) left */
451 wxASSERT( m_imageList
!= NULL
); /* Just in case */
453 /* Construct the new pixmap */
454 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
455 GdkPixmap
*pixmap
= bmp
->GetPixmap();
456 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
457 if ( bmp
->GetMask() )
459 mask
= bmp
->GetMask()->GetBitmap();
462 if (pixmapwid
== NULL
)
464 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
465 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
467 /* CHECKME: Are these pack flags okay? */
468 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
469 gtk_widget_show(pixmapwid
);
473 /* Case 4) Simply replace the pixmap */
474 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
477 nb_page
->m_image
= image
;
482 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
484 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
487 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
489 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
492 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
494 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
497 bool wxNotebook::DeleteAllPages()
499 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
501 while (m_pages
.GetCount() > 0)
502 DeletePage( m_pages
.GetCount()-1 );
507 bool wxNotebook::DeletePage( int page
)
509 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
510 if (!nb_page
) return FALSE
;
512 /* GTK sets GtkNotebook.cur_page to NULL before sending
513 the switvh page event */
514 m_lastSelection
= GetSelection();
516 nb_page
->m_client
->Destroy();
517 m_pages
.DeleteObject( nb_page
);
519 m_lastSelection
= -1;
524 bool wxNotebook::RemovePage( int page
)
526 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
528 if (!nb_page
) return FALSE
;
530 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
532 m_pages
.DeleteObject( nb_page
);
537 bool wxNotebook::InsertPage( int position
, wxWindow
* win
, const wxString
& text
,
538 bool select
, int imageId
)
540 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
542 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
543 wxT("Can't add a page whose parent is not the notebook!") );
545 /* don't receive switch page during addition */
546 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
547 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
549 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
551 wxNotebookPage
*page
= new wxNotebookPage();
554 m_pages
.Append( page
);
556 m_pages
.Insert( m_pages
.Nth( position
), page
);
558 page
->m_client
= win
;
560 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
561 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
563 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
564 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
567 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
569 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
571 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
573 /* set the label image */
574 page
->m_image
= imageId
;
578 wxASSERT( m_imageList
!= NULL
);
580 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
581 GdkPixmap
*pixmap
= bmp
->GetPixmap();
582 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
583 if ( bmp
->GetMask() )
585 mask
= bmp
->GetMask()->GetBitmap();
588 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
590 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
592 gtk_widget_show(pixmapwid
);
595 /* set the label text */
597 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
599 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
600 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
603 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
605 if (select
&& (m_pages
.GetCount() > 1))
608 SetSelection( GetPageCount()-1 );
610 SetSelection( position
);
613 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
614 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
619 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
620 bool select
, int imageId
)
622 return InsertPage( -1, win
, text
, select
, imageId
);
625 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
627 if (event
.IsWindowChange())
628 AdvanceSelection( event
.GetDirection() );
633 wxWindow
*wxNotebook::GetPage( int page
) const
635 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, wxT("invalid notebook") );
637 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
639 return (wxWindow
*) NULL
;
641 return nb_page
->m_client
;
644 // override these 2 functions to do nothing: everything is done in OnSize
645 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
647 // don't set the sizes of the pages - their correct size is not yet known
648 wxControl::SetConstraintSizes(FALSE
);
651 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
656 void wxNotebook::ApplyWidgetStyle()
658 // TODO, font for labels etc
661 gtk_widget_set_style( m_widget
, m_widgetStyle
);
664 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
666 return ((m_widget
->window
== window
) ||
667 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
670 //-----------------------------------------------------------------------------
672 //-----------------------------------------------------------------------------
674 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)