cairo_gal.cpp 26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
 * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
 *
 * CAIRO_GAL - Graphics Abstraction Layer for Cairo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <wx/dcbuffer.h>
Maciej Suminski's avatar
Maciej Suminski committed
28
#include <wx/image.h>
29 30 31
#include <wx/log.h>

#include <gal/cairo/cairo_gal.h>
32
#include <gal/definitions.h>
33

34 35
#include <limits>

36 37 38 39 40 41 42 43 44
using namespace KiGfx;

CAIRO_GAL::CAIRO_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener,
                      wxEvtHandler* aPaintListener, const wxString& aName ) :
                      wxWindow( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxEXPAND, aName )
{
    // Default values
    fillColor   = COLOR4D( 0, 0, 0, 1 );
    strokeColor = COLOR4D( 1, 1, 1, 1 );
45
    screenSize  = VECTOR2D( aParent->GetSize() );
46 47 48 49 50

    parentWindow  = aParent;
    mouseListener = aMouseListener;
    paintListener = aPaintListener;

51 52 53 54
    isGrouping          = false;
    isInitialized       = false;
    isDeleteSavedPixels = false;
    zoomFactor          = 1.0;
55
    groupCounter        = 0;
56 57 58 59

    SetSize( aParent->GetSize() );

    // Connecting the event handlers
Maciej Suminski's avatar
Maciej Suminski committed
60 61
    Connect( wxEVT_PAINT, wxPaintEventHandler( CAIRO_GAL::onPaint ) );

62
    // Mouse events are skipped to the parent
Maciej Suminski's avatar
Maciej Suminski committed
63 64 65 66 67 68
    Connect( wxEVT_MOTION, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
    Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
    Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
    Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
    Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
    Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
69 70
    Connect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
    Connect( wxEVT_MIDDLE_UP, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
71 72 73
#if defined _WIN32 || defined _WIN64
    Connect( wxEVT_ENTER_WINDOW, wxMouseEventHandler( CAIRO_GAL::skipMouseEvent ) );
#endif
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

    // Initialize the cursor shape
    SetCursorColor( COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
    initCursor( 21 );

    // Allocate memory
    allocateBitmaps();

    // Set grid defaults
    SetGridColor( COLOR4D( 0.5, 0.5, 0.5, 0.3 ) );
    SetCoarseGrid( 10 );
    SetGridLineWidth( 0.5 );
}


CAIRO_GAL::~CAIRO_GAL()
{
91 92
    deinitSurface();

Maciej Suminski's avatar
Maciej Suminski committed
93 94 95
    delete cursorPixels;
    delete cursorPixelsSaved;

96
    ClearCache();
97

Maciej Suminski's avatar
Maciej Suminski committed
98
    deleteBitmaps();
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}


void CAIRO_GAL::onPaint( wxPaintEvent& aEvent )
{
    PostPaint();
}


void CAIRO_GAL::ResizeScreen( int aWidth, int aHeight )
{
    deleteBitmaps();

    screenSize  = VECTOR2D( aWidth, aHeight );

    // Recreate the bitmaps
    allocateBitmaps();

    SetSize( wxSize( aWidth, aHeight ) );
}


void CAIRO_GAL::skipMouseEvent( wxMouseEvent& aEvent )
{
    // Post the mouse event to the event listener registered in constructor, if any
    if( mouseListener )
        wxPostEvent( mouseListener, aEvent );
}


129
void CAIRO_GAL::initSurface()
130
{
131 132 133
    if( isInitialized )
        return;

134 135 136 137 138 139
    // The size of the client area needs to be greater than zero
    clientRectangle = parentWindow->GetClientRect();

    if( clientRectangle.width == 0 || clientRectangle.height == 0 )
        throw EXCEPTION_ZERO_CLIENT_RECTANGLE;

140
    // Create the Cairo surface
141 142 143
    cairoSurface = cairo_image_surface_create_for_data( (unsigned char*) bitmapBuffer,
                                                        CAIRO_FORMAT_RGB24, clientRectangle.width,
                                                        clientRectangle.height, stride );
144 145 146 147 148
    cairoImage = cairo_create ( cairoSurface );
#ifdef __WXDEBUG__
    cairo_status_t status = cairo_status( cairoImage );
    wxASSERT_MSG( status == CAIRO_STATUS_SUCCESS, "Cairo context creation error" );
#endif /* __WXDEBUG__ */
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    // -----------------------------------------------------------------

    cairo_set_antialias( cairoImage, CAIRO_ANTIALIAS_SUBPIXEL );

    // Clear the screen
    ClearScreen();

    // Compute the world <-> screen transformations
    ComputeWorldScreenMatrix();

    cairo_matrix_init( &cairoWorldScreenMatrix, worldScreenMatrix.m_data[0][0],
                       worldScreenMatrix.m_data[1][0], worldScreenMatrix.m_data[0][1],
                       worldScreenMatrix.m_data[1][1], worldScreenMatrix.m_data[0][2],
                       worldScreenMatrix.m_data[1][2] );

    cairo_set_matrix( cairoImage, &cairoWorldScreenMatrix );

    isSetAttributes = false;

    // Start drawing with a new path
    cairo_new_path( cairoImage );
    isElementAdded = true;

173 174
    cairo_set_line_join( cairoImage, CAIRO_LINE_JOIN_ROUND );
    cairo_set_line_cap( cairoImage, CAIRO_LINE_CAP_ROUND );
175 176 177 178

    lineWidth = 0;

    isDeleteSavedPixels = true;
179 180

    isInitialized = true;
181 182 183 184 185
}


void CAIRO_GAL::deinitSurface()
{
186 187 188
    if( !isInitialized )
        return;

189 190 191
    // Destroy Cairo objects
    cairo_destroy( cairoImage );
    cairo_surface_destroy( cairoSurface );
192 193

    isInitialized = false;
194 195 196
}


197 198 199 200 201 202 203 204 205 206 207 208 209 210
unsigned int CAIRO_GAL::getGroupNumber()
{
    wxASSERT_MSG( groups.size() < std::numeric_limits<unsigned int>::max(),
            wxT( "There are no free slots to store a group" ) );

    while( groups.find( groupCounter ) != groups.end() )
    {
        groupCounter++;
    }

    return groupCounter++;
}


211 212 213
void CAIRO_GAL::BeginDrawing() throw( int )
{
    initSurface();
214 215

    cairo_push_group( cairoImage );
216 217 218 219 220 221 222 223
}


void CAIRO_GAL::EndDrawing()
{
    // Force remaining objects to be drawn
    Flush();

224 225 226
    cairo_pop_group_to_source( cairoImage );
    cairo_paint_with_alpha( cairoImage, fillColor.a );

227 228 229 230 231 232 233 234 235
    // This code was taken from the wxCairo example - it's not the most efficient one
    // Here is a good place for optimizations

    // Now translate the raw image data from the format stored
    // by cairo into a format understood by wxImage.
    unsigned char* wxOutputPtr = wxOutput;
    for( size_t count = 0; count < bufferSize; count++ )
    {
        unsigned int value = bitmapBuffer[count];
236 237 238
        *wxOutputPtr++ = (value >> 16) & 0xff;  // Red pixel
        *wxOutputPtr++ = (value >> 8) & 0xff;   // Green pixel
        *wxOutputPtr++ = value & 0xff;          // Blue pixel
239 240
    }

Maciej Suminski's avatar
Maciej Suminski committed
241
    wxImage      img( (int) screenSize.x, (int) screenSize.y, (unsigned char*) wxOutput, true );
242 243 244 245 246
    wxBitmap     bmp( img );
    wxClientDC   client_dc( this );
    wxBufferedDC dc;
    dc.Init( &client_dc, bmp );

247
    deinitSurface();
248 249 250 251 252 253 254 255
}


void CAIRO_GAL::SaveScreen()
{
    // Copy the current bitmap to the backup buffer
    int offset = 0;

Maciej Suminski's avatar
Maciej Suminski committed
256
    for( int j = 0; j < screenSize.y; j++ )
257 258 259 260 261 262 263 264 265 266 267 268 269 270
    {
        for( int i = 0; i < stride; i++ )
        {
            bitmapBufferBackup[offset + i] = bitmapBuffer[offset + i];
            offset += stride;
        }
    }
}


void CAIRO_GAL::RestoreScreen()
{
    int offset = 0;

Maciej Suminski's avatar
Maciej Suminski committed
271
    for( int j = 0; j < screenSize.y; j++ )
272 273 274 275 276 277 278 279 280 281
    {
        for( int i = 0; i < stride; i++ )
        {
            bitmapBuffer[offset + i] = bitmapBufferBackup[offset + i];
            offset += stride;
        }
    }
}


282
void CAIRO_GAL::DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
283 284 285 286 287 288 289
{
    cairo_move_to( cairoImage, aStartPoint.x, aStartPoint.y );
    cairo_line_to( cairoImage, aEndPoint.x, aEndPoint.y );
    isElementAdded = true;
}


290 291 292 293
void CAIRO_GAL::DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint, double aWidth )
{
    if( isFillEnabled )
    {
294
        SetLineWidth( aWidth );
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

        cairo_move_to( cairoImage, (double) aStartPoint.x, (double) aStartPoint.y );
        cairo_line_to( cairoImage, (double) aEndPoint.x, (double) aEndPoint.y );
    }
    else
    {
        VECTOR2D startEndVector = aEndPoint - aStartPoint;
        double   lineAngle      = atan2( startEndVector.y, startEndVector.x );
        double   lineLength     = startEndVector.EuclideanNorm();

        cairo_save( cairoImage );

        cairo_translate( cairoImage, aStartPoint.x, aStartPoint.y );
        cairo_rotate( cairoImage, lineAngle );

        cairo_arc( cairoImage, 0.0, 0.0,        aWidth / 2.0,  M_PI / 2.0, 3.0 * M_PI / 2.0 );
        cairo_arc( cairoImage, lineLength, 0.0, aWidth / 2.0, -M_PI / 2.0, M_PI / 2.0 );

        cairo_move_to( cairoImage, 0.0,        aWidth / 2.0 );
        cairo_line_to( cairoImage, lineLength, aWidth / 2.0 );

        cairo_move_to( cairoImage, 0.0,        -aWidth / 2.0 );
        cairo_line_to( cairoImage, lineLength, -aWidth / 2.0 );

        cairo_restore( cairoImage );

    }

    isElementAdded = true;
}


327
void CAIRO_GAL::DrawCircle( const VECTOR2D& aCenterPoint, double aRadius )
328 329 330 331
{
    // A circle is drawn using an arc
    cairo_new_sub_path( cairoImage );
    cairo_arc( cairoImage, aCenterPoint.x, aCenterPoint.y, aRadius, 0.0, 2 * M_PI );
332

333 334 335 336
    isElementAdded = true;
}


337
void CAIRO_GAL::DrawArc( const VECTOR2D& aCenterPoint, double aRadius, double aStartAngle,
338 339
                         double aEndAngle )
{
340 341
    SWAP( aStartAngle, >, aEndAngle );

342 343
    cairo_new_sub_path( cairoImage );
    cairo_arc( cairoImage, aCenterPoint.x, aCenterPoint.y, aRadius, aStartAngle, aEndAngle );
344

345 346 347 348 349 350 351 352 353
    isElementAdded = true;
}


void CAIRO_GAL::DrawPolyline( std::deque<VECTOR2D>& aPointList )
{
    bool isFirstPoint = true;

    // Iterate over the point list and draw the segments
354
    for( std::deque<VECTOR2D>::const_iterator it = aPointList.begin(); it != aPointList.end(); ++it )
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    {
        if( isFirstPoint )
        {
            cairo_move_to( cairoImage, it->x, it->y );
            isFirstPoint = false;
        }
        else
        {
            cairo_line_to( cairoImage, it->x, it->y );
        }
    }

    isElementAdded = true;
}


void CAIRO_GAL::DrawPolygon( const std::deque<VECTOR2D>& aPointList )
{
    bool isFirstPoint = true;

    // Iterate over the point list and draw the polygon
    for( std::deque<VECTOR2D>::const_iterator it = aPointList.begin(); it != aPointList.end(); ++it )
    {
        if( isFirstPoint )
        {
            cairo_move_to( cairoImage, it->x, it->y );
            isFirstPoint = false;
        }
        else
        {
            cairo_line_to( cairoImage, it->x, it->y );
        }
    }

    isElementAdded = true;
}


393
void CAIRO_GAL::DrawRectangle( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
{
    // Calculate the diagonal points
    VECTOR2D diagonalPointA( aEndPoint.x, aStartPoint.y );
    VECTOR2D diagonalPointB( aStartPoint.x, aEndPoint.y );

    // The path is composed from 4 segments
    cairo_move_to( cairoImage, aStartPoint.x, aStartPoint.y );
    cairo_line_to( cairoImage, diagonalPointA.x, diagonalPointA.y );
    cairo_line_to( cairoImage, aEndPoint.x, aEndPoint.y );
    cairo_line_to( cairoImage, diagonalPointB.x, diagonalPointB.y );
    cairo_close_path( cairoImage );

    isElementAdded = true;
}


410 411
void CAIRO_GAL::DrawCurve( const VECTOR2D& aStartPoint, const VECTOR2D& aControlPointA,
                           const VECTOR2D& aControlPointB, const VECTOR2D& aEndPoint )
412 413 414 415 416 417 418 419 420
{
    cairo_move_to( cairoImage, aStartPoint.x, aStartPoint.y );
    cairo_curve_to( cairoImage, aControlPointA.x, aControlPointA.y, aControlPointB.x,
                    aControlPointB.y, aEndPoint.x, aEndPoint.y );
    cairo_line_to( cairoImage, aEndPoint.x, aEndPoint.y );
    isElementAdded = true;
}


421
void CAIRO_GAL::SetBackgroundColor( const COLOR4D& aColor )
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
{
    backgroundColor = aColor;
}


void CAIRO_GAL::SetIsFill( bool aIsFillEnabled )
{
    storePath();
    isFillEnabled = aIsFillEnabled;

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_SET_FILL;
        groupElement.boolArgument = aIsFillEnabled;
437
        currentGroup->push_back( groupElement );
438 439 440 441 442 443 444 445 446 447 448 449 450 451
    }
}


void CAIRO_GAL::SetIsStroke( bool aIsStrokeEnabled )
{
    storePath();
    isStrokeEnabled = aIsStrokeEnabled;

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_SET_STROKE;
        groupElement.boolArgument = aIsStrokeEnabled;
452
        currentGroup->push_back( groupElement );
453 454 455 456
    }
}


457
void CAIRO_GAL::SetStrokeColor( const COLOR4D& aColor )
458 459 460 461 462 463 464 465 466 467 468 469 470
{
    storePath();

    strokeColor = aColor;

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_SET_STROKECOLOR;
        groupElement.arguments[0] = strokeColor.r;
        groupElement.arguments[1] = strokeColor.g;
        groupElement.arguments[2] = strokeColor.b;
        groupElement.arguments[3] = strokeColor.a;
471
        currentGroup->push_back( groupElement );
472 473 474 475
    }
}


