]>
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; | |
34 | m_page = NULL; | |
35 | m_client = NULL; | |
36 | m_parent = NULL; | |
ff7b1510 | 37 | } |
219f895a RR |
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 | 72 | static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win ) |
caac5181 VZ |
73 | { |
74 | if ( win->GetAutoLayout() ) | |
75 | win->Layout(); | |
76 | ||
33d0b396 RR |
77 | if ((win->m_x == alloc->x) && |
78 | (win->m_y == alloc->y) && | |
79 | (win->m_width == alloc->width) && | |
80 | (win->m_height == alloc->height)) | |
81 | { | |
82 | return; | |
ff7b1510 | 83 | } |
caac5181 | 84 | |
33d0b396 RR |
85 | /* |
86 | printf( "OnResize from " ); | |
87 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
88 | printf( win->GetClassInfo()->GetClassName() ); | |
89 | printf( " .\n" ); | |
caac5181 | 90 | |
33d0b396 RR |
91 | printf( " Old: X: %d Y: %d ", win->m_x, win->m_y ); |
92 | printf( " W: %d H: %d ", win->m_width, win->m_height ); | |
93 | printf( " .\n" ); | |
caac5181 | 94 | |
33d0b396 RR |
95 | printf( " New: X: %d Y: %d ", alloc->x, alloc->y ); |
96 | printf( " W: %d H: %d ", alloc->width, alloc->height ); | |
97 | printf( " .\n" ); | |
98 | */ | |
caac5181 | 99 | |
33d0b396 | 100 | win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height ); |
caac5181 | 101 | |
33d0b396 RR |
102 | /* |
103 | printf( " Res: X: %d Y: %d ", win->m_x, win->m_y ); | |
104 | printf( " W: %d H: %d ", win->m_width, win->m_height ); | |
105 | printf( " .\n" ); | |
caac5181 | 106 | */ |
ff7b1510 | 107 | } |
caac5181 | 108 | |
53b28675 RR |
109 | //----------------------------------------------------------------------------- |
110 | // wxNotebook | |
111 | //----------------------------------------------------------------------------- | |
112 | ||
53b28675 RR |
113 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl) |
114 | ||
ff829f3f | 115 | void wxNotebook::Init() |
53b28675 RR |
116 | { |
117 | m_imageList = NULL; | |
53b28675 | 118 | m_pages.DeleteContents( TRUE ); |
ff829f3f VZ |
119 | m_idHandler = 0; |
120 | } | |
121 | ||
122 | wxNotebook::wxNotebook() | |
123 | { | |
124 | Init(); | |
ff7b1510 | 125 | } |
53b28675 | 126 | |
debe6624 | 127 | wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id, |
53b28675 | 128 | const wxPoint& pos, const wxSize& size, |
debe6624 | 129 | long style, const wxString& name ) |
53b28675 | 130 | { |
ff829f3f | 131 | Init(); |
53b28675 | 132 | Create( parent, id, pos, size, style, name ); |
ff7b1510 | 133 | } |
53b28675 | 134 | |
ff829f3f | 135 | wxNotebook::~wxNotebook() |
53b28675 | 136 | { |
ff829f3f VZ |
137 | // don't generate change page events any more |
138 | if ( m_idHandler != 0 ) | |
139 | gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler); | |
140 | ||
53b28675 | 141 | DeleteAllPages(); |
ff7b1510 | 142 | } |
53b28675 | 143 | |
debe6624 | 144 | bool wxNotebook::Create(wxWindow *parent, wxWindowID id, |
53b28675 | 145 | const wxPoint& pos, const wxSize& size, |
debe6624 | 146 | long style, const wxString& name ) |
53b28675 RR |
147 | { |
148 | m_needParent = TRUE; | |
ff829f3f | 149 | |
53b28675 RR |
150 | PreCreation( parent, id, pos, size, style, name ); |
151 | ||
152 | m_widget = gtk_notebook_new(); | |
caac5181 | 153 | |
716b7364 | 154 | gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 ); |
caac5181 | 155 | |
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 | 167 | return TRUE; |
ff7b1510 | 168 | } |
53b28675 | 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 | 185 | node = node->Next(); |
ff7b1510 | 186 | } |
53b28675 | 187 | |
b6af8d80 | 188 | wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" ); |
ff829f3f VZ |
189 | |
190 | return page->m_id; | |
ff7b1510 | 191 | } |
53b28675 | 192 | |
ff829f3f | 193 | int wxNotebook::GetPageCount() const |
53b28675 RR |
194 | { |
195 | return m_pages.Number(); | |
ff7b1510 | 196 | } |
53b28675 | 197 | |
ff829f3f | 198 | int wxNotebook::GetRowCount() const |
53b28675 RR |
199 | { |
200 | return 1; | |
ff7b1510 | 201 | } |
53b28675 | 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 ""; | |
ff7b1510 | 210 | } |
53b28675 | 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; |
ff7b1510 | 219 | } |
53b28675 | 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 | 231 | node = node->Next(); |
ff7b1510 | 232 | } |
ff829f3f | 233 | |
b6af8d80 | 234 | wxLogDebug( "Notebook page %d not found!", page ); |
ff829f3f VZ |
235 | |
236 | return NULL; | |
ff7b1510 | 237 | } |
53b28675 | 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; | |
ff7b1510 | 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; | |
ff7b1510 | 261 | } |
53b28675 | 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; | |
ff7b1510 | 279 | } |
53b28675 | 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 | 289 | return TRUE; |
ff7b1510 | 290 | } |
53b28675 | 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 | 300 | return TRUE; |
ff7b1510 | 301 | } |
53b28675 RR |
302 | |
303 | void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) ) | |
304 | { | |
1a5a8367 | 305 | wxFAIL_MSG(_("wxNotebook::SetPageSize not implemented")); |
ff7b1510 | 306 | } |
53b28675 RR |
307 | |
308 | void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) ) | |
309 | { | |
1a5a8367 | 310 | wxFAIL_MSG(_("wxNotebook::SetPadding not implemented")); |
ff7b1510 | 311 | } |
53b28675 | 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 | 322 | page_node = m_pages.First(); |
ff7b1510 | 323 | } |
ff829f3f | 324 | |
53b28675 | 325 | return TRUE; |
ff7b1510 | 326 | } |
53b28675 | 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; | |
ff7b1510 | 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 | 351 | return TRUE; |
ff7b1510 | 352 | } |
53b28675 | 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(); | |
ff7b1510 | 370 | } |
8aadf227 | 371 | |
ff829f3f | 372 | wxCHECK_MSG(page != NULL, FALSE, |
1a5a8367 | 373 | _("Can't add a page whose parent is not the notebook!")); |
ff829f3f | 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; |
ff7b1510 | 387 | } |
53b28675 | 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; |
ff7b1510 | 396 | } |
53b28675 RR |
397 | |
398 | void wxNotebook::AddChild( wxWindow *win ) | |
399 | { | |
4e6322e0 VZ |
400 | m_children.Append(win); |
401 | ||
4bf58c62 VZ |
402 | wxNotebookPage *page = new wxNotebookPage(); |
403 | ||
404 | page->m_id = GetPageCount(); | |
1a5a8367 | 405 | page->m_label = (GtkLabel *)gtk_label_new(_("Handle")); |
219f895a RR |
406 | page->m_client = win; |
407 | gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget, | |
408 | (GtkWidget *)page->m_label); | |
4bf58c62 | 409 | gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); |
ff829f3f | 410 | |
5a8c929e | 411 | page->m_page = |
219f895a | 412 | (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data); |
5a8c929e | 413 | |
219f895a | 414 | page->m_parent = GTK_NOTEBOOK(m_widget); |
caac5181 | 415 | |
33d0b396 RR |
416 | gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate", |
417 | GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win ); | |
caac5181 | 418 | |
4bf58c62 VZ |
419 | if (!page->m_page) |
420 | { | |
1a5a8367 | 421 | wxLogFatalError( _("Notebook page creation error") ); |
4bf58c62 VZ |
422 | return; |
423 | } | |
424 | ||
425 | m_pages.Append( page ); | |
ff7b1510 | 426 | } |
53b28675 | 427 | |
5a8c929e | 428 | // override these 2 functions to do nothing: everything is done in OnSize |
e3e65dac | 429 | void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) ) |
5a8c929e VZ |
430 | { |
431 | // don't set the sizes of the pages - their correct size is not yet known | |
432 | wxControl::SetConstraintSizes(FALSE); | |
433 | } | |
434 | ||
e3e65dac | 435 | bool wxNotebook::DoPhase( int WXUNUSED(nPhase) ) |
5a8c929e VZ |
436 | { |
437 | return TRUE; | |
438 | } | |
439 | ||
53b28675 | 440 | //----------------------------------------------------------------------------- |
ff829f3f | 441 | // wxNotebookEvent |
53b28675 RR |
442 | //----------------------------------------------------------------------------- |
443 | ||
ff829f3f | 444 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) |