base_struct.cpp 16.7 KB
Newer Older
1 2 3
/**********************************/
/* Basic classes for Kicad:       */
/*      EDA_ITEM                  */
4
/*      EDA_TEXT                  */
5
/**********************************/
plyatov's avatar
plyatov committed
6 7

#include "fctsys.h"
8
#include "gr_basic.h"
plyatov's avatar
plyatov committed
9 10 11
#include "trigo.h"
#include "common.h"
#include "macros.h"
12 13 14 15 16
#include "wxstruct.h"
#include "class_drawpanel.h"
#include "class_base_screen.h"
#include "drawtxt.h"

plyatov's avatar
plyatov committed
17 18

enum textbox {
19
    ID_TEXTBOX_LIST = 8010
plyatov's avatar
plyatov committed
20 21 22
};


23
EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType )
plyatov's avatar
plyatov committed
24
{
25 26
    InitVars();
    m_StructType = idType;
27
    m_Parent     = parent;
plyatov's avatar
plyatov committed
28
}
29

plyatov's avatar
plyatov committed
30

31
EDA_ITEM::EDA_ITEM( KICAD_T idType )
32
{
33 34
    InitVars();
    m_StructType = idType;
plyatov's avatar
plyatov committed
35
}
36

37

38
EDA_ITEM::EDA_ITEM( const EDA_ITEM& base )
39
{
40
    InitVars();
41 42 43 44 45 46 47 48 49 50
    m_StructType = base.m_StructType;
    m_Parent     = base.m_Parent;
    m_Son        = base.m_Son;
    m_Flags      = base.m_Flags;
    m_TimeStamp  = base.m_TimeStamp;
    m_Status     = base.m_Status;
    m_Selected   = base.m_Selected;
}


51
void EDA_ITEM::InitVars()
52
{
53
    m_StructType = TYPE_NOT_INIT;
54 55 56 57 58 59 60 61
    Pnext       = NULL;     // Linked list: Link (next struct)
    Pback       = NULL;     // Linked list: Link (previous struct)
    m_Parent    = NULL;     // Linked list: Link (parent struct)
    m_Son       = NULL;     // Linked list: Link (son struct)
    m_List      = NULL;     // I am not on any list yet
    m_Image     = NULL;     // Link to an image copy for undelete or abort command
    m_Flags     = 0;        // flags for editions and other
    m_TimeStamp = 0;        // Time stamp used for logical links
62
    m_Status    = 0;
63
    m_Selected  = 0;        // Used by block commands, and selective editing
64 65
}

plyatov's avatar
plyatov committed
66

67
void EDA_ITEM::SetModified()
68 69 70 71 72 73 74 75 76
{
    m_Flags |= IS_CHANGED;

    // If this a child object, then the parent modification state also needs to be set.
    if( m_Parent )
        m_Parent->SetModified();
}


77 78 79 80 81 82 83
EDA_ITEM* EDA_ITEM::doClone() const
{
    wxCHECK_MSG( false, NULL, wxT( "doClone not implemented in derived class " ) + GetClass() +
                 wxT( ".  Bad programmer." ) );
}


84 85 86 87
SEARCH_RESULT EDA_ITEM::IterateForward( EDA_ITEM*     listStart,
                                        INSPECTOR*    inspector,
                                        const void*   testData,
                                        const KICAD_T scanTypes[] )
88
{
89
    EDA_ITEM* p = listStart;
90

91 92 93 94 95 96 97 98 99 100 101 102
    for( ; p; p = p->Pnext )
    {
        if( SEARCH_QUIT == p->Visit( inspector, testData, scanTypes ) )
            return SEARCH_QUIT;
    }

    return SEARCH_CONTINUE;
}


// see base_struct.h
// many classes inherit this method, be careful:
103 104
SEARCH_RESULT EDA_ITEM::Visit( INSPECTOR* inspector, const void* testData,
                               const KICAD_T scanTypes[] )
