]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/notebook.cpp
* Fixed a memory leak in wxThread
[wxWidgets.git] / src / gtk1 / notebook.cpp
CommitLineData
53b28675
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: notebook.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
ff829f3f 7// Licence: wxWindows licence
53b28675
RR
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "notebook.h"
12#endif
13
14#include "wx/notebook.h"
15#include "wx/panel.h"
16#include "wx/utils.h"
17#include "wx/imaglist.h"
30dea054 18#include "wx/intl.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;
c67daf87
UR
33 m_page = (GtkNotebookPage *) NULL;
34 m_client = (wxWindow *) NULL;
35 m_parent = (GtkNotebook *) NULL;
24d20a8f 36 m_box = (GtkWidget *) NULL;
ff7b1510 37 }
219f895a 38
219f895a
RR
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;
24d20a8f 46 GtkWidget *m_box; // in which the label and image are packed
219f895a
RR
47};
48
ff829f3f 49//-----------------------------------------------------------------------------
5b011451 50// "switch_page"
ff829f3f
VZ
51//-----------------------------------------------------------------------------
52
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
5b011451 60 int old = notebook->GetSelection();
ff829f3f
VZ
61
62 // TODO: emulate PAGE_CHANGING event
cb43b372
RR
63
64 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
65 notebook->GetId(), nPage, old );
5b011451 66 event.SetEventObject( notebook );
cb43b372 67 notebook->GetEventHandler()->ProcessEvent( event );
ff829f3f
VZ
68}
69
5b011451
RR
70//-----------------------------------------------------------------------------
71// "size_allocate"
72//-----------------------------------------------------------------------------
73
33d0b396 74static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
caac5181 75{
5b011451 76 if (win->GetAutoLayout()) win->Layout();
caac5181 77
33d0b396
RR
78 if ((win->m_x == alloc->x) &&
79 (win->m_y == alloc->y) &&
80 (win->m_width == alloc->width) &&
81 (win->m_height == alloc->height))
82 {
83 return;
ff7b1510 84 }
caac5181 85
33d0b396 86 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
ff7b1510 87}
caac5181 88
6ca41e57
RR
89//-----------------------------------------------------------------------------
90// InsertChild callback for wxNotebook
91//-----------------------------------------------------------------------------
92
93static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
94{
95 wxNotebookPage *page = new wxNotebookPage();
96
97 page->m_id = parent->GetPageCount();
98
99 page->m_box = gtk_hbox_new (FALSE, 0);
100 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
101
102 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
103
104 page->m_client = child;
105 gtk_notebook_append_page( notebook, child->m_widget, page->m_box );
106
107 page->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
108
109 page->m_parent = notebook;
110
111 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
112 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
113
114 if (!page->m_page)
115 {
116 wxLogFatalError( "Notebook page creation error" );
117 return;
118 }
119
120 parent->m_pages.Append( page );
121}
122
53b28675
RR
123//-----------------------------------------------------------------------------
124// wxNotebook
125//-----------------------------------------------------------------------------
126
53b28675
RR
127IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
128
ff829f3f 129void wxNotebook::Init()
53b28675 130{
c67daf87 131 m_imageList = (wxImageList *) NULL;
53b28675 132 m_pages.DeleteContents( TRUE );
ff829f3f
VZ
133 m_idHandler = 0;
134}
135
136wxNotebook::wxNotebook()
137{
138 Init();
ff7b1510 139}
53b28675 140
debe6624 141wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
53b28675 142 const wxPoint& pos, const wxSize& size,
debe6624 143 long style, const wxString& name )
53b28675 144{
ff829f3f 145 Init();
53b28675 146 Create( parent, id, pos, size, style, name );
ff7b1510 147}
53b28675 148
ff829f3f 149wxNotebook::~wxNotebook()
53b28675 150{
ff829f3f 151 // don't generate change page events any more
5b011451 152 if (m_idHandler != 0)
ff829f3f
VZ
153 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
154
53b28675 155 DeleteAllPages();
ff7b1510 156}
53b28675 157
debe6624 158bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
53b28675 159 const wxPoint& pos, const wxSize& size,
debe6624 160 long style, const wxString& name )
53b28675
RR
161{
162 m_needParent = TRUE;
6ca41e57 163 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
ff829f3f 164
53b28675
RR
165 PreCreation( parent, id, pos, size, style, name );
166
167 m_widget = gtk_notebook_new();
caac5181 168
716b7364 169 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
caac5181 170
ff829f3f
VZ
171 m_idHandler = gtk_signal_connect
172 (
173 GTK_OBJECT(m_widget), "switch_page",
174 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
175 (gpointer)this
176 );
177
6ca41e57
RR
178 m_parent->AddChild( this );
179
180 (m_parent->m_insertCallback)( m_parent, this );
181
53b28675 182 PostCreation();
ff829f3f 183
53b28675 184 Show( TRUE );
ff829f3f 185
53b28675 186 return TRUE;
ff7b1510 187}
53b28675 188
ff829f3f 189int wxNotebook::GetSelection() const
53b28675 190{
a81258be
RR
191 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
192
5b011451 193 if (m_pages.Number() == 0) return -1;
53b28675
RR
194
195 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
ff829f3f 196
c67daf87 197 wxNotebookPage *page = (wxNotebookPage *) NULL;
53b28675
RR
198
199 wxNode *node = m_pages.First();
200 while (node)
201 {
202 page = (wxNotebookPage*)node->Data();
ff829f3f
VZ
203 if (page->m_page == g_page)
204 break;
53b28675 205 node = node->Next();
ff7b1510 206 }
53b28675 207
b6af8d80 208 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
ff829f3f
VZ
209
210 return page->m_id;
ff7b1510 211}
53b28675 212
ff829f3f 213int wxNotebook::GetPageCount() const
53b28675
RR
214{
215 return m_pages.Number();
ff7b1510 216}
53b28675 217
ff829f3f 218int wxNotebook::GetRowCount() const
53b28675
RR
219{
220 return 1;
ff7b1510 221}
53b28675 222
ff829f3f 223wxString wxNotebook::GetPageText( int page ) const
53b28675 224{
a81258be
RR
225 wxCHECK_MSG( m_widget != NULL, "", "invalid notebook" );
226
8aadf227
JS
227 wxNotebookPage* nb_page = GetNotebookPage(page);
228 if (nb_page)
229 return nb_page->m_text;
230 else
231 return "";
ff7b1510 232}
53b28675 233
ff829f3f 234int wxNotebook::GetPageImage( int page ) const
53b28675 235{
a81258be
RR
236 wxCHECK_MSG( m_widget != NULL, 0, "invalid notebook" );
237
8aadf227
JS
238 wxNotebookPage* nb_page = GetNotebookPage(page);
239 if (nb_page)
240 return nb_page->m_image;
241 else
4bf58c62 242 return 0;
ff7b1510 243}
53b28675 244
8aadf227 245wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
53b28675 246{
a81258be
RR
247 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*)NULL, "invalid notebook" );
248
c67daf87 249 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
53b28675
RR
250
251 wxNode *node = m_pages.First();
252 while (node)
253 {
254 nb_page = (wxNotebookPage*)node->Data();
ff829f3f
VZ
255 if (nb_page->m_id == page)
256 return nb_page;
53b28675 257 node = node->Next();
ff7b1510 258 }
ff829f3f 259
b6af8d80 260 wxLogDebug( "Notebook page %d not found!", page );
ff829f3f 261
c67daf87 262 return (wxNotebookPage *) NULL;
ff7b1510 263}
53b28675 264
ff829f3f 265int wxNotebook::SetSelection( int page )
53b28675 266{
a81258be
RR
267 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
268
ff829f3f 269 int selOld = GetSelection();
8aadf227 270 wxNotebookPage* nb_page = GetNotebookPage(page);
5b011451
RR
271
272 if (!nb_page) return -1;
ff829f3f 273
53b28675
RR
274 int page_num = 0;
275 GList *child = GTK_NOTEBOOK(m_widget)->children;
276 while (child)
277 {
5b011451 278 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
53b28675
RR
279 page_num++;
280 child = child->next;
ff7b1510 281 }
ff829f3f 282
53b28675 283 if (!child) return -1;
ff829f3f 284
53b28675 285 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
ff829f3f
VZ
286
287 return selOld;
ff7b1510 288}
53b28675 289
5b011451 290void wxNotebook::AdvanceSelection( bool bForward )
ff829f3f 291{
a81258be
RR
292 wxCHECK_RET( m_widget != NULL, "invalid notebook" );
293
5b011451
RR
294 int sel = GetSelection();
295 int max = GetPageCount();
ff829f3f 296
5b011451
RR
297 if (bForward)
298 SetSelection( sel == max ? 0 : sel + 1 );
299 else
300 SetSelection( sel == 0 ? max : sel - 1 );
ff829f3f
VZ
301}
302
53b28675
RR
303void wxNotebook::SetImageList( wxImageList* imageList )
304{
305 m_imageList = imageList;
ff7b1510 306}
53b28675 307
ff829f3f 308bool wxNotebook::SetPageText( int page, const wxString &text )
53b28675 309{
a81258be
RR
310 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
311
8aadf227 312 wxNotebookPage* nb_page = GetNotebookPage(page);
5b011451
RR
313
314 if (!nb_page) return FALSE;
ff829f3f 315
53b28675 316 nb_page->m_text = text;
ff829f3f 317
53b28675 318 return TRUE;
ff7b1510 319}
53b28675 320
debe6624 321bool wxNotebook::SetPageImage( int page, int image )
53b28675 322{
8aadf227 323 wxNotebookPage* nb_page = GetNotebookPage(page);
5b011451
RR
324
325 if (!nb_page) return FALSE;
53b28675 326
ff829f3f 327 nb_page->m_image = image;
53b28675 328
53b28675 329 return TRUE;
ff7b1510 330}
53b28675
RR
331
332void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
333{
5b011451 334 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
ff7b1510 335}
53b28675
RR
336
337void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
338{
5b011451 339 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
ff7b1510 340}
53b28675 341
ff829f3f 342bool wxNotebook::DeleteAllPages()
53b28675 343{
a81258be
RR
344 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
345
53b28675
RR
346 wxNode *page_node = m_pages.First();
347 while (page_node)
348 {
349 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
ff829f3f 350
53b28675 351 DeletePage( page->m_id );
ff829f3f 352
53b28675 353 page_node = m_pages.First();
ff7b1510 354 }
ff829f3f 355
53b28675 356 return TRUE;
ff7b1510 357}
53b28675 358
ff829f3f 359bool wxNotebook::DeletePage( int page )
53b28675 360{
8aadf227
JS
361 wxNotebookPage* nb_page = GetNotebookPage(page);
362 if (!nb_page) return FALSE;
53b28675
RR
363
364 int page_num = 0;
365 GList *child = GTK_NOTEBOOK(m_widget)->children;
366 while (child)
367 {
368 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
369 page_num++;
370 child = child->next;
ff7b1510 371 }
53010e52 372
fed46e72 373 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
ff829f3f 374
219f895a 375 delete nb_page->m_client;
ff829f3f 376
fed46e72
RR
377 m_pages.DeleteObject( nb_page );
378
379 return TRUE;
380}
381
382bool wxNotebook::RemovePage( int page )
383{
384 wxNotebookPage* nb_page = GetNotebookPage(page);
385 if (!nb_page) return FALSE;
386
387 int page_num = 0;
388 GList *child = GTK_NOTEBOOK(m_widget)->children;
389 while (child)
390 {
391 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
392 page_num++;
393 child = child->next;
394 }
395
396 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
397
398 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
ff829f3f 399
53b28675 400 m_pages.DeleteObject( nb_page );
ff829f3f 401
53b28675 402 return TRUE;
ff7b1510 403}
53b28675 404
ff829f3f
VZ
405bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
406 bool bSelect, int imageId)
53b28675 407{
a81258be
RR
408 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
409
4bf58c62
VZ
410 // we've created the notebook page in AddChild(). Now we just have to set
411 // the caption for the page and set the others parameters.
8aadf227 412
c67daf87 413 wxNotebookPage *page = (wxNotebookPage *) NULL;
53b28675 414
4bf58c62
VZ
415 wxNode *node = m_pages.First();
416 while (node)
8aadf227 417 {
4bf58c62 418 page = (wxNotebookPage*)node->Data();
5b011451 419 if ( page->m_client == win ) break;
4bf58c62 420 node = node->Next();
ff7b1510 421 }
8aadf227 422
5b011451 423 wxCHECK_MSG( page != NULL, FALSE, "Can't add a page whose parent is not the notebook!" );
ff829f3f 424
5b011451
RR
425 if (imageId != -1)
426 {
24d20a8f
VZ
427 wxASSERT( m_imageList != NULL );
428
e4a81a2e 429 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
24d20a8f 430 GdkPixmap *pixmap = bmp->GetPixmap();
5b011451 431 GdkBitmap *mask = (GdkBitmap*) NULL;
e4a81a2e
VZ
432 if ( bmp->GetMask() )
433 {
434 mask = bmp->GetMask()->GetBitmap();
435 }
436
5b011451 437 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
24d20a8f
VZ
438
439 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
440
441 gtk_widget_show(pixmapwid);
442 }
443
741fd203
VZ
444 // then set the attributes
445 page->m_text = text;
5b011451 446 if (page->m_text.IsEmpty()) page->m_text = "";
741fd203
VZ
447 page->m_image = imageId;
448 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
5b011451 449 gtk_box_pack_start( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
741fd203
VZ
450
451 // @@@: what does this do? do we still need it?
452 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
453
454 gtk_widget_show((GtkWidget *)page->m_label);
455
5b011451 456 if (bSelect) SetSelection(GetPageCount());
ff829f3f 457
8aadf227 458 return TRUE;
ff7b1510 459}
53b28675 460
ff829f3f 461wxWindow *wxNotebook::GetPage( int page ) const
53b28675 462{
a81258be
RR
463 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, "invalid notebook" );
464
8aadf227 465 wxNotebookPage* nb_page = GetNotebookPage(page);
ff829f3f 466 if (!nb_page)
c67daf87 467 return (wxWindow *) NULL;
ff829f3f 468 else
219f895a 469 return nb_page->m_client;
ff7b1510 470}
53b28675 471
5a8c929e 472// override these 2 functions to do nothing: everything is done in OnSize
e3e65dac 473void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
5a8c929e
VZ
474{
475 // don't set the sizes of the pages - their correct size is not yet known
476 wxControl::SetConstraintSizes(FALSE);
477}
478
e3e65dac 479bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
5a8c929e
VZ
480{
481 return TRUE;
482}
483
58614078 484void wxNotebook::ApplyWidgetStyle()
a81258be 485{
58614078 486 SetWidgetStyle();
a81258be
RR
487 gtk_widget_set_style( m_widget, m_widgetStyle );
488}
489
53b28675 490//-----------------------------------------------------------------------------
ff829f3f 491// wxNotebookEvent
53b28675
RR
492//-----------------------------------------------------------------------------
493
ff829f3f 494IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
5b011451 495