476
void CAIRO_GAL::SetFillColor( const COLOR4D& aColor )
477 478 479 480 481 482 483 484 485 486 487 488
{
    storePath();
    fillColor = aColor;

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_SET_FILLCOLOR;
        groupElement.arguments[0] = fillColor.r;
        groupElement.arguments[1] = fillColor.g;
        groupElement.arguments[2] = fillColor.b;
        groupElement.arguments[3] = fillColor.a;
489
        currentGroup->push_back( groupElement );
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    }
}


void CAIRO_GAL::SetLineWidth( double aLineWidth )
{
    storePath();

    lineWidth = aLineWidth;
    cairo_set_line_width( cairoImage, aLineWidth );

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_SET_LINE_WIDTH;
        groupElement.arguments[0] = aLineWidth;
506
        currentGroup->push_back( groupElement );
507 508 509 510 511 512 513 514 515 516 517 518 519 520
    }
}


void CAIRO_GAL::ClearScreen()
{
    // Clear screen
    cairo_set_source_rgba( cairoImage,
                           backgroundColor.r, backgroundColor.g, backgroundColor.b, 1.0 );
    cairo_rectangle( cairoImage, 0.0, 0.0, screenSize.x, screenSize.y );
    cairo_fill( cairoImage );
}


521 522 523 524
void CAIRO_GAL::SetLayerDepth( double aLayerDepth )
{
    super::SetLayerDepth( aLayerDepth );

525 526 527
    if( isInitialized )
    {
        storePath();
528

529 530
        cairo_pop_group_to_source( cairoImage );
        cairo_paint_with_alpha( cairoImage, fillColor.a );
531

532 533
        cairo_push_group( cairoImage );
    }
534 535 536
}


