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