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