Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1ed87f8c257604a6f003e94f5622283a93e4c53 (plain) (blame)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*******************************************************************************
 * Copyright (c) 2006, 2010, 2012 Wind River Systems, Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Ted R Williams (Wind River Systems, Inc.) - initial implementation
 *     Randy Rohrbach (Wind River Systems, Inc.) - Copied and modified to create the floating point plugin
 *******************************************************************************/

package org.eclipse.cdt.debug.ui.memory.floatingpoint;

import java.math.BigInteger;

import org.eclipse.debug.core.DebugException;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

public class FPDataPane extends FPAbstractPane
{
    public FPDataPane(Rendering parent)
    {
        super(parent);
    }

    // Returns the representation of the given memory bytes as a scientific notation string

    protected String bytesToSciNotation(FPMemoryByte[] bytes)
    {
        return fRendering.sciNotationString(bytes, fRendering.getFPDataType(), fRendering.isDisplayLittleEndian());
    }

    // Cell editing:  Accumulate text entry characters, replacing or inserting them at the current cursor position

    @Override
    protected void editCell(BigInteger cellAddress, int subCellPosition, char character)
    {
        try
        {
            // Switch to cell edit mode if not we're not currently in the middle of an edit

            if (!fRendering.isEditingCell())
            {
                // Calculate the memory address from the cell address

                BigInteger vpStart  = fRendering.getViewportStartAddress();
                BigInteger colChars = BigInteger.valueOf(fRendering.getCharsPerColumn());
                BigInteger dtBytes  = BigInteger.valueOf(fRendering.getFPDataType().getByteLength());
                BigInteger memoryAddress = vpStart.add(((cellAddress.subtract(vpStart)).divide(colChars)).multiply(dtBytes));

                if (!fRendering.insertMode())
                {
                    // Overstrike/overwrite mode: Enter cell-edit mode; start with the current cell contents.

                    fRendering.startCellEditing(cellAddress, memoryAddress, bytesToSciNotation(fRendering.getBytes(cellAddress, fRendering.getFPDataType().getByteLength())));
                }
                else
                {
                    // Insert/Replace mode:  Clear the current cell contents; start
                    // with a blank string.  Move the caret to the start of the cell.

                    fRendering.startCellEditing(cellAddress, memoryAddress, FPutilities.fillString(fRendering.getCharsPerColumn(), ' '));

                    subCellPosition = 0;
                    Point cellCoordinates = getCellLocation(cellAddress);
                    positionCaret(cellCoordinates.x, cellCoordinates.y);
                    fCaret.setVisible(true);
                }
            }

            // Validate the current string:  Only one decimal point and exponent character ('e' or 'E') is allowed.  Number
            // signs may be present up to two times - once for the number and once for the exponent, and may occur at the
            // very beginning of a number or immediately after the exponent character.  If an entry violates a rule, do not
            // echo the character (implicitly, through replacement and re-paint) and do not advance the cursor.

            String cellString = fRendering.getEditBuffer().toString().toLowerCase();

            // Check to see if a second decimal point or exponent was entered

            if ((character == '.' && FPutilities.countMatches(cellString, ".") > 0 && cellString.indexOf('.') != subCellPosition) || //$NON-NLS-1$
                (character == 'e' && FPutilities.countMatches(cellString, "e") > 0 && cellString.indexOf('e') != subCellPosition)) //$NON-NLS-1$
                return;

            // Check to see if more than two number signs have been entered

            if ((character == '+' && FPutilities.countMatches(cellString, "+") > 1) ||  //$NON-NLS-1$
                (character == '-' && FPutilities.countMatches(cellString, "-") > 1)) //$NON-NLS-1$
                return;

            // We've passed the previous checks:  Make the character substitution, if possible.
            // Advance the cursor as long as we're inside the column.  Re-paint the view.

            if (subCellPosition < fRendering.getEditBuffer().length())
                fRendering.getEditBuffer().setCharAt(subCellPosition, character);

            if (subCellPosition < fRendering.getCharsPerColumn())
                advanceCursor();

            redraw();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    // Returns cell width, in pixels

    @Override
    protected int getCellWidth()
    {
        return getCellCharacterCount() * getCellCharacterWidth() + (fRendering.getCellPadding() * 2);
    }

    @Override
    protected int getCellCharacterCount()
    {
        return fRendering.getCharsPerColumn();
    }

    @Override
    public Point computeSize(int wHint, int hHint)
    {
        return new Point(fRendering.getColumnCount() * getCellWidth() + fRendering.getRenderSpacing(), 100);
    }

    private BigInteger getCellAddressAt(int x, int y) throws DebugException
    {
        BigInteger address = fRendering.getViewportStartAddress();

        int col = x / getCellWidth();
        int row = y / getCellHeight();

        if (col >= fRendering.getColumnCount())
            return null;

        address = address.add(BigInteger.valueOf(row * fRendering.getColumnCount() * fRendering.getFPDataType().getByteLength()));
        address = address.add(BigInteger.valueOf(col * fRendering.getFPDataType().getByteLength()));

        return address;
    }

    // Return a Point representing the cell address

    @Override
    protected Point getCellLocation(BigInteger cellAddress)
    {
        try
        {
            BigInteger address = fRendering.getViewportStartAddress();

            int cellOffset = cellAddress.subtract(address).intValue();
            cellOffset *= fRendering.getAddressableSize();

            int row = cellOffset / (fRendering.getColumnCount() * fRendering.getFPDataType().getByteLength());
            cellOffset -= row * fRendering.getColumnCount()     * fRendering.getFPDataType().getByteLength();
            int col = cellOffset / fRendering.getFPDataType().getByteLength();

            int x = col * getCellWidth()  + fRendering.getCellPadding();
            int y = row * getCellHeight() + fRendering.getCellPadding();

            return new Point(x, y);
        }
        catch (Exception e)
        {
            fRendering.logError(FPRenderingMessages.getString("FPRendering.FAILURE_DETERMINE_CELL_LOCATION"), e); //$NON-NLS-1$
            return null;
        }
    }

    @Override
    protected void positionCaret(int x, int y)
    {
        try
        {
            BigInteger cellAddress = getCellAddressAt(x, y);

            if (cellAddress != null)
            {
                Point cellPosition = getCellLocation(cellAddress);
                int offset = x - cellPosition.x;
                int subCellCharacterPosition = offset / getCellCharacterWidth();

                if (subCellCharacterPosition == this.getCellCharacterCount())
                {
                    cellAddress = cellAddress.add(BigInteger.valueOf(fRendering.getFPDataType().getByteLength()));
                    subCellCharacterPosition = 0;
                    cellPosition = getCellLocation(cellAddress);
                }

                fCaret.setLocation(cellPosition.x + subCellCharacterPosition * getCellCharacterWidth(), cellPosition.y);

                this.fCaretAddress = cellAddress;
                this.fSubCellCaretPosition = subCellCharacterPosition;
                setCaretAddress(fCaretAddress);
            }
        }
        catch (Exception e)
        {
            fRendering.logError(FPRenderingMessages.getString("FPRendering.FAILURE_POSITION_CURSOR"), e); //$NON-NLS-1$
        }
    }

    @Override
    protected BigInteger getViewportAddress(int col, int row) throws DebugException
    {
        BigInteger address = fRendering.getViewportStartAddress();
        address = address.add(BigInteger.valueOf((row * fRendering.getColumnCount() + col) * fRendering.getFPDataType().getByteLength()));

        return address;
    }

    @Override
    protected void paint(PaintEvent pe)
    {
        super.paint(pe);
        // Allow subclasses to override this method to do their own painting
        doPaintData(pe);
    }

    // Display text in the cell


    protected void doPaintData(PaintEvent pe)
    {
        GC gc = pe.gc;
        gc.setFont(fRendering.getFont());

        int cellHeight   = getCellHeight();
        int cellWidth    = getCellWidth();
        int boundsHeight = this.getBounds().height;
        int columns      = fRendering.getColumnCount();

        int cellX = 0;
        int cellY = 0;

        String displayString = FPutilities.fillString(fRendering.getCharsPerColumn(), '.');

        try
        {
            BigInteger vpStart = fRendering.getViewportStartAddress();
            BigInteger cellStartAddr = vpStart;
            BigInteger memoryAddr = vpStart;
            BigInteger cellEndAddr;

            for(int row = 0; row < boundsHeight / cellHeight; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    // Set alternating colors for every other column and display the text
                    // FIXME: There is duplicate code in applyCustomColor() in this class

                    if (isOdd(column))
                        gc.setForeground(fRendering.getFPRendering().getColorText());
                    else
                        gc.setForeground(fRendering.getFPRendering().getColorTextAlternate());

                    // Calculate the cell starting address and X/Y coordinates

                    cellStartAddr = vpStart.add(BigInteger.valueOf((row * fRendering.getColumnCount() + column) * fRendering.getFPDataType().getByteLength()));
                    cellEndAddr = cellStartAddr.add(BigInteger.valueOf(fRendering.getFPDataType().getByteLength()).subtract(BigInteger.ONE));

                    cellX = (cellWidth  * column) + fRendering.getCellPadding();
                    cellY = (cellHeight * row   ) + fRendering.getCellPadding();

                    // Cell editing:  If we're in edit mode, change the cell color and then set the
                    // edit buffer as the string to display.  Otherwise, just use the memory contents.

                    if (fRendering.isEditingCell() && cellStartAddr.equals(fRendering.getCellEditAddress()))
                    {
                        gc.setForeground(fRendering.getFPRendering().getColorEdit());
                        FPMemoryByte[] memoryBytes = fRendering.getBytes(cellStartAddr, fRendering.getFPDataType().getByteLength());
                        for (FPMemoryByte memoryByte : memoryBytes)
                            memoryByte.setEdited(true);
                        applyCustomColor(gc, memoryBytes, column);
                        displayString = fRendering.getEditBuffer().toString();
                    }
                    else
                        displayString = bytesToSciNotation(fRendering.getBytes(memoryAddr, fRendering.getFPDataType().getByteLength()));

                    // Cell selection

                    if (fRendering.getSelection().isSelected(cellStartAddr))
                    {
                        gc.setBackground(fRendering.getFPRendering().getColorSelection());
                        gc.fillRectangle(cellX, row * cellHeight, cellWidth, cellHeight);
                        gc.setForeground(fRendering.getFPRendering().getColorBackground());
                    }
                    else
                    {
                        gc.setBackground(fRendering.getFPRendering().getColorBackground());
                        gc.fillRectangle(cellX, row * cellHeight, cellWidth, cellHeight);
                        // Allow subclasses to override this method to do their own coloring
                        applyCustomColor(gc, fRendering.getBytes(cellStartAddr, fRendering.getFPDataType().getByteLength()), column);
                    }

                    gc.drawText(displayString, cellX, cellY);

                    // Move the caret if appropriate

                    if (fCaretEnabled)
                    {
                        if(cellStartAddr.compareTo(fCaretAddress) <= 0 && cellEndAddr.compareTo(fCaretAddress) >= 0)
                        {
                            int x = cellWidth  * column + fRendering.getCellPadding() + fSubCellCaretPosition * this.getCellCharacterWidth();
                            int y = cellHeight * row    + fRendering.getCellPadding();
                            fCaret.setLocation(x, y);
                        }
                    }

                    // For debugging

                    if (fRendering.isDebug())
                        gc.drawRectangle(cellX, cellY, cellWidth, cellHeight);

                    // Increment the memory address by the length of the data type

                    memoryAddr = memoryAddr.add(BigInteger.valueOf(fRendering.getFPDataType().getByteLength()));
                }
            }
        }
        catch(Exception e)
        {
            fRendering.logError(FPRenderingMessages.getString("FPRendering.FAILURE_PAINT"), e); //$NON-NLS-1$
            e.printStackTrace();
        }
    }

    // Allow subclasses to override this method to do their own coloring

    protected void applyCustomColor(GC gc, FPMemoryByte bytes[], int col)
    {
        // Check to see if any byte has been changed and whether we're actually in edit mode

        boolean anyByteEditing = false;

        for (int n = 0; n < bytes.length && !anyByteEditing; n++)
        	if (bytes[n].isEdited()) anyByteEditing = true;

        anyByteEditing = anyByteEditing && fRendering.isEditingCell();

        // Even/odd column coloring

        if (isOdd(col))
            gc.setForeground(fRendering.getFPRendering().getColorText());
        else
            gc.setForeground(fRendering.getFPRendering().getColorTextAlternate());

        // Background

        gc.setBackground(fRendering.getFPRendering().getColorBackground());

        if (anyByteEditing)
        {
            gc.setForeground(fRendering.getFPRendering().getColorEdit());
        }
        else
        {
            boolean isColored = false;

            for (int index = 0; index < fRendering.getHistoryDepth() && !isColored; index++)
            {
                // TODO consider adding finer granularity?

                for (int n = 0; n < bytes.length; n++)
                {
                    if (bytes[n].isChanged(index))
                    {
                        if (index == 0)
                            gc.setForeground(fRendering.getFPRendering().getColorsChanged()[index]);
                        else
                            gc.setBackground(fRendering.getFPRendering().getColorsChanged()[index]);

                        isColored = true;
                        break;
                    }
                }
            }
        }
    }

    // Draw a box around the specified cell

    public void highCellBox(BigInteger memoryAddress)
    {
//        if (fRendering.isDebug())
//            gc.drawRectangle(cellX, cellY, cellWidth, cellHeight);
    }

    // Clear the box around the specified cell

    public void clearCellBox(BigInteger memoryAddress)
    {

    }
}

Back to the top