summaryrefslogtreecommitdiff
path: root/shared/customComboBox.cpp
blob: 722683a59f71da108ef13f530d4e1b82271a9203 (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
// **************************************************************************
// * 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-2010 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#include "customComboBox.h"


CustomComboBox::CustomComboBox(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)
{
    //register key event to enable item deletion
    this->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(CustomComboBox::OnKeyEvent), NULL, this );
}


void CustomComboBox::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
        const int selectedItem = this->GetCurrentSelection();
        if (0 <= selectedItem && selectedItem < static_cast<int>(this->GetCount()) &&
                (GetValue() != GetString(selectedItem) || //avoid problems when letter shall be deleted instead of list item
                 GetValue() == wxEmptyString) //exception: always allow removing empty entry
           )
        {
            //save old (selected) value: deletion seems to have influence on this
            const wxString currentVal = this->GetValue();
            this->SetSelection(wxNOT_FOUND);

            //delete selected row
            this->Delete(selectedItem);

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

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


void CustomComboBox::addPairToFolderHistory(const wxString& newFolder, unsigned int maxHistSize)
{
    //don't add empty directories
    if (newFolder.empty())
        return;

    const wxString oldVal = this->GetValue();

    //insert new folder or put it to the front if already existing
#ifdef FFS_WIN //don't respect case in windows build
    const int pos = FindString(newFolder, false);
#elif defined FFS_LINUX
    const int pos = FindString(newFolder, true);
#endif
    if (pos != wxNOT_FOUND)
        this->Delete(pos);

    this->Insert(newFolder, 0);

    //keep maximal size of history list
    if (this->GetCount() > maxHistSize)
        this->Delete(maxHistSize);

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

bgstack15