summaryrefslogtreecommitdiffstats
path: root/khexedit/lib/kbuffercolumn.h
blob: 37a495a6a6bfe5ccc071258199d868bbb7c68eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***************************************************************************
                          kbuffercolumn.h  -  description
                             -------------------
    begin                : Mit Mai 14 2003
    copyright            : (C) 2003 by Friedrich W. H. Kossebau
    email                : Friedrich.W.H@Kossebau.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License version 2 as published by the Free Software Foundation.       *
 *                                                                         *
 ***************************************************************************/


#ifndef KHE_KBUFFERCOLUMN_H
#define KHE_KBUFFERCOLUMN_H

// lib specific
#include "khe.h"
#include "kdatabuffer.h"
#include "khechar.h"
#include "kcolumn.h"
#include "kbufferlayout.h"
#include "ksection.h"

class QPainter;
class QColor;

namespace KHE
{

// class KHexEdit;
class KBufferRanges;
class KCharCodec;

const int NoByteFound = -1;

/** base class of all buffer column displayers
  * holds all information about the vertical layout of a buffer column
  * knows how to paint the data and the editing things (focus, cursor, selection)
  * but does not offer
  *
  *@author Friedrich W. H. Kossebauint KBufferColumn::posOfX( KPixelX PX, bool *ToTheRightFlag ) const
  */
class KBufferColumn : public KColumn
{
  public:
    enum KFrameStyle { Frame, Left, Right };
  public:
    KBufferColumn( KColumnsView/*KHexEdit*/ *HE, KDataBuffer *B, KBufferLayout *L, KBufferRanges *R );
    virtual ~KBufferColumn();


  public: // KColumn-API
    virtual void paintFirstLine( TQPainter *P, KPixelXs Xs, int FirstLine );
    virtual void paintNextLine( TQPainter *P );

  public:
    void preparePainting( KPixelXs Xs );

  public:
    void paintLine( TQPainter *P, int Line );
    void paintPositions( TQPainter *P, int Line, KSection Positions );
    /** paints a cursor based on the type of the byte.
      * @param Index Index of the byte to paint the cursor for. If -1 a space is used as char.
      */
    void paintCursor( TQPainter *P, int Index );
    /** paints the byte with background.
      * @param Index Index of the byte to paint. If -1 only the background is painted.
      */
    void paintByte( TQPainter *P, int Index );
    /** paints the byte with background and a frame around.
      * @param Index Index of the byte to paint the frame for. If -1 a space is used as char.
      * @param Style the style of the framing
      */
    void paintFramedByte( TQPainter *P, int Index, KFrameStyle Style );


  public: // modification access
    /** sets the spacing in the hex column
      * @param ByteSpacingW spacing between the bytes in pixels
      * @param NewNoOfGroupedBytes numbers of grouped bytes, 0 means no grouping
      * @param GroupSpacingW spacing between the groups in pixels
      * returns true if there was a change
      */
    bool setSpacing( KPixelX ByteSpacingW, int NewNoOfGroupedBytes = 0, KPixelX GroupSpacingW = 0 );
    /** sets the spacing between the bytes in the hex column
      * @param ByteSpacingW spacing between the bytes in pixels
      * returns true if there was a change
      */
    bool setByteSpacingWidth( KPixelX ByteSpacingW );
    /** sets the number of grouped bytes in the hex column
      * @param NewNoOfGroupedBytes numbers of grouped bytes, 0 means no grouping
      * returns true if there was a change
      */
    bool setNoOfGroupedBytes( int NewNoOfGroupedBytes );
    /** sets the spacing between the groups of bytes in the hex column
      * @param GroupSpacingW spacing between the groups in pixels
      * returns true if there was a change
      */
    bool setGroupSpacingWidth( KPixelX GroupSpacingW );
    /** sets width of digits and recalculates depend sizes
      * returns true if there was a change
      */
    bool setDigitWidth( KPixelX DW );
    /** sets the metrics of the used font
      * @param NewDigitWidth the new width of a digit
      * @param NewDigitBaseLine the new baseline of the digits
      */
    void setMetrics( KPixelX NewDigitWidth, KPixelY NewDigitBaseLine );
    /** */
    void set( KDataBuffer *B );
    /** creates new buffer for x-values; to be called on any change of NoOfBytesPerLine or metrics */
    void resetXBuffer();
    /** sets the codec to be used by the char column. */
    void setCodec( KCharCodec *C );

