File: gtk_notebook_ex.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (719 lines) | stat: -rw-r--r-- 21,954 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 The CodeLite Team
// file name            : gtk_notebook_ex.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "cl_defs.h"

#if CL_USE_NATIVEBOOK

#include <wx/xrc/xmlres.h>
#include <wx/choicebk.h>
#include <wx/notebook.h>
#include "notebook_ex_nav_dlg.h"
#include "gtk_notebook_ex.h"
#include <wx/button.h>
#include "wx/sizer.h"
#include <wx/debug.h>
#include <wx/log.h>
#include <wx/wupdlock.h>
#include <wx/aui/auibook.h>

#ifdef __WXGTK20__
// We need this ugly hack to workaround a gtk2-wxGTK name-clash
// See http://trac.wxwidgets.org/ticket/10883
#define GSocket GlibGSocket
#include <gtk/gtk.h>
#undef GSocket
#endif


#if wxUSE_STD_CONTAINERS && wxVERSION_NUMBER >= 2900
// The following avoids an undefined reference from m_pagesData.DeleteObject(data) in Notebook::GTKOnPageReordered

// Copying this definition from wx/src/gtk/notebook.cpp prevents warnings from the WX_DEFINE_LIST below
class wxGtkNotebookPage: public wxObject
{
public:
    GtkWidget* m_box;
    GtkWidget* m_label;
    GtkWidget* m_image;
    int m_imageIndex;
};

#include "wx/listimpl.cpp"
WX_DEFINE_LIST(wxGtkNotebookPagesList)
#endif // wxUSE_STD_CONTAINERS

#include <wx/imaglist.h>

class MyNotebookPage: public wxObject
{
public:
#if wxVERSION_NUMBER < 2900
    MyNotebookPage() {
        m_image = -1;
        m_page = NULL;
        m_box = NULL;
    }

    wxString           m_text;
    int                m_image;
    GtkWidget         *m_page;
    GtkLabel          *m_label;
    GtkWidget         *m_box;     // in which the label and image are packed
#else
    GtkWidget* m_box;
    GtkWidget* m_label;
    GtkWidget* m_image;
    int m_imageIndex;
#endif
};


extern "C" {
    static void OnPageReordered(GtkNotebook*, GtkWidget* page, guint new_pos, Notebook* notebk)
    {
        notebk->GTKOnPageReordered(page, new_pos);
    }
}


// The close button callback
static void OnNotebookButtonClicked(GtkWidget *widget, gpointer data)
{
    MyGtkPageInfo* pgInfo = reinterpret_cast<MyGtkPageInfo*>(data);
    if(pgInfo) {
        pgInfo->m_book->GTKHandleButtonCloseClicked(pgInfo);
    }
}

const wxEventType wxEVT_COMMAND_BOOK_PAGE_CHANGING        = XRCID("notebook_page_changing");
const wxEventType wxEVT_COMMAND_BOOK_PAGE_CHANGED         = XRCID("notebook_page_changed");
const wxEventType wxEVT_COMMAND_BOOK_PAGE_CLOSING         = XRCID("notebook_page_closing");
const wxEventType wxEVT_COMMAND_BOOK_PAGE_CLOSED          = XRCID("notebook_page_closed");
const wxEventType wxEVT_COMMAND_BOOK_PAGE_MIDDLE_CLICKED  = XRCID("notebook_page_middle_clicked");
const wxEventType wxEVT_COMMAND_BOOK_PAGE_X_CLICKED       = XRCID("notebook_page_x_btn_clicked");
const wxEventType wxEVT_COMMAND_BOOK_BG_DCLICK            = XRCID("notebook_page_bg_dclick");

Notebook::Notebook(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, long style)
    : wxNotebook(parent, id, pos, size, wxNB_DEFAULT)
    , m_popupWin(NULL)
    , m_contextMenu(NULL)
    , m_style(style)
    , m_notify (true)
    , m_imgList(NULL)
    , m_startingTab(Notebook::npos)
{
    Initialize();
    SetPadding(wxSize(0, 0));
    Connect(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,  wxNotebookEventHandler(Notebook::OnIternalPageChanged),  NULL, this);
    Connect(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, wxNotebookEventHandler(Notebook::OnIternalPageChanging), NULL, this);
    Connect(wxEVT_NAVIGATION_KEY,                 wxNavigationKeyEventHandler(Notebook::OnNavigationKey),  NULL, this);
    Connect(wxEVT_MIDDLE_DOWN,                    wxMouseEventHandler(Notebook::OnMouseMiddle),            NULL, this);
    Connect(wxEVT_LEFT_DCLICK,                    wxMouseEventHandler(Notebook::OnMouseLeftDClick),        NULL, this);
    Connect(wxEVT_CONTEXT_MENU,                   wxContextMenuEventHandler(Notebook::OnMenu),             NULL, this);
}

