Rewrote wxNotebook, incl. PAGE_CHANGING event
[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 #include "gdk/gdk.h"
22 #include "gtk/gtk.h"
23 #include "wx/gtk/win_gtk.h"
24 #include "gdk/gdkkeysyms.h"
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 //-----------------------------------------------------------------------------
34 // data
35 //-----------------------------------------------------------------------------
36
37 extern bool g_blockEventsOnDrag;
38
39 //-----------------------------------------------------------------------------
40 // debug
41 //-----------------------------------------------------------------------------
42
43 #ifdef __WXDEBUG__
44
45 extern void debug_focus_in( GtkWidget* widget, const wxChar* name, const wxChar *window );
46
47 #endif
48
49 //-----------------------------------------------------------------------------
50 // wxNotebookPage
51 //-----------------------------------------------------------------------------
52
53 class wxNotebookPage: public wxObject
54 {
55 public:
56 wxNotebookPage()
57 {
58 m_text = "";
59 m_image = -1;
60 m_page = (GtkNotebookPage *) NULL;
61 m_client = (wxWindow *) NULL;
62 m_box = (GtkWidget *) NULL;
63 }
64
65 wxString m_text;
66 int m_image;
67 GtkNotebookPage *m_page;
68 GtkLabel *m_label;
69 wxWindow *m_client;
70 GtkWidget *m_box; // in which the label and image are packed
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 page,
80 wxNotebook *notebook )
81 {
82 if (g_isIdle)
83 wxapp_install_idle_handler();
84
85 int old = notebook->GetSelection();
86
87 wxNotebookEvent event1( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING,
88 notebook->GetId(), page, old );
89 event1.SetEventObject( notebook );
90
91 if ((notebook->GetEventHandler()->ProcessEvent( event1 )) &&
92 !event1.IsAllowed() )
93 {
94 /* program doesn't allow the page change */
95 gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook->m_widget), "switch_page" );
96 return;
97 }
98
99 wxNotebookEvent event2( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
100 notebook->GetId(), page, old );
101 event2.SetEventObject( notebook );
102 notebook->GetEventHandler()->ProcessEvent( event2 );
103 }
104
105 //-----------------------------------------------------------------------------
106 // "size_allocate"
107 //-----------------------------------------------------------------------------
108
109 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
110 {
111 if (g_isIdle)
112 wxapp_install_idle_handler();
113
114 if ((win->m_x == alloc->x) &&
115 (win->m_y == alloc->y) &&
116 (win->m_width == alloc->width) &&
117 (win->m_height == alloc->height))
118 {
119 return;
120 }
121
122 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
123
124 if (win->GetAutoLayout()) win->Layout();
125 }
126
127 //-----------------------------------------------------------------------------
128 // "key_press_event"
129 //-----------------------------------------------------------------------------
130
131 static gint
132 gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *notebook )
133 {
134 if (g_isIdle) wxapp_install_idle_handler();
135
136 if (g_blockEventsOnDrag) return FALSE;
137
138 if (!notebook->m_hasVMT) return FALSE;
139
140 /* this code makes jumping down from the handles of the notebooks
141 to the actual items in the visible notebook page possible with
142 the down-arrow key */
143
144 if (gdk_event->keyval != GDK_Down) return FALSE;
145
146 if (notebook != notebook->FindFocus()) return FALSE;
147
148 if (notebook->m_pages.GetCount() == 0) return FALSE;
149
150 wxNode *node = notebook->m_pages.Nth( notebook->GetSelection() );
151
152 if (!node) return FALSE;
153
154 wxNotebookPage *page = (wxNotebookPage*) node->Data();
155
156 // don't let others the key event
157 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
158
159 page->m_client->SetFocus();
160
161 return TRUE;
162 }
163
164 //-----------------------------------------------------------------------------
165 // InsertChild callback for wxNotebook
166 //-----------------------------------------------------------------------------
167
168 static void wxInsertChildInNotebook( wxNotebook* WXUNUSED(parent), wxWindow* WXUNUSED(child) )
169 {
170 /* we don't do anything here but pray */
171 }
172
173 //-----------------------------------------------------------------------------
174 // wxNotebook
175 //-----------------------------------------------------------------------------
176
177 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
178
179 BEGIN_EVENT_TABLE(wxNotebook, wxControl)
180 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
181 END_EVENT_TABLE()
182
183 void wxNotebook::Init()
184 {
185 m_imageList = (wxImageList *) NULL;
186 m_pages.DeleteContents( TRUE );
187 }
188
189 wxNotebook::wxNotebook()
190 {
191 Init();
192 }
193
194 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
195 const wxPoint& pos, const wxSize& size,
196 long style, const wxString& name )
197 {
198 Init();
199 Create( parent, id, pos, size, style, name );
200 }
201
202 wxNotebook::~wxNotebook()
203 {
204 /* don't generate change page events any more */
205 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
206 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
207
208 DeleteAllPages();
209 }
210
211 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
212 const wxPoint& pos, const wxSize& size,
213 long style, const wxString& name )
214 {
215 m_needParent = TRUE;
216 m_acceptsFocus = TRUE;
217 m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook;
218
219 PreCreation( parent, id, pos, size, style, name );
220
221 m_widget = gtk_notebook_new();
222
223 #ifdef __WXDEBUG__
224 debug_focus_in( m_widget, _T("wxNotebook::m_widget"), name );
225 #endif
226
227 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
228
229 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
230 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
231
232 m_parent->DoAddChild( this );
233
234 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
235 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
236
237 PostCreation();
238
239 Show( TRUE );
240
241 return TRUE;
242 }
243
244 int wxNotebook::GetSelection() const
245 {
246 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid notebook") );
247
248 GList *pages = GTK_NOTEBOOK(m_widget)->children;
249
250 if (g_list_length(pages) == 0) return -1;
251
252 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
253
254 GtkNotebookPage *page = GTK_NOTEBOOK_PAGE( notebook->cur_page );
255
256 wxCHECK_MSG( page, -1, "no notebook page selected/current" );
257
258 return g_list_index( pages, (gpointer)page );
259 }
260
261 int wxNotebook::GetPageCount() const
262 {
263 return (int) g_list_length( GTK_NOTEBOOK(m_widget)->children );
264 }
265
266 int wxNotebook::GetRowCount() const
267 {
268 return 1;
269 }
270
271 wxString wxNotebook::GetPageText( int page ) const
272 {
273 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid notebook") );
274
275 wxNotebookPage* nb_page = GetNotebookPage(page);
276 if (nb_page)
277 return nb_page->m_text;
278 else
279 return "";
280 }
281
282 int wxNotebook::GetPageImage( int page ) const
283 {
284 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid notebook") );
285
286 wxNotebookPage* nb_page = GetNotebookPage(page);
287 if (nb_page)
288 return nb_page->m_image;
289 else
290 return -1;
291 }
292
293 wxNotebookPage* wxNotebook::GetNotebookPage( int page ) const
294 {
295 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*) NULL, _T("invalid notebook") );
296
297 wxCHECK_MSG( page < (int)m_pages.GetCount(), (wxNotebookPage*) NULL, _T("invalid notebook index") );
298
299 wxNode *node = m_pages.Nth( page );
300
301 return (wxNotebookPage *) node->Data();
302 }
303
304 int wxNotebook::SetSelection( int page )
305 {
306 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid notebook") );
307
308 wxCHECK_MSG( page < (int)m_pages.GetCount(), -1, _T("invalid notebook index") );
309
310 int selOld = GetSelection();
311
312 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page );
313
314 return selOld;
315 }
316
317 void wxNotebook::AdvanceSelection( bool forward )
318 {
319 wxCHECK_RET( m_widget != NULL, _T("invalid notebook") );
320
321 int sel = GetSelection();
322 int max = GetPageCount();
323
324 if (forward)
325 SetSelection( sel == max ? 0 : sel + 1 );
326 else
327 SetSelection( sel == 0 ? max-1 : sel - 1 );
328 }
329
330 void wxNotebook::SetImageList( wxImageList* imageList )
331 {
332 m_imageList = imageList;
333 }
334
335 bool wxNotebook::SetPageText( int page, const wxString &text )
336 {
337 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid notebook") );
338
339 wxNotebookPage* nb_page = GetNotebookPage(page);
340
341 wxCHECK_MSG( nb_page, FALSE, _T("SetPageText: invalid page index") );
342
343 nb_page->m_text = text;
344
345 gtk_label_set( nb_page->m_label, nb_page->m_text.mbc_str() );
346
347 return TRUE;
348 }
349
350 bool wxNotebook::SetPageImage( int page, int image )
351 {
352 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
353
354 wxNotebookPage* nb_page = GetNotebookPage(page);
355
356 if (!nb_page) return FALSE;
357
358 /* Optimization posibility: return immediately if image unchanged.
359 * Not enabled because it may break existing (stupid) code that
360 * manipulates the imagelist to cycle images */
361
362 /* if (image == nb_page->m_image) return TRUE; */
363
364 /* For different cases:
365 1) no image -> no image
366 2) image -> no image
367 3) no image -> image
368 4) image -> image */
369
370 if (image == -1 && nb_page->m_image == -1)
371 return TRUE; /* Case 1): Nothing to do. */
372
373 GtkWidget *pixmapwid = (GtkWidget*) NULL;
374
375 if (nb_page->m_image != -1)
376 {
377 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
378
379 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
380 while (child)
381 {
382 if (GTK_IS_PIXMAP(child->data))
383 {
384 pixmapwid = GTK_WIDGET(child->data);
385 break;
386 }
387 child = child->next;
388 }
389
390 /* We should have the pixmap widget now */
391 wxASSERT(pixmapwid != NULL);
392
393 if (image == -1)
394 {
395 /* If there's no new widget, just remove the old from the box */
396 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
397 nb_page->m_image = -1;
398
399 return TRUE; /* Case 2) */
400 }
401 }
402
403 /* Only cases 3) and 4) left */
404 wxASSERT( m_imageList != NULL ); /* Just in case */
405
406 /* Construct the new pixmap */
407 const wxBitmap *bmp = m_imageList->GetBitmap(image);
408 GdkPixmap *pixmap = bmp->GetPixmap();
409 GdkBitmap *mask = (GdkBitmap*) NULL;
410 if ( bmp->GetMask() )
411 {
412 mask = bmp->GetMask()->GetBitmap();
413 }
414
415 if (pixmapwid == NULL)
416 {
417 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
418 pixmapwid = gtk_pixmap_new (pixmap, mask );
419
420 /* CHECKME: Are these pack flags okay? */
421 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, 3);
422 gtk_widget_show(pixmapwid);
423 }
424 else
425 {
426 /* Case 4) Simply replace the pixmap */
427 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
428 }
429
430 nb_page->m_image = image;
431
432 return TRUE;
433 }
434
435 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
436 {
437 wxFAIL_MSG( _T("wxNotebook::SetPageSize not implemented") );
438 }
439
440 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
441 {
442 wxFAIL_MSG( _T("wxNotebook::SetPadding not implemented") );
443 }
444
445 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
446 {
447 wxFAIL_MSG( _T("wxNotebook::SetTabSize not implemented") );
448 }
449
450 bool wxNotebook::DeleteAllPages()
451 {
452 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid notebook") );
453
454 while (m_pages.GetCount() > 0)
455 DeletePage( m_pages.GetCount()-1 );
456
457 return TRUE;
458 }
459
460 bool wxNotebook::DeletePage( int page )
461 {
462 wxNotebookPage* nb_page = GetNotebookPage(page);
463 if (!nb_page) return FALSE;
464
465 nb_page->m_client->Destroy();
466
467 m_pages.DeleteObject( nb_page );
468
469 return TRUE;
470 }
471
472 bool wxNotebook::RemovePage( int page )
473 {
474 wxNotebookPage* nb_page = GetNotebookPage(page);
475
476 if (!nb_page) return FALSE;
477
478 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
479
480 m_pages.DeleteObject( nb_page );
481
482 return TRUE;
483 }
484
485 bool wxNotebook::InsertPage( int position, wxWindow* win, const wxString& text,
486 bool select, int imageId )
487 {
488 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid notebook") );
489
490 wxCHECK_MSG( win->GetParent() == this, FALSE,
491 _T("Can't add a page whose parent is not the notebook!") );
492
493 /* don't receive switch page during addition */
494 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
495 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
496
497 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
498
499 wxNotebookPage *page = new wxNotebookPage();
500
501 if (position < 0)
502 m_pages.Append( page );
503 else
504 m_pages.Insert( m_pages.Nth( position ), page );
505
506 page->m_client = win;
507
508 page->m_box = gtk_hbox_new( FALSE, 0 );
509 gtk_container_border_width( GTK_CONTAINER(page->m_box), 2 );
510
511 if (position < 0)
512 gtk_notebook_append_page( notebook, win->m_widget, page->m_box );
513 else
514 gtk_notebook_insert_page( notebook, win->m_widget, page->m_box, position );
515
516 page->m_page = GTK_NOTEBOOK_PAGE( g_list_last(notebook->children)->data );
517
518 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
519 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
520
521 /* set the label image */
522 page->m_image = imageId;
523
524 if (imageId != -1)
525 {
526 wxASSERT( m_imageList != NULL );
527
528 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
529 GdkPixmap *pixmap = bmp->GetPixmap();
530 GdkBitmap *mask = (GdkBitmap*) NULL;
531 if ( bmp->GetMask() )
532 {
533 mask = bmp->GetMask()->GetBitmap();
534 }
535
536 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
537
538 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
539
540 gtk_widget_show(pixmapwid);
541 }
542
543 /* set the label text */
544 page->m_text = text;
545 if (page->m_text.IsEmpty()) page->m_text = _T("");
546
547 page->m_label = GTK_LABEL( gtk_label_new(page->m_text.mbc_str()) );
548 gtk_box_pack_end( GTK_BOX(page->m_box), GTK_WIDGET(page->m_label), FALSE, FALSE, 3 );
549
550 /* show the label */
551 gtk_widget_show( GTK_WIDGET(page->m_label) );
552
553 if (select && (m_pages.GetCount() > 1))
554 {
555 if (position < 0)
556 SetSelection( GetPageCount()-1 );
557 else
558 SetSelection( position );
559 }
560
561 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
562 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
563
564 return TRUE;
565 }
566
567 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
568 bool select, int imageId)
569 {
570 return InsertPage( -1, win, text, select, imageId );
571 }
572
573 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
574 {
575 if (event.IsWindowChange())
576 AdvanceSelection( event.GetDirection() );
577 else
578 event.Skip();
579 }
580
581 wxWindow *wxNotebook::GetPage( int page ) const
582 {
583 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, _T("invalid notebook") );
584
585 wxNotebookPage* nb_page = GetNotebookPage(page);
586 if (!nb_page)
587 return (wxWindow *) NULL;
588 else
589 return nb_page->m_client;
590 }
591
592 // override these 2 functions to do nothing: everything is done in OnSize
593 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
594 {
595 // don't set the sizes of the pages - their correct size is not yet known
596 wxControl::SetConstraintSizes(FALSE);
597 }
598
599 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
600 {
601 return TRUE;
602 }
603
604 void wxNotebook::ApplyWidgetStyle()
605 {
606 SetWidgetStyle();
607 gtk_widget_set_style( m_widget, m_widgetStyle );
608 }
609
610 bool wxNotebook::IsOwnGtkWindow( GdkWindow *window )
611 {
612 return ((m_widget->window == window) ||
613 (GTK_NOTEBOOK(m_widget)->panel == window));
614 }
615
616 //-----------------------------------------------------------------------------
617 // wxNotebookEvent
618 //-----------------------------------------------------------------------------
619
620 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
621