]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/notebook.cpp
Since wxREADONLY has disappeared, I had to change to wxTE_READONLY
[wxWidgets.git] / src / gtk / notebook.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: notebook.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "notebook.h"
13 #endif
14
15 #include "wx/notebook.h"
16 #include "wx/panel.h"
17 #include "wx/utils.h"
18 #include "wx/imaglist.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21
22 //-----------------------------------------------------------------------------
23 // wxNotebookPage
24 //-----------------------------------------------------------------------------
25
26 class wxNotebookPage: public wxObject
27 {
28 public:
29 wxNotebookPage()
30 {
31 m_id = -1;
32 m_text = "";
33 m_image = -1;
34 m_page = NULL;
35 m_client = NULL;
36 m_parent = NULL;
37 };
38
39 //private:
40 int m_id;
41 wxString m_text;
42 int m_image;
43 GtkNotebookPage *m_page;
44 GtkLabel *m_label;
45 wxWindow *m_client;
46 GtkNotebook *m_parent;
47 };
48
49 //-----------------------------------------------------------------------------
50 // GTK callbacks
51 //-----------------------------------------------------------------------------
52
53 // page change callback
54 static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget),
55 GtkNotebookPage *WXUNUSED(page),
56 gint nPage,
57 gpointer data)
58 {
59 wxNotebook *notebook = (wxNotebook *)data;
60
61 int nOld = notebook->GetSelection();
62
63 // TODO: emulate PAGE_CHANGING event
64 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,
65 notebook->GetId(),
66 nPage,
67 nOld);
68 event.SetEventObject(notebook);
69 notebook->ProcessEvent(event);
70 }
71
72 static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win )
73 {
74 if ( win->GetAutoLayout() )
75 win->Layout();
76
77 if ((win->m_x == alloc->x) &&
78 (win->m_y == alloc->y) &&
79 (win->m_width == alloc->width) &&
80 (win->m_height == alloc->height))
81 {
82 return;
83 };
84
85 /*
86 printf( "OnResize from " );
87 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
88 printf( win->GetClassInfo()->GetClassName() );
89 printf( " .\n" );
90
91 printf( " Old: X: %d Y: %d ", win->m_x, win->m_y );
92 printf( " W: %d H: %d ", win->m_width, win->m_height );
93 printf( " .\n" );
94
95 printf( " New: X: %d Y: %d ", alloc->x, alloc->y );
96 printf( " W: %d H: %d ", alloc->width, alloc->height );
97 printf( " .\n" );
98 */
99
100 win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height );
101
102 /*
103 printf( " Res: X: %d Y: %d ", win->m_x, win->m_y );
104 printf( " W: %d H: %d ", win->m_width, win->m_height );
105 printf( " .\n" );
106 */
107 };
108
109 //-----------------------------------------------------------------------------
110 // wxNotebook
111 //-----------------------------------------------------------------------------
112
113 IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl)
114
115 void wxNotebook::Init()
116 {
117 m_imageList = NULL;
118 m_pages.DeleteContents( TRUE );
119 m_idHandler = 0;
120 }
121
122 wxNotebook::wxNotebook()
123 {
124 Init();
125 };
126
127 wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id,
128 const wxPoint& pos, const wxSize& size,
129 long style, const wxString& name )
130 {
131 Init();
132 Create( parent, id, pos, size, style, name );
133 };
134
135 wxNotebook::~wxNotebook()
136 {
137 // don't generate change page events any more
138 if ( m_idHandler != 0 )
139 gtk_signal_disconnect(GTK_OBJECT(m_widget), m_idHandler);
140
141 DeleteAllPages();
142 };
143
144 bool wxNotebook::Create(wxWindow *parent, wxWindowID id,
145 const wxPoint& pos, const wxSize& size,
146 long style, const wxString& name )
147 {
148 m_needParent = TRUE;
149
150 PreCreation( parent, id, pos, size, style, name );
151
152 m_widget = gtk_notebook_new();
153
154 gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 );
155
156 m_idHandler = gtk_signal_connect
157 (
158 GTK_OBJECT(m_widget), "switch_page",
159 GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback),
160 (gpointer)this
161 );
162
163 PostCreation();
164
165 Show( TRUE );
166
167 return TRUE;
168 };
169
170 int wxNotebook::GetSelection() const
171 {
172 if (m_pages.Number() == 0)
173 return -1;
174
175 GtkNotebookPage *g_page = GTK_NOTEBOOK(m_widget)->cur_page;
176
177 wxNotebookPage *page = NULL;
178
179 wxNode *node = m_pages.First();
180 while (node)
181 {
182 page = (wxNotebookPage*)node->Data();
183 if (page->m_page == g_page)
184 break;
185 node = node->Next();
186 };
187
188 wxCHECK_MSG( node != NULL, -1, "wxNotebook: no selection?" );
189
190 return page->m_id;
191 };
192
193 int wxNotebook::GetPageCount() const
194 {
195 return m_pages.Number();
196 };
197
198 int wxNotebook::GetRowCount() const
199 {
200 return 1;
201 };
202
203 wxString wxNotebook::GetPageText( int page ) const
204 {
205 wxNotebookPage* nb_page = GetNotebookPage(page);
206 if (nb_page)
207 return nb_page->m_text;
208 else
209 return "";
210 };
211
212 int wxNotebook::GetPageImage( int page ) const
213 {
214 wxNotebookPage* nb_page = GetNotebookPage(page);
215 if (nb_page)
216 return nb_page->m_image;
217 else
218 return 0;
219 };
220
221 wxNotebookPage* wxNotebook::GetNotebookPage(int page) const
222 {
223 wxNotebookPage *nb_page = NULL;
224
225 wxNode *node = m_pages.First();
226 while (node)
227 {
228 nb_page = (wxNotebookPage*)node->Data();
229 if (nb_page->m_id == page)
230 return nb_page;
231 node = node->Next();
232 };
233
234 wxLogDebug( "Notebook page %d not found!", page );
235
236 return NULL;
237 };
238
239 int wxNotebook::SetSelection( int page )
240 {
241 int selOld = GetSelection();
242 wxNotebookPage* nb_page = GetNotebookPage(page);
243 if (!nb_page)
244 return -1;
245
246 int page_num = 0;
247 GList *child = GTK_NOTEBOOK(m_widget)->children;
248 while (child)
249 {
250 if (nb_page->m_page == (GtkNotebookPage*)child->data)
251 break;
252 page_num++;
253 child = child->next;
254 };
255
256 if (!child) return -1;
257
258 gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page_num );
259
260 return selOld;
261 };
262
263 void wxNotebook::AdvanceSelection(bool bForward)
264 {
265 int nSel = GetSelection(),
266 nMax = GetPageCount();
267
268 if ( bForward ) {
269 SetSelection(nSel == nMax ? 0 : nSel + 1);
270 }
271 else {
272 SetSelection(nSel == 0 ? nMax : nSel - 1);
273 }
274 }
275
276 void wxNotebook::SetImageList( wxImageList* imageList )
277 {
278 m_imageList = imageList;
279 };
280
281 bool wxNotebook::SetPageText( int page, const wxString &text )
282 {
283 wxNotebookPage* nb_page = GetNotebookPage(page);
284 if (!nb_page)
285 return FALSE;
286
287 nb_page->m_text = text;
288
289 return TRUE;
290 };
291
292 bool wxNotebook::SetPageImage( int page, int image )
293 {
294 wxNotebookPage* nb_page = GetNotebookPage(page);
295 if (!nb_page)
296 return FALSE;
297
298 nb_page->m_image = image;
299
300 return TRUE;
301 };
302
303 void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) )
304 {
305 wxFAIL_MSG(_("wxNotebook::SetPageSize not implemented"));
306 };
307
308 void wxNotebook::SetPadding( const wxSize &WXUNUSED(padding) )
309 {
310 wxFAIL_MSG(_("wxNotebook::SetPadding not implemented"));
311 };
312
313 bool wxNotebook::DeleteAllPages()
314 {
315 wxNode *page_node = m_pages.First();
316 while (page_node)
317 {
318 wxNotebookPage *page = (wxNotebookPage*)page_node->Data();
319
320 DeletePage( page->m_id );
321
322 page_node = m_pages.First();
323 };
324
325 return TRUE;
326 };
327
328 bool wxNotebook::DeletePage( int page )
329 {
330 wxNotebookPage* nb_page = GetNotebookPage(page);
331 if (!nb_page) return FALSE;
332
333 int page_num = 0;
334 GList *child = GTK_NOTEBOOK(m_widget)->children;
335 while (child)
336 {
337 if (nb_page->m_page == (GtkNotebookPage*)child->data) break;
338 page_num++;
339 child = child->next;
340 };
341
342 wxASSERT( child );
343
344 delete nb_page->m_client;
345
346 // Amazingly, this is not necessary
347 // gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page_num );
348
349 m_pages.DeleteObject( nb_page );
350
351 return TRUE;
352 };
353
354 bool wxNotebook::AddPage(wxWindow* win, const wxString& text,
355 bool bSelect, int imageId)
356 {
357 // we've created the notebook page in AddChild(). Now we just have to set
358 // the caption for the page and set the others parameters.
359
360 // first, find the page
361 wxNotebookPage *page = NULL;
362
363 wxNode *node = m_pages.First();
364 while (node)
365 {
366 page = (wxNotebookPage*)node->Data();
367 if ( page->m_client == win )
368 break; // found
369 node = node->Next();
370 };
371
372 wxCHECK_MSG(page != NULL, FALSE,
373 _("Can't add a page whose parent is not the notebook!"));
374
375 // then set the attributes
376 page->m_text = text;
377 if ( page->m_text.IsEmpty() )
378 page->m_text = "";
379 page->m_image = imageId;
380 gtk_label_set(page->m_label, page->m_text);
381
382 if ( bSelect ) {
383 SetSelection(GetPageCount());
384 }
385
386 return TRUE;
387 };
388
389 wxWindow *wxNotebook::GetPage( int page ) const
390 {
391 wxNotebookPage* nb_page = GetNotebookPage(page);
392 if (!nb_page)
393 return NULL;
394 else
395 return nb_page->m_client;
396 };
397
398 void wxNotebook::AddChild( wxWindow *win )
399 {
400 m_children.Append(win);
401
402 wxNotebookPage *page = new wxNotebookPage();
403
404 page->m_id = GetPageCount();
405 page->m_label = (GtkLabel *)gtk_label_new(_("Handle"));
406 page->m_client = win;
407 gtk_notebook_append_page( GTK_NOTEBOOK(m_widget), win->m_widget,
408 (GtkWidget *)page->m_label);
409 gtk_misc_set_alignment(GTK_MISC(page->m_label), 0.0, 0.5);
410
411 page->m_page =
412 (GtkNotebookPage*) (g_list_last(GTK_NOTEBOOK(m_widget)->children)->data);
413
414 page->m_parent = GTK_NOTEBOOK(m_widget);
415
416 gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate",
417 GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win );
418
419 if (!page->m_page)
420 {
421 wxLogFatalError( _("Notebook page creation error") );
422 return;
423 }
424
425 m_pages.Append( page );
426 };
427
428 // override these 2 functions to do nothing: everything is done in OnSize
429 void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) )
430 {
431 // don't set the sizes of the pages - their correct size is not yet known
432 wxControl::SetConstraintSizes(FALSE);
433 }
434
435 bool wxNotebook::DoPhase( int WXUNUSED(nPhase) )
436 {
437 return TRUE;
438 }
439
440 //-----------------------------------------------------------------------------
441 // wxNotebookEvent
442 //-----------------------------------------------------------------------------
443
444 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxCommandEvent)