OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [configtool/] [standalone/] [wxwin/] [configitem.cpp] - Blame information for rev 790

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
2
// -------------------------------------------                              
3
// This file is part of the eCos host tools.                                
4
// Copyright (C) 1998, 1999, 2000, 2006, 2008, 2009, 2011 Free Software Foundation, Inc.
5
//
6
// This program is free software; you can redistribute it and/or modify     
7
// it under the terms of the GNU General Public License as published by     
8
// the Free Software Foundation; either version 2 or (at your option) any   
9
// later version.                                                           
10
//
11
// This program is distributed in the hope that it will be useful, but      
12
// WITHOUT ANY WARRANTY; without even the implied warranty of               
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
14
// General Public License for more details.                                 
15
//
16
// You should have received a copy of the GNU General Public License        
17
// along with this program; if not, write to the                            
18
// Free Software Foundation, Inc., 51 Franklin Street,                      
19
// Fifth Floor, Boston, MA  02110-1301, USA.                                
20
// -------------------------------------------                              
21
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
22
// configitem.cpp :
23
//
24
//===========================================================================
25
//#####DESCRIPTIONBEGIN####
26
//
27
// Author(s):   julians, jld
28
// Contact(s):  julians, jld
29
// Date:        2000/09/01
30
// Version:     $Id: configitem.cpp,v 1.10 2001/04/30 17:12:32 julians Exp $
31
// Purpose:
32
// Description: Implementation file for the ConfigTool application class
33
// Requires:
34
// Provides:
35
// See also:
36
// Known bugs:
37
// Usage:
38
//
39
//####DESCRIPTIONEND####
40
//
41
//===========================================================================
42
 
43
// ============================================================================
44
// declarations
45
// ============================================================================
46
 
47
// ----------------------------------------------------------------------------
48
// headers
49
// ----------------------------------------------------------------------------
50
#ifdef __GNUG__
51
    #pragma implementation "configitem.h"
52
#endif
53
 
54
// Includes other headers for precompiled compilation
55
#include "ecpch.h"
56
 
57
#ifdef __BORLANDC__
58
    #pragma hdrstop
59
#endif
60
 
61
#include "wx/settings.h"
62
#include "wx/valgen.h"
63
 
64
#include "configitem.h"
65
#include "configtree.h"
66
#include "configtooldoc.h"
67
#include "configtoolview.h"
68
#include "ecutils.h"
69
 
70
IMPLEMENT_CLASS(ecConfigItem, wxObject)
71
 
72
/*
73
 * ecConfigItem
74
 * Represents a node in the configuration hierarchy.
75
 * For every ecConfigItem, there is also an ecTreeItemData
76
 * that points to it.
77
 */
78
 
79
ecConfigItem::ecConfigItem(ecConfigItem* parent, const wxString& name, ecConfigType ctype,
80
                           ecOptionFlavor flavor, ecOptionType otype,
81
                           bool active, bool enabled, bool modifiable, ecUIHint hint)
82
{
83
    m_CdlItem = NULL;
84
    m_name = name;
85
    m_configType = ctype;
86
    m_optionType = otype;
87
    m_optionFlavor = flavor;
88
    m_enabled = enabled;
89
    m_active = active;
90
    m_modifiable = modifiable;
91
    m_parent = parent;
92
    m_hint = hint;
93
    m_treeItem = wxTreeItemId();
94
 
95
    switch (otype)
96
    {
97
    case ecDouble:
98
        {
99
            m_value = 0.0;
100
            break;
101
        }
102
    case ecString:
103
    case ecEnumerated:
104
        {
105
            m_value = wxT("");
106
            break;
107
        }
108
    case ecLong:
109
        {
110
            m_value = (long) 0;
111
            break;
112
        }
113
    case ecBool:
114
        {
115
            m_value = (bool) FALSE;
116
            break;
117
        }
118
    default:
119
        {
120
            break;
121
        }
122
    }
123
}
124
 
125
ecConfigItem::ecConfigItem(ecConfigItem* parent, CdlUserVisible vitem)
126
{
127
    m_name = wxT("UNNAMED");
128
    m_configType = ecConfigTypeNone;
129
    m_optionType = ecOptionTypeNone;
130
    m_optionFlavor = ecFlavorNone;
131
    m_enabled = FALSE;
132
    m_active = FALSE;
133
    m_modifiable = FALSE;
134
    m_parent = parent;
135
    m_CdlItem = vitem;
136
    m_hint = ecHintNone;
137
    m_treeItem = wxTreeItemId();
138
 
139
    ecConfigTreeCtrl* treeCtrl = wxGetApp().GetTreeCtrl();
140
    m_treeItem = treeCtrl->AppendItem(parent->GetTreeItem(), m_name, -1, -1, new ecTreeItemData(this));
141
 
142
    ConvertFromCdl();
143
    UpdateTreeItem(* treeCtrl);
144
}
145
 
146
ecConfigItem::~ecConfigItem()
147
{
148
    // Make sure that the tree item no longer references this object
149
    ecConfigTreeCtrl* treeCtrl = wxGetApp().GetTreeCtrl();
150
    if (m_treeItem && treeCtrl)
151
    {
152
        ecTreeItemData* data = (ecTreeItemData*) treeCtrl->GetItemData(m_treeItem);
153
        data->SetConfigItem(NULL);
154
    }
155
 
156
    ecConfigToolDoc* doc = wxGetApp().GetConfigToolDoc();
157
    if (doc)
158
    {
159
        doc->GetItems().DeleteObject(this);
160
    }
161
}
162
 
163
// Convert from Cdl to internal representation
164
bool ecConfigItem::ConvertFromCdl()
165
{
166
    if (!GetCdlItem())
167
        return FALSE;
168
 
169
    m_name = GetCdlItem()->get_display ().c_str ();
170
    m_macro = GetCdlItem()->get_name().c_str();
171
    m_strDescr = ecUtils::StripExtraWhitespace (wxString (GetCdlItem()->get_description ().c_str ()));
172
 
173
 
174
    // FIXME: re-implement using CdlValuableBody::get_widget_hint()
175
    // (comment from original MFC configtool)
176
 
177
    if (IsPackage())
178
    {
179
        // If a package item, display the package version string
180
        m_optionType = ecString;
181
        m_configType = ecPackage;
182
        m_optionFlavor = ecFlavorNone;
183
    }
184
    else
185
    {
186
        const CdlValuable valuable = dynamic_cast<CdlValuable> (GetCdlItem());
187
        switch (valuable->get_flavor ()){
188
        case CdlValueFlavor_None:
189
            m_optionFlavor = ecFlavorNone;
190
            m_optionType=ecOptionTypeNone; //??? Shouldn't it be ecBool for CdlValueFlavor_Bool?
191
            m_configType = ecContainer;
192
            break;
193
        case CdlValueFlavor_Bool:
194
            m_optionFlavor = ecFlavorBool;
195
            m_optionType=ecOptionTypeNone; //??? Shouldn't it be ecBool for CdlValueFlavor_Bool?
196
            m_configType = ecOption;
197
            m_hint = (HasRadio() ? ecHintRadio : ecHintCheck);
198
            break;
199
        case CdlValueFlavor_Data:
200
        case CdlValueFlavor_BoolData:
201
 
202
            m_optionFlavor = (valuable->get_flavor() == CdlValueFlavor_Data ? ecFlavorData : ecFlavorBoolData);
203
            m_configType = ecOption;
204
            m_hint = (HasRadio() ? ecHintRadio : ecHintCheck);
205
 
206
            if (! valuable->has_legal_values ()) {
207
                m_optionType=ecString;
208
            } else if (0 == valuable->get_legal_values ()->ranges.size ()) {
209
                m_optionType=ecEnumerated;
210
            } else {
211
                CdlListValue list_value;
212
                CdlEvalContext context (NULL, valuable, valuable->get_property (CdlPropertyId_LegalValues));
213
                valuable->get_legal_values ()->eval (context, list_value);
214
                m_optionType=list_value.get_double_ranges ().size () ? ecDouble : ecLong;
215
            }
216
            break;
217
        default:
218
            wxASSERT (0); // specified flavor not supported
219
            break;
220
        }
221
    }
222
 
223
    m_active = IsActive();
224
    m_enabled = IsEnabled();
225
    m_modifiable = IsModifiable();
226
 
227
    return TRUE;
228
}
229
 
