amath  1.8.5
Simple command line calculator
window_haiku.cpp
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #include "amath.h"
31 #include "amathc.h"
32 #include "window_haiku.h"
33 #include "lib/charbuf.h"
34 #include "lib/aengine.h"
35 #include "main/evaluator.h"
36 #include "loc/text.h"
37 
38 //http://git.netsurf-browser.org/netsurf.git/plain/frontends/beos/about.cpp
39 //https://notabug.org/Tsyesika/Runyu/src/master/RunyuWindow.cpp
40 
41 #if defined(HAIKU)
42 
43 #if __GNUC__ == 2
44 #pragma GCC diagnostic ignored "-Wno-multichar"
45 #endif
46 
47 #include <Application.h>
48 #include <ScrollView.h>
49 #include <LayoutBuilder.h>
50 #include <MenuBar.h>
51 #include <Window.h>
52 #include <private/interface/AboutWindow.h>
53 
54 HaikuWindow::HaikuWindow(const char *prompt, CharValidator *validator) :
55  ConsoleBase(prompt),
56  BWindow(
57  BRect(150, 150, 800, 600),
58  TXTTITLE,
59  B_TITLED_WINDOW,
60  B_ASYNCHRONOUS_CONTROLS
61  )
62 {
63  BRect textRect = Bounds();
64  textRect.OffsetTo(0, 0);
65  textRect.InsetBy(5, 5);
66 
67  BFont font(be_fixed_font);
68  textView = new HaikuTextView(
69  this,
70  Bounds(),
71  "TextView",
72  textRect,
73  &font, 0);
74  textView->SetStylable(true);
75  DetachMemSafe(textView);
76 
77  BScrollView *scrollView = new BScrollView(
78  TXTTITLE "ScrollView",
79  textView,
80  B_FOLLOW_ALL,
81  0,
82  false,
83  true);
84  DetachMemSafe(scrollView);
85  AddChild(scrollView);
86 
87  /*
88  BMenuBar* menuBar = new BMenuBar("MenuBar");
89  BLayoutBuilder::Menu<>(menuBar)
90  .AddMenu("File")
91  .AddItem("About", B_ABOUT_REQUESTED)
92  .AddItem("Quit", B_QUIT_REQUESTED, 'Q')
93  .End();
94  DetachMemSafe(menuBar);
95 
96  BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
97  .SetInsets(0)
98  .Add(menuBar)
99  .Add(scrollView)
100  .End();
101  */
102 }
103 
104 HaikuWindow::~HaikuWindow(void)
105 {
106 }
107 
108 void HaikuWindow::Exit(void)
109 {
110  be_app->PostMessage(B_QUIT_REQUESTED);
111 }
112 
113 void HaikuWindow::Start(void)
114 {
115  textView->MakeFocus(true);
116  StartMessage();
117  Prompt();
118  Show();
119 }
120 
121 void HaikuWindow::StartMessage(void)
122 {
123  text_run run;
124  run.font = be_bold_font;
125  run.offset = 0;
126  run.color.red = 0;
127  run.color.green = 0;
128  run.color.blue = 0;
129  run.color.alpha = 255;
130 
131  text_run_array runArray;
132  runArray.count = 1;
133  runArray.runs[0] = run;
134 
135  textView->Insert(INTROMSG, &runArray);
136 }
137 
138 void HaikuWindow::ShowAbout(void)
139 {
140  BAboutWindow* about = new BAboutWindow("About amath", "application/x-vnd.amath");
141  DetachMemSafe(about);
142 
143  CharBuffer *buf = new CharBuffer();
144  static const char* version = TXTTITLE SPACE RELDATESTAMP;
145  static const char* compiler = TXTCOMPMSG;
146  static const char* footer = "Details specified in license.";
147  int len = StrLen(version) + StrLen(compiler) + StrLen(footer) + 10;
148 
149  buf->EnsureSize(len);
150  buf->Append(version);
151  buf->Append(NEWLINE);
152  buf->Append(compiler);
153  buf->Append(NEWLINE);
154  buf->Append(NEWLINE);
155  buf->Append(footer);
156 
157  about->AddCopyright(2014, "Carsten Sonne Larsen");
158  about->AddExtraInfo(buf->GetString());
159  about->Show();
160 
161  delete buf;
162 }
163 
164 void HaikuWindow::ShowLicense(void)
165 {
166  // TODO: Show license in colors
167  ConsoleBase::ShowLicense();
168 }
169 
170 void HaikuWindow::ShowVersion(void)
171 {
172  text_run run;
173  run.font = be_bold_font;
174  run.offset = 0;
175  run.color.red = 34;
176  run.color.green = 139;
177  run.color.blue = 34;
178  run.color.alpha = 255;
179 
180  text_run_array runArray;
181  runArray.count = 1;
182  runArray.runs[0] = run;
183 
184  textView->Insert(GetVersionText(), &runArray);
185  textView->Insert(NEWLINE);
186 
187  runArray.runs[0].font = be_plain_font;
188  runArray.runs[0].font.SetFace(B_REGULAR_FACE);
189  runArray.runs[0].color.red = 0;
190  runArray.runs[0].color.green = 0;
191  runArray.runs[0].color.blue = 0;
192 
193  textView->Insert(GetCompilerText(), &runArray);
194  textView->Insert(NEWLINE);
195 }
196 
197 void HaikuWindow::Clear(void)
198 {
199  textView->SetText(EMPTYSTRING);
200 }
201 
202 void HaikuWindow::WriteString(const char *string)
203 {
204  text_run run;
205  run.font = be_fixed_font;
206  run.offset = 0;
207  run.color.red = 0;
208  run.color.green = 0;
209  run.color.blue = 0;
210  run.color.alpha = 255;
211 
212  text_run_array runArray;
213  runArray.count = 1;
214  runArray.runs[0] = run;
215 
216  textView->Insert(string, &runArray);
217 }
218 
219 void HaikuWindow::Execute(void)
220 {
221  int lineIndex = textView->CurrentLine();
222  int lineStart = textView->OffsetAt(lineIndex);
223  int lineCount = textView->CountLines();
224 
225  if (lineCount != lineIndex + 1)
226  {
227  return;
228  }
229 
230  int len = textView->TextLength() - lineStart;
231  char *line = new char[len + 1];
232  textView->GetText(lineStart, lineStart + len, line);
233  *(line + len) = '\0';
234 
235  char *input = line;
236  char *p = prompt;
237  while (*input == *p && *input != '\0')
238  {
239  input++;
240  p++;
241  }
242 
243  WriteString(NEWLINE);
244  Evaluator *evaluator = new Evaluator(input);
245  evaluator->Evaluate();
246  const char *out = evaluator->GetResult();
247  WriteString(out);
248 
249  delete evaluator;
250  delete line;
251 
252  Prompt();
253  textView->ScrollToOffset(textView->TextLength() - 1);
254 }
255 
256 void HaikuWindow::MessageReceived(BMessage* msg)
257 {
258  switch (msg->what)
259  {
260  case B_ABOUT_REQUESTED:
261  ShowAbout();
262  break;
263  default:
264  BWindow::MessageReceived(msg);
265  break;
266  }
267 }
268 
269 void HaikuWindow::FrameResized(float w, float h)
270 {
271  UpdateTextRect();
272 }
273 
274 void HaikuWindow::UpdateTextRect(void)
275 {
276  BRect rect(textView->Bounds());
277  rect.InsetBy(5, 5);
278  textView->SetTextRect(rect);
279 }
280 
281 bool HaikuWindow::QuitRequested()
282 {
283  Exit();
284  return true;
285 }
286 
287 HaikuTextView::HaikuTextView(
288  HaikuWindow *window,
289  BRect frame,
290  const char *name,
291  BRect textRect,
292  const BFont *initialFont,
293  const rgb_color *initialColor) :
294  BTextView(
295  frame,
296  name,
297  textRect,
298  initialFont,
299  initialColor,
300  B_FOLLOW_ALL, B_WILL_DRAW | B_PULSE_NEEDED
301  ), window(window)
302 {
303 }
304 
305 HaikuTextView::~HaikuTextView(void)
306 {
307 }
308 
309 void HaikuTextView::KeyDown(const char *bytes, int32 numBytes)
310 {
311  switch (*bytes)
312  {
313  case B_ENTER:
314  window->Execute();
315  break;
316  default:
317  BTextView::KeyDown(bytes, numBytes);
318  break;
319  }
320 }
321 
322 #endif