Skip to content

Ghidra is a software reverse engineering (SRE) framework

BitFields - added bit view component to structure/union editor and refined rendering

ghidra18a1af5c3f0e4
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm b/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm
index 0382a124f5..7f630b2238 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm
@@ -334,9 +334,9 @@
byte size and possible placement conflicts.</li>
<li>The start/end byte offsets may be shared with adjacent bitfield components.</li>
<li>Unoccupied bits within a partially occupied byte are not represented by any component
- (similar to padding bytes within aligned structures). Using the Bitfield Viewer provides
- a bit-level view of a component. (click <IMG alt="[magnifier icon]" src="images/magnifier.png">
- adjacent to bitfield datatype to view within Bitfield Viewer).</li>
+ (similar to padding bytes within aligned structures). Using the bit view provides
+ a precise bit-level view of a component. When a single component is selected, the bit
+ view will highlight the corresponding component block.</li>
<li>A separate Bitfield Editor, for use with unaligned structures only,
must be used to precisley place a bitfield component. Adding a bitfield component via the
structure table view via datatype text entry (e.g., char:1) provides only rough placement.
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java
index e1f5e860a9..21d1afc6e2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldPlacementComponent.java
@@ -31,7 +31,7 @@ import resources.icons.ColorIcon;
public class BitFieldPlacementComponent extends JPanel {
- private static final int CELL_HEIGHT = 30;
+ private static final int CELL_HEIGHT = 25;
private static final int BIT_WIDTH = 10;
private static final int ZERO_BIT_WIDTH = 3;
private static final int BIT_SEPARATOR_THICKNESS = 1;
@@ -46,11 +46,11 @@ public class BitFieldPlacementComponent extends JPanel {
private static final Color LINE_COLOR = Color.black;
private static final Color BYTE_HEADER_COLOR = new Color(0xdfdfdf);
private static final Color UNDEFINED_BIT_COLOR = new Color(0xf8f8f8);
- private static final Color BITFIELD_BITS_COLOR = Color.green;
+ private static final Color ACTIVE_BITFIELD_BITS_COLOR = Color.green;
private static final Color CONFLICT_BITS_COLOR = Color.yellow;
private static final Color BITFIELD_COMPONENT_COLOR = new Color(0xcfcfff);
- private static final Color NON_BITFIELD_COMPONENT_COLOR = new Color(0xafafaf);
- private static final Color INTERIOR_LINE_COLOR = new Color(0xbfbfbf);
+ private static final Color NON_BITFIELD_COMPONENT_COLOR = new Color(0xb8b8ff);
+ private static final Color INTERIOR_LINE_COLOR = new Color(0xcfcfcf);
private final Composite composite;
private final boolean bigEndian;
@@ -78,7 +78,7 @@ public class BitFieldPlacementComponent extends JPanel {
}
viewComponentText += " }";
add(new JLabel(viewComponentText,
- new ColorIcon(BITFIELD_BITS_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
+ new ColorIcon(ACTIVE_BITFIELD_BITS_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
SwingConstants.LEFT));
add(legendPanel);
}
@@ -99,7 +99,7 @@ public class BitFieldPlacementComponent extends JPanel {
if (viewedBitfield == null) {
legendPanel.add(new JLabel("Edit bitfield bits",
- new ColorIcon(BITFIELD_BITS_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
+ new ColorIcon(ACTIVE_BITFIELD_BITS_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
SwingConstants.LEFT));
legendPanel.add(new JLabel("Conflict bits",
new ColorIcon(CONFLICT_BITS_COLOR, INTERIOR_LINE_COLOR, LENEND_BOX_SIZE),
@@ -331,6 +331,45 @@ public class BitFieldPlacementComponent extends JPanel {
return null;
}
+ /**
+ * Get rectangle which fully encompasses specified component.
+ * @param dtc data type component
+ * @param extendToByteBoundary if true rectangle extended to byte boundary
+ * @return component rectangle or null
+ */
+ Rectangle getComponentRectangle(DataTypeComponent dtc, boolean extendToByteBoundary) {
+ if (bitFieldAllocation == null || dtc == null) {
+ return null;
+ }
+
+ if (extendToByteBoundary) {
+ // compute rectangle which extends to byte boundary
+ int byteWidth = 8 * (BIT_WIDTH + BIT_SEPARATOR_THICKNESS);
+ int offset = (dtc.getOffset() - allocationByteOffset);
+ if (!bigEndian) {
+ offset = allocationByteSize - offset - dtc.getLength();
+ }
+ int x = offset * byteWidth;
+ int y = (2 * BYTE_SEPARATOR_THICKNESS) + CELL_HEIGHT;
+ int width = (dtc.getLength() * byteWidth) + (2 * BYTE_SEPARATOR_THICKNESS);
+ return new Rectangle(x, y, width, CELL_HEIGHT);
+ }
+
+ // compute rectangle based on bits only
+ Rectangle rect = null;
+ for (BitAttributes attrs : bitFieldAllocation.bitAttributes) {
+ if (attrs.dtc == dtc) {
+ if (rect == null) {
+ rect = new Rectangle(attrs.rectangle);
+ }
+ else {
+ rect.add(attrs.rectangle);
+ }
+ }
+ }
+ return rect;
+ }
+
@Override
public String getToolTipText(MouseEvent e) {
BitAttributes attrs = getBitAttributes(e.getPoint());
@@ -371,12 +410,22 @@ public class BitFieldPlacementComponent extends JPanel {
private void paintByteHeader(Graphics g, int y, int baseOffset) {
int byteSize = allocationByteSize;
int x = BYTE_SEPARATOR_THICKNESS;
- for (int i = 0; i < byteSize; i++) {
+
+ // start close to the left clip bounds
+ Rectangle clipBounds = g.getClipBounds();
+ int maxX = clipBounds.x + clipBounds.width - 1;
+ int startIndex = clipBounds.x / BYTE_WIDTH;
+ x += startIndex * BYTE_WIDTH;
+
+ for (int i = startIndex; i < byteSize; i++) {
// last byte header needs to slightly wider
int w = BYTE_WIDTH;
if (i == (byteSize - 1)) {
w += BYTE_SEPARATOR_THICKNESS - BIT_SEPARATOR_THICKNESS;
}
+ if (x > maxX) {
+ break; // right clip - return early
+ }
paintByte(g, x, y, w, i, baseOffset);
x += w;
g.fillRect(x - BYTE_SEPARATOR_THICKNESS, y, BYTE_SEPARATOR_THICKNESS, CELL_HEIGHT); // line after each byte
@@ -403,7 +452,7 @@ public class BitFieldPlacementComponent extends JPanel {
String offsetStr = Integer.toString(offset);
FontMetrics fontMetrics = g.getFontMetrics();
- int textY = y + (CELL_HEIGHT + fontMetrics.getMaxAscent()) / 2;
+ int textY = y + (CELL_HEIGHT + fontMetrics.getMaxAscent() - BYTE_SEPARATOR_THICKNESS) / 2;
int textX = x + (width - BYTE_SEPARATOR_THICKNESS - fontMetrics.stringWidth(offsetStr)) / 2;
g.drawString(offsetStr, textX, textY);
@@ -426,17 +475,50 @@ public class BitFieldPlacementComponent extends JPanel {
x += BIT_SEPARATOR_THICKNESS;
}
+ Rectangle dtcRectangle = null;
+ DataTypeComponent prevDtc = null;
BitAttributes prevAttrs = null;
- for (int n = 0; n < bitAttributes.length; n++) {
- BitAttributes attrs = bitAttributes[n];
- boolean paintRightLine = n != (bitAttributes.length - 1);
+ // start close to the left clip bounds
+ Rectangle clipBounds = g.getClipBounds();
+ int maxX = clipBounds.x + clipBounds.width - 1;
+ int bitWidth = bitAttributes[0].rectangle.width;
+ int startIndex = clipBounds.x / (bitAttributes[0].rectangle.width);
+ x += startIndex * bitWidth;
+
+ int bitIndex;
+ for (bitIndex = startIndex; bitIndex < bitAttributes.length; bitIndex++) {
+ BitAttributes attrs = bitAttributes[bitIndex];
+ if (x > maxX) {
+ break; // right clip - return early
+ }
+ boolean paintRightLine = bitIndex != (bitAttributes.length - 1);
attrs.paint(g, prevAttrs, paintRightLine);
- x += attrs.rectangle.width;
+
+ DataTypeComponent dtc = attrs.getDataTypeComponent(false);
+ if (prevDtc != null && prevDtc != dtc) {
+ paintComponentLabel(g, prevDtc, dtcRectangle);
+ prevDtc = null;
+ }
+ if (prevDtc == null) {
+ prevDtc = dtc;
+ dtcRectangle = new Rectangle(attrs.rectangle);
+ }
+ dtcRectangle.add(attrs.rectangle);
+
+ if (attrs.unallocated) {
+ paintDit(g, attrs.rectangle);
+ }
+
prevAttrs = attrs;
+ x += bitWidth;
+ }
+ if (prevDtc != null) {
+ paintComponentLabel(g, prevDtc, dtcRectangle);
}
- if (prevAttrs != null && prevAttrs.rightEndType == EndBitType.TRUNCATED_END) {
+ if (bitIndex == bitAttributes.length && prevAttrs != null &&
+ prevAttrs.rightEndType == EndBitType.TRUNCATED_END) {
x -= BIT_SEPARATOR_THICKNESS; // backup to right line location
drawTruncationLine(g, x, y, CELL_HEIGHT);
}
@@ -444,6 +526,45 @@ public class BitFieldPlacementComponent extends JPanel {
g.setColor(curColor);
}
+ private void paintDit(Graphics2D g, Rectangle r) {
+ Color curColor = g.getColor();
+
+ g.setColor(INTERIOR_LINE_COLOR);
+ int x = r.x + (r.width / 2) - 1;
+ int y = r.y + (r.height / 2) - 1;
+ g.fillRect(x, y, 2, 2);
+
+ g.setColor(curColor);
+ }
+
+ private void paintComponentLabel(Graphics g, DataTypeComponent dtc, Rectangle r) {
+
+ if (dtc.getDataType() == DataType.DEFAULT) {
+ return;
+ }
+
+ String name = dtc.getFieldName();
+ if (name == null) {
+ return;
+ }
+ name = " " + name + " ";
+
+ FontMetrics fontMetrics = g.getFontMetrics();
+ int strWidth = fontMetrics.stringWidth(name);
+ if (strWidth >= r.width) {
+ return;
+ }
+
+ Color curColor = g.getColor();
+ g.setColor(TEXT_COLOR);
+
+ int textY = r.y + (r.height + fontMetrics.getMaxAscent() - BYTE_SEPARATOR_THICKNESS) / 2;
+ int textX = r.x + (r.width - BYTE_SEPARATOR_THICKNESS - strWidth) / 2;
+ g.drawString(name, textX, textY);
+
+ g.setColor(curColor);
+ }
+
private static final Stroke DASH = new BasicStroke(1, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_MITER, 2, new float[] { 3, 3 }, 0);
@@ -579,7 +700,7 @@ public class BitFieldPlacementComponent extends JPanel {
int allocationEndOffset = allocationByteOffset + allocationByteSize - 1;
- for (DataTypeComponent component : struct.getDefinedComponents()) {
+ for (DataTypeComponent component : struct.getComponents()) {
if (component.getOrdinal() == editOrdinal) {
continue;
}
@@ -661,9 +782,7 @@ public class BitFieldPlacementComponent extends JPanel {
Rectangle rectangle;
/**
- * Unallocated bitfield
- * @param dtc
- * @param conflict
+ * Unallocated bitfield (e.g., bitfield padding)
*/
BitAttributes() {
dtc = null;
@@ -675,8 +794,8 @@ public class BitFieldPlacementComponent extends JPanel {
/**
* Zero-length bitfield
- * @param dtc
- * @param conflict
+ * @param dtc data type component residing within structure or null for edit component
+ * @param conflict conflict or null
*/
BitAttributes(DataTypeComponent dtc, BitAttributes conflict) {
this(dtc, dtc != null ? EndBitType.END : EndBitType.NOT_END,
@@ -684,6 +803,13 @@ public class BitFieldPlacementComponent extends JPanel {
zeroBitfield = true;
}
+ /**
+ *
+ * @param dtc data type component residing within structure or null for edit component
+ * @param leftEndType left line type
+ * @param rightEndType right line type
+ * @param conflict conflict or null
+ */
BitAttributes(DataTypeComponent dtc, EndBitType leftEndType, EndBitType rightEndType,
BitAttributes conflict) {
this.dtc = dtc;
@@ -728,7 +854,7 @@ public class BitFieldPlacementComponent extends JPanel {
if (zeroBitfield ||
(dtc != null && conflict != null && conflict.dtc.isZeroBitFieldComponent())) {
- c = BITFIELD_BITS_COLOR;
+ c = ACTIVE_BITFIELD_BITS_COLOR;
Color lineColor = INTERIOR_LINE_COLOR;
if (dtc != null) {
c = BITFIELD_COMPONENT_COLOR;
@@ -778,11 +904,11 @@ public class BitFieldPlacementComponent extends JPanel {
return CONFLICT_BITS_COLOR;
}
}
- if (zeroBitfield) {
- return UNDEFINED_BIT_COLOR;
- }
if (dtc == editComponent) {
- return BITFIELD_BITS_COLOR; // edit field
+ return ACTIVE_BITFIELD_BITS_COLOR; // edit field
+ }
+ if (zeroBitfield || dtc.getDataType() == DataType.DEFAULT) {
+ return UNDEFINED_BIT_COLOR;
}
return dtc.isBitFieldComponent() ? BITFIELD_COMPONENT_COLOR
: NON_BITFIELD_COMPONENT_COLOR;
@@ -797,8 +923,8 @@ public class BitFieldPlacementComponent extends JPanel {
(name != null ? (" " + dtc.getFieldName()) : "");
}
- DataTypeComponent getDataTypeComponent(boolean ignoreEditComponent) {
- if (dtc != null && (dtc.getOrdinal() != editOrdinal || !ignoreEditComponent)) {
+ DataTypeComponent getDataTypeComponent(boolean ignoreActiveComponent) {
+ if (dtc != null && (dtc.getOrdinal() != editOrdinal || !ignoreActiveComponent)) {
return dtc;
}
if (conflict != null) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldViewerDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldViewerDialog.java
deleted file mode 100644
index 7c1355f535..0000000000
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldViewerDialog.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/* ###
- * IP: GHIDRA
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package ghidra.app.plugin.core.compositeeditor;
-
-import javax.swing.JComponent;
-
-import docking.DialogComponentProvider;
-import ghidra.program.model.data.*;
-
-public class BitFieldViewerDialog extends DialogComponentProvider {
-
- BitFieldViewerDialog(Composite composite, int editOrdinal) {
- super("View " + getCompositeType(composite) + " Bitfield");
- addButtons();
- addWorkPanel(buildWorkPanel(composite, editOrdinal));
- setRememberLocation(false);
- setRememberSize(false);
- }
-
- private void addButtons() {
- addCancelButton();
- setCancelButtonText("Close");
- }
-
- private JComponent buildWorkPanel(Composite composite, int viewOrdinal) {
- if (viewOrdinal < 0 || viewOrdinal >= composite.getNumComponents()) {
- throw new IllegalArgumentException("invalid composite ordinal");
- }
- DataTypeComponent dtc = composite.getComponent(viewOrdinal);
- if (!dtc.isBitFieldComponent()) {
- throw new IllegalArgumentException("editOrdinal does not correspond to bitfield");
- }
- return new BitFieldViewerPanel(dtc, BitFieldEditorDialog.getPreferredAllocationOffset(dtc));
- }
-
- private static String getCompositeType(Composite composite) {
- // currently supports unaligned case only!
- String alignmentMode = composite.isInternallyAligned() ? "Aligned" : "Unaligned";
- String type = (composite instanceof Union) ? "Union" : "Structure";
- return alignmentMode + " " + type;
- }
-
-}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldViewerPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldViewerPanel.java
deleted file mode 100644
index d9be420af7..0000000000
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/BitFieldViewerPanel.java
+++ /dev/null
@@ -1,143 +0,0 @@
-/* ###
- * IP: GHIDRA
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package ghidra.app.plugin.core.compositeeditor;
-
-import java.awt.BorderLayout;
-
-import javax.swing.*;
-import javax.swing.border.EmptyBorder;
-
-import ghidra.program.model.data.*;
-import ghidra.util.layout.*;
-
-/**
- * <code>BitFieldViewerPanel</code> provides the ability to examine bitfield placement
- * within structures.
- * TODO: consider using as a hover panel
- */
-public class BitFieldViewerPanel extends JPanel {
-
- private Composite composite;
- private DataTypeComponent bitfieldDtc;
-
- private JLabel allocationOffsetLabel;
-
- private BitFieldPlacementComponent placementComponent;
-
- BitFieldViewerPanel(DataTypeComponent bitfieldDtc, int allocationOffset) {
- super();
- this.bitfieldDtc = bitfieldDtc;
- this.composite = (Composite) bitfieldDtc.getParent();
-
- setLayout(new VerticalLayout(5));
- setFocusTraversalKeysEnabled(true);
-
- setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
-
- if (composite instanceof Structure) {
- add(createAllocationOffsetPanel());
- }
- add(createPlacementPanel());
- add(createLegendPanel());
- initView(allocationOffset);
- }
-
- private JPanel createLegendPanel() {
- JPanel legendPanel = new JPanel(new BorderLayout());
- legendPanel.add(new BitFieldPlacementComponent.BitFieldLegend(bitfieldDtc),
- BorderLayout.WEST);
- return legendPanel;
- }
-
- private JPanel createAllocationOffsetPanel() {
-
- JPanel panel = new JPanel(new HorizontalLayout(5));
-
- allocationOffsetLabel = new JLabel();
- allocationOffsetLabel.setHorizontalTextPosition(SwingConstants.LEFT);
- panel.add(allocationOffsetLabel);
-
- return panel;
- }
-
- private void updateAllocationOffsetLabel() {
- if (composite instanceof Structure) {
- String text =
- "Structure Offset of Allocation Unit: " + placementComponent.getAllocationOffset();
- allocationOffsetLabel.setText(text);
- }
- }
-
- private JPanel createPlacementPanel() {
- JPanel midPanel = new JPanel(new PairLayout(5, 5));
-
- JPanel leftMidPanel = new JPanel(new VerticalLayout(13));
- leftMidPanel.setBorder(BorderFactory.createEmptyBorder(12, 8, 12, 0));
- JLabel byteOffsetLabel = new JLabel("Byte Offset:", SwingConstants.RIGHT);
- byteOffsetLabel.setToolTipText("Byte Offset is relative to start of allocation unit");
- leftMidPanel.add(byteOffsetLabel);
- leftMidPanel.add(new JLabel("Bits:", SwingConstants.RIGHT));
- midPanel.add(leftMidPanel);
-
- placementComponent = new BitFieldPlacementComponent(composite);
- placementComponent.setFont(UIManager.getFont("TextField.font"));
-
- JPanel p = new JPanel(new BorderLayout());
- p.add(placementComponent, BorderLayout.WEST);
- p.setBorder(new EmptyBorder(0, 0, 5, 0));
-
- JScrollPane scrollPane = new JScrollPane(p, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
- ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- scrollPane.getViewport().setBackground(getBackground());
- scrollPane.setBorder(null);
-
- midPanel.add(scrollPane);
- return midPanel;
- }
-
- /**
- * Initialize for edit of existing component or no component if bitfieldDtc is null.
- * If null an allocation size of 4-bytes will be used but may be adjusted.
- * @param allocationOffset allocation offset to be used
- * @param useExistingAllocationSize if true attempt to use existing allocation size
- */
- private void initView(int allocationOffset) {
- DataType initialBaseDataType = null;
- int allocationSize = -1;
- if (bitfieldDtc != null) {
- if (!bitfieldDtc.isBitFieldComponent()) {
- throw new IllegalArgumentException("unsupport data type component");
- }
- BitFieldDataType bitfieldDt = (BitFieldDataType) bitfieldDtc.getDataType();
- initialBaseDataType = bitfieldDt.getBaseDataType();
- if (allocationSize < 1) {
- allocationSize = initialBaseDataType.getLength();
- }
- int allocationAdjust = composite.getLength() - allocationOffset - allocationSize;
- if (allocationAdjust < 0) {
- allocationSize += allocationAdjust;
- }
- }
- if (allocationSize < 1) {
- allocationSize = 4;
- }
-
- placementComponent.updateAllocation(allocationSize, allocationOffset);
- placementComponent.init(bitfieldDtc);
- updateAllocationOffsetLabel();
- }
-
-}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.java
index 01c74a299a..e30e887549 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompEditorPanel.java
@@ -21,6 +21,7 @@ import java.awt.dnd.DropTargetDragEvent;
import java.awt.event.*;
import javax.swing.*;
+import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
@@ -28,11 +29,12 @@ import javax.swing.text.Document;
import docking.widgets.OptionDialog;
import docking.widgets.button.GRadioButton;
import docking.widgets.checkbox.GCheckBox;
+import docking.widgets.fieldpanel.support.FieldSelection;
import docking.widgets.label.GDLabel;
-import ghidra.program.model.data.Category;
+import ghidra.app.plugin.core.compositeeditor.BitFieldPlacementComponent.BitAttributes;
+import ghidra.program.model.data.*;
import ghidra.program.model.data.Composite;
import ghidra.program.model.data.Composite.AlignmentType;
-import ghidra.program.model.data.DataUtilities;
import ghidra.util.HelpLocation;
import ghidra.util.InvalidNameException;
import ghidra.util.exception.*;
@@ -75,6 +77,8 @@ public class CompEditorPanel extends CompositeEditorPanel {
protected JLabel actualAlignmentLabel;
protected JTextField actualAlignmentValueTextField;
+ private BitFieldPlacementComponent bitViewComponent;
+
private DocumentListener fieldDocListener;
private ActionListener fieldActionListener;
@@ -153,6 +157,70 @@ public class CompEditorPanel extends CompositeEditorPanel {
}
}
+ @Override
+ protected JPanel createBitViewerPanel() {
+
+ bitViewComponent = new BitFieldPlacementComponent(model.viewComposite);
+ model.addCompositeViewerModelListener(new CompositeEditorModelAdapter() {
+ @Override
+ public void selectionChanged() {
+ update();
+ }
+
+ @Override
+ public void componentDataChanged() {
+ update();
+ }
+
+ private void update() {
+ DataTypeComponent dtc = null;
+ bitViewComponent.updateAllocation(model.viewComposite.getLength(), 0);
+ if (model.isSingleComponentRowSelection()) {
+ dtc = model.getComponent(model.getSelectedRows()[0]);
+ }
+ bitViewComponent.init(dtc);
+ Rectangle selectedRectangle = bitViewComponent.getComponentRectangle(dtc, true);
+ if (selectedRectangle != null) {
+ SwingUtilities.invokeLater(
+ () -> bitViewComponent.scrollRectToVisible(selectedRectangle));
+ }
+ }
+ });
+
+ bitViewComponent.addMouseListener(new MouseAdapter() {
+
+ @Override
+ public void mousePressed(MouseEvent e) {
+ Point p = e.getPoint();
+ BitAttributes attrs = bitViewComponent.getBitAttributes(p);
+ if (attrs == null) {
+ return;
+ }
+ DataTypeComponent dtc = attrs.getDataTypeComponent(false);
+ if (dtc != null) {
+ model.setSelection(new int[] { dtc.getOrdinal() });
+ }
+ else {
+ model.setSelection(new FieldSelection());
+ }
+ }
+ });
+
+ JPanel p = new JPanel(new BorderLayout());
+ p.add(bitViewComponent, BorderLayout.WEST);
+ p.setBorder(new EmptyBorder(0, 0, 5, 0));
+
+ JScrollPane bitViewScrollPane =
+ new JScrollPane(p, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
+ ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+ bitViewScrollPane.getViewport().setBackground(getBackground());
+ bitViewScrollPane.setBorder(null);
+
+ JPanel bitViewerPanel = new JPanel(new BorderLayout());
+ bitViewerPanel.add(bitViewScrollPane, BorderLayout.CENTER);
+ return bitViewerPanel;
+ }
+
/**
* Create the Info Panel that is horizontally resizable. The panel contains
* the name, category, data type, size, and edit mode for the current
@@ -260,8 +328,7 @@ public class CompEditorPanel extends CompositeEditorPanel {
categoryStatusTextField = new JTextField(" ");
categoryStatusTextField.setEditable(false);
- categoryStatusTextField.setToolTipText(
- "Category of this composite data type.");
+ categoryStatusTextField.setToolTipText("Category of this composite data type.");
categoryStatusTextField.setMargin(TEXTFIELD_INSETS);
gridBagConstraints.insets = VERTICAL_INSETS;
gridBagConstraints.anchor = GridBagConstraints.LINE_START;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java
index e715463b02..eef7d903c4 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorPanel.java
@@ -106,6 +106,10 @@ public abstract class CompositeEditorPanel extends JPanel
this.provider = provider;
this.model = model;
createTable();
+ JPanel bitViewerPanel = createBitViewerPanel();
+ if (bitViewerPanel != null) {
+ lowerPanel.add(bitViewerPanel);
+ }
JPanel infoPanel = createInfoPanel();
if (infoPanel != null) {
adjustCompositeInfo();
@@ -141,8 +145,8 @@ public abstract class CompositeEditorPanel extends JPanel
private void setupTableCellRenderer() {
GTableCellRenderer cellRenderer = new GTableCellRenderer();
- DataTypeCellRenderer dtiCellRenderer = new DataTypeCellRenderer(
- model.getOriginalDataTypeManager(), model.bitfieldsSupported());
+ DataTypeCellRenderer dtiCellRenderer =
+ new DataTypeCellRenderer(model.getOriginalDataTypeManager());
table.setDefaultRenderer(String.class, cellRenderer);
table.setDefaultRenderer(DataTypeInstance.class, dtiCellRenderer);
}
@@ -556,12 +560,6 @@ public abstract class CompositeEditorPanel extends JPanel
table = new CompositeTable(model);
table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
table.addMouseListener(new CompositeTableMouseListener());
- if (model.bitfieldsSupported()) {
- CompositeTableCellMouseListener cellMouseListener =
- new CompositeTableCellMouseListener();
- table.addMouseListener(cellMouseListener);
- table.addMouseMotionListener(cellMouseListener);
- }
CompositeEditorTableAction action = provider.actionMgr.getNamedAction(
CompositeEditorTableAction.EDIT_ACTION_PREFIX + EditFieldAction.ACTION_NAME);
@@ -617,6 +615,18 @@ public abstract class CompositeEditorPanel extends JPanel
}
}
+ /**
+ * Override this method to add your own bit-viewer panel below the
+ * component table.
+ * <P>Creates a panel that appears below the component table. This panel
+ * contains a bit-level view of a selected component.
+ * By default, there is no panel below the component table.
+ * @return the panel or null if there isn't one.
+ */
+ protected JPanel createBitViewerPanel() {
+ return null;
+ }
+
/**
* Override this method to add your own view/edit panel below the
* component table.
@@ -1406,85 +1416,6 @@ public abstract class CompositeEditorPanel extends JPanel
}
}
- private class CompositeTableCellMouseListener extends MouseAdapter {
-
- private boolean trackMovement;
- private Cursor originalCursor;
- private boolean pointerCursorActive;
-
- @Override
- public void mouseExited(MouseEvent e) {
- trackMovement = false;
- table.setCursor(originalCursor);
- }
-
- @Override
- public void mouseEntered(MouseEvent e) {
- trackMovement = true;
- originalCursor = table.getCursor();
- }
-
- @Override
- public void mouseClicked(MouseEvent e) {
- if (!pointerCursorActive || e.getClickCount() != 1) {
- return;
- }
- Point p = e.getPoint();
- int columnIndex = table.columnAtPoint(p);
- int rowIndex = table.rowAtPoint(p);
- if ((columnIndex == -1) || (rowIndex == -1)) {
- return;
- }
- DataTypeComponent dtc = model.getComponent(rowIndex);
- if (dtc != null && dtc.isBitFieldComponent()) {
- e.consume();
- BitFieldViewerDialog dlg =
- new BitFieldViewerDialog(model.viewComposite, dtc.getOrdinal());
- Rectangle cellRect = table.getCellRect(rowIndex, columnIndex, false);
- Point xyPoint = new Point(cellRect.x + DataTypeCellRenderer.ICON_WIDTH,
- cellRect.y + cellRect.height);
- SwingUtilities.convertPointToScreen(xyPoint, table);
- dlg.setInitialLocation(xyPoint.x, xyPoint.y);
- Window w = SwingUtilities.windowForComponent(table);
- DockingWindowManager.showDialog(w, dlg, table);
- }
- }
-
- @Override
- public void mouseMoved(MouseEvent e) {
- if (!trackMovement) {
- return;
- }
- Point p = e.getPoint();
-
- // Locate the renderer under the event location
- int columnIndex = table.columnAtPoint(p);
- int rowIndex = table.rowAtPoint(p);
-
- if ((columnIndex != -1) && (rowIndex != -1) &&
- DataTypeInstance.class.equals(table.getColumnClass(columnIndex))) {
-
- DataTypeComponent dtc = model.getComponent(rowIndex);
- // ignore non-bitfield rows
- if (dtc != null && dtc.isBitFieldComponent()) {
- Rectangle cellRect = table.getCellRect(rowIndex, columnIndex, false);
- p.translate(-cellRect.x, -cellRect.y);
- if (p.x <= (DataTypeCellRenderer.ICON_WIDTH + 2)) {
- if (!pointerCursorActive) {
- pointerCursorActive = true;
- table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- }
- return; // view bitfield cursor active
- }
- }
- }
- if (pointerCursorActive) {
- table.setCursor(originalCursor);
- pointerCursorActive = false;
- }
- }
- }
-
private class CompositeTableMouseListener extends MouseAdapter {
@Override
public void mouseReleased(MouseEvent e) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/DataTypeCellRenderer.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/DataTypeCellRenderer.java
index dd4585445c..3bebf402f4 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/DataTypeCellRenderer.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/DataTypeCellRenderer.java
@@ -18,7 +18,7 @@ package ghidra.app.plugin.core.compositeeditor;
import java.awt.Color;
import java.awt.Component;
-import javax.swing.*;
+import javax.swing.JLabel;
import docking.widgets.table.GTableCellRenderer;
import docking.widgets.table.GTableCellRenderingData;
@@ -27,33 +27,14 @@ import ghidra.app.util.ToolTipUtils;
import ghidra.program.model.data.*;
import ghidra.util.HTMLUtilities;
import ghidra.util.SystemUtilities;
-import resources.ResourceManager;
-import resources.icons.IconWrapper;
-import resources.icons.ScaledImageIconWrapper;
public class DataTypeCellRenderer extends GTableCellRenderer {
private static final long serialVersionUID = 1L;
- static final int ICON_WIDTH = 12;
- static final int ICON_HEIGHT = 12;
-
- public static final Icon MAGNIFIER_ICON = new IconWrapper() {
- @Override
- protected Icon createIcon() {
- ImageIcon viewBitfieldIcon = ResourceManager.loadImage("images/magnifier.png");
- ScaledImageIconWrapper scaledViewBitfieldIcon =
- new ScaledImageIconWrapper(viewBitfieldIcon, ICON_WIDTH, ICON_HEIGHT);
- return scaledViewBitfieldIcon;
- }
- };
-
private DataTypeManager originalDTM;
- private boolean includeViewBitfieldIcon;
- public DataTypeCellRenderer(DataTypeManager originalDataTypeManager,
- boolean includeViewBitfieldIcon) {
+ public DataTypeCellRenderer(DataTypeManager originalDataTypeManager) {
this.originalDTM = originalDataTypeManager;
- this.includeViewBitfieldIcon = includeViewBitfieldIcon;
}
@Override
@@ -85,14 +66,6 @@ public class DataTypeCellRenderer extends GTableCellRenderer {
c.setForeground(Color.RED);
}
- c.setHorizontalTextPosition(RIGHT);
- c.setIcon(null);
-
- if (includeViewBitfieldIcon && (dt instanceof BitFieldDataType)) {
- // add inspect icon and action listener
- c.setIcon(MAGNIFIER_ICON);
- }
-
return c;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorPanel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorPanel.java
index 8edb04769b..32c5925f7f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorPanel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorPanel.java
@@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
- * REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +15,7 @@
*/
package ghidra.app.plugin.core.compositeeditor;
+import javax.swing.JPanel;
public class UnionEditorPanel extends CompEditorPanel {
@@ -23,11 +23,9 @@ public class UnionEditorPanel extends CompEditorPanel {
super(model, provider);
}
- /**
- * Updates the name, description, etc. that appears below the table.
- */
@Override
- protected void adjustCompositeInfo() {
- super.adjustCompositeInfo();
+ protected JPanel createBitViewerPanel() {
+ return null;
}
+
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/ViewBitFieldAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/ViewBitFieldAction.java
deleted file mode 100644
index 837cfc04ef..0000000000
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/ViewBitFieldAction.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/* ###
- * IP: GHIDRA
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package ghidra.app.plugin.core.compositeeditor;
-
-import java.awt.Component;
-import java.awt.Window;
-
-import javax.swing.SwingUtilities;
-
-import docking.ActionContext;
-import docking.DockingWindowManager;
-import ghidra.program.model.data.DataTypeComponent;
-import ghidra.program.model.data.Structure;
-import ghidra.util.exception.AssertException;
-
-/**
- * Action for use in the composite data type editor.
- * This action has help associated with it.
- */
-public class ViewBitFieldAction extends CompositeEditorTableAction {
-
- private final static String ACTION_NAME = "View Bitfield";
- private final static String GROUP_NAME = BITFIELD_ACTION_GROUP;
- private final static String DESCRIPTION = "View an existing bitfield";
- private static String[] popupPath = new String[] { ACTION_NAME };
-
- public ViewBitFieldAction(CompositeEditorProvider provider) {
- super(provider, EDIT_ACTION_PREFIX + ACTION_NAME, GROUP_NAME, popupPath, null, null);
- setDescription(DESCRIPTION);
- if (!(model instanceof CompEditorModel)) {
- throw new AssertException("unsupported use");
- }
- adjustEnablement();
- }
-
- private DataTypeComponent getBitFieldComponent() {
- CompEditorModel editorModel = (CompEditorModel) model;
- if ((editorModel.viewComposite instanceof Structure) &&
- editorModel.getNumSelectedRows() == 1) {
- int rowIndex = model.getSelectedRows()[0];
- if (rowIndex < model.getNumComponents()) {
- DataTypeComponent dtComponent = model.getComponent(rowIndex);
- if (dtComponent.isBitFieldComponent()) {
- return dtComponent;
- }
- }
- }
- return null;
- }
-
- @Override
- public void actionPerformed(ActionContext context) {
-
- CompEditorModel editorModel = (CompEditorModel) model;
-
- DataTypeComponent dtComponent = getBitFieldComponent();
- if (dtComponent == null) {
- return;
- }
-
- BitFieldViewerDialog dlg =
- new BitFieldViewerDialog(editorModel.viewComposite, dtComponent.getOrdinal());
- Component c = provider.getComponent();
- Window w = SwingUtilities.windowForComponent(c);
- DockingWindowManager.showDialog(w, dlg, c);
- requestTableFocus();
- }
-
- @Override
- public void adjustEnablement() {
- setEnabled(getBitFieldComponent() != null);
- }
-
-}