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