105
{
106
    KICAD_T stype;
107

108
#if 0 && defined(DEBUG)
109
    std::cout << GetClass().mb_str() << ' ';
110
#endif
dickelbeck's avatar
dickelbeck committed
111

112
    for( const KICAD_T* p = scanTypes;  (stype = *p) != EOT;   ++p )
113 114
    {
        // If caller wants to inspect my type
115
        if( stype == Type() )
116 117 118 119 120 121 122 123
        {
            if( SEARCH_QUIT == inspector->Inspect( this, testData ) )
                return SEARCH_QUIT;

            break;
        }
    }

dickelbeck's avatar
dickelbeck committed
124
    return SEARCH_CONTINUE;
125 126
}

127 128 129 130 131 132 133 134 135 136

wxString EDA_ITEM::GetSelectMenuText() const
{
    wxFAIL_MSG( wxT( "GetSelectMenuText() was not overridden for schematic item type " ) +
                GetClass() );

    return wxString( wxT( "Undefined menu text for " ) + GetClass() );
}


137 138 139 140 141 142 143 144 145
bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
{
    wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
                                  GetChars( GetClass() ) ) );

    return false;
}


146
#if defined(DEBUG)
147

148

149
// A function that should have been in wxWidgets
150
std::ostream& operator<<( std::ostream& out, const wxSize& size )
151 152 153 154 155
{
    out << " width=\"" << size.GetWidth() << "\" height=\"" << size.GetHeight() << "\"";
    return out;
}

156

157
// A function that should have been in wxWidgets
158
std::ostream& operator<<( std::ostream& out, const wxPoint& pt )
159 160 161 162 163
{
    out << " x=\"" << pt.x << "\" y=\"" << pt.y << "\"";
    return out;
}

164

165
void EDA_ITEM::Show( int nestLevel, std::ostream& os ) const
166
{
167
    // XML output:
dickelbeck's avatar
dickelbeck committed
168
    wxString s = GetClass();
169

170
    NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << ">"
171 172
                                 << " Need ::Show() override for this class "
                                 << "</" << s.Lower().mb_str() << ">\n";
173 174
}

dickelbeck's avatar
dickelbeck committed
175

176
std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
177
{
178
    for( int i = 0; i<nestLevel; ++i )
179 180 181
        os << "  ";

    // number of spaces here controls indent per nest level
182

183 184 185
    return os;
}

186

187
#endif
188

189

190
/**************************************************/
191
/* EDA_TEXT (basic class, not directly used */
192
/**************************************************/
193
EDA_TEXT::EDA_TEXT( const wxString& text )
plyatov's avatar
plyatov committed
194
{
195 196
    m_Size.x    = m_Size.y = DEFAULT_SIZE_TEXT;  // Width and height of font.
    m_Orient    = 0;                             // Rotation angle in 0.1 degrees.
197
    m_Attributs = 0;
198
    m_Mirror    = false;                         // display mirror if true
199 200
    m_HJustify  = GR_TEXT_HJUSTIFY_CENTER;       // Default horizontal justification is centered.
    m_VJustify  = GR_TEXT_VJUSTIFY_CENTER;       // Default vertical justification is centered.
201 202
    m_Thickness     = 0;                         // thickness
    m_Italic    = false;                         // true = italic shape.
charras's avatar
charras committed
203
    m_Bold      = false;
204
    m_MultilineAllowed = false;                  // Set to true for multiline text.
205
    m_Text = text;
plyatov's avatar
plyatov committed
206 207 208
}


209
EDA_TEXT::EDA_TEXT( const EDA_TEXT& aText )
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
{
    m_Pos = aText.m_Pos;
    m_Size = aText.m_Size;
    m_Orient = aText.m_Orient;
    m_Attributs = aText.m_Attributs;
    m_Mirror = aText.m_Mirror;
    m_HJustify = aText.m_HJustify;
    m_VJustify = aText.m_VJustify;
    m_Thickness = aText.m_Thickness;
    m_Italic = aText.m_Italic;
    m_Bold = aText.m_Bold;
    m_MultilineAllowed = aText.m_MultilineAllowed;
    m_Text = aText.m_Text;
}


