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