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