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
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// ctlComboBox.cpp - enhanced combobox control
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
// App headers
#include "ctl/ctlComboBox.h"
#include "db/pgConn.h"
#include "db/pgSet.h"
class StringClientData : public wxClientData
{
public:
StringClientData(const wxChar *c)
{
str = c;
}
wxString str;
};
int ctlComboBoxFix::Append(const wxString &item, const wxString &str)
{
return wxComboBox::Append(item, new StringClientData(str));
}
int ctlComboBoxFix::Append(const wxString &item, long l)
{
return wxComboBox::Append(item, (void *)l);
}
int ctlComboBoxFix::Append(const wxString &item, OID oid)
{
return wxComboBox::Append(item, (void *)oid);
}
int ctlComboBoxFix::FillLongKey(pgConn *conn, const wxChar *qry)
{
int cnt = 0;
pgSetIterator set(conn->ExecuteSet(qry));
while (set.RowsLeft())
{
long l = set.GetLong(0);
wxString txt = set.GetVal(1);
Append(txt, l);
cnt++;
}
return cnt;
}
int ctlComboBoxFix::FillOidKey(pgConn *conn, const wxChar *qry)
{
int cnt = 0;
pgSetIterator set(conn->ExecuteSet(qry));
while (set.RowsLeft())
{
OID oid = set.GetOid(0);
wxString txt = set.GetVal(1);
Append(txt, oid);
cnt++;
}
return cnt;
}
int ctlComboBoxFix::FillStringKey(pgConn *conn, const wxChar *qry)
{
int cnt = 0;
pgSetIterator set(conn->ExecuteSet(qry));
while (set.RowsLeft())
{
wxString str = set.GetVal(0);
wxString txt = set.GetVal(1);
Append(txt, str);
cnt++;
}
return cnt;
}
long ctlComboBoxFix::GetLongKey(int sel)
{
if (sel < 0)
sel = GetSelection();
return (long)wxItemContainer::GetClientData(sel);
}
OID ctlComboBoxFix::GetOIDKey(int sel)
{
if (sel < 0)
sel = GetSelection();
return (OID)wxItemContainer::GetClientData(sel);
}
wxString ctlComboBoxFix::GetStringKey(int sel)
{
if (sel < 0)
sel = GetSelection();
StringClientData *scd = (StringClientData *)wxItemContainer::GetClientObject(sel);
if (scd)
return scd->str;
return wxEmptyString;
}
ctlComboBoxFix::ctlComboBoxFix(wxWindow *wnd, int id, wxPoint pos, wxSize siz, long attr)
: wxComboBox(wnd, id, wxEmptyString, pos, siz, 0, NULL, attr)
{
}
bool ctlComboBoxFix::SetKey(long val)
{
unsigned int i;
for (i = 0 ; i < GetCount() ; i++)
{
if (GetLongKey(i) == val)
{
SetSelection(i);
return true;
}
}
SetSelection(wxNOT_FOUND);
return false;
}
bool ctlComboBoxFix::SetKey(OID val)
{
unsigned int i;
for (i = 0 ; i < GetCount() ; i++)
{
if (GetOIDKey(i) == val)
{
SetSelection(i);
return true;
}
}
SetSelection(wxNOT_FOUND);
return false;
}
bool ctlComboBoxFix::SetKey(const wxString &val)
{
unsigned int i;
for (i = 0 ; i < GetCount() ; i++)
{
if (GetStringKey(i) == val)
{
SetSelection(i);
return true;
}
}
SetSelection(wxNOT_FOUND);
return false;
}
////////////////////////////////////////////
ctlComboBox::ctlComboBox(wxWindow *wnd, int id, wxPoint pos, wxSize siz, long attr)
: ctlComboBoxFix(wnd, id, pos, siz, attr)
{
#ifdef __WXGTK__
SetEditable(false);
#endif
}
int ctlComboBox::GuessSelection(wxCommandEvent &ev)
{
if (ev.GetEventType() != wxEVT_COMMAND_TEXT_UPDATED)
return GetGuessedSelection();
wxString str = wxComboBox::GetValue();
if (str.Length())
{
long pos = GetInsertionPoint();
long sel, count = GetCount();
int len = str.Length();
for (sel = 0 ; sel < count ; sel++)
{
if (str == GetString(sel).Left(len))
{
SetSelection(sel);
wxString current = GetString(sel);
SetSelection(pos, current.Length());
return sel;
}
}
}
return -1;
}
int ctlComboBox::GetGuessedSelection() const
{
int sel = GetCurrentSelection();
if (sel < 0)
sel = FindString(GetValue());
return sel;
}
int ctlComboBox::GetSelection() const
{
int sel = 0;
#ifdef __WXMSW__
sel = GetCurrentSelection();
if (sel < 0)
#endif
sel = FindString(GetValue());
return sel;
}
wxString ctlComboBox::GetGuessedStringSelection() const
{
int sel = GetGuessedSelection();
if (sel < 0)
return wxEmptyString;
else
return GetString(sel);
}
|