]>
Commit | Line | Data |
---|---|---|
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 | wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" ); | |
374 | ||
375 | delete nb_page->m_client; | |
376 | ||
377 | m_pages.DeleteObject( nb_page ); | |
378 | ||
379 | return TRUE; | |
380 | } | |
381 | ||
382 | bool wxNotebook::RemovePage( int page ) | |
383 | { | |
384 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
385 | if (!nb_page) return FALSE; | |
386 | ||
387 | int page_num = 0; | |
388 | GList *child = GTK_NOTEBOOK(m_widget)->children; | |
389 | while (child) | |
390 | { | |
391 | if (nb_page->m_page == (GtkNotebookPage*)child->data) break; | |
392 | page_num++; | |
393 | child = child->next; | |
394 | } | |
395 | ||
396 | wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" ); | |
397 | ||
398 | gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num ); | |
399 | ||
400 | m_pages.DeleteObject( nb_page ); | |
401 | ||
402 | return TRUE; | |
403 | } | |
404 | ||
405 | bool wxNotebook::AddPage(wxWindow* win, const wxString& text, | |
406 | bool bSelect, int imageId) | |
407 | { | |
408 | wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" ); | |
409 | ||
410 | // we've created the notebook page in AddChild(). Now we just have to set | |
411 | // the caption for the page and set the others parameters. | |
412 | ||
413 | wxNotebookPage *page = (wxNotebookPage *) NULL; | |
414 | ||
415 | wxNode *node = m_pages.First(); | |
416 | while (node) | |
417 | { | |
418 | page = (wxNotebookPage*)node->Data(); | |
419 | if ( page->m_client == win ) break; | |
420 | node = node->Next(); | |
421 | } | |
422 | ||
423 | wxCHECK_MSG( page != NULL, FALSE, "Can't add a page whose parent is not the notebook!" ); | |
424 | ||
425 | if (imageId != -1) | |
426 | { | |
427 | wxASSERT( m_imageList != NULL ); | |
428 | ||
429 | const wxBitmap *bmp = m_imageList->GetBitmap(imageId); | |
430 | GdkPixmap *pixmap = bmp->GetPixmap(); | |
431 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
432 | if ( bmp->GetMask() ) | |
433 | { | |
434 | mask = bmp->GetMask()->GetBitmap(); | |
435 | } | |
436 | ||
437 | GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask ); | |
438 | ||
439 | gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3); | |
440 | ||
441 | gtk_widget_show(pixmapwid); | |
442 | } | |
443 | ||
444 | // then set the attributes | |
445 | page->m_text = text; | |
446 | if (page->m_text.IsEmpty()) page->m_text = ""; | |
447 | page->m_image = imageId; | |
448 | page->m_label = (GtkLabel *)gtk_label_new(page->m_text); | |
449 | gtk_box_pack_start( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3); | |
450 | ||
451 | // @@@: what does this do? do we still need it? | |
452 | // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); | |
453 | ||
454 | gtk_widget_show((GtkWidget *)page->m_label); | |
455 | ||
456 | if (bSelect) SetSelection(GetPageCount()); | |
457 | ||
458 | return TRUE; | |
459 | } | |
460 | ||
461 | wxWindow *wxNotebook::GetPage( int page ) const | |
462 | { | |
463 | wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, "invalid notebook" ); | |
464 | ||
465 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
466 | if (!nb_page) | |
467 | return (wxWindow *) NULL; | |
468 | else | |
469 | return nb_page->m_client; | |
470 | } | |
471 | ||
472 | // override these 2 functions to do nothing: everything is done in OnSize | |
473 | void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) ) | |
474 | { | |
475 | // don't set the sizes of the pages - their correct size is not yet known | |
476 | wxControl::SetConstraintSizes(FALSE); | |
477 | } | |
478 | ||
479 | bool wxNotebook::DoPhase( int WXUNUSED(nPhase) ) | |
480 | { | |
481 | return TRUE; | |
482 | } | |
483 | ||
484 | void wxNotebook::ApplyWidgetStyle() | |
485 | { | |
486 | SetWidgetStyle(); | |
487 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
488 | } | |
489 | ||
490 | //----------------------------------------------------------------------------- | |
491 | // wxNotebookEvent | |
492 | //----------------------------------------------------------------------------- | |
493 | ||
494 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) | |
495 |