dialog_edit_label.cpp 8.06 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4 5 6
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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
 */

/**
 * @file sch_text.h
 * @brief Implementation of the label properties dialog.
 */
plyatov's avatar
plyatov committed
30

31 32
#include <fctsys.h>
#include <wx/valgen.h>
33
#include <schframe.h>
34
#include <base_units.h>
plyatov's avatar
plyatov committed
35

36 37 38 39 40
#include <class_drawpanel.h>
#include <general.h>
#include <drawtxt.h>
#include <confirm.h>
#include <sch_text.h>
41

42 43 44 45 46 47
#include <dialog_edit_label_base.h>

class SCH_EDIT_FRAME;
class SCH_TEXT;


48
class DIALOG_LABEL_EDITOR : public DIALOG_LABEL_EDITOR_BASE
49 50
{
public:
51
    DIALOG_LABEL_EDITOR( SCH_EDIT_FRAME* parent, SCH_TEXT* aTextItem );
52 53 54 55 56 57 58 59 60 61 62 63 64

private:
    void InitDialog( );
    virtual void OnEnterKey( wxCommandEvent& aEvent );
    virtual void OnOkClick( wxCommandEvent& aEvent );
    virtual void OnCancelClick( wxCommandEvent& aEvent );
    void TextPropertiesAccept( wxCommandEvent& aEvent );

    SCH_EDIT_FRAME* m_Parent;
    SCH_TEXT*       m_CurrentText;
    wxTextCtrl*     m_textLabel;
};

plyatov's avatar
plyatov committed
65

66

charras's avatar
charras committed
67
/* Edit the properties of the text (Label, Global label, graphic text).. )
68
 *  pointed by "aTextStruct"
charras's avatar
charras committed
69
 */
70
void SCH_EDIT_FRAME::EditSchematicText( SCH_TEXT* aTextItem )
71 72
{
    if( aTextItem == NULL )
charras's avatar
charras committed
73
        return;
74

75
    DIALOG_LABEL_EDITOR dialog( this, aTextItem );
76

77
    dialog.ShowModal();
78 79 80
}


81 82
DIALOG_LABEL_EDITOR::DIALOG_LABEL_EDITOR( SCH_EDIT_FRAME* aParent, SCH_TEXT* aTextItem ) :
    DIALOG_LABEL_EDITOR_BASE( aParent )
plyatov's avatar
plyatov committed
83
{
84 85
    m_Parent = aParent;
    m_CurrentText = aTextItem;
charras's avatar
charras committed
86 87
    InitDialog();

88 89 90 91 92
    GetSizer()->SetSizeHints( this );
    Layout();
    Fit();
    SetMinSize( GetBestSize() );

charras's avatar
charras committed
93
    Centre();
plyatov's avatar
plyatov committed
94 95
}

dickelbeck's avatar
dickelbeck committed
96