537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
void CAIRO_GAL::Transform( MATRIX3x3D aTransformation )
{
    cairo_matrix_t cairoTransformation;

    cairo_matrix_init( &cairoTransformation,
                       aTransformation.m_data[0][0],
                       aTransformation.m_data[1][0],
                       aTransformation.m_data[0][1],
                       aTransformation.m_data[1][1],
                       aTransformation.m_data[0][2],
                       aTransformation.m_data[1][2] );

    cairo_transform( cairoImage, &cairoTransformation );
}


void CAIRO_GAL::Rotate( double aAngle )
{
    storePath();

    cairo_rotate( cairoImage, aAngle );

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_ROTATE;
        groupElement.arguments[0] = aAngle;
564
        currentGroup->push_back( groupElement );
565 566 567 568
    }
}


569
void CAIRO_GAL::Translate( const VECTOR2D& aTranslation )
570 571 572 573 574 575 576 577 578 579 580
{
    storePath();

    cairo_translate( cairoImage, aTranslation.x, aTranslation.y );

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_TRANSLATE;
        groupElement.arguments[0] = aTranslation.x;
        groupElement.arguments[1] = aTranslation.y;
581
        currentGroup->push_back( groupElement );
582 583 584 585
    }
}


586
void CAIRO_GAL::Scale( const VECTOR2D& aScale )
587 588 589 590 591 592 593 594 595 596 597
{
    storePath();

    cairo_scale( cairoImage, aScale.x, aScale.y );

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_SCALE;
        groupElement.arguments[0] = aScale.x;
        groupElement.arguments[1] = aScale.y;
598
        currentGroup->push_back( groupElement );
599 600 601 602 603 604 605 606 607 608 609 610 611 612
    }
}


