void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
{
    wxString filename;

    filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );

    if( filename != wxEmptyString )
    {
        if( GetScreen()->IsModify() && !IsOK( this,
                   _( "The current page layout has been modified.\n"
                      "Do you wish to discard the changes?" ) ) )
            return;

         m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
        ::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
        if( LoadPageLayoutDescrFile( filename ) )
        {
            wxString msg;
            msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
            SetStatusText( msg );
        }

        OnNewPageLayout();
    }
}
bool PL_EDITOR_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
{
    wxString fn = aFileSet[0];

    if( !LoadPageLayoutDescrFile( fn ) )
    {
        wxMessageBox( wxString::Format( _( "Error when loading file \"%s\"" ), fn ) );
        return false;
    }
    else
    {
        OnNewPageLayout();
        return true;
    }
}
PL_EDITOR_FRAME::PL_EDITOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
        EDA_DRAW_FRAME( aKiway, aParent, FRAME_PL_EDITOR, wxT( "PlEditorFrame" ),
                        wxDefaultPosition, wxDefaultSize,
                        KICAD_DEFAULT_DRAWFRAME_STYLE, PL_EDITOR_FRAME_NAME )
{
    m_UserUnits = MILLIMETRES;
    m_zoomLevelCoeff = 290.0;   // Adjusted to roughly displays zoom level = 1
                                // when the screen shows a 1:1 image
                                // obviously depends on the monitor,
                                // but this is an acceptable value

    m_showAxis = false;                 // true to show X and Y axis on screen
    m_showGridAxis = true;
    m_showBorderAndTitleBlock   = true; // true for reference drawings.
    m_hotkeysDescrList = PlEditorHotkeysDescr;
    m_originSelectChoice = 0;
    SetDrawBgColor( WHITE );            // default value, user option (WHITE/BLACK)
    WS_DATA_MODEL::GetTheInstance().m_EditMode = true;
    SetShowPageLimits( true );
    m_AboutTitle = "PlEditor";

    m_propertiesFrameWidth = 200;

    // Give an icon
    wxIcon icon;
    icon.CopyFromBitmap( KiBitmap( icon_pagelayout_editor_xpm ) );
    SetIcon( icon );

    // Create GAL canvas
#ifdef __WXMAC__
    // Cairo renderer doesn't handle Retina displays
    m_canvasType = EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL;
#else
    m_canvasType = EDA_DRAW_PANEL_GAL::GAL_TYPE_CAIRO;
#endif

    auto* drawPanel = new PL_DRAW_PANEL_GAL( this, -1, wxPoint( 0, 0 ), m_FrameSize,
                                             GetGalDisplayOptions(), m_canvasType );
    SetGalCanvas( drawPanel );

    LoadSettings( config() );
    SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );

    wxSize pageSizeIU = GetPageLayout().GetPageSettings().GetSizeIU();
    SetScreen( new PL_EDITOR_SCREEN( pageSizeIU ) );

    if( !GetScreen()->GridExists( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 ) )
        m_LastGridSizeId = ID_POPUP_GRID_LEVEL_1MM - ID_POPUP_GRID_LEVEL_1000;

    GetScreen()->SetGrid( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 );

    setupTools();
    ReCreateMenuBar();
    ReCreateHToolbar();
    ReCreateVToolbar();

    wxWindow* stsbar = GetStatusBar();
    int dims[] = {

        // balance of status bar on far left is set to a default or whatever is left over.
        -1,

        // When using GetTextSize() remember the width of '1' is not the same
        // as the width of '0' unless the font is fixed width, and it usually won't be.

        // zoom:
        GetTextSize( wxT( "Z 762000" ), stsbar ).x + 10,

        // cursor coords
        GetTextSize( wxT( "X 0234.567  Y 0234.567" ), stsbar ).x + 10,

        // delta distances
        GetTextSize( wxT( "dx 0234.567  dx 0234.567" ), stsbar ).x + 10,

        // Coord origin (use the bigger message)
        GetTextSize( _( "coord origin: Right Bottom page corner" ), stsbar ).x + 10,

        // units display, Inches is bigger than mm
        GetTextSize( _( "Inches" ), stsbar ).x + 10
    };

    SetStatusWidths( arrayDim( dims ), dims );

    m_auimgr.SetManagedWindow( this );
    m_auimgr.SetArtProvider( new EDA_DOCKART( this ) );

    m_propertiesPagelayout = new PROPERTIES_FRAME( this );

    // Horizontal items; layers 4 - 6
    m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" ).Top().Layer(6) );
    m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" ).Bottom().Layer(6) );

    // Vertical items; layers 1 - 3
    m_auimgr.AddPane( m_drawToolBar, EDA_PANE().VToolbar().Name( "ToolsToolbar" ).Right().Layer(1) );

    m_auimgr.AddPane( m_propertiesPagelayout, EDA_PANE().Palette().Name( "Props" ).Right().Layer(2)
                      .Caption( _( "Properties" ) ).MinSize( m_propertiesPagelayout->GetMinSize() )
                      .BestSize( m_propertiesFrameWidth, -1 ) );

    m_auimgr.AddPane( m_canvas, EDA_PANE().Canvas().Name( "DrawFrame" ).Center() );
    m_auimgr.AddPane( GetGalCanvas(), EDA_PANE().Canvas().Name( "DrawFrameGal" ).Center().Hide() );

    // Set up viewport
    KIGFX::VIEW* view = GetGalCanvas()->GetView();
    view->SetScale( GetZoomLevelCoeff() / m_canvas->GetZoom() );
    view->SetCenter( VECTOR2D( m_canvas->GetScreenCenterLogicalPosition() ) );

    UseGalCanvas();

    m_auimgr.Update();

    // Initialize the current page layout
    WS_DATA_MODEL& pglayout = WS_DATA_MODEL::GetTheInstance();