230
wxString ecConfigItem::GetItemNameOrMacro() const
231
{
232
    return (wxGetApp().GetSettings().m_showMacroNames && !GetMacro().IsEmpty() ? GetMacro() : GetName());
233
}
234
 
235
// Sets the text and icon for this item
236
bool ecConfigItem::UpdateTreeItem(ecConfigTreeCtrl& treeCtrl)
237
{
238
    treeCtrl.SetItemText(m_treeItem, GetItemNameOrMacro());
239
 
240
#if wxCHECK_VERSION(2, 6, 0)
241
    static wxColour normalColour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
242
    static wxColour disabledColour = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
243
#else
244
    static wxColour normalColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT);
245
    static wxColour disabledColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT);
246
#endif
247
 
248
    treeCtrl.SetItemTextColour(m_treeItem, GetActive() ? normalColour : disabledColour);
249
 
250
 
251
    // Find which icon state we're in so we can get the appropriate icon id
252
    int iconState = 0;
253
    wxString iconName;
254
 
255
    switch (GetConfigType())
256
    {
257
        case ecContainer:
258
            {
259
                iconName = _("Container");
260
                iconState = 0;
261
                break;
262
            }
263
        case ecPackage:
264
            {
265
                iconName = _("Package");
266
                iconState = 0;
267
                break;
268
            }
269
        case ecComponent:
270
            {
271
                iconName = _("??");
272
                iconState = 0;
273
                break;
274
            }
275
        case ecOption:
276
            {
277
                if (GetOptionFlavor() == ecFlavorData)
278
                {
279
                    switch (GetOptionType())
280
                    {
281
                    case ecDouble:
282
                    case ecLong:
283
                        {
284
                            iconName = _("Integer");
285
                            iconState = 0;
286
                            break;
287
                        }
288
                    case ecEnumerated:
289
                        {
290
                            iconName = _("Enumerated");
291
                            iconState = 0;
292
                            break;
293
                        }
294
                    case ecString:
295
                        {
296
                            iconName = _("Text");
297
                            iconState = 0;
298
                            break;
299
                        }
300
                    // ??? Actually I don't think there's such a think as ecBool type, only enabled/disabled
301
                    case ecBool:
302
                        {
303
                            if (GetUIHint() == ecHintCheck)
304
                                iconName = _("Checkbox");
305
                            else
306
                                iconName = _("Radiobox");
307
                            iconState = (m_value.GetBool() ? 0 : 1);
308
                            break;
309
                        }
310
                    default:
311
                        {
312
                            break;
313
                        }
314
                    }
315
                }
316
                if (GetOptionFlavor() == ecFlavorBoolData || GetOptionFlavor() == ecFlavorBool)
317
                {
318
                    if (GetUIHint() == ecHintCheck)
319
                        iconName = _("Checkbox");
320
                    else
321
                        iconName = _("Radiobox");
322
                    iconState = (m_enabled ? 0 : 1);
323
                }
324
                break;
325
            }
326
        default:
327
            {
328
                break;
329
            }
330
    }
331
 
332
    if (!iconName.IsEmpty())
333
    {
334
        int iconId = treeCtrl.GetIconDB().GetIconId(iconName, iconState, GetModifiable());
335
        treeCtrl.SetItemImage(m_treeItem, iconId, wxTreeItemIcon_Normal);
336
        treeCtrl.SetItemImage(m_treeItem, iconId, wxTreeItemIcon_Selected);
337
    }
338
 
339
    return TRUE;
340
}
341
 
342
// Handle a left click on the icon: e.g. (un)check the option
343
// In the old MFC tool, this was handled by CControlView::Bump
344
void ecConfigItem::OnIconLeftDown(ecConfigTreeCtrl& treeCtrl)
345
{
346
    if (GetConfigType() != ecOption)
347
        return;
348
 
349
    switch (GetOptionFlavor())
350
    {
351
    case ecFlavorBool:
352
    case ecFlavorBoolData:
353
        {
354
            if (GetModifiable())
355
            {
356
                wxGetApp().GetConfigToolDoc()->SetEnabled(*this, !m_enabled);
357
            }
358
            break;
359
        }
360
    case ecFlavorData:
361
        {
362
            if (GetModifiable())
363
            {
364
                switch (GetOptionType())
365
                {
366
                case ecLong:
367
                    {
368
                        int nInc = 1;
369
 
370
                        long nOldValue = Value();
371
                        if(nInc==1 && nOldValue == long(-1))
372
                        {
373
                            nOldValue=0;
374
                        } else if(nInc==-1 && nOldValue==0){
375
                            nOldValue = long(-1);
376
                        } else {
377
                            nOldValue+=nInc;
378
                        }
379
                        wxGetApp().GetConfigToolDoc()->SetValue(*this, nOldValue);
380
                        break;
381
                    }
382
                case ecEnumerated:
383
                    {
384
                        int nInc = 1;
385
 
386
                        wxArrayString arEnum;
387
                        EvalEnumStrings (arEnum); // calculate legal values just in time
388
                        if (0 == arEnum.GetCount()) // if no legal values...
389
                            break;           // ...do nothing
390
                        int nIndex = -1;
391
                        const wxString strCurrent = StringValue ();
392
                        int nEnum;
393
                        for (nEnum = 0; (nEnum < arEnum.GetCount()) && (nIndex == -1); nEnum++)
394
                            if (0 == arEnum[nEnum].CompareTo (strCurrent))
395
                                nIndex = nEnum; // the index of the current value
396
 
397
                            if (nIndex != -1) // if the current value is still legal
398
                                nIndex += (nInc < 0 ? -1 : 1); // increment/decrement the index
399
                            else
400
                                nIndex = 0; // otherwise select the first enum
401
 
402
                            if (nIndex < 0) // if the new index is negative
403
                                nIndex = arEnum.GetCount()-1; // make it positive
404
 
405
                            wxGetApp().GetConfigToolDoc()->SetValue(*this, arEnum[nIndex % arEnum.GetCount()]);
406
                            break;
407
                    }
408
                default:
409
                    {
410
                        break;
411
                    }
412
                }
413
            }
414
            break;
415
        }
416
    default:
417
        {
418
            break;
419
        }
420
    }
421
 
422
}
423
 
