]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/notebook.cpp
Applied patch [ 666379 ] CalCtrl doesn't use best sizes for the year combobox
[wxWidgets.git] / src / gtk / notebook.cpp
CommitLineData
53b28675
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: notebook.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
ff829f3f 7// Licence: wxWindows licence
53b28675
RR
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "notebook.h"
12#endif
13
14#include "wx/notebook.h"
dcf924a3
RR
15
16#if wxUSE_NOTEBOOK
17
53b28675
RR
18#include "wx/panel.h"
19#include "wx/utils.h"
20#include "wx/imaglist.h"
30dea054 21#include "wx/intl.h"
4bf58c62 22#include "wx/log.h"
83624f79 23
9e691f46 24#include "wx/gtk/private.h"
83624f79 25#include "wx/gtk/win_gtk.h"
9e691f46 26
5e7e9e1b 27#include <gdk/gdkkeysyms.h>
b292e2f5 28
2e4df4bf
VZ
29// ----------------------------------------------------------------------------
30// events
31// ----------------------------------------------------------------------------
32
33DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
34DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
35
acfd422a
RR
36//-----------------------------------------------------------------------------
37// idle system
38//-----------------------------------------------------------------------------
39
40extern void wxapp_install_idle_handler();
41extern bool g_isIdle;
42
b292e2f5
RR
43//-----------------------------------------------------------------------------
44// data
45//-----------------------------------------------------------------------------
46
07b8d7ec 47extern bool g_blockEventsOnDrag;
53b28675 48
219f895a 49//-----------------------------------------------------------------------------
80a58c99 50// wxGtkNotebookPage
219f895a
RR
51//-----------------------------------------------------------------------------
52
07b8d7ec
VZ
53// VZ: this is rather ugly as we keep the pages themselves in an array (it
54// allows us to have quite a few functions implemented in the base class)
55// but the page data is kept in a separate list, so we must maintain them
56// in sync manually... of course, the list had been there before the base
57// class which explains it but it still would be nice to do something
58// about this one day
59
80a58c99 60class wxGtkNotebookPage: public wxObject
219f895a
RR
61{
62public:
80a58c99 63 wxGtkNotebookPage()
219f895a 64 {
219f895a 65 m_image = -1;
c67daf87 66 m_page = (GtkNotebookPage *) NULL;
24d20a8f 67 m_box = (GtkWidget *) NULL;
ff7b1510 68 }
219f895a 69
219f895a
RR
70 wxString m_text;
71 int m_image;
72 GtkNotebookPage *m_page;
73 GtkLabel *m_label;
24d20a8f 74 GtkWidget *m_box; // in which the label and image are packed
219f895a
RR
75};
76
07b8d7ec
VZ
77#include "wx/listimpl.cpp"
78WX_DEFINE_LIST(wxGtkNotebookPagesList);
79
ff829f3f 80//-----------------------------------------------------------------------------
5b011451 81// "switch_page"
ff829f3f
VZ
82//-----------------------------------------------------------------------------
83
219f895a
RR
84static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
85 GtkNotebookPage *WXUNUSED(page),
587ce561
RR
86 gint page,
87 wxNotebook *notebook )
ff829f3f 88{
36202885
VZ
89 // are you trying to call SetSelection() from a notebook event handler?
90 // you shouldn't!
2b5f62a0 91 wxCHECK_RET( !notebook->m_inSwitchPage,
36202885
VZ
92 _T("gtk_notebook_page_change_callback reentered") );
93
2b5f62a0 94 notebook->m_inSwitchPage = TRUE;
a6aa9b1e 95 if (g_isIdle)
587ce561 96 wxapp_install_idle_handler();
ff829f3f 97
b292e2f5 98 int old = notebook->GetSelection();
ff829f3f 99
36202885
VZ
100 wxNotebookEvent eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,
101 notebook->GetId(), page, old );
102 eventChanging.SetEventObject( notebook );
a6aa9b1e 103
36202885
VZ
104 if ( (notebook->GetEventHandler()->ProcessEvent(eventChanging)) &&
105 !eventChanging.IsAllowed() )
587ce561
RR
106 {
107 /* program doesn't allow the page change */
36202885
VZ
108 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook->m_widget),
109 "switch_page" );
110 }
111 else // change allowed
112 {
113 // make wxNotebook::GetSelection() return the correct (i.e. consistent
114 // with wxNotebookEvent::GetSelection()) value even though the page is
115 // not really changed in GTK+
116 notebook->m_selection = page;
117
118 wxNotebookEvent eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
119 notebook->GetId(), page, old );
120 eventChanged.SetEventObject( notebook );
121 notebook->GetEventHandler()->ProcessEvent( eventChanged );
587ce561 122 }
ef44a621 123
2b5f62a0 124 notebook->m_inSwitchPage = FALSE;
ff829f3f
VZ
125}
126
5b011451
RR
127//-----------------------------------------------------------------------------
128// "size_allocate"
129//-----------------------------------------------------------------------------
130
33d0b396 131static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
caac5181 132{
a6aa9b1e 133 if (g_isIdle)
587ce561 134 wxapp_install_idle_handler();
6d693bb4 135
a2053b27
RR
136 if ((win->m_x == alloc->x) &&
137 (win->m_y == alloc->y) &&
138 (win->m_width == alloc->width) &&
139 (win->m_height == alloc->height))
b292e2f5 140 {
58dea4b0 141 return;
b292e2f5 142 }
a6aa9b1e 143
b292e2f5 144 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
f861258f 145
d7928388
RR
146 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate
147 here in order to make repositioning after resizing to take effect. */
148 if ((gtk_major_version == 1) &&
149 (gtk_minor_version == 2) &&
8712c6e7
VZ
150 (gtk_micro_version < 6) &&
151 (win->m_wxwindow) &&
152 (GTK_WIDGET_REALIZED(win->m_wxwindow)))
d7928388
RR
153 {
154 gtk_widget_size_allocate( win->m_wxwindow, alloc );
155 }
6d693bb4
RR
156}
157
158//-----------------------------------------------------------------------------
159// "realize" from m_widget
160//-----------------------------------------------------------------------------
161
6d693bb4
RR
162static gint
163gtk_notebook_realized_callback( GtkWidget * WXUNUSED(widget), wxWindow *win )
164{
165 if (g_isIdle)
166 wxapp_install_idle_handler();
167
d7928388
RR
168 /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize
169 here in order to make repositioning before showing to take effect. */
6d693bb4
RR
170 gtk_widget_queue_resize( win->m_widget );
171
172 return FALSE;
b292e2f5
RR
173}
174
8253c7fd 175//-----------------------------------------------------------------------------
8712c6e7 176// "key_press_event"
8253c7fd
RR
177//-----------------------------------------------------------------------------
178
179static gint gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *win )
180{
181 if (g_isIdle)
182 wxapp_install_idle_handler();
183
184 if (!win->m_hasVMT) return FALSE;
185 if (g_blockEventsOnDrag) return FALSE;
186
187 /* win is a control: tab can be propagated up */
188 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
189 {
07b8d7ec 190 int sel = win->GetSelection();
b318dc42
JS
191 wxGtkNotebookPage *nb_page = win->GetNotebookPage(sel);
192 wxCHECK_MSG( nb_page, FALSE, _T("invalid selection in wxNotebook") );
8253c7fd
RR
193
194 wxNavigationKeyEvent event;
195 event.SetEventObject( win );
196 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
197 event.SetDirection( (gdk_event->keyval == GDK_Tab) );
198 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
cc023d9f 199 event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
8253c7fd 200 event.SetCurrentFocus( win );
07b8d7ec
VZ
201
202 wxNotebookPage *client = win->GetPage(sel);
203 if ( !client->GetEventHandler()->ProcessEvent( event ) )
8253c7fd 204 {
07b8d7ec 205 client->SetFocus();
8253c7fd 206 }
8712c6e7 207
8253c7fd
RR
208 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
209 return TRUE;
210 }
8712c6e7 211
8253c7fd
RR
212 return FALSE;
213}
214
6ca41e57
RR
215//-----------------------------------------------------------------------------
216// InsertChild callback for wxNotebook
217//-----------------------------------------------------------------------------
218
587ce561 219static void wxInsertChildInNotebook( wxNotebook* WXUNUSED(parent), wxWindow* WXUNUSED(child) )
6ca41e57 220{
587ce561 221 /* we don't do anything here but pray */
6ca41e57
RR
222}
223
53b28675
RR
224//-----------------------------------------------------------------------------
225// wxNotebook
226//-----------------------------------------------------------------------------
227
53b28675
RR
228IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
229
b98d804b
RR
230BEGIN_EVENT_TABLE(wxNotebook, wxControl)
231 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
232END_EVENT_TABLE()
f861258f 233
ff829f3f 234void wxNotebook::Init()
53b28675 235{
b318dc42 236 m_padding = 0;
2b5f62a0
VZ
237 m_inSwitchPage = FALSE;
238
b292e2f5 239 m_imageList = (wxImageList *) NULL;
07b8d7ec 240 m_pagesData.DeleteContents( TRUE );
36202885 241 m_selection = -1;
a2d93e73 242 m_themeEnabled = TRUE;
ff829f3f
VZ
243}
244
245wxNotebook::wxNotebook()
246{
b292e2f5 247 Init();
ff7b1510 248}
53b28675 249
debe6624 250wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
53b28675 251 const wxPoint& pos, const wxSize& size,
debe6624 252 long style, const wxString& name )
53b28675 253{
b292e2f5
RR
254 Init();
255 Create( parent, id, pos, size, style, name );
ff7b1510 256}
53b28675 257
ff829f3f 258wxNotebook::~wxNotebook()
53b28675 259{
587ce561 260 /* don't generate change page events any more */
a6aa9b1e 261 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
587ce561 262 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
ff829f3f 263
b292e2f5 264 DeleteAllPages();
ff7b1510 265}
53b28675 266
debe6624 267bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
07b8d7ec
VZ
268 const wxPoint& pos, const wxSize& size,
269 long style, const wxString& name )
53b28675 270{
b292e2f5
RR
271 m_needParent = TRUE;
272 m_acceptsFocus = TRUE;
273 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
274
4dcaf11a
RR
275 if (!PreCreation( parent, pos, size ) ||
276 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
277 {
223d09f6 278 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
8712c6e7 279 return FALSE;
4dcaf11a
RR
280 }
281
ff829f3f 282
b292e2f5 283 m_widget = gtk_notebook_new();
53b28675 284
b292e2f5 285 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
caac5181 286
587ce561
RR
287 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
288 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
ff829f3f 289
f03fc89f 290 m_parent->DoAddChild( this );
ef44a621 291
8712c6e7
VZ
292 if (m_windowStyle & wxNB_RIGHT)
293 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
294 if (m_windowStyle & wxNB_LEFT)
295 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
296 if (m_windowStyle & wxNB_BOTTOM)
297 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
a3a7f879 298
8253c7fd
RR
299 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
300 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
301
b292e2f5 302 PostCreation();
ff829f3f 303
db434467
RR
304 SetFont( parent->GetFont() );
305
6d693bb4
RR
306 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
307 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback), (gpointer) this );
a6aa9b1e 308
b292e2f5
RR
309 Show( TRUE );
310
311 return TRUE;
ff7b1510 312}
53b28675 313
ff829f3f 314int wxNotebook::GetSelection() const
53b28675 315{
223d09f6 316 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
53b28675 317
36202885
VZ
318 if ( m_selection == -1 )
319 {
b318dc42 320 GList *nb_pages = GTK_NOTEBOOK(m_widget)->children;
53b28675 321
b318dc42 322 if (g_list_length(nb_pages) != 0)
36202885
VZ
323 {
324 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
a6aa9b1e 325
36202885
VZ
326 gpointer cur = notebook->cur_page;
327 if ( cur != NULL )
328 {
329 wxConstCast(this, wxNotebook)->m_selection =
b318dc42 330 g_list_index( nb_pages, cur );
36202885
VZ
331 }
332 }
333 }
a6aa9b1e 334
36202885 335 return m_selection;
ff7b1510 336}
53b28675 337
ff829f3f 338wxString wxNotebook::GetPageText( int page ) const
53b28675 339{
223d09f6 340 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") );
ef44a621 341
80a58c99 342 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
b292e2f5
RR
343 if (nb_page)
344 return nb_page->m_text;
345 else
223d09f6 346 return wxT("");
ff7b1510 347}
53b28675 348
ff829f3f 349int wxNotebook::GetPageImage( int page ) const
53b28675 350{
223d09f6 351 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
a81258be 352
80a58c99 353 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
b292e2f5
RR
354 if (nb_page)
355 return nb_page->m_image;
356 else
587ce561 357 return -1;
ff7b1510 358}
53b28675 359
80a58c99 360wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
53b28675 361{
80a58c99 362 wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") );
ff829f3f 363
07b8d7ec 364 wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") );
a6aa9b1e 365
07b8d7ec 366 return m_pagesData.Item(page)->GetData();
ff7b1510 367}
53b28675 368
ff829f3f 369int wxNotebook::SetSelection( int page )
53b28675 370{
223d09f6 371 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
a81258be 372
07b8d7ec 373 wxCHECK_MSG( page < (int)m_pagesData.GetCount(), -1, wxT("invalid notebook index") );
ff829f3f 374
587ce561 375 int selOld = GetSelection();
a6aa9b1e 376
36202885
VZ
377 // cache the selection
378 m_selection = page;
587ce561 379 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page );
ff829f3f 380
07b8d7ec
VZ
381 wxNotebookPage *client = GetPage(page);
382 if ( client )
383 client->SetFocus();
b656febd 384
07b8d7ec 385 return selOld;
ff7b1510 386}
53b28675 387
ff829f3f 388bool wxNotebook::SetPageText( int page, const wxString &text )
53b28675 389{
223d09f6 390 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
a81258be 391
80a58c99 392 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
ef44a621 393
223d09f6 394 wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") );
ff829f3f 395
3eb78d7e 396 nb_page->m_text = text;
ff829f3f 397
fab591c5 398 gtk_label_set( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) );
a6aa9b1e 399
3eb78d7e 400 return TRUE;
ff7b1510 401}
53b28675 402
debe6624 403bool wxNotebook::SetPageImage( int page, int image )
53b28675 404{
3eb78d7e 405 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
f861258f 406
80a58c99 407 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
ef44a621 408
3eb78d7e 409 if (!nb_page) return FALSE;
f861258f 410
3eb78d7e
RR
411 /* Optimization posibility: return immediately if image unchanged.
412 * Not enabled because it may break existing (stupid) code that
413 * manipulates the imagelist to cycle images */
f861258f 414
3eb78d7e 415 /* if (image == nb_page->m_image) return TRUE; */
f861258f
VZ
416
417 /* For different cases:
3eb78d7e
RR
418 1) no image -> no image
419 2) image -> no image
420 3) no image -> image
421 4) image -> image */
f861258f 422
3eb78d7e
RR
423 if (image == -1 && nb_page->m_image == -1)
424 return TRUE; /* Case 1): Nothing to do. */
f861258f 425
bbe0af5b 426 GtkWidget *pixmapwid = (GtkWidget*) NULL;
f861258f
VZ
427
428 if (nb_page->m_image != -1)
3eb78d7e
RR
429 {
430 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
f861258f 431
3eb78d7e
RR
432 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
433 while (child)
8712c6e7 434 {
f861258f 435 if (GTK_IS_PIXMAP(child->data))
8712c6e7
VZ
436 {
437 pixmapwid = GTK_WIDGET(child->data);
438 break;
3eb78d7e 439 }
8712c6e7
VZ
440 child = child->next;
441 }
f861258f 442
3eb78d7e 443 /* We should have the pixmap widget now */
f861258f
VZ
444 wxASSERT(pixmapwid != NULL);
445
446 if (image == -1)
8712c6e7 447 {
3eb78d7e
RR
448 /* If there's no new widget, just remove the old from the box */
449 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
450 nb_page->m_image = -1;
53b28675 451
3eb78d7e
RR
452 return TRUE; /* Case 2) */
453 }
454 }
f861258f 455
3eb78d7e
RR
456 /* Only cases 3) and 4) left */
457 wxASSERT( m_imageList != NULL ); /* Just in case */
f861258f 458
3eb78d7e
RR
459 /* Construct the new pixmap */
460 const wxBitmap *bmp = m_imageList->GetBitmap(image);
461 GdkPixmap *pixmap = bmp->GetPixmap();
462 GdkBitmap *mask = (GdkBitmap*) NULL;
f861258f 463 if ( bmp->GetMask() )
3eb78d7e
RR
464 {
465 mask = bmp->GetMask()->GetBitmap();
466 }
f861258f
VZ
467
468 if (pixmapwid == NULL)
3eb78d7e
RR
469 {
470 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
471 pixmapwid = gtk_pixmap_new (pixmap, mask );
f861258f 472
3eb78d7e 473 /* CHECKME: Are these pack flags okay? */
b318dc42 474 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
3eb78d7e
RR
475 gtk_widget_show(pixmapwid);
476 }
f861258f 477 else
3eb78d7e
RR
478 {
479 /* Case 4) Simply replace the pixmap */
480 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
481 }
f861258f 482
3eb78d7e 483 nb_page->m_image = image;
53b28675 484
3eb78d7e 485 return TRUE;
ff7b1510 486}
53b28675
RR
487
488void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
489{
223d09f6 490 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
ff7b1510 491}
53b28675 492
b318dc42 493void wxNotebook::SetPadding( const wxSize &padding )
53b28675 494{
b318dc42
JS
495 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
496
497 m_padding = padding.GetWidth();
498
499 int i;
500 for (i=0; i<int(GetPageCount()); i++)
501 {
502 wxGtkNotebookPage* nb_page = GetNotebookPage(i);
503 wxASSERT(nb_page != NULL);
504
505 if (nb_page->m_image != -1)
506 {
507 // gtk_box_set_child_packing sets padding on BOTH sides
508 // icon provides left padding, label provides center and right
509 int image = nb_page->m_image;
510 SetPageImage(i,-1);
511 SetPageImage(i,image);
512 }
513 wxASSERT(nb_page->m_label);
514 gtk_box_set_child_packing(GTK_BOX(nb_page->m_box),
515 GTK_WIDGET(nb_page->m_label),
516 FALSE, FALSE, m_padding, GTK_PACK_END);
517 }
ff7b1510 518}
53b28675 519
74e3313b 520void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
ca8b28f2 521{
223d09f6 522 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
ca8b28f2
JS
523}
524
ff829f3f 525bool wxNotebook::DeleteAllPages()
53b28675 526{
223d09f6 527 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
a81258be 528
07b8d7ec
VZ
529 while (m_pagesData.GetCount() > 0)
530 DeletePage( m_pagesData.GetCount()-1 );
531
532 wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") );
ff829f3f 533
10199e27 534 return wxNotebookBase::DeleteAllPages();
ff7b1510 535}
53b28675 536
ff829f3f 537bool wxNotebook::DeletePage( int page )
53b28675 538{
36202885
VZ
539 // GTK sets GtkNotebook.cur_page to NULL before sending the switch page
540 // event so we have to store the selection internally
541 if ( m_selection == -1 )
542 {
543 m_selection = GetSelection();
07b8d7ec 544 if ( m_selection == (int)m_pagesData.GetCount() - 1 )
36202885
VZ
545 {
546 // the index will become invalid after the page is deleted
547 m_selection = -1;
548 }
549 }
a6aa9b1e 550
10199e27 551 // it will call our DoRemovePage() to do the real work
07b8d7ec 552 return wxNotebookBase::DeletePage(page);
fed46e72
RR
553}
554
1e6feb95 555wxNotebookPage *wxNotebook::DoRemovePage( int page )
fed46e72 556{
10199e27
VZ
557 wxNotebookPage *client = wxNotebookBase::DoRemovePage(page);
558 if ( !client )
559 return NULL;
fed46e72 560
07b8d7ec
VZ
561 gtk_widget_ref( client->m_widget );
562 gtk_widget_unrealize( client->m_widget );
563 gtk_widget_unparent( client->m_widget );
564
587ce561 565 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
ff829f3f 566
10199e27 567 m_pagesData.DeleteObject(GetNotebookPage(page));
ff829f3f 568
07b8d7ec 569 return client;
ff7b1510 570}
53b28675 571
07b8d7ec
VZ
572bool wxNotebook::InsertPage( int position,
573 wxNotebookPage* win,
574 const wxString& text,
575 bool select,
576 int imageId )
53b28675 577{
223d09f6 578 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
a81258be 579
587ce561 580 wxCHECK_MSG( win->GetParent() == this, FALSE,
223d09f6 581 wxT("Can't add a page whose parent is not the notebook!") );
8aadf227 582
07b8d7ec
VZ
583 wxCHECK_MSG( position >= 0 && position <= GetPageCount(), FALSE,
584 _T("invalid page index in wxNotebookPage::InsertPage()") );
585
a6aa9b1e
RD
586 /* don't receive switch page during addition */
587 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
587ce561 588 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
a6aa9b1e 589
a2d93e73
JS
590 if (m_themeEnabled)
591 win->SetThemeEnabled(TRUE);
592
587ce561 593 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
53b28675 594
b318dc42 595 wxGtkNotebookPage *nb_page = new wxGtkNotebookPage();
a6aa9b1e 596
07b8d7ec 597 if ( position == GetPageCount() )
b318dc42 598 m_pagesData.Append( nb_page );
587ce561 599 else
b318dc42 600 m_pagesData.Insert( m_pagesData.Item( position ), nb_page );
a6aa9b1e 601
07b8d7ec 602 m_pages.Insert(win, position);
8aadf227 603
b318dc42
JS
604 nb_page->m_box = gtk_hbox_new( FALSE, 1 );
605 gtk_container_border_width( GTK_CONTAINER(nb_page->m_box), 2 );
587ce561 606
d1af991f
RR
607 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
608 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
609
587ce561 610 if (position < 0)
b318dc42 611 gtk_notebook_append_page( notebook, win->m_widget, nb_page->m_box );
a6aa9b1e 612 else
b318dc42 613 gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position );
587ce561 614
b318dc42 615 nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data;
ef44a621 616
587ce561 617 /* set the label image */
b318dc42 618 nb_page->m_image = imageId;
a6aa9b1e 619
3eb78d7e 620 if (imageId != -1)
e4a81a2e 621 {
3eb78d7e
RR
622 wxASSERT( m_imageList != NULL );
623
624 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
625 GdkPixmap *pixmap = bmp->GetPixmap();
626 GdkBitmap *mask = (GdkBitmap*) NULL;
627 if ( bmp->GetMask() )
628 {
629 mask = bmp->GetMask()->GetBitmap();
630 }
e4a81a2e 631
3eb78d7e 632 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
24d20a8f 633
b318dc42 634 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding);
24d20a8f 635
3eb78d7e
RR
636 gtk_widget_show(pixmapwid);
637 }
24d20a8f 638
587ce561 639 /* set the label text */
b318dc42
JS
640 nb_page->m_text = text;
641 if (nb_page->m_text.IsEmpty()) nb_page->m_text = wxT("");
642
643 nb_page->m_label = GTK_LABEL( gtk_label_new(nb_page->m_text.mbc_str()) );
644 gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding );
645
587ce561 646 /* show the label */
b318dc42 647 gtk_widget_show( GTK_WIDGET(nb_page->m_label) );
07b8d7ec 648 if (select && (m_pagesData.GetCount() > 1))
587ce561
RR
649 {
650 if (position < 0)
651 SetSelection( GetPageCount()-1 );
8712c6e7 652 else
587ce561
RR
653 SetSelection( position );
654 }
741fd203 655
587ce561
RR
656 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
657 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
ff829f3f 658
3eb78d7e 659 return TRUE;
ff7b1510 660}
53b28675 661
b98d804b
RR
662void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
663{
f861258f 664 if (event.IsWindowChange())
b98d804b 665 AdvanceSelection( event.GetDirection() );
f861258f 666 else
b98d804b
RR
667 event.Skip();
668}
669
93d38175
VS
670#if wxUSE_CONSTRAINTS
671
5a8c929e 672// override these 2 functions to do nothing: everything is done in OnSize
e3e65dac 673void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
5a8c929e 674{
b292e2f5
RR
675 // don't set the sizes of the pages - their correct size is not yet known
676 wxControl::SetConstraintSizes(FALSE);
5a8c929e
VZ
677}
678
e3e65dac 679bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
5a8c929e 680{
b292e2f5 681 return TRUE;
5a8c929e
VZ
682}
683
93d38175
VS
684#endif
685
58614078 686void wxNotebook::ApplyWidgetStyle()
a81258be 687{
db434467 688 // TODO, font for labels etc
8712c6e7 689
b292e2f5
RR
690 SetWidgetStyle();
691 gtk_widget_set_style( m_widget, m_widgetStyle );
a81258be
RR
692}
693
58d1c1ae
RR
694bool wxNotebook::IsOwnGtkWindow( GdkWindow *window )
695{
696 return ((m_widget->window == window) ||
9e691f46 697 (NOTEBOOK_PANEL(m_widget) == window));
58d1c1ae
RR
698}
699
53b28675 700//-----------------------------------------------------------------------------
ff829f3f 701// wxNotebookEvent
53b28675
RR
702//-----------------------------------------------------------------------------
703
92976ab6 704IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
5b011451 705
a3a7f879 706#endif