First working version of the wxWidgets interface.
1 parent eefbd62 commit 5927d6e50f8e1f1c1f49516248c8da24bf0ed1cf
@Andreas Jaggi Andreas Jaggi authored on 5 Mar 2007
Showing 1 changed file
View
271
wxmsg.cpp
#include "wx/wx.h"
 
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<string>& buttons, const wxString& default_label, int center, int nearmouse, int timeout);
wxmsgApp();
virtual bool OnInit();
private:
wxString message;
vector<string> buttons;
vector<wxString> 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<string>& buttons);
wxmsgFrame(const wxString& title, const wxString& message, vector<wxString>& buttons, int default_button, int print, int center, int nearmouse, int timeout);
protected:
void onClick(wxCommandEvent& event);
private:
wxPanel *m_panel;
vector<wxButton*> buttons;
vector<wxString> str_buttons;
int print;
};
 
wxmsgApp::wxmsgApp(const wxString& _message, vector<string>& _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;
}
 
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<string>& str_buttons )
buttons = vector<wxString>();
message = _T("");
default_button = -1;
print = 0;
center = 0;
nearmouse = 0;
timeout = 0;
}
 
 
wxmsgFrame::wxmsgFrame ( const wxString& title, const wxString& message, vector<wxString>& _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);
wxBoxSizer *sizerMain = new wxBoxSizer(wxVERTICAL);
sizerMain->Add(sizerBtns);
 
buttons = vector<wxButton*>();
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);
sizerMain->Fit(this);
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 =
#define usage() cout << usagemsg
#define license() cout << licensemsg
#define version() cout << versionmsg
 
void read_buttons(string input, vector<string> &buttons);
 
int main ( int argc, char** argv ) {
void read_buttons(wxString input, vector<wxString> &buttons);
 
IMPLEMENT_APP(wxmsgApp);
 
bool wxmsgApp::OnInit ( ) {
//if ( !wxApp::OnInit() ) {
// return false;
//}
 
int args_consumed = 1;
int i;
 
vector<string> buttons = vector<string>();
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;
int found = 1;
 
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
usage();
args_consumed++;
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 ) {
// error: no label for -default parameter
usage();
exit(1);
}
 
default_label = argv[args_consumed]; // TODO: check that there exists such an label?
args_consumed++;
found = 1;
}
 
if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-print") ) {
default_label = argv[args_consumed];
args_consumed++;
found = 1;
}
 
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
usage();
center = 1;
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
usage();
nearmouse = 1;
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();
exit(1);
}
}
 
if ( buttons.empty() ) {
buttons.push_back("okay:0");
}
 
if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-file") ) {
buttons.push_back(_T("okay:0"));
}
 
if ( (argc-args_consumed > 0) && !wxStrcmp(argv[args_consumed], _T("-file")) ) {
 
args_consumed++;
 
if ( argc - args_consumed < 1 ) {
exit(1);
}
}
 
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;
 
 
exit(0);
}
 
void read_buttons ( string input, vector<string> &buttons ) {
unsigned int commapos = string::npos;
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;
 
 
wxFrame *frame = new wxmsgFrame(_T("wxmsg"), message, buttons, default_button, print, center, nearmouse, timeout);
frame->Show();
 
return true;
}
 
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<wxString> &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;
}