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