| 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 11 | #pragma implementation "notebook.h" |
| 12 | #endif |
| 13 | |
| 14 | // For compilers that support precompilation, includes "wx.h". |
| 15 | #include "wx/wxprec.h" |
| 16 | |
| 17 | #include "wx/notebook.h" |
| 18 | |
| 19 | #if wxUSE_NOTEBOOK |
| 20 | |
| 21 | #include "wx/panel.h" |
| 22 | #include "wx/utils.h" |
| 23 | #include "wx/imaglist.h" |
| 24 | #include "wx/intl.h" |
| 25 | #include "wx/log.h" |
| 26 | #include "wx/bitmap.h" |
| 27 | |
| 28 | #include "wx/gtk/private.h" |
| 29 | #include "wx/gtk/win_gtk.h" |
| 30 | |
| 31 | #include <gdk/gdkkeysyms.h> |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | // events |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) |
| 38 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) |
| 39 | |
| 40 | //----------------------------------------------------------------------------- |
| 41 | // idle system |
| 42 | //----------------------------------------------------------------------------- |
| 43 | |
| 44 | extern void wxapp_install_idle_handler(); |
| 45 | extern bool g_isIdle; |
| 46 | |
| 47 | //----------------------------------------------------------------------------- |
| 48 | // data |
| 49 | //----------------------------------------------------------------------------- |
| 50 | |
| 51 | extern bool g_blockEventsOnDrag; |
| 52 | |
| 53 | //----------------------------------------------------------------------------- |
| 54 | // wxGtkNotebookPage |
| 55 | //----------------------------------------------------------------------------- |
| 56 | |
| 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 | |
| 64 | class wxGtkNotebookPage: public wxObject |
| 65 | { |
| 66 | public: |
| 67 | wxGtkNotebookPage() |
| 68 | { |
| 69 | m_image = -1; |
| 70 | m_page = (GtkNotebookPage *) NULL; |
| 71 | m_box = (GtkWidget *) NULL; |
| 72 | } |
| 73 | |
| 74 | wxString m_text; |
| 75 | int m_image; |
| 76 | GtkNotebookPage *m_page; |
| 77 | GtkLabel *m_label; |
| 78 | GtkWidget *m_box; // in which the label and image are packed |
| 79 | }; |
| 80 | |
| 81 | #include "wx/listimpl.cpp" |
| 82 | WX_DEFINE_LIST(wxGtkNotebookPagesList); |
| 83 | |
| 84 | //----------------------------------------------------------------------------- |
| 85 | // "switch_page" |
| 86 | //----------------------------------------------------------------------------- |
| 87 | |
| 88 | static void gtk_notebook_page_change_callback(GtkNotebook *WXUNUSED(widget), |
| 89 | GtkNotebookPage *WXUNUSED(page), |
| 90 | gint page, |
| 91 | wxNotebook *notebook ) |
| 92 | { |
| 93 | // are you trying to call SetSelection() from a notebook event handler? |
| 94 | // you shouldn't! |
| 95 | wxCHECK_RET( !notebook->m_inSwitchPage, |
| 96 | _T("gtk_notebook_page_change_callback reentered") ); |
| 97 | |
| 98 | notebook->m_inSwitchPage = TRUE; |
| 99 | if (g_isIdle) |
| 100 | wxapp_install_idle_handler(); |
| 101 | |
| 102 | int old = notebook->GetSelection(); |
| 103 | |
| 104 | wxNotebookEvent eventChanging( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, |
| 105 | notebook->GetId(), page, old ); |
| 106 | eventChanging.SetEventObject( notebook ); |
| 107 | |
| 108 | if ( (notebook->GetEventHandler()->ProcessEvent(eventChanging)) && |
| 109 | !eventChanging.IsAllowed() ) |
| 110 | { |
| 111 | /* program doesn't allow the page change */ |
| 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 ); |
| 126 | } |
| 127 | |
| 128 | notebook->m_inSwitchPage = FALSE; |
| 129 | } |
| 130 | |
| 131 | //----------------------------------------------------------------------------- |
| 132 | // "size_allocate" |
| 133 | //----------------------------------------------------------------------------- |
| 134 | |
| 135 | static void gtk_page_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxWindow *win ) |
| 136 | { |
| 137 | if (g_isIdle) |
| 138 | wxapp_install_idle_handler(); |
| 139 | |
| 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)) |
| 144 | { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | win->SetSize( alloc->x, alloc->y, alloc->width, alloc->height ); |
| 149 | |
| 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) && |
| 154 | (gtk_micro_version < 6) && |
| 155 | (win->m_wxwindow) && |
| 156 | (GTK_WIDGET_REALIZED(win->m_wxwindow))) |
| 157 | { |
| 158 | gtk_widget_size_allocate( win->m_wxwindow, alloc ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | //----------------------------------------------------------------------------- |
| 163 | // "realize" from m_widget |
| 164 | //----------------------------------------------------------------------------- |
| 165 | |
| 166 | static gint |
| 167 | gtk_notebook_realized_callback( GtkWidget * WXUNUSED(widget), wxWindow *win ) |
| 168 | { |
| 169 | if (g_isIdle) |
| 170 | wxapp_install_idle_handler(); |
| 171 | |
| 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. */ |
| 174 | gtk_widget_queue_resize( win->m_widget ); |
| 175 | |
| 176 | return FALSE; |
| 177 | } |
| 178 | |
| 179 | //----------------------------------------------------------------------------- |
| 180 | // "key_press_event" |
| 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 | { |
| 194 | int sel = win->GetSelection(); |
| 195 | if (sel == -1) |
| 196 | return TRUE; |
| 197 | wxGtkNotebookPage *nb_page = win->GetNotebookPage(sel); |
| 198 | wxCHECK_MSG( nb_page, FALSE, _T("invalid selection in wxNotebook") ); |
| 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 */ |
| 205 | event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ); |
| 206 | event.SetCurrentFocus( win ); |
| 207 | |
| 208 | wxNotebookPage *client = win->GetPage(sel); |
| 209 | if ( !client->GetEventHandler()->ProcessEvent( event ) ) |
| 210 | { |
| 211 | client->SetFocus(); |
| 212 | } |
| 213 | |
| 214 | gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" ); |
| 215 | return TRUE; |
| 216 | } |
| 217 | |
| 218 | return FALSE; |
| 219 | } |
| 220 | |
| 221 | //----------------------------------------------------------------------------- |
| 222 | // InsertChild callback for wxNotebook |
| 223 | //----------------------------------------------------------------------------- |
| 224 | |
| 225 | static void wxInsertChildInNotebook( wxNotebook* WXUNUSED(parent), wxWindow* WXUNUSED(child) ) |
| 226 | { |
| 227 | /* we don't do anything here but pray */ |
| 228 | } |
| 229 | |
| 230 | //----------------------------------------------------------------------------- |
| 231 | // wxNotebook |
| 232 | //----------------------------------------------------------------------------- |
| 233 | |
| 234 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook,wxControl) |
| 235 | |
| 236 | BEGIN_EVENT_TABLE(wxNotebook, wxControl) |
| 237 | EVT_NAVIGATION_KEY(wxNotebook::OnNavigationKey) |
| 238 | END_EVENT_TABLE() |
| 239 | |
| 240 | void wxNotebook::Init() |
| 241 | { |
| 242 | m_padding = 0; |
| 243 | m_inSwitchPage = FALSE; |
| 244 | |
| 245 | m_imageList = (wxImageList *) NULL; |
| 246 | m_selection = -1; |
| 247 | m_themeEnabled = TRUE; |
| 248 | } |
| 249 | |
| 250 | wxNotebook::wxNotebook() |
| 251 | { |
| 252 | Init(); |
| 253 | } |
| 254 | |
| 255 | wxNotebook::wxNotebook( wxWindow *parent, wxWindowID id, |
| 256 | const wxPoint& pos, const wxSize& size, |
| 257 | long style, const wxString& name ) |
| 258 | { |
| 259 | Init(); |
| 260 | Create( parent, id, pos, size, style, name ); |
| 261 | } |
| 262 | |
| 263 | wxNotebook::~wxNotebook() |
| 264 | { |
| 265 | DeleteAllPages(); |
| 266 | } |
| 267 | |
| 268 | bool wxNotebook::Create(wxWindow *parent, wxWindowID id, |
| 269 | const wxPoint& pos, const wxSize& size, |
| 270 | long style, const wxString& name ) |
| 271 | { |
| 272 | m_needParent = TRUE; |
| 273 | m_acceptsFocus = TRUE; |
| 274 | m_insertCallback = (wxInsertChildFunction)wxInsertChildInNotebook; |
| 275 | |
| 276 | if (!PreCreation( parent, pos, size ) || |
| 277 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) |
| 278 | { |
| 279 | wxFAIL_MSG( wxT("wxNoteBook creation failed") ); |
| 280 | return FALSE; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | m_widget = gtk_notebook_new(); |
| 285 | |
| 286 | gtk_notebook_set_scrollable( GTK_NOTEBOOK(m_widget), 1 ); |
| 287 | |
| 288 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
| 289 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); |
| 290 | |
| 291 | m_parent->DoAddChild( this ); |
| 292 | |
| 293 | if (m_windowStyle & wxNB_RIGHT) |
| 294 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_RIGHT ); |
| 295 | if (m_windowStyle & wxNB_LEFT) |
| 296 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_LEFT ); |
| 297 | if (m_windowStyle & wxNB_BOTTOM) |
| 298 | gtk_notebook_set_tab_pos( GTK_NOTEBOOK(m_widget), GTK_POS_BOTTOM ); |
| 299 | |
| 300 | gtk_signal_connect( GTK_OBJECT(m_widget), "key_press_event", |
| 301 | GTK_SIGNAL_FUNC(gtk_notebook_key_press_callback), (gpointer)this ); |
| 302 | |
| 303 | PostCreation(); |
| 304 | |
| 305 | SetFont( parent->GetFont() ); |
| 306 | |
| 307 | gtk_signal_connect( GTK_OBJECT(m_widget), "realize", |
| 308 | GTK_SIGNAL_FUNC(gtk_notebook_realized_callback), (gpointer) this ); |
| 309 | |
| 310 | Show( TRUE ); |
| 311 | |
| 312 | return TRUE; |
| 313 | } |
| 314 | |
| 315 | int wxNotebook::GetSelection() const |
| 316 | { |
| 317 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
| 318 | |
| 319 | if ( m_selection == -1 ) |
| 320 | { |
| 321 | GList *nb_pages = GTK_NOTEBOOK(m_widget)->children; |
| 322 | |
| 323 | if (g_list_length(nb_pages) != 0) |
| 324 | { |
| 325 | GtkNotebook *notebook = GTK_NOTEBOOK(m_widget); |
| 326 | |
| 327 | gpointer cur = notebook->cur_page; |
| 328 | if ( cur != NULL ) |
| 329 | { |
| 330 | wxConstCast(this, wxNotebook)->m_selection = |
| 331 | g_list_index( nb_pages, cur ); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return m_selection; |
| 337 | } |
| 338 | |
| 339 | wxString wxNotebook::GetPageText( size_t page ) const |
| 340 | { |
| 341 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid notebook") ); |
| 342 | |
| 343 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
| 344 | if (nb_page) |
| 345 | return nb_page->m_text; |
| 346 | else |
| 347 | return wxT(""); |
| 348 | } |
| 349 | |
| 350 | int wxNotebook::GetPageImage( size_t page ) const |
| 351 | { |
| 352 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
| 353 | |
| 354 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
| 355 | if (nb_page) |
| 356 | return nb_page->m_image; |
| 357 | else |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | wxGtkNotebookPage* wxNotebook::GetNotebookPage( int page ) const |
| 362 | { |
| 363 | wxCHECK_MSG( m_widget != NULL, (wxGtkNotebookPage*) NULL, wxT("invalid notebook") ); |
| 364 | |
| 365 | wxCHECK_MSG( page < (int)m_pagesData.GetCount(), (wxGtkNotebookPage*) NULL, wxT("invalid notebook index") ); |
| 366 | |
| 367 | return m_pagesData.Item(page)->GetData(); |
| 368 | } |
| 369 | |
| 370 | int wxNotebook::SetSelection( size_t page ) |
| 371 | { |
| 372 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid notebook") ); |
| 373 | |
| 374 | wxCHECK_MSG( page < m_pagesData.GetCount(), -1, wxT("invalid notebook index") ); |
| 375 | |
| 376 | int selOld = GetSelection(); |
| 377 | |
| 378 | // cache the selection |
| 379 | m_selection = page; |
| 380 | gtk_notebook_set_page( GTK_NOTEBOOK(m_widget), page ); |
| 381 | |
| 382 | wxNotebookPage *client = GetPage(page); |
| 383 | if ( client ) |
| 384 | client->SetFocus(); |
| 385 | |
| 386 | return selOld; |
| 387 | } |
| 388 | |
| 389 | bool wxNotebook::SetPageText( size_t page, const wxString &text ) |
| 390 | { |
| 391 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
| 392 | |
| 393 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
| 394 | |
| 395 | wxCHECK_MSG( nb_page, FALSE, wxT("SetPageText: invalid page index") ); |
| 396 | |
| 397 | nb_page->m_text = text; |
| 398 | |
| 399 | gtk_label_set( nb_page->m_label, wxGTK_CONV( nb_page->m_text ) ); |
| 400 | |
| 401 | return TRUE; |
| 402 | } |
| 403 | |
| 404 | bool wxNotebook::SetPageImage( size_t page, int image ) |
| 405 | { |
| 406 | /* HvdH 28-12-98: now it works, but it's a bit of a kludge */ |
| 407 | |
| 408 | wxGtkNotebookPage* nb_page = GetNotebookPage(page); |
| 409 | |
| 410 | if (!nb_page) return FALSE; |
| 411 | |
| 412 | /* Optimization posibility: return immediately if image unchanged. |
| 413 | * Not enabled because it may break existing (stupid) code that |
| 414 | * manipulates the imagelist to cycle images */ |
| 415 | |
| 416 | /* if (image == nb_page->m_image) return TRUE; */ |
| 417 | |
| 418 | /* For different cases: |
| 419 | 1) no image -> no image |
| 420 | 2) image -> no image |
| 421 | 3) no image -> image |
| 422 | 4) image -> image */ |
| 423 | |
| 424 | if (image == -1 && nb_page->m_image == -1) |
| 425 | return TRUE; /* Case 1): Nothing to do. */ |
| 426 | |
| 427 | GtkWidget *pixmapwid = (GtkWidget*) NULL; |
| 428 | |
| 429 | if (nb_page->m_image != -1) |
| 430 | { |
| 431 | /* Case 2) or 4). There is already an image in the gtkhbox. Let's find it */ |
| 432 | |
| 433 | GList *child = gtk_container_children(GTK_CONTAINER(nb_page->m_box)); |
| 434 | while (child) |
| 435 | { |
| 436 | if (GTK_IS_PIXMAP(child->data)) |
| 437 | { |
| 438 | pixmapwid = GTK_WIDGET(child->data); |
| 439 | break; |
| 440 | } |
| 441 | child = child->next; |
| 442 | } |
| 443 | |
| 444 | /* We should have the pixmap widget now */ |
| 445 | wxASSERT(pixmapwid != NULL); |
| 446 | |
| 447 | if (image == -1) |
| 448 | { |
| 449 | /* If there's no new widget, just remove the old from the box */ |
| 450 | gtk_container_remove(GTK_CONTAINER(nb_page->m_box), pixmapwid); |
| 451 | nb_page->m_image = -1; |
| 452 | |
| 453 | return TRUE; /* Case 2) */ |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /* Only cases 3) and 4) left */ |
| 458 | wxASSERT( m_imageList != NULL ); /* Just in case */ |
| 459 | |
| 460 | /* Construct the new pixmap */ |
| 461 | const wxBitmap *bmp = m_imageList->GetBitmap(image); |
| 462 | GdkPixmap *pixmap = bmp->GetPixmap(); |
| 463 | GdkBitmap *mask = (GdkBitmap*) NULL; |
| 464 | if ( bmp->GetMask() ) |
| 465 | { |
| 466 | mask = bmp->GetMask()->GetBitmap(); |
| 467 | } |
| 468 | |
| 469 | if (pixmapwid == NULL) |
| 470 | { |
| 471 | /* Case 3) No old pixmap. Create a new one and prepend it to the hbox */ |
| 472 | pixmapwid = gtk_pixmap_new (pixmap, mask ); |
| 473 | |
| 474 | /* CHECKME: Are these pack flags okay? */ |
| 475 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); |
| 476 | gtk_widget_show(pixmapwid); |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | /* Case 4) Simply replace the pixmap */ |
| 481 | gtk_pixmap_set(GTK_PIXMAP(pixmapwid), pixmap, mask); |
| 482 | } |
| 483 | |
| 484 | nb_page->m_image = image; |
| 485 | |
| 486 | return TRUE; |
| 487 | } |
| 488 | |
| 489 | void wxNotebook::SetPageSize( const wxSize &WXUNUSED(size) ) |
| 490 | { |
| 491 | wxFAIL_MSG( wxT("wxNotebook::SetPageSize not implemented") ); |
| 492 | } |
| 493 | |
| 494 | void wxNotebook::SetPadding( const wxSize &padding ) |
| 495 | { |
| 496 | wxCHECK_RET( m_widget != NULL, wxT("invalid notebook") ); |
| 497 | |
| 498 | m_padding = padding.GetWidth(); |
| 499 | |
| 500 | int i; |
| 501 | for (i=0; i<int(GetPageCount()); i++) |
| 502 | { |
| 503 | wxGtkNotebookPage* nb_page = GetNotebookPage(i); |
| 504 | wxASSERT(nb_page != NULL); |
| 505 | |
| 506 | if (nb_page->m_image != -1) |
| 507 | { |
| 508 | // gtk_box_set_child_packing sets padding on BOTH sides |
| 509 | // icon provides left padding, label provides center and right |
| 510 | int image = nb_page->m_image; |
| 511 | SetPageImage(i,-1); |
| 512 | SetPageImage(i,image); |
| 513 | } |
| 514 | wxASSERT(nb_page->m_label); |
| 515 | gtk_box_set_child_packing(GTK_BOX(nb_page->m_box), |
| 516 | GTK_WIDGET(nb_page->m_label), |
| 517 | FALSE, FALSE, m_padding, GTK_PACK_END); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | void wxNotebook::SetTabSize(const wxSize& WXUNUSED(sz)) |
| 522 | { |
| 523 | wxFAIL_MSG( wxT("wxNotebook::SetTabSize not implemented") ); |
| 524 | } |
| 525 | |
| 526 | bool wxNotebook::DeleteAllPages() |
| 527 | { |
| 528 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
| 529 | |
| 530 | while (m_pagesData.GetCount() > 0) |
| 531 | DeletePage( m_pagesData.GetCount()-1 ); |
| 532 | |
| 533 | wxASSERT_MSG( GetPageCount() == 0, _T("all pages must have been deleted") ); |
| 534 | |
| 535 | return wxNotebookBase::DeleteAllPages(); |
| 536 | } |
| 537 | |
| 538 | bool wxNotebook::DeletePage( size_t page ) |
| 539 | { |
| 540 | if ( m_selection == (int)m_pagesData.GetCount() - 1 ) |
| 541 | { |
| 542 | // the index will become invalid after the page is deleted |
| 543 | m_selection = -1; |
| 544 | } |
| 545 | |
| 546 | // it will call our DoRemovePage() to do the real work |
| 547 | return wxNotebookBase::DeletePage(page); |
| 548 | } |
| 549 | |
| 550 | wxNotebookPage *wxNotebook::DoRemovePage( size_t page ) |
| 551 | { |
| 552 | wxNotebookPage *client = wxNotebookBase::DoRemovePage(page); |
| 553 | if ( !client ) |
| 554 | return NULL; |
| 555 | |
| 556 | gtk_widget_ref( client->m_widget ); |
| 557 | gtk_widget_unrealize( client->m_widget ); |
| 558 | gtk_widget_unparent( client->m_widget ); |
| 559 | |
| 560 | // gtk_notebook_remove_page() sends "switch_page" signal with some strange |
| 561 | // new page index (when deleting selected page 0, new page is 1 although, |
| 562 | // clearly, the selection should stay 0), so suppress this |
| 563 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
| 564 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this ); |
| 565 | |
| 566 | gtk_notebook_remove_page( GTK_NOTEBOOK(m_widget), page ); |
| 567 | |
| 568 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
| 569 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); |
| 570 | |
| 571 | wxGtkNotebookPage* p = GetNotebookPage(page); |
| 572 | m_pagesData.DeleteObject(p); |
| 573 | delete p; |
| 574 | |
| 575 | return client; |
| 576 | } |
| 577 | |
| 578 | bool wxNotebook::InsertPage( size_t position, |
| 579 | wxNotebookPage* win, |
| 580 | const wxString& text, |
| 581 | bool select, |
| 582 | int imageId ) |
| 583 | { |
| 584 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid notebook") ); |
| 585 | |
| 586 | wxCHECK_MSG( win->GetParent() == this, FALSE, |
| 587 | wxT("Can't add a page whose parent is not the notebook!") ); |
| 588 | |
| 589 | wxCHECK_MSG( position <= GetPageCount(), FALSE, |
| 590 | _T("invalid page index in wxNotebookPage::InsertPage()") ); |
| 591 | |
| 592 | /* don't receive switch page during addition */ |
| 593 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
| 594 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer) this ); |
| 595 | |
| 596 | if (m_themeEnabled) |
| 597 | win->SetThemeEnabled(TRUE); |
| 598 | |
| 599 | GtkNotebook *notebook = GTK_NOTEBOOK(m_widget); |
| 600 | |
| 601 | wxGtkNotebookPage *nb_page = new wxGtkNotebookPage(); |
| 602 | |
| 603 | if ( position == GetPageCount() ) |
| 604 | m_pagesData.Append( nb_page ); |
| 605 | else |
| 606 | m_pagesData.Insert( m_pagesData.Item( position ), nb_page ); |
| 607 | |
| 608 | m_pages.Insert(win, position); |
| 609 | |
| 610 | nb_page->m_box = gtk_hbox_new( FALSE, 1 ); |
| 611 | gtk_container_border_width( GTK_CONTAINER(nb_page->m_box), 2 ); |
| 612 | |
| 613 | gtk_signal_connect( GTK_OBJECT(win->m_widget), "size_allocate", |
| 614 | GTK_SIGNAL_FUNC(gtk_page_size_callback), (gpointer)win ); |
| 615 | |
| 616 | #ifndef __VMS |
| 617 | // On VMS position is unsigned and thus always positive |
| 618 | if (position < 0) |
| 619 | gtk_notebook_append_page( notebook, win->m_widget, nb_page->m_box ); |
| 620 | else |
| 621 | #endif |
| 622 | gtk_notebook_insert_page( notebook, win->m_widget, nb_page->m_box, position ); |
| 623 | |
| 624 | nb_page->m_page = (GtkNotebookPage*) g_list_last(notebook->children)->data; |
| 625 | |
| 626 | /* set the label image */ |
| 627 | nb_page->m_image = imageId; |
| 628 | |
| 629 | if (imageId != -1) |
| 630 | { |
| 631 | wxASSERT( m_imageList != NULL ); |
| 632 | |
| 633 | const wxBitmap *bmp = m_imageList->GetBitmap(imageId); |
| 634 | GdkPixmap *pixmap = bmp->GetPixmap(); |
| 635 | GdkBitmap *mask = (GdkBitmap*) NULL; |
| 636 | if ( bmp->GetMask() ) |
| 637 | { |
| 638 | mask = bmp->GetMask()->GetBitmap(); |
| 639 | } |
| 640 | |
| 641 | GtkWidget *pixmapwid = gtk_pixmap_new (pixmap, mask ); |
| 642 | |
| 643 | gtk_box_pack_start(GTK_BOX(nb_page->m_box), pixmapwid, FALSE, FALSE, m_padding); |
| 644 | |
| 645 | gtk_widget_show(pixmapwid); |
| 646 | } |
| 647 | |
| 648 | /* set the label text */ |
| 649 | nb_page->m_text = text; |
| 650 | if (nb_page->m_text.IsEmpty()) nb_page->m_text = wxT(""); |
| 651 | |
| 652 | nb_page->m_label = GTK_LABEL( gtk_label_new(wxGTK_CONV(nb_page->m_text)) ); |
| 653 | gtk_box_pack_end( GTK_BOX(nb_page->m_box), GTK_WIDGET(nb_page->m_label), FALSE, FALSE, m_padding ); |
| 654 | |
| 655 | /* show the label */ |
| 656 | gtk_widget_show( GTK_WIDGET(nb_page->m_label) ); |
| 657 | if (select && (m_pagesData.GetCount() > 1)) |
| 658 | { |
| 659 | #ifndef __VMS |
| 660 | // On VMS position is unsigned and thus always positive |
| 661 | if (position < 0) |
| 662 | SetSelection( GetPageCount()-1 ); |
| 663 | else |
| 664 | #endif |
| 665 | SetSelection( position ); |
| 666 | } |
| 667 | |
| 668 | gtk_signal_connect( GTK_OBJECT(m_widget), "switch_page", |
| 669 | GTK_SIGNAL_FUNC(gtk_notebook_page_change_callback), (gpointer)this ); |
| 670 | |
| 671 | return TRUE; |
| 672 | } |
| 673 | |
| 674 | // helper for HitTest(): check if the point lies inside the given widget which |
| 675 | // is the child of the notebook whose position and border size are passed as |
| 676 | // parameters |
| 677 | static bool |
| 678 | IsPointInsideWidget(const wxPoint& pt, GtkWidget *w, |
| 679 | gint x, gint y, gint border = 0) |
| 680 | { |
| 681 | return |
| 682 | (pt.x >= w->allocation.x - x - border) && |
| 683 | (pt.x <= w->allocation.x - x + border + w->allocation.width) && |
| 684 | (pt.y >= w->allocation.y - y - border) && |
| 685 | (pt.y <= w->allocation.y - y + border + w->allocation.height); |
| 686 | } |
| 687 | |
| 688 | int wxNotebook::HitTest(const wxPoint& pt, long *flags) const |
| 689 | { |
| 690 | const gint x = m_widget->allocation.x; |
| 691 | const gint y = m_widget->allocation.y; |
| 692 | |
| 693 | const size_t count = GetPageCount(); |
| 694 | for ( size_t i = 0; i < count; i++ ) |
| 695 | { |
| 696 | wxGtkNotebookPage* nb_page = GetNotebookPage(i); |
| 697 | GtkWidget *box = nb_page->m_box; |
| 698 | |
| 699 | // VZ: don't know how to find the border width in GTK+ 1.2 |
| 700 | #ifdef __WXGTK20__ |
| 701 | const gint border = gtk_container_get_border_width(GTK_CONTAINER(box)); |
| 702 | #else // !GTK+ 2.x |
| 703 | const gint border = 0; |
| 704 | #endif |
| 705 | if ( IsPointInsideWidget(pt, box, x, y, border) ) |
| 706 | { |
| 707 | // ok, we're inside this tab -- now find out where, if needed |
| 708 | if ( flags ) |
| 709 | { |
| 710 | GtkWidget *pixmap = NULL; |
| 711 | |
| 712 | GList *children = gtk_container_children(GTK_CONTAINER(box)); |
| 713 | for ( GList *child = children; child; child = child->next ) |
| 714 | { |
| 715 | if ( GTK_IS_PIXMAP(child->data) ) |
| 716 | { |
| 717 | pixmap = GTK_WIDGET(child->data); |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | if ( children ) |
| 723 | g_list_free(children); |
| 724 | |
| 725 | if ( pixmap && IsPointInsideWidget(pt, pixmap, x, y) ) |
| 726 | { |
| 727 | *flags = wxNB_HITTEST_ONICON; |
| 728 | } |
| 729 | else if ( IsPointInsideWidget(pt, GTK_WIDGET(nb_page->m_label), x, y) ) |
| 730 | { |
| 731 | *flags = wxNB_HITTEST_ONLABEL; |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | *flags = wxNB_HITTEST_ONITEM; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | return i; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if ( flags ) |
| 744 | *flags = wxNB_HITTEST_NOWHERE; |
| 745 | |
| 746 | return wxNOT_FOUND; |
| 747 | } |
| 748 | |
| 749 | void wxNotebook::OnNavigationKey(wxNavigationKeyEvent& event) |
| 750 | { |
| 751 | if (event.IsWindowChange()) |
| 752 | AdvanceSelection( event.GetDirection() ); |
| 753 | else |
| 754 | event.Skip(); |
| 755 | } |
| 756 | |
| 757 | #if wxUSE_CONSTRAINTS |
| 758 | |
| 759 | // override these 2 functions to do nothing: everything is done in OnSize |
| 760 | void wxNotebook::SetConstraintSizes( bool WXUNUSED(recurse) ) |
| 761 | { |
| 762 | // don't set the sizes of the pages - their correct size is not yet known |
| 763 | wxControl::SetConstraintSizes(FALSE); |
| 764 | } |
| 765 | |
| 766 | bool wxNotebook::DoPhase( int WXUNUSED(nPhase) ) |
| 767 | { |
| 768 | return TRUE; |
| 769 | } |
| 770 | |
| 771 | #endif |
| 772 | |
| 773 | void wxNotebook::ApplyWidgetStyle() |
| 774 | { |
| 775 | // TODO, font for labels etc |
| 776 | |
| 777 | SetWidgetStyle(); |
| 778 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
| 779 | } |
| 780 | |
| 781 | bool wxNotebook::IsOwnGtkWindow( GdkWindow *window ) |
| 782 | { |
| 783 | return ((m_widget->window == window) || |
| 784 | (NOTEBOOK_PANEL(m_widget) == window)); |
| 785 | } |
| 786 | |
| 787 | //----------------------------------------------------------------------------- |
| 788 | // wxNotebookEvent |
| 789 | //----------------------------------------------------------------------------- |
| 790 | |
| 791 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) |
| 792 | |
| 793 | #endif |