dialog_edit_libentry_fields_in_lib.cpp 25 KB
Newer Older
1 2 3 4 5 6
/*******************************************************************************/
/*	library editor: edition of fields of lib entries (components in libraries) */
/*******************************************************************************/

#include <algorithm>

7
#include "fctsys.h"
8
#include "appl_wxstruct.h"
9
#include "confirm.h"
10
#include "class_drawpanel.h"
11
#include "wxEeschemaStruct.h"
12
#include "id.h"
13

14 15
#include "general.h"
#include "protos.h"
charras's avatar
charras committed
16
#include "libeditframe.h"
17
#include "class_library.h"
18
#include "sch_component.h"
19
#include "sch_field.h"
20
#include "template_fieldnames.h"
jean-pierre charras's avatar
jean-pierre charras committed
21
#include "dialog_helpers.h"
22 23 24 25 26 27

#include "dialog_edit_libentry_fields_in_lib_base.h"

// Local variables:
static int s_SelectedRow;

28 29
#define COLUMN_FIELD_NAME   0
#define COLUMN_TEXT         1
30 31 32 33 34 35

/*****************************************************************************************/
class DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB : public DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB_BASE
/*****************************************************************************************/
{
private:
36 37 38
    LIB_EDIT_FRAME*    m_Parent;
    LIB_COMPONENT*     m_LibEntry;
    bool               m_skipCopyFromPanel;
39

40 41
    /// a copy of the edited component's LIB_FIELDs
    std::vector <LIB_FIELD> m_FieldsBuf;
42 43

public:
44
    DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB( LIB_EDIT_FRAME* aParent, LIB_COMPONENT* aLibEntry );
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    ~DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB();

private:

    // Events handlers:
    void OnInitDialog( wxInitDialogEvent& event );

    void OnListItemDeselected( wxListEvent& event );
    void OnListItemSelected( wxListEvent& event );
    void addFieldButtonHandler( wxCommandEvent& event );
    void deleteFieldButtonHandler( wxCommandEvent& event );
    void moveUpButtonHandler( wxCommandEvent& event );
    void OnCancelButtonClick( wxCommandEvent& event );
    void OnOKButtonClick( wxCommandEvent& event );

    // internal functions:
    void setSelectedFieldNdx( int aFieldNdx );

    int  getSelectedFieldNdx();

    /**
     * Function InitBuffers
     * sets up to edit the given component.
     */
69 70 71 72 73 74 75 76
    void InitBuffers();

    /**
     * Function findField
     * searches m_FieldsBuf and returns a LIB_FIELD with \a aFieldName or NULL if
     * not found.
     */
    LIB_FIELD* findField( const wxString& aFieldName );
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    /**
     * Function copySelectedFieldToPanel
     * sets the values displayed on the panel according to
     * the currently selected field row
     */
    void copySelectedFieldToPanel();

    /**
     * Function copyPanelToSelectedField
     * copies the values displayed on the panel fields to the currently selected field
     * @return bool - true if all fields are OK, else false if the user has put
     *   bad data into a field, and this value can be used to deny a row change.
     */
    bool copyPanelToSelectedField();
92
    void setRowItem( int aFieldNdx, const LIB_FIELD& aField );
93

94 95
    /**
     * Function updateDisplay
96 97 98 99 100
     * update the listbox showing fields, according to the fields texts
     * must be called after a text change in fields, if this change is not an edition
     */
    void updateDisplay( )
    {
101
        for( unsigned ii = MANDATORY_FIELDS;  ii<m_FieldsBuf.size(); ii++ )
102 103
            setRowItem( ii, m_FieldsBuf[ii] );
    }
104 105
};

106

107
void LIB_EDIT_FRAME::InstallFieldsEditorDialog( wxCommandEvent& event )
108
{
109
    if( m_component == NULL )
110 111
        return;

112
    DrawPanel->EndMouseCapture( ID_NO_TOOL_SELECTED, DrawPanel->GetDefaultCursor() );
113

114
    DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB dlg( this, m_component );
115

116 117 118 119
    int abort = dlg.ShowModal();

    if( abort )
        return;
120

121 122
    UpdateAliasSelectList();
    UpdatePartSelectList();
123
    DisplayLibInfos();
124
    Refresh();
125 126 127 128 129
}


