xref: /haiku/src/apps/sudoku/SudokuView.h (revision 6d2f2ec177bf615a117a7428d71be4330545b320)
1 /*
2  * Copyright 2007-2015, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef SUDOKU_VIEW_H
6 #define SUDOKU_VIEW_H
7 
8 
9 #include <View.h>
10 #include <ObjectList.h>
11 
12 
13 class BDataIO;
14 class SudokuField;
15 class SudokuSolver;
16 struct entry_ref;
17 
18 
19 enum {
20 	kMarkValidHints = 0x01,
21 	kMarkInvalid = 0x02,
22 };
23 
24 
25 enum {
26 	kExportAsText,
27 	kExportAsHTML,
28 	kExportAsBitmap,
29 	kExportAsPicture
30 };
31 
32 
33 class SudokuView : public BView {
34 public:
35 								SudokuView(BRect frame, const char* name,
36 									const BMessage& settings,
37 									uint32 resizingMode);
38 								SudokuView(BMessage* archive);
39 	virtual						~SudokuView();
40 
41 	virtual	status_t			Archive(BMessage* into, bool deep = true) const;
42 	static	BArchivable*		Instantiate(BMessage* archive);
43 
44 			status_t			SaveState(BMessage& state) const;
45 
46 			status_t			SetTo(entry_ref& ref);
47 			status_t			SetTo(const char* data);
48 			status_t			SetTo(SudokuField* field);
49 
50 			status_t			SaveTo(entry_ref& ref,
51 									uint32 as = kExportAsText);
52 			status_t			SaveTo(BDataIO &to, uint32 as = kExportAsText);
53 
54 			status_t			CopyToClipboard();
55 
56 			void				ClearChanged();
57 			void				ClearAll();
58 
59 			void				SetHintFlags(uint32 flags);
60 			uint32				HintFlags() const { return fHintFlags; }
61 
62 			SudokuField*		Field() { return fField; }
63 
64 			void				SetEditable(bool editable);
65 			bool				Editable() const { return fEditable; }
66 
67 			bool				CanUndo() { return !fUndos.IsEmpty(); }
68 			bool				CanRedo() { return !fRedos.IsEmpty(); }
69 			void				Undo();
70 			void				Redo();
71 
72 protected:
73 	virtual	void				AttachedToWindow();
74 
75 	virtual void				FrameResized(float width, float height);
76 	virtual void				MouseDown(BPoint where);
77 	virtual void				MouseMoved(BPoint where, uint32 transit,
78 									const BMessage* dragMessage);
79 	virtual void				KeyDown(const char *bytes, int32 numBytes);
80 
81 	virtual void				MessageReceived(BMessage* message);
82 
83 	virtual void				Draw(BRect updateRect);
84 
85 private:
86 			void				_InitObject(const BMessage* archive);
87 
88 			status_t			_FilterString(const char* data,
89 									size_t dataLength, char* buffer,
90 									uint32& out, bool& ignore);
91 			void				_SetText(char* text, uint32 value);
92 			char				_BaseCharacter();
93 			bool				_ValidCharacter(char c);
94 			BPoint				_LeftTop(uint32 x, uint32 y);
95 			BRect				_Frame(uint32, uint32 y);
96 			void				_InvalidateHintField(uint32 x, uint32 y,
97 									uint32 hintX, uint32 hintY);
98 			void				_InvalidateField(uint32 x, uint32 y);
99 			void				_InvalidateValue(uint32 value,
100 									bool invalidateHint = false,
101 									uint32 x = UINT32_MAX,
102 									uint32 y = UINT32_MAX);
103 			void				_InvalidateKeyboardFocus(uint32 x, uint32 y);
104 			void				_InsertKey(char rawKey, int32 modifiers);
105 			bool				_GetHintFieldFor(BPoint where, uint32 x,
106 									uint32 y, uint32& hintX, uint32& hintY);
107 			bool				_GetFieldFor(BPoint where, uint32& x,
108 									uint32& y);
109 			void				_SetValue(uint32 x, uint32 y, uint32 value);
110 			void				_ToggleHintValue(uint32 x, uint32 y,
111 									uint32 hintX, uint32 hintY,
112 									uint32 value, uint32 field);
113 			void				_RemoveHintValues(uint32 atX, uint32 atY,
114 									uint32 value);
115 			void				_RemoveHintValue(uint32 x, uint32 y,
116 									uint32 valueMask);
117 			void				_SetAllHints();
118 			void				_Solve();
119 			void				_SolveSingle();
120 			bool				_GetSolutions(SudokuSolver& solver);
121 			void				_UndoRedo(BObjectList<BMessage>& undos,
122 									BObjectList<BMessage>& redos);
123 			void				_PushUndo();
124 			void				_SetValueHintValue(uint32 value);
125 			void				_RemoveHint();
126 			void				_FitFont(BFont& font, float width,
127 									float height);
128 			void				_DrawKeyboardFocus();
129 			void				_DrawHints(uint32 x, uint32 y);
130 
131 private:
132 			rgb_color			fBackgroundColor;
133 			SudokuField*		fField;
134 			BObjectList<BMessage> fUndos;
135 			BObjectList<BMessage> fRedos;
136 			uint32				fBlockSize;
137 			float				fWidth;
138 			float				fHeight;
139 			float				fBaseline;
140 			BFont				fFieldFont;
141 			BFont				fHintFont;
142 			float				fHintHeight;
143 			float				fHintWidth;
144 			float				fHintBaseline;
145 			uint32				fShowHintX;
146 			uint32				fShowHintY;
147 			uint32				fLastHintValue;
148 			bool				fLastHintValueSet;
149 			uint32				fValueHintValue;
150 			uint32				fLastField;
151 			uint32				fKeyboardX;
152 			uint32				fKeyboardY;
153 			uint32				fHintFlags;
154 			bool				fShowKeyboardFocus;
155 			bool				fShowCursor;
156 			bool				fEditable;
157 };
158 
159 
160 static const uint32 kMsgSudokuSolved = 'susl';
161 static const uint32 kMsgSolveSudoku = 'slvs';
162 static const uint32 kMsgSolveSingle = 'slsg';
163 static const uint32 kMsgSetAllHints = 'salh';
164 
165 // you can observe these:
166 static const int32 kUndoRedoChanged = 'unre';
167 
168 
169 #endif	// SUDOKU_VIEW_H
170