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 if (win
->GetAutoLayout()) win
->Layout();
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
135 gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*notebook
)
137 if (g_isIdle
) wxapp_install_idle_handler();
139 if (g_blockEventsOnDrag
) return FALSE
;
141 if (!notebook
->m_hasVMT
) return FALSE
;
143 /* this code makes jumping down from the handles of the notebooks
144 to the actual items in the visible notebook page possible with
145 the down-arrow key */
147 if (gdk_event
->keyval
!= GDK_Down
) return FALSE
;
149 if (notebook
!= notebook
->FindFocus()) return FALSE
;
151 if (notebook
->m_pages
.GetCount() == 0) return FALSE
;
153 wxNode
*node
= notebook
->m_pages
.Nth( notebook
->GetSelection() );
155 if (!node
) return FALSE
;
157 wxNotebookPage
*page
= (wxNotebookPage
*) node
->Data();
159 // don't let others the key event
160 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
162 page
->m_client
->SetFocus();
167 //-----------------------------------------------------------------------------
168 // InsertChild callback for wxNotebook
169 //-----------------------------------------------------------------------------
171 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
173 /* we don't do anything here but pray */
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
180 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
182 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
183 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
186 void wxNotebook::Init()
188 m_imageList
= (wxImageList
*) NULL
;
189 m_pages
.DeleteContents( TRUE
);
190 m_lastSelection
= -1;
193 wxNotebook::wxNotebook()
198 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
199 const wxPoint
& pos
, const wxSize
& size
,
200 long style
, const wxString
& name
)
203 Create( parent
, id
, pos
, size
, style
, name
);
206 wxNotebook::~wxNotebook()
208 /* don't generate change page events any more */
209 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
210 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
215 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
216 const wxPoint
& pos
, const wxSize
& size
,
217 long style
, const wxString
& name
)
220 m_acceptsFocus
= TRUE
;
221 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
223 if (!PreCreation( parent
, pos
, size
) ||
224 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
226 wxFAIL_MSG( _T("wxNoteBook creation failed") );
231 m_widget
= gtk_notebook_new();
234 debug_focus_in( m_widget
, _T("wxNotebook::m_widget"), name
);
237 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
239 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
240 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
242 m_parent
->DoAddChild( this );
244 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
245 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
254 int wxNotebook::GetSelection() const
256 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
258 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
260 if (g_list_length(pages
) == 0) return -1;
262 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
264 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
266 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
269 int wxNotebook::GetPageCount() const
271 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
274 int wxNotebook::GetRowCount() const
279 wxString
wxNotebook::GetPageText( int page
) const
281 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid notebook") );
283 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
285 return nb_page
->m_text
;
290 int wxNotebook::GetPageImage( int page
) const
292 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
294 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
296 return nb_page
->m_image
;
301 wxNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
303 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*) NULL
, _T("invalid notebook") );
305 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxNotebookPage
*) NULL
, _T("invalid notebook index") );
307 wxNode
*node
= m_pages
.Nth( page
);
309 return (wxNotebookPage
*) node
->Data();
312 int wxNotebook::SetSelection( int page
)
314 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
316 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, _T("invalid notebook index") );
318 int selOld
= GetSelection();
320 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
325 void wxNotebook::AdvanceSelection( bool forward
)
327 wxCHECK_RET( m_widget
!= NULL
, _T("invalid notebook") );
329 int sel
= GetSelection();
330 int max
= GetPageCount();
333 SetSelection( sel
== max
? 0 : sel
+ 1 );
335 SetSelection( sel
== 0 ? max
-1 : sel
- 1 );
338 void wxNotebook::SetImageList( wxImageList
* imageList
)
340 m_imageList
= imageList
;
343 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
345 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
347 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
349 wxCHECK_MSG( nb_page
, FALSE
, _T("SetPageText: invalid page index") );
351 nb_page
->m_text
= text
;
353 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
358 bool wxNotebook::SetPageImage( int page
, int image
)
360 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
362 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
364 if (!nb_page
) return FALSE
;
366 /* Optimization posibility: return immediately if image unchanged.
367 * Not enabled because it may break existing (stupid) code that
368 * manipulates the imagelist to cycle images */
370 /* if (image == nb_page->m_image) return TRUE; */
372 /* For different cases:
373 1) no image -> no image
378 if (image
== -1 && nb_page
->m_image
== -1)
379 return TRUE
; /* Case 1): Nothing to do. */
381 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
383 if (nb_page
->m_image
!= -1)
385 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
387 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
390 if (GTK_IS_PIXMAP(child
->data
))
392 pixmapwid
= GTK_WIDGET(child
->data
);
398 /* We should have the pixmap widget now */
399 wxASSERT(pixmapwid
!= NULL
);
403 /* If there's no new widget, just remove the old from the box */
404 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
405 nb_page
->m_image
= -1;
407 return TRUE
; /* Case 2) */
411 /* Only cases 3) and 4) left */
412 wxASSERT( m_imageList
!= NULL
); /* Just in case */
414 /* Construct the new pixmap */
415 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
416 GdkPixmap
*pixmap
= bmp
->GetPixmap();
417 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
418 if ( bmp
->GetMask() )
420 mask
= bmp
->GetMask()->GetBitmap();
423 if (pixmapwid
== NULL
)
425 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
426 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
428 /* CHECKME: Are these pack flags okay? */
429 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
430 gtk_widget_show(pixmapwid
);
434 /* Case 4) Simply replace the pixmap */
435 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
438 nb_page
->m_image
= image
;
443 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
445 wxFAIL_MSG( _T("wxNotebook::SetPageSize not implemented") );
448 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
450 wxFAIL_MSG( _T("wxNotebook::SetPadding not implemented") );
453 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
455 wxFAIL_MSG( _T("wxNotebook::SetTabSize not implemented") );
458 bool wxNotebook::DeleteAllPages()
460 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
462 while (m_pages
.GetCount() > 0)
463 DeletePage( m_pages
.GetCount()-1 );
468 bool wxNotebook::DeletePage( int page
)
470 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
471 if (!nb_page
) return FALSE
;
473 /* GTK sets GtkNotebook.cur_page to NULL before sending
474 the switvh page event */
475 m_lastSelection
= GetSelection();
477 nb_page
->m_client
->Destroy();
478 m_pages
.DeleteObject( nb_page
);
480 m_lastSelection
= -1;
485 bool wxNotebook::RemovePage( int page
)
487 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
489 if (!nb_page
) return FALSE
;
491 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
493 m_pages
.DeleteObject( nb_page
);
498 bool wxNotebook::InsertPage( int position
, wxWindow
* win
, const wxString
& text
,
499 bool select
, int imageId
)
501 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
503 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
504 _T("Can't add a page whose parent is not the notebook!") );
506 /* don't receive switch page during addition */
507 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
508 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
510 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
512 wxNotebookPage
*page
= new wxNotebookPage();
515 m_pages
.Append( page
);
517 m_pages
.Insert( m_pages
.Nth( position
), page
);
519 page
->m_client
= win
;
521 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
522 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
524 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
525 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
528 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
530 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
532 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
534 /* set the label image */
535 page
->m_image
= imageId
;
539 wxASSERT( m_imageList
!= NULL
);
541 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
542 GdkPixmap
*pixmap
= bmp
->GetPixmap();
543 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
544 if ( bmp
->GetMask() )
546 mask
= bmp
->GetMask()->GetBitmap();
549 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
551 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
553 gtk_widget_show(pixmapwid
);
556 /* set the label text */
558 if (page
->m_text
.IsEmpty()) page
->m_text
= _T("");
560 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
561 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
564 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
566 if (select
&& (m_pages
.GetCount() > 1))
569 SetSelection( GetPageCount()-1 );
571 SetSelection( position
);
574 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
575 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
580 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
581 bool select
, int imageId
)
583 return InsertPage( -1, win
, text
, select
, imageId
);
586 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
588 if (event
.IsWindowChange())
589 AdvanceSelection( event
.GetDirection() );
594 wxWindow
*wxNotebook::GetPage( int page
) const
596 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, _T("invalid notebook") );
598 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
600 return (wxWindow
*) NULL
;
602 return nb_page
->m_client
;
605 // override these 2 functions to do nothing: everything is done in OnSize
606 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
608 // don't set the sizes of the pages - their correct size is not yet known
609 wxControl::SetConstraintSizes(FALSE
);
612 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
617 void wxNotebook::ApplyWidgetStyle()
620 gtk_widget_set_style( m_widget
, m_widgetStyle
);
623 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
625 return ((m_widget
->window
== window
) ||
626 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
629 //-----------------------------------------------------------------------------
631 //-----------------------------------------------------------------------------
633 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)