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