controle.cpp 11.3 KB
Newer Older
dickelbeck's avatar
dickelbeck committed
1 2 3
/********************************************************/
/* Routines generales de gestion des commandes usuelles */
/********************************************************/
plyatov's avatar
plyatov committed
4

CHARRAS's avatar
CHARRAS committed
5
/* controle.cpp */
plyatov's avatar
plyatov committed
6 7 8

#include "fctsys.h"
#include "common.h"
9
#include "class_drawpanel.h"
plyatov's avatar
plyatov committed
10
#include "pcbnew.h"
charras's avatar
charras committed
11
#include "wxPcbStruct.h"
12
#include "protos.h"
13
#include "pcbnew_id.h"
charras's avatar
charras committed
14

15
#include "collectors.h"
plyatov's avatar
plyatov committed
16

charras's avatar
charras committed
17 18 19
//external funtions used here:
extern bool Magnetize( BOARD* m_Pcb, WinEDA_PcbFrame* frame,
                       int aCurrentTool, wxSize grid, wxPoint on_grid, wxPoint* curpos );
plyatov's avatar
plyatov committed
20

21 22 23
/*************************************************************************************/
static BOARD_ITEM* AllAreModulesAndReturnSmallestIfSo( GENERAL_COLLECTOR* aCollector )
/*************************************************************************************/
24 25 26 27 28
/**
 * Function AllAreModulesAndReturnSmallestIfSo
 * tests that all items in the collection are MODULEs and if so, returns the
 * smallest MODULE.
 * @return BOARD_ITEM* - The smallest or NULL.
29
 */
30 31
{
    int count = aCollector->GetCount();
32 33

    for( int i = 0; i<count;  ++i )
34
    {
35
        if( (*aCollector)[i]->Type() != TYPE_MODULE )
36 37
            return NULL;
    }
38

39 40 41 42
    // all are modules, now find smallest MODULE

    int minDim = 0x7FFFFFFF;
    int minNdx = 0;
43 44

    for( int i = 0;  i<count;  ++i )
45 46
    {
        MODULE* module = (MODULE*) (*aCollector)[i];
47 48 49 50 51 52

        int     lx = module->m_BoundaryBox.GetWidth();
        int     ly = module->m_BoundaryBox.GetHeight();

        int     lmin = MIN( lx, ly );

53 54 55 56 57 58
        if( lmin <= minDim )
        {
            minDim = lmin;
            minNdx = i;
        }
    }
59

60 61 62 63
    return (*aCollector)[minNdx];
}


