dialog_edit_label.cpp 7.72 KB
Newer Older
1 2 3 4 5 6 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 source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2008 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * 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 33
#include <fctsys.h>
#include <wx/valgen.h>
#include <wxEeschemaStruct.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 103
    if( m_CurrentText->m_MultilineAllowed )
    {
104 105
        m_textLabel = m_textLabelMultiLine;
        m_textLabelSingleLine->Show(false);
106
        multiLine = true;
107
    }
108 109 110 111 112
    else
    {
        m_textLabel = m_textLabelSingleLine;
        m_textLabelMultiLine->Show(false);
    }
113

114 115
    m_textLabel->SetValue( m_CurrentText->m_Text );
    m_textLabel->SetFocus();
116

117 118
    switch( m_CurrentText->Type() )
    {
119
    case SCH_GLOBAL_LABEL_T:
dickelbeck's avatar
dickelbeck committed
120 121 122
        SetTitle( _( "Global Label Properties" ) );
        break;

123
    case SCH_HIERARCHICAL_LABEL_T:
124
        SetTitle( _( "Hierarchical Label Properties" ) );
dickelbeck's avatar
dickelbeck committed
125
        break;
126

127
    case SCH_LABEL_T:
dickelbeck's avatar
dickelbeck committed
128 129 130
        SetTitle( _( "Label Properties" ) );
        break;

131
    case SCH_SHEET_PIN_T:
132 133 134
        SetTitle( _( "Hierarchical Sheet Pin Properties." ) );
        break;

dickelbeck's avatar
dickelbeck committed
135 136
    default:
        SetTitle( _( "Text Properties" ) );
137
        m_textLabel->Disconnect( wxEVT_COMMAND_TEXT_ENTER,
138
                                 wxCommandEventHandler ( DIALOG_LABEL_EDITOR::OnEnterKey ),
139
                                 NULL, this );
dickelbeck's avatar
dickelbeck committed
140 141
        break;
    }
142

143
    int MINTEXTWIDTH = 40;    // M's are big characters, a few establish a lot of width
144

145
    int max_len = 0;
146

147
    if ( !multiLine )
dickelbeck's avatar
dickelbeck committed
148
    {
149
        max_len =m_CurrentText->m_Text.Length();
150
    }
151 152
    else
    {
153 154
        // calculate the length of the biggest line
        // we cannot use the length of the entire text that has no meaning
155
        int curr_len = MINTEXTWIDTH;
156
        int imax = m_CurrentText->m_Text.Len();
157

158 159 160
        for( int count = 0; count < imax; count++ )
        {
            if( m_CurrentText->m_Text[count] == '\n' ||
161 162 163 164
                m_CurrentText->m_Text[count] == '\r' ) // new line
            {
                curr_len = 0;
            }
165 166 167
            else
            {
                curr_len++;
168

169 170 171 172 173
                if ( max_len < curr_len )
                    max_len = curr_len;
            }
        }
    }
174

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

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

    // Set validators
183
    m_TextOrient->SetSelection( m_CurrentText->GetOrientation() );
184
    m_TextShape->SetSelection( m_CurrentText->GetShape() );
185

186
    int style = 0;
187

dickelbeck's avatar
dickelbeck committed
188
    if( m_CurrentText->m_Italic )
189
        style = 1;
190

charras's avatar
charras committed
191
    if( m_CurrentText->m_Bold )
192
        style += 2;
dickelbeck's avatar
dickelbeck committed
193 194

    m_TextStyle->SetSelection( style );
195

196
    wxString units = ReturnUnitSymbol( g_UserUnit, wxT( "(%s)" ) );
197 198
    msg = _( "H" ) + units + _( " x W" ) + units;
    m_staticSizeUnits->SetLabel( msg );
199

200
    msg = ReturnStringFromValue( g_UserUnit, m_CurrentText->m_Size.x );
dickelbeck's avatar
dickelbeck committed
201
    m_TextSize->SetValue( msg );
charras's avatar
charras committed
202

203 204
    if( m_CurrentText->Type() != SCH_GLOBAL_LABEL_T
     && m_CurrentText->Type() != SCH_HIERARCHICAL_LABEL_T )
