]>
Commit | Line | Data |
---|---|---|
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 | |
8 | // Licence: wxWindows licence | |
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" | |
19 | #include "wx/intl.h" | |
20 | #include "wx/log.h" | |
21 | ||
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 | ||
45 | //----------------------------------------------------------------------------- | |
46 | // wxNotebookPage | |
47 | //----------------------------------------------------------------------------- | |
48 | ||
49 | class wxNotebookPage: public wxObject | |
50 | { | |
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; | |
68 | }; | |
69 | ||
70 | //----------------------------------------------------------------------------- | |
71 | // wxNotebook | |
72 | //----------------------------------------------------------------------------- | |
73 | ||
74 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) | |
75 | EVT_SIZE(wxNotebook::OnSize) | |
76 | END_EVENT_TABLE() | |
77 | ||
78 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl) | |
79 | ||
80 | void wxNotebook::Init() | |
81 | { | |
82 | m_imageList = NULL; | |
83 | m_pages.DeleteContents( TRUE ); | |
84 | m_idHandler = 0; | |
85 | } | |
86 | ||
87 | wxNotebook::wxNotebook() | |
88 | { | |
89 | Init(); | |
90 | }; | |
91 | ||
92 | wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id, | |
93 | const wxPoint& pos, const wxSize& size, | |
94 | long style, const wxString& name ) | |
95 | { | |
96 | Init(); | |
97 | Create( parent, id, pos, size, style, name ); | |
98 | }; | |
99 | ||
100 | wxNotebook::~wxNotebook() | |
101 | { | |
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; | |
108 | DeleteAllPages(); | |
109 | }; | |
110 | ||
111 | bool wxNotebook::Create(wxWindow *parent, wxWindowID id, | |
112 | const wxPoint& pos, const wxSize& size, | |
113 | long style, const wxString& name ) | |
114 | { | |
115 | m_needParent = TRUE; | |
116 | ||
117 | PreCreation( parent, id, pos, size, style, name ); | |
118 | ||
119 | m_widget = gtk_notebook_new(); | |
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 | ||
127 | PostCreation(); | |
128 | ||
129 | Show( TRUE ); | |
130 | ||
131 | return TRUE; | |
132 | }; | |
133 | ||
134 | int wxNotebook::GetSelection() const | |
135 | { | |
136 | if (m_pages.Number() == 0) | |
137 | return -1; | |
138 | ||
139 | GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page; | |
140 | ||
141 | wxNotebookPage *page = NULL; | |
142 | ||
143 | wxNode *node = m_pages.First(); | |
144 | while (node) | |
145 | { | |
146 | page = (wxNotebookPage*)node->Data(); | |
147 | if (page->m_page == g_page) | |
148 | break; | |
149 | node = node->Next(); | |
150 | }; | |
151 | ||
152 | wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?"); | |
153 | ||
154 | return page->m_id; | |
155 | }; | |
156 | ||
157 | int wxNotebook::GetPageCount() const | |
158 | { | |
159 | return m_pages.Number(); | |
160 | }; | |
161 | ||
162 | int wxNotebook::GetRowCount() const | |
163 | { | |
164 | return 1; | |
165 | }; | |
166 | ||
167 | wxString wxNotebook::GetPageText( int page ) const | |
168 | { | |
169 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
170 | if (nb_page) | |
171 | return nb_page->m_text; | |
172 | else | |
173 | return ""; | |
174 | }; | |
175 | ||
176 | int wxNotebook::GetPageImage( int page ) const | |
177 | { | |
178 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
179 | if (nb_page) | |
180 | return nb_page->m_image; | |
181 | else | |
182 | return 0; | |
183 | }; | |
184 | ||
185 | wxNotebookPage* wxNotebook::GetNotebookPage(int page) const | |
186 | { | |
187 | wxNotebookPage *nb_page = NULL; | |
188 | ||
189 | wxNode *node = m_pages.First(); | |
190 | while (node) | |
191 | { | |
192 | nb_page = (wxNotebookPage*)node->Data(); | |
193 | if (nb_page->m_id == page) | |
194 | return nb_page; | |
195 | node = node->Next(); | |
196 | }; | |
197 | ||
198 | wxLogDebug("Notebook page %d not found!", page); | |
199 | ||
200 | return NULL; | |
201 | }; | |
202 | ||
203 | int wxNotebook::SetSelection( int page ) | |
204 | { | |
205 | int selOld = GetSelection(); | |
206 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
207 | if (!nb_page) | |
208 | return -1; | |
209 | ||
210 | int page_num = 0; | |
211 | GList *child = GTK_NOTEBOOK(m_widget)->children; | |
212 | while (child) | |
213 | { | |
214 | if (nb_page->m_page == (GtkNotebookPage*)child->data) | |
215 | break; | |
216 | page_num++; | |
217 | child = child->next; | |
218 | }; | |
219 | ||
220 | if (!child) return -1; | |
221 | ||
222 | gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num ); | |
223 | ||
224 | return selOld; | |
225 | }; | |
226 | ||
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 | ||
240 | void wxNotebook::SetImageList( wxImageList* imageList ) | |
241 | { | |
242 | m_imageList = imageList; | |
243 | }; | |
244 | ||
245 | bool wxNotebook::SetPageText( int page, const wxString &text ) | |
246 | { | |
247 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
248 | if (!nb_page) | |
249 | return FALSE; | |
250 | ||
251 | nb_page->m_text = text; | |
252 | ||
253 | return TRUE; | |
254 | }; | |
255 | ||
256 | bool wxNotebook::SetPageImage( int page, int image ) | |
257 | { | |
258 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
259 | if (!nb_page) | |
260 | return FALSE; | |
261 | ||
262 | nb_page->m_image = image; | |
263 | ||
264 | return TRUE; | |
265 | }; | |
266 | ||
267 | void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) ) | |
268 | { | |
269 | wxFAIL_MSG("wxNotebook::SetPageSize not implemented"); | |
270 | }; | |
271 | ||
272 | void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) ) | |
273 | { | |
274 | wxFAIL_MSG("wxNotebook::SetPadding not implemented"); | |
275 | }; | |
276 | ||
277 | bool wxNotebook::DeleteAllPages() | |
278 | { | |
279 | wxNode *page_node = m_pages.First(); | |
280 | while (page_node) | |
281 | { | |
282 | wxNotebookPage *page = (wxNotebookPage*)page_node->Data(); | |
283 | ||
284 | DeletePage( page->m_id ); | |
285 | ||
286 | page_node = m_pages.First(); | |
287 | }; | |
288 | ||
289 | return TRUE; | |
290 | }; | |
291 | ||
292 | bool wxNotebook::DeletePage( int page ) | |
293 | { | |
294 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
295 | if (!nb_page) return FALSE; | |
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 | }; | |
305 | ||
306 | wxASSERT( child ); | |
307 | ||
308 | delete nb_page->m_clientPanel; | |
309 | ||
310 | // Amazingly, this is not necessary | |
311 | // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num ); | |
312 | ||
313 | m_pages.DeleteObject( nb_page ); | |
314 | ||
315 | return TRUE; | |
316 | }; | |
317 | ||
318 | bool wxNotebook::AddPage(wxWindow* win, const wxString& text, | |
319 | bool bSelect, int imageId) | |
320 | { | |
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. | |
323 | ||
324 | // first, find the page | |
325 | wxNotebookPage *page = NULL; | |
326 | ||
327 | wxNode *node = m_pages.First(); | |
328 | while (node) | |
329 | { | |
330 | page = (wxNotebookPage*)node->Data(); | |
331 | if ( page->m_clientPanel == win ) | |
332 | break; // found | |
333 | node = node->Next(); | |
334 | }; | |
335 | ||
336 | wxCHECK_MSG(page != NULL, FALSE, | |
337 | "Can't add a page whose parent is not the notebook!"); | |
338 | ||
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; | |
344 | gtk_label_set(page->m_label, page->m_text); | |
345 | ||
346 | if ( bSelect ) { | |
347 | SetSelection(GetPageCount()); | |
348 | } | |
349 | ||
350 | return TRUE; | |
351 | }; | |
352 | ||
353 | wxWindow *wxNotebook::GetPage( int page ) const | |
354 | { | |
355 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
356 | if (!nb_page) | |
357 | return NULL; | |
358 | else | |
359 | return nb_page->m_clientPanel; | |
360 | }; | |
361 | ||
362 | void wxNotebook::AddChild( wxWindow *win ) | |
363 | { | |
364 | // @@@ normally done in wxWindow::AddChild but for some reason wxNotebook | |
365 | // case is special there (Robert?) | |
366 | m_children.Append(win); | |
367 | ||
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; | |
373 | gtk_notebook_append_page(GTK_NOTEBOOK(m_widget), win->m_widget, | |
374 | (GtkWidget *)page->m_label); | |
375 | gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); | |
376 | ||
377 | page->m_page = (GtkNotebookPage*) | |
378 | ( | |
379 | g_list_last(GTK_NOTEBOOK(m_widget)->children)->data | |
380 | ); | |
381 | ||
382 | if (!page->m_page) | |
383 | { | |
384 | wxLogFatalError( "Notebook page creation error" ); | |
385 | return; | |
386 | } | |
387 | ||
388 | m_pages.Append( page ); | |
389 | }; | |
390 | ||
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(); | |
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); | |
402 | ||
403 | node = node->Next(); | |
404 | }; | |
405 | } | |
406 | ||
407 | //----------------------------------------------------------------------------- | |
408 | // wxNotebookEvent | |
409 | //----------------------------------------------------------------------------- | |
410 | ||
411 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent) |