mirepcb.cpp 8.67 KB
Newer Older
1 2 3 4
/**
 * @file mirepcb.cpp
 * @brief Functions to edit targets (class MIRE).
 */
plyatov's avatar
plyatov committed
5

6 7 8 9 10
#include <fctsys.h>
#include <class_drawpanel.h>
#include <wxPcbStruct.h>
#include <dialog_helpers.h>
#include <gr_basic.h>
plyatov's avatar
plyatov committed
11

12 13
#include <class_board.h>
#include <class_mire.h>
14

15 16
#include <pcbnew.h>
#include <protos.h>
17

plyatov's avatar
plyatov committed
18 19

/* Routines Locales */
20
static void AbortMoveAndEditTarget( EDA_DRAW_PANEL* Panel, wxDC* DC );
21 22 23 24
static void ShowTargetShapeWhileMovingMouse( EDA_DRAW_PANEL* aPanel,
                                             wxDC*           aDC,
                                             const wxPoint&  aPosition,
                                             bool            aErase );
plyatov's avatar
plyatov committed
25

26
/* Local variables : */
27
static int     MireDefaultSize = 5000;
28
static PCB_TARGET s_TargetCopy( NULL ); /* Used to store "old" values of the
29 30 31
                                         * current item parameters before
                                         * edition (used in undo/redo or
                                         * cancel operations)
32
                                         */
plyatov's avatar
plyatov committed
33

34
/************************************/
35
/* class TARGET_PROPERTIES_DIALOG_EDITOR */
36
/************************************/
plyatov's avatar
plyatov committed
37

38
class TARGET_PROPERTIES_DIALOG_EDITOR : public wxDialog
plyatov's avatar
plyatov committed
39 40 41
{
private:

42
    PCB_EDIT_FRAME*   m_Parent;
43
    wxDC*             m_DC;
44
    PCB_TARGET*       m_Target;
45 46
    EDA_VALUE_CTRL*   m_MireWidthCtrl;
    EDA_VALUE_CTRL*   m_MireSizeCtrl;
47
    wxRadioBox*       m_MireShape;
plyatov's avatar
plyatov committed
48

49
public:
50
    TARGET_PROPERTIES_DIALOG_EDITOR( PCB_EDIT_FRAME* parent, PCB_TARGET* Mire, wxDC* DC );
51
    ~TARGET_PROPERTIES_DIALOG_EDITOR() { }
plyatov's avatar
plyatov committed
52 53

private:
54 55
    void OnOkClick( wxCommandEvent& event );
    void OnCancelClick( wxCommandEvent& event );
plyatov's avatar
plyatov committed
56

57
    DECLARE_EVENT_TABLE()
plyatov's avatar
plyatov committed
58 59
};

60 61 62
BEGIN_EVENT_TABLE( TARGET_PROPERTIES_DIALOG_EDITOR, wxDialog )
    EVT_BUTTON( wxID_OK, TARGET_PROPERTIES_DIALOG_EDITOR::OnOkClick )
    EVT_BUTTON( wxID_CANCEL, TARGET_PROPERTIES_DIALOG_EDITOR::OnCancelClick )
plyatov's avatar
plyatov committed
63 64 65
END_EVENT_TABLE()


66
void PCB_EDIT_FRAME::ShowTargetOptionsDialog( PCB_TARGET* aTarget, wxDC* DC )
plyatov's avatar
plyatov committed
67
{
68
    TARGET_PROPERTIES_DIALOG_EDITOR* frame =
69
        new TARGET_PROPERTIES_DIALOG_EDITOR( this, aTarget, DC );
70

71 72
    frame->ShowModal();
    frame->Destroy();
plyatov's avatar
plyatov committed
73 74 75
}


76 77
TARGET_PROPERTIES_DIALOG_EDITOR::TARGET_PROPERTIES_DIALOG_EDITOR( PCB_EDIT_FRAME* parent,
                                                                  PCB_TARGET* aTarget, wxDC* DC ) :
78
    wxDialog( parent, wxID_ANY, wxString( _( "Target Properties" ) ) )
plyatov's avatar
plyatov committed
79
{
80 81 82 83
    wxString  number;
    wxButton* Button;

    m_Parent = parent;
84
    m_DC     = DC;
85 86
    Centre();

87
    m_Target = aTarget;
88 89 90 91 92 93 94 95

    wxBoxSizer* MainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
    SetSizer( MainBoxSizer );
    wxBoxSizer* LeftBoxSizer  = new wxBoxSizer( wxVERTICAL );
    wxBoxSizer* RightBoxSizer = new wxBoxSizer( wxVERTICAL );
    MainBoxSizer->Add( LeftBoxSizer, 0, wxGROW | wxALL, 5 );
    MainBoxSizer->Add( RightBoxSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );

96
    /* Create of the command buttons. */
97
    Button = new wxButton( this, wxID_OK, _( "OK" ) );
98 99
    RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );

