edtxtmod.cpp 13 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
/*
 * 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) 1992-2011 KiCad Developers, see AUTHORS.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
 */

25 26 27 28
/**
 * @file edtxtmod.cpp
 * @brief Edit module text.
 */
29

30 31 32 33 34 35 36 37
#include <fctsys.h>
#include <gr_basic.h>
#include <common.h>
#include <class_drawpanel.h>
#include <drawtxt.h>
#include <trigo.h>
#include <wxBasePcbFrame.h>
#include <macros.h>
38

39 40
#include <pcbnew.h>
#include <protos.h>
41

42 43 44 45
#include <class_board.h>
#include <class_module.h>
#include <class_text_mod.h>
#include <class_pcb_text.h>
46

47

48 49
static void Show_MoveTexte_Module( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
                                   bool aErase );
50
static void AbortMoveTextModule( EDA_DRAW_PANEL* Panel, wxDC* DC );
51 52


53 54 55 56 57 58
wxPoint        MoveVector;              // Move vector for move edge, exported
                                        // to dialog_edit mod_text.cpp
static wxPoint TextInitialPosition;     // Mouse cursor initial position for
                                        // undo/abort move command
static int     TextInitialOrientation;  // module text initial orientation for
                                        // undo/abort move+rot command+rot
59

60

61
/* Add a new graphical text to the active module (footprint)
62
 *  Note there always are 2 texts: reference and value.
Dick Hollenbeck's avatar
Dick Hollenbeck committed
63
 *  New texts have the member TEXTE_MODULE.GetType() set to TEXT_is_DIVERS
64
 */
65
TEXTE_MODULE* PCB_BASE_FRAME::CreateTextModule( MODULE* Module, wxDC* DC )
66
{
67
    TEXTE_MODULE* Text;
68

69
    Text = new TEXTE_MODULE( Module );
70

71
    /* Add the new text object to the beginning of the draw item list. */
charras's avatar
charras committed
72 73
    if( Module )
        Module->m_Drawings.PushFront( Text );
74

75
    Text->SetFlags( IS_NEW );
76

77
    Text->m_Text = wxT( "text" );
78

79 80 81 82
    GetDesignSettings().m_ModuleTextWidth = Clamp_Text_PenSize( GetDesignSettings().m_ModuleTextWidth,
                                            MIN( GetDesignSettings().m_ModuleTextSize.x, GetDesignSettings().m_ModuleTextSize.y ), true );
    Text->m_Size  = GetDesignSettings().m_ModuleTextSize;
    Text->m_Thickness = GetDesignSettings().m_ModuleTextWidth;
83
    Text->m_Pos   = GetScreen()->GetCrossHairPosition();
84
    Text->SetLocalCoord();
85

charras's avatar
charras committed
86
    InstallTextModOptionsFrame( Text, NULL );
87
    m_canvas->MoveCursorToCrossHair();
88

89
    Text->ClearFlags();
90

charras's avatar
charras committed
91
    if( DC )
92
        Text->Draw( m_canvas, DC, GR_OR );
93

94
    Text->DisplayInfo( this );
95

96
    return Text;
97 98
}

99

100 101
/* Rotate text 90 degrees.
 */
102
void PCB_BASE_FRAME::RotateTextModule( TEXTE_MODULE* Text, wxDC* DC )
103
{
104 105
    if( Text == NULL )
        return;
106

107
    MODULE* module = (MODULE*) Text->GetParent();
108

109
    if( module && module->GetFlags() == 0 && Text->GetFlags() == 0 ) // prepare undo command
110
    {
charras's avatar
charras committed
111
        if( this->m_Ident == PCB_FRAME )
112
            SaveCopyInUndoList( module, UR_CHANGED );
charras's avatar
charras committed
113 114
    }

dickelbeck's avatar
dickelbeck committed
115
    // we expect MoveVector to be (0,0) if there is no move in progress
116
    Text->Draw( m_canvas, DC, GR_XOR, MoveVector );
117

118
    Text->m_Orient += 900;
119

120 121
    while( Text->m_Orient >= 1800 )
        Text->m_Orient -= 1800;
122

123
    Text->Draw( m_canvas, DC, GR_XOR, MoveVector );
124
    Text->DisplayInfo( this );
125

charras's avatar
charras committed
126 127
    if( module )
        module->m_LastEdit_Time = time( NULL );
128

129
    OnModify();
130 131
}