void CAIRO_GAL::Save()
{
    storePath();

    cairo_save( cairoImage );

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_SAVE;
613
        currentGroup->push_back( groupElement );
614 615 616 617 618 619 620 621 622 623 624 625 626 627
    }
}


void CAIRO_GAL::Restore()
{
    storePath();

    cairo_restore( cairoImage );

    if( isGrouping )
    {
        GroupElement groupElement;
        groupElement.command = CMD_RESTORE;
628
        currentGroup->push_back( groupElement );
629 630 631 632 633 634
    }
}


int CAIRO_GAL::BeginGroup()
{
635 636
    initSurface();

637 638 639
    // If the grouping is started: the actual path is stored in the group, when
    // a attribute was changed or when grouping stops with the end group method.
    storePath();
640

641
    Group group;
642 643 644
    int groupNumber = getGroupNumber();
    groups.insert( std::make_pair( groupNumber, group ) );
    currentGroup = &groups[groupNumber];
645
    isGrouping = true;
646 647

    return groupNumber;
648 649 650 651 652 653 654
}


void CAIRO_GAL::EndGroup()
{
    storePath();
    isGrouping = false;
655 656

    deinitSurface();
657 658 659
}


660 661 662 663 664 665 666 667 668
void CAIRO_GAL::ClearCache()
{
    for( int i = groups.size() - 1; i >= 0; --i )
    {
        DeleteGroup( i );
    }
}