  public: // functional logic
    /** returns byte positions covered by pixels with absolute x-coord x */
    KSection posOfX( KPixelX x, KPixelX w ) const;
    /** returns byte pos at pixel with absolute x-coord x */
    int posOfX( KPixelX x ) const;
    /** returns byte pos at pixel with absolute x-coord x, and sets the flag to true if we are closer to the right */
    int magPosOfX( KPixelX PX ) const;
    /** returns absolute x-coord of byte at position Pos */
    KPixelX xOfPos( int Pos ) const;
    /** returns right absolute x-coord of byte at position Pos */
    KPixelX rightXOfPos( int Pos ) const;
    /** returns byte pos at pixel with relative x-coord x */
    int posOfRelX( KPixelX x ) const;
    /** returns byte positions covered by pixels with relative x-coord x */
    KSection posOfRelX( KPixelX x, KPixelX w ) const;
    /** returns relative x-coord of byte at position Pos */
    KPixelX relXOfPos( int Pos ) const;
    /** returns right relative x-coord of byte at position Pos */
    KPixelX relRightXOfPos( int Pos ) const;
    /** returns the positions that overlap with the absolute x-coords */
    KSection visiblePositions( KPixelX x, KPixelX w ) const;
    /** returns the */
    KPixelXs wideXPixelsOfPos( KSection Positions ) const;
    /** */
    KPixelXs relWideXPixelsOfPos( KSection Positions ) const;

  public: // value access
    KPixelX byteWidth()                      const;
    KPixelX digitWidth()                     const;
    KPixelX groupSpacingWidth()              const;
    KPixelX byteSpacingWidth()               const;
    int noOfGroupedBytes()                   const;

    int firstPos() const;
    int lastPos()  const;
    KSection visiblePositions() const;
    const KBufferLayout *layout() const;
    KCharCodec* codec() const;

  protected:
    /** */
    void recalcX();
    void recalcVerticalGridX();


  protected: // API to be refined
    /** default implementation simply prints the byte as ASCII */
    virtual void drawByte( TQPainter *P, char Byte, KHEChar B, const TQColor &Color ) const;
    /** default implementation sets byte width to one digit width */
    virtual void recalcByteWidth();


  protected:
    void paintGrid( TQPainter *P, KSection Range );
    void paintPlain( TQPainter *P, KSection Positions, int Index );
    void paintSelection( TQPainter *P, KSection Positions, int Index, int Flag );
    void paintMarking( TQPainter *P, KSection Positions, int Index, int Flag );
    void paintRange( TQPainter *P, const TQColor &Color, KSection Positions, int Flag );

    bool isSelected( KSection Range, KSection *Selection, unsigned int *Flag ) const;
    bool isMarked( KSection Range, KSection *Marking, unsigned int *Flag ) const;


  protected:
    /** pointer to the buffer */
    KDataBuffer *Buffer;
    /** pointer to the layout */
    const KBufferLayout *Layout;
    /** pointer to the ranges */
    KBufferRanges *Ranges;
    /** */
    KCharCodec *Codec;

    /** */
    KPixelX DigitWidth;
    /** */
    KPixelY DigitBaseLine;
    /** */
    KPixelX VerticalGridX;
    /** */
    bool VerticalGrid;

  protected:  // individual data
    /** total width of byte display in pixel */
    KPixelX ByteWidth;
    /** width of inserting cursor in pixel */
    KPixelX CursorWidth;
    /** size of the line margin */
    KPixelX ByteSpacingWidth;
    /** width of spacing in pixel */
    KPixelX GroupSpacingWidth;

    /** number of grouped bytes */
    int NoOfGroupedBytes;

    /** pointer to array with buffered positions (relative to column position)
      * a spacing gets assigned to the left byte -> ...c|c|c |c|c...
      */
    KPixelX *PosX;
    KPixelX *PosRightX;
    /** index of right position */
    int LastPos;


  protected: // buffering drawing data
    KSection PaintPositions;
    int PaintLine;
    KPixelX PaintX;
    KPixelX PaintW;
    int SpacingTrigger;
};


inline KPixelX KBufferColumn::byteWidth()         const { return ByteWidth; }
inline KPixelX KBufferColumn::digitWidth()        const { return DigitWidth; }
inline KPixelX KBufferColumn::byteSpacingWidth()  const { return ByteSpacingWidth; }
inline KPixelX KBufferColumn::groupSpacingWidth() const { return GroupSpacingWidth; }

inline int KBufferColumn::noOfGroupedBytes()      const { return NoOfGroupedBytes; }

inline int KBufferColumn::firstPos() const { return PaintPositions.start(); }
inline int KBufferColumn::lastPos()  const { return PaintPositions.end(); }
inline KSection KBufferColumn::visiblePositions() const { return PaintPositions; }

inline const KBufferLayout *KBufferColumn::layout() const { return Layout; }


inline void KBufferColumn::setCodec( KCharCodec *C ) { Codec = C; }
inline KCharCodec* KBufferColumn::codec() const { return Codec; }

}

#endif