]>
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 | |
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" | |
4bf58c62 VZ |
19 | #include "wx/intl.h" |
20 | #include "wx/log.h" | |
53b28675 RR |
21 | |
22 | //----------------------------------------------------------------------------- | |
23 | // wxNotebookPage | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | class wxNotebookPage: public wxObject | |
27 | { | |
28 | public: | |
29 | ||
30 | int m_id; | |
31 | wxString m_text; | |
32 | int m_image; | |
33 | void *m_clientData; | |
34 | GtkNotebookPage *m_page; | |
4bf58c62 | 35 | GtkLabel *m_label; |
8aadf227 | 36 | wxWindow *m_clientPanel; |
53b28675 | 37 | |
4bf58c62 | 38 | wxNotebookPage() |
53b28675 RR |
39 | { |
40 | m_id = -1; | |
41 | m_text = ""; | |
42 | m_image = -1; | |
43 | m_clientData = NULL; | |
44 | m_page = NULL; | |
45 | m_clientPanel = NULL; | |
46 | }; | |
47 | ||
48 | }; | |
49 | ||
50 | //----------------------------------------------------------------------------- | |
51 | // wxNotebook | |
52 | //----------------------------------------------------------------------------- | |
53 | ||
54 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) | |
55 | END_EVENT_TABLE() | |
56 | ||
57 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl) | |
58 | ||
59 | wxNotebook::wxNotebook(void) | |
60 | { | |
61 | m_imageList = NULL; | |
53b28675 RR |
62 | m_pages.DeleteContents( TRUE ); |
63 | }; | |
64 | ||
65 | wxNotebook::wxNotebook( wxWindow *parent, const wxWindowID id, | |
66 | const wxPoint& pos, const wxSize& size, | |
67 | const long style, const wxString& name ) | |
68 | { | |
69 | m_imageList = NULL; | |
53b28675 RR |
70 | m_pages.DeleteContents( TRUE ); |
71 | Create( parent, id, pos, size, style, name ); | |
72 | }; | |
73 | ||
74 | wxNotebook::~wxNotebook(void) | |
75 | { | |
76 | if (m_imageList) delete m_imageList; | |
77 | DeleteAllPages(); | |
78 | }; | |
79 | ||
80 | bool wxNotebook::Create(wxWindow *parent, const wxWindowID id, | |
81 | const wxPoint& pos, const wxSize& size, | |
82 | const long style, const wxString& name ) | |
83 | { | |
84 | m_needParent = TRUE; | |
85 | ||
86 | PreCreation( parent, id, pos, size, style, name ); | |
87 | ||
88 | m_widget = gtk_notebook_new(); | |
89 | ||
90 | PostCreation(); | |
91 | ||
92 | Show( TRUE ); | |
93 | ||
94 | return TRUE; | |
95 | }; | |
96 | ||
97 | int wxNotebook::GetSelection(void) const | |
98 | { | |
99 | if (m_pages.Number() == 0) return -1; | |
100 | ||
101 | GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page; | |
102 | ||
103 | wxNotebookPage *page = NULL; | |
104 | ||
105 | wxNode *node = m_pages.First(); | |
106 | while (node) | |
107 | { | |
108 | page = (wxNotebookPage*)node->Data(); | |
109 | if (page->m_page == g_page) break; | |
110 | node = node->Next(); | |
111 | }; | |
112 | ||
113 | if (!node) wxFatalError( "Notebook error." ); | |
114 | ||
115 | return page->m_id; | |
116 | }; | |
117 | ||
118 | wxImageList* wxNotebook::GetImageList(void) const | |
119 | { | |
120 | return m_imageList; | |
121 | }; | |
122 | ||
123 | int wxNotebook::GetPageCount(void) const | |
124 | { | |
125 | return m_pages.Number(); | |
126 | }; | |
127 | ||
128 | int wxNotebook::GetRowCount(void) const | |
129 | { | |
130 | return 1; | |
131 | }; | |
132 | ||
133 | wxString wxNotebook::GetPageText( const int page ) const | |
134 | { | |
8aadf227 JS |
135 | wxNotebookPage* nb_page = GetNotebookPage(page); |
136 | if (nb_page) | |
137 | return nb_page->m_text; | |
138 | else | |
139 | return ""; | |
53b28675 RR |
140 | }; |
141 | ||
142 | int wxNotebook::GetPageImage( const int page ) const | |
143 | { | |
8aadf227 JS |
144 | wxNotebookPage* nb_page = GetNotebookPage(page); |
145 | if (nb_page) | |
146 | return nb_page->m_image; | |
147 | else | |
4bf58c62 | 148 | return 0; |
53b28675 RR |
149 | }; |
150 | ||
151 | void* wxNotebook::GetPageData( const int page ) const | |
8aadf227 JS |
152 | { |
153 | wxNotebookPage* nb_page = GetNotebookPage(page); | |
154 | if (nb_page) | |
155 | return nb_page->m_clientData; | |
156 | else | |
157 | return NULL; | |
158 | }; | |
159 | ||
160 | wxNotebookPage* wxNotebook::GetNotebookPage(int page) const | |
53b28675 RR |
161 | { |
162 | wxNotebookPage *nb_page = NULL; | |
163 | ||
164 | wxNode *node = m_pages.First(); | |
165 | while (node) | |
166 | { | |
167 | nb_page = (wxNotebookPage*)node->Data(); | |
168 | if (nb_page->m_id == page) break; | |
169 | node = node->Next(); | |
170 | }; | |
171 | if (!node) return NULL; | |
8aadf227 | 172 | return nb_page; |
53b28675 RR |
173 | }; |
174 | ||
175 | int wxNotebook::SetSelection( const int page ) | |
176 | { | |
8aadf227 JS |
177 | wxNotebookPage* nb_page = GetNotebookPage(page); |
178 | if (!nb_page) return -1; | |
53b28675 RR |
179 | |
180 | int page_num = 0; | |
181 | GList *child = GTK_NOTEBOOK(m_widget)->children; | |
182 | while (child) | |
183 | { | |
184 | if (nb_page->m_page == (GtkNotebookPage*)child->data) break; | |
185 | page_num++; | |
186 | child = child->next; | |
187 | }; | |
188 | ||
189 | if (!child) return -1; | |
190 | ||
191 | gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num ); | |
192 | ||
193 | return page; | |
194 | }; | |
195 | ||
196 | void wxNotebook::SetImageList( wxImageList* imageList ) | |
197 | { | |
198 | m_imageList = imageList; | |
199 | }; | |
200 | ||
201 | bool wxNotebook::SetPageText( const int page, const wxString &text ) | |
202 | { | |
8aadf227 JS |
203 | wxNotebookPage* nb_page = GetNotebookPage(page); |
204 | if (!nb_page) return FALSE; | |
53b28675 RR |
205 | |
206 | nb_page->m_text = text; | |
207 | ||
208 | // recreate | |
209 | ||
210 | return TRUE; | |
211 | }; | |
212 | ||
213 | bool wxNotebook::SetPageImage( const int page, const int image ) | |
214 | { | |
8aadf227 JS |
215 | wxNotebookPage* nb_page = GetNotebookPage(page); |
216 | if (!nb_page) | |
217 | return FALSE; | |
53b28675 RR |
218 | |
219 | nb_page->m_image = image; | |
220 | ||
221 | // recreate | |
222 | ||
223 | return TRUE; | |
224 | }; | |
225 | ||
226 | bool wxNotebook::SetPageData( const int page, void* data ) | |
227 | { | |
8aadf227 JS |
228 | wxNotebookPage* nb_page = GetNotebookPage(page); |
229 | if (!nb_page) return FALSE; | |
53b28675 | 230 | |
53b28675 RR |
231 | nb_page->m_clientData = data; |
232 | ||
233 | return TRUE; | |
234 | }; | |
235 | ||
236 | void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) ) | |
237 | { | |
238 | }; | |
239 | ||
240 | void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) ) | |
241 | { | |
242 | // what's this ? | |
243 | }; | |
244 | ||
245 | bool wxNotebook::DeleteAllPages(void) | |
246 | { | |
247 | wxNode *page_node = m_pages.First(); | |
248 | while (page_node) | |
249 | { | |
250 | wxNotebookPage *page = (wxNotebookPage*)page_node->Data(); | |
251 | ||
252 | DeletePage( page->m_id ); | |
253 | ||
254 | page_node = m_pages.First(); | |
255 | }; | |
256 | ||
257 | return TRUE; | |
258 | }; | |
259 | ||
260 | bool wxNotebook::DeletePage( const int page ) | |
261 | { | |
8aadf227 JS |
262 | wxNotebookPage* nb_page = GetNotebookPage(page); |
263 | if (!nb_page) return FALSE; | |
53b28675 RR |
264 | |
265 | int page_num = 0; | |
266 | GList *child = GTK_NOTEBOOK(m_widget)->children; | |
267 | while (child) | |
268 | { | |
269 | if (nb_page->m_page == (GtkNotebookPage*)child->data) break; | |
270 | page_num++; | |
271 | child = child->next; | |
272 | }; | |
53010e52 RR |
273 | |
274 | wxASSERT( child ); | |
275 | ||
276 | delete nb_page->m_clientPanel; | |
53b28675 | 277 | |
53010e52 RR |
278 | // Amazingly, this is not necessary |
279 | // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num ); | |
53b28675 RR |
280 | |
281 | m_pages.DeleteObject( nb_page ); | |
282 | ||
283 | return TRUE; | |
284 | }; | |
285 | ||
4bf58c62 | 286 | bool wxNotebook::AddPage(wxWindow* win, const wxString& text, const int imageId, void* data) |
53b28675 | 287 | { |
4bf58c62 VZ |
288 | // we've created the notebook page in AddChild(). Now we just have to set |
289 | // the caption for the page and set the others parameters. | |
8aadf227 | 290 | |
4bf58c62 VZ |
291 | // first, find the page |
292 | wxNotebookPage *page = NULL; | |
53b28675 | 293 | |
4bf58c62 VZ |
294 | wxNode *node = m_pages.First(); |
295 | while (node) | |
8aadf227 | 296 | { |
4bf58c62 VZ |
297 | page = (wxNotebookPage*)node->Data(); |
298 | if ( page->m_clientPanel == win ) | |
299 | break; // found | |
300 | node = node->Next(); | |
301 | }; | |
302 | ||
303 | if ( page == NULL ) { | |
304 | wxFAIL_MSG("Can't add a page whose parent is not the notebook!"); | |
8aadf227 | 305 | |
4bf58c62 VZ |
306 | return FALSE; |
307 | } | |
308 | ||
309 | // then set the attributes | |
310 | page->m_text = text; | |
311 | if ( page->m_text.IsEmpty() ) | |
312 | page->m_text = ""; | |
313 | page->m_image = imageId; | |
314 | page->m_clientData = data; | |
315 | gtk_label_set(page->m_label, page->m_text); | |
53b28675 | 316 | |
8aadf227 | 317 | return TRUE; |
53b28675 RR |
318 | }; |
319 | ||
8aadf227 | 320 | wxWindow *wxNotebook::GetPageWindow( const int page ) const |
53b28675 | 321 | { |
8aadf227 JS |
322 | wxNotebookPage* nb_page = GetNotebookPage(page); |
323 | if (!nb_page) return NULL; | |
53b28675 RR |
324 | |
325 | return nb_page->m_clientPanel; | |
326 | }; | |
327 | ||
328 | void wxNotebook::AddChild( wxWindow *win ) | |
329 | { | |
4bf58c62 VZ |
330 | wxNotebookPage *page = new wxNotebookPage(); |
331 | ||
332 | page->m_id = GetPageCount(); | |
333 | page->m_label = (GtkLabel *)gtk_label_new("no caption"); | |
334 | page->m_clientPanel = win; | |
335 | gtk_notebook_append_page(GTK_NOTEBOOK(m_widget), win->m_widget, | |
336 | (GtkWidget *)page->m_label); | |
337 | gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); | |
338 | ||
339 | page->m_page = (GtkNotebookPage*) | |
340 | ( | |
341 | g_list_last(GTK_NOTEBOOK(m_widget)->children)->data | |
342 | ); | |
53b28675 | 343 | |
4bf58c62 VZ |
344 | if (!page->m_page) |
345 | { | |
346 | wxLogFatalError( "Notebook page creation error" ); | |
347 | return; | |
348 | } | |
349 | ||
350 | m_pages.Append( page ); | |
53b28675 RR |
351 | }; |
352 | ||
353 | //----------------------------------------------------------------------------- | |
354 | // wxTabEvent | |
355 | //----------------------------------------------------------------------------- | |
356 | ||
357 | IMPLEMENT_DYNAMIC_CLASS(wxTabEvent, wxCommandEvent) | |
358 | ||
359 | wxTabEvent::wxTabEvent( WXTYPE commandType, int id ) : | |
360 | wxCommandEvent(commandType, id) | |
361 | { | |
362 | }; | |
363 |