| 1 |
// -*- C++ -*- generated by wxGlade 0.3.5.1 on Wed Jun 29 12:57:38 2005 |
| 2 |
|
| 3 |
#include "mnID.h" |
| 4 |
#include "mnDef.h" |
| 5 |
#include "mnController.h" |
| 6 |
#include <wx/config.h> |
| 7 |
#include <wx/tokenzr.h> |
| 8 |
#include <wx/utils.h> |
| 9 |
|
| 10 |
BEGIN_EVENT_TABLE(mnController, wxEvtHandler) |
| 11 |
EVT_TEXT_ENTER(ID_SearchTextCtrl, mnController::handleSearch) |
| 12 |
EVT_TREE_SEL_CHANGED(ID_SearchTree, mnController::handleTreeItemSelect) |
| 13 |
EVT_TREE_SEL_CHANGING(ID_SearchTree, mnController::handleTreeItemSelecting) |
| 14 |
EVT_TREE_BEGIN_LABEL_EDIT(ID_SearchTree, mnController::handleBeginLabelEdit) |
| 15 |
EVT_TREE_END_LABEL_EDIT(ID_SearchTree, mnController::handleEndLabelEdit) |
| 16 |
EVT_MENU(ID_MenuFileNew, mnController::handleNewButton) |
| 17 |
EVT_MENU(ID_MenuFileRemove, mnController::handleRemoveButton) |
| 18 |
EVT_MENU(ID_MenuFileEdit, mnController::handleEditButton) |
| 19 |
EVT_MENU(ID_MenuFileExit, mnController::handleExit) |
| 20 |
EVT_MENU(ID_MenuFileConfig, mnController::handleConfig) |
| 21 |
EVT_MENU(wxID_ABOUT, mnController::handleAbout) |
| 22 |
EVT_CLOSE(mnController::handleClose) |
| 23 |
EVT_COMMAND(ID_HtmlTextCtrl, mnEVT_LINK_CLICK, mnController::handleLinkClick) |
| 24 |
END_EVENT_TABLE() |
| 25 |
|
| 26 |
mnController::mnController(mnFrame* inframe) |
| 27 |
{ |
| 28 |
wxString* dirName = new wxString(); |
| 29 |
wxString gotDirName; |
| 30 |
wxDirDialog* dlg; |
| 31 |
|
| 32 |
frame = inframe; |
| 33 |
|
| 34 |
/* create wiki model */ |
| 35 |
wxConfig* config = new wxConfig(wxT(CONF_APP_NAME)); |
| 36 |
if(config->Read(wxT(CONF_WIKI_DIR), dirName) == FALSE) { |
| 37 |
delete dirName; |
| 38 |
dlg = new wxDirDialog(frame); |
| 39 |
if(dlg->ShowModal() == wxID_OK) { |
| 40 |
gotDirName = dlg->GetPath(); |
| 41 |
wiki = new mnModel(gotDirName.mb_str()); |
| 42 |
config->Write(wxT(CONF_WIKI_DIR), gotDirName.c_str()); |
| 43 |
} |
| 44 |
else { |
| 45 |
frame->Close(); |
| 46 |
} |
| 47 |
} |
| 48 |
else { |
| 49 |
wiki = new mnModel(dirName->mb_str()); |
| 50 |
} |
| 51 |
|
| 52 |
delete config; |
| 53 |
} |
| 54 |
|
| 55 |
void mnController::handleConfig(wxCommandEvent& event) |
| 56 |
{ |
| 57 |
wxDirDialog* dlg; |
| 58 |
wxString gotDirName; |
| 59 |
wxString* dirName = new wxString(); |
| 60 |
|
| 61 |
wxConfig* config = new wxConfig(wxT(CONF_APP_NAME)); |
| 62 |
|
| 63 |
if(config->Read(wxT(CONF_WIKI_DIR), dirName)) { |
| 64 |
if(wxMessageBox(*dirName, wxT("Change data directory?"), wxYES_NO|wxICON_QUESTION) == wxNO){ |
| 65 |
delete config; |
| 66 |
return; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
dlg = new wxDirDialog(frame); |
| 71 |
if(dlg->ShowModal() == wxID_OK) { |
| 72 |
gotDirName = dlg->GetPath(); |
| 73 |
delete wiki; |
| 74 |
wiki = new mnModel(gotDirName.mb_str()); |
| 75 |
config->Write(wxT(CONF_WIKI_DIR), gotDirName.c_str()); |
| 76 |
} |
| 77 |
delete config; |
| 78 |
|
| 79 |
frame->showSearchResult(wiki); |
| 80 |
} |
| 81 |
|
| 82 |
void mnController::handleSearch(wxCommandEvent& event) |
| 83 |
{ |
| 84 |
wxString* str = new wxString(event.GetString()); |
| 85 |
WikiList* wikiList; |
| 86 |
WikiList::Node* node; |
| 87 |
|
| 88 |
wikiList = wiki->search(str->mb_str()); |
| 89 |
|
| 90 |
node = wikiList->GetFirst(); |
| 91 |
if(!node) { |
| 92 |
wxLogMessage(wxT("Not Found! [%s]"), (const char*)str->c_str()); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
wiki->addSearchStr(str); |
| 97 |
frame->showSearchResult(wiki); |
| 98 |
} |
| 99 |
|
| 100 |
void mnController::handleNewButton(wxCommandEvent& event) |
| 101 |
{ |
| 102 |
int ans; |
| 103 |
WikiData* wikiData = wiki->newWikiData(); |
| 104 |
wxString plainTextStr; |
| 105 |
wxString newSubjectStr; |
| 106 |
mnWikiDataEntryDialog* dlg = new mnWikiDataEntryDialog(frame, -1, wxT("Malon Note")); |
| 107 |
const wxString* wikiText = wikiData->getText(); |
| 108 |
const wxString* subject = wikiData->getSubject(); |
| 109 |
|
| 110 |
dlg->setSubject(*subject); |
| 111 |
dlg->setPlainText(*wikiText); |
| 112 |
ans = dlg->ShowModal(); |
| 113 |
|
| 114 |
if(ans != wxID_OK) return ; |
| 115 |
|
| 116 |
plainTextStr = dlg->getPlainText(); |
| 117 |
newSubjectStr = dlg->getSubject(); |
| 118 |
wikiData->modText(&plainTextStr); |
| 119 |
wikiData->save(); |
| 120 |
|
| 121 |
wikiData->modSubject(&newSubjectStr); |
| 122 |
|
| 123 |
frame->showSearchResult(wiki); |
| 124 |
delete dlg; |
| 125 |
} |
| 126 |
|
| 127 |
void mnController::handleRemoveButton(wxCommandEvent& event) |
| 128 |
{ |
| 129 |
wxTreeItemData* item = frame->getSelectedItem(); |
| 130 |
|
| 131 |
if(item){ |
| 132 |
((WikiData*)(item))->removeDataFile(); |
| 133 |
} |
| 134 |
else { |
| 135 |
wiki->removeSearchStr(frame->getSelectedLabel()); |
| 136 |
} |
| 137 |
frame->showSearchResult(wiki); |
| 138 |
} |
| 139 |
|
| 140 |
void mnController::handleTreeItemSelect(wxTreeEvent& event) |
| 141 |
{ |
| 142 |
frame->showSelectedItemText(event); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
void mnController::handleTreeItemSelecting(wxTreeEvent& event) |
| 147 |
{ |
| 148 |
} |
| 149 |
|
| 150 |
void mnController::handleBeginLabelEdit(wxTreeEvent& event) |
| 151 |
{ |
| 152 |
if(!frame->isEditableTreeLabel(event.GetItem())) event.Veto(); |
| 153 |
} |
| 154 |
|
| 155 |
void mnController::handleEndLabelEdit(wxTreeEvent& event) |
| 156 |
{ |
| 157 |
wxString newSubject = event.GetLabel(); |
| 158 |
wxTreeItemData* item = frame->getSelectedItem(); |
| 159 |
|
| 160 |
if(item == NULL) return ; |
| 161 |
|
| 162 |
if(newSubject.Len() == 0) { |
| 163 |
event.Veto(); |
| 164 |
frame->showSearchResult(wiki); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
((WikiData*)(item))->modSubject(&newSubject); |
| 169 |
|
| 170 |
event.Veto(); |
| 171 |
frame->showSearchResult(wiki); |
| 172 |
} |
| 173 |
|
| 174 |
void mnController::handleClose(wxCloseEvent& event) |
| 175 |
{ |
| 176 |
frame->Destroy(); |
| 177 |
} |
| 178 |
|
| 179 |
void mnController::handleExit(wxCommandEvent& event) |
| 180 |
{ |
| 181 |
frame->Close(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void mnController::handleAbout(wxCommandEvent& event) |
| 186 |
{ |
| 187 |
wxMessageBox(wxT(MN_APP_VERSION), wxT("Abount MalonNote"), wxOK|wxICON_INFORMATION); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
void mnController::handleEditButton(wxCommandEvent& event) |
| 192 |
{ |
| 193 |
wxString plainTextStr; |
| 194 |
wxString newSubjectStr; |
| 195 |
wxTreeItemData* itemData = frame->getSelectedItem(); |
| 196 |
wxString subject = frame->getSelectedLabel(); |
| 197 |
mnWikiDataEntryDialog* dlg = new mnWikiDataEntryDialog(frame, -1, wxT("Malon Note")); |
| 198 |
int ans; |
| 199 |
|
| 200 |
if(itemData == NULL) return; |
| 201 |
const wxString* wikiText = ((WikiData*)(itemData))->getText(); |
| 202 |
|
| 203 |
dlg->setSubject(subject); |
| 204 |
dlg->setPlainText(*wikiText); |
| 205 |
ans = dlg->ShowModal(); |
| 206 |
|
| 207 |
if(ans != wxID_OK) return ; |
| 208 |
if(itemData == NULL) return ; |
| 209 |
|
| 210 |
plainTextStr = dlg->getPlainText(); |
| 211 |
newSubjectStr = dlg->getSubject(); |
| 212 |
((WikiData*)(itemData))->modText(&plainTextStr); |
| 213 |
((WikiData*)(itemData))->save(); |
| 214 |
|
| 215 |
((WikiData*)(itemData))->modSubject(&newSubjectStr); |
| 216 |
|
| 217 |
frame->showSearchResult(wiki); |
| 218 |
delete dlg; |
| 219 |
} |
| 220 |
|
| 221 |
void mnController::handleLinkClick(wxCommandEvent &event) |
| 222 |
{ |
| 223 |
const wxString* wikiDataDir; |
| 224 |
wxString href = event.GetString(); |
| 225 |
wxString tag; |
| 226 |
wxString val; |
| 227 |
wxString doc; |
| 228 |
wxStringTokenizer tkz(href, wxT(":")); |
| 229 |
|
| 230 |
tag = tkz.GetNextToken(); |
| 231 |
val = tkz.GetNextToken(); |
| 232 |
if(tag.CmpNoCase(wxT("mnlink")) == 0) { |
| 233 |
event.SetString(val); |
| 234 |
handleSearch(event); |
| 235 |
} |
| 236 |
else if(tag.CmpNoCase(wxT("mndoc")) == 0) { |
| 237 |
wikiDataDir = wiki->getWikiDataDir(); |
| 238 |
doc = *wikiDataDir + wxT("/docs/") + val; |
| 239 |
openDoc(doc); |
| 240 |
} |
| 241 |
else { |
| 242 |
openUrl(href); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
void mnController::openUrl(wxString& url) |
| 247 |
{ |
| 248 |
wxString cmd; |
| 249 |
|
| 250 |
#ifdef __WXMSW__ |
| 251 |
cmd.sprintf(wxT("explorer %s"), url.c_str()); |
| 252 |
#endif |
| 253 |
|
| 254 |
#ifdef __WXMAC__ |
| 255 |
cmd.sprintf(wxT("open %s"), url.c_str()); |
| 256 |
#endif |
| 257 |
|
| 258 |
#ifdef __WXCOCOA__ |
| 259 |
cmd.sprintf(wxT("open %s"), url.c_str()); |
| 260 |
#endif |
| 261 |
|
| 262 |
#ifdef __WXGTK__ |
| 263 |
wxLogMessage(wxT("Ooops!, I don't know how to launch %s"), url.c_str()); |
| 264 |
return; |
| 265 |
#endif |
| 266 |
|
| 267 |
//wxExecute(argv); |
| 268 |
wxExecute(cmd); |
| 269 |
} |
| 270 |
|
| 271 |
void mnController::openDoc(wxString& doc) |
| 272 |
{ |
| 273 |
wxString cmd; |
| 274 |
|
| 275 |
#ifdef __WXMSW__ |
| 276 |
cmd.sprintf(wxT("cmd /c \"start %s\""), doc.c_str()); |
| 277 |
#endif |
| 278 |
|
| 279 |
#ifdef __WXMAC__ |
| 280 |
cmd.sprintf(wxT("open %s"), doc.c_str()); |
| 281 |
#endif |
| 282 |
|
| 283 |
#ifdef __WXCOCOA__ |
| 284 |
cmd.sprintf(wxT("open %s"), doc.c_str()); |
| 285 |
#endif |
| 286 |
|
| 287 |
#ifdef __WXGTK__ |
| 288 |
wxLogMessage(wxT("Ooops!, I don't know how to launch %s"), doc.c_str()); |
| 289 |
return; |
| 290 |
#endif |
| 291 |
|
| 292 |
//wxExecute(argv); |
| 293 |
wxExecute(cmd); |
| 294 |
} |