/* 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;
    }
}
/* Add a new item to the page layout item list.
 * aType = WS_TEXT, WS_SEGMENT, WS_RECT, WS_POLYPOLYGON
 */
WORKSHEET_DATAITEM * PL_EDITOR_FRAME::AddPageLayoutItem( int aType, int aIdx )
{
    WORKSHEET_DATAITEM * item = NULL;

    switch( aType )
    {
        case WORKSHEET_DATAITEM::WS_TEXT:
            item = new WORKSHEET_DATAITEM_TEXT( wxT("Text") );
            break;

        case WORKSHEET_DATAITEM::WS_SEGMENT:
            item = new WORKSHEET_DATAITEM( WORKSHEET_DATAITEM::WS_SEGMENT );
            break;

        case WORKSHEET_DATAITEM::WS_RECT:
            item = new WORKSHEET_DATAITEM( WORKSHEET_DATAITEM::WS_RECT );
            break;

        case WORKSHEET_DATAITEM::WS_POLYPOLYGON:
            item = new WORKSHEET_DATAITEM_POLYPOLYGON();
            break;

        case WORKSHEET_DATAITEM::WS_BITMAP:
        {
            wxFileDialog fileDlg( this, _( "Choose Image" ), wxEmptyString, wxEmptyString,
                                  _( "Image Files " ) + wxImage::GetImageExtWildcard(),
                                  wxFD_OPEN );

            if( fileDlg.ShowModal() != wxID_OK )
                return NULL;

            wxString fullFilename = fileDlg.GetPath();

            if( !wxFileExists( fullFilename ) )
            {
                wxMessageBox( _( "Couldn't load image from <%s>" ), GetChars( fullFilename ) );
                break;
            }
            BITMAP_BASE* image = new BITMAP_BASE();

            if( !image->ReadImageFile( fullFilename ) )
            {
                wxMessageBox( _( "Couldn't load image from <%s>" ),
                                 GetChars( fullFilename ) );
                delete image;
                break;
            }
            item = new WORKSHEET_DATAITEM_BITMAP( image );
        }
            break;
    }

    if( item == NULL )
        return NULL;

    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
    pglayout.Insert( item, aIdx );
    RebuildDesignTree();

    return item;
}
/* Handles the selection of tools, menu, and popup menu commands.
 */
