📄 odcombo.cpp
字号:
}
void wxPGComboPopup::OnComboDoubleClick()
{
}
void wxPGComboPopup::SetStringValue( const wxString& WXUNUSED(value) )
{
}
bool wxPGComboPopup::LazyCreate()
{
return false;
}
void wxPGComboPopup::Dismiss()
{
m_combo->HidePopup();
}
// ----------------------------------------------------------------------------
// wxPGVListBoxComboPopup is a wxVListBox customized to act as a popup control
//
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(wxPGVListBoxComboPopup, wxVListBox)
EVT_MOTION(wxPGVListBoxComboPopup::OnMouseMove)
EVT_KEY_DOWN(wxPGVListBoxComboPopup::OnKey)
EVT_LEFT_UP(wxPGVListBoxComboPopup::OnLeftClick)
END_EVENT_TABLE()
wxPGVListBoxComboPopup::wxPGVListBoxComboPopup(wxPGComboControl* combo)
: wxVListBox(), wxPGComboPopup(combo)
{
//m_callback = callback;
m_widestWidth = 0;
m_avgCharWidth = 0;
m_baseImageWidth = 0;
m_itemHeight = 0;
m_value = -1;
m_itemHover = -1;
m_clientDataItemsType = wxClientData_None;
}
bool wxPGVListBoxComboPopup::Create(wxWindow* parent)
{
if ( !wxVListBox::Create(parent,
wxID_ANY,
wxDefaultPosition,
wxDefaultSize,
wxBORDER_SIMPLE | wxLB_INT_HEIGHT | wxWANTS_CHARS) )
return false;
m_font = m_combo->GetFont();
wxVListBox::SetItemCount(m_strings.GetCount());
// TODO: Move this to SetFont
m_itemHeight = GetCharHeight() + 0;
return true;
}
wxPGVListBoxComboPopup::~wxPGVListBoxComboPopup()
{
Clear();
}
bool wxPGVListBoxComboPopup::LazyCreate()
{
// NB: There is a bug with wxVListBox that can be avoided by creating
// it later (bug causes empty space to be shown if initial selection
// is at the end of a list longer than the control can show at once).
return true;
}
// paint the control itself
void wxPGVListBoxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
{
if ( !(m_combo->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT) )
{
m_combo->DrawFocusBackground(dc,rect,0);
if ( m_combo->OnDrawListItem(dc,rect,m_value,wxPGCC_PAINTING_CONTROL) )
return;
}
wxPGComboPopup::PaintComboControl(dc,rect);
}
void wxPGVListBoxComboPopup::OnDrawItem( wxDC& dc, const wxRect& rect, size_t n) const
{
dc.SetFont(m_font);
bool isHilited = wxVListBox::GetSelection() == (int) n;
// Set correct text colour for selected items
// (must always set the correct colour - atleast GTK may have lost it
// in between calls).
if ( isHilited )
dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
else
dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
if ( !m_combo->OnDrawListItem(dc,rect,(int)n,0) )
dc.DrawText( GetString(n), rect.x + 2, rect.y );
}
wxCoord wxPGVListBoxComboPopup::OnMeasureItem(size_t n) const
{
int itemHeight = m_combo->OnMeasureListItem(n);
if ( itemHeight < 0 )
itemHeight = m_itemHeight;
return itemHeight;
}
void wxPGVListBoxComboPopup::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
{
// we need to render selected and current items differently
if ( IsCurrent(n) )
{
m_combo->DrawFocusBackground( dc, rect, wxCONTROL_ISSUBMENU|wxCONTROL_SELECTED );
}
//else: do nothing for the normal items
}
void wxPGVListBoxComboPopup::DismissWithEvent()
{
int selection = wxVListBox::GetSelection();
Dismiss();
if ( selection != wxNOT_FOUND )
m_stringValue = m_strings[selection];
else
m_stringValue = wxEmptyString;
if ( m_stringValue != m_combo->GetValue() )
m_combo->SetValue(m_stringValue);
m_value = selection;
SendComboBoxEvent(selection);
}
void wxPGVListBoxComboPopup::SendComboBoxEvent( int selection )
{
wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId());
evt.SetEventObject(m_combo);
evt.SetInt(selection);
// Set client data, if any
if ( selection >= 0 && (int)m_clientDatas.GetCount() > selection )
{
void* clientData = m_clientDatas[selection];
if ( m_clientDataItemsType == wxClientData_Object )
evt.SetClientObject((wxClientData*)clientData);
else
evt.SetClientData(clientData);
}
m_combo->GetEventHandler()->AddPendingEvent(evt);
}
// returns true if key was consumed
bool wxPGVListBoxComboPopup::HandleKey( int keycode, bool saturate )
{
int value = m_value;
int itemCount = GetCount();
if ( keycode == WXK_DOWN || keycode == WXK_RIGHT )
{
value++;
}
else if ( keycode == WXK_UP || keycode == WXK_LEFT )
{
value--;
}
else if ( keycode == WXK_PAGEDOWN )
{
value+=10;
}
else if ( keycode == WXK_PAGEUP )
{
value-=10;
}
/*
else if ( keycode == WXK_END )
{
value = itemCount-1;
}
else if ( keycode == WXK_HOME )
{
value = 0;
}
*/
else
return false;
if ( saturate )
{
if ( value >= itemCount )
value = itemCount - 1;
else if ( value < 0 )
value = 0;
}
else
{
if ( value >= itemCount )
value -= itemCount;
else if ( value < 0 )
value += itemCount;
}
if ( value == m_value )
// Even if value was same, don't skip the event
// (good for consistency)
return true;
m_value = value;
wxString valStr;
if ( value >= 0 )
m_combo->SetValue(m_strings[value]);
SendComboBoxEvent(m_value);
return true;
}
void wxPGVListBoxComboPopup::OnComboDoubleClick()
{
// Cycle on dclick (disable saturation to allow true cycling).
if ( !::wxGetKeyState(WXK_SHIFT) )
HandleKey(WXK_DOWN,false);
else
HandleKey(WXK_UP,false);
}
void wxPGVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
{
// Saturated key movement on
if ( !HandleKey(event.GetKeyCode(),true) )
event.Skip();
}
void wxPGVListBoxComboPopup::OnPopup()
{
// *must* set value after size is set (this is because of a vlbox bug)
wxVListBox::SetSelection(m_value);
}
void wxPGVListBoxComboPopup::OnMouseMove(wxMouseEvent& event)
{
// Move selection to cursor if it is inside the popup
int itemHere = GetItemAtPosition(event.GetPosition());
if ( itemHere >= 0 )
wxVListBox::SetSelection(itemHere);
event.Skip();
}
void wxPGVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event))
{
DismissWithEvent();
}
void wxPGVListBoxComboPopup::OnKey(wxKeyEvent& event)
{
// Select item if ENTER is pressed
if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER )
{
DismissWithEvent();
}
// Hide popup if ESC is pressed
else if ( event.GetKeyCode() == WXK_ESCAPE )
Dismiss();
else
event.Skip();
}
void wxPGVListBoxComboPopup::CheckWidth( int pos )
{
wxCoord x = m_combo->OnMeasureListItemWidth(pos);
if ( x < 0 )
{
if ( !m_font.Ok() )
m_font = m_combo->GetFont();
wxCoord y;
m_combo->GetTextExtent(m_strings[pos], &x, &y, 0, 0, &m_font);
x += 4;
}
if ( m_widestWidth < x )
{
m_widestWidth = x;
}
}
void wxPGVListBoxComboPopup::Insert( const wxString& item, int pos )
{
// Need to change selection?
wxString strValue;
if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) &&
m_combo->GetValue() == item )
m_value = pos;
else if ( m_value >= pos )
m_value++;
m_strings.Insert(item,pos);
if ( IsCreated() )
wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
// Calculate width
CheckWidth(pos);
}
int wxPGVListBoxComboPopup::Append(const wxString& item)
{
int pos = (int)m_strings.GetCount();
if ( m_combo->GetWindowStyle() & wxCB_SORT )
{
// Find position
// TODO: Could be optimized with binary search
wxArrayString strings = m_strings;
unsigned int i;
for ( i=0; i<strings.GetCount(); i++ )
{
if ( item.Cmp(strings.Item(i)) < 0 )
{
pos = (int)i;
break;
}
}
}
Insert(item,pos);
return pos;
}
void wxPGVListBoxComboPopup::Clear()
{
wxASSERT(m_combo);
m_strings.Empty();
ClearClientDatas();
if ( IsCreated() )
wxVListBox::SetItemCount(0);
}
void wxPGVListBoxComboPopup::ClearClientDatas()
{
if ( m_clientDataItemsType == wxClientData_Object )
{
size_t i;
for ( i=0; i<m_clientDatas.GetCount(); i++ )
delete (wxClientData*) m_clientDatas[i];
}
m_clientDatas.Empty();
}
void wxPGVListBoxComboPopup::SetItemClientData( wxODCIndex n,
void* clientData,
wxClientDataType clientDataItemsType )
{
// It should be sufficient to update this variable only here
m_clientDataItemsType = clientDataItemsType;
m_clientDatas.SetCount(n+1,NULL);
m_clientDatas[n] = clientData;
}
void* wxPGVListBoxComboPopup::GetItemClientData(wxODCIndex n) const
{
if ( ((wxODCIndex)m_clientDatas.GetCount()) > n )
return m_clientDatas[n];
return NULL;
}
void wxPGVListBoxComboPopup::Delete( wxODCIndex item )
{
// Remove client data, if set
if ( m_clientDatas.GetCount() )
{
if ( m_clientDataItemsType == wxClientData_Object )
delete (wxClientData*) m_clientDatas[item];
m_clientDatas.RemoveAt(item);
}
m_strings.RemoveAt(item);
int sel = GetSelection();
if ( IsCreated() )
wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
if ( (int)item < sel )
SetSelection(sel-1);
else if ( (int)item == sel )
SetSelection(wxNOT_FOUND);
}
int wxPGVListBoxComboPopup::FindString(const wxString& s) const
{
return m_strings.Index(s);
}
wxODCCount wxPGVListBoxComboPopup::GetCount() const
{
return m_strings.GetCount();
}
wxString wxPGVListBoxComboPopup::GetString( int item ) const
{
return m_strings[item];
}
void wxPGVListBoxComboPopup::SetString( int item, const wxString& str )
{
m_strings[item] = str;
}
wxString wxPGVListBoxComboPopup::GetStringValue() const
{
return m_stringValue;
}
void wxPGVListBoxComboPopup::SetSelection( int item )
{
// This seems to be necessary (2.5.3 w/ MingW atleast)
if ( item < -1 || item >= (int)m_strings.GetCount() )
item = -1;
m_value = item;
if ( item >= 0 )
m_stringValue = m_strings[item];
else
m_stringValue = wxEmptyString;
if ( IsCreated() )
wxVListBox::SetSelection(item);
}
int wxPGVListBoxComboPopup::GetSelection() const
{
return m_value;
}
void wxPGVListBoxComboPopup::SetStringValue( const wxString& value )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -