xref: /haiku/src/apps/deskcalc/CalcView.h (revision c5e9dd9b6872feb2b71f033caba7339fb3468e36)
1 /*
2  * Copyright 2006-2013, Haiku, Inc. All rights reserved.
3  * Copyright 1997, 1998 R3 Software Ltd. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  *
6  * Authors:
7  *		Stephan Aßmus, superstippi@gmx.de
8  *		Philippe Saint-Pierre, stpere@gmail.com
9  *		John Scipione, jscipione@gmail.com
10  *		Timothy Wayper, timmy@wunderbear.com
11  */
12 #ifndef _CALC_VIEW_H
13 #define _CALC_VIEW_H
14 
15 
16 #include <View.h>
17 
18 
19 enum {
20 	MSG_OPTIONS_AUTO_NUM_LOCK				= 'oanl',
21 	MSG_OPTIONS_ANGLE_MODE_RADIAN			= 'oamr',
22 	MSG_OPTIONS_ANGLE_MODE_DEGREE			= 'oamd',
23 	MSG_OPTIONS_KEYPAD_MODE_COMPACT			= 'okmc',
24 	MSG_OPTIONS_KEYPAD_MODE_BASIC			= 'okmb',
25 	MSG_OPTIONS_KEYPAD_MODE_SCIENTIFIC		= 'okms',
26 	MSG_UNFLASH_KEY							= 'uflk'
27 };
28 
29 static const float kMinimumWidthBasic		= 130.0f;
30 static const float kMaximumWidthBasic		= 400.0f;
31 static const float kMinimumHeightBasic		= 130.0f;
32 static const float kMaximumHeightBasic		= 400.0f;
33 
34 class BString;
35 class BMenuItem;
36 class BMessage;
37 class BMessageRunner;
38 class BPopUpMenu;
39 struct CalcOptions;
40 class CalcOptionsWindow;
41 class ExpressionTextView;
42 
43 _EXPORT
44 class CalcView : public BView {
45  public:
46 
47 	static	CalcView*			Instantiate(BMessage* archive);
48 
49 
50 								CalcView(BRect frame,
51 									rgb_color rgbBaseColor,
52 									BMessage* settings);
53 								CalcView(BMessage* archive);
54 	virtual						~CalcView();
55 
56 	virtual	void				AttachedToWindow();
57 	virtual	void				MessageReceived(BMessage* message);
58 	virtual	void				Draw(BRect updateRect);
59 	virtual	void				MouseDown(BPoint point);
60 	virtual	void				MouseUp(BPoint point);
61 	virtual	void				KeyDown(const char* bytes, int32 numBytes);
62 	virtual	void				MakeFocus(bool focused = true);
63 	virtual	void				FrameResized(float width, float height);
64 
65 			// Archive this view.
66 	virtual	status_t			Archive(BMessage* archive, bool deep) const;
67 
68 			// Cut contents of view to system clipboard.
69 			void				Cut();
70 
71 			// Copy contents of view to system clipboard.
72 			void				Copy();
73 
74 			// Paste contents of system clipboard to view.
75 			void				Paste(BMessage* message);
76 
77 			// Save current settings
78 			status_t			SaveSettings(BMessage* archive) const;
79 
80 			// Evaluate the expression
81 			void				Evaluate();
82 
83 			// Flash the key on the keypad
84 			void				FlashKey(const char* bytes, int32 numBytes);
85 
86 			// Toggle whether or not the Num Lock key starts on
87 			void				ToggleAutoNumlock(void);
88 
89 			// Set the angle mode to degrees or radians
90 			void				SetDegreeMode(bool degrees);
91 
92 			// Set the keypad mode
93 			void				SetKeypadMode(uint8 mode);
94 
95  private:
96 	static	status_t			_EvaluateThread(void* data);
97 			void				_Init(BMessage* settings);
98 			status_t			_LoadSettings(BMessage* archive);
99 			void				_ParseCalcDesc(const char** keypadDescription);
100 
101 			void				_PressKey(int key);
102 			void				_PressKey(const char* label);
103 			int32				_KeyForLabel(const char* label) const;
104 			void				_FlashKey(int32 key, uint32 flashFlags);
105 
106 			void				_Colorize();
107 
108 			void				_CreatePopUpMenu(bool addKeypadModeMenuItems);
109 
110 			BRect				_ExpressionRect() const;
111 			BRect				_KeypadRect() const;
112 
113 			void				_MarkKeypadItems(uint8 mode);
114 
115 			void				_FetchAppIcon(BBitmap* into);
116 			bool				_IsEmbedded();
117 
118 			void				_SetEnabled(bool enable);
119 
120 			// grid dimensions
121 			int16				fColumns;
122 			int16				fRows;
123 
124 			// color scheme
125 			rgb_color			fBaseColor;
126 			rgb_color			fLightColor;
127 			rgb_color			fDarkColor;
128 			rgb_color			fButtonTextColor;
129 			rgb_color			fExpressionBGColor;
130 			rgb_color			fExpressionTextColor;
131 
132 			bool				fHasCustomBaseColor;
133 
134 			// view dimensions
135 			float				fWidth;
136 			float				fHeight;
137 
138 			// keypad grid
139 			struct CalcKey;
140 
141 			const char**		fKeypadDescription;
142 			CalcKey*			fKeypad;
143 
144 			// icon
145 			BBitmap*			fCalcIcon;
146 
147 			// expression
148 			ExpressionTextView*	fExpressionTextView;
149 
150 			// pop-up context menu.
151 			BPopUpMenu*			fPopUpMenu;
152 			BMenuItem*			fAutoNumlockItem;
153 
154 			BMenuItem*			fAngleModeRadianItem;
155 			BMenuItem*			fAngleModeDegreeItem;
156 
157 			BMenuItem*			fKeypadModeCompactItem;
158 			BMenuItem*			fKeypadModeBasicItem;
159 			BMenuItem*			fKeypadModeScientificItem;
160 
161 			// calculator options.
162 			CalcOptions*		fOptions;
163 
164 			thread_id			fEvaluateThread;
165 			BMessageRunner*		fEvaluateMessageRunner;
166 			sem_id				fEvaluateSemaphore;
167 			bool				fEnabled;
168 };
169 
170 #endif // _CALC_VIEW_H
171