block.cpp 6.26 KB
Newer Older
1
/*
2
 * This program source code file is part of KiCad, a free EDA CAD application.
3 4
 *
 * Copyright (C) 1992-2010 <Jean-Pierre Charras>
5
 * Copyright (C) 1992-2010 KiCad Developers, see change_log.txt for contributors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
 */

25 26 27 28 29
/**
 * @file gerbview/block.cpp
 * @brief Block operations: displacement.
 */

30

31 32 33 34 35
#include <fctsys.h>
#include <common.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <gr_basic.h>
36

37 38
#include <gerbview.h>
#include <class_gerber_draw_item.h>
39

40
#include <wx/debug.h>
41 42 43 44

#define BLOCK_COLOR BROWN


45 46
static void DrawMovingBlockOutlines( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
                                     bool erase );
47

48

49
int GERBVIEW_FRAME::ReturnBlockCommand( int key )
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
    int cmd = 0;

    switch( key )
    {
    default:
        cmd = key & 0x255;
        break;

    case 0:
        cmd = BLOCK_MOVE;
        break;

    case GR_KB_SHIFT:
    case GR_KB_CTRL:
    case GR_KB_SHIFTCTRL:
    case GR_KB_ALT:
        break;

    case MOUSE_MIDDLE:
        cmd = BLOCK_ZOOM;
        break;
    }

    return cmd;
75 76 77
}


78
void GERBVIEW_FRAME::HandleBlockPlace( wxDC* DC )
79
{
80
    wxASSERT( m_canvas->IsMouseCaptured() );
81

82
    GetScreen()->m_BlockLocate.SetState( STATE_BLOCK_STOP );
83

84
    switch( GetScreen()->m_BlockLocate.GetCommand() )
85 86
    {
    case BLOCK_MOVE:                /* Move */
87
        if( m_canvas->IsMouseCaptured() )
88
            m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
89

90
        Block_Move( DC );
91
        GetScreen()->m_BlockLocate.ClearItemsList();
92 93
        break;

94
    default:
95
        wxFAIL_MSG( wxT("HandleBlockPlace: Unexpected block command") );
96 97 98
        break;
    }

99
    m_canvas->EndMouseCapture( GetToolId(), m_canvas->GetCurrentCursor(), wxEmptyString, false );
100
    GetScreen()->SetModify();
101
    GetScreen()->ClearBlockCommand();
102

103
    wxASSERT( GetScreen()->m_BlockLocate.GetCount() == 0 );
104 105

    DisplayToolMsg( wxEmptyString );
106 107
}

108

109
bool GERBVIEW_FRAME::HandleBlockEnd( wxDC* DC )
110
{
111 112
    bool nextcmd  = false;
    bool zoom_command = false;
113

114
    if( m_canvas->IsMouseCaptured() )
115

116
        switch( GetScreen()->m_BlockLocate.GetCommand() )
117 118
        {
        case BLOCK_MOVE:            /* Move */
119
            GetScreen()->m_BlockLocate.SetState( STATE_BLOCK_MOVE );
120
            nextcmd = true;
121 122 123
            m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
            m_canvas->SetMouseCaptureCallback( DrawMovingBlockOutlines );
            m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
124 125
            break;

126 127 128 129
        case BLOCK_ZOOM: /* Window Zoom */
            zoom_command = true;
            break;

130
        default:
131
            wxFAIL_MSG( wxT("HandleBlockEnd: Unexpected block command") );
132 133 134
            break;
        }

135
    if( ! nextcmd )
136
    {
137
        GetScreen()->ClearBlockCommand();
138 139
        m_canvas->EndMouseCapture( GetToolId(), m_canvas->GetCurrentCursor(), wxEmptyString,
                                   false );
140 141 142
    }

    if( zoom_command )
143
        Window_Zoom( GetScreen()->m_BlockLocate );
144

145
    return nextcmd ;
146 147 148
}


149
/* Traces the outline of the block structures of a repositioning move
150
 */
151 152
static void DrawMovingBlockOutlines( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPositon,
                                     bool aErase )
153
{
154
    BASE_SCREEN* screen = aPanel->GetScreen();
155

156
    EDA_COLOR_T Color = YELLOW;
157

158
    if( aErase )
159
    {
160 161
        screen->m_BlockLocate.Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode, Color );

162
        if( screen->m_BlockLocate.GetMoveVector().x|| screen->m_BlockLocate.GetMoveVector().y )
163
        {
164 165
            screen->m_BlockLocate.Draw( aPanel,
                                        aDC,
166
                                        screen->m_BlockLocate.GetMoveVector(),
167 168
                                        g_XorMode,
                                        Color );
169 170 171
        }
    }

172
    if( screen->m_BlockLocate.GetState() != STATE_BLOCK_STOP )
173
    {
174 175 176 177
        screen->m_BlockLocate.SetMoveVector( wxPoint( screen->GetCrossHairPosition().x -
                                                      screen->m_BlockLocate.GetRight(),
                                                      screen->GetCrossHairPosition().y -
                                                      screen->m_BlockLocate.GetBottom() ) );
178 179
    }

180 181
    screen->m_BlockLocate.Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode, Color );

182
    if( screen->m_BlockLocate.GetMoveVector().x || screen->m_BlockLocate.GetMoveVector().y )
183
    {
184 185
        screen->m_BlockLocate.Draw( aPanel,
                                    aDC,
186
                                    screen->m_BlockLocate.GetMoveVector(),
187 188
                                    g_XorMode,
                                    Color );
189
    }
190 191 192
}


193
void GERBVIEW_FRAME::Block_Move( wxDC* DC )
194
{
charras's avatar
charras committed
195
    wxPoint delta;
196
    wxPoint oldpos;
197

198
    oldpos = GetScreen()->GetCrossHairPosition();
199
    m_canvas->SetMouseCaptureCallback( NULL );
200

201
    GetScreen()->SetCrossHairPosition( oldpos );
202
    m_canvas->MoveCursorToCrossHair();
203
    GetScreen()->SetModify();
204
    GetScreen()->m_BlockLocate.Normalize();
205

206
    /* Calculate displacement vectors. */
207
    delta = GetScreen()->m_BlockLocate.GetMoveVector();
208

209
    /* Move items in block */
210
    for( GERBER_DRAW_ITEM* item = GetItemsList(); item; item = item->Next() )
211
    {
212
        GERBER_DRAW_ITEM* gerb_item = (GERBER_DRAW_ITEM*) item;
213

214
        if( gerb_item->HitTest( GetScreen()->m_BlockLocate ) )
215
            gerb_item->MoveAB( delta );
216
    }
217

218
    m_canvas->Refresh( true );
219
}