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