1/*
2 * Copyright 2011-2015, Rene Gollent, rene@gollent.com. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6#ifndef MEMORY_VIEW_H
7#define MEMORY_VIEW_H
8
9
10#include <set>
11
12#include <View.h>
13
14#include "Types.h"
15
16
17enum {
18	MSG_SET_HEX_MODE 	= 'sehe',
19	MSG_SET_ENDIAN_MODE	= 'seme',
20	MSG_SET_TEXT_MODE	= 'stme'
21};
22
23enum {
24	HexModeNone	= 0,
25	HexMode8BitInt,
26	HexMode16BitInt,
27	HexMode32BitInt,
28	HexMode64BitInt
29};
30
31enum {
32	EndianModeLittleEndian = 0,
33	EndianModeBigEndian = 1
34};
35
36enum {
37	TextModeNone = 0,
38	TextModeASCII
39};
40
41
42class BMessageRunner;
43
44class Team;
45class TeamMemoryBlock;
46
47
48class MemoryView : public BView {
49public:
50	class Listener;
51
52								MemoryView(::Team* team, Listener* listener);
53	virtual						~MemoryView();
54
55	static MemoryView*			Create(::Team* team, Listener* listener);
56									// throws
57
58			void				SetTargetAddress(TeamMemoryBlock* block,
59									target_addr_t address);
60			void				UnsetListener();
61
62	inline	bool				GetEditMode() const
63									{ return fEditMode; }
64			status_t			SetEditMode(bool enabled);
65
66	inline	void*				GetEditedData() const
67									{ return fEditableData; }
68
69			void				CommitChanges();
70			void				RevertChanges();
71
72	virtual	void				AttachedToWindow();
73	virtual void				Draw(BRect rect);
74	virtual void				FrameResized(float width, float height);
75	virtual void				KeyDown(const char* bytes, int32 numBytes);
76	virtual void				MakeFocus(bool isFocused);
77	virtual void				MessageReceived(BMessage* message);
78	virtual void				MouseDown(BPoint point);
79	virtual void				MouseMoved(BPoint point, uint32 transit,
80									const BMessage* dragMessage);
81	virtual void				MouseUp(BPoint point);
82			void				ScrollToSelection();
83	virtual void				TargetedByScrollView(BScrollView* scrollView);
84
85
86	virtual	BSize				MinSize();
87	virtual	BSize				PreferredSize();
88	virtual	BSize				MaxSize();
89
90private:
91	void						_Init();
92	void						_RecalcScrollBars();
93	void						_GetNextHexBlock(char* buffer,
94									int32 bufferSize,
95									const char* address) const;
96
97	int32						_GetOffsetAt(BPoint point) const;
98	BPoint						_GetPointForOffset(int32 offset) const;
99	void						_RecalcBounds();
100	float						_GetAddressDisplayWidth() const;
101
102	inline int32				_GetHexDigitsPerBlock() const
103									{ return 1 << fHexMode; };
104
105	void						_GetEditCaretRect(BRect& rect) const;
106	void						_GetSelectionRegion(BRegion& region) const;
107	void						_GetSelectedText(BString& text) const;
108	void						_CopySelectionToClipboard();
109
110	void						_HandleAutoScroll();
111	void						_ScrollByLines(int32 lineCount);
112	void						_HandleContextMenu(BPoint point);
113
114	status_t					_SetupEditableData();
115
116private:
117	typedef std::set<int32>		ModifiedIndexSet;
118
119private:
120	::Team*						fTeam;
121	TeamMemoryBlock*			fTargetBlock;
122	uint8*						fEditableData;
123	ModifiedIndexSet			fEditedOffsets;
124	target_addr_t				fTargetAddress;
125	bool						fEditMode;
126	bool						fEditLowNybble;
127	float						fCharWidth;
128	float						fLineHeight;
129	int32						fTargetAddressSize;
130	int32						fTextCharsPerLine;
131	int32						fHexBlocksPerLine;
132	int32						fCurrentEndianMode;
133	int32						fHexMode;
134	int32						fTextMode;
135	float						fHexLeft;
136	float						fHexRight;
137	float						fTextLeft;
138	float						fTextRight;
139	int32						fSelectionBase;
140	int32						fSelectionStart;
141	int32						fSelectionEnd;
142	BMessageRunner*				fScrollRunner;
143
144	bool						fTrackingMouse;
145
146	Listener*					fListener;
147};
148
149
150class MemoryView::Listener {
151public:
152	virtual						~Listener();
153
154	virtual	void				TargetAddressChanged(target_addr_t address)
155									= 0;
156
157	virtual	void				HexModeChanged(int32 newMode) = 0;
158	virtual	void				TextModeChanged(int32 newMode) = 0;
159	virtual	void				EndianModeChanged(int32 newMode) = 0;
160};
161
162
163#endif // MEMORY_VIEW_H
164