Main Page   Class Hierarchy   Compound List   File List   Compound Members  

AddressManagerFrame.cpp

00001 /*
00002  * Purpose: AddressManager frame
00003  * Author:  Frank Buß
00004  * Created: 2002
00005  */
00006 
00007 #include <wx/clipbrd.h>
00008 #include <stdlib.h>
00009 
00010 #include "AddressManager.h"
00011 #include "AddressManagerFrame.h"
00012 #include "dialogs.h"
00013 
00014 
00015 static const wxUint32 g_admFileId = ('a' | ('d' << 8) | ('m' << 16));
00016 static const wxUint32 g_admFileVersion = 1;
00017 
00018 
00019 class AddEmptyRowGridCellTextEditor : public wxGridCellTextEditor
00020 {
00021 public:
00022     AddEmptyRowGridCellTextEditor(AddressManagerFrame& frame) : m_frame(frame) {}
00023     void BeginEdit(int row, int col, wxGrid* grid)
00024     {
00025         // add an empty row, if editing the a cell in the last line
00026         if (grid->GetRows() - 1 == row) grid->AppendRows(1, false);
00027         wxGridCellTextEditor::BeginEdit(row, col, grid);
00028         m_frame.setDirty();
00029     }
00030 private:
00031     AddressManagerFrame& m_frame;
00032 };
00033 
00034 // AddressManagerFrame constructor
00035 AddressManagerFrame::AddressManagerFrame(wxFrame *frame, const wxString& title,
00036                  const wxPoint& pos, const wxSize& size):
00037     wxFrame(frame, -1, title, pos, size)
00038 {
00039     m_dirty = false;
00040     m_pGrid = new wxGrid(this, -1, wxPoint(0,0), wxSize(600, 400));
00041     m_pTable = new AddressGridTable();
00042     m_pGrid->SetTable(m_pTable);
00043     m_pGrid->RegisterDataType(wxGRID_VALUE_STRING,
00044         new wxGridCellStringRenderer,
00045         new AddEmptyRowGridCellTextEditor(*this));
00046     m_pGrid->DisableDragRowSize();
00047     m_pGrid->SetColLabelSize(m_pGrid->GetDefaultRowSize());
00048     m_pGrid->SetDefaultRowSize(20);
00049 
00050     wxAcceleratorEntry entries[6];
00051     entries[0].Set(wxACCEL_CTRL,  (int) 'S', wxID_SAVE);
00052     entries[1].Set(wxACCEL_CTRL,  (int) 'X', MENU_EDIT_CUT);
00053     entries[2].Set(wxACCEL_CTRL,  (int) 'C', MENU_EDIT_COPY);
00054     entries[3].Set(wxACCEL_CTRL,  (int) 'V', MENU_EDIT_PASTE);
00055     entries[4].Set(wxACCEL_CTRL,  (int) 'F', wxID_FIND);
00056     entries[5].Set(wxACCEL_CTRL,  (int) 'Q', MENU_FILE_EXIT);
00057     wxAcceleratorTable accel(6, entries);
00058     SetAcceleratorTable(accel);
00059 
00060     m_pGrid->UpdateDimensions();
00061     Refresh();
00062 
00063     wxCommandEvent dummy;
00064     OnMenuFileNew(dummy);
00065 }
00066 
00067 AddressManagerFrame::~AddressManagerFrame()
00068 {
00069     delete m_pGrid;
00070     delete m_pTable;
00071 }
00072 
00073 BEGIN_EVENT_TABLE(AddressManagerFrame, wxFrame)
00074     EVT_MENU(wxID_ABOUT, AddressManagerFrame::OnAbout)
00075     EVT_MENU(wxID_NEW, AddressManagerFrame::OnMenuFileNew)
00076     EVT_MENU(wxID_OPEN, AddressManagerFrame::OnMenuFileOpen)
00077 //    EVT_MENU(wxID_EXIT, AddressManagerFrame::OnMenuFileClose)
00078     EVT_MENU(wxID_SAVE, AddressManagerFrame::OnMenuFileSave)
00079     EVT_MENU(wxID_SAVEAS, AddressManagerFrame::OnMenuFileSaveAs)
00080     EVT_MENU(MENU_FILE_EXIT, AddressManagerFrame::OnMenuFileExit)
00081     EVT_MENU(MENU_EDIT_CUT, AddressManagerFrame::OnMenuEditCut)
00082     EVT_MENU(MENU_EDIT_COPY, AddressManagerFrame::OnMenuEditCopy)
00083     EVT_MENU(MENU_EDIT_PASTE, AddressManagerFrame::OnMenuEditPaste)
00084     EVT_MENU(wxID_FIND, AddressManagerFrame::OnMenuEditFind)
00085     EVT_MENU(MENU_TABLE_DOUBLET, AddressManagerFrame::OnMenuTableDoublet)
00086     EVT_MENU(MENU_TABLE_SORT, AddressManagerFrame::OnMenuTableSort)
00087     EVT_MENU(MENU_TABLE_CONFIG, AddressManagerFrame::OnMenuTableConfig)
00088     EVT_MENU(MENU_EXPORT_EMAIL, AddressManagerFrame::OnMenuExportEmail)
00089     EVT_MENU(MENU_EXPORT_SMS, AddressManagerFrame::OnMenuExportSMS)
00090     EVT_MENU(MENU_EXPORT_FAX, AddressManagerFrame::OnMenuExportFax)
00091     EVT_CLOSE(AddressManagerFrame::OnClose)
00092 
00093     EVT_SIZE(AddressManagerFrame::OnSize)
00094 END_EVENT_TABLE()
00095 
00096 void AddressManagerFrame::OnAbout(wxCommandEvent& event)
00097 {
00098     // save current edit cell, if neccessary
00099     m_pGrid->DisableCellEditControl();
00100 
00101     AddressManagerAboutDialog dialog(this);
00102     dialog.ShowModal();
00103 }
00104 
00105 void AddressManagerFrame::OnMenuFileNew(wxCommandEvent& event)
00106 {
00107     // save current edit cell, if neccessary
00108     m_pGrid->DisableCellEditControl();
00109 
00110     if (m_dirty) {
00111         int result = wxMessageBox("Änderungen speichern?", "Address Manager", wxICON_QUESTION | wxYES_NO | wxCANCEL, this);
00112         if (result == wxCANCEL) return;
00113         if (result == wxYES) {
00114             if (!FileSaveImpl()) return;
00115         }
00116     }
00117     wxBusyCursor wait;
00118     m_pTable->Initialize();
00119 
00120     int width, height;
00121     GetSize(&width, &height);
00122     SetSize(width - 1, height - 1);
00123     SetSize(width, height);
00124     m_dirty = false;
00125 }
00126 
00127 void AddressManagerFrame::OnMenuFileOpen(wxCommandEvent& event)
00128 {
00129     // save current edit cell, if neccessary
00130     m_pGrid->DisableCellEditControl();
00131 
00132     if (m_dirty) {
00133         int result = wxMessageBox("Änderungen speichern?", "Address Manager", wxICON_QUESTION | wxYES_NO | wxCANCEL, this);
00134         if (result == wxCANCEL) return;
00135         if (result == wxYES) {
00136             if (!FileSaveImpl()) return;
00137         }
00138     }
00139     wxFileDialog dialog(this, "Datei Öffnen", "", "", "Address Manager Dateien (*.adm)|*.adm", wxOPEN);
00140     if (dialog.ShowModal() == wxID_OK) {
00141         wxBusyCursor wait;
00142         m_currentFilename = dialog.GetPath();
00143 
00144         // open file; TODO: file locking
00145         wxFileInputStream fileInStream(m_currentFilename);
00146         wxDataInputStream in(fileInStream);
00147 
00148         // check if right file format and file is readable
00149         wxUint32 id = in.Read32();
00150         wxUint32 version = in.Read32();
00151         if (!in.IsOk() || id != g_admFileId || version > g_admFileVersion) {
00152             wxMessageDialog errorDialog(this, "Fehler beim Öffnen der Datei", "Address Manager", wxOK | wxICON_ERROR);
00153             errorDialog.ShowModal();
00154             return;
00155         }
00156 
00157         // read file
00158         bool end = false;
00159         while (!end) {
00160             wxUint32 tag = in.Read32();
00161             wxUint32 size = in.Read32();
00162             switch (tag) {
00163                 case admTagGrid:
00164                     m_pTable->Initialize(in);
00165                     break;
00166                 case admTagEnd:
00167                     end = true;
00168                     break;
00169                 default:
00170                     fileInStream.SeekI(size, wxFromCurrent);
00171             }
00172         }
00173 
00174         // update view
00175         int width, height;
00176         m_dirty = false;
00177         GetSize(&width, &height);
00178         SetSize(width - 1, height - 1);
00179         SetSize(width, height);
00180         m_pGrid->SetFocus();
00181 
00182         // not modified
00183         m_dirty = false;
00184     }
00185 }
00186 
00187 void AddressManagerFrame::OnMenuFileClose(wxCommandEvent& event)
00188 {
00189     // save current edit cell, if neccessary
00190     m_pGrid->DisableCellEditControl();
00191     // TODO
00192 /*  if (m_grid) {
00193         delete m_grid;
00194         m_grid = NULL;
00195     }*/
00196 }
00197 
00198 bool AddressManagerFrame::FileSaveImpl()
00199 {
00200     if (m_currentFilename.Length() == 0) {
00201         if (!FileSaveAsImpl()) return false;
00202     }
00203     wxBusyCursor wait;
00204     wxFileOutputStream fileOutStream(m_currentFilename);
00205     wxDataOutputStream out(fileOutStream);
00206     out.Write32(g_admFileId);
00207     out.Write32(g_admFileVersion);
00208     out.Write32(admTagGrid);
00209     out.Write32(0);
00210     off_t cur = fileOutStream.TellO();
00211     m_pTable->serialize(out);
00212     off_t end = fileOutStream.TellO();
00213     fileOutStream.SeekO(cur - 4);
00214     out.Write32(end - cur);
00215     fileOutStream.SeekO(end);
00216     out.Write32(admTagEnd);
00217     out.Write32(0);
00218     if (!fileOutStream.IsOk()) {
00219         wxMessageDialog errorDialog(this, "Fehler beim Speichern der Datei", "Address Manager", wxOK | wxICON_ERROR);
00220         errorDialog.ShowModal();
00221         return false;
00222     }
00223     m_dirty = false;
00224     return true;
00225 }
00226 
00227 void AddressManagerFrame::OnMenuFileSave(wxCommandEvent& event)
00228 {
00229     // save current edit cell, if neccessary
00230     m_pGrid->DisableCellEditControl();
00231 
00232     FileSaveImpl();
00233 }
00234 
00235 bool AddressManagerFrame::FileSaveAsImpl()
00236 {
00237     wxFileDialog dialog(this, "Datei Speichern unter", "", m_currentFilename, "Address Manager Dateien (*.adm)|*.adm", wxSAVE|wxOVERWRITE_PROMPT);
00238     if (dialog.ShowModal() == wxID_OK) {
00239         m_currentFilename = dialog.GetPath();
00240 
00241         // add extension, if missing (on Mac)
00242         size_t pos = m_currentFilename.Length() - 4;
00243         if (pos < 0) pos = 0;
00244         wxString ext = m_currentFilename.Mid(pos);
00245         if (ext.CmpNoCase(".adm") != 0) m_currentFilename += ".adm";
00246 
00247         // save file
00248         if (!FileSaveImpl()) return false;
00249 
00250         // notify that the file was saved
00251         return true;
00252     }
00253 
00254     // notify that the dialog was canceled
00255     return false;
00256 }
00257 
00258 void AddressManagerFrame::OnMenuFileSaveAs(wxCommandEvent& event)
00259 {
00260     // save current edit cell, if neccessary
00261     m_pGrid->DisableCellEditControl();
00262 
00263     FileSaveAsImpl();
00264 }
00265 
00266 bool AddressManagerFrame::onExit()
00267 {
00268     // save current edit cell, if neccessary
00269     m_pGrid->DisableCellEditControl();
00270 
00271     if (m_dirty) {
00272         int result = wxMessageBox("Änderungen speichern?", "Address Manager", wxICON_QUESTION | wxYES_NO | wxCANCEL, this);
00273         if (result == wxCANCEL) return false;
00274         if (result == wxYES) {
00275             if (!FileSaveImpl()) return false;
00276         }
00277     }
00278     return true;
00279 }
00280 
00281 void AddressManagerFrame::OnMenuFileExit(wxCommandEvent& event)
00282 {
00283     // save current edit cell, if neccessary
00284     m_pGrid->DisableCellEditControl();
00285 
00286     if (onExit()) Close(true);
00287 }
00288 
00289 void AddressManagerFrame::OnMenuEditCut(wxCommandEvent& event)
00290 {
00291     // save current edit cell, if neccessary
00292     m_pGrid->DisableCellEditControl();
00293 
00294     m_dirty = true;
00295     copy(true);
00296 }
00297 
00298 void AddressManagerFrame::copy(bool erase)
00299 {
00300     // search first start
00301     int rowCount = m_pGrid->GetRows();
00302     int colCount = m_pGrid->GetCols();
00303     int startRow = -1;
00304     int startCol = -1;
00305     int row, col;
00306     for (row = 0; row < rowCount; row++) {
00307         for (col = 0; col < colCount; col++) {
00308             if (m_pGrid->IsInSelection(row, col)) {
00309                 startRow = row;
00310                 startCol = col;
00311                 break;
00312             }
00313         }
00314         if (startRow != -1) break;
00315     }
00316 
00317     // search last end
00318     int endRow = -1;
00319     int endCol = -1;
00320     if (startRow != -1) {
00321         for (row = startRow; row < rowCount; row++) {
00322             for (col = startCol; col < colCount; col++) {
00323                 if (m_pGrid->IsInSelection(row, col)) {
00324                     if (row > endRow) endRow = row;
00325                     if (col > endCol) endCol = col;
00326                 }
00327             }
00328         }
00329     }
00330 
00331     // get cursor position, if no selection
00332     if (startRow == -1) {
00333         startRow = endRow = m_pGrid->GetGridCursorRow();
00334         startCol = endCol = m_pGrid->GetGridCursorCol();
00335     }
00336 
00337     // copy to clipboard
00338     wxBusyCursor wait;
00339     if (!wxTheClipboard->Open()) return;
00340     wxString text;
00341     bool firstRow = true;
00342     for (row = startRow; row <= endRow; row++) {
00343         if (firstRow) {
00344             firstRow = false;
00345         } else {
00346             text += "\n";
00347         }
00348         bool firstCol = true;
00349         for (col = startCol; col <= endCol; col++) {
00350             if (firstCol) {
00351                 firstCol = false;
00352             } else {
00353                 text += "\t";
00354             }
00355             text += m_pGrid->GetCellValue(row, col);
00356             if (erase) m_pGrid->SetCellValue(row, col, "");
00357         }
00358     }
00359     wxTheClipboard->SetData(new wxTextDataObject(text));
00360     wxTheClipboard->Close();
00361     if (erase) m_dirty = true;
00362 }
00363 
00364 void AddressManagerFrame::OnMenuEditCopy(wxCommandEvent& event)
00365 {
00366     // save current edit cell, if neccessary
00367     m_pGrid->DisableCellEditControl();
00368 
00369     copy(false);
00370 }
00371 
00372 // splits a wxString into lines
00373 // detects all possible line breaking charaters: \n, \r, \n\r or \r\n
00374 class LineSplitter
00375 {
00376 public:
00377     LineSplitter(const wxString& text) : m_text(text), m_ofs(0)
00378     {
00379         m_len = text.Length();
00380     }
00381 
00382     bool extractNext()
00383     {
00384         if (m_ofs == m_len) return false;
00385         size_t end = m_ofs;
00386         while (end < m_len) {
00387             wxChar c = m_text[end];
00388             if (c == 10 || c == 13) {
00389                 m_currentLine = m_text.Mid(m_ofs, end - m_ofs);
00390                 if (end == m_len - 1) {
00391                     m_ofs = m_len;
00392                     return true;
00393                 }
00394                 if ((c == 10 && m_text[end + 1] == 13) ||
00395                     (c == 13 && m_text[end + 1] == 10))
00396                 {
00397                     m_ofs = end + 2;
00398                     return true;
00399                 }
00400                 m_ofs = end + 1;
00401                 return true;
00402             }
00403             end++;
00404         }
00405         m_currentLine = m_text.Mid(m_ofs, end - m_ofs);
00406         m_ofs = end;
00407         return true;
00408     }
00409 
00410     wxString getNext()
00411     {
00412         return m_currentLine;
00413     }
00414 
00415 private:
00416     const wxString& m_text;
00417     wxString m_currentLine;
00418     size_t m_ofs;
00419     size_t m_len;
00420 };
00421 
00422 
00423 void AddressManagerFrame::OnMenuEditPaste(wxCommandEvent& event)
00424 {
00425     // save current edit cell, if neccessary
00426     m_pGrid->DisableCellEditControl();
00427 
00428     wxBusyCursor wait;
00429 
00430     // search first start
00431     int rowCount = m_pGrid->GetRows();
00432     int colCount = m_pGrid->GetCols();
00433     int startRow = -1;
00434     int startCol = -1;
00435     int row, col;
00436     for (row = 0; row < rowCount; row++) {
00437         for (col = 0; col < colCount; col++) {
00438             if (m_pGrid->IsInSelection(row, col)) {
00439                 startRow = row;
00440                 startCol = col;
00441                 break;
00442             }
00443         }
00444         if (startRow != -1) break;
00445     }
00446 
00447     // get cursor position, if no selection
00448     if (startRow == -1) {
00449         startRow = m_pGrid->GetGridCursorRow();
00450         startCol = m_pGrid->GetGridCursorCol();
00451     }
00452 
00453     // paste from clipboard
00454     if (!wxTheClipboard->Open()) return;
00455     if (!wxTheClipboard->IsSupported(wxDF_TEXT)) return;
00456     wxTextDataObject data;
00457     wxTheClipboard->GetData(data);
00458     wxString text = data.GetText();
00459     m_pGrid->BeginBatch();
00460     LineSplitter ls(text);
00461     while (ls.extractNext()) {
00462         if (startRow >= rowCount - 1) {
00463             m_pGrid->AppendRows(1, false);
00464             rowCount++;
00465         }
00466         wxString line = ls.getNext();
00467         unsigned lpos = 0;
00468         for (int col = startCol; col < colCount; col++) {
00469             unsigned lsearch = lpos;
00470             while (lsearch < line.Length()) {
00471                 if (line[lsearch] == '\t') break;
00472                 lsearch++;
00473             }
00474             wxString word = line.Mid(lpos, lsearch - lpos);
00475             m_pGrid->SetCellValue(startRow, col, word);
00476             lpos = lsearch;
00477             lpos++;
00478             if (lpos >= line.Length()) break;
00479         }
00480         startRow++;
00481     }
00482     m_pGrid->EndBatch();
00483     wxTheClipboard->Close();
00484     m_dirty = true;
00485 }
00486 
00487 bool substringMatch(const wxString& search, const wxString& value)
00488 {
00489     int slen = search.Length();
00490     int vlen = value.Length();
00491     if (slen == 0 || vlen == 0) return false;
00492 
00493     for (int valueStart = 0; valueStart < vlen - 1; valueStart++) {
00494         int searchIndex = 0;
00495         for (int valueIndex = valueStart; valueIndex < vlen; valueIndex++) {
00496             if (wxTolower(search[searchIndex++]) != wxTolower(value[valueIndex])) break;
00497             if (searchIndex == slen) return true;
00498         }
00499     }
00500     return false;
00501 }
00502 
00503 void AddressManagerFrame::OnMenuEditFind(wxCommandEvent& event)
00504 {
00505     // save current edit cell, if neccessary
00506     m_pGrid->DisableCellEditControl();
00507 
00508     // get search text
00509     wxTextEntryDialog dialog(this, "Suchen nach:", "Address Manager", m_lastSearchString, wxOK | wxCANCEL);
00510     if (dialog.ShowModal() != wxID_OK) return;
00511     m_lastSearchString = dialog.GetValue();
00512 
00513     // calulate next column to start from
00514     int row = m_pGrid->GetGridCursorRow();
00515     int rowCount = m_pGrid->GetRows();
00516     int colCount = m_pGrid->GetCols();
00517     int col = m_pGrid->GetGridCursorCol() + 1;
00518     if (col == colCount) {
00519         col = 0;
00520         row++;
00521         if (row == rowCount) row = 0;
00522     }
00523 
00524     // search
00525     for (; row < rowCount; row++) {
00526         for (; col < colCount; col++) {
00527             if (substringMatch(m_lastSearchString, m_pGrid->GetCellValue(row, col))) {
00528                 m_pGrid->SetGridCursor(row, col);
00529                 m_pGrid->MakeCellVisible(row, col);
00530                 return;
00531             }
00532         }
00533         col = 0;
00534     }
00535 
00536     // if not found, search from start
00537     for (row = 0; row < rowCount; row++) {
00538         for (col = 0; col < colCount; col++) {
00539             if (substringMatch(m_lastSearchString, m_pGrid->GetCellValue(row, col))) {
00540                 m_pGrid->SetGridCursor(row, col);
00541                 m_pGrid->MakeCellVisible(row, col);
00542                 return;
00543             }
00544         }
00545     }
00546 }
00547 
00548 void AddressManagerFrame::OnMenuTableDoublet(wxCommandEvent& event)
00549 {
00550     // save current edit cell, if neccessary
00551     m_pGrid->DisableCellEditControl();
00552 
00553     unsigned rowCount = m_pGrid->GetRows();
00554     unsigned colCount = m_pGrid->GetCols();
00555     if (rowCount < 2) return;
00556 
00557     // get eMail index
00558     unsigned col;
00559     bool found = false;
00560     for (col = 0; col < colCount; col++) {
00561         wxString label = m_pGrid->GetColLabelValue(col);
00562         if (label.CmpNoCase("email") == 0) {
00563             found = true;
00564             break;
00565         }
00566     }
00567     if (!found) {
00568         wxMessageDialog errorDialog(this, "Doublettenableich nicht möglich, da es keine eMail-Tabellenspalte gibt.", "Address Manager", wxOK | wxICON_INFORMATION);
00569         errorDialog.ShowModal();
00570         return;
00571     }
00572 
00573     // get row to start from
00574     unsigned row = m_pGrid->GetGridCursorRow();
00575 
00576     // search
00577     for (; row < rowCount - 1; row++) {
00578         wxString value = m_pGrid->GetCellValue(row, col);
00579         if (value.Length() > 0 && value.CmpNoCase(m_pGrid->GetCellValue(row + 1, col)) == 0) {
00580             m_pGrid->SetGridCursor(row + 1, col);
00581             m_pGrid->MakeCellVisible(row + 1, col);
00582             m_pGrid->SelectBlock(row, 0, row + 1, colCount);
00583             return;
00584         }
00585     }
00586 
00587     // if not found, search from start
00588     for (row = 0; row < rowCount - 1; row++) {
00589         wxString value = m_pGrid->GetCellValue(row, col);
00590         if (value.Length() > 0 && value.CmpNoCase(m_pGrid->GetCellValue(row + 1, col)) == 0) {
00591             m_pGrid->SetGridCursor(row + 1, col);
00592             m_pGrid->MakeCellVisible(row + 1, col);
00593             m_pGrid->SelectBlock(row, 0, row + 1, colCount);
00594             return;
00595         }
00596     }
00597 }
00598 
00599 void AddressManagerFrame::OnMenuTableSort(wxCommandEvent& event)
00600 {
00601     // save current edit cell, if neccessary
00602     m_pGrid->DisableCellEditControl();
00603 
00604     // copy column names
00605     ColumnFieldArray columns;
00606     columns.Add(NullColumnField());
00607     unsigned i = 0;
00608     for (i = 0; i < (unsigned) m_pGrid->GetCols(); i++) {
00609         columns.Add(ColumnField(m_pGrid->GetColLabelValue(i), i));
00610     }
00611 
00612     // ask user for sort order
00613     SortDialog dialog(this, columns);
00614     if (dialog.ShowModal() != wxID_OK) return;
00615 
00616     // get sort order
00617     wxBusyCursor wait;
00618     columns = dialog.getSortOrder();
00619     wxASSERT(columns.GetCount() > 0);
00620     int sortCol1 = -1;
00621     int sortCol2 = -1;
00622     int sortCol3 = -1;
00623     if (columns.GetCount() >= 1) sortCol1 = columns[0].getOldIndex();
00624     if (columns.GetCount() >= 2) sortCol2 = columns[1].getOldIndex();
00625     if (columns.GetCount() == 3) sortCol3 = columns[2].getOldIndex();
00626     m_pTable->Sort(sortCol1, sortCol2, sortCol3);
00627 
00628     m_dirty = true;
00629 
00630     int width, height;
00631     GetSize(&width, &height);
00632     SetSize(width - 1, height - 1);
00633     SetSize(width, height);
00634 }
00635 
00636 void AddressManagerFrame::OnMenuTableConfig(wxCommandEvent& event)
00637 {
00638     // save current edit cell, if neccessary
00639     m_pGrid->DisableCellEditControl();
00640 
00641     // copy column names
00642     ColumnFieldArray columns;
00643     unsigned i = 0;
00644     for (i = 0; i < (unsigned) m_pGrid->GetCols(); i++) {
00645         columns.Add(ColumnField(m_pGrid->GetColLabelValue(i), i, m_pGrid->GetColumnWidth(i)));
00646     }
00647 
00648     // edit
00649     ColumnDialog dialog(this, columns);
00650     if (dialog.ShowModal() != wxID_OK) return;
00651 
00652     // get all columns, which are not deleted
00653     wxBusyCursor wait;
00654     columns = dialog.getNewColumns();
00655 
00656     // copy in new grid
00657     m_pTable->ChangeColumns(columns);
00658 
00659     m_dirty = true;
00660 
00661     int width, height;
00662     GetSize(&width, &height);
00663     SetSize(width - 1, height - 1);
00664     SetSize(width, height);
00665 }
00666 
00667 void AddressManagerFrame::OnMenuExportEmail(wxCommandEvent& event)
00668 {
00669     // save current edit cell, if neccessary
00670     m_pGrid->DisableCellEditControl();
00671 
00672     unsigned rowCount = m_pGrid->GetRows();
00673     unsigned colCount = m_pGrid->GetCols();
00674     if (rowCount < 1) return;
00675 
00676     // get eMail index
00677     unsigned col;
00678     bool found = false;
00679     for (col = 0; col < colCount; col++) {
00680         wxString label = m_pGrid->GetColLabelValue(col);
00681         if (label.CmpNoCase("email") == 0) {
00682             found = true;
00683             break;
00684         }
00685     }
00686     if (!found) {
00687         wxMessageDialog errorDialog(this, "eMail-Export nicht möglich, da es keine eMail-Tabellenspalte gibt.", "Address Manager", wxOK | wxICON_INFORMATION);
00688         errorDialog.ShowModal();
00689         return;
00690     }
00691 
00692     // init array for testing for valid email characters
00693     bool validChars[256];
00694     unsigned i;
00695     for (i = 0; i <= 255; i++) validChars[i] = false;
00696     for (i = 'a'; i <= 'z'; i++) validChars[i] = true;
00697     for (i = 'A'; i <= 'Z'; i++) validChars[i] = true;
00698     for (i = '0'; i <= '9'; i++) validChars[i] = true;
00699     validChars['@'] = true;
00700     validChars['.'] = true;
00701     validChars['_'] = true;
00702     validChars['-'] = true;
00703     validChars['~'] = true;
00704 
00705     // open export file
00706 #if defined(__WXMSW__)
00707     wxFileOutputStream fileOutStream("c:\\email-liste.txt");
00708 #else
00709     wxFileOutputStream fileOutStream("Macintosh HD:email-liste.txt");
00710 #endif
00711     wxTextOutputStream ostr(fileOutStream);
00712 
00713     // export
00714     int count = 0;
00715     for (unsigned row = 0; row < rowCount; row++) {
00716         wxString email = m_pGrid->GetCellValue(row, col);
00717         email.Trim(true);
00718         email.Trim(false);
00719 
00720         // check for valid emails
00721         unsigned atcount = 0;
00722         unsigned atposition = 0;
00723         if (email.Length() < 3) continue;
00724         for (unsigned i = 0; i < email.Length(); i++) {
00725             wxChar c = email[i];
00726             if (!validChars[c]) continue;
00727             if (c == '@') {
00728                 atcount++;
00729                 atposition = i;
00730                 if (atcount > 1) continue;
00731             }
00732         }
00733         if (atcount != 1) continue;
00734         if (atposition == 0 || atposition == email.Length() - 1) continue;
00735 
00736         // write email
00737         ostr << "\t\t" << email << "\n";
00738         count++;
00739     }
00740 
00741     // show number of exported emails
00742     wxString msg;
00743     msg << "Es wurden " << count << " eMails exportiert.";
00744     wxMessageBox(msg, "Address Manager", wxICON_INFORMATION | wxOK, this);
00745 }
00746 
00747 void AddressManagerFrame::OnMenuExportSMS(wxCommandEvent& event)
00748 {
00749     // save current edit cell, if neccessary
00750     m_pGrid->DisableCellEditControl();
00751 
00752     wxMessageBox("Noch nicht implementiert.", "Address Manager", wxICON_INFORMATION | wxOK, this);
00753 }
00754 
00755 void AddressManagerFrame::OnMenuExportFax(wxCommandEvent& event)
00756 {
00757     // save current edit cell, if neccessary
00758     m_pGrid->DisableCellEditControl();
00759 
00760     wxMessageBox("Noch nicht implementiert.", "Address Manager", wxICON_INFORMATION | wxOK, this);
00761 }
00762 
00763 // Ensure that the grid's edit control always has the focus.
00764 void AddressManagerFrame::OnActivate(wxActivateEvent& event)
00765 {
00766     if (m_pGrid) m_pGrid->OnActivate(event.GetActive());
00767 }
00768 
00769 // hack: enable scrollbars, if neccessary
00770 void AddressManagerFrame::OnSize( wxSizeEvent &event )
00771 {
00772     wxFrame::OnSize(event);
00773     if (m_pGrid) {
00774         m_pGrid->UpdateDimensions();
00775         m_pGrid->Refresh();
00776     }
00777 }
00778 
00779 
00780 void AddressManagerFrame::OnClose(wxCloseEvent& event)
00781 {
00782     // save current edit cell, if neccessary
00783     m_pGrid->DisableCellEditControl();
00784 
00785     // avoid if we can
00786     if (event.CanVeto()) {
00787         if (!onExit()) {
00788             event.Veto();
00789         } else {
00790             Destroy();
00791         }
00792     } else {
00793         Destroy();
00794     }
00795 }
00796 
00797 void AddressManagerFrame::setDirty()
00798 {
00799     m_dirty = true;
00800 }
00801 

Generated on Thu May 2 15:13:51 2002 for Wino by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001