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 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
128 here in order to make repositioning after resizing to take effect. */
129 if ((gtk_major_version
== 1) &&
130 (gtk_minor_version
== 2) &&
131 (gtk_micro_version
< 6) &&
133 (GTK_WIDGET_REALIZED(win
->m_wxwindow
)))
135 gtk_widget_size_allocate( win
->m_wxwindow
, alloc
);
139 //-----------------------------------------------------------------------------
140 // "realize" from m_widget
141 //-----------------------------------------------------------------------------
144 gtk_notebook_realized_callback( GtkWidget
* WXUNUSED(widget
), wxWindow
*win
)
147 wxapp_install_idle_handler();
149 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
150 here in order to make repositioning before showing to take effect. */
151 gtk_widget_queue_resize( win
->m_widget
);
156 //-----------------------------------------------------------------------------
157 // InsertChild callback for wxNotebook
158 //-----------------------------------------------------------------------------
160 static void wxInsertChildInNotebook( wxNotebook
* WXUNUSED(parent
), wxWindow
* WXUNUSED(child
) )
162 /* we don't do anything here but pray */
165 //-----------------------------------------------------------------------------
167 //-----------------------------------------------------------------------------
169 IMPLEMENT_DYNAMIC_CLASS(wxNotebook
,wxControl
)
171 BEGIN_EVENT_TABLE(wxNotebook
, wxControl
)
172 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey
)
175 void wxNotebook::Init()
177 m_imageList
= (wxImageList
*) NULL
;
178 m_pages
.DeleteContents( TRUE
);
179 m_lastSelection
= -1;
182 wxNotebook::wxNotebook()
187 wxNotebook::wxNotebook( wxWindow
*parent
, wxWindowID id
,
188 const wxPoint
& pos
, const wxSize
& size
,
189 long style
, const wxString
& name
)
192 Create( parent
, id
, pos
, size
, style
, name
);
195 wxNotebook::~wxNotebook()
197 /* don't generate change page events any more */
198 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
199 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
204 bool wxNotebook::Create(wxWindow
*parent
, wxWindowID id
,
205 const wxPoint
& pos
, const wxSize
& size
,
206 long style
, const wxString
& name
)
209 m_acceptsFocus
= TRUE
;
210 m_insertCallback
= (wxInsertChildFunction
)wxInsertChildInNotebook
;
212 if (!PreCreation( parent
, pos
, size
) ||
213 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
215 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
220 m_widget
= gtk_notebook_new();
223 debug_focus_in( m_widget
, wxT("wxNotebook::m_widget"), name
);
226 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget
), 1 );
228 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
229 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
231 m_parent
->DoAddChild( this );
233 if(m_windowStyle
& wxNB_RIGHT
)
234 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_RIGHT
);
235 if(m_windowStyle
& wxNB_LEFT
)
236 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_LEFT
);
237 if(m_windowStyle
& wxNB_BOTTOM
)
238 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget
), GTK_POS_BOTTOM
);
242 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
243 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback
), (gpointer
) this );
250 void wxNotebook::SetFocus()
252 if (m_pages
.GetCount() == 0) return;
254 wxNode
*node
= m_pages
.Nth( GetSelection() );
258 wxNotebookPage
*page
= (wxNotebookPage
*) node
->Data();
260 page
->m_client
->SetFocus();
263 int wxNotebook::GetSelection() const
265 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
267 GList
*pages
= GTK_NOTEBOOK(m_widget
)->children
;
269 if (g_list_length(pages
) == 0) return -1;
271 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
273 if (notebook
->cur_page
== NULL
) return m_lastSelection
;
275 return g_list_index( pages
, (gpointer
)(notebook
->cur_page
) );
278 int wxNotebook::GetPageCount() const
280 return (int) g_list_length( GTK_NOTEBOOK(m_widget
)->children
);
283 int wxNotebook::GetRowCount() const
288 wxString
wxNotebook::GetPageText( int page
) const
290 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid notebook") );
292 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
294 return nb_page
->m_text
;
299 int wxNotebook::GetPageImage( int page
) const
301 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
303 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
305 return nb_page
->m_image
;
310 wxNotebookPage
* wxNotebook::GetNotebookPage( int page
) const
312 wxCHECK_MSG( m_widget
!= NULL
, (wxNotebookPage
*) NULL
, wxT("invalid notebook") );
314 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), (wxNotebookPage
*) NULL
, wxT("invalid notebook index") );
316 wxNode
*node
= m_pages
.Nth( page
);
318 return (wxNotebookPage
*) node
->Data();
321 int wxNotebook::SetSelection( int page
)
323 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid notebook") );
325 wxCHECK_MSG( page
< (int)m_pages
.GetCount(), -1, wxT("invalid notebook index") );
327 int selOld
= GetSelection();
329 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget
), page
);
334 void wxNotebook::AdvanceSelection( bool forward
)
336 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid notebook") );
338 int sel
= GetSelection();
339 int max
= GetPageCount();
342 SetSelection( sel
== max
? 0 : sel
+ 1 );
344 SetSelection( sel
== 0 ? max
-1 : sel
- 1 );
347 void wxNotebook::SetImageList( wxImageList
* imageList
)
349 m_imageList
= imageList
;
352 bool wxNotebook::SetPageText( int page
, const wxString
&text
)
354 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
356 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
358 wxCHECK_MSG( nb_page
, FALSE
, wxT("SetPageText: invalid page index") );
360 nb_page
->m_text
= text
;
362 gtk_label_set( nb_page
->m_label
, nb_page
->m_text
.mbc_str() );
367 bool wxNotebook::SetPageImage( int page
, int image
)
369 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
371 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
373 if (!nb_page
) return FALSE
;
375 /* Optimization posibility: return immediately if image unchanged.
376 * Not enabled because it may break existing (stupid) code that
377 * manipulates the imagelist to cycle images */
379 /* if (image == nb_page->m_image) return TRUE; */
381 /* For different cases:
382 1) no image -> no image
387 if (image
== -1 && nb_page
->m_image
== -1)
388 return TRUE
; /* Case 1): Nothing to do. */
390 GtkWidget
*pixmapwid
= (GtkWidget
*) NULL
;
392 if (nb_page
->m_image
!= -1)
394 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
396 GList
*child
= gtk_container_children(GTK_CONTAINER(nb_page
->m_box
));
399 if (GTK_IS_PIXMAP(child
->data
))
401 pixmapwid
= GTK_WIDGET(child
->data
);
407 /* We should have the pixmap widget now */
408 wxASSERT(pixmapwid
!= NULL
);
412 /* If there's no new widget, just remove the old from the box */
413 gtk_container_remove(GTK_CONTAINER(nb_page
->m_box
), pixmapwid
);
414 nb_page
->m_image
= -1;
416 return TRUE
; /* Case 2) */
420 /* Only cases 3) and 4) left */
421 wxASSERT( m_imageList
!= NULL
); /* Just in case */
423 /* Construct the new pixmap */
424 const wxBitmap
*bmp
= m_imageList
->GetBitmap(image
);
425 GdkPixmap
*pixmap
= bmp
->GetPixmap();
426 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
427 if ( bmp
->GetMask() )
429 mask
= bmp
->GetMask()->GetBitmap();
432 if (pixmapwid
== NULL
)
434 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
435 pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
437 /* CHECKME: Are these pack flags okay? */
438 gtk_box_pack_start(GTK_BOX(nb_page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
439 gtk_widget_show(pixmapwid
);
443 /* Case 4) Simply replace the pixmap */
444 gtk_pixmap_set(GTK_PIXMAP(pixmapwid
), pixmap
, mask
);
447 nb_page
->m_image
= image
;
452 void wxNotebook::SetPageSize( const wxSize
&WXUNUSED(size
) )
454 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
457 void wxNotebook::SetPadding( const wxSize
&WXUNUSED(padding
) )
459 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
462 void wxNotebook::SetTabSize(const wxSize
& WXUNUSED(sz
))
464 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
467 bool wxNotebook::DeleteAllPages()
469 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
471 while (m_pages
.GetCount() > 0)
472 DeletePage( m_pages
.GetCount()-1 );
477 bool wxNotebook::DeletePage( int page
)
479 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
480 if (!nb_page
) return FALSE
;
482 /* GTK sets GtkNotebook.cur_page to NULL before sending
483 the switvh page event */
484 m_lastSelection
= GetSelection();
486 nb_page
->m_client
->Destroy();
487 m_pages
.DeleteObject( nb_page
);
489 m_lastSelection
= -1;
494 bool wxNotebook::RemovePage( int page
)
496 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
498 if (!nb_page
) return FALSE
;
500 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget
), page
);
502 m_pages
.DeleteObject( nb_page
);
507 bool wxNotebook::InsertPage( int position
, wxWindow
* win
, const wxString
& text
,
508 bool select
, int imageId
)
510 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid notebook") );
512 wxCHECK_MSG( win
->GetParent() == this, FALSE
,
513 wxT("Can't add a page whose parent is not the notebook!") );
515 /* don't receive switch page during addition */
516 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
517 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
) this );
519 GtkNotebook
*notebook
= GTK_NOTEBOOK(m_widget
);
521 wxNotebookPage
*page
= new wxNotebookPage();
524 m_pages
.Append( page
);
526 m_pages
.Insert( m_pages
.Nth( position
), page
);
528 page
->m_client
= win
;
530 page
->m_box
= gtk_hbox_new( FALSE
, 0 );
531 gtk_container_border_width( GTK_CONTAINER(page
->m_box
), 2 );
533 gtk_signal_connect( GTK_OBJECT(win
->m_widget
), "size_allocate",
534 GTK_SIGNAL_FUNC(gtk_page_size_callback
), (gpointer
)win
);
537 gtk_notebook_append_page( notebook
, win
->m_widget
, page
->m_box
);
539 gtk_notebook_insert_page( notebook
, win
->m_widget
, page
->m_box
, position
);
541 page
->m_page
= (GtkNotebookPage
*) g_list_last(notebook
->children
)->data
;
543 /* set the label image */
544 page
->m_image
= imageId
;
548 wxASSERT( m_imageList
!= NULL
);
550 const wxBitmap
*bmp
= m_imageList
->GetBitmap(imageId
);
551 GdkPixmap
*pixmap
= bmp
->GetPixmap();
552 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
553 if ( bmp
->GetMask() )
555 mask
= bmp
->GetMask()->GetBitmap();
558 GtkWidget
*pixmapwid
= gtk_pixmap_new (pixmap
, mask
);
560 gtk_box_pack_start(GTK_BOX(page
->m_box
), pixmapwid
, FALSE
, FALSE
, 3);
562 gtk_widget_show(pixmapwid
);
565 /* set the label text */
567 if (page
->m_text
.IsEmpty()) page
->m_text
= wxT("");
569 page
->m_label
= GTK_LABEL( gtk_label_new(page
->m_text
.mbc_str()) );
570 gtk_box_pack_end( GTK_BOX(page
->m_box
), GTK_WIDGET(page
->m_label
), FALSE
, FALSE
, 3 );
573 gtk_widget_show( GTK_WIDGET(page
->m_label
) );
575 if (select
&& (m_pages
.GetCount() > 1))
578 SetSelection( GetPageCount()-1 );
580 SetSelection( position
);
583 gtk_signal_connect( GTK_OBJECT(m_widget
), "switch_page",
584 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback
), (gpointer
)this );
589 bool wxNotebook::AddPage(wxWindow
* win
, const wxString
& text
,
590 bool select
, int imageId
)
592 return InsertPage( -1, win
, text
, select
, imageId
);
595 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent
& event
)
597 if (event
.IsWindowChange())
598 AdvanceSelection( event
.GetDirection() );
603 wxWindow
*wxNotebook::GetPage( int page
) const
605 wxCHECK_MSG( m_widget
!= NULL
, (wxWindow
*) NULL
, wxT("invalid notebook") );
607 wxNotebookPage
* nb_page
= GetNotebookPage(page
);
609 return (wxWindow
*) NULL
;
611 return nb_page
->m_client
;
614 // override these 2 functions to do nothing: everything is done in OnSize
615 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse
) )
617 // don't set the sizes of the pages - their correct size is not yet known
618 wxControl::SetConstraintSizes(FALSE
);
621 bool wxNotebook::DoPhase( int WXUNUSED(nPhase
) )
626 void wxNotebook::ApplyWidgetStyle()
629 gtk_widget_set_style( m_widget
, m_widgetStyle
);
632 bool wxNotebook::IsOwnGtkWindow( GdkWindow
*window
)
634 return ((m_widget
->window
== window
) ||
635 (GTK_NOTEBOOK(m_widget
)->panel
== window
));
638 //-----------------------------------------------------------------------------
640 //-----------------------------------------------------------------------------
642 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent
, wxNotifyEvent
)