dialog_pcb_text_properties.cpp 9.53 KB
Newer Older
1
/*
2
 * This program source code file is part of KiCad, a free EDA CAD application.
3 4
 *
 * Copyright (C) 2004-2010 Jean-Pierre Charras <jean-pierre.charras@gpisa-lab.inpg.fr>
5
 * Copyright (C) 2010 KiCad Developers, see change_log.txt for contributors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
 */

charras's avatar
charras committed
25 26 27
/***************************************************************************/
/* Dialog editor for text on copper and technical layers (TEXTE_PCB class) */
/***************************************************************************/
plyatov's avatar
plyatov committed
28

29 30 31 32 33 34 35
#include <fctsys.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <pcbnew.h>
#include <wxPcbStruct.h>
#include <drawtxt.h>
#include <confirm.h>
36
#include <base_units.h>
plyatov's avatar
plyatov committed
37

38 39
#include <class_board.h>
#include <class_pcb_text.h>
40

41 42 43
#include <vector>
#include <wx/wx.h>
#include <dialog_pcb_text_properties_base.h>
44
#include <class_pcb_layer_box_selector.h>
45 46 47 48 49 50


class PCB_EDIT_FRAME;
class TEXTE_PCB;


51
class DIALOG_PCB_TEXT_PROPERTIES : public DIALOG_PCB_TEXT_PROPERTIES_BASE
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{
public:
    DIALOG_PCB_TEXT_PROPERTIES( PCB_EDIT_FRAME* parent, TEXTE_PCB* passedTextPCB, wxDC* DC );

private:
    PCB_EDIT_FRAME*     m_Parent;
    wxDC*               m_DC;
    TEXTE_PCB*          m_SelectedPCBText;

    void MyInit();

    // Handlers for DIALOG_PCB_TEXT_PROPERTIES_BASE events.
    void OnClose( wxCloseEvent& event );
    void OnCancelClick( wxCommandEvent& event );
    void OnOkClick( wxCommandEvent& event );
};
68 69


70 71 72 73 74 75
/**
 *  DIALOG_PCB_TEXT_PROPERTIES, derived from DIALOG_PCB_TEXT_PROPERTIES_BASE
 *  @see dialog_dialog_pcb_text_properties_base.h and
 *  dialog_dialog_pcb_text_properties_base.cpp, automatically created by
 *  wxFormBuilder.
 */
plyatov's avatar
plyatov committed
76

77 78
DIALOG_PCB_TEXT_PROPERTIES::DIALOG_PCB_TEXT_PROPERTIES( PCB_EDIT_FRAME* parent,
                                                        TEXTE_PCB* passedTextPCB, wxDC* DC )
79
                            : DIALOG_PCB_TEXT_PROPERTIES_BASE( parent )
plyatov's avatar
plyatov committed
80
{
81 82 83
    m_Parent = parent;
    m_DC = DC;
    m_SelectedPCBText = passedTextPCB;
plyatov's avatar
plyatov committed
84

85
    MyInit();
86
    m_StandardSizerOK->SetDefault();
87 88 89
    GetSizer()->SetSizeHints( this );
    Centre();
}
plyatov's avatar
plyatov committed
90

91

92 93 94
/**
 * Routine for main window class to launch text properties dialog.
 */
95
void PCB_EDIT_FRAME::InstallTextPCBOptionsFrame( TEXTE_PCB* TextPCB, wxDC* DC )
plyatov's avatar
plyatov committed
96
{
97
    m_canvas->SetIgnoreMouseEvents( true );
98
#ifndef __WXMAC__
99
    DIALOG_PCB_TEXT_PROPERTIES dlg( this, TextPCB, DC );
100 101 102 103 104
#else
    // Avoid "writes" in the dialog, creates errors with WxOverlay and NSView
    // Raising an Exception - Fixes #891347
    DIALOG_PCB_TEXT_PROPERTIES dlg( this, TextPCB, NULL );
#endif
105
    dlg.ShowModal();
106
    m_canvas->MoveCursorToCrossHair();
107
    m_canvas->SetIgnoreMouseEvents( false );
plyatov's avatar
plyatov committed
108 109
}

110