132

133
/*
134
 * Deletes text in module (if not the reference or value)
135
 */
136
void PCB_BASE_FRAME::DeleteTextModule( TEXTE_MODULE* Text )
137
{
138
    MODULE* Module;
139

140 141
    if( Text == NULL )
        return;
142

143
    Module = (MODULE*) Text->GetParent();
144

Dick Hollenbeck's avatar
Dick Hollenbeck committed
145
    if( Text->GetType() == TEXT_is_DIVERS )
146
    {
147
        m_canvas->RefreshDrawingRect( Text->GetBoundingBox() );
dickelbeck's avatar
dickelbeck committed
148
        Text->DeleteStructure();
149
        OnModify();
150 151
        Module->m_LastEdit_Time = time( NULL );
    }
152 153 154 155
}


/*
156 157 158
 * Abort text move in progress.
 *
 * If a text is selected, its initial coordinates are regenerated.
159
 */
160
static void AbortMoveTextModule( EDA_DRAW_PANEL* Panel, wxDC* DC )
161
{
162 163 164
    BASE_SCREEN*  screen = Panel->GetScreen();
    TEXTE_MODULE* Text   = (TEXTE_MODULE*) screen->GetCurItem();
    MODULE*       Module;
165

166 167
    Panel->SetMouseCapture( NULL, NULL );

168 169
    if( Text == NULL )
        return;
170

171
    Module = (MODULE*) Text->GetParent();
dickelbeck's avatar
dickelbeck committed
172

173
    Text->DrawUmbilical( Panel, DC, GR_XOR, -MoveVector );
dickelbeck's avatar
dickelbeck committed
174
    Text->Draw( Panel, DC, GR_XOR, MoveVector );
175

charras's avatar
charras committed
176 177
    // If the text was moved (the move does not change internal data)
    // it could be rotated while moving. So set old value for orientation
178
    if( Text->IsMoving() )
charras's avatar
charras committed
179 180 181
        Text->m_Orient = TextInitialOrientation;

    /* Redraw the text */
182
    Panel->RefreshDrawingRect( Text->GetBoundingBox() );
dickelbeck's avatar
dickelbeck committed
183 184 185

    // leave it at (0,0) so we can use it Rotate when not moving.
    MoveVector.x = MoveVector.y = 0;
186

187 188
    Text->ClearFlags();
    Module->ClearFlags();
189

190
    screen->SetCurItem( NULL );
191 192
}

193

194
/* Start a text move.
195
 */
196
void PCB_BASE_FRAME::StartMoveTexteModule( TEXTE_MODULE* Text, wxDC* DC )
197
{
198 199 200 201
    MODULE* Module;

    if( Text == NULL )
        return;
202

203
    Module = (MODULE*) Text->GetParent();
204

205 206
    Text->SetFlags( IS_MOVED );
    Module->SetFlags( IN_EDIT );
207

208
    MoveVector.x = MoveVector.y = 0;
dickelbeck's avatar
dickelbeck committed
209

210
    TextInitialPosition    = Text->m_Pos;
charras's avatar
charras committed
211
    TextInitialOrientation = Text->m_Orient;
212

213
    // Center cursor on initial position of text
214
    GetScreen()->SetCrossHairPosition( TextInitialPosition );
215
    m_canvas->MoveCursorToCrossHair();
216

217
    Text->DisplayInfo( this );
218

219
    SetCurItem( Text );
220
    m_canvas->SetMouseCapture( Show_MoveTexte_Module, AbortMoveTextModule );
221
    m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
222 223 224
}


225
/* Place the text a the cursor position when the left mouse button is clicked.
226
 */
