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