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