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