diff --git a/wxmsg.cpp b/wxmsg.cpp index a80cd37..0f0cad0 100644 --- a/wxmsg.cpp +++ b/wxmsg.cpp @@ -41,46 +41,55 @@ using namespace std; -#define std2wx(x) wxString((wxChar*)x.c_str(), strlen(x.c_str())) +int numb(wxString input); +wxString label(wxString input); class wxmsgApp: public wxApp { public: - wxmsgApp(const wxString& message, vector& buttons, const wxString& default_label, int center, int nearmouse, int timeout); + wxmsgApp(); virtual bool OnInit(); private: wxString message; - vector buttons; + vector buttons; + int default_button; + int print; + int center; + int nearmouse; + int timeout; }; class wxmsgFrame: public wxFrame { public: - wxmsgFrame(const wxString& title, const wxString& message, vector& buttons); + wxmsgFrame(const wxString& title, const wxString& message, vector& buttons, int default_button, int print, int center, int nearmouse, int timeout); +protected: + void onClick(wxCommandEvent& event); private: wxPanel *m_panel; vector buttons; + vector str_buttons; + int print; }; -wxmsgApp::wxmsgApp(const wxString& _message, vector& _buttons, const wxString& default_label, int center, int nearmouse, int timeout) +DECLARE_APP(wxmsgApp); + +wxmsgApp::wxmsgApp()//const wxString& default_label, int center, int nearmouse, int timeout) : wxApp ( ) { - // TODO: handle other parameters - message = _message; - buttons = _buttons; + buttons = vector(); + message = _T(""); + default_button = -1; + print = 0; + center = 0; + nearmouse = 0; + timeout = 0; } -bool wxmsgApp::OnInit ( ) { - if ( !wxApp::OnInit() ) { - return false; - } - wxFrame *frame = new wxmsgFrame(_T("wxmsg"), message, buttons); - frame->Show(); - return true; -} - -wxmsgFrame::wxmsgFrame ( const wxString& title, const wxString& message, vector& str_buttons ) +wxmsgFrame::wxmsgFrame ( const wxString& title, const wxString& message, vector& _str_buttons, int default_button, int _print, int center, int nearmouse, int timeout ) : wxFrame(NULL, wxID_ANY, title, wxPoint(0, 50), wxDefaultSize, wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE | wxCLIP_CHILDREN | wxTAB_TRAVERSAL) { + str_buttons = _str_buttons; + print = _print; int i = 0; m_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN); @@ -93,8 +102,14 @@ buttons = vector(); for ( i = 0; i < str_buttons.size(); i++ ) { - buttons.push_back(new wxButton(m_panel, wxmsg_BUTTON_BASE_ID + i, std2wx(str_buttons[i]))); + buttons.push_back(new wxButton(m_panel, wxmsg_BUTTON_BASE_ID + i, label(str_buttons[i]))); + + if ( i == default_button ) + buttons[i]->SetDefault(); + sizerBtns->Add(buttons[i]); + + Connect(wxmsg_BUTTON_BASE_ID + i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxmsgFrame::onClick)); } m_panel->SetSizer(sizerMain); @@ -102,6 +117,13 @@ sizerMain->SetSizeHints(this); } +void wxmsgFrame::onClick( wxCommandEvent& event ) { + if ( print ) { + printf("%s\n", (const char*)(label(str_buttons[event.GetId() - wxmsg_BUTTON_BASE_ID]).mb_str(wxConvUTF8))); + } + exit(numb(str_buttons[event.GetId() - wxmsg_BUTTON_BASE_ID])); +} + const string usagemsg = "usage: -> man xmessage\n" @@ -144,20 +166,20 @@ #define license() cout << licensemsg #define version() cout << versionmsg -void read_buttons(string input, vector &buttons); +void read_buttons(wxString input, vector &buttons); -int main ( int argc, char** argv ) { +IMPLEMENT_APP(wxmsgApp); + +bool wxmsgApp::OnInit ( ) { + //if ( !wxApp::OnInit() ) { + // return false; + //} + int args_consumed = 1; int i; - vector buttons = vector(); - string default_label = ""; // string with size 0 == no label given - string message_file = ""; - string message = ""; - int print = 0; - int center = 0; - int nearmouse = 0; - int timeout = 0; + wxString default_label = _T(""); // string with size 0 == no label given + wxString message_file = _T(""); ifstream fd; string line; @@ -166,20 +188,20 @@ while ( found ) { found = 0; - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-h") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-h")) ) { usage(); exit(0); } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-l") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-l")) ) { license(); exit(0); } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-v") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-v")) ) { version(); exit(0); } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-buttons") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-buttons")) ) { args_consumed++; if ( argc - args_consumed < 1 ) { // error: buttons argument w/o button definitions @@ -191,7 +213,7 @@ found = 1; } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-default") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-default")) ) { args_consumed++; if ( argc - args_consumed < 1 ) { @@ -200,18 +222,18 @@ exit(1); } - default_label = argv[args_consumed]; // TODO: check that there exists such an label? + default_label = argv[args_consumed]; args_consumed++; found = 1; } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-print") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-print")) ) { args_consumed++; print = 1; found = 1; } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-center") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-center")) ) { args_consumed++; if ( nearmouse ) { // error: -center AND -nearmouse were given @@ -222,7 +244,7 @@ found = 1; } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-nearmouse") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-nearmouse")) ) { args_consumed++; if ( center ) { // error: -center AND -nearmouse were given @@ -233,14 +255,14 @@ found = 1; } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-timeout") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-timeout")) ) { args_consumed++; if ( argc - args_consumed < 1 ) { // error: no timeout value given usage(); exit(1); } - timeout = atoi(argv[args_consumed]); + timeout = wxAtoi(argv[args_consumed]); if ( timeout < 0 ) { // error: negative timeout usage(); @@ -252,10 +274,10 @@ } if ( buttons.empty() ) { - buttons.push_back("okay:0"); + buttons.push_back(_T("okay:0")); } - if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-file") ) { + if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-file")) ) { args_consumed++; @@ -279,59 +301,75 @@ } } - if ( argc - args_consumed == 0 && message_file == "" ) { + if ( argc - args_consumed == 0 && message_file == _T("") ) { // error: neither message nor file were given usage(); exit(1); } - if ( message_file == "" ) { + if ( message_file == _T("") ) { for ( i = args_consumed; i < argc; i++ ) { message.append(argv[i]); - message.append(" "); + message.append(_T(" ")); } } else { - fd.open(message_file.c_str(), ios::in); + fd.open((const char*)(message_file.mb_str(wxConvUTF8)), ios::in); + if ( !fd.is_open() ) { + cout << "Unable to open file: " << message_file << endl; + exit(-1); + } while ( !fd.bad() && !fd.eof() ) { getline(fd, line); - message.append(line); - message.append("\n"); + message.append(wxString(line.c_str(), wxConvUTF8)); + message.append(_("\n")); } fd.close(); } - wxmsgApp* app = new wxmsgApp(std2wx(message), buttons, std2wx(default_label), center, nearmouse, timeout); - - /* TODO: add wxWidgets interface here (instead of this cruft) */ - - cout << "message:" << endl << message << endl; - - while ( !buttons.empty() ) { - cout << "Button: " << buttons[buttons.size()-1] << endl; - buttons.pop_back(); - } - if ( default_label != "" ) - cout << "default_label: " << default_label << endl; - if ( message_file != "" ) - cout << "message_file: " << message_file << endl; - if ( print ) - cout << "-print" << endl; if ( center ) - cout << "-center" << endl; + cout << "-center option is not yet implemented" << endl; if ( nearmouse ) - cout << "-nearmouse" << endl; + cout << "-nearmouse option is not yet implemented" << endl; if ( timeout > 0 ) - cout << "timeout: " << timeout << endl; + cout << "-timeout option is not yet implemented" << endl; + + if ( default_label != _T("") ) + for ( i = 0; i < buttons.size(); i++ ) + if ( label(buttons[i]) == default_label ) + default_button = i; - exit(0); + wxFrame *frame = new wxmsgFrame(_T("wxmsg"), message, buttons, default_button, print, center, nearmouse, timeout); + frame->Show(); + + return true; } -void read_buttons ( string input, vector &buttons ) { - unsigned int commapos = string::npos; +int numb ( wxString input ) { + unsigned int lastpos = wxString::npos; + lastpos = input.find(':', true); + + if ( lastpos != wxString::npos ) + return wxAtoi(input.AfterLast(':')); + else + return 0; +} + +wxString label ( wxString input ) { + unsigned int lastpos = wxString::npos; + lastpos = input.find(':', true); + + if ( lastpos != wxString::npos ) + return input.BeforeLast(':'); + else + return input; +} + +void read_buttons ( wxString input, vector &buttons ) { + unsigned int commapos = wxString::npos; unsigned int lastpos = 0; - while ( (commapos = input.find(",", lastpos)) != string::npos ) { + while ( (commapos = input.find(_T(","), lastpos)) != wxString::npos ) { buttons.push_back(input.substr(lastpos, (commapos - lastpos))); lastpos = commapos+1; }