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