424
// Gets the value to display (often an empty string)
425
wxString ecConfigItem::GetDisplayValue() const
426
{
427
    wxString str;
428
    switch(GetOptionType())
429
    {
430
    case ecEnumerated:
431
    case ecLong:
432
    case ecDouble:
433
    case ecString:
434
        {
435
            if (GetCdlValuable())
436
                str = StringValue();
437
                str.Replace("\n", "\\n"); // escape any newline chars (eg CYGDAT_UITRON_TASK_INITIALIZERS)
438
        }
439
        break;
440
    default:
441
        break;
442
    }
443
    return str;
444
#if 0
445
    switch (GetConfigType())
446
    {
447
        case ecComponent:
448
        case ecContainer:
449
            {
450
                return wxEmptyString;
451
                break;
452
            }
453
        case ecPackage:
454
            {
455
                return m_value.GetString();
456
                break;
457
            }
458
        case ecOption:
459
            {
460
                switch (GetOptionType())
461
                {
462
                    case ecDouble:
463
                        {
464
                            wxString val;
465
                            val.Printf("%.4lf", (double) m_value.GetDouble());
466
                            return val;
467
                        }
468
                    case ecLong:
469
                        {
470
                            wxString val;
471
                            val.Printf("%.ld", (long) m_value.GetLong());
472
                            return val;
473
                            break;
474
                        }
475
                    case ecEnumerated:
476
                    case ecString:
477
                        {
478
                            return m_value.GetString();
479
                            break;
480
                        }
481
                    case ecBool:
482
                        {
483
                            return wxEmptyString;
484
                            break;
485
                        }
486
                    default:
487
                        {
488
                            break;
489
                        }
490
                }
491
                break;
492
            }
493
        default:
494
            {
495
                break;
496
            }
497
    }
498
 
499
    return wxEmptyString;
500
#endif
501
}
502
 
503
// Can we start editing this item?
504
bool ecConfigItem::CanEdit() const
505
{
506
    if (!GetModifiable())
507
        return FALSE;
508
 
509
    if (GetConfigType() != ecOption)
510
        return FALSE;
511
 
512
    if (GetOptionFlavor() != ecFlavorData && GetOptionFlavor() != ecFlavorBoolData)
513
        return FALSE;
514
 
515
    // TODO: other criteria for editability
516
    return TRUE;
517
}
518
 
519
 
520
// Creates an edit window. It will be positioned by the caller.
521
wxWindow* ecConfigItem::CreateEditWindow(wxWindow* parent)
522
{
523
    wxWindow* window = NULL;
524
 
525
    switch(GetOptionType())
526
    {
527
    case ecEnumerated:
528
        {
529
            window = new ecEnumEditorCtrl(parent, ecID_ITEM_EDIT_WINDOW, wxDefaultPosition, wxDefaultSize,
530
                /* wxNO_BORDER */ 0);
531
            wxArrayString arEnumStrings;
532
            EvalEnumStrings(arEnumStrings);
533
            int i;
534
            for (i = 0; i < arEnumStrings.GetCount(); i++)
535
            {
536
                ((ecEnumEditorCtrl*) window)->Append(arEnumStrings[i]);
537
            }
538
            break;
539
        }
540
    case ecLong:
541
        {
542
            window = new ecIntegerEditorCtrl(parent, ecID_ITEM_EDIT_WINDOW, wxDefaultPosition, wxDefaultSize,
543
                /* wxNO_BORDER | */ wxSP_ARROW_KEYS);
544
            break;
545
        }
546
    case ecDouble:
547
        {
548
            window = new ecDoubleEditorCtrl(parent, ecID_ITEM_EDIT_WINDOW, wxDefaultPosition, wxDefaultSize,
549
                /* wxNO_BORDER|*/ wxTE_PROCESS_ENTER);
550
            break;
551
        }
552
    case ecString:
553
        {
554
            window = new ecTextEditorCtrl(parent, ecID_ITEM_EDIT_WINDOW, wxDefaultPosition, wxDefaultSize,
555
                /* wxNO_BORDER|*/ wxTE_PROCESS_ENTER);
556
            break;
557
        }
558
    default:
559
        break;
560
    }
561
 
562
    wxASSERT (window != NULL) ;
563
 
564
    return window;
565
}
566
 
567
// Transfers data between item and window
568
bool ecConfigItem::TransferDataToWindow(wxWindow* window)
569
{
570
    if (window->IsKindOf(CLASSINFO(ecTextEditorCtrl)))
571
    {
572
        ecTextEditorCtrl* win = (ecTextEditorCtrl*) window;
573
        win->SetValue(GetDisplayValue());
574
    }
575
    else if (window->IsKindOf(CLASSINFO(ecDoubleEditorCtrl)))
576
    {
577
        ecDoubleEditorCtrl* win = (ecDoubleEditorCtrl*) window;
578
        win->SetValue(GetDisplayValue());
579
    }
580
    else if (window->IsKindOf(CLASSINFO(ecEnumEditorCtrl)))
581
    {
582
        ecEnumEditorCtrl* win = (ecEnumEditorCtrl*) window;
583
        win->SetStringSelection(GetDisplayValue());
584
    }
585
    else if (window->IsKindOf(CLASSINFO(ecIntegerEditorCtrl)))
586
    {
587
        ecIntegerEditorCtrl* win = (ecIntegerEditorCtrl*) window;
588
        long i;
589
        ecUtils::StrToItemIntegerType(StringValue(), i);
590
 
591
        wxString val;
592
        val.Printf(wxT("%ld"), i);
593
 
594
        win->SetValue(val);
595
    }
596
    return TRUE;
597
}
598
 
599
bool ecConfigItem::TransferDataFromWindow(wxWindow* window)
600
{
601
    ecConfigToolDoc* doc = wxGetApp().GetConfigToolDoc();
602
    wxASSERT (doc != NULL);
603
 
604
    if (!doc)
605
        return FALSE;
606
 
607
    if (window->IsKindOf(CLASSINFO(ecTextEditorCtrl)))
608
    {
609
        ecTextEditorCtrl* win = (ecTextEditorCtrl*) window;
610
 
611
        wxASSERT ( GetOptionType() == ecString );
612
 
613
        // TODO: do checking
614
        wxString str(win->GetValue());
615
        str.Replace("\\n", "\n"); // remove escaping of newline chars
616
        doc->SetValue(*this, str);
617
    }
618
    else if (window->IsKindOf(CLASSINFO(ecDoubleEditorCtrl)))
619
    {
620
        ecDoubleEditorCtrl* win = (ecDoubleEditorCtrl*) window;
621
 
622
        wxASSERT ( GetOptionType() == ecDouble );
623
 
624
        // TODO: do checking
625
        doc->SetValue(*this, atof(win->GetValue()));
626
    }
627
    else if (window->IsKindOf(CLASSINFO(ecEnumEditorCtrl)))
628
    {
629
        ecEnumEditorCtrl* win = (ecEnumEditorCtrl*) window;
630
 
631
        wxASSERT ( GetOptionType() == ecEnumerated );
632
 
633
        // TODO: do checking
634
        doc->SetValue(*this, win->GetStringSelection());
635
    }
636
    else if (window->IsKindOf(CLASSINFO(ecIntegerEditorCtrl)))
637
    {
638
        ecIntegerEditorCtrl* win = (ecIntegerEditorCtrl*) window;
639
 
640
        wxASSERT ( GetOptionType() == ecLong );
641
 
642
        // TODO: do checking
643
        doc->SetValue(*this, (long) win->GetValue());
644
    }
645
 
646
    return TRUE;
647
}
648
 
649
//// Taken from MFC version
650
 
