xref: /haiku/src/apps/sudoku/SudokuView.h (revision 81ec973846ea4816c53ed8901822e43c8b06878d)
1 /*
2  * Copyright 2007-2014, 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 			void				InitObject(const BMessage* archive);
44 
45 			status_t			SaveState(BMessage& state) const;
46 
47 			status_t			SetTo(entry_ref& ref);
48 			status_t			SetTo(const char* data);
49 			status_t			SetTo(SudokuField* field);
50 
51 			status_t			SaveTo(entry_ref& ref,
52 									uint32 as = kExportAsText);
53 			status_t			SaveTo(BDataIO &to, uint32 as = kExportAsText);
54 
55 			status_t			CopyToClipboard();
56 
57 			void				ClearChanged();
58 			void				ClearAll();
59 
60 			void				SetHintFlags(uint32 flags);
61 			uint32				HintFlags() const { return fHintFlags; }
62 
63 			SudokuField*		Field() { return fField; }
64 
65 			void				SetEditable(bool editable);
66 			bool				Editable() const { return fEditable; }
67 
68 			bool				CanUndo() { return !fUndos.IsEmpty(); }
69 			bool				CanRedo() { return !fRedos.IsEmpty(); }
70 			void				Undo();
71 			void				Redo();
72 
73 protected:
74 	virtual	void				AttachedToWindow();
75 
76 	virtual void				FrameResized(float width, float height);
77 	virtual void				MouseDown(BPoint where);
78 	virtual void				MouseMoved(BPoint where, uint32 transit,
79 									const BMessage* dragMessage);
80 	virtual void				KeyDown(const char *bytes, int32 numBytes);
81 
82 	virtual void				MessageReceived(BMessage* message);
83 
84 	virtual void				Draw(BRect updateRect);
85 
86 private:
87 			status_t			_FilterString(const char* data,
88 									size_t dataLength, char* buffer,
89 									uint32& out, bool& ignore);
90 			void				_SetText(char* text, uint32 value);
91 			char				_BaseCharacter();
92 			bool				_ValidCharacter(char c);
93 			BPoint				_LeftTop(uint32 x, uint32 y);
94 			BRect				_Frame(uint32, uint32 y);
95 			void				_InvalidateHintField(uint32 x, uint32 y,
96 									uint32 hintX, uint32 hintY);
97 			void				_InvalidateField(uint32 x, uint32 y);
98 			void				_InvalidateValue(uint32 value,
99 									bool invalidateHint = false,
100 									uint32 x = UINT32_MAX,
101 									uint32 y = UINT32_MAX);
102 			void				_InvalidateKeyboardFocus(uint32 x, uint32 y);
103 			void				_InsertKey(char rawKey, int32 modifiers);
104 			void				_SetValueHintValue(uint32 value);
105 			void				_RemoveHint();
106 			bool				_GetHintFieldFor(BPoint where, uint32 x,
107 									uint32 y, uint32& hintX, uint32& hintY);
108 			bool				_GetFieldFor(BPoint where, uint32& x,
109 									uint32& y);
110 			void				_FitFont(BFont& font, float width,
111 									float height);
112 			void				_DrawKeyboardFocus();
113 			void				_DrawHints(uint32 x, uint32 y);
114 			void				_UndoRedo(BObjectList<BMessage>& undos,
115 									BObjectList<BMessage>& redos);
116 			void				_PushUndo();
117 			void				_SetAllHints();
118 			void				_Solve();
119 			void				_SolveSingle();
120 			bool				_GetSolutions(SudokuSolver& solver);
121 
122 private:
123 			rgb_color			fBackgroundColor;
124 			SudokuField*		fField;
125 			BObjectList<BMessage> fUndos;
126 			BObjectList<BMessage> fRedos;
127 			uint32				fBlockSize;
128 			float				fWidth;
129 			float				fHeight;
130 			float				fBaseline;
131 			BFont				fFieldFont;
132 			BFont				fHintFont;
133 			float				fHintHeight;
134 			float				fHintWidth;
135 			float				fHintBaseline;
136 			uint32				fShowHintX;
137 			uint32				fShowHintY;
138 			uint32				fLastHintValue;
139 			bool				fLastHintValueSet;
140 			uint32				fValueHintValue;
141 			uint32				fLastField;
142 			uint32				fKeyboardX;
143 			uint32				fKeyboardY;
144 			uint32				fHintFlags;
145 			bool				fShowKeyboardFocus;
146 			bool				fShowCursor;
147 			bool				fEditable;
148 };
149 
150 
151 static const uint32 kMsgSudokuSolved = 'susl';
152 static const uint32 kMsgSolveSudoku = 'slvs';
153 static const uint32 kMsgSolveSingle = 'slsg';
154 static const uint32 kMsgSetAllHints = 'salh';
155 
156 // you can observe these:
157 static const int32 kUndoRedoChanged = 'unre';
158 
159 
160 #endif	// SUDOKU_VIEW_H
161