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