xref: /haiku/src/apps/deskcalc/CalcView.h (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
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 class _EXPORT CalcView : public BView {
44  public:
45 
46 	static	CalcView*			Instantiate(BMessage* archive);
47 
48 
49 								CalcView(BRect frame,
50 									rgb_color rgbBaseColor,
51 									BMessage* settings);
52 								CalcView(BMessage* archive);
53 	virtual						~CalcView();
54 
55 	virtual	void				AttachedToWindow();
56 	virtual	void				MessageReceived(BMessage* message);
57 	virtual	void				Draw(BRect updateRect);
58 	virtual	void				MouseDown(BPoint point);
59 	virtual	void				MouseUp(BPoint point);
60 	virtual	void				KeyDown(const char* bytes, int32 numBytes);
61 	virtual	void				MakeFocus(bool focused = true);
62 	virtual	void				FrameResized(float width, float height);
63 
64 			// Archive this view.
65 	virtual	status_t			Archive(BMessage* archive, bool deep) const;
66 
67 			// Cut contents of view to system clipboard.
68 			void				Cut();
69 
70 			// Copy contents of view to system clipboard.
71 			void				Copy();
72 
73 			// Paste contents of system clipboard to view.
74 			void				Paste(BMessage* message);
75 
76 			// Save current settings
77 			status_t			SaveSettings(BMessage* archive) const;
78 
79 			// Evaluate the expression
80 			void				Evaluate();
81 
82 			// Flash the key on the keypad
83 			void				FlashKey(const char* bytes, int32 numBytes);
84 
85 			// Toggle whether or not the Num Lock key starts on
86 			void				ToggleAutoNumlock(void);
87 
88 			// Set the angle mode to degrees or radians
89 			void				SetDegreeMode(bool degrees);
90 
91 			// Set the keypad mode
92 			void				SetKeypadMode(uint8 mode);
93 
94  private:
95 	static	status_t			_EvaluateThread(void* data);
96 			void				_Init(BMessage* settings);
97 			status_t			_LoadSettings(BMessage* archive);
98 			void				_ParseCalcDesc(const char** keypadDescription);
99 
100 			void				_PressKey(int key);
101 			void				_PressKey(const char* label);
102 			int32				_KeyForLabel(const char* label) const;
103 			void				_FlashKey(int32 key, uint32 flashFlags);
104 
105 			void				_Colorize();
106 
107 			void				_CreatePopUpMenu(bool addKeypadModeMenuItems);
108 
109 			BRect				_ExpressionRect() const;
110 			BRect				_KeypadRect() const;
111 
112 			void				_MarkKeypadItems(uint8 mode);
113 
114 			void				_FetchAppIcon(BBitmap* into);
115 			bool				_IsEmbedded();
116 
117 			void				_SetEnabled(bool enable);
118 
119 			// grid dimensions
120 			int16				fColumns;
121 			int16				fRows;
122 
123 			// color scheme
124 			rgb_color			fBaseColor;
125 			rgb_color			fButtonTextColor;
126 			rgb_color			fExpressionBGColor;
127 			rgb_color			fExpressionTextColor;
128 
129 			bool				fHasCustomBaseColor;
130 
131 			// view dimensions
132 			float				fWidth;
133 			float				fHeight;
134 
135 			// keypad grid
136 			struct CalcKey;
137 
138 			const char**		fKeypadDescription;
139 			CalcKey*			fKeypad;
140 
141 			// icon
142 			BBitmap*			fCalcIcon;
143 
144 			// expression
145 			ExpressionTextView*	fExpressionTextView;
146 
147 			// pop-up context menu.
148 			BPopUpMenu*			fPopUpMenu;
149 			BMenuItem*			fAutoNumlockItem;
150 
151 			BMenuItem*			fAngleModeRadianItem;
152 			BMenuItem*			fAngleModeDegreeItem;
153 
154 			BMenuItem*			fKeypadModeCompactItem;
155 			BMenuItem*			fKeypadModeBasicItem;
156 			BMenuItem*			fKeypadModeScientificItem;
157 
158 			// calculator options.
159 			CalcOptions*		fOptions;
160 
161 			thread_id			fEvaluateThread;
162 			BMessageRunner*		fEvaluateMessageRunner;
163 			sem_id				fEvaluateSemaphore;
164 			bool				fEnabled;
165 };
166 
167 #endif // _CALC_VIEW_H
168