File: tabgroupspane.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (751 lines) | stat: -rw-r--r-- 31,747 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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2010 by Eran Ifrah
// file name            : tabgroupspane.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 "codelite_events.h"
#include "editor_config.h"
#include "event_notifier.h"
#include "fileutils.h"
#include "frame.h"
#include "globals.h"
#include "ieditor.h"
#include "pluginmanager.h"
#include "tabgroupmanager.h"
#include "tabgroupspane.h"
#include "wx_xml_compatibility.h"
#include <algorithm>
#include <wx/accel.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
#include <wx/regex.h>
#include <wx/sizer.h>
#include <wx/textdlg.h>
#include <wx/wupdlock.h>
#include <wx/xml/xml.h>
#include <wx/xrc/xmlres.h>

class MyDropTarget : public wxTextDropTarget
{
    TabgroupsPane* m_pane;

public:
    MyDropTarget(TabgroupsPane* pane)
        : m_pane(pane)
    {
    }
    virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& data)
    {
        static wxRegEx re("\\{Class:Notebook,TabIndex:([0-9]+)\\}\\{(.*?)\\}", wxRE_ADVANCED);
        if(re.Matches(data)) { m_pane->FileDropped(re.GetMatch(data, 2)); }
        return true;
    }
};

TabgroupsPane::TabgroupsPane(wxWindow* parent, const wxString& caption)
    : wxPanel(parent, wxID_ANY)
{
    m_node = NULL;
    wxBoxSizer* sz = new wxBoxSizer(wxVERTICAL);
    m_tree = new wxTreeCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                            wxTR_LINES_AT_ROOT | wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS,
                            wxDefaultValidator, wxT("tabgrouptree"));
    MSWSetNativeTheme(m_tree);
    m_tree->SetDropTarget(new MyDropTarget(this));

    // Add icons to the tree
    BitmapLoader* bmpLoader = clGetManager()->GetStdIcons();
    wxImageList* imgList = bmpLoader->MakeStandardMimeImageList();
    m_tree->AssignImageList(imgList);
    sz->Add(m_tree, 1, wxEXPAND);

    // DisplayTabgroups(); No, we can't do this here: it's too soon, a 'session' hasn't been loaded
    SetSizer(sz);
    sz->Layout();

    // We can't use an accelerator table here for Cut/Copy/Paste, as the events are hijacked by the frame's shortcuts
    // But delete isn't, so..
    wxAcceleratorEntry entries[1];
    entries[0].Set(wxACCEL_NORMAL, WXK_DELETE, wxID_DELETE);
    wxAcceleratorTable accel(1, entries);
    m_tree->SetAcceleratorTable(accel);

    m_tree->AddRoot(wxT("Tab Groups"), -1, -1, NULL);

    m_tree->Connect(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, wxTreeEventHandler(TabgroupsPane::OnItemActivated), NULL, this);
    m_tree->Connect(wxEVT_COMMAND_TREE_ITEM_MENU, wxTreeEventHandler(TabgroupsPane::OnItemRtClick), NULL, this);
    m_tree->Connect(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, wxTreeEventHandler(TabgroupsPane::OnBeginLabelEdit), NULL,
                    this);
    m_tree->Connect(wxEVT_COMMAND_TREE_END_LABEL_EDIT, wxTreeEventHandler(TabgroupsPane::OnEndLabelEdit), NULL, this);
    m_tree->Connect(wxEVT_COMMAND_TREE_BEGIN_DRAG, wxTreeEventHandler(TabgroupsPane::OnBeginDrag), NULL, this);
    m_tree->Connect(wxEVT_COMMAND_TREE_END_DRAG, wxTreeEventHandler(TabgroupsPane::OnEndDrag), NULL, this);
    // Context menu:
    Connect(TGM_ID_Add, TGM_ID_RemoveItem, wxEVT_COMMAND_MENU_SELECTED,
            wxCommandEventHandler(TabgroupsPane::OnContextMenu), NULL, this);
    // Shortcuts:
    m_tree->Connect(wxID_COPY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnCopy), NULL, this);
    m_tree->Connect(wxID_PASTE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnPaste), NULL, this);
    m_tree->Connect(wxID_CUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnCut), NULL, this);
    m_tree->Connect(wxID_DELETE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnDelete), NULL,
                    this);

    m_themeHelper = new ThemeHandlerHelper(this);
    EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &TabgroupsPane::OnWorkspaceClosed, this);
    EventNotifier::Get()->Bind(wxEVT_INIT_DONE, &TabgroupsPane::OnInitDone, this);
}