Notebook::~Notebook()
{
    Disconnect(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED,  wxNotebookEventHandler(Notebook::OnIternalPageChanged),  NULL, this);
    Disconnect(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, wxNotebookEventHandler(Notebook::OnIternalPageChanging), NULL, this);
    Disconnect(wxEVT_NAVIGATION_KEY,                 wxNavigationKeyEventHandler(Notebook::OnNavigationKey),  NULL, this);
    Disconnect(wxEVT_MIDDLE_DOWN,                    wxMouseEventHandler(Notebook::OnMouseMiddle),            NULL, this);
    Disconnect(wxEVT_LEFT_DCLICK,                    wxMouseEventHandler(Notebook::OnMouseLeftDClick),        NULL, this);
    Disconnect(wxEVT_CONTEXT_MENU,                   wxContextMenuEventHandler(Notebook::OnMenu),             NULL, this);

    std::map<wxWindow*, MyGtkPageInfo*>::iterator iter = m_gtk_page_info.begin();
    for(; iter != m_gtk_page_info.end(); iter++) {
        gtk_widget_destroy(iter->second->m_button);
        delete iter->second;
    }
    m_gtk_page_info.clear();

    if(m_imgList) {
        delete m_imgList;
        m_imgList = NULL;
    }
}

void Notebook::AddPage(wxWindow *win, const wxString &text, bool selected, const wxBitmap& bmp)
{
    if(win->GetParent() != this) {
        win->Reparent(this);
    }

    int imgid = DoGetBmpIdx(bmp);
    if (wxNotebook::AddPage(win, text, selected, imgid)) {
        win->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);
        PushPageHistory(win);
        int idx = (int)GetPageCount();
        GTKAddCloseButtonAndReorderable(idx-1);
    }
}

void Notebook::Initialize()
{
    static bool style_applied = false;
    if(!style_applied) {
        gtk_rc_parse_string(
            "style \"tab-close-button-style\" {                              "
            "      GtkWidget::focus-padding = 0                              "
            "      GtkWidget::focus-line-width = 0                           "
            "      xthickness = 0                                            "
            "      ythickness = 0                                            "
            " }                                                              "
            "  widget \"*.tab-close-button\" style \"tab-close-button-style\""
        );
        style_applied = true;
    }
}

void Notebook::SetSelection(size_t page, bool notify)
{
    if (page >= GetPageCount())
        return;

    m_notify = notify;
    wxNotebook::SetSelection(page);
    m_notify = true;

    PushPageHistory(GetPage(page));
    GetPage(page)->SetFocus();
}


wxWindow* Notebook::GetPage(size_t page) const
{
    if (page >= GetPageCount())
        return NULL;

    return wxNotebook::GetPage(page);
}

bool Notebook::RemovePage(size_t page, bool notify)
{
    if (notify) {
        //send event to noitfy that the page has changed
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSING, GetId());
        event.SetSelection( page );
        event.SetEventObject( this );
        GetEventHandler()->ProcessEvent(event);

        if (!event.IsAllowed()) {
            return false;
        }
    }

    wxWindow* win = GetPage(page);
    win->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);

    bool rc = wxNotebook::RemovePage(page);
    if (rc) {
        GTKDeletePgInfo(win);
        PopPageHistory(win);
    }

    if (rc && notify) {
        //send event to noitfy that the page has been closed
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSED, GetId());
        event.SetSelection( page );
        event.SetEventObject( this );
        GetEventHandler()->ProcessEvent(event);
    }

    return rc;
}

bool Notebook::DeletePage(size_t page, bool notify)
{
    if (page >= GetPageCount())
        return false;

    if (notify) {
        //send event to noitfy that the page has changed
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSING, GetId());
        event.SetSelection( page );
        event.SetEventObject( this );
        GetEventHandler()->ProcessEvent(event);

        if (!event.IsAllowed()) {
            return false;
        }
    }

    wxWindow* win = GetPage(page);
    win->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);
    GTKDeletePgInfo(win);

    bool rc = wxNotebook::DeletePage(page);
    if (rc) {
        PopPageHistory(win);
    }

    if (rc && notify) {
        //send event to noitfy that the page has been closed
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSED, GetId());
        event.SetSelection( page );
        event.SetEventObject( this );
        GetEventHandler()->ProcessEvent(event);
    }

    return rc;

}

