unit MainFrm;

{******************************************************************************}
{                                                                              }
{                StarUML - The Open Source UML/MDA Platform.                   }
{                                                                              }
{              Copyright (C) 2002-2005 - Plastic Software, Inc.                }
{                                                                              }
{                                                                              }
{ This program is free software; you can redistribute it and/or modify it      }
{ under the terms of the GNU General Public License as published by the Free   }
{ Software Foundation; either version 2 of the License, or (at your option)    }
{ any later version.                                                           }
{                                                                              }
{ This program is distributed in the hope that it will be useful, but WITHOUT  }
{ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        }
{ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for     }
{ more details.                                                                }
{                                                                              }
{ You should have received a copy of the GNU General Public License along with }
{ this program; if not, write to the Free Software Foundation, Inc., 51        }
{ Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                          }
{                                                                              }
{ Linking StarUML statically or dynamically with other modules is making a     }
{ combined work based on StarUML. Thus, the terms and conditions of the GNU    }
{ General Public License cover the whole combination.                          }
{                                                                              }
{ In addition, as a special exception, Plastic Software give you permission to }
{ combine StarUML program with free software programs or libraries that are    }
{ released under the GNU LGPL/Mozilla/Apache/BSD and with code included in the }
{ standard release of ExpressBar, ExpressNavBar, ExpressInspector,             }
{ ExpressPageControl, ProGrammar, NextGrid under the commercial license (or    }
{ modified versions of such code, with unchanged license). You may copy and    }
{ distribute such a system following the terms of the GNU GPL for StarUML and  }
{ the licenses of the other code concerned, provided that you include the      }
{ source code of that other code when and as the GNU GPL requires distribution }
{ of source code. Plastic Software also give you permission to combine StarUML }
{ program with dynamically linking plug-in (or add-in) programs that are       }
{ released under the GPL-incompatible and proprietary license.                 }
{                                                                              }
{ Note that people who make modified versions of StarUML are not obligated to  }
{ grant this special exception for their modified versions; it is their choice }
{ whether to do so. The GNU General Public License gives permission to release }
{ a modified version without this exception; this exception also makes it      }
{ possible to release a modified version which carries forward this exception. }
{******************************************************************************}

interface

uses
  BasicClasses, Core, ViewCore, ExtCore, UMLModels, UMLViews, UMLAux, Handlers,
  DiagramEditors, QuickDialogFrm,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, GraphicClasses, Dialogs, ExtCtrls, StdCtrls, ImgList, dxBar,
  ComCtrls, ToolWin, dxPageControl, Menus, dxBarExtItems, Registry, ExtDlgs,
  AreaTitleBar, InspectorFrm, WorkingAreaFrm,
  FlatPanel, CompactFontDlg, SaveDialogEx, DiagramExplorerFrame,
  dxDockControl, dxDockPanel, ModelExplorerFrame, VirtualTrees,
  MessageFrame, OutputFrame, AttachEdt,
  DocuEdt, PropEdt, XPMan, dxNavBarCollns, dxNavBarBase, dxNavBar;

const
  FILE_EXT_BMP = '.BMP';