226
EDA_TEXT::~EDA_TEXT()
plyatov's avatar
plyatov committed
227 228
{
}
229

230

231
int EDA_TEXT::LenSize( const wxString& aLine ) const
232
{
233
    return ReturnGraphicTextWidth( aLine, m_Size.x, m_Italic, m_Bold );
234 235
}

plyatov's avatar
plyatov committed
236

237
EDA_RECT EDA_TEXT::GetTextBox( int aLine, int aThickness, bool aInvertY ) const
238
{
239
    EDA_RECT       rect;
240 241
    wxPoint        pos;
    wxArrayString* list = NULL;
242 243
    wxString       text = m_Text;
    int            thickness = ( aThickness < 0 ) ? m_Thickness : aThickness;
244 245 246 247

    if( m_MultilineAllowed )
    {
        list = wxStringSplit( m_Text, '\n' );
248

249 250 251
        if ( list->GetCount() )     // GetCount() == 0 for void strings
        {
            if( aLine >= 0 && (aLine < (int)list->GetCount()) )
252
                text = list->Item( aLine );
253
            else
254
                text = list->Item( 0 );
255
        }
256 257 258
    }

    // calculate the H and V size
259
    int    dx = LenSize( text );
260
    int    dy = GetInterline();
261 262 263

    /* Creates bounding box (rectangle) for an horizontal text */
    wxSize textsize = wxSize( dx, dy );
264 265 266 267 268 269

    if( aInvertY )
        rect.SetOrigin( m_Pos.x, -m_Pos.y );
    else
        rect.SetOrigin( m_Pos );

270 271
    // extra dy interval for letters like j and y and ]
    int extra_dy = dy - m_Size.y;
272
    rect.Move( wxPoint( 0, -extra_dy / 2 ) ); // move origin by the half extra interval
273

274
    // for multiline texts and aLine < 0, merge all rectangles
275
    if( m_MultilineAllowed && list && aLine < 0 )
276 277 278
    {
        for( unsigned ii = 1; ii < list->GetCount(); ii++ )
        {
279 280
            text = list->Item( ii );
            dx   = LenSize( text );
281 282 283 284
            textsize.x  = MAX( textsize.x, dx );
            textsize.y += dy;
        }
    }
285

286 287 288 289 290
    delete list;

    rect.SetSize( textsize );

    /* Now, calculate the rect origin, according to text justification
291
     * At this point the rectangle origin is the text origin (m_Pos).
292 293
     * This is true only for left and top text justified texts (using top to bottom Y axis
     * orientation). and must be recalculated for others justifications
294 295 296 297 298
     * also, note the V justification is relative to the first line
     */
    switch( m_HJustify )
    {
    case GR_TEXT_HJUSTIFY_LEFT:
299 300
        if( m_Mirror )
            rect.SetX( rect.GetX() - rect.GetWidth() );
301 302 303 304 305 306 307
        break;

    case GR_TEXT_HJUSTIFY_CENTER:
        rect.SetX( rect.GetX() - (rect.GetWidth() / 2) );
        break;

    case GR_TEXT_HJUSTIFY_RIGHT:
308 309
        if( !m_Mirror )
            rect.SetX( rect.GetX() - rect.GetWidth() );
310 311 312
        break;
    }

313 314
    dy = m_Size.y + thickness;

315 316 317 318 319 320 321 322 323 324
    switch( m_VJustify )
    {
    case GR_TEXT_VJUSTIFY_TOP:
        break;

    case GR_TEXT_VJUSTIFY_CENTER:
        rect.SetY( rect.GetY() - (dy / 2) );
        break;

    case GR_TEXT_VJUSTIFY_BOTTOM:
325
        rect.SetY( rect.GetY() - dy );
326 327 328
        break;
    }

329
    rect.Inflate( thickness / 2 );
330
    rect.Normalize();       // Make h and v sizes always >= 0
331

332 333 334 335
    return rect;
}


