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