651
const ecFileName ecConfigItem::GetFilename() const
652
{
653
    wxString sep(wxFILE_SEP_PATH);
654
 
655
    ecFileName strFile;
656
    const CdlNode node = dynamic_cast<CdlNode> (m_CdlItem);
657
    if (node){
658
        // get the package which owns the configuration item
659
        const CdlPackage package = GetOwnerPackage();
660
        if (package){
661
 
662
            // return the filename of the config header
663
            wxString pkg(wxT("include"));
664
            pkg += sep;
665
            pkg += wxT("pkgconf");
666
            strFile=ecFileName(wxGetApp().GetConfigToolDoc()->GetInstallTree()+sep+pkg) + package->get_config_header ().c_str ();
667
        }
668
    }
669
    return strFile;
670
}
671
 
672
// Change version (of a package)
673
bool ecConfigItem::ChangeVersion(const wxString &strVersion)
674
{
675
    bool rc=FALSE;
676
    CdlPackage package=dynamic_cast<CdlPackage>(GetCdlItem());
677
    wxASSERT(package != 0);
678
    const CdlValuable valuable = GetCdlValuable();
679
    wxASSERT (valuable != 0);
680
    const wxString strMacroName(GetMacro());
681
    if (strVersion != valuable->get_value ().c_str ()) { // if the wrong version is loaded
682
        // TRACE (wxT("Changing package %s to version '%s'\n"), strMacroName, strVersion);
683
        try {
684
            wxGetApp().GetConfigToolDoc()->GetCdlConfig()->change_package_version (package, ecUtils::UnicodeToStdStr (strVersion), ecConfigToolDoc::CdlParseErrorHandler, ecConfigToolDoc::CdlParseWarningHandler);
685
            rc=TRUE;
686
        }
687
        catch (CdlStringException exception) {
688
            wxString msg;
689
            msg.Printf(wxT("Error changing package %s to version '%s':\n\n%s"), (const wxChar*) strMacroName, (const wxChar*) strVersion, (const wxChar*) wxString (exception.get_message ().c_str ())) ;
690
            wxMessageBox(msg);
691
        }
692
        catch (...) {
693
            wxString msg;
694
            msg.Printf(wxT("Error changing package %s to version '%s'"), (const wxChar*) strMacroName, (const wxChar*) strVersion) ;
695
            wxMessageBox(msg);
696
        }
697
    }
698
    return rc;
699
}
700
 
701
// Unload (a package)
702
bool ecConfigItem::Unload()
703
{
704
    bool rc=FALSE;
705
    CdlPackage package=dynamic_cast<CdlPackage>(GetCdlItem());
706
    wxASSERT(package);
707
    ecConfigToolDoc* pDoc=wxGetApp().GetConfigToolDoc();
708
 
709
    // Remove its objects from the view to prevent any painting problems
710
    ecTreeItemData* data = (ecTreeItemData*) wxGetApp().GetTreeCtrl()->GetItemData(GetTreeItem());
711
    wxASSERT(data);
712
 
713
    // I _think_ we should do this to stop 'this' from being deleted when we delete the item.
714
    // But, in that case, where do we delete this item?
715
    // Perhaps should store them in an array in the document, as per the MFC tool.
716
    data->SetConfigItem(NULL);
717
 
718
    wxGetApp().GetTreeCtrl()->Delete(GetTreeItem());
719
 
720
#if wxCHECK_VERSION(2, 6, 0)
721
    wxNode* node = pDoc->GetItems().GetFirst();
722
    while (node)
723
    {
724
        ecConfigItem* item = wxDynamicCast(node->GetData(), ecConfigItem);
725
        if (package == item->GetOwnerPackage() && item != this)
726
        {
727
            item->SetTreeItem(wxTreeItemId()); // Make sure we can't attempt to paint it
728
            item->SetCdlItem(NULL); // Make sure we can't access stale data
729
            delete item; // Delete the item itself
730
        }
731
        node = node->GetNext();
732
    }
733
#else
734
    wxNode* node = pDoc->GetItems().First();
735
    while (node)
736
    {
737
        ecConfigItem* item = wxDynamicCast(node->Data(), ecConfigItem);
738
        if (package == item->GetOwnerPackage() && item != this)
739
        {
740
            item->SetTreeItem(wxTreeItemId()); // Make sure we can't attempt to paint it
741
            item->SetCdlItem(NULL); // Make sure we can't access stale data
742
            delete item; // Delete the item itself
743
        }
744
        node = node->Next();
745
    }
746
#endif
747
 
748
    const wxString strMacroName(GetMacro());
749
    //TRACE (wxT("Unloading package %s\n"), strMacroName);
750
    try {
751
        pDoc->GetCdlConfig()->unload_package (package);
752
        rc=TRUE;
753
    }
754
    catch (CdlStringException exception) {
755
        wxString msg;
756
        wxString exceptionMsg(exception.get_message ().c_str ());
757
        msg.Printf(wxT("Error unloading package %s:\n\n%s"), (const wxChar*) strMacroName, (const wxChar*) exceptionMsg );
758
        wxMessageBox(msg);
759
    }
760
    catch (...) {
761
        wxString msg;
762
        msg.Printf(wxT("Error unloading package %s"), (const wxChar*) strMacroName);
763
        wxMessageBox(msg);
764
    }
765
    m_treeItem=wxTreeItemId();   // Make sure we can't attempt to paint it
766
    m_CdlItem=NULL; // Make sure we can't access stale data
767
    return rc;
768
}
769
 
770
wxString ecConfigItem::GetURL() const
771
{
772
    for(const ecConfigItem *pItem=this;pItem;pItem=pItem->GetParent()){
773
        if(pItem->GetCdlItem()){
774
            wxString strURL;
775
            strURL=pItem->GetCdlItem()->get_doc_url().c_str();
776
            if(strURL.Len()){
777
                return strURL;
778
            }
779
            strURL=pItem->GetCdlItem()->get_doc().c_str();
780
            if(strURL.Len()){
781
                return strURL;
782
            }
783
        }
784
    }
785
    return wxT("ref/ecos-ref.html"); // the default URL
786
}
787
 
788
bool ecConfigItem::SetValue(const wxString& value, CdlTransaction transaction/*=NULL*/)
789
{
790
    wxASSERT ((m_optionType == ecString) || (m_optionType == ecEnumerated));
791
    const CdlValuable valuable = GetCdlValuable();
792
    wxASSERT (valuable);
793
    const std::string str = value.c_str();
794
    if(transaction){
795
        if (CdlValueFlavor_BoolData == valuable->get_flavor ()){
796
            // set the user bool to the current bool when changing a booldata
797
            // value to avoid a possible change in the current bool
798
            valuable->set_enabled_and_value (transaction, valuable->is_enabled (), str, CdlValueSource_User);
799
        } else {// CdlValueFlavor_Data
800
            valuable->set_value (transaction, str, CdlValueSource_User);
801
        }
802
    } else {
803
        if (CdlValueFlavor_BoolData == valuable->get_flavor ()){
804
            // set the user bool to the current bool when changing a booldata
805
            // value to avoid a possible change in the current bool
806
            valuable->set_enabled_and_value (valuable->is_enabled (), str, CdlValueSource_User);
807
        } else {// CdlValueFlavor_Data
808
            valuable->set_value (str, CdlValueSource_User);
809
        }
810
    }
811
 
812
    // TODO: eliminate m_value, since the value is always taken from the Cdl object.
813
    m_value = value;
814
 
815
    return TRUE;
816
}
817
 
