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