]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/notebook.cpp
several fixes
[wxWidgets.git] / src / gtk1 / notebook.cpp
CommitLineData
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
ff829f3f 8// Licence: wxWindows licence
53b28675
RR
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 19#include "wx/log.h"
53b28675 20
219f895a
RR
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
ff829f3f
VZ
48//-----------------------------------------------------------------------------
49// GTK callbacks
50//-----------------------------------------------------------------------------
51
52// page change callback
219f895a
RR
53static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
54 GtkNotebookPage *WXUNUSED(page),
ff829f3f
VZ
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
33d0b396 71static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
caac5181
VZ
72{
73 if ( win->GetAutoLayout() )
74 win->Layout();
75
33d0b396
RR
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 };
caac5181 83
33d0b396
RR
84/*
85 printf( "OnResize from " );
86 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
87 printf( win->GetClassInfo()->GetClassName() );
88 printf( " .\n" );
caac5181 89
33d0b396
RR
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" );
caac5181 93
33d0b396
RR
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*/
caac5181 98
33d0b396 99 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
caac5181 100
33d0b396
RR
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" );
caac5181 105*/
33d0b396 106};
caac5181 107
53b28675
RR
108//-----------------------------------------------------------------------------
109// wxNotebook
110//-----------------------------------------------------------------------------
111
53b28675
RR
112IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
113
ff829f3f 114void wxNotebook::Init()
53b28675
RR
115{
116 m_imageList = NULL;
53b28675 117 m_pages.DeleteContents( TRUE );
ff829f3f
VZ
118 m_idHandler = 0;
119}
120
121wxNotebook::wxNotebook()
122{
123 Init();
53b28675
RR
124};
125
debe6624 126wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
53b28675 127 const wxPoint& pos, const wxSize& size,
debe6624 128 long style, const wxString& name )
53b28675 129{
ff829f3f 130 Init();
53b28675
RR
131 Create( parent, id, pos, size, style, name );
132};
133
ff829f3f 134wxNotebook::~wxNotebook()
53b28675 135{
ff829f3f
VZ
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
53b28675
RR
140 DeleteAllPages();
141};
142
debe6624 143bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
53b28675 144 const wxPoint& pos, const wxSize& size,
debe6624 145 long style, const wxString& name )
53b28675
RR
146{
147 m_needParent = TRUE;
ff829f3f 148
53b28675
RR
149 PreCreation( parent, id, pos, size, style, name );
150
151 m_widget = gtk_notebook_new();
caac5181 152
716b7364 153 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
caac5181 154
ff829f3f
VZ
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
53b28675 162 PostCreation();
ff829f3f 163
53b28675 164 Show( TRUE );
ff829f3f 165
53b28675
RR
166 return TRUE;
167};
168
ff829f3f 169int wxNotebook::GetSelection() const
53b28675 170{
ff829f3f
VZ
171 if (m_pages.Number() == 0)
172 return -1;
53b28675
RR
173
174 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
ff829f3f 175
53b28675
RR
176 wxNotebookPage *page = NULL;
177
178 wxNode *node = m_pages.First();
179 while (node)
180 {
181 page = (wxNotebookPage*)node->Data();
ff829f3f
VZ
182 if (page->m_page == g_page)
183 break;
53b28675
RR
184 node = node->Next();
185 };
53b28675 186
ff829f3f
VZ
187 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?");
188
189 return page->m_id;
53b28675
RR
190};
191
ff829f3f 192int wxNotebook::GetPageCount() const
53b28675
RR
193{
194 return m_pages.Number();
195};
196
ff829f3f 197int wxNotebook::GetRowCount() const
53b28675
RR
198{
199 return 1;
200};
201
ff829f3f 202wxString wxNotebook::GetPageText( int page ) const
53b28675 203{
8aadf227
JS
204 wxNotebookPage* nb_page = GetNotebookPage(page);
205 if (nb_page)
206 return nb_page->m_text;
207 else
208 return "";
53b28675
RR
209};
210
ff829f3f 211int wxNotebook::GetPageImage( int page ) const
53b28675 212{
8aadf227
JS
213 wxNotebookPage* nb_page = GetNotebookPage(page);
214 if (nb_page)
215 return nb_page->m_image;
216 else
4bf58c62 217 return 0;
53b28675
RR
218};
219
8aadf227 220wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
53b28675
RR
221{
222 wxNotebookPage *nb_page = NULL;
223
224 wxNode *node = m_pages.First();
225 while (node)
226 {
227 nb_page = (wxNotebookPage*)node->Data();
ff829f3f
VZ
228 if (nb_page->m_id == page)
229 return nb_page;
53b28675
RR
230 node = node->Next();
231 };
ff829f3f 232
219f895a 233 wxLogDebug( "Notebook page %d not found!", page );
ff829f3f
VZ
234
235 return NULL;
53b28675
RR
236};
237
ff829f3f 238int wxNotebook::SetSelection( int page )
53b28675 239{
ff829f3f 240 int selOld = GetSelection();
8aadf227 241 wxNotebookPage* nb_page = GetNotebookPage(page);
ff829f3f
VZ
242 if (!nb_page)
243 return -1;
244
53b28675
RR
245 int page_num = 0;
246 GList *child = GTK_NOTEBOOK(m_widget)->children;
247 while (child)
248 {
ff829f3f
VZ
249 if (nb_page->m_page == (GtkNotebookPage*)child->data)
250 break;
53b28675
RR
251 page_num++;
252 child = child->next;
253 };
ff829f3f 254
53b28675 255 if (!child) return -1;
ff829f3f 256
53b28675 257 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
ff829f3f
VZ
258
259 return selOld;
53b28675
RR
260};
261
ff829f3f
VZ
262void 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
53b28675
RR
275void wxNotebook::SetImageList( wxImageList* imageList )
276{
277 m_imageList = imageList;
278};
279
ff829f3f 280bool wxNotebook::SetPageText( int page, const wxString &text )
53b28675 281{
8aadf227 282 wxNotebookPage* nb_page = GetNotebookPage(page);
ff829f3f
VZ
283 if (!nb_page)
284 return FALSE;
285
53b28675 286 nb_page->m_text = text;
ff829f3f 287
53b28675
RR
288 return TRUE;
289};
290
debe6624 291bool wxNotebook::SetPageImage( int page, int image )
53b28675 292{
8aadf227
JS
293 wxNotebookPage* nb_page = GetNotebookPage(page);
294 if (!nb_page)
295 return FALSE;
53b28675 296
ff829f3f 297 nb_page->m_image = image;
53b28675 298
53b28675
RR
299 return TRUE;
300};
301
302void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
303{
ff829f3f 304 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
53b28675
RR
305};
306
307void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
308{
ff829f3f 309 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
53b28675
RR
310};
311
ff829f3f 312bool wxNotebook::DeleteAllPages()
53b28675
RR
313{
314 wxNode *page_node = m_pages.First();
315 while (page_node)
316 {
317 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
ff829f3f 318
53b28675 319 DeletePage( page->m_id );
ff829f3f 320
53b28675
RR
321 page_node = m_pages.First();
322 };
ff829f3f 323
53b28675
RR
324 return TRUE;
325};
326
ff829f3f 327bool wxNotebook::DeletePage( int page )
53b28675 328{
8aadf227
JS
329 wxNotebookPage* nb_page = GetNotebookPage(page);
330 if (!nb_page) return FALSE;
53b28675
RR
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 };
53010e52
RR
340
341 wxASSERT( child );
ff829f3f 342
219f895a 343 delete nb_page->m_client;
ff829f3f 344
53010e52
RR
345// Amazingly, this is not necessary
346// gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
ff829f3f 347
53b28675 348 m_pages.DeleteObject( nb_page );
ff829f3f 349
53b28675
RR
350 return TRUE;
351};
352
ff829f3f
VZ
353bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
354 bool bSelect, int imageId)
53b28675 355{
4bf58c62
VZ
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.
8aadf227 358
4bf58c62
VZ
359 // first, find the page
360 wxNotebookPage *page = NULL;
53b28675 361
4bf58c62
VZ
362 wxNode *node = m_pages.First();
363 while (node)
8aadf227 364 {
4bf58c62 365 page = (wxNotebookPage*)node->Data();
219f895a 366 if ( page->m_client == win )
4bf58c62
VZ
367 break; // found
368 node = node->Next();
369 };
8aadf227 370
ff829f3f
VZ
371 wxCHECK_MSG(page != NULL, FALSE,
372 "Can't add a page whose parent is not the notebook!");
373
4bf58c62
VZ
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;
4bf58c62 379 gtk_label_set(page->m_label, page->m_text);
53b28675 380
ff829f3f
VZ
381 if ( bSelect ) {
382 SetSelection(GetPageCount());
383 }
384
8aadf227 385 return TRUE;
53b28675
RR
386};
387
ff829f3f 388wxWindow *wxNotebook::GetPage( int page ) const
53b28675 389{
8aadf227 390 wxNotebookPage* nb_page = GetNotebookPage(page);
ff829f3f
VZ
391 if (!nb_page)
392 return NULL;
393 else
219f895a 394 return nb_page->m_client;
53b28675
RR
395};
396
397void wxNotebook::AddChild( wxWindow *win )
398{
4e6322e0
VZ
399 m_children.Append(win);
400
4bf58c62
VZ
401 wxNotebookPage *page = new wxNotebookPage();
402
403 page->m_id = GetPageCount();
219f895a
RR
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);
4bf58c62 408 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
ff829f3f 409
5a8c929e 410 page->m_page =
219f895a 411 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
5a8c929e 412
219f895a 413 page->m_parent = GTK_NOTEBOOK(m_widget);
caac5181 414
33d0b396
RR
415 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
416 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
caac5181 417
4bf58c62
VZ
418 if (!page->m_page)
419 {
420 wxLogFatalError( "Notebook page creation error" );
421 return;
422 }
423
424 m_pages.Append( page );
53b28675
RR
425};
426
5a8c929e 427// override these 2 functions to do nothing: everything is done in OnSize
e3e65dac 428void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
5a8c929e
VZ
429{
430 // don't set the sizes of the pages - their correct size is not yet known
431 wxControl::SetConstraintSizes(FALSE);
432}
433
e3e65dac 434bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
5a8c929e
VZ
435{
436 return TRUE;
437}
438
53b28675 439//-----------------------------------------------------------------------------
ff829f3f 440// wxNotebookEvent
53b28675
RR
441//-----------------------------------------------------------------------------
442
ff829f3f 443IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)