wxString Notebook::GetPageText(size_t page) const
{
    if (page >= GetPageCount())
        return wxT("");

    return wxNotebook::GetPageText(page);
}

void Notebook::OnNavigationKey(wxNavigationKeyEvent &e)
{
    if ( e.IsWindowChange() ) {
        if (DoNavigate())
            return;
    }

    e.Skip();
}

const wxArrayPtrVoid& Notebook::GetHistory() const
{
    return m_history;
}

void Notebook::InsertPage(size_t index, wxWindow* win, const wxString& text, bool selected, const wxBitmap& bmp)
{
    win->Reparent(this);
    int imgId = DoGetBmpIdx(bmp);
    if (wxNotebook::InsertPage(index, win, text, selected, imgId)) {
        win->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);
        PushPageHistory(win);
        GTKAddCloseButtonAndReorderable(index);
    }
}

void Notebook::SetRightClickMenu(wxMenu* menu)
{
    m_contextMenu = menu;
}

wxWindow* Notebook::GetCurrentPage() const
{
    size_t selection = GetSelection();
    if (selection != Notebook::npos) {
        return GetPage(selection);
    }
    return NULL;
}

size_t Notebook::GetPageIndex(wxWindow *page) const
{
    if ( !page )
        return Notebook::npos;

    for (size_t i=0; i< GetPageCount(); i++) {
        if (GetPage(i) == page) {
            return i;
        }
    }
    return Notebook::npos;
}

size_t Notebook::GetPageIndex(const wxString& text) const
{
    for (size_t i=0; i< GetPageCount(); i++) {

        if (GetPageText(i) == text) {
            return i;
        }
    }
    return Notebook::npos;
}

bool Notebook::SetPageText(size_t index, const wxString &text)
{
    if (index >= GetPageCount())
        return false;
    return wxNotebook::SetPageText(index, text);
}

bool Notebook::DeleteAllPages(bool notify)
{
    bool res = true;
    size_t count = GetPageCount();
    for (size_t i=0; i<count && res; i++) {
        res = DeletePage(0, notify);
    }
    return res;
}

void Notebook::PushPageHistory(wxWindow *page)
{
    if (page == NULL)
        return;

    int where = m_history.Index(page);
    //remove old entry of this page and re-insert it as first
    if (where != wxNOT_FOUND) {
        m_history.Remove(page);
    }
    m_history.Insert(page, 0);
}

void Notebook::PopPageHistory(wxWindow *page)
{
    int where = m_history.Index(page);
    while (where != wxNOT_FOUND) {
        wxWindow *tab = static_cast<wxWindow *>(m_history.Item(where));
        m_history.Remove(tab);

        //remove all appearances of this page
        where = m_history.Index(page);
    }
}

wxWindow* Notebook::GetPreviousSelection()
{
    if (m_history.empty()) {
        return NULL;
    }
    //return the top of the heap
    return static_cast<wxWindow*>( m_history.Item(0));
}

void Notebook::OnIternalPageChanged(wxNotebookEvent &e)
{
    if(e.GetEventObject() == this) {
        DoPageChangedEvent(e);

    } else {
        e.Skip();
    }
}

void Notebook::OnIternalPageChanging(wxNotebookEvent &e)
{
    if(e.GetEventObject() == this) {
        DoPageChangingEvent(e);
    } else {
        e.Skip();
    }
}

void Notebook::OnMouseMiddle(wxMouseEvent &e)
{
    long flags(0);
    int where = wxNotebook::HitTest( e.GetPosition(), &flags );

    if (where != wxNOT_FOUND && HasCloseMiddle()) {

        //send event to noitfy that the page is changing
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_MIDDLE_CLICKED, GetId());
        event.SetSelection   ( where );
        event.SetOldSelection( wxNOT_FOUND );
        event.SetEventObject ( this );
        GetEventHandler()->AddPendingEvent(event);
    }
}

void Notebook::DoPageChangedEvent(wxBookCtrlBaseEvent& e)
{
    if (!m_notify) {
        e.Skip();
        return;
    }

    //send event to noitfy that the page is changing
    NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CHANGED, GetId());
    event.SetSelection   ( e.GetSelection()    );
    event.SetOldSelection( e.GetOldSelection() );
    event.SetEventObject ( this );
    GetEventHandler()->ProcessEvent(event);
    PushPageHistory( GetPage(e.GetSelection()) );
    e.Skip();
}

