Annotation of OpenXM_contrib2/windows/asir32gui/asir32guiview.cpp, Revision 1.10
1.1 noro 1: // asir32guiView.cpp : CAsir32guiView クラスの動作の定義を行います。
2: //
3:
4: #include "stdafx.h"
5:
6: #include "asir32gui.h"
7: #include "asir32guiDoc.h"
8: #include "asir32guiView.h"
9: #include "FatalDialog.h"
1.3 noro 10: #include <direct.h>
1.6 noro 11: #include <io.h>
1.1 noro 12:
13: #ifdef _DEBUG
14: #define new DEBUG_NEW
15: #undef THIS_FILE
16: static char THIS_FILE[] = __FILE__;
17: #endif
18:
1.5 noro 19: // const TextBufferSize = 32768;
20: const TextBufferSize = 16384;
1.1 noro 21:
22: CAsir32guiView *theView;
23:
24: extern "C"
25: {
26: char *prev_hist(void), *next_hist(void), *search_hist(char *p);
27: void read_input_history(),write_input_history();
28: void init_input_history();
29: void add_hist(char *p), reset_traverse(void);
30: void asir_terminate(void);
31: void Main(int,char*[]);
32: void loadfile(char *);
33: void put_line(char *);
34: void send_intr(void);
35: void insert_to_theView(char *);
36: void get_rootdir(char *,int,char *);
37: extern int bigfloat, prtime, prresult;
38: extern char *asir_libdir;
1.2 noro 39: extern int asirgui_kind;
1.1 noro 40:
41: void insert_to_theView(char *str)
42: {
43: (void)theView->Insert(str);
44: }
45: void flush_log()
46: {
47: if ( theView->Logging )
48: theView->OnFileLog();
49: }
50: }
51:
52: /////////////////////////////////////////////////////////////////////////////
53: // CAsir32guiView
54:
55: IMPLEMENT_DYNCREATE(CAsir32guiView, CEditView)
56:
57: BEGIN_MESSAGE_MAP(CAsir32guiView, CEditView)
58: //{{AFX_MSG_MAP(CAsir32guiView)
59: ON_WM_KEYDOWN()
60: ON_WM_CHAR()
61: ON_WM_LBUTTONUP()
62: ON_WM_RBUTTONDOWN()
63: ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
64: ON_COMMAND(ID_FILE_LOG, OnFileLog)
65: ON_COMMAND(ID_ASIRHELP, OnAsirhelp)
66: ON_UPDATE_COMMAND_UI(ID_FILE_LOG, OnUpdateFileLog)
67: ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
1.3 noro 68: ON_COMMAND(IDR_FONT, OnFont)
69: ON_WM_CREATE()
1.6 noro 70: ON_COMMAND(ID_CONTRIBHELP, OnContribhelp)
71: ON_UPDATE_COMMAND_UI(ID_CONTRIBHELP, OnUpdateContribhelp)
1.9 noro 72: ON_WM_SIZE()
1.1 noro 73: //}}AFX_MSG_MAP
74: // 標準印刷コマンド
75: // ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
76: // ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
77: // ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
78: END_MESSAGE_MAP()
79:
80: /////////////////////////////////////////////////////////////////////////////
81: // CAsir32guiView クラスの構築/消滅
82:
83: CAsir32guiView::CAsir32guiView()
84: {
85: // TODO: この場所に構築用のコードを追加してください。
86: static int tmpView_created = 0;
87: char errmsg[BUFSIZ];
88:
89: init_input_history();
90: read_input_history();
91: DebugMode = 0;
92: DebugInMain = 0;
1.9 noro 93: // MaxLineLength = 80;
1.1 noro 94: LogStart = 0;
95: Logging = 0;
96: Logfp = NULL;
97: ResetIndex();
98: theView = this;
99: }
100:
101: CAsir32guiView::~CAsir32guiView()
102: {
103: write_input_history();
104: }
105:
106: BOOL CAsir32guiView::PreCreateWindow(CREATESTRUCT& cs)
107: {
108: // TODO: この位置で CREATESTRUCT cs を修正して Window クラスまたはスタイルを
109: // 修正してください。
110:
111: m_dwDefaultStyle = dwStyleDefault & ~ES_AUTOHSCROLL ;
112: m_dwDefaultStyle &= ~WS_HSCROLL;
113: return CCtrlView::PreCreateWindow(cs);
114: }
115:
116: /////////////////////////////////////////////////////////////////////////////
117: // CAsir32guiView クラスの描画
118:
119: void CAsir32guiView::OnDraw(CDC* pDC)
120: {
121: CAsir32guiDoc* pDoc = GetDocument();
122: ASSERT_VALID(pDoc);
123:
124: // TODO: この場所にネイティブ データ用の描画コードを追加します。
125: }
126:
127: /////////////////////////////////////////////////////////////////////////////
128: // CAsir32guiView クラスの印刷
129:
130: BOOL CAsir32guiView::OnPreparePrinting(CPrintInfo* pInfo)
131: {
132: // デフォルトの印刷準備
133: return DoPreparePrinting(pInfo);
134: }
135:
136: void CAsir32guiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
137: {
138: // TODO: 印刷前の特別な初期化処理を追加してください。
139: }
140:
141: void CAsir32guiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
142: {
143: // TODO: 印刷後の後処理を追加してください。
144: }
145:
146: /////////////////////////////////////////////////////////////////////////////
147: // CAsir32guiView クラスの診断
148:
149: #ifdef _DEBUG
150: void CAsir32guiView::AssertValid() const
151: {
152: CEditView::AssertValid();
153: }
154:
155: void CAsir32guiView::Dump(CDumpContext& dc) const
156: {
157: CEditView::Dump(dc);
158: }
159:
160: CAsir32guiDoc* CAsir32guiView::GetDocument() // 非デバッグ バージョンはインラインです。
161: {
162: ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAsir32guiDoc)));
163: return (CAsir32guiDoc*)m_pDocument;
164: }
165: #endif //_DEBUG
166:
167: /////////////////////////////////////////////////////////////////////////////
168: // CAsir32guiView クラスのメッセージ ハンドラ
169:
170: void CAsir32guiView::PutChar(int c)
171: {
1.8 noro 172: int i,len;
1.1 noro 173: char buf[2];
174:
1.8 noro 175: if ( EndPos >= sizeof(Buffer)-1 ) {
176: Beep(); return;
177: }
1.1 noro 178: for ( i = EndPos-1; i >= CurrentPos; i-- )
1.8 noro 179: Buffer[i+1] = Buffer[i];
1.1 noro 180: Buffer[CurrentPos] = (char)c;
1.8 noro 181: // buf[0] = (char)c; buf[1] = 0;
182: // Insert(buf,FALSE);
183: len = GetWindowTextLength();
184: if ( len+1 >= TextBufferSize )
185: DeleteTop();
186: CEditView::OnChar(c, 1, 0);
1.1 noro 187: CurrentPos++;
188: EndPos++;
189: }
190:
191: void CAsir32guiView::DeleteChar(int count) {
192: int i,s,e;
193: char null = 0;
194:
195: if ( count > (EndPos-CurrentPos) )
196: count = EndPos - CurrentPos;
197: if ( count == 0 )
198: Beep();
199: else {
200: EndPos -= count;
201: for ( i = CurrentPos; i < EndPos; i++ )
202: Buffer[i] = Buffer[i+count];
203: GetEditCtrl().GetSel(s,e);
204: GetEditCtrl().SetSel(s,s+count);
205: GetEditCtrl().ReplaceSel(&null);
206: }
207: }
208:
209: void CAsir32guiView::Insert(char *buf,BOOL wrap)
210: {
211: char t[BUFSIZ*4];
212: char *p,*q,c;
213: int len;
214: int i;
215:
216: if ( wrap ) {
217: i = GetEditCtrl().LineLength(-1);
218: for ( p = t, q = buf; (c = *p++ = *q++) != 0; )
219: if ( c == '\r' ) {
220: *p++ = *q++; i = 0;
221: } else if ( (*q != '\r') && (++i == MaxLineLength) ) {
222: *p++ = '\r'; *p++ = '\r'; *p++ = '\n'; i = 0;
223: }
224: p = t;
225: } else
226: p = buf;
227: len = GetWindowTextLength();
228: if ( strlen(p) + len >= TextBufferSize )
229: DeleteTop();
230: GetEditCtrl().ReplaceSel(p);
231: }
232:
233: void CAsir32guiView::DeleteTop()
234: {
235: const char *p,*q;
236: const char *buf;
237: char null = 0;
238: int len,len0,len1;
239:
240: buf = LockBuffer();
241: len0 = len = strlen(buf);
242: for ( p = buf; len >= TextBufferSize/2; ) {
243: q = strchr(p,'\n');
244: if ( !q ) {
245: len = 0; break;
246: } else {
247: q++; len -= (q-p); p = q;
248: }
249: }
250: if ( Logging ) {
1.4 noro 251: if ( len < LogStart )
252: LogStart -= len;
253: else {
254: fwrite(buf+LogStart,1,len-LogStart,Logfp);
255: LogStart = 0;
256: }
1.1 noro 257: }
258: UnlockBuffer();
259: if ( q ) {
260: GetEditCtrl().SetSel(0,len);
261: GetEditCtrl().ReplaceSel(&null);
262: }
263: UpdateCursor(EndPos);
264: }
265:
266: void CAsir32guiView::UpdateCursor(int pos)
267: {
268: int s;
269: int len;
270:
271: if ( pos < 0 || pos > EndPos )
272: Beep();
273: else {
274: len = GetWindowTextLength();
275: CurrentPos = pos;
276: s = len-EndPos+CurrentPos;
277: GetEditCtrl().SetSel(s,s);
278: }
279: }
280:
281: void CAsir32guiView::ReplaceString(char *str)
282: {
283: UpdateCursor(0); DeleteChar(EndPos);
284: Insert(str,FALSE);
285: strcpy(Buffer,str);
286: EndPos = CurrentPos = strlen(str);
287: }
288:
289: void CAsir32guiView::Beep(void) {
290: ::MessageBeep(0xffffffff);
291: }
292:
293: void CAsir32guiView::Paste(void) {
1.8 noro 294: char buf[2*BUFSIZ];
295: const char *src;
296: char c;
297: int i,j,l;
298: HGLOBAL hClip;
299:
300: if ( asirgui_kind == ASIRGUI_MESSAGE ) {
301: Beep(); return;
302: }
1.2 noro 303:
1.8 noro 304: if ( OpenClipboard() == FALSE ) {
305: Beep(); return;
1.1 noro 306: }
307: hClip = GetClipboardData(CF_TEXT);
308: src = (const char *)::GlobalLock(hClip);
1.10 ! noro 309: if ( !src || (l=strlen(src)) >= sizeof(Buffer)-EndPos ) {
1.1 noro 310: ::CloseClipboard();
311: Beep(); return;
312: }
1.8 noro 313: for ( i = j = 0; i < l; i++ )
314: if ( (c = src[i]) != '\n' && c != '\r' )
315: buf[j++] = c;
316: buf[j] = 0;
1.1 noro 317: ::GlobalUnlock(hClip);
318: ::CloseClipboard();
319: GetEditCtrl().ReplaceSel(buf);
1.10 ! noro 320: l = strlen(buf);
! 321: for ( i = EndPos-1; i >= CurrentPos; i-- )
! 322: Buffer[i+l] = Buffer[i];
! 323: for ( j = 0, i = CurrentPos; j < l; j++, i++ )
! 324: Buffer[i] = buf[j];
! 325: EndPos += l; CurrentPos += l;
1.1 noro 326: }
327:
328: #define CTRL(c) (c)&0x1f
329:
330: void CAsir32guiView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
331: {
332: // TODO: この位置にメッセージ ハンドラ用のコードを追加するかまたはデフォルトの処理を呼び出してください
333: char *exp_result;
334:
335: switch ( nChar ) {
336: case VK_LEFT:
337: UpdateCursor(CurrentPos-1); return;
338: case VK_RIGHT:
339: UpdateCursor(CurrentPos+1); return;
340: case VK_DELETE:
341: DeleteChar(1); return;
342: case VK_UP:
343: if ( (exp_result = prev_hist()) != 0 )
344: ReplaceString(exp_result);
345: else
346: Beep();
347: return;
348: case VK_DOWN:
349: if ( (exp_result = next_hist()) != 0 )
350: ReplaceString(exp_result);
351: else
352: Beep();
353: return;
354: default:
355: break;
356: }
357: CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
358: }
359:
360: void CAsir32guiView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
361: {
362: // TODO: この位置にメッセージ ハンドラ用のコードを追加するかまたはデフォルトの処理を呼び出してください
363: char *exp_result;
364:
1.2 noro 365: if ( asirgui_kind == ASIRGUI_MESSAGE ) {
366: Beep(); return;
367: }
368:
1.1 noro 369: switch ( nChar ) {
370: case CTRL('M'): case CTRL('J'):
371: UpdateCursor(EndPos);
372: Buffer[EndPos] = 0;
373: reset_traverse();
374: if ( Buffer[0] == '!' ) {
375: exp_result = search_hist(Buffer+1);
376: if ( exp_result )
377: ReplaceString(exp_result);
378: else {
379: UpdateCursor(0); DeleteChar(EndPos); Beep();
380: }
381: return;
382: } else {
383: add_hist(Buffer);
384: put_line(Buffer);
385: ResetIndex();
386: nChar = CTRL('M');
387: }
388: break;
389: case CTRL('N'):
390: if ( (exp_result = next_hist()) != 0 )
391: ReplaceString(exp_result);
392: else
393: Beep();
394: return;
395: case CTRL('P'):
396: if ( (exp_result = prev_hist()) != 0 )
397: ReplaceString(exp_result);
398: else
399: Beep();
400: return;
401: case CTRL('A'):
402: UpdateCursor(0); return;
403: case CTRL('E'):
404: UpdateCursor(EndPos); return;
405: case CTRL('F'):
406: UpdateCursor(CurrentPos+1); return;
407: case CTRL('B'):
408: UpdateCursor(CurrentPos-1); return;
409: case CTRL('K'):
410: DeleteChar(EndPos-CurrentPos); return;
411: case CTRL('U'):
412: UpdateCursor(0); DeleteChar(EndPos); return;
413: case CTRL('D'):
414: DeleteChar(1); return;
415: case CTRL('H'):
416: if ( CurrentPos > 0 ) {
417: UpdateCursor(CurrentPos-1); DeleteChar(1);
418: } else
419: Beep();
420: return;
421: case CTRL('C'):
422: send_intr();
423: /* wake up the engine */
424: put_line("");
425: return;
426: default :
427: if ( nChar >= 0x20 && nChar < 0x7f )
428: PutChar(nChar);
429: else
430: Beep();
431: return;
432: }
433: CEditView::OnChar(nChar, nRepCnt, nFlags);
434: }
435:
436: void CAsir32guiView::OnLButtonUp(UINT nFlags, CPoint point)
437: {
438: // TODO: この位置にメッセージ ハンドラ用のコードを追加するかまたはデフォルトの処理を呼び出してください
439: GetEditCtrl().Copy(); UpdateCursor(EndPos);
440: CEditView::OnLButtonUp(nFlags, point);
441: }
442:
443: void CAsir32guiView::OnRButtonDown(UINT nFlags, CPoint point)
444: {
445: // TODO: この位置にメッセージ ハンドラ用のコードを追加するかまたはデフォルトの処理を呼び出してください
446: Paste();
447:
448: // CEditView::OnRButtonDown(nFlags, point);
449: }
450:
451: void CAsir32guiView::OnFileOpen()
452: {
453: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
454: char cmd[BUFSIZ*2]; // XXX
455: char *p;
1.3 noro 456: static char errmsg[BUFSIZ];
457: static char prevdir[BUFSIZ];
1.1 noro 458:
1.3 noro 459: if ( !prevdir[0] ) {
460: get_rootdir(prevdir,sizeof(prevdir),errmsg);
461: strcat(prevdir,"\\lib");
462: }
463:
464: _chdir(prevdir);
1.1 noro 465: CFileDialog fileDialog(TRUE);
466: if ( fileDialog.DoModal() == IDOK ) {
467: CString pathName = fileDialog.GetPathName();
468: sprintf(cmd,"load(\"%s\");",pathName);
469: for ( p = cmd; *p; p++ )
470: if ( *p == '\\' )
471: *p = '/';
472: put_line(cmd);
1.3 noro 473: p = strrchr(pathName,'\\');
474: if ( p ) {
475: *p = 0;
476: strcpy(prevdir,pathName);
477: }
1.1 noro 478: }
479: }
480:
481: void CAsir32guiView::OnAsirhelp()
482: {
483: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
484: char root[BUFSIZ],errmsg[BUFSIZ],helpfile[BUFSIZ];
485:
486: get_rootdir(root,sizeof(root),errmsg);
487: sprintf(helpfile,"%s\\bin\\asirhelp.chm",root);
488: HtmlHelp(NULL, helpfile, HH_DISPLAY_TOPIC, 0);
489: }
490:
491: void CAsir32guiView::OnFileLog()
492: {
493: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
494: if ( !Logging ) {
495: CFileDialog fileDialog(TRUE);
496: if ( fileDialog.DoModal() == IDOK ) {
497: CString logfile = fileDialog.GetPathName();
498: Logfp = fopen(logfile,"ab");
499: if ( Logfp ) {
500: LogStart = GetWindowTextLength();
501: Logging = 1;
502: }
503: }
504: } else {
505: const char *buf;
506: buf = LockBuffer();
507: fwrite(buf+LogStart,1,strlen(buf)-LogStart,Logfp);
508: UnlockBuffer();
509: fclose(Logfp);
510: Logging = 0;
511: }
512: }
513:
514: void CAsir32guiView::OnUpdateFileLog(CCmdUI* pCmdUI)
515: {
516: // TODO: この位置に command update UI ハンドラ用のコードを追加してください
517: if ( Logging )
518: pCmdUI->SetCheck(1);
519: else
520: pCmdUI->SetCheck(0);
521: }
522:
523: void CAsir32guiView::OnEditPaste()
524: {
525: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
526: Paste();
1.3 noro 527: }
528:
1.9 noro 529: void CAsir32guiView::UpdateMetrics()
530: {
531: TEXTMETRIC tm;
532: RECT r;
533:
534: GetEditCtrl().GetRect(&r);
535: CDC *pDC = GetDC();
536: pDC->SelectObject(GetFont());
537: pDC->GetTextMetrics(&tm);
538: MaxLineLength = (r.right-r.left)/tm.tmAveCharWidth-1;
539: }
540:
1.3 noro 541: void CAsir32guiView::OnFont()
542: {
543: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
544: int ret;
1.9 noro 545: LOGFONT lf;
546:
547: GetFont()->GetLogFont(&lf);
548: CFontDialog fd(&lf,CF_EFFECTS | CF_SCREENFONTS | CF_FIXEDPITCHONLY);
1.3 noro 549: static CFont *f = 0;
550:
551: ret = fd.DoModal();
552: if ( ret == IDOK ) {
553: fd.GetCurrentFont(&lf);
554: if ( f )
555: delete f;
556: f = new CFont;
557: f->CreateFontIndirect(&lf);
558: SetFont(f);
1.9 noro 559: UpdateMetrics();
1.3 noro 560: }
561: }
562:
563: int CAsir32guiView::OnCreate(LPCREATESTRUCT lpCreateStruct)
564: {
565: if (CEditView::OnCreate(lpCreateStruct) == -1)
566: return -1;
567:
568: // TODO: この位置に固有の作成用コードを追加してください
1.9 noro 569:
1.3 noro 570: LOGFONT logFont; memset(&logFont, 0, sizeof(LOGFONT));
571: logFont.lfHeight = 20;
572: logFont.lfCharSet = DEFAULT_CHARSET;
573: strcpy(logFont.lfFaceName, "Terminal");
574: CFont *f = new CFont;
575: f->CreateFontIndirect(&logFont);
576: SetFont(f);
1.9 noro 577: UpdateMetrics();
1.3 noro 578: GetEditCtrl().LimitText(nMaxSize);
1.9 noro 579: GetEditCtrl().SetTabStops(m_nTabStops);
1.3 noro 580:
581: return 0;
1.6 noro 582: }
583:
584: void CAsir32guiView::OnContribhelp()
585: {
586: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
587: char root[BUFSIZ],errmsg[BUFSIZ],helpfile[BUFSIZ];
588:
589: get_rootdir(root,sizeof(root),errmsg);
590: sprintf(helpfile,"%s\\bin\\cmanhelp.chm",root);
591: HtmlHelp(NULL, helpfile, HH_DISPLAY_TOPIC, 0);
592: }
593:
594: void CAsir32guiView::OnUpdateContribhelp(CCmdUI* pCmdUI)
595: {
596: // TODO: この位置に command update UI ハンドラ用のコードを追加してください
597: char root[BUFSIZ],errmsg[BUFSIZ],helpfile[BUFSIZ];
598:
1.7 noro 599: if ( getenv("OpenXM_WIN_ROOT") ) {
600: get_rootdir(root,sizeof(root),errmsg);
601: sprintf(helpfile,"%s\\bin\\cmanhelp.chm",root);
602: if ( _access(helpfile,04) != -1 ) {
603: pCmdUI->Enable( TRUE );
604: return;
605: }
606: }
607: pCmdUI->Enable( FALSE );
1.9 noro 608: }
609:
610: void CAsir32guiView::OnSize(UINT nType, int cx, int cy)
611: {
612: CEditView::OnSize(nType, cx, cy);
613:
614: // TODO: この位置にメッセージ ハンドラ用のコードを追加してください
615: UpdateMetrics();
1.1 noro 616: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>