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