| 1 |
// -*- C++ -*- generated by wxGlade 0.3.5.1 on Wed Jun 29 12:57:38 2005 |
| 2 |
#include <wx/regex.h> |
| 3 |
#include <wx/tokenzr.h> |
| 4 |
#include <wx/xrc/xmlres.h> |
| 5 |
#include "mnID.h" |
| 6 |
#include "mnDef.h" |
| 7 |
#include "mnNotePanel.h" |
| 8 |
#include "mnDialog.h" |
| 9 |
#include "mnNotePanelController.h" |
| 10 |
|
| 11 |
mnNotePanel::mnNotePanel(wxWindow* parent, wxString* dirName) |
| 12 |
{ |
| 13 |
wxXmlResource::Get()->LoadPanel(this, parent, wxT("notePanel")); |
| 14 |
searchTree = XRCCTRL(*this, "searchTree", wxTreeCtrl); |
| 15 |
|
| 16 |
#ifdef __WXMAC__ |
| 17 |
wxSplitterWindow* spWindow = XRCCTRL(*this, "splitWindow", wxSplitterWindow); |
| 18 |
wikiHtmlTextCtrl = new mnHtmlWindow(spWindow, -1, wxT(""), wxDefaultPosition, wxSize(200, 200)); |
| 19 |
spWindow->SplitVertically(searchTree, wikiHtmlTextCtrl, 250); |
| 20 |
#else |
| 21 |
/* init members */ |
| 22 |
wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); |
| 23 |
wikiHtmlTextCtrl = XRCCTRL(*this, "wikiHtmlTextCtrl", mnHtmlWindow); |
| 24 |
wikiHtmlTextCtrl->SetFonts(font.GetFaceName(), font.GetFaceName()); |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
/* create search tree */ |
| 29 |
searchItemRoot = searchTree->AddRoot(wxT(LABEL_ITEM_TREE), -1, -1, NULL); |
| 30 |
|
| 31 |
/* create controller, and push as event handler */ |
| 32 |
panelController = new mnNotePanelController(this, dirName); |
| 33 |
PushEventHandler(panelController); |
| 34 |
} |
| 35 |
|
| 36 |
mnNotePanelController* mnNotePanel::getController() |
| 37 |
{ |
| 38 |
return panelController; |
| 39 |
} |
| 40 |
|
| 41 |
void mnNotePanel::updateSearchResult(mnModel* wiki, wxString* searchStr, bool isExpand) |
| 42 |
{ |
| 43 |
wxTreeItemIdValue cookie1, cookie2; |
| 44 |
wxTreeItemId searchStrId; |
| 45 |
wxTreeItemId wikiDataId; |
| 46 |
const WikiList* wikiList; |
| 47 |
WikiList::Node* node; |
| 48 |
WikiData* data; |
| 49 |
const wxString* subject; |
| 50 |
|
| 51 |
memset(&cookie1, 1, sizeof(cookie1)); |
| 52 |
memset(&cookie2, 2, sizeof(cookie2)); |
| 53 |
searchStrId = searchTree->GetFirstChild(searchItemRoot, cookie1); |
| 54 |
while(searchStrId.IsOk()) { |
| 55 |
if(searchTree->GetItemText(searchStrId) == *searchStr) { |
| 56 |
wikiDataId = searchTree->GetFirstChild(searchStrId, cookie2); |
| 57 |
while(wikiDataId.IsOk()) { |
| 58 |
searchTree->SetItemData(wikiDataId, NULL); |
| 59 |
wikiDataId = searchTree->GetNextChild(searchStrId, cookie2); |
| 60 |
} |
| 61 |
searchTree->DeleteChildren(searchStrId); |
| 62 |
break; |
| 63 |
} |
| 64 |
searchStrId = searchTree->GetNextChild(searchItemRoot, cookie1); |
| 65 |
} |
| 66 |
|
| 67 |
wikiList = wiki->getSearchResultList(searchStr); |
| 68 |
if(wikiList == NULL) { |
| 69 |
MN_FATAL_ERROR(wxT("wikiList is null")); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
node = wikiList->GetFirst(); |
| 74 |
while(node) { |
| 75 |
data = node->GetData(); |
| 76 |
subject = data->getSubject(); |
| 77 |
searchTree->AppendItem(searchStrId, *subject, -1, -1, (wxTreeItemData*)data); |
| 78 |
node = node->GetNext(); |
| 79 |
} |
| 80 |
if(isExpand){ |
| 81 |
searchTree->Expand(searchStrId); |
| 82 |
} |
| 83 |
else { |
| 84 |
searchTree->Collapse(searchStrId); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
void mnNotePanel::addSearchResult(mnModel* wiki, wxString* searchStr, bool isExpand) |
| 90 |
{ |
| 91 |
wxTreeItemId searchItem; |
| 92 |
const WikiList* wikiList; |
| 93 |
WikiList::Node* node; |
| 94 |
WikiData* data; |
| 95 |
const wxString* subject; |
| 96 |
|
| 97 |
wikiList = wiki->getSearchResultList(searchStr); |
| 98 |
if(wikiList == NULL) { |
| 99 |
MN_FATAL_ERROR(wxT("wikiList is null")); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
//searchItem = searchTree->AppendItem(searchItemRoot, *searchStr, -1, -1, NULL); |
| 104 |
searchItem = searchTree->InsertItem(searchItemRoot, 0, *searchStr, -1, -1, NULL); |
| 105 |
node = wikiList->GetFirst(); |
| 106 |
while(node) { |
| 107 |
data = node->GetData(); |
| 108 |
subject = data->getSubject(); |
| 109 |
searchTree->AppendItem(searchItem, *subject, -1, -1, (wxTreeItemData*)data); |
| 110 |
node = node->GetNext(); |
| 111 |
} |
| 112 |
|
| 113 |
searchTree->Expand(searchItemRoot); |
| 114 |
if(isExpand){ |
| 115 |
searchTree->Expand(searchItem); |
| 116 |
} |
| 117 |
else { |
| 118 |
searchTree->Collapse(searchItem); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void mnNotePanel::makeHtml(WikiData* wikiData, bool isHighlight) |
| 123 |
{ |
| 124 |
const wxString* fileName = wikiData->getFileName(); |
| 125 |
const wxString* wikiText = wikiData->getText(); |
| 126 |
wxString htmlText(*wikiText); |
| 127 |
|
| 128 |
/* escape < */ |
| 129 |
wxRegEx exp01(wxT("<"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 130 |
exp01.Replace(&htmlText, wxT("\\< ")); |
| 131 |
|
| 132 |
/* escape > */ |
| 133 |
wxRegEx exp02(wxT(">"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 134 |
exp02.Replace(&htmlText, wxT("\\> ")); |
| 135 |
|
| 136 |
/* http:// */ |
| 137 |
wxRegEx exp2(wxT("((http|https|ftp)://[-_.!~*'a-zA-Z0-9;/?:@&=+$,%#]+)"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 138 |
exp2.Replace(&htmlText, wxT("<a href=\\1>\\1</a>")); |
| 139 |
|
| 140 |
/* [[ ]] */ |
| 141 |
wxRegEx exp1(wxT("\\[\\[([^]]+)\\]\\]")); |
| 142 |
exp1.Replace(&htmlText, wxT("<a href=\"" MNLINK "\\1\">\\1</a>")); |
| 143 |
|
| 144 |
/* file:// */ |
| 145 |
wxRegEx exp3(wxT("(file://[^ ]+)"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 146 |
exp3.Replace(&htmlText, wxT("<a href=\\1>\\1</a>")); |
| 147 |
|
| 148 |
/* **XXXX */ |
| 149 |
wxRegEx exp5(wxT("^\\*\\*([^*]+)"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 150 |
#ifdef __WXMAC__ |
| 151 |
exp5.Replace(&htmlText, wxT("<TABLE WIDTH=\"100%\" CELLPADDING=2 CELLSPACING=1 BORDER=0><TR><TD BGCOLOR=\"#CCFFFF\"><FONT SIZE=3><B>\\1</B></FONT></TD></TR></TABLE>")); |
| 152 |
#else |
| 153 |
exp5.Replace(&htmlText, wxT("<TABLE WIDTH=\"100%\" CELLPADDING=2 CELLSPACING=1 BORDER=0><TR><TD BGCOLOR=\"#CCFFFF\"><FONT SIZE=3><B>\\1</B></FONT></TD></TR></TABLE><BR>")); |
| 154 |
#endif |
| 155 |
|
| 156 |
/* *XXXX */ |
| 157 |
wxRegEx exp4(wxT("^\\*([^*]+)"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 158 |
#ifdef __WXMAC__ |
| 159 |
exp4.Replace(&htmlText, wxT("<TABLE WIDTH=\"100%\" CELLPADDING=5 CELLSPACING=2 BORDER=1><TR><TD BGCOLOR=\"#CCFFFF\"><FONT SIZE=4><B>\\1</B></FONT></TD></TR></TABLE>")); |
| 160 |
#else |
| 161 |
exp4.Replace(&htmlText, wxT("<TABLE WIDTH=\"100%\" CELLPADDING=5 CELLSPACING=2 BORDER=1><TR><TD BGCOLOR=\"#CCFFFF\"><FONT SIZE=5><B>\\1</B></FONT></TD></TR></TABLE><BR>")); |
| 162 |
#endif |
| 163 |
|
| 164 |
/* :''XXXX'': */ |
| 165 |
wxRegEx exp6(wxT("^:''(.+)'':(.+)"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 166 |
exp6.Replace(&htmlText, wxT("<u><b>\\1:\\2</b></u>")); |
| 167 |
|
| 168 |
/* ---- */ |
| 169 |
wxRegEx exp7(wxT("^----.*"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 170 |
exp7.Replace(&htmlText, wxT("<hr>")); |
| 171 |
|
| 172 |
/* {{ }} */ |
| 173 |
wxRegEx exp8(wxT("\\{\\{([^\\}]+)\\}\\}")); |
| 174 |
exp8.Replace(&htmlText, wxT("<a href=\"" MNDOC "\\1\">\\1</a>")); |
| 175 |
|
| 176 |
/* table */ |
| 177 |
wxRegEx exp10(wxT("^,([^,]+)"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 178 |
exp10.Replace(&htmlText, wxT("<td> \\1 </td>")); |
| 179 |
for(int i = 0; i < 10; i++){ // It's not good. :P |
| 180 |
wxRegEx exp16(wxT("</td>,([^,\n]+)"), wxRE_DEFAULT); |
| 181 |
exp16.Replace(&htmlText, wxT("</td><td>\\1</td>")); |
| 182 |
} |
| 183 |
wxRegEx exp11(wxT("^<td>"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 184 |
exp11.Replace(&htmlText, wxT("<tr><td>")); |
| 185 |
wxRegEx exp12(wxT("</td>$"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 186 |
exp12.Replace(&htmlText, wxT("</td></tr>")); |
| 187 |
wxRegEx exp15(wxT("</tr>\n")); |
| 188 |
exp15.Replace(&htmlText, wxT("</tr>")); |
| 189 |
|
| 190 |
wxRegEx exp13(wxT("^<tr>"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 191 |
exp13.Replace(&htmlText, wxT("<table border><tr>")); |
| 192 |
wxRegEx exp14(wxT("</tr>$"), wxRE_NEWLINE|wxRE_DEFAULT); |
| 193 |
exp14.Replace(&htmlText, wxT("</tr></table>")); |
| 194 |
|
| 195 |
|
| 196 |
/* Highlight search text */ |
| 197 |
highlight = isHighlight; |
| 198 |
if(isHighlight) { |
| 199 |
wxTreeItemId parentId = searchTree->GetItemParent(searchTree->GetSelection()); |
| 200 |
wxString searchWords = searchTree->GetItemText(parentId); |
| 201 |
wxStringTokenizer tkz(searchWords, wxT(" ")); |
| 202 |
while ( tkz.HasMoreTokens() ) |
| 203 |
{ |
| 204 |
wxString token = tkz.GetNextToken(); |
| 205 |
wxRegEx exp9(wxT("(?!<[^>]+)(") + token + wxT(")(?![^<]+>)"), wxRE_ADVANCED|wxRE_ICASE|wxRE_NEWLINE); |
| 206 |
exp9.Replace(&htmlText, wxT("<b><font color=\"red\">\\1</font></b>")); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/* make HTML document */ |
| 211 |
wxString wikiHtml; |
| 212 |
wikiHtml.Append(wxT("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=")); |
| 213 |
wikiHtml.Append(wxT(CODE_SET_SYSTEM)); |
| 214 |
wikiHtml.Append(wxT("\">")); |
| 215 |
wikiHtml.Append(wxT("<pre>")); |
| 216 |
wikiHtml.Append(wxT("<html>")); |
| 217 |
wikiHtml.Append(htmlText.c_str()); |
| 218 |
wikiHtml.Append(wxT("</pre>")); |
| 219 |
wikiHtml.Append(wxT("</html>")); |
| 220 |
|
| 221 |
wikiHtmlTextCtrl->SetPage(wikiHtml); |
| 222 |
} |
| 223 |
|
| 224 |
void mnNotePanel::showSelectedItemText(wxTreeEvent& event) |
| 225 |
{ |
| 226 |
wxTreeItemId id; |
| 227 |
wxTreeItemData* item; |
| 228 |
|
| 229 |
id = searchTree->GetSelection(); |
| 230 |
item = searchTree->GetItemData(id); |
| 231 |
|
| 232 |
if(!id.IsOk() || !isEditableTreeLabel(id) || item == NULL) { /* selection is not memo data */ |
| 233 |
wikiHtmlTextCtrl->SetPage(wxT("<html></html>")); |
| 234 |
return; |
| 235 |
} |
| 236 |
makeHtml((WikiData*)item, false); |
| 237 |
} |
| 238 |
|
| 239 |
wxTreeItemId mnNotePanel::getSelection() |
| 240 |
{ |
| 241 |
return searchTree->GetSelection(); |
| 242 |
} |
| 243 |
|
| 244 |
void mnNotePanel::highlightSelectedItem() |
| 245 |
{ |
| 246 |
wxTreeItemData* item = getSelectedItem(); |
| 247 |
|
| 248 |
if(!isEditableTreeLabel(searchTree->GetSelection())) { /* selection is not memo data */ |
| 249 |
wikiHtmlTextCtrl->SetPage(wxT("")); |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
if(highlight){ |
| 254 |
makeHtml((WikiData*)item, false); |
| 255 |
} |
| 256 |
else { |
| 257 |
makeHtml((WikiData*)item, true); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
void mnNotePanel::expandAllTree() |
| 263 |
{ |
| 264 |
wxTreeItemIdValue cookie; |
| 265 |
wxTreeItemId itemId; |
| 266 |
|
| 267 |
memset(&cookie, 1, sizeof(cookie)); |
| 268 |
itemId = searchTree->GetFirstChild(searchItemRoot, cookie); |
| 269 |
while(itemId.IsOk()) { |
| 270 |
searchTree->Expand(itemId); |
| 271 |
itemId = searchTree->GetNextChild(searchItemRoot, cookie); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
void mnNotePanel::collapseAllTree() |
| 276 |
{ |
| 277 |
wxTreeItemIdValue cookie; |
| 278 |
wxTreeItemId itemId; |
| 279 |
|
| 280 |
memset(&cookie, 1, sizeof(cookie)); |
| 281 |
itemId = searchTree->GetFirstChild(searchItemRoot, cookie); |
| 282 |
while(itemId.IsOk()) { |
| 283 |
searchTree->Collapse(itemId); |
| 284 |
itemId = searchTree->GetNextChild(searchItemRoot, cookie); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
void mnNotePanel::clearAllTree() |
| 289 |
{ |
| 290 |
wxTreeItemIdValue cookie1, cookie2; |
| 291 |
wxTreeItemId searchStrId; |
| 292 |
wxTreeItemId wikiDataId; |
| 293 |
|
| 294 |
memset(&cookie1, 1, sizeof(cookie1)); |
| 295 |
memset(&cookie2, 2, sizeof(cookie2)); |
| 296 |
searchStrId = searchTree->GetFirstChild(searchItemRoot, cookie1); |
| 297 |
while(searchStrId.IsOk()) { |
| 298 |
wikiDataId = searchTree->GetFirstChild(searchStrId, cookie2); |
| 299 |
while(wikiDataId.IsOk()) { |
| 300 |
searchTree->SetItemData(wikiDataId, NULL); |
| 301 |
wikiDataId = searchTree->GetNextChild(searchStrId, cookie2); |
| 302 |
} |
| 303 |
searchStrId = searchTree->GetNextChild(searchItemRoot, cookie1); |
| 304 |
} |
| 305 |
|
| 306 |
searchTree->DeleteChildren(searchItemRoot); |
| 307 |
} |
| 308 |
|
| 309 |
void mnNotePanel::removeSelectedItem() |
| 310 |
{ |
| 311 |
searchTree->Delete(searchTree->GetSelection()); |
| 312 |
} |
| 313 |
|
| 314 |
wxTreeItemData* mnNotePanel::getSelectedItem() |
| 315 |
{ |
| 316 |
return searchTree->GetItemData(searchTree->GetSelection()); |
| 317 |
} |
| 318 |
|
| 319 |
wxString mnNotePanel::getSelectedLabel() |
| 320 |
{ |
| 321 |
return searchTree->GetItemText(searchTree->GetSelection()); |
| 322 |
} |
| 323 |
|
| 324 |
bool mnNotePanel::isEditableTreeLabel(wxTreeItemId id) |
| 325 |
{ |
| 326 |
if(id.IsOk()){ |
| 327 |
return !(searchTree->GetItemParent(id) == searchItemRoot || id == searchItemRoot); |
| 328 |
} |
| 329 |
else { |
| 330 |
return false; |
| 331 |
} |
| 332 |
} |
| 333 |
|