Refactor: use wxBookCtrlBase::m_selection in all derived classes.
[wxWidgets.git] / src / gtk / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_NOTEBOOK
14
15 #include "wx/notebook.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/log.h"
20 #include "wx/utils.h"
21 #include "wx/msgdlg.h"
22 #include "wx/bitmap.h"
23 #endif
24
25 #include "wx/imaglist.h"
26 #include "wx/fontutil.h"
27
28 #include "wx/gtk/private.h"
29
30 //-----------------------------------------------------------------------------
31 // wxGtkNotebookPage
32 //-----------------------------------------------------------------------------
33
34 // VZ: this is rather ugly as we keep the pages themselves in an array (it
35 // allows us to have quite a few functions implemented in the base class)
36 // but the page data is kept in a separate list, so we must maintain them
37 // in sync manually... of course, the list had been there before the base
38 // class which explains it but it still would be nice to do something
39 // about this one day
40
41 class wxGtkNotebookPage: public wxObject
42 {
43 public:
44 GtkWidget* m_box;
45 GtkWidget* m_label;
46 GtkWidget* m_image;
47 int m_imageIndex;
48 };
49
50
51 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST(wxGtkNotebookPagesList)
53
54 extern "C" {
55 static void event_after(GtkNotebook*, GdkEvent*, wxNotebook*);
56 }
57
58 //-----------------------------------------------------------------------------
59 // "switch_page"
60 //-----------------------------------------------------------------------------
61
62 extern "C" {
63 static void
64 switch_page_after(GtkNotebook* widget, GtkNotebookPage*, guint, wxNotebook* win)
65 {
66 g_signal_handlers_block_by_func(widget, (void*)switch_page_after, win);
67
68 win->GTKOnPageChanged();
69 }
70 }
71
72 extern "C" {
73 static void
74 switch_page(GtkNotebook* widget, GtkNotebookPage*, int page, wxNotebook* win)
75 {
76 win->m_oldSelection = gtk_notebook_get_current_page(widget);
77
78 if (win->SendPageChangingEvent(page))
79 // allow change, unblock handler for changed event
80 g_signal_handlers_unblock_by_func(widget, (void*)switch_page_after, win);
81 else
82 // change vetoed, unblock handler to set selection back
83 g_signal_handlers_unblock_by_func(widget, (void*)event_after, win);
84 }
85 }
86
87 //-----------------------------------------------------------------------------
88 // "event_after" from m_widget
89 //-----------------------------------------------------------------------------
90
91 extern "C" {
92 static void event_after(GtkNotebook* widget, GdkEvent*, wxNotebook* win)
93 {
94 g_signal_handlers_block_by_func(widget, (void*)event_after, win);
95 g_signal_handlers_block_by_func(widget, (void*)switch_page, win);
96
97 // restore previous selection
98 gtk_notebook_set_current_page(widget, win->m_oldSelection);
99
100 g_signal_handlers_unblock_by_func(widget, (void*)switch_page, win);
101 }
102 }
103
104 //-----------------------------------------------------------------------------
105 // InsertChild callback for wxNotebook
106 //-----------------------------------------------------------------------------
107
108 void wxNotebook::AddChildGTK(wxWindowGTK* child)
109 {
110 // Hack Alert! (Part I): This sets the notebook as the parent of the child
111 // widget, and takes care of some details such as updating the state and
112 // style of the child to reflect its new location. We do this early
113 // because without it GetBestSize (which is used to set the initial size
114 // of controls if an explicit size is not given) will often report
115 // incorrect sizes since the widget's style context is not fully known.
116 // See bug #901694 for details
117 // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863)
118 gtk_widget_set_parent(child->m_widget, m_widget);
119
120 // NOTE: This should be considered a temporary workaround until we can
121 // work out the details and implement delaying the setting of the initial
122 // size of widgets until the size is really needed.
123 }
124
125 //-----------------------------------------------------------------------------
126 // wxNotebook
127 //-----------------------------------------------------------------------------
128
129 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxBookCtrlBase)
130
131 BEGIN_EVENT_TABLE(wxNotebook, wxBookCtrlBase)
132 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
133 END_EVENT_TABLE()
134
135 void wxNotebook::Init()
136 {
137 m_padding = 0;
138 m_oldSelection = -1;
139 m_themeEnabled = true;
140 }
141
142 wxNotebook::wxNotebook()
143 {
144 Init();
145 }
146
147 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
148 const wxPoint& pos, const wxSize& size,
149 long style, const wxString& name )
150 {
151 Init();
152 Create( parent, id, pos, size, style, name );
153 }
154
155 wxNotebook::~wxNotebook()
156 {
157 DeleteAllPages();
158 }
159
160 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
161 const wxPoint& pos, const wxSize& size,
162 long style, const wxString& name )
163 {
164 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
165 style |= wxBK_TOP;
166
167 if (!PreCreation( parent, pos, size ) ||
168 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
169 {
170 wxFAIL_MSG( wxT("wxNoteBook creation failed") );
171 return false;
172 }
173
174
175 m_widget = gtk_notebook_new();
176 g_object_ref(m_widget);
177
178 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
179
180 g_signal_connect (m_widget, "switch_page",
181 G_CALLBACK(switch_page), this);
182
183 g_signal_connect_after (m_widget, "switch_page",
184 G_CALLBACK(switch_page_after), this);
185 g_signal_handlers_block_by_func(m_widget, (void*)switch_page_after, this);
186
187 g_signal_connect(m_widget, "event_after", G_CALLBACK(event_after), this);
188 g_signal_handlers_block_by_func(m_widget, (void*)event_after, this);
189
190 m_parent->DoAddChild( this );
191
192 if (m_windowStyle & wxBK_RIGHT)
193 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT );
194 if (m_windowStyle & wxBK_LEFT)
195 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT );
196 if (m_windowStyle & wxBK_BOTTOM)
197 gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM );
198
199 PostCreation(size);
200
201 return true;
202 }
203
204 int wxNotebook::GetSelection() const
205 {
206 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid notebook") );
207
208 return gtk_notebook_get_current_page( GTK_NOTEBOOK(m_widget) );
209 }
210
211 wxString wxNotebook::GetPageText( size_t page ) const
212 {
213 wxCHECK_MSG(page < GetPageCount(), wxEmptyString, "invalid notebook index");
214
215 GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
216 return wxGTK_CONV_BACK(gtk_label_get_text(label));
217 }
218
219 int wxNotebook::GetPageImage( size_t page ) const
220 {
221 wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
222
223 return GetNotebookPage(page)->m_imageIndex;
224 }
225
226 wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const
227 {
228 return m_pagesData.Item(page)->GetData();
229 }
230
231 int wxNotebook::DoSetSelection( size_t page, int flags )
232 {
233 wxCHECK_MSG(page < GetPageCount(), wxNOT_FOUND, "invalid notebook index");
234
235 int selOld = GetSelection();
236
237 if ( !(flags & SetSelection_SendEvent) )
238 {
239 g_signal_handlers_block_by_func(m_widget, (void*)switch_page, this);
240 }
241
242 gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget), page );
243
244 if ( !(flags & SetSelection_SendEvent) )
245 {
246 g_signal_handlers_unblock_by_func(m_widget, (void*)switch_page, this);
247 }
248
249 m_selection = page;
250
251 wxNotebookPage *client = GetPage(page);
252 if ( client )
253 client->SetFocus();
254
255 return selOld;
256 }
257
258 void wxNotebook::GTKOnPageChanged()
259 {
260 m_selection = gtk_notebook_get_current_page(GTK_NOTEBOOK(m_widget));
261
262 SendPageChangedEvent(m_oldSelection);
263 }
264
265 bool wxNotebook::SetPageText( size_t page, const wxString &text )
266 {
267 wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
268
269 GtkLabel* label = GTK_LABEL(GetNotebookPage(page)->m_label);
270 gtk_label_set_text(label, wxGTK_CONV(text));
271
272 return true;
273 }
274
275 bool wxNotebook::SetPageImage( size_t page, int image )
276 {
277 wxCHECK_MSG(page < GetPageCount(), false, "invalid notebook index");
278
279 wxGtkNotebookPage* pageData = GetNotebookPage(page);
280 if (image >= 0)
281 {
282 wxCHECK_MSG(m_imageList, false, "invalid notebook imagelist");
283 const wxBitmap* bitmap = m_imageList->GetBitmapPtr(image);
284 if (bitmap == NULL)
285 return false;
286 if (pageData->m_image)
287 {
288 gtk_image_set_from_pixbuf(
289 GTK_IMAGE(pageData->m_image), bitmap->GetPixbuf());
290 }
291 else
292 {
293 pageData->m_image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
294 gtk_widget_show(pageData->m_image);
295 gtk_box_pack_start(GTK_BOX(pageData->m_box),
296 pageData->m_image, false, false, m_padding);
297 }
298 }
299 else if (pageData->m_image)
300 {
301 gtk_widget_destroy(pageData->m_image);
302 pageData->m_image = NULL;
303 }
304 pageData->m_imageIndex = image;
305
306 return true;
307 }
308
309 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
310 {
311 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
312 }
313
314 void wxNotebook::SetPadding( const wxSize &padding )
315 {
316 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
317
318 m_padding = padding.GetWidth();
319
320 for (size_t i = GetPageCount(); i--;)
321 {
322 wxGtkNotebookPage* pageData = GetNotebookPage(i);
323 if (pageData->m_image)
324 {
325 gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
326 pageData->m_image, false, false, m_padding, GTK_PACK_START);
327 }
328 gtk_box_set_child_packing(GTK_BOX(pageData->m_box),
329 pageData->m_label, false, false, m_padding, GTK_PACK_END);
330 }
331 }
332
333 void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
334 {
335 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
336 }
337
338 bool wxNotebook::DeleteAllPages()
339 {
340 for (size_t i = GetPageCount(); i--;)
341 DeletePage(i);
342
343 return wxNotebookBase::DeleteAllPages();
344 }
345
346 wxNotebookPage *wxNotebook::DoRemovePage( size_t page )
347 {
348 // We cannot remove the page yet, as GTK sends the "switch_page"
349 // signal before it has removed the notebook-page from its
350 // corresponding list. Thus, if we were to remove the page from
351 // m_pages at this point, the two lists of pages would be out
352 // of sync during the PAGE_CHANGING/PAGE_CHANGED events.
353 wxNotebookPage *client = GetPage(page);
354 if ( !client )
355 return NULL;
356
357 gtk_widget_unrealize( client->m_widget );
358
359 // we don't need to unparent the client->m_widget; GTK+ will do
360 // that for us (and will throw a warning if we do it!)
361 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
362
363 // It's safe to remove the page now.
364 wxASSERT_MSG(GetPage(page) == client, wxT("pages changed during delete"));
365 wxNotebookBase::DoRemovePage(page);
366
367 wxGtkNotebookPage* p = GetNotebookPage(page);
368 m_pagesData.DeleteObject(p);
369 delete p;
370
371 return client;
372 }
373
374 bool wxNotebook::InsertPage( size_t position,
375 wxNotebookPage* win,
376 const wxString& text,
377 bool select,
378 int imageId )
379 {
380 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid notebook") );
381
382 wxCHECK_MSG( win->GetParent() == this, false,
383 wxT("Can't add a page whose parent is not the notebook!") );
384
385 wxCHECK_MSG( position <= GetPageCount(), false,
386 wxT("invalid page index in wxNotebookPage::InsertPage()") );
387
388 // Hack Alert! (Part II): See above in wxNotebook::AddChildGTK
389 // why this has to be done.
390 gtk_widget_unparent(win->m_widget);
391
392 if (m_themeEnabled)
393 win->SetThemeEnabled(true);
394
395 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
396
397 wxGtkNotebookPage* pageData = new wxGtkNotebookPage;
398
399 m_pages.Insert(win, position);
400 m_pagesData.Insert(position, pageData);
401
402 // set the label image and text
403 // this must be done before adding the page, as GetPageText
404 // and GetPageImage will otherwise return wrong values in
405 // the page-changed event that results from inserting the
406 // first page.
407 pageData->m_imageIndex = imageId;
408
409 pageData->m_box = gtk_hbox_new(false, 1);
410 gtk_container_set_border_width(GTK_CONTAINER(pageData->m_box), 2);
411
412 pageData->m_image = NULL;
413 if (imageId != -1)
414 {
415 if (m_imageList)
416 {
417 const wxBitmap* bitmap = m_imageList->GetBitmapPtr(imageId);
418 pageData->m_image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
419 gtk_box_pack_start(GTK_BOX(pageData->m_box),
420 pageData->m_image, false, false, m_padding);
421 }
422 else
423 wxFAIL_MSG("invalid notebook imagelist");
424 }
425
426 /* set the label text */
427 pageData->m_label = gtk_label_new(wxGTK_CONV(wxStripMenuCodes(text)));
428 gtk_box_pack_end(GTK_BOX(pageData->m_box),
429 pageData->m_label, false, false, m_padding);
430
431 gtk_widget_show_all(pageData->m_box);
432 gtk_notebook_insert_page(notebook, win->m_widget, pageData->m_box, position);
433
434 /* apply current style */
435 GtkRcStyle *style = GTKCreateWidgetStyle();
436 if ( style )
437 {
438 gtk_widget_modify_style(pageData->m_label, style);
439 gtk_rc_style_unref(style);
440 }
441
442 if (select && GetPageCount() > 1)
443 {
444 SetSelection( position );
445 }
446
447 InvalidateBestSize();
448 return true;
449 }
450
451 // helper for HitTest(): check if the point lies inside the given widget which
452 // is the child of the notebook whose position and border size are passed as
453 // parameters
454 static bool
455 IsPointInsideWidget(const wxPoint& pt, GtkWidget *w,
456 gint x, gint y, gint border = 0)
457 {
458 return
459 (pt.x >= w->allocation.x - x - border) &&
460 (pt.x <= w->allocation.x - x + border + w->allocation.width) &&
461 (pt.y >= w->allocation.y - y - border) &&
462 (pt.y <= w->allocation.y - y + border + w->allocation.height);
463 }
464
465 int wxNotebook::HitTest(const wxPoint& pt, long *flags) const
466 {
467 const gint x = m_widget->allocation.x;
468 const gint y = m_widget->allocation.y;
469
470 const size_t count = GetPageCount();
471 size_t i = 0;
472
473 GtkNotebook * notebook = GTK_NOTEBOOK(m_widget);
474 if (gtk_notebook_get_scrollable(notebook))
475 i = g_list_position( notebook->children, notebook->first_tab );
476
477 for ( ; i < count; i++ )
478 {
479 wxGtkNotebookPage* pageData = GetNotebookPage(i);
480 GtkWidget* box = pageData->m_box;
481
482 const gint border = gtk_container_get_border_width(GTK_CONTAINER(box));
483
484 if ( IsPointInsideWidget(pt, box, x, y, border) )
485 {
486 // ok, we're inside this tab -- now find out where, if needed
487 if ( flags )
488 {
489 if (pageData->m_image && IsPointInsideWidget(pt, pageData->m_image, x, y))
490 {
491 *flags = wxBK_HITTEST_ONICON;
492 }
493 else if (IsPointInsideWidget(pt, pageData->m_label, x, y))
494 {
495 *flags = wxBK_HITTEST_ONLABEL;
496 }
497 else
498 {
499 *flags = wxBK_HITTEST_ONITEM;
500 }
501 }
502
503 return i;
504 }
505 }
506
507 if ( flags )
508 {
509 *flags = wxBK_HITTEST_NOWHERE;
510 wxWindowBase * page = GetCurrentPage();
511 if ( page )
512 {
513 // rect origin is in notebook's parent coordinates
514 wxRect rect = page->GetRect();
515
516 // adjust it to the notebook's coordinates
517 wxPoint pos = GetPosition();
518 rect.x -= pos.x;
519 rect.y -= pos.y;
520 if ( rect.Contains( pt ) )
521 *flags |= wxBK_HITTEST_ONPAGE;
522 }
523 }
524
525 return wxNOT_FOUND;
526 }
527
528 void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
529 {
530 if (event.IsWindowChange())
531 AdvanceSelection( event.GetDirection() );
532 else
533 event.Skip();
534 }
535
536 #if wxUSE_CONSTRAINTS
537
538 // override these 2 functions to do nothing: everything is done in OnSize
539 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
540 {
541 // don't set the sizes of the pages - their correct size is not yet known
542 wxControl::SetConstraintSizes(false);
543 }
544
545 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
546 {
547 return true;
548 }
549
550 #endif
551
552 void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style)
553 {
554 gtk_widget_modify_style(m_widget, style);
555 for (size_t i = GetPageCount(); i--;)
556 gtk_widget_modify_style(GetNotebookPage(i)->m_label, style);
557 }
558
559 GdkWindow *wxNotebook::GTKGetWindow(wxArrayGdkWindows& windows) const
560 {
561 windows.push_back(m_widget->window);
562 windows.push_back(GTK_NOTEBOOK(m_widget)->event_window);
563
564 return NULL;
565 }
566
567 // static
568 wxVisualAttributes
569 wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
570 {
571 return GetDefaultAttributesFromGTKWidget(gtk_notebook_new);
572 }
573
574 #endif