TabgroupsPane::~TabgroupsPane()
{
    wxDELETE(m_themeHelper);
    m_tree->Disconnect(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, wxTreeEventHandler(TabgroupsPane::OnItemActivated), NULL,
                       this);
    m_tree->Disconnect(wxEVT_COMMAND_TREE_ITEM_MENU, wxTreeEventHandler(TabgroupsPane::OnItemRtClick), NULL, this);
    m_tree->Disconnect(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, wxTreeEventHandler(TabgroupsPane::OnBeginLabelEdit), NULL,
                       this);
    m_tree->Disconnect(wxEVT_COMMAND_TREE_END_LABEL_EDIT, wxTreeEventHandler(TabgroupsPane::OnEndLabelEdit), NULL,
                       this);
    m_tree->Disconnect(wxEVT_COMMAND_TREE_BEGIN_DRAG, wxTreeEventHandler(TabgroupsPane::OnBeginDrag), NULL, this);
    m_tree->Disconnect(wxEVT_COMMAND_TREE_END_DRAG, wxTreeEventHandler(TabgroupsPane::OnEndDrag), NULL, this);
    // Context menu:
    Disconnect(TGM_ID_Add, TGM_ID_RemoveItem, wxEVT_COMMAND_MENU_SELECTED,
               wxCommandEventHandler(TabgroupsPane::OnContextMenu), NULL, this);
    // Shortcuts:
    m_tree->Disconnect(wxID_COPY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnCopy), NULL,
                       this);
    m_tree->Disconnect(wxID_PASTE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnPaste), NULL,
                       this);
    m_tree->Disconnect(wxID_CUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnCut), NULL, this);
    m_tree->Disconnect(wxID_DELETE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(TabgroupsPane::OnDelete), NULL,
                       this);

    delete m_node;

    EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &TabgroupsPane::OnWorkspaceClosed, this);
    EventNotifier::Get()->Bind(wxEVT_INIT_DONE, &TabgroupsPane::OnInitDone, this);
}

void TabgroupsPane::OnInitDone(wxCommandEvent& e)
{
    e.Skip();
    DisplayTabgroups(true); // Display any Global tabgroups
}

bool sortfunction(const spTabGrp& x, const spTabGrp& y) { return x.first.CmpNoCase(y.first) < 0; }

void TabgroupsPane::DisplayTabgroups(bool isGlobal /*=false*/)
{
    vTabGrps& tabgroups = TabGroupsManager::Get()->GetTabgroups(isGlobal);
    std::sort(tabgroups.begin(), tabgroups.end(), sortfunction);
    vTabGrps::const_iterator iter = tabgroups.begin();
    for(; iter != tabgroups.end(); ++iter) {
        AddTreeItem(isGlobal, iter->first, iter->second);
    }
    m_tree->Expand(GetRootItemForTabgroup(isGlobal));
    GetSizer()->Layout();
}

