block_libedit.cpp 10.4 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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
 *
 * 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
 */

26 27 28
/**
 * @file block_libedit.cpp
 */
plyatov's avatar
plyatov committed
29

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

#include <general.h>
#include <class_library.h>
#include <libeditframe.h>
plyatov's avatar
plyatov committed
38 39


40 41
static void DrawMovingBlockOutlines( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
                                     bool aErase );
plyatov's avatar
plyatov committed
42

43

44
int LIB_EDIT_FRAME::BlockCommand( int key )
plyatov's avatar
plyatov committed
45
{
46
    int cmd = BLOCK_IDLE;
plyatov's avatar
plyatov committed
47

48 49 50
    switch( key )
    {
    default:
51
        cmd = key & 0xFF;
52
        break;
plyatov's avatar
plyatov committed
53

54
    case -1:
55 56
        cmd = BLOCK_PRESELECT_MOVE;
        break;
plyatov's avatar
plyatov committed
57

58 59 60
    case 0:
        cmd = BLOCK_MOVE;
        break;
plyatov's avatar
plyatov committed
61

62 63 64
    case GR_KB_SHIFT:
        cmd = BLOCK_COPY;
        break;
plyatov's avatar
plyatov committed
65

66 67 68 69
    case GR_KB_ALT:
        cmd = BLOCK_ROTATE;
        break;

70 71 72
    case GR_KB_SHIFTCTRL:
        cmd = BLOCK_DELETE;
        break;
plyatov's avatar
plyatov committed
73

74
    case GR_KB_CTRL:
75
        cmd = BLOCK_MIRROR_Y;
76
        break;
plyatov's avatar
plyatov committed
77

78 79 80 81
    case MOUSE_MIDDLE:
        cmd = BLOCK_ZOOM;
        break;
    }
plyatov's avatar
plyatov committed
82

83
    return cmd;
plyatov's avatar
plyatov committed
84 85 86
}


