xref: /haiku/src/apps/sudoku/SudokuView.h (revision 99d027cd0238c1d86da86d7c3f4200509ccc61a6)
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 = ~0UL, uint32 y = ~0UL);
100 			void				_InvalidateKeyboardFocus(uint32 x, uint32 y);
101 			void				_InsertKey(char rawKey, int32 modifiers);
102 			void				_SetValueHintValue(uint32 value);
103 			void				_RemoveHint();
104 			bool				_GetHintFieldFor(BPoint where, uint32 x,
105 									uint32 y, uint32& hintX, uint32& hintY);
106 			bool				_GetFieldFor(BPoint where, uint32& x,
107 									uint32& y);
108 			void				_FitFont(BFont& font, float width,
109 									float height);
110 			void				_DrawKeyboardFocus();
111 			void				_DrawHints(uint32 x, uint32 y);
112 			void				_UndoRedo(BObjectList<BMessage>& undos,
113 									BObjectList<BMessage>& redos);
114 			void				_PushUndo();
115 
116 private:
117 			rgb_color			fBackgroundColor;
118 			SudokuField*		fField;
119 			BObjectList<BMessage> fUndos;
120 			BObjectList<BMessage> fRedos;
121 			uint32				fBlockSize;
122 			float				fWidth;
123 			float				fHeight;
124 			float				fBaseline;
125 			BFont				fFieldFont;
126 			BFont				fHintFont;
127 			float				fHintHeight;
128 			float				fHintWidth;
129 			float				fHintBaseline;
130 			uint32				fShowHintX;
131 			uint32				fShowHintY;
132 			uint32				fLastHintValue;
133 			bool				fLastHintValueSet;
134 			uint32				fValueHintValue;
135 			uint32				fLastField;
136 			uint32				fKeyboardX;
137 			uint32				fKeyboardY;
138 			uint32				fHintFlags;
139 			bool				fShowKeyboardFocus;
140 			bool				fShowCursor;
141 			bool				fEditable;
142 };
143 
144 
145 static const uint32 kMsgSudokuSolved = 'susl';
146 static const uint32 kMsgSolveSudoku = 'slvs';
147 static const uint32 kMsgSolveSingle = 'slsg';
148 
149 // you can observe these:
150 static const int32 kUndoRedoChanged = 'unre';
151 
152 
153 #endif	// SUDOKU_VIEW_H
154