818
bool ecConfigItem::SetValue (double dValue, CdlTransaction transaction/*=NULL*/)
819
{
820
    wxASSERT (m_optionType == ecDouble);
821
 
822
    const CdlValuable valuable = GetCdlValuable();
823
    wxASSERT (valuable);
824
 
825
    if(transaction) {
826
        if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
827
            // set the user bool to the current bool when changing a booldata
828
            // value to avoid a possible change in the current bool
829
            valuable->set_enabled_and_value (transaction, valuable->is_enabled (), dValue, CdlValueSource_User);
830
        } else {// CdlValueFlavor_Data
831
            valuable->set_double_value (transaction, dValue, CdlValueSource_User);
832
        }
833
    } else {
834
        if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
835
            // set the user bool to the current bool when changing a booldata
836
            // value to avoid a possible change in the current bool
837
            valuable->set_enabled_and_value (valuable->is_enabled (), dValue, CdlValueSource_User);
838
        } else {// CdlValueFlavor_Data
839
            valuable->set_double_value (dValue, CdlValueSource_User);
840
        }
841
    }
842
 
843
    // TODO: BoolData?
844
    m_value = dValue;
845
 
846
    return TRUE;
847
}
848
 
849
bool ecConfigItem::SetValue (long nValue, CdlTransaction transaction/*=NULL*/)
850
{
851
    wxASSERT (m_optionType == ecLong);
852
    const CdlValuable valuable = GetCdlValuable();
853
    wxASSERT (valuable);
854
 
855
    if(transaction) {
856
        if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
857
            // set the user bool to the current bool when changing a booldata
858
            // value to avoid a possible change in the current bool
859
            valuable->set_enabled_and_value (transaction, valuable->is_enabled (), (cdl_int) nValue, CdlValueSource_User);
860
        } else { // CdlValueFlavor_Data
861
            valuable->set_integer_value (transaction, nValue, CdlValueSource_User);
862
        }
863
    } else {
864
        if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
865
            // set the user bool to the current bool when changing a booldata
866
            // value to avoid a possible change in the current bool
867
            valuable->set_enabled_and_value (valuable->is_enabled (), (cdl_int) nValue, CdlValueSource_User);
868
        } else { // CdlValueFlavor_Data
869
            valuable->set_integer_value (nValue, CdlValueSource_User);
870
        }
871
    }
872
 
873
    // TODO: BoolData?
874
    m_value = nValue;
875
 
876
    return TRUE;
877
}
878
 
879
bool ecConfigItem::HasRadio() const
880
{
881
    const CdlValuable valuable = GetCdlValuable();
882
    if (! valuable)
883
        return FALSE;
884
 
885
    CdlWidgetHint hint;
886
    valuable->get_widget_hint (hint);
887
    return (CdlBoolWidget_Radio == hint.bool_widget);
888
}
889
 
890
ecConfigItem *ecConfigItem::FirstRadio() const
891
{
892
    wxASSERT(HasRadio ());
893
 
894
    for(ecConfigItem *h=GetParent()->FirstChild();h;h=h->NextSibling()){
895
        if(h->HasRadio ()){
896
            return h;
897
        }
898
    }
899
    // No radio buttons found
900
    wxASSERT(FALSE);
901
    return FALSE;
902
}
903
 
904
ecConfigItem *ecConfigItem::FirstChild() const
905
{
906
    ecConfigTreeCtrl* treeCtrl = wxGetApp().GetTreeCtrl();
907
 
908
#if wxCHECK_VERSION(2, 6, 0)
909
                wxTreeItemIdValue cookie;
910
#else
911
        long cookie;
912
#endif
913
    wxTreeItemId hChild=treeCtrl->GetFirstChild(GetTreeItem(), cookie);
914
    if (hChild)
915
    {
916
        ecTreeItemData* data = (ecTreeItemData*) wxGetApp().GetTreeCtrl()->GetItemData(hChild);
917
        wxASSERT(data);
918
 
919
        return data->GetConfigItem();
920
    }
921
    else
922
        return NULL;
923
}
924
 
925
ecConfigItem *ecConfigItem::NextSibling() const
926
{
927
    ecConfigTreeCtrl* treeCtrl = wxGetApp().GetTreeCtrl();
928
 
929
    wxTreeItemId hChild=treeCtrl->GetNextSibling(GetTreeItem());
930
    if (hChild)
931
    {
932
        ecTreeItemData* data = (ecTreeItemData*) wxGetApp().GetTreeCtrl()->GetItemData(hChild);
933
        wxASSERT(data);
934
 
935
        return data->GetConfigItem();
936
    }
937
    else
938
        return NULL;
939
}
940
 
941
bool ecConfigItem::IsEnabled() const
942
{
943
    const CdlValuable valuable = GetCdlValuable();
944
    return NULL==valuable ||valuable->is_enabled();
945
}
946
 
947
bool ecConfigItem::IsModifiable() const
948
{
949
    const CdlValuable valuable = GetCdlValuable();
950
    if (valuable && ((GetOptionType() != ecOptionTypeNone) || HasBool()))
951
    {
952
        return (valuable->is_modifiable () && valuable->is_active ());
953
    }
954
    else
955
        return GetCdlItem()->is_active();
956
}
957
 
958
bool ecConfigItem::IsActive() const
959
{
960
//    return GetCdlItem()->is_active();
961
    const CdlValuable valuable = GetCdlValuable();
962
    if (valuable && ((GetOptionType() != ecOptionTypeNone) || HasBool()))
963
    {
964
        return (valuable->is_active ());
965
    }
966
    else
967
        return GetCdlItem()->is_active();
968
}
969
 
970
bool ecConfigItem::HasModifiedChildren() const
971
{
972
    for(ecConfigItem *pItem=FirstChild();pItem;pItem=pItem->NextSibling()){
973
        if(pItem->Modified()||pItem->HasModifiedChildren()){
974
            return TRUE;
975
        }
976
    }
977
    return FALSE;
978
}
979
 
980
bool ecConfigItem::Modified () const
981
{
982
    const CdlValuable valuable = GetCdlValuable();
983
    return
984
        valuable        // accommodate the root config item which has no CDL item
985
        && !IsPackage() // packages are never modified
986
        && valuable->get_source () != CdlValueSource_Default;
987
}
988
 
989
void ecConfigItem::DumpItem()
990
{
991
    //TRACE(wxT("Item %08x\n\tDisplay Name='%s'\n\tMacro Name='%s'\n\tType=%s"), this,  Name(),           Macro(),    TreeItemTypeImage[m_Type]);
992
    //TRACE(wxT("\n\tValue=%s\n\tURL=%s\n\tParent=%08x"),StringValue(), GetURL(), Parent());
993
 
994
    //TRACE(wxT("\n"));
995
}
996
 
997
ecConfigItem * ecConfigItem::NextRadio() const
998
{
999
    wxASSERT(this->HasRadio ());
1000
    for(ecConfigItem *pItem=NextSibling();pItem;pItem=pItem->NextSibling()){
1001
        if(pItem->HasRadio()){
1002
            return pItem;
1003
        }
1004
    }
1005
    return NULL;
1006
}
1007
 
1008
bool ecConfigItem::IsDescendantOf(ecConfigItem * pAncestor)
1009
{
1010
    for(ecConfigItem *pItem=GetParent();pItem;pItem=pItem->GetParent()){
1011
        if(pItem==pAncestor){
1012
            return TRUE;
1013
        }
1014
    }
1015
    return FALSE;
1016
}
1017
 
