diff options
-rw-r--r-- | COPYING | 25 | ||||
-rwxr-xr-x | Calculator.cpp | 24 | ||||
-rwxr-xr-x | Calculator.h | 10 | ||||
-rw-r--r-- | CalculatorButton.cpp | 24 | ||||
-rw-r--r-- | CalculatorButton.h | 17 | ||||
-rw-r--r-- | CalculatorWindow.cpp | 133 | ||||
-rw-r--r-- | CalculatorWindow.h | 20 | ||||
-rwxr-xr-x | Makefile | 17 |
8 files changed, 270 insertions, 0 deletions
@@ -0,0 +1,25 @@ +* Copyright (c) 2007, Matt Turner +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of the <organization> nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/Calculator.cpp b/Calculator.cpp new file mode 100755 index 0000000..e823e25 --- /dev/null +++ b/Calculator.cpp @@ -0,0 +1,24 @@ +#include "Calculator.h" + +Calculator::Calculator(int argc, char* argv[]): + Application("application/x-vnd.Calculator", argc, argv) { + + Rect r(Point(100, 100), Point(275, 275)); + + CalculatorWindow * pCalculatorWindow = + new CalculatorWindow(r, "Calculator", WINDOW_LAYOUT_NOTHING, + APPLICATION_WINDOW_NO_VIEW | WINDOW_FLAG_NOT_SIZEABLE); + + pCalculatorWindow->GetTitleWindow()->SetFlags( + (TitleWindowFlags)(pCalculatorWindow->GetTitleWindow()->GetFlags()) + ); + + pCalculatorWindow->Show(); +} + +int main(int argc, char *argv[]) { + Calculator pCalculator(argc, argv); + + return pCalculator.Run(); +} + diff --git a/Calculator.h b/Calculator.h new file mode 100755 index 0000000..596ec1c --- /dev/null +++ b/Calculator.h @@ -0,0 +1,10 @@ +#include <gui/ApplicationWindow.h> +#include <gui/Application.h> + +using namespace SkyGI; + +class Calculator : public Application { + public: + Calculator(int argc, char *argv[]); +}; + diff --git a/CalculatorButton.cpp b/CalculatorButton.cpp new file mode 100644 index 0000000..59fcfe3 --- /dev/null +++ b/CalculatorButton.cpp @@ -0,0 +1,24 @@ +#include "CalculatorButton.h" + +CalculatorButton::CalculatorButton( Window * pParent, + const Rect rFrame, + const String &pLabel, + unsigned int nWindowLayoutFlags, + unsigned int uiVKey) +{ + pButton = new Button(pParent, rFrame, pLabel, nWindowLayoutFlags); + + Shortcut pShortcut(pButton, uiVKey, 0, KEY_QUAL_NONE); + pShortcut.Fire.Connect(this, &CalculatorButton::OnClicked); +} + +CalculatorButton::CalculatorButton( Window * pParent, + const Rect rFrame, + const String &pLabel, + unsigned int nWindowLayoutFlags) +{ + pButton = new Button(pParent, rFrame, pLabel, nWindowLayoutFlags); +} + +void CalculatorButton::OnClicked() { +} diff --git a/CalculatorButton.h b/CalculatorButton.h new file mode 100644 index 0000000..ad16d8c --- /dev/null +++ b/CalculatorButton.h @@ -0,0 +1,17 @@ + +class CalculatorButton : public Button { + public: + CalculatorButton::CalculatorButton( Window * pParent, + const Rect rFrame, + const String &pLabel, + unsigned int nWindowLayoutFlags, + unsigned int uiVKey); + CalculatorButton::CalculatorButton( Window * pParent, + const Rect rFrame, + const String &pLabel, + unsigned int nWindowLayoutFlags); + void OnClicked(); + private: + Button * pButton; +}; + diff --git a/CalculatorWindow.cpp b/CalculatorWindow.cpp new file mode 100644 index 0000000..ebbbc77 --- /dev/null +++ b/CalculatorWindow.cpp @@ -0,0 +1,133 @@ +#include "skyos/vkey.h" + +#include "CalculatorWindow.h" +#include "CalculatorButton.h" + +CalculatorWindow::CalculatorWindow( const Rect &rFrame, + const String& szTitle, + unsigned int nWindowLayoutFlags, + unsigned int uiFlags + ): ApplicationWindow(rFrame, szTitle, nWindowLayoutFlags, 0, uiFlags) +{ + Clear(); + + pText = new TextView(this, Rect(15, 30, 160, 50), WINDOW_LAYOUT_FOLLOW_RIGHT, TEXTVIEW_FLAG_SINGLE_LINE); + pText->Enable(false); + pText->Set("0.0"); + + CalculatorButton * pCalculatorButton; + + pCalculatorButton = new CalculatorButton(this, Rect(Point(15, 60), Point(40, 82)), "7", 0, VKEY_KEYPAD_7); + pCalculatorButton = new CalculatorButton(this, Rect(Point(45, 60), Point(70, 82)), "8", 0, VKEY_KEYPAD_8); + pCalculatorButton = new CalculatorButton(this, Rect(Point(75, 60), Point(100, 82)), "9", 0, VKEY_KEYPAD_9); + pCalculatorButton = new CalculatorButton(this, Rect(Point(105, 60), Point(160, 82)), "Clear", 0, VKEY_BACKSPACE); + + pCalculatorButton = new CalculatorButton(this, Rect(Point(15, 87), Point(40, 109)), "4", 0, VKEY_KEYPAD_4); + pCalculatorButton = new CalculatorButton(this, Rect(Point(45, 87), Point(70, 109)), "5", 0, VKEY_KEYPAD_5); + pCalculatorButton = new CalculatorButton(this, Rect(Point(75, 87), Point(100, 109)), "6", 0, VKEY_KEYPAD_6); + pCalculatorButton = new CalculatorButton(this, Rect(Point(105, 87), Point(130, 109)), "+", 0, VKEY_KEYPAD_PLUS); + pCalculatorButton = new CalculatorButton(this, Rect(Point(135, 87), Point(160, 109)), "-", 0, VKEY_KEYPAD_MINUS); + + pCalculatorButton = new CalculatorButton(this, Rect(Point(15, 114), Point(40, 136)), "1", 0, VKEY_KEYPAD_1); + pCalculatorButton = new CalculatorButton(this, Rect(Point(45, 114), Point(70, 136)), "2", 0, VKEY_KEYPAD_2); + pCalculatorButton = new CalculatorButton(this, Rect(Point(75, 114), Point(100, 136)), "3", 0, VKEY_KEYPAD_3); + pCalculatorButton = new CalculatorButton(this, Rect(Point(105, 114), Point(130, 136)), "\u00D7", 0, VKEY_KEYPAD_MULT); + pCalculatorButton = new CalculatorButton(this, Rect(Point(135, 114), Point(160, 136)), "\u00F7", 0, VKEY_KEYPAD_DIVIDE); + + pCalculatorButton = new CalculatorButton(this, Rect(Point(15, 141), Point(40, 163)), "0", 0, VKEY_KEYPAD_0); + pCalculatorButton = new CalculatorButton(this, Rect(Point(45, 141), Point(70, 163)), ".", 0, VKEY_KEYPAD_SPERATOR); // FIXME: SPERATOR (sic) + pCalculatorButton = new CalculatorButton(this, Rect(Point(75, 141), Point(100, 164)), "\u00B1", 0); + pCalculatorButton = new CalculatorButton(this, Rect(Point(105, 141), Point(160, 164)), "=", 0, VKEY_KEYPAD_ENTER); +} + +void CalculatorWindow::OnCommand(const MessageCommand *pMessage) { + const String minus = String("-"); + const String point = String("."); + char c[2] = {'0', '\0'}; + int message = pMessage->GetID(); + + line = pText->GetDocument()->GetLine(0); + + switch (message) { + case BUTTON_ZERO: + case BUTTON_ONE: + case BUTTON_TWO: + case BUTTON_THREE: + case BUTTON_FOUR: + case BUTTON_FIVE: + case BUTTON_SIX: + case BUTTON_SEVEN: + case BUTTON_EIGHT: + case BUTTON_NINE: + c[0] += message; + if (new_operand) { + pText->Set((const char *)&c); + new_operand = false; + } else { + pText->Append((const char *)&c); + } + break; + case BUTTON_PLUS: + case BUTTON_MINUS: + case BUTTON_TIMES: + case BUTTON_DIVIDE: + case BUTTON_EQUALS: + doOperation(message); + break; + case BUTTON_POINT: +/* if(line->find(point)) { // FIXME: figure out how to do this more directly + pText->Append(point); + }*/ + if (!strchr(line->c_str(), '.')) { + pText->Append(point); + } + break; + case BUTTON_SIGN: + if (line->c_str()[0] != '-') { + pText->Insert(minus, Point(0, 0)); + } else { + pText->Remove(Point(0,0)); + } + break; + case BUTTON_CLEAR: + Clear(); + pText->Set("0.0"); + break; + } + printf("line: %s; total: %.0f; operand: %.0f; operator: %d\n", line->c_str(), total, operand, operation); +} + +void CalculatorWindow::Clear() { + operation = BUTTON_PLUS; + total = 0.0; + operand = 0.0; + new_operand = true; +} + +void CalculatorWindow::doOperation(int next_operation) { + char str[32] = {0}; + + operand = atof(line->c_str()); + + switch (operation) { + case BUTTON_PLUS: + total += operand; + break; + case BUTTON_MINUS: + total -= operand; + break; + case BUTTON_TIMES: + total *= operand; + break; + case BUTTON_DIVIDE: + total /= operand; + break; + } + snprintf(str, 32, "%f", total); + pText->Set((const char *)&str); + + operation = next_operation; + operand = 0.0; + new_operand = true; +} + diff --git a/CalculatorWindow.h b/CalculatorWindow.h new file mode 100644 index 0000000..8a29e6a --- /dev/null +++ b/CalculatorWindow.h @@ -0,0 +1,20 @@ + +class CalculatorWindow : public ApplicationWindow { + public: + CalculatorWindow( const Rect &rFrame, + const String& szTitle, + unsigned int nWindowLayoutFlags, + unsigned int uiFlags = 0); + void OnCommand( const MessageCommand *pMessage); + private: + void Clear(); + void doOperation( int next_operation); + + TextView * pText; + String * line; + double total; + double operand; + int operation; + bool new_operand; +}; + diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..0106a82 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +CPP=g++ +WARN=-W -Wall +CFLAGS=-O2 ${WARN} +LDFLAGS=-los + +EXE=Calculator.app + +OBJS=CalculatorButton.o CalculatorWindow.o Calculator.o + +all: ${OBJS} + ${CPP} ${LDFLAGS} ${OBJS} -o ${EXE} + +%.o: %.cpp + ${CPP} ${CFLAGS} -c -o $@ $< + +clean: + rm ${OBJS} ${EXE} |