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