File: clChoice.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136,244 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (206 lines) | stat: -rw-r--r-- 5,366 bytes parent folder | download | duplicates (2)
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
#include "clChoice.h"

#include "wxCustomControls.hpp"

#if wxUSE_NATIVE_CHOICE
clChoice::clChoice() {}

clChoice::~clChoice() {}

bool clChoice::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
                      const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name)
{
    return wxChoice::Create(parent, id, pos, size, choices, style, validator, name);
}

clChoice::clChoice(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
                   const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name)
    : wxChoice(parent, id, pos, size, choices, style, validator, name)
{
}

void clChoice::SetText(const wxString& text) { SetStringSelection(text); }

#else

#include <unordered_map>
#include <wx/menu.h>
#include <wx/xrc/xmlres.h>

wxDEFINE_EVENT(wxEVT_CHOICE_MENU_SHOWING, wxNotifyEvent);

clChoice::clChoice() {}

clChoice::~clChoice() { Unbind(wxEVT_BUTTON, &clChoice::OnClick, this); }

bool clChoice::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
                      const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name)
{
    wxUnusedVar(style);
    m_choices.insert(m_choices.end(), choices.begin(), choices.end());
    wxString initialValue;
    if(!choices.IsEmpty()) {
        m_selection = 0;
        initialValue = m_choices[0];
    }

    if(!clButtonBase::Create(parent, id, initialValue, pos, size, 0, validator, name)) {
        return false;
    }
    SetHasDropDownMenu(true);
    Bind(wxEVT_BUTTON, &clChoice::OnClick, this);
    return true;
}

clChoice::clChoice(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
                   const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name)
{
    wxUnusedVar(style);
    Create(parent, id, pos, size, choices, 0, validator, name);
}

int clChoice::FindString(const wxString& s, bool caseSensitive) const
{
    for(size_t i = 0; i < m_choices.size(); ++i) {
        if(caseSensitive) {
            if(m_choices[i] == s) {
                return i;
            }
        } else {
            if(s.CmpNoCase(m_choices[i]) == 0) {
                return i;
            }
        }
    }
    return wxNOT_FOUND;
}

int clChoice::GetSelection() const { return m_selection; }

wxString clChoice::GetString(size_t index) const
{
    if(index >= m_choices.size()) {
        return "";
    }
    return m_choices[index];
}

void clChoice::SetSelection(size_t index)
{
    if(index >= m_choices.size()) {
        return;
    }
    m_selection = index;
    SetText(m_choices[m_selection]);
}

void clChoice::SetString(size_t index, const wxString& str)
{
    if(index >= m_choices.size()) {
        return;
    }
    m_choices[index] = str;
    // if we are updating the selected item, refresh the view
    if(index == (size_t)m_selection) {
        SetText(m_choices[m_selection]);
    }
}

wxString clChoice::GetStringSelection() const { return GetString((size_t)m_selection); }

void clChoice::SetStringSelection(const wxString& str)
{
    int where = FindString(str, true);
    if(where != wxNOT_FOUND) {
        SetSelection((size_t)where);
    }
}

void clChoice::OnClick(wxCommandEvent& event)
{
    wxUnusedVar(event);
    DoShowMenu();
}

void clChoice::DoShowMenu()
{
    // Check if the user allows us to disable the action
    wxNotifyEvent eventShowing(wxEVT_CHOICE_MENU_SHOWING);
    eventShowing.SetEventObject(this);
    GetEventHandler()->ProcessEvent(eventShowing);
    if(!eventShowing.IsAllowed()) {
        return;
    }

    wxMenu menu;
    int selectedIndex = wxNOT_FOUND;
    std::unordered_map<int, int> idToIndex;
    for(size_t i = 0; i < m_choices.size(); ++i) {
        int menuId = wxXmlResource::GetXRCID(m_choices[i]);
        const wxString& label = m_choices[i];
        wxMenuItem* item = menu.Append(menuId, label, label, wxITEM_CHECK);
        item->Check((int)i == m_selection);
        idToIndex.insert({ menuId, i });
        menu.Bind(
            wxEVT_MENU,
            [&](wxCommandEvent& e) {
                if(idToIndex.count(e.GetId())) {
                    selectedIndex = idToIndex[e.GetId()];
                }
            },
            menuId);
    }

    // Show the menu
    ShowMenu(menu);

    // Update the button label
    if(selectedIndex != wxNOT_FOUND) {
        SetSelection(selectedIndex);
        // Fire event
        wxCommandEvent evt(wxEVT_CHOICE);
        evt.SetEventObject(this);
        evt.SetInt(GetSelection());
        GetEventHandler()->AddPendingEvent(evt);
    }
}

void clChoice::Render(wxDC& dc)
{
#if !wxUSE_NATIVE_CHOICE
#if !wxUSE_NATIVE_BUTTON
    if(m_popupShown) {
        SetPressed();
    }
    clButtonBase::Render(dc);
#endif
#endif
}

int clChoice::Append(const wxString& str)
{
    m_choices.push_back(str);
    return (m_choices.size() - 1);
}

void clChoice::Append(const wxArrayString& items)
{
    m_choices.reserve(m_choices.size() + items.size());
    m_choices.insert(m_choices.end(), items.begin(), items.end());
}

void clChoice::Clear()
{
    m_choices.clear();
    SetText("");
    Refresh();
}

void clChoice::Set(const wxArrayString& items)
{
    m_choices.clear();
    Append(items);
    SetText("");
    Refresh();
}
#endif