# Cppcheck report - LibreOffice: extensions/source/scanner/grid.cxx

      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
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     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
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    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
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    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
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    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
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    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
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706

    /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
    /*
     * This file is part of the LibreOffice project.
     *
     * This Source Code Form is subject to the terms of the Mozilla Public
     * License, v. 2.0. If a copy of the MPL was not distributed with this
     * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     *
     * This file incorporates work covered by the following license notice:
     *
     *   Licensed to the Apache Software Foundation (ASF) under one or more
     *   contributor license agreements. See the NOTICE file distributed
     *   with this work for additional information regarding copyright
     *   ownership. The ASF licenses this file to you under the Apache
     *   License, Version 2.0 (the "License"); you may not use this file
     *   except in compliance with the License. You may obtain a copy of
     *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
     */

    #include <sal/config.h>
    #include <osl/thread.h>
    #include <cstdio>
    #include <math.h>
    #include <boost/math/special_functions/expm1.hpp>

    #include <cmath>

    #include <grid.hxx>

    #include <algorithm>
    #include <boost/scoped_array.hpp>

    class GridWindow : public vcl::Window
    {
        // helper class for handles
        struct impHandle
        {
            Point           maPos;
            sal_uInt16      mnOffX;
            sal_uInt16      mnOffY;

            impHandle(const Point& rPos, sal_uInt16 nX, sal_uInt16 nY)
            :   maPos(rPos), mnOffX(nX), mnOffY(nY)
            {
            }

            bool operator<(const impHandle& rComp) const
            {
                return (maPos.X() < rComp.maPos.X());
            }

            void draw(vcl::Window& rWin, const BitmapEx& rBitmapEx)
            {
                const Point aOffset(rWin.PixelToLogic(Point(mnOffX, mnOffY)));
                rWin.DrawBitmapEx(maPos - aOffset, rBitmapEx);
            }

            bool isHit(vcl::Window& rWin, const Point& rPos)
            {
                const Point aOffset(rWin.PixelToLogic(Point(mnOffX, mnOffY)));
                const Rectangle aTarget(maPos - aOffset, maPos + aOffset);
                return aTarget.IsInside(rPos);
            }
        };

        Rectangle       m_aGridArea;

        double          m_fMinX;
        double          m_fMinY;
        double          m_fMaxX;
        double          m_fMaxY;

        double          m_fChunkX;
        double          m_fMinChunkX;
        double          m_fChunkY;
        double          m_fMinChunkY;

        double*         m_pXValues;
        double*         m_pOrigYValues;
        int             m_nValues;
        double*         m_pNewYValues;

        sal_uInt16      m_BmOffX;
        sal_uInt16      m_BmOffY;

        bool            m_bCutValues;

        // stuff for handles
        std::vector< impHandle >    m_aHandles;
        sal_uInt32                  m_nDragIndex;

        BitmapEx        m_aMarkerBitmap;

        Point transform( double x, double y );
        void transform( const Point& rOriginal, double& x, double& y );

        double findMinX();
        double findMinY();
        double findMaxX();
        double findMaxY();

        void drawGrid();
        void drawOriginal();
        void drawNew();
        void drawHandles();

        void computeExtremes();
        void computeChunk( double fMin, double fMax, double& fChunkOut, double& fMinChunkOut );
        void computeNew();
        double interpolate( double x, double* pNodeX, double* pNodeY, int nNodes );

        virtual void MouseMove( const MouseEvent& ) SAL_OVERRIDE;
        virtual void MouseButtonDown( const MouseEvent& ) SAL_OVERRIDE;
        virtual void MouseButtonUp( const MouseEvent& ) SAL_OVERRIDE;
        void onResize();
        virtual void Resize() SAL_OVERRIDE;
        virtual Size GetOptimalSize() const SAL_OVERRIDE;
        void drawLine( double x1, double y1, double x2, double y2 );
    public:
        GridWindow(vcl::Window* pParent);
        void Init(double* pXValues, double* pYValues, int nValues, bool bCutValues, const BitmapEx &rMarkerBitmap);
        virtual ~GridWindow();

        void setBoundings( double fMinX, double fMinY, double fMaxX, double fMaxY );

        double* getNewYValues() { return m_pNewYValues; }

        void ChangeMode(int nType);

        virtual void Paint( const Rectangle& rRect ) SAL_OVERRIDE;
    };

    GridWindow::GridWindow(vcl::Window* pParent)
        : Window(pParent, 0)
        , m_aGridArea(50, 15, 100, 100)
        , m_fMinX(0.0)
        , m_fMinY(0.0)
        , m_fMaxX(0.0)
        , m_fMaxY(0.0)
        , m_fChunkX(0.0)
        , m_fMinChunkX(0.0)
        , m_fChunkY(0.0)
        , m_fMinChunkY(0.0)
        , m_pXValues(NULL)
        , m_pOrigYValues(NULL)
        , m_nValues(0)
        , m_pNewYValues(NULL)
        , m_BmOffX(0)
        , m_BmOffY(0)
        , m_bCutValues(false)
        , m_aHandles()
        , m_nDragIndex(0xffffffff)
    {
        SetMapMode(MapMode(MAP_PIXEL));
    }

    void GridWindow::Init(double* pXValues, double* pYValues, int nValues, bool bCutValues, const BitmapEx &rMarkerBitmap)
    {
        m_aMarkerBitmap = rMarkerBitmap;
        m_pXValues = pXValues;
        m_pOrigYValues = pYValues;
        m_nValues = nValues;
        m_bCutValues = bCutValues;

        SetSizePixel(GetOptimalSize());
        onResize();

        if (m_pOrigYValues && m_nValues)
        {
            m_pNewYValues = new double[ m_nValues ];
            memcpy( m_pNewYValues, m_pOrigYValues, sizeof( double ) * m_nValues );
        }

        setBoundings( 0, 0, 1023, 1023 );
        computeExtremes();

        // create left and right marker as first and last entry
        m_BmOffX = sal_uInt16(m_aMarkerBitmap.GetSizePixel().Width() >> 1);
        m_BmOffY = sal_uInt16(m_aMarkerBitmap.GetSizePixel().Height() >> 1);
        m_aHandles.push_back(impHandle(transform(findMinX(), findMinY()), m_BmOffX, m_BmOffY));
        m_aHandles.push_back(impHandle(transform(findMaxX(), findMaxY()), m_BmOffX, m_BmOffY));
    }

    void GridWindow::Resize()
    {
        onResize();
    }

    void GridWindow::onResize()
    {
        Size aSize = GetSizePixel();
        m_aGridArea.setWidth( aSize.Width() - 80 );
        m_aGridArea.setHeight( aSize.Height() - 40 );
    }

    Size GridWindow::GetOptimalSize() const
    {
        return LogicToPixel(Size(240, 200), MAP_APPFONT);
    }

    GridDialog::GridDialog(double* pXValues, double* pYValues, int nValues, vcl::Window* pParent, bool bCutValues )
        : ModalDialog(pParent, "GridDialog", "modules/scanner/ui/griddialog.ui")
    {
        get(m_pOKButton, "ok");
        get(m_pResetTypeBox, "resetTypeCombobox");
        get(m_pResetButton, "resetButton");
        get(m_pGridWindow, "gridwindow");
        m_pGridWindow->Init(pXValues, pYValues, nValues, bCutValues, get<FixedImage>("handle")->GetImage().GetBitmapEx());

        m_pResetTypeBox->SelectEntryPos( 0 );

        m_pResetButton->SetClickHdl( LINK( this, GridDialog, ClickButtonHdl ) );
    }

    GridWindow::~GridWindow()
    {
        delete [] m_pNewYValues;
    }

    double GridWindow::findMinX()
    {
        if( ! m_pXValues )
            return 0.0;
        double fMin = m_pXValues[0];
        for( int i = 1; i < m_nValues; i++ )
            if( m_pXValues[ i ] < fMin )
                fMin = m_pXValues[ i ];
        return fMin;
    }

    double GridWindow::findMinY()
    {
        if( ! m_pNewYValues )
            return 0.0;
        double fMin = m_pNewYValues[0];
        for( int i = 1; i < m_nValues; i++ )
            if( m_pNewYValues[ i ] < fMin )
                fMin = m_pNewYValues[ i ];
        return fMin;
    }

    double GridWindow::findMaxX()
    {
        if( ! m_pXValues )
            return 0.0;
        double fMax = m_pXValues[0];
        for( int i = 1; i < m_nValues; i++ )
            if( m_pXValues[ i ] > fMax )
                fMax = m_pXValues[ i ];
        return fMax;
    }

    double GridWindow::findMaxY()
    {
        if( ! m_pNewYValues )
            return 0.0;
        double fMax = m_pNewYValues[0];
        for( int i = 1; i < m_nValues; i++ )
            if( m_pNewYValues[ i ] > fMax )
                fMax = m_pNewYValues[ i ];
        return fMax;
    }

    void GridWindow::computeExtremes()
    {
        if( m_nValues && m_pXValues && m_pOrigYValues )
        {
            m_fMaxX = m_fMinX = m_pXValues[0];
            m_fMaxY = m_fMinY = m_pOrigYValues[0];
            for( int i = 1; i < m_nValues; i++ )
            {
                if( m_pXValues[ i ] > m_fMaxX )
                    m_fMaxX = m_pXValues[ i ];
                else if( m_pXValues[ i ] < m_fMinX )
                    m_fMinX = m_pXValues[ i ];
                if( m_pOrigYValues[ i ] > m_fMaxY )
                    m_fMaxY = m_pOrigYValues[ i ];
                else if( m_pOrigYValues[ i ] < m_fMinY )
                    m_fMinY = m_pOrigYValues[ i ];
            }
            setBoundings( m_fMinX, m_fMinY, m_fMaxX, m_fMaxY );
        }
    }

    Point GridWindow::transform( double x, double y )
    {
        Point aRet;

        aRet.X() = (long)( ( x - m_fMinX ) *
            (double)m_aGridArea.GetWidth() / ( m_fMaxX - m_fMinX )
            + m_aGridArea.Left() );
        aRet.Y() = (long)(
            m_aGridArea.Bottom() -
            ( y - m_fMinY ) *
            (double)m_aGridArea.GetHeight() / ( m_fMaxY - m_fMinY ) );
        return aRet;
    }

    void GridWindow::transform( const Point& rOriginal, double& x, double& y )
    {
        const long nWidth = m_aGridArea.GetWidth();
        const long nHeight = m_aGridArea.GetHeight();
        if (!nWidth || !nHeight)
            return;
        x = ( rOriginal.X() - m_aGridArea.Left() ) * (m_fMaxX - m_fMinX) / (double)nWidth + m_fMinX;
        y = ( m_aGridArea.Bottom() - rOriginal.Y() ) * (m_fMaxY - m_fMinY) / (double)nHeight + m_fMinY;
    }

    void GridWindow::drawLine( double x1, double y1, double x2, double y2 )
    {
        DrawLine( transform( x1, y1 ), transform( x2, y2 ) );
    }

    void GridWindow::computeChunk( double fMin, double fMax, double& fChunkOut, double& fMinChunkOut )
    {
        // get a nice chunk size like 10, 100, 25 or such
        fChunkOut = ( fMax - fMin ) / 6.0;
        int logchunk = (int)std::log10( fChunkOut );
        int nChunk = (int)( fChunkOut / std::exp( (double)(logchunk-1) * M_LN10 ) );
        if( nChunk >= 75 )
            nChunk = 100;
        else if( nChunk >= 35 )
            nChunk = 50;
        else if ( nChunk > 20 )
            nChunk = 25;
        else if ( nChunk >= 13 )
            nChunk = 20;
        else if( nChunk > 5 )
            nChunk = 10;
        else
            nChunk = 5;
        fChunkOut = (double) nChunk * exp( (double)(logchunk-1) * M_LN10 );
        // compute whole chunks fitting into fMin
        nChunk = (int)( fMin / fChunkOut );
        fMinChunkOut = (double)nChunk * fChunkOut;
        while( fMinChunkOut < fMin )
            fMinChunkOut += fChunkOut;
    }

    void GridWindow::computeNew()
    {
        if(2L == m_aHandles.size())
        {
            // special case: only left and right markers
            double xleft, yleft;
            double xright, yright;
            transform(m_aHandles[0L].maPos, xleft, yleft);
            transform(m_aHandles[1L].maPos, xright, yright );
            double factor = (yright-yleft)/(xright-xleft);
            for( int i = 0; i < m_nValues; i++ )
            {
                m_pNewYValues[ i ] = yleft + ( m_pXValues[ i ] - xleft )*factor;
            }
        }
        else
        {
            // sort markers
            std::sort(m_aHandles.begin(), m_aHandles.end());
            const int nSorted = m_aHandles.size();
            int i;

            // get node arrays
            boost::scoped_array<double> nodex(new double[ nSorted ]);
            boost::scoped_array<double> nodey(new double[ nSorted ]);

            for( i = 0L; i < nSorted; i++ )
                transform( m_aHandles[i].maPos, nodex[ i ], nodey[ i ] );

            for( i = 0; i < m_nValues; i++ )
            {
                double x = m_pXValues[ i ];
                m_pNewYValues[ i ] = interpolate( x, nodex.get(), nodey.get(), nSorted );
                if( m_bCutValues )
                {
                    if( m_pNewYValues[ i ] > m_fMaxY )
                        m_pNewYValues[ i ] = m_fMaxY;
                    else if( m_pNewYValues[ i ] < m_fMinY )
                        m_pNewYValues[ i ] = m_fMinY;
                }
            }
        }
    }

    double GridWindow::interpolate(
        double x,
        double* pNodeX,
        double* pNodeY,
        int nNodes )
    {
        // compute Lagrange interpolation
        double ret = 0;
        for( int i = 0; i < nNodes; i++ )
        {
            double sum = pNodeY[ i ];
            for( int n = 0; n < nNodes; n++ )
            {
                if( n != i )
                {
                    sum *= x - pNodeX[ n ];
                    sum /= pNodeX[ i ] - pNodeX[ n ];
                }
            }
            ret += sum;
        }
        return ret;
    }

    void GridDialog::setBoundings(double fMinX, double fMinY, double fMaxX, double fMaxY)
    {
        m_pGridWindow->setBoundings(fMinX, fMinY, fMaxX, fMaxY);
    }

    void GridWindow::setBoundings(double fMinX, double fMinY, double fMaxX, double fMaxY)
    {
        m_fMinX = fMinX;
        m_fMinY = fMinY;
        m_fMaxX = fMaxX;
        m_fMaxY = fMaxY;

        computeChunk( m_fMinX, m_fMaxX, m_fChunkX, m_fMinChunkX );
        computeChunk( m_fMinY, m_fMaxY, m_fChunkY, m_fMinChunkY );
    }

    void GridWindow::drawGrid()
    {
        char pBuf[256];
        SetLineColor( Color( COL_BLACK ) );
        // draw vertical lines
        for( double fX = m_fMinChunkX; fX < m_fMaxX; fX += m_fChunkX )
        {
            drawLine( fX, m_fMinY, fX, m_fMaxY );
            // draw tickmarks
            Point aPt = transform( fX, m_fMinY );
            std::sprintf( pBuf, "%g", fX );
            OUString aMark( pBuf, strlen(pBuf), osl_getThreadTextEncoding() );
            Size aTextSize( GetTextWidth( aMark ), GetTextHeight() );
            aPt.X() -= aTextSize.Width()/2;
            aPt.Y() += aTextSize.Height()/2;
            DrawText( aPt, aMark );
        }
        // draw horizontal lines
        for( double fY = m_fMinChunkY; fY < m_fMaxY; fY += m_fChunkY )
        {
            drawLine( m_fMinX, fY, m_fMaxX, fY );
            // draw tickmarks
            Point aPt = transform( m_fMinX, fY );
            std::sprintf( pBuf, "%g", fY );
            OUString aMark( pBuf, strlen(pBuf), osl_getThreadTextEncoding() );
            Size aTextSize( GetTextWidth( aMark ), GetTextHeight() );
            aPt.X() -= aTextSize.Width() + 2;
            aPt.Y() -= aTextSize.Height()/2;
            DrawText( aPt, aMark );
        }

        // draw boundings
        drawLine( m_fMinX, m_fMinY, m_fMaxX, m_fMinY );
        drawLine( m_fMinX, m_fMaxY, m_fMaxX, m_fMaxY );
        drawLine( m_fMinX, m_fMinY, m_fMinX, m_fMaxY );
        drawLine( m_fMaxX, m_fMinY, m_fMaxX, m_fMaxY );
    }

    void GridWindow::drawOriginal()
    {
        if( m_nValues && m_pXValues && m_pOrigYValues )
        {
            SetLineColor( Color( COL_RED ) );
            for( int i = 0; i < m_nValues-1; i++ )
            {
                drawLine( m_pXValues[ i   ], m_pOrigYValues[ i   ],
                          m_pXValues[ i+1 ], m_pOrigYValues[ i+1 ] );
            }
        }
    }

    void GridWindow::drawNew()
    {
        if( m_nValues && m_pXValues && m_pNewYValues )
        {
            SetClipRegion(vcl::Region(m_aGridArea));
            SetLineColor( Color( COL_YELLOW ) );
            for( int i = 0; i < m_nValues-1; i++ )
            {
                drawLine( m_pXValues[ i   ], m_pNewYValues[ i   ],
                          m_pXValues[ i+1 ], m_pNewYValues[ i+1 ] );
            }
            SetClipRegion();
        }
    }

    void GridWindow::drawHandles()
    {
        for(sal_uInt32 i(0L); i < m_aHandles.size(); i++)
        {
            m_aHandles[i].draw(*this, m_aMarkerBitmap);
        }
    }

    void GridWindow::Paint( const Rectangle& rRect )
    {
        Window::Paint(rRect);
        drawGrid();
        drawOriginal();
        drawNew();
        drawHandles();
    }

    void GridWindow::MouseMove( const MouseEvent& rEvt )
    {
        if( rEvt.GetButtons() == MOUSE_LEFT && m_nDragIndex != 0xffffffff )
        {
            Point aPoint( rEvt.GetPosPixel() );

            if( m_nDragIndex == 0L || m_nDragIndex == m_aHandles.size() - 1L)
            {
                aPoint.X() = m_aHandles[m_nDragIndex].maPos.X();
            }
            else
            {
                if(aPoint.X() < m_aGridArea.Left())
                    aPoint.X() = m_aGridArea.Left();
                else if(aPoint.X() > m_aGridArea.Right())
                    aPoint.X() = m_aGridArea.Right();
            }

            if( aPoint.Y() < m_aGridArea.Top() )
                aPoint.Y() = m_aGridArea.Top();
            else if( aPoint.Y() > m_aGridArea.Bottom() )
                aPoint.Y() = m_aGridArea.Bottom();

            if( aPoint != m_aHandles[m_nDragIndex].maPos )
            {
                m_aHandles[m_nDragIndex].maPos = aPoint;
                Invalidate( m_aGridArea );
            }
        }

        Window::MouseMove( rEvt );
    }

    void GridWindow::MouseButtonUp( const MouseEvent& rEvt )
    {
        if( rEvt.GetButtons() == MOUSE_LEFT )
        {
            if( m_nDragIndex != 0xffffffff )
            {
                m_nDragIndex = 0xffffffff;
                computeNew();
                Invalidate( m_aGridArea );
                Paint( m_aGridArea );
            }
        }

        Window::MouseButtonUp( rEvt );
    }

    void GridWindow::MouseButtonDown( const MouseEvent& rEvt )
    {
        Point aPoint( rEvt.GetPosPixel() );
        sal_uInt32 nMarkerIndex = 0xffffffff;

        for(sal_uInt32 a(0L); nMarkerIndex == 0xffffffff && a < m_aHandles.size(); a++)
        {
            if(m_aHandles[a].isHit(*this, aPoint))
            {
                nMarkerIndex = a;
            }
        }

        if( rEvt.GetButtons() == MOUSE_LEFT )
        {
            // user wants to drag a button
            if( nMarkerIndex != 0xffffffff )
            {
                m_nDragIndex = nMarkerIndex;
            }
        }
        else if( rEvt.GetButtons() == MOUSE_RIGHT )
        {
            // user wants to add/delete a button
            if( nMarkerIndex != 0xffffffff )
            {
                if( nMarkerIndex != 0L && nMarkerIndex != m_aHandles.size() - 1L)
                {
                    // delete marker under mouse
                    if( m_nDragIndex == nMarkerIndex )
                        m_nDragIndex = 0xffffffff;

                    m_aHandles.erase(m_aHandles.begin() + nMarkerIndex);
                }
            }
            else
            {
                m_BmOffX = sal_uInt16(m_aMarkerBitmap.GetSizePixel().Width() >> 1);
                m_BmOffY = sal_uInt16(m_aMarkerBitmap.GetSizePixel().Height() >> 1);
                m_aHandles.push_back(impHandle(aPoint, m_BmOffX, m_BmOffY));
            }

            computeNew();
            Invalidate( m_aGridArea );
            Paint( m_aGridArea );
        }

        Window::MouseButtonDown( rEvt );
    }

    void GridWindow::ChangeMode(int nType)
    {
        switch( nType )
        {
            case LINEAR_ASCENDING:
            {
                for( int i = 0; i < m_nValues; i++ )
                {
                    m_pNewYValues[ i ] = m_fMinY + (m_fMaxY-m_fMinY)/(m_fMaxX-m_fMinX)*(m_pXValues[i]-m_fMinX);
                }
            }
            break;
            case LINEAR_DESCENDING:
            {
                for( int i = 0; i < m_nValues; i++ )
                {
                    m_pNewYValues[ i ] = m_fMaxY - (m_fMaxY-m_fMinY)/(m_fMaxX-m_fMinX)*(m_pXValues[i]-m_fMinX);
                }
            }
            break;
            case RESET:
            {
                if( m_pOrigYValues && m_pNewYValues && m_nValues )
                    memcpy( m_pNewYValues, m_pOrigYValues, m_nValues*sizeof(double) );
            }
            break;
            case EXPONENTIAL:
            {
                for( int i = 0; i < m_nValues; i++ )
                {
                    m_pNewYValues[ i ] = m_fMinY + (m_fMaxY-m_fMinY)*(boost::math::expm1((m_pXValues[i]-m_fMinX)/(m_fMaxX-m_fMinX)))/(M_E-1.0);
                }
            }
            break;

            default:
                break;
        }

        if (m_pNewYValues)
        {
            for(sal_uInt32 i(0L); i < m_aHandles.size(); i++)
            {
                // find nearest xvalue
                double x, y;
                transform( m_aHandles[i].maPos, x, y );
                int nIndex = 0;
                double delta = std::fabs( x-m_pXValues[0] );
                for( int n = 1; n < m_nValues; n++ )
                {
                    if( delta > std::fabs( x - m_pXValues[ n ] ) )
                    {
                        delta = std::fabs( x - m_pXValues[ n ] );
                        nIndex = n;
                    }
                }
                if( 0 == i )
                    m_aHandles[i].maPos = transform( m_fMinX, m_pNewYValues[ nIndex ] );
                else if( m_aHandles.size() - 1L == i )
                    m_aHandles[i].maPos = transform( m_fMaxX, m_pNewYValues[ nIndex ] );
                else
                    m_aHandles[i].maPos = transform( m_pXValues[ nIndex ], m_pNewYValues[ nIndex ] );
            }
        }

        Invalidate();
    }

    IMPL_LINK( GridDialog, ClickButtonHdl, Button*, pButton )
    {
        if (pButton == m_pResetButton)
        {
            int nType = m_pResetTypeBox->GetSelectEntryPos();
            m_pGridWindow->ChangeMode(nType);
        }
        return 0;
    }

    double* GridDialog::getNewYValues()
    {
        return m_pGridWindow->getNewYValues();
    }

    extern "C" SAL_DLLPUBLIC_EXPORT vcl::Window* SAL_CALL makeGridWindow(vcl::Window *pParent, VclBuilder::stringmap &)<--- The function 'makeGridWindow' is never used.
    {
        return new GridWindow(pParent);
    }

    /* vim:set shiftwidth=4 softtabstop=4 expandtab: */