void Notebook::DoPageChangingEvent(wxBookCtrlBaseEvent& e)
{
    if (!m_notify) {
        e.Skip();
        return;
    }


    //send event to noitfy that the page is changing
    NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CHANGING, GetId());
    event.SetSelection   ( e.GetSelection()    );
    event.SetOldSelection( e.GetOldSelection() );
    event.SetEventObject ( this );
    GetEventHandler()->ProcessEvent(event);

    if ( !event.IsAllowed() ) {
        e.Veto();
    }
    e.Skip();
}

void Notebook::GetEditorsInOrder(std::vector<wxWindow*> &editors)
{
    editors.clear();
    for(size_t i=0; i<GetPageCount(); ++i) {
        editors.push_back( GetPage(i) );
    }
}

void Notebook::AssignImageList(wxImageList* imageList)
{
    wxNotebook::AssignImageList(imageList);
}

void Notebook::SetImageList(wxImageList* imageList)
{
    m_imgList = imageList;
    wxNotebook::SetImageList(m_imgList);
}

void Notebook::OnKeyDown(wxKeyEvent& e)
{
    if (e.GetKeyCode() == WXK_TAB && e.m_controlDown) {
        if (DoNavigate())
            return;

    } else {
        e.Skip();
    }
}

bool Notebook::DoNavigate()
{
    if ( !m_popupWin && GetPageCount() > 1) {

        m_popupWin = new NotebookNavDialog( this );
        m_popupWin->ShowModal();

        wxWindow *page = m_popupWin->GetSelection();
        m_popupWin->Destroy();
        m_popupWin = NULL;

        SetSelection( GetPageIndex(page), true );
        return true;
    }
    return false;
}

void Notebook::OnMenu(wxContextMenuEvent& e)
{
    int where = HitTest( ScreenToClient(::wxGetMousePosition()) );
    if(where != wxNOT_FOUND && m_contextMenu) {
        SetSelection(where, false);
        // dont notify the user about changes
        PopupMenu(m_contextMenu);
    }
    e.Skip();
}

void Notebook::OnMouseLeftDClick(wxMouseEvent& e)
{
    long flags(0);
    int where = HitTest( e.GetPosition(), &flags );

    if (where == wxNOT_FOUND) {

        //send event to noitfy that the page is changing
        NotebookEvent event(wxEVT_COMMAND_BOOK_BG_DCLICK, GetId());
        event.SetEventObject ( this );
        GetEventHandler()->AddPendingEvent(event);
    }
}

void Notebook::GTKAddCloseButtonAndReorderable(int idx)
{
    MyNotebookPage *pg = (MyNotebookPage*) wxNotebook::GetNotebookPage(idx);
    wxWindow* page = GetPage((size_t)idx);
    // Place a close button
    if(HasCloseButton()) {
        GtkWidget *image;
        MyGtkPageInfo *pgInfo = new MyGtkPageInfo;
        pgInfo->m_button = gtk_button_new();
        pgInfo->m_box    = pg->m_box;
        pgInfo->m_book   = this;

        image  = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
        gtk_widget_set_size_request(image, 12, 12);
        gtk_button_set_image (GTK_BUTTON(pgInfo->m_button), image);
        gtk_widget_set_name  (pgInfo->m_button, "tab-close-button");
        gtk_button_set_relief(GTK_BUTTON(pgInfo->m_button), GTK_RELIEF_NONE);
        gtk_box_pack_end     (GTK_BOX(pg->m_box), pgInfo->m_button, FALSE, FALSE, 0);
        // wxGTK has already used gtk_box_pack_end for the tab's label, so atm the close button will be on the left
        // so re-order it to position 0, which in a GTK_PACK_END box means the far right
        gtk_box_reorder_child(GTK_BOX(pg->m_box), pgInfo->m_button, 0);
        gtk_box_set_spacing  (GTK_BOX(pg->m_box), 5);
#ifdef __WXGTK3__
        g_signal_connect (pgInfo->m_button, "clicked", G_CALLBACK (OnNotebookButtonClicked), pgInfo);
#else
        gtk_signal_connect (GTK_OBJECT (pgInfo->m_button), "clicked", GTK_SIGNAL_FUNC (OnNotebookButtonClicked), pgInfo);
#endif
        m_gtk_page_info[page] = pgInfo;
        GTKShowCloseButton(idx);
    }

    // Make this tab re-orderable
    gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(this->m_widget), page->m_widget, true);
    g_signal_connect(GTK_NOTEBOOK(this->m_widget), "page-reordered", G_CALLBACK(OnPageReordered), this);
}