227
void PCB_BASE_FRAME::PlaceTexteModule( TEXTE_MODULE* Text, wxDC* DC )
228
{
229 230
    if( Text != NULL )
    {
231 232
        m_canvas->RefreshDrawingRect( Text->GetBoundingBox() );
        Text->DrawUmbilical( m_canvas, DC, GR_XOR, -MoveVector );
dickelbeck's avatar
dickelbeck committed
233

234
        /* Update the coordinates for anchor. */
235
        MODULE* Module = (MODULE*) Text->GetParent();
236

237 238
        if( Module )
        {
charras's avatar
charras committed
239
            // Prepare undo command (a rotation can be made while moving)
240
            EXCHG( Text->m_Orient, TextInitialOrientation );
241

charras's avatar
charras committed
242
            if( m_Ident == PCB_FRAME )
243
                SaveCopyInUndoList( Module, UR_CHANGED );
charras's avatar
charras committed
244
            else
245
                SaveCopyInUndoList( Module, UR_MODEDIT );
246

247
            EXCHG( Text->m_Orient, TextInitialOrientation );
charras's avatar
charras committed
248

249
            // Set the new position for text.
250
            Text->m_Pos = GetScreen()->GetCrossHairPosition();
charras's avatar
charras committed
251 252
            wxPoint textRelPos = Text->m_Pos - Module->m_Pos;
            RotatePoint( &textRelPos, -Module->m_Orient );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
253
            Text->SetPos0( textRelPos );
254 255
            Text->ClearFlags();
            Module->ClearFlags();
256
            Module->m_LastEdit_Time = time( NULL );
257
            OnModify();
258

259
            /* Redraw text. */
260
            m_canvas->RefreshDrawingRect( Text->GetBoundingBox() );
261
        }
charras's avatar
charras committed
262
        else
263
        {
264
            Text->m_Pos = GetScreen()->GetCrossHairPosition();
265
        }
266 267
    }

dickelbeck's avatar
dickelbeck committed
268 269 270
    // leave it at (0,0) so we can use it Rotate when not moving.
    MoveVector.x = MoveVector.y = 0;

271
    m_canvas->SetMouseCapture( NULL, NULL );
272 273 274
}


275 276
static void Show_MoveTexte_Module( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
                                   bool aErase )
277
{
278
    BASE_SCREEN*  screen = aPanel->GetScreen();
279
    TEXTE_MODULE* Text   = (TEXTE_MODULE*) screen->GetCurItem();
280

281 282
    if( Text == NULL )
        return;
283

284
    // Erase umbilical and text if necessary
285
    if( aErase )
286
    {
287 288
        Text->DrawUmbilical( aPanel, aDC, GR_XOR, -MoveVector );
        Text->Draw( aPanel, aDC, GR_XOR, MoveVector );
289
    }
290

291
    MoveVector = TextInitialPosition - screen->GetCrossHairPosition();
292

293 294
    // Draw umbilical if text moved
    if( MoveVector.x || MoveVector.y )
295 296
        Text->DrawUmbilical( aPanel, aDC, GR_XOR, -MoveVector );

297
    // Redraw text
298
    Text->Draw( aPanel, aDC, GR_XOR, MoveVector );
299
}
300

301
void PCB_BASE_FRAME::ResetTextSize( BOARD_ITEM* aItem, wxDC* aDC )
302 303 304
{
    wxSize newSize;
    int newThickness;
305 306
    TEXTE_PCB* pcbText = NULL;
    TEXTE_MODULE* moduleText = NULL;
307
    EDA_TEXT* text;
308 309 310

    switch( aItem->Type() )
    {
311
    case PCB_TEXT_T:
312 313
        newSize = GetDesignSettings().m_PcbTextSize;
        newThickness = GetDesignSettings().m_PcbTextWidth;
314
        pcbText = (TEXTE_PCB*) aItem;
315
        text = (EDA_TEXT*) pcbText;
316
        break;
317

318
    case PCB_MODULE_TEXT_T:
319 320
        newSize = GetDesignSettings().m_ModuleTextSize;
        newThickness = GetDesignSettings().m_ModuleTextWidth;
321
        moduleText = (TEXTE_MODULE*) aItem;
322
        text = (EDA_TEXT*) moduleText;
323
        break;
324

325 326 327 328 329 330 331
    default:
        // Exit if aItem is not a text field
        return;
        break;
    }

    // Exit if there's nothing to do
332
    if( text->GetSize() == newSize && text->GetThickness() == newThickness )
333 334 335 336 337
        return;

    // Push item to undo list
    switch( aItem->Type() )
    {
338
    case PCB_TEXT_T:
339 340
        SaveCopyInUndoList( pcbText, UR_CHANGED );
        break;
341

342
    case PCB_MODULE_TEXT_T:
343 344
        SaveCopyInUndoList( moduleText->GetParent(), UR_CHANGED );
        break;
345

346 347 348 349 350 351 352 353 354
    default:
        break;
    }

    // Apply changes
    text->SetSize( newSize );
    text->SetThickness( newThickness );

    if( aDC )
355
        m_canvas->Refresh();
356 357 358 359

    OnModify();
}

