1 /* 2 * Copyright 2004-2018, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef DATA_VIEW_H 6 #define DATA_VIEW_H 7 8 9 #include <Path.h> 10 #include <String.h> 11 #include <View.h> 12 13 14 class DataEditor; 15 16 17 enum base_type { 18 kHexBase = 16, 19 kDecimalBase = 10 20 }; 21 22 23 enum view_focus { 24 kNoFocus, 25 kHexFocus, 26 kAsciiFocus 27 }; 28 29 30 class DataView : public BView { 31 public: 32 DataView(DataEditor& editor); 33 virtual ~DataView(); 34 35 virtual void DetachedFromWindow(); 36 virtual void AttachedToWindow(); 37 virtual void MessageReceived(BMessage* message); 38 virtual void Draw(BRect updateRect); 39 40 virtual void MouseDown(BPoint where); 41 virtual void MouseMoved(BPoint where, uint32 transit, 42 const BMessage* message); 43 virtual void MouseUp(BPoint where); 44 45 virtual void KeyDown(const char* bytes, int32 numBytes); 46 47 virtual void WindowActivated(bool active); 48 virtual void MakeFocus(bool focus); 49 virtual void FrameResized(float width, float height); 50 virtual void SetFont(const BFont* font, 51 uint32 properties = B_FONT_ALL); 52 virtual void GetPreferredSize(float* _width, float* _height); 53 FontSizeFitsBounds()54 bool FontSizeFitsBounds() const 55 { return fFitFontSize; } 56 float FontSize() const; 57 void SetFontSize(float point); 58 59 void UpdateScroller(); 60 61 void MakeVisible(int32 position); 62 void SetSelection(int32 start, int32 end, 63 view_focus focus = kNoFocus); 64 void GetSelection(int32& start, int32& end); 65 void InvalidateRange(int32 start, int32 end); 66 Base()67 base_type Base() const { return fBase; } 68 void SetBase(base_type type); 69 70 const uint8* DataAt(int32 start); 71 72 static int32 WidthForFontSize(float size); 73 74 private: 75 BRect DataBounds(bool inView = false) const; 76 BRect SelectionFrame(view_focus which, int32 start, 77 int32 end); 78 int32 PositionAt(view_focus focus, BPoint point, 79 view_focus* _newFocus = NULL); 80 81 void DrawSelectionFrame(view_focus which); 82 void DrawSelectionBlock(view_focus which, 83 int32 start, int32 end); 84 void DrawSelectionBlock(view_focus which); 85 void DrawSelection(bool frameOnly = false); 86 void SetActive(bool active); 87 void SetFocus(view_focus which); 88 89 void UpdateFromEditor(BMessage* message = NULL); 90 void ConvertLine(char* line, off_t offset, 91 const uint8* buffer, size_t size); 92 93 bool AcceptsDrop(const BMessage* message); 94 void InitiateDrag(view_focus focus); 95 void Copy(); 96 void Paste(); 97 98 private: 99 DataEditor& fEditor; 100 uint8* fData; 101 size_t fDataSize; 102 off_t fFileSize; 103 size_t fSizeInView; 104 off_t fOffset; 105 float fAscent; 106 int32 fFontHeight; 107 float fCharWidth; 108 view_focus fFocus; 109 base_type fBase; 110 bool fIsActive; 111 int32 fStart; 112 int32 fEnd; 113 int32 fMouseSelectionStart; 114 int32 fKeySelectionStart; 115 int32 fBitPosition; 116 bool fFitFontSize; 117 int32 fDragMessageSize; 118 int32 fStoredStart; 119 int32 fStoredEnd; 120 }; 121 122 123 static const uint32 kMsgBaseType = 'base'; 124 static const uint32 kMsgUpdateData = 'updt'; 125 static const uint32 kMsgSetSelection = 'ssel'; 126 127 // observer notices 128 static const uint32 kDataViewCursorPosition = 'curs'; 129 static const uint32 kDataViewSelection = 'dsel'; 130 static const uint32 kDataViewPreferredSize = 'dvps'; 131 132 extern bool is_valid_utf8(uint8* data, size_t size); 133 134 135 #endif /* DATA_VIEW_H */ 136