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