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