// frame.cpp #include "frame.h" #include #include wxString weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size) : wxFrame((wxFrame *)NULL, -1, title, pos, size) { m_title = title; m_panel = (wxPanel *) NULL; m_panel = new wxPanel(this, -1, pos, size, wxCLIP_CHILDREN | wxTAB_TRAVERSAL); // fill day array day[0].setName("Sunday"); day[1].setName("Monday"); day[2].setName("Tuesday"); day[3].setName("Wednesday"); day[4].setName("Thursday"); day[5].setName("Friday"); day[6].setName("Saturday"); // create program menus wxMenu *menuFile = new wxMenu; menuFile->Append(ID_Quit, "E&xit"); wxMenu *menuHelp = new wxMenu; menuHelp->Append(ID_About, "&About..."); wxMenuBar *menuBar = new wxMenuBar; menuBar->Append(menuFile, "&File"); menuBar->Append(menuHelp, "&Help"); SetMenuBar(menuBar); gridRow = 0; createPanelLayout(); } MyFrame::~MyFrame() { // empty } void MyFrame::createPanelLayout() { wxString minutes[] = {"00", "15", "30", "45" }; wxString hours[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}; wxBoxSizer *vboxMain = new wxBoxSizer(wxVERTICAL); wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL); wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL); // days area days = new wxRadioBox(m_panel, ID_DAYRADIO, wxT("Days"), wxDefaultPosition, wxDefaultSize, 7, weekdays, 1, wxRA_SPECIFY_COLS); days->SetSelection(0); // add time area wxStaticBox *times = new wxStaticBox(m_panel, ID_TIMES, wxT("Add Time"), wxDefaultPosition, wxDefaultSize); wxStaticBoxSizer *sbTimes = new wxStaticBoxSizer(times, wxVERTICAL); // select boxes for in-time and am/pm wxBoxSizer *mybox1 = new wxBoxSizer(wxHORIZONTAL); inHours = new wxChoice(m_panel, ID_InHours, wxDefaultPosition, wxDefaultSize, 12, hours); inHours->SetSelection(0); inMinutes = new wxChoice(m_panel, ID_InMinutes, wxDefaultPosition, wxDefaultSize, 4, minutes); inMinutes->SetSelection(0); mybox1->Add(new wxStaticText(m_panel, -1, "In : "), 0, wxALIGN_CENTER, 10); mybox1->Add(inHours, 0, wxALIGN_CENTER, 10); mybox1->Add(inMinutes, 0, wxALIGN_CENTER, 10); wxBoxSizer *apvbox = new wxBoxSizer(wxVERTICAL); amin = new wxRadioButton(m_panel, ID_amin, wxT("am"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP); amin->SetValue(TRUE); pmin = new wxRadioButton(m_panel, ID_pmin, wxT("pm")); apvbox->Add(amin, 0, wxALIGN_CENTER, 10); apvbox->Add(pmin, 0, wxALIGN_CENTER, 10); mybox1->Add(apvbox, 0, wxALIGN_CENTER, 10); wxBoxSizer *mybox2 = new wxBoxSizer(wxHORIZONTAL); outHours = new wxChoice(m_panel, ID_OutHours, wxDefaultPosition, wxDefaultSize, 12, hours); outHours->SetSelection(0); outMinutes = new wxChoice(m_panel, ID_OutMinutes, wxDefaultPosition, wxDefaultSize, 4, minutes); outMinutes->SetSelection(0); mybox2->Add(new wxStaticText(m_panel, -1, "Out : "), 0, wxALIGN_CENTER, 10); mybox2->Add(outHours, 0, wxALIGN_CENTER, 10); mybox2->Add(outMinutes, 0, wxALIGN_CENTER, 10); apvbox = new wxBoxSizer(wxVERTICAL); amout = new wxRadioButton(m_panel, ID_amout, wxT("am"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP); pmout = new wxRadioButton(m_panel, ID_pmout, wxT("pm")); pmout->SetValue(TRUE); apvbox->Add(amout, 0, wxALIGN_CENTER, 10); apvbox->Add(pmout, 0, wxALIGN_CENTER, 10); mybox2->Add(apvbox, 0, wxALIGN_CENTER, 10); // add time button wxBoxSizer *mybox3 = new wxBoxSizer(wxHORIZONTAL); addButton = new wxButton(m_panel, ID_AddButton, wxT("Add Time"), wxDefaultPosition, wxDefaultSize); mybox3->Add(addButton, 1, 0, 10); // add clear all button wxBoxSizer *mybox4 = new wxBoxSizer(wxHORIZONTAL); clearButton = new wxButton(m_panel, ID_ClearButton, wxT("Clear All Times"), wxDefaultPosition, wxDefaultSize); mybox4->Add(clearButton, 1, 0, 0); sbTimes->Add(mybox1, 1, wxEXPAND); sbTimes->Add(mybox2, 1, wxEXPAND); sbTimes->Add(mybox3, 1, wxALIGN_CENTER | wxEXPAND, 0); sbTimes->Add(mybox4, 1, wxALIGN_CENTER | wxEXPAND, 0); // fill out all of first row hbox1->Add(days, 0, wxEXPAND, 10); hbox1->Add(sbTimes, 0, wxEXPAND, 10); // put row in main vbox vboxMain->Add(hbox1, 0, 0, 10); // make grid grid = new wxGrid(m_panel, ID_Grid, wxDefaultPosition, GRID_SIZE); grid->CreateGrid(15, 3); // make title row grid->SetColLabelValue(0, "In"); grid->SetColLabelValue(1, "Out"); grid->SetColLabelValue(2, "Total"); grid->SetColFormatFloat(2, 2, 2); hbox2->Add(grid, 0, 0, 10); vboxMain->Add(hbox2, 1, 0, 10); SetSizer(vboxMain); } // // callback/event table declaration // BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(ID_Quit, MyFrame::OnQuit) EVT_MENU(ID_About, MyFrame::OnAbout) EVT_BUTTON(ID_AddButton, MyFrame::OnAddTime) EVT_BUTTON(ID_ClearButton, MyFrame::reset) END_EVENT_TABLE() // // menu callbacks // void MyFrame::OnQuit(wxCommandEvent &WXUNUSED(event)) { Close(TRUE); } void MyFrame::OnAbout(wxCommandEvent &WXUNUSED(event)) { wxString msg = m_title; msg << "\n\n(c) A. Jacob Cord, 2004"; wxMessageBox(msg, "Jacob's Time Calculator", wxOK | wxICON_INFORMATION, this); } void MyFrame::OnAddTime(wxCommandEvent &WXUNUSED(event)) { int dayIndex = days->GetSelection(); Span span; wxString Hstart = inHours->GetStringSelection(); wxString Mstart = inMinutes->GetStringSelection(); wxString Hend = outHours->GetStringSelection(); wxString Mend = outMinutes->GetStringSelection(); int hs = atoi(Hstart.c_str()); int ms = atoi(Mstart.c_str()); int he = atoi(Hend.c_str()); int me = atoi(Mend.c_str()); if(hs == 12) { // this fixes for military time hs = 0; } if(he == 12) { // fix for end hours too he = 0; } if(pmin->GetValue() == TRUE) { // in-time is pm hs += 12; } if(pmout->GetValue() == TRUE) { // out-time is pm he += 12; } wxString sStart, sEnd, sTotal; // add actual time to structure day[dayIndex].addTime(hs, ms, he, me); // format times for output sStart << Hstart << ":" << Mstart; sEnd << Hend << ":" << Mend; // calculate total hours float total = 0.0; for(int i=0; i<7; i++) { total += day[i].getHours(); } sTotal << total; // output to grid grid->SetRowLabelValue(gridRow,weekdays[dayIndex]); grid->SetCellValue(gridRow, 0, sStart); grid->SetCellValue(gridRow, 1, sEnd); grid->SetCellValue(gridRow, 2, sTotal); // make rows uneditable for(int i=0; i<5; i++) { grid->SetReadOnly(gridRow, i, TRUE); } // get ready for next row gridRow++; } void MyFrame::reset(void) { // clear out all internal data for(int i=0; i<7; i++) { day[i].deleteTimes(); } // reset grid display grid->ClearGrid(); for(int row=0; row<15; row++) { wxString temp; temp << row + 1; grid->SetRowLabelValue(row, temp); gridRow = 0; } }