void Notebook::GTKDeletePgInfo(wxWindow* page)
{
    std::map<wxWindow*, MyGtkPageInfo*>::iterator iter = m_gtk_page_info.find(page);
    if(iter != m_gtk_page_info.end()) {
        delete iter->second;
        m_gtk_page_info.erase(iter);
    }
}

MyGtkPageInfo* Notebook::GTKGetPgInfo(wxWindow* page)
{
    std::map<wxWindow*, MyGtkPageInfo*>::iterator iter = m_gtk_page_info.find(page);
    if(iter == m_gtk_page_info.end())
        return NULL;
    return iter->second;
}

void Notebook::GTKShowCloseButton(int idx)
{
    if ((idx != wxNOT_FOUND) && (idx < (int)GetPageCount())) {
        MyGtkPageInfo* pgInfo = GTKGetPgInfo(GetPage(idx));
        if(pgInfo) {
            gtk_widget_show(pgInfo->m_button);
            gtk_widget_show(pgInfo->m_box);
        }
    }
}

void Notebook::GTKHandleButtonCloseClicked(MyGtkPageInfo* pgInfo)
{
    // Notebook button was clicked
    int idx = GTKIndexFromPgInfo(pgInfo);
    if(idx == wxNOT_FOUND)
        return;

    NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_X_CLICKED, GetId());
    event.SetSelection   ( idx );
    event.SetOldSelection( wxNOT_FOUND );
    event.SetEventObject ( this );
    GetEventHandler()->AddPendingEvent(event);
}

int Notebook::GTKIndexFromPgInfo(MyGtkPageInfo* pgInfo)
{
    for(size_t i=0; i<GetPageCount(); i++) {
        MyGtkPageInfo* info = GTKGetPgInfo(GetPage(i));
        if(info == pgInfo)
            return (int)i;
    }
    return wxNOT_FOUND;
}

int Notebook::DoGetBmpIdx(const wxBitmap& bmp)
{
    int idx = wxNOT_FOUND;
    if(bmp.IsOk()) {
        if(m_imgList == NULL) {
            SetImageList(new wxImageList(bmp.GetWidth(), bmp.GetHeight(), true));
        }
        idx = m_imgList->Add(bmp);
    }
    return idx;
}

int Notebook::GetPageindexFromWidget(GtkWidget* gtk_page)
{
    wxCHECK_MSG(gtk_page, wxNOT_FOUND, wxT("Null gtk widget page in notebook"));

    for (size_t n=0; n < GetPageCount(); ++n) {
        wxWindow* page = GetPage(n);
        wxCHECK_MSG(page, wxNOT_FOUND, wxT("Null page in notebook"));
        if (page->m_widget == gtk_page) {
            return (int)n;
        }
    }

    return wxNOT_FOUND;
}

void Notebook::GTKOnPageReordered(GtkWidget* page, int new_pos)
{
    wxCHECK_RET(page, wxT("Null gtk widget page"));

    // gtk tells us the new position of the tab, but we need to deduce the old one
    // As the wxGTK notebook hasn't been re-ordered yet, we can do so by finding the page's GtkWidget
    int old_pos = GetPageindexFromWidget(page);
    if (((size_t)new_pos) == Notebook::npos || ((size_t)new_pos) == Notebook::npos || new_pos == old_pos) {
        return;
    }

    // Now update the wxNotebook to match the new reality. First the wxNotebookPage array
    wxWindow* win = m_pages[old_pos];
    m_pages.RemoveAt(old_pos);
    m_pages.Insert(win, new_pos);

    // Then the 'extra' data list
    wxGtkNotebookPage* data = m_pagesData.Item(old_pos)->GetData();
    m_pagesData.DeleteObject(data);
    m_pagesData.Insert(new_pos, data);

    // In case the Workspace View 'Tabs' pane is being sorted by editor order, fire an event
    // and make it the Aui equivalent, since that's being Connected() to already
    wxAuiNotebookEvent event(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG, GetId());
    wxPostEvent(this, event);
}

wxBitmap Notebook::GetPageBitmap(size_t page) const
{
    if(page >= GetPageCount()) {
        return wxNullBitmap;
    }

    int imgIdx = wxNotebook::GetPageImage(page);
    if (imgIdx == wxNOT_FOUND) {
        return wxNullBitmap;
    }

    return m_imgList->GetBitmap(imgIdx);
}

wxArrayString Notebook::GetPagesTextInOrder() const
{
    wxArrayString labels;
    for(size_t i=0; i<GetPageCount(); ++i) {
        labels.Add( GetPageText(i) );
    }
    return labels;
}

#endif