New idle handling. Only that.
[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 #include "wx/panel.h"
16 #include "wx/utils.h"
17 #include "wx/imaglist.h"
18 #include "wx/intl.h"
19 #include "wx/log.h"
20
21 #include "gdk/gdk.h"
22 #include "gtk/gtk.h"
23 #include "wx/gtk/win_gtk.h"
24 #include "gdk/gdkkeysyms.h"
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 //-----------------------------------------------------------------------------
34 // data
35 //-----------------------------------------------------------------------------
36
37 extern bool g_blockEventsOnDrag;
38
39 //-----------------------------------------------------------------------------
40 // wxNotebookPage
41 //-----------------------------------------------------------------------------
42
43 class wxNotebookPage: public wxObject
44 {
45 public:
46 wxNotebookPage()
47 {
48 m_id = -1;
49 m_text = "";
50 m_image = -1;
51 m_page = (GtkNotebookPage *) NULL;
52 m_client = (wxWindow *) NULL;
53 m_parent = (GtkNotebook *) NULL;
54 m_box = (GtkWidget *) NULL;
55 m_added = FALSE;
56 }
57
58 /*
59 mark page as "added' to the notebook, return FALSE if the page was
60 already added
61 */
62
63 bool Add()
64 {
65 if ( WasAdded() )
66 return FALSE;
67
68 m_added = TRUE;
69 return TRUE;
70 }
71
72 bool WasAdded() const { return m_added; }
73
74 int m_id;
75 wxString m_text;
76 int m_image;
77 GtkNotebookPage *m_page;
78 GtkLabel *m_label;
79 wxWindow *m_client;
80 GtkNotebook *m_parent;
81 GtkWidget *m_box; // in which the label and image are packed
82
83 private:
84 bool m_added;
85 };
86
87 //-----------------------------------------------------------------------------
88 // "switch_page"
89 //-----------------------------------------------------------------------------
90
91 static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
92 GtkNotebookPage *WXUNUSED(page),
93 gint nPage,
94 gpointer data)
95 {
96 if (g_isIdle) wxapp_install_idle_handler();
97
98 wxNotebook *notebook = (wxNotebook *)data;
99
100 int old = notebook->GetSelection();
101
102 // TODO: emulate PAGE_CHANGING event
103
104 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
105 notebook->GetId(), nPage, old );
106 event.SetEventObject( notebook );
107 notebook->GetEventHandler()->ProcessEvent( event );
108 }
109
110 //-----------------------------------------------------------------------------
111 // "size_allocate"
112 //-----------------------------------------------------------------------------
113
114 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
115 {
116 if (g_isIdle) wxapp_install_idle_handler();
117
118 if ((win->m_x == alloc->x) &&
119 (win->m_y == alloc->y) &&
120 (win->m_width == alloc->width) &&
121 (win->m_height == alloc->height))
122 {
123 return;
124 }
125
126 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
127
128 if (win->GetAutoLayout()) win->Layout();
129 }
130
131 //-----------------------------------------------------------------------------
132 // "key_press_event"
133 //-----------------------------------------------------------------------------
134
135 static gint
136 gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *notebook )
137 {
138 if (g_isIdle) wxapp_install_idle_handler();
139
140 if (g_blockEventsOnDrag) return FALSE;
141
142 if (!notebook->HasVMT()) return FALSE;
143
144 /* this code makes jumping down from the handles of the notebooks
145 to the actual items in the visible notebook page possible with
146 the down-arrow key */
147
148 if (gdk_event->keyval != GDK_Down) return FALSE;
149
150 if (notebook != notebook->FindFocus()) return FALSE;
151
152 if (notebook->m_pages.GetCount() == 0) return FALSE;
153
154 wxNode *node = notebook->m_pages.Nth( notebook->GetSelection() );
155
156 if (!node) return FALSE;
157
158 wxNotebookPage *page = (wxNotebookPage*) node->Data();
159
160 // don't let others the key event
161 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
162
163 page->m_client->SetFocus();
164
165 return TRUE;
166 }
167
168 //-----------------------------------------------------------------------------
169 // InsertChild callback for wxNotebook
170 //-----------------------------------------------------------------------------
171
172 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
173 {
174 wxNotebookPage *page = new wxNotebookPage();
175
176 page->m_id = parent->GetPageCount();
177
178 page->m_box = gtk_hbox_new (FALSE, 0);
179 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
180
181 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
182
183 page->m_client = child;
184 gtk_notebook_append_page( notebook, child->m_widget, page->m_box );
185
186 page->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
187
188 page->m_parent = notebook;
189
190 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
191 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
192
193 wxASSERT_MSG( page->m_page, _T("Notebook page creation error") );
194
195 parent->m_pages.Append( page );
196 }
197
198 //-----------------------------------------------------------------------------
199 // wxNotebook
200 //-----------------------------------------------------------------------------
201
202 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
203
204 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
205 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
206 END_EVENT_TABLE()
207
208 void wxNotebook::Init()
209 {
210 m_imageList = (wxImageList *) NULL;
211 m_pages.DeleteContents( TRUE );
212 m_idHandler = 0;
213 }
214
215 wxNotebook::wxNotebook()
216 {
217 Init();
218 }
219
220 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
221 const wxPoint& pos, const wxSize& size,
222 long style, const wxString& name )
223 {
224 Init();
225 Create( parent, id, pos, size, style, name );
226 }
227
228 wxNotebook::~wxNotebook()
229 {
230 // don't generate change page events any more
231 if (m_idHandler != 0)
232 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
233
234 DeleteAllPages();
235 }
236
237 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
238 const wxPoint& pos, const wxSize& size,
239 long style, const wxString& name )
240 {
241 m_needParent = TRUE;
242 m_acceptsFocus = TRUE;
243 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
244
245 PreCreation( parent, id, pos, size, style, name );
246
247 m_widget = gtk_notebook_new();
248
249 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
250
251 m_idHandler = gtk_signal_connect (
252 GTK_OBJECT(m_widget), "switch_page",
253 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
254 (gpointer)this );
255
256 m_parent->AddChild( this );
257
258 (m_parent->m_insertCallback)( m_parent, this );
259
260 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
261 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
262
263 PostCreation();
264
265 Show( TRUE );
266
267 return TRUE;
268 }
269
270 int wxNotebook::GetSelection() const
271 {
272 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid notebook") );
273
274 if (m_pages.Number() == 0) return -1;
275
276 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
277 if (!g_page) return -1;
278
279 wxNotebookPage *page = (wxNotebookPage *) NULL;
280
281 wxNode *node = m_pages.First();
282 while (node)
283 {
284 page = (wxNotebookPage*)node->Data();
285
286 if ((page->m_page == g_page) || (page->m_page == (GtkNotebookPage*)NULL))
287 {
288 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
289 // "switch_page" then and we ask for GetSelection() in the handler for
290 // "switch_page". otherwise m_page should never be NULL. all this
291 // might also be wrong.
292 break;
293 }
294 node = node->Next();
295 }
296
297 wxCHECK_MSG( node != NULL, -1, _T("wxNotebook: no selection?") );
298
299 return page->m_id;
300 }
301
302 int wxNotebook::GetPageCount() const
303 {
304 // count only the pages which were already added to the notebook for MSW
305 // compatibility (and, in fact, this behaviour makes more sense anyhow
306 // because only the added pages are shown)
307
308 int n = 0;
309 for ( wxNode *node = m_pages.First(); node; node = node->Next() )
310 {
311 wxNotebookPage *page = (wxNotebookPage*)node->Data();
312
313 if (page->WasAdded()) n++;
314 }
315
316 return n;
317 }
318
319 int wxNotebook::GetRowCount() const
320 {
321 return 1;
322 }
323
324 wxString wxNotebook::GetPageText( int page ) const
325 {
326 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid notebook") );
327
328 wxNotebookPage* nb_page = GetNotebookPage(page);
329 if (nb_page)
330 return nb_page->m_text;
331 else
332 return "";
333 }
334
335 int wxNotebook::GetPageImage( int page ) const
336 {
337 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid notebook") );
338
339 wxNotebookPage* nb_page = GetNotebookPage(page);
340 if (nb_page)
341 return nb_page->m_image;
342 else
343 return 0;
344 }
345
346 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
347 {
348 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*)NULL, _T("invalid notebook") );
349
350 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
351
352 wxNode *node = m_pages.First();
353 while (node)
354 {
355 nb_page = (wxNotebookPage*)node->Data();
356 if (nb_page->m_id == page)
357 return nb_page;
358 node = node->Next();
359 }
360
361 wxFAIL_MSG( _T("Notebook page not found!") );
362
363 return (wxNotebookPage *) NULL;
364 }
365
366 int wxNotebook::SetSelection( int page )
367 {
368 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid notebook") );
369
370 int selOld = GetSelection();
371 wxNotebookPage* nb_page = GetNotebookPage(page);
372
373 if (!nb_page) return -1;
374
375 int page_num = 0;
376 GList *child = GTK_NOTEBOOK(m_widget)->children;
377 while (child)
378 {
379 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
380 page_num++;
381 child = child->next;
382 }
383
384 if (!child) return -1;
385
386 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
387
388 return selOld;
389 }
390
391 void wxNotebook::AdvanceSelection( bool bForward )
392 {
393 wxCHECK_RET( m_widget != NULL, _T("invalid notebook") );
394
395 int sel = GetSelection();
396 int max = GetPageCount();
397
398 if (bForward)
399 SetSelection( sel == max ? 0 : sel + 1 );
400 else
401 SetSelection( sel == 0 ? max-1 : sel - 1 );
402 }
403
404 void wxNotebook::SetImageList( wxImageList* imageList )
405 {
406 m_imageList = imageList;
407 }
408
409 bool wxNotebook::SetPageText( int page, const wxString &text )
410 {
411 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid notebook") );
412
413 wxNotebookPage* nb_page = GetNotebookPage(page);
414
415 wxCHECK_MSG( nb_page, FALSE, _T("SetPageText: invalid page index") );
416
417 nb_page->m_text = text;
418
419 gtk_label_set(nb_page->m_label, nb_page->m_text.mbc_str());
420
421 return TRUE;
422 }
423
424 bool wxNotebook::SetPageImage( int page, int image )
425 {
426 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
427
428 wxNotebookPage* nb_page = GetNotebookPage(page);
429
430 if (!nb_page) return FALSE;
431
432 /* Optimization posibility: return immediately if image unchanged.
433 * Not enabled because it may break existing (stupid) code that
434 * manipulates the imagelist to cycle images */
435
436 /* if (image == nb_page->m_image) return TRUE; */
437
438 /* For different cases:
439 1) no image -> no image
440 2) image -> no image
441 3) no image -> image
442 4) image -> image */
443
444 if (image == -1 && nb_page->m_image == -1)
445 return TRUE; /* Case 1): Nothing to do. */
446
447 GtkWidget *pixmapwid = (GtkWidget*) NULL;
448
449 if (nb_page->m_image != -1)
450 {
451 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
452
453 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
454 while (child)
455 {
456 if (GTK_IS_PIXMAP(child->data))
457 {
458 pixmapwid = GTK_WIDGET(child->data);
459 break;
460 }
461 child = child->next;
462 }
463
464 /* We should have the pixmap widget now */
465 wxASSERT(pixmapwid != NULL);
466
467 if (image == -1)
468 {
469 /* If there's no new widget, just remove the old from the box */
470 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
471 nb_page->m_image = -1;
472
473 return TRUE; /* Case 2) */
474 }
475 }
476
477 /* Only cases 3) and 4) left */
478 wxASSERT( m_imageList != NULL ); /* Just in case */
479
480 /* Construct the new pixmap */
481 const wxBitmap *bmp = m_imageList->GetBitmap(image);
482 GdkPixmap *pixmap = bmp->GetPixmap();
483 GdkBitmap *mask = (GdkBitmap*) NULL;
484 if ( bmp->GetMask() )
485 {
486 mask = bmp->GetMask()->GetBitmap();
487 }
488
489 if (pixmapwid == NULL)
490 {
491 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
492 pixmapwid = gtk_pixmap_new (pixmap, mask );
493
494 /* CHECKME: Are these pack flags okay? */
495 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, 3);
496 gtk_widget_show(pixmapwid);
497 }
498 else
499 {
500 /* Case 4) Simply replace the pixmap */
501 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
502 }
503
504 nb_page->m_image = image;
505
506 return TRUE;
507 }
508
509 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
510 {
511 wxFAIL_MSG( _T("wxNotebook::SetPageSize not implemented") );
512 }
513
514 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
515 {
516 wxFAIL_MSG( _T("wxNotebook::SetPadding not implemented") );
517 }
518
519 void wxNotebook::SetTabSize(const wxSize& sz)
520 {
521 wxFAIL_MSG( _T("wxNotebook::SetTabSize not implemented") );
522 }
523
524 bool wxNotebook::DeleteAllPages()
525 {
526 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid notebook") );
527
528 wxNode *page_node = m_pages.First();
529 while (page_node)
530 {
531 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
532
533 DeletePage( page->m_id );
534
535 page_node = m_pages.First();
536 }
537
538 return TRUE;
539 }
540
541 bool wxNotebook::DeletePage( int page )
542 {
543 wxNotebookPage* nb_page = GetNotebookPage(page);
544 if (!nb_page) return FALSE;
545
546 int page_num = 0;
547 GList *child = GTK_NOTEBOOK(m_widget)->children;
548 while (child)
549 {
550 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
551 page_num++;
552 child = child->next;
553 }
554
555 wxCHECK_MSG( child != NULL, FALSE, _T("illegal notebook index") );
556
557 delete nb_page->m_client;
558
559 m_pages.DeleteObject( nb_page );
560
561 return TRUE;
562 }
563
564 bool wxNotebook::RemovePage( int page )
565 {
566 wxNotebookPage* nb_page = GetNotebookPage(page);
567 if (!nb_page) return FALSE;
568
569 int page_num = 0;
570 GList *child = GTK_NOTEBOOK(m_widget)->children;
571 while (child)
572 {
573 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
574 page_num++;
575 child = child->next;
576 }
577
578 wxCHECK_MSG( child != NULL, FALSE, _T("illegal notebook index") );
579
580 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
581
582 m_pages.DeleteObject( nb_page );
583
584 return TRUE;
585 }
586
587 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
588 bool select, int imageId)
589 {
590 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid notebook") );
591
592 /* we've created the notebook page in AddChild(). Now we just have to set
593 the caption for the page and set the others parameters. */
594
595 wxNotebookPage *page = (wxNotebookPage *) NULL;
596
597 wxNode *node = m_pages.First();
598 while (node)
599 {
600 page = (wxNotebookPage*)node->Data();
601 if ( page->m_client == win ) break;
602 node = node->Next();
603 }
604
605 wxCHECK_MSG( page != NULL, FALSE,
606 _T("Can't add a page whose parent is not the notebook!") );
607
608 wxCHECK_MSG( page->Add(), FALSE,
609 _T("Can't add the same page twice to a notebook.") );
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 /* then set the attributes */
631 page->m_text = text;
632 if (page->m_text.IsEmpty()) page->m_text = _T("");
633 page->m_image = imageId;
634 page->m_label = (GtkLabel *)gtk_label_new(page->m_text.mbc_str());
635 gtk_box_pack_end( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
636
637 /* @@@: what does this do? do we still need it?
638 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); */
639
640 gtk_widget_show((GtkWidget *)page->m_label);
641
642 if (select) SetSelection( GetPageCount()-1 );
643
644 return TRUE;
645 }
646
647 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
648 {
649 if (event.IsWindowChange())
650 AdvanceSelection( event.GetDirection() );
651 else
652 event.Skip();
653 }
654
655 wxWindow *wxNotebook::GetPage( int page ) const
656 {
657 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, _T("invalid notebook") );
658
659 wxNotebookPage* nb_page = GetNotebookPage(page);
660 if (!nb_page)
661 return (wxWindow *) NULL;
662 else
663 return nb_page->m_client;
664 }
665
666 // override these 2 functions to do nothing: everything is done in OnSize
667 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
668 {
669 // don't set the sizes of the pages - their correct size is not yet known
670 wxControl::SetConstraintSizes(FALSE);
671 }
672
673 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
674 {
675 return TRUE;
676 }
677
678 void wxNotebook::ApplyWidgetStyle()
679 {
680 SetWidgetStyle();
681 gtk_widget_set_style( m_widget, m_widgetStyle );
682 }
683
684 //-----------------------------------------------------------------------------
685 // wxNotebookEvent
686 //-----------------------------------------------------------------------------
687
688 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
689