87
bool LIB_EDIT_FRAME::HandleBlockEnd( wxDC* DC )
plyatov's avatar
plyatov committed
88
{
89
    int ItemCount = 0;
90
    int nextCmd = false;
91
    wxPoint pt;
92

93
    if( GetScreen()->m_BlockLocate.GetCount() )
94
    {
95 96
        BLOCK_STATE_T state     = GetScreen()->m_BlockLocate.GetState();
        BLOCK_COMMAND_T command = GetScreen()->m_BlockLocate.GetCommand();
97
        m_canvas->CallEndMouseCapture( DC );
98 99
        GetScreen()->m_BlockLocate.SetState( state );
        GetScreen()->m_BlockLocate.SetCommand( command );
100
        m_canvas->SetMouseCapture( DrawAndSizingBlockOutlines, AbortBlockCurrentCommand );
101 102
        SetCrossHairPosition( wxPoint( GetScreen()->m_BlockLocate.GetRight(),
                                       GetScreen()->m_BlockLocate.GetBottom() ) );
103
        m_canvas->MoveCursorToCrossHair();
104 105
    }

106
    switch( GetScreen()->m_BlockLocate.GetCommand() )
107 108 109 110 111
    {
    case  BLOCK_IDLE:
        DisplayError( this, wxT( "Error in HandleBlockPLace" ) );
        break;

112
    case BLOCK_DRAG:        // Drag
113
    case BLOCK_DRAG_ITEM:
114 115 116 117
    case BLOCK_MOVE:        // Move
    case BLOCK_COPY:        // Copy
        if( GetCurPart() )
            ItemCount = GetCurPart()->SelectItems( GetScreen()->m_BlockLocate,
118
                                                  m_unit, m_convert,
119
                                                  m_editPinsPerPartOrConvert );
120
        if( ItemCount )
121
        {
122
            nextCmd = true;
123

124
            if( m_canvas->IsMouseCaptured() )
125
            {
126 127 128
                m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
                m_canvas->SetMouseCaptureCallback( DrawMovingBlockOutlines );
                m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
129
            }
130

131
            GetScreen()->m_BlockLocate.SetState( STATE_BLOCK_MOVE );
132
            m_canvas->Refresh( true );
133 134 135
        }
        break;

136
    case BLOCK_PRESELECT_MOVE:     // Move with preselection list
137
        nextCmd = true;
138
        m_canvas->SetMouseCaptureCallback( DrawMovingBlockOutlines );
139
        GetScreen()->m_BlockLocate.SetState( STATE_BLOCK_MOVE );
140 141
        break;

142 143 144
    case BLOCK_DELETE:     // Delete
        if( GetCurPart() )
            ItemCount = GetCurPart()->SelectItems( GetScreen()->m_BlockLocate,
145
                                                  m_unit, m_convert,
146
                                                  m_editPinsPerPartOrConvert );
147
        if( ItemCount )
148
            SaveCopyInUndoList( GetCurPart() );
149

150
        if( GetCurPart() )
151
        {
152
            GetCurPart()->DeleteSelectedItems();
153 154
            OnModify();
        }
155 156
        break;

157
    case BLOCK_SAVE:     // Save
158
    case BLOCK_PASTE:
159
    case BLOCK_FLIP:
160 161
        break;

162 163
    case BLOCK_ROTATE:
    case BLOCK_MIRROR_X:
164
    case BLOCK_MIRROR_Y:
165 166
        if( GetCurPart() )
            ItemCount = GetCurPart()->SelectItems( GetScreen()->m_BlockLocate,
167
                                                  m_unit, m_convert,
168
                                                  m_editPinsPerPartOrConvert );
169
        if( ItemCount )
170
            SaveCopyInUndoList( GetCurPart() );
171

172
        pt = GetScreen()->m_BlockLocate.Centre();
173
        pt = GetNearestGridPosition( pt );
174
        NEGATE( pt.y );
175

176
        if( GetCurPart() )
177 178
        {
            OnModify();
179
            int block_cmd = GetScreen()->m_BlockLocate.GetCommand();
180

181
            if( block_cmd == BLOCK_MIRROR_Y)
182
                GetCurPart()->MirrorSelectedItemsH( pt );
183
            else if( block_cmd == BLOCK_MIRROR_X)
184
                GetCurPart()->MirrorSelectedItemsV( pt );
185
            else if( block_cmd == BLOCK_ROTATE )
186
                GetCurPart()->RotateSelectedItems( pt );
187
        }
188

189 190
        break;

191
    case BLOCK_ZOOM:     // Window Zoom
192
        Window_Zoom( GetScreen()->m_BlockLocate );
193 194 195 196 197 198 199 200 201
        break;

    case BLOCK_ABORT:
        break;

    case BLOCK_SELECT_ITEMS_ONLY:
        break;
    }

202
    if( !nextCmd )
203
    {
204 205
        if( GetScreen()->m_BlockLocate.GetCommand() != BLOCK_SELECT_ITEMS_ONLY &&  GetCurPart() )
            GetCurPart()->ClearSelectedItems();
206

207 208
        GetScreen()->m_BlockLocate.SetState( STATE_NO_BLOCK );
        GetScreen()->m_BlockLocate.SetCommand( BLOCK_IDLE );
209
        GetScreen()->SetCurItem( NULL );
210 211 212
        m_canvas->EndMouseCapture( GetToolId(), m_canvas->GetCurrentCursor(), wxEmptyString,
                                   false );
        m_canvas->Refresh( true );
213 214
    }

215
    return nextCmd;
plyatov's avatar
plyatov committed
216 217 218
}


