pressing Enter when a wxButton has the focus

cai public.cai at gmail.com
Thu Oct 5 06:21:43 PDT 2006


Eugen Stoianovici wrote:
>post the code. if it's a simple dialog than it shouldn't be too big.

Here it is. I stripped down what's not of interest for this topic, to 
make the sample shorter.

Thanks,

Cristina.

------------------ search.h
#define NoName "< no name >"

#define SEARCH_TYPE                     1030
#define SEARCH_CONFIGUREBTN      1031
#define SEARCH_SEARCHBTN                1032
#define SEARCH_MATCHCASE         1033
#define SEARCH_REGEXP            1034
#define SEARCH_FIELD                    1035
#define SEARCH_FINDWHAT                 1036
#define SEARCH_CONFIGURETYPESBTN 1037

class SearchDlg: public wxDialog
{
    DECLARE_EVENT_TABLE();
public:
    SearchDlg(wxWindow* parent);
    ~SearchDlg();

    void enableSearchButton();
    void onChangeType();

    void onClose(wxCloseEvent& event);
    void onUpdateFindWhat(wxCommandEvent& event);
    void onSelectField(wxCommandEvent& event);
    void onConfigureTypes(wxCommandEvent& event);
    void onConfigure(wxCommandEvent& event);
    void onSearch(wxCommandEvent& event);
    void onKeyDown(wxKeyEvent& ev);

protected:
    wxComboBox* findWhatCombo;
    wxStaticText* typeStatic;
    wxButton* configureTypesBtn;
    wxChoice* fieldChoice;
    wxCheckBox* matchCaseCheckbox;
    wxCheckBox* regExpCheckbox;
    wxButton* configureBtn;
    wxButton* searchBtn;
};

------------------ search.cpp
#include "search.h"

#define __all__ _("__All__")



///////////////////////////////////////////////////////////////////
// SearchDlg
///////////////////////////////////////////////////////////////////


BEGIN_EVENT_TABLE(SearchDlg, wxDialog)
    EVT_CLOSE(SearchDlg::onClose)
    EVT_COMBOBOX(SEARCH_FINDWHAT, SearchDlg::onUpdateFindWhat)
    EVT_TEXT(SEARCH_FINDWHAT, SearchDlg::onUpdateFindWhat)
    EVT_CHOICE(SEARCH_FIELD, SearchDlg::onSelectField)
    EVT_BUTTON(SEARCH_CONFIGURETYPESBTN, SearchDlg::onConfigureTypes)
    EVT_BUTTON(SEARCH_CONFIGUREBTN, SearchDlg::onConfigure)
    EVT_BUTTON(SEARCH_SEARCHBTN, SearchDlg::onSearch)
    EVT_KEY_DOWN(SearchDlg::onKeyDown)
END_EVENT_TABLE()


SearchDlg::SearchDlg(wxWindow* parent):
     wxDialog(parent, -1, _("Search"), wxDefaultPosition, wxDefaultSize,
              wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxMINIMIZE_BOX|wxMAXIMIZE_BOX)
{
    SetIcon(theWxwApp->getAcpIcon());

    wxStaticText* findWhatStatic = new wxStaticText(this, -1, _("Find what"));
    findWhatCombo = new wxComboBox(this, SEARCH_FINDWHAT, "",
       wxDefaultPosition, wxDefaultSize, 0, NULL);

    typeStatic = new wxStaticText(this, -1, _("Types: All"));
    configureTypesBtn = new wxButton(this, SEARCH_CONFIGURETYPESBTN, 
_("Select types..."));

    wxStaticText* fieldStatic = new wxStaticText(this, -1, _("Fields"));
    fieldChoice = new wxChoice(this, SEARCH_FIELD, wxDefaultPosition, 
wxDefaultSize, 0, NULL);

    matchCaseCheckbox = new wxCheckBox(this, -1, _("Match case"));
    regExpCheckbox = new wxCheckBox(this, -1, _("Regular expression"));

    configureBtn = new wxButton(this, SEARCH_CONFIGUREBTN, _("Configure..."));
    wxButton* cancelBtn = new wxButton(this, wxID_CANCEL, _("Cancel"));
    searchBtn = new wxButton(this, SEARCH_SEARCHBTN, _("Search"));

    fieldChoice->Insert(__all__, 0);
    fieldChoice->SetSelection(0);

    matchCaseCheckbox->Hide();
    regExpCheckbox->Hide();

    searchBtn->Disable();
    searchBtn->SetDefault();

    // do the layout
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* findWhatSizer = new wxBoxSizer(wxHORIZONTAL);
    wxStaticBoxSizer* staticboxSizer = new wxStaticBoxSizer(
       wxVERTICAL, this, _("Options"));
    wxBoxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL);

    findWhatSizer->Add(findWhatStatic, 0, wxRIGHT, 16);
    findWhatSizer->Add(findWhatCombo, 1, 0, 0);

    staticboxSizer->Add(typeStatic, 0, wxEXPAND|wxLEFT, 10);
    staticboxSizer->AddSpacer(3);
    staticboxSizer->Add(configureTypesBtn, 1, 
wxEXPAND|wxBOTTOM|wxLEFT|wxRIGHT, 10);
    staticboxSizer->Add(fieldStatic, 0, wxEXPAND|wxLEFT, 10);
    staticboxSizer->Add(fieldChoice, 0, wxEXPAND|wxBOTTOM|wxLEFT|wxRIGHT, 10);
    staticboxSizer->Add(matchCaseCheckbox, 0, 
wxEXPAND|wxTOP|wxBOTTOM|wxLEFT, 10);
    staticboxSizer->Add(regExpCheckbox, 0, wxEXPAND|wxLEFT|wxBOTTOM, 10);

    buttonSizer->Add(configureBtn, 0, 0, 0);
    buttonSizer->Add(cancelBtn, 0, wxLEFT|wxRIGHT, 10);
    buttonSizer->Add(searchBtn, 0, 0, 0);

    mainSizer->Add(findWhatSizer, 0, wxEXPAND|wxLEFT|wxRIGHT|wxTOP, 20);
    mainSizer->Add(staticboxSizer, 0, wxEXPAND|wxALL, 20);
    mainSizer->Add(buttonSizer, 0,
       wxEXPAND|wxLEFT|wxRIGHT|wxBOTTOM|wxALIGN_CENTER_HORIZONTAL, 20);

    theWxwApp->restoreWindowPosition(this,"searchWindowPosition");

    SetSizer(mainSizer);
    mainSizer->SetSizeHints(this);
}


SearchDlg::~SearchDlg()
{
}


void SearchDlg::enableSearchButton()
{
}


void SearchDlg::onChangeType()
{
}


void SearchDlg::onClose(wxCloseEvent& event)
{
}


void SearchDlg::onUpdateFindWhat(wxCommandEvent& event)
{
}


void SearchDlg::onSelectField(wxCommandEvent& event)
{
}


void SearchDlg::onConfigureTypes(wxCommandEvent& event)
{
}


void SearchDlg::onConfigure(wxCommandEvent& event)
{
}


void SearchDlg::onSearch(wxCommandEvent& event)
{
}


void SearchDlg::onKeyDown(wxKeyEvent& ev)
{ // it never gets here!!!
/*
    if (ev.GetKeyCode() == WXK_RETURN) // Enter key
    {
       wxCommandEvent event;
       onSearch(event);
    }
*/
    ev.Skip();
}





More information about the wx-users mailing list