111
void DIALOG_PCB_TEXT_PROPERTIES::MyInit()
plyatov's avatar
plyatov committed
112
{
113 114 115 116 117 118 119 120
    // Put units symbols to text labels where appropriate
    AddUnitSymbol( *m_SizeXLabel );
    AddUnitSymbol( *m_SizeYLabel );
    AddUnitSymbol( *m_ThicknessLabel );
    AddUnitSymbol( *m_PositionXLabel );
    AddUnitSymbol( *m_PositionYLabel );

    // Fill fields with current values
121
    *m_TextContentCtrl << m_SelectedPCBText->GetText();
122

123 124 125 126 127
    PutValueInLocalUnits( *m_SizeXCtrl, m_SelectedPCBText->GetSize().x );
    PutValueInLocalUnits( *m_SizeYCtrl, m_SelectedPCBText->GetSize().y );
    PutValueInLocalUnits( *m_ThicknessCtrl, m_SelectedPCBText->GetThickness() );
    PutValueInLocalUnits( *m_PositionXCtrl, m_SelectedPCBText->GetTextPosition().x );
    PutValueInLocalUnits( *m_PositionYCtrl, m_SelectedPCBText->GetTextPosition().y );
128

129 130 131 132 133 134 135
    // Configure the layers list selector
    m_LayerSelectionCtrl->SetLayersHotkeys( false );
    // A text has no sense on edge cut layer
    m_LayerSelectionCtrl->SetLayerMask( EDGE_LAYER );
    m_LayerSelectionCtrl->SetBoardFrame( m_Parent );
    m_LayerSelectionCtrl->Resync();
    m_LayerSelectionCtrl->SetLayerSelection( m_SelectedPCBText->GetLayer() );
136

137 138 139
    wxString orientationStr;
    orientationStr << m_SelectedPCBText->GetOrientation();
    m_OrientationCtrl->SetValue( orientationStr );
140

141
    if( m_SelectedPCBText->IsMirrored() )
142 143 144
        m_DisplayCtrl->SetSelection( 1 );
    else
        m_DisplayCtrl->SetSelection( 0 );
145

146
    if( m_SelectedPCBText->IsItalic() )
147 148 149
        m_StyleCtrl->SetSelection( 1 );
    else
        m_StyleCtrl->SetSelection( 0 );
charras's avatar
charras committed
150

151
    // Set justification
152
    EDA_TEXT_HJUSTIFY_T hJustify = m_SelectedPCBText->GetHorizJustify();
153 154
    m_justifyChoice->SetSelection( (int) hJustify + 1 );

155 156 157
    // Set focus on most important control
    m_TextContentCtrl->SetFocus();
    m_TextContentCtrl->SetSelection( -1, -1 );
plyatov's avatar
plyatov committed
158 159
}

160 161 162 163 164 165 166

void DIALOG_PCB_TEXT_PROPERTIES::OnClose( wxCloseEvent& event )
{
    EndModal( 0 );
}


167
void DIALOG_PCB_TEXT_PROPERTIES::OnCancelClick( wxCommandEvent& event )
plyatov's avatar
plyatov committed
168
{
169
    EndModal( wxID_CANCEL );
plyatov's avatar
plyatov committed
170 171
}

172

