]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/notebook.cpp
compilation fixes for Unix - IT DOES NOT WORK YET, DON'T TRY IT
[wxWidgets.git] / src / gtk1 / notebook.cpp
... / ...
CommitLineData
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
33extern void wxapp_install_idle_handler();
34extern bool g_isIdle;
35
36//-----------------------------------------------------------------------------
37// data
38//-----------------------------------------------------------------------------
39
40extern bool g_blockEventsOnDrag;
41
42//-----------------------------------------------------------------------------
43// debug
44//-----------------------------------------------------------------------------
45
46#ifdef __WXDEBUG__
47
48extern void debug_focus_in( GtkWidget* widget, const wxChar* name, const wxChar *window );
49
50#endif
51
52//-----------------------------------------------------------------------------
53// wxNotebookPage
54//-----------------------------------------------------------------------------
55
56class wxNotebookPage: public wxObject
57{
58public:
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
80static 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
112static 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
143static gint
144gtk_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
160static 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
169IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
170
171BEGIN_EVENT_TABLE(wxNotebook, wxControl)
172 EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey)
173END_EVENT_TABLE()
174
175void wxNotebook::Init()
176{
177 m_imageList = (wxImageList *) NULL;
178 m_pages.DeleteContents( TRUE );
179 m_lastSelection = -1;
180}
181
182wxNotebook::wxNotebook()
183{
184 Init();
185}
186
187wxNotebook::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
195wxNotebook::~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
204bool 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 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
243 GTK_SIGNAL_FUNC(gtk_notebook_realized_callback), (gpointer) this );
244
245 Show( TRUE );
246
247 return TRUE;
248}
249
250void wxNotebook::SetFocus()
251{
252 if (m_pages.GetCount() == 0) return;
253
254 wxNode *node = m_pages.Nth( GetSelection() );
255
256 if (!node) return;
257
258 wxNotebookPage *page = (wxNotebookPage*) node->Data();
259
260 page->m_client->SetFocus();
261}
262
263int wxNotebook::GetSelection() const
264{
265 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
266
267 GList *pages = GTK_NOTEBOOK(m_widget)->children;
268
269 if (g_list_length(pages) == 0) return -1;
270
271 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
272
273 if (notebook->cur_page == NULL) return m_lastSelection;
274
275 return g_list_index( pages, (gpointer)(notebook->cur_page) );
276}
277
278int wxNotebook::GetPageCount() const
279{
280 return (int) g_list_length( GTK_NOTEBOOK(m_widget)->children );
281}
282
283int wxNotebook::GetRowCount() const
284{
285 return 1;
286}
287
288wxString wxNotebook::GetPageText( int page ) const
289{
290 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") );
291
292 wxNotebookPage* nb_page = GetNotebookPage(page);
293 if (nb_page)
294 return nb_page->m_text;
295 else
296 return wxT("");
297}
298
299int wxNotebook::GetPageImage( int page ) const
300{
301 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
302
303 wxNotebookPage* nb_page = GetNotebookPage(page);
304 if (nb_page)
305 return nb_page->m_image;
306 else
307 return -1;
308}
309
310wxNotebookPage* wxNotebook::GetNotebookPage( int page ) const
311{
312 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*) NULL, wxT("invalid notebook") );
313
314 wxCHECK_MSG( page < (int)m_pages.GetCount(), (wxNotebookPage*) NULL, wxT("invalid notebook index") );
315
316 wxNode *node = m_pages.Nth( page );
317
318 return (wxNotebookPage *) node->Data();
319}
320
321int wxNotebook::SetSelection( int page )
322{
323 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") );
324
325 wxCHECK_MSG( page < (int)m_pages.GetCount(), -1, wxT("invalid notebook index") );
326
327 int selOld = GetSelection();
328
329 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page );
330
331 return selOld;
332}
333
334void wxNotebook::AdvanceSelection( bool forward )
335{
336 wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") );
337
338 int sel = GetSelection();
339 int max = GetPageCount();
340
341 if (forward)
342 SetSelection( sel == max ? 0 : sel + 1 );
343 else
344 SetSelection( sel == 0 ? max-1 : sel - 1 );
345}
346
347void wxNotebook::SetImageList( wxImageList* imageList )
348{
349 m_imageList = imageList;
350}
351
352bool wxNotebook::SetPageText( int page, const wxString &text )
353{
354 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
355
356 wxNotebookPage* nb_page = GetNotebookPage(page);
357
358 wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") );
359
360 nb_page->m_text = text;
361
362 gtk_label_set( nb_page->m_label, nb_page->m_text.mbc_str() );
363
364 return TRUE;
365}
366
367bool wxNotebook::SetPageImage( int page, int image )
368{
369 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
370
371 wxNotebookPage* nb_page = GetNotebookPage(page);
372
373 if (!nb_page) return FALSE;
374
375 /* Optimization posibility: return immediately if image unchanged.
376 * Not enabled because it may break existing (stupid) code that
377 * manipulates the imagelist to cycle images */
378
379 /* if (image == nb_page->m_image) return TRUE; */
380
381 /* For different cases:
382 1) no image -> no image
383 2) image -> no image
384 3) no image -> image
385 4) image -> image */
386
387 if (image == -1 && nb_page->m_image == -1)
388 return TRUE; /* Case 1): Nothing to do. */
389
390 GtkWidget *pixmapwid = (GtkWidget*) NULL;
391
392 if (nb_page->m_image != -1)
393 {
394 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
395
396 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
397 while (child)
398 {
399 if (GTK_IS_PIXMAP(child->data))
400 {
401 pixmapwid = GTK_WIDGET(child->data);
402 break;
403 }
404 child = child->next;
405 }
406
407 /* We should have the pixmap widget now */
408 wxASSERT(pixmapwid != NULL);
409
410 if (image == -1)
411 {
412 /* If there's no new widget, just remove the old from the box */
413 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
414 nb_page->m_image = -1;
415
416 return TRUE; /* Case 2) */
417 }
418 }
419
420 /* Only cases 3) and 4) left */
421 wxASSERT( m_imageList != NULL ); /* Just in case */
422
423 /* Construct the new pixmap */
424 const wxBitmap *bmp = m_imageList->GetBitmap(image);
425 GdkPixmap *pixmap = bmp->GetPixmap();
426 GdkBitmap *mask = (GdkBitmap*) NULL;
427 if ( bmp->GetMask() )
428 {
429 mask = bmp->GetMask()->GetBitmap();
430 }
431
432 if (pixmapwid == NULL)
433 {
434 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
435 pixmapwid = gtk_pixmap_new (pixmap, mask );
436
437 /* CHECKME: Are these pack flags okay? */
438 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, 3);
439 gtk_widget_show(pixmapwid);
440 }
441 else
442 {
443 /* Case 4) Simply replace the pixmap */
444 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
445 }
446
447 nb_page->m_image = image;
448
449 return TRUE;
450}
451
452void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
453{
454 wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") );
455}
456
457void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
458{
459 wxFAIL_MSG( wxT("wxNotebook::SetPadding not implemented") );
460}
461
462void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz))
463{
464 wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") );
465}
466
467bool wxNotebook::DeleteAllPages()
468{
469 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
470
471 while (m_pages.GetCount() > 0)
472 DeletePage( m_pages.GetCount()-1 );
473
474 return TRUE;
475}
476
477bool wxNotebook::DeletePage( int page )
478{
479 wxNotebookPage* nb_page = GetNotebookPage(page);
480 if (!nb_page) return FALSE;
481
482 /* GTK sets GtkNotebook.cur_page to NULL before sending
483 the switvh page event */
484 m_lastSelection = GetSelection();
485
486 nb_page->m_client->Destroy();
487 m_pages.DeleteObject( nb_page );
488
489 m_lastSelection = -1;
490
491 return TRUE;
492}
493
494bool wxNotebook::RemovePage( int page )
495{
496 wxNotebookPage* nb_page = GetNotebookPage(page);
497
498 if (!nb_page) return FALSE;
499
500 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page );
501
502 m_pages.DeleteObject( nb_page );
503
504 return TRUE;
505}
506
507bool wxNotebook::InsertPage( int position, wxWindow* win, const wxString& text,
508 bool select, int imageId )
509{
510 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") );
511
512 wxCHECK_MSG( win->GetParent() == this, FALSE,
513 wxT("Can't add a page whose parent is not the notebook!") );
514
515 /* don't receive switch page during addition */
516 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
517 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this );
518
519 GtkNotebook *notebook = GTK_NOTEBOOK(m_widget);
520
521 wxNotebookPage *page = new wxNotebookPage();
522
523 if (position < 0)
524 m_pages.Append( page );
525 else
526 m_pages.Insert( m_pages.Nth( position ), page );
527
528 page->m_client = win;
529
530 page->m_box = gtk_hbox_new( FALSE, 0 );
531 gtk_container_border_width( GTK_CONTAINER(page->m_box), 2 );
532
533 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
534 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
535
536 if (position < 0)
537 gtk_notebook_append_page( notebook, win->m_widget, page->m_box );
538 else
539 gtk_notebook_insert_page( notebook, win->m_widget, page->m_box, position );
540
541 page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data;
542
543 /* set the label image */
544 page->m_image = imageId;
545
546 if (imageId != -1)
547 {
548 wxASSERT( m_imageList != NULL );
549
550 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
551 GdkPixmap *pixmap = bmp->GetPixmap();
552 GdkBitmap *mask = (GdkBitmap*) NULL;
553 if ( bmp->GetMask() )
554 {
555 mask = bmp->GetMask()->GetBitmap();
556 }
557
558 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
559
560 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
561
562 gtk_widget_show(pixmapwid);
563 }
564
565 /* set the label text */
566 page->m_text = text;
567 if (page->m_text.IsEmpty()) page->m_text = wxT("");
568
569 page->m_label = GTK_LABEL( gtk_label_new(page->m_text.mbc_str()) );
570 gtk_box_pack_end( GTK_BOX(page->m_box), GTK_WIDGET(page->m_label), FALSE, FALSE, 3 );
571
572 /* show the label */
573 gtk_widget_show( GTK_WIDGET(page->m_label) );
574
575 if (select && (m_pages.GetCount() > 1))
576 {
577 if (position < 0)
578 SetSelection( GetPageCount()-1 );
579 else
580 SetSelection( position );
581 }
582
583 gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page",
584 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this );
585
586 return TRUE;
587}
588
589bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
590 bool select, int imageId)
591{
592 return InsertPage( -1, win, text, select, imageId );
593}
594
595void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event)
596{
597 if (event.IsWindowChange())
598 AdvanceSelection( event.GetDirection() );
599 else
600 event.Skip();
601}
602
603wxWindow *wxNotebook::GetPage( int page ) const
604{
605 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, wxT("invalid notebook") );
606
607 wxNotebookPage* nb_page = GetNotebookPage(page);
608 if (!nb_page)
609 return (wxWindow *) NULL;
610 else
611 return nb_page->m_client;
612}
613
614// override these 2 functions to do nothing: everything is done in OnSize
615void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
616{
617 // don't set the sizes of the pages - their correct size is not yet known
618 wxControl::SetConstraintSizes(FALSE);
619}
620
621bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
622{
623 return TRUE;
624}
625
626void wxNotebook::ApplyWidgetStyle()
627{
628 SetWidgetStyle();
629 gtk_widget_set_style( m_widget, m_widgetStyle );
630}
631
632bool wxNotebook::IsOwnGtkWindow( GdkWindow *window )
633{
634 return ((m_widget->window == window) ||
635 (GTK_NOTEBOOK(m_widget)->panel == window));
636}
637
638//-----------------------------------------------------------------------------
639// wxNotebookEvent
640//-----------------------------------------------------------------------------
641
642IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
643
644#endif