]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/notebook.cpp
Introduced wxSOCKET_INT to socket.cpp. The previous code wouldn't compile on Windows.
[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 #include "gdk/gdkkeysyms.h"
21
22 //-----------------------------------------------------------------------------
23 // data
24 //-----------------------------------------------------------------------------
25
26 extern bool g_blockEventsOnDrag;
27
28 //-----------------------------------------------------------------------------
29 // wxNotebookPage
30 //-----------------------------------------------------------------------------
31
32 class wxNotebookPage: public wxObject
33 {
34 public:
35 wxNotebookPage()
36 {
37 m_id = -1;
38 m_text = "";
39 m_image = -1;
40 m_page = (GtkNotebookPage *) NULL;
41 m_client = (wxWindow *) NULL;
42 m_parent = (GtkNotebook *) NULL;
43 m_box = (GtkWidget *) NULL;
44 m_added = FALSE;
45 }
46
47 /*
48 mark page as "added' to the notebook, return FALSE if the page was
49 already added
50 */
51
52 bool Add()
53 {
54 if ( WasAdded() )
55 return FALSE;
56
57 m_added = TRUE;
58 return TRUE;
59 }
60
61 bool WasAdded() const { return m_added; }
62
63 int m_id;
64 wxString m_text;
65 int m_image;
66 GtkNotebookPage *m_page;
67 GtkLabel *m_label;
68 wxWindow *m_client;
69 GtkNotebook *m_parent;
70 GtkWidget *m_box; // in which the label and image are packed
71
72 private:
73 bool m_added;
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 nPage,
83 gpointer data)
84 {
85 wxNotebook *notebook = (wxNotebook *)data;
86
87 int old = notebook->GetSelection();
88
89 // TODO: emulate PAGE_CHANGING event
90
91 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
92 notebook->GetId(), nPage, old );
93 event.SetEventObject( notebook );
94 notebook->GetEventHandler()->ProcessEvent( event );
95 }
96
97 //-----------------------------------------------------------------------------
98 // "size_allocate"
99 //-----------------------------------------------------------------------------
100
101 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
102 {
103 if ((win->m_x == alloc->x) &&
104 (win->m_y == alloc->y) &&
105 (win->m_width == alloc->width) &&
106 (win->m_height == alloc->height))
107 {
108 return;
109 }
110
111 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
112
113 if (win->GetAutoLayout()) win->Layout();
114 }
115
116 //-----------------------------------------------------------------------------
117 // "key_press_event"
118 //-----------------------------------------------------------------------------
119
120 static gint
121 gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *notebook )
122 {
123 if (g_blockEventsOnDrag) return FALSE;
124
125 if (!notebook->HasVMT()) return FALSE;
126
127 if (gdk_event->keyval != GDK_Down) return FALSE;
128
129 if (notebook != notebook->FindFocus()) return FALSE;
130
131 if (notebook->m_pages.GetCount() == 0) return FALSE;
132
133 wxNode *node = notebook->m_pages.Nth( notebook->GetSelection() );
134
135 if (!node) return FALSE;
136
137 wxNotebookPage *page = (wxNotebookPage*) node->Data();
138
139 // don't let others the key event
140 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
141
142 page->m_client->SetFocus();
143
144 return TRUE;
145 }
146
147 //-----------------------------------------------------------------------------
148 // InsertChild callback for wxNotebook
149 //-----------------------------------------------------------------------------
150
151 static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child )
152 {
153 wxNotebookPage *page = new wxNotebookPage();
154
155 page->m_id = parent->GetPageCount();
156
157 page->m_box = gtk_hbox_new (FALSE, 0);
158 gtk_container_border_width(GTK_CONTAINER(page->m_box), 2);
159
160 GtkNotebook *notebook = GTK_NOTEBOOK(parent->m_widget);
161
162 page->m_client = child;
163 gtk_notebook_append_page( notebook, child->m_widget, page->m_box );
164
165 page->m_page = (GtkNotebookPage*) (g_list_last(notebook->children)->data);
166
167 page->m_parent = notebook;
168
169 gtk_signal_connect( GTK_OBJECT(child->m_widget), "size_allocate",
170 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)child );
171
172 wxASSERT_MSG( page->m_page, "Notebook page creation error" );
173
174 parent->m_pages.Append( page );
175 }
176
177 //-----------------------------------------------------------------------------
178 // wxNotebook
179 //-----------------------------------------------------------------------------
180
181 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
182
183 void wxNotebook::Init()
184 {
185 m_imageList = (wxImageList *) NULL;
186 m_pages.DeleteContents( TRUE );
187 m_idHandler = 0;
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 if (m_idHandler != 0)
207 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
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, "wxNotebook::m_widget", name );
226 #endif
227
228 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
229
230 m_idHandler = gtk_signal_connect (
231 GTK_OBJECT(m_widget), "switch_page",
232 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
233 (gpointer)this );
234
235 m_parent->AddChild( this );
236
237 (m_parent->m_insertCallback)( m_parent, this );
238
239 gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event",
240 GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this );
241
242 PostCreation();
243
244 Show( TRUE );
245
246 return TRUE;
247 }
248
249 int wxNotebook::GetSelection() const
250 {
251 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
252
253 if (m_pages.Number() == 0) return -1;
254
255 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
256 if (!g_page) return -1;
257
258 wxNotebookPage *page = (wxNotebookPage *) NULL;
259
260 wxNode *node = m_pages.First();
261 while (node)
262 {
263 page = (wxNotebookPage*)node->Data();
264
265 if ((page->m_page == g_page) || (page->m_page == (GtkNotebookPage*)NULL))
266 {
267 // page->m_page is NULL directly after gtk_notebook_append. gtk emits
268 // "switch_page" then and we ask for GetSelection() in the handler for
269 // "switch_page". otherwise m_page should never be NULL. all this
270 // might also be wrong.
271 break;
272 }
273 node = node->Next();
274 }
275
276 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
277
278 return page->m_id;
279 }
280
281 int wxNotebook::GetPageCount() const
282 {
283 // count only the pages which were already added to the notebook for MSW
284 // compatibility (and, in fact, this behaviour makes more sense anyhow
285 // because only the added pages are shown)
286
287 int n = 0;
288 for ( wxNode *node = m_pages.First(); node; node = node->Next() )
289 {
290 wxNotebookPage *page = (wxNotebookPage*)node->Data();
291
292 if (page->WasAdded()) n++;
293 }
294
295 return n;
296 }
297
298 int wxNotebook::GetRowCount() const
299 {
300 return 1;
301 }
302
303 wxString wxNotebook::GetPageText( int page ) const
304 {
305 wxCHECK_MSG( m_widget != NULL, "", "invalid notebook" );
306
307 wxNotebookPage* nb_page = GetNotebookPage(page);
308 if (nb_page)
309 return nb_page->m_text;
310 else
311 return "";
312 }
313
314 int wxNotebook::GetPageImage( int page ) const
315 {
316 wxCHECK_MSG( m_widget != NULL, 0, "invalid notebook" );
317
318 wxNotebookPage* nb_page = GetNotebookPage(page);
319 if (nb_page)
320 return nb_page->m_image;
321 else
322 return 0;
323 }
324
325 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
326 {
327 wxCHECK_MSG( m_widget != NULL, (wxNotebookPage*)NULL, "invalid notebook" );
328
329 wxNotebookPage *nb_page = (wxNotebookPage *) NULL;
330
331 wxNode *node = m_pages.First();
332 while (node)
333 {
334 nb_page = (wxNotebookPage*)node->Data();
335 if (nb_page->m_id == page)
336 return nb_page;
337 node = node->Next();
338 }
339
340 wxFAIL_MSG( "Notebook page not found!" );
341
342 return (wxNotebookPage *) NULL;
343 }
344
345 int wxNotebook::SetSelection( int page )
346 {
347 wxCHECK_MSG( m_widget != NULL, -1, "invalid notebook" );
348
349 int selOld = GetSelection();
350 wxNotebookPage* nb_page = GetNotebookPage(page);
351
352 if (!nb_page) return -1;
353
354 int page_num = 0;
355 GList *child = GTK_NOTEBOOK(m_widget)->children;
356 while (child)
357 {
358 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
359 page_num++;
360 child = child->next;
361 }
362
363 if (!child) return -1;
364
365 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
366
367 return selOld;
368 }
369
370 void wxNotebook::AdvanceSelection( bool bForward )
371 {
372 wxCHECK_RET( m_widget != NULL, "invalid notebook" );
373
374 int sel = GetSelection();
375 int max = GetPageCount();
376
377 if (bForward)
378 SetSelection( sel == max ? 0 : sel + 1 );
379 else
380 SetSelection( sel == 0 ? max : sel - 1 );
381 }
382
383 void wxNotebook::SetImageList( wxImageList* imageList )
384 {
385 m_imageList = imageList;
386 }
387
388 bool wxNotebook::SetPageText( int page, const wxString &text )
389 {
390 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
391
392 wxNotebookPage* nb_page = GetNotebookPage(page);
393
394 if (!nb_page) return FALSE;
395
396 nb_page->m_text = text;
397
398 if (nb_page->m_text.IsEmpty()) nb_page->m_text = "";
399
400 gtk_label_set(nb_page->m_label, nb_page->m_text);
401
402 return TRUE;
403 }
404
405 bool wxNotebook::SetPageImage( int page, int image )
406 {
407 /* HvdH 28-12-98: now it works, but it's a bit of a kludge */
408
409 wxNotebookPage* nb_page = GetNotebookPage(page);
410
411 if (!nb_page) return FALSE;
412
413 /* Optimization posibility: return immediately if image unchanged.
414 * Not enabled because it may break existing (stupid) code that
415 * manipulates the imagelist to cycle images */
416
417 /* if (image == nb_page->m_image) return TRUE; */
418
419 /* For different cases:
420 1) no image -> no image
421 2) image -> no image
422 3) no image -> image
423 4) image -> image */
424
425 if (image == -1 && nb_page->m_image == -1)
426 return TRUE; /* Case 1): Nothing to do. */
427
428 GtkWidget *pixmapwid = NULL;
429
430 if (nb_page->m_image != -1)
431 {
432 /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */
433
434 GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box));
435 while (child)
436 {
437 if (GTK_IS_PIXMAP(child->data))
438 {
439 pixmapwid = GTK_WIDGET(child->data);
440 break;
441 }
442 child = child->next;
443 }
444
445 /* We should have the pixmap widget now */
446 wxASSERT(pixmapwid != NULL);
447
448 if (image == -1)
449 {
450 /* If there's no new widget, just remove the old from the box */
451 gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid);
452 nb_page->m_image = -1;
453
454 return TRUE; /* Case 2) */
455 }
456 }
457
458 /* Only cases 3) and 4) left */
459 wxASSERT( m_imageList != NULL ); /* Just in case */
460
461 /* Construct the new pixmap */
462 const wxBitmap *bmp = m_imageList->GetBitmap(image);
463 GdkPixmap *pixmap = bmp->GetPixmap();
464 GdkBitmap *mask = (GdkBitmap*) NULL;
465 if ( bmp->GetMask() )
466 {
467 mask = bmp->GetMask()->GetBitmap();
468 }
469
470 if (pixmapwid == NULL)
471 {
472 /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */
473 pixmapwid = gtk_pixmap_new (pixmap, mask );
474
475 /* CHECKME: Are these pack flags okay? */
476 gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, 3);
477 gtk_widget_show(pixmapwid);
478 }
479 else
480 {
481 /* Case 4) Simply replace the pixmap */
482 gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask);
483 }
484
485 nb_page->m_image = image;
486
487 return TRUE;
488 }
489
490 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
491 {
492 wxFAIL_MSG( "wxNotebook::SetPageSize not implemented" );
493 }
494
495 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
496 {
497 wxFAIL_MSG( "wxNotebook::SetPadding not implemented" );
498 }
499
500 bool wxNotebook::DeleteAllPages()
501 {
502 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
503
504 wxNode *page_node = m_pages.First();
505 while (page_node)
506 {
507 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
508
509 DeletePage( page->m_id );
510
511 page_node = m_pages.First();
512 }
513
514 return TRUE;
515 }
516
517 bool wxNotebook::DeletePage( int page )
518 {
519 wxNotebookPage* nb_page = GetNotebookPage(page);
520 if (!nb_page) return FALSE;
521
522 int page_num = 0;
523 GList *child = GTK_NOTEBOOK(m_widget)->children;
524 while (child)
525 {
526 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
527 page_num++;
528 child = child->next;
529 }
530
531 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
532
533 delete nb_page->m_client;
534
535 m_pages.DeleteObject( nb_page );
536
537 return TRUE;
538 }
539
540 bool wxNotebook::RemovePage( int page )
541 {
542 wxNotebookPage* nb_page = GetNotebookPage(page);
543 if (!nb_page) return FALSE;
544
545 int page_num = 0;
546 GList *child = GTK_NOTEBOOK(m_widget)->children;
547 while (child)
548 {
549 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
550 page_num++;
551 child = child->next;
552 }
553
554 wxCHECK_MSG( child != NULL, FALSE, "illegal notebook index" );
555
556 gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
557
558 m_pages.DeleteObject( nb_page );
559
560 return TRUE;
561 }
562
563 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
564 bool select, int imageId)
565 {
566 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid notebook" );
567
568 /* we've created the notebook page in AddChild(). Now we just have to set
569 the caption for the page and set the others parameters. */
570
571 wxNotebookPage *page = (wxNotebookPage *) NULL;
572
573 wxNode *node = m_pages.First();
574 while (node)
575 {
576 page = (wxNotebookPage*)node->Data();
577 if ( page->m_client == win ) break;
578 node = node->Next();
579 }
580
581 wxCHECK_MSG( page != NULL, FALSE,
582 "Can't add a page whose parent is not the notebook!" );
583
584 wxCHECK_MSG( page->Add(), FALSE,
585 "Can't add the same page twice to a notebook." );
586
587 if (imageId != -1)
588 {
589 wxASSERT( m_imageList != NULL );
590
591 const wxBitmap *bmp = m_imageList->GetBitmap(imageId);
592 GdkPixmap *pixmap = bmp->GetPixmap();
593 GdkBitmap *mask = (GdkBitmap*) NULL;
594 if ( bmp->GetMask() )
595 {
596 mask = bmp->GetMask()->GetBitmap();
597 }
598
599 GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask );
600
601 gtk_box_pack_start(GTK_BOX(page->m_box), pixmapwid, FALSE, FALSE, 3);
602
603 gtk_widget_show(pixmapwid);
604 }
605
606 /* then set the attributes */
607 page->m_text = text;
608 if (page->m_text.IsEmpty()) page->m_text = "";
609 page->m_image = imageId;
610 page->m_label = (GtkLabel *)gtk_label_new(page->m_text);
611 gtk_box_pack_end( GTK_BOX(page->m_box), (GtkWidget *)page->m_label, FALSE, FALSE, 3);
612
613 /* @@@: what does this do? do we still need it?
614 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5); */
615
616 gtk_widget_show((GtkWidget *)page->m_label);
617
618 if (select) SetSelection( GetPageCount()-1 );
619
620 return TRUE;
621 }
622
623 wxWindow *wxNotebook::GetPage( int page ) const
624 {
625 wxCHECK_MSG( m_widget != NULL, (wxWindow*) NULL, "invalid notebook" );
626
627 wxNotebookPage* nb_page = GetNotebookPage(page);
628 if (!nb_page)
629 return (wxWindow *) NULL;
630 else
631 return nb_page->m_client;
632 }
633
634 // override these 2 functions to do nothing: everything is done in OnSize
635 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
636 {
637 // don't set the sizes of the pages - their correct size is not yet known
638 wxControl::SetConstraintSizes(FALSE);
639 }
640
641 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
642 {
643 return TRUE;
644 }
645
646 void wxNotebook::ApplyWidgetStyle()
647 {
648 SetWidgetStyle();
649 gtk_widget_set_style( m_widget, m_widgetStyle );
650 }
651
652 //-----------------------------------------------------------------------------
653 // wxNotebookEvent
654 //-----------------------------------------------------------------------------
655
656 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
657