summaryrefslogtreecommitdiff
path: root/ui/folder_history_box.h
blob: 695ee930b472e78b28f74e56b649db98f04c0a9d (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
// **************************************************************************
// * 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) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#ifndef CUSTOMCOMBOBOX_H_INCLUDED
#define CUSTOMCOMBOBOX_H_INCLUDED

#include <wx/combobox.h>
#include <memory>
#include <zen/zstring.h>
#include <zen/stl_tools.h>
#include <zen/utf.h>

//combobox with history function + functionality to delete items (DEL)


class FolderHistory
{
public:
    FolderHistory() : maxSize_(0) {}

    FolderHistory(const std::vector<Zstring>& dirnames, size_t maxSize) :
        maxSize_(maxSize),
        dirnames_(dirnames)
    {
        if (dirnames_.size() > maxSize_) //keep maximal size of history list
            dirnames_.resize(maxSize_);
    }

    const std::vector<Zstring>& getList() const { return dirnames_; }

    static const wxString separationLine() { return L"---------------------------------------------------------------------------------------------------------------"; }

    void addItem(const Zstring& dirname)
    {
        if (dirname.empty() || dirname == zen::utfCvrtTo<Zstring>(separationLine()))
            return;

        Zstring nameTmp = dirname;
        zen::trim(nameTmp);

        //insert new folder or put it to the front if already existing
        auto iter = std::find_if(dirnames_.begin(), dirnames_.end(),
        [&](const Zstring& entry) { return ::EqualFilename()(entry, nameTmp); });

        if (iter != dirnames_.end())
            dirnames_.erase(iter);
        dirnames_.insert(dirnames_.begin(), nameTmp);

        if (dirnames_.size() > maxSize_) //keep maximal size of history list
            dirnames_.resize(maxSize_);
    }

    void delItem(const Zstring& dirname) { zen::vector_remove_if(dirnames_, [&](const Zstring& entry) { return ::EqualFilename()(entry, dirname); }); }

private:

    size_t maxSize_;
    std::vector<Zstring> dirnames_;
};


class FolderHistoryBox : public wxComboBox
{
public:
    FolderHistoryBox(wxWindow* parent,
                     wxWindowID id,
                     const wxString& value = wxEmptyString,
                     const wxPoint& pos = wxDefaultPosition,
                     const wxSize& size = wxDefaultSize,
                     int n = 0,
                     const wxString choices[] = nullptr,
                     long style = 0,
                     const wxValidator& validator = wxDefaultValidator,
                     const wxString& name = wxComboBoxNameStr);

    void init(const std::shared_ptr<FolderHistory>& sharedHistory) { sharedHistory_ = sharedHistory; }

    void setValue(const wxString& dirname)
    {
        setValueAndUpdateList(dirname); //required for setting value correctly + Linux to ensure the dropdown is shown as being populated
    }

    // GetValue

private:
    void OnKeyEvent(wxKeyEvent& event);
    void OnMouseWheel(wxMouseEvent& event);
    void OnSelection(wxCommandEvent& event);
    void OnValidateSelection(wxCommandEvent& event);
    void OnMouseClick(wxEvent& event) { setValueAndUpdateList(GetValue()); event.Skip(); }

    void setValueAndUpdateList(const wxString& dirname);

#if wxCHECK_VERSION(2, 9, 1)
    void OnShowDropDown(wxCommandEvent& event);
    void OnHideDropDown(wxCommandEvent& event);

    bool dropDownShown;
#endif

    std::shared_ptr<FolderHistory> sharedHistory_;
};


#endif // CUSTOMCOMBOBOX_H_INCLUDED
bgstack15