Guest User

Untitled

a guest
Jan 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. /*
  2. Jeuxjieta Address Book System
  3. Version 1.0
  4. By:
  5. Denz Del Villar (101205)
  6. Jojee Dumayaca (101329)
  7. Chi Punzalan (103043)
  8. Special Thanks To:
  9. The Kuyas at CTC314 (I forgot their names :C -Chi)
  10. Google
  11. wxFormBuilder - for sparing us from the pain of studying how to build forms
  12. */
  13.  
  14. #include <wx/wxprec.h>
  15. #ifndef WX_PRECOMP
  16. #include <wx/wx.h>
  17. #endif
  18.  
  19. #include "base.h"
  20.  
  21. IMPLEMENT_APP(MainApp)
  22.  
  23. //This declares what happens once the program is executed.
  24. bool MainApp::OnInit()
  25. {
  26. MainFrame *win = new MainFrame(_("Jeuxjieta Address Book System"), wxPoint (100, 100),
  27. wxSize(600, 400));
  28. win->Show(TRUE);
  29. SetTopWindow(win);
  30.  
  31. return TRUE;
  32. }
  33.  
  34.  
  35. BEGIN_EVENT_TABLE(MainFrame, wxFrame)
  36. EVT_CLOSE(MainFrame::OnClose)
  37. END_EVENT_TABLE()
  38.  
  39. //This defines the Main Window of the program, from its layout, contents to the events
  40. MainFrame::MainFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
  41. : wxFrame((wxFrame *) NULL, -1, title, pos, size)
  42. {
  43.  
  44. this->SetSizeHints( wxDefaultSize, wxDefaultSize );
  45. this->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
  46.  
  47. wxBoxSizer* bSizer9;
  48. bSizer9 = new wxBoxSizer( wxVERTICAL );
  49.  
  50. m_staticText21 = new wxStaticText( this, wxID_ANY, wxT("JEUXJIETA ADDRESS BOOK SYSTEM"), wxDefaultPosition, wxDefaultSize, 0 );
  51. m_staticText21->SetFont( wxFont( 20, 74, 90, 92, false, wxT("Tahoma") ) );
  52.  
  53. bSizer9->Add( m_staticText21, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
  54.  
  55. wxBoxSizer* bSizer10;
  56. bSizer10 = new wxBoxSizer( wxHORIZONTAL );
  57.  
  58. nameslist = new wxListBox( this, wxID_nameslist, wxDefaultPosition, wxSize( 400,320 ), 0, NULL, wxLB_SORT );
  59. bSizer10->Add( nameslist, 0, wxALL, 5 );
  60.  
  61. //This part here is the function used to get the previously stored data.
  62. startup();
  63.  
  64. wxBoxSizer* bSizer14;
  65. bSizer14 = new wxBoxSizer( wxVERTICAL );
  66.  
  67. //Buttons
  68. addButton = new wxButton( this, ADD_BUTTON, wxT("Add New Contact"), wxDefaultPosition, wxSize( 150,50 ), 0 );
  69. bSizer14->Add( addButton, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
  70.  
  71. edtButton = new wxButton( this, EDT_BUTTON, wxT("Edit Contact"), wxDefaultPosition, wxSize( 150,50 ), 0 );
  72. bSizer14->Add( edtButton, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
  73.  
  74. delButton = new wxButton( this, DEL_BUTTON, wxT("Delete Contact"), wxDefaultPosition, wxSize( 150,50 ), 0 );
  75. bSizer14->Add( delButton, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
  76.  
  77. clrButton = new wxButton( this, CLR_BUTTON, wxT("Clear List"), wxDefaultPosition, wxSize( 150,50 ), 0 );
  78. bSizer14->Add( clrButton, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
  79.  
  80. sveButton = new wxButton( this, SVE_BUTTON, wxT("Save Data"), wxDefaultPosition, wxSize( 150,50 ), 0 );
  81. bSizer14->Add( sveButton, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
  82.  
  83. //Connection of Buttons to Events (instead of using the switch method)
  84. Connect(ADD_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
  85. wxCommandEventHandler(MainFrame::OnNew) );
  86. Connect(EDT_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
  87. wxCommandEventHandler(MainFrame::OnRename) );
  88. Connect(CLR_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
  89. wxCommandEventHandler(MainFrame::OnClear) );
  90. Connect(DEL_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
  91. wxCommandEventHandler(MainFrame::OnDelete) );
  92. Connect(SVE_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
  93. wxCommandEventHandler(MainFrame::OnSave) );
  94.  
  95. bSizer10->Add( bSizer14, 1, wxEXPAND, 5 );
  96.  
  97. bSizer9->Add( bSizer10, 1, wxEXPAND, 5 );
  98.  
  99. this->SetSizer( bSizer9 );
  100. this->Layout();
  101.  
  102. this->Centre( wxBOTH );
  103. }
  104.  
  105. //Function for getting user input for new contact entry
  106. void MainFrame::OnNew(wxCommandEvent& event)
  107. {
  108. wxString str = wxGetTextFromUser(wxT("Enter Last Name"));
  109. wxString str2 = wxGetTextFromUser(wxT("Enter First Name"));
  110. wxString str3 = wxGetTextFromUser(wxT("Enter Contact Number"));
  111. wxString str4 = wxGetTextFromUser(wxT("Enter E-Mail Address"));
  112. wxString str5 = wxGetTextFromUser(wxT("Enter Address"));
  113. wxString *comma = new wxString(wxT(", "));
  114. wxString *space = new wxString(wxT(" | "));// this, wxID_ANY, wxT(" "), wxDefaultPosition, wxDefaultSize, 0 );
  115. wxString strgr = str + *comma + str2 + *space + str3 + *space + str4 + *space + str5;
  116.  
  117. if (str.Len() > 0)
  118. nameslist->Append(strgr);
  119.  
  120. }
  121.  
  122. //Function for deleting all contact entries
  123. void MainFrame::OnClear(wxCommandEvent& event)
  124. {
  125. nameslist->Clear();
  126. }
  127.  
  128. //Function for editing details of a selected contact entry
  129. void MainFrame::OnRename(wxCommandEvent& event)
  130. {
  131. wxString text;
  132. wxString newLast = wxGetTextFromUser(wxT("Enter New Last Name"));
  133. wxString newFirst = wxGetTextFromUser(wxT("Enter New First Name"));
  134. wxString newNum = wxGetTextFromUser(wxT("Enter New Number"));
  135. wxString newMail = wxGetTextFromUser(wxT("Enter New E-Mail"));
  136. wxString newAdd = wxGetTextFromUser(wxT("Enter New Address"));
  137. wxString *comma = new wxString(wxT(", "));
  138. wxString *space = new wxString(wxT(" | "));
  139. wxString renamed = newLast + *comma + newFirst + *space + newNum + *space + newMail + *space + newAdd;
  140.  
  141.  
  142. int sel = nameslist->GetSelection();
  143. if (sel != -1) {
  144. text.Printf(nameslist->GetString(sel));
  145. renamed;
  146. }
  147.  
  148. if (!renamed.IsEmpty()) {
  149. nameslist->Delete(sel);
  150. nameslist->Insert(renamed, sel);
  151. }
  152. }
  153.  
  154. //Function for deleting a contact entry
  155. void MainFrame::OnDelete(wxCommandEvent& event)
  156. {
  157. int sel = nameslist->GetSelection();
  158. if (sel != -1) {
  159. nameslist->Delete(sel);
  160. }
  161. }
  162.  
  163. //Function for saving data on data.txt
  164. void MainFrame::OnSave(wxCommandEvent& event)
  165. {
  166. int listCount = nameslist->GetCount();
  167.  
  168. wxTextFile file( wxT("data.txt") );
  169. file.Open();
  170. file.Clear();
  171.  
  172. for (int h=0; h<=listCount; h++) {
  173. const wxChar* listItem = nameslist->GetString(h);
  174. file.AddLine(listItem);
  175. }
  176. file.Write();
  177. file.Close();
  178. }
  179.  
  180. //Function for retrieving previously stored data from data.txt and storing it in the contact list
  181. bool MainFrame::startup() {
  182. wxString str;
  183. nameslist->Deselect(0);
  184. nameslist->SetFirstItem(wxT("Last Name, First Name | Contact Number | E-mail Address | Address"));
  185. wxTextFile file( wxT("data.txt") );
  186. //Checks if data.txt exists & creates a new file if data.txt does not exist
  187. if (!file.Exists()){
  188. file.Create();
  189. }
  190. else {
  191. file.Open();
  192. str = file.GetFirstLine();
  193. nameslist->Append(str);
  194.  
  195. while(!file.Eof())
  196. {
  197. str = file.GetNextLine();
  198. nameslist->Append(str);
  199. }
  200. }
  201. }
  202. //Just in case the user forgets to save his/her data, it will automatically save when closed.
  203. void MainFrame::OnClose (wxCloseEvent& event) {
  204. int listCount = nameslist->GetCount();
  205.  
  206. wxTextFile file( wxT("data.txt") );
  207. file.Open();
  208. file.Clear();
  209.  
  210. for (int h=0; h<=listCount; h++) {
  211. const wxChar* listItem = nameslist->GetString(h);
  212. file.AddLine(listItem);
  213. }
  214. file.Write();
  215. file.Close();
  216. Destroy();
  217. }
Add Comment
Please, Sign In to add comment