summaryrefslogtreecommitdiff
path: root/pagelayout_editor/dialogs/dialog_new_dataitem.cpp
blob: 12a6744bf35e9eb88dab3f8a7e698e130f852f6a (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
 * @file dialog_new_dataitem.cpp
 * @brief a dialog called on creating a new plage layout data item.
*/

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
 *
 * 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
 */

#include <fctsys.h>
#include <common.h>
#include <class_drawpanel.h>

#include <pl_editor_frame.h>
#include <class_worksheet_dataitem.h>
#include <dialog_new_dataitem_base.h>

class DIALOG_NEW_DATAITEM : public DIALOG_NEW_DATAITEM_BASE
{
    WORKSHEET_DATAITEM* m_item;

public:
    DIALOG_NEW_DATAITEM( PL_EDITOR_FRAME* aCaller, WORKSHEET_DATAITEM* aItem );

private:
    void OnCancelClick( wxCommandEvent& event );
    void OnOKClick( wxCommandEvent& event );

    void initDlg();
};


int InvokeDialogNewItem( PL_EDITOR_FRAME* aCaller, WORKSHEET_DATAITEM* aItem )
{
    DIALOG_NEW_DATAITEM dlg( aCaller, aItem );
    return dlg.ShowModal();
}

DIALOG_NEW_DATAITEM::DIALOG_NEW_DATAITEM( PL_EDITOR_FRAME* aCaller,
                                          WORKSHEET_DATAITEM* aItem )
    : DIALOG_NEW_DATAITEM_BASE( aCaller )
{
    m_item = aItem;
    initDlg();

    GetSizer()->SetSizeHints( this );
    Centre();
}

void DIALOG_NEW_DATAITEM::OnCancelClick( wxCommandEvent& event )
{
    EndModal( wxID_CANCEL);
}

void DIALOG_NEW_DATAITEM::OnOKClick( wxCommandEvent& event )
{
    if( m_item->GetType() == WORKSHEET_DATAITEM::WS_TEXT )
    {
        WORKSHEET_DATAITEM_TEXT* text = ((WORKSHEET_DATAITEM_TEXT*)m_item);
        text->m_TextBase = m_textCtrlText->GetValue();
        // For multiline texts, replace the '\n' char by the "\\n" sequence",
        // in internal string
        text->m_TextBase.Replace( wxT("\n"), wxT("\\n") );
    }

    wxString msg;

    // Import Start point
    double dtmp;
    msg = m_textCtrlPosX->GetValue();
    msg.ToDouble( &dtmp );
    m_item->m_Pos.m_Pos.x = dtmp;

    msg = m_textCtrlPosY->GetValue();
    msg.ToDouble( &dtmp );
    m_item->m_Pos.m_Pos.y = dtmp;

    switch( m_choiceCornerPos->GetSelection() )
    {
        case 2: m_item->m_Pos.m_Anchor = RB_CORNER; break;
        case 0: m_item->m_Pos.m_Anchor = RT_CORNER; break;
        case 3: m_item->m_Pos.m_Anchor = LB_CORNER; break;
        case 1: m_item->m_Pos.m_Anchor = LT_CORNER; break;
    }

    // Import End point
    msg = m_textCtrlEndX->GetValue();
    msg.ToDouble( &dtmp );
    m_item->m_End.m_Pos.x = dtmp;

    msg = m_textCtrlEndY->GetValue();
    msg.ToDouble( &dtmp );
    m_item->m_End.m_Pos.y = dtmp;

    switch( m_choiceCornerEnd->GetSelection() )
    {
        case 2: m_item->m_End.m_Anchor = RB_CORNER; break;
        case 0: m_item->m_End.m_Anchor = RT_CORNER; break;
        case 3: m_item->m_End.m_Anchor = LB_CORNER; break;
        case 1: m_item->m_End.m_Anchor = LT_CORNER; break;
    }

    EndModal( wxID_OK);
}

void DIALOG_NEW_DATAITEM::initDlg()
{
    // Disable useless widgets, depending on WORKSHEET_DATAITEM type
    switch( m_item->GetType() )
    {
        case WORKSHEET_DATAITEM::WS_SEGMENT:
        case WORKSHEET_DATAITEM::WS_RECT:
            m_textCtrlText->Enable( false );
            break;

        case WORKSHEET_DATAITEM::WS_BITMAP:
        case WORKSHEET_DATAITEM::WS_POLYPOLYGON:
            m_textCtrlText->Enable( false );
            // fall through
        case WORKSHEET_DATAITEM::WS_TEXT:
            m_textCtrlEndX->Enable( false );
            m_textCtrlEndY->Enable( false );
            m_choiceCornerEnd->Enable( false );
            break;
    }

    wxString msg;

    // Position/ start point
    msg.Printf( wxT("%.3f"), m_item->m_Pos.m_Pos.x );
    m_textCtrlPosX->SetValue( msg );
    msg.Printf( wxT("%.3f"), m_item->m_Pos.m_Pos.y );
    m_textCtrlPosY->SetValue( msg );
    switch(  m_item->m_Pos.m_Anchor )
    {
        case RB_CORNER:      // right bottom corner
            m_choiceCornerPos->SetSelection( 2 ); break;
        case RT_CORNER:      // right top corner
            m_choiceCornerPos->SetSelection( 0 ); break;
        case LB_CORNER:      // left bottom corner
            m_choiceCornerPos->SetSelection( 3 ); break;
        case LT_CORNER:      // left top corner
            m_choiceCornerPos->SetSelection( 1 ); break;
    }

    // End point
    msg.Printf( wxT("%.3f"), m_item->m_End.m_Pos.x );
    m_textCtrlEndX->SetValue( msg );
    msg.Printf( wxT("%.3f"), m_item->m_End.m_Pos.y );
    m_textCtrlEndY->SetValue( msg );
    switch( m_item->m_End.m_Anchor )
    {
        case RB_CORNER:      // right bottom corner
            m_choiceCornerEnd->SetSelection( 2 ); break;
        case RT_CORNER:      // right top corner
            m_choiceCornerEnd->SetSelection( 0 ); break;
        case LB_CORNER:      // left bottom corner
            m_choiceCornerEnd->SetSelection( 3 ); break;
        case LT_CORNER:      // left top corner
            m_choiceCornerEnd->SetSelection( 1 ); break;
    }

    if( m_item->GetType() == WORKSHEET_DATAITEM::WS_TEXT )
        m_textCtrlText->SetValue( ((WORKSHEET_DATAITEM_TEXT*)m_item)->m_TextBase );
}