1480 lines
46 KiB
TypeScript
1480 lines
46 KiB
TypeScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
/**
|
|
* `EDA_DRAW_FRAME` (include/eda_draw_frame.h, common/eda_draw_frame.cpp): the
|
|
* base of every frame with a drawing canvas, a KIWAY_PLAYER as upstream's
|
|
* is. What is here is the part the model layer reaches: the canvas accessor, the colour settings, the message
|
|
* panel hooks and the item resolver — and, first, the grid snapping the
|
|
* frame owns.
|
|
*
|
|
* Grid snapping, `EDA_DRAW_FRAME::GetNearestGridPosition` /
|
|
* `::GetNearestHalfGridPosition` (`common/eda_draw_frame.cpp:1073/1098`).
|
|
*
|
|
* Upstream reads the grid size and origin off the frame's GAL, so both live on
|
|
* the *frame* — in `common/`, instantiated once and used by every editor. Ours
|
|
* therefore belongs in a shared module too, parameterised by the grid rather
|
|
* than duplicated per editor: the symbol editor already had a private copy
|
|
* (`designer/src/editors/symbol/edits.ts`), and the schematic transform needs
|
|
* exactly the same answer.
|
|
*
|
|
* The grid origin is a user setting that we do not model anywhere yet, so
|
|
* `gridOrigin` defaults to zero, which makes both `fmod` offsets zero.
|
|
*/
|
|
|
|
import { EDA_SEARCH_DATA } from './eda_search_data.js';
|
|
import { LIB_ID } from './lib_id.js';
|
|
import type { LibTreeNode } from './lib_tree_model.js';
|
|
import { type Color4d, LEGACY_COLORS } from './gal/color4d.js';
|
|
import { EDITOR_CONDITIONS } from './tool/editor_conditions.js';
|
|
import { ACTION_CONDITIONS } from './tool/action_manager.js';
|
|
import { GAL_DISPLAY_OPTIONS_IMPL } from './gal_display_options_common.js';
|
|
import type { Vec2 } from '@ziroeda/kimath/src/math/vector2.js';
|
|
import { BOX2D } from '@ziroeda/kimath/src/math/box2.js';
|
|
import type { EdaIuScale, EdaUnits } from './eda_units.js';
|
|
import { KIWAY_PLAYER } from './kiway_player.js';
|
|
import type { FRAME_T } from './frame_type.js';
|
|
import type { KIID } from './kiid.js';
|
|
import type { EDA_DRAW_FRAME_LIKE, EDA_ITEM } from './eda_item.js';
|
|
import type { ORIGIN_TRANSFORMS } from './origin_transforms.js';
|
|
import type { BASE_SCREEN } from './base_screen.js';
|
|
import { DS_DRAW_ITEM_LIST } from './drawing_sheet/ds_draw_item.js';
|
|
import { DoubleValueFromStringIn, unityScale } from './eda_units.js';
|
|
import type { PAGE_INFO } from './page_info.js';
|
|
import type { RENDER_SETTINGS } from './render_settings.js';
|
|
import type { PROJECT_TEXT_VARS, TITLE_BLOCK } from './title_block.js';
|
|
import { type EDA_DRAW_PANEL_GAL, GAL_TYPE } from './draw_panel_gal.js';
|
|
import { DEFAULT_THEME, GetColorSettings, PgmOrNull } from './pgm_base.js';
|
|
import type { COLOR_SETTINGS } from './settings/color_settings.js';
|
|
import { MSG_PANEL_ITEM } from './widgets/msgpanel.js';
|
|
import { type APP_SETTINGS_BASE, EdaUnitsFromInt, EdaUnitsToInt } from './settings/app_settings.js';
|
|
import type { SELECTION } from './tool/selection.js';
|
|
import { ACTIONS } from './tool/actions.js';
|
|
import { COMMON_TOOLS } from './tool/common_tools.js';
|
|
import { TOOL_EVENT } from './tool/tool_event.js';
|
|
import { GRID_MENU } from './tool/grid_menu.js';
|
|
import { SELECTION_CONDITIONS } from './tool/selection_conditions.js';
|
|
import type { TOOL_MENU } from './tool/tool_menu.js';
|
|
import { ZOOM_MENU } from './tool/zoom_menu.js';
|
|
import { IsImperialUnit } from './units_provider.js';
|
|
import { RENDER_TARGET } from './gal/definitions.js';
|
|
import { type wxChoice, wxNOT_FOUND } from '@ziroeda/wx/choice.js';
|
|
import type { ChooserFilter } from '@ziroeda/wx/filedlg.js';
|
|
import type { wxFileDialogCustomizeHook } from './widgets/filedlg_hook_new_library.js';
|
|
import {
|
|
wxDD_DEFAULT_STYLE,
|
|
wxDD_DIR_MUST_EXIST,
|
|
wxFD_CHANGE_DIR,
|
|
wxFD_FILE_MUST_EXIST,
|
|
wxFD_OPEN,
|
|
wxFD_OVERWRITE_PROMPT,
|
|
wxFD_SAVE,
|
|
} from '@ziroeda/wx/defs.js';
|
|
import { BOX2I } from '@ziroeda/kimath/src/math/box2.js';
|
|
import { WX_IMAGE } from '@ziroeda/wx/wx_image.js';
|
|
import { BITMAP_TYPE } from './bitmap.js';
|
|
import { KiROUND } from '@ziroeda/kimath/src/math/util.js';
|
|
import type { VECTOR2I } from '@ziroeda/kimath/src/math/vector2.js';
|
|
|
|
/**
|
|
* `GRID_HELPER` as `COMMON_TOOLS::CursorControl` reads it. A frame that
|
|
* snaps to items (the PCB and schematic editors) returns one from
|
|
* MakeGridHelper; the base and GerbView return none.
|
|
*/
|
|
export interface GRID_HELPER_LIKE {
|
|
GetSelectionGrid(aSelection: SELECTION): number;
|
|
GetGridSize(aGrid: number): { x: number; y: number };
|
|
}
|
|
|
|
/** `KiROUND`: round half *away from zero*, not half up as `Math.round` does. */
|
|
const kiRound = (v: number): number => (v < 0 ? -Math.round(-v) : Math.round(v));
|
|
|
|
/** `KiROUND( (p - offset) / size ) * size + offset` on one axis. */
|
|
const snapAxis = (v: number, size: number, origin: number): number => {
|
|
const offset = origin % size;
|
|
return kiRound((v - offset) / size) * size + offset;
|
|
};
|
|
|
|
/** `EDA_DRAW_FRAME::GetNearestGridPosition`: the nearest point on the grid. */
|
|
export function nearestGridPosition(
|
|
p: Vec2,
|
|
grid: number,
|
|
gridOrigin: Vec2 = { x: 0, y: 0 },
|
|
): Vec2 {
|
|
return {
|
|
x: snapAxis(p.x, grid, gridOrigin.x),
|
|
y: snapAxis(p.y, grid, gridOrigin.y),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* `EDA_DRAW_FRAME::GetNearestHalfGridPosition`: the same, on a grid of half the
|
|
* step. Rotate and mirror snap their centre with this one, so a two-item
|
|
* selection whose midpoint falls between grid points still lands somewhere the
|
|
* items' own endpoints can reach.
|
|
*/
|
|
export function nearestHalfGridPosition(
|
|
p: Vec2,
|
|
grid: number,
|
|
gridOrigin: Vec2 = { x: 0, y: 0 },
|
|
): Vec2 {
|
|
return nearestGridPosition(p, grid / 2, gridOrigin);
|
|
}
|
|
|
|
// The frame names (`eda_draw_frame.h:70-77`), which an item's message panel reads to know
|
|
// which editor it is describing itself for.
|
|
/**
|
|
* `LIB_TREE` (`include/widgets/lib_tree.h`), the part of it a frame's tools
|
|
* reach through `GetLibTree()`. The widget itself is `widgets/lib_tree.tsx`.
|
|
*/
|
|
export interface LIB_TREE {
|
|
/** Append the selected rows' nodes; return how many rows are selected. */
|
|
GetSelectedTreeNodes(aSelection: LibTreeNode[]): number;
|
|
/** Regenerate the tree, keeping the expanded state when @p aKeepState. */
|
|
Regenerate(aKeepState: boolean): void;
|
|
/** Scroll so @p aLibId's row is centred. */
|
|
CenterLibId(aLibId: LIB_ID): void;
|
|
/**
|
|
* `GetSelectedLibId( int* aUnit = nullptr )`: the selected row's LIB_ID — a
|
|
* library row's has no item name — or an empty one.
|
|
*/
|
|
GetSelectedLibId(): LIB_ID;
|
|
/** `SelectLibId( aLibId )`: select the row, expanding its library. */
|
|
SelectLibId(aLibId: LIB_ID): void;
|
|
/** `Unselect()`. */
|
|
Unselect(): void;
|
|
/** `RefreshLibTree()`: repaint the rows without rebuilding them. */
|
|
RefreshLibTree(): void;
|
|
}
|
|
|
|
/**
|
|
* `PROPERTIES_PANEL` (`include/widgets/properties_panel.h`), the two calls
|
|
* `UpdateProperties` makes. The panels themselves are React views.
|
|
*/
|
|
export interface PROPERTIES_PANEL {
|
|
IsShownOnScreen(): boolean;
|
|
/** Rebuild the grid from the current selection. */
|
|
UpdateData(): void;
|
|
}
|
|
|
|
export const LIB_EDIT_FRAME_NAME = 'LibeditFrame';
|
|
export const SCH_EDIT_FRAME_NAME = 'SchematicFrame';
|
|
export const SYMBOL_CHOOSER_FRAME_NAME = 'SymbolChooserFrame';
|
|
export const PL_EDITOR_FRAME_NAME = 'PlEditorFrame';
|
|
export const FOOTPRINT_WIZARD_FRAME_NAME = 'FootprintWizard';
|
|
export const FOOTPRINT_CHOOSER_FRAME_NAME = 'FootprintChooserFrame';
|
|
export const FOOTPRINT_EDIT_FRAME_NAME = 'ModEditFrame';
|
|
export const FOOTPRINT_VIEWER_FRAME_NAME = 'ModViewFrame';
|
|
export const PCB_EDIT_FRAME_NAME = 'PcbFrame';
|
|
|
|
/** `HOTKEY_CYCLE_POPUP`, as the frame and its tools call it. */
|
|
export interface HOTKEY_CYCLE_POPUP_LIKE {
|
|
Popup(aTitle: string, aItems: readonly string[], aSelection: number): void;
|
|
}
|
|
|
|
export abstract class EDA_DRAW_FRAME extends KIWAY_PLAYER {
|
|
/** `m_hotkeyPopup` (eda_draw_frame.h). */
|
|
protected m_hotkeyPopup: HOTKEY_CYCLE_POPUP_LIKE | null = null;
|
|
|
|
/**
|
|
* `wxFileDialog( this, aTitle, aDefaultDir, aDefaultFile, aWildcard, aStyle ).ShowModal()` then
|
|
* `GetPath()`: the window's file picker. Null is wxID_CANCEL - and with no window there is
|
|
* nothing to pick from, so a frame without one always cancels.
|
|
*/
|
|
ShowFileDialog(
|
|
_aTitle: string,
|
|
_aDefaultDir: string,
|
|
_aDefaultFile: string,
|
|
_aWildcard: string | readonly ChooserFilter[],
|
|
_aStyle: number,
|
|
aCustomizeHook?: wxFileDialogCustomizeHook,
|
|
): Promise<string | null> {
|
|
// `dlg.SetCustomizeHook( hook )`: a hook the window does not show stays unattached.
|
|
if (aCustomizeHook) aCustomizeHook.attached = false;
|
|
|
|
return Promise.resolve(null);
|
|
}
|
|
|
|
/**
|
|
* Not KiCad's: where a generated file goes. Upstream writes to the path a wxFileDialog
|
|
* returned; outputs download here, so a frame hands the bytes to its window. A frame with no
|
|
* window has nowhere to put them.
|
|
*/
|
|
WriteOutputFile(_aPath: string, _aBytes: Uint8Array, _aMime: string): void {}
|
|
|
|
/**
|
|
* Save the current view as an image file, \a aFileName in \a aBitmapType's format. Only PNG
|
|
* has an encoder here (both callers ask for PNG); the others return false. `wxImage::SaveFile`
|
|
* becomes the bytes handed to WriteOutputFile.
|
|
*/
|
|
SaveCanvasImageToFile(aFileName: string, aBitmapType: BITMAP_TYPE): boolean {
|
|
const image = new WX_IMAGE();
|
|
|
|
if (!this.GetCanvas()?.GetScreenshot(image)) {
|
|
// Upstream blits the window through a wxClientDC for a backend with no GL screenshot. The
|
|
// Cairo canvas's pixels are not read back here, so there is nothing to save.
|
|
return false;
|
|
}
|
|
|
|
let bytes: Uint8Array | null = null;
|
|
|
|
switch (aBitmapType) {
|
|
case BITMAP_TYPE.PNG:
|
|
bytes = image.SaveFilePng();
|
|
break;
|
|
case BITMAP_TYPE.BMP:
|
|
case BITMAP_TYPE.JPG:
|
|
break;
|
|
}
|
|
|
|
if (!bytes) return false;
|
|
|
|
this.WriteOutputFile(aFileName, bytes, 'image/png');
|
|
return true;
|
|
}
|
|
|
|
/** `wxDirDialog( this, aTitle, aDefaultDir, aStyle ).ShowModal()` then `GetPath()`; null is Cancel. */
|
|
ShowDirDialog(_aTitle: string, _aDefaultDir: string, _aStyle: number): Promise<string | null> {
|
|
return Promise.resolve(null);
|
|
}
|
|
|
|
/**
|
|
* `LibraryFileBrowser( aTitle, doOpen, aFilename, wildcard, ext, isDirectory, aFileDlgHook )`
|
|
* (eda_draw_frame.cpp:1259): ask for a library file or folder; \a aFilename is in-out.
|
|
*/
|
|
async LibraryFileBrowser(
|
|
aTitle: string,
|
|
doOpen: boolean,
|
|
aFilename: { value: string },
|
|
wildcard: string | readonly ChooserFilter[],
|
|
ext: string,
|
|
isDirectory = false,
|
|
aFileDlgHook?: wxFileDialogCustomizeHook,
|
|
): Promise<boolean> {
|
|
const setExt = (aPath: string) => {
|
|
const slash = aPath.lastIndexOf('/');
|
|
const dot = aPath.lastIndexOf('.');
|
|
const stem = dot > slash + 1 ? aPath.slice(0, dot) : aPath;
|
|
|
|
return ext === '' ? stem : `${stem}.${ext}`;
|
|
};
|
|
|
|
aFilename.value = setExt(aFilename.value);
|
|
|
|
const slash = aFilename.value.lastIndexOf('/');
|
|
let defaultDir = slash < 0 ? '' : aFilename.value.slice(0, slash) || '/';
|
|
|
|
if (defaultDir === '') defaultDir = this.GetMruPath();
|
|
|
|
if (isDirectory && doOpen) {
|
|
const path = await this.ShowDirDialog(
|
|
aTitle,
|
|
defaultDir,
|
|
wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST,
|
|
);
|
|
|
|
if (path === null) return false;
|
|
|
|
aFilename.value = setExt(path);
|
|
} else {
|
|
// Ensure the file has a dummy name, otherwise GTK will display the regex from the filter
|
|
let fullName = aFilename.value.slice(slash + 1);
|
|
|
|
if (fullName === '' || fullName.startsWith('.')) fullName = setExt('Library');
|
|
|
|
const path = await this.ShowFileDialog(
|
|
aTitle,
|
|
defaultDir,
|
|
fullName,
|
|
wildcard,
|
|
doOpen
|
|
? wxFD_OPEN | wxFD_FILE_MUST_EXIST
|
|
: wxFD_SAVE | wxFD_CHANGE_DIR | wxFD_OVERWRITE_PROMPT,
|
|
aFileDlgHook,
|
|
);
|
|
|
|
if (path === null) return false;
|
|
|
|
aFilename.value = setExt(path);
|
|
}
|
|
|
|
const at = aFilename.value.lastIndexOf('/');
|
|
this.SetMruPath(at < 0 ? '' : aFilename.value.slice(0, at) || '/');
|
|
|
|
return true;
|
|
}
|
|
|
|
/** `PropertiesPaneName()` (eda_draw_frame.h:437). */
|
|
static PropertiesPaneName(): string {
|
|
return 'PropertiesManager';
|
|
}
|
|
|
|
/** `NetInspectorPanelName()` (eda_draw_frame.h:439). */
|
|
static NetInspectorPanelName(): string {
|
|
return 'NetInspector';
|
|
}
|
|
|
|
/** `DesignBlocksPaneName()` (eda_draw_frame.h:441). */
|
|
static DesignBlocksPaneName(): string {
|
|
return 'DesignBlocks';
|
|
}
|
|
|
|
/** `AppearancePanelName()` (eda_draw_frame.h:445). */
|
|
static AppearancePanelName(): string {
|
|
return 'LayersManager';
|
|
}
|
|
|
|
protected m_canvas: EDA_DRAW_PANEL_GAL | null = null;
|
|
/** `m_findReplaceData`: the last Find's options and string. */
|
|
protected m_findReplaceData = new EDA_SEARCH_DATA();
|
|
/** `m_findStringHistoryList`: the Find combo's history, newest first. */
|
|
protected m_findStringHistoryList: string[] = [];
|
|
protected m_propertiesPanel: PROPERTIES_PANEL | null = null;
|
|
protected m_currentScreen: BASE_SCREEN | null = null; ///< current used SCREEN
|
|
protected m_colorSettings: COLOR_SETTINGS | null = null;
|
|
protected m_galDisplayOptions: GAL_DISPLAY_OPTIONS_IMPL = new GAL_DISPLAY_OPTIONS_IMPL();
|
|
protected m_canvasType: GAL_TYPE = GAL_TYPE.GAL_TYPE_OPENGL;
|
|
/// The background color of the draw canvas; BLACK for Pcbnew, BLACK or WHITE
|
|
/// for Eeschema (eda_draw_frame.cpp:121).
|
|
protected m_drawBgColor: Color4d = LEGACY_COLORS.BLACK;
|
|
|
|
abstract GetPageSettings(): PAGE_INFO;
|
|
|
|
abstract GetTitleBlock(): TITLE_BLOCK;
|
|
|
|
/** `GetScreenDesc()`: the base class returns an empty string (eda_draw_frame.cpp:1249-1253). */
|
|
GetScreenDesc(): string {
|
|
return '';
|
|
}
|
|
|
|
/** `GetFullScreenDesc()`: likewise empty in the base class (:1256-1260). */
|
|
GetFullScreenDesc(): string {
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Print the drawing-sheet (frame and title block) to the settings' print DC
|
|
* (`eda_draw_frame.cpp:1219-1246`).
|
|
*
|
|
* @param aScreen screen to draw.
|
|
* @param aProperties optional properties for text variable resolution.
|
|
* @param aMils2Iu the scaling factor from mils to internal units.
|
|
* @param aFilename the filename to display in basic inscriptions.
|
|
* @param aSheetLayer the layer displayed in the title block.
|
|
*/
|
|
PrintDrawingSheet(
|
|
aSettings: RENDER_SETTINGS,
|
|
aScreen: BASE_SCREEN,
|
|
aProperties: Map<string, string> | null,
|
|
aMils2Iu: number,
|
|
aFilename: string,
|
|
aSheetLayer = '',
|
|
): void {
|
|
if (!this.m_showBorderAndTitleBlock) return;
|
|
|
|
const DC = aSettings.GetPrintDC()!;
|
|
const origin = DC.GetDeviceOrigin();
|
|
|
|
if (origin.y > 0) {
|
|
DC.SetDeviceOrigin(0, 0);
|
|
DC.SetAxisOrientation(true, false);
|
|
}
|
|
|
|
PrintDrawingSheet(
|
|
aSettings,
|
|
this.GetPageSettings(),
|
|
this.GetScreenDesc(),
|
|
this.GetFullScreenDesc(),
|
|
aFilename,
|
|
this.GetTitleBlock(),
|
|
aProperties,
|
|
aScreen.GetPageCount(),
|
|
aScreen.GetPageNumber(),
|
|
aMils2Iu,
|
|
this.Prj(),
|
|
aSheetLayer,
|
|
aScreen.GetVirtualPageNumber() === 1,
|
|
);
|
|
|
|
if (origin.y > 0) {
|
|
DC.SetDeviceOrigin(origin.x, origin.y);
|
|
DC.SetAxisOrientation(true, true);
|
|
}
|
|
}
|
|
|
|
GetDrawBgColor(): Color4d {
|
|
return this.m_drawBgColor;
|
|
}
|
|
|
|
SetDrawBgColor(aColor: Color4d): void {
|
|
this.m_drawBgColor = aColor;
|
|
}
|
|
|
|
/// Show the drawing sheet (border & title block).
|
|
protected m_showBorderAndTitleBlock = false;
|
|
|
|
/// Choice box to choose the grid size.
|
|
protected m_gridSelectBox: wxChoice | null = null;
|
|
/// Choice box to choose the zoom value.
|
|
protected m_zoomSelectBox: wxChoice | null = null;
|
|
/// Index of the off-preset "Zoom %.2f" entry, or wxNOT_FOUND.
|
|
protected m_zoomCustomEntry = wxNOT_FOUND;
|
|
|
|
/// For those frames that support polar coordinates.
|
|
protected m_polarCoords = false;
|
|
|
|
protected m_replaceStringHistoryList: string[] = [];
|
|
|
|
constructor(aFrameType: FRAME_T, aIuScale: EdaIuScale, aUnits: EdaUnits) {
|
|
super(aFrameType, aIuScale, aUnits);
|
|
}
|
|
|
|
/** `GetFindReplaceData()` (eda_draw_frame.cpp:198). */
|
|
GetFindReplaceData(): EDA_SEARCH_DATA {
|
|
return this.m_findReplaceData;
|
|
}
|
|
|
|
/** `GetFindHistoryList()` (eda_draw_frame.h:121). */
|
|
GetFindHistoryList(): string[] {
|
|
return this.m_findStringHistoryList;
|
|
}
|
|
|
|
/**
|
|
* Return a pointer to GAL-based canvas of given EDA draw frame.
|
|
*/
|
|
GetCanvas(): EDA_DRAW_PANEL_GAL | null {
|
|
return this.m_canvas;
|
|
}
|
|
|
|
SetCanvas(aPanel: EDA_DRAW_PANEL_GAL | null): void {
|
|
this.m_canvas = aPanel;
|
|
}
|
|
|
|
/**
|
|
* `findDialogs()`: the DIALOG_SHIM children of the frame, as the screen
|
|
* rectangles FocusOnLocation and FocusOnItems steer clear of. A window
|
|
* without children answers none; the designer's frames list their open
|
|
* modeless dialogs.
|
|
*/
|
|
findDialogRects(): BOX2D[] {
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* `KIGFX::VIEW::SetCenter( aPos, dialogScreenRects )` as the window applies it.
|
|
* The designer's editor still owns the view transform (its VIEW is synced
|
|
* from it), so the frame lets it perform the centre; the base uses the VIEW.
|
|
*/
|
|
protected setViewCenter(aPos: Vec2, aObscuringScreenRects: readonly BOX2D[]): void {
|
|
this.GetCanvas()!.GetView().SetCenter(aPos, aObscuringScreenRects);
|
|
}
|
|
|
|
/**
|
|
* Focus on a particular canvas location.
|
|
*
|
|
* The view will be centred on the location if it is not already in view, or
|
|
* behind an obscuring dialog.
|
|
*/
|
|
FocusOnLocation(aPos: Vec2, aAllowScroll = true): void {
|
|
let centerView = false;
|
|
const dialogScreenRects: BOX2D[] = [];
|
|
|
|
if (aAllowScroll) {
|
|
const r = this.GetCanvas()!.GetView().GetViewport();
|
|
|
|
// Center if we're off the current view, or within 10% of its edge
|
|
r.Inflate(-r.GetWidth() / 10.0);
|
|
|
|
if (!r.Contains(aPos)) centerView = true;
|
|
|
|
for (const dialog of this.findDialogRects()) dialogScreenRects.push(dialog);
|
|
|
|
// Center if we're behind an obscuring dialog, or within 10% of its edge
|
|
for (const rect of dialogScreenRects) {
|
|
const inflated = new BOX2D(rect.GetOrigin(), rect.GetSize());
|
|
inflated.Inflate(inflated.GetWidth() / 10);
|
|
|
|
if (inflated.Contains(this.GetCanvas()!.GetView().ToScreen(aPos))) centerView = true;
|
|
}
|
|
}
|
|
|
|
if (centerView) {
|
|
try {
|
|
this.setViewCenter(aPos, dialogScreenRects);
|
|
} catch (e) {
|
|
// wxFAIL_MSG( "Clipper2 exception occurred centering object" )
|
|
console.error('Clipper2 exception occurred centering object:', e);
|
|
}
|
|
}
|
|
|
|
this.GetCanvas()!.GetViewControls().SetCrossHairCursorPosition(aPos);
|
|
}
|
|
|
|
/**
|
|
* Focus on a particular item.
|
|
*/
|
|
FocusOnItem(_aItem: EDA_ITEM | null, _aAllowScroll = true): void {}
|
|
|
|
ClearFocus(): void {
|
|
this.FocusOnItem(null);
|
|
}
|
|
|
|
/**
|
|
* `m_overrideLocksCb`: the "Override locks" checkbox `configureToolbars` puts on a
|
|
* toolbar that asks for `ACTION_TOOLBAR_CONTROLS::overrideLocks`
|
|
* (eda_draw_frame.cpp:238-246). Null until a window wires one.
|
|
*/
|
|
m_overrideLocksCb: { GetValue(): boolean } | null = null;
|
|
|
|
/** `GetOverrideLocks()` (eda_draw_frame.cpp:577-583). */
|
|
GetOverrideLocks(): boolean {
|
|
if (this.m_overrideLocksCb) return this.m_overrideLocksCb.GetValue();
|
|
|
|
return false;
|
|
}
|
|
|
|
GetGalDisplayOptions(): GAL_DISPLAY_OPTIONS_IMPL {
|
|
return this.m_galDisplayOptions;
|
|
}
|
|
|
|
/** `Kiway().Player( aFrameType, false )`: the sibling frame if it is open. */
|
|
KiwayPlayer(aFrameType: FRAME_T): KIWAY_PLAYER | null {
|
|
return this.Kiway()?.GetPlayerFrame(aFrameType) ?? null;
|
|
}
|
|
|
|
/**
|
|
* `wxWindow::Raise()`: bring the frame to the front - here, the program's `Kiway().Player()`
|
|
* for this frame type, which shows it.
|
|
*/
|
|
Raise(): void {
|
|
this.Kiway()?.Player(this.GetFrameType());
|
|
}
|
|
|
|
/**
|
|
* Use to start up the GAL drawing canvas.
|
|
*/
|
|
ActivateGalCanvas(): void {
|
|
// GetCanvas()->SetEvtHandlerEnabled( true );
|
|
this.GetCanvas()!.StartDrawing();
|
|
}
|
|
|
|
/**
|
|
* Change the current rendering backend.
|
|
*/
|
|
SwitchCanvas(aCanvasType: GAL_TYPE): void {
|
|
this.GetCanvas()!.SwitchBackend(aCanvasType);
|
|
this.m_canvasType = this.GetCanvas()!.GetBackend();
|
|
|
|
this.ActivateGalCanvas();
|
|
}
|
|
|
|
/**
|
|
* Return a pointer to the active color theme settings.
|
|
*/
|
|
GetColorSettings(aForceRefresh = false): COLOR_SETTINGS {
|
|
if (!this.m_colorSettings || aForceRefresh) {
|
|
const colorSettings = GetColorSettings(DEFAULT_THEME);
|
|
this.m_colorSettings = colorSettings;
|
|
}
|
|
|
|
return this.m_colorSettings;
|
|
}
|
|
|
|
GetScreen(): BASE_SCREEN | null {
|
|
return this.m_currentScreen;
|
|
}
|
|
|
|
SetScreen(aScreen: BASE_SCREEN | null): void {
|
|
this.m_currentScreen = aScreen;
|
|
}
|
|
|
|
override GetToolCanvas(): unknown {
|
|
return this.GetCanvas();
|
|
}
|
|
|
|
/**
|
|
* Return the frame's name, for the message panel and cross-references.
|
|
*/
|
|
abstract GetName(): string;
|
|
|
|
/**
|
|
* Return a reference to the default ORIGIN_TRANSFORMS object
|
|
*/
|
|
abstract GetOriginTransforms(): ORIGIN_TRANSFORMS;
|
|
|
|
/**
|
|
* Fetch an item by KIID. Frame-type-specific implementation.
|
|
*/
|
|
ResolveItem(_aId: KIID, _aAllowNullptrReturn = false): EDA_ITEM | null {
|
|
return null;
|
|
}
|
|
|
|
/// The message panel's rows, `EDA_MSG_PANEL::m_Items`.
|
|
private m_msgPanelItems: MSG_PANEL_ITEM[] = [];
|
|
private m_msgPanelSink: ((aItems: readonly MSG_PANEL_ITEM[]) => void) | null = null;
|
|
|
|
/** The page's message panel: where the rows go whenever they change. */
|
|
SetMsgPanelSink(aSink: ((aItems: readonly MSG_PANEL_ITEM[]) => void) | null): void {
|
|
this.m_msgPanelSink = aSink;
|
|
}
|
|
|
|
GetMsgPanelItems(): readonly MSG_PANEL_ITEM[] {
|
|
return this.m_msgPanelItems;
|
|
}
|
|
|
|
private msgPanelChanged(): void {
|
|
this.m_msgPanelSink?.(this.m_msgPanelItems);
|
|
}
|
|
|
|
/**
|
|
* Append a message to the message panel.
|
|
*
|
|
* This helper method checks to make sure the message panel exists in the frame and
|
|
* appends a message to it using the message panel AppendMessage() method.
|
|
*/
|
|
AppendMsgPanel(aTextUpper: string, aTextLower: string, aPadding = 6): void {
|
|
if (this.m_isClosing) return;
|
|
|
|
this.m_msgPanelItems.push(new MSG_PANEL_ITEM(aTextUpper, aTextLower, aPadding));
|
|
this.msgPanelChanged();
|
|
}
|
|
|
|
/** `EraseMsgBox()`: the message panel's EraseMsgBox. */
|
|
EraseMsgBox(): void {
|
|
this.m_msgPanelItems = [];
|
|
this.msgPanelChanged();
|
|
}
|
|
|
|
/**
|
|
* Clear all messages from the message panel.
|
|
*/
|
|
ClearMsgPanel(): void {
|
|
if (this.m_isClosing) return;
|
|
|
|
this.m_msgPanelItems = [];
|
|
this.msgPanelChanged();
|
|
}
|
|
|
|
/**
|
|
* Clear the message panel and populates it with the contents of \a aList.
|
|
*
|
|
* @param aList is the list of #MSG_PANEL_ITEM objects to fill the message panel.
|
|
*/
|
|
SetMsgPanel(aList: readonly MSG_PANEL_ITEM[]): void;
|
|
SetMsgPanel(aTextUpper: string, aTextLower: string, aPadding?: number): void;
|
|
SetMsgPanel(aItem: EDA_ITEM): void;
|
|
SetMsgPanel(a: readonly MSG_PANEL_ITEM[] | EDA_ITEM | string, b = '', c = 6): void {
|
|
if (typeof a === 'string') {
|
|
if (this.m_isClosing) return;
|
|
|
|
this.m_msgPanelItems = [new MSG_PANEL_ITEM(a, b, c)];
|
|
this.msgPanelChanged();
|
|
return;
|
|
}
|
|
|
|
if (Array.isArray(a)) {
|
|
if (this.m_isClosing) return;
|
|
|
|
this.m_msgPanelItems = [...(a as readonly MSG_PANEL_ITEM[])];
|
|
this.msgPanelChanged();
|
|
return;
|
|
}
|
|
|
|
const items: MSG_PANEL_ITEM[] = [];
|
|
(a as EDA_ITEM).GetMsgPanelInfo(this.AsDrawFrameLike(), items);
|
|
this.SetMsgPanel(items);
|
|
}
|
|
|
|
/**
|
|
* The frame as an item's GetMsgPanelInfo takes it: the C++ frame IS a
|
|
* UNITS_PROVIDER, ours holds one, so this is the provider with the frame's
|
|
* three calls laid over it (units changes show through, as it is the same
|
|
* object underneath).
|
|
*/
|
|
AsDrawFrameLike(): EDA_DRAW_FRAME_LIKE {
|
|
const frameLike = Object.create(this.GetUnitsProvider()) as EDA_DRAW_FRAME_LIKE;
|
|
|
|
frameLike.GetName = () => this.GetName();
|
|
frameLike.GetOriginTransforms = () => this.GetOriginTransforms();
|
|
frameLike.ResolveItem = (aId, aAllowNullptrReturn) =>
|
|
this.ResolveItem(aId, aAllowNullptrReturn);
|
|
|
|
return frameLike;
|
|
}
|
|
|
|
/**
|
|
* Redraw the message panel.
|
|
*/
|
|
UpdateMsgPanel(): void {}
|
|
|
|
GetLibTree(): LIB_TREE | null {
|
|
return null;
|
|
}
|
|
GetTargetLibId(): LIB_ID {
|
|
return new LIB_ID();
|
|
}
|
|
|
|
IsLibraryTreeShown(): boolean {
|
|
return false;
|
|
}
|
|
ToggleLibraryTree(): void {}
|
|
FocusLibraryTreeInput(): void {}
|
|
|
|
GetPropertiesPanel(): PROPERTIES_PANEL | null {
|
|
return this.m_propertiesPanel;
|
|
}
|
|
|
|
/** The window that shows `m_propertiesPanel` hands it over here; null when it closes. */
|
|
SetPropertiesPanel(aPanel: PROPERTIES_PANEL | null): void {
|
|
this.m_propertiesPanel = aPanel;
|
|
}
|
|
|
|
/** `EDA_DRAW_FRAME::UpdateProperties()` (eda_draw_frame.cpp:1336-1342). */
|
|
UpdateProperties(): void {
|
|
if (this.m_isClosing || !this.m_propertiesPanel || !this.m_propertiesPanel.IsShownOnScreen())
|
|
return;
|
|
|
|
this.m_propertiesPanel.UpdateData();
|
|
}
|
|
|
|
ToggleProperties(): void {}
|
|
|
|
// ---- grid ---------------------------------------------------------------
|
|
|
|
/**
|
|
* Return the absolute coordinates of the origin of the snap grid.
|
|
*
|
|
* This is treated as a relative offset and snapping will occur at multiples
|
|
* of the grid size relative to this point.
|
|
*/
|
|
abstract GetGridOrigin(): VECTOR2I;
|
|
abstract SetGridOrigin(aPosition: VECTOR2I): void;
|
|
|
|
MakeGridHelper(): GRID_HELPER_LIKE | null {
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @return true if the grid must be shown.
|
|
*/
|
|
IsGridVisible(): boolean {
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return true;
|
|
|
|
return this.GetWindowSettings(cfg).grid.show;
|
|
}
|
|
|
|
/**
|
|
* It may be overloaded by derived classes.
|
|
*
|
|
* @param aVisible true if the grid must be shown.
|
|
*/
|
|
SetGridVisibility(aVisible: boolean): void {
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return;
|
|
|
|
this.GetWindowSettings(cfg).grid.show = aVisible;
|
|
|
|
// Update the display with the new grid
|
|
const canvas = this.GetCanvas();
|
|
|
|
if (canvas) {
|
|
// Check to ensure these exist, since this function could be called before
|
|
// the GAL and View have been created
|
|
canvas.GetGAL()?.SetGridVisibility(aVisible);
|
|
canvas.GetView()?.MarkTargetDirty(RENDER_TARGET.TARGET_NONCACHED);
|
|
|
|
canvas.Refresh();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `IsScriptingConsoleVisible()`: KiCad's Python console, which a browser
|
|
* build has none of.
|
|
*/
|
|
IsScriptingConsoleVisible(): boolean {
|
|
return false;
|
|
}
|
|
|
|
/** `setupUIConditions()` (`eda_draw_frame.cpp`): the three units rows. */
|
|
protected override setupUIConditions(): void {
|
|
super.setupUIConditions();
|
|
|
|
const mgr = this.m_toolManager!.GetActionManager();
|
|
const cond = new EDITOR_CONDITIONS(this);
|
|
|
|
mgr.SetConditions(ACTIONS.millimetersUnits, new ACTION_CONDITIONS().Check(cond.Units('mm')));
|
|
mgr.SetConditions(ACTIONS.inchesUnits, new ACTION_CONDITIONS().Check(cond.Units('in')));
|
|
mgr.SetConditions(ACTIONS.milsUnits, new ACTION_CONDITIONS().Check(cond.Units('mils')));
|
|
}
|
|
|
|
IsGridOverridden(): boolean {
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return false;
|
|
|
|
return this.GetWindowSettings(cfg).grid.overrides_enabled;
|
|
}
|
|
|
|
SetGridOverrides(aOverride: boolean): void {
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return;
|
|
|
|
this.GetWindowSettings(cfg).grid.overrides_enabled = aOverride;
|
|
}
|
|
|
|
/**
|
|
* `GetCanvas()->GetGAL()->GetGridSize()`. A frame driven without a canvas (the headless
|
|
* netlister, the AI's edits before the window hosts a GAL canvas) has none to ask, so it takes
|
|
* the grid COMMON_TOOLS::OnGridChanged would have handed the GAL: the window settings' current
|
|
* preset (common_tools.cpp Reset), in this frame's units.
|
|
*/
|
|
private galGridSize(): Vec2 {
|
|
const gal = this.GetCanvas()?.GetGAL();
|
|
|
|
if (gal) return gal.GetGridSize();
|
|
|
|
const cfg = this.config();
|
|
const grid = cfg ? this.GetWindowSettings(cfg).grid : null;
|
|
const grids = grid && grid.grids.length > 0 ? grid.grids : (cfg?.DefaultGridSizeList() ?? []);
|
|
const def = grids[Math.max(0, Math.min(grid?.last_size_idx ?? 0, grids.length - 1))];
|
|
|
|
if (!def) return { x: 1, y: 1 };
|
|
|
|
const scale = this.GetIuScale();
|
|
return {
|
|
x: KiROUND(DoubleValueFromStringIn(scale, 'mm', def.x)),
|
|
y: KiROUND(DoubleValueFromStringIn(scale, 'mm', def.y)),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Return the nearest \a aGridSize location to \a aPosition.
|
|
*
|
|
* @param aPosition The position to check.
|
|
* @return The nearest grid position.
|
|
*/
|
|
GetNearestGridPosition(aPosition: VECTOR2I): VECTOR2I {
|
|
const gridOrigin = this.GetGridOrigin();
|
|
const gridSize = this.galGridSize();
|
|
|
|
const xOffset = gridOrigin.x % gridSize.x;
|
|
const x = KiROUND((aPosition.x - xOffset) / gridSize.x);
|
|
const yOffset = gridOrigin.y % gridSize.y;
|
|
const y = KiROUND((aPosition.y - yOffset) / gridSize.y);
|
|
|
|
return { x: KiROUND(x * gridSize.x + xOffset), y: KiROUND(y * gridSize.y + yOffset) };
|
|
}
|
|
|
|
/**
|
|
* Return the nearest \a aGridSize / 2 location to \a aPosition.
|
|
*/
|
|
GetNearestHalfGridPosition(aPosition: VECTOR2I): VECTOR2I {
|
|
const gridOrigin = this.GetGridOrigin();
|
|
const g = this.GetCanvas()!.GetGAL()!.GetGridSize();
|
|
const gridSize = { x: g.x / 2.0, y: g.y / 2.0 };
|
|
|
|
const xOffset = gridOrigin.x % gridSize.x;
|
|
const x = KiROUND((aPosition.x - xOffset) / gridSize.x);
|
|
const yOffset = gridOrigin.y % gridSize.y;
|
|
const y = KiROUND((aPosition.y - yOffset) / gridSize.y);
|
|
|
|
return { x: KiROUND(x * gridSize.x + xOffset), y: KiROUND(y * gridSize.y + yOffset) };
|
|
}
|
|
|
|
/** `ReCreateAuxiliaryToolbar()` (eda_draw_frame.h:222): nothing, unless a frame has one. */
|
|
ReCreateAuxiliaryToolbar(): void {}
|
|
|
|
/** `GetHotkeyPopup()` (eda_draw_frame.h:506). */
|
|
GetHotkeyPopup(): HOTKEY_CYCLE_POPUP_LIKE | null {
|
|
return this.m_hotkeyPopup;
|
|
}
|
|
|
|
/**
|
|
* `CreateHotkeyPopup()` (eda_draw_frame.cpp:1341-1347). The popup is a page
|
|
* widget the window renders, so the window creates it and hands it over with
|
|
* {@link SetHotkeyPopup}; a frame without a window has none.
|
|
*/
|
|
CreateHotkeyPopup(): void {}
|
|
|
|
/** The window's HOTKEY_CYCLE_POPUP, or null when it unmounts. */
|
|
SetHotkeyPopup(aPopup: HOTKEY_CYCLE_POPUP_LIKE | null): void {
|
|
this.m_hotkeyPopup = aPopup;
|
|
}
|
|
|
|
/**
|
|
* Rebuild the grid combobox to respond to any changes in the GUI (units, user
|
|
* grid changes, etc.).
|
|
*/
|
|
UpdateGridSelectBox(): void {
|
|
this.UpdateStatusBar();
|
|
this.DisplayUnitsMsg();
|
|
|
|
if (this.m_gridSelectBox === null) return;
|
|
|
|
// Update grid values with the current units setting.
|
|
this.m_gridSelectBox.Clear();
|
|
const gridsList: string[] = [];
|
|
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return;
|
|
|
|
GRID_MENU.BuildChoiceList(gridsList, this.GetWindowSettings(cfg), this);
|
|
|
|
for (const grid of gridsList) this.m_gridSelectBox.Append(grid);
|
|
|
|
this.m_gridSelectBox.Append('---');
|
|
this.m_gridSelectBox.Append('Edit Grids...');
|
|
|
|
this.m_gridSelectBox.SetSelection(this.GetWindowSettings(cfg).grid.last_size_idx);
|
|
}
|
|
|
|
/**
|
|
* Update the checked item in the grid wxchoice.
|
|
*/
|
|
OnUpdateSelectGrid(): void {
|
|
// No need to update the grid select box if it doesn't exist or the grid setting change
|
|
// was made using the select box.
|
|
if (this.m_gridSelectBox === null) return;
|
|
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return;
|
|
|
|
let idx = this.GetWindowSettings(cfg).grid.last_size_idx;
|
|
idx = Math.min(Math.max(idx, 0), this.m_gridSelectBox.GetCount() - 1);
|
|
|
|
if (idx !== this.m_gridSelectBox.GetSelection()) this.m_gridSelectBox.SetSelection(idx);
|
|
}
|
|
|
|
/**
|
|
* Command event handler for selecting grid sizes.
|
|
*
|
|
* All commands that set the grid size should eventually end up here.
|
|
* This is where the application setting is saved. If you override
|
|
* this method, make sure you call down to the base class.
|
|
*/
|
|
OnSelectGrid(): void {
|
|
if (!this.m_gridSelectBox) {
|
|
console.assert(false, 'm_gridSelectBox uninitialized');
|
|
return;
|
|
}
|
|
|
|
const idx = this.m_gridSelectBox.GetCurrentSelection();
|
|
|
|
if (idx === this.m_gridSelectBox.GetCount() - 2) {
|
|
// wxWidgets will check the separator, which we don't want.
|
|
// Re-check the current grid.
|
|
this.OnUpdateSelectGrid();
|
|
} else if (idx === this.m_gridSelectBox.GetCount() - 1) {
|
|
// wxWidgets will check the Grid Settings... entry, which we don't want.
|
|
// Re-check the current grid.
|
|
this.OnUpdateSelectGrid();
|
|
|
|
this.m_toolManager!.RunAction(ACTIONS.gridProperties);
|
|
} else {
|
|
this.m_toolManager!.RunAction(ACTIONS.gridPreset, idx);
|
|
}
|
|
|
|
this.UpdateStatusBar();
|
|
this.m_canvas?.Refresh();
|
|
|
|
// Needed on Windows because clicking on m_gridSelectBox remove the focus from m_canvas
|
|
// (Windows specific
|
|
this.m_canvas?.SetFocus();
|
|
}
|
|
|
|
/** The grid choice box, when the frame's toolbar has one. */
|
|
GetGridSelectBox(): wxChoice | null {
|
|
return this.m_gridSelectBox;
|
|
}
|
|
|
|
/** `ClearToolbarControl` / the grid control factory's `new wxChoice`. */
|
|
SetGridSelectBox(aBox: wxChoice | null): void {
|
|
this.m_gridSelectBox = aBox;
|
|
}
|
|
|
|
/**
|
|
* Construct "basic" menus for a tool: the Zoom and Grid submenus at the end
|
|
* of every draw frame's context menu.
|
|
*/
|
|
AddStandardSubMenus(aToolMenu: TOOL_MENU): void {
|
|
const commonTools = this.m_toolManager!.GetTool(COMMON_TOOLS)!;
|
|
const aMenu = aToolMenu.GetMenu();
|
|
|
|
aMenu.AddSeparator(1000);
|
|
|
|
const zoomMenu = new ZOOM_MENU(this);
|
|
zoomMenu.SetTool(commonTools);
|
|
aToolMenu.RegisterSubMenu(zoomMenu);
|
|
|
|
const gridMenu = new GRID_MENU(this);
|
|
gridMenu.SetTool(commonTools);
|
|
aToolMenu.RegisterSubMenu(gridMenu);
|
|
|
|
aMenu.AddMenu(zoomMenu, SELECTION_CONDITIONS.ShowAlways, 1000);
|
|
aMenu.AddMenu(gridMenu, SELECTION_CONDITIONS.ShowAlways, 1000);
|
|
}
|
|
|
|
// ---- zoom ---------------------------------------------------------------
|
|
|
|
/**
|
|
* Update the checked item in the zoom wxchoice.
|
|
*/
|
|
UpdateZoomSelectBox(): void {
|
|
if (this.m_zoomSelectBox === null) return;
|
|
|
|
const zoom = this.m_canvas!.GetGAL()!.GetZoomFactor();
|
|
|
|
this.m_zoomSelectBox.Clear();
|
|
this.m_zoomSelectBox.Append('Zoom Auto');
|
|
this.m_zoomSelectBox.SetSelection(0);
|
|
this.m_zoomCustomEntry = wxNOT_FOUND;
|
|
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return;
|
|
|
|
const zoomFactors = this.GetWindowSettings(cfg).zoom_factors;
|
|
|
|
for (let ii = 0; ii < zoomFactors.length; ++ii) {
|
|
const current = zoomFactors[ii]!;
|
|
|
|
this.m_zoomSelectBox.Append(`Zoom ${current.toFixed(2)}`);
|
|
|
|
if (zoom === current) this.m_zoomSelectBox.SetSelection(ii + 1);
|
|
}
|
|
}
|
|
|
|
OnUpdateSelectZoom(): void {
|
|
if (this.m_zoomSelectBox === null) return;
|
|
|
|
const zoom = this.GetCanvas()!.GetGAL()!.GetZoomFactor();
|
|
|
|
const cfg = this.config();
|
|
|
|
if (!cfg) return;
|
|
|
|
const zoomList = this.GetWindowSettings(cfg).zoom_factors;
|
|
let preset = wxNOT_FOUND;
|
|
|
|
for (let jj = 0; jj < zoomList.length; ++jj) {
|
|
if (zoomList[jj] === zoom) {
|
|
preset = jj + 1; // index 0 is Zoom Auto
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (preset !== wxNOT_FOUND) {
|
|
// Zoom is on a preset, so drop the custom entry and select the preset.
|
|
if (this.m_zoomCustomEntry !== wxNOT_FOUND) {
|
|
this.m_zoomSelectBox.Delete(this.m_zoomCustomEntry);
|
|
this.m_zoomCustomEntry = wxNOT_FOUND;
|
|
}
|
|
|
|
if (this.m_zoomSelectBox.GetSelection() !== preset) this.m_zoomSelectBox.SetSelection(preset);
|
|
} else {
|
|
// Off-preset zoom, show the exact value in its own entry just below Zoom Auto.
|
|
const text = `Zoom ${zoom.toFixed(2)}`;
|
|
|
|
if (this.m_zoomCustomEntry === wxNOT_FOUND)
|
|
this.m_zoomCustomEntry = this.m_zoomSelectBox.Insert(text, 1);
|
|
else if (this.m_zoomSelectBox.GetString(this.m_zoomCustomEntry) !== text)
|
|
this.m_zoomSelectBox.SetString(this.m_zoomCustomEntry, text);
|
|
|
|
if (this.m_zoomSelectBox.GetSelection() !== this.m_zoomCustomEntry)
|
|
this.m_zoomSelectBox.SetSelection(this.m_zoomCustomEntry);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the zoom factor when selected by the zoom list box in the main tool bar.
|
|
*
|
|
* @note List position 0 is fit to page.
|
|
* List position >= 1 = zoom (1 to zoom max).
|
|
* Last list position is custom zoom not in zoom list.
|
|
*/
|
|
OnSelectZoom(): void {
|
|
if (!this.m_zoomSelectBox) {
|
|
console.assert(false, 'm_zoomSelectBox uninitialized');
|
|
return;
|
|
}
|
|
|
|
const id = this.m_zoomSelectBox.GetCurrentSelection();
|
|
|
|
if (id < 0 || id >= this.m_zoomSelectBox.GetCount()) return;
|
|
|
|
// Picking the custom entry means keep the current zoom, so nothing to do.
|
|
if (id === this.m_zoomCustomEntry) return;
|
|
|
|
// The custom entry pushes the presets down by one, so shift back to the real preset id.
|
|
let preset = id;
|
|
|
|
if (this.m_zoomCustomEntry !== wxNOT_FOUND && id > this.m_zoomCustomEntry) preset = id - 1;
|
|
|
|
this.m_toolManager!.RunAction(ACTIONS.zoomPreset, preset);
|
|
this.UpdateStatusBar();
|
|
this.m_canvas?.Refresh();
|
|
|
|
// Needed on Windows (only) because clicking on m_zoomSelectBox removes the focus from m_canvas
|
|
this.m_canvas?.SetFocus();
|
|
}
|
|
|
|
GetZoomSelectBox(): wxChoice | null {
|
|
return this.m_zoomSelectBox;
|
|
}
|
|
|
|
SetZoomSelectBox(aBox: wxChoice | null): void {
|
|
this.m_zoomSelectBox = aBox;
|
|
this.m_zoomCustomEntry = wxNOT_FOUND;
|
|
}
|
|
|
|
/**
|
|
* Return a human readable value for display in dialogs.
|
|
*/
|
|
GetZoomLevelIndicator(): string {
|
|
// returns a human readable value which can be displayed as zoom
|
|
// level indicator in dialogs.
|
|
const zoom = this.m_canvas!.GetGAL()!.GetZoomFactor();
|
|
return `Z ${zoom.toFixed(2)}`;
|
|
}
|
|
|
|
/**
|
|
* Rebuild all toolbars and update the checked state of check tools.
|
|
*/
|
|
Zoom_Automatique(_aWarpPointer: boolean): void {
|
|
this.m_toolManager!.RunAction(ACTIONS.zoomFitScreen);
|
|
}
|
|
|
|
/**
|
|
* Returns bbox of document with option to not include some items.
|
|
*
|
|
* Used most commonly by "Zoom to Fit" and "Zoom to Objects". In Eeschema
|
|
* for "Zoom to Fit" it's the page and any items outside the page.
|
|
*/
|
|
GetDocumentExtents(_aIncludeAllVisible = true): BOX2I {
|
|
return new BOX2I();
|
|
}
|
|
|
|
/**
|
|
* Rebuild the GAL and redraws the screen. Call when something went wrong.
|
|
*/
|
|
HardRedraw(): void {
|
|
// To be implemented by subclasses.
|
|
}
|
|
|
|
// ---- units and the status bar -------------------------------------------
|
|
|
|
GetShowPolarCoords(): boolean {
|
|
return this.m_polarCoords;
|
|
}
|
|
|
|
SetShowPolarCoords(aShow: boolean): void {
|
|
this.m_polarCoords = aShow;
|
|
}
|
|
|
|
/** `EDA_BASE_FRAME::ChangeUserUnits`. */
|
|
ChangeUserUnits(aUnits: EdaUnits): void {
|
|
this.SetUserUnits(aUnits);
|
|
this.unitsChangeRefresh();
|
|
}
|
|
|
|
/**
|
|
* Called when when the units setting has changed to allow for any derived classes
|
|
* to handle refreshing and controls that have units based measurements in them.
|
|
*
|
|
* The toolbar's check state is `ACTION_MANAGER`'s conditions here, so the
|
|
* `SelectToolbarAction` calls have nothing to set.
|
|
*/
|
|
protected unitsChangeRefresh(): void {
|
|
// Notify all tools the units have changed
|
|
if (this.m_toolManager) this.m_toolManager.RunAction(ACTIONS.updateUnits);
|
|
|
|
this.UpdateStatusBar();
|
|
this.UpdateMsgPanel();
|
|
this.UpdateProperties();
|
|
}
|
|
|
|
ToggleUserUnits(): void {
|
|
const cmnTool = this.m_toolManager?.GetTool(COMMON_TOOLS) ?? null;
|
|
|
|
if (cmnTool) {
|
|
const dummy = new TOOL_EVENT();
|
|
cmnTool.ToggleUnits(dummy);
|
|
} else {
|
|
this.SetUserUnits(this.GetUserUnits() === 'in' ? 'mm' : 'in');
|
|
this.unitsChangeRefresh();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the pair or units in current use.
|
|
*
|
|
* The primary unit is the main unit of the frame, and the secondary unit is the unit
|
|
* of the other system that was used most recently.
|
|
*/
|
|
GetUnitPair(): { primary: EdaUnits; secondary: EdaUnits } {
|
|
const cmnTool = this.m_toolManager?.GetTool(COMMON_TOOLS) ?? null;
|
|
|
|
const primary = this.GetUserUnits();
|
|
let secondary: EdaUnits = 'mils';
|
|
|
|
if (IsImperialUnit(primary)) {
|
|
if (cmnTool) secondary = cmnTool.GetLastMetricUnits();
|
|
else secondary = 'mm';
|
|
} else {
|
|
if (cmnTool) secondary = cmnTool.GetLastImperialUnits();
|
|
else secondary = 'mils';
|
|
}
|
|
|
|
return { primary, secondary };
|
|
}
|
|
|
|
protected setupUnits(aCfg: APP_SETTINGS_BASE): void {
|
|
const cmnTool = this.m_toolManager!.GetTool(COMMON_TOOLS);
|
|
|
|
if (cmnTool) {
|
|
// Tell the tool what the units used last session
|
|
cmnTool.SetLastUnits(EdaUnitsFromInt(aCfg.m_System.last_imperial_units));
|
|
cmnTool.SetLastUnits(EdaUnitsFromInt(aCfg.m_System.last_metric_units));
|
|
}
|
|
|
|
// Tell the tool what units the frame is currently using
|
|
switch (EdaUnitsFromInt(aCfg.m_System.units)) {
|
|
default:
|
|
case 'mm':
|
|
this.m_toolManager!.RunAction(ACTIONS.millimetersUnits);
|
|
break;
|
|
case 'in':
|
|
this.m_toolManager!.RunAction(ACTIONS.inchesUnits);
|
|
break;
|
|
case 'mils':
|
|
this.m_toolManager!.RunAction(ACTIONS.milsUnits);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the status bar information.
|
|
*
|
|
* The EDA_DRAW_FRAME level updates the zoom and units display; derived
|
|
* frames add the cursor position and deltas.
|
|
*/
|
|
override UpdateStatusBar(): void {
|
|
if (this.m_isClosing) return;
|
|
|
|
// Upstream every draw frame has its canvas from construction. Here a
|
|
// frame whose canvas is not hosted on the GAL yet (the board editor's)
|
|
// has no zoom factor to report, and the field is the page's.
|
|
if (this.m_canvas?.GetGAL()) this.SetStatusText(this.GetZoomLevelIndicator(), 1);
|
|
|
|
// Absolute and relative cursor positions are handled by overloading this function and
|
|
// handling the internal to user units conversion at the appropriate level.
|
|
|
|
// refresh units display
|
|
this.DisplayUnitsMsg();
|
|
}
|
|
|
|
/**
|
|
* Send a message to the status bar field reserved for the current tool.
|
|
*/
|
|
override DisplayToolMsg(msg: string): void {
|
|
if (this.m_isClosing) return;
|
|
|
|
this.SetStatusText(msg, 6);
|
|
}
|
|
|
|
/**
|
|
* Send a message to the status bar field reserved for the constraints mode.
|
|
*/
|
|
DisplayConstraintsMsg(msg: string): void {
|
|
if (this.m_isClosing) return;
|
|
|
|
this.SetStatusText(msg, 7);
|
|
}
|
|
|
|
/**
|
|
* Display current grid size in the status bar.
|
|
*/
|
|
DisplayGridMsg(): void {
|
|
if (this.m_isClosing) return;
|
|
|
|
const cfg = this.m_toolManager?.GetSettings() as APP_SETTINGS_BASE | null;
|
|
|
|
if (!cfg) return;
|
|
|
|
const gridSettings = cfg.m_Window.grid;
|
|
const currentIdx = cfg.m_Window.grid.last_size_idx;
|
|
const grid = gridSettings.grids[currentIdx];
|
|
|
|
if (!grid) return;
|
|
|
|
const msg = `grid ${grid.UserUnitsMessageText(this, false)}`;
|
|
|
|
this.SetStatusText(msg, 4);
|
|
}
|
|
|
|
/**
|
|
* Display current unit pane in the status bar.
|
|
*/
|
|
DisplayUnitsMsg(): void {
|
|
if (this.m_isClosing) return;
|
|
|
|
let msg: string;
|
|
|
|
switch (this.GetUserUnits()) {
|
|
case 'in':
|
|
msg = 'inches';
|
|
break;
|
|
case 'mils':
|
|
msg = 'mils';
|
|
break;
|
|
case 'mm':
|
|
msg = 'mm';
|
|
break;
|
|
default:
|
|
msg = 'Units';
|
|
break;
|
|
}
|
|
|
|
this.SetStatusText(msg, 5);
|
|
}
|
|
|
|
// ---- settings -----------------------------------------------------------
|
|
|
|
/**
|
|
* Load settings related to the canvas and the frame's units.
|
|
*
|
|
* The find/replace history and the toolbar icon size are the page's.
|
|
*/
|
|
override LoadSettings(aCfg: APP_SETTINGS_BASE): void {
|
|
super.LoadSettings(aCfg);
|
|
|
|
const window = this.GetWindowSettings(aCfg);
|
|
|
|
// Read units used in dialogs and toolbars
|
|
this.SetUserUnits(EdaUnitsFromInt(aCfg.m_System.units));
|
|
|
|
this.m_undoRedoCountMax = aCfg.m_System.max_undo_items;
|
|
|
|
const cmnCfg = PgmOrNull()?.GetCommonSettings() ?? null;
|
|
|
|
// m_galDisplayOptions.ReadConfig( *cmnCfg, *window, this ): the antialiasing mode and DPI
|
|
// come from COMMON_SETTINGS, the grid and cursor from the frame's window settings.
|
|
if (cmnCfg)
|
|
this.m_galDisplayOptions.ReadConfig(
|
|
cmnCfg,
|
|
window,
|
|
typeof globalThis.window !== 'undefined' ? globalThis.window : null,
|
|
);
|
|
else this.m_galDisplayOptions.ReadWindowSettings(window);
|
|
}
|
|
|
|
override SaveSettings(aCfg: APP_SETTINGS_BASE): void {
|
|
super.SaveSettings(aCfg);
|
|
|
|
const window = this.GetWindowSettings(aCfg);
|
|
|
|
aCfg.m_System.units = EdaUnitsToInt(this.GetUserUnits());
|
|
aCfg.m_System.max_undo_items = this.GetMaxUndoItems();
|
|
|
|
this.m_galDisplayOptions.WriteConfig(window);
|
|
|
|
// Save the units used in this frame
|
|
if (this.m_toolManager) {
|
|
const cmnTool = this.m_toolManager.GetTool(COMMON_TOOLS);
|
|
|
|
if (cmnTool) {
|
|
aCfg.m_System.last_imperial_units = EdaUnitsToInt(cmnTool.GetLastImperialUnits());
|
|
aCfg.m_System.last_metric_units = EdaUnitsToInt(cmnTool.GetLastMetricUnits());
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- dialogs the tools open ---------------------------------------------
|
|
|
|
private m_pointEntryPresenter:
|
|
| ((
|
|
aTitle: string,
|
|
aLabelX: string,
|
|
aLabelY: string,
|
|
aValue: VECTOR2I,
|
|
aShowResetButton: boolean,
|
|
) => Promise<VECTOR2I | null>)
|
|
| null = null;
|
|
|
|
/**
|
|
* `WX_PT_ENTRY_DIALOG( this, title, labelX, labelY, value, showReset ).ShowModal()`,
|
|
* resolved with the entered point on OK and null on Cancel. The dialog is
|
|
* the page's (`common/dialogs/dialog_unit_entry.tsx`).
|
|
*/
|
|
ShowPointEntryDialog(
|
|
aTitle: string,
|
|
aLabelX: string,
|
|
aLabelY: string,
|
|
aValue: VECTOR2I,
|
|
aShowResetButton: boolean,
|
|
): Promise<VECTOR2I | null> {
|
|
if (!this.m_pointEntryPresenter) return Promise.resolve(null);
|
|
|
|
return this.m_pointEntryPresenter(aTitle, aLabelX, aLabelY, aValue, aShowResetButton);
|
|
}
|
|
|
|
SetPointEntryPresenter(
|
|
aPresenter:
|
|
| ((
|
|
aTitle: string,
|
|
aLabelX: string,
|
|
aLabelY: string,
|
|
aValue: VECTOR2I,
|
|
aShowResetButton: boolean,
|
|
) => Promise<VECTOR2I | null>)
|
|
| null,
|
|
): void {
|
|
this.m_pointEntryPresenter = aPresenter;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Print the frame references (drawing sheet) through the settings' print DC
|
|
* (`eda_draw_frame.cpp:1191-1216`).
|
|
*/
|
|
export function PrintDrawingSheet(
|
|
aSettings: RENDER_SETTINGS,
|
|
aPageInfo: PAGE_INFO,
|
|
aSheetName: string,
|
|
aSheetPath: string,
|
|
aFileName: string,
|
|
aTitleBlock: TITLE_BLOCK,
|
|
aProperties: Map<string, string> | null,
|
|
aSheetCount: number,
|
|
aPageNumber: string,
|
|
aMils2Iu: number,
|
|
aProject: PROJECT_TEXT_VARS | null,
|
|
aSheetLayer = '',
|
|
aIsFirstPage = true,
|
|
): void {
|
|
const drawList = new DS_DRAW_ITEM_LIST(unityScale);
|
|
|
|
drawList.SetDefaultPenSize(aSettings.GetDefaultPenWidth());
|
|
drawList.SetPlotterMilsToIUfactor(aMils2Iu);
|
|
drawList.SetPageNumber(aPageNumber);
|
|
drawList.SetSheetCount(aSheetCount);
|
|
drawList.SetFileName(aFileName);
|
|
drawList.SetSheetName(aSheetName);
|
|
drawList.SetSheetPath(aSheetPath);
|
|
drawList.SetSheetLayer(aSheetLayer);
|
|
if (aProject) drawList.SetProject(aProject);
|
|
drawList.SetIsFirstPage(aIsFirstPage);
|
|
drawList.SetProperties(aProperties);
|
|
|
|
drawList.BuildDrawItemsList(aPageInfo, aTitleBlock);
|
|
|
|
// Draw item list
|
|
drawList.Print(aSettings);
|
|
}
|