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"
20 #include "gdk/gdkkeysyms.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 class wxNotebookPage
: public wxObject
40 m_page
= (GtkNotebookPage
*) NULL
;
41 m_client
= (wxWindow
*) NULL
;
42 m_parent
= (GtkNotebook
*) NULL
;
43 m_box
= (GtkWidget
*) NULL
;
48 mark page as "added' to the notebook, return FALSE if the page was
61 bool WasAdded() const { return m_added
; }
66 GtkNotebookPage
*m_page
;
69 GtkNotebook
*m_parent
;
70 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
),
85 wxNotebook
*notebook
= (wxNotebook
*)data
;
87 int old
= notebook
->GetSelection();
89 // TODO: emulate PAGE_CHANGING event
91 wxNotebookEvent
event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED
,
92 notebook
->GetId(), nPage
, old
);
93 event
.SetEventObject( notebook
);
94 notebook
->GetEventHandler()->ProcessEvent( event
);
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
101 static void gtk_page_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxWindow
*win
)
103 if ((win
->m_x
== alloc
->x
) &&
104 (win
->m_y
== alloc
->y
) &&
105 (win
->m_width
== alloc
->width
) &&
106 (win
->m_height
== alloc
->height
))
111 win
->SetSize( alloc
->x
, alloc
->y
, alloc
->width
, alloc
->height
);
113 if (win
->GetAutoLayout()) win
->Layout();
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
121 gtk_notebook_key_press_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxNotebook
*notebook
)
123 if (g_blockEventsOnDrag
) return FALSE
;
125 if (!notebook
->HasVMT()) return FALSE
;
127 if (gdk_event
->keyval
!= GDK_Down
) return FALSE
;
129 if (notebook
!= notebook
->FindFocus()) return FALSE
;
131 if (notebook
->m_pages
.GetCount() == 0) return FALSE
;
133 wxNode
*node
= notebook
->m_pages
.Nth( notebook
->GetSelection() );
135 if (!node
) return FALSE
;
137 wxNotebookPage
*page
= (wxNotebookPage
*) node
->Data();
139 // don't let others the key event
140 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
142 page
->m_client
->SetFocus();
147 //-----------------------------------------------------------------------------
148 // InsertChild callback for wxNotebook
149 //-----------------------------------------------------------------------------
151 static void wxInsertChildInNotebook( wxNotebook
* parent
, wxWindow
* child
)
153 wxNotebookPage
*page
= new wxNotebookPage();
155 page
->m_id
= parent
->GetPageCount();
157 page
->m_box
= gtk_hbox_new (FALSE
, 0);
158 gtk_container_border_width(GTK_CONTAINER(page
->m_box
), 2);
160 GtkNotebook
*notebook
= GTK_NOTEBOOK(parent
->m_widget
);
162 page
->m_client
= child
;
163 gtk_notebook_append_page( notebook
, child
->m_widget
, page
->m_box
);
165 page
->m_page
= (GtkNotebookPage
*) (g_list_last(notebook
->children
)->data
);
167 page
->m_parent
= notebook
;
169 gtk_signal_connect( GTK_OBJECT(child
->m_widget
), "size_allocate",
170 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)child
);
172 wxASSERT_MSG( page
->m_page
, "Notebook page creation error" );
174 parent
->m_pages
.Append( page
);
177 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
181 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
183 void wxNotebook::Init()
185 m_imageList
= (wxImageList
*) NULL
;
186 m_pages
.DeleteContents( TRUE
);
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 if (m_idHandler
!= 0)
207 gtk_signal_disconnect(GTK_OBJECT(m_widget
), m_idHandler
);
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
, "wxNotebook::m_widget", name
);
228 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
230 m_idHandler
= gtk_signal_connect (
231 GTK_OBJECT(m_widget
), "switch_page",
232 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
),
235 m_parent
->AddChild( this );
237 (m_parent
->m_insertCallback
)( m_parent
, this );
239 gtk_signal_connect( GTK_OBJECT(m_widget
), "key_press_event",
240 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback
), (gpointer
)this );
249 int wxNotebook::GetSelection() const
251 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
253 if (m_pages
.Number() == 0) return -1;
255 GtkNotebookPage
*g_page
= GTK_NOTEBOOK(m_widget
)->cur_page
;
256 if (!g_page
) return -1;
258 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
260 wxNode
*node
= m_pages
.First();
263 page
= (wxNotebookPage
*)node
->Data();
265 if ((page
->m_page
== g_page
) || (page
->m_page
== (GtkNotebookPage
*)NULL
))
267 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
268 // "switch_page" then and we ask for GetSelection() in the handler for
269 // "switch_page". otherwise m_page should never be NULL. all this
270 // might also be wrong.
276 wxCHECK_MSG( node
!= NULL
, -1, "wxNotebook: no selection?" );
281 int wxNotebook::GetPageCount() const
283 // count only the pages which were already added to the notebook for MSW
284 // compatibility (and, in fact, this behaviour makes more sense anyhow
285 // because only the added pages are shown)
288 for ( wxNode
*node
= m_pages
.First(); node
; node
= node
->Next() )
290 wxNotebookPage
*page
= (wxNotebookPage
*)node
->Data();
292 if (page
->WasAdded()) n
++;
298 int wxNotebook::GetRowCount() const
303 wxString
wxNotebook::GetPageText( int page
) const
305 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid notebook" );
307 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
309 return nb_page
->m_text
;
314 int wxNotebook::GetPageImage( int page
) const
316 wxCHECK_MSG( m_widget
!= NULL
, 0, "invalid notebook" );
318 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
320 return nb_page
->m_image
;
325 wxNotebookPage
* wxNotebook::GetNotebookPage(int page
) const
327 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*)NULL
, "invalid notebook" );
329 wxNotebookPage
*nb_page
= (wxNotebookPage
*) NULL
;
331 wxNode
*node
= m_pages
.First();
334 nb_page
= (wxNotebookPage
*)node
->Data();
335 if (nb_page
->m_id
== page
)
340 wxFAIL_MSG( "Notebook page not found!" );
342 return (wxNotebookPage
*) NULL
;
345 int wxNotebook::SetSelection( int page
)
347 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid notebook" );
349 int selOld
= GetSelection();
350 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
352 if (!nb_page
) return -1;
355 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
358 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
363 if (!child
) return -1;
365 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page_num
);
370 void wxNotebook::AdvanceSelection( bool bForward
)
372 wxCHECK_RET( m_widget
!= NULL
, "invalid notebook" );
374 int sel
= GetSelection();
375 int max
= GetPageCount();
378 SetSelection( sel
== max
? 0 : sel
+ 1 );
380 SetSelection( sel
== 0 ? max
: sel
- 1 );
383 void wxNotebook::SetImageList( wxImageList
* imageList
)
385 m_imageList
= imageList
;
388 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
390 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
392 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
394 if (!nb_page
) return FALSE
;
396 nb_page
->m_text
= text
;
398 if (nb_page
->m_text
.IsEmpty()) nb_page
->m_text
= "";
400 gtk_label_set(nb_page
->m_label
, nb_page
->m_text
);
405 bool wxNotebook::SetPageImage( int page
, int image
)
407 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
409 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
411 if (!nb_page
) return FALSE
;
413 /* Optimization posibility: return immediately if image unchanged.
414 * Not enabled because it may break existing (stupid) code that
415 * manipulates the imagelist to cycle images */
417 /* if (image == nb_page->m_image) return TRUE; */
419 /* For different cases:
420 1) no image -> no image
425 if (image
== -1 && nb_page
->m_image
== -1)
426 return TRUE
; /* Case 1): Nothing to do. */
428 GtkWidget
*pixmapwid
= NULL
;
430 if (nb_page
->m_image
!= -1)
432 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
434 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
437 if (GTK_IS_PIXMAP(child
->data
))
439 pixmapwid
= GTK_WIDGET(child
->data
);
445 /* We should have the pixmap widget now */
446 wxASSERT(pixmapwid
!= NULL
);
450 /* If there's no new widget, just remove the old from the box */
451 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
452 nb_page
->m_image
= -1;
454 return TRUE
; /* Case 2) */
458 /* Only cases 3) and 4) left */
459 wxASSERT( m_imageList
!= NULL
); /* Just in case */
461 /* Construct the new pixmap */
462 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
463 GdkPixmap
*pixmap
= bmp
->GetPixmap();
464 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
465 if ( bmp
->GetMask() )
467 mask
= bmp
->GetMask()->GetBitmap();
470 if (pixmapwid
== NULL
)
472 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
473 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
475 /* CHECKME: Are these pack flags okay? */
476 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
477 gtk_widget_show(pixmapwid
);
481 /* Case 4) Simply replace the pixmap */
482 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
485 nb_page
->m_image
= image
;
490 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
492 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
495 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
497 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
500 bool wxNotebook::DeleteAllPages()
502 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
504 wxNode
*page_node
= m_pages
.First();
507 wxNotebookPage
*page
= (wxNotebookPage
*)page_node
->Data();
509 DeletePage( page
->m_id
);
511 page_node
= m_pages
.First();
517 bool wxNotebook::DeletePage( int page
)
519 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
520 if (!nb_page
) return FALSE
;
523 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
526 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
531 wxCHECK_MSG( child
!= NULL
, FALSE
, "illegal notebook index" );
533 delete nb_page
->m_client
;
535 m_pages
.DeleteObject( nb_page
);
540 bool wxNotebook::RemovePage( int page
)
542 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
543 if (!nb_page
) return FALSE
;
546 GList
*child
= GTK_NOTEBOOK(m_widget
)->children
;
549 if (nb_page
->m_page
== (GtkNotebookPage
*)child
->data
) break;
554 wxCHECK_MSG( child
!= NULL
, FALSE
, "illegal notebook index" );
556 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page_num
);
558 m_pages
.DeleteObject( nb_page
);
563 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
564 bool select
, int imageId
)
566 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid notebook" );
568 /* we've created the notebook page in AddChild(). Now we just have to set
569 the caption for the page and set the others parameters. */
571 wxNotebookPage
*page
= (wxNotebookPage
*) NULL
;
573 wxNode
*node
= m_pages
.First();
576 page
= (wxNotebookPage
*)node
->Data();
577 if ( page
->m_client
== win
) break;
581 wxCHECK_MSG( page
!= NULL
, FALSE
,
582 "Can't add a page whose parent is not the notebook!" );
584 wxCHECK_MSG( page
->Add(), FALSE
,
585 "Can't add the same page twice to a notebook." );
589 wxASSERT( m_imageList
!= NULL
);
591 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
592 GdkPixmap
*pixmap
= bmp
->GetPixmap();
593 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
594 if ( bmp
->GetMask() )
596 mask
= bmp
->GetMask()->GetBitmap();
599 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
601 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
603 gtk_widget_show(pixmapwid
);
606 /* then set the attributes */
608 if (page
->m_text
.IsEmpty()) page
->m_text
= "";
609 page
->m_image
= imageId
;
610 page
->m_label
= (GtkLabel
*)gtk_label_new(page
->m_text
);
611 gtk_box_pack_end( GTK_BOX(page
->m_box
), (GtkWidget
*)page
->m_label
, FALSE
, FALSE
, 3);
613 /* @@@: what does this do? do we still need it?
614 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); */
616 gtk_widget_show((GtkWidget
*)page
->m_label
);
618 if (select
) SetSelection( GetPageCount()-1 );
623 wxWindow
*wxNotebook::GetPage( int page
) const
625 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, "invalid notebook" );
627 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
629 return (wxWindow
*) NULL
;
631 return nb_page
->m_client
;
634 // override these 2 functions to do nothing: everything is done in OnSize
635 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
637 // don't set the sizes of the pages - their correct size is not yet known
638 wxControl::SetConstraintSizes(FALSE
);
641 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
646 void wxNotebook::ApplyWidgetStyle()
649 gtk_widget_set_style( m_widget
, m_widgetStyle
);
652 //-----------------------------------------------------------------------------
654 //-----------------------------------------------------------------------------
656 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)