]>
Commit | Line | Data |
---|---|---|
53b28675 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: notebook.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin |
65571936 | 7 | // Licence: wxWindows licence |
53b28675 RR |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
53b28675 RR |
11 | #pragma implementation "notebook.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
53b28675 | 17 | #include "wx/notebook.h" |
dcf924a3 RR |
18 | |
19 | #if wxUSE_NOTEBOOK | |
20 | ||
53b28675 RR |
21 | #include "wx/panel.h" |
22 | #include "wx/utils.h" | |
23 | #include "wx/imaglist.h" | |
30dea054 | 24 | #include "wx/intl.h" |
4bf58c62 | 25 | #include "wx/log.h" |
22cbd10e | 26 | #include "wx/bitmap.h" |
c077ee94 | 27 | #include "wx/fontutil.h" |
83624f79 | 28 | |
9e691f46 | 29 | #include "wx/gtk/private.h" |
83624f79 | 30 | #include "wx/gtk/win_gtk.h" |
9e691f46 | 31 | |
5e7e9e1b | 32 | #include <gdk/gdkkeysyms.h> |
b292e2f5 | 33 | |
c077ee94 RR |
34 | #include "wx/msgdlg.h" |
35 | ||
2e4df4bf VZ |
36 | // ---------------------------------------------------------------------------- |
37 | // events | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) | |
41 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
42 | ||
acfd422a RR |
43 | //----------------------------------------------------------------------------- |
44 | // idle system | |
45 | //----------------------------------------------------------------------------- | |
46 | ||
47 | extern void wxapp_install_idle_handler(); | |
48 | extern bool g_isIdle; | |
49 | ||
b292e2f5 RR |
50 | //----------------------------------------------------------------------------- |
51 | // data | |
52 | //----------------------------------------------------------------------------- | |
53 | ||
07b8d7ec | 54 | extern bool g_blockEventsOnDrag; |
53b28675 | 55 | |
219f895a | 56 | //----------------------------------------------------------------------------- |
80a58c99 | 57 | // wxGtkNotebookPage |
219f895a RR |
58 | //----------------------------------------------------------------------------- |
59 | ||
07b8d7ec VZ |
60 | // VZ: this is rather ugly as we keep the pages themselves in an array (it |
61 | // allows us to have quite a few functions implemented in the base class) | |
62 | // but the page data is kept in a separate list, so we must maintain them | |
63 | // in sync manually... of course, the list had been there before the base | |
64 | // class which explains it but it still would be nice to do something | |
65 | // about this one day | |
66 | ||
80a58c99 | 67 | class wxGtkNotebookPage: public wxObject |
219f895a RR |
68 | { |
69 | public: | |
c077ee94 RR |
70 | wxGtkNotebookPage() |
71 | { | |
72 | m_image = -1; | |
73 | m_page = (GtkNotebookPage *) NULL; | |
74 | m_box = (GtkWidget *) NULL; | |
c077ee94 RR |
75 | } |
76 | ||
77 | bool SetFont(const wxFont& font); | |
78 | ||
79 | wxString m_text; | |
80 | int m_image; | |
81 | GtkNotebookPage *m_page; | |
82 | GtkLabel *m_label; | |
83 | GtkWidget *m_box; // in which the label and image are packed | |
219f895a RR |
84 | }; |
85 | ||
c077ee94 RR |
86 | |
87 | bool wxGtkNotebookPage::SetFont(const wxFont& font) | |
88 | { | |
89 | if (!m_label) | |
90 | return false; | |
91 | ||
c077ee94 | 92 | #ifdef __WXGTK20__ |
f40fdaa3 VS |
93 | gtk_widget_modify_font(GTK_WIDGET(m_label), |
94 | font.GetNativeFontInfo()->description); | |
c077ee94 | 95 | #else |
f40fdaa3 VS |
96 | GtkRcStyle *style = gtk_rc_style_new(); |
97 | style->fontset_name = | |
98 | g_strdup(font.GetNativeFontInfo()->GetXFontName().c_str()); | |
99 | gtk_widget_modify_style(GTK_WIDGET(m_label), style); | |
100 | gtk_rc_style_unref(style); | |
c077ee94 RR |
101 | #endif |
102 | ||
c077ee94 RR |
103 | return true; |
104 | } | |
105 | ||
106 | ||
07b8d7ec VZ |
107 | #include "wx/listimpl.cpp" |
108 | WX_DEFINE_LIST(wxGtkNotebookPagesList); | |
109 | ||
c077ee94 | 110 | |
ff829f3f | 111 | //----------------------------------------------------------------------------- |
5b011451 | 112 | // "switch_page" |
ff829f3f VZ |
113 | //----------------------------------------------------------------------------- |
114 | ||
219f895a RR |
115 | static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget), |
116 | GtkNotebookPage *WXUNUSED(page), | |
587ce561 RR |
117 | gint page, |
118 | wxNotebook *notebook ) | |
ff829f3f | 119 | { |
36202885 VZ |
120 | // are you trying to call SetSelection() from a notebook event handler? |
121 | // you shouldn't! | |
2b5f62a0 | 122 | wxCHECK_RET( !notebook->m_inSwitchPage, |
36202885 VZ |
123 | _T("gtk_notebook_page_change_callback reentered") ); |
124 | ||
2b5f62a0 | 125 | notebook->m_inSwitchPage = TRUE; |
a6aa9b1e | 126 | if (g_isIdle) |
587ce561 | 127 | wxapp_install_idle_handler(); |
ff829f3f | 128 | |
b292e2f5 | 129 | int old = notebook->GetSelection(); |
ff829f3f | 130 | |
36202885 VZ |
131 | wxNotebookEvent eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, |
132 | notebook->GetId(), page, old ); | |
133 | eventChanging.SetEventObject( notebook ); | |
a6aa9b1e | 134 | |
36202885 VZ |
135 | if ( (notebook->GetEventHandler()->ProcessEvent(eventChanging)) && |
136 | !eventChanging.IsAllowed() ) | |
587ce561 RR |
137 | { |
138 | /* program doesn't allow the page change */ | |
36202885 VZ |
139 | gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook->m_widget), |
140 | "switch_page" ); | |
141 | } | |
142 | else // change allowed | |
143 | { | |
144 | // make wxNotebook::GetSelection() return the correct (i.e. consistent | |
145 | // with wxNotebookEvent::GetSelection()) value even though the page is | |
146 | // not really changed in GTK+ | |
147 | notebook->m_selection = page; | |
148 | ||
149 | wxNotebookEvent eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, | |
150 | notebook->GetId(), page, old ); | |
151 | eventChanged.SetEventObject( notebook ); | |
152 | notebook->GetEventHandler()->ProcessEvent( eventChanged ); | |
587ce561 | 153 | } |
ef44a621 | 154 | |
2b5f62a0 | 155 | notebook->m_inSwitchPage = FALSE; |
ff829f3f VZ |
156 | } |
157 | ||
5b011451 RR |
158 | //----------------------------------------------------------------------------- |
159 | // "size_allocate" | |
160 | //----------------------------------------------------------------------------- | |
161 | ||
33d0b396 | 162 | static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win ) |
caac5181 | 163 | { |
a6aa9b1e | 164 | if (g_isIdle) |
587ce561 | 165 | wxapp_install_idle_handler(); |
6d693bb4 | 166 | |
a2053b27 RR |
167 | if ((win->m_x == alloc->x) && |
168 | (win->m_y == alloc->y) && | |
169 | (win->m_width == alloc->width) && | |
170 | (win->m_height == alloc->height)) | |
b292e2f5 | 171 | { |
58dea4b0 | 172 | return; |
b292e2f5 | 173 | } |
a6aa9b1e | 174 | |
b292e2f5 | 175 | win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height ); |
f861258f | 176 | |
d7928388 RR |
177 | /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate |
178 | here in order to make repositioning after resizing to take effect. */ | |
179 | if ((gtk_major_version == 1) && | |
180 | (gtk_minor_version == 2) && | |
8712c6e7 VZ |
181 | (gtk_micro_version < 6) && |
182 | (win->m_wxwindow) && | |
183 | (GTK_WIDGET_REALIZED(win->m_wxwindow))) | |
d7928388 RR |
184 | { |
185 | gtk_widget_size_allocate( win->m_wxwindow, alloc ); | |
186 | } | |
6d693bb4 RR |
187 | } |
188 | ||
189 | //----------------------------------------------------------------------------- | |
190 | // "realize" from m_widget | |
191 | //----------------------------------------------------------------------------- | |
192 | ||
6d693bb4 RR |
193 | static gint |
194 | gtk_notebook_realized_callback( GtkWidget * WXUNUSED(widget), wxWindow *win ) | |
195 | { | |
196 | if (g_isIdle) | |
197 | wxapp_install_idle_handler(); | |
198 | ||
d7928388 RR |
199 | /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize |
200 | here in order to make repositioning before showing to take effect. */ | |
6d693bb4 RR |
201 | gtk_widget_queue_resize( win->m_widget ); |
202 | ||
203 | return FALSE; | |
b292e2f5 RR |
204 | } |
205 | ||
8253c7fd | 206 | //----------------------------------------------------------------------------- |
8712c6e7 | 207 | // "key_press_event" |
8253c7fd RR |
208 | //----------------------------------------------------------------------------- |
209 | ||
210 | static gint gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *win ) | |
211 | { | |
212 | if (g_isIdle) | |
213 | wxapp_install_idle_handler(); | |
214 | ||
215 | if (!win->m_hasVMT) return FALSE; | |
216 | if (g_blockEventsOnDrag) return FALSE; | |
217 | ||
218 | /* win is a control: tab can be propagated up */ | |
219 | if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) | |
220 | { | |
07b8d7ec | 221 | int sel = win->GetSelection(); |
461573cc RR |
222 | if (sel == -1) |
223 | return TRUE; | |
b318dc42 JS |
224 | wxGtkNotebookPage *nb_page = win->GetNotebookPage(sel); |
225 | wxCHECK_MSG( nb_page, FALSE, _T("invalid selection in wxNotebook") ); | |
8253c7fd RR |
226 | |
227 | wxNavigationKeyEvent event; | |
228 | event.SetEventObject( win ); | |
229 | /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */ | |
230 | event.SetDirection( (gdk_event->keyval == GDK_Tab) ); | |
231 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
cc023d9f | 232 | event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ); |
8253c7fd | 233 | event.SetCurrentFocus( win ); |
07b8d7ec VZ |
234 | |
235 | wxNotebookPage *client = win->GetPage(sel); | |
236 | if ( !client->GetEventHandler()->ProcessEvent( event ) ) | |
8253c7fd | 237 | { |
07b8d7ec | 238 | client->SetFocus(); |
8253c7fd | 239 | } |
8712c6e7 | 240 | |
8253c7fd RR |
241 | gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" ); |
242 | return TRUE; | |
243 | } | |
8712c6e7 | 244 | |
8253c7fd RR |
245 | return FALSE; |
246 | } | |
247 | ||
6ca41e57 RR |
248 | //----------------------------------------------------------------------------- |
249 | // InsertChild callback for wxNotebook | |
250 | //----------------------------------------------------------------------------- | |
251 | ||
d7f1759a | 252 | static void wxInsertChildInNotebook( wxNotebook* parent, wxWindow* child ) |
6ca41e57 | 253 | { |
d7f1759a RR |
254 | // Hack alert! We manually set the child window |
255 | // parent field so that GTK can query the | |
2ded391d VS |
256 | // notebook's style and font. Without that, GetBestSize could return |
257 | // incorrect size, see bug #901694 for details | |
258 | // (http://sourceforge.net/tracker/?func=detail&aid=901694&group_id=9863&atid=109863) | |
d7f1759a | 259 | child->m_widget->parent = parent->m_widget; |
6ca41e57 RR |
260 | } |
261 | ||
53b28675 RR |
262 | //----------------------------------------------------------------------------- |
263 | // wxNotebook | |
264 | //----------------------------------------------------------------------------- | |
265 | ||
53b28675 RR |
266 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl) |
267 | ||
b98d804b RR |
268 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) |
269 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
270 | END_EVENT_TABLE() | |
f861258f | 271 | |
ff829f3f | 272 | void wxNotebook::Init() |
53b28675 | 273 | { |
b318dc42 | 274 | m_padding = 0; |
2b5f62a0 VZ |
275 | m_inSwitchPage = FALSE; |
276 | ||
b292e2f5 | 277 | m_imageList = (wxImageList *) NULL; |
36202885 | 278 | m_selection = -1; |
a2d93e73 | 279 | m_themeEnabled = TRUE; |
ff829f3f VZ |
280 | } |
281 | ||
282 | wxNotebook::wxNotebook() | |
283 | { | |
b292e2f5 | 284 | Init(); |
ff7b1510 | 285 | } |
53b28675 | 286 | |
debe6624 | 287 | wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id, |
53b28675 | 288 | const wxPoint& pos, const wxSize& size, |
debe6624 | 289 | long style, const wxString& name ) |
53b28675 | 290 | { |
b292e2f5 RR |
291 | Init(); |
292 | Create( parent, id, pos, size, style, name ); | |
ff7b1510 | 293 | } |
53b28675 | 294 | |
ff829f3f | 295 | wxNotebook::~wxNotebook() |
53b28675 | 296 | { |
b292e2f5 | 297 | DeleteAllPages(); |
ff7b1510 | 298 | } |
53b28675 | 299 | |
debe6624 | 300 | bool wxNotebook::Create(wxWindow *parent, wxWindowID id, |
07b8d7ec VZ |
301 | const wxPoint& pos, const wxSize& size, |
302 | long style, const wxString& name ) | |
53b28675 | 303 | { |
b292e2f5 RR |
304 | m_needParent = TRUE; |
305 | m_acceptsFocus = TRUE; | |
306 | m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook; | |
307 | ||
4dcaf11a RR |
308 | if (!PreCreation( parent, pos, size ) || |
309 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
310 | { | |
223d09f6 | 311 | wxFAIL_MSG( wxT("wxNoteBook creation failed") ); |
8712c6e7 | 312 | return FALSE; |
4dcaf11a RR |
313 | } |
314 | ||
ff829f3f | 315 | |
b292e2f5 | 316 | m_widget = gtk_notebook_new(); |
53b28675 | 317 | |
b292e2f5 | 318 | gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 ); |
caac5181 | 319 | |
587ce561 RR |
320 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
321 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); | |
ff829f3f | 322 | |
f03fc89f | 323 | m_parent->DoAddChild( this ); |
ef44a621 | 324 | |
8712c6e7 VZ |
325 | if (m_windowStyle & wxNB_RIGHT) |
326 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT ); | |
327 | if (m_windowStyle & wxNB_LEFT) | |
328 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT ); | |
329 | if (m_windowStyle & wxNB_BOTTOM) | |
330 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM ); | |
a3a7f879 | 331 | |
8253c7fd RR |
332 | gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event", |
333 | GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this ); | |
334 | ||
abdeb9e7 | 335 | PostCreation(size); |
ff829f3f | 336 | |
6d693bb4 RR |
337 | gtk_signal_connect( GTK_OBJECT(m_widget), "realize", |
338 | GTK_SIGNAL_FUNC(gtk_notebook_realized_callback), (gpointer) this ); | |
a6aa9b1e | 339 | |
b292e2f5 | 340 | return TRUE; |
ff7b1510 | 341 | } |
53b28675 | 342 | |
ff829f3f | 343 | int wxNotebook::GetSelection() const |
53b28675 | 344 | { |
223d09f6 | 345 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
53b28675 | 346 | |
36202885 VZ |
347 | if ( m_selection == -1 ) |
348 | { | |
b318dc42 | 349 | GList *nb_pages = GTK_NOTEBOOK(m_widget)->children; |
53b28675 | 350 | |
b318dc42 | 351 | if (g_list_length(nb_pages) != 0) |
36202885 VZ |
352 | { |
353 | GtkNotebook *notebook = GTK_NOTEBOOK(m_widget); | |
a6aa9b1e | 354 | |
36202885 VZ |
355 | gpointer cur = notebook->cur_page; |
356 | if ( cur != NULL ) | |
357 | { | |
358 | wxConstCast(this, wxNotebook)->m_selection = | |
b318dc42 | 359 | g_list_index( nb_pages, cur ); |
36202885 VZ |
360 | } |
361 | } | |
362 | } | |
a6aa9b1e | 363 | |
36202885 | 364 | return m_selection; |
ff7b1510 | 365 | } |
53b28675 | 366 | |
789d0a3d | 367 | wxString wxNotebook::GetPageText( size_t page ) const |
53b28675 | 368 | { |
223d09f6 | 369 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") ); |
ef44a621 | 370 | |
80a58c99 | 371 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
b292e2f5 RR |
372 | if (nb_page) |
373 | return nb_page->m_text; | |
374 | else | |
223d09f6 | 375 | return wxT(""); |
ff7b1510 | 376 | } |
53b28675 | 377 | |
789d0a3d | 378 | int wxNotebook::GetPageImage( size_t page ) const |
53b28675 | 379 | { |
223d09f6 | 380 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
a81258be | 381 | |
80a58c99 | 382 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
b292e2f5 RR |
383 | if (nb_page) |
384 | return nb_page->m_image; | |
385 | else | |
587ce561 | 386 | return -1; |
ff7b1510 | 387 | } |
53b28675 | 388 | |
80a58c99 | 389 | wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const |
53b28675 | 390 | { |
80a58c99 | 391 | wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") ); |
ff829f3f | 392 | |
07b8d7ec | 393 | wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") ); |
a6aa9b1e | 394 | |
07b8d7ec | 395 | return m_pagesData.Item(page)->GetData(); |
ff7b1510 | 396 | } |
53b28675 | 397 | |
789d0a3d | 398 | int wxNotebook::SetSelection( size_t page ) |
53b28675 | 399 | { |
223d09f6 | 400 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
a81258be | 401 | |
789d0a3d | 402 | wxCHECK_MSG( page < m_pagesData.GetCount(), -1, wxT("invalid notebook index") ); |
ff829f3f | 403 | |
587ce561 | 404 | int selOld = GetSelection(); |
a6aa9b1e | 405 | |
36202885 VZ |
406 | // cache the selection |
407 | m_selection = page; | |
587ce561 | 408 | gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page ); |
ff829f3f | 409 | |
07b8d7ec VZ |
410 | wxNotebookPage *client = GetPage(page); |
411 | if ( client ) | |
412 | client->SetFocus(); | |
b656febd | 413 | |
07b8d7ec | 414 | return selOld; |
ff7b1510 | 415 | } |
53b28675 | 416 | |
789d0a3d | 417 | bool wxNotebook::SetPageText( size_t page, const wxString &text ) |
53b28675 | 418 | { |
223d09f6 | 419 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
a81258be | 420 | |
80a58c99 | 421 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
ef44a621 | 422 | |
223d09f6 | 423 | wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") ); |
ff829f3f | 424 | |
3eb78d7e | 425 | nb_page->m_text = text; |
ff829f3f | 426 | |
fab591c5 | 427 | gtk_label_set( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) ); |
a6aa9b1e | 428 | |
3eb78d7e | 429 | return TRUE; |
ff7b1510 | 430 | } |
53b28675 | 431 | |
789d0a3d | 432 | bool wxNotebook::SetPageImage( size_t page, int image ) |
53b28675 | 433 | { |
3eb78d7e | 434 | /* HvdH 28-12-98: now it works, but it's a bit of a kludge */ |
f861258f | 435 | |
80a58c99 | 436 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
ef44a621 | 437 | |
3eb78d7e | 438 | if (!nb_page) return FALSE; |
f861258f | 439 | |
3eb78d7e RR |
440 | /* Optimization posibility: return immediately if image unchanged. |
441 | * Not enabled because it may break existing (stupid) code that | |
442 | * manipulates the imagelist to cycle images */ | |
f861258f | 443 | |
3eb78d7e | 444 | /* if (image == nb_page->m_image) return TRUE; */ |
f861258f VZ |
445 | |
446 | /* For different cases: | |
3eb78d7e RR |
447 | 1) no image -> no image |
448 | 2) image -> no image | |
449 | 3) no image -> image | |
450 | 4) image -> image */ | |
f861258f | 451 | |
3eb78d7e RR |
452 | if (image == -1 && nb_page->m_image == -1) |
453 | return TRUE; /* Case 1): Nothing to do. */ | |
f861258f | 454 | |
bbe0af5b | 455 | GtkWidget *pixmapwid = (GtkWidget*) NULL; |
f861258f VZ |
456 | |
457 | if (nb_page->m_image != -1) | |
3eb78d7e RR |
458 | { |
459 | /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */ | |
f861258f | 460 | |
279b5e2e VZ |
461 | GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box)); |
462 | while (child) | |
8712c6e7 | 463 | { |
f861258f | 464 | if (GTK_IS_PIXMAP(child->data)) |
8712c6e7 VZ |
465 | { |
466 | pixmapwid = GTK_WIDGET(child->data); | |
467 | break; | |
3eb78d7e | 468 | } |
279b5e2e | 469 | child = child->next; |
8712c6e7 | 470 | } |
f861258f | 471 | |
3eb78d7e | 472 | /* We should have the pixmap widget now */ |
f861258f VZ |
473 | wxASSERT(pixmapwid != NULL); |
474 | ||
475 | if (image == -1) | |
8712c6e7 | 476 | { |
3eb78d7e RR |
477 | /* If there's no new widget, just remove the old from the box */ |
478 | gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid); | |
479 | nb_page->m_image = -1; | |
53b28675 | 480 | |
3eb78d7e RR |
481 | return TRUE; /* Case 2) */ |
482 | } | |
483 | } | |
f861258f | 484 | |
3eb78d7e RR |
485 | /* Only cases 3) and 4) left */ |
486 | wxASSERT( m_imageList != NULL ); /* Just in case */ | |
f861258f | 487 | |
3eb78d7e RR |
488 | /* Construct the new pixmap */ |
489 | const wxBitmap *bmp = m_imageList->GetBitmap(image); | |
490 | GdkPixmap *pixmap = bmp->GetPixmap(); | |
491 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
f861258f | 492 | if ( bmp->GetMask() ) |
3eb78d7e RR |
493 | { |
494 | mask = bmp->GetMask()->GetBitmap(); | |
495 | } | |
f861258f VZ |
496 | |
497 | if (pixmapwid == NULL) | |
3eb78d7e RR |
498 | { |
499 | /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */ | |
500 | pixmapwid = gtk_pixmap_new (pixmap, mask ); | |
f861258f | 501 | |
3eb78d7e | 502 | /* CHECKME: Are these pack flags okay? */ |
b318dc42 | 503 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); |
3eb78d7e RR |
504 | gtk_widget_show(pixmapwid); |
505 | } | |
f861258f | 506 | else |
3eb78d7e RR |
507 | { |
508 | /* Case 4) Simply replace the pixmap */ | |
509 | gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask); | |
510 | } | |
f861258f | 511 | |
3eb78d7e | 512 | nb_page->m_image = image; |
53b28675 | 513 | |
3eb78d7e | 514 | return TRUE; |
ff7b1510 | 515 | } |
53b28675 RR |
516 | |
517 | void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) ) | |
518 | { | |
223d09f6 | 519 | wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") ); |
ff7b1510 | 520 | } |
53b28675 | 521 | |
b318dc42 | 522 | void wxNotebook::SetPadding( const wxSize &padding ) |
53b28675 | 523 | { |
b318dc42 JS |
524 | wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") ); |
525 | ||
526 | m_padding = padding.GetWidth(); | |
527 | ||
528 | int i; | |
529 | for (i=0; i<int(GetPageCount()); i++) | |
530 | { | |
531 | wxGtkNotebookPage* nb_page = GetNotebookPage(i); | |
532 | wxASSERT(nb_page != NULL); | |
533 | ||
534 | if (nb_page->m_image != -1) | |
535 | { | |
536 | // gtk_box_set_child_packing sets padding on BOTH sides | |
537 | // icon provides left padding, label provides center and right | |
538 | int image = nb_page->m_image; | |
539 | SetPageImage(i,-1); | |
540 | SetPageImage(i,image); | |
541 | } | |
542 | wxASSERT(nb_page->m_label); | |
543 | gtk_box_set_child_packing(GTK_BOX(nb_page->m_box), | |
544 | GTK_WIDGET(nb_page->m_label), | |
545 | FALSE, FALSE, m_padding, GTK_PACK_END); | |
546 | } | |
ff7b1510 | 547 | } |
53b28675 | 548 | |
74e3313b | 549 | void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz)) |
ca8b28f2 | 550 | { |
223d09f6 | 551 | wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") ); |
ca8b28f2 JS |
552 | } |
553 | ||
ff829f3f | 554 | bool wxNotebook::DeleteAllPages() |
53b28675 | 555 | { |
223d09f6 | 556 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
a81258be | 557 | |
07b8d7ec VZ |
558 | while (m_pagesData.GetCount() > 0) |
559 | DeletePage( m_pagesData.GetCount()-1 ); | |
560 | ||
561 | wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") ); | |
ff829f3f | 562 | |
10199e27 | 563 | return wxNotebookBase::DeleteAllPages(); |
ff7b1510 | 564 | } |
53b28675 | 565 | |
789d0a3d | 566 | bool wxNotebook::DeletePage( size_t page ) |
53b28675 | 567 | { |
5cb4253e | 568 | if ( m_selection == (int)m_pagesData.GetCount() - 1 ) |
36202885 | 569 | { |
5cb4253e VZ |
570 | // the index will become invalid after the page is deleted |
571 | m_selection = -1; | |
36202885 | 572 | } |
a6aa9b1e | 573 | |
10199e27 | 574 | // it will call our DoRemovePage() to do the real work |
07b8d7ec | 575 | return wxNotebookBase::DeletePage(page); |
fed46e72 RR |
576 | } |
577 | ||
789d0a3d | 578 | wxNotebookPage *wxNotebook::DoRemovePage( size_t page ) |
fed46e72 | 579 | { |
10199e27 VZ |
580 | wxNotebookPage *client = wxNotebookBase::DoRemovePage(page); |
581 | if ( !client ) | |
582 | return NULL; | |
fed46e72 | 583 | |
07b8d7ec VZ |
584 | gtk_widget_ref( client->m_widget ); |
585 | gtk_widget_unrealize( client->m_widget ); | |
586 | gtk_widget_unparent( client->m_widget ); | |
587 | ||
5cb4253e VZ |
588 | // gtk_notebook_remove_page() sends "switch_page" signal with some strange |
589 | // new page index (when deleting selected page 0, new page is 1 although, | |
590 | // clearly, the selection should stay 0), so suppress this | |
591 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), | |
592 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this ); | |
593 | ||
587ce561 | 594 | gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page ); |
ff829f3f | 595 | |
5cb4253e VZ |
596 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
597 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); | |
598 | ||
222ed1d6 MB |
599 | wxGtkNotebookPage* p = GetNotebookPage(page); |
600 | m_pagesData.DeleteObject(p); | |
601 | delete p; | |
ff829f3f | 602 | |
07b8d7ec | 603 | return client; |
ff7b1510 | 604 | } |
53b28675 | 605 | |
789d0a3d | 606 | bool wxNotebook::InsertPage( size_t position, |
07b8d7ec VZ |
607 | wxNotebookPage* win, |
608 | const wxString& text, | |
609 | bool select, | |
610 | int imageId ) | |
53b28675 | 611 | { |
223d09f6 | 612 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
a81258be | 613 | |
587ce561 | 614 | wxCHECK_MSG( win->GetParent() == this, FALSE, |
223d09f6 | 615 | wxT("Can't add a page whose parent is not the notebook!") ); |
8aadf227 | 616 | |
789d0a3d | 617 | wxCHECK_MSG( position <= GetPageCount(), FALSE, |
07b8d7ec VZ |
618 | _T("invalid page index in wxNotebookPage::InsertPage()") ); |
619 | ||
d7f1759a RR |
620 | // Hack alert part II! See above in InsertChildInNotebook |
621 | // callback why this has to be done. | |
622 | win->m_widget->parent = NULL; | |
623 | ||
624 | // don't receive switch page during addition | |
a6aa9b1e | 625 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
587ce561 | 626 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this ); |
a6aa9b1e | 627 | |
a2d93e73 JS |
628 | if (m_themeEnabled) |
629 | win->SetThemeEnabled(TRUE); | |
630 | ||
587ce561 | 631 | GtkNotebook *notebook = GTK_NOTEBOOK(m_widget); |
53b28675 | 632 | |
b318dc42 | 633 | wxGtkNotebookPage *nb_page = new wxGtkNotebookPage(); |
a6aa9b1e | 634 | |
07b8d7ec | 635 | if ( position == GetPageCount() ) |
b318dc42 | 636 | m_pagesData.Append( nb_page ); |
587ce561 | 637 | else |
b318dc42 | 638 | m_pagesData.Insert( m_pagesData.Item( position ), nb_page ); |
a6aa9b1e | 639 | |
07b8d7ec | 640 | m_pages.Insert(win, position); |
8aadf227 | 641 | |
b318dc42 JS |
642 | nb_page->m_box = gtk_hbox_new( FALSE, 1 ); |
643 | gtk_container_border_width( GTK_CONTAINER(nb_page->m_box), 2 ); | |
587ce561 | 644 | |
d1af991f RR |
645 | gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate", |
646 | GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win ); | |
647 | ||
1cc4f822 JJ |
648 | #ifndef __VMS |
649 | // On VMS position is unsigned and thus always positive | |
650 | if (position < 0) | |
b318dc42 | 651 | gtk_notebook_append_page( notebook, win->m_widget, nb_page->m_box ); |
a6aa9b1e | 652 | else |
1cc4f822 JJ |
653 | #endif |
654 | gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position ); | |
587ce561 | 655 | |
b318dc42 | 656 | nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data; |
ef44a621 | 657 | |
587ce561 | 658 | /* set the label image */ |
b318dc42 | 659 | nb_page->m_image = imageId; |
a6aa9b1e | 660 | |
3eb78d7e | 661 | if (imageId != -1) |
e4a81a2e | 662 | { |
3eb78d7e RR |
663 | wxASSERT( m_imageList != NULL ); |
664 | ||
665 | const wxBitmap *bmp = m_imageList->GetBitmap(imageId); | |
666 | GdkPixmap *pixmap = bmp->GetPixmap(); | |
667 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
668 | if ( bmp->GetMask() ) | |
669 | { | |
670 | mask = bmp->GetMask()->GetBitmap(); | |
671 | } | |
e4a81a2e | 672 | |
3eb78d7e | 673 | GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask ); |
24d20a8f | 674 | |
b318dc42 | 675 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); |
24d20a8f | 676 | |
3eb78d7e RR |
677 | gtk_widget_show(pixmapwid); |
678 | } | |
24d20a8f | 679 | |
587ce561 | 680 | /* set the label text */ |
c077ee94 | 681 | |
b318dc42 JS |
682 | nb_page->m_text = text; |
683 | if (nb_page->m_text.IsEmpty()) nb_page->m_text = wxT(""); | |
279b5e2e | 684 | |
73e68c1d | 685 | nb_page->m_label = GTK_LABEL( gtk_label_new(wxGTK_CONV(nb_page->m_text)) ); |
b318dc42 | 686 | gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding ); |
279b5e2e | 687 | |
587ce561 | 688 | /* show the label */ |
b318dc42 | 689 | gtk_widget_show( GTK_WIDGET(nb_page->m_label) ); |
07b8d7ec | 690 | if (select && (m_pagesData.GetCount() > 1)) |
587ce561 | 691 | { |
1cc4f822 JJ |
692 | #ifndef __VMS |
693 | // On VMS position is unsigned and thus always positive | |
587ce561 RR |
694 | if (position < 0) |
695 | SetSelection( GetPageCount()-1 ); | |
8712c6e7 | 696 | else |
1cc4f822 JJ |
697 | #endif |
698 | SetSelection( position ); | |
587ce561 | 699 | } |
741fd203 | 700 | |
587ce561 RR |
701 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
702 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); | |
ff829f3f | 703 | |
3eb78d7e | 704 | return TRUE; |
ff7b1510 | 705 | } |
53b28675 | 706 | |
279b5e2e VZ |
707 | // helper for HitTest(): check if the point lies inside the given widget which |
708 | // is the child of the notebook whose position and border size are passed as | |
709 | // parameters | |
710 | static bool | |
711 | IsPointInsideWidget(const wxPoint& pt, GtkWidget *w, | |
712 | gint x, gint y, gint border = 0) | |
713 | { | |
714 | return | |
715 | (pt.x >= w->allocation.x - x - border) && | |
716 | (pt.x <= w->allocation.x - x + border + w->allocation.width) && | |
717 | (pt.y >= w->allocation.y - y - border) && | |
718 | (pt.y <= w->allocation.y - y + border + w->allocation.height); | |
719 | } | |
720 | ||
721 | int wxNotebook::HitTest(const wxPoint& pt, long *flags) const | |
722 | { | |
723 | const gint x = m_widget->allocation.x; | |
724 | const gint y = m_widget->allocation.y; | |
725 | ||
726 | const size_t count = GetPageCount(); | |
727 | for ( size_t i = 0; i < count; i++ ) | |
728 | { | |
729 | wxGtkNotebookPage* nb_page = GetNotebookPage(i); | |
730 | GtkWidget *box = nb_page->m_box; | |
731 | ||
732 | // VZ: don't know how to find the border width in GTK+ 1.2 | |
733 | #ifdef __WXGTK20__ | |
734 | const gint border = gtk_container_get_border_width(GTK_CONTAINER(box)); | |
735 | #else // !GTK+ 2.x | |
736 | const gint border = 0; | |
737 | #endif | |
738 | if ( IsPointInsideWidget(pt, box, x, y, border) ) | |
739 | { | |
740 | // ok, we're inside this tab -- now find out where, if needed | |
741 | if ( flags ) | |
742 | { | |
743 | GtkWidget *pixmap = NULL; | |
744 | ||
745 | GList *children = gtk_container_children(GTK_CONTAINER(box)); | |
746 | for ( GList *child = children; child; child = child->next ) | |
747 | { | |
748 | if ( GTK_IS_PIXMAP(child->data) ) | |
749 | { | |
750 | pixmap = GTK_WIDGET(child->data); | |
751 | break; | |
752 | } | |
753 | } | |
754 | ||
755 | if ( children ) | |
756 | g_list_free(children); | |
757 | ||
758 | if ( pixmap && IsPointInsideWidget(pt, pixmap, x, y) ) | |
759 | { | |
760 | *flags = wxNB_HITTEST_ONICON; | |
761 | } | |
762 | else if ( IsPointInsideWidget(pt, GTK_WIDGET(nb_page->m_label), x, y) ) | |
763 | { | |
764 | *flags = wxNB_HITTEST_ONLABEL; | |
765 | } | |
766 | else | |
767 | { | |
768 | *flags = wxNB_HITTEST_ONITEM; | |
769 | } | |
770 | } | |
771 | ||
772 | return i; | |
773 | } | |
774 | } | |
775 | ||
776 | if ( flags ) | |
777 | *flags = wxNB_HITTEST_NOWHERE; | |
778 | ||
779 | return wxNOT_FOUND; | |
780 | } | |
781 | ||
b98d804b RR |
782 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) |
783 | { | |
f861258f | 784 | if (event.IsWindowChange()) |
b98d804b | 785 | AdvanceSelection( event.GetDirection() ); |
f861258f | 786 | else |
b98d804b RR |
787 | event.Skip(); |
788 | } | |
789 | ||
93d38175 VS |
790 | #if wxUSE_CONSTRAINTS |
791 | ||
5a8c929e | 792 | // override these 2 functions to do nothing: everything is done in OnSize |
e3e65dac | 793 | void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) ) |
5a8c929e | 794 | { |
b292e2f5 RR |
795 | // don't set the sizes of the pages - their correct size is not yet known |
796 | wxControl::SetConstraintSizes(FALSE); | |
5a8c929e VZ |
797 | } |
798 | ||
e3e65dac | 799 | bool wxNotebook::DoPhase( int WXUNUSED(nPhase) ) |
5a8c929e | 800 | { |
b292e2f5 | 801 | return TRUE; |
5a8c929e VZ |
802 | } |
803 | ||
93d38175 VS |
804 | #endif |
805 | ||
f40fdaa3 | 806 | void wxNotebook::DoApplyWidgetStyle(GtkRcStyle *style) |
a81258be | 807 | { |
db434467 | 808 | // TODO, font for labels etc |
f40fdaa3 | 809 | gtk_widget_modify_style( m_widget, style ); |
a81258be RR |
810 | } |
811 | ||
58d1c1ae RR |
812 | bool wxNotebook::IsOwnGtkWindow( GdkWindow *window ) |
813 | { | |
814 | return ((m_widget->window == window) || | |
9e691f46 | 815 | (NOTEBOOK_PANEL(m_widget) == window)); |
58d1c1ae RR |
816 | } |
817 | ||
c077ee94 RR |
818 | bool wxNotebook::SetFont(const wxFont& font) |
819 | { | |
820 | bool rc=wxNotebookBase::SetFont(font); | |
821 | ||
822 | if (rc) | |
823 | { | |
824 | size_t i; | |
825 | for (i=0 ; i < m_pagesData.GetCount() ; i++) | |
826 | GetNotebookPage(i)->SetFont(font); | |
827 | } | |
828 | return rc; | |
829 | } | |
830 | ||
9d522606 RD |
831 | // static |
832 | wxVisualAttributes | |
833 | wxNotebook::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
834 | { | |
835 | return GetDefaultAttributesFromGTKWidget(gtk_notebook_new); | |
836 | } | |
837 | ||
53b28675 | 838 | //----------------------------------------------------------------------------- |
ff829f3f | 839 | // wxNotebookEvent |
53b28675 RR |
840 | //----------------------------------------------------------------------------- |
841 | ||
92976ab6 | 842 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) |
5b011451 | 843 | |
a3a7f879 | 844 | #endif |