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