100
    Button = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
101 102 103
    RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );

    // Size:
104
    m_MireSizeCtrl = new EDA_VALUE_CTRL( this, _( "Size" ),
Dick Hollenbeck's avatar
Dick Hollenbeck committed
105
                                         m_Target->GetSize(),
106
                                         g_UserUnit, LeftBoxSizer,
107
                                         m_Parent->GetInternalUnits() );
108 109

    // Width:
110
    m_MireWidthCtrl = new EDA_VALUE_CTRL( this, _( "Width" ),
Dick Hollenbeck's avatar
Dick Hollenbeck committed
111
                                          m_Target->GetWidth(),
112
                                          g_UserUnit, LeftBoxSizer,
113
                                          m_Parent->GetInternalUnits() );
114 115 116

    // Shape
    wxString shape_list[2] = { _( "shape +" ), _( "shape X" ) };
charras's avatar
charras committed
117
    m_MireShape = new wxRadioBox( this, wxID_ANY,
dickelbeck's avatar
dickelbeck committed
118
                                  _( "Target Shape:" ),
119 120
                                  wxDefaultPosition, wxSize( -1, -1 ),
                                  2, shape_list, 1 );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
121
    m_MireShape->SetSelection( m_Target->GetShape() ? 1 : 0 );
122 123 124 125
    LeftBoxSizer->Add( m_MireShape, 0, wxGROW | wxALL, 5 );

    GetSizer()->Fit( this );
    GetSizer()->SetSizeHints( this );
plyatov's avatar
plyatov committed
126 127 128
}


129
void TARGET_PROPERTIES_DIALOG_EDITOR::OnCancelClick( wxCommandEvent& event )
plyatov's avatar
plyatov committed
130
{
131
    EndModal( -1 );
plyatov's avatar
plyatov committed
132 133 134
}


135
/* Updates the different parameters for the component being edited
136
 */
137
void TARGET_PROPERTIES_DIALOG_EDITOR::OnOkClick( wxCommandEvent& event )
plyatov's avatar
plyatov committed
138
{
139
    m_Target->Draw( m_Parent->GetCanvas(), m_DC, GR_XOR );
plyatov's avatar
plyatov committed
140

141
    // Save old item in undo list, if is is not currently edited (will be later if so)
142
    if( m_Target->GetFlags() == 0 )
143
        m_Parent->SaveCopyInUndoList( m_Target, UR_CHANGED );
144

145 146 147
    if( m_Target->GetFlags() != 0 )         // other edition in progress (MOVE, NEW ..)
        m_Target->SetFlags( IN_EDIT );      // set flag in edit to force
                                            // undo/redo/abort proper operation
148

Dick Hollenbeck's avatar
Dick Hollenbeck committed
149 150 151 152
    m_Target->SetWidth( m_MireWidthCtrl->GetValue() );
    MireDefaultSize  = m_MireSizeCtrl->GetValue();
    m_Target->SetSize( m_MireSizeCtrl->GetValue() );
    m_Target->SetShape( m_MireShape->GetSelection() ? 1 : 0 );
plyatov's avatar
plyatov committed
153

154
    m_Target->Draw( m_Parent->GetCanvas(), m_DC, ( m_Target->IsMoving() ) ? GR_XOR : GR_OR );
plyatov's avatar
plyatov committed
155

156
    m_Parent->OnModify();
157
    EndModal( 1 );
plyatov's avatar
plyatov committed
158 159
}

160

161
void PCB_EDIT_FRAME::DeleteTarget( PCB_TARGET* aTarget, wxDC* DC )
plyatov's avatar
plyatov committed
162
{
163
    if( aTarget == NULL )
164
        return;
plyatov's avatar
plyatov committed
165

166
    aTarget->Draw( m_canvas, DC, GR_XOR );
167 168
    SaveCopyInUndoList( aTarget, UR_DELETED );
    aTarget->UnLink();
plyatov's avatar
plyatov committed
169 170 171
}


