1 /***********************************(GPL)********************************
2 *   wxHexEditor is a hex edit tool for editing massive files in Linux   *
3 *   Copyright (C) 2010  Erdem U. Altinyurt                              *
4 *                                                                       *
5 *   This program is free software; you can redistribute it and/or       *
6 *   modify it under the terms of the GNU General Public License         *
7 *   as published by the Free Software Foundation; either version 2      *
8 *   of the License.                                                     *
9 *                                                                       *
10 *   This program is distributed in the hope that it will be useful,     *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
13 *   GNU General Public License for more details.                        *
14 *                                                                       *
15 *   You should have received a copy of the GNU General Public License   *
16 *   along with this program;                                            *
17 *   if not, write to the Free Software Foundation, Inc.,                *
18 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA        *
19 *                                                                       *
20 *               home  : www.wxhexeditor.org                             *
21 *               email : spamjunkeater@gmail.com                         *
22 *************************************************************************/
23 
24 #ifndef __wxHexEditorCtrl__
25 #define __wxHexEditorCtrl__
26 
27 #ifdef __WXOSX__
28    #include <wx/version.h>
29 	#if !wxCHECK_VERSION(3,0,0)
30 		#define DO_NOT_USE_THREAD_FOR_SCROLL
31 	#endif
32 #endif
33 #ifdef __WXMSW__  //WXMSW Stuck sometimes if thread on
34 	#define DO_NOT_USE_THREAD_FOR_SCROLL
35 #endif
36 
37 
38 #include <wx/xml/xml.h>
39 #include <wx/filename.h>
40 #include <wx/event.h>
41 #include <wx/config.h>
42 
43 #include "HexEditorCtrlGui.h"
44 #define SELECT_EVENT 50005
45 #define UNREDO_EVENT 50006
46 #define TAG_CHANGE_EVENT 50007
47 #define SEARCH_CHANGE_EVENT 50008
48 #define COMPARE_CHANGE_EVENT 50009
49 #define XORVIEW_EVENT 50010
50 #define RESET_STYLE_EVENT 50011
51 #define THREAD_UPDATE_EVENT 50012
52 #define REDRAW_EVENT 50013
53 
54 
55 WX_DECLARE_OBJARRAY(uint64_t, wxArrayUINT64);
56 
57 class Select{	//for using EventHandler
58 	public:
Select(wxEvtHandler * evth_)59 		Select( wxEvtHandler* evth_ ){
60 			StartOffset = EndOffset = 0;
61 			state = false;
62 			evth = evth_;
63 			}
GetSize(void)64 		uint64_t GetSize( void ){
65 			return abs(static_cast<int64_t>(EndOffset - StartOffset))+1;};	//for select byte 13 start=13, end=13
SetState(bool new_state)66 		void SetState( bool new_state ){
67 			state = new_state;
68 
69 			std::cout << "Send UpdateUI Event" << std::endl;
70 			wxUpdateUIEvent event;
71 			if( new_state )
72 				event.SetString( wxT("Selected") );
73 			else
74 				event.SetString( wxT("NotSelected") );
75 			event.SetId( SELECT_EVENT );//idFileRO
76 			evth->ProcessEvent( event );
77 			}
GetState()78 		bool GetState(){
79 			return state;
80 			}
GetStart(void)81 		uint64_t GetStart( void ){
82 			return StartOffset < EndOffset ? StartOffset : EndOffset;
83 			}
GetEnd(void)84 		uint64_t GetEnd( void ){
85 			return StartOffset > EndOffset ? StartOffset : EndOffset;
86 			}
87 
88 		uint64_t StartOffset;	//real selection start position
89 		uint64_t OriginalStartOffset;  //real selection start position backup for HexTextCTRL
90 		uint64_t EndOffset;		//real selection end position, included to select
91 	private:
92 		bool  state;
93 		wxEvtHandler* evth;
94 	};
95 
96 class wxHugeScrollBar: public wxEvtHandler{ //64bit wrapper for wxScrollbar
97    //friend class wxHexEditorCtrl;
98    public:
99       wxHugeScrollBar( wxScrollBar* m_scrollbar_ );
100 		~wxHugeScrollBar();
101 
Enable(bool en)102       void Enable( bool en ){ m_scrollbar->Enable(en); }
GetSize(void)103       wxSize GetSize( void ){ return m_scrollbar->GetSize(); }
GetRange(void)104       int64_t GetRange( void ){ return m_range; };
GetThumbPosition(void)105       int64_t GetThumbPosition( void ){ return m_thumb; }
106       void SetThumbPosition(int64_t setpos);
107       void SetScrollbar( int64_t Current_Position,int page_x, int64_t new_range, int pagesize, bool repaint=true );
108 		void OnOffsetScroll( wxScrollEvent& event );
109 
110    private:
111       uint64_t m_range;
112       uint64_t m_thumb;
113       wxScrollBar *m_scrollbar;
114    };
115 
116 class HexEditorCtrl: public HexEditorCtrlGui{
117 	public:
118 		HexEditorCtrl(wxWindow* parent, int id,
119 						const wxPoint& pos=wxDefaultPosition,
120 						const wxSize& size=wxDefaultSize,
121 						long style=0);
122 		~HexEditorCtrl( void );
123 		enum panels { HEX_CTRL=0x1, TEXT_CTRL=0x2, OFFSET_CTRL=0x4 } focus;
124 		class Select *select;
125 		enum IDS{ idTagAddSelection=2001,idTagEdit, idTagQuick };
126 		void ReadFromBuffer( uint64_t position, unsigned lenght, char *buffer, bool cursor_reset = true, bool paint = true );
127 		void RePaint( void );
128 		int64_t CursorOffset( void );
129 		void SetFont( wxFont f );
130 		void SetFont( );
131 		void SetStyle( );
132 		void ControlShow( panels control, bool hide );
133 		bool ControlIsShown(panels control);
134       //void OnOffsetScroll( wxScrollEvent& event );
135 		wxHugeScrollBar* offset_scroll;
136 
137 		ArrayOfTAG CompareArray;
138 		ArrayOfTAG HighlightArray;
139 		ArrayOfTAG MainTagArray;
140 		wxFont stdfont;
141 		bool ZebraEnable;
142 		int *ZebraStriping;
143 
144 		bool LoadTAGS( wxFileName );
145 		bool SaveTAGS( wxFileName );
146 	protected:
147 		void Dynamic_Connector();
148 		void Dynamic_Disconnector();
149 		void Clear( bool RePaint=false, bool cursor_reset=true );
150 //		DECLARE_EVENT_TABLE()
151 		virtual void OnResize( wxSizeEvent& event);
152 		void ClearPaint( void );
153 		void PaintSelection( void );
154 		void PreparePaintTAGs( void );
155 		void PaintTAGsPrefilter( ArrayOfTAG& Arr );
156 		void PushTAGToControls( TagElement* TAG);
157 //		void TagPaint( void );
158 		bool TAGMutex;
159 		void MoveTAGS( uint64_t location, int64_t size );
160 		uint8_t GetCharToHexSize();
161 
162 	public:
163 		bool Selector( void );
164 		bool Select( uint64_t start_offset, uint64_t end_offset );
165 
166 	protected:
167 		void HexCharReplace( long char_location, const wxChar chr);
168 		void TextCharReplace( long char_location, const wxChar chr);
169 		void MyFreeze();
170 		void MyThaw();
171 		void ShowContextMenu(const wxMouseEvent& event);
172 		//------EVENTS---------//
173 		void OnMouseLeft(wxMouseEvent& event);
174 		void OnMouseMove( wxMouseEvent& event );
175 		void OnMouseSelectionEnd( wxMouseEvent& event );
176 		void OnMouseRight( wxMouseEvent& event );
177 		void TagCreator( bool QuickTag );
178 		void OnTagAddSelection( wxCommandEvent& event );
179 		void OnTagQuick( wxCommandEvent& event );
180 		void OnTagEdit( wxCommandEvent& event );
181 		void OnFocus( wxFocusEvent& event );
182 		void OnKillFocus( wxFocusEvent& event );
183 		void UpdateUI(wxUpdateUIEvent& event);
184 
185 		uint64_t LastRightClickAt;
186 		wxWindow* LastFocused;
187 		//----ADAPTERS----//
188 	public:
189 		void TagHideAll( void );
HexPerLine(void)190 		int HexPerLine( void )  { return (hex_ctrl->IsShown() ? hex_ctrl->CharacterPerLine() : text_ctrl->CharacterPerLine()*2); }
BytePerLine(void)191 		int BytePerLine( void )	{ return (hex_ctrl->IsShown() ? hex_ctrl->BytePerLine() : text_ctrl->BytePerLine() ); }
ByteCapacity(void)192 		unsigned ByteCapacity( void ){ return (hex_ctrl->IsShown() ? hex_ctrl->ByteCapacity() : text_ctrl->ByteCapacity()); }
LineCount(void)193 		int LineCount( void )	{ return (hex_ctrl->IsShown() ? hex_ctrl->LineCount() : text_ctrl->LineCount()); }
ActiveLine(void)194 		int ActiveLine( void )	{ return (hex_ctrl->IsShown() ? hex_ctrl->ActiveLine() : text_ctrl->ActiveLine() ) ; }
GetByteCount(void)195 		int GetByteCount( void ){ return (hex_ctrl->IsShown() ? hex_ctrl->GetByteCount() : text_ctrl->GetByteCount()); }
GetLastPosition(void)196 		int GetLastPosition( void ){ return (hex_ctrl->IsShown() ? hex_ctrl->GetLastPosition() : text_ctrl->GetLastPosition()); }
197 		int GetLocalHexInsertionPoint( void );
198 		int GetLocalInsertionPoint( void );
GetFormatedOffsetString(uint64_t offset)199 		wxString GetFormatedOffsetString( uint64_t offset ){ return offset_ctrl->GetFormatedOffsetString( offset ); }
200 virtual void SetLocalHexInsertionPoint( int hex_location );
201 		void OnOffsetMouse( wxMouseEvent& event );
202 		int sector_size;
203 		wxArrayUINT64 ProcessRAMMap;
204 		uint64_t ProcessRAM_GetFootPrint();
205 		uint64_t ProcessRAM_GetVirtualOffset( uint64_t offset );
206 		uint64_t ProcessRAM_FindNextMap( uint64_t current_offset, bool backward=false );
207 		bool ProcessRAM_FindMap( uint64_t current_offset, uint64_t& start, uint64_t& end, bool backward=false);
208 
209 	protected:
210 		int64_t page_offset;	//holds current start offset of file. Declared signed for error checking.
211 /*
212 virtual int ToExactPosition( int InternalPosition );
213 virtual int ToInternalPosition( int ExactPosition );
214 virtual int PixelCoordToInternalPosition( wxPoint mouse );
215 		wxPoint InternalPositionToExactCoord( int position );
216 		wxPoint PixelCoordToInternalCoord( wxPoint mouse );
217 */
218 //		virtual void OnKeyboardChar( wxKeyEvent& event );
219 //		virtual void OnKeyboardInput( wxKeyEvent& event );
220 	};
221 #endif
222