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