dickelbeck's avatar
dickelbeck committed
205 206 207
    {
        m_TextShape->Show( false );
    }
208 209

    m_sdbSizer1OK->SetDefault();
plyatov's avatar
plyatov committed
210 211
}

212

213
/*!
214
 * wxTE_PROCESS_ENTER  event handler for m_textLabel
215 216
 */

217
void DIALOG_LABEL_EDITOR::OnEnterKey( wxCommandEvent& aEvent )
218
{
219
    TextPropertiesAccept( aEvent );
220
}
221

222

plyatov's avatar
plyatov committed
223 224 225 226
/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
 */

227
void DIALOG_LABEL_EDITOR::OnOkClick( wxCommandEvent& aEvent )
plyatov's avatar
plyatov committed
228
{
229
    TextPropertiesAccept( aEvent );
plyatov's avatar
plyatov committed
230 231
}

dickelbeck's avatar
dickelbeck committed
232

plyatov's avatar
plyatov committed
233
/*!
234
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
plyatov's avatar
plyatov committed
235 236
 */

237
void DIALOG_LABEL_EDITOR::OnCancelClick( wxCommandEvent& aEvent )
plyatov's avatar
plyatov committed
238
{
239
    m_Parent->GetCanvas()->MoveCursorToCrossHair();
charras's avatar
charras committed
240
    EndModal( wxID_CANCEL );
plyatov's avatar
plyatov committed
241
}
242

243

244
void DIALOG_LABEL_EDITOR::TextPropertiesAccept( wxCommandEvent& aEvent )
charras's avatar
charras committed
245 246 247 248 249
{
    wxString text;
    int      value;

    /* save old text in undo list if not already in edit */
250
    if( m_CurrentText->GetFlags() == 0 )
charras's avatar
charras committed
251 252
        m_Parent->SaveCopyInUndoList( m_CurrentText, UR_CHANGED );

253
    m_Parent->GetCanvas()->RefreshDrawingRect( m_CurrentText->GetBoundingBox() );
charras's avatar
charras committed
254

255
    text = m_textLabel->GetValue();
256

charras's avatar
charras committed
257 258
    if( !text.IsEmpty() )
        m_CurrentText->m_Text = text;
259
    else if( !m_CurrentText->IsNew() )
260
    {
charras's avatar
charras committed
261
        DisplayError( this, _( "Empty Text!" ) );
262 263
        return;
    }
charras's avatar
charras committed
264

265
    m_CurrentText->SetOrientation( m_TextOrient->GetSelection() );
charras's avatar
charras committed
266
    text  = m_TextSize->GetValue();
267
    value = ReturnValueFromString( g_UserUnit, text );
charras's avatar
charras committed
268
    m_CurrentText->m_Size.x = m_CurrentText->m_Size.y = value;
269

charras's avatar
charras committed
270
    if( m_TextShape )
271
        m_CurrentText->SetShape( m_TextShape->GetSelection() );
charras's avatar
charras committed
272 273

    int style = m_TextStyle->GetSelection();
274

charras's avatar
charras committed
275 276 277 278 279 280 281 282
    if( ( style & 1 ) )
        m_CurrentText->m_Italic = 1;
    else
        m_CurrentText->m_Italic = 0;

    if( ( style & 2 ) )
    {
        m_CurrentText->m_Bold  = true;
283
        m_CurrentText->m_Thickness = GetPenSizeForBold( m_CurrentText->m_Size.x );
charras's avatar
charras committed
284 285 286 287
    }
    else
    {
        m_CurrentText->m_Bold  = false;
288
        m_CurrentText->m_Thickness = 0;
charras's avatar
charras committed
289 290
    }

291
    m_Parent->OnModify();
charras's avatar
charras committed
292 293

    /* Make the text size as new default size if it is a new text */
294
    if( m_CurrentText->IsNew() )
295
        m_Parent->SetDefaultLabelSize( m_CurrentText->m_Size.x );
charras's avatar
charras committed
296

297 298
    m_Parent->GetCanvas()->RefreshDrawingRect( m_CurrentText->GetBoundingBox() );
    m_Parent->GetCanvas()->MoveCursorToCrossHair();
299
    EndModal( wxID_OK );
charras's avatar
charras committed
300
}