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