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