1 /* 2 * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 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 class SudokuField; 13 struct entry_ref; 14 15 16 enum { 17 kMarkValidHints = 0x01, 18 kMarkInvalid = 0x02, 19 }; 20 21 22 class SudokuView : public BView { 23 public: 24 SudokuView(BRect frame, const char* name, const BMessage& settings, 25 uint32 resizingMode); 26 virtual ~SudokuView(); 27 28 status_t SaveState(BMessage& state); 29 30 status_t SetTo(entry_ref& ref); 31 status_t SetTo(const char* data); 32 status_t SetTo(SudokuField* field); 33 34 status_t SaveTo(entry_ref& ref, bool asText); 35 36 void ClearChanged(); 37 void ClearAll(); 38 39 void SetHintFlags(uint32 flags); 40 uint32 HintFlags() const { return fHintFlags; } 41 42 SudokuField* Field() { return fField; } 43 44 void SetEditable(bool editable); 45 bool Editable() const { return fEditable; } 46 47 bool CanUndo() { return !fUndos.IsEmpty(); } 48 bool CanRedo() { return !fRedos.IsEmpty(); } 49 void Undo(); 50 void Redo(); 51 52 protected: 53 virtual void AttachedToWindow(); 54 55 virtual void FrameResized(float width, float height); 56 virtual void MouseDown(BPoint where); 57 virtual void MouseMoved(BPoint where, uint32 transit, 58 const BMessage* dragMessage); 59 virtual void KeyDown(const char *bytes, int32 numBytes); 60 61 virtual void MessageReceived(BMessage* message); 62 63 virtual void Draw(BRect updateRect); 64 65 private: 66 status_t _FilterString(const char* data, size_t dataLength, char* buffer, 67 uint32& out, bool& ignore); 68 void _SetText(char* text, uint32 value); 69 char _BaseCharacter(); 70 bool _ValidCharacter(char c); 71 BPoint _LeftTop(uint32 x, uint32 y); 72 BRect _Frame(uint32, uint32 y); 73 void _InvalidateHintField(uint32 x, uint32 y, uint32 hintX, uint32 hintY); 74 void _InvalidateField(uint32 x, uint32 y); 75 void _InvalidateKeyboardFocus(uint32 x, uint32 y); 76 void _InsertKey(char rawKey, int32 modifiers); 77 void _RemoveHint(); 78 bool _GetHintFieldFor(BPoint where, uint32 x, uint32 y, 79 uint32& hintX, uint32& hintY); 80 bool _GetFieldFor(BPoint where, uint32& x, uint32& y); 81 void _FitFont(BFont& font, float width, float height); 82 void _DrawKeyboardFocus(); 83 void _DrawHints(uint32 x, uint32 y); 84 void _UndoRedo(BObjectList<BMessage>& undos, BObjectList<BMessage>& redos); 85 void _PushUndo(); 86 87 rgb_color fBackgroundColor; 88 SudokuField* fField; 89 BObjectList<BMessage> fUndos; 90 BObjectList<BMessage> fRedos; 91 uint32 fBlockSize; 92 float fWidth, fHeight, fBaseline; 93 BFont fFieldFont; 94 BFont fHintFont; 95 float fHintHeight, fHintWidth, fHintBaseline; 96 uint32 fShowHintX, fShowHintY; 97 uint32 fLastHintValue; 98 bool fLastHintValueSet; 99 uint32 fLastField; 100 uint32 fKeyboardX, fKeyboardY; 101 uint32 fHintFlags; 102 bool fShowKeyboardFocus; 103 bool fShowCursor; 104 bool fEditable; 105 }; 106 107 static const uint32 kMsgSudokuSolved = 'susl'; 108 static const uint32 kMsgSolveSudoku = 'slvs'; 109 static const uint32 kMsgSolveSingle = 'slsg'; 110 111 // you can observe these: 112 static const int32 kUndoRedoChanged = 'unre'; 113 114 #endif // SUDOKU_VIEW_H 115