// Data transfert from  properties frame to item parameters
bool PROPERTIES_FRAME::CopyPrmsFromPanelToItem( WORKSHEET_DATAITEM* aItem )
{
    if( aItem == NULL )
        return false;

    double dtmp;
    wxString msg;

    // Import common parameters:
    aItem->m_Info = m_textCtrlComment->GetValue();

    switch( m_choicePageOpt->GetSelection() )
    {
    default:
    case 0:
        aItem->SetPage1Option( 0 );
        break;

    case 1:
        aItem->SetPage1Option( 1 );
        break;

    case 2:
        aItem->SetPage1Option( -1 );
        break;
    }

    // Import thickness
    msg = m_textCtrlThickness->GetValue();
    msg.ToDouble( &dtmp );
    aItem->m_LineWidth = dtmp;

    // Import Start point
    msg = m_textCtrlPosX->GetValue();
    msg.ToDouble( &dtmp );
    aItem->m_Pos.m_Pos.x = dtmp;

    msg = m_textCtrlPosY->GetValue();
    msg.ToDouble( &dtmp );
    aItem->m_Pos.m_Pos.y = dtmp;

    switch( m_comboBoxCornerPos->GetSelection() )
    {
    case 2:
        aItem->m_Pos.m_Anchor = RB_CORNER;
        break;
    case 0:
        aItem->m_Pos.m_Anchor = RT_CORNER;
        break;
    case 3:
        aItem->m_Pos.m_Anchor = LB_CORNER;
        break;
    case 1:
        aItem->m_Pos.m_Anchor = LT_CORNER;
        break;
    }

    // Import End point
    msg = m_textCtrlEndX->GetValue();
    msg.ToDouble( &dtmp );
    aItem->m_End.m_Pos.x = dtmp;

    msg = m_textCtrlEndY->GetValue();
    msg.ToDouble( &dtmp );
    aItem->m_End.m_Pos.y = dtmp;

    switch( m_comboBoxCornerEnd->GetSelection() )
    {
    case 2:
        aItem->m_End.m_Anchor = RB_CORNER;
        break;
    case 0:
        aItem->m_End.m_Anchor = RT_CORNER;
        break;
    case 3:
        aItem->m_End.m_Anchor = LB_CORNER;
        break;
    case 1:
        aItem->m_End.m_Anchor = LT_CORNER;
        break;
    }

    // Import Repeat prms
    long itmp;
    msg = m_textCtrlRepeatCount->GetValue();
    msg.ToLong( &itmp );
    aItem->m_RepeatCount = itmp;

    msg = m_textCtrlStepX->GetValue();
    msg.ToDouble( &dtmp );
    aItem->m_IncrementVector.x = dtmp;

    msg = m_textCtrlStepY->GetValue();
    msg.ToDouble( &dtmp );
    aItem->m_IncrementVector.y = dtmp;

    if( aItem->GetType() == WORKSHEET_DATAITEM::WS_TEXT )
    {
        WORKSHEET_DATAITEM_TEXT* item = (WORKSHEET_DATAITEM_TEXT*) aItem;

        item->m_TextBase = m_textCtrlText->GetValue();

        msg = m_textCtrlTextIncrement->GetValue();
        msg.ToLong( &itmp );
        item->m_IncrementLabel = itmp;

        item->SetBold( m_checkBoxBold->IsChecked() );
        item->SetItalic( m_checkBoxItalic->IsChecked() );

        switch( m_choiceHjustify->GetSelection() )
        {
        case 0:
            item->m_Hjustify = GR_TEXT_HJUSTIFY_LEFT;
            break;
        case 1:
            item->m_Hjustify = GR_TEXT_HJUSTIFY_CENTER;
            break;
        case 2:
            item->m_Hjustify = GR_TEXT_HJUSTIFY_RIGHT;
            break;
        }
        switch( m_choiceVjustify->GetSelection() )
        {
        case 0:
            item->m_Vjustify = GR_TEXT_VJUSTIFY_TOP;
            break;
        case 1:
            item->m_Vjustify = GR_TEXT_VJUSTIFY_CENTER;
            break;
        case 2:
            item->m_Vjustify = GR_TEXT_VJUSTIFY_BOTTOM;
            break;
        }

        msg = m_textCtrlRotation->GetValue();
        msg.ToDouble( &dtmp );
        item->m_Orient = dtmp;

        // Import text size
        msg = m_textCtrlTextSizeX->GetValue();
        msg.ToDouble( &dtmp );
        item->m_TextSize.x = dtmp;

        msg = m_textCtrlTextSizeY->GetValue();
        msg.ToDouble( &dtmp );
        item->m_TextSize.y = dtmp;

        // Import constraints:
        msg = m_textCtrlConstraintX->GetValue();
        msg.ToDouble( &dtmp );
        item->m_BoundingBoxSize.x = dtmp;

        msg = m_textCtrlConstraintY->GetValue();
        msg.ToDouble( &dtmp );
        item->m_BoundingBoxSize.y = dtmp;
    }

    if( aItem->GetType() == WORKSHEET_DATAITEM::WS_POLYPOLYGON )
    {
        WORKSHEET_DATAITEM_POLYPOLYGON* item = (WORKSHEET_DATAITEM_POLYPOLYGON*) aItem;

        msg = m_textCtrlRotation->GetValue();
        msg.ToDouble( &dtmp );
        item->m_Orient = dtmp;
    }

    if( aItem->GetType() == WORKSHEET_DATAITEM::WS_BITMAP )
    {
        WORKSHEET_DATAITEM_BITMAP* item = (WORKSHEET_DATAITEM_BITMAP*) aItem;
        // Set definition in PPI
        long value;
        msg = m_textCtrlBitmapPPI->GetValue();
        if( msg.ToLong( &value ) )
            item->SetPPI( (int)value );
    }

    return true;
}
void WS_DRAW_ITEM_LIST::BuildWorkSheetGraphicList(
                       const PAGE_INFO& aPageInfo,
                       const TITLE_BLOCK& aTitleBlock,
                       COLOR4D aColor, COLOR4D aAltColor )
{
    WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();

    #define milsTomm (25.4/1000)

    m_titleBlock = &aTitleBlock;
    m_paperFormat = &aPageInfo.GetType();

    wxPoint LTmargin( Mm2mils( pglayout.GetLeftMargin() ),
                      Mm2mils( pglayout.GetTopMargin() ) );
    wxPoint RBmargin( Mm2mils( pglayout.GetRightMargin() ),
                      Mm2mils( pglayout.GetBottomMargin() ) );

    SetMargins( LTmargin, RBmargin );
    SetPageSize( aPageInfo.GetSizeMils() );

    // Build the basic layout shape, if the layout list is empty
    if( pglayout.GetCount() == 0 && !pglayout.VoidListAllowed() )
        pglayout.SetPageLayout();

    WORKSHEET_DATAITEM::m_WSunits2Iu = m_milsToIu / milsTomm;
    WORKSHEET_DATAITEM::m_Color = aColor;       // the default color to draw items
    WORKSHEET_DATAITEM::m_AltColor = aAltColor; // an alternate color to draw items

    // Left top corner position
    DPOINT lt_corner;
    lt_corner.x = pglayout.GetLeftMargin();
    lt_corner.y = pglayout.GetTopMargin();
    WORKSHEET_DATAITEM::m_LT_Corner = lt_corner;

    // Right bottom corner position
    DPOINT rb_corner;
    rb_corner.x = (m_pageSize.x*milsTomm) - pglayout.GetRightMargin();
    rb_corner.y = (m_pageSize.y*milsTomm) - pglayout.GetBottomMargin();
    WORKSHEET_DATAITEM::m_RB_Corner = rb_corner;

    WS_DRAW_ITEM_TEXT* gtext;
    int pensize;

    for( unsigned ii = 0; ; ii++ )
    {
        WORKSHEET_DATAITEM*  wsItem = pglayout.GetItem( ii );

        if( wsItem == NULL )
            break;

        // Generate it only if the page option allows this
        if( wsItem->GetPage1Option() < 0    // Not on page 1
            && m_sheetNumber <= 1 )
            continue;

        if( wsItem->GetPage1Option() > 0    // Only on page 1
            && m_sheetNumber > 1 )
            continue;

        COLOR4D color = wsItem->GetItemColor();

        pensize = wsItem->GetPenSizeUi();

        switch( wsItem->GetType() )
        {
        case WORKSHEET_DATAITEM::WS_TEXT:
        {
            WORKSHEET_DATAITEM_TEXT * wsText = (WORKSHEET_DATAITEM_TEXT*)wsItem;
            bool multilines = false;

            if( wsText->m_SpecialMode )
                wsText->m_FullText = wsText->m_TextBase;
            else
            {
                wsText->m_FullText = BuildFullText( wsText->m_TextBase );
                multilines = wsText->ReplaceAntiSlashSequence();
            }

            if( wsText->m_FullText.IsEmpty() )
                break;

            if( pensize == 0 )
                pensize = m_penSize;

            wsText->SetConstrainedTextSize();
            wxSize textsize;

            textsize.x = KiROUND( wsText->m_ConstrainedTextSize.x
                                  * WORKSHEET_DATAITEM::m_WSunits2Iu );
            textsize.y = KiROUND( wsText->m_ConstrainedTextSize.y
                                  * WORKSHEET_DATAITEM::m_WSunits2Iu );

            if( wsText->IsBold())
                pensize = GetPenSizeForBold( std::min( textsize.x, textsize.y ) );

            for( int jj = 0; jj < wsText->m_RepeatCount; jj++)
            {
                if( jj && ! wsText->IsInsidePage( jj ) )
                    continue;

                gtext = new WS_DRAW_ITEM_TEXT( wsText, wsText->m_FullText,
                                               wsText->GetStartPosUi( jj ),
                                               textsize, pensize, color,
                                               wsText->IsItalic(),
                                               wsText->IsBold() );
                Append( gtext );
                gtext->SetMultilineAllowed( multilines );
                wsText->TransfertSetupToGraphicText( gtext );

                // Increment label for the next text
                // (has no meaning for multiline texts)
                if( wsText->m_RepeatCount > 1 && !multilines )
                    wsText->IncrementLabel( (jj+1)*wsText->m_IncrementLabel);
            }
        }
            break;

        case WORKSHEET_DATAITEM::WS_SEGMENT:
            if( pensize == 0 )
                pensize = m_penSize;

            for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
            {
                if( jj && ! wsItem->IsInsidePage( jj ) )
                    continue;
                Append( new WS_DRAW_ITEM_LINE( wsItem, wsItem->GetStartPosUi( jj ),
                                               wsItem->GetEndPosUi( jj ),
                                               pensize, color ) );
            }
            break;

        case WORKSHEET_DATAITEM::WS_RECT:
            if( pensize == 0 )
                pensize = m_penSize;

            for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
            {
                if( jj && ! wsItem->IsInsidePage( jj ) )
                    break;

                Append( new WS_DRAW_ITEM_RECT( wsItem, wsItem->GetStartPosUi( jj ),
                                               wsItem->GetEndPosUi( jj ),
                                               pensize, color ) );
            }
            break;

        case WORKSHEET_DATAITEM::WS_POLYPOLYGON:
        {
            WORKSHEET_DATAITEM_POLYPOLYGON * wspoly =
                (WORKSHEET_DATAITEM_POLYPOLYGON*) wsItem;
            for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
            {
                if( jj && ! wsItem->IsInsidePage( jj ) )
                    continue;

                for( int kk = 0; kk < wspoly->GetPolyCount(); kk++ )
                {
                    const bool fill = true;
                    WS_DRAW_ITEM_POLYGON* poly = new WS_DRAW_ITEM_POLYGON( wspoly,
                                                wspoly->GetStartPosUi( jj ),
                                                fill, pensize, color );
                    Append( poly );

                    // Create polygon outline
                    unsigned ist = wspoly->GetPolyIndexStart( kk );
                    unsigned iend = wspoly->GetPolyIndexEnd( kk );
                    while( ist <= iend )
                        poly->m_Corners.push_back(
                            wspoly->GetCornerPositionUi( ist++, jj ) );

                }
            }
        }
            break;

        case WORKSHEET_DATAITEM::WS_BITMAP:

            ((WORKSHEET_DATAITEM_BITMAP*)wsItem)->SetPixelScaleFactor();

            for( int jj = 0; jj < wsItem->m_RepeatCount; jj++ )
            {
                if( jj && ! wsItem->IsInsidePage( jj ) )
                    continue;

                Append( new WS_DRAW_ITEM_BITMAP( wsItem,
                    wsItem->GetStartPosUi( jj ) ) );
            }
            break;

        }
    }
}
// Data transfert from item to widgets in properties frame
void PROPERTIES_FRAME::CopyPrmsFromItemToPanel( WORKSHEET_DATAITEM* aItem )
{
    wxString msg;

    // Set parameters common to all WORKSHEET_DATAITEM types
    m_textCtrlType->SetValue( aItem->GetClassName() );
    m_textCtrlComment->SetValue( aItem->m_Info );

    switch( aItem->GetPage1Option() )
    {
    default:
    case 0:
        m_choicePageOpt->SetSelection( 0 );
        break;

    case 1:
        m_choicePageOpt->SetSelection( 1 );
        break;

    case -1:
        m_choicePageOpt->SetSelection( 2 );
        break;
    }

    // Position/ start point
    msg.Printf( wxT("%.3f"), aItem->m_Pos.m_Pos.x );
    m_textCtrlPosX->SetValue( msg );
    msg.Printf( wxT("%.3f"), aItem->m_Pos.m_Pos.y );
    m_textCtrlPosY->SetValue( msg );

    switch(  aItem->m_Pos.m_Anchor )
    {
    case RB_CORNER:      // right bottom corner
        m_comboBoxCornerPos->SetSelection( 2 );
        break;
    case RT_CORNER:      // right top corner
        m_comboBoxCornerPos->SetSelection( 0 );
        break;
    case LB_CORNER:      // left bottom corner
        m_comboBoxCornerPos->SetSelection( 3 );
        break;
    case LT_CORNER:      // left top corner
        m_comboBoxCornerPos->SetSelection( 1 );
        break;
    }

    // End point
    msg.Printf( wxT("%.3f"), aItem->m_End.m_Pos.x );
    m_textCtrlEndX->SetValue( msg );
    msg.Printf( wxT("%.3f"), aItem->m_End.m_Pos.y );
    m_textCtrlEndY->SetValue( msg );

    switch( aItem->m_End.m_Anchor )
    {
    case RB_CORNER:      // right bottom corner
        m_comboBoxCornerEnd->SetSelection( 2 );
        break;
    case RT_CORNER:      // right top corner
        m_comboBoxCornerEnd->SetSelection( 0 );
        break;
    case LB_CORNER:      // left bottom corner
        m_comboBoxCornerEnd->SetSelection( 3 );
        break;
    case LT_CORNER:      // left top corner
        m_comboBoxCornerEnd->SetSelection( 1 );
        break;
    }

    msg.Printf( wxT("%.3f"), aItem->m_LineWidth );
    m_textCtrlThickness->SetValue( msg );

    // Now, set prms more specific to WORKSHEET_DATAITEM types
    // For a given type, disable widgets which are not relevant,
    // and be sure widgets which are relevant are enabled
    if( aItem->GetType() == WORKSHEET_DATAITEM::WS_TEXT )
    {
        m_SizerTextOptions->Show(true);
        m_SizerTextIncrementLabel->Show( true );

        WORKSHEET_DATAITEM_TEXT* item = (WORKSHEET_DATAITEM_TEXT*) aItem;
        item->m_FullText = item->m_TextBase;
        // Replace our '\' 'n' sequence by the EOL char
        item->ReplaceAntiSlashSequence();;
        m_textCtrlText->SetValue( item->m_FullText );

        msg.Printf( wxT("%d"), item->m_IncrementLabel );
        m_textCtrlTextIncrement->SetValue( msg );

        // Rotation (poly and text)
        msg.Printf( wxT("%.3f"), item->m_Orient );
        m_textCtrlRotation->SetValue( msg );

        // Constraints:
        msg.Printf( wxT("%.3f"), item->m_BoundingBoxSize.x );
        m_textCtrlConstraintX->SetValue( msg );
        msg.Printf( wxT("%.3f"), item->m_BoundingBoxSize.y );
        m_textCtrlConstraintY->SetValue( msg );

        // Font Options
        m_checkBoxBold->SetValue( item->IsBold() );
        m_checkBoxItalic->SetValue( item->IsItalic() );
        switch( item->m_Hjustify )
        {
        case GR_TEXT_HJUSTIFY_LEFT:
            m_choiceHjustify->SetSelection( 0 );
            break;
        case GR_TEXT_HJUSTIFY_CENTER:
            m_choiceHjustify->SetSelection( 1 );
            break;
        case GR_TEXT_HJUSTIFY_RIGHT:
            m_choiceHjustify->SetSelection( 2 );
            break;
        }
        switch( item->m_Vjustify )
        {
        case GR_TEXT_VJUSTIFY_TOP:
            m_choiceVjustify->SetSelection( 0 );
            break;
        case GR_TEXT_VJUSTIFY_CENTER:
            m_choiceVjustify->SetSelection( 1 );
            break;
        case GR_TEXT_VJUSTIFY_BOTTOM:
            m_choiceVjustify->SetSelection( 2 );
            break;
        }

        // Text size
        msg.Printf( wxT("%.3f"), item->m_TextSize.x );
        m_textCtrlTextSizeX->SetValue( msg );
        msg.Printf( wxT("%.3f"), item->m_TextSize.y );
        m_textCtrlTextSizeY->SetValue( msg );
    }
    else
    {
        m_SizerTextOptions->Show(false);
        m_SizerTextIncrementLabel->Show(false);
    }

    if( aItem->GetType() == WORKSHEET_DATAITEM::WS_POLYPOLYGON )
    {
        WORKSHEET_DATAITEM_POLYPOLYGON* item = (WORKSHEET_DATAITEM_POLYPOLYGON*) aItem;
        // Rotation (poly and text)
        msg.Printf( wxT("%.3f"), item->m_Orient );
        m_textCtrlRotation->SetValue( msg );
    }

    if( aItem->GetType() == WORKSHEET_DATAITEM::WS_BITMAP )
    {
        WORKSHEET_DATAITEM_BITMAP* item = (WORKSHEET_DATAITEM_BITMAP*) aItem;
        // select definition in PPI
        msg.Printf( wxT("%d"), item->GetPPI() );
        m_textCtrlBitmapPPI->SetValue( msg );
    }

    switch( aItem->GetType() )
    {
    case WORKSHEET_DATAITEM::WS_SEGMENT:
    case WORKSHEET_DATAITEM::WS_RECT:
        m_SizerBitmapPPI->Show( false );
        m_SizerLineThickness->Show( true );
        m_staticTextInfoThickness->Show( true );
        m_SizerRotation->Show( false );
        m_SizerEndPosition->Show(true);
        break;

    case WORKSHEET_DATAITEM::WS_TEXT:
        m_SizerBitmapPPI->Show( false );
        m_SizerLineThickness->Show( true );
        m_staticTextInfoThickness->Show( true );
        m_SizerRotation->Show( true );
        m_SizerEndPosition->Show(false);
        break;

    case WORKSHEET_DATAITEM::WS_POLYPOLYGON:
        m_SizerBitmapPPI->Show( false );
        m_SizerLineThickness->Show( true );
        m_staticTextInfoThickness->Show( false );   // No defaut value for thickness
        m_SizerRotation->Show( true );
        m_SizerEndPosition->Show(false);
        break;

    case WORKSHEET_DATAITEM::WS_BITMAP:
        m_SizerBitmapPPI->Show( true );
        m_SizerLineThickness->Show( false );
        m_SizerRotation->Show( false );
        m_SizerEndPosition->Show(false);
        break;
    }

    // Repeat parameters
    msg.Printf( wxT("%d"), aItem->m_RepeatCount );
    m_textCtrlRepeatCount->SetValue( msg );
    msg.Printf( wxT("%.3f"), aItem->m_IncrementVector.x );
    m_textCtrlStepX->SetValue( msg );
    msg.Printf( wxT("%.3f"), aItem->m_IncrementVector.y );
    m_textCtrlStepY->SetValue( msg );

    // The number of widgets was modified
    m_swItemProperties->Layout();
    m_swItemProperties->Refresh();
}