#if 0       //start with empty layout
    pglayout.AllowVoidList( true );
    pglayout.ClearList();
#else       // start with the default Kicad layout
    pglayout.SetPageLayout();
#endif
    OnNewPageLayout();
}
PL_EDITOR_FRAME::PL_EDITOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
    EDA_DRAW_FRAME( aKiway, aParent, FRAME_PL_EDITOR, wxT( "PlEditorFrame" ),
            wxDefaultPosition, wxDefaultSize, KICAD_DEFAULT_DRAWFRAME_STYLE, PL_EDITOR_FRAME_NAME )
{
    m_zoomLevelCoeff = 290.0;   // Adjusted to roughly displays zoom level = 1
                                // when the screen shows a 1:1 image
                                // obviously depends on the monitor,
                                // but this is an acceptable value

    m_showAxis = false;                 // true to show X and Y axis on screen
    m_showGridAxis = true;
    m_showBorderAndTitleBlock   = true; // true for reference drawings.
    m_hotkeysDescrList   = PlEditorHokeysDescr;
    m_originSelectChoice = 0;
    SetDrawBgColor( WHITE );            // default value, user option (WHITE/BLACK)
    SetShowPageLimits( true );

    m_designTreeWidth = 150;
    m_propertiesFrameWidth = 200;

    if( m_canvas )
        m_canvas->SetEnableBlockCommands( false );

    // Give an icon
    wxIcon icon;
    icon.CopyFromBitmap( KiBitmap( icon_pagelayout_editor_xpm ) );
    SetIcon( icon );
    wxSize pageSizeIU = GetPageLayout().GetPageSettings().GetSizeIU();
    SetScreen( new PL_EDITOR_SCREEN( pageSizeIU ) );

    LoadSettings( config() );
    SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );

    if( ! GetScreen()->GridExists( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 ) )
        m_LastGridSizeId = ID_POPUP_GRID_LEVEL_1MM - ID_POPUP_GRID_LEVEL_1000;

    GetScreen()->SetGrid( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 );

    ReCreateMenuBar();
    ReCreateHToolbar();
    ReCreateOptToolbar();

    wxWindow* stsbar = GetStatusBar();
    int dims[] = {

        // balance of status bar on far left is set to a default or whatever is left over.
        -1,

        // When using GetTextSize() remember the width of '1' is not the same
        // as the width of '0' unless the font is fixed width, and it usually won't be.

        // zoom:
        GetTextSize( wxT( "Z 762000" ), stsbar ).x + 10,

        // cursor coords
        GetTextSize( wxT( "X 0234.567  Y 0234.567" ), stsbar ).x + 10,

        // delta distances
        GetTextSize( wxT( "dx 0234.567  dx 0234.567" ), stsbar ).x + 10,

        // Coord origin (use the bigger message)
        GetTextSize( _( "coord origin: Right Bottom page corner" ), stsbar ).x + 10,

        // units display, Inches is bigger than mm
        GetTextSize( _( "Inches" ), stsbar ).x + 10
    };

    SetStatusWidths( DIM( dims ), dims );


    m_auimgr.SetManagedWindow( this );

    EDA_PANEINFO    horiz;
    horiz.HorizontalToolbarPane();

    EDA_PANEINFO    vert;
    vert.VerticalToolbarPane();

    EDA_PANEINFO    mesg;
    mesg.MessageToolbarPane();

    m_propertiesPagelayout = new PROPERTIES_FRAME( this );
    EDA_PANEINFO    props;
    props.LayersToolbarPane();
    props.MinSize( m_propertiesPagelayout->GetMinSize() );
    props.BestSize( m_propertiesFrameWidth, -1 );
    props.Caption( _( "Properties" ) );

    m_treePagelayout = new DESIGN_TREE_FRAME( this );
    EDA_PANEINFO    tree;
    tree.LayersToolbarPane();
    tree.MinSize( m_treePagelayout->GetMinSize() );
    tree.BestSize( m_designTreeWidth, -1 );
    tree.Caption( _( "Design" ) );

    if( m_mainToolBar )
        m_auimgr.AddPane( m_mainToolBar,
                          wxAuiPaneInfo( horiz ).Name( wxT( "m_mainToolBar" ) ).Top().Row( 0 ) );

    if( m_drawToolBar )
        m_auimgr.AddPane( m_drawToolBar,
                          wxAuiPaneInfo( vert ).Name( wxT( "m_drawToolBar" ) ).Right().Row( 1 ) );

    m_auimgr.AddPane( m_propertiesPagelayout,
                      props.Name( wxT( "m_propertiesPagelayout" ) ).Right().Layer( 1 ) );

    m_auimgr.AddPane( m_treePagelayout,
                      tree.Name( wxT( "m_treePagelayout" ) ).Left().Layer( 0 ) );

    if( m_optionsToolBar )
        m_auimgr.AddPane( m_optionsToolBar,
                          wxAuiPaneInfo( vert ).Name( wxT( "m_optionsToolBar" ) ).Left() );

    if( m_canvas )
        m_auimgr.AddPane( m_canvas,
                          wxAuiPaneInfo().Name( wxT( "DrawFrame" ) ).CentrePane().Layer( 5 ) );

    if( m_messagePanel )
        m_auimgr.AddPane( m_messagePanel,
                          wxAuiPaneInfo( mesg ).Name( wxT( "MsgPanel" ) ).Bottom().Layer( 10 ) );

    m_auimgr.Update();

    // Initialize the current page layout
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
#if 0       //start with empty layout
    pglayout.AllowVoidList( true );
    pglayout.ClearList();
