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

26
/**
27
 * @file hierarch.schframe
28
 */
29

30 31 32
#include <fctsys.h>
#include <class_drawpanel.h>
#include <confirm.h>
33
#include <id.h>
34
#include <schframe.h>
35

36 37 38
#include <general.h>
#include <sch_sheet.h>
#include <sch_sheet_path.h>
39

40 41
#include <wx/imaglist.h>
#include <wx/treectrl.h>
42 43


44 45
enum
{
46
    ID_TREECTRL_HIERARCHY = 1600
47 48 49
};


50
class HIERARCHY_NAVIG_DLG;
51

52
/* This class derived from wxTreeItemData stores the SCH_SHEET_PATH of each
53
 * sheet in hierarchy in each TreeItem, in its associated data buffer
54
*/
55
class TreeItemData : public wxTreeItemData
56 57
{
public:
58
    SCH_SHEET_PATH m_SheetPath;
59 60

    TreeItemData( SCH_SHEET_PATH& sheet ) : wxTreeItemData()
61
    {
62
        m_SheetPath = sheet;
63
    }
64 65
};

66
/* Class to handle hierarchy tree. */
67
class HIERARCHY_TREE : public wxTreeCtrl
68 69
{
private:
70
    HIERARCHY_NAVIG_DLG* m_Parent;
71
    wxImageList*      imageList;
72 73

public:
74 75 76 77 78 79
    HIERARCHY_TREE()
    {
        m_Parent = NULL;
        imageList = NULL;
    }

80
    HIERARCHY_TREE( HIERARCHY_NAVIG_DLG* parent );
81

82
    DECLARE_DYNAMIC_CLASS( HIERARCHY_TREE )
83
};
84

85
IMPLEMENT_DYNAMIC_CLASS( HIERARCHY_TREE, wxTreeCtrl )
86 87


88
HIERARCHY_TREE::HIERARCHY_TREE( HIERARCHY_NAVIG_DLG* parent ) :
89
    wxTreeCtrl( (wxWindow*)parent, ID_TREECTRL_HIERARCHY, wxDefaultPosition, wxDefaultSize,
90
                wxTR_HAS_BUTTONS, wxDefaultValidator, wxT( "HierachyTreeCtrl" ) )
91
{
92
    m_Parent = parent;
93

94
    // Make an image list containing small icons
95 96
    // All icons are expected having the same size.
    wxBitmap tree_nosel_bm( KiBitmap( tree_nosel_xpm ) );
97 98
    imageList = new wxImageList( tree_nosel_bm.GetWidth(),
                                 tree_nosel_bm.GetHeight(), true, 2 );
99

100
    imageList->Add( tree_nosel_bm );
101
    imageList->Add( KiBitmap( tree_sel_xpm ) );
102

103
    AssignImageList( imageList );
104 105
}

106

107
class HIERARCHY_NAVIG_DLG : public wxDialog
108 109
{
public:
110
    SCH_EDIT_FRAME* m_Parent;
111
    HIERARCHY_TREE* m_Tree;
112 113
    int             m_nbsheets;
    wxDC*           m_DC;
114 115

private:
116
    wxSize m_TreeSize;
117
    int    maxposx;
118 119

public:
120
    HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* parent, wxDC* DC, const wxPoint& pos );
121
    void BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu );
122

123
    ~HIERARCHY_NAVIG_DLG();
124 125

    void OnSelect( wxTreeEvent& event );
126 127

private:
128
    void OnQuit( wxCommandEvent& event );
129

130
    DECLARE_EVENT_TABLE()
131 132
};

133 134
BEGIN_EVENT_TABLE( HIERARCHY_NAVIG_DLG, wxDialog )
    EVT_TREE_ITEM_ACTIVATED( ID_TREECTRL_HIERARCHY, HIERARCHY_NAVIG_DLG::OnSelect )
135 136 137
END_EVENT_TABLE()


138
void SCH_EDIT_FRAME::InstallHierarchyFrame( wxDC* DC, wxPoint& pos )
139
{
140
    HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, DC, pos );
141

142 143
    treeframe->ShowModal();
    treeframe->Destroy();
144 145 146
}


