]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/notebook.cpp
Some corrections
[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 <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.SetCurrentFocus( win );
182 if (!page->m_client->GetEventHandler()->ProcessEvent( event ))
183 {
184 page->m_client->SetFocus();
185 }
186
187 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
188 return TRUE;
189 }
190
191 return FALSE;
192 }
193
194 //-----------------------------------------------------------------------------
195 // InsertChild callback for wxNotebook
196 //-----------------------------------------------------------------------------
197
198 static void wxInsertChildInNotebook( wxNotebook* WXUNUSED(parent), wxWindow* WXUNUSED(child) )
199 {
200 /* we don't do anything here but pray */
201 }
202
203 //-----------------------------------------------------------------------------
204 // wxNotebook
205 //-----------------------------------------------------------------------------
206
207 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
208
209 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
210 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
211 END_EVENT_TABLE()
212
213 void wxNotebook::Init()
214 {
215 m_imageList = (wxImageList *) NULL;
216 m_pages.DeleteContents( TRUE );
217 m_lastSelection = -1;
218 }
219
220 wxNotebook::wxNotebook()
221 {
222 Init();
223 }
224
225 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
226 const wxPoint& pos, const wxSize& size,
227 long style, const wxString& name )
228 {
229 Init();
230 Create( parent, id, pos, size, style, name );
231 }
232
233 wxNotebook::~wxNotebook()
234 {
235 /* don't generate change page events any more */
236 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
237 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
238
239 DeleteAllPages();
240 }
241
242 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
243 const wxPoint& pos, const wxSize& size,
244 long style, const wxString& name )
245 {
246 m_needParent = TRUE;
247 m_acceptsFocus = TRUE;
248 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
249
250 if (!PreCreation( parent, pos, size ) ||
251 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
252 {
253 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
254 return FALSE;
255 }
256
257
258 m_widget = gtk_notebook_new();
259
260 #ifdef __WXDEBUG__
261 debug_focus_in( m_widget, wxT("wxNotebook::m_widget"), name );
262 #endif
263
264 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
265
266 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
267 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
268
269 m_parent->DoAddChild( this );
270
271 if (m_windowStyle & wxNB_RIGHT)
272 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
273 if (m_windowStyle & wxNB_LEFT)
274 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
275 if (m_windowStyle & wxNB_BOTTOM)
276 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
277
278 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
279 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
280
281 PostCreation();
282
283 SetFont( parent->GetFont() );
284
285 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
286 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback), (gpointer) this );
287
288 Show( TRUE );
289
290 return TRUE;
291 }
292
293 int wxNotebook::GetSelection() const
294 {
295 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
296
297 GList *pages = GTK_NOTEBOOK(m_widget)->children;
298
299 if (g_list_length(pages) == 0) return -1;
300
301 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
302
303 if (notebook->cur_page == NULL) return m_lastSelection;
304
305 return g_list_index( pages, (gpointer)(notebook->cur_page) );
306 }
307
308 int wxNotebook::GetPageCount() const
309 {
310 return (int) g_list_length( GTK_NOTEBOOK(m_widget)->children );
311 }
312
313 int wxNotebook::GetRowCount() const
314 {
315 return 1;
316 }
317
318 wxString wxNotebook::GetPageText( int page ) const
319 {
320 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") );
321
322 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
323 if (nb_page)
324 return nb_page->m_text;
325 else
326 return wxT("");
327 }
328
329 int wxNotebook::GetPageImage( int page ) const
330 {
331 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
332
333 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
334 if (nb_page)
335 return nb_page->m_image;
336 else
337 return -1;
338 }
339
340 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
341 {
342 wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") );
343
344 wxCHECK_MSG( page < (int)m_pages.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") );
345
346 wxNode *node = m_pages.Nth( page );
347
348 return (wxGtkNotebookPage *) node->Data();
349 }
350
351 int wxNotebook::SetSelection( int page )
352 {
353 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
354
355 wxCHECK_MSG( page < (int)m_pages.GetCount(), -1, wxT("invalid notebook index") );
356
357 int selOld = GetSelection();
358
359 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page );
360
361 wxGtkNotebookPage* g_page = GetNotebookPage( page );
362 if (g_page->m_client)
363 g_page->m_client->SetFocus();
364
365 return selOld;
366 }
367
368 void wxNotebook::AdvanceSelection( bool forward )
369 {
370 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
371
372 int max = GetPageCount();
373 if ( !max )
374 {
375 // nothing to do with empty notebook
376 return;
377 }
378
379 int sel = GetSelection();
380
381 if (forward)
382 SetSelection( sel == max - 1 ? 0 : sel + 1 );
383 else
384 SetSelection( sel == 0 ? max - 1 : sel - 1 );
385 }
386
387 void wxNotebook::SetImageList( wxImageList* imageList )
388 {
389 m_imageList = imageList;
390 }
391
392 bool wxNotebook::SetPageText( int page, const wxString &text )
393 {
394 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
395
396 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
397
398 wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") );
399
400 nb_page->m_text = text;
401
402 gtk_label_set( nb_page->m_label, nb_page->m_text.mbc_str() );
403
404 return TRUE;
405 }
406
407 bool wxNotebook::SetPageImage( int page, int image )
408 {
409 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
410
411 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
412
413 if (!nb_page) return FALSE;
414
415 /* Optimization posibility: return immediately if image unchanged.
416 * Not enabled because it may break existing (stupid) code that
417 * manipulates the imagelist to cycle images */
418
419 /* if (image == nb_page->m_image) return TRUE; */
420
421 /* For different cases:
422 1) no image -> no image
423 2) image -> no image
424 3) no image -> image
425 4) image -> image */
426
427 if (image == -1 && nb_page->m_image == -1)
428 return TRUE; /* Case 1): Nothing to do. */
429
430 GtkWidget *pixmapwid = (GtkWidget*) NULL;
431
432 if (nb_page->m_image != -1)
433 {
434 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
435
436 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
437 while (child)
438 {
439 if (GTK_IS_PIXMAP(child->data))
440 {
441 pixmapwid = GTK_WIDGET(child->data);
442 break;
443 }
444 child = child->next;
445 }
446
447 /* We should have the pixmap widget now */
448 wxASSERT(pixmapwid != NULL);
449
450 if (image == -1)
451 {
452 /* If there's no new widget, just remove the old from the box */
453 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
454 nb_page->m_image = -1;
455
456 return TRUE; /* Case 2) */
457 }
458 }
459
460 /* Only cases 3) and 4) left */
461 wxASSERT( m_imageList != NULL ); /* Just in case */
462
463 /* Construct the new pixmap */
464 const wxBitmap *bmp = m_imageList->GetBitmap(image);
465 GdkPixmap *pixmap = bmp->GetPixmap();
466 GdkBitmap *mask = (GdkBitmap*) NULL;
467 if ( bmp->GetMask() )
468 {
469 mask = bmp->GetMask()->GetBitmap();
470 }
471
472 if (pixmapwid == NULL)
473 {
474 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
475 pixmapwid = gtk_pixmap_new (pixmap, mask );
476
477 /* CHECKME: Are these pack flags okay? */
478 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, 3);
479 gtk_widget_show(pixmapwid);
480 }
481 else
482 {
483 /* Case 4) Simply replace the pixmap */
484 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
485 }
486
487 nb_page->m_image = image;
488
489 return TRUE;
490 }
491
492 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
493 {
494 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
495 }
496
497 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
498 {
499 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
500 }
501
502 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
503 {
504 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
505 }
506
507 bool wxNotebook::DeleteAllPages()
508 {
509 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
510
511 while (m_pages.GetCount() > 0)
512 DeletePage( m_pages.GetCount()-1 );
513
514 return TRUE;
515 }
516
517 bool wxNotebook::DeletePage( int page )
518 {
519 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
520 if (!nb_page) return FALSE;
521
522 /* GTK sets GtkNotebook.cur_page to NULL before sending
523 the switvh page event */
524 m_lastSelection = GetSelection();
525
526 nb_page->m_client->Destroy();
527 m_pages.DeleteObject( nb_page );
528
529 m_lastSelection = -1;
530
531 return TRUE;
532 }
533
534 bool wxNotebook::RemovePage( int page )
535 {
536 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
537
538 if (!nb_page) return FALSE;
539
540 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
541
542 m_pages.DeleteObject( nb_page );
543
544 return TRUE;
545 }
546
547 bool wxNotebook::InsertPage( int position, wxNotebookPage* win, const wxString& text,
548 bool select, int imageId )
549 {
550 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
551
552 wxCHECK_MSG( win->GetParent() == this, FALSE,
553 wxT("Can't add a page whose parent is not the notebook!") );
554
555 /* don't receive switch page during addition */
556 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
557 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
558
559 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
560
561 wxGtkNotebookPage *page = new wxGtkNotebookPage();
562
563 if (position < 0)
564 m_pages.Append( page );
565 else
566 m_pages.Insert( m_pages.Nth( position ), page );
567
568 page->m_client = win;
569
570 page->m_box = gtk_hbox_new( FALSE, 0 );
571 gtk_container_border_width( GTK_CONTAINER(page->m_box), 2 );
572
573 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
574 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
575
576 if (position < 0)
577 gtk_notebook_append_page( notebook, win->m_widget, page->m_box );
578 else
579 gtk_notebook_insert_page( notebook, win->m_widget, page->m_box, position );
580
581 page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data;
582
583 /* set the label image */
584 page->m_image = imageId;
585
586 if (imageId != -1)
587 {
588 wxASSERT( m_imageList != NULL );
589
590 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
591 GdkPixmap *pixmap = bmp->GetPixmap();
592 GdkBitmap *mask = (GdkBitmap*) NULL;
593 if ( bmp->GetMask() )
594 {
595 mask = bmp->GetMask()->GetBitmap();
596 }
597
598 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
599
600 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
601
602 gtk_widget_show(pixmapwid);
603 }
604
605 /* set the label text */
606 page->m_text = text;
607 if (page->m_text.IsEmpty()) page->m_text = wxT("");
608
609 page->m_label = GTK_LABEL( gtk_label_new(page->m_text.mbc_str()) );
610 gtk_box_pack_end( GTK_BOX(page->m_box), GTK_WIDGET(page->m_label), FALSE, FALSE, 3 );
611
612 /* show the label */
613 gtk_widget_show( GTK_WIDGET(page->m_label) );
614
615 if (select && (m_pages.GetCount() > 1))
616 {
617 if (position < 0)
618 SetSelection( GetPageCount()-1 );
619 else
620 SetSelection( position );
621 }
622
623 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
624 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
625
626 return TRUE;
627 }
628
629 bool wxNotebook::AddPage(wxNotebookPage* win, const wxString& text,
630 bool select, int imageId)
631 {
632 return InsertPage( -1, win, text, select, imageId );
633 }
634
635 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
636 {
637 if (event.IsWindowChange())
638 AdvanceSelection( event.GetDirection() );
639 else
640 event.Skip();
641 }
642
643 wxNotebookPage *wxNotebook::GetPage( int page ) const
644 {
645 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, wxT("invalid notebook") );
646
647 wxGtkNotebookPage* nb_page = GetNotebookPage(page);
648 if (!nb_page)
649 return (wxNotebookPage *) NULL;
650 else
651 return nb_page->m_client;
652 }
653
654 #if wxUSE_CONSTRAINTS
655
656 // override these 2 functions to do nothing: everything is done in OnSize
657 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
658 {
659 // don't set the sizes of the pages - their correct size is not yet known
660 wxControl::SetConstraintSizes(FALSE);
661 }
662
663 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
664 {
665 return TRUE;
666 }
667
668 #endif
669
670 void wxNotebook::ApplyWidgetStyle()
671 {
672 // TODO, font for labels etc
673
674 SetWidgetStyle();
675 gtk_widget_set_style( m_widget, m_widgetStyle );
676 }
677
678 bool wxNotebook::IsOwnGtkWindow( GdkWindow *window )
679 {
680 return ((m_widget->window == window) ||
681 (GTK_NOTEBOOK(m_widget)->panel == window));
682 }
683
684 //-----------------------------------------------------------------------------
685 // wxNotebookEvent
686 //-----------------------------------------------------------------------------
687
688 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
689
690 #endif