336
bool EDA_TEXT::TextHitTest( const wxPoint& aPoint, int aAccuracy ) const
plyatov's avatar
plyatov committed
337
{
338
    EDA_RECT rect = GetTextBox( -1 );   // Get the full text area.
339
    wxPoint location = aPoint;
340

341
    rect.Inflate( aAccuracy );
342
    RotatePoint( &location, m_Pos, -m_Orient );
dickelbeck's avatar
dickelbeck committed
343

344
    return rect.Contains( location );
plyatov's avatar
plyatov committed
345 346
}

347

348
bool EDA_TEXT::TextHitTest( const EDA_RECT& aRect, bool aContains, int aAccuracy ) const
349
{
350
    EDA_RECT rect = aRect;
351 352 353 354

    rect.Inflate( aAccuracy );

    if( aContains )
355
        return rect.Contains( GetTextBox( -1 ) );
356 357

    return rect.Intersects( GetTextBox( -1 ) );
358
}
359

360

361
void EDA_TEXT::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
362 363
                     EDA_Colors aColor, int aDrawMode,
                     GRTraceMode aFillMode, EDA_Colors aAnchor_color )
364
{
365
    if( m_MultilineAllowed )
366 367 368
    {
        wxPoint        pos  = m_Pos;
        wxArrayString* list = wxStringSplit( m_Text, '\n' );
369
        wxPoint        offset;
370

371
        offset.y = GetInterline();
372

373
        RotatePoint( &offset, m_Orient );
374

375 376
        for( unsigned i = 0; i<list->Count(); i++ )
        {
377
            wxString txt = list->Item( i );
378
            DrawOneLineOfText( aPanel,
379 380 381 382
                               aDC,
                               aOffset,
                               aColor,
                               aDrawMode,
383
                               aFillMode,
384
                               i ?  UNSPECIFIED_COLOR : aAnchor_color,
385 386
                               txt,
                               pos );
387 388 389 390 391 392
            pos += offset;
        }

        delete (list);
    }
    else
393
        DrawOneLineOfText( aPanel,
394 395 396 397
                           aDC,
                           aOffset,
                           aColor,
                           aDrawMode,
398
                           aFillMode,
399 400 401
                           aAnchor_color,
                           m_Text,
                           m_Pos );
402
}
dickelbeck's avatar
dickelbeck committed
403 404


405
void EDA_TEXT::DrawOneLineOfText( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
406 407 408 409
                                  const wxPoint& aOffset, EDA_Colors aColor,
                                  int aDrawMode, GRTraceMode aFillMode,
                                  EDA_Colors aAnchor_color,
                                  wxString& aText, wxPoint aPos )
410
{
411
    int width = m_Thickness;
412

413
    if( aFillMode == FILAIRE )
414 415 416 417 418 419 420 421
        width = 0;

    if( aDrawMode != -1 )
        GRSetDrawMode( aDC, aDrawMode );

    /* Draw text anchor, if allowed */
    if( aAnchor_color != UNSPECIFIED_COLOR )
    {
422 423 424

        int anchor_size = aDC->DeviceToLogicalXRel( 2 );

425
        aAnchor_color = (EDA_Colors) ( aAnchor_color & MASKCOLOR );
426

427 428
        int cX = aPos.x + aOffset.x;
        int cY = aPos.y + aOffset.y;
429 430 431 432 433 434 435 436

        GRLine( &aPanel->m_ClipBox, aDC, cX - anchor_size, cY,
                cX + anchor_size, cY, 0, aAnchor_color );

        GRLine( &aPanel->m_ClipBox, aDC, cX, cY - anchor_size,
                cX, cY + anchor_size, 0, aAnchor_color );
    }

437
    if( aFillMode == SKETCH )
438 439 440 441 442 443 444
        width = -width;

    wxSize size = m_Size;

    if( m_Mirror )
        size.x = -size.x;

445
    DrawGraphicText( aPanel, aDC, aOffset + aPos, aColor, aText, m_Orient, size,
charras's avatar
charras committed
446
                     m_HJustify, m_VJustify, width, m_Italic, m_Bold );
447
}
plyatov's avatar
plyatov committed
448

