KiCad PCB EDA Suite
edit_pcb_text.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #include <fctsys.h>
32 #include <gr_basic.h>
33 #include <class_drawpanel.h>
34 #include <pcbnew.h>
35 #include <wxPcbStruct.h>
36 #include <macros.h>
37 
38 #include <class_board.h>
39 #include <class_pcb_text.h>
40 #include <class_board_item.h>
41 
42 
43 static void Move_Texte_Pcb( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
44  bool aErase );
45 static void Abort_Edit_Pcb_Text( EDA_DRAW_PANEL* Panel, wxDC* DC );
46 
47 
48 static TEXTE_PCB s_TextCopy( (BOARD_ITEM*) NULL ); /* copy of the edited text
49  * (used to undo/redo/abort
50  * a complex edition command
51  */
52 
53 
54 /*
55  * Abort current text edit progress.
56  * If a text is selected, its initial coord are regenerated
57  */
58 void Abort_Edit_Pcb_Text( EDA_DRAW_PANEL* Panel, wxDC* DC )
59 {
60  TEXTE_PCB* TextePcb = (TEXTE_PCB*) Panel->GetScreen()->GetCurItem();
61  ( (PCB_EDIT_FRAME*) Panel->GetParent() )->SetCurItem( NULL );
62 
63  Panel->SetMouseCapture( NULL, NULL );
64 
65  if( TextePcb == NULL ) // Should not occur
66  return;
67 
68 #ifndef USE_WX_OVERLAY
69  TextePcb->Draw( Panel, DC, GR_XOR );
70 #endif
71 
72  if( TextePcb->IsNew() ) // If new: remove it
73  {
74  TextePcb->DeleteStructure();
75  return;
76  }
77 
78 
79  TextePcb->SwapData( &s_TextCopy );
80  TextePcb->ClearFlags();
81 #ifndef USE_WX_OVERLAY
82  TextePcb->Draw( Panel, DC, GR_OR );
83 #else
84  Panel->Refresh();
85 #endif
86 }
87 
88 
89 /*
90  * Place the current text being moving
91  */
92 void PCB_EDIT_FRAME::Place_Texte_Pcb( TEXTE_PCB* TextePcb, wxDC* DC )
93 {
94  m_canvas->SetMouseCapture( NULL, NULL );
95  SetCurItem( NULL );
96 
97  if( TextePcb == NULL )
98  return;
99 
100  TextePcb->Draw( m_canvas, DC, GR_OR );
101  OnModify();
102 
103  if( TextePcb->IsNew() ) // If new: prepare undo command
104  {
105  SaveCopyInUndoList( TextePcb, UR_NEW );
106  TextePcb->ClearFlags();
107  return;
108  }
109 
110  if( TextePcb->IsMoving() ) // If moved only
111  {
112  SaveCopyInUndoList( TextePcb, UR_MOVED,
113  TextePcb->GetTextPos() - s_TextCopy.GetTextPos() );
114  }
115  else
116  {
117  // Restore initial params
118  TextePcb->SwapData( &s_TextCopy );
119  // Prepare undo command
120  SaveCopyInUndoList( TextePcb, UR_CHANGED );
121  // Restore current params
122  TextePcb->SwapData( &s_TextCopy );
123  }
124 
125  TextePcb->ClearFlags();
126 #ifdef USE_WX_OVERLAY
127  m_canvas->Refresh();
128 #endif
129 }
130 
131 
132 void PCB_EDIT_FRAME::StartMoveTextePcb( TEXTE_PCB* aTextePcb, wxDC* aDC, bool aErase )
133 {
134  if( aTextePcb == NULL )
135  return;
136 
137  // if it is an existing item: prepare a copy to undo/abort command
138  if( !aTextePcb->IsNew() )
139  s_TextCopy = *aTextePcb;
140 
141  aTextePcb->SetFlags( IS_MOVED );
142  SetMsgPanel( aTextePcb );
143 
144 #ifdef USE_WX_OVERLAY
145  m_canvas->Refresh();
146 #endif
147 
148  SetCrossHairPosition( aTextePcb->GetTextPos() );
150 
152  SetCurItem( aTextePcb );
153  m_canvas->CallMouseCapture( aDC, wxDefaultPosition, aErase );
154 }
155 
156 
157 // Move PCB text following the cursor.
158 static void Move_Texte_Pcb( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
159  bool aErase )
160 {
161  TEXTE_PCB* TextePcb = (TEXTE_PCB*) aPanel->GetScreen()->GetCurItem();
162 
163  if( TextePcb == NULL )
164  return;
165 
166  if( aErase )
167  TextePcb->Draw( aPanel, aDC, GR_XOR );
168 
169  TextePcb->SetTextPos( aPanel->GetParent()->GetCrossHairPosition() );
170 
171  TextePcb->Draw( aPanel, aDC, GR_XOR );
172 }
173 
174 
175 void PCB_EDIT_FRAME::Delete_Texte_Pcb( TEXTE_PCB* TextePcb, wxDC* DC )
176 {
177  if( TextePcb == NULL )
178  return;
179 
180  TextePcb->Draw( m_canvas, DC, GR_XOR );
181 
182  SaveCopyInUndoList( TextePcb, UR_DELETED );
183  TextePcb->UnLink();
184  m_canvas->SetMouseCapture( NULL, NULL );
185  SetCurItem( NULL );
186 }
187 
188 
190 {
191  TEXTE_PCB* textePcb = new TEXTE_PCB( GetBoard() );
192 
193  if( aText )
194  {
195  *textePcb = *aText;
196  GetBoard()->Add( textePcb );
197  textePcb->SetFlags( IS_NEW );
198  if( aDC )
199  StartMoveTextePcb( textePcb, aDC, false ); // Don't erase aText when copying
200  }
201  else
202  {
203  GetBoard()->Add( textePcb );
204  textePcb->SetFlags( IS_NEW );
205 
206  PCB_LAYER_ID layer = GetActiveLayer();
207 
208  textePcb->SetLayer( layer );
209 
210  // Set the mirrored option for layers on the BACK side of the board
211  if( layer == B_Cu || layer == B_SilkS ||
212  layer == B_Paste || layer == B_Mask ||
213  layer == B_Adhes
214  )
215  textePcb->SetMirrored( true );
216 
217  textePcb->SetTextSize( GetBoard()->GetDesignSettings().m_PcbTextSize );
218  textePcb->SetTextPos( GetCrossHairPosition() );
219  textePcb->SetThickness( GetBoard()->GetDesignSettings().m_PcbTextWidth );
220 
221  InstallTextPCBOptionsFrame( textePcb, aDC );
222 
223  if( textePcb->GetText().IsEmpty() )
224  {
225  textePcb->DeleteStructure();
226  textePcb = NULL;
227  }
228  else if( aDC )
229  {
230  StartMoveTextePcb( textePcb, aDC );
231  }
232  }
233 
234  return textePcb;
235 }
236 
237 
238 void PCB_EDIT_FRAME::Rotate_Texte_Pcb( TEXTE_PCB* TextePcb, wxDC* DC )
239 {
240  if( TextePcb == NULL )
241  return;
242 
243  // Erase previous text:
244  TextePcb->Draw( m_canvas, DC, GR_XOR );
245 
246  TextePcb->SetTextAngle( TextePcb->GetTextAngle() + 900 );
247 
248  // Redraw text in new position:
249  TextePcb->Draw( m_canvas, DC, GR_XOR );
250  SetMsgPanel( TextePcb );
251 
252  if( TextePcb->GetFlags() == 0 ) // i.e. not edited, or moved
253  SaveCopyInUndoList( TextePcb, UR_ROTATED, TextePcb->GetTextPos() );
254  else // set flag edit, to show it was a complex command
255  TextePcb->SetFlags( IN_EDIT );
256 
257  OnModify();
258 #ifdef USE_WX_OVERLAY
259  m_canvas->Refresh();
260 #endif
261 }
262 
263 
264 void PCB_EDIT_FRAME::FlipTextePcb( TEXTE_PCB* aTextePcb, wxDC* aDC )
265 {
266  if( aTextePcb == NULL )
267  return;
268 
269  aTextePcb->Draw( m_canvas, aDC, GR_XOR );
270 
271  aTextePcb->Flip( aTextePcb->GetTextPos() );
272 
273  aTextePcb->Draw( m_canvas, aDC, GR_XOR );
274  SetMsgPanel( aTextePcb );
275 
276  if( aTextePcb->GetFlags() == 0 ) // i.e. not edited, or moved
277  SaveCopyInUndoList( aTextePcb, UR_FLIPPED, aTextePcb->GetTextPos() );
278  else // set edit flag, for the current command
279  aTextePcb->SetFlags( IN_EDIT );
280 
281  OnModify();
282 #ifdef USE_WX_OVERLAY
283  m_canvas->Refresh();
284 #endif
285 }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:178
virtual void OnModify() override
Function OnModify must be called after a board change to set the modified flag.
Definition: pcbframe.cpp:999
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:269
void SetTextAngle(double aAngle)
bool IsMoving() const
Definition: base_struct.h:232
#define IN_EDIT
Item currently edited.
Definition: base_struct.h:125
TEXTE_PCB class definition.
const wxPoint & GetTextPos() const
Definition: eda_text.h:224
void InstallTextPCBOptionsFrame(TEXTE_PCB *TextPCB, wxDC *DC)
Routine for main window class to launch text properties dialog.
EDA_ITEM * GetCurItem() const
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
bool IsNew() const
Definition: base_struct.h:230
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
void FlipTextePcb(TEXTE_PCB *aTextePcb, wxDC *aDC)
Class BOARD to handle a board.
void Flip(const wxPoint &aCentre) override
Function Flip Flip this object, i.e.
void Draw(EDA_DRAW_PANEL *panel, wxDC *DC, GR_DRAWMODE aDrawMode, const wxPoint &offset=ZeroOffset) override
Function Draw BOARD_ITEMs have their own color information.
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:223
void SetCurItem(BOARD_ITEM *aItem, bool aDisplayInfo=true)
Function SetCurItem sets the currently selected item and displays it in the MsgPanel.
void DeleteStructure()
Function DeleteStructure deletes this object after UnLink()ing it from its owner if it has one...
BOARD * GetBoard() const
void Delete_Texte_Pcb(TEXTE_PCB *TextePcb, wxDC *DC)
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
Definition: draw_panel.cpp:347
virtual void UnLink()
Function UnLink detaches this object from its owner.
double GetTextAngle() const
Definition: eda_text.h:164
This file contains miscellaneous commonly used macros and functions.
#define IS_NEW
New item, just created.
Definition: base_struct.h:127
void Place_Texte_Pcb(TEXTE_PCB *TextePcb, wxDC *DC)
void SwapData(BOARD_ITEM *aImage)
Swap data between aItem and aImage.
Definition: undo_redo.cpp:567
PCB_LAYER_ID
A quick note on layer IDs:
void StartMoveTextePcb(TEXTE_PCB *aTextePcb, wxDC *aDC, bool aErase=true)
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:267
TEXTE_PCB * CreateTextePcb(wxDC *aDC, TEXTE_PCB *aText=NULL)
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Function SetMsgPanel clears the message panel and populates it with the contents of aList...
Definition: draw_frame.cpp:773
void Rotate_Texte_Pcb(TEXTE_PCB *TextePcb, wxDC *DC)
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
void CallMouseCapture(wxDC *aDC, const wxPoint &aPosition, bool aErase)
Function CallMouseCapture calls the mouse capture callback.
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
static void Move_Texte_Pcb(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:174
void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0)) override
Function SaveCopyInUndoList Creates a new entry in undo list of commands.
Definition: undo_redo.cpp:172
static TEXTE_PCB s_TextCopy((BOARD_ITEM *) NULL)
Definition: gr_basic.h:42
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:187
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:268
void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Function SetCrossHairPosition sets the screen cross hair position to aPosition in logical (drawing) u...
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
static void Abort_Edit_Pcb_Text(EDA_DRAW_PANEL *Panel, wxDC *DC)
virtual PCB_LAYER_ID GetActiveLayer() const
Function GetActiveLayer returns the active layer.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:148
#define IS_MOVED
Item being moved.
Definition: base_struct.h:126