/***********************************************************************/
DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB(
130 131
    LIB_EDIT_FRAME* aParent,
    LIB_COMPONENT*  aLibEntry ) :
132
    DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB_BASE( aParent )
133
/***********************************************************************/
134 135 136
{
    m_Parent   = aParent;
    m_LibEntry = aLibEntry;
jean-pierre charras's avatar
jean-pierre charras committed
137 138 139

    GetSizer()->SetSizeHints( this );
    Centre();
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}


/***********************************************************************/
DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::~DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB()
/***********************************************************************/
{
}


/**********************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnInitDialog( wxInitDialogEvent& event )
/**********************************************************************************/
{
    m_skipCopyFromPanel = false;
    wxListItem columnLabel;

    columnLabel.SetImage( -1 );

    columnLabel.SetText( _( "Name" ) );
charras's avatar
charras committed
160
    fieldListCtrl->InsertColumn( COLUMN_FIELD_NAME, columnLabel );
161 162

    columnLabel.SetText( _( "Value" ) );
charras's avatar
charras committed
163
    fieldListCtrl->InsertColumn( COLUMN_TEXT, columnLabel );
164

165
    wxString label = _( "Size" ) + ReturnUnitSymbol( g_UserUnit );
166 167 168 169
    textSizeLabel->SetLabel( label );

    label  = _( "Pos " );
    label += _( "X" );
170
    label += ReturnUnitSymbol( g_UserUnit );
171 172 173 174
    posXLabel->SetLabel( label );

    label  = _( "Pos " );
    label += _( "Y" );
175
    label += ReturnUnitSymbol( g_UserUnit );
176 177 178 179 180
    posYLabel->SetLabel( label );

    InitBuffers();
    copySelectedFieldToPanel();

181
    stdDialogButtonSizerOK->SetDefault();
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
}


/**********************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnListItemDeselected( wxListEvent& event )
/**********************************************************************************/
{
    if( !m_skipCopyFromPanel )
    {
        if( !copyPanelToSelectedField() )
            event.Skip();   // do not go to the next row
    }
}


/**********************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnListItemSelected( wxListEvent& event )
/**********************************************************************************/
{
    // remember the selected row, statically
    s_SelectedRow = event.GetIndex();

    copySelectedFieldToPanel();
}


/***********************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnCancelButtonClick( wxCommandEvent& event )
/***********************************************************************************/
{
    EndModal( 1 );
}


/**********************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnOKButtonClick( wxCommandEvent& event )
/**********************************************************************************/
{
    if( !copyPanelToSelectedField() )
        return;

223 224 225 226 227 228 229
    // test if reference prefix is acceptable
    if( ! SCH_COMPONENT::IsReferenceStringValid( m_FieldsBuf[REFERENCE].m_Text ) )
    {
        DisplayError( NULL, _( "Illegal reference prefix. A reference must start by a letter" ) );
        return;
    }

230 231 232 233 234
    /* Note: this code is now (2010-dec-04) not used, because the value field is no more editable
     * because changing the value is equivalent to create a new component or alias.
     * This is now handled in libedit main frame, and no more in this dialog
     * but this code is not removed, just in case
     */
235 236 237
    /* If a new name entered in the VALUE field, that it not an existing alias name
     * or root alias of the component */
    wxString newvalue = m_FieldsBuf[VALUE].m_Text;
238

239
    if( m_LibEntry->HasAlias( newvalue ) && !m_LibEntry->GetAlias( newvalue )->IsRoot() )
240
    {
241 242
        wxString msg;
        msg.Printf( _( "A new name is entered for this component\n\
243
An alias %s already exists!\nCannot update this component" ),
244
                    GetChars( newvalue ) );
245 246
        DisplayError( this, msg );
        return;
247
    }
248
    /* End unused code */
249 250 251 252

    /* save old cmp in undo list */
    m_Parent->SaveCopyInUndoList( m_LibEntry, IS_CHANGED );

253 254 255
    // delete any fields with no name or no value before we copy all of m_FieldsBuf
    // back into the component
    for( unsigned i = MANDATORY_FIELDS; i < m_FieldsBuf.size(); )
256
    {
257
        if( m_FieldsBuf[i].GetName().IsEmpty() || m_FieldsBuf[i].m_Text.IsEmpty() )
258 259 260 261 262 263 264 265
        {
            m_FieldsBuf.erase( m_FieldsBuf.begin() + i );
            continue;
        }

        ++i;
    }

266
#if defined(DEBUG)
267
    for( unsigned i=0;  i<m_FieldsBuf.size();  ++i )
268 269
    {
        printf( "save[%d].name:'%s' value:'%s'\n", i,
270 271
                TO_UTF8( m_FieldsBuf[i].GetName() ),
                TO_UTF8( m_FieldsBuf[i].m_Text ) );
272 273 274 275
    }
#endif

    // copy all the fields back, fully replacing any previous fields
276 277
    m_LibEntry->SetFields( m_FieldsBuf );

278
    // We need to keep the name and the value the same at the moment!
279
    SetName( m_LibEntry->GetValueField().m_Text );
280

281
    m_Parent->OnModify();
282 283 284 285 286 287 288 289 290 291 292 293

    EndModal( 0 );
}


void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::addFieldButtonHandler( wxCommandEvent& event )
{
    // in case m_FieldsBuf[REFERENCE].m_Orient has changed on screen only, grab
    // screen contents.
    if( !copyPanelToSelectedField() )
        return;

294
    unsigned fieldNdx = m_FieldsBuf.size();
295

296
    LIB_FIELD blank( fieldNdx );
297 298

    m_FieldsBuf.push_back( blank );
299
    m_FieldsBuf[fieldNdx].SetName( TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx ) );
300 301 302 303 304 305 306 307 308 309 310 311

    setRowItem( fieldNdx, m_FieldsBuf[fieldNdx] );

    m_skipCopyFromPanel = true;
    setSelectedFieldNdx( fieldNdx );
    m_skipCopyFromPanel = false;
}


