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