669 670 671 672 673
void CAIRO_GAL::DeleteGroup( int aGroupNumber )
{
    storePath();

    // Delete the Cairo paths
674 675
    std::deque<GroupElement>::iterator it, end;
    for( it = groups[aGroupNumber].begin(), end = groups[aGroupNumber].end(); it != end; ++it )
676 677 678
    {
        if( it->command == CMD_FILL_PATH || it->command == CMD_STROKE_PATH )
        {
679
            cairo_path_destroy( it->cairoPath );
680 681 682 683
        }
    }

    // Delete the group
684
    groups.erase( aGroupNumber );
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
}


void CAIRO_GAL::DrawGroup( int aGroupNumber )
{
    // This method implements a small Virtual Machine - all stored commands
    // are executed; nested calling is also possible

    storePath();

    for( Group::iterator it = groups[aGroupNumber].begin();
         it != groups[aGroupNumber].end(); ++it )
    {
        switch( it->command )
        {
        case CMD_SET_FILL:
            isFillEnabled = it->boolArgument;
            break;

        case CMD_SET_STROKE:
            isStrokeEnabled = it->boolArgument;
            break;

        case CMD_SET_FILLCOLOR:
            fillColor = COLOR4D( it->arguments[0], it->arguments[1], it->arguments[2],
                                 it->arguments[3] );
            break;

        case CMD_SET_STROKECOLOR:
            strokeColor = COLOR4D( it->arguments[0], it->arguments[1], it->arguments[2],
                                   it->arguments[3] );
            break;

        case CMD_SET_LINE_WIDTH:
            cairo_set_line_width( cairoImage, it->arguments[0] );
            break;

        case CMD_STROKE_PATH:
723
            cairo_set_source_rgb( cairoImage, strokeColor.r, strokeColor.g, strokeColor.b );
724 725 726 727 728
            cairo_append_path( cairoImage, it->cairoPath );
            cairo_stroke( cairoImage );
            break;

        case CMD_FILL_PATH:
729
            cairo_set_source_rgb( cairoImage, fillColor.r, fillColor.g, fillColor.b );
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
            cairo_append_path( cairoImage, it->cairoPath );
            cairo_fill( cairoImage );
            break;

        case CMD_TRANSFORM:
            cairo_matrix_t matrix;
            cairo_matrix_init( &matrix, it->arguments[0], it->arguments[1], it->arguments[2],
                               it->arguments[3], it->arguments[4], it->arguments[5] );
            cairo_transform( cairoImage, &matrix );
            break;

        case CMD_ROTATE:
            cairo_rotate( cairoImage, it->arguments[0] );
            break;

        case CMD_TRANSLATE:
            cairo_translate( cairoImage, it->arguments[0], it->arguments[1] );
            break;

        case CMD_SCALE:
            cairo_scale( cairoImage, it->arguments[0], it->arguments[1] );
            break;

        case CMD_SAVE:
            cairo_save( cairoImage );
            break;

        case CMD_RESTORE:
            cairo_restore( cairoImage );
            break;

        case CMD_CALL_GROUP:
            DrawGroup( it->intArgument );
            break;
        }
    }
}


