]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/notebook.cpp
7eee0d52467083293d297e2a04e08196a87299d7
[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 #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 if (gdk_event->keyval != GDK_Down) return FALSE;
132
133 if (notebook != notebook->FindFocus()) return FALSE;
134
135 if (notebook->m_pages.GetCount() == 0) return FALSE;
136
137 wxNode *node = notebook->m_pages.Nth( notebook->GetSelection() );
138
139 if (!node) return FALSE;
140
141 wxNotebookPage *page = (wxNotebookPage*) node->Data();
142
143 // don't let others the key event
144 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
145
146 page->m_client->SetFocus();
147
148 return TRUE;
149 }
150
151 //-----------------------------------------------------------------------------
152 // InsertChild callback for wxNotebook
153 //-----------------------------------------------------------------------------
154
155 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
156 {
157 wxNotebookPage *page = new wxNotebookPage();
158
159 page->m_id = parent->GetPageCount();
160
161 page->m_box = gtk_hbox_new (FALSE, 0);
162 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
163
164 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
165
166 page->m_client = child;
167 gtk_notebook_append_page( notebook, child->m_widget, page->m_box );
168
169 page->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
170
171 page->m_parent = notebook;
172
173 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
174 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
175
176 wxASSERT_MSG( page->m_page, "Notebook page creation error" );
177
178 parent->m_pages.Append( page );
179 }
180
181 //-----------------------------------------------------------------------------
182 // wxNotebook
183 //-----------------------------------------------------------------------------
184
185 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
186
187 void wxNotebook::Init()
188 {
189 m_imageList = (wxImageList *) NULL;
190 m_pages.DeleteContents( TRUE );
191 m_idHandler = 0;
192 }
193
194 wxNotebook::wxNotebook()
195 {
196 Init();
197 }
198
199 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
200 const wxPoint& pos, const wxSize& size,
201 long style, const wxString& name )
202 {
203 Init();
204 Create( parent, id, pos, size, style, name );
205 }
206
207 wxNotebook::~wxNotebook()
208 {
209 // don't generate change page events any more
210 if (m_idHandler != 0)
211 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
212
213 DeleteAllPages();
214 }
215
216 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
217 const wxPoint& pos, const wxSize& size,
218 long style, const wxString& name )
219 {
220 m_needParent = TRUE;
221 m_acceptsFocus = TRUE;
222 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
223
224 PreCreation( parent, id, pos, size, style, name );
225
226 m_widget = gtk_notebook_new();
227
228 #ifdef __WXDEBUG__
229 debug_focus_in( m_widget, "wxNotebook::m_widget", name );
230 #endif
231
232 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
233
234 m_idHandler = gtk_signal_connect (
235 GTK_OBJECT(m_widget), "switch_page",
236 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
237 (gpointer)this );
238
239 m_parent->AddChild( this );
240
241 (m_parent->m_insertCallback)( m_parent, this );
242
243 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
244 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
245
246 PostCreation();
247
248 Show( TRUE );
249
250 return TRUE;
251 }
252
253 int wxNotebook::GetSelection() const
254 {
255 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
256
257 if (m_pages.Number() == 0) return -1;
258
259 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
260 if (!g_page) return -1;
261
262 wxNotebookPage *page = (wxNotebookPage *) NULL;
263
264 wxNode *node = m_pages.First();
265 while (node)
266 {
267 page = (wxNotebookPage*)node->Data();
268
269 if ((page->m_page == g_page) || (page->m_page == (GtkNotebookPage*)NULL))
270 {
271 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
272 // "switch_page" then and we ask for GetSelection() in the handler for
273 // "switch_page". otherwise m_page should never be NULL. all this
274 // might also be wrong.
275 break;
276 }
277 node = node->Next();
278 }
279
280 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
281
282 return page->m_id;
283 }
284
285 int wxNotebook::GetPageCount() const
286 {
287 // count only the pages which were already added to the notebook for MSW
288 // compatibility (and, in fact, this behaviour makes more sense anyhow
289 // because only the added pages are shown)
290
291 int n = 0;
292 for ( wxNode *node = m_pages.First(); node; node = node->Next() )
293 {
294 wxNotebookPage *page = (wxNotebookPage*)node->Data();
295
296 if (page->WasAdded()) n++;
297 }
298
299 return n;
300 }
301
302 int wxNotebook::GetRowCount() const
303 {
304 return 1;
305 }
306
307 wxString wxNotebook::GetPageText( int page ) const
308 {
309 wxCHECK_MSG( m_widget != NULL, "", "invalid notebook" );
310
311 wxNotebookPage* nb_page = GetNotebookPage(page);
312 if (nb_page)
313 return nb_page->m_text;
314 else
315 return "";
316 }
317
318 int wxNotebook::GetPageImage( int page ) const
319 {
320 wxCHECK_MSG( m_widget != NULL, 0, "invalid notebook" );
321
322 wxNotebookPage* nb_page = GetNotebookPage(page);
323 if (nb_page)
324 return nb_page->m_image;
325 else
326 return 0;
327 }
328
329 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
330 {
331 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*)NULL, "invalid notebook" );
332
333 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
334
335 wxNode *node = m_pages.First();
336 while (node)
337 {
338 nb_page = (wxNotebookPage*)node->Data();
339 if (nb_page->m_id == page)
340 return nb_page;
341 node = node->Next();
342 }
343
344 wxFAIL_MSG( "Notebook page not found!" );
345
346 return (wxNotebookPage *) NULL;
347 }
348
349 int wxNotebook::SetSelection( int page )
350 {
351 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
352
353 int selOld = GetSelection();
354 wxNotebookPage* nb_page = GetNotebookPage(page);
355
356 if (!nb_page) return -1;
357
358 int page_num = 0;
359 GList *child = GTK_NOTEBOOK(m_widget)->children;
360 while (child)
361 {
362 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
363 page_num++;
364 child = child->next;
365 }
366
367 if (!child) return -1;
368
369 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
370
371 return selOld;
372 }
373
374 void wxNotebook::AdvanceSelection( bool bForward )
375 {
376 wxCHECK_RET( m_widget != NULL, "invalid notebook" );
377
378 int sel = GetSelection();
379 int max = GetPageCount();
380
381 if (bForward)
382 SetSelection( sel == max ? 0 : sel + 1 );
383 else
384 SetSelection( sel == 0 ? max : 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, "invalid notebook" );
395
396 wxNotebookPage* nb_page = GetNotebookPage(page);
397
398 if (!nb_page) return FALSE;
399
400 nb_page->m_text = text;
401
402 if (nb_page->m_text.IsEmpty()) nb_page->m_text = "";
403
404 gtk_label_set(nb_page->m_label, nb_page->m_text);
405
406 return TRUE;
407 }
408
409 bool wxNotebook::SetPageImage( int page, int image )
410 {
411 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
412
413 wxNotebookPage* nb_page = GetNotebookPage(page);
414
415 if (!nb_page) return FALSE;
416
417 /* Optimization posibility: return immediately if image unchanged.
418 * Not enabled because it may break existing (stupid) code that
419 * manipulates the imagelist to cycle images */
420
421 /* if (image == nb_page->m_image) return TRUE; */
422
423 /* For different cases:
424 1) no image -> no image
425 2) image -> no image
426 3) no image -> image
427 4) image -> image */
428
429 if (image == -1 && nb_page->m_image == -1)
430 return TRUE; /* Case 1): Nothing to do. */
431
432 GtkWidget *pixmapwid = (GtkWidget*) NULL;
433
434 if (nb_page->m_image != -1)
435 {
436 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
437
438 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
439 while (child)
440 {
441 if (GTK_IS_PIXMAP(child->data))
442 {
443 pixmapwid = GTK_WIDGET(child->data);
444 break;
445 }
446 child = child->next;
447 }
448
449 /* We should have the pixmap widget now */
450 wxASSERT(pixmapwid != NULL);
451
452 if (image == -1)
453 {
454 /* If there's no new widget, just remove the old from the box */
455 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
456 nb_page->m_image = -1;
457
458 return TRUE; /* Case 2) */
459 }
460 }
461
462 /* Only cases 3) and 4) left */
463 wxASSERT( m_imageList != NULL ); /* Just in case */
464
465 /* Construct the new pixmap */
466 const wxBitmap *bmp = m_imageList->GetBitmap(image);
467 GdkPixmap *pixmap = bmp->GetPixmap();
468 GdkBitmap *mask = (GdkBitmap*) NULL;
469 if ( bmp->GetMask() )
470 {
471 mask = bmp->GetMask()->GetBitmap();
472 }
473
474 if (pixmapwid == NULL)
475 {
476 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
477 pixmapwid = gtk_pixmap_new (pixmap, mask );
478
479 /* CHECKME: Are these pack flags okay? */
480 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, 3);
481 gtk_widget_show(pixmapwid);
482 }
483 else
484 {
485 /* Case 4) Simply replace the pixmap */
486 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
487 }
488
489 nb_page->m_image = image;
490
491 return TRUE;
492 }
493
494 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
495 {
496 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
497 }
498
499 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
500 {
501 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
502 }
503
504 bool wxNotebook::DeleteAllPages()
505 {
506 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
507
508 wxNode *page_node = m_pages.First();
509 while (page_node)
510 {
511 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
512
513 DeletePage( page->m_id );
514
515 page_node = m_pages.First();
516 }
517
518 return TRUE;
519 }
520
521 bool wxNotebook::DeletePage( int page )
522 {
523 wxNotebookPage* nb_page = GetNotebookPage(page);
524 if (!nb_page) return FALSE;
525
526 int page_num = 0;
527 GList *child = GTK_NOTEBOOK(m_widget)->children;
528 while (child)
529 {
530 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
531 page_num++;
532 child = child->next;
533 }
534
535 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
536
537 delete nb_page->m_client;
538
539 m_pages.DeleteObject( nb_page );
540
541 return TRUE;
542 }
543
544 bool wxNotebook::RemovePage( int page )
545 {
546 wxNotebookPage* nb_page = GetNotebookPage(page);
547 if (!nb_page) return FALSE;
548
549 int page_num = 0;
550 GList *child = GTK_NOTEBOOK(m_widget)->children;
551 while (child)
552 {
553 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
554 page_num++;
555 child = child->next;
556 }
557
558 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
559
560 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
561
562 m_pages.DeleteObject( nb_page );
563
564 return TRUE;
565 }
566
567 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
568 bool select, int imageId)
569 {
570 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
571
572 /* we've created the notebook page in AddChild(). Now we just have to set
573 the caption for the page and set the others parameters. */
574
575 wxNotebookPage *page = (wxNotebookPage *) NULL;
576
577 wxNode *node = m_pages.First();
578 while (node)
579 {
580 page = (wxNotebookPage*)node->Data();
581 if ( page->m_client == win ) break;
582 node = node->Next();
583 }
584
585 wxCHECK_MSG( page != NULL, FALSE,
586 "Can't add a page whose parent is not the notebook!" );
587
588 wxCHECK_MSG( page->Add(), FALSE,
589 "Can't add the same page twice to a notebook." );
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 /* then set the attributes */
611 page->m_text = text;
612 if (page->m_text.IsEmpty()) page->m_text = "";
613 page->m_image = imageId;
614 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
615 gtk_box_pack_end( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
616
617 /* @@@: what does this do? do we still need it?
618 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); */
619
620 gtk_widget_show((GtkWidget *)page->m_label);
621
622 if (select) SetSelection( GetPageCount()-1 );
623
624 return TRUE;
625 }
626
627 wxWindow *wxNotebook::GetPage( int page ) const
628 {
629 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, "invalid notebook" );
630
631 wxNotebookPage* nb_page = GetNotebookPage(page);
632 if (!nb_page)
633 return (wxWindow *) NULL;
634 else
635 return nb_page->m_client;
636 }
637
638 // override these 2 functions to do nothing: everything is done in OnSize
639 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
640 {
641 // don't set the sizes of the pages - their correct size is not yet known
642 wxControl::SetConstraintSizes(FALSE);
643 }
644
645 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
646 {
647 return TRUE;
648 }
649
650 void wxNotebook::ApplyWidgetStyle()
651 {
652 SetWidgetStyle();
653 gtk_widget_set_style( m_widget, m_widgetStyle );
654 }
655
656 //-----------------------------------------------------------------------------
657 // wxNotebookEvent
658 //-----------------------------------------------------------------------------
659
660 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
661