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