void PL_EDITOR_FRAME::Process_Special_Functions( wxCommandEvent& event )
{
    int id = event.GetId();
    int idx;
    wxString msg;
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
    WORKSHEET_DATAITEM* item = NULL;
    wxCommandEvent cmd( wxEVT_COMMAND_MENU_SELECTED );
    cmd.SetEventObject( this );

    switch( id )
    {
    case ID_NO_TOOL_SELECTED:
        SetNoToolSelected();
        break;

    case ID_MENU_ZOOM_SELECTION:
    case ID_ZOOM_SELECTION:
        // This tool is located on the main toolbar: switch it on or off on click
        if( GetToolId() != ID_ZOOM_SELECTION )
            SetToolID( ID_ZOOM_SELECTION, wxCURSOR_MAGNIFIER, _( "Zoom to selection" ) );
        else
            SetNoToolSelected();
        break;

    case ID_SELECT_PAGE_NUMBER:
        m_canvas->Refresh();
        break;

    case ID_SHEET_SET:
        {
        DIALOG_PAGES_SETTINGS dlg( this );
        dlg.SetWksFileName( GetCurrFileName() );
        dlg.EnableWksFileNamePicker( false );
        dlg.ShowModal();

        cmd.SetId( ID_ZOOM_PAGE );
        wxPostEvent( this, cmd );
        }
        break;

    case ID_POPUP_CANCEL_CURRENT_COMMAND:
        if( m_canvas->IsMouseCaptured() )
        {
            m_canvas->EndMouseCapture();
            SetToolID( GetToolId(), m_canvas->GetCurrentCursor(), wxEmptyString );
        }
        else
        {
            SetNoToolSelected();
        }
        break;

    case ID_POPUP_DESIGN_TREE_ITEM_DELETE:
    case ID_POPUP_ITEM_DELETE:
    case wxID_CUT:
        // Delete item, and select the previous item
        item = m_treePagelayout->GetPageLayoutSelectedItem();

        if( item == NULL )
            break;

        SaveCopyInUndoList();
        idx = pglayout.GetItemIndex( item );
        pglayout.Remove( item );
        RebuildDesignTree();

        if( id == ID_POPUP_DESIGN_TREE_ITEM_DELETE )
        {
            item = pglayout.GetItem( (unsigned) (idx-1) );

            if( item )
                m_treePagelayout->SelectCell( item );
        }

        item = NULL;
        OnModify();
        m_canvas->Refresh();
        break;

    case ID_POPUP_ITEM_ADD_LINE:
        SaveCopyInUndoList();
        idx =  m_treePagelayout->GetSelectedItemIndex();
        item = AddPageLayoutItem( WORKSHEET_DATAITEM::WS_SEGMENT, idx );

        if( InvokeDialogNewItem( this, item ) == wxID_CANCEL )
        {
            RemoveLastCommandInUndoList();
            pglayout.Remove( item );
            RebuildDesignTree();
            item = NULL;
        }
        else
        {
            // Put the new item in move mode, after putting the cursor
            // on the start point:
            wxPoint position = item->GetStartPosUi();
            SetCrossHairPosition( position, false );
            position = GetCrossHairPosition();

            if( m_canvas->IsPointOnDisplay( position ) )
                m_canvas->MoveCursorToCrossHair();
            else
                RedrawScreen( position, true );

            item->SetFlags( NEW_ITEM );
            MoveItem( item );
        }
        break;

    case ID_POPUP_ITEM_ADD_RECT:
        SaveCopyInUndoList();
        idx =  m_treePagelayout->GetSelectedItemIndex();
        item = AddPageLayoutItem( WORKSHEET_DATAITEM::WS_RECT, idx );

        if( InvokeDialogNewItem( this, item ) == wxID_CANCEL )
        {
            RemoveLastCommandInUndoList();
            pglayout.Remove( item );
            RebuildDesignTree();
            item = NULL;
        }
        else
        {
            // Put the new item in move mode, after putting the cursor
            // on the start point:
            wxPoint position = item->GetStartPosUi();
            SetCrossHairPosition( position, false );
            position = GetCrossHairPosition();

            if( m_canvas->IsPointOnDisplay( position ) )
                m_canvas->MoveCursorToCrossHair();
            else
                RedrawScreen( position, true );

            item->SetFlags( NEW_ITEM );
            MoveItem( item );
        }
        break;

    case ID_POPUP_ITEM_ADD_TEXT:
        SaveCopyInUndoList();
        idx =  m_treePagelayout->GetSelectedItemIndex();
        item = AddPageLayoutItem( WORKSHEET_DATAITEM::WS_TEXT, idx );
        if( InvokeDialogNewItem( this, item ) == wxID_CANCEL )
        {
            RemoveLastCommandInUndoList();
            pglayout.Remove( item );
            RebuildDesignTree();
            item = NULL;
        }
        else
        {
            // Put the new text in move mode:
            item->SetFlags( NEW_ITEM | LOCATE_STARTPOINT );
            MoveItem( item );
        }
        break;

    case ID_POPUP_ITEM_ADD_BITMAP:
        SaveCopyInUndoList();
        idx =  m_treePagelayout->GetSelectedItemIndex();
        item = AddPageLayoutItem( WORKSHEET_DATAITEM::WS_BITMAP, idx );
        if( item && InvokeDialogNewItem( this, item ) == wxID_CANCEL )
        {
            RemoveLastCommandInUndoList();
            pglayout.Remove( item );
            RebuildDesignTree();
            item = NULL;
        }
        if( item )
        {
            // Put the new text in move mode:
            item->SetFlags( NEW_ITEM | LOCATE_STARTPOINT );
            MoveItem( item );
        }
        break;

    case ID_POPUP_ITEM_APPEND_PAGE_LAYOUT:
        cmd.SetId( ID_APPEND_DESCR_FILE );
        wxPostEvent( this, cmd );
        break;

    case ID_POPUP_ITEM_PLACE:
        item = GetScreen()->GetCurItem();
        PlaceItem( item );
        break;

    case ID_POPUP_ITEM_PLACE_CANCEL:
        if(  m_canvas->IsMouseCaptured() )
             m_canvas->EndMouseCapture();
        break;

    case ID_POPUP_ITEM_MOVE_START_POINT:
        item = m_treePagelayout->GetPageLayoutSelectedItem();
        // Ensure flags are properly set
        item->ClearFlags( LOCATE_ENDPOINT );
        item->SetFlags( LOCATE_STARTPOINT );
        MoveItem( item );
        break;

    case ID_POPUP_ITEM_MOVE_END_POINT:
        item = m_treePagelayout->GetPageLayoutSelectedItem();
        // Ensure flags are properly set
        item->ClearFlags( LOCATE_STARTPOINT );
        item->SetFlags( LOCATE_ENDPOINT );
        MoveItem( item );
        break;

    case ID_POPUP_ITEM_MOVE:
        item = m_treePagelayout->GetPageLayoutSelectedItem();
        item->ClearFlags( LOCATE_ENDPOINT|LOCATE_STARTPOINT );
        MoveItem( item );
        break;

    default:
        wxMessageBox( wxT( "PL_EDITOR_FRAME::Process_Special_Functions error" ) );
        break;
    }

    if( item )
    {
        OnModify();
        m_propertiesPagelayout->CopyPrmsFromItemToPanel( item );
        m_treePagelayout->SelectCell( item );
    }

}