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