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