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