]> 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
01111366
RR
5// Id: $id$
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{
5b011451 153 if (m_pages.Number() == 0) return -1;
53b28675
RR
154
155 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
ff829f3f 156
c67daf87 157 wxNotebookPage *page = (wxNotebookPage *) NULL;
53b28675
RR
158
159 wxNode *node = m_pages.First();
160 while (node)
161 {
162 page = (wxNotebookPage*)node->Data();
ff829f3f
VZ
163 if (page->m_page == g_page)
164 break;
53b28675 165 node = node->Next();
ff7b1510 166 }
53b28675 167
b6af8d80 168 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
ff829f3f
VZ
169
170 return page->m_id;
ff7b1510 171}
53b28675 172
ff829f3f 173int wxNotebook::GetPageCount() const
53b28675
RR
174{
175 return m_pages.Number();
ff7b1510 176}
53b28675 177
ff829f3f 178int wxNotebook::GetRowCount() const
53b28675
RR
179{
180 return 1;
ff7b1510 181}
53b28675 182
ff829f3f 183wxString wxNotebook::GetPageText( int page ) const
53b28675 184{
8aadf227
JS
185 wxNotebookPage* nb_page = GetNotebookPage(page);
186 if (nb_page)
187 return nb_page->m_text;
188 else
189 return "";
ff7b1510 190}
53b28675 191
ff829f3f 192int wxNotebook::GetPageImage( int page ) const
53b28675 193{
8aadf227
JS
194 wxNotebookPage* nb_page = GetNotebookPage(page);
195 if (nb_page)
196 return nb_page->m_image;
197 else
4bf58c62 198 return 0;
ff7b1510 199}
53b28675 200
8aadf227 201wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
53b28675 202{
c67daf87 203 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
53b28675
RR
204
205 wxNode *node = m_pages.First();
206 while (node)
207 {
208 nb_page = (wxNotebookPage*)node->Data();
ff829f3f
VZ
209 if (nb_page->m_id == page)
210 return nb_page;
53b28675 211 node = node->Next();
ff7b1510 212 }
ff829f3f 213
b6af8d80 214 wxLogDebug( "Notebook page %d not found!", page );
ff829f3f 215
c67daf87 216 return (wxNotebookPage *) NULL;
ff7b1510 217}
53b28675 218
ff829f3f 219int wxNotebook::SetSelection( int page )
53b28675 220{
ff829f3f 221 int selOld = GetSelection();
8aadf227 222 wxNotebookPage* nb_page = GetNotebookPage(page);
5b011451
RR
223
224 if (!nb_page) return -1;
ff829f3f 225
53b28675
RR
226 int page_num = 0;
227 GList *child = GTK_NOTEBOOK(m_widget)->children;
228 while (child)
229 {
5b011451 230 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
53b28675
RR
231 page_num++;
232 child = child->next;
ff7b1510 233 }
ff829f3f 234
53b28675 235 if (!child) return -1;
ff829f3f 236
53b28675 237 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
ff829f3f
VZ
238
239 return selOld;
ff7b1510 240}
53b28675 241
5b011451 242void wxNotebook::AdvanceSelection( bool bForward )
ff829f3f 243{
5b011451
RR
244 int sel = GetSelection();
245 int max = GetPageCount();
ff829f3f 246
5b011451
RR
247 if (bForward)
248 SetSelection( sel == max ? 0 : sel + 1 );
249 else
250 SetSelection( sel == 0 ? max : sel - 1 );
ff829f3f
VZ
251}
252
53b28675
RR
253void wxNotebook::SetImageList( wxImageList* imageList )
254{
255 m_imageList = imageList;
ff7b1510 256}
53b28675 257
ff829f3f 258bool wxNotebook::SetPageText( int page, const wxString &text )
53b28675 259{
8aadf227 260 wxNotebookPage* nb_page = GetNotebookPage(page);
5b011451
RR
261
262 if (!nb_page) return FALSE;
ff829f3f 263
53b28675 264 nb_page->m_text = text;
ff829f3f 265
53b28675 266 return TRUE;
ff7b1510 267}
53b28675 268
debe6624 269bool wxNotebook::SetPageImage( int page, int image )
53b28675 270{
8aadf227 271 wxNotebookPage* nb_page = GetNotebookPage(page);
5b011451
RR
272
273 if (!nb_page) return FALSE;
53b28675 274
ff829f3f 275 nb_page->m_image = image;
53b28675 276
53b28675 277 return TRUE;
ff7b1510 278}
53b28675
RR
279
280void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
281{
5b011451 282 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
ff7b1510 283}
53b28675
RR
284
285void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
286{
5b011451 287 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
ff7b1510 288}
53b28675 289
ff829f3f 290bool wxNotebook::DeleteAllPages()
53b28675
RR
291{
292 wxNode *page_node = m_pages.First();
293 while (page_node)
294 {
295 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
ff829f3f 296
53b28675 297 DeletePage( page->m_id );
ff829f3f 298
53b28675 299 page_node = m_pages.First();
ff7b1510 300 }
ff829f3f 301
53b28675 302 return TRUE;
ff7b1510 303}
53b28675 304
ff829f3f 305bool wxNotebook::DeletePage( int page )
53b28675 306{
8aadf227
JS
307 wxNotebookPage* nb_page = GetNotebookPage(page);
308 if (!nb_page) return FALSE;
53b28675
RR
309
310 int page_num = 0;
311 GList *child = GTK_NOTEBOOK(m_widget)->children;
312 while (child)
313 {
314 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
315 page_num++;
316 child = child->next;
ff7b1510 317 }
53010e52
RR
318
319 wxASSERT( child );
ff829f3f 320
219f895a 321 delete nb_page->m_client;
ff829f3f 322
53010e52
RR
323// Amazingly, this is not necessary
324// gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
ff829f3f 325
53b28675 326 m_pages.DeleteObject( nb_page );
ff829f3f 327
53b28675 328 return TRUE;
ff7b1510 329}
53b28675 330
ff829f3f
VZ
331bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
332 bool bSelect, int imageId)
53b28675 333{
4bf58c62
VZ
334 // we've created the notebook page in AddChild(). Now we just have to set
335 // the caption for the page and set the others parameters.
8aadf227 336
c67daf87 337 wxNotebookPage *page = (wxNotebookPage *) NULL;
53b28675 338
4bf58c62
VZ
339 wxNode *node = m_pages.First();
340 while (node)
8aadf227 341 {
4bf58c62 342 page = (wxNotebookPage*)node->Data();
5b011451 343 if ( page->m_client == win ) break;
4bf58c62 344 node = node->Next();
ff7b1510 345 }
8aadf227 346
5b011451 347 wxCHECK_MSG( page != NULL, FALSE, "Can't add a page whose parent is not the notebook!" );
ff829f3f 348
5b011451
RR
349 if (imageId != -1)
350 {
24d20a8f
VZ
351 wxASSERT( m_imageList != NULL );
352
e4a81a2e 353 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
24d20a8f 354 GdkPixmap *pixmap = bmp->GetPixmap();
5b011451 355 GdkBitmap *mask = (GdkBitmap*) NULL;
e4a81a2e
VZ
356 if ( bmp->GetMask() )
357 {
358 mask = bmp->GetMask()->GetBitmap();
359 }
360
5b011451 361 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
24d20a8f
VZ
362
363 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
364
365 gtk_widget_show(pixmapwid);
366 }
367
741fd203
VZ
368 // then set the attributes
369 page->m_text = text;
5b011451 370 if (page->m_text.IsEmpty()) page->m_text = "";
741fd203
VZ
371 page->m_image = imageId;
372 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
5b011451 373 gtk_box_pack_start( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
741fd203
VZ
374
375 // @@@: what does this do? do we still need it?
376 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
377
378 gtk_widget_show((GtkWidget *)page->m_label);
379
5b011451 380 if (bSelect) SetSelection(GetPageCount());
ff829f3f 381
8aadf227 382 return TRUE;
ff7b1510 383}
53b28675 384
ff829f3f 385wxWindow *wxNotebook::GetPage( int page ) const
53b28675 386{
8aadf227 387 wxNotebookPage* nb_page = GetNotebookPage(page);
ff829f3f 388 if (!nb_page)
c67daf87 389 return (wxWindow *) NULL;
ff829f3f 390 else
219f895a 391 return nb_page->m_client;
ff7b1510 392}
53b28675
RR
393
394void wxNotebook::AddChild( wxWindow *win )
395{
4e6322e0
VZ
396 m_children.Append(win);
397
4bf58c62
VZ
398 wxNotebookPage *page = new wxNotebookPage();
399
400 page->m_id = GetPageCount();
24d20a8f
VZ
401
402 page->m_box = gtk_hbox_new (FALSE, 0);
403 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
404
219f895a 405 page->m_client = win;
5b011451 406 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget, page->m_box );
ff829f3f 407
5a8c929e 408 page->m_page =
219f895a 409 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
5a8c929e 410
219f895a 411 page->m_parent = GTK_NOTEBOOK(m_widget);
caac5181 412
33d0b396
RR
413 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
414 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
caac5181 415
4bf58c62
VZ
416 if (!page->m_page)
417 {
5b011451 418 wxLogFatalError( "Notebook page creation error" );
4bf58c62
VZ
419 return;
420 }
421
422 m_pages.Append( page );
ff7b1510 423}
53b28675 424
5a8c929e 425// override these 2 functions to do nothing: everything is done in OnSize
e3e65dac 426void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
5a8c929e
VZ
427{
428 // don't set the sizes of the pages - their correct size is not yet known
429 wxControl::SetConstraintSizes(FALSE);
430}
431
e3e65dac 432bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
5a8c929e
VZ
433{
434 return TRUE;
435}
436
53b28675 437//-----------------------------------------------------------------------------
ff829f3f 438// wxNotebookEvent
53b28675
RR
439//-----------------------------------------------------------------------------
440
ff829f3f 441IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)
5b011451 442