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