769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
void CAIRO_GAL::ChangeGroupColor( int aGroupNumber, const COLOR4D& aNewColor )
{
    storePath();

    for( Group::iterator it = groups[aGroupNumber].begin();
         it != groups[aGroupNumber].end(); ++it )
    {
        if( it->command == CMD_SET_FILLCOLOR || it->command == CMD_SET_STROKECOLOR )
        {
            it->arguments[0] = aNewColor.r;
            it->arguments[1] = aNewColor.g;
            it->arguments[2] = aNewColor.b;
            it->arguments[3] = aNewColor.a;
        }
    }
}


787 788 789 790 791 792 793
void CAIRO_GAL::ChangeGroupDepth( int aGroupNumber, int aDepth )
{
    // Cairo does not have any possibilities to change the depth coordinate of stored items,
    // it depends only on the order of drawing
}


794 795 796 797 798 799 800 801 802 803 804 805 806 807
void CAIRO_GAL::Flush()
{
    storePath();
}


void CAIRO_GAL::ComputeWorldScreenMatrix()
{
    ComputeWorldScale();

    worldScreenMatrix.SetIdentity();

    MATRIX3x3D translation;
    translation.SetIdentity();
808
    translation.SetTranslation( 0.5 * screenSize );
809 810 811

    MATRIX3x3D scale;
    scale.SetIdentity();
812
    scale.SetScale( VECTOR2D( worldScale, worldScale ) );
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

    MATRIX3x3D lookat;
    lookat.SetIdentity();
    lookat.SetTranslation( -lookAtPoint );

    worldScreenMatrix = translation * scale * lookat * worldScreenMatrix;
}


void CAIRO_GAL::storePath()
{
    if( isElementAdded )
    {
        isElementAdded = false;

        if( !isGrouping )
        {
            if( isFillEnabled )
            {
832
                cairo_set_source_rgb( cairoImage, fillColor.r, fillColor.g, fillColor.b );
833 834 835 836 837
                cairo_fill_preserve( cairoImage );
            }

            if( isStrokeEnabled )
            {
838
                cairo_set_source_rgb( cairoImage, strokeColor.r, strokeColor.g, strokeColor.b );
839 840 841 842 843 844 845 846 847 848 849
                cairo_stroke_preserve( cairoImage );
            }
        }
        else
        {
            // Copy the actual path, append it to the global path list
            // then check, if the path needs to be stroked/filled and
            // add this command to the group list;
            if( isStrokeEnabled )
            {
                GroupElement groupElement;
850
                groupElement.cairoPath = cairo_copy_path( cairoImage );
851
                groupElement.command   = CMD_STROKE_PATH;
852
                currentGroup->push_back( groupElement );
853 854 855 856 857
            }

            if( isFillEnabled )
            {
                GroupElement groupElement;
858
                groupElement.cairoPath = cairo_copy_path( cairoImage );
859
                groupElement.command   = CMD_FILL_PATH;
860
                currentGroup->push_back( groupElement );
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
            }
        }

        cairo_new_path( cairoImage );
    }
}