#else       // start with the default Kicad layout
    pglayout.SetPageLayout();
#endif
    OnNewPageLayout();
}
/* File commands. */
void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
{
    wxString msg;
    int        id = event.GetId();
    wxString   filename = GetCurrFileName();
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();

    if( filename.IsEmpty() && id == wxID_SAVE )
        id = wxID_SAVEAS;

    switch( id )
    {
    case ID_LOAD_DEFAULT_PAGE_LAYOUT:
    case wxID_NEW:
    case wxID_OPEN:
        if( GetScreen()->IsModify() && !IsOK( this,
                   _( "The current page layout has been modified.\n"
                      "Do you wish to discard the changes?" ) ) )
            return;
        break;

    default:
        break;
    }


    switch( id )
    {
    case ID_LOAD_DEFAULT_PAGE_LAYOUT:
        pglayout.SetPageLayout();
        OnNewPageLayout();
        break;

    case wxID_NEW:
        pglayout.AllowVoidList( true );
        SetCurrFileName( wxEmptyString );
        pglayout.ClearList();
        OnNewPageLayout();
        break;

    case ID_APPEND_DESCR_FILE:
    {
         wxFileDialog openFileDialog(this, _("Append Page Layout Descr File"),
                wxEmptyString,
                wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);

        if (openFileDialog.ShowModal() == wxID_CANCEL)
            return;

        filename = openFileDialog.GetPath();
        if( ! InsertPageLayoutDescrFile( filename ) )
        {
            msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
            wxMessageBox( msg );
        }
        else
        {
            GetScreen()->SetModify();
            RebuildDesignTree();
            m_canvas->Refresh();
            msg.Printf( _("File <%s> inserted"), GetChars( filename ) );
            SetStatusText( msg );
        }
    }
        break;

    case wxID_OPEN:
    {
         wxFileDialog openFileDialog(this, _("Open file"), wxEmptyString,
                wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);

        if (openFileDialog.ShowModal() == wxID_CANCEL)
            return;

        filename = openFileDialog.GetPath();
        if( ! LoadPageLayoutDescrFile( filename ) )
        {
            msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
            wxMessageBox( msg );
        }
        else
        {
            OnNewPageLayout();
            msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
            SetStatusText( msg );
        }
    }
        break;

    case wxID_SAVE:
        if( !SavePageLayoutDescrFile( filename ) )
        {
            msg.Printf( _("Unable to write <%s>"), GetChars( filename ) );
            wxMessageBox( msg );
        }
        else
        {
            msg.Printf( _("File <%s> written"), GetChars( filename ) );
            SetStatusText( msg );
        }
        break;

    case wxID_SAVEAS:
    {
         wxFileDialog openFileDialog(this, _("Create file"), wxEmptyString,
                wxEmptyString, PageLayoutDescrFileWildcard, wxFD_SAVE);

        if (openFileDialog.ShowModal() == wxID_CANCEL)
            return;

        filename = openFileDialog.GetPath();
        // Ensure the file has the right extension:
        // because a name like name.subname.subsubname is legal,
        // add the right extension without replacing the wxFileName
        // extension
        wxFileName fn(filename);

        if( fn.GetExt() != PageLayoutDescrFileExtension )
            filename << wxT(".") << PageLayoutDescrFileExtension;

        if( !SavePageLayoutDescrFile( filename ) )
        {
            msg.Printf( _("Unable to create <%s>"), GetChars( filename ) );
            wxMessageBox( msg );
        }

        else
        {
            msg.Printf( _("File <%s> written"), GetChars( filename ) );
            SetStatusText( msg );
            if( GetCurrFileName().IsEmpty() )
                SetCurrFileName( filename );
        }
    }
        break;

    default:
        wxMessageBox( wxT( "File_io: unexpected command id" ) );
        break;
    }
}