source: dev/ProjectEditor/Search.cpp@ 49

Last change on this file since 49 was 24, checked in by dai_9181, 18 years ago

保存されていないドキュメントのタブに(*)をつける機能に対応。
MDITEXTEDITINFOをCMdiTextEditに変更。今後、オブジェクト指向化を進める。

File size: 14.3 KB
Line 
1#include "common.h"
2
3long *pFindPos;
4int FindPosCounter;
5
6char *CompareSuper(HWND hDlg,char *buffer,char *szFind,BOOL IsBigSmall,BOOL IsRegExp,int *pLength){
7 char *pTemp;
8
9 if(buffer[0]=='\0') return 0;
10
11 if(IsRegExp){
12 //正規表現を有効にしながら検索
13 pTemp=obj_RegExp.compare(hDlg,buffer,szFind,IsBigSmall,pLength);
14 }
15 else{
16 //通常の検索
17 pTemp=ComparisonString(buffer,szFind,IsBigSmall);
18
19 *pLength=lstrlen(szFind);
20 }
21
22 return pTemp;
23}
24
25void SetSearchData(HWND hDlg,char *str,_int8 IsBigSmall){
26 extern HANDLE hHeap;
27 extern MDIINFO MdiInfo[MAX_WNDNUM];
28 int i,i2,i3,i4,i5,length;
29 int WndNum;
30 char *buffer,*FindStr,*LineStr;
31 HWND hEdit;
32
33 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
34 if(WndNum==-1) return;
35 hEdit=GetWindow(GetWindow(hClient,GW_CHILD),GW_CHILD);
36
37 i=lstrlen(MdiInfo[WndNum].pMdiTextEdit->buffer)+1;
38
39 buffer=(char *)HeapAlloc(hHeap,0,i);
40 lstrcpy(buffer,MdiInfo[WndNum].pMdiTextEdit->buffer);
41
42 LineStr=(char *)HeapAlloc(hHeap,0,i+64);
43
44 pFindPos[0]=(long)hEdit;
45 i=0;
46 i3=1;
47 while(1){
48 FindStr=CompareSuper(hDlg,buffer+i,str,IsBigSmall,pobj_nv->bRegExp,&length);
49 if(FindStr==(char *)-1){
50 //エラー
51 return;
52 }
53 if(!FindStr) break;
54
55 i=FindStr-buffer;
56 for(i4=0,i5=1;i>i4;i4++){
57 if(buffer[i4-1]=='\r'&&buffer[i4]=='\n') i5++;
58 }
59 for(;i>0;i--){
60 if(buffer[i-2]=='\r'&&buffer[i-1]=='\n') break;
61 }
62
63 FindPosCounter+=sizeof(long);
64 pFindPos=(long *)HeapReAlloc(hHeap,0,pFindPos,FindPosCounter);
65 pFindPos[i3]=i;
66
67 sprintf(LineStr,"Line:%d ",i5);
68 for(i2=lstrlen(LineStr);;i++,i2++){
69 if((buffer[i]=='\r'&&buffer[i+1]=='\n')||buffer[i]=='\0'){
70 LineStr[i2]=0;
71 break;
72 }
73 LineStr[i2]=buffer[i];
74 }
75 SendDlgItemMessage(hDlg,IDC_FINDLIST,LB_ADDSTRING,0,(long)LineStr);
76 i3++;
77 }
78 HeapDefaultFree(buffer);
79 HeapDefaultFree(LineStr);
80}
81BOOL CALLBACK DlgFindAll(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
82 extern MDIINFO MdiInfo[MAX_WNDNUM];
83 int i,WndNum;
84
85 switch(message){
86 case WM_INITDIALOG:
87 SetPosCenter(hwnd);
88 break;
89 case WM_COMMAND:
90 switch(LOWORD(wParam)){
91 case IDC_SHOWITEM:
92 i=SendDlgItemMessage(hwnd,IDC_FINDLIST,LB_GETCURSEL,0,0);
93 if(i==LB_ERR) return 1;
94 i++;
95
96 if(!IsWindow((HWND)pFindPos[0])){
97 MessageBeep(0);
98 return 1;
99 }
100
101 WndNum=GetWndNum(GetParent((HWND)pFindPos[0]));
102 if(WndNum==-1) return 1;
103
104 CHARRANGE CharRange;
105 CharRange.cpMin=pFindPos[i];
106
107 char *pBuf;
108 pBuf=MdiInfo[WndNum].pMdiTextEdit->buffer;
109
110 //行の末端位置を取得
111 for(i=CharRange.cpMin;;i++){
112 if(pBuf[i]=='\0') break;
113 if(pBuf[i]=='\r'&&pBuf[i+1]=='\n') break;
114 }
115 CharRange.cpMax=i;
116
117 TextEdit_SetSel(WndNum,CharRange.cpMin,CharRange.cpMax,TRUE);
118
119 SetFocus((HWND)pFindPos[0]);
120 return 1;
121 case IDCANCEL:
122 HeapDefaultFree(pFindPos);
123 EndDialog(hwnd,0);
124 return 1;
125 case IDC_FINDLIST:
126 if(HIWORD(wParam)==LBN_DBLCLK) SendMessage(hwnd,WM_COMMAND,IDC_SHOWITEM,0);
127 return 1;
128 }
129 break;
130 case WM_SIZE:
131 RECT rect;
132 GetClientRect(hwnd,&rect);
133 MoveWindow(GetDlgItem(hwnd,IDC_FINDLIST),rect.left,26,rect.right,rect.bottom-26,1);
134 return 1;
135 }
136 return 0;
137}
138BOOL StartSearch(HWND hDlg,char *FindString,_int8 IsBigSmall,BOOL IsShowMessage){
139 extern MDIINFO MdiInfo[MAX_WNDNUM];
140 int i;
141 char *pBuf,msg[255];
142 CHARRANGE CharRange;
143
144 int WndNum;
145 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
146 if(WndNum==-1) return 0;
147
148 pBuf=MdiInfo[WndNum].pMdiTextEdit->buffer;
149
150 TextEdit_GetSel(WndNum,&CharRange);
151 i=CharRange.cpMax;
152
153FindStart:
154
155 int length;
156
157 i=(int)CompareSuper(hDlg,pBuf+i,FindString,IsBigSmall,pobj_nv->bRegExp,&length);
158 if(i==-1){
159 //エラー
160 return 0;
161 }
162
163 if(i){
164 //検索文字列を反転表示
165 i-=(int)pBuf;
166 CharRange.cpMin=i;
167 CharRange.cpMax=i+length;
168 TextEdit_SetSel(WndNum,CharRange.cpMin,CharRange.cpMax,TRUE);
169
170 return 1;
171 }
172 else{
173 if(!IsShowMessage) return 0;
174
175 i=(int)CompareSuper(hDlg,pBuf,FindString,IsBigSmall,pobj_nv->bRegExp,&length);
176 if(i==-1){
177 //エラー
178 return 0;
179 }
180
181 if(i){
182 //"ファイルの最初から検索しますか?"
183 if(MessageBox(hDlg,STRING_SEARCH_AGAIN,"ProjectEditor - Search",MB_YESNO|MB_ICONINFORMATION)==IDYES){
184 i=0;
185 goto FindStart;
186 }
187 }
188 else{
189 //"文字列 \"%s\" が見つかりません"
190 sprintf(msg,STRING_SEARCH_NOTFOUND,FindString);
191 MessageBox(hDlg,msg,"ProjectEditor - Search",MB_OK|MB_ICONINFORMATION);
192 }
193 }
194 return 0;
195}
196void AddFindData(HWND hwnd,char ppData[MAX_FINDLIST][MAX_PATH],char *str){
197 int i;
198 for(i=0;i<MAX_FINDLIST;i++){
199 if(lstrcmp(ppData[i],str)==0) break;
200 }
201 if(i==MAX_FINDLIST) i--;
202 for(;i>0;i--) lstrcpy(ppData[i],ppData[i-1]);
203 lstrcpy(ppData[0],str);
204 SendMessage(hwnd,CB_RESETCONTENT,0,0);
205 for(i=0;i<MAX_FINDLIST;i++){
206 if(ppData[i][0]=='\0') break;
207 SendMessage(hwnd,CB_ADDSTRING,0,(long)ppData[i]);
208 }
209 SendMessage(hwnd,CB_SETCURSEL,0,0);
210}
211BOOL CALLBACK DlgFind(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
212 extern HANDLE hHeap;
213 extern MDIINFO MdiInfo[MAX_WNDNUM];
214 HWND FindAllDlg;
215 int i,WndNum;
216 char temporary[8192],*pTemp;
217 CHARRANGE CharRange;
218
219 switch(message){
220 case WM_INITDIALOG:
221 SetPosCenter(hwnd);
222
223 //大文字・小文字
224 if(pobj_nv->bFindStrBigSmall) SendDlgItemMessage(hwnd,IDC_ISBIGSMALL,BM_SETCHECK,BST_CHECKED,0);
225
226 //正規表現
227 if(pobj_nv->bRegExp) SendDlgItemMessage(hwnd,IDC_REGEXP,BM_SETCHECK,BST_CHECKED,0);
228
229 //検索文字列コンボボックスを初期化
230 SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_RESETCONTENT,0,0);
231 for(i=0;i<MAX_FINDLIST;i++){
232 if(pobj_nv->FindStr[i][0]=='\0') break;
233 SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_ADDSTRING,0,(long)pobj_nv->FindStr[i]);
234 }
235 SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_LIMITTEXT,8192,0);
236
237 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
238
239 //選択されている文字列を取得
240 TextEdit_GetSel(WndNum,&CharRange);
241 pTemp=(char *)HeapAlloc(hHeap,0,CharRange.cpMax-CharRange.cpMin+1);
242 memcpy(pTemp,
243 MdiInfo[WndNum].pMdiTextEdit->buffer+CharRange.cpMin,
244 CharRange.cpMax-CharRange.cpMin);
245 pTemp[CharRange.cpMax-CharRange.cpMin]=0;
246
247 if(pTemp[0]) SetDlgItemText(hwnd,IDC_FINDSTR,pTemp);
248 else SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_SETCURSEL,0,0);
249
250 HeapDefaultFree(pTemp);
251
252 SetFocus(GetDlgItem(hwnd,IDC_FINDSTR));
253 break;
254 case WM_COMMAND:
255 switch(LOWORD(wParam)){
256 case IDOK:
257 GetDlgItemText(hwnd,IDC_FINDSTR,temporary,MAX_PATH);
258 if(temporary[0]==0) return 1;
259 AddFindData(GetDlgItem(hwnd,IDC_FINDSTR),pobj_nv->FindStr,temporary);
260
261 //大文字・小文字
262 if(SendDlgItemMessage(hwnd,IDC_ISBIGSMALL,BM_GETCHECK,0,0)) pobj_nv->bFindStrBigSmall=1;
263 else pobj_nv->bFindStrBigSmall=0;
264
265 //正規表現
266 if(SendDlgItemMessage(hwnd,IDC_REGEXP,BM_GETCHECK,0,0)) pobj_nv->bRegExp=1;
267 else pobj_nv->bRegExp=0;
268
269 StartSearch(hwnd,temporary,pobj_nv->bFindStrBigSmall,1);
270 return 1;
271 case IDC_FINDALL:
272 GetDlgItemText(hwnd,IDC_FINDSTR,temporary,MAX_PATH);
273 if(temporary[0]==0) return 1;
274 AddFindData(GetDlgItem(hwnd,IDC_FINDSTR),pobj_nv->FindStr,temporary);
275
276 //大文字・小文字
277 if(SendDlgItemMessage(hwnd,IDC_ISBIGSMALL,BM_GETCHECK,0,0)) pobj_nv->bFindStrBigSmall=1;
278 else pobj_nv->bFindStrBigSmall=0;
279
280 //正規表現
281 if(SendDlgItemMessage(hwnd,IDC_REGEXP,BM_GETCHECK,0,0)) pobj_nv->bRegExp=1;
282 else pobj_nv->bRegExp=0;
283
284 FindAllDlg=CreateDialog(hResInst,MAKEINTRESOURCE(IDD_FINDALL),GetWindow(hwnd,GW_OWNER),(DLGPROC)DlgFindAll);
285 ShowWindow(FindAllDlg,SW_SHOW);
286
287 FindPosCounter=sizeof(long);
288 pFindPos=(long *)HeapAlloc(hHeap,0,FindPosCounter);
289 SetSearchData(FindAllDlg,temporary,pobj_nv->bFindStrBigSmall);
290 case IDCANCEL:
291 EndDialog(hwnd,0);
292 return 1;
293 }
294 break;
295 }
296 return 0;
297}
298BOOL CALLBACK DlgPermutation(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
299 extern HANDLE hHeap;
300 extern HINSTANCE hInst;
301 extern MDIINFO MdiInfo[MAX_WNDNUM];
302 int i;
303 int WndNum;
304 char temporary[8192],temp2[8192],*pTemp;
305 CHARRANGE CharRange;
306 switch(message){
307 case WM_INITDIALOG:
308 SetPosCenter(hwnd);
309
310 //大文字・小文字
311 if(pobj_nv->bFindStrBigSmall) SendDlgItemMessage(hwnd,IDC_ISBIGSMALL,BM_SETCHECK,BST_CHECKED,0);
312
313 //正規表現
314 if(pobj_nv->bRegExp) SendDlgItemMessage(hwnd,IDC_REGEXP,BM_SETCHECK,BST_CHECKED,0);
315
316
317 //検索文字列コンボボックスを初期化
318 SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_RESETCONTENT,0,0);
319 for(i=0;i<MAX_FINDLIST;i++){
320 if(pobj_nv->FindStr[i][0]=='\0') break;
321 SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_ADDSTRING,0,(long)pobj_nv->FindStr[i]);
322 }
323 SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_LIMITTEXT,8192,0);
324
325 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
326
327 //選択されている文字列を取得
328 TextEdit_GetSel(WndNum,&CharRange);
329 pTemp=(char *)HeapAlloc(hHeap,0,CharRange.cpMax-CharRange.cpMin+1);
330 memcpy(pTemp,
331 MdiInfo[WndNum].pMdiTextEdit->buffer+CharRange.cpMin,
332 CharRange.cpMax-CharRange.cpMin);
333 pTemp[CharRange.cpMax-CharRange.cpMin]=0;
334
335 if(pTemp[0]) SetDlgItemText(hwnd,IDC_FINDSTR,pTemp);
336 else SendDlgItemMessage(hwnd,IDC_FINDSTR,CB_SETCURSEL,0,0);
337
338 HeapDefaultFree(pTemp);
339
340 //置換文字列コンボボックスを初期化
341 SendDlgItemMessage(hwnd,IDC_PERMUTATIONSTR,CB_RESETCONTENT,0,0);
342 for(i=0;i<MAX_FINDLIST;i++){
343 if(pobj_nv->PermutationStr[i][0]=='\0') break;
344 SendDlgItemMessage(hwnd,IDC_PERMUTATIONSTR,CB_ADDSTRING,0,(long)pobj_nv->PermutationStr[i]);
345 }
346 SendDlgItemMessage(hwnd,IDC_PERMUTATIONSTR,CB_SETCURSEL,0,0);
347 SendDlgItemMessage(hwnd,IDC_PERMUTATIONSTR,CB_LIMITTEXT,8192,0);
348
349 SetFocus(GetDlgItem(hwnd,IDC_FINDSTR));
350 break;
351 case WM_COMMAND:
352 switch(LOWORD(wParam)){
353 case IDC_FIND:
354 //大文字・小文字
355 if(SendDlgItemMessage(hwnd,IDC_ISBIGSMALL,BM_GETCHECK,0,0)) pobj_nv->bFindStrBigSmall=1;
356 else pobj_nv->bFindStrBigSmall=0;
357
358 //正規表現
359 if(SendDlgItemMessage(hwnd,IDC_REGEXP,BM_GETCHECK,0,0)) pobj_nv->bRegExp=1;
360 else pobj_nv->bRegExp=0;
361
362 GetDlgItemText(hwnd,IDC_FINDSTR,temporary,MAX_PATH);
363 if(temporary[0]==0) return 1;
364 AddFindData(GetDlgItem(hwnd,IDC_FINDSTR),pobj_nv->FindStr,temporary);
365
366 StartSearch(hwnd,temporary,pobj_nv->bFindStrBigSmall,1);
367 return 1;
368 case IDC_PERMUTATIONNEXT:
369 //////////
370 // 置換
371 //////////
372
373 //大文字・小文字
374 if(SendDlgItemMessage(hwnd,IDC_ISBIGSMALL,BM_GETCHECK,0,0)) pobj_nv->bFindStrBigSmall=1;
375 else pobj_nv->bFindStrBigSmall=0;
376
377 //正規表現
378 if(SendDlgItemMessage(hwnd,IDC_REGEXP,BM_GETCHECK,0,0)) pobj_nv->bRegExp=1;
379 else pobj_nv->bRegExp=0;
380
381 //検索する文字列を取得
382 GetDlgItemText(hwnd,IDC_FINDSTR,temporary,MAX_PATH);
383 if(temporary[0]==0) return 1;
384 AddFindData(GetDlgItem(hwnd,IDC_FINDSTR),pobj_nv->FindStr,temporary);
385
386 //置換する文字列を取得
387 GetDlgItemText(hwnd,IDC_PERMUTATIONSTR,temp2,MAX_PATH);
388 AddFindData(GetDlgItem(hwnd,IDC_PERMUTATIONSTR),pobj_nv->PermutationStr,temp2);
389
390 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
391
392 //選択されている文字列を取得
393 TextEdit_GetSel(WndNum,&CharRange);
394 pTemp=(char *)HeapAlloc(hHeap,0,CharRange.cpMax-CharRange.cpMin+1);
395 memcpy(pTemp,
396 MdiInfo[WndNum].pMdiTextEdit->buffer+CharRange.cpMin,
397 CharRange.cpMax-CharRange.cpMin);
398 pTemp[CharRange.cpMax-CharRange.cpMin]=0;
399
400 int length;
401 i=(int)CompareSuper(hwnd,pTemp,temporary,pobj_nv->bFindStrBigSmall,pobj_nv->bRegExp,&length);
402 if(i==-1){
403 HeapDefaultFree(pTemp);
404 return 1;
405 }
406
407 if(i){
408 if(pobj_nv->bRegExp){
409 char *pTempPermu;
410
411 //正規表現の結果文字列を取得(置換後用の文字列を取得)
412 pTempPermu=obj_RegExp.GetPermuStr(hwnd,pTemp,temporary,temp2,pobj_nv->bFindStrBigSmall);
413 if(pTempPermu==(char *)-1||pTempPermu==0){
414 HeapDefaultFree(pTemp);
415 return 1;
416 }
417
418 //置換する
419 TextEdit_ReplaceUpdateUndoData(WndNum,pTempPermu,0,1);
420
421 HeapDefaultFree(pTempPermu);
422 }
423 else{
424 //置換する
425 TextEdit_ReplaceUpdateUndoData(WndNum,temp2,0,1);
426 }
427 }
428 HeapDefaultFree(pTemp);
429
430 //次を検索
431 SendMessage(hwnd,WM_COMMAND,IDC_FIND,0);
432
433 return 1;
434 case IDC_PERMUTATIONALL:
435 //////////////
436 // すべて置換
437 //////////////
438
439 //大文字・小文字
440 if(SendDlgItemMessage(hwnd,IDC_ISBIGSMALL,BM_GETCHECK,0,0)) pobj_nv->bFindStrBigSmall=1;
441 else pobj_nv->bFindStrBigSmall=0;
442
443 //正規表現
444 if(SendDlgItemMessage(hwnd,IDC_REGEXP,BM_GETCHECK,0,0)) pobj_nv->bRegExp=1;
445 else pobj_nv->bRegExp=0;
446
447 //検索する文字列を取得
448 GetDlgItemText(hwnd,IDC_FINDSTR,temporary,MAX_PATH);
449 if(temporary[0]==0) return 1;
450 AddFindData(GetDlgItem(hwnd,IDC_FINDSTR),pobj_nv->FindStr,temporary);
451
452 //置換する文字列を取得
453 GetDlgItemText(hwnd,IDC_PERMUTATIONSTR,temp2,MAX_PATH);
454 AddFindData(GetDlgItem(hwnd,IDC_PERMUTATIONSTR),pobj_nv->PermutationStr,temp2);
455
456 //正規表現
457 if(SendDlgItemMessage(hwnd,IDC_REGEXP,BM_GETCHECK,0,0)) pobj_nv->bRegExp=1;
458 else pobj_nv->bRegExp=0;
459
460 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
461
462 TextEdit_SetSel(WndNum,0,0,TRUE);
463
464 while(StartSearch(hwnd,temporary,pobj_nv->bFindStrBigSmall,0)){
465 if(pobj_nv->bRegExp){
466 //選択されている文字列を取得
467 TextEdit_GetSel(WndNum,&CharRange);
468 pTemp=(char *)HeapAlloc(hHeap,0,CharRange.cpMax-CharRange.cpMin+1);
469 memcpy(pTemp,
470 MdiInfo[WndNum].pMdiTextEdit->buffer+CharRange.cpMin,
471 CharRange.cpMax-CharRange.cpMin);
472 pTemp[CharRange.cpMax-CharRange.cpMin]=0;
473
474 char *pTempPermu;
475
476 //正規表現の結果文字列を取得(置換後用の文字列を取得)
477 pTempPermu=obj_RegExp.GetPermuStr(hwnd,pTemp,temporary,temp2,pobj_nv->bFindStrBigSmall);
478 if(pTempPermu==(char *)-1||pTempPermu==0){
479 HeapDefaultFree(pTemp);
480 return 1;
481 }
482
483 //置換する
484 TextEdit_ReplaceUpdateUndoData(WndNum,pTempPermu,0,1);
485
486 HeapDefaultFree(pTempPermu);
487 HeapDefaultFree(pTemp);
488 }
489 else{
490 //置換する
491 TextEdit_ReplaceUpdateUndoData(WndNum,temp2,0,1);
492 }
493 }
494
495 return 1;
496 case IDCANCEL:
497 EndDialog(hwnd,0);
498 return 1;
499 }
500 break;
501 }
502 return 0;
503}
Note: See TracBrowser for help on using the repository browser.