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