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