219
void LIB_EDIT_FRAME::HandleBlockPlace( wxDC* DC )
plyatov's avatar
plyatov committed
220
{
221
    wxPoint pt;
222

223
    if( !m_canvas->IsMouseCaptured() )
224
    {
225
        DisplayError( this, wxT( "HandleBlockPLace : m_mouseCaptureCallback = NULL" ) );
226 227
    }

228
    GetScreen()->m_BlockLocate.SetState( STATE_BLOCK_STOP );
229

230
    switch( GetScreen()->m_BlockLocate.GetCommand() )
231 232 233 234
    {
    case  BLOCK_IDLE:
        break;

235
    case BLOCK_DRAG:                // Drag
236
    case BLOCK_DRAG_ITEM:
237 238
    case BLOCK_MOVE:                // Move
    case BLOCK_PRESELECT_MOVE:      // Move with preselection list
239
        GetScreen()->m_BlockLocate.ClearItemsList();
240

241 242
        if( GetCurPart() )
            SaveCopyInUndoList( GetCurPart() );
243

244
        pt = GetScreen()->m_BlockLocate.GetMoveVector();
245
        pt.y *= -1;
246

247 248
        if( GetCurPart() )
            GetCurPart()->MoveSelectedItems( pt );
249

250
        m_canvas->Refresh( true );
251 252
        break;

253
    case BLOCK_COPY:     // Copy
254
        GetScreen()->m_BlockLocate.ClearItemsList();
255

256 257
        if( GetCurPart() )
            SaveCopyInUndoList( GetCurPart() );
258

259
        pt = GetScreen()->m_BlockLocate.GetMoveVector();
260
        NEGATE( pt.y );
261

262 263
        if( GetCurPart() )
            GetCurPart()->CopySelectedItems( pt );
264

265 266
        break;

267
    case BLOCK_PASTE:       // Paste (recopy the last block saved)
268
        GetScreen()->m_BlockLocate.ClearItemsList();
269 270
        break;

271 272 273
    case BLOCK_ROTATE:      // Invert by popup menu, from block move
    case BLOCK_MIRROR_X:    // Invert by popup menu, from block move
    case BLOCK_MIRROR_Y:    // Invert by popup menu, from block move
274 275
        if( GetCurPart() )
            SaveCopyInUndoList( GetCurPart() );
276

277
        pt = GetScreen()->m_BlockLocate.Centre();
278
        pt = GetNearestGridPosition( pt );
279
        NEGATE( pt.y );
280

281
        if( GetCurPart() )
282
        {
283
            int block_cmd = GetScreen()->m_BlockLocate.GetCommand();
284

285
            if( block_cmd == BLOCK_MIRROR_Y)
286
                GetCurPart()->MirrorSelectedItemsH( pt );
287
            else if( block_cmd == BLOCK_MIRROR_X)
288
                GetCurPart()->MirrorSelectedItemsV( pt );
289
            else if( block_cmd == BLOCK_ROTATE )
290
                GetCurPart()->RotateSelectedItems( pt );
291
        }
292

293 294 295 296 297 298 299 300 301 302
        break;

    case BLOCK_ZOOM:        // Handled by HandleBlockEnd
    case BLOCK_DELETE:
    case BLOCK_SAVE:
    case BLOCK_ABORT:
    default:
        break;
    }

303
    OnModify();
304

305 306
    GetScreen()->m_BlockLocate.SetState( STATE_NO_BLOCK );
    GetScreen()->m_BlockLocate.SetCommand( BLOCK_IDLE );
307
    GetScreen()->SetCurItem( NULL );
308 309
    m_canvas->EndMouseCapture( GetToolId(), m_canvas->GetCurrentCursor(), wxEmptyString, false );
    m_canvas->Refresh( true );
plyatov's avatar
plyatov committed
310 311 312
}


313
/*
314 315
 * Traces the outline of the search block structures
 * The entire block follows the cursor
316
 */
317 318
void DrawMovingBlockOutlines( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
                              bool aErase )
plyatov's avatar
plyatov committed
319
{
320
    BLOCK_SELECTOR* block;
321
    BASE_SCREEN* screen = aPanel->GetScreen();
322
    wxPoint move_offset;
323
    block = &screen->m_BlockLocate;
324

325
    LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) aPanel->GetParent();
326 327
    wxASSERT( parent != NULL );

328
    LIB_PART* component = parent->GetCurPart();
329 330 331 332

    if( component == NULL )
        return;

333 334 335
    int unit = parent->GetUnit();
    int convert = parent->GetConvert();

336
    if( aErase )
337
    {
338
        block->Draw( aPanel, aDC, block->GetMoveVector(), g_XorMode, block->GetColor() );
339

340
        component->Draw( aPanel, aDC, block->GetMoveVector(), unit, convert,
341
                         g_XorMode, UNSPECIFIED_COLOR, DefaultTransform, true, true, true );
342 343
    }

344
    // Repaint new view
345
    block->SetMoveVector( parent->GetCrossHairPosition() - block->GetLastCursorPosition() );
346

347
    GRSetDrawMode( aDC, g_XorMode );
348
    block->Draw( aPanel, aDC, block->GetMoveVector(), g_XorMode, block->GetColor() );
349

350
    component->Draw( aPanel, aDC, block->GetMoveVector(), unit, convert,
351
                     g_XorMode, UNSPECIFIED_COLOR, DefaultTransform, true, true, true );
plyatov's avatar
plyatov committed
352
}