2011-10-19 20:32:21 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2018-01-28 21:02:31 +00:00
|
|
|
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2019-05-20 18:43:47 +00:00
|
|
|
* Copyright (C) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2011-10-19 20:32:21 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <gr_basic.h>
|
|
|
|
#include <base_struct.h>
|
2019-05-31 12:15:25 +00:00
|
|
|
#include <gr_text.h>
|
2018-08-03 12:18:26 +00:00
|
|
|
#include <sch_draw_panel.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <confirm.h>
|
2018-01-30 10:49:51 +00:00
|
|
|
#include <sch_edit_frame.h>
|
2019-04-29 17:50:46 +00:00
|
|
|
#include <tool/tool_manager.h>
|
2019-05-10 17:19:48 +00:00
|
|
|
#include <tools/ee_actions.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <general.h>
|
|
|
|
#include <sch_text.h>
|
2019-04-29 17:50:46 +00:00
|
|
|
#include <sch_view.h>
|
2019-06-20 06:20:33 +00:00
|
|
|
|
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
|
2019-06-19 10:31:21 +00:00
|
|
|
#include "invoke_sch_dialog.h"
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2020-01-09 07:52:30 +00:00
|
|
|
static PINSHEETLABEL_SHAPE lastGlobalLabelShape = PINSHEETLABEL_SHAPE::PS_INPUT;
|
2020-01-08 19:07:55 +00:00
|
|
|
static LABEL_SPIN_STYLE lastTextOrientation = LABEL_SPIN_STYLE::LEFT;
|
2016-02-28 18:16:59 +00:00
|
|
|
static bool lastTextBold = false;
|
|
|
|
static bool lastTextItalic = false;
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2019-08-22 11:49:58 +00:00
|
|
|
static std::deque<std::unique_ptr<SCH_TEXT>> s_queuedTexts;
|
2019-06-20 06:20:33 +00:00
|
|
|
|
|
|
|
SCH_TEXT* SCH_EDIT_FRAME::GetNextNewText()
|
|
|
|
{
|
2019-08-22 11:49:58 +00:00
|
|
|
if( s_queuedTexts.empty() )
|
2019-06-20 06:20:33 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2019-08-22 11:49:58 +00:00
|
|
|
auto next_text = std::move( s_queuedTexts.front() );
|
|
|
|
s_queuedTexts.pop_front();
|
2019-06-20 06:20:33 +00:00
|
|
|
|
|
|
|
return next_text.release();
|
|
|
|
}
|
|
|
|
|
2009-11-03 13:26:31 +00:00
|
|
|
|
2018-09-09 21:06:38 +00:00
|
|
|
SCH_TEXT* SCH_EDIT_FRAME::CreateNewText( int aType )
|
2009-12-06 18:04:41 +00:00
|
|
|
{
|
2019-06-13 17:28:55 +00:00
|
|
|
wxPoint cursorPos = (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition();
|
2019-06-13 13:34:38 +00:00
|
|
|
SCH_TEXT* textItem = nullptr;
|
2007-08-20 01:20:48 +00:00
|
|
|
|
2019-08-22 11:49:58 +00:00
|
|
|
s_queuedTexts.clear();
|
2019-06-20 06:20:33 +00:00
|
|
|
|
2011-03-03 01:58:12 +00:00
|
|
|
switch( aType )
|
2007-08-20 01:20:48 +00:00
|
|
|
{
|
|
|
|
case LAYER_NOTES:
|
2019-06-13 13:34:38 +00:00
|
|
|
textItem = new SCH_TEXT( cursorPos );
|
2007-08-20 01:20:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LAYER_LOCLABEL:
|
2019-06-13 13:34:38 +00:00
|
|
|
textItem = new SCH_LABEL( cursorPos );
|
2007-08-20 01:20:48 +00:00
|
|
|
break;
|
|
|
|
|
2008-03-20 01:50:21 +00:00
|
|
|
case LAYER_HIERLABEL:
|
2019-06-13 13:34:38 +00:00
|
|
|
textItem = new SCH_HIERLABEL( cursorPos );
|
2011-12-08 15:45:01 +00:00
|
|
|
textItem->SetShape( lastGlobalLabelShape );
|
2008-02-12 21:12:46 +00:00
|
|
|
break;
|
2008-03-20 01:50:21 +00:00
|
|
|
|
2007-08-20 01:20:48 +00:00
|
|
|
case LAYER_GLOBLABEL:
|
2019-06-13 13:34:38 +00:00
|
|
|
textItem = new SCH_GLOBALLABEL( cursorPos );
|
2011-12-08 15:45:01 +00:00
|
|
|
textItem->SetShape( lastGlobalLabelShape );
|
2007-08-20 01:20:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-12-08 20:12:46 +00:00
|
|
|
DisplayError( this, wxT( "SCH_EDIT_FRAME::CreateNewText() Internal error" ) );
|
2019-06-13 13:34:38 +00:00
|
|
|
return nullptr;
|
2007-08-20 01:20:48 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 19:36:07 +00:00
|
|
|
textItem->SetBold( lastTextBold );
|
|
|
|
textItem->SetItalic( lastTextItalic );
|
2017-01-23 20:30:11 +00:00
|
|
|
textItem->SetLabelSpinStyle( lastTextOrientation );
|
|
|
|
textItem->SetTextSize( wxSize( GetDefaultTextSize(), GetDefaultTextSize() ) );
|
2011-03-03 01:58:12 +00:00
|
|
|
textItem->SetFlags( IS_NEW | IS_MOVED );
|
2007-08-20 01:20:48 +00:00
|
|
|
|
2019-06-19 10:31:21 +00:00
|
|
|
if( InvokeDialogLabelEditor( this, textItem ) != wxID_OK || textItem->GetText().IsEmpty() )
|
2007-08-20 01:20:48 +00:00
|
|
|
{
|
2013-04-28 14:28:13 +00:00
|
|
|
delete textItem;
|
2019-06-13 13:34:38 +00:00
|
|
|
return nullptr;
|
2007-08-20 01:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 06:20:33 +00:00
|
|
|
if( aType != LAYER_NOTES )
|
|
|
|
{
|
2019-11-15 14:35:11 +00:00
|
|
|
UTF8 text( textItem->GetText() );
|
|
|
|
int brace_count = 0;
|
|
|
|
int bracket_count = 0;
|
|
|
|
bool last_space = false;
|
|
|
|
UTF8 token;
|
2019-06-20 06:20:33 +00:00
|
|
|
|
2019-11-15 14:35:11 +00:00
|
|
|
for( auto chIt = text.ubegin(); chIt != text.uend(); chIt++ )
|
2019-06-20 06:20:33 +00:00
|
|
|
{
|
2019-11-15 14:35:11 +00:00
|
|
|
switch( *chIt )
|
2019-08-22 11:49:58 +00:00
|
|
|
{
|
2019-11-15 14:35:11 +00:00
|
|
|
case '{':
|
|
|
|
brace_count++;
|
|
|
|
last_space = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
bracket_count++;
|
|
|
|
last_space = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '}':
|
|
|
|
brace_count = std::max( 0, brace_count - 1 );
|
|
|
|
last_space = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ']':
|
|
|
|
bracket_count = std::max( 0, bracket_count - 1 );
|
|
|
|
last_space = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
if( !token.empty() && bracket_count == 0 && brace_count == 0 )
|
2019-08-22 11:49:58 +00:00
|
|
|
{
|
2019-11-15 14:35:11 +00:00
|
|
|
std::unique_ptr<SCH_TEXT> nextitem( static_cast<SCH_TEXT*>( textItem->Clone() ) );
|
|
|
|
nextitem->SetText( token.wx_str() );
|
|
|
|
s_queuedTexts.push_back( std::move( nextitem ) );
|
|
|
|
token.clear();
|
|
|
|
continue;
|
2019-08-22 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:35:11 +00:00
|
|
|
// Skip leading whitespace
|
|
|
|
if( token.empty() || last_space )
|
|
|
|
continue;
|
2019-08-22 11:49:58 +00:00
|
|
|
|
2019-11-15 14:35:11 +00:00
|
|
|
last_space = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
last_space = false;
|
|
|
|
break;
|
2019-08-22 11:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:35:11 +00:00
|
|
|
token += *chIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !token.empty() )
|
|
|
|
{
|
2019-06-20 06:20:33 +00:00
|
|
|
std::unique_ptr<SCH_TEXT> nextitem( static_cast<SCH_TEXT*>( textItem->Clone() ) );
|
2019-11-15 14:35:11 +00:00
|
|
|
nextitem->SetText( token.wx_str() );
|
2019-08-22 11:49:58 +00:00
|
|
|
s_queuedTexts.push_back( std::move( nextitem ) );
|
2019-06-20 06:20:33 +00:00
|
|
|
}
|
2019-08-22 11:49:58 +00:00
|
|
|
|
|
|
|
delete textItem;
|
|
|
|
textItem = GetNextNewText();
|
|
|
|
|
|
|
|
if( !textItem )
|
|
|
|
return nullptr;
|
2019-06-20 06:20:33 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 19:36:07 +00:00
|
|
|
lastTextBold = textItem->IsBold();
|
|
|
|
lastTextItalic = textItem->IsItalic();
|
2017-01-23 20:30:11 +00:00
|
|
|
lastTextOrientation = textItem->GetLabelSpinStyle();
|
2010-03-16 18:22:59 +00:00
|
|
|
|
2019-04-15 23:56:41 +00:00
|
|
|
if( textItem->Type() == SCH_GLOBAL_LABEL_T || textItem->Type() == SCH_HIER_LABEL_T )
|
2011-12-08 15:45:01 +00:00
|
|
|
lastGlobalLabelShape = textItem->GetShape();
|
2007-08-20 01:20:48 +00:00
|
|
|
|
2011-03-03 01:58:12 +00:00
|
|
|
return textItem;
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
2011-10-19 20:32:21 +00:00
|
|
|
|
2019-06-08 15:32:49 +00:00
|
|
|
void SCH_EDIT_FRAME::ConvertTextType( SCH_TEXT* aText, KICAD_T aNewType )
|
2010-03-16 18:22:59 +00:00
|
|
|
{
|
2019-10-01 14:03:08 +00:00
|
|
|
KICAD_T oldType = aText->Type();
|
|
|
|
bool selected = aText->IsSelected();
|
2011-03-03 01:58:12 +00:00
|
|
|
|
2019-04-29 17:50:46 +00:00
|
|
|
wxCHECK_RET( aText->CanIncrementLabel(), "Cannot convert text type." );
|
2011-03-03 01:58:12 +00:00
|
|
|
|
2019-10-01 14:03:08 +00:00
|
|
|
if( oldType == aNewType )
|
2007-08-20 01:20:48 +00:00
|
|
|
return;
|
|
|
|
|
2020-01-08 19:07:55 +00:00
|
|
|
SCH_TEXT* newtext = nullptr;
|
|
|
|
const wxPoint& position = aText->GetPosition();
|
|
|
|
LABEL_SPIN_STYLE orientation = aText->GetLabelSpinStyle();
|
|
|
|
wxString txt = UnescapeString( aText->GetText() );
|
2019-05-20 18:43:47 +00:00
|
|
|
|
|
|
|
// There can be characters in a SCH_TEXT object that can break labels so we have to
|
|
|
|
// fix them here.
|
2019-10-01 14:03:08 +00:00
|
|
|
if( oldType == SCH_TEXT_T )
|
2019-05-20 18:43:47 +00:00
|
|
|
{
|
|
|
|
txt.Replace( "\n", "_" );
|
|
|
|
txt.Replace( "\r", "_" );
|
|
|
|
txt.Replace( "\t", "_" );
|
|
|
|
txt.Replace( " ", "_" );
|
|
|
|
}
|
2007-08-20 01:20:48 +00:00
|
|
|
|
2019-06-08 15:32:49 +00:00
|
|
|
// label strings are "escaped" i.e. a '/' is replaced by "{slash}"
|
|
|
|
if( aNewType != SCH_TEXT_T )
|
|
|
|
txt = EscapeString( txt, CTX_NETNAME );
|
|
|
|
|
|
|
|
switch( aNewType )
|
2007-08-20 01:20:48 +00:00
|
|
|
{
|
2019-04-29 17:50:46 +00:00
|
|
|
case SCH_LABEL_T: newtext = new SCH_LABEL( position, txt ); break;
|
|
|
|
case SCH_GLOBAL_LABEL_T: newtext = new SCH_GLOBALLABEL( position, txt ); break;
|
|
|
|
case SCH_HIER_LABEL_T: newtext = new SCH_HIERLABEL( position, txt ); break;
|
|
|
|
case SCH_TEXT_T: newtext = new SCH_TEXT( position, txt ); break;
|
2018-09-18 13:41:06 +00:00
|
|
|
|
|
|
|
default:
|
2019-10-01 14:03:08 +00:00
|
|
|
wxFAIL_MSG( wxString::Format( "Invalid text type: %d.", aNewType ) );
|
2018-09-18 13:41:06 +00:00
|
|
|
return;
|
2007-09-13 11:28:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 17:50:46 +00:00
|
|
|
// Copy the old text item settings to the new one. Justifications are not copied
|
|
|
|
// because they are not used in labels. Justifications will be set to default value
|
|
|
|
// in the new text item type.
|
|
|
|
//
|
|
|
|
newtext->SetFlags( aText->GetEditFlags() );
|
|
|
|
newtext->SetShape( aText->GetShape() );
|
2020-01-08 19:07:55 +00:00
|
|
|
newtext->SetLabelSpinStyle( orientation );
|
2019-04-29 17:50:46 +00:00
|
|
|
newtext->SetTextSize( aText->GetTextSize() );
|
|
|
|
newtext->SetThickness( aText->GetThickness() );
|
|
|
|
newtext->SetItalic( aText->IsItalic() );
|
|
|
|
newtext->SetBold( aText->IsBold() );
|
|
|
|
newtext->SetIsDangling( aText->IsDangling() );
|
|
|
|
|
|
|
|
if( selected )
|
2019-05-10 17:19:48 +00:00
|
|
|
m_toolManager->RunAction( EE_ACTIONS::removeItemFromSel, true, aText );
|
2019-04-29 17:50:46 +00:00
|
|
|
|
|
|
|
if( !aText->IsNew() )
|
2013-01-25 17:29:54 +00:00
|
|
|
{
|
2019-04-29 17:50:46 +00:00
|
|
|
SaveCopyInUndoList( aText, UR_DELETED );
|
|
|
|
SaveCopyInUndoList( newtext, UR_NEW, true );
|
|
|
|
|
|
|
|
RemoveFromScreen( aText );
|
|
|
|
AddToScreen( newtext );
|
2013-01-25 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 17:50:46 +00:00
|
|
|
if( selected )
|
2019-05-10 17:19:48 +00:00
|
|
|
m_toolManager->RunAction( EE_ACTIONS::addItemToSel, true, newtext );
|
2011-04-05 14:46:51 +00:00
|
|
|
|
2019-05-21 15:19:48 +00:00
|
|
|
// Otherwise, pointer is owned by the undo stack
|
|
|
|
if( aText->IsNew() )
|
|
|
|
delete aText;
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2019-10-01 14:03:08 +00:00
|
|
|
if( aNewType == SCH_TEXT_T )
|
|
|
|
{
|
|
|
|
if( newtext->IsDangling() )
|
|
|
|
{
|
|
|
|
newtext->SetIsDangling( false );
|
|
|
|
GetCanvas()->GetView()->Update( newtext, KIGFX::REPAINT );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TestDanglingEnds();
|
|
|
|
|
2019-04-29 17:50:46 +00:00
|
|
|
OnModify();
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
2011-07-03 18:51:07 +00:00
|
|
|
|
|
|
|
|
2019-04-29 17:50:46 +00:00
|
|
|
/*
|
|
|
|
* Function to increment bus label numbers. Adds aIncrement to labels which end in numbers.
|
2011-07-03 18:51:07 +00:00
|
|
|
*/
|
2015-04-22 11:39:00 +00:00
|
|
|
void IncrementLabelMember( wxString& name, int aIncrement )
|
2011-07-03 18:51:07 +00:00
|
|
|
{
|
|
|
|
int ii, nn;
|
|
|
|
long number = 0;
|
|
|
|
|
|
|
|
ii = name.Len() - 1; nn = 0;
|
|
|
|
|
2019-03-04 19:40:23 +00:00
|
|
|
if( !wxIsdigit( name.GetChar( ii ) ) )
|
2011-07-03 18:51:07 +00:00
|
|
|
return;
|
|
|
|
|
2019-03-04 19:40:23 +00:00
|
|
|
while( (ii >= 0) && wxIsdigit( name.GetChar( ii ) ) )
|
2011-07-03 18:51:07 +00:00
|
|
|
{
|
|
|
|
ii--; nn++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ii++; /* digits are starting at ii position */
|
|
|
|
wxString litt_number = name.Right( nn );
|
|
|
|
|
|
|
|
if( litt_number.ToLong( &number ) )
|
|
|
|
{
|
2015-04-22 11:39:00 +00:00
|
|
|
number += aIncrement;
|
2011-07-03 18:51:07 +00:00
|
|
|
name.Remove( ii ); name << number;
|
|
|
|
}
|
|
|
|
}
|