Skip to content

Commit

Permalink
GP-1899 Corrected bit-field component and editor issues for structure
Browse files Browse the repository at this point in the history
editor
  • Loading branch information
ghidra1 committed Jul 29, 2024
1 parent d7c7fc0 commit 02b0dfe
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,17 +15,9 @@
*/
package ghidra.app.plugin.core.compositeeditor;

import java.awt.Component;

import docking.ActionContext;
import docking.DockingWindowManager;
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 AddBitFieldAction extends CompositeEditorTableAction {

public final static String ACTION_NAME = "Add Bitfield";
Expand All @@ -45,33 +37,17 @@ public AddBitFieldAction(CompositeEditorProvider provider) {

@Override
public void actionPerformed(ActionContext context) {

CompEditorModel editorModel = (CompEditorModel) model;
if (editorModel.getNumSelectedRows() != 1) {
return;
}
int rowIndex = model.getSelectedRows()[0];

BitFieldEditorDialog dlg =
new BitFieldEditorDialog(editorModel.viewComposite, provider.dtmService,
-(rowIndex + 1), model.showHexNumbers,
ordinal -> refreshTableAndSelection(editorModel, ordinal));
Component c = provider.getComponent();
DockingWindowManager.showDialog(c, dlg);
requestTableFocus();
}

private void refreshTableAndSelection(CompEditorModel editorModel, int ordinal) {
editorModel.notifyCompositeChanged();
StructureEditorProvider structProvider = (StructureEditorProvider) provider;
structProvider.showAddBitFieldEditor();
}

@Override
public void adjustEnablement() {
boolean enabled = true;
CompEditorModel editorModel = (CompEditorModel) model;
// Union do not support non-packed placement of bitfields
if (!(editorModel.viewComposite instanceof Structure) || editorModel.isPackingEnabled() ||
editorModel.getNumSelectedRows() != 1) {
// Unions do not support non-packed manipulation of bitfields
if (!(provider instanceof StructureEditorProvider structProvider) ||
editorModel.isPackingEnabled() || editorModel.getNumSelectedRows() != 1) {
enabled = false;
}
setEnabled(enabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -171,6 +171,8 @@ public void setComposite(Composite composite) {
if (!editUseEnabled && composite != null) {
allocationByteSize = composite.getLength();
}
setBounds(0, 0, getPreferredWidth(), getPreferredHeight());
invalidate();
init(null);
}

Expand Down Expand Up @@ -315,8 +317,8 @@ private void updatePreferredSize() {
* (allocation unit size is determine by current {@link #allocationByteSize}).
*/
void refresh(int bitSize, int bitOffset) {
bitFieldAllocation = new BitFieldAllocation(bitSize, bitOffset);
updatePreferredSize();
bitFieldAllocation = new BitFieldAllocation(bitSize, bitOffset);
repaint();
}

Expand All @@ -333,8 +335,8 @@ void refresh(int bitSize, int bitOffset) {
void refresh(int byteSize, int byteOffset, int bitSize, int bitOffset) {
this.allocationByteOffset = byteOffset;
this.allocationByteSize = byteSize;
bitFieldAllocation = new BitFieldAllocation(bitSize, bitOffset);
updatePreferredSize();
bitFieldAllocation = new BitFieldAllocation(bitSize, bitOffset);
repaint();
}

Expand Down Expand Up @@ -1030,6 +1032,14 @@ private void refresh(boolean force) {
rightChopBytes = rightChop;
allocationBytes = allocationByteSize - leftChopBytes - rightChopBytes;

if (allocationBytes <= 0) {
int junk = 0;
// allocation shrunk - need to adjust window

// TODO: Need to adjust view port sizing when allocationByteSize changes

}

allocateBits();
layoutBits();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -281,6 +281,7 @@ protected void cancelCellEditing() {
if (cellEditor != null) {
cellEditor.cancelCellEditing();
}
provider.closeDependentEditors();
}

protected void stopCellEditing() {
Expand Down Expand Up @@ -545,7 +546,7 @@ private void scrollToCell(int rowIndex, int columnIndex) {
}
}

public void dataTypeManagerRestored() {
protected void dataTypeManagerRestored() {
DataTypeManager originalDTM = model.getOriginalDataTypeManager();
if (originalDTM == null) {
// editor unloaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,6 +21,7 @@

import docking.*;
import docking.widgets.OptionDialog;
import docking.widgets.table.GTable;
import generic.theme.GIcon;
import ghidra.app.context.ProgramActionContext;
import ghidra.app.services.DataTypeManagerService;
Expand Down Expand Up @@ -338,4 +339,23 @@ protected void registerHelp(Object object, String anchor) {
help.registerHelp(object, new HelpLocation(getHelpTopic(), getHelpName() + "_" + anchor));
}

protected void requestTableFocus() {

JTable table = editorPanel.getTable();
if (!table.isEditing()) {
table.requestFocus();
return;
}

if (table instanceof GTable gTable) {
gTable.requestTableEditorFocus();
}
else {
table.getEditorComponent().requestFocus();
}
}

protected void closeDependentEditors() {
// do nothing by default
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,7 +20,6 @@
import javax.swing.*;

import docking.action.*;
import docking.widgets.table.GTable;
import ghidra.framework.plugintool.Plugin;
import ghidra.framework.plugintool.PluginTool;
import ghidra.util.HelpLocation;
Expand Down Expand Up @@ -88,21 +87,8 @@ public void dispose() {
}

protected void requestTableFocus() {
if (provider == null) {
return; // must have been disposed
}

JTable table = ((CompositeEditorPanel) provider.getComponent()).getTable();
if (!table.isEditing()) {
table.requestFocus();
return;
}

if (table instanceof GTable gTable) {
gTable.requestTableEditorFocus();
}
else {
table.getEditorComponent().requestFocus();
if (provider != null) {
provider.requestTableFocus();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,14 +15,7 @@
*/
package ghidra.app.plugin.core.compositeeditor;

import java.awt.Component;

import javax.swing.JTable;

import docking.ActionContext;
import docking.DockingWindowManager;
import ghidra.program.model.data.DataTypeComponent;
import ghidra.program.model.data.Structure;
import ghidra.util.exception.AssertException;

public class EditBitFieldAction extends CompositeEditorTableAction {
Expand All @@ -41,50 +34,18 @@ public EditBitFieldAction(CompositeEditorProvider provider) {
adjustEnablement();
}

private DataTypeComponent getUnalignedBitFieldComponent() {
CompEditorModel editorModel = (CompEditorModel) model;
if ((editorModel.viewComposite instanceof Structure) &&
!editorModel.viewComposite.isPackingEnabled() &&
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 = getUnalignedBitFieldComponent();
if (dtComponent == null) {
return;
}

BitFieldEditorDialog dlg = new BitFieldEditorDialog(editorModel.viewComposite,
provider.dtmService, dtComponent.getOrdinal(), model.showHexNumbers,
ordinal -> refreshTableAndSelection(editorModel, ordinal));
Component c = provider.getComponent();
DockingWindowManager.showDialog(c, dlg);
requestTableFocus();
}

private void refreshTableAndSelection(CompEditorModel editorModel, int ordinal) {
editorModel.fireTableDataChanged();
editorModel.compositeInfoChanged();
JTable editorTable = provider.getTable();
editorTable.getSelectionModel().setSelectionInterval(ordinal, ordinal);
StructureEditorProvider structProvider = (StructureEditorProvider) provider;
structProvider.showBitFieldEditor();
}

@Override
public void adjustEnablement() {
setEnabled(getUnalignedBitFieldComponent() != null);
// Unions do not support non-packed manipulation of bitfields
boolean enabled = (provider instanceof StructureEditorProvider structProvider) &&
structProvider.getSelectedNonPackedBitFieldComponent() != null;
setEnabled(enabled);
}

}
Loading

0 comments on commit 02b0dfe

Please sign in to comment.