173
void DIALOG_PCB_TEXT_PROPERTIES::OnOkClick( wxCommandEvent& event )
plyatov's avatar
plyatov committed
174
{
175 176 177 178 179 180 181 182 183
    // Test for acceptable layer.
    // Incorrect layer can happen for old boards,
    // having texts on edge cut layer for instance
    if( m_LayerSelectionCtrl->GetLayerSelection() < 0 )
    {
        wxMessageBox( wxT("No layer selected, Please select the text layer") );
        return;
    }

184 185 186
    wxPoint newPosition;
    wxSize  newSize;

187 188
    // If no other command in progress, prepare undo command
    // (for a command in progress, will be made later, at the completion of command)
189
    if( m_SelectedPCBText->GetFlags() == 0 )
190
        m_Parent->SaveCopyInUndoList( m_SelectedPCBText, UR_CHANGED );
191 192 193 194 195

    /* set flag in edit to force undo/redo/abort proper operation,
     * and avoid new calls to SaveCopyInUndoList for the same text
     * this can occurs when a text is moved, and then rotated, edited ..
    */
196 197
    if( m_SelectedPCBText->GetFlags() != 0 )
        m_SelectedPCBText->SetFlags( IN_EDIT );
198

199
#ifndef USE_WX_OVERLAY
200 201
    // Erase old text on screen if context is available
    if( m_DC )
202
    {
203
        m_SelectedPCBText->Draw( m_Parent->GetCanvas(), m_DC, GR_XOR );
204
    }
205
#endif
206

207 208
    // Set the new text content
    if( !m_TextContentCtrl->GetValue().IsEmpty() )
209
    {
210
        m_SelectedPCBText->SetText( m_TextContentCtrl->GetValue() );
211
    }
212

213
    // Set PCB Text position
214 215
    newPosition.x = ReturnValueFromString( g_UserUnit, m_PositionXCtrl->GetValue() );
    newPosition.y = ReturnValueFromString( g_UserUnit, m_PositionYCtrl->GetValue() );
216
    m_SelectedPCBText->SetTextPosition( newPosition );
217 218

    // Check constraints and set PCB Text size
219 220
    newSize.x = ReturnValueFromString( g_UserUnit, m_SizeXCtrl->GetValue() );
    newSize.y = ReturnValueFromString( g_UserUnit, m_SizeYCtrl->GetValue() );
221

222 223
    if( newSize.x < TEXTS_MIN_SIZE )
        newSize.x = TEXTS_MIN_SIZE;
224

225 226
    if( newSize.y < TEXTS_MIN_SIZE )
        newSize.y = TEXTS_MIN_SIZE;
227

228 229
    if( newSize.x > TEXTS_MAX_WIDTH )
        newSize.x = TEXTS_MAX_WIDTH;
230

231 232
    if( newSize.y > TEXTS_MAX_WIDTH )
        newSize.y = TEXTS_MAX_WIDTH;
233

234
    m_SelectedPCBText->SetSize( newSize );
235 236

    // Set the new thickness
237 238
    m_SelectedPCBText->SetThickness( ReturnValueFromString( g_UserUnit,
                                                            m_ThicknessCtrl->GetValue() ) );
239 240

    // Test for acceptable values for thickness and size and clamp if fails
241 242
    int maxthickness = Clamp_Text_PenSize( m_SelectedPCBText->GetThickness(),
                                           m_SelectedPCBText->GetSize()  );
243

244
    if( m_SelectedPCBText->GetThickness() > maxthickness )
245
    {
246 247
        DisplayError( NULL,
                      _( "The text thickness is too large for the text size. It will be clamped" ) );
248
        m_SelectedPCBText->SetThickness( maxthickness );
249
    }
250

251
    // Set the layer on which the PCB text is laying
252
    m_SelectedPCBText->SetLayer( m_LayerSelectionCtrl->GetLayerSelection() );
253 254

    // Set whether the PCB text is mirrored (faced down from layer face perspective)
255
    m_SelectedPCBText->SetMirrored( m_DisplayCtrl->GetSelection() == 1 );
256 257

    // Set the text orientation
258 259 260 261
    long orientation;
    m_OrientationCtrl->GetValue().ToLong( &orientation );
    orientation = orientation % 3600;
    m_SelectedPCBText->SetOrientation( orientation );
262 263

    // Set whether the PCB text is slanted (it is not italics, as italics has additional curves in style)
264
    m_SelectedPCBText->SetItalic( m_StyleCtrl->GetSelection() );
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    // Set justification
    switch( m_justifyChoice->GetSelection() )
    {
    case 0:
        m_SelectedPCBText->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
        break;
    case 1:
        m_SelectedPCBText->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER );
        break;
    case 2:
        m_SelectedPCBText->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
        break;
    default:
        break;
    }

282
#ifndef USE_WX_OVERLAY
283 284
    // Finally, display new text if there is a context to do so
    if( m_DC )
285
    {
286
        m_SelectedPCBText->Draw( m_Parent->GetCanvas(), m_DC, GR_OR );
287
    }
288 289 290
#else
    m_parent->Refresh();
#endif
291
    m_Parent->OnModify();
292
    EndModal( 1 );
plyatov's avatar
plyatov committed
293
}