1018
bool ecConfigItem::ViewHeader()
1019
{
1020
    bool rc=FALSE;
1021
    const ecFileName strFile(GetFilename());
1022
    if(!strFile.IsEmpty())
1023
    {
1024
        ecConfigToolDoc *pDoc=wxGetApp().GetConfigToolDoc();
1025
        if(pDoc->GetBuildTree().IsEmpty()){
1026
            wxString msg;
1027
            msg.Printf(wxT("Cannot display header file until configuration is saved"));
1028
            wxMessageBox(msg);
1029
        } else
1030
        {
1031
            rc=wxGetApp().Launch(strFile, wxGetApp().GetSettings().m_strViewer);
1032
        }
1033
    }
1034
    return rc;
1035
}
1036
 
1037
bool ecConfigItem::ViewURL()
1038
{
1039
    return wxGetApp().GetConfigToolDoc()->ShowURL(GetURL());
1040
}
1041
 
1042
bool ecConfigItem::HasBool() const
1043
{
1044
    if (!m_CdlItem) {
1045
        return FALSE;
1046
    } else if (IsPackage()) {
1047
        return FALSE;
1048
    } else {
1049
        const CdlValuable valuable = GetCdlValuable();
1050
        CdlValueFlavor flavor = valuable->get_flavor ();
1051
        return (flavor == CdlValueFlavor_Bool) || (flavor == CdlValueFlavor_BoolData);
1052
    }
1053
}
1054
 
1055
bool ecConfigItem::SetEnabled(bool bEnabled, CdlTransaction current_transaction/*=NULL*/)
1056
{
1057
    const CdlValuable valuable = GetCdlValuable();
1058
    wxASSERT (valuable);
1059
 
1060
    // use a transaction object to ensure that all config items are changed together
1061
    CdlTransaction transaction = current_transaction ? current_transaction : CdlTransactionBody::make (wxGetApp().GetConfigToolDoc ()->GetCdlConfig ());
1062
 
1063
    if (HasRadio () && bEnabled) { // if a new radio button has been selected
1064
        for (ecConfigItem *pItem = FirstRadio(); pItem; pItem = pItem->NextRadio ()) { // for each radio button in the group
1065
            if (pItem != this) { // if not the newly selected radio button
1066
                pItem->SetEnabled (FALSE, transaction); // disable the radio button
1067
            }
1068
        }
1069
    }
1070
 
1071
    if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
1072
        // set the user value to the current data value when enabling/disabling
1073
        // a booldata item to avoid a possible change in the current data value
1074
        CdlSimpleValue simple_value = valuable->get_simple_value ();
1075
        valuable->set_enabled_and_value (transaction, bEnabled, simple_value, CdlValueSource_User);
1076
    } else { // CdlValueFlavor_Bool
1077
        valuable->set_enabled (transaction, bEnabled, CdlValueSource_User);
1078
    }
1079
 
1080
    if (! current_transaction) { // if not a recursive call to disable a radio button
1081
        transaction->body (); // commit the transaction
1082
        delete transaction;
1083
        transaction = NULL;
1084
    }
1085
 
1086
    return TRUE;
1087
}
1088
 
1089
long ecConfigItem::DefaultValue () const
1090
{
1091
    return (long) atoi (StringValue (CdlValueSource_Default)) ;
1092
}
1093
 
1094
long ecConfigItem::Value () const
1095
{
1096
    wxASSERT (!IsPackage()); // not a package item
1097
    const CdlValuable valuable = GetCdlValuable();
1098
    wxASSERT (valuable);
1099
    long nValue (0);
1100
 
1101
    switch (valuable->get_flavor ())
1102
    {
1103
        //      case CdlValueFlavor_Bool:
1104
        //              nValue = valuable->is_enabled (CdlValueSource_Current) ? 1 : 0;
1105
        //              break;
1106
 
1107
    case CdlValueFlavor_BoolData:
1108
    case CdlValueFlavor_Data:
1109
        nValue = (long) valuable->get_integer_value (CdlValueSource_Current);
1110
        break;
1111
 
1112
    default:
1113
        wxASSERT (0); // specified flavor not supported
1114
    }
1115
 
1116
    return nValue;
1117
}
1118
 
1119
const double ecConfigItem::DoubleValue (CdlValueSource source /* = CdlValueSource_Current */ ) const
1120
{
1121
    wxASSERT (!IsPackage()); // not a package item
1122
    const CdlValuable valuable = GetCdlValuable();
1123
    wxASSERT (valuable);
1124
    wxASSERT (valuable->has_double_value (source));
1125
    return valuable->get_double_value (source);
1126
}
1127
 
1128
const wxString ecConfigItem::StringValue (CdlValueSource source /* = CdlValueSource_Current */ ) const
1129
{
1130
    //  wxASSERT (!IsPackage()); // not a package item
1131
    const CdlValuable valuable = GetCdlValuable();
1132
    wxASSERT (valuable);
1133
    wxString strValue (wxT(""));
1134
 
1135
    switch (valuable->get_flavor ())
1136
    {
1137
    case CdlValueFlavor_Data:
1138
    case CdlValueFlavor_BoolData:
1139
    case CdlValueFlavor_None: // a package
1140
        if (m_optionType == ecLong)
1141
            strValue = ecUtils::IntToStr (Value (), wxGetApp().GetSettings().m_bHex);
1142
        else if (m_optionType == ecDouble)
1143
            strValue = ecUtils::DoubleToStr (DoubleValue ());
1144
        else
1145
            strValue = valuable->get_value (source).c_str ();
1146
        break;
1147
 
1148
    default:
1149
        wxASSERT (0); // specified flavor not supported
1150
    }
1151
 
1152
    return strValue;
1153
}
1154
 
1155
const wxString ecConfigItem::StringValue(ecWhereType where) const
1156
{
1157
    wxString str;
1158
    switch(where){
1159
    case ecInName:
1160
        str=GetName();
1161
        break;
1162
    case ecInMacro:
1163
        str=GetMacro();
1164
        break;
1165
    case ecInDesc:
1166
        str=GetDescription();
1167
        break;
1168
    case ecInCurrentValue:
1169
        if (ecOptionTypeNone==GetOptionType())
1170
            str = wxEmptyString;
1171
        else
1172
            str = StringValue(CdlValueSource_Current);
1173
        break;
1174
    case ecInDefaultValue:
1175
        if (ecOptionTypeNone==GetOptionType())
1176
            str = wxEmptyString;
1177
        else
1178
            str = StringValue(CdlValueSource_Default);
1179
        break;
1180
    default:
1181
        wxASSERT(FALSE);
1182
        break;
1183
    }
1184
    return str;
1185
}
1186
 
1187
int ecConfigItem::EvalEnumStrings (wxArrayString &arEnumStrings) const
1188
{
1189
    const CdlValuable valuable = GetCdlValuable();
1190
    wxASSERT (valuable);
1191
    /*
1192
    if (m_Type == Boolean)
1193
    {
1194
    arEnumStrings.SetSize (2);
1195
    arEnumStrings.SetAt (0, wxT("True"));
1196
    arEnumStrings.SetAt (1, wxT("False"));
1197
    }
1198
    else
1199
    */
1200
    {
1201
        wxASSERT (m_optionType == ecEnumerated);
1202
        CdlListValue list_value;
1203
        CdlEvalContext context (NULL, m_CdlItem, m_CdlItem->get_property (CdlPropertyId_LegalValues));
1204
        valuable->get_legal_values ()->eval (context, list_value);
1205
        const std::vector<CdlSimpleValue> & table = list_value.get_table ();
1206
 
1207
        // add legal values to the list
1208
        for (unsigned int nValue = 0; nValue < table.size (); nValue++)
1209
        {
1210
            arEnumStrings.Add (table [nValue].get_value ().c_str ());
1211
        }
1212
    }
1213
    return arEnumStrings.GetCount();
1214
}
1215
 