147
HIERARCHY_NAVIG_DLG::HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* parent, wxDC* DC, const wxPoint& pos ) :
148 149
    wxDialog( parent, -1, _( "Navigator" ), pos, wxSize( 110, 50 ),
    wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
150
{
151
    wxTreeItemId cellule;
152

153 154
    m_Parent = parent;
    m_DC   = DC;
155
    m_Tree = new HIERARCHY_TREE( this );
156

157
    m_nbsheets = 1;
158

159
    cellule = m_Tree->AddRoot( _( "Root" ), 0, 1 );
160
    m_Tree->SetItemBold( cellule, true );
161

162
    SCH_SHEET_PATH list;
163 164
    list.Push( g_RootSheet );
    m_Tree->SetItemData( cellule, new TreeItemData( list ) );
165

166
    if( m_Parent->GetCurrentSheet().Last() == g_RootSheet )
167
        m_Tree->SelectItem( cellule ); //root.
168

169
    maxposx = 15;
170
    BuildSheetsTree( &list, &cellule );
171

172
    m_Tree->Expand( cellule );
173

174 175 176 177 178
    wxRect itemrect;
    m_Tree->GetBoundingRect( cellule, itemrect );

    // Set dialog window size to be large enough
    m_TreeSize.x = itemrect.GetWidth() + 20;
179
    m_TreeSize.x = std::max( m_TreeSize.x, 250 );
180 181 182 183 184 185

    // Readjust the size of the frame to an optimal value.
    m_TreeSize.y = m_nbsheets * itemrect.GetHeight();
    m_TreeSize.y += 10;

    SetClientSize( m_TreeSize );
186 187
}

188

189
HIERARCHY_NAVIG_DLG::~HIERARCHY_NAVIG_DLG()
190 191 192 193
{
}


194
void HIERARCHY_NAVIG_DLG::OnQuit( wxCommandEvent& event )
195 196
{
    // true is to force the frame to close
197
    Close( true );
198 199
}

200

201
/* Routine to create the hierarchical tree of the schematic
202 203
 * This routine is re-entrant!
 */
204
void HIERARCHY_NAVIG_DLG::BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId*  previousmenu )
205

206
{
207 208 209 210 211 212 213
    wxTreeItemId menu;

    if( m_nbsheets > NB_MAX_SHEET )
    {
        if( m_nbsheets == (NB_MAX_SHEET + 1) )
        {
            wxString msg;
214
            msg << wxT( "BuildSheetsTree: Error: nbsheets > " ) << NB_MAX_SHEET;
215 216 217
            DisplayError( this, msg );
            m_nbsheets++;
        }
218

219 220 221 222
        return;
    }

    maxposx += m_Tree->GetIndent();
223
    SCH_ITEM* schitem = list->LastDrawList();
224

225
    while( schitem && m_nbsheets < NB_MAX_SHEET )
226
    {
227
        if( schitem->Type() == SCH_SHEET_T )
228
        {
229
            SCH_SHEET* sheet = (SCH_SHEET*) schitem;
230
            m_nbsheets++;
231
            menu = m_Tree->AppendItem( *previousmenu, sheet->GetName(), 0, 1 );
232
            list->Push( sheet );
233 234
            m_Tree->SetItemData( menu, new TreeItemData( *list ) );
            int ll = m_Tree->GetItemText( menu ).Len();
235

236
#ifdef __WINDOWS__
237
            ll *= 9;    //  * char width
238
#else
239
            ll *= 12;   //  * char width
240
#endif
241
            ll += maxposx + 20;
242
            m_TreeSize.x  = std::max( m_TreeSize.x, ll );
243
            m_TreeSize.y += 1;
244

245
            if( *list == m_Parent->GetCurrentSheet() )
246 247 248 249
            {
                m_Tree->EnsureVisible( menu );
                m_Tree->SelectItem( menu );
            }
250

251
            BuildSheetsTree( list, &menu );
252 253 254
            m_Tree->Expand( menu );
            list->Pop();
        }
255

256
        schitem = schitem->Next();
257 258 259
    }

    maxposx -= m_Tree->GetIndent();
260 261 262
}


263
/* Called on a double-click on a tree item:
264
 * Open the selected sheet, and display the corresponding screen
265
 */
266
void HIERARCHY_NAVIG_DLG::OnSelect( wxTreeEvent& event )
267

268
{
269
    wxTreeItemId ItemSel = m_Tree->GetSelection();
270

271
    m_Parent->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
272 273
    m_Parent->DisplayCurrentSheet();
    Close( true );
274 275
}

276

277
void SCH_EDIT_FRAME::DisplayCurrentSheet()
278
{
279
    SetRepeatItem( NULL );
280
    ClearMsgPanel();
281

282
    SCH_SCREEN* screen = m_CurrentSheet->LastScreen();
283

284 285
    // Switch to current sheet,
    // and update the grid size, because it can be modified in latest screen
286
    SetScreen( screen );
287
    GetScreen()->SetGrid( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 );
288

289
    // update the References
290 291
    m_CurrentSheet->UpdateAllScreenReferences();
    SetSheetNumberAndCount();
292
    m_canvas->SetCanStartBlock( -1 );
293

294
    if( screen->m_FirstRedraw )
295
    {
296
        Zoom_Automatique( false );
297
        screen->m_FirstRedraw = false;
298
        SetCrossHairPosition( GetScrollCenterPosition() );
299
        m_canvas->MoveCursorToCrossHair();
300 301 302
    }
    else
    {
303
        RedrawScreen( GetScrollCenterPosition(), true );
304
    }
305

306
    // Now refresh m_canvas. Should be not necessary, but because screen has changed
307 308 309 310 311 312
    // the previous refresh has set all new draw parameters (scroll position ..)
    // but most of time there were some inconsitencies about cursor parameters
    // ( previous position of cursor ...) and artefacts can happen
    // mainly when sheet size has changed
    // This second refresh clears artefacts because at this point,
    // all parameters are now updated
313
    m_canvas->Refresh();
314
}