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"
17 #include "wx/imaglist.h"
23 #include "wx/gtk/win_gtk.h"
24 #include "gdk/gdkkeysyms.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
45 extern void debug_focus_in( GtkWidget
* widget
, const wxChar
* name
, const wxChar
*window
);
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 class wxNotebookPage
: public wxObject
60 m_page
= (GtkNotebookPage
*) NULL
;
61 m_client
= (wxWindow
*) NULL
;
62 m_box
= (GtkWidget
*) NULL
;
67 GtkNotebookPage
*m_page
;
70 GtkWidget
*m_box
; // in which the label and image are packed
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
78 GtkNotebookPage
*WXUNUSED(page
),
80 wxNotebook
*notebook
)
83 wxapp_install_idle_handler();
85 int old
= notebook
->GetSelection();
87 wxNotebookEvent
event1( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING
,
88 notebook
->GetId(), page
, old
);
89 event1
.SetEventObject( notebook
);
91 if ((notebook
->GetEventHandler()->ProcessEvent( event1
)) &&
94 /* program doesn't allow the page change */
95 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook
->m_widget
), "switch_page" );
99 wxNotebookEvent
event2( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
100 notebook
->GetId(), page
, old
);
101 event2
.SetEventObject( notebook
);
102 notebook
->GetEventHandler()->ProcessEvent( event2
);
105 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
109 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
112 wxapp_install_idle_handler();
114 if ((win
->m_x
== alloc
->x
) &&
115 (win
->m_y
== alloc
->y
) &&
116 (win
->m_width
== alloc
->width
) &&
117 (win
->m_height
== alloc
->height
))
122 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
124 if (win
->GetAutoLayout()) win
->Layout();
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
132 gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*notebook
)
134 if (g_isIdle
) wxapp_install_idle_handler();
136 if (g_blockEventsOnDrag
) return FALSE
;
138 if (!notebook
->m_hasVMT
) return FALSE
;
140 /* this code makes jumping down from the handles of the notebooks
141 to the actual items in the visible notebook page possible with
142 the down-arrow key */
144 if (gdk_event
->keyval
!= GDK_Down
) return FALSE
;
146 if (notebook
!= notebook
->FindFocus()) return FALSE
;
148 if (notebook
->m_pages
.GetCount() == 0) return FALSE
;
150 wxNode
*node
= notebook
->m_pages
.Nth( notebook
->GetSelection() );
152 if (!node
) return FALSE
;
154 wxNotebookPage
*page
= (wxNotebookPage
*) node
->Data();
156 // don't let others the key event
157 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
159 page
->m_client
->SetFocus();
164 //-----------------------------------------------------------------------------
165 // InsertChild callback for wxNotebook
166 //-----------------------------------------------------------------------------
168 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
170 /* we don't do anything here but pray */
173 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
177 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
179 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
180 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
183 void wxNotebook::Init()
185 m_imageList
= (wxImageList
*) NULL
;
186 m_pages
.DeleteContents( TRUE
);
187 m_lastSelection
= -1;
190 wxNotebook::wxNotebook()
195 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
196 const wxPoint
& pos
, const wxSize
& size
,
197 long style
, const wxString
& name
)
200 Create( parent
, id
, pos
, size
, style
, name
);
203 wxNotebook::~wxNotebook()
205 /* don't generate change page events any more */
206 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
207 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
212 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
213 const wxPoint
& pos
, const wxSize
& size
,
214 long style
, const wxString
& name
)
217 m_acceptsFocus
= TRUE
;
218 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
220 PreCreation( parent
, id
, pos
, size
, style
, name
);
222 m_widget
= gtk_notebook_new();
225 debug_focus_in( m_widget
, _T("wxNotebook::m_widget"), name
);
228 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
230 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
231 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
233 m_parent
->DoAddChild( this );
235 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
236 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
245 int wxNotebook::GetSelection() const
247 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
249 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
251 if (g_list_length(pages
) == 0) return -1;
253 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
255 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
257 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
260 int wxNotebook::GetPageCount() const
262 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
265 int wxNotebook::GetRowCount() const
270 wxString
wxNotebook::GetPageText( int page
) const
272 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid notebook") );
274 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
276 return nb_page
->m_text
;
281 int wxNotebook::GetPageImage( int page
) const
283 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
285 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
287 return nb_page
->m_image
;
292 wxNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
294 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*) NULL
, _T("invalid notebook") );
296 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxNotebookPage
*) NULL
, _T("invalid notebook index") );
298 wxNode
*node
= m_pages
.Nth( page
);
300 return (wxNotebookPage
*) node
->Data();
303 int wxNotebook::SetSelection( int page
)
305 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
307 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, _T("invalid notebook index") );
309 int selOld
= GetSelection();
311 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
316 void wxNotebook::AdvanceSelection( bool forward
)
318 wxCHECK_RET( m_widget
!= NULL
, _T("invalid notebook") );
320 int sel
= GetSelection();
321 int max
= GetPageCount();
324 SetSelection( sel
== max
? 0 : sel
+ 1 );
326 SetSelection( sel
== 0 ? max
-1 : sel
- 1 );
329 void wxNotebook::SetImageList( wxImageList
* imageList
)
331 m_imageList
= imageList
;
334 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
336 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
338 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
340 wxCHECK_MSG( nb_page
, FALSE
, _T("SetPageText: invalid page index") );
342 nb_page
->m_text
= text
;
344 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
349 bool wxNotebook::SetPageImage( int page
, int image
)
351 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
353 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
355 if (!nb_page
) return FALSE
;
357 /* Optimization posibility: return immediately if image unchanged.
358 * Not enabled because it may break existing (stupid) code that
359 * manipulates the imagelist to cycle images */
361 /* if (image == nb_page->m_image) return TRUE; */
363 /* For different cases:
364 1) no image -> no image
369 if (image
== -1 && nb_page
->m_image
== -1)
370 return TRUE
; /* Case 1): Nothing to do. */
372 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
374 if (nb_page
->m_image
!= -1)
376 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
378 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
381 if (GTK_IS_PIXMAP(child
->data
))
383 pixmapwid
= GTK_WIDGET(child
->data
);
389 /* We should have the pixmap widget now */
390 wxASSERT(pixmapwid
!= NULL
);
394 /* If there's no new widget, just remove the old from the box */
395 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
396 nb_page
->m_image
= -1;
398 return TRUE
; /* Case 2) */
402 /* Only cases 3) and 4) left */
403 wxASSERT( m_imageList
!= NULL
); /* Just in case */
405 /* Construct the new pixmap */
406 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
407 GdkPixmap
*pixmap
= bmp
->GetPixmap();
408 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
409 if ( bmp
->GetMask() )
411 mask
= bmp
->GetMask()->GetBitmap();
414 if (pixmapwid
== NULL
)
416 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
417 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
419 /* CHECKME: Are these pack flags okay? */
420 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
421 gtk_widget_show(pixmapwid
);
425 /* Case 4) Simply replace the pixmap */
426 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
429 nb_page
->m_image
= image
;
434 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
436 wxFAIL_MSG( _T("wxNotebook::SetPageSize not implemented") );
439 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
441 wxFAIL_MSG( _T("wxNotebook::SetPadding not implemented") );
444 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
446 wxFAIL_MSG( _T("wxNotebook::SetTabSize not implemented") );
449 bool wxNotebook::DeleteAllPages()
451 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
453 while (m_pages
.GetCount() > 0)
454 DeletePage( m_pages
.GetCount()-1 );
459 bool wxNotebook::DeletePage( int page
)
461 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
462 if (!nb_page
) return FALSE
;
464 /* GTK sets GtkNotebook.cur_page to NULL before sending
465 the switvh page event */
466 m_lastSelection
= GetSelection();
468 nb_page
->m_client
->Destroy();
469 m_pages
.DeleteObject( nb_page
);
471 m_lastSelection
= -1;
476 bool wxNotebook::RemovePage( int page
)
478 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
480 if (!nb_page
) return FALSE
;
482 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
484 m_pages
.DeleteObject( nb_page
);
489 bool wxNotebook::InsertPage( int position
, wxWindow
* win
, const wxString
& text
,
490 bool select
, int imageId
)
492 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
494 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
495 _T("Can't add a page whose parent is not the notebook!") );
497 /* don't receive switch page during addition */
498 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
499 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
501 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
503 wxNotebookPage
*page
= new wxNotebookPage();
506 m_pages
.Append( page
);
508 m_pages
.Insert( m_pages
.Nth( position
), page
);
510 page
->m_client
= win
;
512 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
513 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
516 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
518 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
520 page
->m_page
= GTK_NOTEBOOK_PAGE( g_list_last(notebook
->children
)->data
);
522 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
523 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
525 /* set the label image */
526 page
->m_image
= imageId
;
530 wxASSERT( m_imageList
!= NULL
);
532 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
533 GdkPixmap
*pixmap
= bmp
->GetPixmap();
534 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
535 if ( bmp
->GetMask() )
537 mask
= bmp
->GetMask()->GetBitmap();
540 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
542 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
544 gtk_widget_show(pixmapwid
);
547 /* set the label text */
549 if (page
->m_text
.IsEmpty()) page
->m_text
= _T("");
551 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
552 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
555 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
557 if (select
&& (m_pages
.GetCount() > 1))
560 SetSelection( GetPageCount()-1 );
562 SetSelection( position
);
565 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
566 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
571 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
572 bool select
, int imageId
)
574 return InsertPage( -1, win
, text
, select
, imageId
);
577 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
579 if (event
.IsWindowChange())
580 AdvanceSelection( event
.GetDirection() );
585 wxWindow
*wxNotebook::GetPage( int page
) const
587 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, _T("invalid notebook") );
589 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
591 return (wxWindow
*) NULL
;
593 return nb_page
->m_client
;
596 // override these 2 functions to do nothing: everything is done in OnSize
597 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
599 // don't set the sizes of the pages - their correct size is not yet known
600 wxControl::SetConstraintSizes(FALSE
);
603 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
608 void wxNotebook::ApplyWidgetStyle()
611 gtk_widget_set_style( m_widget
, m_widgetStyle
);
614 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
616 return ((m_widget
->window
== window
) ||
617 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
620 //-----------------------------------------------------------------------------
622 //-----------------------------------------------------------------------------
624 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)