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