2 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
4 * Permission to use, copy, and distribute this software and its
5 * documentation for any purpose with or without fee is hereby granted,
6 * provided that the above copyright notice appear in all copies and
7 * that both that copyright notice and this permission notice appear
8 * in supporting documentation.
10 * Permission to modify the software is granted, but not the right to
11 * distribute the complete modified source code. Modifications are to
12 * be distributed as patches to the released version. Permission to
13 * distribute binaries produced by compiling modified sources is granted,
15 * 1. distribute the corresponding source modifications from the
16 * released version in the form of a patch file along with the binaries,
17 * 2. add special version identification to distinguish your version
18 * in addition to the base release version number,
19 * 3. provide your name and address as the primary contact for the
20 * support of your modified version, and
21 * 4. retain our contact information in regard to use of the base
23 * Permission to distribute the released version of the source code along
24 * with corresponding source modifications in the form of a patch file is
25 * granted with same provisions 2 through 4 for binary distributions.
27 * This software is provided "as is" without express or implied warranty
28 * to the extent permitted by applicable law.
31 #include <Application.h>
32 #include <Messenger.h>
40 #include <FilePanel.h>
44 #include <ScrollView.h>
49 #include "constants.h"
55 // Application's signature
57 const char *APP_SIGNATURE = "application/x-vnd.Xingo-gnuplotViewer";
59 BRect windowRect(50,50,600,400);
64 // The main() function's only real job in a basic BeOS
65 // application is to create the BApplication object
69 GPApp theApp; // The application object
77 // The constructor for the WEApp class. This
78 // will create our window.
81 : BApplication(APP_SIGNATURE) {
83 window_count = 0; // No windows yet
84 next_untitled_number = 1; // Next window is "Untitled 1"
86 // Create the Open file panel
87 // openPanel = new BFilePanel;
92 // GPApp::MessageReceived
94 // Handle incoming messages. In particular, handle the
95 // WINDOW_REGISTRY_ADD and WINDOW_REGISTRY_SUB messages.
97 void GPApp::MessageReceived(BMessage *message) {
98 switch(message->what) {
99 case WINDOW_REGISTRY_ADD:
101 bool need_id = false;
102 BMessage reply(WINDOW_REGISTRY_ADDED);
104 if (message->FindBool("need_id", &need_id) == B_OK) {
106 reply.AddInt32("new_window_number", next_untitled_number);
107 next_untitled_number++;
111 reply.AddRect("rect", windowRect);
112 windowRect.OffsetBy(20,20);
113 message->SendReply(&reply);
116 case WINDOW_REGISTRY_SUB:
123 // openPanel->Show(); // Show the file panel
126 BApplication::MessageReceived(message);
132 // GPApp::RefsReceived
134 // Handle a refs received message.
136 void GPApp::RefsReceived(BMessage *message) {
137 entry_ref ref; // The entry_ref to open
138 status_t err; // The error code
139 int32 ref_num; // The index into the ref list
141 // Loop through the ref list and open each one
145 if ((err = message->FindRef("refs", ref_num, &ref)) != B_OK) {
148 new GPWindow(windowRect, &ref);
154 void GPApp::ReadyToRun(void)
156 io_thread = spawn_thread(&io_loop, "gnuplot io_loop", B_LOW_PRIORITY, NULL);
157 resume_thread(io_thread);
160 int32 GPApp::io_loop(void* data)
162 static plot_struct plot_array[MAX_WINDOWS];
165 res = io_task(plot_array);
169 int32 GPApp::io_task(plot_struct *plot_array)
172 struct plot_struct *plot = plot_array;
174 BMessage msg(bmsgNewCmd);
177 while (fgets(buf, 256, fp)) {
178 // printf("Got : %s", buf);
180 case 'G': /* enter graphics mode */
182 //printf("entering gfx mode\n");
183 int plot_number = atoi(buf + 1); /* 0 if none specified */
185 if (plot_number < 0 || plot_number >= MAX_WINDOWS)
188 //printf("plot for window number %d\n", plot_number);
189 plot = plot_array + plot_number;
190 prepare_plot(plot, plot_number);
193 case 'E': /* leave graphics mode / suspend */
195 // BMessage msg(bmsgBitmapDirty);
196 msg.AddInt32("numcmds",cnt);
197 msg.AddPointer("cmds", plot->commands);
199 plot->window->PostMessage(&msg);
200 // printf("displaying %d cmds, %X at %X\n",cnt,plot->commands[0],&plot->commands[0]);
205 case 'R': /* leave x11 mode */
207 //printf("leaving gfx mode\n");
212 // msg.AddString("cmd",buf);
213 // plot->window->PostMessage(&msg);
214 store_command(buf, plot);
220 /* get here if fgets fails */
221 return (feof(fp) || ferror(fp)) ? 0 : 1;
224 void GPApp::prepare_plot(plot_struct *plot, int term_number)
228 for (i = 0; i < plot->ncommands; ++i)
229 free(plot->commands[i]);
232 if (!plot->posn_flags) {
233 /* first time this window has been used - use default or -geometry
236 plot->posn_flags = 1;
244 windowRect.Set(plot->x,plot->y,plot->width,plot->height);
245 plot->window = new GPWindow(windowRect);
247 BMessage msg(bmsgClrCmd);
248 plot->window->PostMessage(&msg);
252 void GPApp::display(plot_struct *plot)
254 BMessage msg(bmsgBitmapDirty);
256 plot->window->PostMessage(&msg);
259 /* store a command in a plot structure */
261 void GPApp::store_command(char *buffer, plot_struct *plot)
264 // BMessage msg(bmsgNewCmd);
266 // FPRINTF((stderr, "Store in %d : %s", plot - plot_array, buffer));
268 if (plot->ncommands >= plot->max_commands) {
269 plot->max_commands = plot->max_commands * 2 + 1;
270 plot->commands = (plot->commands)
271 ? (char **) realloc(plot->commands, plot->max_commands * sizeof(char *))
272 : (char **) malloc(sizeof(char *));
274 p = (char *) malloc((unsigned) strlen(buffer) + 1);
275 if (!plot->commands || !p) {
276 fputs("gnuplot: can't get memory. aborted.\n", stderr);
279 plot->commands[plot->ncommands++] = strcpy(p, buffer);
281 // msg.AddString("cmd",buffer);
282 // plot->window->PostMessage(&msg);