360
void PCB_BASE_FRAME::ResetModuleTextSizes( int aType, wxDC* aDC )
361 362 363 364 365 366 367 368 369 370 371 372 373
{
    MODULE* module;
    BOARD_ITEM* boardItem;
    TEXTE_MODULE* item;
    ITEM_PICKER itemWrapper( NULL, UR_CHANGED );
    PICKED_ITEMS_LIST undoItemList;
    unsigned int ii;

    module = GetBoard()->m_Modules;

    // Prepare undo list
    while( module )
    {
374
        itemWrapper.SetItem( module );
375

376 377 378 379
        switch( aType )
        {
        case TEXT_is_REFERENCE:
            item = module->m_Reference;
380

381
            if( item->GetSize() != GetDesignSettings().m_ModuleTextSize || item->GetThickness() != GetDesignSettings().m_ModuleTextWidth )
382
                undoItemList.PushItem( itemWrapper );
383

384
            break;
385

386 387
        case TEXT_is_VALUE:
            item = module->m_Value;
388

389 390
            if( item->GetSize() != GetDesignSettings().m_ModuleTextSize ||
                item->GetThickness() != GetDesignSettings().m_ModuleTextWidth )
391
                undoItemList.PushItem( itemWrapper );
392

393
            break;
394

395 396
        case TEXT_is_DIVERS:
            // Go through all other module text fields
397
            for( boardItem = module->m_Drawings; boardItem; boardItem = boardItem->Next() )
398
            {
399
                if( boardItem->Type() == PCB_MODULE_TEXT_T )
400 401
                {
                    item = (TEXTE_MODULE*) boardItem;
402

403 404
                    if( item->GetSize() != GetDesignSettings().m_ModuleTextSize
                        || item->GetThickness() != GetDesignSettings().m_ModuleTextWidth )
405 406 407 408 409 410
                    {
                        undoItemList.PushItem( itemWrapper );
                        break;
                    }
                }
            }
411

412
            break;
413

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        default:
            break;
        }
        module = module->Next();
    }

    // Exit if there's nothing to do
    if( !undoItemList.GetCount() )
        return;

    SaveCopyInUndoList( undoItemList, UR_CHANGED );

    // Apply changes to modules in the undo list
    for( ii = 0; ii < undoItemList.GetCount(); ii++ )
    {
        module = (MODULE*) undoItemList.GetPickedItem( ii );
430

431 432 433
        switch( aType )
        {
        case TEXT_is_REFERENCE:
434 435
            module->m_Reference->SetThickness( GetDesignSettings().m_ModuleTextWidth );
            module->m_Reference->SetSize( GetDesignSettings().m_ModuleTextSize );
436
            break;
437

438
        case TEXT_is_VALUE:
439 440
            module->m_Value->SetThickness( GetDesignSettings().m_ModuleTextWidth );
            module->m_Value->SetSize( GetDesignSettings().m_ModuleTextSize );
441
            break;
442

443
        case TEXT_is_DIVERS:
444
            for( boardItem = module->m_Drawings; boardItem; boardItem = boardItem->Next() )
445
            {
446
                if( boardItem->Type() == PCB_MODULE_TEXT_T )
447 448
                {
                    item = (TEXTE_MODULE*) boardItem;
449 450
                    item->SetThickness( GetDesignSettings().m_ModuleTextWidth );
                    item->SetSize( GetDesignSettings().m_ModuleTextSize );
451 452
                }
            }
453

454 455 456 457 458
            break;
        }
    }

    if( aDC )
459
        m_canvas->Refresh();
460 461 462

    OnModify();
}