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