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