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