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 __wxHexCtrl__
25 #define __wxHexCtrl__
26 #include <stdint.h>
27 #include <wx/defs.h>
28 #include <wx/buffer.h>
29 #include <wx/graphics.h>
30 #include <wx/textctrl.h>
31 #include <wx/caret.h>
32 #include <wx/wx.h>
33 #include <wx/config.h>
34 #include <wx/fileconf.h>
35 #include <wx/dcbuffer.h>
36 #include <wx/clipbrd.h>
37
38 #include "Tag.h"
39
40 #define __idTagAddSelect__ 1500
41 #define __idTagEdit__ 1501
42 #define __idOffsetHex__ 1502
43
44 //#define _Use_Graphics_Contex_
45 wxArrayString GetSupportedEncodings();
46 inline wxChar CP473toUnicode( unsigned char ch );
47 inline wxString CP473toUnicode( wxString& line );
48 int atoh( const char );
49
50 WX_DEFINE_ARRAY(TagElement*, ArrayOfTAG);
51
52 class wxHexCtrl : public wxScrolledWindow{
53 public:
54 // wxHexCtrl() { }
wxHexCtrl(wxWindow * parent)55 wxHexCtrl( wxWindow *parent ){};
56
57 wxHexCtrl( wxWindow *parent,
58 wxWindowID id,
59 const wxString &value = wxEmptyString,
60 const wxPoint &pos = wxDefaultPosition,
61 const wxSize &size = wxDefaultSize,
62 long style = 0,
63 const wxValidator& validator = wxDefaultValidator);
64 ~wxHexCtrl();
CharAt(unsigned offset)65 wxChar CharAt(unsigned offset){
66 if( offset >= m_text.Len() ){
67 //std::cout << "Buff lower for offset : " << offset << std::endl;
68 return 0;
69 }
70 return m_text.GetChar(offset);}
71
72 // Operations
73 void SetFormat( wxString fmt );
74 wxString GetFormat( void );
75 virtual void SetDefaultStyle( wxTextAttr& new_attr );
76 void SetSelectionStyle( wxTextAttr& new_attr );
77 virtual void CreateCaret( void );
78 void MoveCaret( int x );
79 void MoveCaret( wxPoint p );
80 void Clear( bool ClearDC=true, bool cursor_reset=true );
81 void SetBinValue( wxString buffer, bool repaint = true );
82 void SetBinValue( char* buffer, int byte_count, bool repaint = true );
83 void SetValue( wxString buffer, bool repaint = true );
84 wxString GetValue( void );
85 long ReadBytes( char* buffer, int start_location, int byte_count, bool no_repaint = false );
86 char ReadByte(int byte_location);
87 static wxMemoryBuffer HexToBin(const wxString& HexValue);
88 virtual void Replace(unsigned from, unsigned to, const wxString& value);
89 virtual void Replace(unsigned hex_location, const wxChar& value, bool paint=true);
90 void WriteByte( const unsigned char& byte );
91 void WriteHex( const wxString& text_value );
92
93 // Caret Movement
94 wxCaret* mycaret;
95 void Home( bool MoveCaret = true );
96 void End( bool MoveCaret = true );
97 void FirstLine( bool MoveCaret = true );
98 void PrevChar( bool MoveCaret = true );
99 void NextChar( bool MoveCaret = true );
100 void PrevLine( bool MoveCaret = true );
101 void NextLine( bool MoveCaret = true );
102 void LastLine( bool MoveCaret = true );
103 void DoMoveCaret(); // move the caret to m_Caret.x, m_Caret.y
104
105 // Shaper Classes, All other classes has to be depended to this function for proper action!
106 bool IsDeniedCache[1024];//cache, Enought for this days...
107 int CPL;
IsDenied()108 virtual bool IsDenied() { return IsDenied( m_Caret.x );}
109 virtual bool IsDenied( int x );
110 virtual bool IsDenied_NoCache( int x );
111 virtual bool IsAllowedChar(const char& chr);
112 //virtual const char Filter(const char& ch);
113 int xCountDenied( int x );
GetCharSize()114 wxSize GetCharSize(){return m_CharSize;}
115
116 // Movement Support
117 virtual void DrawCursorShadow(wxDC*);
118 virtual int CharacterPerLine( bool NoCache=false );
BytePerLine(void)119 virtual int BytePerLine( void ){ return CharacterPerLine() / 2; }
ByteCapacity(void)120 virtual int ByteCapacity( void ){ return m_Window.y*BytePerLine(); }
GetByteCount(void)121 virtual int GetByteCount( void ){ return m_text.Length()/2; }
LineCount(void)122 int LineCount( void ) { return m_Window.y; }
ActiveLine(void)123 int ActiveLine( void ) { return m_Caret.y+1; } //ReAllocated, start from 1, NOT 0
124 int GetInsertionPoint( void );
125 virtual void SetInsertionPoint( unsigned int pos );
GetLastPosition(void)126 int GetLastPosition( void ){ return m_text.Length() - 1; }
127 virtual int ToVisiblePosition( int InternalPosition );
128 virtual int ToInternalPosition( int VisiblePosition );
129 virtual int PixelCoordToInternalPosition( wxPoint mouse );
130 wxPoint InternalPositionToVisibleCoord( int position );
131 wxPoint PixelCoordToInternalCoord( wxPoint mouse );
132
133 // TAG Support and Selection
134 ArrayOfTAG TagArray;
135 wxArrayInt ThinSeparationLines;
136 TagElement* GetTagByPix( wxPoint PixelCoord );
137 void SetSelection( unsigned start, unsigned end );
138 void ClearSelection( bool RePaint = true );
139 struct selector: public TagElement{ //select
140 bool selected; //select available variable
141 } select;
142 virtual void TagPainter( wxDC* DC, TagElement& TG );
143 virtual void TagPainterGC( wxGraphicsContext* gc, TagElement& TG );
144 void RePaint( void );
145
146 inline void DrawSeperationLineAfterChar( wxDC* DC, int offset );
147 void OnTagHideAll( void );
148 bool *TagMutex;
149 int *ZebraStriping;
150
151 protected:
152 wxDC* UpdateDC( wxDC* dc=NULL);
153 wxMemoryDC* CreateDC( void );
154 wxMemoryDC* internalBufferDC;
155 wxBitmap* internalBufferBMP;
156 wxString HexFormat;
157 bool DrawCharByChar;
158 int CtrlType;
159 // event handlers
160 wxPoint LastRightClickPosition; //Holds last right click for TagEdit function
161 void OnPaint( wxPaintEvent &event );
162 void OnSize( wxSizeEvent &event );
163 void OnChar( wxKeyEvent &event );
164 virtual void OnMouseLeft( wxMouseEvent& event );
165 virtual void OnMouseRight( wxMouseEvent& event );
166 virtual void OnMouseMove( wxMouseEvent& event );
167 void OnKillFocus( wxFocusEvent& event ); //tempotarily publicized
168 void OnFocus( wxFocusEvent& event );
169
170 void OnTagAddSelection( wxCommandEvent &event );
171 void OnTagEdit( wxCommandEvent &event );
172 void ShowContextMenu( wxPoint pos );
173 DECLARE_EVENT_TABLE();
174
175 void OnTestCall(void); // 4 Test
176 public:
177 virtual void ChangeSize(); // update the geometry
178 protected:
179 wxPoint m_Margin; // the margin around the text (looks nicer)
180 wxPoint m_Caret; // position (in text coords) of the caret
181 wxPoint m_Window; // the size (in text coords) of the window
182 wxString m_text;
183 wxTextAttr HexDefaultAttr;
184 wxMutex PaintMutex;
185 //wxTextAttr HexSelectAttr;
186
187 wxSize m_CharSize; // size (in pixels) of one character
188 // DECLARE_DYNAMIC_CLASS(wxHexCtrl)
189 };
190
191 ///Wrapper for Portable vs Registry configbase.
192 //if there are wxHexEditor.cfg file exits on current path, wxHexEditor switches to portable version.
193 class myConfigBase{
194 public:
Get()195 static wxConfigBase* Get(){
196 static wxFileConfig* AppConfigFile=new wxFileConfig("", "","wxHexEditor.cfg", "", wxCONFIG_USE_RELATIVE_PATH);
197 if( wxFileExists ("wxHexEditor.cfg") )
198 return AppConfigFile;
199 else
200 return wxConfigBase::Get();
201 }
202 };
203
204 class wxHexTextCtrl : public wxHexCtrl{
205 public:
206 wxString CodepageTable;
207 wxString Codepage;
208 wxFontEncoding FontEnc;
209 // wxHexTextCtrl():wxHexCtrl(){}
wxHexTextCtrl(wxWindow * parent)210 wxHexTextCtrl( wxWindow *parent ): wxHexCtrl( parent ){CtrlType=1;}
211 wxHexTextCtrl( wxWindow *parent,
212 wxWindowID id,
213 const wxString &value = wxEmptyString,
214 const wxPoint &pos = wxDefaultPosition,
215 const wxSize &size = wxDefaultSize,
216 long style = 0,
217 const wxValidator& validator = wxDefaultValidator) :
wxHexCtrl(parent,id,value,pos,size,style,validator)218 wxHexCtrl(parent, id, value, pos, size, style, validator){
219 CtrlType=1;
220 wxWindow::SetCursor( wxCURSOR_CHAR );
221
222 FontEnc=wxFONTENCODING_ALTERNATIVE;
223
224 wxString cp;
225 myConfigBase::Get()->Read( _T("CharacterEncoding"), &cp, wxT("DOS CP437") );
226 PrepareCodepageTable(cp);
227 };
228
229 //wxArrayString GetSupportedEncodings(void);
230 wxString PrepareCodepageTable(wxString);
IsDenied()231 inline bool IsDenied(){ return false; }
IsDenied(int x)232 inline bool IsDenied( int x ){ return false; }
CharacterPerLine(void)233 inline int CharacterPerLine( void ){ return m_Window.x; }
BytePerLine(void)234 inline int BytePerLine( void ){ return CharacterPerLine(); }
GetByteCount(void)235 inline int GetByteCount( void ){ return m_text.Length(); }
236 void Replace(unsigned text_location, const wxChar& value, bool paint);
237 void ChangeValue( const wxString& value, bool paint );
238 void SetBinValue( char* buffer, int len, bool paint );
239 void SetDefaultStyle( wxTextAttr& new_attr ); //For caret diet (to 1 pixel)
240 int PixelCoordToInternalPosition( wxPoint mouse );
ToVisiblePosition(int InternalPosition)241 int ToVisiblePosition( int InternalPosition ){ return InternalPosition; }
ToInternalPosition(int VisiblePosition)242 int ToInternalPosition( int VisiblePosition ){ return VisiblePosition; }
243 // bool IsAllowedChar(const unsigned char& chr);
244 int GetInsertionPoint( void );
245 void SetInsertionPoint( unsigned int pos );
246 void ChangeSize();
247 wxChar Filter(const unsigned char& chr);
248 wxString FilterMBBuffer(const char *str, int len, int fontenc);
249 virtual void DrawCursorShadow(wxDC* dcTemp);
250
251 };
252
253 class wxHexOffsetCtrl : public wxHexCtrl{
254 public:
255 // wxHexOffsetCtrl():wxHexCtrl(){ hex_offset=false;offset_position=0; }
wxHexOffsetCtrl(wxWindow * parent)256 wxHexOffsetCtrl(wxWindow *parent): wxHexCtrl( parent ){
257 offset_mode='u';
258 offset_position=0;
259 digit_count=6;
260 }
261
262 wxHexOffsetCtrl(wxWindow *parent,
263 wxWindowID id,
264 const wxString &value = wxEmptyString,
265 const wxPoint &pos = wxDefaultPosition,
266 const wxSize &size = wxDefaultSize,
267 long style = 0,
268 const wxValidator& validator = wxDefaultValidator) :
wxHexCtrl(parent,id,value,pos,size,style,validator)269 wxHexCtrl(parent, id, value, pos, size, style, validator){
270 wxCaret *caret = GetCaret();
271 if(caret)
272 GetCaret()->Hide();
273 SetCaret( NULL );
274
275 //offset_mode='u';
276 CtrlType=2;
277 offset_mode=myConfigBase::Get()->Read( _T("LastOffsetMode"), wxT("u") )[0];
278 if( offset_mode=='s' ) // No force to sector mode at startup.
279 offset_mode='u';
280
281 offset_position=0;
282 digit_count=6;
283 };
284 wxString GetFormatString( bool minimal=false );
285 wxString GetFormatedOffsetString( uint64_t c_offset, bool minimal=false );
SetOffsetLimit(uint64_t max_offset)286 void SetOffsetLimit( uint64_t max_offset ){offset_limit = max_offset;}
287 unsigned GetDigitCount( void );
288 unsigned GetLineSize( void ); //Digit count plus Formating chars like h,o if required
IsDenied()289 inline bool IsDenied(){ return false; }
IsDenied(int x)290 inline bool IsDenied( int x ){ return false; }
ToVisiblePosition(int InternalPosition)291 int ToVisiblePosition( int InternalPosition ){ return InternalPosition; }
ToInternalPosition(int VisiblePosition)292 int ToInternalPosition( int VisiblePosition ){ return VisiblePosition; }
293 void SetValue( uint64_t position );
294 void SetValue( uint64_t position, int byteperline );
295 void OnMouseRight( wxMouseEvent& event );
296 void OnMouseLeft( wxMouseEvent& event );
OnMouseMove(wxMouseEvent & event)297 void OnMouseMove( wxMouseEvent& event ){event.Skip(false);}
298 char offset_mode;
299 uint64_t offset_position;
300 int BytePerLine;
301 int sector_size;
302
303 private:
304 uint64_t offset_limit;
305 unsigned digit_count;
DrawCursorShadow(wxDC * dcTemp)306 inline void DrawCursorShadow(wxDC* dcTemp){}
307 };
308 #endif
309