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
61 m_page
= (GtkNotebookPage
*) NULL
;
62 m_client
= (wxWindow
*) NULL
;
63 m_parent
= (GtkNotebook
*) NULL
;
64 m_box
= (GtkWidget
*) NULL
;
69 mark page as "added' to the notebook, return FALSE if the page was
82 bool WasAdded() const { return m_added
; }
87 GtkNotebookPage
*m_page
;
90 GtkNotebook
*m_parent
;
91 GtkWidget
*m_box
; // in which the label and image are packed
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
101 static void gtk_notebook_page_change_callback(GtkNotebook
*WXUNUSED(widget
),
102 GtkNotebookPage
*WXUNUSED(page
),
106 if (g_isIdle
) wxapp_install_idle_handler();
108 wxNotebook
*notebook
= (wxNotebook
*)data
;
110 int old
= notebook
->GetSelection();
112 // TODO: emulate PAGE_CHANGING event
114 wxNotebookEvent
event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
115 notebook
->GetId(), nPage
, old
);
116 event
.SetEventObject( notebook
);
117 notebook
->GetEventHandler()->ProcessEvent( event
);
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
124 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
126 if (g_isIdle
) wxapp_install_idle_handler();
128 if ((win
->m_x
== alloc
->x
) &&
129 (win
->m_y
== alloc
->y
) &&
130 (win
->m_width
== alloc
->width
) &&
131 (win
->m_height
== alloc
->height
))
136 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
138 if (win
->GetAutoLayout()) win
->Layout();
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
146 gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*notebook
)
148 if (g_isIdle
) wxapp_install_idle_handler();
150 if (g_blockEventsOnDrag
) return FALSE
;
152 if (!notebook
->HasVMT()) return FALSE
;
154 /* this code makes jumping down from the handles of the notebooks
155 to the actual items in the visible notebook page possible with
156 the down-arrow key */
158 if (gdk_event
->keyval
!= GDK_Down
) return FALSE
;
160 if (notebook
!= notebook
->FindFocus()) return FALSE
;
162 if (notebook
->m_pages
.GetCount() == 0) return FALSE
;
164 wxNode
*node
= notebook
->m_pages
.Nth( notebook
->GetSelection() );
166 if (!node
) return FALSE
;
168 wxNotebookPage
*page
= (wxNotebookPage
*) node
->Data();
170 // don't let others the key event
171 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
173 page
->m_client
->SetFocus();
178 //-----------------------------------------------------------------------------
179 // InsertChild callback for wxNotebook
180 //-----------------------------------------------------------------------------
182 static void wxInsertChildInNotebook( wxNotebook
* parent
, wxWindow
* child
)
184 wxNotebookPage
*page
= new wxNotebookPage();
186 page
->m_id
= parent
->GetPageCount();
188 page
->m_box
= gtk_hbox_new (FALSE
, 0);
189 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
191 GtkNotebook
*notebook
= GTK_NOTEBOOK(parent
->m_widget
);
193 page
->m_client
= child
;
194 gtk_notebook_append_page( notebook
, child
->m_widget
, page
->m_box
);
196 page
->m_page
= (GtkNotebookPage
*) (g_list_last(notebook
->children
)->data
);
198 page
->m_parent
= notebook
;
200 gtk_signal_connect( GTK_OBJECT(child
->m_widget
), "size_allocate",
201 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)child
);
203 wxASSERT_MSG( page
->m_page
, _T("Notebook page creation error") );
205 parent
->m_pages
.Append( page
);
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
214 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
215 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
218 void wxNotebook::Init()
220 m_imageList
= (wxImageList
*) NULL
;
221 m_pages
.DeleteContents( TRUE
);
225 wxNotebook::wxNotebook()
230 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
231 const wxPoint
& pos
, const wxSize
& size
,
232 long style
, const wxString
& name
)
235 Create( parent
, id
, pos
, size
, style
, name
);
238 wxNotebook::~wxNotebook()
240 // don't generate change page events any more
241 if (m_idHandler
!= 0)
242 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
247 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
248 const wxPoint
& pos
, const wxSize
& size
,
249 long style
, const wxString
& name
)
252 m_acceptsFocus
= TRUE
;
253 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
255 PreCreation( parent
, id
, pos
, size
, style
, name
);
257 m_widget
= gtk_notebook_new();
260 debug_focus_in( m_widget
, _T("wxNotebook::m_widget"), name
);
263 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
265 m_idHandler
= gtk_signal_connect (
266 GTK_OBJECT(m_widget
), "switch_page",
267 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
270 m_parent
->AddChild( this );
272 (m_parent
->m_insertCallback
)( m_parent
, this );
274 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
275 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
284 int wxNotebook::GetSelection() const
286 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
288 if (m_pages
.Number() == 0) return -1;
290 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
291 if (!g_page
) return -1;
293 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
295 wxNode
*node
= m_pages
.First();
298 page
= (wxNotebookPage
*)node
->Data();
300 if ((page
->m_page
== g_page
) || (page
->m_page
== (GtkNotebookPage
*)NULL
))
302 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
303 // "switch_page" then and we ask for GetSelection() in the handler for
304 // "switch_page". otherwise m_page should never be NULL. all this
305 // might also be wrong.
311 wxCHECK_MSG( node
!= NULL
, -1, _T("wxNotebook: no selection?") );
316 int wxNotebook::GetPageCount() const
318 // count only the pages which were already added to the notebook for MSW
319 // compatibility (and, in fact, this behaviour makes more sense anyhow
320 // because only the added pages are shown)
323 for ( wxNode
*node
= m_pages
.First(); node
; node
= node
->Next() )
325 wxNotebookPage
*page
= (wxNotebookPage
*)node
->Data();
327 if (page
->WasAdded()) n
++;
333 int wxNotebook::GetRowCount() const
338 wxString
wxNotebook::GetPageText( int page
) const
340 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid notebook") );
342 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
344 return nb_page
->m_text
;
349 int wxNotebook::GetPageImage( int page
) const
351 wxCHECK_MSG( m_widget
!= NULL
, 0, _T("invalid notebook") );
353 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
355 return nb_page
->m_image
;
360 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
362 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*)NULL
, _T("invalid notebook") );
364 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
366 wxNode
*node
= m_pages
.First();
369 nb_page
= (wxNotebookPage
*)node
->Data();
370 if (nb_page
->m_id
== page
)
375 wxFAIL_MSG( _T("Notebook page not found!") );
377 return (wxNotebookPage
*) NULL
;
380 int wxNotebook::SetSelection( int page
)
382 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
384 int selOld
= GetSelection();
385 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
387 if (!nb_page
) return -1;
390 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
393 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
398 if (!child
) return -1;
400 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
405 void wxNotebook::AdvanceSelection( bool bForward
)
407 wxCHECK_RET( m_widget
!= NULL
, _T("invalid notebook") );
409 int sel
= GetSelection();
410 int max
= GetPageCount();
413 SetSelection( sel
== max
? 0 : sel
+ 1 );
415 SetSelection( sel
== 0 ? max
-1 : sel
- 1 );
418 void wxNotebook::SetImageList( wxImageList
* imageList
)
420 m_imageList
= imageList
;
423 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
425 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
427 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
429 wxCHECK_MSG( nb_page
, FALSE
, _T("SetPageText: invalid page index") );
431 nb_page
->m_text
= text
;
433 gtk_label_set(nb_page
->m_label
, nb_page
->m_text
.mbc_str());
438 bool wxNotebook::SetPageImage( int page
, int image
)
440 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
442 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
444 if (!nb_page
) return FALSE
;
446 /* Optimization posibility: return immediately if image unchanged.
447 * Not enabled because it may break existing (stupid) code that
448 * manipulates the imagelist to cycle images */
450 /* if (image == nb_page->m_image) return TRUE; */
452 /* For different cases:
453 1) no image -> no image
458 if (image
== -1 && nb_page
->m_image
== -1)
459 return TRUE
; /* Case 1): Nothing to do. */
461 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
463 if (nb_page
->m_image
!= -1)
465 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
467 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
470 if (GTK_IS_PIXMAP(child
->data
))
472 pixmapwid
= GTK_WIDGET(child
->data
);
478 /* We should have the pixmap widget now */
479 wxASSERT(pixmapwid
!= NULL
);
483 /* If there's no new widget, just remove the old from the box */
484 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
485 nb_page
->m_image
= -1;
487 return TRUE
; /* Case 2) */
491 /* Only cases 3) and 4) left */
492 wxASSERT( m_imageList
!= NULL
); /* Just in case */
494 /* Construct the new pixmap */
495 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
496 GdkPixmap
*pixmap
= bmp
->GetPixmap();
497 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
498 if ( bmp
->GetMask() )
500 mask
= bmp
->GetMask()->GetBitmap();
503 if (pixmapwid
== NULL
)
505 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
506 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
508 /* CHECKME: Are these pack flags okay? */
509 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
510 gtk_widget_show(pixmapwid
);
514 /* Case 4) Simply replace the pixmap */
515 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
518 nb_page
->m_image
= image
;
523 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
525 wxFAIL_MSG( _T("wxNotebook::SetPageSize not implemented") );
528 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
530 wxFAIL_MSG( _T("wxNotebook::SetPadding not implemented") );
533 void wxNotebook::SetTabSize(const wxSize
& sz
)
535 wxFAIL_MSG( _T("wxNotebook::SetTabSize not implemented") );
538 bool wxNotebook::DeleteAllPages()
540 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
542 wxNode
*page_node
= m_pages
.First();
545 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
547 DeletePage( page
->m_id
);
549 page_node
= m_pages
.First();
555 bool wxNotebook::DeletePage( int page
)
557 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
558 if (!nb_page
) return FALSE
;
561 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
564 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
569 wxCHECK_MSG( child
!= NULL
, FALSE
, _T("illegal notebook index") );
571 delete nb_page
->m_client
;
573 m_pages
.DeleteObject( nb_page
);
578 bool wxNotebook::RemovePage( int page
)
580 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
581 if (!nb_page
) return FALSE
;
584 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
587 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
592 wxCHECK_MSG( child
!= NULL
, FALSE
, _T("illegal notebook index") );
594 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page_num
);
596 m_pages
.DeleteObject( nb_page
);
601 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
602 bool select
, int imageId
)
604 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
606 /* we've created the notebook page in AddChild(). Now we just have to set
607 the caption for the page and set the others parameters. */
609 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
611 wxNode
*node
= m_pages
.First();
614 page
= (wxNotebookPage
*)node
->Data();
615 if ( page
->m_client
== win
) break;
619 wxCHECK_MSG( page
!= NULL
, FALSE
,
620 _T("Can't add a page whose parent is not the notebook!") );
622 wxCHECK_MSG( page
->Add(), FALSE
,
623 _T("Can't add the same page twice to a notebook.") );
627 wxASSERT( m_imageList
!= NULL
);
629 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
630 GdkPixmap
*pixmap
= bmp
->GetPixmap();
631 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
632 if ( bmp
->GetMask() )
634 mask
= bmp
->GetMask()->GetBitmap();
637 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
639 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
641 gtk_widget_show(pixmapwid
);
644 /* then set the attributes */
646 if (page
->m_text
.IsEmpty()) page
->m_text
= _T("");
647 page
->m_image
= imageId
;
648 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
.mbc_str());
649 gtk_box_pack_end( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
651 /* @@@: what does this do? do we still need it?
652 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); */
654 gtk_widget_show((GtkWidget
*)page
->m_label
);
656 if (select
) SetSelection( GetPageCount()-1 );
661 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
663 if (event
.IsWindowChange())
664 AdvanceSelection( event
.GetDirection() );
669 wxWindow
*wxNotebook::GetPage( int page
) const
671 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, _T("invalid notebook") );
673 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
675 return (wxWindow
*) NULL
;
677 return nb_page
->m_client
;
680 // override these 2 functions to do nothing: everything is done in OnSize
681 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
683 // don't set the sizes of the pages - their correct size is not yet known
684 wxControl::SetConstraintSizes(FALSE
);
687 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
692 void wxNotebook::ApplyWidgetStyle()
695 gtk_widget_set_style( m_widget
, m_widgetStyle
);
698 //-----------------------------------------------------------------------------
700 //-----------------------------------------------------------------------------
702 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)