762 lines
25 KiB
TypeScript
762 lines
25 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_BASE_FRAME` (include/eda_base_frame.h, common/eda_base_frame.cpp): the
|
|
* base of every editor window — here the object the editor holds, not a
|
|
* window: its identity (`FRAME_T`), its TOOLS_HOLDER half, its undo and redo
|
|
* stacks and the modified flag. The wx window, the menu bar and the status
|
|
* bar are the designer's React frame, which subclasses this.
|
|
*
|
|
* `EDA_BASE_FRAME` is also a `UNITS_PROVIDER` in the C++; the provider is
|
|
* held as a member here and its two accessors forwarded, since a class has one
|
|
* base.
|
|
*/
|
|
import { DisplayErrorMessage } from './confirm.js';
|
|
import { wxDirExists, wxFileExists, wxIsDirWritable } from '@ziroeda/wx/filefn.js';
|
|
import type { MessageDialogIcon } from './confirm_types.js';
|
|
import type { KIWAY } from './kiway.js';
|
|
import type { EdaIuScale, EdaUnits } from './eda_units.js';
|
|
import { FRAME_T } from './frame_type.js';
|
|
import { Pgm, PgmOrNull } from './pgm_base.js';
|
|
import type { PROJECT } from './project.js';
|
|
import type { TOOL_MANAGER_FRAME_WITH_STATUS_BAR } from './tool/tool_manager.js';
|
|
import { TOOLS_HOLDER } from './tool/tools_holder.js';
|
|
import { type PICKED_ITEMS_LIST, UNDO_REDO_CONTAINER } from './undo_redo_container.js';
|
|
import { UNITS_PROVIDER } from './units_provider.js';
|
|
import { RPT_SEVERITY_UNDEFINED, type Severity } from './reporter.js';
|
|
import type { TOOL_ACTION } from './tool/tool_action.js';
|
|
import type { ACTION_CONDITIONS } from './tool/action_manager.js';
|
|
import { ACTIONS } from './tool/actions.js';
|
|
import type { wxUpdateUIEvent } from '@ziroeda/wx/wx_event.js';
|
|
import type { ACTION_MENU } from './tool/action_menu.js';
|
|
import type { APP_SETTINGS_BASE, WINDOW_SETTINGS } from './settings/app_settings.js';
|
|
|
|
export const DEFAULT_MAX_UNDO_ITEMS = 0;
|
|
export const ABS_MAX_UNDO_ITEMS = 65536;
|
|
|
|
/**
|
|
* Specify whether we are interacting with the undo or redo stacks.
|
|
*/
|
|
export enum UNDO_REDO_LIST {
|
|
UNDO_LIST,
|
|
REDO_LIST,
|
|
}
|
|
export const { UNDO_LIST, REDO_LIST } = UNDO_REDO_LIST;
|
|
|
|
/** A `wxHyperlinkCtrl` added to the infobar: its label and its `wxEVT_COMMAND_HYPERLINK`. */
|
|
export interface WX_INFOBAR_HYPERLINK {
|
|
label: string;
|
|
onClick(): void;
|
|
}
|
|
|
|
/**
|
|
* `WX_INFOBAR` (include/widgets/wx_infobar.h), as the frames, their tools and
|
|
* the draw panel use it. The bar is a page widget; the window that renders it
|
|
* installs it on the frame with `SetInfoBar`.
|
|
*/
|
|
export interface WX_INFOBAR {
|
|
/** Whether the bar is locked open (the canvas keeps its bottom edge fixed under it). */
|
|
IsLocked(): boolean;
|
|
/** `AddButton( wxHyperlinkCtrl* )` (wx_infobar.h:127). */
|
|
AddButton(aHypertextButton: WX_INFOBAR_HYPERLINK): void;
|
|
/**
|
|
* `AddCloseButton( aTooltip = _( "Hide this message." ) )` (wx_infobar.cpp:375). A bar that
|
|
* draws no close button leaves it out.
|
|
*/
|
|
AddCloseButton?(aTooltip?: string): void;
|
|
/** `RemoveAllButtons()` (wx_infobar.h:141). */
|
|
RemoveAllButtons(): void;
|
|
/** `ShowMessage( aMessage, aFlags )` (wx_infobar.h:157): shown until dismissed or replaced. */
|
|
ShowMessage(aMessage: string, aFlags?: MessageDialogIcon): void;
|
|
/** `ShowMessageFor( aMessage, aTime, aFlags )` (wx_infobar.h:165): shown, then dismissed after aTime ms. */
|
|
ShowMessageFor(aMessage: string, aTime: number, aFlags?: MessageDialogIcon): void;
|
|
/** `Dismiss()` (wx_infobar.h:189). */
|
|
Dismiss(): void;
|
|
}
|
|
|
|
/** `wxMenuBar` as `GetRunMenuCommandDescription` reads it. */
|
|
export interface MENU_BAR_LIKE {
|
|
GetMenuCount(): number;
|
|
GetMenuLabelText(aIndex: number): string;
|
|
GetMenuItems(aIndex: number): readonly { GetItemLabelText(): string }[];
|
|
}
|
|
|
|
export abstract class EDA_BASE_FRAME
|
|
extends TOOLS_HOLDER
|
|
implements TOOL_MANAGER_FRAME_WITH_STATUS_BAR
|
|
{
|
|
protected m_ident: FRAME_T; // Id Type (pcb, schematic, library..)
|
|
protected m_unitsProvider: UNITS_PROVIDER;
|
|
|
|
protected m_autoSaveRequired = false; // true to trigger an auto save on the next update
|
|
|
|
protected m_undoRedoCountMax: number; // undo/Redo command Max depth
|
|
|
|
protected m_undoList = new UNDO_REDO_CONTAINER(); // Objects list for the undo command (old data)
|
|
protected m_redoList = new UNDO_REDO_CONTAINER(); // Objects list for the redo command (old data)
|
|
|
|
protected m_isClosing = false; // Set by the close window event handler after frames are asked if they can close
|
|
// Allows other functions when called to know our state is cleanup
|
|
|
|
protected m_infoBar: WX_INFOBAR | null = null; // Infobar for the frame
|
|
|
|
/**
|
|
* `KIWAY_HOLDER::Prj()`: the active project, through `KIWAY::Prj()` which is
|
|
* `Pgm().GetSettingsManager().Prj()`. KIWAY_HOLDER is not a class here; a
|
|
* frame is the only holder ported.
|
|
*/
|
|
Prj(): PROJECT {
|
|
return Pgm().GetSettingsManager().Prj();
|
|
}
|
|
|
|
protected m_mruPath = ''; // Most recently used path.
|
|
|
|
SetMruPath(aPath: string): void {
|
|
this.m_mruPath = aPath;
|
|
}
|
|
|
|
GetMruPath(): string {
|
|
return this.m_mruPath;
|
|
}
|
|
|
|
constructor(aFrameType: FRAME_T, aIuScale: EdaIuScale, aUnits: EdaUnits) {
|
|
super();
|
|
|
|
this.m_ident = aFrameType;
|
|
this.m_unitsProvider = new UNITS_PROVIDER(aIuScale, aUnits);
|
|
this.m_undoRedoCountMax = DEFAULT_MAX_UNDO_ITEMS;
|
|
}
|
|
|
|
IsType(aType: FRAME_T): boolean {
|
|
return this.m_ident === aType;
|
|
}
|
|
|
|
/** `WX_INFOBAR* GetInfoBar()`: the frame's infobar; the designer's frames hold one when shown. */
|
|
GetInfoBar(): WX_INFOBAR | null {
|
|
return this.m_infoBar;
|
|
}
|
|
|
|
/**
|
|
* `SelectToolbarAction( aAction )` (eda_base_frame.cpp): each toolbar shows
|
|
* `aAction` as its group's current entry. The toolbars are the window's,
|
|
* which installs the sink.
|
|
*/
|
|
SelectToolbarAction(aAction: TOOL_ACTION): void {
|
|
this.m_selectToolbarActionSink?.(aAction);
|
|
}
|
|
|
|
private m_selectToolbarActionSink: ((aAction: TOOL_ACTION) => void) | null = null;
|
|
|
|
/** The window's ACTION_TOOLBAR::SelectAction, for {@link SelectToolbarAction}. */
|
|
SetSelectToolbarActionSink(aSink: ((aAction: TOOL_ACTION) => void) | null): void {
|
|
this.m_selectToolbarActionSink = aSink;
|
|
}
|
|
|
|
/** The window's WX_INFOBAR, which it renders; null when it unmounts. */
|
|
SetInfoBar(aInfoBar: WX_INFOBAR | null): void {
|
|
this.m_infoBar = aInfoBar;
|
|
}
|
|
|
|
/**
|
|
* `ShowInfoBarWarning( aWarningMsg, aShowCloseButton )` (eda_base_frame.cpp:1451): the
|
|
* infobar, warning icon, 6 s. The infobar is the window's; a frame with no window shows
|
|
* nothing, and a frame with one overrides this.
|
|
*/
|
|
ShowInfoBarWarning(_aWarningMsg: string, _aShowCloseButton = false): void {}
|
|
|
|
/**
|
|
* `ShowInfoBarError( aErrorMsg, aShowCloseButton )` (eda_base_frame.cpp:1424): an error in the
|
|
* infobar for 8 s. The close button is the bar's own here (WX_INFOBAR has no AddCloseButton).
|
|
*/
|
|
ShowInfoBarError(aErrorMsg: string, _aShowCloseButton = false): void {
|
|
const infoBar = this.GetInfoBar();
|
|
|
|
if (!infoBar) return;
|
|
|
|
infoBar.RemoveAllButtons();
|
|
infoBar.ShowMessageFor(aErrorMsg, 8000, 'error');
|
|
}
|
|
|
|
/**
|
|
* `ShowInfoBarMsg` (eda_base_frame.cpp:1462): an information message in the infobar for 8 s.
|
|
* The close button is the bar's own here (WX_INFOBAR has no AddCloseButton).
|
|
*/
|
|
ShowInfoBarMsg(aMsg: string, _aShowCloseButton = false): void {
|
|
const infoBar = this.GetInfoBar();
|
|
|
|
if (!infoBar) return;
|
|
|
|
infoBar.RemoveAllButtons();
|
|
infoBar.ShowMessageFor(aMsg, 8000, 'information');
|
|
}
|
|
|
|
GetFrameType(): FRAME_T {
|
|
return this.m_ident;
|
|
}
|
|
|
|
/* UNITS_PROVIDER, the C++'s second base */
|
|
GetUserUnits(): EdaUnits {
|
|
return this.m_unitsProvider.GetUserUnits();
|
|
}
|
|
|
|
SetUserUnits(aUnits: EdaUnits): void {
|
|
this.m_unitsProvider.SetUserUnits(aUnits);
|
|
}
|
|
|
|
GetIuScale(): EdaIuScale {
|
|
return this.m_unitsProvider.GetIuScale();
|
|
}
|
|
|
|
GetUnitsProvider(): UNITS_PROVIDER {
|
|
return this.m_unitsProvider;
|
|
}
|
|
|
|
GetSeverity(_aErrorCode: number): Severity {
|
|
return RPT_SEVERITY_UNDEFINED;
|
|
}
|
|
|
|
/** `wxMenuBar* GetMenuBar()`: the window's menu bar as a label model, or null. */
|
|
GetMenuBar(): MENU_BAR_LIKE | null {
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get the description of the menu command for the given action, to be used in a tooltip or
|
|
* message box, e.g. "Run: File > Import > Netlist"
|
|
*/
|
|
GetRunMenuCommandDescription(aAction: TOOL_ACTION): string {
|
|
let menuItemLabel = aAction.GetMenuLabel();
|
|
const menuBar = this.GetMenuBar();
|
|
|
|
if (menuBar) {
|
|
for (let ii = 0; ii < menuBar.GetMenuCount(); ++ii) {
|
|
for (const menuItem of menuBar.GetMenuItems(ii)) {
|
|
if (menuItem.GetItemLabelText() === menuItemLabel) {
|
|
let menuTitleLabel = menuBar.GetMenuLabelText(ii);
|
|
|
|
menuTitleLabel = menuTitleLabel.replaceAll('&', '&&');
|
|
menuItemLabel = menuItemLabel.replaceAll('&', '&&');
|
|
|
|
return `Run: ${menuTitleLabel} > ${menuItemLabel}`;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return `Run: ${aAction.GetFriendlyName()}`;
|
|
}
|
|
|
|
/**
|
|
* Remove the \a aItemCount of old commands from \a aList and delete commands, pickers
|
|
* and picked items if needed.
|
|
*
|
|
* Because picked items must be deleted only if they are not in use, this is a virtual
|
|
* pure function that must be created for #SCH_SCREEN and #PCB_SCREEN.
|
|
*
|
|
* @param aList which #UNDO_REDO_CONTAINER of commands.
|
|
* @param aItemCount number of old commands to delete. -1 to remove all old commands
|
|
* this will empty the list of commands.
|
|
*/
|
|
ClearUndoORRedoList(_aList: UNDO_REDO_LIST, _aItemCount = -1): void {}
|
|
|
|
/**
|
|
* Clear the undo and redo list using #ClearUndoORRedoList()
|
|
*
|
|
* Picked items are deleted by ClearUndoORRedoList() according to their status.
|
|
*/
|
|
ClearUndoRedoList(): void {
|
|
this.ClearUndoORRedoList(UNDO_REDO_LIST.UNDO_LIST);
|
|
this.ClearUndoORRedoList(UNDO_REDO_LIST.REDO_LIST);
|
|
}
|
|
|
|
/**
|
|
* Add a command to undo in the undo list.
|
|
*
|
|
* Delete the very old commands when the max count of undo commands is reached.
|
|
*/
|
|
PushCommandToUndoList(aNewitem: PICKED_ITEMS_LIST): void {
|
|
this.m_undoList.PushCommand(aNewitem);
|
|
|
|
// Delete the extra items, if count max reached
|
|
if (this.m_undoRedoCountMax > 0) {
|
|
const extraitems = this.GetUndoCommandCount() - this.m_undoRedoCountMax;
|
|
|
|
if (extraitems > 0) this.ClearUndoORRedoList(UNDO_REDO_LIST.UNDO_LIST, extraitems);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add a command to redo in the redo list.
|
|
*
|
|
* Delete the very old commands when the max count of redo commands is reached.
|
|
*/
|
|
PushCommandToRedoList(aNewitem: PICKED_ITEMS_LIST): void {
|
|
this.m_redoList.PushCommand(aNewitem);
|
|
|
|
// Delete the extra items, if count max reached
|
|
if (this.m_undoRedoCountMax > 0) {
|
|
const extraitems = this.GetRedoCommandCount() - this.m_undoRedoCountMax;
|
|
|
|
if (extraitems > 0) this.ClearUndoORRedoList(UNDO_REDO_LIST.REDO_LIST, extraitems);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return the last command to undo and remove it from list, nothing is deleted.
|
|
*/
|
|
PopCommandFromUndoList(): PICKED_ITEMS_LIST | null {
|
|
return this.m_undoList.PopCommand();
|
|
}
|
|
|
|
/**
|
|
* Return the last command to undo and remove it from list, nothing is deleted.
|
|
*/
|
|
PopCommandFromRedoList(): PICKED_ITEMS_LIST | null {
|
|
return this.m_redoList.PopCommand();
|
|
}
|
|
|
|
GetUndoCommandCount(): number {
|
|
return this.m_undoList.m_CommandsList.length;
|
|
}
|
|
GetRedoCommandCount(): number {
|
|
return this.m_redoList.m_CommandsList.length;
|
|
}
|
|
|
|
/**
|
|
* Get if the contents of the frame have been modified since the last save.
|
|
*
|
|
* @return true if the contents of the frame have not been saved
|
|
*/
|
|
IsContentModified(): boolean {
|
|
// This function should be overridden in child classes
|
|
return false;
|
|
}
|
|
|
|
GetUndoActionDescription(): string {
|
|
if (this.GetUndoCommandCount() > 0)
|
|
return this.m_undoList.m_CommandsList[
|
|
this.m_undoList.m_CommandsList.length - 1
|
|
]!.GetDescription();
|
|
|
|
return '';
|
|
}
|
|
|
|
GetRedoActionDescription(): string {
|
|
if (this.GetRedoCommandCount() > 0)
|
|
return this.m_redoList.m_CommandsList[
|
|
this.m_redoList.m_CommandsList.length - 1
|
|
]!.GetDescription();
|
|
|
|
return '';
|
|
}
|
|
|
|
GetMaxUndoItems(): number {
|
|
return this.m_undoRedoCountMax;
|
|
}
|
|
|
|
/**
|
|
* Must be called after a model change in order to set the "modify" flag and
|
|
* do other frame-specific processing.
|
|
*/
|
|
OnModify(): void {
|
|
this.m_autoSaveRequired = true;
|
|
}
|
|
|
|
IsAutoSaveRequired(): boolean {
|
|
return this.m_autoSaveRequired;
|
|
}
|
|
|
|
ClearAutoSaveRequired(): void {
|
|
this.m_autoSaveRequired = false;
|
|
}
|
|
|
|
IsClosing(): boolean {
|
|
return this.m_isClosing;
|
|
}
|
|
|
|
/**
|
|
* Update the status bar information.
|
|
*
|
|
* The status bar can draw itself. This is not a drawing function per se, but rather
|
|
* updates lines of text held by the components within the status bar which is owned
|
|
* by the wxFrame.
|
|
*/
|
|
UpdateStatusBar(): void {}
|
|
|
|
GetToolCanvas(): unknown {
|
|
return null;
|
|
}
|
|
|
|
// ---- settings -----------------------------------------------------------
|
|
|
|
/**
|
|
* Return the settings object used in SaveSettings(), and is overloaded in
|
|
* KICAD_MANAGER_FRAME. Upstream it is `Kiface().KifaceSettings()`: the
|
|
* settings of the kiface the frame belongs to, which each frame's package
|
|
* supplies by overriding this.
|
|
*/
|
|
config(): APP_SETTINGS_BASE | null {
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Return a pointer to the window settings for this frame.
|
|
*
|
|
* By default, points to aCfg->m_Window for top-level frames.
|
|
*/
|
|
GetWindowSettings(aCfg: APP_SETTINGS_BASE): WINDOW_SETTINGS {
|
|
return aCfg.m_Window;
|
|
}
|
|
|
|
/**
|
|
* Load common frame parameters from a configuration file.
|
|
*
|
|
* The file history is the page's own (FILE_HISTORY has no model here).
|
|
*/
|
|
LoadSettings(aCfg: APP_SETTINGS_BASE): void {
|
|
this.LoadWindowSettings(this.GetWindowSettings(aCfg));
|
|
}
|
|
|
|
/**
|
|
* `LoadWindowSettings( aCfg )` (eda_base_frame.cpp:1215). The window state and AUI layout are
|
|
* the page's; the MRU path, and the input settings TOOLS_HOLDER::CommonSettingsChanged copies
|
|
* (the left-drag action, warp-on-move, immediate actions), are the frame's. Without that last
|
|
* call a frame keeps TOOLS_HOLDER's constructor drag action, SELECT, and every left-drag draws
|
|
* a selection box.
|
|
*/
|
|
LoadWindowSettings(aCfg: WINDOW_SETTINGS): void {
|
|
this.m_mruPath = aCfg.mru_path;
|
|
|
|
TOOLS_HOLDER.prototype.CommonSettingsChanged.call(this);
|
|
}
|
|
|
|
/**
|
|
* `m_acceptedExts`: the file extensions a drop on this frame takes, each with the action run for
|
|
* such a file (`ACTIONS::ddAddLibrary` for a symbol library, ...). A frame fills it in its
|
|
* constructor.
|
|
*/
|
|
protected m_acceptedExts = new Map<string, TOOL_ACTION>();
|
|
/** `m_AcceptedFiles`: the dropped files the frame takes, until DoWithAcceptedFiles runs them. */
|
|
protected m_AcceptedFiles: string[] = [];
|
|
|
|
/**
|
|
* `OnDropFiles( aEvent )` (eda_base_frame.cpp:1789-1810): of the files dropped, the ones whose
|
|
* extension the frame accepts, each run through its action. (Gerber extensions alias to one; no
|
|
* frame here accepts Gerbers yet.)
|
|
*/
|
|
OnDropFiles(aFiles: readonly string[]): void {
|
|
for (const file of aFiles) {
|
|
if (this.IsDropAccepted(file)) this.m_AcceptedFiles.push(file);
|
|
}
|
|
|
|
this.DoWithAcceptedFiles();
|
|
this.m_AcceptedFiles = [];
|
|
}
|
|
|
|
/**
|
|
* OnDropFiles' test of one file: its extension is one the frame accepts. The window asks it
|
|
* before it puts a dropped file's bytes anywhere.
|
|
*/
|
|
IsDropAccepted(aFileName: string): boolean {
|
|
const leaf = aFileName.slice(aFileName.lastIndexOf('/') + 1);
|
|
const dot = leaf.lastIndexOf('.');
|
|
|
|
return this.m_acceptedExts.has(dot > 0 ? leaf.slice(dot + 1) : '');
|
|
}
|
|
|
|
/** `DoWithAcceptedFiles()` (:1813-1820): each accepted file's action, with the file as parameter. */
|
|
protected DoWithAcceptedFiles(): void {
|
|
for (const file of this.m_AcceptedFiles) {
|
|
const leaf = file.slice(file.lastIndexOf('/') + 1);
|
|
|
|
// `m_acceptedExts.at( ext )`: OnDropFiles only lets an accepted extension through.
|
|
this.m_toolManager?.RunAction(
|
|
this.m_acceptedExts.get(leaf.slice(leaf.lastIndexOf('.') + 1))!,
|
|
file,
|
|
);
|
|
}
|
|
}
|
|
|
|
/** `GetWindowSize()`: the page's measure of the frame, null while it has none. */
|
|
private m_windowSize: (() => { x: number; y: number } | null) | null = null;
|
|
|
|
/** The page tells the frame how to measure itself. */
|
|
SetWindowSizeSource(aSource: (() => { x: number; y: number } | null) | null): void {
|
|
this.m_windowSize = aSource;
|
|
}
|
|
|
|
/**
|
|
* `SaveWindowSettings( aCfg )` (eda_base_frame.cpp:1229): the frame's size, which a frame's
|
|
* LoadSettings measures its panes against. The position, the maximised state, the display and
|
|
* the AUI layout are the browser's to keep, not the frame's.
|
|
*/
|
|
SaveWindowSettings(aCfg: WINDOW_SETTINGS): void {
|
|
const size = this.m_windowSize?.() ?? null;
|
|
|
|
if (!size) return;
|
|
|
|
aCfg.state.size_x = size.x;
|
|
aCfg.state.size_y = size.y;
|
|
}
|
|
|
|
/**
|
|
* Save common frame parameters to a configuration data file: `SaveSettings( aCfg )`
|
|
* (eda_base_frame.cpp:1296). The file history is the page's own.
|
|
*/
|
|
SaveSettings(aCfg: APP_SETTINGS_BASE): void {
|
|
this.SaveWindowSettings(this.GetWindowSettings(aCfg));
|
|
}
|
|
|
|
// ---- wxFrame's status bar ---------------------------------------------
|
|
|
|
/** The fields `SetStatusText` writes, by index. */
|
|
private m_statusText: string[] = [];
|
|
private m_statusSink: ((aText: string, aField: number) => void) | null = null;
|
|
|
|
/**
|
|
* `wxFrame::SetStatusText( text, number )`. The page's status bar installs
|
|
* a sink with {@link SetStatusTextSink} and writes the field itself, as a
|
|
* wxStatusBar repaints only the field that changed.
|
|
*/
|
|
/**
|
|
* `IsWritable( aFileName, aVerbose )` (eda_base_frame.cpp:1823): whether \a aFileName can be
|
|
* written, on the mounted file system (common/wx/filefn.ts) - a path no writable mount covers is
|
|
* a folder without permission.
|
|
*/
|
|
IsWritable(aFileName: string, aVerbose = true): boolean {
|
|
let msg = '';
|
|
const dir = aFileName.includes('/')
|
|
? aFileName.slice(0, aFileName.lastIndexOf('/')) || '/'
|
|
: '';
|
|
|
|
if (dir === '') return false; // wxCHECK: "File name object path is not set. Bad programmer!"
|
|
|
|
// `fn.IsDir()`: the name has no file part (wxFileName's notion, not the file system's).
|
|
const isDir = aFileName.endsWith('/');
|
|
|
|
if (isDir && !wxIsDirWritable(dir)) msg = `Insufficient permissions to folder '${dir}'.`;
|
|
else if (!wxFileExists(aFileName) && !wxIsDirWritable(dir))
|
|
msg = `Insufficient permissions to save file '${aFileName}'.`;
|
|
else if (wxFileExists(aFileName) && !wxIsDirWritable(dir))
|
|
msg = `Insufficient permissions to save file '${aFileName}'.`;
|
|
|
|
if (msg !== '') {
|
|
if (aVerbose) DisplayErrorMessage(msg);
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
SetStatusText(aText: string, aNumber = 0): void {
|
|
if (this.m_statusText[aNumber] === aText) return;
|
|
|
|
this.m_statusText[aNumber] = aText;
|
|
this.m_statusSink?.(aText, aNumber);
|
|
}
|
|
|
|
/** `wxStatusBar::GetStatusText( number )`. */
|
|
GetStatusText(aNumber = 0): string {
|
|
return this.m_statusText[aNumber] ?? '';
|
|
}
|
|
|
|
/** The page's half of the status bar: where a changed field goes. */
|
|
SetStatusTextSink(aSink: ((aText: string, aField: number) => void) | null): void {
|
|
this.m_statusSink = aSink;
|
|
}
|
|
|
|
// ---- wxEvtHandler / window services ----------------------------------
|
|
|
|
/**
|
|
* `wxEvtHandler::CallAfter( fn )`: run `fn` once the current event has
|
|
* been handled - a queued event, which a macrotask is. An application that has exited
|
|
* dispatches nothing more: a call queued under a program object that is gone by then is
|
|
* dropped (one queued with none runs, as it always did).
|
|
*/
|
|
CallAfter(aFn: () => void): void {
|
|
const pgm = PgmOrNull();
|
|
|
|
setTimeout(() => {
|
|
if (pgm !== null && PgmOrNull() !== pgm) return;
|
|
|
|
aFn();
|
|
}, 0);
|
|
}
|
|
|
|
private m_popupMenuPresenter: ((aMenu: ACTION_MENU, aOnClose: () => void) => void) | null = null;
|
|
|
|
/**
|
|
* `wxWindow::PopupMenu( menu, pos )`: show a context menu at the pointer.
|
|
* Upstream it blocks until the menu closes; here `aOnClose` runs then. The
|
|
* popup is the page's (`common/tool/action_menu_popup.tsx`); a frame with
|
|
* none closes the menu at once, unselected.
|
|
*/
|
|
PopupMenu(aMenu: ACTION_MENU, aOnClose: () => void): void {
|
|
if (this.m_popupMenuPresenter) this.m_popupMenuPresenter(aMenu, aOnClose);
|
|
else aOnClose();
|
|
}
|
|
|
|
SetPopupMenuPresenter(
|
|
aPresenter: ((aMenu: ACTION_MENU, aOnClose: () => void) => void) | null,
|
|
): void {
|
|
this.m_popupMenuPresenter = aPresenter;
|
|
}
|
|
|
|
private m_preferencesPresenter: ((aStartPage: string, aStartParentPage: string) => void) | null =
|
|
null;
|
|
|
|
/**
|
|
* `EDA_BASE_FRAME::ShowPreferences`: the Preferences dialog, opened at a
|
|
* page. The dialog is the page's (`prefs/PreferencesDialog.tsx`), installed
|
|
* with {@link SetPreferencesPresenter}.
|
|
*/
|
|
// ---- UI conditions -------------------------------------------------------
|
|
|
|
/** `m_uiUpdateMap`: the conditions each control id is updated from. */
|
|
private m_uiUpdateMap = new Map<number, ACTION_CONDITIONS>();
|
|
|
|
/** `RegisterUIUpdateHandler( int aID, ... )` (`eda_base_frame.cpp:602-617`). */
|
|
protected override registerUIUpdateHandler(aID: number, aConditions: ACTION_CONDITIONS): void {
|
|
this.m_uiUpdateMap.set(aID, aConditions);
|
|
}
|
|
|
|
protected override unregisterUIUpdateHandler(aID: number): void {
|
|
this.m_uiUpdateMap.delete(aID);
|
|
}
|
|
|
|
/**
|
|
* `onUpdateUI`: `wxEVT_UPDATE_UI` for the control `aEvent.GetId()`. wx
|
|
* sends one before it draws a control; the page asks here when it renders
|
|
* its toolbars and menus. Skipped when no handler is registered.
|
|
*/
|
|
ProcessUpdateUI(aEvent: wxUpdateUIEvent): boolean {
|
|
const cond = this.m_uiUpdateMap.get(aEvent.GetId());
|
|
|
|
if (!cond) {
|
|
aEvent.Skip();
|
|
return false;
|
|
}
|
|
|
|
EDA_BASE_FRAME.HandleUpdateUIEvent(aEvent, this, cond);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Handle events generated when the UI is trying to figure out the current
|
|
* state of the UI controls related to TOOL_ACTIONs (`:638-702`).
|
|
*/
|
|
static HandleUpdateUIEvent(
|
|
aEvent: wxUpdateUIEvent,
|
|
aFrame: EDA_BASE_FRAME,
|
|
aCond: ACTION_CONDITIONS,
|
|
): void {
|
|
let checkRes = false;
|
|
let enableRes = true;
|
|
let showRes = true;
|
|
const isCut = aEvent.GetId() === ACTIONS.cut.GetUIId();
|
|
const isCopy = aEvent.GetId() === ACTIONS.copy.GetUIId();
|
|
const isPaste = aEvent.GetId() === ACTIONS.paste.GetUIId();
|
|
const selection = aFrame.GetCurrentSelection();
|
|
|
|
try {
|
|
checkRes = aCond.checkCondition(selection);
|
|
enableRes = aCond.enableCondition(selection);
|
|
showRes = aCond.showCondition(selection);
|
|
} catch {
|
|
// Something broke with the conditions, just skip the event.
|
|
aEvent.Skip();
|
|
return;
|
|
}
|
|
|
|
if (showRes && aEvent.GetId() === ACTIONS.undo.GetUIId()) {
|
|
let msg = 'Undo';
|
|
|
|
if (enableRes) msg += ` ${aFrame.GetUndoActionDescription()}`;
|
|
|
|
aEvent.SetText(msg);
|
|
} else if (showRes && aEvent.GetId() === ACTIONS.redo.GetUIId()) {
|
|
let msg = 'Redo';
|
|
|
|
if (enableRes) msg += ` ${aFrame.GetRedoActionDescription()}`;
|
|
|
|
aEvent.SetText(msg);
|
|
}
|
|
|
|
if (isCut || isCopy || isPaste) {
|
|
// wxWindow::FindFocus() as a wxTextEntry: a focused text field.
|
|
const focus = typeof document === 'undefined' ? null : document.activeElement;
|
|
const textEntry =
|
|
focus !== null &&
|
|
(focus instanceof HTMLInputElement || focus instanceof HTMLTextAreaElement)
|
|
? focus
|
|
: null;
|
|
const hasSelection =
|
|
textEntry !== null && (textEntry.selectionStart ?? 0) !== (textEntry.selectionEnd ?? 0);
|
|
const editable = textEntry !== null && !textEntry.readOnly && !textEntry.disabled;
|
|
|
|
if (textEntry && isCut && hasSelection && editable) enableRes = true;
|
|
else if (textEntry && isCopy && hasSelection) enableRes = true;
|
|
else if (textEntry && isPaste && editable) enableRes = true;
|
|
}
|
|
|
|
aEvent.Enable(enableRes);
|
|
aEvent.Show(showRes);
|
|
|
|
if (aEvent.IsCheckable()) aEvent.Check(checkRes);
|
|
}
|
|
|
|
/**
|
|
* Set up the UI conditions for the various actions and their controls in
|
|
* this frame (`:704-718`): the language menu's checks, which a browser
|
|
* build with one language has no rows for.
|
|
*/
|
|
protected setupUIConditions(): void {}
|
|
|
|
private m_kiway: KIWAY | null = null;
|
|
|
|
/** `KIWAY_HOLDER::SetKiway`: the program's services, {@link KIWAY}. */
|
|
SetKiway(aKiway: KIWAY | null): void {
|
|
this.m_kiway = aKiway;
|
|
}
|
|
|
|
/** `KIWAY_HOLDER::Kiway()`: null for a frame no program has adopted (a test's). */
|
|
Kiway(): KIWAY | null {
|
|
return this.m_kiway;
|
|
}
|
|
|
|
/**
|
|
* `m_aboutTitle`: the application name ShowAboutDialog( this ) titles the
|
|
* About dialog with. Each frame's constructor sets it.
|
|
*/
|
|
m_aboutTitle = '';
|
|
|
|
private m_aboutPresenter: ((aTitle: string) => void) | null = null;
|
|
|
|
/**
|
|
* `ShowAboutDialog( EDA_BASE_FRAME* aParent )` (AboutDialog_main.cpp) for
|
|
* this frame. The dialog is the page's, installed with
|
|
* {@link SetAboutPresenter}, as the Preferences dialog is.
|
|
*/
|
|
ShowAboutDialog(): void {
|
|
this.m_aboutPresenter?.(this.m_aboutTitle);
|
|
}
|
|
|
|
SetAboutPresenter(aPresenter: ((aTitle: string) => void) | null): void {
|
|
this.m_aboutPresenter = aPresenter;
|
|
}
|
|
|
|
ShowPreferences(aStartPage: string, aStartParentPage: string): void {
|
|
this.m_preferencesPresenter?.(aStartPage, aStartParentPage);
|
|
}
|
|
|
|
SetPreferencesPresenter(
|
|
aPresenter: ((aStartPage: string, aStartParentPage: string) => void) | null,
|
|
): void {
|
|
this.m_preferencesPresenter = aPresenter;
|
|
}
|
|
}
|
|
|
|
export { FRAME_T };
|