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