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 //-----------------------------------------------------------------------------
131 // InsertChild callback for wxNotebook
132 //-----------------------------------------------------------------------------
134 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
136 /* we don't do anything here but pray */
139 //-----------------------------------------------------------------------------
141 //-----------------------------------------------------------------------------
143 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
145 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
146 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
149 void wxNotebook::Init()
151 m_imageList
= (wxImageList
*) NULL
;
152 m_pages
.DeleteContents( TRUE
);
153 m_lastSelection
= -1;
156 wxNotebook::wxNotebook()
161 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
162 const wxPoint
& pos
, const wxSize
& size
,
163 long style
, const wxString
& name
)
166 Create( parent
, id
, pos
, size
, style
, name
);
169 wxNotebook::~wxNotebook()
171 /* don't generate change page events any more */
172 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
173 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
178 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
179 const wxPoint
& pos
, const wxSize
& size
,
180 long style
, const wxString
& name
)
183 m_acceptsFocus
= TRUE
;
184 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
186 if (!PreCreation( parent
, pos
, size
) ||
187 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
189 wxFAIL_MSG( _T("wxNoteBook creation failed") );
194 m_widget
= gtk_notebook_new();
197 debug_focus_in( m_widget
, _T("wxNotebook::m_widget"), name
);
200 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
202 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
203 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
205 m_parent
->DoAddChild( this );
214 void wxNotebook::SetFocus()
216 if (m_pages
.GetCount() == 0) return;
218 wxNode
*node
= m_pages
.Nth( GetSelection() );
222 wxNotebookPage
*page
= (wxNotebookPage
*) node
->Data();
224 page
->m_client
->SetFocus();
227 int wxNotebook::GetSelection() const
229 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
231 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
233 if (g_list_length(pages
) == 0) return -1;
235 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
237 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
239 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
242 int wxNotebook::GetPageCount() const
244 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
247 int wxNotebook::GetRowCount() const
252 wxString
wxNotebook::GetPageText( int page
) const
254 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid notebook") );
256 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
258 return nb_page
->m_text
;
263 int wxNotebook::GetPageImage( int page
) const
265 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
267 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
269 return nb_page
->m_image
;
274 wxNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
276 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*) NULL
, _T("invalid notebook") );
278 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxNotebookPage
*) NULL
, _T("invalid notebook index") );
280 wxNode
*node
= m_pages
.Nth( page
);
282 return (wxNotebookPage
*) node
->Data();
285 int wxNotebook::SetSelection( int page
)
287 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid notebook") );
289 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, _T("invalid notebook index") );
291 int selOld
= GetSelection();
293 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
298 void wxNotebook::AdvanceSelection( bool forward
)
300 wxCHECK_RET( m_widget
!= NULL
, _T("invalid notebook") );
302 int sel
= GetSelection();
303 int max
= GetPageCount();
306 SetSelection( sel
== max
? 0 : sel
+ 1 );
308 SetSelection( sel
== 0 ? max
-1 : sel
- 1 );
311 void wxNotebook::SetImageList( wxImageList
* imageList
)
313 m_imageList
= imageList
;
316 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
318 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
320 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
322 wxCHECK_MSG( nb_page
, FALSE
, _T("SetPageText: invalid page index") );
324 nb_page
->m_text
= text
;
326 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
331 bool wxNotebook::SetPageImage( int page
, int image
)
333 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
335 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
337 if (!nb_page
) return FALSE
;
339 /* Optimization posibility: return immediately if image unchanged.
340 * Not enabled because it may break existing (stupid) code that
341 * manipulates the imagelist to cycle images */
343 /* if (image == nb_page->m_image) return TRUE; */
345 /* For different cases:
346 1) no image -> no image
351 if (image
== -1 && nb_page
->m_image
== -1)
352 return TRUE
; /* Case 1): Nothing to do. */
354 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
356 if (nb_page
->m_image
!= -1)
358 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
360 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
363 if (GTK_IS_PIXMAP(child
->data
))
365 pixmapwid
= GTK_WIDGET(child
->data
);
371 /* We should have the pixmap widget now */
372 wxASSERT(pixmapwid
!= NULL
);
376 /* If there's no new widget, just remove the old from the box */
377 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
378 nb_page
->m_image
= -1;
380 return TRUE
; /* Case 2) */
384 /* Only cases 3) and 4) left */
385 wxASSERT( m_imageList
!= NULL
); /* Just in case */
387 /* Construct the new pixmap */
388 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
389 GdkPixmap
*pixmap
= bmp
->GetPixmap();
390 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
391 if ( bmp
->GetMask() )
393 mask
= bmp
->GetMask()->GetBitmap();
396 if (pixmapwid
== NULL
)
398 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
399 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
401 /* CHECKME: Are these pack flags okay? */
402 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
403 gtk_widget_show(pixmapwid
);
407 /* Case 4) Simply replace the pixmap */
408 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
411 nb_page
->m_image
= image
;
416 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
418 wxFAIL_MSG( _T("wxNotebook::SetPageSize not implemented") );
421 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
423 wxFAIL_MSG( _T("wxNotebook::SetPadding not implemented") );
426 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
428 wxFAIL_MSG( _T("wxNotebook::SetTabSize not implemented") );
431 bool wxNotebook::DeleteAllPages()
433 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
435 while (m_pages
.GetCount() > 0)
436 DeletePage( m_pages
.GetCount()-1 );
441 bool wxNotebook::DeletePage( int page
)
443 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
444 if (!nb_page
) return FALSE
;
446 /* GTK sets GtkNotebook.cur_page to NULL before sending
447 the switvh page event */
448 m_lastSelection
= GetSelection();
450 nb_page
->m_client
->Destroy();
451 m_pages
.DeleteObject( nb_page
);
453 m_lastSelection
= -1;
458 bool wxNotebook::RemovePage( int page
)
460 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
462 if (!nb_page
) return FALSE
;
464 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
466 m_pages
.DeleteObject( nb_page
);
471 bool wxNotebook::InsertPage( int position
, wxWindow
* win
, const wxString
& text
,
472 bool select
, int imageId
)
474 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid notebook") );
476 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
477 _T("Can't add a page whose parent is not the notebook!") );
479 /* don't receive switch page during addition */
480 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
481 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
483 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
485 wxNotebookPage
*page
= new wxNotebookPage();
488 m_pages
.Append( page
);
490 m_pages
.Insert( m_pages
.Nth( position
), page
);
492 page
->m_client
= win
;
494 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
495 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
497 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
498 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
501 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
503 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
505 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
507 /* set the label image */
508 page
->m_image
= imageId
;
512 wxASSERT( m_imageList
!= NULL
);
514 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
515 GdkPixmap
*pixmap
= bmp
->GetPixmap();
516 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
517 if ( bmp
->GetMask() )
519 mask
= bmp
->GetMask()->GetBitmap();
522 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
524 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
526 gtk_widget_show(pixmapwid
);
529 /* set the label text */
531 if (page
->m_text
.IsEmpty()) page
->m_text
= _T("");
533 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
534 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
537 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
539 if (select
&& (m_pages
.GetCount() > 1))
542 SetSelection( GetPageCount()-1 );
544 SetSelection( position
);
547 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
548 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
553 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
554 bool select
, int imageId
)
556 return InsertPage( -1, win
, text
, select
, imageId
);
559 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
561 if (event
.IsWindowChange())
562 AdvanceSelection( event
.GetDirection() );
567 wxWindow
*wxNotebook::GetPage( int page
) const
569 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, _T("invalid notebook") );
571 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
573 return (wxWindow
*) NULL
;
575 return nb_page
->m_client
;
578 // override these 2 functions to do nothing: everything is done in OnSize
579 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
581 // don't set the sizes of the pages - their correct size is not yet known
582 wxControl::SetConstraintSizes(FALSE
);
585 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
590 void wxNotebook::ApplyWidgetStyle()
593 gtk_widget_set_style( m_widget
, m_widgetStyle
);
596 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
598 return ((m_widget
->window
== window
) ||
599 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
602 //-----------------------------------------------------------------------------
604 //-----------------------------------------------------------------------------
606 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)