1216
static const wxChar* gs_whereTypes[] =
1217
{
1218
        _("Macro names"),
1219
        _("Item names"),
1220
        _("Short descriptions"),
1221
        _("Current Values"),
1222
        _("Default Values")
1223
};
1224
 
1225
// Convert a string representation of 'where' (e.g. "Macro names") to
1226
// ecWhereType
1227
ecWhereType ecConfigItem::WhereStringToType(const wxString& whereString)
1228
{
1229
    int sz = 5;
1230
    int i;
1231
    for (i = 0; i < sz; i++)
1232
        if (whereString == gs_whereTypes[i])
1233
            return (ecWhereType) i;
1234
 
1235
    wxASSERT( FALSE );
1236
 
1237
    return (ecWhereType) 0;
1238
}
1239
 
1240
// Convert a type representation of 'where' to a string
1241
wxString ecConfigItem::WhereTypeToString(ecWhereType whereType)
1242
{
1243
    return gs_whereTypes[(size_t) whereType] ;
1244
}
1245
 
1246
// Bump by specified amount, or toggle if a boolean value
1247
bool ecConfigItem::BumpItem(int nInc)
1248
{
1249
    bool rc = FALSE;
1250
 
1251
    // Take an action for clicking on the icon
1252
    ecConfigToolDoc* pDoc = wxGetApp().GetConfigToolDoc();
1253
 
1254
    // do not modify the option value if it is inactive or not modifiable
1255
    const CdlValuable valuable = GetCdlValuable();
1256
    if (!valuable || (valuable->is_modifiable () && valuable->is_active ()))
1257
    {
1258
        if (0 == nInc) // if a toggle request
1259
        {
1260
            if (HasBool () && ! (HasRadio () && IsEnabled ())) { // only enable (not disable) a radio button
1261
                rc = pDoc->SetEnabled (*this, ! this->IsEnabled ()); // toggle enabled/disabled state
1262
            }
1263
        } else if (IsEnabled ()) { // the item is enabled...
1264
            switch(GetOptionType())
1265
            {
1266
            case ecOptionTypeNone:
1267
            case ecString:
1268
            case ecDouble:
1269
                break;
1270
            case ecEnumerated:
1271
                {
1272
                    wxArrayString arEnum;
1273
                    EvalEnumStrings (arEnum); // calculate legal values just in time
1274
                    if (0==arEnum.Count()) // if no legal values...
1275
                        break;           // ...do nothing
1276
                    int nIndex = -1;
1277
                    const wxString strCurrent = StringValue ();
1278
                    int nEnum;
1279
                    for (nEnum = 0; (nEnum < arEnum.Count()) && (nIndex == -1); nEnum++)
1280
                        if (strCurrent == arEnum[nEnum])
1281
                            nIndex = nEnum; // the index of the current value
1282
 
1283
                        if (nIndex != -1) // if the current value is still legal
1284
                            nIndex += (nInc < 0 ? -1 : 1); // increment/decrement the index
1285
                        else
1286
                            nIndex = 0; // otherwise select the first enum
1287
 
1288
                        if (nIndex < 0) // if the new index is negative
1289
                            nIndex = arEnum.Count()-1; // make it positive
1290
 
1291
                        rc=pDoc->SetValue (*this, arEnum[nIndex % arEnum.Count()]);
1292
                }
1293
                break;
1294
            case ecLong:
1295
                {
1296
                    // TODO: if we're editing, we should get the value in the edit item
1297
                    // and not the ecConfigItem.
1298
                    long nOldValue = Value();
1299
                    if(nInc==1 && nOldValue==-1){
1300
                        nOldValue=0;
1301
                    } else if(nInc==-1 && nOldValue==0){
1302
                        nOldValue=-1;
1303
                    } else {
1304
                        nOldValue+=nInc;
1305
                    }
1306
                    rc=pDoc->SetValue(*this, nOldValue);
1307
                    break;
1308
                }
1309
 
1310
                break;
1311
                /*
1312
                case CConfigItem::Boolean:
1313
 
1314
                  {
1315
                  ItemIntegerType nOldValue=Value(h);
1316
                  pDoc->SetValue(ti,nOldValue^1);
1317
                  }
1318
                  break;
1319
                  case CConfigItem::Radio:
1320
 
1321
                    if(0==Value(h)){
1322
                    pDoc->SetValue(ti, (ItemIntegerType) 1);
1323
                    }
1324
                    break;
1325
                */
1326
            default:
1327
                break;
1328
            }
1329
        }
1330
    }
1331
    return rc;
1332
}
1333
 
1334
#if 0
1335
 
1336
/* Presumably we don't need this since we use the m_parent member instead
1337
ecConfigItem *ecConfigItem::Parent() const
1338
{
1339
    CTreeCtrl &tree=CConfigTool::GetControlView()->GetTreeCtrl();
1340
    HTREEITEM hParent=tree.GetParentItem(HItem());
1341
    return (NULL==hParent||TVI_ROOT==hParent)?NULL:(ecConfigItem *)tree.GetItemData(hParent);
1342
}
1343
*/
1344
 
1345
#endif
1346
 
1347
/*
1348
 * ecTextEditorCtrl
1349
 * A specialised wxTextCtrl, for editing config values
1350
 */
1351
 
1352
BEGIN_EVENT_TABLE(ecTextEditorCtrl, wxTextCtrl)
1353
    EVT_TEXT_ENTER(-1, ecTextEditorCtrl::OnEnter)
1354
    EVT_KILL_FOCUS(ecTextEditorCtrl::OnKillFocus)
1355
    EVT_LEFT_DCLICK(ecTextEditorCtrl::OnLeftDClick)
1356
END_EVENT_TABLE()
1357
 
1358
IMPLEMENT_CLASS(ecTextEditorCtrl, wxTextCtrl)
1359
 
1360
ecTextEditorCtrl::ecTextEditorCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1361
                                   long style):
1362
    wxTextCtrl(parent, id, wxEmptyString, pos, size, style)
1363
{
1364
}
1365
 
1366
void ecTextEditorCtrl::OnEnter(wxCommandEvent& event)
1367
{
1368
    ecValueWindow* parent = (ecValueWindow*) GetParent();
1369
    parent->EndEditing();
1370
}
1371
 
1372
void ecTextEditorCtrl::OnKillFocus(wxFocusEvent& event)
1373
{
1374
    ecValueWindow* parent = (ecValueWindow*) GetParent();
1375
    parent->EndEditing();
1376
}
1377
 
1378
// Edit the string in a separate dialog, for convenience
1379
void ecTextEditorCtrl::OnLeftDClick(wxMouseEvent& event)
1380
{
1381
    ecValueWindow* parent = (ecValueWindow*) GetParent();
1382
    ecConfigItem* item = parent->GetCurrentConfigItem();
1383
    ecConfigToolDoc* doc = wxGetApp().GetConfigToolDoc();
1384
 
1385
    wxString initialValue(GetValue());
1386
    initialValue.Replace("\\n", "\n"); // remove escaping of newline chars
1387
 
1388
    ecEditStringDialog dialog(initialValue, wxGetApp().GetTopWindow(), ecID_EDIT_STRING_DIALOG);
1389
    if (dialog.ShowModal() == wxID_OK)
1390
    {
1391
        wxString val = dialog.GetValue() ;
1392
        // This control will have been deleted at this point, due to losing the focus.
1393
        // So update the item, not the control.
1394
        // wxTextCtrl::SetValue(val);
1395
        doc->SetValue(*item, val);
1396
    }
1397
}
1398
 
