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