void TabgroupsPane::AddTreeItem(bool isGlobal, const wxString& tabgroupfpath, const wxArrayString& tabfilepaths,
                                const wxTreeItemId insertafter /*=wxTreeItemId()*/)
{
    wxCHECK_RET(!tabgroupfpath.IsEmpty(), wxT("A tabgroup with an empty name in TabgroupsPane::AddTreeItem"));

    // Both for the tabgroup and its constituent files, we display the filename but save the filepaths in the
    // TreeItemData
    wxString tabgroupname = tabgroupfpath.AfterLast(wxFILE_SEP_PATH).BeforeLast('.');
    wxTreeItemId tbnameId;

    int folderImgID = clGetManager()->GetStdIcons()->GetMimeImageId(FileExtManager::TypeFolder);
    if(insertafter.IsOk()) {
        // There's a valid id to insert after, so..
        tbnameId = m_tree->InsertItem(GetRootItemForTabgroup(isGlobal), insertafter, tabgroupname, folderImgID, folderImgID,
                                      new TabGrpTreeItemData(tabgroupfpath, TGT_group));
    } else {
        // Otherwise, append
        tbnameId = m_tree->AppendItem(GetRootItemForTabgroup(isGlobal), tabgroupname, folderImgID, folderImgID,
                                      new TabGrpTreeItemData(tabgroupfpath, TGT_group));
    }
    wxCHECK_RET(tbnameId.IsOk(), wxT("Failed to add the tabgroup to the tree"));
    // NB. I'm not going to try to cater for idiots who have >1 tab with the same filename!
    for(size_t n = 0; n < tabfilepaths.GetCount(); ++n) {
        wxString name = tabfilepaths.Item(n).AfterLast(wxFILE_SEP_PATH);
        int imgIdx = DoGetIconIndex(name);
        m_tree->AppendItem(tbnameId, name, imgIdx, imgIdx, new TabGrpTreeItemData(tabfilepaths.Item(n)));
    }
}

void TabgroupsPane::OnItemActivated(wxTreeEvent& event)
{
    wxTreeItemId item = event.GetItem();
    if(!item.IsOk()) { return; }
    TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
    if(!data) { return; }
    wxString filepath = data->GetFilepath();
    wxFileName fn(filepath);

    if(data->GetType() == TGT_group) { // Activating a tabgroup means offer to load it
        wxArrayString previousgroups;
        EditorConfigST::Get()->GetRecentItems(previousgroups, wxT("RecentTabgroups"));

        if(!fn.FileExists()) {
            wxMessageBox(_("I'm afraid that tabgroup no longer exists :/"), _("CodeLite"), wxICON_ERROR | wxOK, this);
            m_tree->DeleteChildren(item);
            m_tree->Delete(item);
            // Remove it from the RecentTabgroups list too
            int index = previousgroups.Index(filepath);
            if(index != wxNOT_FOUND) {
                previousgroups.RemoveAt(index);
                EditorConfigST::Get()->SetRecentItems(previousgroups, wxT("RecentTabgroups"));
            }
            return;
        }

        std::vector<LEditor*> editors;
        clMainFrame::Get()->GetMainBook()->GetAllEditors(editors, MainBook::kGetAll_IncludeDetached |
                                                                      MainBook::kGetAll_RetainOrder);
        if(editors.size() > 0) {
            // If there are editors currently loaded, ask if they are to be replaced or added to
            wxString msg(_("Do you want to replace the existing editors? (Say 'No' to load the new ones alongside)"));
            int ans = wxMessageBox(msg, _("Load the tabgroup"), wxICON_QUESTION | wxYES_NO | wxCANCEL, this);
            if(ans == wxCANCEL) { return; }
            if(ans == wxYES) { clMainFrame::Get()->GetMainBook()->CloseAll(true); }
        }

        clWindowUpdateLocker locker(clMainFrame::Get());
        TabGroupEntry session;
        if(SessionManager::Get().GetSession(filepath.BeforeLast(wxT('.')), session, wxString(wxT("tabgroup")),
                                            tabgroupTag)) {
            clMainFrame::Get()->GetMainBook()->RestoreSession(session);

            // Remove any previous instance of this group from the history, then prepend it and save
            int index = previousgroups.Index(filepath);
            if(index != wxNOT_FOUND) { previousgroups.RemoveAt(index); }
            previousgroups.Insert(filepath, 0);
            EditorConfigST::Get()->SetRecentItems(previousgroups, wxT("RecentTabgroups"));
        }
    } else {
        // Activating an item means load just it, as an editor
        if(!fn.FileExists()) {
            wxMessageBox(_("I'm afraid that tabgroup item no longer exists :/"), _("CodeLite"), wxICON_ERROR | wxOK,
                         this);
            DeleteTabgroupItem();
            return;
        }

        clMainFrame::Get()->GetMainBook()->OpenFile(filepath);
    }
}

