xref: /haiku/src/apps/sudoku/SudokuView.h (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2007-2012, 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 struct entry_ref;
16 
17 
18 enum {
19 	kMarkValidHints = 0x01,
20 	kMarkInvalid = 0x02,
21 };
22 
23 
24 enum {
25 	kExportAsText,
26 	kExportAsHTML,
27 	kExportAsBitmap,
28 	kExportAsPicture
29 };
30 
31 
32 class SudokuView : public BView {
33 public:
34 								SudokuView(BRect frame, const char* name,
35 									const BMessage& settings,
36 									uint32 resizingMode);
37 								SudokuView(BMessage* archive);
38 	virtual						~SudokuView();
39 
40 	virtual	status_t			Archive(BMessage* into, bool deep = true) const;
41 	static	BArchivable*		Instantiate(BMessage* archive);
42 			void				InitObject(const 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 			status_t			_FilterString(const char* data,
87 									size_t dataLength, char* buffer,
88 									uint32& out, bool& ignore);
89 			void				_SetText(char* text, uint32 value);
90 			char				_BaseCharacter();
91 			bool				_ValidCharacter(char c);
92 			BPoint				_LeftTop(uint32 x, uint32 y);
93 			BRect				_Frame(uint32, uint32 y);
94 			void				_InvalidateHintField(uint32 x, uint32 y,
95 									uint32 hintX, uint32 hintY);
96 			void				_InvalidateField(uint32 x, uint32 y);
97 			void				_InvalidateValue(uint32 value,
98 									bool invalidateHint = false,
99 									uint32 x = UINT32_MAX,
100 									uint32 y = UINT32_MAX);
101 			void				_InvalidateKeyboardFocus(uint32 x, uint32 y);
102 			void				_InsertKey(char rawKey, int32 modifiers);
103 			void				_SetValueHintValue(uint32 value);
104 			void				_RemoveHint();
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				_FitFont(BFont& font, float width,
110 									float height);
111 			void				_DrawKeyboardFocus();
112 			void				_DrawHints(uint32 x, uint32 y);
113 			void				_UndoRedo(BObjectList<BMessage>& undos,
114 									BObjectList<BMessage>& redos);
115 			void				_PushUndo();
116 
117 private:
118 			rgb_color			fBackgroundColor;
119 			SudokuField*		fField;
120 			BObjectList<BMessage> fUndos;
121 			BObjectList<BMessage> fRedos;
122 			uint32				fBlockSize;
123 			float				fWidth;
124 			float				fHeight;
125 			float				fBaseline;
126 			BFont				fFieldFont;
127 			BFont				fHintFont;
128 			float				fHintHeight;
129 			float				fHintWidth;
130 			float				fHintBaseline;
131 			uint32				fShowHintX;
132 			uint32				fShowHintY;
133 			uint32				fLastHintValue;
134 			bool				fLastHintValueSet;
135 			uint32				fValueHintValue;
136 			uint32				fLastField;
137 			uint32				fKeyboardX;
138 			uint32				fKeyboardY;
139 			uint32				fHintFlags;
140 			bool				fShowKeyboardFocus;
141 			bool				fShowCursor;
142 			bool				fEditable;
143 };
144 
145 
146 static const uint32 kMsgSudokuSolved = 'susl';
147 static const uint32 kMsgSolveSudoku = 'slvs';
148 static const uint32 kMsgSolveSingle = 'slsg';
149 
150 // you can observe these:
151 static const int32 kUndoRedoChanged = 'unre';
152 
153 
154 #endif	// SUDOKU_VIEW_H
155