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