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