]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/notebook.cpp
added stubs for wxTreeCtrl functions not yet implemented under MSW
[wxWidgets.git] / src / gtk / 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 #include "gdk/gdkkeysyms.h"
21
22 //-----------------------------------------------------------------------------
23 // data
24 //-----------------------------------------------------------------------------
25
26 extern bool g_blockEventsOnDrag;
27
28 //-----------------------------------------------------------------------------
29 // wxNotebookPage
30 //-----------------------------------------------------------------------------
31
32 class wxNotebookPage: public wxObject
33 {
34 public:
35 wxNotebookPage()
36 {
37 m_id = -1;
38 m_text = "";
39 m_image = -1;
40 m_page = (GtkNotebookPage *) NULL;
41 m_client = (wxWindow *) NULL;
42 m_parent = (GtkNotebook *) NULL;
43 m_box = (GtkWidget *) NULL;
44 m_added = FALSE;
45 }
46
47 // mark page as "added' to the notebook, return FALSE if the page was
48 // already added
49 bool Add()
50 {
51 if ( WasAdded() )
52 return FALSE;
53
54 m_added = TRUE;
55 return TRUE;
56 }
57
58 bool WasAdded() const { return m_added; }
59
60 int m_id;
61 wxString m_text;
62 int m_image;
63 GtkNotebookPage *m_page;
64 GtkLabel *m_label;
65 wxWindow *m_client;
66 GtkNotebook *m_parent;
67 GtkWidget *m_box; // in which the label and image are packed
68
69 private:
70 bool m_added;
71 };
72
73 //-----------------------------------------------------------------------------
74 // "switch_page"
75 //-----------------------------------------------------------------------------
76
77 static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
78 GtkNotebookPage *WXUNUSED(page),
79 gint nPage,
80 gpointer data)
81 {
82 wxNotebook *notebook = (wxNotebook *)data;
83
84 int old = notebook->GetSelection();
85
86 // TODO: emulate PAGE_CHANGING event
87
88 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
89 notebook->GetId(), nPage, old );
90 event.SetEventObject( notebook );
91 notebook->GetEventHandler()->ProcessEvent( event );
92 }
93
94 //-----------------------------------------------------------------------------
95 // "size_allocate"
96 //-----------------------------------------------------------------------------
97
98 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
99 {
100 if ((win->m_x == alloc->x) &&
101 (win->m_y == alloc->y) &&
102 (win->m_width == alloc->width) &&
103 (win->m_height == alloc->height))
104 {
105 return;
106 }
107
108 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
109
110 if (win->GetAutoLayout()) win->Layout();
111 }
112
113 //-----------------------------------------------------------------------------
114 // "key_press_event"
115 //-----------------------------------------------------------------------------
116
117 static gint
118 gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *notebook )
119 {
120 if (g_blockEventsOnDrag) return FALSE;
121
122 if (!notebook->HasVMT()) return FALSE;
123
124 if (gdk_event->keyval != GDK_Down) return FALSE;
125
126 if (notebook != notebook->FindFocus()) return FALSE;
127
128 if (notebook->m_pages.GetCount() == 0) return FALSE;
129
130 wxNode *node = notebook->m_pages.Nth( notebook->GetSelection() );
131
132 if (!node) return FALSE;
133
134 wxNotebookPage *page = (wxNotebookPage*) node->Data();
135
136 // don't let others the key event
137 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
138
139 page->m_client->SetFocus();
140
141 return TRUE;
142 }
143
144 //-----------------------------------------------------------------------------
145 // InsertChild callback for wxNotebook
146 //-----------------------------------------------------------------------------
147
148 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
149 {
150 wxNotebookPage *page = new wxNotebookPage();
151
152 page->m_id = parent->GetPageCount();
153
154 page->m_box = gtk_hbox_new (FALSE, 0);
155 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
156
157 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
158
159 page->m_client = child;
160 gtk_notebook_append_page( notebook, child->m_widget, page->m_box );
161
162 page->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
163
164 page->m_parent = notebook;
165
166 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
167 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
168
169 wxASSERT_MSG( page->m_page, "Notebook page creation error" );
170
171 parent->m_pages.Append( page );
172 }
173
174 //-----------------------------------------------------------------------------
175 // wxNotebook
176 //-----------------------------------------------------------------------------
177
178 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
179
180 void wxNotebook::Init()
181 {
182 m_imageList = (wxImageList *) NULL;
183 m_pages.DeleteContents( TRUE );
184 m_idHandler = 0;
185 }
186
187 wxNotebook::wxNotebook()
188 {
189 Init();
190 }
191
192 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
193 const wxPoint& pos, const wxSize& size,
194 long style, const wxString& name )
195 {
196 Init();
197 Create( parent, id, pos, size, style, name );
198 }
199
200 wxNotebook::~wxNotebook()
201 {
202 // don't generate change page events any more
203 if (m_idHandler != 0)
204 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
205
206 DeleteAllPages();
207 }
208
209 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
210 const wxPoint& pos, const wxSize& size,
211 long style, const wxString& name )
212 {
213 m_needParent = TRUE;
214 m_acceptsFocus = TRUE;
215 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
216
217 PreCreation( parent, id, pos, size, style, name );
218
219 m_widget = gtk_notebook_new();
220
221 #ifdef __WXDEBUG__
222 debug_focus_in( m_widget, "wxNotebook::m_widget", name );
223 #endif
224
225 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
226
227 m_idHandler = gtk_signal_connect (
228 GTK_OBJECT(m_widget), "switch_page",
229 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
230 (gpointer)this );
231
232 m_parent->AddChild( this );
233
234 (m_parent->m_insertCallback)( m_parent, this );
235
236 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
237 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
238
239 PostCreation();
240
241 Show( TRUE );
242
243 return TRUE;
244 }
245
246 int wxNotebook::GetSelection() const
247 {
248 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
249
250 if (m_pages.Number() == 0) return -1;
251
252 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
253 if (!g_page) return -1;
254
255 wxNotebookPage *page = (wxNotebookPage *) NULL;
256
257 wxNode *node = m_pages.First();
258 while (node)
259 {
260 page = (wxNotebookPage*)node->Data();
261
262 if ((page->m_page == g_page) || (page->m_page == (GtkNotebookPage*)NULL))
263 {
264 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
265 // "switch_page" then and we ask for GetSelection() in the handler for
266 // "switch_page". otherwise m_page should never be NULL. all this
267 // might also be wrong.
268 break;
269 }
270 node = node->Next();
271 }
272
273 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
274
275 return page->m_id;
276 }
277
278 int wxNotebook::GetPageCount() const
279 {
280 // count only the pages which were already added to the notebook for MSW
281 // compatibility (and, in fact, this behaviour makes more sense anyhow
282 // because only the added pages are shown)
283
284 int n = 0;
285 for ( wxNode *node = m_pages.First(); node; node = node->Next() )
286 {
287 wxNotebookPage *page = (wxNotebookPage*)node->Data();
288
289 if (page->WasAdded()) n++;
290 }
291
292 return n;
293 }
294
295 int wxNotebook::GetRowCount() const
296 {
297 return 1;
298 }
299
300 wxString wxNotebook::GetPageText( int page ) const
301 {
302 wxCHECK_MSG( m_widget != NULL, "", "invalid notebook" );
303
304 wxNotebookPage* nb_page = GetNotebookPage(page);
305 if (nb_page)
306 return nb_page->m_text;
307 else
308 return "";
309 }
310
311 int wxNotebook::GetPageImage( int page ) const
312 {
313 wxCHECK_MSG( m_widget != NULL, 0, "invalid notebook" );
314
315 wxNotebookPage* nb_page = GetNotebookPage(page);
316 if (nb_page)
317 return nb_page->m_image;
318 else
319 return 0;
320 }
321
322 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
323 {
324 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*)NULL, "invalid notebook" );
325
326 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
327
328 wxNode *node = m_pages.First();
329 while (node)
330 {
331 nb_page = (wxNotebookPage*)node->Data();
332 if (nb_page->m_id == page)
333 return nb_page;
334 node = node->Next();
335 }
336
337 wxFAIL_MSG( "Notebook page not found!" );
338
339 return (wxNotebookPage *) NULL;
340 }
341
342 int wxNotebook::SetSelection( int page )
343 {
344 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
345
346 int selOld = GetSelection();
347 wxNotebookPage* nb_page = GetNotebookPage(page);
348
349 if (!nb_page) return -1;
350
351 int page_num = 0;
352 GList *child = GTK_NOTEBOOK(m_widget)->children;
353 while (child)
354 {
355 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
356 page_num++;
357 child = child->next;
358 }
359
360 if (!child) return -1;
361
362 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
363
364 return selOld;
365 }
366
367 void wxNotebook::AdvanceSelection( bool bForward )
368 {
369 wxCHECK_RET( m_widget != NULL, "invalid notebook" );
370
371 int sel = GetSelection();
372 int max = GetPageCount();
373
374 if (bForward)
375 SetSelection( sel == max ? 0 : sel + 1 );
376 else
377 SetSelection( sel == 0 ? max : sel - 1 );
378 }
379
380 void wxNotebook::SetImageList( wxImageList* imageList )
381 {
382 m_imageList = imageList;
383 }
384
385 bool wxNotebook::SetPageText( int page, const wxString &text )
386 {
387 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
388
389 wxNotebookPage* nb_page = GetNotebookPage(page);
390
391 if (!nb_page) return FALSE;
392
393 nb_page->m_text = text;
394
395 return TRUE;
396 }
397
398 bool wxNotebook::SetPageImage( int page, int image )
399 {
400 wxNotebookPage* nb_page = GetNotebookPage(page);
401
402 if (!nb_page) return FALSE;
403
404 nb_page->m_image = image;
405
406 return TRUE;
407 }
408
409 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
410 {
411 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
412 }
413
414 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
415 {
416 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
417 }
418
419 bool wxNotebook::DeleteAllPages()
420 {
421 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
422
423 wxNode *page_node = m_pages.First();
424 while (page_node)
425 {
426 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
427
428 DeletePage( page->m_id );
429
430 page_node = m_pages.First();
431 }
432
433 return TRUE;
434 }
435
436 bool wxNotebook::DeletePage( int page )
437 {
438 wxNotebookPage* nb_page = GetNotebookPage(page);
439 if (!nb_page) return FALSE;
440
441 int page_num = 0;
442 GList *child = GTK_NOTEBOOK(m_widget)->children;
443 while (child)
444 {
445 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
446 page_num++;
447 child = child->next;
448 }
449
450 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
451
452 delete nb_page->m_client;
453
454 m_pages.DeleteObject( nb_page );
455
456 return TRUE;
457 }
458
459 bool wxNotebook::RemovePage( int page )
460 {
461 wxNotebookPage* nb_page = GetNotebookPage(page);
462 if (!nb_page) return FALSE;
463
464 int page_num = 0;
465 GList *child = GTK_NOTEBOOK(m_widget)->children;
466 while (child)
467 {
468 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
469 page_num++;
470 child = child->next;
471 }
472
473 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
474
475 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
476
477 m_pages.DeleteObject( nb_page );
478
479 return TRUE;
480 }
481
482 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
483 bool select, int imageId)
484 {
485 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
486
487 // we've created the notebook page in AddChild(). Now we just have to set
488 // the caption for the page and set the others parameters.
489
490 wxNotebookPage *page = (wxNotebookPage *) NULL;
491
492 wxNode *node = m_pages.First();
493 while (node)
494 {
495 page = (wxNotebookPage*)node->Data();
496 if ( page->m_client == win ) break;
497 node = node->Next();
498 }
499
500 wxCHECK_MSG( page != NULL, FALSE,
501 "Can't add a page whose parent is not the notebook!" );
502
503 wxCHECK_MSG( page->Add(), FALSE,
504 "Can't add the same page twice to a notebook." );
505
506 if (imageId != -1)
507 {
508 wxASSERT( m_imageList != NULL );
509
510 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
511 GdkPixmap *pixmap = bmp->GetPixmap();
512 GdkBitmap *mask = (GdkBitmap*) NULL;
513 if ( bmp->GetMask() )
514 {
515 mask = bmp->GetMask()->GetBitmap();
516 }
517
518 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
519
520 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
521
522 gtk_widget_show(pixmapwid);
523 }
524
525 // then set the attributes
526 page->m_text = text;
527 if (page->m_text.IsEmpty()) page->m_text = "";
528 page->m_image = imageId;
529 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
530 gtk_box_pack_start( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
531
532 // @@@: what does this do? do we still need it?
533 // gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
534
535 gtk_widget_show((GtkWidget *)page->m_label);
536
537 if (select) SetSelection( GetPageCount()-1 );
538
539 return TRUE;
540 }
541
542 wxWindow *wxNotebook::GetPage( int page ) const
543 {
544 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, "invalid notebook" );
545
546 wxNotebookPage* nb_page = GetNotebookPage(page);
547 if (!nb_page)
548 return (wxWindow *) NULL;
549 else
550 return nb_page->m_client;
551 }
552
553 // override these 2 functions to do nothing: everything is done in OnSize
554 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
555 {
556 // don't set the sizes of the pages - their correct size is not yet known
557 wxControl::SetConstraintSizes(FALSE);
558 }
559
560 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
561 {
562 return TRUE;
563 }
564
565 void wxNotebook::ApplyWidgetStyle()
566 {
567 SetWidgetStyle();
568 gtk_widget_set_style( m_widget, m_widgetStyle );
569 }
570
571 //-----------------------------------------------------------------------------
572 // wxNotebookEvent
573 //-----------------------------------------------------------------------------
574
575 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
576