void TabgroupsPane::OnItemRtClick(wxTreeEvent& event)
{
    wxTreeItemId item = event.GetItem();
    if(!item.IsOk()) { return; }
    TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
    if(!data) { return; }

    wxMenu menu; // Tabgroup Menu
    if(data->GetType() == TGT_group) {
        menu.Append(TGM_ID_Add, _("&Add a new item to this tabgroup"));
        if(m_node) { menu.Append(TGM_ID_Paste, _("&Paste an item into this tabgroup")); }
        menu.AppendSeparator();
        menu.Append(TGM_ID_Duplicate, _("Du&plicate this tabgroup"));
        menu.Append(TGM_ID_Delete, _("&Delete this tabgroup"));
    } else {
        menu.Append(TGM_ID_CopyItem, _("&Copy this item, to be pasted to another tabgroup"));
        menu.Append(TGM_ID_CutItem, _("Cu&t this item, to be pasted to another tabgroup"));
        if(m_node) { menu.Append(TGM_ID_Paste, _("&Paste an item into this tabgroup")); }
        menu.Append(TGM_ID_RemoveItem, _("&Remove this item from the tabgroup"));
    }
    wxPoint pt = event.GetPoint();
    PopupMenu(&menu, pt.x, pt.y);
}

void TabgroupsPane::OnContextMenu(wxCommandEvent& event)
{
    switch(event.GetId()) {
    case TGM_ID_Add:
        AddTabgroupItem();
        break;
    case TGM_ID_Paste:
        PasteTabgroupItem();
        break;
    case TGM_ID_Delete:
        DeleteTabgroup();
        break;
    case TGM_ID_Duplicate:
        DuplicateTabgroup();
        break;
    case TGM_ID_CopyItem:
        CopyTabgroupItem();
        break;
    case TGM_ID_CutItem:
        DeleteTabgroupItem(true);
        break;
    case TGM_ID_RemoveItem:
        DeleteTabgroupItem(false);
        break;
    }
}

void TabgroupsPane::OnDelete(wxCommandEvent& WXUNUSED(event)) // From a keypress, not the context menu
{
    wxTreeItemId item = m_tree->GetSelection();
    if(item.IsOk()) {
        TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
        if(!data) { return; }
        if(data->GetType() == TGT_group) {
            DeleteTabgroup();
        } else {
            DeleteTabgroupItem();
        }
    }
}

void TabgroupsPane::OnBeginLabelEdit(wxTreeEvent& event)
{
    wxTreeItemId item = event.GetItem();
    if(item.IsOk()) {
        TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
        if(!data || data->GetType() == TGT_group) {
            // Only allow renaming of a group; renaming an item makes little sense: the name is the filename
            return;
        }
    }

    event.Veto();
}

void TabgroupsPane::OnEndLabelEdit(wxTreeEvent& event)
{
    wxTreeItemId item = event.GetItem();
    if(!item.IsOk()) {
        event.Veto();
        return;
    }

    TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
    if(!data) { return; }
    wxFileName oldfilepath(data->GetFilepath());

    wxString newfilename = event.GetLabel();
    if(newfilename.IsEmpty() || newfilename == oldfilepath.GetFullName()) {
        event.Veto();
        return;
    }

    wxFileName newfilepath(oldfilepath.GetPath(), newfilename);
    if(newfilepath.FileExists()) {
        wxString msg(_("There is already a file with this name. Overwrite it?"));
        if(wxMessageBox(msg, _("Overwrite?"), wxICON_QUESTION | wxYES_NO, this) != wxYES) {
            event.Veto(); // The user doesn't want to overwrite
            return;
        }
    }
    // If we're here, the event won't be vetoed, so the tree shows the new name
    // Update the file system correspondingly
    if(wxRenameFile(oldfilepath.GetFullPath(), newfilepath.GetFullPath(), true)) {
        clMainFrame::Get()->GetStatusBar()->SetMessage(_("Tabgroup renamed"));
        return;
    }
}

void TabgroupsPane::OnBeginDrag(wxTreeEvent& event)
{
    wxTreeItemId item = event.GetItem();
    if(item.IsOk()) {
        TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
        if(data && data->GetType() == TGT_item) {
            // Only allow dragging of an item, not a group
            m_draggedItem = item;
            event.Allow(); // need explicitly to allow drag
        }
    }
}

