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