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