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