image in the notebook is now at the left of the label (as in wxMSW), not at
[wxWidgets.git] / src / gtk / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: notebook.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "notebook.h"
13 #endif
14
15 #include "wx/notebook.h"
16 #include "wx/panel.h"
17 #include "wx/utils.h"
18 #include "wx/imaglist.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21
22 //-----------------------------------------------------------------------------
23 // wxNotebookPage
24 //-----------------------------------------------------------------------------
25
26 class wxNotebookPage: public wxObject
27 {
28 public:
29 wxNotebookPage()
30 {
31 m_id = -1;
32 m_text = "";
33 m_image = -1;
34 m_page = (GtkNotebookPage *) NULL;
35 m_client = (wxWindow *) NULL;
36 m_parent = (GtkNotebook *) NULL;
37 m_box = (GtkWidget *) NULL;
38 }
39
40 //private:
41 int m_id;
42 wxString m_text;
43 int m_image;
44 GtkNotebookPage *m_page;
45 GtkLabel *m_label;
46 wxWindow *m_client;
47 GtkNotebook *m_parent;
48 GtkWidget *m_box; // in which the label and image are packed
49 };
50
51 //-----------------------------------------------------------------------------
52 // GTK callbacks
53 //-----------------------------------------------------------------------------
54
55 // page change callback
56 static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
57 GtkNotebookPage *WXUNUSED(page),
58 gint nPage,
59 gpointer data)
60 {
61 wxNotebook *notebook = (wxNotebook *)data;
62
63 int nOld = notebook->GetSelection();
64
65 // TODO: emulate PAGE_CHANGING event
66 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
67 notebook->GetId(),
68 nPage,
69 nOld);
70 event.SetEventObject(notebook);
71 notebook->ProcessEvent(event);
72 }
73
74 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
75 {
76 if ( win->GetAutoLayout() )
77 win->Layout();
78
79 if ((win->m_x == alloc->x) &&
80 (win->m_y == alloc->y) &&
81 (win->m_width == alloc->width) &&
82 (win->m_height == alloc->height))
83 {
84 return;
85 }
86
87 /*
88 printf( "OnResize from " );
89 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
90 printf( win->GetClassInfo()->GetClassName() );
91 printf( " .\n" );
92
93 printf( " Old: X: %d Y: %d ", win->m_x, win->m_y );
94 printf( " W: %d H: %d ", win->m_width, win->m_height );
95 printf( " .\n" );
96
97 printf( " New: X: %d Y: %d ", alloc->x, alloc->y );
98 printf( " W: %d H: %d ", alloc->width, alloc->height );
99 printf( " .\n" );
100 */
101
102 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
103
104 /*
105 printf( " Res: X: %d Y: %d ", win->m_x, win->m_y );
106 printf( " W: %d H: %d ", win->m_width, win->m_height );
107 printf( " .\n" );
108 */
109 }
110
111 //-----------------------------------------------------------------------------
112 // wxNotebook
113 //-----------------------------------------------------------------------------
114
115 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
116
117 void wxNotebook::Init()
118 {
119 m_imageList = (wxImageList *) NULL;
120 m_pages.DeleteContents( TRUE );
121 m_idHandler = 0;
122 }
123
124 wxNotebook::wxNotebook()
125 {
126 Init();
127 }
128
129 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
130 const wxPoint& pos, const wxSize& size,
131 long style, const wxString& name )
132 {
133 Init();
134 Create( parent, id, pos, size, style, name );
135 }
136
137 wxNotebook::~wxNotebook()
138 {
139 // don't generate change page events any more
140 if ( m_idHandler != 0 )
141 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
142
143 DeleteAllPages();
144 }
145
146 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
147 const wxPoint& pos, const wxSize& size,
148 long style, const wxString& name )
149 {
150 m_needParent = TRUE;
151
152 PreCreation( parent, id, pos, size, style, name );
153
154 m_widget = gtk_notebook_new();
155
156 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
157
158 m_idHandler = gtk_signal_connect
159 (
160 GTK_OBJECT(m_widget), "switch_page",
161 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
162 (gpointer)this
163 );
164
165 PostCreation();
166
167 Show( TRUE );
168
169 return TRUE;
170 }
171
172 int wxNotebook::GetSelection() const
173 {
174 if (m_pages.Number() == 0)
175 return -1;
176
177 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
178
179 wxNotebookPage *page = (wxNotebookPage *) NULL;
180
181 wxNode *node = m_pages.First();
182 while (node)
183 {
184 page = (wxNotebookPage*)node->Data();
185 if (page->m_page == g_page)
186 break;
187 node = node->Next();
188 }
189
190 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
191
192 return page->m_id;
193 }
194
195 int wxNotebook::GetPageCount() const
196 {
197 return m_pages.Number();
198 }
199
200 int wxNotebook::GetRowCount() const
201 {
202 return 1;
203 }
204
205 wxString wxNotebook::GetPageText( int page ) const
206 {
207 wxNotebookPage* nb_page = GetNotebookPage(page);
208 if (nb_page)
209 return nb_page->m_text;
210 else
211 return "";
212 }
213
214 int wxNotebook::GetPageImage( int page ) const
215 {
216 wxNotebookPage* nb_page = GetNotebookPage(page);
217 if (nb_page)
218 return nb_page->m_image;
219 else
220 return 0;
221 }
222
223 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
224 {
225 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
226
227 wxNode *node = m_pages.First();
228 while (node)
229 {
230 nb_page = (wxNotebookPage*)node->Data();
231 if (nb_page->m_id == page)
232 return nb_page;
233 node = node->Next();
234 }
235
236 wxLogDebug( "Notebook page %d not found!", page );
237
238 return (wxNotebookPage *) NULL;
239 }
240
241 int wxNotebook::SetSelection( int page )
242 {
243 int selOld = GetSelection();
244 wxNotebookPage* nb_page = GetNotebookPage(page);
245 if (!nb_page)
246 return -1;
247
248 int page_num = 0;
249 GList *child = GTK_NOTEBOOK(m_widget)->children;
250 while (child)
251 {
252 if (nb_page->m_page == (GtkNotebookPage*)child->data)
253 break;
254 page_num++;
255 child = child->next;
256 }
257
258 if (!child) return -1;
259
260 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
261
262 return selOld;
263 }
264
265 void wxNotebook::AdvanceSelection(bool bForward)
266 {
267 int nSel = GetSelection(),
268 nMax = GetPageCount();
269
270 if ( bForward ) {
271 SetSelection(nSel == nMax ? 0 : nSel + 1);
272 }
273 else {
274 SetSelection(nSel == 0 ? nMax : nSel - 1);
275 }
276 }
277
278 void wxNotebook::SetImageList( wxImageList* imageList )
279 {
280 m_imageList = imageList;
281 }
282
283 bool wxNotebook::SetPageText( int page, const wxString &text )
284 {
285 wxNotebookPage* nb_page = GetNotebookPage(page);
286 if (!nb_page)
287 return FALSE;
288
289 nb_page->m_text = text;
290
291 return TRUE;
292 }
293
294 bool wxNotebook::SetPageImage( int page, int image )
295 {
296 wxNotebookPage* nb_page = GetNotebookPage(page);
297 if (!nb_page)
298 return FALSE;
299
300 nb_page->m_image = image;
301
302 return TRUE;
303 }
304
305 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
306 {
307 wxFAIL_MSG(_("wxNotebook::SetPageSize not implemented"));
308 }
309
310 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
311 {
312 wxFAIL_MSG(_("wxNotebook::SetPadding not implemented"));
313 }
314
315 bool wxNotebook::DeleteAllPages()
316 {
317 wxNode *page_node = m_pages.First();
318 while (page_node)
319 {
320 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
321
322 DeletePage( page->m_id );
323
324 page_node = m_pages.First();
325 }
326
327 return TRUE;
328 }
329
330 bool wxNotebook::DeletePage( int page )
331 {
332 wxNotebookPage* nb_page = GetNotebookPage(page);
333 if (!nb_page) return FALSE;
334
335 int page_num = 0;
336 GList *child = GTK_NOTEBOOK(m_widget)->children;
337 while (child)
338 {
339 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
340 page_num++;
341 child = child->next;
342 }
343
344 wxASSERT( child );
345
346 delete nb_page->m_client;
347
348 // Amazingly, this is not necessary
349 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
350
351 m_pages.DeleteObject( nb_page );
352
353 return TRUE;
354 }
355
356 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
357 bool bSelect, int imageId)
358 {
359 // we've created the notebook page in AddChild(). Now we just have to set
360 // the caption for the page and set the others parameters.
361
362 // first, find the page
363 wxNotebookPage *page = (wxNotebookPage *) NULL;
364
365 wxNode *node = m_pages.First();
366 while (node)
367 {
368 page = (wxNotebookPage*)node->Data();
369 if ( page->m_client == win )
370 break; // found
371 node = node->Next();
372 }
373
374 wxCHECK_MSG(page != NULL, FALSE,
375 "Can't add a page whose parent is not the notebook!");
376
377 // create the image if any
378 if ( imageId != -1 ) {
379 wxASSERT( m_imageList != NULL );
380
381 wxBitmap *bmp = m_imageList->GetBitmap(imageId);
382 GdkPixmap *pixmap = bmp->GetPixmap();
383 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, NULL /* @@@@ */);
384
385 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
386
387 gtk_widget_show(pixmapwid);
388 }
389
390 // then set the attributes
391 page->m_text = text;
392 if ( page->m_text.IsEmpty() )
393 page->m_text = "";
394 page->m_image = imageId;
395 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
396 gtk_box_pack_start(GTK_BOX(page->m_box), (GtkWidget *)page->m_label,
397 FALSE, FALSE, 3);
398
399 // @@@: what does this do? do we still need it?
400 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
401
402 gtk_widget_show((GtkWidget *)page->m_label);
403
404 if ( bSelect ) {
405 SetSelection(GetPageCount());
406 }
407
408 return TRUE;
409 }
410
411 wxWindow *wxNotebook::GetPage( int page ) const
412 {
413 wxNotebookPage* nb_page = GetNotebookPage(page);
414 if (!nb_page)
415 return (wxWindow *) NULL;
416 else
417 return nb_page->m_client;
418 }
419
420 void wxNotebook::AddChild( wxWindow *win )
421 {
422 m_children.Append(win);
423
424 wxNotebookPage *page = new wxNotebookPage();
425
426 page->m_id = GetPageCount();
427
428 page->m_box = gtk_hbox_new (FALSE, 0);
429 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
430
431 page->m_client = win;
432 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget,
433 page->m_box );
434
435 page->m_page =
436 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
437
438 page->m_parent = GTK_NOTEBOOK(m_widget);
439
440 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
441 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
442
443 if (!page->m_page)
444 {
445 wxLogFatalError( _("Notebook page creation error") );
446 return;
447 }
448
449 m_pages.Append( page );
450 }
451
452 // override these 2 functions to do nothing: everything is done in OnSize
453 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
454 {
455 // don't set the sizes of the pages - their correct size is not yet known
456 wxControl::SetConstraintSizes(FALSE);
457 }
458
459 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
460 {
461 return TRUE;
462 }
463
464 //-----------------------------------------------------------------------------
465 // wxNotebookEvent
466 //-----------------------------------------------------------------------------
467
468 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)