How to manage combo box drop-down size?

Carsten A. Arnholm arnholm at offline.no
Sat Jan 26 11:00:19 PST 2008


Vadim Zeitlin wrote:
> On Sat, 26 Jan 2008 13:18:17 +0100 "Carsten A. Arnholm"
> <arnholm at offline.no> wrote:
>
>> I have placed the complete sample referred to in that thread, with
>> the class "wxChoiceWx" included at
>> <http://arnholm.org/wx/wxChoiceList_20080126.zip>
>>
>> You can use this under the wxWidgets license. The sample is a
>> Code::Blocks projects set up to use VC2005 Express compiler on
>> windows and GCC on
>> Linux Kubuntu. The sample has been tested for both.
>>
>> As mentioned before, this could be considered for further
>> improvements and contribution to wxWidgets.
>
> After looking at the sources one more time I see that we already have
> wxComboCtrl::SetPopupMaxHeight() so I wonder if you couldn't just
> have used wxOwnerDrawnComboBox instead of deriving your own class.
> Have you tried
> doing it and, if so, what was the problem with doing this?

Hi Vadim,

Oops, that looks like better alternative, I agree. Why did I trust the 
initial assertions that nothing like that was available? :-) I have tried it 
in wxCB_READONLY mode now, and it is far simpler than my derivation above. 
There appears to be some minor issues only

1. I would like to specify the dropdown list heigth in number of items 
instead of pixels. See SetPopupMaxItems below

2. Mouse wheel scrolling appears a little odd. I.e. unlike a wxComboBox or 
wxChoice, you cannot use mouse wheel scrolling to select another alternative 
when the drop down menu is not open.

To deal with these, I tried making another derivation:
class wxSimpleComboBox : public wxOwnerDrawnComboBox

BEGIN_EVENT_TABLE(wxSimpleComboBox, wxOwnerDrawnComboBox)
   EVT_MOUSEWHEEL(wxSimpleComboBox::OnMouseWheel)
END_EVENT_TABLE()

wxSimpleComboBox::wxSimpleComboBox(wxWindow *parent,
                                   wxWindowID id,
                                   const wxString& value,
                                   const wxPoint& pos,
                                   const wxSize& size,
                                   int n ,const wxString choices[],
                                   long style,
                                   const wxValidator& validator,
                                   const wxString& name)
: 
wxOwnerDrawnComboBox(parent,id,value,pos,size,n,choices,style,validator,name)
{
   SetPopupMaxItems(10);
}

wxSimpleComboBox::~wxSimpleComboBox()
{
   //dtor
}


void wxSimpleComboBox::SetPopupMaxItems(int nitems)
{
   // the following calculation of drop height in pixels
   // is just a result of trying, but it seems to work ok
   // on wxMSW and wxGTK. There might be better ways to do it
   int w,h;
   wxClientDC dc(this);
   dc.SetFont(GetFont());
   dc.GetTextExtent(wxT("X"),&w,&h);
   int drop_height = h*nitems+3;

   // call the function that takes the height in pixels
   SetPopupMaxHeight(drop_height);
}

void wxSimpleComboBox::OnMouseWheel(wxMouseEvent& event)
{
   // scroll in drop down list using mouse wheel
   int rot = event.GetWheelRotation()/event.GetWheelDelta();
   int lines = rot*1;// event.GetLinesPerAction();

   SetSelection(GetSelection()-lines);
}


This appears almost perfect, scrolling works. Setting the dropdown soze 
works. The only thing I have found is that on Windows you cannot cancel the 
popup menu with the ESC key when mouse scrolling has been used, without at 
the same time selecting the currently focused value, i.e. ESC works like 
pressing Return key. On Linux, no keyboard key presses are have any effect 
when the popup is open, none of WXK_UP/WXK_DOWN, WXK_ESCAPE, WXK_RETURN 
work. That is something one would like to have.

So I agree this is much simpler, and a good alternative to wxChoice, but 
missing a few things related to key handling to be a complete alternative.

Regards
Carsten A. Arnholm
http://arnholm.org/
N59.776 E10.457 








More information about the wx-users mailing list