void TabgroupsPane::OnEndDrag(wxTreeEvent& event)
{
    if(!m_draggedItem.IsOk()) { return; }

    wxTreeItemId ItemSrc = m_draggedItem;
    m_draggedItem = wxTreeItemId(); // Reset the dragged-item store

    if(wxGetKeyState(WXK_ESCAPE)) {
        return; // If ESC is being pressed, take this as a hint
    }

    wxTreeItemId ItemDest = event.GetItem();
    if(!ItemDest.IsOk()) { return; }

    // Are we doing a Move or a Copy/Paste? Use the Ctrl/Alt key to decide
    wxKeyCode copykey =
#ifdef __WXMAC__
        WXK_ALT; // Apparently the ALT key is used for this in Macs
#else
        WXK_CONTROL;
#endif
    if(wxGetKeyState(copykey)) {
        CopyTabgroupItem(ItemSrc);
    } else {
        DeleteTabgroupItem(true, ItemSrc);
    }

    PasteTabgroupItem(ItemDest);
}

void TabgroupsPane::AddTabgroupItem()
{
    wxTreeItemId selection = m_tree->GetSelection();
    if(!selection.IsOk()) { return; }

    wxString defaultPath;
    IEditor* editor = PluginManager::Get()->GetActiveEditor();
    if(editor) { defaultPath = editor->GetFileName().GetPath(); }

    wxString newfilepath = ::wxFileSelector(_("Select the file you want to add"), defaultPath);
    if(newfilepath.IsEmpty()) { return; }

    if(!wxFileExists(newfilepath)) {
        if(wxMessageBox(_("That filepath doesn't seem to exist. Are you sure?"), _("CodeLite"),
                        wxICON_QUESTION | wxYES_NO, this) != wxYES) {
            return;
        }
    }

    AddFile(newfilepath);
}

void TabgroupsPane::PasteTabgroupItem(wxTreeItemId itemtopaste /*= wxTreeItemId()*/)
{
    if(m_node == NULL) {
        return; // The data to be pasted should have been stored here, so...
    }

    wxTreeItemId GroupId;
    if(itemtopaste.IsOk()) {
        GroupId = itemtopaste; // We got here from D'n'D, so the item was passed
    } else {
        GroupId = m_tree->GetSelection(); // Just a plain 'Paste'
    }
    if(!GroupId.IsOk()) { return; }

    // We don't yet know if we're pasting onto a tabgroup or a contained item, so duplicate
    wxTreeItemId ItemId(GroupId);

    TabGrpTreeItemData* groupdata = (TabGrpTreeItemData*)m_tree->GetItemData(GroupId);
    if(!groupdata) { return; }
    if(groupdata->GetType() == TGT_item) {
        GroupId = m_tree->GetItemParent(ItemId);
        wxCHECK_RET(GroupId.IsOk(), wxT("Trying to paste on something that isn't a tabgroup"));
        groupdata = (TabGrpTreeItemData*)m_tree->GetItemData(GroupId);
    } else {
        // The paste *was* on the group, so null ItemId: we use ItemId.IsOk() later as a flag
        ItemId = wxTreeItemId();
    }
    if(!groupdata) { return; }
    wxCHECK_RET(groupdata->GetType() == TGT_group, wxT("Trying to paste on something that isn't a tabgroup"));

    wxString filepath = groupdata->GetFilepath();
    wxXmlDocument doc(filepath);
    if(!doc.IsOk()) {
        wxMessageBox(_("Failed to load the destination tabgroup :/"), _("CodeLite"), wxICON_ERROR | wxOK, this);
        return;
    }

    bool NeedToChangeTree = true;
    wxXmlNode* node = TabGroupsManager::Get()->FindTabgroupItem(doc, filepath, m_copieditem_filepath);
    if(node) {
        if(wxMessageBox(_("There is already an item with this filepath in the tabgroup. Overwrite it?"), _("CodeLite"),
                        wxICON_QUESTION | wxYES_NO, this) == wxYES) {
            wxXmlNode* parent = node->GetParent();
            if(parent) { parent->RemoveChild(node); }
            NeedToChangeTree = false;
        } else {
            return; // The user doesn't want to overwrite
        }
    }

    // If the paste was onto an item (not the group) and there wasn't already an item with this filepath,
    // pass info to DoAddItemToTabgroup() to find the node corresponding to the item, so that we can insert after it in
    // the doc
    wxString nextitemfilepath;
    if(ItemId.IsOk() && NeedToChangeTree) {
        TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(ItemId);
        if(data) { nextitemfilepath = data->GetFilepath(); }
    }

    if(TabGroupsManager::Get()->DoAddItemToTabgroup(doc, new wxXmlNode(*m_node), filepath, nextitemfilepath)) {
        doc.Save(filepath);

        if(NeedToChangeTree) {
            // We're not overwriting a pre-existing tab, so add the pasted tab to the tree
            wxString name = m_copieditem_filepath.AfterLast(wxFILE_SEP_PATH);
            int imgIdx = DoGetIconIndex(name);
            if(ItemId.IsOk()) {
                // either after the selected item (if there was one)..
                m_tree->InsertItem(GroupId, ItemId, name, imgIdx, imgIdx,
                                   new TabGrpTreeItemData(m_copieditem_filepath));
            } else {
                // ..or append to the group
                m_tree->AppendItem(GroupId, name, imgIdx, imgIdx, new TabGrpTreeItemData(m_copieditem_filepath));
            }
        }

        clMainFrame::Get()->GetStatusBar()->SetMessage(_("Tabgroup item pasted"));
    }
}