dickelbeck's avatar
dickelbeck committed
64 65 66
/****************************************************************************/
BOARD_ITEM* WinEDA_BasePcbFrame::PcbGeneralLocateAndDisplay( int aHotKeyCode )
/****************************************************************************/
67
{
68 69 70
    BOARD_ITEM* item;

    GENERAL_COLLECTORS_GUIDE guide = GetCollectorsGuide();
71

dickelbeck's avatar
dickelbeck committed
72 73
    // Assign to scanList the proper item types desired based on tool type
    // or hotkey that is in play.
74

75
    const KICAD_T* scanList = NULL;
76

dickelbeck's avatar
dickelbeck committed
77 78
    if( aHotKeyCode )
    {
dickelbeck's avatar
dickelbeck committed
79 80
        // @todo: add switch here and add calls to PcbGeneralLocateAndDisplay( int aHotKeyCode )
        // when searching is needed from a hotkey handler
dickelbeck's avatar
dickelbeck committed
81 82
    }
    else if( m_ID_current_state == 0 )
83 84 85 86 87 88
    {
        switch( m_HTOOL_current_state )
        {
        case ID_TOOLBARH_PCB_AUTOPLACE:
            scanList = GENERAL_COLLECTOR::ModuleItems;
            break;
89

90
        default:
91
            scanList = DisplayOpt.DisplayZonesMode == 0 ?
92 93
                       GENERAL_COLLECTOR::AllBoardItems :
                       GENERAL_COLLECTOR::AllButZones;
94 95 96 97 98 99 100 101 102
            break;
        }
    }
    else
    {
        switch( m_ID_current_state )
        {
        case ID_PCB_SHOW_1_RATSNEST_BUTT:
            scanList = GENERAL_COLLECTOR::PadsOrModules;
103
            break;
104 105 106

        case ID_TRACK_BUTT:
            scanList = GENERAL_COLLECTOR::Tracks;
107
            break;
108 109 110 111

        case ID_COMPONENT_BUTT:
            scanList = GENERAL_COLLECTOR::ModuleItems;
            break;
112

113
        default:
114
            scanList = DisplayOpt.DisplayZonesMode == 0 ?
115 116
                       GENERAL_COLLECTOR::AllBoardItems :
                       GENERAL_COLLECTOR::AllButZones;
117 118 119
        }
    }

120
    m_Collector->Collect( m_Pcb, scanList, GetScreen()->RefPos( true ), guide );
121

122
#if 0
dickelbeck's avatar
dickelbeck committed
123
    // debugging: print out the collected items, showing their priority order too.
124
    for( int i = 0; i<m_Collector->GetCount();  ++i )
dickelbeck's avatar
dickelbeck committed
125
        (*m_Collector)[i]->Show( 0, std::cout );
126
#endif
127

128
    /* Remove redundancies: sometime, zones are found twice,
129
     * because zones can be filled by overlapping segments (this is a fill option)
130 131
     */
    unsigned long timestampzone = 0;
132

dickelbeck's avatar
dickelbeck committed
133
    for( int ii = 0;  ii < m_Collector->GetCount(); ii++ )
134 135
    {
        item = (*m_Collector)[ii];
136
        if( item->Type() != TYPE_ZONE )
137
            continue;
138

139 140 141 142 143 144 145 146 147
        /* Found a TYPE ZONE */
        if( item->m_TimeStamp == timestampzone )    // Remove it, redundant, zone already found
        {
            m_Collector->Remove( ii );
            ii--;
        }
        else
            timestampzone = item->m_TimeStamp;
    }
148

149
    if( m_Collector->GetCount() <= 1 )
150 151
    {
        item = (*m_Collector)[0];
152
        SetCurItem( item );
153
    }
dickelbeck's avatar
dickelbeck committed
154
    // If the count is 2, and first item is a pad or moduletext, and the 2nd item is its parent module:
155
    else if( m_Collector->GetCount() == 2
156 157 158
             && ( (*m_Collector)[0]->Type() == TYPE_PAD || (*m_Collector)[0]->Type() ==
                 TYPE_TEXTE_MODULE )
             && (*m_Collector)[1]->Type() == TYPE_MODULE && (*m_Collector)[0]->GetParent()==
159
             (*m_Collector)[1] )
160 161
    {
        item = (*m_Collector)[0];
162
        SetCurItem( item );
163
    }
164
    // if all are modules, find the smallest one amoung the primary choices
165
    else if( ( item = AllAreModulesAndReturnSmallestIfSo( m_Collector ) ) != NULL )
166
    {
167
        SetCurItem( item );
168
    }
169

dickelbeck's avatar
dickelbeck committed
170
    else    // we can't figure out which item user wants, do popup menu so user can choose
171
    {
172 173
        wxMenu itemMenu;

174 175
        /* Give a title to the selection menu. This is also a cancel menu item */
        wxMenuItem * item_title = new wxMenuItem(&itemMenu, -1, _( "Selection Clarification" ) );
176
#ifdef __WINDOWS__
177 178 179 180
        wxFont bold_font(*wxNORMAL_FONT);
        bold_font.SetWeight(wxFONTWEIGHT_BOLD);
        bold_font.SetStyle( wxFONTSTYLE_ITALIC);
        item_title->SetFont(bold_font);
181
#endif
182 183
        itemMenu.Append(item_title);
        itemMenu.AppendSeparator();
184

185 186
        int limit = MIN( MAX_ITEMS_IN_PICKER, m_Collector->GetCount() );

187
        for( int i = 0;  i<limit;  ++i )
188
        {
189 190 191
            wxString     text;
            const char** xpm;

192 193
            item = (*m_Collector)[i];

194
            text = item->MenuText( m_Pcb );
195
            xpm  = item->MenuIcon();
196 197

            ADD_MENUITEM( &itemMenu, ID_POPUP_PCB_ITEM_SELECTION_START + i, text, xpm );
198
        }
199

dickelbeck's avatar
dickelbeck committed
200
        /* @todo: rather than assignment to TRUE, these should be increment and decrement operators throughout _everywhere_.
201 202 203 204 205 206
         *  That way we can handle nesting.
         *  But I tried that and found there cases where the assignment to TRUE (converted to a m_IgnoreMouseEvents++ )
         *  was not balanced with the -- (now m_IgnoreMouseEvents=FALSE), so I had to revert.
         *  Somebody should track down these and make them balanced.
         *  DrawPanel->m_IgnoreMouseEvents = TRUE;
         */
207 208

        // this menu's handler is void WinEDA_BasePcbFrame::ProcessItemSelection()
209
        // and it calls SetCurItem() which in turn calls DisplayInfo() on the item.
210 211
        DrawPanel->m_AbortRequest = true;   // changed in false if an item
        PopupMenu( &itemMenu ); // m_AbortRequest = false if an item is selected
212

213
        DrawPanel->MouseToCursorSchema();
214

dickelbeck's avatar
dickelbeck committed
215
//        DrawPanel->m_IgnoreMouseEvents = FALSE;
216

217 218
        // The function ProcessItemSelection() has set the current item, return it.
        item = GetCurItem();
219
    }
220

221
    return item;
plyatov's avatar
plyatov committed
222 223 224 225
}


