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