/*****************************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::deleteFieldButtonHandler( wxCommandEvent& event )
/*****************************************************************************************/
charras's avatar
charras committed
312
/* Delete a field.
313
 * MANDATORY_FIELDS cannot be deleted.
charras's avatar
charras committed
314 315 316
 * If a field is empty, it is removed.
 * if not empty, the text is removed.
 */
317 318 319 320 321 322
{
    unsigned fieldNdx = getSelectedFieldNdx();

    if( fieldNdx >= m_FieldsBuf.size() )    // traps the -1 case too
        return;

charras's avatar
charras committed
323
    if( fieldNdx <= VALUE )
324 325 326 327 328
    {
        wxBell();
        return;
    }

329
    m_skipCopyFromPanel = true;
330

charras's avatar
charras committed
331 332 333 334
    if( m_FieldsBuf[fieldNdx].m_Text.IsEmpty() )
    {
        m_FieldsBuf.erase( m_FieldsBuf.begin() + fieldNdx );
        fieldListCtrl->DeleteItem( fieldNdx );
335

charras's avatar
charras committed
336 337 338 339 340 341 342 343
        if( fieldNdx >= m_FieldsBuf.size() )
            --fieldNdx;
    }
    else
    {
        m_FieldsBuf[fieldNdx].m_Text.Empty();
        copySelectedFieldToPanel();
    }
344

345 346 347
    updateDisplay( );

    setRowItem( fieldNdx, m_FieldsBuf[fieldNdx] );
348 349 350 351 352 353 354 355 356 357 358 359 360 361
    setSelectedFieldNdx( fieldNdx );
    m_skipCopyFromPanel = false;
}


/*************************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB:: moveUpButtonHandler( wxCommandEvent& event )
/*************************************************************************************/
{
    unsigned fieldNdx = getSelectedFieldNdx();

    if( fieldNdx >= m_FieldsBuf.size() )    // traps the -1 case too
        return;

362
    if( fieldNdx <= MANDATORY_FIELDS )
363 364 365 366 367 368 369 370 371 372
    {
        wxBell();
        return;
    }

    if( !copyPanelToSelectedField() )
        return;

    // swap the fieldNdx field with the one before it, in both the vector
    // and in the fieldListCtrl
373
    LIB_FIELD tmp = m_FieldsBuf[fieldNdx - 1];
374 375 376 377 378 379 380

    m_FieldsBuf[fieldNdx - 1] = m_FieldsBuf[fieldNdx];
    setRowItem( fieldNdx - 1, m_FieldsBuf[fieldNdx] );

    m_FieldsBuf[fieldNdx] = tmp;
    setRowItem( fieldNdx, tmp );

381 382
    updateDisplay( );

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    m_skipCopyFromPanel = true;
    setSelectedFieldNdx( fieldNdx - 1 );
    m_skipCopyFromPanel = false;
}


