Annotation of OpenXM_contrib/gnuplot/beos/GPBitmap.cpp, Revision 1.1.1.1
1.1 ohara 1:
2: #include <stdio.h>
3: #include <stdlib.h>
4: #include <string.h>
5:
6: #include "GPBitmap.h"
7: #include "GPView.h"
8: #include "constants.h"
9: #include <Region.h>
10: #include <ScrollBar.h>
11:
12: #define LineSolid 1
13:
14:
15: /*******************************************************************/
16: // GPBitmap
17: GPBitmap::GPBitmap(float width, float height)
18: {
19: m_bitmap = new BBitmap(BRect(0,0,width-1,height-1),B_RGB_32_BIT,TRUE);
20: BRect r(0,0,width-1,height-1);
21: m_view = new BView(r,"BitmapDrawer",B_FOLLOW_ALL,0);
22: m_bitmap->AddChild(m_view);
23: max_commands = 7;
24: ncommands = 0;
25: m_needRedraw = false;
26: m_redrawing = false;
27: commands = (char **) malloc(max_commands * sizeof(char *));
28: this->width = nwidth = width;
29: this->height = nheight = height;
30:
31: for(int i=1 ; i<16 ; i++) {
32: colors[i].red = i*16;
33: colors[i].green = i*16;
34: colors[i].blue = i*16;
35: colors[i].alpha = 255;
36: }
37: };
38:
39: GPBitmap::GPBitmap(float width, float height, char **cmds)
40: {
41:
42: }
43:
44: GPBitmap::~GPBitmap()
45: {
46: if (m_bitmap)
47: delete m_bitmap;
48: if(m_view)
49: delete m_view;
50: if(ncommands)
51: clearCommands();
52: if(commands) {
53: free(commands);
54: commands = NULL;
55: }
56: }
57:
58: void GPBitmap::SetDirty(BRegion *r)
59: {
60: BMessage msg(bmsgBitmapDirty);
61: m_needRedraw = true;
62: }
63:
64: void GPBitmap::clearCommands()
65: {
66: int32 ncmds = atomic_add(&ncommands,-ncommands);
67: if(ncmds > 0) {
68: for( --ncmds ; ncmds >= 0 ; ncmds--) {
69: if(commands[ncmds])
70: free(commands[ncmds]);
71: commands[ncmds] = NULL;
72: }
73: }
74: }
75:
76: #if 1
77: void GPBitmap::addCommands(BMessage *msg, int32 numCmds)
78: {
79: char *p, **cmd = NULL;
80:
81: if (numCmds+ncommands >= max_commands) {
82: max_commands = max_commands + numCmds + 2;
83: commands = (commands)
84: ? (char **) realloc(commands, max_commands * sizeof(char *))
85: : (char **) malloc(sizeof(char *));
86: }
87: if (!commands) {
88: fputs("gnuplot: can't get memory. aborted.\n", stderr);
89: // exit(1);
90: } else {
91: msg->FindPointer("cmds", (void **)&cmd);
92: // printf("got : %X at %X\n",cmd[0], cmd);
93: for(int i=0; i< numCmds; i++) {
94: // cmd = msg->FindString("cmd", i);
95: // p = (char *) malloc((unsigned) strlen(cmd[i]) + 1);
96: // if (!p) {
97: // fputs("gnuplot: can't get memory. aborted.\n", stderr);
98: // exit(1);
99: // } else
100: // commands[ncommands++] = strcpy(p, cmd[i]);
101: commands[ncommands++] = strdup(cmd[i]);
102: // commands[ncommands++] = cmd[i];
103: m_needRedraw = true;
104: }
105: }
106: }
107: #else
108: void GPBitmap::addCommands(BMessage *msg, int32 numCmds)
109: {
110: char *cmd = NULL;
111:
112: if (numCmds+ncommands >= max_commands) {
113: max_commands = max_commands + numCmds + 2;
114: commands = (commands)
115: ? (char **) realloc(commands, max_commands * sizeof(char *))
116: : (char **) malloc(sizeof(char *));
117: }
118: if (!commands) {
119: fputs("gnuplot: can't get memory. aborted.\n", stderr);
120: // exit(1);
121: } else {
122: for(int i=0; i< numCmds; i++) {
123: cmd = msg->FindString("cmd", i);
124: commands[ncommands++] = strdup(cmd);
125: m_needRedraw = true;
126: }
127: }
128: }
129: #endif
130:
131: void GPBitmap::addCommand(char *cmd)
132: {
133: // printf("adding a cmd : %s\n",cmd);
134: if(!cmd)
135: return;
136: if (ncommands >= max_commands) {
137: max_commands = max_commands * 2 + 1;
138: commands = (commands)
139: ? (char **) realloc(commands, max_commands * sizeof(char *))
140: : (char **) malloc(sizeof(char *));
141: }
142: if (!commands) {
143: fputs("gnuplot: can't get memory. X11 aborted.\n", stderr);
144: return; // exit(1);
145: } else {
146: commands[ncommands++] = strdup(cmd);
147: m_needRedraw = true;
148: }
149: }
150:
151: void GPBitmap::ResizeTo(float width, float height, uint32 btns)
152: {
153: if(btns) {
154: nwidth = width;
155: nheight = height;
156: // m_needRedraw = true;
157: return;
158: }
159: BRect r(0,0,width-1,height-1);
160: if (m_bitmap)
161: m_bitmap->Lock();
162: if(m_view) {
163: m_bitmap->RemoveChild(m_view);
164: m_view->ResizeTo(nwidth, nheight);
165: } else
166: m_view = new BView(r,"BitmapDrawer",B_FOLLOW_ALL,0);
167:
168: // drawing_thread = spawn_thread(&drawing_loop, "gnuplot io_loop", B_LOW_PRIORITY, this);
169: // resume_thread(drawing_thread);
170: // kill_thread(drawing_thread);
171:
172: if (m_bitmap) {
173: m_bitmap->Unlock();
174: delete m_bitmap;
175: }
176:
177: m_bitmap = new BBitmap(r,B_RGB_32_BIT,TRUE);
178: m_bitmap->Lock();
179: m_bitmap->AddChild(m_view);
180: this->width = nwidth = width;
181: this->height = nheight = height;
182: m_needRedraw = true;
183: }
184:
185: int32 GPBitmap::drawing_loop(void* data)
186: {
187: int32 res = 1;
188: GPBitmap * bmp = (GPBitmap *)data;
189:
190: return res;
191: }
192:
193: /*-----------------------------------------------------------------------------
194: * display - display a stored plot
195: *---------------------------------------------------------------------------*/
196:
197: void GPBitmap::display(float v_width, float v_height)
198: {
199: int n, x, y, jmode, sl, lt = 0, type, point, px, py;
200: int uwidth, user_width = 1; /* as specified by plot...linewidth */
201: char *buffer, *str;
202: float sw, vchar;
203:
204: uint32 buttons;
205: BPoint cursor;
206: m_view->GetMouse(&cursor, &buttons);
207:
208: if(buttons == 0) {
209: if ((width != v_width ) || (height != v_height))
210: ResizeTo(v_width, v_height, 0);
211: }
212:
213: if (ncommands == 0 || m_needRedraw == false)
214: return;
215:
216: vchar = 11.0;
217: m_view->SetFontSize(vchar);
218:
219: /* set scaling factor between internal driver & window geometry */
220: xscale = width / 4096.0;
221: yscale = height / 4096.0;
222:
223: /* initial point sizes, until overridden with P7xxxxyyyy */
224: px = (int) (xscale * pointsize);
225: py = (int) (yscale * pointsize);
226:
227: /* set pixmap background */
228: m_view->SetHighColor(255,255,255);
229: m_view->FillRect(BRect(0,0,width,height));
230: m_view->SetViewColor(colors[2]);
231:
232: /* loop over accumulated commands from inboard driver */
233: for (n = 0; n < ncommands; n++) {
234: buffer = commands[n];
235:
236: /* X11_vector(x,y) - draw vector */
237: if (*buffer == 'V') {
238: sscanf(buffer, "V%4d%4d", &x, &y);
239: m_view->StrokeLine( BPoint(X(cx), Y(cy)),BPoint(X(x), Y(y)));
240: cx = x;
241: cy = y;
242: }
243: /* X11_move(x,y) - move */
244: else if (*buffer == 'M') {
245: sscanf(buffer, "M%4d%4d", &cx, &cy);
246: m_view->MovePenTo(BPoint(X(cx), Y(cy)));
247: }
248: /* X11_put_text(x,y,str) - draw text */
249: else if (*buffer == 'T') {
250:
251: sscanf(buffer, "T%4d%4d", &x, &y);
252: str = buffer + 9;
253: sl = strlen(str) - 1;
254: sw = m_view->StringWidth(str, sl);
255:
256: switch (jmode) {
257: case CENTRE: sw = -sw / 2; break;
258: case RIGHT: sw = -sw; break;
259: case LEFT: sw = 0; break;
260: }
261:
262: m_view->SetHighColor(colors[2]);
263: m_view->DrawString(str, sl, BPoint(X(x) + sw, Y(y) + vchar / 3 ));
264: m_view->SetHighColor(colors[lt + 3]);
265: }
266: else if (*buffer == 'F') { /* fill box */
267: int style, xtmp, ytmp, w, h;
268:
269: if (sscanf(buffer + 1, "%4d%4d%4d%4d%4d", &style, &xtmp, &ytmp, &w, &h) == 5) {
270: /* gnuplot has origin at bottom left, but X uses top left
271: * There may be an off-by-one (or more) error here.
272: * style ignored here for the moment
273: */
274: m_view->SetHighColor(colors[0]);
275: m_view->FillRect(BRect(X(xtmp), Y(ytmp + h), w * xscale, h * yscale));
276: m_view->SetHighColor(colors[lt + 3]);
277: }
278: }
279: /* X11_justify_text(mode) - set text justification mode */
280: else if (*buffer == 'J')
281: sscanf(buffer, "J%4d", (int *) &jmode);
282:
283: /* X11_linewidth(width) - set line width */
284: else if (*buffer == 'W')
285: sscanf(buffer + 1, "%4d", &user_width);
286:
287: /* X11_linetype(type) - set line type */
288: else if (*buffer == 'L') {
289: sscanf(buffer, "L%4d", <);
290: lt = (lt % 8) + 2;
291: /* default width is 0 {which X treats as 1} */
292: uwidth = user_width; // widths[lt] ? user_width * widths[lt] : user_width;
293: // if (dashes[lt][0]) {
294: // type = LineOnOffDash;
295: // XSetDashes(dpy, gc, 0, dashes[lt], strlen(dashes[lt]));
296: // } else {
297: type = LineSolid;
298: // }
299: m_view->SetHighColor(colors[lt + 3]);
300: m_view->SetPenSize(uwidth);
301: m_view->SetLineMode(B_ROUND_CAP,B_BEVEL_JOIN);
302: // XSetLineAttributes(dpy, gc, width, type, CapButt, JoinBevel);
303: }
304: /* X11_point(number) - draw a point */
305: else if (*buffer == 'P') {
306: /* linux sscanf does not like %1d%4d%4d" with Oxxxxyyyy */
307: /* sscanf(buffer, "P%1d%4d%4d", &point, &x, &y); */
308: point = buffer[1] - '0';
309: sscanf(buffer + 2, "%4d%4d", &x, &y);
310: if (point == 7) {
311: /* set point size */
312: px = (int) (x * xscale * pointsize);
313: py = (int) (y * yscale * pointsize);
314: } else {
315: if (type != LineSolid || uwidth != 0) { /* select solid line */
316: m_view->SetPenSize(1.0);
317: m_view->SetLineMode(B_ROUND_CAP,B_BEVEL_JOIN);
318: // XSetLineAttributes(dpy, gc, 0, LineSolid, CapButt, JoinBevel);
319: }
320: switch (point) {
321: case 0: /* dot */ doDot(x,y); break;
322: case 1: /* do diamond */ doDiamond(x,y,px,py); break;
323: case 2: /* do plus */ doPlus(x,y,px,py); break;
324: case 3: /* do box */ doBox(x,y,px,py); break;
325: case 4: /* do X */ doCross(x,y,px,py); break;
326: case 5: /* do triangle */ doTriangle(x,y,px,py); break;
327: case 6: /* do star */ doStar(x,y,px,py); break;
328: }
329: if (type != LineSolid || uwidth != 0) { /* select solid line */
330: m_view->SetPenSize(uwidth);
331: // canview->SetLineMode(B_ROUND_CAP,B_ROUND_JOIN);
332: // XSetLineAttributes(dpy, gc, width, type, CapButt, JoinBevel);
333: }
334: }
335: } /* end X11_point(number) - draw a point */
336: } /* end loop over accumulated commands from inboard driver */
337: m_view->Sync();
338: m_needRedraw = false;
339: }
340:
341: void GPBitmap::doDiamond(int x, int y, int px, int py) {
342: m_view->StrokeLine(BPoint(X(x) - px, Y(y)),BPoint(X(x), Y(y) - py));
343: m_view->StrokeLine(BPoint(X(x) + px, Y(y)));
344: m_view->StrokeLine(BPoint(X(x), Y(y) + py));
345: m_view->StrokeLine(BPoint(X(x) - px, Y(y)));
346: m_view->StrokeLine(BPoint(X(x), Y(y)),BPoint(X(x), Y(y)));
347: }
348:
349: void GPBitmap::doPlus(int x, int y, int px, int py) {
350: m_view->StrokeLine(BPoint(X(x) - px, Y(y)),BPoint(X(x) + px, Y(y)));
351: m_view->StrokeLine(BPoint(X(x), Y(y) - py),BPoint(X(x), Y(y) + py));
352: }
353:
354: void GPBitmap::doBox(int x, int y, int px, int py) {
355: m_view->StrokeRect(BRect(X(x) - px, Y(y) - py, (px + px), (py + py)));
356: m_view->StrokeLine(BPoint(X(x), Y(y)),BPoint(X(x), Y(y)));
357: }
358:
359: void GPBitmap::doCross(int x, int y, int px, int py) {
360: m_view->StrokeLine(BPoint(X(x) - px, Y(y) - py),BPoint(X(x) + px, Y(y) + py));
361: m_view->StrokeLine(BPoint(X(x) - px, Y(y) + py),BPoint(X(x) + px, Y(y) - py));
362: }
363:
364: void GPBitmap::doTriangle(int x, int y, int px, int py) {
365: short temp_x, temp_y;
366:
367: temp_x = (short) (1.33 * (double) px + 0.5);
368: temp_y = (short) (1.33 * (double) py + 0.5);
369:
370: m_view->StrokeLine(BPoint(X(x), Y(y) - temp_y), BPoint(X(x) + temp_x, Y(y) - temp_y + 2 * py));
371: m_view->StrokeLine(BPoint(X(x) - temp_x, Y(y) - temp_y + 2 * py));
372: m_view->StrokeLine(BPoint(X(x), Y(y) - temp_y));
373: m_view->StrokeLine(BPoint(X(x), Y(y)),BPoint(X(x), Y(y)));
374: }
375:
376: void GPBitmap::doStar(int x, int y, int px, int py) {
377: m_view->StrokeLine(BPoint(X(x)-px, Y(y)), BPoint(X(x) + px, Y(y)));
378: m_view->StrokeLine(BPoint(X(x), Y(y)-py), BPoint(X(x), Y(y)+py));
379: m_view->StrokeLine(BPoint(X(x)-px, Y(y)-py), BPoint(X(x)+px, Y(y)+py));
380: m_view->StrokeLine(BPoint(X(x)-px, Y(y)+py), BPoint(X(x)+px, Y(y)-py));
381: }
382:
383: void GPBitmap::doDot(int x, int y) {
384: m_view->StrokeLine(BPoint(X(x), Y(y)),BPoint(X(x), Y(y)));
385: }
386:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>