type
  // Enumeration types
  PFrameWindowKind = (fwNone, fwBrowser, fwInspector, fwInformation, fwWorkingArea);

  // Event types
  PModelAddMenuClickEvent = procedure(ModelKind: string; Argument: Integer) of object;
  PModelAddDiagramMenuClickEvent = procedure(DiagramKind: string) of object;
  PCheckTypeMenuClickEvent = procedure(Sender: TObject; Checked: Boolean) of object;
  PWndClipboardEvent = procedure(var Msg: TMessage) of object;
  PWndDropFilesEvent = procedure (var Msg: TWMDROPFILES) of object;
  PZoomEvent = procedure(Sender: TObject; Zoom: Integer) of object;
  PFontFaceEvent = procedure(Sender: TObject; FontFace: string) of object;
  PFontSizeEvent = procedure(Sender: TObject; FontSize: Integer) of object;
  PRecentFileClickEvent = procedure(Sender: TObject; RecentFile: string) of object;
  PRecentFileAddEvent = procedure (Sender: TObject; RecentFile: string) of object;
  PDiagramEditorEvent = procedure(Sender: TObject; DiagramEditor: PDiagramEditor) of object;

  // TMainForm
  TMainForm = class(TForm)
    BarManager: TdxBarManager;
    { File Menu Items }
    FileCreate: TdxBarButton;
    FileMenu: TdxBarSubItem;
    FileNew: TdxBarButton;
    FileOpen: TdxBarButton;
    FileSave: TdxBarButton;
    FileSaveAs: TdxBarButton;
    FileClose: TdxBarButton;
    FileUnits: TdxBarSubItem;
    FileUnitsSeparateUnit: TdxBarButton;
    FileUnitsMergeUnit: TdxBarButton;
    FileUnitsSave: TdxBarButton;
    FileUnitsSaveAs: TdxBarButton;
    FilePrint: TdxBarButton;
    FilePageSetup: TdxBarButton;
    FileImport: TdxBarSubItem;
    FileExport: TdxBarSubItem;
    FileImportModelFragment: TdxBarButton;
    FileImportFramework: TdxBarButton;
    FileExportModelFragment: TdxBarButton;
    FileExportDiagram: TdxBarButton;
    FileRecentFiles: TdxBarSubItem;
    FileExit: TdxBarButton;
    { Edit Menu Items }
    EditMenu: TdxBarSubItem;
    EditUndo: TdxBarButton;
    EditRedo: TdxBarButton;
    EditCut: TdxBarButton;
    EditCopy: TdxBarButton;
    EditCopyDiagram: TdxBarButton;
    EditPaste: TdxBarButton;
    EditDelete: TdxBarButton;
    EditDeleteFromModel: TdxBarButton;
    EditSelectAll: TdxBarButton;
    EditFind: TdxBarButton;
    { Format Menu Items }
    FormatMenu: TdxBarSubItem;
    FormatFont: TdxBarButton;
    FormatLineColor: TdxBarButton;
    FormatFillColor: TdxBarButton;
    FormatLineStyle: TdxBarSubItem;
    FormatLineStyleRectilinear: TdxBarButton;
    FormatLineStyleOblique: TdxBarButton;
    FormatSuppressAttributes: TdxBarButton;
    FormatSuppressOperations: TdxBarButton;
    FormatSuppressEnumerationLiterals: TdxBarButton;
    FormatStereotypeDisplay: TdxBarSubItem;
    FormatShowOperationSignature: TdxBarButton;
    FormatShowCompartmentVisibility: TdxBarButton;
    FormatShowParentName: TdxBarButton;
    FormatShowCompartmentStereotype: TdxBarButton;
    FormatAlignmentSendToBack: TdxBarButton;
    FormatAlignmentBringToFront: TdxBarButton;
    FormatAlignment: TdxBarSubItem;
    FormatAlignmentLeft: TdxBarButton;
    FormatAlignmentRight: TdxBarButton;
    FormatAlignmentMiddle: TdxBarButton;
    FormatAlignmentTop: TdxBarButton;
    FormatAlignmentBottom: TdxBarButton;
    FormatAlignmentCenter: TdxBarButton;
    FormatAlignmentSpaceEvenlyHorizontally: TdxBarButton;
    FormatAlignmentSpaceEvenlyVertically: TdxBarButton;
    FormatAutoResize: TdxBarButton;
    FormatStereotypeDisplayNone: TdxBarButton;
    FormatStereotypeDisplayText: TdxBarButton;
    FormatStereotypeDisplayIcon: TdxBarButton;
    FormatFontNameCombo: TdxBarFontNameCombo;
    FormatShowProperty: TdxBarButton;
    { Model Menu Items }
    ModelMenu: TdxBarSubItem;
    ModelAdd: TdxBarSubItem;
    ModelAddModel: TdxBarButton;
    ModelAddSubsystem: TdxBarButton;
    ModelAddPackage: TdxBarButton;
    ModelAddClass: TdxBarButton;
    ModelAddInterface: TdxBarButton;
    ModelAddEnumeration: TdxBarButton;
    ModelAddUseCase: TdxBarButton;
    ModelAddActor: TdxBarButton;
    ModelAddAttribute: TdxBarButton;
    ModelAddOperation: TdxBarButton;
    ModelAddTemplateParameter: TdxBarButton;
    ModelAddComponent: TdxBarButton;
    ModelAddComponentInstance: TdxBarButton;
    ModelAddNode: TdxBarButton;
    ModelAddNodeInstance: TdxBarButton;
    ModelAddParameter: TdxBarButton;
    ModelAddSignal: TdxBarButton;
    ModelAddObject: TdxBarButton;
    ModelAddActivityGraph: TdxBarButton;
    ModelAddCollaboration: TdxBarButton;
    ModelAddCollaborationInstanceSet: TdxBarButton;
    ModelAddStateMachine: TdxBarButton;
    ModelAddInteraction: TdxBarButton;
    ModelAddInteractionInstanceSet: TdxBarButton;
    ModelAddActionState: TdxBarButton;
    ModelAddSubactivityState: TdxBarButton;
    ModelAddDecision: TdxBarButton;
    ModelAddInitialState: TdxBarButton;
    ModelAddFinalState: TdxBarButton;
    ModelAddSwimlane: TdxBarButton;
    ModelAddSynchronization: TdxBarButton;
    ModelAddState: TdxBarButton;
    ModelAddSubmachineState: TdxBarButton;
    ModelAddJunctionPoint: TdxBarButton;
    ModelAddChoicePoint: TdxBarButton;
    ModelAddShallowHistory: TdxBarButton;
    ModelAddDeepHistory: TdxBarButton;
    ModelAddEnumerationLiteral: TdxBarButton;
    ModelAddEffect: TdxBarButton;
    ModelAddEntryAction: TdxBarButton;
    ModelAddDoAction: TdxBarButton;
    ModelAddExitAction: TdxBarButton;
    ModelAddSignalEvent: TdxBarButton;
    ModelAddCallEvent: TdxBarButton;
    ModelAddChangeEvent: TdxBarButton;
    ModelAddTimeEvent: TdxBarButton;
    ModelAddClassifierRole: TdxBarButton;
    ModelAddException: TdxBarButton;
    ModelAddDiagram: TdxBarSubItem;
    ModelAddDiagramUsecaseDiagram: TdxBarButton;
    ModelAddDiagramClassDiagram: TdxBarButton;
    ModelAddDiagramSequenceDiagram: TdxBarButton;
    ModelAddDiagramSequenceRoleDiagram: TdxBarButton;
    ModelAddDiagramCollaborationDiagram: TdxBarButton;
    ModelAddDiagramCollaborationRoleDiagram: TdxBarButton;
    ModelAddDiagramStatechartDiagram: TdxBarButton;
    ModelAddDiagramActivityDiagram: TdxBarButton;
    ModelAddDiagramComponentDiagram: TdxBarButton;
    ModelAddDiagramDeploymentDiagram: TdxBarButton;
    ModelProfiles: TdxBarButton;
    ModelConstraints: TdxBarButton;
    ModelTaggedValues: TdxBarButton;
    ModelModelVerification: TdxBarButton;
    { View Menu Items }
    ViewMenu: TdxBarSubItem;
    ViewRefresh: TdxBarButton;
    ViewZoom: TdxBarSubItem;
    ViewZoomFitInWindow: TdxBarButton;
    ViewZoomZoomIn: TdxBarButton;
    ViewZoomZoomOut: TdxBarButton;
    ViewZoom50: TdxBarButton;
    ViewZoom100: TdxBarButton;
    ViewZoom150: TdxBarButton;
    ViewZoom75: TdxBarButton;
    ViewZoom125: TdxBarButton;
    ViewZoom175: TdxBarButton;
    ViewZoom200: TdxBarButton;
    FormatLayoutDiagram: TdxBarButton;
    ViewCloseDiagram: TdxBarButton;
    ViewSelectInModelExplorer: TdxBarButton;
    ViewToolbars: TdxBarSubItem;
    ViewToolbarsStandard: TdxBarButton;
    ViewToolbarsFormat: TdxBarButton;
    ViewToolbarsView: TdxBarButton;
    ViewToolbarsAlignment: TdxBarButton;
    { Tools menu Items }
    ToolsMenu: TdxBarSubItem;
    ToolsOptions: TdxBarButton;
    ToolsAddInManager: TdxBarButton;
    { Help Menu Items }
    HelpMenu: TdxBarSubItem;
    HelpAbout: TdxBarButton;
    HelpContents: TdxBarButton;
    HelpStarUMLOnTheWeb: TdxBarButton;
    AllModelsGroup: TdxBarGroup;
    NamespacesGroup: TdxBarGroup;
    ClassifiersGroup: TdxBarGroup;
    InstancesGroup: TdxBarGroup;
    BehaviorsGroup: TdxBarGroup;
    FeaturesGroup: TdxBarGroup;
    ActionsGroup: TdxBarGroup;
    EventsGroup: TdxBarGroup;
    AllDiagramsGroup: TdxBarGroup;
    AlwaysGroup: TdxBarGroup;
    ProjectOpenedGroup: TdxBarGroup;
    DiagramActivatedGroup: TdxBarGroup;
    ViewSelectedGroup: TdxBarGroup;
    ModelSelectedGroup: TdxBarGroup;
    UnitSelectedGroup: TdxBarGroup;
    TotalImageList: TImageList;
    { Dialogs }
    SaveDialog: TSaveDialogEx;
    OpenDialog: TOpenDialog;
    SaveUnitDialog: TSaveDialogEx;
    SaveModelFragmentDialog: TSaveDialogEx;
    ImportModelFragmentDialog: TOpenDialog;
    StatusBarInfo: TdxBarStatic;
    StatusBarProgress: TdxBarProgressItem;
    StatusBarModifiedInfo: TdxBarStatic;
    InspectorAttachmentOpen: TdxBarButton;
    InspectorAttachmentAdd: TdxBarButton;
    InspectorAttachmentEdit: TdxBarButton;
    InspectorAttachmentDelete: TdxBarButton;
    InspectorAttachmentMoveUp: TdxBarButton;
    InspectorAttachmentMoveDown: TdxBarButton;
    AttachmentsPopupMenu: TdxBarPopupMenu;
    MessagesPopupMenu: TdxBarPopupMenu;
    MessagesClearAll: TdxBarButton;
    MessagesClearFindResults: TdxBarButton;
    MessagesClearVerificationResults: TdxBarButton;
    MessagesSaveMessages: TdxBarButton;
    ModelExplorerPopupMenu: TdxBarPopupMenu;
    ViewZoomCombo: TdxBarCombo;
    FormatFontSizeCombo: TdxBarCombo;
    DiagramTabPopupMenu: TdxBarPopupMenu;
    ViewCloseAllDiagrams: TdxBarButton;
    DiagramExplorerPopupMenu: TdxBarPopupMenu;
    FileRecentFilesMRU: TdxBarMRUListItem;
    BehavioralDiagramsGroup: TdxBarGroup;
    ExportDiagramDialog: TSaveDialogEx;
    ModelCollectionEditor: TdxBarButton;
    OpenUnitDialog: TOpenDialog;
    FileUnitsDeleteUnit: TdxBarButton;
    DiagramEditorPopupMenu: TdxBarPopupMenu;
    FontDialog: TCompactFontDialog;
    FileSelectProject: TdxBarButton;
    ColorDialog: TColorDialog;
    FileCreatePopupMenu: TdxBarPopupMenu;
    DockingManager: TdxDockingManager;
    DockSite: TdxDockSite;
    dxTabContainerDockSite1: TdxTabContainerDockSite;
    ModelExplorerDockPanel: TdxDockPanel;
    DiagramExplorerDockPanel: TdxDockPanel;
    ModelExplorer: TModelExplorerPanel;
    DiagramExplorer: TDiagramExplorerPanel;
    PropertiesDockPanel: TdxDockPanel;
    WorkingAreaDockPanel: TdxDockPanel;
    dxLayoutDockSite2: TdxLayoutDockSite;
    dxLayoutDockSite1: TdxLayoutDockSite;
    dxVertContainerDockSite2: TdxVertContainerDockSite;
    OutputDockPanel: TdxDockPanel;
    dxTabContainerDockSite2: TdxTabContainerDockSite;
    MessageDockPanel: TdxDockPanel;
    OutputPanel: TOutputPanel;
    DockPanelImageList: TImageList;
    MessagePanel: TMessagePanel;
    dxTabContainerDockSite3: TdxTabContainerDockSite;
    DocumentationDockPanel: TdxDockPanel;
    AttachmentsDockPanel: TdxDockPanel;
    DocumentationEditor: TDocumentationEditor;
    AttachmentEditor: TAttachmentEditor;
    XPManifest: TXPManifest;
    ViewModelExplorer: TdxBarButton;
    ViewDiagramExplorer: TdxBarButton;
    ViewPropertyEditor: TdxBarButton;
    ViewDocumentationEditor: TdxBarButton;
    ViewAttachmentEditor: TdxBarButton;
    ViewOutputWindow: TdxBarButton;
    ViewMessageWindow: TdxBarButton;
    FormatStereotypeDisplayDecoration: TdxBarButton;
    ModelAddDiagramCompositeStructureDiagram: TdxBarButton;
    EditCopyDiagramAsBitmap: TdxBarButton;
    ToolBoxDockPanel: TdxDockPanel;
    dxLayoutDockSite3: TdxLayoutDockSite;
    dxLayoutDockSite4: TdxLayoutDockSite;
    ViewDiagramPages: TdxBarSubItem;
    ViewDiagramPageList: TdxBarListItem;
    ViewToolbox: TdxBarButton;
    WorkingAreaFrame: TWorkingAreaFrame;
    EditGoTo: TdxBarSubItem;
    EditGoToItemList: TdxBarListItem;
    PaletteNavBar: TdxNavBar;
    AnnotationNavBarGroup: TdxNavBarGroup;
    BuiltInItemsNavBarGroup: TdxNavBarGroup;
    ClassDiagramNavBarGroup: TdxNavBarGroup;
    UseCaseDiagramNavBarGroup: TdxNavBarGroup;
    ComponentDiagramNavBarGroup: TdxNavBarGroup;
    DeploymentDiagramNavBarGroup: TdxNavBarGroup;
    SequenceDiagramNavBarGroup: TdxNavBarGroup;
    SequenceRoleDiagramNavBarGroup: TdxNavBarGroup;
    CollaborationDiagramNavBarGroup: TdxNavBarGroup;
    CollaborationRoleDiagramNavBarGroup: TdxNavBarGroup;
    StatechartDiagramNavBarGroup: TdxNavBarGroup;
    ActivityDiagramNavBarGroup: TdxNavBarGroup;
    SelectNavBarItem: TdxNavBarItem;
    TextNavBarItem: TdxNavBarItem;
    NoteNavBarItem: TdxNavBarItem;
    NoteLinkNavBarItem: TdxNavBarItem;
    SubsystemNavBarItem: TdxNavBarItem;
    PackageNavBarItem: TdxNavBarItem;
    ActorNavBarItem: TdxNavBarItem;
    UseCaseNavBarItem: TdxNavBarItem;
    ClassNavBarItem: TdxNavBarItem;
    InterfaceNavBarItem: TdxNavBarItem;
    EnumerationNavBarItem: TdxNavBarItem;
    SignalNavBarItem: TdxNavBarItem;
    ExceptionNavBarItem: TdxNavBarItem;
    ComponentNavBarItem: TdxNavBarItem;
    ComponentInstanceNavBarItem: TdxNavBarItem;
    NodeNavBarItem: TdxNavBarItem;
    NodeInstanceNavBarItem: TdxNavBarItem;
    AssociationNavBarItem: TdxNavBarItem;
    DirectedAssociationNavBarItem: TdxNavBarItem;
    AggregationNavBarItem: TdxNavBarItem;
    CompositionNavBarItem: TdxNavBarItem;
    GeneralizationNavBarItem: TdxNavBarItem;
    DependencyNavBarItem: TdxNavBarItem;
    RealizationNavBarItem: TdxNavBarItem;
    AssociationClassNavBarItem: TdxNavBarItem;
    IncludeNavBarItem: TdxNavBarItem;
    ExtendNavBarItem: TdxNavBarItem;
    ObjectNavBarItem: TdxNavBarItem;
    ClassifierRoleNavBarItem: TdxNavBarItem;
    AssociationRoleNavBarItem: TdxNavBarItem;
    SelfAssociationRoleNavBarItem: TdxNavBarItem;
    LinkNavBarItem: TdxNavBarItem;
    SelfLinkNavBarItem: TdxNavBarItem;
    MessageNavBarItem: TdxNavBarItem;
    ForwardMessageNavBarItem: TdxNavBarItem;
    ReverseMessageNavBarItem: TdxNavBarItem;
    SelfMessageNavBarItem: TdxNavBarItem;
    StimulusNavBarItem: TdxNavBarItem;
    ForwardStimulusNavBarItem: TdxNavBarItem;
    ReverseStimulusNavBarItem: TdxNavBarItem;
    SelfStimulusNavBarItem: TdxNavBarItem;
    StateNavBarItem: TdxNavBarItem;
    SubmachineStateNavBarItem: TdxNavBarItem;
    ActionStateNavBarItem: TdxNavBarItem;
    SubactivityStateNavBarItem: TdxNavBarItem;
    InitialStateNavBarItem: TdxNavBarItem;
    FinalStateNavBarItem: TdxNavBarItem;
    JunctionPointNavBarItem: TdxNavBarItem;
    ChoicePointNavBarItem: TdxNavBarItem;
    ShallowHistoryNavBarItem: TdxNavBarItem;
    DeepHistoryNavBarItem: TdxNavBarItem;
    SynchronizationNavBarItem: TdxNavBarItem;
    DecisionNavBarItem: TdxNavBarItem;
    TransitionNavBarItem: TdxNavBarItem;
    SelfTransitionNavBarItem: TdxNavBarItem;
    HorizontalSwimlaneNavBarItem: TdxNavBarItem;
    FormatShowExtendedNotation: TdxBarButton;
    ModelProperty: TdxBarButton;
    VerticalSwimlaneNavBarItem: TdxNavBarItem;
    ObjectFlowStateNavBarItem: TdxNavBarItem;
    FlowFinalStateNavBarItem: TdxNavBarItem;
    SystemBoundaryNavBarItem: TdxNavBarItem;
    SignalAcceptStateNavBarItem: TdxNavBarItem;
    SignalSendStateNavBarItem: TdxNavBarItem;
    ArtifactNavBarItem: TdxNavBarItem;
    PortNavBarItem: TdxNavBarItem;
    ConnectorNavBarItem: TdxNavBarItem;
    CombinedFragmentNavBarItem: TdxNavBarItem;
    InteractionOperandNavBarItem: TdxNavBarItem;
    RectangleNavBarItem: TdxNavBarItem;
    EllipseNavBarItem: TdxNavBarItem;
    RoundRectNavBarItem: TdxNavBarItem;
    LineNavBarItem: TdxNavBarItem;
    ImageNavBarItem: TdxNavBarItem;
    FrameNavBarItem: TdxNavBarItem;
    PartNavBarItem: TdxNavBarItem;
    CompositeStructureDiagramNavBarGroup: TdxNavBarGroup;
    InspectorFrame: TInspectorFrame;
    FormatWordWrap: TdxBarButton;
    ModelAddObjectFlowState: TdxBarButton;
    ModelAddFlowFinalState: TdxBarButton;
    ModelAddSignalAcceptState: TdxBarButton;
    ModelAddSignalSendState: TdxBarButton;
    ModelAddArtifact: TdxBarButton;
    ModelAddAttributeLink: TdxBarButton;
    ModelAddPort: TdxBarButton;
    ModelAddExtensionPoint: TdxBarButton;
    ModelAddCombinedFragment: TdxBarButton;
    ModelAddInteractionOperand: TdxBarButton;
    ModelAddFrame: TdxBarButton;
    DocumentStateImageList: TImageList;
    // Event Handlers (On Main Form Menu Item Clicked)
    procedure FileMenuClick(Sender: TObject);
    procedure EditMenuClick(Sender: TObject);
    procedure FormatMenuClick(Sender: TObject);
    procedure FormatCheckTypeMenuClick(Sender: TObject);
    procedure FormatFontNameComboChange(Sender: TObject);
    procedure FormatFontSizeComboChange(Sender: TObject);
    procedure ModelAddMenuClick(Sender: TObject);
    procedure ModelAddDiagramMenuClick(Sender: TObject);
    procedure ModelMenuClick(Sender: TObject);
    procedure ViewCheckTypeMenuClick(Sender: TObject);
    procedure ViewCheckTypeMenuMainFormOnlyClick(Sender: TObject);
    procedure ViewMenuClick(Sender: TObject);
    procedure ViewZoomMenuClick(Sender: TObject);
    procedure ViewZoomComboChange(Sender: TObject);
    procedure ToolsMenuClick(Sender: TObject);
    procedure HelpMenuClick(Sender: TObject);
    procedure FileRecentFilesMRUClick(Sender: TObject);
    // Event Handlers (On Inspector Form Attachment Item Clicked)
    procedure InspectorAttachmentButtonClick(Sender: TObject);
    // Event Handlers (On Information Form Messages Item Clicked)
    procedure InformationMessagesButtonClick(Sender: TObject);
    // Event Handlers (On Main Form Palette Item Clicked)
    procedure HandlerButtonClick(Sender: TObject);
    // Event Handlers (On Main Form Bar Manager Item Clicked)
    procedure BarManagerClickItem(Sender: TdxBarManager; ClickedItem: TdxBarItem);
    // Event Handlers (On System Clipboard related events)
    procedure WMDrawClipboardHandler(var Msg: TMessage); message WM_DRAWCLIPBOARD;
    procedure WMChangeCBChainHandler(var Msg: TMessage); message WM_CHANGECBCHAIN;
    // Event Handlers (On File Dropped)
    procedure WMDropFilesHandler(var Msg : TWMDROPFILES); message WM_DROPFILES;
    // Event Handlers (On Main Form Mouse Wheel event)
    procedure MainFormMouseWheelHandler(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
    // Event Handlers (On Dock Control Closed)
    procedure DockPanelVisibleChanged(Sender: TdxCustomDockControl);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure PaletteNavBarActiveGroupChanged(Sender: TObject);
    procedure PaletteNavBarCustomDrawLink(Sender: TObject;
      ACanvas: TCanvas; AViewInfo: TdxNavBarLinkViewInfo;
      var AHandled: Boolean);
  private
    AllowToTriggerFontFaceChangedEvent: Boolean;
    AllowToTriggerFontSizeChangedEvent: Boolean;
    AllowToTriggerZoomChangedEvent: Boolean;
    AllowToTriggerFormStartedEvent: Boolean;
    OldBrowserHeight: Integer;
    ActionProcessor: PActionProcessor;
    FFileName: string;
    FIsActivated: Boolean;
    FBrowserVisible: Boolean;
    FInspectorVisible: Boolean;
    FInformationVisible: Boolean;
    FNumberOfRecentProjects: Integer;
    // Event Fields (On MainForm's events)
    FOnFileMenuClicked: TNotifyEvent;
    FOnEditMenuClicked: TNotifyEvent;
    FOnFormatMenuClicked: TNotifyEvent;
    FOnFormatCheckTypeMenuClicked: PCheckTypeMenuClickEvent;
    FOnFontFaceChanged: PFontFaceEvent;
    FOnFontSizeChanged: PFontSizeEvent;
    FOnModelMenuClicked: TNotifyEvent;
    FOnModelAddMenuClicked: TNotifyEvent;
    FOnModelAddDiagramMenuClicked: TNotifyEvent;
    FOnHandlerButtonClicked: TNotifyEvent;
    FOnViewMenuClicked: TNotifyEvent;
    FOnViewCheckTypeMenuClicked: PCheckTypeMenuClickEvent;
    FOnZoomChanged: PZoomEvent;
    FOnToolsMenuClicked: TNotifyEvent;
    FOnHelpMenuClicked: TNotifyEvent;
    FOnRecentFileClicked: PRecentFileClickEvent;
    FOnRecentFileAdd: PRecentFileAddEvent;
    FOnActiveFrameChanged: TNotifyEvent;
    FOnStarted: TNotifyEvent;
    FOnDiagramOpen: PDiagramEvent;
    FOnDiagramClose: PDiagramEvent;
    FOnPaletteChanged: TNotifyEvent;
    FOnDockPanelVisibleChanged: TdxDockControlNotifyEvent;
    FOnPaletteNavBarCustomDrawLink: TdxNavBarCustomDrawLinkEvent;
    // Event Fields (On InspectorForm Attachment Toolbar's events)
    FOnInspectorAttachmentButtonClicked: TNotifyEvent;
    // Event Fields (On InformationForm Messages Toolbar's events)
    FOnInformationMessagesButtonClicked: TNotifyEvent;
    // Event Fields (On ActionProcessor's events)
    FOnElementCreating: PElementCreateHandlingEvent;
    FOnViewSelecting: PViewSelectingEvent;
    FOnViewDeselecting: TNotifyEvent;
    FOnAllViewsDeselecting: TNotifyEvent;
    FOnAdditionalViewSelecting: TNotifyEvent;
    FOnAreaSelecting: PAreaSelectingEvent;
    FOnViewMoving: PViewMovingEvent;
    FOnViewContainerChanging: PViewContainerChangingEvent;
    FOnSelectedViewsMoving: PSelectedViewsMovingEvent;
    FOnSelectedViewsContainerChanging: PSelectedViewsContainerChangingEvent;
    FOnParasiticViewMoving: PParasiticViewMovingEvent;
    FOnNodeResizing: PNodeResizingEvent;
    FOnEdgeModifying: PEdgeModifyingEvent;
    FOnEdgeReconnecting: PEdgeReconnectingEvent;
    FOnViewDoubleClicked: PViewMouseEvent;
    // Event Fields (On DiagramEditor's events)
    FOnDiagramViewDragOver: PDiagramViewDragOverEvent;
    FOnDiagramViewDragDrop: PDiagramViewDragDropEvent;
    // Event Fields (On Clipboard related)
    FOnWMDrawClipboard: PWndClipboardEvent;
    FOnWMChangeCBChain: PWndClipboardEvent;
    // Event Fields (On File Dropped)
    FOnWMDropFiles: PWndDropFilesEvent;
    // Event Fields (On QuickDialogManager events)
    FOnQuickDlgPopup: PQuickDlgPopupEvent;
    FOnGeneralNameExpressionApplying: PQuickDlgExpressionApplyingEvent;
    FOnClassifierRoleExpressionApplying: PQuickDlgExpressionApplyingEvent;
    FOnObjectExpressionApplying: PQuickDlgExpressionApplyingEvent;
    FOnAttributeExpressionApplying: PQuickDlgExpressionApplyingEvent;
    FOnOperationExpressionApplying: PQuickDlgExpressionApplyingEvent;
    FOnMessageExpressionApplying: PQuickDlgExpressionApplyingEvent;
    FOnQuickDlgModelCreating: PQuickDlgModelCreatingEvent;
    FOnQuickDlgModelCreatingWithName: PQuickDlgModelCreatingWithNameEvent;
    FOnQuickDlgElementDeleting: PQuickDlgElementDeletingEvent;
    FOnQuickDlgVisibilityChanging: PQuickDlgVisibilityChangingEvent;
    FOnQuickDlgAggregationKindChanging: PQuickDlgAggregationKindChangingEvent;
    FOnQuickDlgNavigableChanging: PQuickDlgNavigableChangingEvent;
    FOnQuickDlgMultiplicityChanging: PQuickDlgMultiplicityChangingEvent;
    FOnQuickDlgActionKindChanging: PQuickDlgActionKindChangingEvent;
    FOnQuickDlgOperationChanging: PQuickDlgOperationChangingEvent;
    FOnQuickDlgSignalChanging: PQuickDlgSignalChangingEvent;
    FOnQuickDlgInstantiationChanging: PQuickDlgInstantiationChangingEvent;
    FOnQuickDlgNoteTextChanging: PQuickDlgNoteTextChangingEvent;
    FOnQuickDlgOrderChanging: PQuickDlgOrderChangingEvent;
    FOnQuickDlgSeqNumberChanging: PQuickDlgSeqNumberChangingEvent;
    FOnQuickDlgObjectClassCreating: PQuickDlgObjectClassCreatingEvent;
    FOnQuickDlgCallActionOperationCreating: PQuickDlgCallActionOperationCreatingEvent;
    // Event Handlers (On DiagramEditor's events)
    procedure WorkingAreaMouseUpHandler(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure WorkingAreaMouseDownHandler(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure WorkingAreaMouseMoveHandler(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure WorkingAreaDragOverHandler(Sender, Source: TObject; var Accept: Boolean; var Msg: string);
    procedure WorkingAreaDragDropHandler(X, Y: Integer);
    procedure WorkingAreaDiagramOpenHandler(Sender: TObject; Diagram: PDiagram);
    procedure WorkingAreaDiagramCloseHandler(Sender: TObject; Diagram: PDiagram);
    // Event Handlers (On ActionProcessor's events)
    procedure ActionProcElementCreatingHandler(Sender: TObject; HandlerName: string; X1, Y1, X2, Y2: Integer);
    // procedure ActionProcElementCreatingAbortHandler(Sender: TObject);
    procedure ActionProcViewSelectingHandler(AView: PView);
    procedure ActionProcViewDeselectingHandler(Sender: TObject);
    procedure ActionProcAllViewsDeselectingHandler(Sender: TObject);
    procedure ActionProcAdditionalViewSelectingHandler(Sender: TObject);
    procedure ActionProcAreaSelectingHandler(X1, Y1, X2, Y2: Integer);
    procedure ActionProcQuickDlgPopupHandler(AView: PView; X, Y: Integer);
    procedure ActionProcViewDoubleClickedHandler(Sender: TObject; AView: PView; X, Y: Integer);
    procedure ActionProcViewMovingHandler(View: PView; DX, DY: Integer);
    procedure ActionProcViewContainerChangingHandler(View: PView; DX, DY: Integer; AContainerView: PView);
    procedure ActionProcSelectedViewsMovingHandler(DX, DY: Integer);
    procedure ActionProcSelectedViewsContainerChangingHandler(DX, DY: Integer; AContainerView: PView);
    procedure ActionProcParasiticViewMovingHandler(AParasiticView: PParasiticView; DX, DY: Extended);
    procedure ActionProcNodeResizingHandler(Node: PNodeView; Left, Top, Right, Bottom: Integer);
    procedure ActionProcEdgeModifyingHandler(Edge: PEdgeView; Points: PPoints);
    procedure ActionProcEdgeReconnectingHandler(Edge: PEdgeView; Points: PPoints; NewParticipant: PView; IsTailSide: Boolean);
    // Event Handlers (On Quick Dialog Manager)
    procedure QuickDlgGeneralNameExpApplyingHandler(AModel: PModel; Value: string);
    procedure QuickDlgClassifierRoleExpApplyingHandler(AModel: PModel; Value: string);
    procedure QuickDlgObjectExpApplyingHandler(AModel: PModel; Value: string);
    procedure QuickDlgAttributeExpApplyingHandler(AModel: PModel; Value: string);
    procedure QuickDlgOperationExpApplyingHandler(AModel: PModel; Value: string);
    procedure QuickDlgMessageExpApplyingHandler(AModel: PModel; Value: string);
    procedure QuickDlgModelCreatingHandler(Owner: PModel; ModelKind: string; Argument: Integer = 0; InsertIndex: Integer = -1);
    procedure QuickDlgModelCreatingWithNameHandler(Owner: PModel; ModelKind: string; Name: string = '');
    procedure QuickDlgElementDeletingHandler(AModel: PModel);
    procedure QuickDlgVisibilityChangingHandler(AModel: PModel; Kind: PUMLVisibilityKind);
    procedure QuickDlgAggregationKindChangingHandler(AModel: PModel; Kind: PUMLAggregationKind);
    procedure QuickDlgNavigableChangingHandler(AModel: PModel; IsNavigable: Boolean);
    procedure QuickDlgMultiplicityChangingHandler(AModel: PModel; Str: string);
    procedure QuickDlgActionKindChangingHandler(AModel: PModel; Kind: string);
    procedure QuickDlgOperationChangingHandler(AModel: PModel; AnOp: PUMLOperation);
    procedure QuickDlgSignalChangingHandler(AModel: PModel; ASignal: PUMLSignal);
    procedure QuickDlgInstantiationChangingHandler(AModel: PModel; AInstantiation: PUMLClassifier);
    procedure QuickDlgNoteTextChangingHandler(AView: PUMLCustomTextView; Strs: string);
    procedure QuickDlgOrderChangingHandler(AModel: PModel; AnItem: PModel; Key: string; NewOrder: Integer);
    procedure QuickDlgSeqNumberChangingHandler(AElement: PElement; ACollectionItem: PElement; Key: string; NewIndex: Integer);
    procedure QuickDlgObjectClassCreatingHandler(Owner: PUMLNamespace; AModel: PUMLModelElement; Name: string = '');
    procedure QuickDlgCallActionOperationCreatingHandler(AClassifier: PUMLClassifier; ACallAction: PUMLCallAction; Name: string = '');
    procedure QuickDlgHideHandler(Sender: TObject);
    // INNER Methods
    procedure InitializeActionProcessor;
    procedure InitializeQuickDialogManager;
    procedure SetFileName(Value: string);
    function GetFontFaceComboText: string;
    procedure SetFontFaceComboText(Value: string);
    function GetFontSizeComboText: string;
    procedure SetFontSizeComboText(Value: string);
    function GetZoomComboText: string;
    procedure SetZoomComboText(Value: string);
    procedure SetNumberOfRecentProjects(Value: Integer);
    procedure FontFaceChanged(FontFace: string);
    procedure FontSizeChanged(FontSize: Integer);
    procedure ZoomChanged(Zoom: Integer);
    procedure SelectSelectNavBarItem(NavBarGroup: TdxNavBarGroup);
    function GetActiveHandlerName: string;
  protected
    procedure Activate; override;
    procedure Deactivate; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure SetStatusBarInfo(Info: string; Progress: Integer = 0);
    procedure AddRecentFile(FileName: string);
    procedure DeleteRecentFile(FileName: string);
    procedure SaveToRegistry;
    procedure LoadFromRegistry;
    procedure LoadWindowPositionFromRegistry;
    procedure ShowQuickDialog(AView: PView; MousePoint: TPoint; Force: Boolean = False);
    procedure ShowQuickDialogByModelCreation(AView: PView; ArgModel: PModel);

    procedure ActivateSelectHandler;
    procedure ActivateHandler(HanderName: string);
    procedure AddCreateHandler(HandlerName: string; ContainerCandidates: array of PClass; SkeletonPaintingKind: PSkeletonPaintingKind);

    property FileName: string read FFileName write SetFileName;
    property FontFaceComboText: string read GetFontFaceComboText write SetFontFaceComboText;
    property FontSizeComboText: string read GetFontSizeComboText write SetFontSizeComboText;
    property ZoomComboText: string read GetZoomComboText write SetZoomComboText;
    property IsActivated: Boolean read FIsActivated;
    property NumberOfRecentProjects: Integer read FNumberOfRecentProjects write SetNumberOfRecentProjects;
    property ActiveHandlerName: string read GetActiveHandlerName;
    // Events (On MainForm's Menu Item Clicked)
    property OnFileMenuClicked: TNotifyEvent write FOnFileMenuClicked;
    property OnEditMenuClicked: TNotifyEvent write FOnEditMenuClicked;
    property OnFormatMenuClicked: TNotifyEvent write FOnFormatMenuClicked;
    property OnFormatCheckTypeMenuClicked: PCheckTypeMenuClickEvent write FOnFormatCheckTypeMenuClicked;
    property OnFontFaceChanged: PFontFaceEvent read FOnFontFaceChanged write FOnFontFaceChanged;
    property OnFontSizeChanged: PFontSizeEvent read FOnFontSizeChanged write FOnFontSizeChanged;
    property OnModelMenuClicked: TNotifyEvent write FOnModelMenuClicked;
    property OnModelAddMenuClicked: TNotifyEvent write FOnModelAddMenuClicked;
    property OnModelAddDiagramMenuClicked: TNotifyEvent read FOnModelAddDiagramMenuClicked write FOnModelAddDiagramMenuClicked;
    property OnHandlerButtonClicked: TNotifyEvent read FOnHandlerButtonClicked write FOnHandlerButtonClicked;
    property OnViewMenuClicked: TNotifyEvent write FOnViewMenuClicked;
    property OnViewCheckTypeMenuClicked: PCheckTypeMenuClickEvent write FOnViewCheckTypeMenuClicked;
    property OnZoomChanged: PZoomEvent read FOnZoomChanged write FOnZoomChanged;
    property OnToolsMenuClicked: TNotifyEvent write FOnToolsMenuClicked;
    property OnHelpMenuClicked: TNotifyEvent write FOnHelpMenuClicked;
    property OnRecentFileClicked: PRecentFileClickEvent read FOnRecentFileClicked write FOnRecentFileClicked;
    property OnRecentFileAdd: PRecentFileAddEvent read FOnRecentFileAdd write FOnRecentFileAdd;
    property OnActiveFrameChanged: TNotifyEvent read FOnActiveFrameChanged write FOnActiveFrameChanged;
    property OnStarted: TNotifyEvent read FOnStarted write FOnStarted;
    property OnDiagramOpen: PDiagramEvent read FOnDiagramOpen write FOnDiagramOpen;
    property OnDiagramClose: PDiagramEvent read FOnDiagramClose write FOnDiagramClose;
    property OnPaletteChanged: TNotifyEvent read FOnPaletteChanged write FOnPaletteChanged;
    property OnDockPanelVisibleChanged: TdxDockControlNotifyEvent read FOnDockPanelVisibleChanged write FOnDockPanelVisibleChanged;
    property OnPaletteNavBarCustomDrawLink: TdxNavBarCustomDrawLinkEvent read FOnPaletteNavBarCustomDrawLink write FOnPaletteNavBarCustomDrawLink;
    // Event Fields (On InspectorForm Attachment Toolbar's events)
    property OnInspectorAttachmentButtonClicked: TNotifyEvent write FOnInspectorAttachmentButtonClicked;
    // Event Fields (On InformationForm Messages Toolbar's events)
    property OnInformationMessagesButtonClicked: TNotifyEvent write FOnInformationMessagesButtonClicked;
    // Event (On ActionProcessor's events)
    property OnElementCreating: PElementCreateHandlingEvent write FOnElementCreating;
    property OnViewSelecting: PViewSelectingEvent write FOnViewSelecting;
    property OnViewDeselecting: TNotifyEvent write FOnViewDeselecting;
    property OnAllViewsDeselecting: TNotifyEvent write FOnAllViewsDeselecting;
    property OnAdditionalViewSelecting: TNotifyEvent write FOnAdditionalViewSelecting;
    property OnAreaSelecting: PAreaSelectingEvent write FOnAreaSelecting;
    property OnViewMoving: PViewMovingEvent write FOnViewMoving;
    property OnViewContainerChanging: PViewContainerChangingEvent write FOnViewContainerChanging;
    property OnSelectedViewsMoving: PSelectedViewsMovingEvent write FOnSelectedViewsMoving;
    property OnSelectedViewsContainerChanging: PSelectedViewsContainerChangingEvent write FOnSelectedViewsContainerChanging;
    property OnParasiticViewMoving: PParasiticViewMovingEvent write FOnParasiticViewMoving;
    property OnNodeResizing: PNodeResizingEvent write FOnNodeResizing;
    property OnEdgeModifying: PEdgeModifyingEvent write FOnEdgeModifying;
    property OnEdgeReconnecting: PEdgeReconnectingEvent write FOnEdgeReconnecting;
    property OnViewDoubleClicked: PViewMouseEvent write FOnViewDoubleClicked;
    // Event (On DiagramEditor's events)
    property OnDiagramViewDragOver: PDiagramViewDragOverEvent write FOnDiagramViewDragOver;
    property OnDiagramViewDragDrop: PDiagramViewDragDropEvent write FOnDiagramViewDragDrop;
    // Event (On Message from System Clipboard)
    property OnWMDrawClipboard: PWndClipboardEvent write FOnWMDrawClipboard;
    property OnWMChangeCBChain: PWndClipboardEvent write FOnWMChangeCBChain;
    // Event Fields (On File Dropped)
    property OnWMDropFiles: PWndDropFilesEvent write FOnWMDropFiles;
    // Event (On QuickDialogManager's events)
    property OnQuickDlgPopup: PQuickDlgPopupEvent write FOnQuickDlgPopup;
    property OnGeneralNameExpressionApplying: PQuickDlgExpressionApplyingEvent write FOnGeneralNameExpressionApplying;
    property OnClassifierRoleExpressionApplying: PQuickDlgExpressionApplyingEvent write FOnClassifierRoleExpressionApplying;
    property OnObjectExpressionApplying: PQuickDlgExpressionApplyingEvent write FOnObjectExpressionApplying;
    property OnAttributeExpressionApplying: PQuickDlgExpressionApplyingEvent write FOnAttributeExpressionApplying;
    property OnOperationExpressionApplying: PQuickDlgExpressionApplyingEvent write FOnOperationExpressionApplying;
    property OnMessageExpressionApplying: PQuickDlgExpressionApplyingEvent write FOnMessageExpressionApplying;
    property OnQuickDlgModelCreating: PQuickDlgModelCreatingEvent write FOnQuickDlgModelCreating;
    property OnQuickDlgModelCreatingWithName: PQuickDlgModelCreatingWithNameEvent write FOnQuickDlgModelCreatingWithName;
    property OnQuickDlgElementDeleting: PQuickDlgElementDeletingEvent write FOnQuickDlgElementDeleting;
    property OnQuickDlgVisibilityChanging: PQuickDlgVisibilityChangingEvent write FOnQuickDlgVisibilityChanging;
    property OnQuickDlgAggregationKindChanging: PQuickDlgAggregationKindChangingEvent write FOnQuickDlgAggregationKindChanging;
    property OnQuickDlgNavigableChanging: PQuickDlgNavigableChangingEvent write FOnQuickDlgNavigableChanging;
    property OnQuickDlgMultiplicityChanging: PQuickDlgMultiplicityChangingEvent write FOnQuickDlgMultiplicityChanging;
    property OnQuickDlgActionKindChanging: PQuickDlgActionKindChangingEvent write FOnQuickDlgActionKindChanging;
    property OnQuickDlgOperationChanging: PQuickDlgOperationChangingEvent write FOnQuickDlgOperationChanging;
    property OnQuickDlgSignalChanging: PQuickDlgSignalChangingEvent write FOnQuickDlgSignalChanging;
    property OnQuickDlgInstantiationChanging: PQuickDlgInstantiationChangingEvent write FOnQuickDlgInstantiationChanging;
    property OnQuickDlgNoteTextChanging: PQuickDlgNoteTextChangingEvent write FOnQuickDlgNoteTextChanging;
    property OnQuickDlgOrderChanging: PQuickDlgOrderChangingEvent write FOnQuickDlgOrderChanging;
    property OnQuickDlgSeqNumberChanging: PQuickDlgSeqNumberChangingEvent write FOnQuickDlgSeqNumberChanging;
    property OnQuickDlgObjectClassCreating: PQuickDlgObjectClassCreatingEvent write FOnQuickDlgObjectClassCreating;
    property OnQuickDlgCallActionOperationCreating: PQuickDlgCallActionOperationCreatingEvent write FOnQuickDlgCallActionOperationCreating;
  end;

var
  MainForm: TMainForm;

const
  RK_LOCATION = '\SOFTWARE\StarUML\';
  RK_MAIN = 'Main Window';
  RK_MODELEXPLORER = 'ModelExplorer';
  RK_RECENTFILES = 'Recent Files';
  RK_BARS = 'Bars';
  RK_DOCK = 'Dock';
  RK_COLORDIALOG = 'Color Dialog';
  POSTFIX_HANDLER = 'Handler';

implementation

uses
  StarUMLApp, UMLFacto, NLS, NLS_StarUML,
  StrUtils, Math, ShellAPI;

{$R *.dfm}

////////////////////////////////////////////////////////////////////////////////
// TMainForm

constructor TMainForm.Create(AOwner: TComponent);
begin
  inherited;
  AllowToTriggerFontFaceChangedEvent := True;
  AllowToTriggerFontSizeChangedEvent := True;
  AllowToTriggerZoomChangedEvent := True;
  AllowToTriggerFormStartedEvent := True;

  // ActionProcessor creatioin and settings
  InitializeActionProcessor;

  OnMouseWheel := MainFormMouseWheelHandler;

  WorkingAreaFrame.ImageList := TotalImageList;
  WorkingAreaFrame.DiagramPopupMenu := DiagramEditorPopupMenu;
  WorkingAreaFrame.TabPopupMenu := DiagramTabPopupMenu;
  WorkingAreaFrame.OnMouseUp := WorkingAreaMouseUpHandler;
  WorkingAreaFrame.OnMouseMove := WorkingAreaMouseMoveHandler;
  WorkingAreaFrame.OnMouseDown := WorkingAreaMouseDownHandler;
  WorkingAreaFrame.OnDragOver := WorkingAreaDragOverHandler;
  WorkingAreaFrame.OnDragDrop := WorkingAreaDragDropHandler;
  WorkingAreaFrame.OnDiagramOpen := WorkingAreaDiagramOpenHandler;
  WorkingAreaFrame.OnDiagramClose := WorkingAreaDiagramCloseHandler;

  // MenuStateHandler creatioin and Start-time Menu status settings
  BarManager.AutoHideEmptyBars := True;

  FIsActivated := False;
  FBrowserVisible := True;
  FInspectorVisible := True;
  FInformationVisible := True;
  FileName := '';
  NumberOfRecentProjects := 5;

  QuickDialogManager := PQuickDialogManager.Create;
  InitializeQuickDialogManager;
end;

destructor TMainForm.Destroy;
begin
  QuickDialogManager.Free;
  ColorDialog.Free;
  if ActionProcessor <> nil then ActionProcessor.Free;
  inherited;
end;

procedure TMainForm.WorkingAreaMouseUpHandler(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Sender is PDiagramEditor then
    ActionProcessor.MouseUp(Sender as PDiagramEditor, (Sender as PDiagramEditor).DiagramView.Canvas, Button, Shift, X, Y);
end;

procedure TMainForm.WorkingAreaMouseDownHandler(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Sender is PDiagramEditor then
    ActionProcessor.MouseDown(Sender as PDiagramEditor, (Sender as PDiagramEditor).DiagramView.Canvas, Button, Shift, X, Y);
end;

procedure TMainForm.WorkingAreaMouseMoveHandler(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if Sender is PDiagramEditor then
    ActionProcessor.MouseMove(Sender as PDiagramEditor, (Sender as PDiagramEditor).DiagramView.Canvas, Shift, X, Y);
end;

procedure TMainForm.WorkingAreaDragOverHandler(Sender, Source: TObject; var Accept: Boolean; var Msg: string);
begin
  if Assigned(FOnDiagramViewDragOver) then FOnDiagramViewDragOver(Sender, Source, Accept, Msg);
end;

procedure TMainForm.WorkingAreaDragDropHandler(X, Y: Integer);
begin
  if Assigned(FOnDiagramViewDragDrop) then FOnDiagramViewDragDrop(X, Y);
end;

procedure TMainForm.WorkingAreaDiagramOpenHandler(Sender: TObject; Diagram: PDiagram);
begin
  if Assigned(FOnDiagramOpen) then
    FOnDiagramOpen(Self, Diagram);
end;

procedure TMainForm.WorkingAreaDiagramCloseHandler(Sender: TObject; Diagram: PDiagram);
begin
  if Assigned(FOnDiagramClose) then
    FOnDiagramClose(Self, Diagram);
end;

procedure TMainForm.ActionProcElementCreatingHandler(Sender: TObject; HandlerName: string; X1, Y1, X2, Y2: Integer);
begin
  if Assigned(FOnElementCreating) then
    FOnElementCreating(Self, HandlerName, X1, Y1, X2, Y2);
end;

{
procedure TMainForm.ActionProcElementCreatingAbortHandler(Sender: TObject);
begin
//  if LockedNavBarItem = nil then
    ActivateSelectHandler;
end;
}

procedure TMainForm.ActionProcViewSelectingHandler(AView: PView);
begin
  if Assigned(FOnViewSelecting) then FOnViewSelecting(AView);
end;

procedure TMainForm.ActionProcViewDeselectingHandler(Sender: TObject);
begin
  if Assigned(FOnViewDeselecting) then FOnViewDeselecting(Sender);
end;

procedure TMainForm.ActionProcAllViewsDeselectingHandler(Sender: TObject);
begin
  if Assigned(FOnAllViewsDeselecting) then FOnAllViewsDeselecting(Sender);
end;

procedure TMainForm.ActionProcAdditionalViewSelectingHandler(Sender: TObject);
begin
  if Assigned(FOnAdditionalViewSelecting) then FOnAdditionalViewSelecting(Sender);
end;

procedure TMainForm.ActionProcAreaSelectingHandler(X1, Y1, X2, Y2: Integer);
begin
  if Assigned(FOnAreaSelecting) then FOnAreaSelecting(X1, Y1, X2, Y2);
end;

procedure TMainForm.ActionProcQuickDlgPopupHandler(AView: PView; X, Y: Integer);
begin
  if Assigned(FOnQuickDlgPopup) then FOnQuickDlgPopup(AView, X, Y);
end;

procedure TMainForm.ActionProcViewDoubleClickedHandler(Sender: TObject; AView: PView; X, Y: Integer);
begin
  if Assigned(FOnViewDoubleClicked) then FOnViewDoubleClicked(Self, AView, X, Y);
end;

procedure TMainForm.ActionProcViewMovingHandler(View: PView; DX, DY: Integer);
begin
  if Assigned(FOnViewMoving) then FOnViewMoving(View, DX, DY);
end;

procedure TMainForm.ActionProcViewContainerChangingHandler(View: PView; DX, DY: Integer; AContainerView: PView);
begin
  if Assigned(FOnViewContainerChanging) then FOnViewContainerChanging(View, DX, DY, AContainerView);
end;

procedure TMainForm.ActionProcSelectedViewsMovingHandler(DX, DY: Integer);
begin
  if Assigned(FOnSelectedViewsMoving) then FOnSelectedViewsMoving(DX, DY);
end;

procedure TMainForm.ActionProcSelectedViewsContainerChangingHandler(DX, DY: Integer; AContainerView: PView);
begin
  if Assigned(FOnSelectedViewsContainerChanging) then FOnSelectedViewsContainerChanging(DX, DY, AContainerView);
end;

procedure TMainForm.ActionProcParasiticViewMovingHandler(AParasiticView: PParasiticView; DX, DY: Extended);
begin
  if Assigned(FOnParasiticViewMoving) then FOnParasiticViewMoving(AParasiticView, DX, DY);
end;

procedure TMainForm.ActionProcNodeResizingHandler(Node: PNodeView; Left, Top, Right, Bottom: Integer);
begin
  if Assigned(FOnNodeResizing) then FOnNodeResizing(Node, Left, Top, Right, Bottom);
end;

procedure TMainForm.ActionProcEdgeModifyingHandler(Edge: PEdgeView; Points: PPoints);
begin
  if Assigned(FOnEdgeModifying) then FOnEdgeModifying(Edge, Points);
end;

procedure TMainForm.ActionProcEdgeReconnectingHandler(Edge: PEdgeView; Points: PPoints; NewParticipant: PView; IsTailSide: Boolean);
begin
  if Assigned(FOnEdgeReconnecting) then FOnEdgeReconnecting(Edge, Points, NewParticipant, IsTailSide);
end;

procedure TMainForm.SetFileName(Value: string);
begin
  if FFileName <> Value then
  begin
    FFileName := Value;
    if FFileName <> '' then
      Caption := Application.Title + ' - ' + FFileName
    else
      Caption := Application.Title;
  end;
end;

function TMainForm.GetFontFaceComboText: string;
begin
  Result := FormatFontNameCombo.Text;
end;

procedure TMainForm.SetFontFaceComboText(Value: string);
begin
  AllowToTriggerFontFaceChangedEvent := False;
  FormatFontNameCombo.Text := Value;
  AllowToTriggerFontFaceChangedEvent := True;
end;

function TMainForm.GetFontSizeComboText: string;
begin
  Result := FormatFontSizeCombo.Text;
end;

procedure TMainForm.SetFontSizeComboText(Value: string);
begin
  AllowToTriggerFontSizeChangedEvent := False;
  FormatFontSizeCombo.Text := Value;
  AllowToTriggerFontSizeChangedEvent := True;
end;

function TMainForm.GetZoomComboText: string;
begin
  Result := ViewZoomCombo.Text;
end;

procedure TMainForm.SetZoomComboText(Value: string);
begin
  AllowToTriggerZoomChangedEvent := False;
  ViewZoomCombo.Text := Value;
  AllowToTriggerZoomChangedEvent := True;
end;

procedure TMainForm.SetNumberOfRecentProjects(Value: Integer);
begin
  if FNumberOfRecentProjects <> Value then
  begin
    FNumberOfRecentProjects := Value;
    FileRecentFilesMRU.MaxItemCount := FNumberOfRecentProjects;
  end;
end;

procedure TMainForm.FontFaceChanged(FontFace: string);
begin
  if Assigned(FOnFontFaceChanged) and AllowToTriggerFontFaceChangedEvent then
    FOnFontFaceChanged(Self, FontFace);
end;

procedure TMainForm.FontSizeChanged(FontSize: Integer);
begin
  if Assigned(FOnFontSizeChanged) and AllowToTriggerFontSizeChangedEvent then
    FOnFontSizeChanged(Self, FontSize);
end;

procedure TMainForm.ZoomChanged(Zoom: Integer);
begin
  if Assigned(FOnZoomChanged) and AllowToTriggerZoomChangedEvent then
    FOnZoomChanged(Self, Zoom);
end;

procedure TMainForm.SelectSelectNavBarItem(NavBarGroup: TdxNavBarGroup);
var
  Link: TdxNavBarItemLink;
  I: Integer;
begin
  NavBarGroup.SelectedLinkIndex := -1;
  for I := 0 to NavBarGroup.LinkCount - 1 do begin
    Link := NavBarGroup.Links[I];
    if Link.Item = SelectNavBarItem then begin
      NavBarGroup.SelectedLinkIndex := Link.Index;
      Exit;
    end;
  end;
end;

function TMainForm.GetActiveHandlerName: string;
begin
  Result := ActionProcessor.ActiveHandler.HandlerName;
end;

procedure TMainForm.Activate;
begin
  FIsActivated := True;
  inherited;
  if AllowToTriggerFormStartedEvent and Assigned(FOnStarted) then FOnStarted(Self);
  AllowToTriggerFormStartedEvent := False;
end;

procedure TMainForm.Deactivate;
begin
  FIsActivated := False;
  inherited;
end;

procedure TMainForm.WMDrawClipboardHandler(var Msg: TMessage);
begin
  inherited;
  if Assigned(FOnWMDrawClipboard) then FOnWMDrawClipboard(Msg);
end;

procedure TMainForm.WMChangeCBChainHandler(var Msg: TMessage);
begin
  inherited;
  if Assigned(FOnWMChangeCBChain) then FOnWMChangeCBChain(Msg);
end;

procedure TMainForm.WMDropFilesHandler(var Msg : TWMDROPFILES);
begin
  inherited;
  if Assigned(FOnWMDropFiles) then FOnWMDropFiles(Msg);
end;

procedure TMainForm.MainFormMouseWheelHandler(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
  if dxDockingController.ActiveDockControl = WorkingAreaDockPanel then begin
    if WorkingAreaFrame.ActiveDiagramEditor <> nil then
      WorkingAreaFrame.ActiveDiagramEditor.ScrollBy(0, -(WheelDelta div DEFAULT_WHEEL_DIVIDER));
  end;
end;

procedure TMainForm.DockPanelVisibleChanged(Sender: TdxCustomDockControl);
begin
  if Assigned(FOnDockPanelVisibleChanged) then
    FOnDockPanelVisibleChanged(Sender);
end;

procedure TMainForm.QuickDlgGeneralNameExpApplyingHandler(AModel: PModel; Value: string);
begin
  if Assigned(FOnGeneralNameExpressionApplying) then FOnGeneralNameExpressionApplying(AModel, Value);
end;

procedure TMainForm.QuickDlgClassifierRoleExpApplyingHandler(AModel: PModel; Value: string);
begin
  if Assigned(FOnClassifierRoleExpressionApplying) then FOnClassifierRoleExpressionApplying(AModel, Value);
end;

procedure TMainForm.QuickDlgObjectExpApplyingHandler(AModel: PModel; Value: string);
begin
  if Assigned(FOnObjectExpressionApplying) then FOnObjectExpressionApplying(AModel, Value);
end;

procedure TMainForm.QuickDlgAttributeExpApplyingHandler(AModel: PModel; Value: string);
begin
  if Assigned(FOnAttributeExpressionApplying) then FOnAttributeExpressionApplying(AModel, Value);
end;

procedure TMainForm.QuickDlgOperationExpApplyingHandler(AModel: PModel; Value: string);
begin
  if Assigned(FOnOperationExpressionApplying) then FOnOperationExpressionApplying(AModel, Value);
end;

procedure TMainForm.QuickDlgMessageExpApplyingHandler(AModel: PModel; Value: string);
begin
  if Assigned(FOnMessageExpressionApplying) then FOnMessageExpressionApplying(AModel, Value);
end;

procedure TMainForm.QuickDlgModelCreatingHandler(Owner: PModel; ModelKind: string; Argument: Integer = 0;
  InsertIndex: Integer = -1);
begin
  if Assigned(FOnQuickDlgModelCreating) then FOnQuickDlgModelCreating(Owner, ModelKind, Argument, InsertIndex);
end;

procedure TMainForm.QuickDlgModelCreatingWithNameHandler(Owner: PModel; ModelKind: string; Name: string = '');
begin
  if Assigned(FOnQuickDlgModelCreatingWithName) then FOnQuickDlgModelCreatingWithName(Owner, ModelKind, Name);
end;

procedure TMainForm.QuickDlgElementDeletingHandler(AModel: PModel);
begin
  if Assigned(FOnQuickDlgElementDeleting) then FOnQuickDlgElementDeleting(AModel); 
end;

procedure TMainForm.QuickDlgVisibilityChangingHandler(AModel: PModel; Kind: PUMLVisibilityKind);
begin
  if Assigned(FOnQuickDlgVisibilityChanging) then FOnQuickDlgVisibilityChanging(AModel, Kind);
end;

procedure TMainForm.QuickDlgAggregationKindChangingHandler(AModel: PModel; Kind: PUMLAggregationKind);
begin
  if Assigned(FOnQuickDlgAggregationKindChanging) then FOnQuickDlgAggregationKindChanging(AModel, Kind);
end;

procedure TMainForm.QuickDlgNavigableChangingHandler(AModel: PModel; IsNavigable: Boolean);
begin
  if Assigned(FOnQuickDlgNavigableChanging) then FOnQuickDlgNavigableChanging(AModel, IsNavigable);
end;

procedure TMainForm.QuickDlgMultiplicityChangingHandler(AModel: PModel; Str: string);
begin
  if Assigned(FOnQuickDlgMultiplicityChanging) then FOnQuickDlgMultiplicityChanging(AModel, Str);
end;

procedure TMainForm.QuickDlgActionKindChangingHandler(AModel: PModel; Kind: string);
begin
  if Assigned(FOnQuickDlgActionKindChanging) then FOnQuickDlgActionKindChanging(AModel, Kind);
end;

procedure TMainForm.QuickDlgOperationChangingHandler(AModel: PModel; AnOp: PUMLOperation);
begin
  if Assigned(FOnQuickDlgOperationChanging) then FOnQuickDlgOperationChanging(AModel, AnOp);
end;

procedure TMainForm.QuickDlgSignalChangingHandler(AModel: PModel; ASignal: PUMLSignal);
begin
  if Assigned(FOnQuickDlgSignalChanging) then FOnQuickDlgSignalChanging(AModel, ASignal);
end;

procedure TMainForm.QuickDlgInstantiationChangingHandler(AModel: PModel; AInstantiation: PUMLClassifier);
begin
  if Assigned(FOnQuickDlgInstantiationChanging) then FOnQuickDlgInstantiationChanging(AModel, AInstantiation);
end;

procedure TMainForm.QuickDlgNoteTextChangingHandler(AView: PUMLCustomTextView; Strs: string);
begin
  if Assigned(FOnQuickDlgNoteTextChanging) then FOnQuickDlgNoteTextChanging(AView, Strs);
end;

procedure TMainForm.QuickDlgOrderChangingHandler(AModel: PModel; AnItem: PModel; Key: string; NewOrder: Integer);
begin
  if Assigned(FOnQuickDlgOrderChanging) then FOnQuickDlgOrderChanging(AModel, AnItem, Key, NewOrder);
end;

procedure TMainForm.QuickDlgSeqNumberChangingHandler(AElement: PElement; ACollectionItem: PElement; Key: string; NewIndex: Integer);
begin
  if Assigned(FOnQuickDlgSeqNumberChanging) then FOnQuickDlgSeqNumberChanging(AElement, ACollectionItem, Key, NewIndex);
end;

procedure TMainForm.QuickDlgObjectClassCreatingHandler(Owner: PUMLNamespace; AModel: PUMLModelElement; Name: string = '');
begin
  if Assigned(FOnQuickDlgObjectClassCreating) then FOnQuickDlgObjectClassCreating(Owner, AModel, Name);
end;

procedure TMainForm.QuickDlgCallActionOperationCreatingHandler(AClassifier: PUMLClassifier; ACallAction: PUMLCallAction; Name: string = '');
begin
  if Assigned(FOnQuickDlgCallActionOperationCreating) then FOnQuickDlgCallActionOperationCreating(AClassifier, ACallAction, Name);
end;

procedure TMainForm.QuickDlgHideHandler(Sender: TObject);
begin
  SetFocus;
end;

procedure TMainForm.InitializeActionProcessor;
begin
  ActionProcessor := PActionProcessor.Create;
  with ActionProcessor do begin
    OnElementCreating := ActionProcElementCreatingHandler;
    // OnElementCreatingAbort := ActionProcElementCreatingAbortHandler;
    OnViewSelecting := ActionProcViewSelectingHandler;
    OnViewDeselecting := ActionProcViewDeselectingHandler;
    OnAllViewsDeselecting := ActionProcAllViewsDeselectingHandler;
    OnAdditionalViewSelecting := ActionProcAdditionalViewSelectingHandler;
    OnAreaSelecting := ActionProcAreaSelectingHandler;
    OnQuickDlgPopup := ActionProcQuickDlgPopupHandler;
    OnViewMoving := ActionProcViewMovingHandler;
    OnViewContainerChanging := ActionProcViewContainerChangingHandler;
    OnSelectedViewsMoving := ActionProcSelectedViewsMovingHandler;
    OnSelectedViewsContainerChanging := ActionProcSelectedViewsContainerChangingHandler;
    OnParasiticViewMoving := ActionProcParasiticViewMovingHandler;
    OnNodeResizing := ActionProcNodeResizingHandler;
    OnEdgeModifying := ActionProcEdgeModifyingHandler;
    OnEdgeReconnecting := ActionProcEdgeReconnectingHandler;
    OnViewDoubleClicked := ActionProcViewDoubleClickedHandler;
  end;
end;

procedure TMainForm.InitializeQuickDialogManager;
begin
  QuickDialogManager.OnGeneralNameExpApplying := QuickDlgGeneralNameExpApplyingHandler;
  QuickDialogManager.OnOrderChanging := QuickDlgOrderChangingHandler;
  QuickDialogManager.OnModelCreating := QuickDlgModelCreatingHandler;
  QuickDialogManager.OnModelCreatingWithName := QuickDlgModelCreatingWithNameHandler;
  QuickDialogManager.OnElementDeleting := QuickDlgElementDeletingHandler;
  QuickDialogManager.OnClassifierRoleExpApplying := QuickDlgClassifierRoleExpApplyingHandler;
  QuickDialogManager.OnObjectExpApplying := QuickDlgObjectExpApplyingHandler;
  QuickDialogManager.OnAttributeExpApplying := QuickDlgAttributeExpApplyingHandler;
  QuickDialogManager.OnActionKindChanging := QuickDlgActionKindChangingHandler;
  QuickDialogManager.OnOperationChanging := QuickDlgOperationChangingHandler;
  QuickDialogManager.OnSignalChanging := QuickDlgSignalChangingHandler;
  QuickDialogManager.OnInstantiationChanging := QuickDlgInstantiationChangingHandler;
  QuickDialogManager.OnOperationExpApplying := QuickDlgOperationExpApplyingHandler;
  QuickDialogManager.OnMessageExpApplying := QuickDlgMessageExpApplyingHandler;
  QuickDialogManager.OnVisibilityChanging := QuickDlgVisibilityChangingHandler;
  QuickDialogManager.OnAggregationKindChanging := QuickDlgAggregationKindChangingHandler;
  QuickDialogManager.OnNavigableChanging := QuickDlgNavigableChangingHandler;
  QuickDialogManager.OnMultiplicityChanging := QuickDlgMultiplicityChangingHandler;
  QuickDialogManager.OnNoteTextChanging := QuickDlgNoteTextChangingHandler;
  QuickDialogManager.OnSeqNumberChanging := QuickDlgSeqNumberChangingHandler;
  QuickDialogManager.OnObjectClassCreating := QuickDlgObjectClassCreatingHandler;
  QuickDialogManager.OnCallActionOperationCreating := QuickDlgCallActionOperationCreatingHandler;
  QuickDialogManager.OnQuickDialogHide := QuickDlgHideHandler;
end;

procedure TMainForm.SetStatusBarInfo(Info: string; Progress: Integer = 0);
begin
  BarManager.LockUpdate := True;
  StatusBarInfo.Caption := Info;
  StatusBarProgress.Position := Progress;
  BarManager.LockUpdate := False;
end;

procedure TMainForm.AddRecentFile(FileName: string);
begin
  if (FileName <> '') then
  begin
    FileRecentFilesMRU.AddItem(FileName, nil);
    if Assigned(FOnRecentFileAdd) then FOnRecentFileAdd(Self, FileName);
  end;
end;

procedure TMainForm.DeleteRecentFile(FileName: string);
var
  I: Integer;
  RecentFile: string;
begin
  for I := FileRecentFilesMru.Items.Count - 1 downto 0 do begin
    RecentFile := FileRecentFilesMru.Items.Strings[I];
    if (RecentFile = FileName) then FileRecentFilesMru.Items.Delete(I)
  end;
end;

procedure TMainForm.SaveToRegistry;
var
  Reg: TRegistry;
  I, J: Integer;
  NormalRect: TRect;
  S: string;

  procedure GetRestoredCoord(var R: TRect);
  var
    Struct: TWindowPlacement;
  begin
    with Struct do
    begin
      Length := SizeOf(Struct);
      GetWindowPlacement(Handle, @Struct);
      R := rcNormalPosition;
    end;
  end;

begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    // Main Window
    if Reg.OpenKey(RK_LOCATION + RK_MAIN, True) then
    begin
      Reg.WriteInteger('WindowState', Ord(WindowState));
      // WindowState := wsNormal;
      GetRestoredCoord(NormalRect);
      Reg.WriteInteger('Left', NormalRect.Left);
      Reg.WriteInteger('Top', NormalRect.Top);
      Reg.WriteInteger('Width', Abs(NormalRect.Right - NormalRect.Left) + 1);
      Reg.WriteInteger('Height', Abs(NormalRect.Bottom - NormalRect.Top) + 1);
      Reg.CloseKey;
    end;

    // ModelExplorer Window
    ModelExplorer.SaveToRegistry(RK_LOCATION + RK_MODELEXPLORER);

    // Recent Files
    Reg.DeleteKey(RK_LOCATION + RK_RECENTFILES);
    if Reg.OpenKey(RK_LOCATION + RK_RECENTFILES, True) then
    begin
      Reg.WriteInteger('RecentFileCount', FileRecentFilesMRU.Items.Count);
      for I := 0 to FileRecentFilesMRU.Items.Count - 1 do
        Reg.WriteString('RecentFiles[' + IntToStr(I) + ']', FileRecentFilesMRU.Items[I]);
      Reg.CloseKey;
    end;

    // Color Dialog
    Reg.DeleteKey(RK_LOCATION + RK_COLORDIALOG);
    if Reg.OpenKey(RK_LOCATION + RK_COLORDIALOG, True) then
    begin
      for I := 0 to 15 do begin
        S := ColorDialog.CustomColors.Values['Color' + Chr(65 + I)];
        if S <> '' then begin
          J := StrToInt('0x' + S);
          Reg.WriteInteger('CustomColor' + IntToStr(I), J);
        end;
      end;
      ColorDialog.Free;
      Reg.CloseKey;
    end;
    // Bar
    BarManager.SaveToRegistry(RK_LOCATION + RK_BARS);
    // DockingManager
    DockingManager.SaveLayoutToRegistry(RK_LOCATION + RK_DOCK);
  finally
    Reg.Free;
  end;
end;

procedure TMainForm.LoadFromRegistry;
var
  Reg: TRegistry;
  I, C, Value: Integer;
  S: string;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    // Main Window
    if Reg.OpenKey(RK_LOCATION + RK_MAIN, False) then
    begin
      Reg.CloseKey;
    end;
    // ModelExplorer Window
    ModelExplorer.LoadFromRegistry(RK_LOCATION + RK_MODELEXPLORER);

    // Recent Files
    if Reg.OpenKey(RK_LOCATION + RK_RECENTFILES, False) then
    begin
      C := Reg.ReadInteger('RecentFileCount');
      for I := C - 1 downto 0 do
      begin
        S := Reg.ReadString('RecentFiles[' + IntToStr(I) + ']');
        FileRecentFilesMRU.AddItem(S, nil);
        FOnRecentFileAdd(Self,S);
      end;
      Reg.CloseKey;
    end;

    // Color Dialog
    if Reg.OpenKey(RK_LOCATION + RK_COLORDIALOG, False) then
    begin
      for I := 0 to 15 do begin
        if Reg.ValueExists('CustomColor' + IntToStr(I)) then begin
          Value := Reg.ReadInteger('CustomColor' + IntToStr(I));
          ColorDialog.CustomColors.Add('Color' + Chr(65 + I) + '=' + IntToHex(Value, 2));
        end
        else ColorDialog.CustomColors.Add('Color' + Chr(65 + I) + '=' + 'FFFFFF')
      end;
      Reg.CloseKey;
    end;
    // Bars
    BarManager.LoadFromRegistry(RK_LOCATION + RK_BARS);
    // DockingManager
    if Reg.KeyExists(RK_LOCATION + RK_DOCK) then
      DockingManager.LoadLayoutFromRegistry(RK_LOCATION + RK_DOCK);
  finally
    Reg.Free;
  end;
end;

procedure TMainForm.LoadWindowPositionFromRegistry;
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    // Main Window
    if Reg.OpenKey(RK_LOCATION + RK_MAIN, False) then
    begin
      WindowState := wsNormal;
      Left := Reg.ReadInteger('Left');
      Top := Reg.ReadInteger('Top');
      Width := Reg.ReadInteger('Width');
      Height := Reg.ReadInteger('Height');
      WindowState := TWindowState(Reg.ReadInteger('WindowState'));
      Reg.CloseKey;
    end else
    begin
      WindowState := wsMaximized;
    end;
  finally
    Reg.Free;
  end;
end;

procedure TMainForm.ShowQuickDialog(AView: PView; MousePoint: TPoint; Force: Boolean = False);
var
  P: TPoint;
begin
  if Assigned(QuickDialogManager) then begin
    P := WorkingAreaFrame.ActiveDiagramEditor.PaintBox.ClientOrigin;
    QuickDialogManager.ShowQuickDialog(AView, P, MousePoint, Force);
  end;
end;

procedure TMainForm.ShowQuickDialogByModelCreation(AView: PView; ArgModel: PModel);
var
  P: TPoint;
begin
  if Assigned(QuickDialogManager) then begin
    P := WorkingAreaFrame.ActiveDiagramEditor.PaintBox.ClientOrigin;
    QuickDialogManager.ShowQuickDialogByModelCreation(AView, ArgModel, P);
  end;
end;

procedure TMainForm.ActivateSelectHandler;
begin
  if PaletteNavBar.ActiveGroup <> nil then
    SelectSelectNavBarItem(PaletteNavBar.ActiveGroup);
  ActionProcessor.ActivateHandler('SelectHandler');
end;

procedure TMainForm.ActivateHandler(HanderName: string);
begin
  ActionProcessor.ActivateHandler(HanderName);
end;

procedure TMainForm.AddCreateHandler(HandlerName: string; ContainerCandidates: array of PClass;
  SkeletonPaintingKind: PSkeletonPaintingKind);
begin
  ActionProcessor.AddCreateHandler(HandlerName, ContainerCandidates, SkeletonPaintingKind);
end;

// TMainForm
////////////////////////////////////////////////////////////////////////////////

procedure TMainForm.FileMenuClick(Sender: TObject);
begin
  if Assigned(FOnFileMenuClicked) then FOnFileMenuClicked(Sender);
end;

procedure TMainForm.EditMenuClick(Sender: TObject);
begin
  if Assigned(FOnEditMenuClicked) then FOnEditMenuClicked(Sender);
end;

procedure TMainForm.FormatMenuClick(Sender: TObject);
begin
  if Assigned(FOnFormatMenuClicked) then FOnFormatMenuClicked(Sender);
end;

procedure TMainForm.FormatCheckTypeMenuClick(Sender: TObject);
begin
  if Assigned(FOnFormatCheckTypeMenuClicked) then
    FOnFormatCheckTypeMenuClicked(Sender, (Sender as TdxBarButton).Down);
end;

procedure TMainForm.FormatFontNameComboChange(Sender: TObject);
begin
  FontFaceChanged(FormatFontNameCombo.Text);
end;

procedure TMainForm.FormatFontSizeComboChange(Sender: TObject);
begin
  try
    if FormatFontSizeCombo.Text <> '' then
      FontSizeChanged(StrToInt(FormatFontSizeCombo.Text));
  except
    MessageDlg(ERR_INVALID_VALUE, mtError, [mbOK], 0);
  end
end;

procedure TMainForm.ModelAddMenuClick(Sender: TObject);
begin
  if Assigned(FOnModelAddMenuClicked) then
    FOnModelAddMenuClicked(Sender);
end;

procedure TMainForm.ModelAddDiagramMenuClick(Sender: TObject);
begin
  if Assigned(FOnModelAddDiagramMenuClicked) then
    FOnModelAddDiagramMenuClicked(Sender);
end;

procedure TMainForm.ModelMenuClick(Sender: TObject);
begin
  if Assigned(FOnModelMenuClicked) then FOnModelMenuClicked(Sender);
end;

procedure TMainForm.ViewCheckTypeMenuClick(Sender: TObject);
begin
  if Assigned(FOnViewCheckTypeMenuClicked) then
    FOnViewCheckTypeMenuClicked(Sender, (Sender as TdxBarButton).Down);
end;

procedure TMainForm.ViewCheckTypeMenuMainFormOnlyClick(Sender: TObject);
begin
  if Sender = ViewToolbarsStandard then BarManager.Bars[1].Visible := (Sender as TdxBarButton).Down
  else if Sender = ViewToolbarsFormat then BarManager.Bars[3].Visible := (Sender as TdxBarButton).Down
  else if Sender = ViewToolbarsView then BarManager.Bars[4].Visible := (Sender as TdxBarButton).Down
  else if Sender = ViewToolbarsAlignment then BarManager.Bars[5].Visible := (Sender as TdxBarButton).Down;
end;

procedure TMainForm.ViewMenuClick(Sender: TObject);
begin
  if Assigned(FOnViewMenuClicked) then FOnViewMenuClicked(Sender);
end;

procedure TMainForm.ViewZoomMenuClick(Sender: TObject);
begin
  if Sender = ViewZoom50 then ZoomChanged(50)
  else if Sender = ViewZoom75 then ZoomChanged(75)
  else if Sender = ViewZoom100 then ZoomChanged(100)
  else if Sender = ViewZoom125 then ZoomChanged(125)
  else if Sender = ViewZoom150 then ZoomChanged(150)
  else if Sender = ViewZoom175 then ZoomChanged(175)
  else if Sender = ViewZoom200 then ZoomChanged(200)
  else ZoomChanged(100);
end;

procedure TMainForm.ViewZoomComboChange(Sender: TObject);
var
  ZoomVal: Integer;
begin
  try
    ZoomVal := StrToInt(Trim(AnsiReplaceStr(ViewZoomCombo.Text, '%', '')));
    ZoomChanged(ZoomVal);
  except
    MessageDlg(ERR_INVALID_VALUE, mtError, [mbOK], 0);
  end;
end;

procedure TMainForm.ToolsMenuClick(Sender: TObject);
begin
  if Assigned(FOnToolsMenuClicked) then FOnToolsMenuClicked(Sender);
end;

procedure TMainForm.HelpMenuClick(Sender: TObject);
begin
  if Assigned(FOnHelpMenuClicked) then FOnHelpMenuClicked(Sender);
end;

procedure TMainForm.FileRecentFilesMRUClick(Sender: TObject);
var
  AFileName: string;
begin
  AFileName := FileRecentFilesMRU.Items[FileRecentFilesMRU.ItemIndex];
  if FileExists(AFileName) then begin
    if Assigned(FOnRecentFileClicked) then
      FOnRecentFileClicked(Self, AFileName);
  end
  else begin
    MessageDlg(Format(ERR_FILE_NOT_FOUND, [AFileName]), mtError, [mbOK], 0);
  end;
end;

procedure TMainForm.InspectorAttachmentButtonClick(Sender: TObject);
begin
  if Assigned(FOnInspectorAttachmentButtonClicked) then FOnInspectorAttachmentButtonClicked(Sender);
end;

procedure TMainForm.InformationMessagesButtonClick(Sender: TObject);
begin
  if Assigned(FOnInformationMessagesButtonClicked) then FOnInformationMessagesButtonClicked(Sender);
end;

procedure TMainForm.HandlerButtonClick(Sender: TObject);
begin
  if Assigned(FOnHandlerButtonClicked) then
    FOnHandlerButtonClicked(Sender);
end;

procedure TMainForm.BarManagerClickItem(Sender: TdxBarManager;
  ClickedItem: TdxBarItem);
begin
  // if menu is selected, changes focus to DiagramPanel from Browser, Inspector,
  // Information Window, etc. after all reflect the change in its. But, if sub
  // menu is selected, Any menu is not executed but other menu is expanded.
  // In this case don't need to move focus.

  if not (ClickedItem is TdxBarSubItem) then begin
    InspectorFrame.ApplyChanges;
    DocumentationEditor.ApplyChanges;
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  NLSManager.SetFile(ExtractFilePath(Application.ExeName) + 'NLS\MAINFRM.LNG');
  NLSManager.TranslateComponent(Self, [TCompactFontDialog]);
  NLSManager.SetFile(ExtractFilePath(Application.ExeName) + 'NLS\FONTDLG.LNG');
  NLSManager.TranslateComponent(FontDialog, []);
  Application.Title := TXT_APPLICATION_TITLE;
end;

procedure TMainForm.PaletteNavBarActiveGroupChanged(Sender: TObject);
begin
  if ActionProcessor <> nil then
    if Assigned(FOnPaletteChanged) then
      FOnPaletteChanged(Sender);
end;

procedure TMainForm.FormKeyPress(Sender: TObject; var Key: Char);
var
  DgmEditor: PDiagramEditor;
begin
  if dxDockingController.ActiveDockControl = WorkingAreaDockPanel then begin
    DgmEditor := WorkingAreaFrame.ActiveDiagramEditor;
    if DgmEditor <> nil then
      ActionProcessor.KeyPress(DgmEditor, DgmEditor.Canvas, Key);
  end;
end;

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  DgmEditor: PDiagramEditor;
begin
  if dxDockingController.ActiveDockControl = WorkingAreaDockPanel then begin
    DgmEditor := WorkingAreaFrame.ActiveDiagramEditor;
    if DgmEditor <> nil then
      ActionProcessor.KeyDown(DgmEditor, DgmEditor.Canvas, Key, Shift);
  end;
end;

procedure TMainForm.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  DgmEditor: PDiagramEditor;
begin
{
  if Key = VK_ESCAPE then begin
    if LockHandlerButton.Down then LockHandlerButton.Down := False;
  end;
}  
  if dxDockingController.ActiveDockControl = WorkingAreaDockPanel then begin
    DgmEditor := WorkingAreaFrame.ActiveDiagramEditor;
    if DgmEditor <> nil then
      ActionProcessor.KeyUp(DgmEditor, DgmEditor.Canvas, Key, Shift);
  end;
end;

procedure TMainForm.PaletteNavBarCustomDrawLink(Sender: TObject; ACanvas: TCanvas;
  AViewInfo: TdxNavBarLinkViewInfo; var AHandled: Boolean);
begin
  if Assigned(FOnPaletteNavBarCustomDrawLink) then
    FOnPaletteNavBarCustomDrawLink(Self, ACanvas, AViewInfo, AHandled);
end;

end.




