]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/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 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_NOTEBOOK | |
14 | ||
15 | #include "wx/notebook.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/intl.h" | |
19 | #include "wx/log.h" | |
20 | #include "wx/utils.h" | |
21 | #include "wx/msgdlg.h" | |
22 | #include "wx/bitmap.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/imaglist.h" | |
26 | #include "wx/fontutil.h" | |
27 | ||
28 | #include "wx/gtk/private.h" | |
29 | ||
30 | #include <gdk/gdkkeysyms.h> | |
31 | ||
32 | // ---------------------------------------------------------------------------- | |
33 | // events | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) | |
37 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
38 | ||
39 | //----------------------------------------------------------------------------- | |
40 | // data | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | extern bool g_blockEventsOnDrag; | |
44 | ||
45 | //----------------------------------------------------------------------------- | |
46 | // wxGtkNotebookPage | |
47 | //----------------------------------------------------------------------------- | |
48 | ||
49 | // VZ: this is rather ugly as we keep the pages themselves in an array (it | |
50 | // allows us to have quite a few functions implemented in the base class) | |
51 | // but the page data is kept in a separate list, so we must maintain them | |
52 | // in sync manually... of course, the list had been there before the base | |
53 | // class which explains it but it still would be nice to do something | |
54 | // about this one day | |
55 | ||
56 | class wxGtkNotebookPage: public wxObject | |
57 | { | |
58 | public: | |
59 | wxGtkNotebookPage() | |
60 | { | |
61 | m_image = -1; | |
62 | m_page = (GtkNotebookPage *) NULL; | |
63 | m_box = (GtkWidget *) NULL; | |
64 | } | |
65 | ||
66 | wxString m_text; | |
67 | int m_image; | |
68 | GtkNotebookPage *m_page; | |
69 | GtkLabel *m_label; | |
70 | GtkWidget *m_box; // in which the label and image are packed | |
71 | }; | |
72 | ||
73 | ||
74 | #include "wx/listimpl.cpp" | |
75 | WX_DEFINE_LIST(wxGtkNotebookPagesList) | |
76 | ||
77 | ||
78 | //----------------------------------------------------------------------------- | |
79 | // "switch_page" | |
80 | //----------------------------------------------------------------------------- | |
81 | ||
82 | extern "C" { | |
83 | static void gtk_notebook_page_changing_callback( GtkNotebook *widget, | |
84 | GtkNotebookPage *WXUNUSED(gpage), | |
85 | guint page, | |
86 | wxNotebook *notebook ) | |
87 | { | |
88 | int old = gtk_notebook_get_current_page( widget ); | |
89 | ||
90 | if ( !notebook->SendPageChangingEvent(page) ) | |
91 | { | |
92 | // program doesn't allow the page change | |
93 | g_signal_stop_emission_by_name(notebook->m_widget, "switch_page"); | |
94 | } | |
95 | else | |
96 | { | |
97 | // the page change event also reports the old page | |
98 | notebook->m_oldSelection = old; | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | extern "C" { | |
104 | static void gtk_notebook_page_changed_callback( GtkNotebook *widget, | |
105 | GtkNotebookPage *WXUNUSED(gpage), | |
106 | guint page, | |
107 | wxNotebook *notebook ) | |
108 | { | |
109 | int old = notebook->m_oldSelection; | |
110 | notebook->SendPageChangedEvent( old ); | |
111 | } | |
112 | } | |
113 | ||
114 | //----------------------------------------------------------------------------- | |
115 | // "size_allocate" | |
116 | //----------------------------------------------------------------------------- | |
117 | ||
118 | extern "C" { | |
119 | static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win ) | |
120 | { | |
121 | if ((win->m_x == alloc->x) && | |
122 | (win->m_y == alloc->y) && | |
123 | (win->m_width == alloc->width) && | |
124 | (win->m_height == alloc->height)) | |
125 | { | |
126 | return; | |
127 | } | |
128 | ||
129 | win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height ); | |
130 | } | |
131 | } | |
132 | ||
133 | //----------------------------------------------------------------------------- | |
134 | // "realize" from m_widget | |
135 | //----------------------------------------------------------------------------- | |
136 | ||
137 | extern "C" { | |
138 | static void | |
139 | gtk_notebook_realized_callback( GtkWidget * WXUNUSED(widget), wxWindow *win ) | |
140 | { | |
141 | /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize | |
142 | here in order to make repositioning before showing to take effect. */ | |
143 | gtk_widget_queue_resize( win->m_widget ); | |
144 | } | |
145 | } | |
146 | ||
147 | //----------------------------------------------------------------------------- | |
148 | // InsertChild callback for wxNotebook | |
149 | //----------------------------------------------------------------------------- | |
150 | ||
151 | static void wxInsertChildInNotebook(wxWindow* parent, wxWindow* child) | |
152 | { | |
153 | // Hack Alert! (Part I): This sets the notebook as the parent of the child | |
154 | // widget, and takes care of some details such as updating the state and | |
155 | // style of the child to reflect its new location. We do this early | |
156 | // because without it GetBestSize (which is used to set the initial size | |
157 | // of controls if an explicit size is not given) will often report | |
158 | // incorrect sizes since the widget's style context is not fully known. | |
159 | // See bug #901694 for details | |
160 | // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863) | |
161 | gtk_widget_set_parent(child->m_widget, parent->m_widget); | |
162 | ||
163 | // NOTE: This should be considered a temporary workaround until we can | |
164 | // work out the details and implement delaying the setting of the initial | |
165 | // size of widgets until the size is really needed. | |
166 | } | |
167 | ||
168 | //----------------------------------------------------------------------------- | |
169 | // wxNotebook | |
170 | //----------------------------------------------------------------------------- | |
171 | ||
172 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl) | |
173 | ||
174 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) | |
175 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
176 | END_EVENT_TABLE() | |
177 | ||
178 | void wxNotebook::Init() | |
179 | { | |
180 | m_padding = 0; | |
181 | ||
182 | m_imageList = (wxImageList *) NULL; | |
183 | m_oldSelection = -1; | |
184 | m_themeEnabled = true; | |
185 | } | |
186 | ||
187 | wxNotebook::wxNotebook() | |
188 | { | |
189 | Init(); | |
190 | } | |
191 | ||
192 | wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id, | |
193 | const wxPoint& pos, const wxSize& size, | |
194 | long style, const wxString& name ) | |
195 | { | |
196 | Init(); | |
197 | Create( parent, id, pos, size, style, name ); | |
198 | } | |
199 | ||
200 | wxNotebook::~wxNotebook() | |
201 | { | |
202 | DeleteAllPages(); | |
203 | } | |
204 | ||
205 | bool wxNotebook::Create(wxWindow *parent, wxWindowID id, | |
206 | const wxPoint& pos, const wxSize& size, | |
207 | long style, const wxString& name ) | |
208 | { | |
209 | m_insertCallback = wxInsertChildInNotebook; | |
210 | ||
211 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) | |
212 | style |= wxBK_TOP; | |
213 | ||
214 | if (!PreCreation( parent, pos, size ) || | |
215 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
216 | { | |
217 | wxFAIL_MSG( wxT("wxNoteBook creation failed") ); | |
218 | return false; | |
219 | } | |
220 | ||
221 | ||
222 | m_widget = gtk_notebook_new(); | |
223 | ||
224 | gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 ); | |
225 | ||
226 | g_signal_connect (m_widget, "switch_page", | |
227 | G_CALLBACK (gtk_notebook_page_changing_callback), this); | |
228 | ||
229 | g_signal_connect_after (m_widget, "switch_page", | |
230 | G_CALLBACK (gtk_notebook_page_changed_callback), this); | |
231 | ||
232 | m_parent->DoAddChild( this ); | |
233 | ||
234 | if (m_windowStyle & wxBK_RIGHT) | |
235 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT ); | |
236 | if (m_windowStyle & wxBK_LEFT) | |
237 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT ); | |
238 | if (m_windowStyle & wxBK_BOTTOM) | |
239 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM ); | |
240 | ||
241 | PostCreation(size); | |
242 | ||
243 | g_signal_connect (m_widget, "realize", | |
244 | G_CALLBACK (gtk_notebook_realized_callback), this); | |
245 | ||
246 | return true; | |
247 | } | |
248 | ||
249 | int wxNotebook::GetSelection() const | |
250 | { | |
251 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); | |
252 | ||
253 | return gtk_notebook_get_current_page( GTK_NOTEBOOK(m_widget) ); | |
254 | } | |
255 | ||
256 | wxString wxNotebook::GetPageText( size_t page ) const | |
257 | { | |
258 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid notebook") ); | |
259 | ||
260 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); | |
261 | if (nb_page) | |
262 | return nb_page->m_text; | |
263 | else | |
264 | return wxEmptyString; | |
265 | } | |
266 | ||
267 | int wxNotebook::GetPageImage( size_t page ) const | |
268 | { | |
269 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); | |
270 | ||
271 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); | |
272 | if (nb_page) | |
273 | return nb_page->m_image; | |
274 | else | |
275 | return -1; | |
276 | } | |
277 | ||
278 | wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const | |
279 | { | |
280 | wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") ); | |
281 | ||
282 | wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") ); | |
283 | ||
284 | return m_pagesData.Item(page)->GetData(); | |
285 | } | |
286 | ||
287 | int wxNotebook::DoSetSelection( size_t page, int flags ) | |
288 | { | |
289 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); | |
290 | ||
291 | wxCHECK_MSG( page < m_pagesData.GetCount(), -1, wxT("invalid notebook index") ); | |
292 | ||
293 | int selOld = GetSelection(); | |
294 | ||
295 | if ( !(flags & SetSelection_SendEvent) ) | |
296 | { | |
297 | g_signal_handlers_disconnect_by_func (m_widget, | |
298 | (gpointer) gtk_notebook_page_changing_callback, | |
299 | this); | |
300 | ||
301 | g_signal_handlers_disconnect_by_func (m_widget, | |
302 | (gpointer) gtk_notebook_page_changed_callback, | |
303 | this); | |
304 | } | |
305 | ||
306 | gtk_notebook_set_current_page( GTK_NOTEBOOK(m_widget), page ); | |
307 | ||
308 | if ( !(flags & SetSelection_SendEvent) ) | |
309 | { | |
310 | // reconnect to signals | |
311 | ||
312 | g_signal_connect (m_widget, "switch_page", | |
313 | G_CALLBACK (gtk_notebook_page_changing_callback), this); | |
314 | ||
315 | g_signal_connect_after (m_widget, "switch_page", | |
316 | G_CALLBACK (gtk_notebook_page_changed_callback), this); | |
317 | } | |
318 | ||
319 | wxNotebookPage *client = GetPage(page); | |
320 | if ( client ) | |
321 | client->SetFocus(); | |
322 | ||
323 | return selOld; | |
324 | } | |
325 | ||
326 | bool wxNotebook::SetPageText( size_t page, const wxString &text ) | |
327 | { | |
328 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid notebook") ); | |
329 | ||
330 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); | |
331 | ||
332 | wxCHECK_MSG( nb_page, false, wxT("SetPageText: invalid page index") ); | |
333 | ||
334 | nb_page->m_text = text; | |
335 | ||
336 | gtk_label_set_text( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) ); | |
337 | ||
338 | return true; | |
339 | } | |
340 | ||
341 | bool wxNotebook::SetPageImage( size_t page, int image ) | |
342 | { | |
343 | /* HvdH 28-12-98: now it works, but it's a bit of a kludge */ | |
344 | ||
345 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); | |
346 | ||
347 | if (!nb_page) return false; | |
348 | ||
349 | /* Optimization posibility: return immediately if image unchanged. | |
350 | * Not enabled because it may break existing (stupid) code that | |
351 | * manipulates the imagelist to cycle images */ | |
352 | ||
353 | /* if (image == nb_page->m_image) return true; */ | |
354 | ||
355 | /* For different cases: | |
356 | 1) no image -> no image | |
357 | 2) image -> no image | |
358 | 3) no image -> image | |
359 | 4) image -> image */ | |
360 | ||
361 | if (image == -1 && nb_page->m_image == -1) | |
362 | return true; /* Case 1): Nothing to do. */ | |
363 | ||
364 | GtkWidget *pixmapwid = (GtkWidget*) NULL; | |
365 | ||
366 | if (nb_page->m_image != -1) | |
367 | { | |
368 | /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */ | |
369 | ||
370 | GList *child = gtk_container_get_children(GTK_CONTAINER(nb_page->m_box)); | |
371 | while (child) | |
372 | { | |
373 | if (GTK_IS_IMAGE(child->data)) | |
374 | { | |
375 | pixmapwid = GTK_WIDGET(child->data); | |
376 | break; | |
377 | } | |
378 | child = child->next; | |
379 | } | |
380 | ||
381 | /* We should have the pixmap widget now */ | |
382 | wxASSERT(pixmapwid != NULL); | |
383 | ||
384 | if (image == -1) | |
385 | { | |
386 | /* If there's no new widget, just remove the old from the box */ | |
387 | gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid); | |
388 | nb_page->m_image = -1; | |
389 | ||
390 | return true; /* Case 2) */ | |
391 | } | |
392 | } | |
393 | ||
394 | /* Only cases 3) and 4) left */ | |
395 | wxASSERT( m_imageList != NULL ); /* Just in case */ | |
396 | ||
397 | /* Construct the new pixmap */ | |
398 | const wxBitmap *bmp = m_imageList->GetBitmapPtr(image); | |
399 | ||
400 | if (pixmapwid == NULL) | |
401 | { | |
402 | /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */ | |
403 | pixmapwid = gtk_image_new_from_pixbuf(bmp->GetPixbuf()); | |
404 | ||
405 | /* CHECKME: Are these pack flags okay? */ | |
406 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); | |
407 | gtk_widget_show(pixmapwid); | |
408 | } | |
409 | else | |
410 | { | |
411 | /* Case 4) Simply replace the pixmap */ | |
412 | gtk_image_set_from_pixbuf((GtkImage*)pixmapwid, bmp->GetPixbuf()); | |
413 | } | |
414 | ||
415 | nb_page->m_image = image; | |
416 | ||
417 | return true; | |
418 | } | |
419 | ||
420 | void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) ) | |
421 | { | |
422 | wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") ); | |
423 | } | |
424 | ||
425 | void wxNotebook::SetPadding( const wxSize &padding ) | |
426 | { | |
427 | wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") ); | |
428 | ||
429 | m_padding = padding.GetWidth(); | |
430 | ||
431 | int i; | |
432 | for (i=0; i<int(GetPageCount()); i++) | |
433 | { | |
434 | wxGtkNotebookPage* nb_page = GetNotebookPage(i); | |
435 | wxASSERT(nb_page != NULL); | |
436 | ||
437 | if (nb_page->m_image != -1) | |
438 | { | |
439 | // gtk_box_set_child_packing sets padding on BOTH sides | |
440 | // icon provides left padding, label provides center and right | |
441 | int image = nb_page->m_image; | |
442 | SetPageImage(i,-1); | |
443 | SetPageImage(i,image); | |
444 | } | |
445 | wxASSERT(nb_page->m_label); | |
446 | gtk_box_set_child_packing(GTK_BOX(nb_page->m_box), | |
447 | GTK_WIDGET(nb_page->m_label), | |
448 | FALSE, FALSE, m_padding, GTK_PACK_END); | |
449 | } | |
450 | } | |
451 | ||
452 | void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz)) | |
453 | { | |
454 | wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") ); | |
455 | } | |
456 | ||
457 | bool wxNotebook::DeleteAllPages() | |
458 | { | |
459 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid notebook") ); | |
460 | ||
461 | while (m_pagesData.GetCount() > 0) | |
462 | DeletePage( m_pagesData.GetCount()-1 ); | |
463 | ||
464 | wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") ); | |
465 | ||
466 | InvalidateBestSize(); | |
467 | return wxNotebookBase::DeleteAllPages(); | |
468 | } | |
469 | ||
470 | wxNotebookPage *wxNotebook::DoRemovePage( size_t page ) | |
471 | { | |
472 | wxNotebookPage *client = wxNotebookBase::DoRemovePage(page); | |
473 | if ( !client ) | |
474 | return NULL; | |
475 | ||
476 | gtk_widget_ref( client->m_widget ); | |
477 | gtk_widget_unrealize( client->m_widget ); | |
478 | ||
479 | // we don't need to unparent the client->m_widget; GTK+ will do | |
480 | // that for us (and will throw a warning if we do it!) | |
481 | ||
482 | gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page ); | |
483 | ||
484 | wxGtkNotebookPage* p = GetNotebookPage(page); | |
485 | m_pagesData.DeleteObject(p); | |
486 | delete p; | |
487 | ||
488 | return client; | |
489 | } | |
490 | ||
491 | bool wxNotebook::InsertPage( size_t position, | |
492 | wxNotebookPage* win, | |
493 | const wxString& text, | |
494 | bool select, | |
495 | int imageId ) | |
496 | { | |
497 | wxCHECK_MSG( m_widget != NULL, false, wxT("invalid notebook") ); | |
498 | ||
499 | wxCHECK_MSG( win->GetParent() == this, false, | |
500 | wxT("Can't add a page whose parent is not the notebook!") ); | |
501 | ||
502 | wxCHECK_MSG( position <= GetPageCount(), false, | |
503 | _T("invalid page index in wxNotebookPage::InsertPage()") ); | |
504 | ||
505 | // Hack Alert! (Part II): See above in wxInsertChildInNotebook callback | |
506 | // why this has to be done. NOTE: using gtk_widget_unparent here does not | |
507 | // work as it seems to undo too much and will cause errors in the | |
508 | // gtk_notebook_insert_page below, so instead just clear the parent by | |
509 | // hand here. | |
510 | win->m_widget->parent = NULL; | |
511 | ||
512 | if (m_themeEnabled) | |
513 | win->SetThemeEnabled(true); | |
514 | ||
515 | GtkNotebook *notebook = GTK_NOTEBOOK(m_widget); | |
516 | ||
517 | wxGtkNotebookPage *nb_page = new wxGtkNotebookPage(); | |
518 | ||
519 | if ( position == GetPageCount() ) | |
520 | m_pagesData.Append( nb_page ); | |
521 | else | |
522 | m_pagesData.Insert( position, nb_page ); | |
523 | ||
524 | m_pages.Insert(win, position); | |
525 | ||
526 | nb_page->m_box = gtk_hbox_new( FALSE, 1 ); | |
527 | gtk_container_set_border_width((GtkContainer*)nb_page->m_box, 2); | |
528 | ||
529 | g_signal_connect (win->m_widget, "size_allocate", | |
530 | G_CALLBACK (gtk_page_size_callback), win); | |
531 | ||
532 | gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position ); | |
533 | ||
534 | nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data; | |
535 | ||
536 | /* set the label image */ | |
537 | nb_page->m_image = imageId; | |
538 | ||
539 | if (imageId != -1) | |
540 | { | |
541 | wxASSERT( m_imageList != NULL ); | |
542 | ||
543 | const wxBitmap *bmp = m_imageList->GetBitmapPtr(imageId); | |
544 | GtkWidget* pixmapwid = gtk_image_new_from_pixbuf(bmp->GetPixbuf()); | |
545 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); | |
546 | gtk_widget_show(pixmapwid); | |
547 | } | |
548 | ||
549 | /* set the label text */ | |
550 | ||
551 | nb_page->m_text = wxStripMenuCodes(text); | |
552 | if (nb_page->m_text.empty()) nb_page->m_text = wxEmptyString; | |
553 | ||
554 | nb_page->m_label = GTK_LABEL( gtk_label_new(wxGTK_CONV(nb_page->m_text)) ); | |
555 | gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding ); | |
556 | ||
557 | /* apply current style */ | |
558 | GtkRcStyle *style = CreateWidgetStyle(); | |
559 | if ( style ) | |
560 | { | |
561 | gtk_widget_modify_style(GTK_WIDGET(nb_page->m_label), style); | |
562 | gtk_rc_style_unref(style); | |
563 | } | |
564 | ||
565 | /* show the label */ | |
566 | gtk_widget_show( GTK_WIDGET(nb_page->m_label) ); | |
567 | ||
568 | if (select && (m_pagesData.GetCount() > 1)) | |
569 | { | |
570 | SetSelection( position ); | |
571 | } | |
572 | ||
573 | InvalidateBestSize(); | |
574 | return true; | |
575 | } | |
576 | ||
577 | // helper for HitTest(): check if the point lies inside the given widget which | |
578 | // is the child of the notebook whose position and border size are passed as | |
579 | // parameters | |
580 | static bool | |
581 | IsPointInsideWidget(const wxPoint& pt, GtkWidget *w, | |
582 | gint x, gint y, gint border = 0) | |
583 | { | |
584 | return | |
585 | (pt.x >= w->allocation.x - x - border) && | |
586 | (pt.x <= w->allocation.x - x + border + w->allocation.width) && | |
587 | (pt.y >= w->allocation.y - y - border) && | |
588 | (pt.y <= w->allocation.y - y + border + w->allocation.height); | |
589 | } | |
590 | ||
591 | int wxNotebook::HitTest(const wxPoint& pt, long *flags) const | |
592 | { | |
593 | const gint x = m_widget->allocation.x; | |
594 | const gint y = m_widget->allocation.y; | |
595 | ||
596 | const size_t count = GetPageCount(); | |
597 | size_t i = 0; | |
598 | ||
599 | GtkNotebook * notebook = GTK_NOTEBOOK(m_widget); | |
600 | if (gtk_notebook_get_scrollable(notebook)) | |
601 | i = g_list_position( notebook->children, notebook->first_tab ); | |
602 | ||
603 | for ( ; i < count; i++ ) | |
604 | { | |
605 | wxGtkNotebookPage* nb_page = GetNotebookPage(i); | |
606 | GtkWidget *box = nb_page->m_box; | |
607 | ||
608 | const gint border = gtk_container_get_border_width(GTK_CONTAINER(box)); | |
609 | ||
610 | if ( IsPointInsideWidget(pt, box, x, y, border) ) | |
611 | { | |
612 | // ok, we're inside this tab -- now find out where, if needed | |
613 | if ( flags ) | |
614 | { | |
615 | GtkWidget *pixmap = NULL; | |
616 | ||
617 | GList *children = gtk_container_get_children(GTK_CONTAINER(box)); | |
618 | for ( GList *child = children; child; child = child->next ) | |
619 | { | |
620 | if (GTK_IS_IMAGE(child->data)) | |
621 | { | |
622 | pixmap = GTK_WIDGET(child->data); | |
623 | break; | |
624 | } | |
625 | } | |
626 | ||
627 | if ( children ) | |
628 | g_list_free(children); | |
629 | ||
630 | if ( pixmap && IsPointInsideWidget(pt, pixmap, x, y) ) | |
631 | { | |
632 | *flags = wxBK_HITTEST_ONICON; | |
633 | } | |
634 | else if ( IsPointInsideWidget(pt, GTK_WIDGET(nb_page->m_label), x, y) ) | |
635 | { | |
636 | *flags = wxBK_HITTEST_ONLABEL; | |
637 | } | |
638 | else | |
639 | { | |
640 | *flags = wxBK_HITTEST_ONITEM; | |
641 | } | |
642 | } | |
643 | ||
644 | return i; | |
645 | } | |
646 | } | |
647 | ||
648 | if ( flags ) | |
649 | { | |
650 | *flags = wxBK_HITTEST_NOWHERE; | |
651 | wxWindowBase * page = GetCurrentPage(); | |
652 | if ( page ) | |
653 | { | |
654 | // rect origin is in notebook's parent coordinates | |
655 | wxRect rect = page->GetRect(); | |
656 | ||
657 | // adjust it to the notebook's coordinates | |
658 | wxPoint pos = GetPosition(); | |
659 | rect.x -= pos.x; | |
660 | rect.y -= pos.y; | |
661 | if ( rect.Contains( pt ) ) | |
662 | *flags |= wxBK_HITTEST_ONPAGE; | |
663 | } | |
664 | } | |
665 | ||
666 | return wxNOT_FOUND; | |
667 | } | |
668 | ||
669 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) | |
670 | { | |
671 | if (event.IsWindowChange()) | |
672 | AdvanceSelection( event.GetDirection() ); | |
673 | else | |
674 | event.Skip(); | |
675 | } | |
676 | ||
677 | #if wxUSE_CONSTRAINTS | |
678 | ||
679 | // override these 2 functions to do nothing: everything is done in OnSize | |
680 | void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) ) | |
681 | { | |
682 | // don't set the sizes of the pages - their correct size is not yet known | |
683 | wxControl::SetConstraintSizes(false); | |
684 | } | |
685 | ||
686 | bool wxNotebook::DoPhase( int WXUNUSED(nPhase) ) | |
687 | { | |
688 | return true; | |
689 | } | |
690 | ||
691 | #endif | |
692 | ||
693 | void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style) | |
694 | { | |
695 | gtk_widget_modify_style(m_widget, style); | |
696 | size_t cnt = m_pagesData.GetCount(); | |
697 | for (size_t i = 0; i < cnt; i++) | |
698 | gtk_widget_modify_style(GTK_WIDGET(GetNotebookPage(i)->m_label), style); | |
699 | } | |
700 | ||
701 | GdkWindow *wxNotebook::GTKGetWindow(wxArrayGdkWindows& windows) const | |
702 | { | |
703 | windows.push_back(m_widget->window); | |
704 | windows.push_back(GTK_NOTEBOOK(m_widget)->event_window); | |
705 | ||
706 | return NULL; | |
707 | } | |
708 | ||
709 | // static | |
710 | wxVisualAttributes | |
711 | wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
712 | { | |
713 | return GetDefaultAttributesFromGTKWidget(gtk_notebook_new); | |
714 | } | |
715 | ||
716 | //----------------------------------------------------------------------------- | |
717 | // wxNotebookEvent | |
718 | //----------------------------------------------------------------------------- | |
719 | ||
720 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) | |
721 | ||
722 | #endif |