Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6045 - Updated the structure editor to correctly scroll to the field being edited when first opened

dragonmacherb5f221555355
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/actions/AbstractFindReferencesDataTypeAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/actions/AbstractFindReferencesDataTypeAction.java
index 55517e73ff..390d5fffc0 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/actions/AbstractFindReferencesDataTypeAction.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/actions/AbstractFindReferencesDataTypeAction.java
@@ -4,9 +4,9 @@
* 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.
@@ -119,8 +119,7 @@ public abstract class AbstractFindReferencesDataTypeAction extends DockingAction
private Integer getOffsetForDeafaultFieldName(Structure structure, String fieldName) {
DataTypeComponent[] components = structure.getComponents();
for (DataTypeComponent dtc : components) {
- String defaultName = dtc.getDefaultFieldName();
- if (fieldName.equals(defaultName)) {
+ if (dtc.isDefaultFieldName(fieldName)) {
return dtc.getOffset();
}
}
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 51d5920b1c..2dd4fe4e2b 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
@@ -43,6 +43,7 @@ import docking.widgets.label.GLabel;
import docking.widgets.table.*;
import docking.widgets.textfield.GValidatedTextField;
import generic.theme.GColor;
+import generic.timer.ExpiringSwingTimer;
import ghidra.app.util.datatype.DataTypeSelectionEditor;
import ghidra.app.util.datatype.NavigationDirection;
import ghidra.framework.plugintool.Plugin;
@@ -258,8 +259,8 @@ public abstract class CompositeEditorPanel<T extends Composite, M extends Compos
if (Objects.equals(fieldName, dtcFieldName)) {
return row;
}
- String defaultName = dtc.getDefaultFieldName();
- if (Objects.equals(fieldName, defaultName)) {
+
+ if (dtc.isDefaultFieldName(fieldName)) {
return row;
}
}
@@ -546,6 +547,15 @@ public abstract class CompositeEditorPanel<T extends Composite, M extends Compos
table.dispose();
}
+ private void showSelectedRow() {
+ if (table.isShowing()) {
+ table.scrollToSelectedRow();
+ }
+ else {
+ ExpiringSwingTimer.runWhen(() -> table.isShowing(), table::scrollToSelectedRow);
+ }
+ }
+
private void createTable() {
table = new CompositeEditorTable(model);
@@ -566,10 +576,9 @@ public abstract class CompositeEditorPanel<T extends Composite, M extends Compos
if (e.getValueIsAdjusting()) {
return;
}
+
model.setSelection(table.getSelectedRows());
- if (table.getAutoscrolls()) {
- table.scrollToSelectedRow();
- }
+ showSelectedRow();
});
table.getColumnModel().getSelectionModel().addListSelectionListener(e -> {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorProvider.java
index 2c91aef45b..cb8e3b9603 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/CompositeEditorProvider.java
@@ -71,6 +71,7 @@ public abstract class CompositeEditorProvider<T extends Composite, M extends Com
setTransient();
listeners = WeakDataStructureFactory.createSingleThreadAccessWeakSet();
initializeServices();
+
}
protected String getProviderSubTitle(DataType dataType) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java
index ae08d6b323..a69b9c6f51 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/StructureEditorProvider.java
@@ -50,7 +50,6 @@ public class StructureEditorProvider
plugin.getTool().addComponentProvider(this, true);
updateTitle();
addActionsToTool();
- editorPanel.getTable().requestFocus();
editorModel.selectionChanged();
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java
index e3ec1ade7d..c3044ceaee 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/compositeeditor/UnionEditorProvider.java
@@ -43,7 +43,6 @@ public class UnionEditorProvider extends CompositeEditorProvider<Union, UnionEdi
updateTitle();
plugin.getTool().addComponentProvider(this, true);
addActionsToTool();
- editorPanel.getTable().requestFocus();
editorModel.selectionChanged();
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/DataTypeEditorManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/DataTypeEditorManager.java
index d405afe68c..6fa0f74901 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/DataTypeEditorManager.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/datamgr/editor/DataTypeEditorManager.java
@@ -129,6 +129,7 @@ public class DataTypeEditorManager implements EditorListener {
* @param composite the structure.
* @param fieldName the optional name of the field to select in the editor.
*/
+ @SuppressWarnings("rawtypes") // ignore on CompositeEditorProvider
public void edit(Composite composite, String fieldName) {
CompositeEditorProvider editor = (CompositeEditorProvider) getEditor(composite);
@@ -144,9 +145,11 @@ public class DataTypeEditorManager implements EditorListener {
editor = new StructureEditorProvider(plugin, (Structure) composite,
showStructureNumbersInHex());
}
- editor.selectField(fieldName);
+
editor.addEditorListener(this);
editorList.add(editor);
+
+ editor.selectField(fieldName);
}
private EditorProvider reuseExistingEditor(DataType dataType) {
@@ -626,14 +629,14 @@ public class DataTypeEditorManager implements EditorListener {
@Override
protected List<String> getCallingConventionNames() {
// can't rely on functionDefinition which may be null for new definition
- DataTypeManager dtMgr = getDataTypeManager();
- if (dtMgr instanceof CompositeViewerDataTypeManager) {
- dtMgr = ((CompositeViewerDataTypeManager) dtMgr).getOriginalDataTypeManager();
+ DataTypeManager dtm = getDataTypeManager();
+ if (dtm instanceof CompositeViewerDataTypeManager cvdtm) {
+ dtm = cvdtm.getOriginalDataTypeManager();
}
ArrayList<String> list = new ArrayList<>();
list.add(Function.UNKNOWN_CALLING_CONVENTION_STRING);
list.add(Function.DEFAULT_CALLING_CONVENTION_STRING);
- list.addAll(dtMgr.getDefinedCallingConventionNames());
+ list.addAll(dtm.getDefinedCallingConventionNames());
return list;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorProvider.java
index bfa7af5ec1..9340810f2b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/stackeditor/StackEditorProvider.java
@@ -82,7 +82,6 @@ public class StackEditorProvider
plugin.getTool().addComponentProvider(this, true);
addActionsToTool();
- editorPanel.getTable().requestFocus();
}
@Override
@@ -235,7 +234,7 @@ public class StackEditorProvider
}
break;
case SYMBOL_PRIMARY_STATE_CHANGED:
- sym = (Symbol) ((ProgramChangeRecord) rec).getNewValue();
+ sym = (Symbol) rec.getNewValue();
if (sym.getAddress().equals(function.getEntryPoint())) {
scheduleRefreshName = true;
delayedUpdateMgr.updateLater();
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.java
index 5af9519f30..cfffc6179f 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.java
@@ -4,9 +4,9 @@
* 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.
@@ -80,7 +80,7 @@ public class EditDataTypeAction extends AbstractDecompilerAction {
DataTypeManagerService service =
context.getTool().getService(DataTypeManagerService.class);
- if (dataType instanceof Structure structure) {
+ if (baseDataType instanceof Structure structure) {
editStructure(service, structure, context);
}
else {
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/ComponentPlaceholder.java b/Ghidra/Framework/Docking/src/main/java/docking/ComponentPlaceholder.java
index cc9d50aa94..b738f5b536 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/ComponentPlaceholder.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/ComponentPlaceholder.java
@@ -297,7 +297,7 @@ public class ComponentPlaceholder {
activateWindow();
// make sure the tab has time to become active before trying to request focus
- ExpiringSwingTimer.runWhen(this::isShowing, 750, () -> {
+ ExpiringSwingTimer.runWhen(this::isShowing, () -> {
doRequestFocus(tmp);
});
}
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.java
index f96b249139..145a0faf37 100644
--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.java
+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/table/GTable.java
@@ -1012,21 +1012,33 @@ public class GTable extends JTable {
}
public void scrollToSelectedRow() {
+ Container parent = getParent();
+ if (!(parent instanceof JViewport viewport)) {
+ return;
+ }
+
int[] selectedRows = getSelectedRows();
if (selectedRows == null || selectedRows.length == 0) {
return;
}
- // just make sure that the first row is visible
+ // Update the cell rectangle to be the entire row so that if the user is horizontally
+ // scrolled, then we do not change that.
int row = selectedRows[0];
-
- // update the cell rectangle to be the entire row so that if the user is horizontally
- // scrolled, then we do not change that
- Rectangle visibleRect = getVisibleRect();
Rectangle cellRect = getCellRect(row, 0, true);
- cellRect.x = visibleRect.x;
+ Rectangle visibleRect = getVisibleRect();
+ cellRect.x = visibleRect.x; // use the view x to prevent side scrolling
cellRect.width = visibleRect.width;
+ // Swing will scroll the view such that the given cell rectangle is at the bottom of the
+ // scroll pane. It looks nicer if the row is centered.
+ int halfViewport = viewport.getHeight() / 2;
+ int halfCell = cellRect.height / 2;
+ int center = halfViewport - halfCell;
+ int middleY = visibleRect.y + center;
+ boolean below = cellRect.y > middleY;
+ cellRect.y += below ? center : -center;
+
scrollRectToVisible(cellRect);
}
diff --git a/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.java b/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.java
index 5805dab0c2..a55f1496a5 100644
--- a/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.java
+++ b/Ghidra/Framework/Generic/src/main/java/generic/timer/ExpiringSwingTimer.java
@@ -32,6 +32,8 @@ import utility.function.Dummy;
*/
public class ExpiringSwingTimer extends GhidraSwingTimer {
+ private static int DEFAULT_EXPIRE_MS = 750;
+
private static Set<ExpiringSwingTimer> instances = new HashSet<>();
private long startMs = System.currentTimeMillis();
@@ -64,6 +66,31 @@ public class ExpiringSwingTimer extends GhidraSwingTimer {
return timer;
}
+ /**
+ * Runs the given client runnable when the given condition returns true. The returned timer
+ * will be running.
+ *
+ * <p>Once the timer has performed the work, any calls to start the returned timer will
+ * not perform any work. You can check {@link #didRun()} to see if the work has been completed.
+ *
+ * <p>The timer's expiration is set to the default value of
+ * {@value ExpiringSwingTimer#DEFAULT_EXPIRE_MS}.
+ *
+ * @param isReady true if the code should be run
+ * @param runnable the code to run
+ * @return the timer object that is running, which will execute the given code when ready
+ */
+ public static ExpiringSwingTimer runWhen(BooleanSupplier isReady, Runnable runnable) {
+
+ // Note: we could let the client specify the delay, but that would add an extra argument
+ // to this method. For now, just use something reasonable.
+ int delay = 250;
+ ExpiringSwingTimer timer =
+ new ExpiringSwingTimer(delay, DEFAULT_EXPIRE_MS, isReady, runnable);
+ timer.start();
+ return timer;
+ }
+
/**
* Calls the given consumer with the non-null value returned from the given supplier. The
* returned timer will be running.
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeComponent.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeComponent.java
index a724e332f2..dd1a63388e 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeComponent.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/DataTypeComponent.java
@@ -139,6 +139,29 @@ public interface DataTypeComponent {
return name;
}
+ /**
+ * Returns true if the given string represents the default field name for this data type
+ * component. This value returned from {@link #getDefaultFieldName()} may not be a default name
+ * when this method returns true.
+ *
+ * @param s the string to check
+ * @return true if the given string is the default name for this component
+ */
+ public default boolean isDefaultFieldName(String s) {
+ if (isZeroBitFieldComponent()) {
+ return false;
+ }
+
+ String offset = "";
+ if (getParent() instanceof Structure) {
+ offset += "_0x" + Integer.toHexString(getOffset());
+ }
+
+ String newStyleName = DEFAULT_FIELD_NAME_PREFIX + getOrdinal() + offset;
+ String oldStyleName = DEFAULT_FIELD_NAME_PREFIX + offset;
+ return newStyleName.equals(s) || oldStyleName.equals(s);
+ }
+
/**
* Returns true if the given dataTypeComponent is equivalent to this dataTypeComponent.
* A dataTypeComponent is "equivalent" if the other component has a data type