Initial release of Maemo 5 port of gnuplot
[gnuplot] / src / beos / GPWindow.cpp
1 /*[
2  * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
3  *
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.
9  *
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,
14  * provided you
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
22  *    software.
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.
26  *
27  * This software is provided "as is" without express or implied warranty
28  * to the extent permitted by applicable law.
29 ]*/
30
31 #include <Application.h>
32 #include <Messenger.h>
33 #include <Message.h>
34 #include <Alert.h>
35 #include <Roster.h>
36 #include <Window.h>
37 #include <View.h>
38 #include <MenuBar.h>
39 #include <Menu.h>
40 #include <MenuField.h>
41 #include <Entry.h>
42 #include <Path.h>
43 #include <Box.h>
44 #include <MenuItem.h>
45 #include <TextView.h>
46 #include <FilePanel.h>
47 #include <ScrollView.h>
48 #include <OutlineListView.h>
49 #include <string.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52
53 #include "constants.h"
54 #include "GPApp.h"
55 #include "GPBitmap.h"
56 #include "GPView.h"
57 #include "GPWindow.h"
58
59 // Constructs the window we'll be drawing into.
60 //
61 GPWindow::GPWindow(BRect frame)
62                         : BWindow(frame, " ", B_TITLED_WINDOW, 0) {
63         _InitWindow();
64         Show();
65 }
66
67
68 // Create a window from a file.
69 //
70 GPWindow::GPWindow(BRect frame, plot_struct *plot)
71                         : BWindow(frame, "Untitled ", B_TITLED_WINDOW, 0) {
72         
73         _InitWindow();
74         
75         Show();
76 }
77
78
79 void GPWindow::_InitWindow(void) {
80         BRect r, rtool, plotframe;
81         BMenu *menu;
82         BMenuItem *item;
83
84          
85         // Initialize variables
86         
87         savemessage = NULL;                     // No saved path yet
88         r = rtool = plotframe = Bounds();
89 //      rtool = Bounds();
90 //      plotframe = Bounds();
91         
92         // Add the menu bar
93         menubar = new BMenuBar(r, "menu_bar");
94
95         // Add File menu to menu bar
96         menu = new BMenu("File");
97         menu->AddItem(new BMenuItem("New", new BMessage(MENU_FILE_NEW), 'N'));
98         
99         menu->AddItem(item=new BMenuItem("Open" B_UTF8_ELLIPSIS, new BMessage(MENU_FILE_OPEN), 'O'));
100         item->SetTarget(be_app);
101         menu->AddItem(new BMenuItem("Close", new BMessage(MENU_FILE_CLOSE), 'W'));
102         menu->AddSeparatorItem();
103         menu->AddItem(saveitem=new BMenuItem("Save", new BMessage(MENU_FILE_SAVE), 'S'));
104         saveitem->SetEnabled(false);
105         menu->AddItem(new BMenuItem("Save as" B_UTF8_ELLIPSIS, new BMessage(MENU_FILE_SAVEAS)));
106                                         
107         menu->AddSeparatorItem();
108         menu->AddItem(item=new BMenuItem("Page Setup" B_UTF8_ELLIPSIS, new BMessage(MENU_FILE_PAGESETUP)));
109         item->SetEnabled(false);
110         menu->AddItem(item=new BMenuItem("Print" B_UTF8_ELLIPSIS, new BMessage(MENU_FILE_PRINT), 'P'));
111         item->SetEnabled(false);
112
113         menu->AddSeparatorItem();
114         menu->AddItem(new BMenuItem("Quit", new BMessage(MENU_FILE_QUIT), 'Q'));
115         menubar->AddItem(menu);
116         
117         // Attach the menu bar to the window
118         AddChild(menubar);
119         
120         // Add the plot view
121 //      plotframe.left +=rtool.right+5;
122         plotframe.top = menubar->Bounds().bottom+2;
123 //      plotframe.right -= B_V_SCROLL_BAR_WIDTH;
124 //      plotframe.bottom -= B_H_SCROLL_BAR_HEIGHT;
125         
126         BRect plotrect = plotframe;
127         plotrect.OffsetTo(B_ORIGIN);
128         r.InsetBy(3.0,3.0);
129         
130         plotview = new GPView(plotframe, B_FOLLOW_ALL_SIDES, B_WILL_DRAW|B_PULSE_NEEDED, NULL);
131 //      plotview = new BView(plotframe, "test", B_FOLLOW_ALL_SIDES, B_WILL_DRAW|B_PULSE_NEEDED);
132         AddChild(plotview);
133         //help menu
134         menu = new BMenu("Help");
135         menu->AddItem(new BMenuItem("Help",new BMessage(MENU_HELP_REQUESTED)));
136         menu->AddItem(new BMenuItem("About...",new BMessage(MENU_HELP_ABOUT)));
137         
138         menubar->AddItem(menu);
139         
140         // Create the save filepanel for this window
141         
142         savePanel = new BFilePanel(B_SAVE_PANEL, new BMessenger(this), NULL, B_FILE_NODE, false);
143
144         // Tell the application that there's one more window
145         // and get the number for this untitled window.
146         Register(true);
147         Minimize(false);                // So Show() doesn't really make it visible
148 }
149
150
151 //
152 // GPWindow::FrameResized
153 //
154 // Adjust the size of the BTextView's text rectangle
155 // when the window is resized.
156 //
157 void GPWindow::FrameResized(float width, float height) {
158         BRect plotrect = plotview->Bounds();
159         
160         plotrect.right = plotrect.left + (width - 3.0);
161 //      plotview->SetTextRect(plotrect);
162 }
163
164
165 //
166 // GPWindow::~GPWindow
167 //
168 // Destruct the window.  This calls Unregister().
169 //
170 GPWindow::~GPWindow() {
171         Unregister();
172         if (savemessage) {
173                 delete savemessage;
174         }
175         delete savePanel;
176 }
177
178
179 //
180 // GPWindow::MessageReceived
181 //
182 // Called when a message is received by our
183 // application.
184 //
185 void GPWindow::MessageReceived(BMessage *message) {
186
187         switch(message->what) {
188
189                 case WINDOW_REGISTRY_ADDED:
190                         {
191                                 char s[22];
192                                 BRect rect;
193                                 if (message->FindInt32("new_window_number", &window_id) == B_OK) {
194                                         if (!savemessage) {             // if it's untitled
195                                                 sprintf(s, "File%ld.html", window_id);
196                                                 SetTitle(s);
197                                         }
198                                 }
199                                 if (message->FindRect("rect", &rect) == B_OK) {
200                                         MoveTo(rect.LeftTop());
201                                         ResizeTo(rect.Width(), rect.Height());
202                                 }
203                                 Minimize(false);
204                         }
205                         break;
206                         
207                 case MENU_FILE_NEW:
208                         {
209                                 BRect r;
210                                 r = Frame();
211                                 new GPWindow(r);
212                         }
213                         break;
214                 
215                 case MENU_FILE_CLOSE:
216                         Quit();
217                         break;
218                 case MENU_FILE_QUIT:
219                         be_app->PostMessage(B_QUIT_REQUESTED);
220                         break;
221                 case MENU_FILE_SAVEAS:
222                         savePanel->Show();
223                         break;
224                 case MENU_FILE_SAVE:
225                         Save(NULL);
226                         break;
227                 case B_SAVE_REQUESTED:
228                         Save(message);
229                         break;
230                 
231                 case MENU_HELP_REQUESTED:{
232                 int arg_c;
233                 char **_arg;
234                 arg_c=1;
235                 _arg = (char **)malloc(sizeof(char *) * (arg_c+ 1));
236                 _arg[0]="/boot/home/peojects/WebEditor/help.html";
237                 _arg[1]=NULL;
238                 be_roster->Launch("application/x-vnd.Be-NPOS",arg_c,_arg,NULL);
239                 free(_arg);
240                 }       
241                 break; 
242                 
243                 case MENU_HELP_ABOUT:{
244                         BAlert          *alert;
245                         alert= new BAlert("About ", "WebEditor for BeOS™\n©François Jouen 1999.\ne-mail:jouen@epeire.univ-rouen.fr", "OK");
246                         alert->Go();
247         
248                 }       
249                 break;                          
250                                         
251 //              case B_MOUSE_UP:
252 //                      printf("gor something\n");
253 //              break;                          
254
255                 case bmsgNewCmd:
256                 case bmsgClrCmd:
257                 case bmsgBitmapDirty:
258                 case bmsgBitmapResize:
259 //                      printf("gor something\n");
260                         plotview->MessageReceived(message);
261                 break;                          
262                                         
263                 default:
264                         BWindow::MessageReceived(message);
265                         break;
266         }
267 }
268
269
270 //
271 // GPWindow::Register
272 //
273 // Since MessageWorld can have multiple windows and
274 // we need to know when there aren't any left so the
275 // application can be shut down, this function is used
276 // to tell the application that a new window has been
277 // opened.
278 //
279 // If the need_id argument is true, we'll specify true
280 // for the "need_id" field in the message we send; this
281 // will cause the application to send back a
282 // WINDOW_REGISTRY_ADDED message containing the window's
283 // unique ID number.  If this argument is false, we won't
284 // request an ID.
285 //
286 void GPWindow::Register(bool need_id) {
287         BMessenger messenger(APP_SIGNATURE);
288         BMessage message(WINDOW_REGISTRY_ADD);
289         
290         message.AddBool("need_id", need_id);
291         messenger.SendMessage(&message, this);
292 }
293
294
295 //
296 // GPWindow::Unregister
297 //
298 // Unregisters a window.  This tells the application that
299 // one fewer windows are open.  The application will
300 // automatically quit if the count goes to zero because
301 // of this call.
302 //
303 void GPWindow::Unregister(void) {
304         BMessenger messenger(APP_SIGNATURE);
305         
306         messenger.SendMessage(new BMessage(WINDOW_REGISTRY_SUB));
307 }
308
309
310 //
311 // GPWindow::QuitRequested
312 //
313 // Here we just give permission to close the window.
314 //
315 bool GPWindow::QuitRequested() {
316         return true;
317 }
318
319
320 //
321 // GPWindow::Save
322 //
323 // Save the contents of the window.  The message specifies
324 // where to save it (see BFilePanel in the Storage Kit chapter
325 // of the Be Book).
326 //
327 status_t GPWindow::Save(BMessage *message) {
328         entry_ref ref;          // For the directory to save into
329         status_t err = B_OK;            // For the return code
330
331         return err;
332 }