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