Annotation of OpenXM_contrib2/windows/engine2000/ChildView.cpp, Revision 1.6
1.1 noro 1: // ChildView.cpp : CChildView クラスの動作の定義を行います。
2: //
3:
4: #include "stdafx.h"
5: #include "ox_plot.h"
6: #include "ChildView.h"
7:
8: #ifdef _DEBUG
9: #define new DEBUG_NEW
10: #undef THIS_FILE
11: static char THIS_FILE[] = __FILE__;
12: #endif
13:
14: #include <math.h>
15:
16: extern "C" {
1.6 ! ohara 17: #include "if_canvas.h"
! 18:
! 19: struct pa {
! 20: int length;
! 21: POINT *pos;
! 22: };
! 23:
! 24: typedef struct RealVect {
! 25: int len;
! 26: int body[1];
! 27: } RealVect;
1.1 noro 28:
29: #define EXP10(a) pow(10.0,a)
1.6 ! ohara 30:
! 31: extern struct canvas *current_can;
! 32: extern POINT start_point,end_point;
1.1 noro 33:
34: HANDLE hResizeNotify,hResizeNotify_Ack;
35: void destroy_canvas(struct canvas *);
36: void pline(int *,struct canvas *, HDC);
37: void draw_wideframe(struct canvas *, HDC);
38: double adjust_scale(double,double);
39: }
40: /////////////////////////////////////////////////////////////////////////////
41: // CChildView
42:
43: CChildView::CChildView()
44: {
45: can = 0;
46: }
47:
48: CChildView::~CChildView()
49: {
50: }
51:
52:
53: BEGIN_MESSAGE_MAP(CChildView,CWnd )
54: //{{AFX_MSG_MAP(CChildView)
55: ON_WM_PAINT()
56: ON_WM_LBUTTONDOWN()
57: ON_WM_MOUSEMOVE()
58: ON_WM_LBUTTONUP()
59: ON_COMMAND(ID_OPT_NOAXIS, OnOptNoaxis)
60: ON_UPDATE_COMMAND_UI(ID_OPT_NOAXIS, OnUpdateOptNoaxis)
61: ON_COMMAND(ID_OPT_PRECISE, OnOptPrecise)
62: ON_UPDATE_COMMAND_UI(ID_OPT_PRECISE, OnUpdateOptPrecise)
63: ON_COMMAND(ID_OPT_WIDE, OnOptWide)
64: ON_UPDATE_COMMAND_UI(ID_OPT_WIDE, OnUpdateOptWide)
65: //}}AFX_MSG_MAP
66: END_MESSAGE_MAP()
67:
68:
69: /////////////////////////////////////////////////////////////////////////////
70: // CChildView メッセージ ハンドラ
71:
72: BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
73: {
74: if (!CWnd::PreCreateWindow(cs))
75: return FALSE;
76:
77: cs.dwExStyle |= WS_EX_CLIENTEDGE;
78: cs.style &= ~WS_BORDER;
79: cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
80: ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);
81: return TRUE;
82: }
83:
84: void CChildView::OnPaint()
85: {
86: CPaintDC dc(this); // 描画用のデバイス コンテキスト
87:
88: // TODO: メッセージ ハンドラのコードをここに追加してください。
89:
90: // 描画のために CWnd::OnPaint を呼び出してはいけません。
1.6 ! ohara 91: if ( canvas_is_interactive(can) ) {
! 92: canvas_bitblt(can,dc.m_hDC,0,0);
1.1 noro 93: } else {
1.6 ! ohara 94: if ( !canvas_wide(can) ) {
! 95: canvas_bitblt(can,dc.m_hDC,0,0);
1.1 noro 96: pline(0,can,dc.m_hDC);
97: } else
98: draw_wideframe(can,dc.m_hDC);
99: }
100: BringWindowToTop();
101: }
102:
103: void CChildView::OnPrint(CDC &dc)
104: {
105: DOCINFO docinfo;
1.6 ! ohara 106: // NODE n;
1.3 noro 107: int width,height,ratio,x,y,step;
1.6 ! ohara 108: int ch,cw,len,i;
1.1 noro 109:
110: memset(&docinfo,0,sizeof(DOCINFO));
111: docinfo.cbSize = sizeof(DOCINFO);
112: docinfo.lpszDocName = "test";
113: docinfo.lpszOutput = 0;
114:
115: // black pen
116: CPen pen(PS_SOLID,0,(COLORREF)0);
117: dc.SelectObject(&pen);
118:
119: // black brush
120: CBrush brush((COLORREF)0);
121: dc.SelectObject(&brush);
122:
123: // setup
124: // isotropic mode
125: dc.SetMapMode(MM_ISOTROPIC);
126:
127: // the page size in device pixel
128: width = dc.GetDeviceCaps(HORZRES)*9/10;
129: height = dc.GetDeviceCaps(VERTRES)*9/10;
130:
131: dc.StartDoc(&docinfo);
132: dc.StartPage();
133:
1.6 ! ohara 134: cw = canvas_width(can);
! 135: ch = canvas_height(can);
! 136:
! 137: if ( canvas_is_interactive(can) ) {
1.3 noro 138:
139: // We want to associate a rectangle of a fixed size to
1.1 noro 140: // one bitmap pixel
1.6 ! ohara 141: // if cw/ch > width/height
1.1 noro 142: // then match the widths, else match the height
1.6 ! ohara 143: if ( cw*height > ch*width )
! 144: ratio = width/cw;
1.1 noro 145: else
1.6 ! ohara 146: ratio = height/ch;
1.1 noro 147:
148: // set the logical src bitmap size
1.6 ! ohara 149: dc.SetWindowExt(cw*ratio,ch*ratio);
1.1 noro 150:
151: // set the viewport size and the origine in printer pixel
152: dc.SetViewportOrg(width/18,height/18);
153: dc.SetViewportExt(width,height);
154:
1.3 noro 155: step = (ratio+4)/5;
1.6 ! ohara 156: RealVect **p = (RealVect **)canvas_history(can, &len);
! 157: for(i=0; i<len; i++) {
! 158: RealVect *rv = p[i];
1.1 noro 159: if ( rv->len == 2 ) {
160: // dc.SetPixel(rv->body[0],rv->body[1],0);
161: x = rv->body[0]*ratio;
162: y = rv->body[1]*ratio;
163: // dc.FillRect(CRect(x,y,x+1,y+1),&brush);
1.3 noro 164: // dc.FillRect(CRect(x-1,y-1,x+1,y+1),&brush);
165: dc.FillRect(CRect(x,y,x+step,y+step),&brush);
1.1 noro 166: } else if ( rv->len == 4 ) {
167: dc.MoveTo(rv->body[0]*ratio,rv->body[1]*ratio);
168: dc.LineTo(rv->body[2]*ratio,rv->body[3]*ratio);
169: }
170: }
171: } else {
172: // set the logical src bitmap size
1.6 ! ohara 173: dc.SetWindowExt(cw,ch);
1.1 noro 174:
175: // set the viewport size and the origine in printer pixel
176: dc.SetViewportOrg(width/18,height/18);
177: dc.SetViewportExt(width,height);
178:
1.6 ! ohara 179: if ( canvas_is_plot(can) )
! 180: dc.Polyline(canvas_pa(can)->pos,canvas_pa(can)->length);
1.1 noro 181: else
1.6 ! ohara 182: ::StretchBlt(dc.m_hDC,0,0,cw,ch,canvas_pix(can),0,0,cw,ch,SRCCOPY);
1.1 noro 183: PrintAxis(dc);
184: }
185: dc.EndPage();
186: dc.EndDoc();
187: }
188:
189: void CChildView::PrintAxis(CDC &dc)
190: {
191: #define D 5
1.6 ! ohara 192: #define DEFAULTWIDTH 400
! 193: #define DEFAULTHEIGHT 400
1.1 noro 194:
1.2 noro 195: double w,w1,e,n;
1.1 noro 196: int x0,y0,x,y,xadj,yadj;
197: char buf[BUFSIZ];
1.6 ! ohara 198: double cxmin,cxmax,cymin,cymax;
! 199: int cw,ch;
! 200:
! 201: cxmin = canvas_xmin(can);
! 202: cxmax = canvas_xmax(can);
! 203: cymin = canvas_ymin(can);
! 204: cymax = canvas_ymax(can);
! 205: cw = canvas_width(can);
! 206: ch = canvas_height(can);
1.1 noro 207:
208: /* XXX : should be cleaned up */
1.6 ! ohara 209: if ( canvas_noaxis(can) || (canvas_is_plot(can) && !canvas_pa(can))
! 210: || canvas_is_interactive(can) )
1.1 noro 211: return;
212:
213: xadj = yadj = 0;
1.6 ! ohara 214: if ( (cxmin < 0) && (cxmax > 0) ) {
! 215: x0 = (int)((cw)*(-cxmin/(cxmax-cxmin)));
! 216: dc.MoveTo(x0,0); dc.LineTo(x0,ch);
! 217: } else if ( cxmin >= 0 )
1.1 noro 218: x0 = 0;
219: else
1.6 ! ohara 220: x0 = cw-D;
! 221: if ( (cymin < 0) && (cymax > 0) ) {
! 222: y0 = (int)((ch)*(cymax/(cymax-cymin)));
! 223: dc.MoveTo(0,y0); dc.LineTo(cw,y0);
! 224: } else if ( cymin >= 0 )
! 225: y0 = ch;
1.1 noro 226: else
227: y0 = D;
1.6 ! ohara 228: w = cxmax-cxmin;
! 229: w1 = w * DEFAULTWIDTH/cw;
1.1 noro 230: e = adjust_scale(EXP10(floor(log10(w1))),w1);
1.6 ! ohara 231: for ( n = ceil(cxmin/e); n*e<= cxmax; n++ ) {
! 232: x = (int)(cw*(n*e-cxmin)/w);
1.1 noro 233: dc.MoveTo(x,y0); dc.LineTo(x,y0-D);
234: sprintf(buf,"%g",n*e);
235: dc.TextOut(x+2,y0+2,buf,strlen(buf));
236: }
1.6 ! ohara 237: w = cymax-cymin;
! 238: w1 = w * DEFAULTHEIGHT/ch;
1.1 noro 239: e = adjust_scale(EXP10(floor(log10(w1))),w1);
1.6 ! ohara 240: for ( n = ceil(cymin/e); n*e<= cymax; n++ ) {
! 241: y = (int)(ch*(1-(n*e-cymin)/w));
1.1 noro 242: dc.MoveTo(x0,y); dc.LineTo(x0+D,y);
243: sprintf(buf,"%g",n*e);
1.6 ! ohara 244: if ( cxmax <= 0 ) {
1.1 noro 245: xadj = dc.GetOutputTextExtent(buf,strlen(buf)).cx;
246: }
247: dc.TextOut(x0+2-xadj,y+2,buf,strlen(buf));
248: }
249: }
250:
251: static CPoint start,prev;
252:
253: void CChildView::OnLButtonDown(UINT nFlags, CPoint point) {
254: start = point;
255: prev = point;
256: SetCapture();
257: CWnd::OnLButtonDown(nFlags,point);
258: }
259:
260: void CChildView::OnMouseMove(UINT nFlags, CPoint point) {
261: if ( nFlags & MK_LBUTTON ) {
262: CDC *pDC = GetDC();
263: CRect prevrect(start,prev),rect(start,point);
264: CSize size(1,1);
265:
266: pDC->DrawDragRect(&rect,size,&prevrect,size);
267: ReleaseDC(pDC);
268: prev = point;
269: }
270: CWnd::OnMouseMove(nFlags,point);
271: }
272:
273: void CChildView::OnLButtonUp(UINT nFlags, CPoint point) {
274: CDC *pDC = GetDC();
275: CRect rect(start,point);
276: CSize size(1,1);
277:
278: pDC->DrawDragRect(&rect,size,0,size);
279: ReleaseDC(pDC);
280: ReleaseCapture();
281: start_point = start; end_point = point;
282: current_can = can;
283: SetEvent(hResizeNotify);
284: WaitForSingleObject(hResizeNotify_Ack,(DWORD)-1);
285:
286: CWnd::OnLButtonUp(nFlags,point);
287: }
288:
289: void CChildView::DestroyCanvas() {
290: destroy_canvas(can);
291: }
292:
293: void CChildView::OnOptNoaxis()
294: {
295: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
1.6 ! ohara 296: canvas_toggle_noaxis(can);
1.1 noro 297: RedrawWindow();
298: }
299:
300: void CChildView::OnUpdateOptNoaxis(CCmdUI* pCmdUI)
301: {
302: // TODO: この位置に command update UI ハンドラ用のコードを追加してください
1.6 ! ohara 303: pCmdUI->SetCheck(canvas_noaxis(can));
1.1 noro 304: }
305:
306: void CChildView::OnOptPrecise()
307: {
308: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
1.6 ! ohara 309: canvas_toggle_precise(can);
1.1 noro 310: }
311:
312: void CChildView::OnUpdateOptPrecise(CCmdUI* pCmdUI)
313: {
314: // TODO: この位置に command update UI ハンドラ用のコードを追加してください
1.6 ! ohara 315: pCmdUI->SetCheck(canvas_precise(can));
1.1 noro 316: }
317:
318: void CChildView::OnOptWide()
319: {
320: // TODO: この位置にコマンド ハンドラ用のコードを追加してください
1.6 ! ohara 321: canvas_toggle_wide(can);
1.1 noro 322: RedrawWindow();
323: }
324:
325: void CChildView::OnUpdateOptWide(CCmdUI* pCmdUI)
326: {
327: // TODO: この位置に command update UI ハンドラ用のコードを追加してください
1.6 ! ohara 328: pCmdUI->SetCheck(canvas_wide(can));
1.1 noro 329: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>