]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/notebook.cpp
fixed appending of '/' to empty string
[wxWidgets.git] / src / gtk1 / notebook.cpp
... / ...
CommitLineData
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
25class wxNotebookPage: public wxObject
26{
27public:
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
53static 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
71static 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
112BEGIN_EVENT_TABLE(wxNotebook, wxControl)
113 EVT_SIZE(wxNotebook::OnSize)
114END_EVENT_TABLE()
115
116IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
117
118void wxNotebook::Init()
119{
120 m_imageList = NULL;
121 m_pages.DeleteContents( TRUE );
122 m_idHandler = 0;
123}
124
125wxNotebook::wxNotebook()
126{
127 Init();
128};
129
130wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
131 const wxPoint& pos, const wxSize& size,
132 long style, const wxString& name )
133{
134 Init();
135 Create( parent, id, pos, size, style, name );
136};
137
138wxNotebook::~wxNotebook()
139{
140 // don't generate change page events any more
141 if ( m_idHandler != 0 )
142 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
143
144 DeleteAllPages();
145};
146
147bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
148 const wxPoint& pos, const wxSize& size,
149 long style, const wxString& name )
150{
151 m_needParent = TRUE;
152
153 PreCreation( parent, id, pos, size, style, name );
154
155 m_widget = gtk_notebook_new();
156
157 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
158
159 m_idHandler = gtk_signal_connect
160 (
161 GTK_OBJECT(m_widget), "switch_page",
162 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
163 (gpointer)this
164 );
165
166 PostCreation();
167
168 Show( TRUE );
169
170 return TRUE;
171};
172
173int wxNotebook::GetSelection() const
174{
175 if (m_pages.Number() == 0)
176 return -1;
177
178 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
179
180 wxNotebookPage *page = NULL;
181
182 wxNode *node = m_pages.First();
183 while (node)
184 {
185 page = (wxNotebookPage*)node->Data();
186 if (page->m_page == g_page)
187 break;
188 node = node->Next();
189 };
190
191 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?");
192
193 return page->m_id;
194};
195
196int wxNotebook::GetPageCount() const
197{
198 return m_pages.Number();
199};
200
201int wxNotebook::GetRowCount() const
202{
203 return 1;
204};
205
206wxString wxNotebook::GetPageText( int page ) const
207{
208 wxNotebookPage* nb_page = GetNotebookPage(page);
209 if (nb_page)
210 return nb_page->m_text;
211 else
212 return "";
213};
214
215int wxNotebook::GetPageImage( int page ) const
216{
217 wxNotebookPage* nb_page = GetNotebookPage(page);
218 if (nb_page)
219 return nb_page->m_image;
220 else
221 return 0;
222};
223
224wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
225{
226 wxNotebookPage *nb_page = NULL;
227
228 wxNode *node = m_pages.First();
229 while (node)
230 {
231 nb_page = (wxNotebookPage*)node->Data();
232 if (nb_page->m_id == page)
233 return nb_page;
234 node = node->Next();
235 };
236
237 wxLogDebug( "Notebook page %d not found!", page );
238
239 return NULL;
240};
241
242int wxNotebook::SetSelection( int page )
243{
244 int selOld = GetSelection();
245 wxNotebookPage* nb_page = GetNotebookPage(page);
246 if (!nb_page)
247 return -1;
248
249 int page_num = 0;
250 GList *child = GTK_NOTEBOOK(m_widget)->children;
251 while (child)
252 {
253 if (nb_page->m_page == (GtkNotebookPage*)child->data)
254 break;
255 page_num++;
256 child = child->next;
257 };
258
259 if (!child) return -1;
260
261 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
262
263 return selOld;
264};
265
266void wxNotebook::AdvanceSelection(bool bForward)
267{
268 int nSel = GetSelection(),
269 nMax = GetPageCount();
270
271 if ( bForward ) {
272 SetSelection(nSel == nMax ? 0 : nSel + 1);
273 }
274 else {
275 SetSelection(nSel == 0 ? nMax : nSel - 1);
276 }
277}
278
279void wxNotebook::SetImageList( wxImageList* imageList )
280{
281 m_imageList = imageList;
282};
283
284bool wxNotebook::SetPageText( int page, const wxString &text )
285{
286 wxNotebookPage* nb_page = GetNotebookPage(page);
287 if (!nb_page)
288 return FALSE;
289
290 nb_page->m_text = text;
291
292 return TRUE;
293};
294
295bool wxNotebook::SetPageImage( int page, int image )
296{
297 wxNotebookPage* nb_page = GetNotebookPage(page);
298 if (!nb_page)
299 return FALSE;
300
301 nb_page->m_image = image;
302
303 return TRUE;
304};
305
306void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
307{
308 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
309};
310
311void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
312{
313 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
314};
315
316bool wxNotebook::DeleteAllPages()
317{
318 wxNode *page_node = m_pages.First();
319 while (page_node)
320 {
321 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
322
323 DeletePage( page->m_id );
324
325 page_node = m_pages.First();
326 };
327
328 return TRUE;
329};
330
331bool wxNotebook::DeletePage( int page )
332{
333 wxNotebookPage* nb_page = GetNotebookPage(page);
334 if (!nb_page) return FALSE;
335
336 int page_num = 0;
337 GList *child = GTK_NOTEBOOK(m_widget)->children;
338 while (child)
339 {
340 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
341 page_num++;
342 child = child->next;
343 };
344
345 wxASSERT( child );
346
347 delete nb_page->m_client;
348
349// Amazingly, this is not necessary
350// gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
351
352 m_pages.DeleteObject( nb_page );
353
354 return TRUE;
355};
356
357bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
358 bool bSelect, int imageId)
359{
360 // we've created the notebook page in AddChild(). Now we just have to set
361 // the caption for the page and set the others parameters.
362
363 // first, find the page
364 wxNotebookPage *page = NULL;
365
366 wxNode *node = m_pages.First();
367 while (node)
368 {
369 page = (wxNotebookPage*)node->Data();
370 if ( page->m_client == win )
371 break; // found
372 node = node->Next();
373 };
374
375 wxCHECK_MSG(page != NULL, FALSE,
376 "Can't add a page whose parent is not the notebook!");
377
378 // then set the attributes
379 page->m_text = text;
380 if ( page->m_text.IsEmpty() )
381 page->m_text = "";
382 page->m_image = imageId;
383 gtk_label_set(page->m_label, page->m_text);
384
385 if ( bSelect ) {
386 SetSelection(GetPageCount());
387 }
388
389 return TRUE;
390};
391
392wxWindow *wxNotebook::GetPage( int page ) const
393{
394 wxNotebookPage* nb_page = GetNotebookPage(page);
395 if (!nb_page)
396 return NULL;
397 else
398 return nb_page->m_client;
399};
400
401void wxNotebook::AddChild( wxWindow *win )
402{
403 // @@@ normally done in wxWindow::AddChild but for some reason wxNotebook
404 // case is special there (Robert?)
405 // Robert: Don't you think the code below looks different from the one
406 // in wxWindow::AddChild :-)
407
408 m_children.Append(win);
409
410 wxNotebookPage *page = new wxNotebookPage();
411
412 page->m_id = GetPageCount();
413 page->m_label = (GtkLabel *)gtk_label_new("Handle");
414 page->m_client = win;
415 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget,
416 (GtkWidget *)page->m_label);
417 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
418
419 page->m_page =
420 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
421
422 page->m_parent = GTK_NOTEBOOK(m_widget);
423
424 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
425 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
426
427 if (!page->m_page)
428 {
429 wxLogFatalError( "Notebook page creation error" );
430 return;
431 }
432
433 m_pages.Append( page );
434};
435
436// override these 2 functions to do nothing: everything is done in OnSize
437void wxNotebook::SetConstraintSizes(bool /* recurse */)
438{
439 // don't set the sizes of the pages - their correct size is not yet known
440 wxControl::SetConstraintSizes(FALSE);
441}
442
443bool wxNotebook::DoPhase(int /* nPhase */)
444{
445 return TRUE;
446}
447
448//-----------------------------------------------------------------------------
449// wxNotebookEvent
450//-----------------------------------------------------------------------------
451
452IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)