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