]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/notebook.cpp
Prealpha, prebeta of new wxTreeCtrl for GTK. It is possible to AddRoot,
[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
e4a81a2e 354 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
24d20a8f 355 GdkPixmap *pixmap = bmp->GetPixmap();
5b011451 356 GdkBitmap *mask = (GdkBitmap*) NULL;
e4a81a2e
VZ
357 if ( bmp->GetMask() )
358 {
359 mask = bmp->GetMask()->GetBitmap();
360 }
361
5b011451 362 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
24d20a8f
VZ
363
364 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
365
366 gtk_widget_show(pixmapwid);
367 }
368
741fd203
VZ
369 // then set the attributes
370 page->m_text = text;
5b011451 371 if (page->m_text.IsEmpty()) page->m_text = "";
741fd203
VZ
372 page->m_image = imageId;
373 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
5b011451 374 gtk_box_pack_start( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
741fd203
VZ
375
376 // @@@: what does this do? do we still need it?
377 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
378
379 gtk_widget_show((GtkWidget *)page->m_label);
380
5b011451 381 if (bSelect) SetSelection(GetPageCount());
ff829f3f 382
8aadf227 383 return TRUE;
ff7b1510 384}
53b28675 385
ff829f3f 386wxWindow *wxNotebook::GetPage( int page ) const
53b28675 387{
8aadf227 388 wxNotebookPage* nb_page = GetNotebookPage(page);
ff829f3f 389 if (!nb_page)
c67daf87 390 return (wxWindow *) NULL;
ff829f3f 391 else
219f895a 392 return nb_page->m_client;
ff7b1510 393}
53b28675
RR
394
395void wxNotebook::AddChild( wxWindow *win )
396{
4e6322e0
VZ
397 m_children.Append(win);
398
4bf58c62
VZ
399 wxNotebookPage *page = new wxNotebookPage();
400
401 page->m_id = GetPageCount();
24d20a8f
VZ
402
403 page->m_box = gtk_hbox_new (FALSE, 0);
404 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
405
219f895a 406 page->m_client = win;
5b011451 407 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget, page->m_box );
ff829f3f 408
5a8c929e 409 page->m_page =
219f895a 410 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
5a8c929e 411
219f895a 412 page->m_parent = GTK_NOTEBOOK(m_widget);
caac5181 413
33d0b396
RR
414 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
415 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
caac5181 416
4bf58c62
VZ
417 if (!page->m_page)
418 {
5b011451 419 wxLogFatalError( "Notebook page creation error" );
4bf58c62
VZ
420 return;
421 }
422
423 m_pages.Append( page );
ff7b1510 424}
53b28675 425
5a8c929e 426// override these 2 functions to do nothing: everything is done in OnSize
e3e65dac 427void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
5a8c929e
VZ
428{
429 // don't set the sizes of the pages - their correct size is not yet known
430 wxControl::SetConstraintSizes(FALSE);
431}
432
e3e65dac 433bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
5a8c929e
VZ
434{
435 return TRUE;
436}
437
53b28675 438//-----------------------------------------------------------------------------
ff829f3f 439// wxNotebookEvent
53b28675
RR
440//-----------------------------------------------------------------------------
441
ff829f3f 442IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
5b011451 443