]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/notebook.cpp
fixed config incosistency
[wxWidgets.git] / src / gtk1 / notebook.cpp
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
8 // Licence: wxWindows licence
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"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21
22 //-----------------------------------------------------------------------------
23 // wxNotebookPage
24 //-----------------------------------------------------------------------------
25
26 class wxNotebookPage: public wxObject
27 {
28 public:
29 wxNotebookPage()
30 {
31 m_id = -1;
32 m_text = "";
33 m_image = -1;
34 m_page = NULL;
35 m_client = NULL;
36 m_parent = 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 };
48
49 //-----------------------------------------------------------------------------
50 // GTK callbacks
51 //-----------------------------------------------------------------------------
52
53 // page change callback
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 nOld = notebook->GetSelection();
62
63 // TODO: emulate PAGE_CHANGING event
64 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
65 notebook->GetId(),
66 nPage,
67 nOld);
68 event.SetEventObject(notebook);
69 notebook->ProcessEvent(event);
70 }
71
72 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
73 {
74 if ((win->m_x == alloc->x) &&
75 (win->m_y == alloc->y) &&
76 (win->m_width == alloc->width) &&
77 (win->m_height == alloc->height))
78 {
79 return;
80 };
81
82 /*
83 printf( "OnResize from " );
84 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
85 printf( win->GetClassInfo()->GetClassName() );
86 printf( " .\n" );
87
88 printf( " Old: X: %d Y: %d ", win->m_x, win->m_y );
89 printf( " W: %d H: %d ", win->m_width, win->m_height );
90 printf( " .\n" );
91
92 printf( " New: X: %d Y: %d ", alloc->x, alloc->y );
93 printf( " W: %d H: %d ", alloc->width, alloc->height );
94 printf( " .\n" );
95 */
96
97 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
98
99 /*
100 printf( " Res: X: %d Y: %d ", win->m_x, win->m_y );
101 printf( " W: %d H: %d ", win->m_width, win->m_height );
102 printf( " .\n" );
103 */
104 };
105
106 //-----------------------------------------------------------------------------
107 // wxNotebook
108 //-----------------------------------------------------------------------------
109
110 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
111 EVT_SIZE(wxNotebook::OnSize)
112 END_EVENT_TABLE()
113
114 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
115
116 void wxNotebook::Init()
117 {
118 m_imageList = NULL;
119 m_pages.DeleteContents( TRUE );
120 m_idHandler = 0;
121 }
122
123 wxNotebook::wxNotebook()
124 {
125 Init();
126 };
127
128 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
129 const wxPoint& pos, const wxSize& size,
130 long style, const wxString& name )
131 {
132 Init();
133 Create( parent, id, pos, size, style, name );
134 };
135
136 wxNotebook::~wxNotebook()
137 {
138 // don't generate change page events any more
139 if ( m_idHandler != 0 )
140 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
141
142 if (m_imageList)
143 delete m_imageList;
144 DeleteAllPages();
145 };
146
147 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
148 const wxPoint& pos, const wxSize& size,
149 long style, const wxString& name )
150 {
151 m_needParent = TRUE;
152
153 PreCreation( parent, id, pos, size, style, name );
154
155 m_widget = gtk_notebook_new();
156 m_idHandler = gtk_signal_connect
157 (
158 GTK_OBJECT(m_widget), "switch_page",
159 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
160 (gpointer)this
161 );
162
163 PostCreation();
164
165 Show( TRUE );
166
167 return TRUE;
168 };
169
170 int wxNotebook::GetSelection() const
171 {
172 if (m_pages.Number() == 0)
173 return -1;
174
175 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
176
177 wxNotebookPage *page = NULL;
178
179 wxNode *node = m_pages.First();
180 while (node)
181 {
182 page = (wxNotebookPage*)node->Data();
183 if (page->m_page == g_page)
184 break;
185 node = node->Next();
186 };
187
188 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?");
189
190 return page->m_id;
191 };
192
193 int wxNotebook::GetPageCount() const
194 {
195 return m_pages.Number();
196 };
197
198 int wxNotebook::GetRowCount() const
199 {
200 return 1;
201 };
202
203 wxString wxNotebook::GetPageText( int page ) const
204 {
205 wxNotebookPage* nb_page = GetNotebookPage(page);
206 if (nb_page)
207 return nb_page->m_text;
208 else
209 return "";
210 };
211
212 int wxNotebook::GetPageImage( int page ) const
213 {
214 wxNotebookPage* nb_page = GetNotebookPage(page);
215 if (nb_page)
216 return nb_page->m_image;
217 else
218 return 0;
219 };
220
221 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
222 {
223 wxNotebookPage *nb_page = NULL;
224
225 wxNode *node = m_pages.First();
226 while (node)
227 {
228 nb_page = (wxNotebookPage*)node->Data();
229 if (nb_page->m_id == page)
230 return nb_page;
231 node = node->Next();
232 };
233
234 wxLogDebug( "Notebook page %d not found!", page );
235
236 return NULL;
237 };
238
239 int wxNotebook::SetSelection( int page )
240 {
241 int selOld = GetSelection();
242 wxNotebookPage* nb_page = GetNotebookPage(page);
243 if (!nb_page)
244 return -1;
245
246 int page_num = 0;
247 GList *child = GTK_NOTEBOOK(m_widget)->children;
248 while (child)
249 {
250 if (nb_page->m_page == (GtkNotebookPage*)child->data)
251 break;
252 page_num++;
253 child = child->next;
254 };
255
256 if (!child) return -1;
257
258 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
259
260 return selOld;
261 };
262
263 void wxNotebook::AdvanceSelection(bool bForward)
264 {
265 int nSel = GetSelection(),
266 nMax = GetPageCount();
267
268 if ( bForward ) {
269 SetSelection(nSel == nMax ? 0 : nSel + 1);
270 }
271 else {
272 SetSelection(nSel == 0 ? nMax : nSel - 1);
273 }
274 }
275
276 void wxNotebook::SetImageList( wxImageList* imageList )
277 {
278 m_imageList = imageList;
279 };
280
281 bool wxNotebook::SetPageText( int page, const wxString &text )
282 {
283 wxNotebookPage* nb_page = GetNotebookPage(page);
284 if (!nb_page)
285 return FALSE;
286
287 nb_page->m_text = text;
288
289 return TRUE;
290 };
291
292 bool wxNotebook::SetPageImage( int page, int image )
293 {
294 wxNotebookPage* nb_page = GetNotebookPage(page);
295 if (!nb_page)
296 return FALSE;
297
298 nb_page->m_image = image;
299
300 return TRUE;
301 };
302
303 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
304 {
305 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
306 };
307
308 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
309 {
310 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
311 };
312
313 bool wxNotebook::DeleteAllPages()
314 {
315 wxNode *page_node = m_pages.First();
316 while (page_node)
317 {
318 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
319
320 DeletePage( page->m_id );
321
322 page_node = m_pages.First();
323 };
324
325 return TRUE;
326 };
327
328 bool wxNotebook::DeletePage( int page )
329 {
330 wxNotebookPage* nb_page = GetNotebookPage(page);
331 if (!nb_page) return FALSE;
332
333 int page_num = 0;
334 GList *child = GTK_NOTEBOOK(m_widget)->children;
335 while (child)
336 {
337 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
338 page_num++;
339 child = child->next;
340 };
341
342 wxASSERT( child );
343
344 delete nb_page->m_client;
345
346 // Amazingly, this is not necessary
347 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
348
349 m_pages.DeleteObject( nb_page );
350
351 return TRUE;
352 };
353
354 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
355 bool bSelect, int imageId)
356 {
357 // we've created the notebook page in AddChild(). Now we just have to set
358 // the caption for the page and set the others parameters.
359
360 // first, find the page
361 wxNotebookPage *page = NULL;
362
363 wxNode *node = m_pages.First();
364 while (node)
365 {
366 page = (wxNotebookPage*)node->Data();
367 if ( page->m_client == win )
368 break; // found
369 node = node->Next();
370 };
371
372 wxCHECK_MSG(page != NULL, FALSE,
373 "Can't add a page whose parent is not the notebook!");
374
375 // then set the attributes
376 page->m_text = text;
377 if ( page->m_text.IsEmpty() )
378 page->m_text = "";
379 page->m_image = imageId;
380 gtk_label_set(page->m_label, page->m_text);
381
382 if ( bSelect ) {
383 SetSelection(GetPageCount());
384 }
385
386 return TRUE;
387 };
388
389 wxWindow *wxNotebook::GetPage( int page ) const
390 {
391 wxNotebookPage* nb_page = GetNotebookPage(page);
392 if (!nb_page)
393 return NULL;
394 else
395 return nb_page->m_client;
396 };
397
398 void wxNotebook::AddChild( wxWindow *win )
399 {
400 // @@@ normally done in wxWindow::AddChild but for some reason wxNotebook
401 // case is special there (Robert?)
402 // Robert: Don't you think the code below looks different from the one
403 // in wxWindow::AddChild :-)
404
405 m_children.Append(win);
406
407 wxNotebookPage *page = new wxNotebookPage();
408
409 page->m_id = GetPageCount();
410 page->m_label = (GtkLabel *)gtk_label_new("Handle");
411 page->m_client = win;
412 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget,
413 (GtkWidget *)page->m_label);
414 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
415
416 page->m_page =
417 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
418
419 page->m_parent = GTK_NOTEBOOK(m_widget);
420
421 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
422 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
423
424 if (!page->m_page)
425 {
426 wxLogFatalError( "Notebook page creation error" );
427 return;
428 }
429
430 m_pages.Append( page );
431 };
432
433 void wxNotebook::OnSize(wxSizeEvent& event)
434 {
435 // forward this event to all pages
436 wxNode *node = m_pages.First();
437 while (node)
438 {
439 wxWindow *page = ((wxNotebookPage*)node->Data())->m_client;
440 // @@@@ the numbers I substract here are completely arbitrary, instead we
441 // should somehow calculate the size of the page from the size of the
442 // notebook
443 /* page->SetSize(event.GetSize().GetX() - 5,
444 event.GetSize().GetY() - 30);
445
446 if ( page->GetAutoLayout() )
447 page->Layout();
448 */
449 node = node->Next();
450 };
451 }
452
453 // override these 2 functions to do nothing: everything is done in OnSize
454 void wxNotebook::SetConstraintSizes(bool /* recurse */)
455 {
456 // don't set the sizes of the pages - their correct size is not yet known
457 wxControl::SetConstraintSizes(FALSE);
458 }
459
460 bool wxNotebook::DoPhase(int /* nPhase */)
461 {
462 return TRUE;
463 }
464
465 //-----------------------------------------------------------------------------
466 // wxNotebookEvent
467 //-----------------------------------------------------------------------------
468
469 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)