]>
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" |
83624f79 | 27 | |
9e691f46 | 28 | #include "wx/gtk/private.h" |
83624f79 | 29 | #include "wx/gtk/win_gtk.h" |
9e691f46 | 30 | |
5e7e9e1b | 31 | #include <gdk/gdkkeysyms.h> |
b292e2f5 | 32 | |
2e4df4bf VZ |
33 | // ---------------------------------------------------------------------------- |
34 | // events | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
37 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) | |
38 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
39 | ||
acfd422a RR |
40 | //----------------------------------------------------------------------------- |
41 | // idle system | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | extern void wxapp_install_idle_handler(); | |
45 | extern bool g_isIdle; | |
46 | ||
b292e2f5 RR |
47 | //----------------------------------------------------------------------------- |
48 | // data | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
07b8d7ec | 51 | extern bool g_blockEventsOnDrag; |
53b28675 | 52 | |
219f895a | 53 | //----------------------------------------------------------------------------- |
80a58c99 | 54 | // wxGtkNotebookPage |
219f895a RR |
55 | //----------------------------------------------------------------------------- |
56 | ||
07b8d7ec VZ |
57 | // VZ: this is rather ugly as we keep the pages themselves in an array (it |
58 | // allows us to have quite a few functions implemented in the base class) | |
59 | // but the page data is kept in a separate list, so we must maintain them | |
60 | // in sync manually... of course, the list had been there before the base | |
61 | // class which explains it but it still would be nice to do something | |
62 | // about this one day | |
63 | ||
80a58c99 | 64 | class wxGtkNotebookPage: public wxObject |
219f895a RR |
65 | { |
66 | public: | |
80a58c99 | 67 | wxGtkNotebookPage() |
219f895a | 68 | { |
219f895a | 69 | m_image = -1; |
c67daf87 | 70 | m_page = (GtkNotebookPage *) NULL; |
24d20a8f | 71 | m_box = (GtkWidget *) NULL; |
ff7b1510 | 72 | } |
219f895a | 73 | |
219f895a RR |
74 | wxString m_text; |
75 | int m_image; | |
76 | GtkNotebookPage *m_page; | |
77 | GtkLabel *m_label; | |
24d20a8f | 78 | GtkWidget *m_box; // in which the label and image are packed |
219f895a RR |
79 | }; |
80 | ||
07b8d7ec VZ |
81 | #include "wx/listimpl.cpp" |
82 | WX_DEFINE_LIST(wxGtkNotebookPagesList); | |
83 | ||
ff829f3f | 84 | //----------------------------------------------------------------------------- |
5b011451 | 85 | // "switch_page" |
ff829f3f VZ |
86 | //----------------------------------------------------------------------------- |
87 | ||
219f895a RR |
88 | static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget), |
89 | GtkNotebookPage *WXUNUSED(page), | |
587ce561 RR |
90 | gint page, |
91 | wxNotebook *notebook ) | |
ff829f3f | 92 | { |
36202885 VZ |
93 | // are you trying to call SetSelection() from a notebook event handler? |
94 | // you shouldn't! | |
2b5f62a0 | 95 | wxCHECK_RET( !notebook->m_inSwitchPage, |
36202885 VZ |
96 | _T("gtk_notebook_page_change_callback reentered") ); |
97 | ||
2b5f62a0 | 98 | notebook->m_inSwitchPage = TRUE; |
a6aa9b1e | 99 | if (g_isIdle) |
587ce561 | 100 | wxapp_install_idle_handler(); |
ff829f3f | 101 | |
b292e2f5 | 102 | int old = notebook->GetSelection(); |
ff829f3f | 103 | |
36202885 VZ |
104 | wxNotebookEvent eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, |
105 | notebook->GetId(), page, old ); | |
106 | eventChanging.SetEventObject( notebook ); | |
a6aa9b1e | 107 | |
36202885 VZ |
108 | if ( (notebook->GetEventHandler()->ProcessEvent(eventChanging)) && |
109 | !eventChanging.IsAllowed() ) | |
587ce561 RR |
110 | { |
111 | /* program doesn't allow the page change */ | |
36202885 VZ |
112 | gtk_signal_emit_stop_by_name( GTK_OBJECT(notebook->m_widget), |
113 | "switch_page" ); | |
114 | } | |
115 | else // change allowed | |
116 | { | |
117 | // make wxNotebook::GetSelection() return the correct (i.e. consistent | |
118 | // with wxNotebookEvent::GetSelection()) value even though the page is | |
119 | // not really changed in GTK+ | |
120 | notebook->m_selection = page; | |
121 | ||
122 | wxNotebookEvent eventChanged( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, | |
123 | notebook->GetId(), page, old ); | |
124 | eventChanged.SetEventObject( notebook ); | |
125 | notebook->GetEventHandler()->ProcessEvent( eventChanged ); | |
587ce561 | 126 | } |
ef44a621 | 127 | |
2b5f62a0 | 128 | notebook->m_inSwitchPage = FALSE; |
ff829f3f VZ |
129 | } |
130 | ||
5b011451 RR |
131 | //----------------------------------------------------------------------------- |
132 | // "size_allocate" | |
133 | //----------------------------------------------------------------------------- | |
134 | ||
33d0b396 | 135 | static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win ) |
caac5181 | 136 | { |
a6aa9b1e | 137 | if (g_isIdle) |
587ce561 | 138 | wxapp_install_idle_handler(); |
6d693bb4 | 139 | |
a2053b27 RR |
140 | if ((win->m_x == alloc->x) && |
141 | (win->m_y == alloc->y) && | |
142 | (win->m_width == alloc->width) && | |
143 | (win->m_height == alloc->height)) | |
b292e2f5 | 144 | { |
58dea4b0 | 145 | return; |
b292e2f5 | 146 | } |
a6aa9b1e | 147 | |
b292e2f5 | 148 | win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height ); |
f861258f | 149 | |
d7928388 RR |
150 | /* GTK 1.2 up to version 1.2.5 is broken so that we have to call allocate |
151 | here in order to make repositioning after resizing to take effect. */ | |
152 | if ((gtk_major_version == 1) && | |
153 | (gtk_minor_version == 2) && | |
8712c6e7 VZ |
154 | (gtk_micro_version < 6) && |
155 | (win->m_wxwindow) && | |
156 | (GTK_WIDGET_REALIZED(win->m_wxwindow))) | |
d7928388 RR |
157 | { |
158 | gtk_widget_size_allocate( win->m_wxwindow, alloc ); | |
159 | } | |
6d693bb4 RR |
160 | } |
161 | ||
162 | //----------------------------------------------------------------------------- | |
163 | // "realize" from m_widget | |
164 | //----------------------------------------------------------------------------- | |
165 | ||
6d693bb4 RR |
166 | static gint |
167 | gtk_notebook_realized_callback( GtkWidget * WXUNUSED(widget), wxWindow *win ) | |
168 | { | |
169 | if (g_isIdle) | |
170 | wxapp_install_idle_handler(); | |
171 | ||
d7928388 RR |
172 | /* GTK 1.2 up to version 1.2.5 is broken so that we have to call a queue_resize |
173 | here in order to make repositioning before showing to take effect. */ | |
6d693bb4 RR |
174 | gtk_widget_queue_resize( win->m_widget ); |
175 | ||
176 | return FALSE; | |
b292e2f5 RR |
177 | } |
178 | ||
8253c7fd | 179 | //----------------------------------------------------------------------------- |
8712c6e7 | 180 | // "key_press_event" |
8253c7fd RR |
181 | //----------------------------------------------------------------------------- |
182 | ||
183 | static gint gtk_notebook_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxNotebook *win ) | |
184 | { | |
185 | if (g_isIdle) | |
186 | wxapp_install_idle_handler(); | |
187 | ||
188 | if (!win->m_hasVMT) return FALSE; | |
189 | if (g_blockEventsOnDrag) return FALSE; | |
190 | ||
191 | /* win is a control: tab can be propagated up */ | |
192 | if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) | |
193 | { | |
07b8d7ec | 194 | int sel = win->GetSelection(); |
461573cc RR |
195 | if (sel == -1) |
196 | return TRUE; | |
b318dc42 JS |
197 | wxGtkNotebookPage *nb_page = win->GetNotebookPage(sel); |
198 | wxCHECK_MSG( nb_page, FALSE, _T("invalid selection in wxNotebook") ); | |
8253c7fd RR |
199 | |
200 | wxNavigationKeyEvent event; | |
201 | event.SetEventObject( win ); | |
202 | /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */ | |
203 | event.SetDirection( (gdk_event->keyval == GDK_Tab) ); | |
204 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
cc023d9f | 205 | event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ); |
8253c7fd | 206 | event.SetCurrentFocus( win ); |
07b8d7ec VZ |
207 | |
208 | wxNotebookPage *client = win->GetPage(sel); | |
209 | if ( !client->GetEventHandler()->ProcessEvent( event ) ) | |
8253c7fd | 210 | { |
07b8d7ec | 211 | client->SetFocus(); |
8253c7fd | 212 | } |
8712c6e7 | 213 | |
8253c7fd RR |
214 | gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" ); |
215 | return TRUE; | |
216 | } | |
8712c6e7 | 217 | |
8253c7fd RR |
218 | return FALSE; |
219 | } | |
220 | ||
6ca41e57 RR |
221 | //----------------------------------------------------------------------------- |
222 | // InsertChild callback for wxNotebook | |
223 | //----------------------------------------------------------------------------- | |
224 | ||
587ce561 | 225 | static void wxInsertChildInNotebook( wxNotebook* WXUNUSED(parent), wxWindow* WXUNUSED(child) ) |
6ca41e57 | 226 | { |
587ce561 | 227 | /* we don't do anything here but pray */ |
6ca41e57 RR |
228 | } |
229 | ||
53b28675 RR |
230 | //----------------------------------------------------------------------------- |
231 | // wxNotebook | |
232 | //----------------------------------------------------------------------------- | |
233 | ||
53b28675 RR |
234 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl) |
235 | ||
b98d804b RR |
236 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) |
237 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) | |
238 | END_EVENT_TABLE() | |
f861258f | 239 | |
ff829f3f | 240 | void wxNotebook::Init() |
53b28675 | 241 | { |
b318dc42 | 242 | m_padding = 0; |
2b5f62a0 VZ |
243 | m_inSwitchPage = FALSE; |
244 | ||
b292e2f5 | 245 | m_imageList = (wxImageList *) NULL; |
36202885 | 246 | m_selection = -1; |
a2d93e73 | 247 | m_themeEnabled = TRUE; |
ff829f3f VZ |
248 | } |
249 | ||
250 | wxNotebook::wxNotebook() | |
251 | { | |
b292e2f5 | 252 | Init(); |
ff7b1510 | 253 | } |
53b28675 | 254 | |
debe6624 | 255 | wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id, |
53b28675 | 256 | const wxPoint& pos, const wxSize& size, |
debe6624 | 257 | long style, const wxString& name ) |
53b28675 | 258 | { |
b292e2f5 RR |
259 | Init(); |
260 | Create( parent, id, pos, size, style, name ); | |
ff7b1510 | 261 | } |
53b28675 | 262 | |
ff829f3f | 263 | wxNotebook::~wxNotebook() |
53b28675 | 264 | { |
587ce561 | 265 | /* don't generate change page events any more */ |
a6aa9b1e | 266 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
587ce561 | 267 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this ); |
ff829f3f | 268 | |
b292e2f5 | 269 | DeleteAllPages(); |
ff7b1510 | 270 | } |
53b28675 | 271 | |
debe6624 | 272 | bool wxNotebook::Create(wxWindow *parent, wxWindowID id, |
07b8d7ec VZ |
273 | const wxPoint& pos, const wxSize& size, |
274 | long style, const wxString& name ) | |
53b28675 | 275 | { |
b292e2f5 RR |
276 | m_needParent = TRUE; |
277 | m_acceptsFocus = TRUE; | |
278 | m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook; | |
279 | ||
4dcaf11a RR |
280 | if (!PreCreation( parent, pos, size ) || |
281 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
282 | { | |
223d09f6 | 283 | wxFAIL_MSG( wxT("wxNoteBook creation failed") ); |
8712c6e7 | 284 | return FALSE; |
4dcaf11a RR |
285 | } |
286 | ||
ff829f3f | 287 | |
b292e2f5 | 288 | m_widget = gtk_notebook_new(); |
53b28675 | 289 | |
b292e2f5 | 290 | gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 ); |
caac5181 | 291 | |
587ce561 RR |
292 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
293 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); | |
ff829f3f | 294 | |
f03fc89f | 295 | m_parent->DoAddChild( this ); |
ef44a621 | 296 | |
8712c6e7 VZ |
297 | if (m_windowStyle & wxNB_RIGHT) |
298 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT ); | |
299 | if (m_windowStyle & wxNB_LEFT) | |
300 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT ); | |
301 | if (m_windowStyle & wxNB_BOTTOM) | |
302 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM ); | |
a3a7f879 | 303 | |
8253c7fd RR |
304 | gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event", |
305 | GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this ); | |
306 | ||
b292e2f5 | 307 | PostCreation(); |
ff829f3f | 308 | |
db434467 RR |
309 | SetFont( parent->GetFont() ); |
310 | ||
6d693bb4 RR |
311 | gtk_signal_connect( GTK_OBJECT(m_widget), "realize", |
312 | GTK_SIGNAL_FUNC(gtk_notebook_realized_callback), (gpointer) this ); | |
a6aa9b1e | 313 | |
b292e2f5 RR |
314 | Show( TRUE ); |
315 | ||
316 | return TRUE; | |
ff7b1510 | 317 | } |
53b28675 | 318 | |
ff829f3f | 319 | int wxNotebook::GetSelection() const |
53b28675 | 320 | { |
223d09f6 | 321 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
53b28675 | 322 | |
36202885 VZ |
323 | if ( m_selection == -1 ) |
324 | { | |
b318dc42 | 325 | GList *nb_pages = GTK_NOTEBOOK(m_widget)->children; |
53b28675 | 326 | |
b318dc42 | 327 | if (g_list_length(nb_pages) != 0) |
36202885 VZ |
328 | { |
329 | GtkNotebook *notebook = GTK_NOTEBOOK(m_widget); | |
a6aa9b1e | 330 | |
36202885 VZ |
331 | gpointer cur = notebook->cur_page; |
332 | if ( cur != NULL ) | |
333 | { | |
334 | wxConstCast(this, wxNotebook)->m_selection = | |
b318dc42 | 335 | g_list_index( nb_pages, cur ); |
36202885 VZ |
336 | } |
337 | } | |
338 | } | |
a6aa9b1e | 339 | |
36202885 | 340 | return m_selection; |
ff7b1510 | 341 | } |
53b28675 | 342 | |
ff829f3f | 343 | wxString wxNotebook::GetPageText( int page ) const |
53b28675 | 344 | { |
223d09f6 | 345 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") ); |
ef44a621 | 346 | |
80a58c99 | 347 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
b292e2f5 RR |
348 | if (nb_page) |
349 | return nb_page->m_text; | |
350 | else | |
223d09f6 | 351 | return wxT(""); |
ff7b1510 | 352 | } |
53b28675 | 353 | |
ff829f3f | 354 | int wxNotebook::GetPageImage( int page ) const |
53b28675 | 355 | { |
223d09f6 | 356 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
a81258be | 357 | |
80a58c99 | 358 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
b292e2f5 RR |
359 | if (nb_page) |
360 | return nb_page->m_image; | |
361 | else | |
587ce561 | 362 | return -1; |
ff7b1510 | 363 | } |
53b28675 | 364 | |
80a58c99 | 365 | wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const |
53b28675 | 366 | { |
80a58c99 | 367 | wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") ); |
ff829f3f | 368 | |
07b8d7ec | 369 | wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") ); |
a6aa9b1e | 370 | |
07b8d7ec | 371 | return m_pagesData.Item(page)->GetData(); |
ff7b1510 | 372 | } |
53b28675 | 373 | |
ff829f3f | 374 | int wxNotebook::SetSelection( int page ) |
53b28675 | 375 | { |
223d09f6 | 376 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
a81258be | 377 | |
65b683fd | 378 | wxCHECK_MSG( page >= 0 && page < (int)m_pagesData.GetCount(), -1, wxT("invalid notebook index") ); |
ff829f3f | 379 | |
587ce561 | 380 | int selOld = GetSelection(); |
a6aa9b1e | 381 | |
36202885 VZ |
382 | // cache the selection |
383 | m_selection = page; | |
587ce561 | 384 | gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page ); |
ff829f3f | 385 | |
07b8d7ec VZ |
386 | wxNotebookPage *client = GetPage(page); |
387 | if ( client ) | |
388 | client->SetFocus(); | |
b656febd | 389 | |
07b8d7ec | 390 | return selOld; |
ff7b1510 | 391 | } |
53b28675 | 392 | |
ff829f3f | 393 | bool wxNotebook::SetPageText( int page, const wxString &text ) |
53b28675 | 394 | { |
223d09f6 | 395 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
a81258be | 396 | |
80a58c99 | 397 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
ef44a621 | 398 | |
223d09f6 | 399 | wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") ); |
ff829f3f | 400 | |
3eb78d7e | 401 | nb_page->m_text = text; |
ff829f3f | 402 | |
fab591c5 | 403 | gtk_label_set( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) ); |
a6aa9b1e | 404 | |
3eb78d7e | 405 | return TRUE; |
ff7b1510 | 406 | } |
53b28675 | 407 | |
debe6624 | 408 | bool wxNotebook::SetPageImage( int page, int image ) |
53b28675 | 409 | { |
3eb78d7e | 410 | /* HvdH 28-12-98: now it works, but it's a bit of a kludge */ |
f861258f | 411 | |
80a58c99 | 412 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
ef44a621 | 413 | |
3eb78d7e | 414 | if (!nb_page) return FALSE; |
f861258f | 415 | |
3eb78d7e RR |
416 | /* Optimization posibility: return immediately if image unchanged. |
417 | * Not enabled because it may break existing (stupid) code that | |
418 | * manipulates the imagelist to cycle images */ | |
f861258f | 419 | |
3eb78d7e | 420 | /* if (image == nb_page->m_image) return TRUE; */ |
f861258f VZ |
421 | |
422 | /* For different cases: | |
3eb78d7e RR |
423 | 1) no image -> no image |
424 | 2) image -> no image | |
425 | 3) no image -> image | |
426 | 4) image -> image */ | |
f861258f | 427 | |
3eb78d7e RR |
428 | if (image == -1 && nb_page->m_image == -1) |
429 | return TRUE; /* Case 1): Nothing to do. */ | |
f861258f | 430 | |
bbe0af5b | 431 | GtkWidget *pixmapwid = (GtkWidget*) NULL; |
f861258f VZ |
432 | |
433 | if (nb_page->m_image != -1) | |
3eb78d7e RR |
434 | { |
435 | /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */ | |
f861258f | 436 | |
3eb78d7e RR |
437 | GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box)); |
438 | while (child) | |
8712c6e7 | 439 | { |
f861258f | 440 | if (GTK_IS_PIXMAP(child->data)) |
8712c6e7 VZ |
441 | { |
442 | pixmapwid = GTK_WIDGET(child->data); | |
443 | break; | |
3eb78d7e | 444 | } |
8712c6e7 VZ |
445 | child = child->next; |
446 | } | |
f861258f | 447 | |
3eb78d7e | 448 | /* We should have the pixmap widget now */ |
f861258f VZ |
449 | wxASSERT(pixmapwid != NULL); |
450 | ||
451 | if (image == -1) | |
8712c6e7 | 452 | { |
3eb78d7e RR |
453 | /* If there's no new widget, just remove the old from the box */ |
454 | gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid); | |
455 | nb_page->m_image = -1; | |
53b28675 | 456 | |
3eb78d7e RR |
457 | return TRUE; /* Case 2) */ |
458 | } | |
459 | } | |
f861258f | 460 | |
3eb78d7e RR |
461 | /* Only cases 3) and 4) left */ |
462 | wxASSERT( m_imageList != NULL ); /* Just in case */ | |
f861258f | 463 | |
3eb78d7e RR |
464 | /* Construct the new pixmap */ |
465 | const wxBitmap *bmp = m_imageList->GetBitmap(image); | |
466 | GdkPixmap *pixmap = bmp->GetPixmap(); | |
467 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
f861258f | 468 | if ( bmp->GetMask() ) |
3eb78d7e RR |
469 | { |
470 | mask = bmp->GetMask()->GetBitmap(); | |
471 | } | |
f861258f VZ |
472 | |
473 | if (pixmapwid == NULL) | |
3eb78d7e RR |
474 | { |
475 | /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */ | |
476 | pixmapwid = gtk_pixmap_new (pixmap, mask ); | |
f861258f | 477 | |
3eb78d7e | 478 | /* CHECKME: Are these pack flags okay? */ |
b318dc42 | 479 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); |
3eb78d7e RR |
480 | gtk_widget_show(pixmapwid); |
481 | } | |
f861258f | 482 | else |
3eb78d7e RR |
483 | { |
484 | /* Case 4) Simply replace the pixmap */ | |
485 | gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask); | |
486 | } | |
f861258f | 487 | |
3eb78d7e | 488 | nb_page->m_image = image; |
53b28675 | 489 | |
3eb78d7e | 490 | return TRUE; |
ff7b1510 | 491 | } |
53b28675 RR |
492 | |
493 | void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) ) | |
494 | { | |
223d09f6 | 495 | wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") ); |
ff7b1510 | 496 | } |
53b28675 | 497 | |
b318dc42 | 498 | void wxNotebook::SetPadding( const wxSize &padding ) |
53b28675 | 499 | { |
b318dc42 JS |
500 | wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") ); |
501 | ||
502 | m_padding = padding.GetWidth(); | |
503 | ||
504 | int i; | |
505 | for (i=0; i<int(GetPageCount()); i++) | |
506 | { | |
507 | wxGtkNotebookPage* nb_page = GetNotebookPage(i); | |
508 | wxASSERT(nb_page != NULL); | |
509 | ||
510 | if (nb_page->m_image != -1) | |
511 | { | |
512 | // gtk_box_set_child_packing sets padding on BOTH sides | |
513 | // icon provides left padding, label provides center and right | |
514 | int image = nb_page->m_image; | |
515 | SetPageImage(i,-1); | |
516 | SetPageImage(i,image); | |
517 | } | |
518 | wxASSERT(nb_page->m_label); | |
519 | gtk_box_set_child_packing(GTK_BOX(nb_page->m_box), | |
520 | GTK_WIDGET(nb_page->m_label), | |
521 | FALSE, FALSE, m_padding, GTK_PACK_END); | |
522 | } | |
ff7b1510 | 523 | } |
53b28675 | 524 | |
74e3313b | 525 | void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz)) |
ca8b28f2 | 526 | { |
223d09f6 | 527 | wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") ); |
ca8b28f2 JS |
528 | } |
529 | ||
ff829f3f | 530 | bool wxNotebook::DeleteAllPages() |
53b28675 | 531 | { |
223d09f6 | 532 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
a81258be | 533 | |
07b8d7ec VZ |
534 | while (m_pagesData.GetCount() > 0) |
535 | DeletePage( m_pagesData.GetCount()-1 ); | |
536 | ||
537 | wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") ); | |
ff829f3f | 538 | |
10199e27 | 539 | return wxNotebookBase::DeleteAllPages(); |
ff7b1510 | 540 | } |
53b28675 | 541 | |
ff829f3f | 542 | bool wxNotebook::DeletePage( int page ) |
53b28675 | 543 | { |
36202885 VZ |
544 | // GTK sets GtkNotebook.cur_page to NULL before sending the switch page |
545 | // event so we have to store the selection internally | |
546 | if ( m_selection == -1 ) | |
547 | { | |
548 | m_selection = GetSelection(); | |
07b8d7ec | 549 | if ( m_selection == (int)m_pagesData.GetCount() - 1 ) |
36202885 VZ |
550 | { |
551 | // the index will become invalid after the page is deleted | |
552 | m_selection = -1; | |
553 | } | |
554 | } | |
a6aa9b1e | 555 | |
10199e27 | 556 | // it will call our DoRemovePage() to do the real work |
07b8d7ec | 557 | return wxNotebookBase::DeletePage(page); |
fed46e72 RR |
558 | } |
559 | ||
1e6feb95 | 560 | wxNotebookPage *wxNotebook::DoRemovePage( int page ) |
fed46e72 | 561 | { |
10199e27 VZ |
562 | wxNotebookPage *client = wxNotebookBase::DoRemovePage(page); |
563 | if ( !client ) | |
564 | return NULL; | |
fed46e72 | 565 | |
07b8d7ec VZ |
566 | gtk_widget_ref( client->m_widget ); |
567 | gtk_widget_unrealize( client->m_widget ); | |
568 | gtk_widget_unparent( client->m_widget ); | |
569 | ||
587ce561 | 570 | gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page ); |
ff829f3f | 571 | |
222ed1d6 MB |
572 | wxGtkNotebookPage* p = GetNotebookPage(page); |
573 | m_pagesData.DeleteObject(p); | |
574 | delete p; | |
ff829f3f | 575 | |
07b8d7ec | 576 | return client; |
ff7b1510 | 577 | } |
53b28675 | 578 | |
07b8d7ec VZ |
579 | bool wxNotebook::InsertPage( int position, |
580 | wxNotebookPage* win, | |
581 | const wxString& text, | |
582 | bool select, | |
583 | int imageId ) | |
53b28675 | 584 | { |
223d09f6 | 585 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
a81258be | 586 | |
587ce561 | 587 | wxCHECK_MSG( win->GetParent() == this, FALSE, |
223d09f6 | 588 | wxT("Can't add a page whose parent is not the notebook!") ); |
8aadf227 | 589 | |
07b8d7ec VZ |
590 | wxCHECK_MSG( position >= 0 && position <= GetPageCount(), FALSE, |
591 | _T("invalid page index in wxNotebookPage::InsertPage()") ); | |
592 | ||
a6aa9b1e RD |
593 | /* don't receive switch page during addition */ |
594 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), | |
587ce561 | 595 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this ); |
a6aa9b1e | 596 | |
a2d93e73 JS |
597 | if (m_themeEnabled) |
598 | win->SetThemeEnabled(TRUE); | |
599 | ||
587ce561 | 600 | GtkNotebook *notebook = GTK_NOTEBOOK(m_widget); |
53b28675 | 601 | |
b318dc42 | 602 | wxGtkNotebookPage *nb_page = new wxGtkNotebookPage(); |
a6aa9b1e | 603 | |
07b8d7ec | 604 | if ( position == GetPageCount() ) |
b318dc42 | 605 | m_pagesData.Append( nb_page ); |
587ce561 | 606 | else |
b318dc42 | 607 | m_pagesData.Insert( m_pagesData.Item( position ), nb_page ); |
a6aa9b1e | 608 | |
07b8d7ec | 609 | m_pages.Insert(win, position); |
8aadf227 | 610 | |
b318dc42 JS |
611 | nb_page->m_box = gtk_hbox_new( FALSE, 1 ); |
612 | gtk_container_border_width( GTK_CONTAINER(nb_page->m_box), 2 ); | |
587ce561 | 613 | |
d1af991f RR |
614 | gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate", |
615 | GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win ); | |
616 | ||
587ce561 | 617 | if (position < 0) |
b318dc42 | 618 | gtk_notebook_append_page( notebook, win->m_widget, nb_page->m_box ); |
a6aa9b1e | 619 | else |
b318dc42 | 620 | gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position ); |
587ce561 | 621 | |
b318dc42 | 622 | nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data; |
ef44a621 | 623 | |
587ce561 | 624 | /* set the label image */ |
b318dc42 | 625 | nb_page->m_image = imageId; |
a6aa9b1e | 626 | |
3eb78d7e | 627 | if (imageId != -1) |
e4a81a2e | 628 | { |
3eb78d7e RR |
629 | wxASSERT( m_imageList != NULL ); |
630 | ||
631 | const wxBitmap *bmp = m_imageList->GetBitmap(imageId); | |
632 | GdkPixmap *pixmap = bmp->GetPixmap(); | |
633 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
634 | if ( bmp->GetMask() ) | |
635 | { | |
636 | mask = bmp->GetMask()->GetBitmap(); | |
637 | } | |
e4a81a2e | 638 | |
3eb78d7e | 639 | GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask ); |
24d20a8f | 640 | |
b318dc42 | 641 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); |
24d20a8f | 642 | |
3eb78d7e RR |
643 | gtk_widget_show(pixmapwid); |
644 | } | |
24d20a8f | 645 | |
587ce561 | 646 | /* set the label text */ |
b318dc42 JS |
647 | nb_page->m_text = text; |
648 | if (nb_page->m_text.IsEmpty()) nb_page->m_text = wxT(""); | |
649 | ||
650 | nb_page->m_label = GTK_LABEL( gtk_label_new(nb_page->m_text.mbc_str()) ); | |
651 | gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding ); | |
652 | ||
587ce561 | 653 | /* show the label */ |
b318dc42 | 654 | gtk_widget_show( GTK_WIDGET(nb_page->m_label) ); |
07b8d7ec | 655 | if (select && (m_pagesData.GetCount() > 1)) |
587ce561 RR |
656 | { |
657 | if (position < 0) | |
658 | SetSelection( GetPageCount()-1 ); | |
8712c6e7 | 659 | else |
587ce561 RR |
660 | SetSelection( position ); |
661 | } | |
741fd203 | 662 | |
587ce561 RR |
663 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
664 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); | |
ff829f3f | 665 | |
3eb78d7e | 666 | return TRUE; |
ff7b1510 | 667 | } |
53b28675 | 668 | |
b98d804b RR |
669 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) |
670 | { | |
f861258f | 671 | if (event.IsWindowChange()) |
b98d804b | 672 | AdvanceSelection( event.GetDirection() ); |
f861258f | 673 | else |
b98d804b RR |
674 | event.Skip(); |
675 | } | |
676 | ||
93d38175 VS |
677 | #if wxUSE_CONSTRAINTS |
678 | ||
5a8c929e | 679 | // override these 2 functions to do nothing: everything is done in OnSize |
e3e65dac | 680 | void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) ) |
5a8c929e | 681 | { |
b292e2f5 RR |
682 | // don't set the sizes of the pages - their correct size is not yet known |
683 | wxControl::SetConstraintSizes(FALSE); | |
5a8c929e VZ |
684 | } |
685 | ||
e3e65dac | 686 | bool wxNotebook::DoPhase( int WXUNUSED(nPhase) ) |
5a8c929e | 687 | { |
b292e2f5 | 688 | return TRUE; |
5a8c929e VZ |
689 | } |
690 | ||
93d38175 VS |
691 | #endif |
692 | ||
58614078 | 693 | void wxNotebook::ApplyWidgetStyle() |
a81258be | 694 | { |
db434467 | 695 | // TODO, font for labels etc |
8712c6e7 | 696 | |
b292e2f5 RR |
697 | SetWidgetStyle(); |
698 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
a81258be RR |
699 | } |
700 | ||
58d1c1ae RR |
701 | bool wxNotebook::IsOwnGtkWindow( GdkWindow *window ) |
702 | { | |
703 | return ((m_widget->window == window) || | |
9e691f46 | 704 | (NOTEBOOK_PANEL(m_widget) == window)); |
58d1c1ae RR |
705 | } |
706 | ||
53b28675 | 707 | //----------------------------------------------------------------------------- |
ff829f3f | 708 | // wxNotebookEvent |
53b28675 RR |
709 | //----------------------------------------------------------------------------- |
710 | ||
92976ab6 | 711 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) |
5b011451 | 712 | |
a3a7f879 | 713 | #endif |