449

450
wxString EDA_TEXT::GetTextStyleName()
451 452
{
    int style = 0;
453

454 455
    if( m_Italic )
        style = 1;
456

457 458
    if( m_Bold )
        style += 2;
459

460 461 462 463 464 465 466 467 468 469
    wxString stylemsg[4] = {
        _("Normal"),
        _("Italic"),
        _("Bold"),
        _("Bold+Italic")
    };

    return stylemsg[style];
}

470

471
/******************/
472
/* Class EDA_RECT */
473 474
/******************/

475
void EDA_RECT::Normalize()
plyatov's avatar
plyatov committed
476
{
477 478 479 480 481
    if( m_Size.y < 0 )
    {
        m_Size.y = -m_Size.y;
        m_Pos.y -= m_Size.y;
    }
482

483 484 485 486 487
    if( m_Size.x < 0 )
    {
        m_Size.x = -m_Size.x;
        m_Pos.x -= m_Size.x;
    }
plyatov's avatar
plyatov committed
488 489 490
}


491
void EDA_RECT::Move( const wxPoint& aMoveVector )
492 493 494 495
{
    m_Pos += aMoveVector;
}

496

497
bool EDA_RECT::Contains( const wxPoint& aPoint ) const
plyatov's avatar
plyatov committed
498
{
499
    wxPoint rel_pos = aPoint - m_Pos;
500
    wxSize size     = m_Size;
501 502 503

    if( size.x < 0 )
    {
504
        size.x    = -size.x;
505
        rel_pos.x += size.x;
506 507 508 509
    }

    if( size.y < 0 )
    {
510
        size.y    = -size.y;
511
        rel_pos.y += size.y;
512
    }
dickelbeck's avatar
dickelbeck committed
513

514
    return (rel_pos.x >= 0) && (rel_pos.y >= 0) && ( rel_pos.y <= size.y) && ( rel_pos.x <= size.x);
515 516
}

517

518 519 520
/*
 * return true if aRect is inside me (or on boundaries)
 */
521
bool EDA_RECT::Contains( const EDA_RECT& aRect ) const
522
{
523
    return Contains( aRect.GetOrigin() ) && Contains( aRect.GetEnd() );
plyatov's avatar
plyatov committed
524 525
}

526

527 528 529 530
/* Intersects
 * test for a common area between 2 rect.
 * return true if at least a common point is found
 */
531
bool EDA_RECT::Intersects( const EDA_RECT& aRect ) const
dickelbeck's avatar
dickelbeck committed
532 533
{
    // this logic taken from wxWidgets' geometry.cpp file:
dickelbeck's avatar
dickelbeck committed
534
    bool rc;
535 536
    EDA_RECT me(*this);
    EDA_RECT rect(aRect);
537 538 539 540 541 542 543 544 545 546 547 548 549 550
    me.Normalize();         // ensure size is >= 0
    rect.Normalize();       // ensure size is >= 0

    // calculate the left common area coordinate:
    int  left   = MAX( me.m_Pos.x, rect.m_Pos.x );
    // calculate the right common area coordinate:
    int  right  = MIN( me.m_Pos.x + m_Size.x, rect.m_Pos.x + rect.m_Size.x );
    // calculate the upper common area coordinate:
    int  top    = MAX( me.m_Pos.y, aRect.m_Pos.y );
    // calculate the lower common area coordinate:
    int  bottom = MIN( me.m_Pos.y + m_Size.y, rect.m_Pos.y + rect.m_Size.y );

    // if a common area exists, it must have a positive (null accepted) size
    if( left <= right && top <= bottom )
dickelbeck's avatar
dickelbeck committed
551 552 553 554 555
        rc = true;
    else
        rc = false;

    return rc;
dickelbeck's avatar
dickelbeck committed
556 557 558
}