void TabgroupsPane::DeleteTabgroup()
{
    wxTreeItemId item = m_tree->GetSelection();
    if(!item.IsOk()) { return; }
    TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
    if(!data) { return; }
    wxCHECK_RET(data->GetType() == TGT_group, wxT("The selection wasn't a tabgroup"));

    wxString filepath = data->GetFilepath();
    if(!wxFileName::FileExists(filepath)) {
        m_tree->DeleteChildren(item);
        m_tree->Delete(item);
        return;
    }

    wxString msg = wxString::Format(_("Delete tabgroup %s?"), m_tree->GetItemText(item).c_str());
    if(wxMessageBox(msg, _("CodeLite"), wxICON_QUESTION | wxYES_NO, this) == wxYES) {
        m_tree->Delete(item);

        // Remove it from the RecentTabgroups list too
        wxArrayString previousgroups;
        EditorConfigST::Get()->GetRecentItems(previousgroups, wxT("RecentTabgroups"));
        int index = previousgroups.Index(filepath);
        if(index != wxNOT_FOUND) {
            previousgroups.RemoveAt(index);
            EditorConfigST::Get()->SetRecentItems(previousgroups, wxT("RecentTabgroups"));
        }

        clRemoveFile(filepath);

        clMainFrame::Get()->GetStatusBar()->SetMessage(_("Tabgroup deleted"));
    }
}

void TabgroupsPane::DuplicateTabgroup()
{
    wxTreeItemId selection = m_tree->GetSelection();
    if(!selection.IsOk()) { return; }
    TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(selection);
    if(!data) { return; }

    wxCHECK_RET(data->GetType() == TGT_group, wxT("The selection wasn't a tabgroup"));

    wxFileName oldfilepath(data->GetFilepath());
    wxCHECK_RET(oldfilepath.FileExists(), wxT("There was no file corresponding to the tree selection"));

    wxString newname = wxGetTextFromUser(_("What name to you want to give the duplicated tabgroup?"),
                                         _("Duplicate a tabgroup"), oldfilepath.GetFullName(), this);
    if(newname.IsEmpty() || newname == oldfilepath.GetFullName()) { return; }

    if (!newname.EndsWith(".tabgroup")) {
        newname << ".tabgroup"; // Otherwise things break
    }
    wxFileName newfilepath(oldfilepath.GetPath(), newname);
    if(newfilepath.FileExists()) {
        wxMessageBox(_("Sorry, there is already a tabgroup with this name"), _("CodeLite"), wxICON_INFORMATION | wxOK,
                     this);
        return;
    }

    if(!wxCopyFile(oldfilepath.GetFullPath(), newfilepath.GetFullPath())) {
        wxMessageBox(_("Sorry, duplication of the tabgroup failed :/"), _("CodeLite"), wxICON_ERROR | wxOK, this);
        return;
    }
    wxTreeItemId parent = m_tree->GetItemParent(selection);
    wxCHECK_RET(parent.IsOk(), "A parentless tabgroup");
    TabGrpTreeItemData* parentdata = (TabGrpTreeItemData*)m_tree->GetItemData(parent);
    wxCHECK_RET(parentdata, "An unnamed tabgroup parent");
    bool isGlobal = parentdata->GetFilepath().StartsWith(_("Global"));

    // Do the rest in a separate method, which is also called by Frame::OnFileSaveTabGroup
    if(AddNewTabgroupToTree(isGlobal, newfilepath.GetFullPath(), selection)) {
        clMainFrame::Get()->GetStatusBar()->SetMessage(_("Tabgroup duplicated"));
    }
}