1399
/*
1400
 * ecDoubleEditorCtrl
1401
 * A specialised wxTextCtrl, for editing double config values
1402
 */
1403
 
1404
BEGIN_EVENT_TABLE(ecDoubleEditorCtrl, wxTextCtrl)
1405
    EVT_TEXT_ENTER(-1, ecDoubleEditorCtrl::OnEnter)
1406
    EVT_KILL_FOCUS(ecDoubleEditorCtrl::OnKillFocus)
1407
END_EVENT_TABLE()
1408
 
1409
IMPLEMENT_CLASS(ecDoubleEditorCtrl, wxTextCtrl)
1410
 
1411
ecDoubleEditorCtrl::ecDoubleEditorCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1412
                                   long style):
1413
    wxTextCtrl(parent, id, wxEmptyString, pos, size, style)
1414
{
1415
}
1416
 
1417
void ecDoubleEditorCtrl::OnEnter(wxCommandEvent& event)
1418
{
1419
    ecValueWindow* parent = (ecValueWindow*) GetParent();
1420
    parent->EndEditing();
1421
}
1422
 
1423
void ecDoubleEditorCtrl::OnKillFocus(wxFocusEvent& event)
1424
{
1425
    ecValueWindow* parent = (ecValueWindow*) GetParent();
1426
    parent->EndEditing();
1427
}
1428
 
1429
/*
1430
 * ecIntegerEditorCtrl
1431
 * A specialised wxTextCtrl, for editing double config values
1432
 */
1433
 
1434
BEGIN_EVENT_TABLE(ecIntegerEditorCtrl, wxSpinCtrl)
1435
    EVT_TEXT_ENTER(-1, ecIntegerEditorCtrl::OnEnter)
1436
    EVT_KILL_FOCUS(ecIntegerEditorCtrl::OnKillFocus)
1437
END_EVENT_TABLE()
1438
 
1439
IMPLEMENT_CLASS(ecIntegerEditorCtrl, wxSpinCtrl)
1440
 
1441
ecIntegerEditorCtrl::ecIntegerEditorCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1442
                                   long style):
1443
    wxSpinCtrl(parent, id, "0", pos, size, style, INT_MIN, INT_MAX, 0)
1444
{
1445
}
1446
 
1447
void ecIntegerEditorCtrl::OnEnter(wxCommandEvent& event)
1448
{
1449
    ecValueWindow* parent = (ecValueWindow*) GetParent();
1450
    parent->EndEditing();
1451
}
1452
 
1453
void ecIntegerEditorCtrl::OnKillFocus(wxFocusEvent& event)
1454
{
1455
    ecValueWindow* parent = (ecValueWindow*) GetParent();
1456
    parent->EndEditing();
1457
}
1458
 
1459
/*
1460
 * ecEnumEditorCtrl
1461
 * A specialised wxChoice, for editing enumerated config values
1462
 */
1463
 
1464
BEGIN_EVENT_TABLE(ecEnumEditorCtrl, wxChoice)
1465
    EVT_CHAR(ecEnumEditorCtrl::OnChar)
1466
    EVT_KILL_FOCUS(ecEnumEditorCtrl::OnKillFocus)
1467
END_EVENT_TABLE()
1468
 
1469
IMPLEMENT_CLASS(ecEnumEditorCtrl, wxChoice)
1470
 
1471
ecEnumEditorCtrl::ecEnumEditorCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1472
                                   long style):
1473
    wxChoice(parent, id, pos, size, 0, 0, style)
1474
{
1475
}
1476
 
1477
void ecEnumEditorCtrl::OnChar(wxKeyEvent& event)
1478
{
1479
    if (event.GetKeyCode() == WXK_RETURN)
1480
    {
1481
        ecValueWindow* parent = (ecValueWindow*) GetParent();
1482
        parent->EndEditing();
1483
    }
1484
    else
1485
        event.Skip();
1486
}
1487
 
1488
void ecEnumEditorCtrl::OnKillFocus(wxFocusEvent& event)
1489
{
1490
        /* event may result from drop down of menu on wxGTK so
1491
           check that the mouse is outside the window */
1492
    wxPoint mouse = ScreenToClient(::wxGetMousePosition());
1493
    wxSize size = GetSize();
1494
    if (mouse.x < 0 || mouse.x > size.x || mouse.y < 0 || mouse.y > size.y) {
1495
        ecValueWindow* parent = (ecValueWindow*) GetParent();
1496
                parent->EndEditing();
1497
    }
1498
}
1499
 
1500
/*
1501
 * ecEditStringDialog
1502
 * Pops up to make it easier to edit large string values
1503
 */
1504
 
1505
BEGIN_EVENT_TABLE(ecEditStringDialog, ecDialog)
1506
    EVT_BUTTON(wxID_OK, ecEditStringDialog::OnOK)
1507
END_EVENT_TABLE()
1508
 
1509
ecEditStringDialog::ecEditStringDialog(const wxString& initialValue, wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
1510
        long style)
1511
{
1512
    m_value = initialValue;
1513
    //SetExtraStyle(wxDIALOG_EX_CONTEXTHELP);
1514
 
1515
    ecDialog::Create(parent, id, _("String Edit"),
1516
        wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER);
1517
 
1518
    CreateControls(this);
1519
 
1520
    TransferDataToWindow();
1521
 
1522
    Centre(wxBOTH);
1523
}
1524
 
1525
ecEditStringDialog::~ecEditStringDialog()
1526
{
1527
}
1528
 
1529
//// Event handlers
1530
 
1531
void ecEditStringDialog::OnOK(wxCommandEvent& event)
1532
{
1533
    event.Skip();
1534
}
1535
 
1536
//// Operations
1537
void ecEditStringDialog::CreateControls(wxWindow* parent)
1538
{
1539
    wxSizer *item0 = new wxBoxSizer( wxVERTICAL );
1540
 
1541
    wxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
1542
 
1543
    item1->Add( 20, 20, 10, wxALIGN_CENTRE|wxALL, 5 );
1544
 
1545
    wxButton *item2 = new wxButton( parent, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
1546
    item2->SetDefault();
1547
    item1->Add( item2, 0, wxALIGN_CENTRE|wxALL, 5 );
1548
 
1549
    wxButton *item3 = new wxButton( parent, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
1550
    item1->Add( item3, 0, wxALIGN_CENTRE|wxALL, 5 );
1551
 
1552
    item0->Add( item1, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxTOP, 5 );
1553
 
1554
    wxTextCtrl *item4 = new wxTextCtrl( parent, ecID_STRING_EDIT_TEXTCTRL, _(""), wxDefaultPosition, wxSize(420,250), wxTE_MULTILINE );
1555
    item0->Add( item4, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM, 5 );
1556
 
1557
    parent->SetAutoLayout( TRUE );
1558
    parent->SetSizer( item0 );
1559
    parent->Layout();
1560
    item0->Fit( parent );
1561
    item0->SetSizeHints( parent );
1562
 
1563
    FindWindow(ecID_STRING_EDIT_TEXTCTRL)->SetValidator(wxGenericValidator(& m_value));
1564
    FindWindow(ecID_STRING_EDIT_TEXTCTRL)->SetFocus();
1565
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.