559
EDA_RECT& EDA_RECT::Inflate( int aDelta )
560 561
{
    Inflate( aDelta, aDelta );
562
    return *this;
563 564
}

565

566
EDA_RECT& EDA_RECT::Inflate( wxCoord dx, wxCoord dy )
plyatov's avatar
plyatov committed
567
{
568
    if( m_Size.x >= 0 )
569
    {
570 571 572 573 574 575 576 577 578 579 580 581
        if( m_Size.x < -2 * dx )
        {
            // Don't allow deflate to eat more width than we have,
            m_Pos.x += m_Size.x / 2;
            m_Size.x = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.x  -= dx;
            m_Size.x += 2 * dx;
        }
582
    }
583
    else    // size.x < 0:
584
    {
585 586 587 588 589 590 591 592 593 594 595 596
        if( m_Size.x > -2 * dx )
        {
            // Don't allow deflate to eat more width than we have,
            m_Pos.x -= m_Size.x / 2;
            m_Size.x = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.x  += dx;
            m_Size.x -= 2 * dx; // m_Size.x <0: inflate when dx > 0
        }
597 598
    }

599
    if( m_Size.y >= 0 )
600
    {
601 602 603 604 605 606 607 608 609 610 611 612
        if( m_Size.y < -2 * dy )
        {
            // Don't allow deflate to eat more height than we have,
            m_Pos.y += m_Size.y / 2;
            m_Size.y = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.y  -= dy;
            m_Size.y += 2 * dy;
        }
613
    }
614
    else    // size.y < 0:
615
    {
616 617 618 619 620 621 622 623 624 625 626 627
        if( m_Size.y > 2 * dy )
        {
            // Don't allow deflate to eat more height than we have,
            m_Pos.y -= m_Size.y / 2;
            m_Size.y = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.y  += dy;
            m_Size.y -= 2 * dy; // m_Size.y <0: inflate when dy > 0
        }
628
    }
plyatov's avatar
plyatov committed
629 630 631 632 633

    return *this;
}


634
void EDA_RECT::Merge( const EDA_RECT& aRect )
635 636
{
    Normalize();        // ensure width and height >= 0
637
    EDA_RECT rect = aRect;
638
    rect.Normalize();   // ensure width and height >= 0
639
    wxPoint  end = GetEnd();
640
    wxPoint  rect_end = rect.GetEnd();
641

dickelbeck's avatar
dickelbeck committed
642 643
    // Change origin and size in order to contain the given rect
    m_Pos.x = MIN( m_Pos.x, rect.m_Pos.x );
644
    m_Pos.y = MIN( m_Pos.y, rect.m_Pos.y );
645 646
    end.x   = MAX( end.x, rect_end.x );
    end.y   = MAX( end.y, rect_end.y );
647 648
    SetEnd( end );
}
649

650

651
void EDA_RECT::Merge( const wxPoint& aPoint )
652 653 654 655 656 657 658 659 660 661 662
{
    Normalize();        // ensure width and height >= 0

    wxPoint  end = GetEnd();
    // Change origin and size in order to contain the given rect
    m_Pos.x = MIN( m_Pos.x, aPoint.x );
    m_Pos.y = MIN( m_Pos.y, aPoint.y );
    end.x   = MAX( end.x, aPoint.x );
    end.y   = MAX( end.y, aPoint.y );
    SetEnd( end );
}
663 664


665
double EDA_RECT::GetArea() const
666 667 668
{
    return (double) GetWidth() * (double) GetHeight();
}