97
void DIALOG_LABEL_EDITOR::InitDialog()
plyatov's avatar
plyatov committed
98
{
dickelbeck's avatar
dickelbeck committed
99
    wxString msg;
100
    bool multiLine = false;
charras's avatar
charras committed
101

102
    if( m_CurrentText->IsMultilineAllowed() )
103
    {
104
        m_textLabel = m_textLabelMultiLine;
105
        m_textLabelSingleLine->Show( false );
106
        multiLine = true;
107
    }
108 109 110
    else
    {
        m_textLabel = m_textLabelSingleLine;
111 112 113 114 115 116 117
        m_textLabelMultiLine->Show( false );
        wxTextValidator* validator = (wxTextValidator*) m_textLabel->GetValidator();
        wxArrayString excludes;

        // Add invalid label characters to this list.
        excludes.Add( wxT( " " ) );
        validator->SetExcludes( excludes );
118
    }
119

120
    m_textLabel->SetValue( m_CurrentText->GetText() );
121
    m_textLabel->SetFocus();
122

123 124
    switch( m_CurrentText->Type() )
    {
125
    case SCH_GLOBAL_LABEL_T:
dickelbeck's avatar
dickelbeck committed
126 127 128
        SetTitle( _( "Global Label Properties" ) );
        break;

129
    case SCH_HIERARCHICAL_LABEL_T:
130
        SetTitle( _( "Hierarchical Label Properties" ) );
dickelbeck's avatar
dickelbeck committed
131
        break;
132

133
    case SCH_LABEL_T:
dickelbeck's avatar
dickelbeck committed
134 135 136
        SetTitle( _( "Label Properties" ) );
        break;

137
    case SCH_SHEET_PIN_T:
138 139 140
        SetTitle( _( "Hierarchical Sheet Pin Properties." ) );
        break;

dickelbeck's avatar
dickelbeck committed
141 142
    default:
        SetTitle( _( "Text Properties" ) );
143
        m_textLabel->Disconnect( wxEVT_COMMAND_TEXT_ENTER,
144
                                 wxCommandEventHandler ( DIALOG_LABEL_EDITOR::OnEnterKey ),
145
                                 NULL, this );
dickelbeck's avatar
dickelbeck committed
146 147
        break;
    }
148

149
    const int MINTEXTWIDTH = 40;    // M's are big characters, a few establish a lot of width
150

151
    int max_len = 0;
152

153
    if ( !multiLine )
dickelbeck's avatar
dickelbeck committed
154
    {
155
        max_len = m_CurrentText->GetText().Length();
156
    }
157 158
    else
    {
159 160
        // calculate the length of the biggest line
        // we cannot use the length of the entire text that has no meaning
161
        int curr_len = MINTEXTWIDTH;
162
        int imax = m_CurrentText->GetText().Length();
163

164 165
        for( int count = 0; count < imax; count++ )
        {
166 167
            if( m_CurrentText->GetText()[count] == '\n' ||
                m_CurrentText->GetText()[count] == '\r' ) // new line
168 169 170
            {
                curr_len = 0;
            }
171 172 173
            else
            {
                curr_len++;
174

175 176 177 178 179
                if ( max_len < curr_len )
                    max_len = curr_len;
            }
        }
    }
180

181 182 183 184 185 186
    if( max_len < MINTEXTWIDTH )
        max_len = MINTEXTWIDTH;

    wxString textWidth;
    textWidth.Append( 'M', MINTEXTWIDTH );
    EnsureTextCtrlWidth( m_textLabel, &textWidth );
dickelbeck's avatar
dickelbeck committed
187 188

    // Set validators
189
    m_TextOrient->SetSelection( m_CurrentText->GetOrientation() );
190
    m_TextShape->SetSelection( m_CurrentText->GetShape() );
191

192
    int style = 0;
193

194
    if( m_CurrentText->IsItalic() )
195
        style = 1;
196

197
    if( m_CurrentText->IsBold() )
198
        style += 2;
dickelbeck's avatar
dickelbeck committed
199 200

    m_TextStyle->SetSelection( style );
201

202
    wxString units = ReturnUnitSymbol( g_UserUnit, wxT( "(%s)" ) );
203
    msg.Printf( _( "H%s x W%s" ), GetChars( units ), GetChars( units ) );
204
    m_staticSizeUnits->SetLabel( msg );
205

206
    msg = StringFromValue( g_UserUnit, m_CurrentText->GetSize().x );
dickelbeck's avatar
dickelbeck committed
207
    m_TextSize->SetValue( msg );
charras's avatar
charras committed
208

209 210
    if( m_CurrentText->Type() != SCH_GLOBAL_LABEL_T
     && m_CurrentText->Type() != SCH_HIERARCHICAL_LABEL_T )
dickelbeck's avatar
dickelbeck committed
211 212 213
    {
        m_TextShape->Show( false );
    }
214 215

    m_sdbSizer1OK->SetDefault();
plyatov's avatar
plyatov committed
216 217
}

218