/****************************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::setSelectedFieldNdx( int aFieldNdx )
/****************************************************************************/
{
    /* deselect old selection, but I think this is done by single selection flag within fieldListCtrl
     *  fieldListCtrl->SetItemState( s_SelectedRow, 0, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
     */

    if( aFieldNdx >= (int) m_FieldsBuf.size() )
        aFieldNdx = m_FieldsBuf.size() - 1;

    if( aFieldNdx < 0 )
        aFieldNdx = 0;

    fieldListCtrl->SetItemState( aFieldNdx, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
    fieldListCtrl->EnsureVisible( aFieldNdx );

    s_SelectedRow = aFieldNdx;
}


int DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::getSelectedFieldNdx()
{
    return s_SelectedRow;
}


416 417 418 419
/**
 * Function findfield
 * searches a LIB_FIELD_LIST for aFieldName.
 */
420
static LIB_FIELD* findfield( const LIB_FIELDS& aList, const wxString& aFieldName )
421
{
422 423 424 425
    const LIB_FIELD*  field = NULL;

    for( unsigned i=0;  i<aList.size();  ++i )
    {
426
        if( aFieldName == aList[i].GetName() )
427 428 429 430 431 432
        {
            field = &aList[i];  // best to avoid casting here.
            break;
        }
    }
    return (LIB_FIELD*) field;  // remove const-ness last
433 434
}

435 436 437 438 439

LIB_FIELD* DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::findField( const wxString& aFieldName )
{
    for( unsigned i=0;  i<m_FieldsBuf.size();  ++i )
    {
440
        if( aFieldName == m_FieldsBuf[i].GetName() )
441 442 443 444 445 446
            return &m_FieldsBuf[i];
    }
    return NULL;
}


447
/***********************************************************/
448
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::InitBuffers()
449 450
/***********************************************************/
{
451
    LIB_FIELDS cmpFields;
452 453

    m_LibEntry->GetFields( cmpFields );
454

455 456 457
#if defined(DEBUG)
    for( unsigned i=0; i<cmpFields.size();  ++i )
    {
458
        printf( "cmpFields[%d].name:%s\n", i, TO_UTF8( cmpFields[i].GetName() ) );
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
    }
#endif

    /*  We have 3 component related field lists to be aware of: 1) UI
        presentation (m_FieldsBuf), 2) fields in component ram copy, and 3)
        fields recorded with component on disk. m_FieldsBuf is the list of UI
        fields, and this list is not the same as the list which is in the
        component, which is also not the same as the list on disk. All 3 lists
        are potentially different. In the UI we choose to preserve the order of
        the first MANDATORY_FIELDS which are sometimes called fixed fields. Then
        we append the template fieldnames in the exact same order as the
        template fieldname editor shows them. Then we append any user defined
        fieldnames which came from the component, and user can modify it during
        editing, but cannot delete or move a fixed field.
    */
474

475
    m_FieldsBuf.clear();
476

477 478 479 480 481
    /*  When this code was written, all field constructors ensured that the
        MANDATORY_FIELDS are all present within a component (in ram only). So we can
        knowingly copy them over in the normal order. Copy only the fixed fields
        at first. Please do not break the field constructors.
    */
482

483 484
    // fixed fields:
    for( int i=0; i<MANDATORY_FIELDS; ++i )
485
    {
486
        D( printf( "add fixed:%s\n", TO_UTF8( cmpFields[i].GetName() ) ); )
487
        m_FieldsBuf.push_back( cmpFields[i] );
488 489
    }

490 491 492 493
    // Add template fieldnames:
    // Now copy in the template fields, in the order that they are present in the
    // template field editor UI.
    const TEMPLATE_FIELDNAMES& tfnames =
494
        ((SCH_EDIT_FRAME*)m_Parent->GetParent())->GetTemplateFieldNames();
495 496 497 498 499 500 501 502 503 504

    for( TEMPLATE_FIELDNAMES::const_iterator it = tfnames.begin();  it!=tfnames.end();  ++it )
    {
        // add a new field unconditionally to the UI only for this template fieldname

        // field id must not be in range 0 - MANDATORY_FIELDS, set before saving to disk
        LIB_FIELD   fld(-1);

        // See if field by same name already exists in component.
        LIB_FIELD* libField = findfield( cmpFields, it->m_Name );
505

506 507 508 509 510
        // If the field does not already exist in the component, then we
        // use defaults from the template fieldname, otherwise the original
        // values from the component will be set.
        if( !libField )
        {
511
            D( printf( "add template:%s\n", TO_UTF8( it->m_Name ) ); )
512

Dick Hollenbeck's avatar
Dick Hollenbeck committed
513
            fld.SetName( it->m_Name );
514 515 516 517 518 519 520 521 522
            fld.m_Text = it->m_Value;   // empty? ok too.

            if( !it->m_Visible )
                fld.m_Attributs |= TEXT_NO_VISIBLE;
            else
                fld.m_Attributs &= ~TEXT_NO_VISIBLE;
        }
        else
        {
523
            D( printf( "match template:%s\n", TO_UTF8( libField->GetName() ) ); )
524 525 526 527 528
            fld = *libField;    // copy values from component, m_Name too
        }

        m_FieldsBuf.push_back( fld );
    }
529

530 531 532
    // Lastly, append any original fields from the component which were not added
    // from the set of fixed fields nor from the set of template fields.
    for( unsigned i=MANDATORY_FIELDS;  i<cmpFields.size();  ++i )
533
    {
534
        LIB_FIELD*  cmp = &cmpFields[i];
535
        LIB_FIELD*  buf = findField( cmp->GetName() );
536 537 538

        if( !buf )
        {
539
            D( printf( "add cmp:%s\n", TO_UTF8( cmp->GetName() ) ); )
540 541
            m_FieldsBuf.push_back( *cmp );
        }
542 543
    }

544 545 546 547 548
    /*  field names have become more important than field ids, so we cannot
        mangle the names in the buffer, but can do so in the panel, see elsewhere.
    m_FieldsBuf[VALUE].m_Name << wxT( "/" ) << _( "Chip Name" );
    */

549 550 551 552 553 554 555 556 557
    for( unsigned ii = 0; ii < m_FieldsBuf.size();  ++ii )
    {
        setRowItem( ii, m_FieldsBuf[ii] );
    }

    // put focus on the list ctrl
    fieldListCtrl->SetFocus();

    // resume editing at the last row edited, last time dialog was up.
558 559
    if ( s_SelectedRow < (int) m_FieldsBuf.size() )
        s_SelectedRow = 0;
560

561 562 563 564 565
    setSelectedFieldNdx( s_SelectedRow );
}


/***********************************************************************************************/
566
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::setRowItem( int aFieldNdx, const LIB_FIELD& aField )
567 568 569 570 571 572 573 574 575 576 577
/***********************************************************************************************/
{
    wxASSERT( aFieldNdx >= 0 );

    // insert blanks if aFieldNdx is referencing a "yet to be defined" row
    while( aFieldNdx >= fieldListCtrl->GetItemCount() )
    {
        long ndx = fieldListCtrl->InsertItem( fieldListCtrl->GetItemCount(), wxEmptyString );

        wxASSERT( ndx >= 0 );

charras's avatar
charras committed
578
        fieldListCtrl->SetItem( ndx, COLUMN_TEXT, wxEmptyString );
579 580
    }

581
    fieldListCtrl->SetItem( aFieldNdx, COLUMN_FIELD_NAME, aField.GetName() );
charras's avatar
charras committed
582
    fieldListCtrl->SetItem( aFieldNdx, COLUMN_TEXT, aField.m_Text );
583 584

    // recompute the column widths here, after setting texts
charras's avatar
charras committed
585 586
    fieldListCtrl->SetColumnWidth( COLUMN_FIELD_NAME, wxLIST_AUTOSIZE );
    fieldListCtrl->SetColumnWidth( COLUMN_TEXT, wxLIST_AUTOSIZE );
587 588 589 590 591 592 593 594 595 596 597 598
}


/****************************************************************/
void DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::copySelectedFieldToPanel()
/****************************************************************/
{
    unsigned fieldNdx = getSelectedFieldNdx();

    if( fieldNdx >= m_FieldsBuf.size() )    // traps the -1 case too
        return;

599
    LIB_FIELD& field = m_FieldsBuf[fieldNdx];
600 601 602 603 604 605 606 607

    showCheckBox->SetValue( !(field.m_Attributs & TEXT_NO_VISIBLE) );

    rotateCheckBox->SetValue( field.m_Orient == TEXT_ORIENT_VERT );

    int style = 0;
    if( field.m_Italic )
        style = 1;
608

charras's avatar
charras committed
609
    if( field.m_Bold )
610
        style |= 2;
611

612 613
    m_StyleRadioBox->SetSelection( style );

614
    // Select the right text justification
615 616 617 618 619 620 621
    if( field.m_HJustify == GR_TEXT_HJUSTIFY_LEFT )
        m_FieldHJustifyCtrl->SetSelection(0);
    else if( field.m_HJustify == GR_TEXT_HJUSTIFY_RIGHT )
        m_FieldHJustifyCtrl->SetSelection(2);
    else
        m_FieldHJustifyCtrl->SetSelection(1);

622 623 624 625 626 627 628
    if( field.m_VJustify == GR_TEXT_VJUSTIFY_BOTTOM )
        m_FieldVJustifyCtrl->SetSelection(0);
    else if( field.m_VJustify == GR_TEXT_VJUSTIFY_TOP )
        m_FieldVJustifyCtrl->SetSelection(2);
    else
        m_FieldVJustifyCtrl->SetSelection(1);

629 630 631

    // Field names have become more important than field ids, so we cannot
    // mangle the names in the buffer but we can do so in the panel.
632 633 634 635 636 637 638
    if( field.GetId() == VALUE )
    {
        // This field is the lib name and the default value when loading this component in
        // schematic.  The value is now not editable here (in this dialog) because changing
        // it is equivalent to create a new component or alias. This is handles in libedir,
        // not in this dialog.
        fieldNameTextCtrl->SetValue( field.GetName() + wxT( " / " ) + _( "Chip Name" ) );
639 640
        fieldValueTextCtrl->Enable( false );
    }
641
    else
642 643
    {
        fieldValueTextCtrl->Enable( true );
644
        fieldNameTextCtrl->SetValue( field.GetName() );
645
    }
646

647
    // if fieldNdx == REFERENCE, VALUE, FOOTPRINT, or DATASHEET, then disable field name editing
648 649 650 651 652 653 654
    fieldNameTextCtrl->Enable(  fieldNdx >= MANDATORY_FIELDS );
    fieldNameTextCtrl->SetEditable( fieldNdx >= MANDATORY_FIELDS );

    // only user defined fields may be moved, and not the top most user defined
    // field since it would be moving up into the fixed fields, > not >=
    moveUpButton->Enable( fieldNdx > MANDATORY_FIELDS );

charras's avatar
charras committed
655 656
    // if fieldNdx == REFERENCE, VALUE, then disable delete button
    deleteFieldButton->Enable( fieldNdx > VALUE );
657 658 659

    fieldValueTextCtrl->SetValue( field.m_Text );

660 661
    textSizeTextCtrl->SetValue( EDA_GRAPHIC_TEXT_CTRL::FormatSize( EESCHEMA_INTERNAL_UNIT,
                                                                   g_UserUnit, field.m_Size.x ) );
662 663 664 665 666 667 668 669 670 671 672 673

    wxPoint coord = field.m_Pos;
    wxPoint zero;

    // If the field value is empty and the position is at relative zero, we set the
    // initial position as a small offset from the ref field, and orient
    // it the same as the ref field.  That is likely to put it at least
    // close to the desired position.
    if( coord == zero && field.m_Text.IsEmpty() )
    {
        rotateCheckBox->SetValue( m_FieldsBuf[REFERENCE].m_Orient == TEXT_ORIENT_VERT );

674 675
        coord.x = m_FieldsBuf[REFERENCE].m_Pos.x + (fieldNdx - MANDATORY_FIELDS + 1) * 100;
        coord.y = m_FieldsBuf[REFERENCE].m_Pos.y + (fieldNdx - MANDATORY_FIELDS + 1) * 100;
676

677
        // coord can compute negative if field is < MANDATORY_FIELDS, e.g. FOOTPRINT.
678 679 680 681
        // That is ok, we basically don't want all the new empty fields on
        // top of each other.
    }

682
    wxString coordText = ReturnStringFromValue( g_UserUnit, coord.x, EESCHEMA_INTERNAL_UNIT );
683 684 685 686 687
    posXTextCtrl->SetValue( coordText );

    // Note: the Y axis for components in lib is from bottom to top
    // and the screen axis is top to bottom: we must change the y coord sign for editing
    NEGATE( coord.y );
688
    coordText = ReturnStringFromValue( g_UserUnit, coord.y, EESCHEMA_INTERNAL_UNIT );
689 690 691 692 693 694 695 696 697 698 699 700 701
    posYTextCtrl->SetValue( coordText );
}


/*****************************************************************/
bool DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::copyPanelToSelectedField()
/*****************************************************************/
{
    unsigned fieldNdx = getSelectedFieldNdx();

    if( fieldNdx >= m_FieldsBuf.size() )        // traps the -1 case too
        return true;

702
    LIB_FIELD& field = m_FieldsBuf[fieldNdx];
703 704 705 706 707 708 709 710 711 712 713

    if( showCheckBox->GetValue() )
        field.m_Attributs &= ~TEXT_NO_VISIBLE;
    else
        field.m_Attributs |= TEXT_NO_VISIBLE;

    if( rotateCheckBox->GetValue() )
        field.m_Orient = TEXT_ORIENT_VERT;
    else
        field.m_Orient = TEXT_ORIENT_HORIZ;

714
    // Copy the text justification
715
    static const GRTextHorizJustifyType hjustify[3] = {
716 717 718
        GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_HJUSTIFY_CENTER,
        GR_TEXT_HJUSTIFY_RIGHT
    };
719

720
    static const GRTextVertJustifyType vjustify[3] = {
721 722 723
        GR_TEXT_VJUSTIFY_BOTTOM, GR_TEXT_VJUSTIFY_CENTER,
        GR_TEXT_VJUSTIFY_TOP
    };
724

725 726 727
    field.m_HJustify = hjustify[m_FieldHJustifyCtrl->GetSelection()];
    field.m_VJustify = vjustify[m_FieldVJustifyCtrl->GetSelection()];

728 729 730
    // Blank/empty field texts for REFERENCE and VALUE are not allowed.
    // (Value is the name of the component in lib!)
    // Change them only if user provided a non blank value
731
    if( !fieldValueTextCtrl->GetValue().IsEmpty() || fieldNdx > VALUE )
732 733
        field.m_Text = fieldValueTextCtrl->GetValue();

734 735
    // FieldNameTextCtrl has a tricked value in it for VALUE index, do not copy it back.
    // It has the "Chip Name" appended.
736
    if( field.GetId() >= MANDATORY_FIELDS )
737 738 739 740 741 742 743
    {
        wxString name = fieldNameTextCtrl->GetValue();
        D( printf("name:%s\n", TO_UTF8( name ) ); )
        field.SetName( name );
    }

    D( printf("setname:%s\n", TO_UTF8( field.GetName() ) ); )
744 745 746

    setRowItem( fieldNdx, field );  // update fieldListCtrl

747 748
    field.m_Size.x = EDA_GRAPHIC_TEXT_CTRL::ParseSize( textSizeTextCtrl->GetValue(),
                                                       EESCHEMA_INTERNAL_UNIT, g_UserUnit );
749

750 751 752 753 754 755 756 757 758
    field.m_Size.y = field.m_Size.x;

    int style = m_StyleRadioBox->GetSelection();
    if( (style & 1 ) != 0 )
        field.m_Italic = true;
    else
        field.m_Italic = false;

    if( (style & 2 ) != 0 )
charras's avatar
charras committed
759
        field.m_Bold = true;
760
    else
761
        field.m_Bold = false;
762

763
    field.m_Pos.x = ReturnValueFromString( g_UserUnit, posXTextCtrl->GetValue(),
764
            EESCHEMA_INTERNAL_UNIT );
765
    field.m_Pos.y = ReturnValueFromString( g_UserUnit, posYTextCtrl->GetValue(),
766
            EESCHEMA_INTERNAL_UNIT );
767

768 769 770 771 772 773
    // Note: the Y axis for components in lib is from bottom to top
    // and the screen axis is top to bottom: we must change the y coord sign for editing
    NEGATE( field.m_Pos.y );

    return true;
}