1. wxNotebook::GetPageCount() returns only the number of pages actually added
[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 //-----------------------------------------------------------------------------
22 // wxNotebookPage
23 //-----------------------------------------------------------------------------
24
25 class wxNotebookPage: public wxObject
26 {
27 public:
28 wxNotebookPage()
29 {
30 m_id = -1;
31 m_text = "";
32 m_image = -1;
33 m_page = (GtkNotebookPage *) NULL;
34 m_client = (wxWindow *) NULL;
35 m_parent = (GtkNotebook *) NULL;
36 m_box = (GtkWidget *) NULL;
37 m_added = FALSE;
38 }
39
40 // mark page as "added' to the notebook, return FALSE if the page was
41 // already added
42 bool Add()
43 {
44 if ( WasAdded() )
45 return FALSE;
46
47 m_added = TRUE;
48 return TRUE;
49 }
50
51 bool WasAdded() const { return m_added; }
52
53 int m_id;
54 wxString m_text;
55 int m_image;
56 GtkNotebookPage *m_page;
57 GtkLabel *m_label;
58 wxWindow *m_client;
59 GtkNotebook *m_parent;
60 GtkWidget *m_box; // in which the label and image are packed
61
62 private:
63 bool m_added;
64 };
65
66 //-----------------------------------------------------------------------------
67 // "switch_page"
68 //-----------------------------------------------------------------------------
69
70 static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
71 GtkNotebookPage *WXUNUSED(page),
72 gint nPage,
73 gpointer data)
74 {
75 wxNotebook *notebook = (wxNotebook *)data;
76
77 int old = notebook->GetSelection();
78
79 // TODO: emulate PAGE_CHANGING event
80
81 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
82 notebook->GetId(), nPage, old );
83 event.SetEventObject( notebook );
84 notebook->GetEventHandler()->ProcessEvent( event );
85 }
86
87 //-----------------------------------------------------------------------------
88 // "size_allocate"
89 //-----------------------------------------------------------------------------
90
91 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
92 {
93 if ((win->m_x == alloc->x) &&
94 (win->m_y == alloc->y) &&
95 (win->m_width == alloc->width) &&
96 (win->m_height == alloc->height))
97 {
98 return;
99 }
100
101 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
102 }
103
104 //-----------------------------------------------------------------------------
105 // InsertChild callback for wxNotebook
106 //-----------------------------------------------------------------------------
107
108 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
109 {
110 wxNotebookPage *page = new wxNotebookPage();
111
112 page->m_id = parent->GetPageCount();
113
114 page->m_box = gtk_hbox_new (FALSE, 0);
115 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
116
117 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
118
119 page->m_client = child;
120 gtk_notebook_append_page( notebook, child->m_widget, page->m_box );
121
122 page->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
123
124 page->m_parent = notebook;
125
126 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
127 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
128
129 if (!page->m_page)
130 {
131 wxLogFatalError( "Notebook page creation error" );
132 return;
133 }
134
135 parent->m_pages.Append( page );
136 }
137
138 //-----------------------------------------------------------------------------
139 // wxNotebook
140 //-----------------------------------------------------------------------------
141
142 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
143
144 void wxNotebook::Init()
145 {
146 m_imageList = (wxImageList *) NULL;
147 m_pages.DeleteContents( TRUE );
148 m_idHandler = 0;
149 }
150
151 wxNotebook::wxNotebook()
152 {
153 Init();
154 }
155
156 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
157 const wxPoint& pos, const wxSize& size,
158 long style, const wxString& name )
159 {
160 Init();
161 Create( parent, id, pos, size, style, name );
162 }
163
164 wxNotebook::~wxNotebook()
165 {
166 // don't generate change page events any more
167 if (m_idHandler != 0)
168 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
169
170 DeleteAllPages();
171 }
172
173 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
174 const wxPoint& pos, const wxSize& size,
175 long style, const wxString& name )
176 {
177 m_needParent = TRUE;
178 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
179
180 PreCreation( parent, id, pos, size, style, name );
181
182 m_widget = gtk_notebook_new();
183
184 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
185
186 m_idHandler = gtk_signal_connect (
187 GTK_OBJECT(m_widget), "switch_page",
188 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
189 (gpointer)this );
190
191 m_parent->AddChild( this );
192
193 (m_parent->m_insertCallback)( m_parent, this );
194
195 PostCreation();
196
197 Show( TRUE );
198
199 return TRUE;
200 }
201
202 int wxNotebook::GetSelection() const
203 {
204 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
205
206 if (m_pages.Number() == 0) return -1;
207
208 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
209 if (!g_page) return -1;
210
211 wxNotebookPage *page = (wxNotebookPage *) NULL;
212
213 wxNode *node = m_pages.First();
214 while (node)
215 {
216 page = (wxNotebookPage*)node->Data();
217
218 if ((page->m_page == g_page) || (page->m_page == (GtkNotebookPage*)NULL))
219 {
220 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
221 // "switch_page" then and we ask for GetSelection() in the handler for
222 // "switch_page". otherwise m_page should never be NULL. all this
223 // might also be wrong.
224 break;
225 }
226 node = node->Next();
227 }
228
229 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
230
231 return page->m_id;
232 }
233
234 int wxNotebook::GetPageCount() const
235 {
236 // count only the pages which were already added to the notebook for MSW
237 // compatibility (and, in fact, this behaviour makes more sense anyhow
238 // because only the added pages are shown)
239 int n = 0;
240 for ( wxNode *node = m_pages.First(); node; node = node->Next() )
241 {
242 wxNotebookPage *page = (wxNotebookPage*)node->Data();
243 if ( page->WasAdded() )
244 n++;
245 }
246
247 return n;
248 }
249
250 int wxNotebook::GetRowCount() const
251 {
252 return 1;
253 }
254
255 wxString wxNotebook::GetPageText( int page ) const
256 {
257 wxCHECK_MSG( m_widget != NULL, "", "invalid notebook" );
258
259 wxNotebookPage* nb_page = GetNotebookPage(page);
260 if (nb_page)
261 return nb_page->m_text;
262 else
263 return "";
264 }
265
266 int wxNotebook::GetPageImage( int page ) const
267 {
268 wxCHECK_MSG( m_widget != NULL, 0, "invalid notebook" );
269
270 wxNotebookPage* nb_page = GetNotebookPage(page);
271 if (nb_page)
272 return nb_page->m_image;
273 else
274 return 0;
275 }
276
277 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
278 {
279 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*)NULL, "invalid notebook" );
280
281 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
282
283 wxNode *node = m_pages.First();
284 while (node)
285 {
286 nb_page = (wxNotebookPage*)node->Data();
287 if (nb_page->m_id == page)
288 return nb_page;
289 node = node->Next();
290 }
291
292 wxLogDebug( "Notebook page %d not found!", page );
293
294 return (wxNotebookPage *) NULL;
295 }
296
297 int wxNotebook::SetSelection( int page )
298 {
299 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
300
301 int selOld = GetSelection();
302 wxNotebookPage* nb_page = GetNotebookPage(page);
303
304 if (!nb_page) return -1;
305
306 int page_num = 0;
307 GList *child = GTK_NOTEBOOK(m_widget)->children;
308 while (child)
309 {
310 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
311 page_num++;
312 child = child->next;
313 }
314
315 if (!child) return -1;
316
317 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
318
319 return selOld;
320 }
321
322 void wxNotebook::AdvanceSelection( bool bForward )
323 {
324 wxCHECK_RET( m_widget != NULL, "invalid notebook" );
325
326 int sel = GetSelection();
327 int max = GetPageCount();
328
329 if (bForward)
330 SetSelection( sel == max ? 0 : sel + 1 );
331 else
332 SetSelection( sel == 0 ? max : sel - 1 );
333 }
334
335 void wxNotebook::SetImageList( wxImageList* imageList )
336 {
337 m_imageList = imageList;
338 }
339
340 bool wxNotebook::SetPageText( int page, const wxString &text )
341 {
342 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
343
344 wxNotebookPage* nb_page = GetNotebookPage(page);
345
346 if (!nb_page) return FALSE;
347
348 nb_page->m_text = text;
349
350 return TRUE;
351 }
352
353 bool wxNotebook::SetPageImage( int page, int image )
354 {
355 wxNotebookPage* nb_page = GetNotebookPage(page);
356
357 if (!nb_page) return FALSE;
358
359 nb_page->m_image = image;
360
361 return TRUE;
362 }
363
364 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
365 {
366 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
367 }
368
369 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
370 {
371 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
372 }
373
374 bool wxNotebook::DeleteAllPages()
375 {
376 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
377
378 wxNode *page_node = m_pages.First();
379 while (page_node)
380 {
381 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
382
383 DeletePage( page->m_id );
384
385 page_node = m_pages.First();
386 }
387
388 return TRUE;
389 }
390
391 bool wxNotebook::DeletePage( int page )
392 {
393 wxNotebookPage* nb_page = GetNotebookPage(page);
394 if (!nb_page) return FALSE;
395
396 int page_num = 0;
397 GList *child = GTK_NOTEBOOK(m_widget)->children;
398 while (child)
399 {
400 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
401 page_num++;
402 child = child->next;
403 }
404
405 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
406
407 delete nb_page->m_client;
408
409 m_pages.DeleteObject( nb_page );
410
411 return TRUE;
412 }
413
414 bool wxNotebook::RemovePage( int page )
415 {
416 wxNotebookPage* nb_page = GetNotebookPage(page);
417 if (!nb_page) return FALSE;
418
419 int page_num = 0;
420 GList *child = GTK_NOTEBOOK(m_widget)->children;
421 while (child)
422 {
423 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
424 page_num++;
425 child = child->next;
426 }
427
428 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
429
430 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
431
432 m_pages.DeleteObject( nb_page );
433
434 return TRUE;
435 }
436
437 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
438 bool bSelect, int imageId)
439 {
440 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
441
442 // we've created the notebook page in AddChild(). Now we just have to set
443 // the caption for the page and set the others parameters.
444
445 wxNotebookPage *page = (wxNotebookPage *) NULL;
446
447 wxNode *node = m_pages.First();
448 while (node)
449 {
450 page = (wxNotebookPage*)node->Data();
451 if ( page->m_client == win ) break;
452 node = node->Next();
453 }
454
455 wxCHECK_MSG( page != NULL, FALSE,
456 "Can't add a page whose parent is not the notebook!" );
457
458 wxCHECK_MSG( page->Add(), FALSE,
459 "Can't add the same page twice to a notebook." );
460
461 if (imageId != -1)
462 {
463 wxASSERT( m_imageList != NULL );
464
465 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
466 GdkPixmap *pixmap = bmp->GetPixmap();
467 GdkBitmap *mask = (GdkBitmap*) NULL;
468 if ( bmp->GetMask() )
469 {
470 mask = bmp->GetMask()->GetBitmap();
471 }
472
473 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
474
475 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
476
477 gtk_widget_show(pixmapwid);
478 }
479
480 // then set the attributes
481 page->m_text = text;
482 if (page->m_text.IsEmpty()) page->m_text = "";
483 page->m_image = imageId;
484 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
485 gtk_box_pack_start( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
486
487 // @@@: what does this do? do we still need it?
488 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
489
490 gtk_widget_show((GtkWidget *)page->m_label);
491
492 if (bSelect) SetSelection(GetPageCount());
493
494 return TRUE;
495 }
496
497 wxWindow *wxNotebook::GetPage( int page ) const
498 {
499 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, "invalid notebook" );
500
501 wxNotebookPage* nb_page = GetNotebookPage(page);
502 if (!nb_page)
503 return (wxWindow *) NULL;
504 else
505 return nb_page->m_client;
506 }
507
508 // override these 2 functions to do nothing: everything is done in OnSize
509 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
510 {
511 // don't set the sizes of the pages - their correct size is not yet known
512 wxControl::SetConstraintSizes(FALSE);
513 }
514
515 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
516 {
517 return TRUE;
518 }
519
520 void wxNotebook::ApplyWidgetStyle()
521 {
522 SetWidgetStyle();
523 gtk_widget_set_style( m_widget, m_widgetStyle );
524 }
525
526 //-----------------------------------------------------------------------------
527 // wxNotebookEvent
528 //-----------------------------------------------------------------------------
529
530 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
531