172
static void AbortMoveAndEditTarget( EDA_DRAW_PANEL* Panel, wxDC* DC )
plyatov's avatar
plyatov committed
173
{
174
    BASE_SCREEN* screen  = Panel->GetScreen();
175
    PCB_TARGET*  target = (PCB_TARGET*) screen->GetCurItem();
176

177
    ( (PCB_EDIT_FRAME*) Panel->GetParent() )->SetCurItem( NULL );
178

179 180
    Panel->SetMouseCapture( NULL, NULL );

181
    if( target == NULL )
182 183
        return;

184
    target->Draw( Panel, DC, GR_XOR );
185

186
    if( target->IsNew() )     // If it is new, delete it
187
    {
188 189 190
        target->Draw( Panel, DC, GR_XOR );
        target->DeleteStructure();
        target = NULL;
191
    }
Dick Hollenbeck's avatar
Dick Hollenbeck committed
192
    else    // it is an existing item: retrieve initial values of parameters
193
    {
194
        if( ( target->GetFlags() & (IN_EDIT | IS_MOVED) ) )
195
        {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
196 197 198 199
            target->SetPosition( s_TargetCopy.GetPosition() );
            target->SetWidth(    s_TargetCopy.GetWidth() );
            target->SetSize(     s_TargetCopy.GetSize() );
            target->SetShape(    s_TargetCopy.GetShape() );
200
        }
201
        target->ClearFlags();
202
        target->Draw( Panel, DC, GR_OR );
203
    }
plyatov's avatar
plyatov committed
204 205
}

206

207
/* Draw Symbol PCB type MIRE.
208
 */
209
PCB_TARGET* PCB_EDIT_FRAME::CreateTarget( wxDC* DC )
plyatov's avatar
plyatov committed
210
{
211
    PCB_TARGET* target = new PCB_TARGET( GetBoard() );
plyatov's avatar
plyatov committed
212

213
    target->SetFlags( IS_NEW );
214

215
    GetBoard()->Add( target );
plyatov's avatar
plyatov committed
216

217
    target->SetLayer( EDGE_N );
218
    target->SetWidth( GetDesignSettings().m_EdgeSegmentWidth );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
219
    target->SetSize( MireDefaultSize );
220
    target->SetPosition( m_canvas->GetScreen()->GetCrossHairPosition() );
plyatov's avatar
plyatov committed
221

222
    PlaceTarget( target, DC );
plyatov's avatar
plyatov committed
223

224
    return target;
plyatov's avatar
plyatov committed
225 226 227
}


228
/* Routine to initialize the displacement of a focal
229
 */
230
void PCB_EDIT_FRAME::BeginMoveTarget( PCB_TARGET* aTarget, wxDC* DC )
plyatov's avatar
plyatov committed
231
{
232
    if( aTarget == NULL )
233 234
        return;

235
    s_TargetCopy      = *aTarget;
236
    aTarget->SetFlags( IS_MOVED );
237
    m_canvas->SetMouseCapture( ShowTargetShapeWhileMovingMouse, AbortMoveAndEditTarget );
238
    SetCurItem( aTarget );
plyatov's avatar
plyatov committed
239 240 241
}


242
void PCB_EDIT_FRAME::PlaceTarget( PCB_TARGET* aTarget, wxDC* DC )
plyatov's avatar
plyatov committed
243
{
244
    if( aTarget == NULL )
245
        return;
plyatov's avatar
plyatov committed
246

247 248
    aTarget->Draw( m_canvas, DC, GR_OR );
    m_canvas->SetMouseCapture( NULL, NULL );
249
    SetCurItem( NULL );
250
    OnModify();
251

252
    if( aTarget->IsNew() )
253
    {
254
        SaveCopyInUndoList( aTarget, UR_NEW );
255
        aTarget->ClearFlags();
256 257 258
        return;
    }

259
    if( aTarget->GetFlags() == IS_MOVED )
260
    {
261 262 263
        SaveCopyInUndoList( aTarget, UR_MOVED,
                            aTarget->GetPosition() - s_TargetCopy.GetPosition() );
        aTarget->ClearFlags();
264 265 266
        return;
    }

267
    if( (aTarget->GetFlags() & IN_EDIT) )
268
    {
269 270 271
        SwapData( aTarget, &s_TargetCopy );
        SaveCopyInUndoList( aTarget, UR_CHANGED );
        SwapData( aTarget, &s_TargetCopy );
272
    }
273

274
    aTarget->ClearFlags();
plyatov's avatar
plyatov committed
275 276 277
}


278
/* Redraw the contour of the track while moving the mouse */
279 280
static void ShowTargetShapeWhileMovingMouse( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
                                             const wxPoint& aPosition, bool aErase )
plyatov's avatar
plyatov committed
281
{
282
    BASE_SCREEN* screen  = aPanel->GetScreen();
283
    PCB_TARGET*  target = (PCB_TARGET*) screen->GetCurItem();
plyatov's avatar
plyatov committed
284

285
    if( target == NULL )
286
        return;
plyatov's avatar
plyatov committed
287

288
    if( aErase )
289
        target->Draw( aPanel, aDC, GR_XOR );
plyatov's avatar
plyatov committed
290

Dick Hollenbeck's avatar
Dick Hollenbeck committed
291
    target->SetPosition( screen->GetCrossHairPosition() );
plyatov's avatar
plyatov committed
292

293
    target->Draw( aPanel, aDC, GR_XOR );
plyatov's avatar
plyatov committed
294
}