/****************************************************************/
226
void WinEDA_PcbFrame::GeneralControle( wxDC* DC, wxPoint Mouse )
plyatov's avatar
plyatov committed
227 228
/*****************************************************************/
{
229
    wxRealPoint delta;
dickelbeck's avatar
dickelbeck committed
230 231 232 233 234 235 236
    wxPoint curpos, oldpos;
    int     hotkey = 0;

    ActiveScreen = GetScreen();

    // Save the board after the time out :
    int CurrentTime = time( NULL );
237

dickelbeck's avatar
dickelbeck committed
238
    if( !GetScreen()->IsModify() || GetScreen()->IsSave() )
239
    {
240
        /* If no change, reset the time out */
dickelbeck's avatar
dickelbeck committed
241 242 243 244 245 246
        g_SaveTime = CurrentTime;
    }

    if( (CurrentTime - g_SaveTime) > g_TimeOut )
    {
        wxString tmpFileName = GetScreen()->m_FileName;
247
        wxFileName fn = wxFileName( wxEmptyString, g_SaveFileName, PcbExtBuffer );
dickelbeck's avatar
dickelbeck committed
248
        bool     flgmodify   = GetScreen()->IsModify();
249

250
        SavePcbFile( fn.GetFullPath() );
251

dickelbeck's avatar
dickelbeck committed
252 253 254 255 256 257 258 259 260 261 262 263
        if( flgmodify ) // Set the flags m_Modify cleared by SavePcbFile()
        {
            GetScreen()->SetModify();
            GetScreen()->SetSave(); // Set the flags m_FlagSave cleared by SetModify()
        }
        GetScreen()->m_FileName = tmpFileName;
        SetTitle( GetScreen()->m_FileName );
    }

    curpos = DrawPanel->CursorRealPosition( Mouse );
    oldpos = GetScreen()->m_Curseur;

264
    delta = GetScreen()->GetGridSize();
265
    GetScreen()->Scale( delta );
266

dickelbeck's avatar
dickelbeck committed
267 268
    if( delta.x <= 0 )
        delta.x = 1;
dickelbeck's avatar
dickelbeck committed
269

dickelbeck's avatar
dickelbeck committed
270 271 272 273 274 275 276
    if( delta.y <= 0 )
        delta.y = 1;

    switch( g_KeyPressed )
    {
    case WXK_NUMPAD8:       /* Deplacement curseur vers le haut */
    case WXK_UP:
277
        Mouse.y -= wxRound(delta.y);
dickelbeck's avatar
dickelbeck committed
278 279 280 281 282
        DrawPanel->MouseTo( Mouse );
        break;

    case WXK_NUMPAD2:       /* Deplacement curseur vers le bas */
    case WXK_DOWN:
283
        Mouse.y += wxRound(delta.y);
dickelbeck's avatar
dickelbeck committed
284 285 286 287 288
        DrawPanel->MouseTo( Mouse );
        break;

    case WXK_NUMPAD4:       /* Deplacement curseur vers la gauche */
    case WXK_LEFT:
289
        Mouse.x -= wxRound(delta.x);
dickelbeck's avatar
dickelbeck committed
290 291 292 293 294
        DrawPanel->MouseTo( Mouse );
        break;

    case WXK_NUMPAD6:      /* Deplacement curseur vers la droite */
    case WXK_RIGHT:
295
        Mouse.x += wxRound(delta.x);
dickelbeck's avatar
dickelbeck committed
296 297 298 299 300 301 302 303 304 305 306 307
        DrawPanel->MouseTo( Mouse );
        break;

    default:
        hotkey = g_KeyPressed;
        break;
    }

    /* Put cursor in new position, according to the zoom keys (if any) */
    GetScreen()->m_Curseur = curpos;

    /* Put cursor on grid or a pad centre if requested
308 309
     * But if the tool DELETE is active the cursor is left off grid
     * this is better to reach items to delete off grid
dickelbeck's avatar
dickelbeck committed
310 311 312 313
     */
    bool   keep_on_grid = TRUE;
    if( m_ID_current_state == ID_PCB_DELETE_ITEM_BUTT )
        keep_on_grid = FALSE;
314

dickelbeck's avatar
dickelbeck committed
315
    /* Cursor is left off grid if no block in progress and no moving object */
316
    if( GetScreen()->m_BlockLocate.m_State != STATE_NO_BLOCK )
dickelbeck's avatar
dickelbeck committed
317
        keep_on_grid = TRUE;
318

319
    EDA_BaseStruct* DrawStruct = GetScreen()->GetCurItem();
dickelbeck's avatar
dickelbeck committed
320 321 322
    if( DrawStruct && DrawStruct->m_Flags )
        keep_on_grid = TRUE;

dickelbeck's avatar
dickelbeck committed
323 324 325
    if( keep_on_grid )
    {
        wxPoint on_grid = curpos;
dickelbeck's avatar
dickelbeck committed
326

dickelbeck's avatar
dickelbeck committed
327
        PutOnGrid( &on_grid );
328
        wxSize grid;
329 330
        grid.x = (int) GetScreen()->GetGridSize().x;
        grid.y = (int) GetScreen()->GetGridSize().y;
dickelbeck's avatar
dickelbeck committed
331
        if( Magnetize(m_Pcb, (WinEDA_PcbFrame *) this, m_ID_current_state,
332
                      grid, on_grid, &curpos) )
333
        {
dickelbeck's avatar
dickelbeck committed
334
            GetScreen()->m_Curseur = curpos;
335
        }
dickelbeck's avatar
dickelbeck committed
336 337
        else
        {
338 339
            // If there's no intrusion and DRC is active, we pass the cursor
            // "as is", and let ShowNewTrackWhenMovingCursor figure out what to do.
340 341
            if( !Drc_On || !g_CurrentTrackSegment
                || g_CurrentTrackSegment != this->GetCurItem()
342
                || !LocateIntrusion( m_Pcb->m_Track, g_CurrentTrackSegment ))
343
            {
dickelbeck's avatar
dickelbeck committed
344
                GetScreen()->m_Curseur = on_grid;
345
            }
dickelbeck's avatar
dickelbeck committed
346
        }
dickelbeck's avatar
dickelbeck committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    }

    if( oldpos != GetScreen()->m_Curseur )
    {
        curpos = GetScreen()->m_Curseur;
        GetScreen()->m_Curseur = oldpos;
        DrawPanel->CursorOff( DC );

        GetScreen()->m_Curseur = curpos;
        DrawPanel->CursorOn( DC );

        if( DrawPanel->ManageCurseur )
        {
            DrawPanel->ManageCurseur( DrawPanel, DC, TRUE );
        }
    }

CHARRAS's avatar
CHARRAS committed
364 365 366 367 368
    if( hotkey )
    {
        OnHotKey( DC, hotkey, NULL );
    }

dickelbeck's avatar
dickelbeck committed
369 370 371 372 373
    if( GetScreen()->IsRefreshReq() )
    {
        RedrawActiveWindow( DC, TRUE );
    }

dickelbeck's avatar
dickelbeck committed
374
    SetToolbars();
dickelbeck's avatar
dickelbeck committed
375

376
    UpdateStatusBar();    /* Display new cursor coordinates */
plyatov's avatar
plyatov committed
377
}