bool TabgroupsPane::AddNewTabgroupToTree(bool isGlobal, const wxString& newfilepath, wxTreeItemId selection /*=wxTreeItemId()*/)
{
    // Tell TabGroupsManager to load the new group. Probably a good idea anyway, but we'll need it below
    TabGroupsManager::Get()->LoadTabgroupData(isGlobal, newfilepath);
    // Now add the newly-loaded info to the tree
    wxArrayString items;
    if(TabGroupsManager::Get()->FindTabgroup(isGlobal, newfilepath, items)) {
        AddTreeItem(isGlobal, newfilepath, items, selection);
        return true;
    }

    return false;
}

void TabgroupsPane::CopyTabgroupItem(wxTreeItemId itemtocopy /*= wxTreeItemId()*/)
{
    wxTreeItemId item;
    if(itemtocopy.IsOk()) {
        item = itemtocopy; // We got here from D'n'D, so the item was passed
    } else {
        item = m_tree->GetSelection(); // Just a plain 'Copy'
    }
    if(!item.IsOk()) { return; }

    TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
    if(!data) { return; }
    if(data->GetType() != TGT_item) {
        return; // I can't think of any reason to copy a whole group
    }

    wxTreeItemId GroupId = m_tree->GetItemParent(item);
    TabGrpTreeItemData* groupdata = (TabGrpTreeItemData*)m_tree->GetItemData(GroupId);
    if(!groupdata) { return; }

    wxString filepath = groupdata->GetFilepath();
    wxXmlDocument doc(filepath);
    wxCHECK_RET(doc.IsOk(), wxT("Failed to load the tabgroup to be copied :/"));

    wxString itemfilepath = data->GetFilepath();
    wxXmlNode* node = TabGroupsManager::Get()->FindTabgroupItem(doc, filepath, itemfilepath);
    wxCHECK_RET(node != NULL, wxT("Failed to find the tab-item to be copied :/"));

    // All is well, so store the data in m_node and m_copieditem_filepath
    delete m_node;
    m_node = new wxXmlNode(*node);
    m_copieditem_filepath = itemfilepath;
    clMainFrame::Get()->GetStatusBar()->SetMessage(_("Tabgroup item copied"));
}

void TabgroupsPane::DeleteTabgroupItem(bool DoCut /*=false*/, wxTreeItemId itemtocut /*= wxTreeItemId()*/)
{
    wxTreeItemId item;
    if(itemtocut.IsOk()) {
        item = itemtocut; // We got here from D'n'D, so the item was passed
    } else {
        item = m_tree->GetSelection(); // Just a plain 'Cut' or 'Delete'
    }
    if(!item.IsOk()) { return; }
    TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(item);
    if(!data) { return; }
    wxCHECK_RET(data->GetType() == TGT_item, wxT("The selection was a tabgroup, not a tabgroup item"));
    wxString itemfilepath = data->GetFilepath(); // Need to do this here, before the item is deleted!
    wxTreeItemId parent = m_tree->GetItemParent(item);
    TabGrpTreeItemData* tabgrpdata = (TabGrpTreeItemData*)m_tree->GetItemData(parent);
    if(!tabgrpdata) { return; }
    wxString filepath = tabgrpdata->GetFilepath();
    if(!wxFileName::FileExists(filepath)) { return; }

    if(!DoCut) { // If we're deleting, check the user really meant it
        wxString msg = wxString::Format(_("Remove item %s from %s?"), m_tree->GetItemText(item).c_str(),
                                        m_tree->GetItemText(parent).c_str());
        if(wxMessageBox(msg, _("CodeLite"), wxICON_QUESTION | wxYES_NO, this) != wxYES) { return; }
    }
    m_tree->Delete(item);

    wxXmlDocument doc(filepath);
    if(doc.IsOk()) {
        wxXmlNode* TabInfoNode = TabGroupsManager::Get()->DoDeleteTabgroupItem(doc, filepath, itemfilepath);
        if(TabInfoNode) {
            if(DoCut) {
                // If we're cutting, store the deleted node ready for paste
                m_node = new wxXmlNode(*TabInfoNode);
                m_copieditem_filepath = itemfilepath;
                clMainFrame::Get()->GetStatusBar()->SetMessage(_("Tabgroup item Cut"));
            } else {
                clMainFrame::Get()->GetStatusBar()->SetMessage(_("Tabgroup item deleted"));
            }
            return;
        }
    }

    // Either !doc.IsOk() or !TabInfoNode
    wxMessageBox(_("Failed to amend the tabgroup :/"), _("CodeLite"), wxICON_ERROR | wxOK, this);
}