219
/*!
220
 * wxTE_PROCESS_ENTER  event handler for m_textLabel
221 222
 */

223
void DIALOG_LABEL_EDITOR::OnEnterKey( wxCommandEvent& aEvent )
224
{
225
    TextPropertiesAccept( aEvent );
226
}
227

228

plyatov's avatar
plyatov committed
229 230 231 232
/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
 */

233
void DIALOG_LABEL_EDITOR::OnOkClick( wxCommandEvent& aEvent )
plyatov's avatar
plyatov committed
234
{
235
    TextPropertiesAccept( aEvent );
plyatov's avatar
plyatov committed
236 237
}

dickelbeck's avatar
dickelbeck committed
238

plyatov's avatar
plyatov committed
239
/*!
240
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
plyatov's avatar
plyatov committed
241 242
 */

243
void DIALOG_LABEL_EDITOR::OnCancelClick( wxCommandEvent& aEvent )
plyatov's avatar
plyatov committed
244
{
245
    m_Parent->GetCanvas()->MoveCursorToCrossHair();
charras's avatar
charras committed
246
    EndModal( wxID_CANCEL );
plyatov's avatar
plyatov committed
247
}
248

249

250
void DIALOG_LABEL_EDITOR::TextPropertiesAccept( wxCommandEvent& aEvent )
charras's avatar
charras committed
251 252 253 254 255
{
    wxString text;
    int      value;

    /* save old text in undo list if not already in edit */
256 257 258
    /* or the label to be edited is part of a block */
    if( m_CurrentText->GetFlags() == 0 ||
        m_Parent->GetScreen()->m_BlockLocate.GetState() != STATE_NO_BLOCK )
charras's avatar
charras committed
259 260
        m_Parent->SaveCopyInUndoList( m_CurrentText, UR_CHANGED );

261
    m_Parent->GetCanvas()->RefreshDrawingRect( m_CurrentText->GetBoundingBox() );
charras's avatar
charras committed
262

263
    text = m_textLabel->GetValue();
264

charras's avatar
charras committed
265
    if( !text.IsEmpty() )
266
        m_CurrentText->SetText( text );
267
    else if( !m_CurrentText->IsNew() )
268
    {
charras's avatar
charras committed
269
        DisplayError( this, _( "Empty Text!" ) );
270 271
        return;
    }
charras's avatar
charras committed
272

273
    m_CurrentText->SetOrientation( m_TextOrient->GetSelection() );
charras's avatar
charras committed
274
    text  = m_TextSize->GetValue();
275
    value = ValueFromString( g_UserUnit, text );
276
    m_CurrentText->SetSize( wxSize( value, value ) );
277

charras's avatar
charras committed
278
    if( m_TextShape )
279
        m_CurrentText->SetShape( m_TextShape->GetSelection() );
charras's avatar
charras committed
280 281

    int style = m_TextStyle->GetSelection();
282

283
    m_CurrentText->SetItalic( ( style & 1 ) );
charras's avatar
charras committed
284 285 286

    if( ( style & 2 ) )
    {
287 288
        m_CurrentText->SetBold( true );
        m_CurrentText->SetThickness( GetPenSizeForBold( m_CurrentText->GetSize().x ) );
charras's avatar
charras committed
289 290 291
    }
    else
    {
292 293
        m_CurrentText->SetBold( false );
        m_CurrentText->SetThickness( 0 );
charras's avatar
charras committed
294 295
    }

296
    m_Parent->OnModify();
charras's avatar
charras committed
297

298
    // Make the text size the new default size ( if it is a new text ):
299
    if( m_CurrentText->IsNew() )
300
        SetDefaultTextSize( m_CurrentText->GetSize().x );
charras's avatar
charras committed
301

302 303
    m_Parent->GetCanvas()->RefreshDrawingRect( m_CurrentText->GetBoundingBox() );
    m_Parent->GetCanvas()->MoveCursorToCrossHair();
304
    EndModal( wxID_OK );
charras's avatar
charras committed
305
}