summaryrefslogtreecommitdiff
path: root/common/base_screen.cpp
blob: 8fa0b7db8346c6fdfdbb6f5dd620134cc5f0e70c (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2012 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 base_screen.cpp
 * @brief BASE_SCREEN object implementation.
 */

#include <fctsys.h>
#include <macros.h>
#include <common.h>
#include <base_struct.h>
#include <class_base_screen.h>
#include <id.h>
#include <base_units.h>

wxString BASE_SCREEN::m_PageLayoutDescrFileName;   // the name of the page layout descr file.

BASE_SCREEN::BASE_SCREEN( KICAD_T aType ) :
    EDA_ITEM( aType )
{
    m_UndoRedoCountMax = DEFAULT_MAX_UNDO_ITEMS;
    m_FirstRedraw      = true;
    m_ScreenNumber     = 1;
    m_NumberOfScreens  = 1;      // Hierarchy: Root: ScreenNumber = 1
    m_Zoom             = 32.0;
    m_Grid.m_Size      = wxRealPoint( 50, 50 );   // Default grid size
    m_Grid.m_CmdId     = ID_POPUP_GRID_LEVEL_50;
    m_Center           = true;
    m_IsPrinting       = false;
    m_ScrollPixelsPerUnitX = 1;
    m_ScrollPixelsPerUnitY = 1;

    m_FlagModified     = false;     // Set when any change is made on board.
    m_FlagSave         = false;     // Used in auto save set when an auto save is required.

    SetCurItem( NULL );
}


BASE_SCREEN::~BASE_SCREEN()
{
}


void BASE_SCREEN::InitDataPoints( const wxSize& aPageSizeIU )
{
    if( m_Center )
    {
        m_crossHairPosition.x = 0;
        m_crossHairPosition.y = 0;

        m_DrawOrg.x = -aPageSizeIU.x / 2;
        m_DrawOrg.y = -aPageSizeIU.y / 2;
    }
    else
    {
        m_crossHairPosition.x = aPageSizeIU.x / 2;
        m_crossHairPosition.y = aPageSizeIU.y / 2;

        m_DrawOrg.x = 0;
        m_DrawOrg.y = 0;
    }

    m_O_Curseur.x = m_O_Curseur.y = 0;
}


double BASE_SCREEN::GetScalingFactor() const
{
    double scale = 1.0 / GetZoom();
    return scale;
}


void BASE_SCREEN::SetScalingFactor( double aScale )
{
    // Limit zoom to max and min allowed values:
    double zoom = Clamp( GetMinAllowedZoom(), aScale, GetMaxAllowedZoom() );

    SetZoom( zoom );
}


bool BASE_SCREEN::SetFirstZoom()
{
    return SetZoom( GetMinAllowedZoom() );
}


bool BASE_SCREEN::SetLastZoom()
{
    return SetZoom( GetMaxAllowedZoom() );
}


bool BASE_SCREEN::SetZoom( double iu_per_du )
{
    if( iu_per_du == m_Zoom )
        return false;

    //wxLogDebug( "Zoom:%.16g  1/Zoom:%.16g", iu_per_du, 1/iu_per_du );

    if( iu_per_du < GetMinAllowedZoom() )
        return false;

    if( iu_per_du > GetMaxAllowedZoom() )
        return false;

    m_Zoom = iu_per_du;

    return true;
}


bool BASE_SCREEN::SetNextZoom()
{
    for( unsigned i=0; i < m_ZoomList.size();  ++i )
    {
        if( m_Zoom < m_ZoomList[i] )
        {
            SetZoom( m_ZoomList[i] );
            return true;
        }
    }

    return false;
}


bool BASE_SCREEN::SetPreviousZoom()
{
    for( unsigned i = m_ZoomList.size(); i != 0;  --i )
    {
        if( m_Zoom > m_ZoomList[i - 1] )
        {
            SetZoom( m_ZoomList[i - 1] );
            return true;
        }
    }

    return false;
}

/* Build the list of human readable grid list.
 * The list shows the grid size both in mils or mm.
 * aMmFirst = true to have mm first and mils after
 *            false to have mils first and mm after
 */
int BASE_SCREEN::BuildGridsChoiceList( wxArrayString& aGridsList, bool aMmFirst) const
{
    wxString msg;
    wxRealPoint curr_grid_size = GetGridSize();
    int idx = -1;
    int idx_usergrid = -1;

    for( size_t i = 0; i < GetGridCount(); i++ )
    {
        const GRID_TYPE& grid = m_grids[i];
        double gridValueMils = To_User_Unit( INCHES, grid.m_Size.x ) * 1000;
        double gridValue_mm = To_User_Unit( MILLIMETRES, grid.m_Size.x );

        if( grid.m_CmdId == ID_POPUP_GRID_USER )
        {
            msg = _( "User Grid" );
            idx_usergrid = i;
        }
        else
        {
            if( aMmFirst )
                msg.Printf( _( "Grid: %.4f mm (%.2f mils)" ),
                            gridValue_mm, gridValueMils );
            else
                msg.Printf( _( "Grid: %.2f mils (%.4f mm)" ),
                            gridValueMils, gridValue_mm );
        }

        aGridsList.Add( msg );

        if( curr_grid_size == grid.m_Size )
            idx = i;
    }

    if( idx < 0 )
        idx = idx_usergrid;

    return idx;
}


void BASE_SCREEN::SetGridList( GRIDS& gridlist )
{
    if( !m_grids.empty() )
        m_grids.clear();

    m_grids = gridlist;
}


int BASE_SCREEN::SetGrid( const wxRealPoint& size )
{
    wxASSERT( !m_grids.empty() );

    GRID_TYPE nearest_grid = m_grids[0];
    int gridIdx = 0;

    for( unsigned i = 0; i < m_grids.size(); i++ )
    {
        if( m_grids[i].m_Size == size )
        {
            m_Grid = m_grids[i];
            return m_grids[i].m_CmdId - ID_POPUP_GRID_LEVEL_1000;
        }

        // keep track of the nearest larger grid size, if the exact size is not found
        if ( size.x < m_grids[i].m_Size.x )
        {
            gridIdx = m_grids[i].m_CmdId - ID_POPUP_GRID_LEVEL_1000;
            nearest_grid = m_grids[i];
        }
    }

    m_Grid = nearest_grid;

    wxLogWarning( wxT( "Grid size( %f, %f ) not in grid list, falling back " ) \
                  wxT( "to grid size( %f, %f )." ),
                  size.x, size.y, m_Grid.m_Size.x, m_Grid.m_Size.y );

    return gridIdx;
}


int BASE_SCREEN::SetGrid( int aCommandId  )
{
    wxASSERT( !m_grids.empty() );

    for( unsigned i = 0; i < m_grids.size(); i++ )
    {
        if( m_grids[i].m_CmdId == aCommandId )
        {
            m_Grid = m_grids[i];
            return m_grids[i].m_CmdId - ID_POPUP_GRID_LEVEL_1000;
        }
    }

    m_Grid = m_grids[0];

    wxLogWarning( wxT( "Grid ID %d not in grid list, falling back to " ) \
                  wxT( "grid size( %g, %g )." ), aCommandId,
                  m_Grid.m_Size.x, m_Grid.m_Size.y );

    return m_grids[0].m_CmdId - ID_POPUP_GRID_LEVEL_1000;
}



void BASE_SCREEN::AddGrid( const GRID_TYPE& grid )
{
    for( unsigned i = 0; i < m_grids.size(); i++ )
    {
        if( m_grids[i].m_Size == grid.m_Size && grid.m_CmdId != ID_POPUP_GRID_USER )
        {
            wxLogDebug( wxT( "Discarding duplicate grid size( %g, %g )." ),
                        grid.m_Size.x, grid.m_Size.y );
            return;
        }

        if( m_grids[i].m_CmdId == grid.m_CmdId )
        {
            wxLogDebug( wxT( "Changing grid ID %d from size( %g, %g ) to " ) \
                        wxT( "size( %g, %g )." ),
                        grid.m_CmdId, m_grids[i].m_Size.x,
                        m_grids[i].m_Size.y, grid.m_Size.x, grid.m_Size.y );
            m_grids[i].m_Size = grid.m_Size;
            return;
        }
    }

    m_grids.push_back( grid );
}


void BASE_SCREEN::AddGrid( const wxRealPoint& size, int id )
{
    GRID_TYPE grid;

    grid.m_Size = size;
    grid.m_CmdId = id;
    AddGrid( grid );
}


void BASE_SCREEN::AddGrid( const wxRealPoint& size, EDA_UNITS_T aUnit, int id )
{
    wxRealPoint new_size;
    GRID_TYPE new_grid;

    new_size.x = From_User_Unit( aUnit, size.x );
    new_size.y = From_User_Unit( aUnit, size.y );
    new_grid.m_CmdId = id;
    new_grid.m_Size = new_size;

    AddGrid( new_grid );
}


GRID_TYPE& BASE_SCREEN::GetGrid( size_t aIndex )
{
    wxCHECK_MSG( !m_grids.empty() && aIndex < m_grids.size(), m_Grid,
                 wxT( "Cannot get grid object outside the bounds of the grid list." ) );

    return m_grids[ aIndex ];
}


bool BASE_SCREEN::GridExists( int aCommandId )
{
    // tests for grid command ID (not an index in grid list, but a wxID) exists in grid list.
    for( unsigned i = 0; i < m_grids.size(); i++ )
    {
        if( m_grids[i].m_CmdId == aCommandId )
            return true;
    }

    return false;
}


wxPoint BASE_SCREEN::getNearestGridPosition( const wxPoint& aPosition,
    const wxPoint& aGridOrigin, wxRealPoint* aGridSize ) const
{
    wxPoint     pt;
    wxRealPoint gridSize;

    if( aGridSize )
        gridSize = *aGridSize;
    else
        gridSize = GetGridSize();

    double  offset = fmod( aGridOrigin.x, gridSize.x );
    int x = KiROUND( (aPosition.x - offset) / gridSize.x );

    pt.x = KiROUND( x * gridSize.x + offset );

    offset = fmod( aGridOrigin.y, gridSize.y );

    int y = KiROUND( (aPosition.y - offset) / gridSize.y );
    pt.y = KiROUND ( y * gridSize.y + offset );

    return pt;
}


wxPoint BASE_SCREEN::getCursorPosition( bool aOnGrid, const wxPoint& aGridOrigin, wxRealPoint* aGridSize ) const
{
    if( aOnGrid )
        return getNearestGridPosition( m_crossHairPosition, aGridOrigin, aGridSize );

    return m_crossHairPosition;
}


wxPoint BASE_SCREEN::getCrossHairScreenPosition() const
{
    wxPoint pos = m_crossHairPosition - m_DrawOrg;
    double  scalar = GetScalingFactor();

    pos.x = KiROUND( (double) pos.x * scalar );
    pos.y = KiROUND( (double) pos.y * scalar );

    return pos;
}


void BASE_SCREEN::setCrossHairPosition( const wxPoint& aPosition, const wxPoint& aGridOrigin, bool aSnapToGrid )
{
    if( aSnapToGrid )
        m_crossHairPosition = getNearestGridPosition( aPosition, aGridOrigin, NULL );
    else
        m_crossHairPosition = aPosition;
}


void BASE_SCREEN::ClearUndoRedoList()
{
    ClearUndoORRedoList( m_UndoList );
    ClearUndoORRedoList( m_RedoList );
}


void BASE_SCREEN::PushCommandToUndoList( PICKED_ITEMS_LIST* aNewitem )
{
    m_UndoList.PushCommand( aNewitem );

    // Delete the extra items, if count max reached
    if( m_UndoRedoCountMax > 0 )
    {
        int extraitems = GetUndoCommandCount() - m_UndoRedoCountMax;
        if( extraitems > 0 )
            ClearUndoORRedoList( m_UndoList, extraitems );
    }
}


void BASE_SCREEN::PushCommandToRedoList( PICKED_ITEMS_LIST* aNewitem )
{
    m_RedoList.PushCommand( aNewitem );

    // Delete the extra items, if count max reached
    if( m_UndoRedoCountMax > 0 )
    {
        int extraitems = GetRedoCommandCount() - m_UndoRedoCountMax;
        if( extraitems > 0 )
            ClearUndoORRedoList( m_RedoList, extraitems );
    }
}


PICKED_ITEMS_LIST* BASE_SCREEN::PopCommandFromUndoList( )
{
    return m_UndoList.PopCommand( );
}


PICKED_ITEMS_LIST* BASE_SCREEN::PopCommandFromRedoList( )
{
    return m_RedoList.PopCommand( );
}


#if defined(DEBUG)

void BASE_SCREEN::Show( int nestLevel, std::ostream& os ) const
{
    // for now, make it look like XML, expand on this later.
    NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << ">\n";

    /* this class will eventually go away, but here's a place holder until then.
    for( EDA_ITEM* item = m_drawList;  item;  item = item->Next() )
    {
        item->Show( nestLevel+1, os );
    }
    */

    NestedSpace( nestLevel, os ) << "</" << GetClass().Lower().mb_str() << ">\n";
}

#endif