// ---------------
// Cursor handling
// ---------------


void CAIRO_GAL::initCursor( int aCursorSize )
{
    cursorPixels      = new wxBitmap( aCursorSize, aCursorSize );
    cursorPixelsSaved = new wxBitmap( aCursorSize, aCursorSize );
    cursorSize        = aCursorSize;

    wxMemoryDC cursorShape( *cursorPixels );

    cursorShape.SetBackground( *wxTRANSPARENT_BRUSH );
    wxColour color( cursorColor.r * cursorColor.a * 255, cursorColor.g * cursorColor.a * 255,
                    cursorColor.b * cursorColor.a * 255, 255 );
    wxPen    pen = wxPen( color );
    cursorShape.SetPen( pen );
    cursorShape.Clear();

    cursorShape.DrawLine( 0, aCursorSize / 2, aCursorSize, aCursorSize / 2 );
    cursorShape.DrawLine( aCursorSize / 2, 0, aCursorSize / 2, aCursorSize );
}


894
VECTOR2D CAIRO_GAL::ComputeCursorToWorld( const VECTOR2D& aCursorPosition )
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
{
    MATRIX3x3D inverseMatrix = worldScreenMatrix.Inverse();
    VECTOR2D   cursorPositionWorld = inverseMatrix * aCursorPosition;

    return cursorPositionWorld;
}


void CAIRO_GAL::DrawCursor( VECTOR2D aCursorPosition )
{
    if( !IsShownOnScreen() )
        return;

    wxClientDC clientDC( this );
    wxMemoryDC cursorSave( *cursorPixelsSaved );
    wxMemoryDC cursorShape( *cursorPixels );

    // Snap to grid
    VECTOR2D cursorPositionWorld = ComputeCursorToWorld( aCursorPosition );

    cursorPositionWorld.x = round( cursorPositionWorld.x / gridSize.x ) * gridSize.x;
    cursorPositionWorld.y = round( cursorPositionWorld.y / gridSize.y ) * gridSize.y;
    aCursorPosition       = worldScreenMatrix * cursorPositionWorld;
    aCursorPosition       = aCursorPosition - VECTOR2D( cursorSize / 2, cursorSize / 2 );

    if( !isDeleteSavedPixels )
    {
        clientDC.Blit( savedCursorPosition.x, savedCursorPosition.y, cursorSize, cursorSize,
                       &cursorSave, 0, 0 );
    }
    else
    {
        isDeleteSavedPixels = false;
    }

    cursorSave.Blit( 0, 0, cursorSize, cursorSize, &clientDC, aCursorPosition.x,
                     aCursorPosition.y );

    clientDC.Blit( aCursorPosition.x, aCursorPosition.y, cursorSize, cursorSize, &cursorShape, 0,
                   0, wxOR );

    savedCursorPosition.x = (wxCoord) aCursorPosition.x;
    savedCursorPosition.y = (wxCoord) aCursorPosition.y;
}


941
void CAIRO_GAL::DrawGridLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoint )
942 943 944 945 946 947 948 949 950 951
{
    cairo_move_to( cairoImage, aStartPoint.x, aStartPoint.y );
    cairo_line_to( cairoImage, aEndPoint.x, aEndPoint.y );
    cairo_set_source_rgba( cairoImage, gridColor.r, gridColor.g, gridColor.b, gridColor.a );
    cairo_stroke( cairoImage );
}


void CAIRO_GAL::allocateBitmaps()
{
952
    // Create buffer, use the system independent Cairo image backend
953 954 955 956 957
    stride     = cairo_format_stride_for_width( CAIRO_FORMAT_RGB24, screenSize.x );
    bufferSize = stride * screenSize.y;

    bitmapBuffer       	= new unsigned int[bufferSize];
    bitmapBufferBackup 	= new unsigned int[bufferSize];
Maciej Suminski's avatar
Maciej Suminski committed
958
    wxOutput            = new unsigned char[bufferSize * 3];
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
}


void CAIRO_GAL::deleteBitmaps()
{
    delete[] bitmapBuffer;
    delete[] bitmapBufferBackup;
    delete[] wxOutput;
}


bool CAIRO_GAL::Show( bool aShow )
{
    bool s = wxWindow::Show( aShow );

    if( aShow )
        wxWindow::Raise();

    return s;
}