int TabgroupsPane::DoGetIconIndex(const wxString& filename)
{
    int imgIdx = clGetManager()->GetStdIcons()->GetMimeImageId(filename);
    if(imgIdx == wxNOT_FOUND) { imgIdx = clGetManager()->GetStdIcons()->GetMimeImageId(FileExtManager::TypeText); }
    return imgIdx;
}

void TabgroupsPane::OnWorkspaceClosed(wxCommandEvent& e)
{
    e.Skip();
    m_tree->DeleteChildren(GetRootItemForTabgroup(false));
}

void TabgroupsPane::FileDropped(const wxString& filename)
{
    // An editor was DnD to the TabGroup pane view
    AddFile(filename);
}

void TabgroupsPane::AddFile(const wxString& filename)
{
    // Create a suitable node, then piggyback on PasteTabgroupItem() to do the real work
    // Reuse m_copieditem_filepath and m_node for this, so cache them first
    wxString oldcopieditem_filepath = m_copieditem_filepath;
    wxXmlNode* oldnode = m_node;

    m_copieditem_filepath = filename;
    m_node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("TabInfo"));
    wxXmlNode* fp = new wxXmlNode(m_node, wxXML_ELEMENT_NODE, wxT("wxString"));
    fp->AddProperty(wxT("Value"), filename);
    fp->AddProperty(wxT("Name"), wxT("FileName"));
    wxXmlNode* fvl = new wxXmlNode(m_node, wxXML_ELEMENT_NODE, wxT("int"));
    fvl->AddProperty(wxT("Name"), wxT("FirstVisibleLine"));
    wxXmlNode* cl = new wxXmlNode(m_node, wxXML_ELEMENT_NODE, wxT("int"));
    cl->AddProperty(wxT("Name"), wxT("CurrentLine"));
    wxXmlNode* bm = new wxXmlNode(m_node, wxXML_ELEMENT_NODE, wxT("wxArrayString"));
    bm->AddProperty(wxT("Name"), wxT("Bookmarks"));

    PasteTabgroupItem();

    // Restore the old values
    m_copieditem_filepath = oldcopieditem_filepath;
    wxDELETE(m_node);
    m_node = oldnode;
}

wxTreeItemId TabgroupsPane::GetRootItemForTabgroup(bool global)
{
    wxString label = global ? "Global tabgroups" : "Workspace tabgroups";

    // First see if the appropriate tabgroup base-item has already been created
    wxTreeItemIdValue cookie;
    wxTreeItemId child = m_tree->GetFirstChild(m_tree->GetRootItem(), cookie);
    while (child.IsOk()) {
        TabGrpTreeItemData* data = (TabGrpTreeItemData*)m_tree->GetItemData(child);
        if(data && data->GetFilepath() == label) {
            return child;
        }
        child = m_tree->GetNextSibling(child);
    }
    // Nope, so create it
    int folderImgID = clGetManager()->GetStdIcons()->GetMimeImageId(FileExtManager::TypeFolder);
    return m_tree->AppendItem(m_tree->GetRootItem(), label, folderImgID, folderImgID,
                                      new TabGrpTreeItemData(label, TGT_group));
}