summaryrefslogtreecommitdiff
path: root/shared/folder_history_box.cpp
blob: f787e2cba56e4aa1a35c59e4aac699e4f0b99ac6 (plain)
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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************

#include "folder_history_box.h"
#include "resolve_path.h"
#include <list>

using namespace zen;

FolderHistoryBox::FolderHistoryBox(wxWindow* parent,
                                   wxWindowID id,
                                   const wxString& value,
                                   const wxPoint& pos,
                                   const wxSize& size,
                                   int n,
                                   const wxString choices[],
                                   long style,
                                   const wxValidator& validator,
                                   const wxString& name) :
    wxComboBox(parent, id, value, pos, size, n, choices, style, validator, name)
#if wxCHECK_VERSION(2, 9, 1)
    , dropDownShown(false)
#endif
{
    //#####################################
    /*##*/ SetMinSize(wxSize(150, -1)); //## workaround yet another wxWidgets bug: default minimum size is much too large for a wxComboBox
    //#####################################

    //register key event to enable item deletion
    Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(FolderHistoryBox::OnKeyEvent), NULL, this);

    //refresh history list on mouse click
    Connect(wxEVT_LEFT_DOWN, wxEventHandler(FolderHistoryBox::OnUpdateList), NULL, this);

    Connect(wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler(FolderHistoryBox::OnSelection), NULL, this);

#if wxCHECK_VERSION(2, 9, 1)
    Connect(wxEVT_COMMAND_COMBOBOX_DROPDOWN, wxCommandEventHandler(FolderHistoryBox::OnShowDropDown), NULL, this);
    Connect(wxEVT_COMMAND_COMBOBOX_CLOSEUP, wxCommandEventHandler(FolderHistoryBox::OnHideDropDown), NULL, this);
#endif
}


#if wxCHECK_VERSION(2, 9, 1)
void FolderHistoryBox::OnShowDropDown(wxCommandEvent& event)
{
    dropDownShown = true;
    event.Skip();
}


void FolderHistoryBox::OnHideDropDown(wxCommandEvent& event)
{
    dropDownShown = false;
    event.Skip();
}
#endif


void FolderHistoryBox::update()
{
    std::list<Zstring> dirList;

    //add some aliases to allow user changing to volume name and back, if possible
#ifdef FFS_WIN
    const Zstring activePath = toZ(GetValue());
    std::vector<Zstring> aliases = getDirectoryAliases(activePath);
    dirList.insert(dirList.end(), aliases.begin(), aliases.end());
#endif

    if (sharedHistory_.get())
    {
        auto tmp = sharedHistory_->getList();
        //std::sort(tmp.begin(), tmp.end(), LessFilename());

        if (!dirList.empty() && !tmp.empty())
            dirList.push_back(FolderHistory::lineSeparator());

        dirList.insert(dirList.end(), tmp.begin(), tmp.end());
    }
    //###########################################################################################


    //it may be a little lame to update the list on each mouse-button click, but it should be working and we dont't have to manipulate wxComboBox internals
    const wxString oldVal = this->GetValue();

    Clear();
    std::for_each(dirList.begin(), dirList.end(),
    [&](const Zstring& dir) { this->Append(toWx(dir)); });

    this->SetSelection(wxNOT_FOUND); //don't select anything
    this->SetValue(oldVal);          //but preserve main text!
}


void FolderHistoryBox::OnSelection(wxCommandEvent& event)
{
    event.Skip();
}


void FolderHistoryBox::OnKeyEvent(wxKeyEvent& event)
{
    const int keyCode = event.GetKeyCode();
    if (keyCode == WXK_DELETE || keyCode == WXK_NUMPAD_DELETE)
    {
        //try to delete the currently selected config history item
        int pos = this->GetCurrentSelection();
        if (0 <= pos && pos < static_cast<int>(this->GetCount()) &&
#if wxCHECK_VERSION(2, 9, 1)
            dropDownShown)
#else
            //what a mess...:
            (GetValue() != GetString(pos) || //avoid problems when a character shall be deleted instead of list item
             GetValue() == wxEmptyString)) //exception: always allow removing empty entry
#endif
        {
            //save old (selected) value: deletion seems to have influence on this
            const wxString currentVal = this->GetValue();
            //this->SetSelection(wxNOT_FOUND);

            //delete selected row
            if (sharedHistory_.get())
                sharedHistory_->delItem(toZ(GetString(pos)));
            SetString(pos, wxString()); //in contrast to Delete(), this one does not kill the drop-down list and gives a nice visual feedback!
            //Delete(pos);

            //(re-)set value
            this->SetValue(currentVal);

            //eat up key event
            return;
        }
    }
    event.Skip();
}
bgstack15