📄 listbox.cpp
字号:
///////////////////////////////////////////////////////////////////////////////// Name: src/mac/carbon/listbox.cpp// Purpose: wxListBox// Author: Stefan Csomor// Modified by:// Created: 1998-01-01// RCS-ID: $Id: listbox.cpp,v 1.135 2006/12/08 15:03:53 SC Exp $// Copyright: (c) Stefan Csomor// Licence: wxWindows licence///////////////////////////////////////////////////////////////////////////////#include "wx/wxprec.h"#if wxUSE_LISTBOX#include "wx/listbox.h"#ifndef WX_PRECOMP #include "wx/log.h" #include "wx/intl.h" #include "wx/utils.h" #include "wx/settings.h" #include "wx/arrstr.h" #include "wx/dcclient.h"#endifIMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)BEGIN_EVENT_TABLE(wxListBox, wxControl)END_EVENT_TABLE()#include "wx/mac/uma.h"// ============================================================================// list box control implementation// ============================================================================wxListBox::wxListBox(){}bool wxListBox::Create( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name ){ wxCArrayString chs(choices); return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), style, validator, name );}wxMacListControl* wxListBox::GetPeer() const{ wxMacDataBrowserListControl *lb = wxDynamicCast(m_peer,wxMacDataBrowserListControl); return lb ? wx_static_cast(wxMacListControl*,lb) : 0 ;}bool wxListBox::Create( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int n, const wxString choices[], long style, const wxValidator& validator, const wxString& name ){ m_macIsUserPane = false; wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), wxT("only a single listbox selection mode can be specified") ); if ( !wxListBoxBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) ) return false; wxMacDataBrowserListControl* control = new wxMacDataBrowserListControl( this, pos, size, style ); control->SetClientDataType( m_clientDataItemsType ); m_peer = control; MacPostControlCreate( pos, size ); InsertItems( n, choices, 0 ); // Needed because it is a wxControlWithItems SetInitialSize( size ); return true;}wxListBox::~wxListBox(){ FreeData(); m_peer->SetReference( 0 );}void wxListBox::FreeData(){ GetPeer()->MacClear();}void wxListBox::DoSetFirstItem(int n){ GetPeer()->MacScrollTo( n );}void wxListBox::EnsureVisible(int n){ GetPeer()->MacScrollTo( n );}void wxListBox::Delete(unsigned int n){ wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") ); GetPeer()->MacDelete( n );}int wxListBox::DoAppend(const wxString& item){ InvalidateBestSize(); return GetPeer()->MacAppend( item );}void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData){ Clear(); unsigned int n = choices.GetCount(); for ( size_t i = 0; i < n; ++i ) { if ( clientData ) { Append( choices[i], clientData[i] ); } else Append( choices[i] ); }}int wxListBox::FindString(const wxString& s, bool bCase) const{ for ( size_t i = 0; i < GetCount(); ++ i ) { if (s.IsSameAs( GetString( i ), bCase) ) return (int)i; } return wxNOT_FOUND;}void wxListBox::Clear(){ FreeData();}void wxListBox::DoSetSelection(int n, bool select){ wxCHECK_RET( n == wxNOT_FOUND || IsValid(n), wxT("invalid index in wxListBox::SetSelection") ); if ( n == wxNOT_FOUND ) GetPeer()->MacDeselectAll(); else GetPeer()->MacSetSelection( n, select, HasMultipleSelection() );}bool wxListBox::IsSelected(int n) const{ wxCHECK_MSG( IsValid(n), false, wxT("invalid index in wxListBox::Selected") ); return GetPeer()->MacIsSelected( n );}void *wxListBox::DoGetItemClientData(unsigned int n) const{ wxCHECK_MSG( IsValid(n), NULL, wxT("invalid index in wxListBox::GetClientData")); return GetPeer()->MacGetClientData( n );}wxClientData *wxListBox::DoGetItemClientObject(unsigned int n) const{ return (wxClientData*)DoGetItemClientData( n );}void wxListBox::DoSetItemClientData(unsigned int n, void *clientData){ wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetClientData") ); GetPeer()->MacSetClientData( n , clientData);}void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData){ DoSetItemClientData(n, clientData);}// Return number of selections and an array of selected integersint wxListBox::GetSelections(wxArrayInt& aSelections) const{ return GetPeer()->MacGetSelections( aSelections );}// Get single selection, for single choice list itemsint wxListBox::GetSelection() const{ return GetPeer()->MacGetSelection();}// Find string for positionwxString wxListBox::GetString(unsigned int n) const{ wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("invalid index in wxListBox::GetString") ); return GetPeer()->MacGetString(n);}void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos){ wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") ); InvalidateBestSize(); GetPeer()->MacInsert( pos, items );}void wxListBox::SetString(unsigned int n, const wxString& s){ GetPeer()->MacSetString( n, s );}wxSize wxListBox::DoGetBestSize() const{ int lbWidth = 100; // some defaults int lbHeight = 110; int wLine; {#if wxMAC_USE_CORE_GRAPHICS wxClientDC dc(const_cast<wxListBox*>(this));#else wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef)MacGetTopLevelWindowRef() ) ); // TODO: clean this up if ( m_font.Ok() ) { ::TextFont( m_font.MacGetFontNum() ); ::TextSize( m_font.MacGetFontSize() ); ::TextFace( m_font.MacGetFontStyle() ); } else { ::TextFont( kFontIDMonaco ); ::TextSize( 9 ); ::TextFace( 0 ); }#endif // Find the widest line for (unsigned int i = 0; i < GetCount(); i++) { wxString str( GetString( i ) );#if wxMAC_USE_CORE_GRAPHICS wxCoord width, height ; dc.GetTextExtent( str , &width, &height); wLine = width ;#else#if wxUSE_UNICODE Point bounds = {0, 0}; SInt16 baseline; // NB: what if m_font.Ok() == false ??? ::GetThemeTextDimensions( wxMacCFStringHolder( str, m_font.GetEncoding() ), kThemeCurrentPortFont, kThemeStateActive, false, &bounds, &baseline ); wLine = bounds.h;#else wLine = ::TextWidth( str.c_str(), 0, str.length() );#endif lbWidth = wxMax( lbWidth, wLine );#endif } // Add room for the scrollbar lbWidth += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X ); // And just a bit more int cy = 12;#if wxMAC_USE_CORE_GRAPHICS wxCoord width, height ; dc.GetTextExtent( wxT("X") , &width, &height); int cx = width ;#else int cx = ::TextWidth( "X", 0, 1 );#endif lbWidth += cx; // don't make the listbox too tall (limit height to around 10 items) // but don't make it too small neither lbHeight = wxMax( (cy + 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 ); } return wxSize( lbWidth, lbHeight );}unsigned int wxListBox::GetCount() const{ return GetPeer()->MacGetCount();}void wxListBox::Refresh(bool eraseBack, const wxRect *rect){ wxControl::Refresh( eraseBack, rect );}// Some custom controls depend on this/* static */ wxVisualAttributeswxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)){ wxVisualAttributes attr; attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ); attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ); return attr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -