mdi private menus
[wxWidgets.git] / src / gtk / 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
157 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
158
159 m_idHandler = gtk_signal_connect
160 (
161 GTK_OBJECT(m_widget), "switch_page",
162 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
163 (gpointer)this
164 );
165
166 PostCreation();
167
168 Show( TRUE );
169
170 return TRUE;
171 };
172
173 int wxNotebook::GetSelection() const
174 {
175 if (m_pages.Number() == 0)
176 return -1;
177
178 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
179
180 wxNotebookPage *page = NULL;
181
182 wxNode *node = m_pages.First();
183 while (node)
184 {
185 page = (wxNotebookPage*)node->Data();
186 if (page->m_page == g_page)
187 break;
188 node = node->Next();
189 };
190
191 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?");
192
193 return page->m_id;
194 };
195
196 int wxNotebook::GetPageCount() const
197 {
198 return m_pages.Number();
199 };
200
201 int wxNotebook::GetRowCount() const
202 {
203 return 1;
204 };
205
206 wxString wxNotebook::GetPageText( int page ) const
207 {
208 wxNotebookPage* nb_page = GetNotebookPage(page);
209 if (nb_page)
210 return nb_page->m_text;
211 else
212 return "";
213 };
214
215 int wxNotebook::GetPageImage( int page ) const
216 {
217 wxNotebookPage* nb_page = GetNotebookPage(page);
218 if (nb_page)
219 return nb_page->m_image;
220 else
221 return 0;
222 };
223
224 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
225 {
226 wxNotebookPage *nb_page = NULL;
227
228 wxNode *node = m_pages.First();
229 while (node)
230 {
231 nb_page = (wxNotebookPage*)node->Data();
232 if (nb_page->m_id == page)
233 return nb_page;
234 node = node->Next();
235 };
236
237 wxLogDebug( "Notebook page %d not found!", page );
238
239 return NULL;
240 };
241
242 int wxNotebook::SetSelection( int page )
243 {
244 int selOld = GetSelection();
245 wxNotebookPage* nb_page = GetNotebookPage(page);
246 if (!nb_page)
247 return -1;
248
249 int page_num = 0;
250 GList *child = GTK_NOTEBOOK(m_widget)->children;
251 while (child)
252 {
253 if (nb_page->m_page == (GtkNotebookPage*)child->data)
254 break;
255 page_num++;
256 child = child->next;
257 };
258
259 if (!child) return -1;
260
261 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
262
263 return selOld;
264 };
265
266 void wxNotebook::AdvanceSelection(bool bForward)
267 {
268 int nSel = GetSelection(),
269 nMax = GetPageCount();
270
271 if ( bForward ) {
272 SetSelection(nSel == nMax ? 0 : nSel + 1);
273 }
274 else {
275 SetSelection(nSel == 0 ? nMax : nSel - 1);
276 }
277 }
278
279 void wxNotebook::SetImageList( wxImageList* imageList )
280 {
281 m_imageList = imageList;
282 };
283
284 bool wxNotebook::SetPageText( int page, const wxString &text )
285 {
286 wxNotebookPage* nb_page = GetNotebookPage(page);
287 if (!nb_page)
288 return FALSE;
289
290 nb_page->m_text = text;
291
292 return TRUE;
293 };
294
295 bool wxNotebook::SetPageImage( int page, int image )
296 {
297 wxNotebookPage* nb_page = GetNotebookPage(page);
298 if (!nb_page)
299 return FALSE;
300
301 nb_page->m_image = image;
302
303 return TRUE;
304 };
305
306 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
307 {
308 wxFAIL_MSG("wxNotebook::SetPageSize not implemented");
309 };
310
311 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
312 {
313 wxFAIL_MSG("wxNotebook::SetPadding not implemented");
314 };
315
316 bool wxNotebook::DeleteAllPages()
317 {
318 wxNode *page_node = m_pages.First();
319 while (page_node)
320 {
321 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
322
323 DeletePage( page->m_id );
324
325 page_node = m_pages.First();
326 };
327
328 return TRUE;
329 };
330
331 bool wxNotebook::DeletePage( int page )
332 {
333 wxNotebookPage* nb_page = GetNotebookPage(page);
334 if (!nb_page) return FALSE;
335
336 int page_num = 0;
337 GList *child = GTK_NOTEBOOK(m_widget)->children;
338 while (child)
339 {
340 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
341 page_num++;
342 child = child->next;
343 };
344
345 wxASSERT( child );
346
347 delete nb_page->m_client;
348
349 // Amazingly, this is not necessary
350 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
351
352 m_pages.DeleteObject( nb_page );
353
354 return TRUE;
355 };
356
357 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
358 bool bSelect, int imageId)
359 {
360 // we've created the notebook page in AddChild(). Now we just have to set
361 // the caption for the page and set the others parameters.
362
363 // first, find the page
364 wxNotebookPage *page = NULL;
365
366 wxNode *node = m_pages.First();
367 while (node)
368 {
369 page = (wxNotebookPage*)node->Data();
370 if ( page->m_client == win )
371 break; // found
372 node = node->Next();
373 };
374
375 wxCHECK_MSG(page != NULL, FALSE,
376 "Can't add a page whose parent is not the notebook!");
377
378 // then set the attributes
379 page->m_text = text;
380 if ( page->m_text.IsEmpty() )
381 page->m_text = "";
382 page->m_image = imageId;
383 gtk_label_set(page->m_label, page->m_text);
384
385 if ( bSelect ) {
386 SetSelection(GetPageCount());
387 }
388
389 return TRUE;
390 };
391
392 wxWindow *wxNotebook::GetPage( int page ) const
393 {
394 wxNotebookPage* nb_page = GetNotebookPage(page);
395 if (!nb_page)
396 return NULL;
397 else
398 return nb_page->m_client;
399 };
400
401 void wxNotebook::AddChild( wxWindow *win )
402 {
403 // @@@ normally done in wxWindow::AddChild but for some reason wxNotebook
404 // case is special there (Robert?)
405 // Robert: Don't you think the code below looks different from the one
406 // in wxWindow::AddChild :-)
407
408 m_children.Append(win);
409
410 wxNotebookPage *page = new wxNotebookPage();
411
412 page->m_id = GetPageCount();
413 page->m_label = (GtkLabel *)gtk_label_new("Handle");
414 page->m_client = win;
415 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget,
416 (GtkWidget *)page->m_label);
417 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
418
419 page->m_page =
420 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
421
422 page->m_parent = GTK_NOTEBOOK(m_widget);
423
424 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
425 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
426
427 if (!page->m_page)
428 {
429 wxLogFatalError( "Notebook page creation error" );
430 return;
431 }
432
433 m_pages.Append( page );
434 };
435
436 void wxNotebook::OnSize(wxSizeEvent& event)
437 {
438 // forward this event to all pages
439 wxNode *node = m_pages.First();
440 while (node)
441 {
442 wxWindow *page = ((wxNotebookPage*)node->Data())->m_client;
443 // @@@@ the numbers I substract here are completely arbitrary, instead we
444 // should somehow calculate the size of the page from the size of the
445 // notebook
446 /* page->SetSize(event.GetSize().GetX() - 5,
447 event.GetSize().GetY() - 30);
448
449 if ( page->GetAutoLayout() )
450 page->Layout();
451 */
452 node = node->Next();
453 };
454 }
455
456 // override these 2 functions to do nothing: everything is done in OnSize
457 void wxNotebook::SetConstraintSizes(bool /* recurse */)
458 {
459 // don't set the sizes of the pages - their correct size is not yet known
460 wxControl::SetConstraintSizes(FALSE);
461 }
462
463 bool wxNotebook::DoPhase(int /* nPhase */)
464 {
465 return TRUE;
466 }
467
468 //-----------------------------------------------------------------------------
469 // wxNotebookEvent
470 //-----------------------------------------------------------------------------
471
472 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)