kicad/eeschema/edit_label.cpp

374 lines
10 KiB
C++
Raw Normal View History

/*********************************************************************/
/* EESchema */
/* edit_label.cpp: label, global label and text creation or edition */
/*********************************************************************/
2007-05-06 16:03:28 +00:00
#include "fctsys.h"
#include "gr_basic.h"
#include "common.h"
#include "base_struct.h"
#include "drawtxt.h"
#include "class_drawpanel.h"
#include "confirm.h"
#include "class_sch_screen.h"
#include "wxEeschemaStruct.h"
2007-05-06 16:03:28 +00:00
#include "general.h"
#include "protos.h"
#include "sch_text.h"
2007-05-06 16:03:28 +00:00
static void ShowWhileMoving( EDA_DRAW_PANEL* panel, wxDC* DC, bool erase );
static void ExitMoveTexte( EDA_DRAW_PANEL* panel, wxDC* DC );
2007-05-06 16:03:28 +00:00
2007-05-06 16:03:28 +00:00
static wxPoint ItemInitialPosition;
static int OldOrient;
static wxSize OldSize;
static int lastGlobalLabelShape = (int) NET_INPUT;
static int lastTextOrientation = 0;
static bool lastTextBold = false;
static bool lastTextItalic = false;
2007-05-06 16:03:28 +00:00
void SCH_EDIT_FRAME::StartMoveTexte( SCH_TEXT* TextStruct, wxDC* DC )
2007-05-06 16:03:28 +00:00
{
if( TextStruct == NULL )
return;
m_itemToRepeat = NULL;
if( (TextStruct->m_Flags & IS_NEW) == 0 )
{
delete g_ItemToUndoCopy;
g_ItemToUndoCopy = TextStruct->Clone();
}
TextStruct->m_Flags |= IS_MOVED;
2007-09-01 12:00:30 +00:00
switch( TextStruct->Type() )
{
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIERARCHICAL_LABEL_T:
case SCH_TEXT_T:
ItemInitialPosition = TextStruct->m_Pos;
OldSize = TextStruct->m_Size;
OldOrient = TextStruct->GetSchematicTextOrientation();
break;
default:
break;
}
DrawPanel->CursorOff( DC );
2008-03-20 01:50:21 +00:00
GetScreen()->m_Curseur = ItemInitialPosition;
DrawPanel->MouseToCursorSchema();
OnModify( );
DrawPanel->ManageCurseur = ShowWhileMoving;
DrawPanel->ForceCloseManageCurseur = ExitMoveTexte;
2008-03-20 01:50:21 +00:00
GetScreen()->SetCurItem( TextStruct );
DrawPanel->ManageCurseur( DrawPanel, DC, TRUE );
DrawPanel->CursorOn( DC );
2007-05-06 16:03:28 +00:00
}
void SCH_EDIT_FRAME::ChangeTextOrient( SCH_TEXT* TextStruct, wxDC* DC )
2007-05-06 16:03:28 +00:00
{
if( TextStruct == NULL )
2008-03-20 01:50:21 +00:00
TextStruct = (SCH_TEXT*) PickStruct( GetScreen()->m_Curseur,
GetScreen(), TEXT_T | LABEL_T );
if( TextStruct == NULL )
return;
/* save old text in undo list if is not already in edit */
if( TextStruct->m_Flags == 0 )
SaveCopyInUndoList( TextStruct, UR_CHANGED );
2009-11-02 20:36:20 +00:00
/* Erase old text */
DrawPanel->CursorOff( DC );
TextStruct->Draw( DrawPanel, DC, wxPoint( 0, 0 ), g_XorMode );
int orient;
2007-09-01 12:00:30 +00:00
switch( TextStruct->Type() )
{
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIERARCHICAL_LABEL_T:
case SCH_TEXT_T:
orient = TextStruct->GetSchematicTextOrientation() + 1;
orient &= 3;
TextStruct->SetSchematicTextOrientation( orient );
break;
default:
break;
}
OnModify( );
TextStruct->Draw( DrawPanel, DC, wxPoint( 0, 0 ), g_XorMode );
DrawPanel->CursorOn( DC );
2007-05-06 16:03:28 +00:00
}
2007-05-06 16:03:28 +00:00
/* Routine to create new text struct (GraphicText, label or Glabel).
*/
SCH_TEXT* SCH_EDIT_FRAME::CreateNewText( wxDC* DC, int type )
{
2008-03-20 01:50:21 +00:00
SCH_TEXT* NewText = NULL;
m_itemToRepeat = NULL;
switch( type )
{
case LAYER_NOTES:
2008-03-20 01:50:21 +00:00
NewText = new SCH_TEXT( GetScreen()->m_Curseur );
break;
case LAYER_LOCLABEL:
2008-03-20 01:50:21 +00:00
NewText = new SCH_LABEL( GetScreen()->m_Curseur );
break;
2008-03-20 01:50:21 +00:00
case LAYER_HIERLABEL:
NewText = new SCH_HIERLABEL( GetScreen()->m_Curseur );
NewText->m_Shape = lastGlobalLabelShape;
break;
2008-03-20 01:50:21 +00:00
case LAYER_GLOBLABEL:
NewText = new SCH_GLOBALLABEL( GetScreen()->m_Curseur );
NewText->m_Shape = lastGlobalLabelShape;
break;
default:
DisplayError( this, wxT( "SCH_EDIT_FRAME::CreateNewText() Internal error" ) );
return NULL;
}
NewText->m_Bold = lastTextBold;
NewText->m_Italic = lastTextItalic;
NewText->SetSchematicTextOrientation( lastTextOrientation );
NewText->m_Size.x = NewText->m_Size.y = g_DefaultTextLabelSize;
NewText->m_Flags = IS_NEW | IS_MOVED;
NewText->Draw( DrawPanel, DC, wxPoint( 0, 0 ), g_XorMode );
2009-12-27 14:01:21 +00:00
EditSchematicText( NewText );
if( NewText->m_Text.IsEmpty() )
{
2008-03-20 01:50:21 +00:00
SAFE_DELETE( NewText );
return NULL;
}
lastTextBold = NewText->m_Bold;
lastTextItalic = NewText->m_Italic;
lastTextOrientation = NewText->GetSchematicTextOrientation();
if( type == LAYER_GLOBLABEL || type == LAYER_HIERLABEL )
{
lastGlobalLabelShape = NewText->m_Shape;
}
NewText->Draw( DrawPanel, DC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
DrawPanel->ManageCurseur = ShowWhileMoving;
DrawPanel->ForceCloseManageCurseur = ExitMoveTexte;
2008-03-20 01:50:21 +00:00
GetScreen()->SetCurItem( NewText );
return NewText;
2007-05-06 16:03:28 +00:00
}
/************************************/
2007-09-19 15:29:50 +00:00
/* Redraw a Text while moving */
/************************************/
static void ShowWhileMoving( EDA_DRAW_PANEL* panel, wxDC* DC, bool erase )
2007-05-06 16:03:28 +00:00
{
SCH_ITEM* TextStruct = (SCH_ITEM*) panel->GetScreen()->GetCurItem();
2007-09-19 15:29:50 +00:00
/* "Undraw" the current text at its old position*/
if( erase )
TextStruct->Draw( panel, DC, wxPoint( 0, 0 ), g_XorMode );
2007-09-19 15:29:50 +00:00
/* redraw the text */
2007-09-01 12:00:30 +00:00
switch( TextStruct->Type() )
{
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIERARCHICAL_LABEL_T:
case SCH_TEXT_T:
2008-03-20 01:50:21 +00:00
( (SCH_TEXT*) TextStruct )->m_Pos = panel->GetScreen()->m_Curseur;
break;
default:
break;
}
TextStruct->Draw( panel, DC, wxPoint( 0, 0 ), g_XorMode );
2007-05-06 16:03:28 +00:00
}
/* Abort function for the command move text */
static void ExitMoveTexte( EDA_DRAW_PANEL* Panel, wxDC* DC )
{
BASE_SCREEN* screen = Panel->GetScreen();
SCH_ITEM* Struct = (SCH_ITEM*) screen->GetCurItem();
SCH_EDIT_FRAME* parent = ( SCH_EDIT_FRAME* ) Panel->GetParent();
parent->SetRepeatItem( NULL );
Panel->ManageCurseur = NULL;
Panel->ForceCloseManageCurseur = NULL;
2007-09-19 15:29:50 +00:00
if( Struct == NULL ) /* no current item */
{
return;
}
/* "Undraw" the text, and delete it if new (i.e. it was being just
* created)*/
Struct->Draw( Panel, DC, wxPoint( 0, 0 ), g_XorMode );
2007-09-19 15:29:50 +00:00
if( Struct->m_Flags & IS_NEW )
{
2008-03-20 01:50:21 +00:00
SAFE_DELETE( Struct );
screen->SetCurItem( NULL );
}
else /* this was a move command on "old" text: restore its old settings. */
{
2007-09-01 12:00:30 +00:00
switch( Struct->Type() )
{
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIERARCHICAL_LABEL_T:
case SCH_TEXT_T:
{
2008-03-20 01:50:21 +00:00
SCH_TEXT* Text = (SCH_TEXT*) Struct;
Text->m_Pos = ItemInitialPosition;
Text->m_Size = OldSize;
Text->SetSchematicTextOrientation( OldOrient );
}
break;
default:
break;
}
Struct->Draw( Panel, DC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
Struct->m_Flags = 0;
}
2007-05-06 16:03:28 +00:00
}
/* Routine to change a text type to an other one (GraphicText, label or Glabel).
* A new test, label or hierarchical or global label is created from the old text.
* the old text is deleted
*/
void SCH_EDIT_FRAME::ConvertTextType( SCH_TEXT* Text, wxDC* DC, int newtype )
{
if( Text == NULL )
return;
2008-03-20 01:50:21 +00:00
SCH_TEXT* newtext;
switch( newtype )
{
case SCH_LABEL_T:
2008-03-20 01:50:21 +00:00
newtext = new SCH_LABEL( Text->m_Pos, Text->m_Text );
break;
case SCH_GLOBAL_LABEL_T:
newtext = new SCH_GLOBALLABEL( Text->m_Pos, Text->m_Text );
2008-03-20 01:50:21 +00:00
break;
case SCH_HIERARCHICAL_LABEL_T:
newtext = new SCH_HIERLABEL( Text->m_Pos, Text->m_Text );
break;
case SCH_TEXT_T:
2008-03-20 01:50:21 +00:00
newtext = new SCH_TEXT( Text->m_Pos, Text->m_Text );
break;
default:
newtext = NULL;
DisplayError( this, wxT( "ConvertTextType: Internal error" ) );
return;
}
/* copy the old text settings
* Justifications are not copied because they are not used in labels,
* and can be used in texts
* So they will be set to default in conversion.
*/
newtext->m_Shape = Text->m_Shape;
newtext->SetSchematicTextOrientation( Text->GetSchematicTextOrientation() );
newtext->m_Size = Text->m_Size;
newtext->m_Thickness = Text->m_Thickness;
newtext->m_Italic = Text->m_Italic;
newtext->m_Bold = Text->m_Bold;
// save current text flag:
int flags = Text->m_Flags;
/* add the new text in linked list if old text is in list */
if( (flags & IS_NEW) == 0 )
{
newtext->SetNext( GetScreen()->GetDrawItems() );
GetScreen()->SetDrawItems( newtext );
OnModify();
}
/* now delete the old text
* If it is a text flagged IS_NEW it will be deleted by ForceCloseManageCurseur()
* If not, we must delete it.
*/
if( DrawPanel->ManageCurseur && DrawPanel->ForceCloseManageCurseur )
{
DrawPanel->ForceCloseManageCurseur( DrawPanel, DC );
}
if( (flags & IS_NEW) == 0 ) // Remove old text from current list and
// save it in undo list
{
Text->m_Flags = 0;
DeleteStruct( DrawPanel, DC, Text ); // old text is really saved in
// undo list
2008-03-20 01:50:21 +00:00
GetScreen()->SetCurItem( NULL );
m_itemToRepeat = NULL;
}
GetScreen()->SetCurItem( NULL );
delete g_ItemToUndoCopy;
g_ItemToUndoCopy = NULL;
DrawPanel->CursorOff( DC ); // Erase schematic cursor
/* Save the new text in undo list if the old text was not itself a "new created text"
* In this case, the old text is already in undo list as a deleted item.
* Of course if the old text was a "new created text" the new text will be
* put in undo list later, at the end of the current command (if not aborted)
*/
if( (flags & IS_NEW) == 0 )
{
SaveCopyInUndoList( newtext, UR_NEW );
}
else
2008-03-20 01:50:21 +00:00
{
GetScreen()->SetCurItem( newtext );
newtext->m_Flags = IS_NEW;
2008-03-20 01:50:21 +00:00
}
if( (flags & IS_MOVED) != 0 )
{
2008-03-20 01:50:21 +00:00
GetScreen()->SetCurItem( newtext );
StartMoveTexte( newtext, DC );
}
newtext->Draw( DrawPanel, DC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
DrawPanel->CursorOn( DC ); // redraw schematic cursor
2007-05-06 16:03:28 +00:00
}