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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [configtool/] [common/] [win32/] [ConfigItem.cpp] - Blame information for rev 786

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 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
//
23
//===========================================================================
24
//===========================================================================
25
//#####DESCRIPTIONBEGIN####
26
//
27
// Author(s):   sdf
28
// Contact(s):  sdf
29
// Date:                1998/08/11
30
// Version:             0.01
31
// Purpose:     
32
// Description: This is the implementation of the configuration item class
33
// Requires:    
34
// Provides:    
35
// See also:    
36
// Known bugs:  
37
// Usage:       
38
//
39
//####DESCRIPTIONEND####
40
//
41
//===========================================================================
42
#include "stdafx.h"
43
#include "ConfigItem.h"
44
#include "ControlView.h"
45
#include "CTUtils.h"
46
#ifdef PLUGIN
47
  #define INCLUDEFILE "ide.common.h" // for setEditLocation
48
  #include "IncludeSTL.h"
49
  #include "common/CodeCoordinate.h"
50
#endif
51
#include "ConfigToolDoc.h"
52
#include "ConfigTool.h"
53
 
54
LPCTSTR CConfigItem::TreeItemTypeImage[MaxTreeItemType + 1]={
55
  _T("None"), _T("Integer"), _T("Enumeration"), _T("String"), _T("Double"), 0}; // Internationalization OK
56
 
57
const CFileName CConfigItem::FileName() const
58
{
59
  CFileName strFile;
60
  const CdlNode node = dynamic_cast<CdlNode> (m_CdlItem);
61
  if (node){
62
    // get the package which owns the configuration item
63
    const CdlPackage package = GetOwnerPackage();
64
    if (package){
65
 
66
      // return the filename of the config header
67
      strFile=CFileName(CConfigTool::GetConfigToolDoc()->InstallTree()+_T("include\\pkgconf"))+package->get_config_header ().c_str ();
68
    }
69
  }
70
  return strFile;
71
}
72
 
73
CConfigItem::CConfigItem(CConfigItem *pParent, CdlUserVisible CdlItem):
74
  m_CdlItem(CdlItem)
75
{
76
  CTreeCtrl &tree=CConfigTool::GetControlView()->GetTreeCtrl();
77
  HTREEITEM hParent;
78
  if(NULL==CdlItem){
79
    // This is the root item
80
    hParent=TVI_ROOT;
81
    m_Type=None;
82
  } else {
83
    hParent=pParent->HItem();
84
 
85
    // FIXME: re-implement using CdlValuableBody::get_widget_hint()
86
    if (IsPackage()) {
87
      // If a package item, display the package version string
88
      m_Type=String;
89
    } else {
90
      const CdlValuable valuable = dynamic_cast<CdlValuable> (CdlItem);
91
      switch (valuable->get_flavor ()){
92
        case CdlValueFlavor_None:
93
        case CdlValueFlavor_Bool:
94
          m_Type=None;
95
          break;
96
        case CdlValueFlavor_Data:
97
        case CdlValueFlavor_BoolData:
98
          if (! valuable->has_legal_values ()) {
99
            m_Type=String;
100
          } else if (0 == valuable->get_legal_values ()->ranges.size ()) {
101
            m_Type=Enum;
102
          } else {
103
            CdlListValue list_value;
104
            CdlEvalContext context (NULL, valuable, valuable->get_property (CdlPropertyId_LegalValues));
105
            valuable->get_legal_values ()->eval (context, list_value);
106
            m_Type=list_value.get_double_ranges ().size () ? Double : Integer;
107
          }
108
          break;
109
        default:
110
          ASSERT (0); // specified flavor not supported
111
          break;
112
      }
113
    }
114
  }
115
  m_hItem=tree.InsertItem(ItemNameOrMacro(),hParent);
116
  tree.SetItemData(m_hItem,(DWORD)this);
117
  CConfigTool::GetControlView()->AdjustItemImage(m_hItem);
118
}
119
 
120
CConfigItem::~CConfigItem()
121
{
122
}
123
 
124
CString CConfigItem::GetURL() const
125
{
126
  for(const CConfigItem *pItem=this;pItem;pItem=pItem->Parent()){
127
    if(pItem->GetCdlItem()){
128
      CString strURL;
129
      strURL=pItem->GetCdlItem()->get_doc_url().c_str();
130
      if(strURL.GetLength()){
131
        return strURL;
132
      }
133
      strURL=pItem->GetCdlItem()->get_doc().c_str();
134
      if(strURL.GetLength()){
135
        return strURL;
136
      }
137
    }
138
  }
139
  return _T("ref/ecos-ref.html"); // the default URL
140
}
141
 
142
bool CConfigItem::SetValue(LPCTSTR pszValue, CdlTransaction transaction/*=NULL*/)
143
{
144
  ASSERT ((m_Type == String) || (m_Type == Enum));
145
  const CdlValuable valuable = GetCdlValuable();
146
  ASSERT (valuable);
147
  const std::string str=CUtils::UnicodeToStdStr (pszValue);
148
  if(transaction){
149
    if (CdlValueFlavor_BoolData == valuable->get_flavor ()){
150
      // set the user bool to the current bool when changing a booldata
151
      // value to avoid a possible change in the current bool
152
      valuable->set_enabled_and_value (transaction, valuable->is_enabled (), str, CdlValueSource_User);
153
    } else {// CdlValueFlavor_Data
154
      valuable->set_value (transaction, str, CdlValueSource_User);
155
    }
156
  } else {
157
    if (CdlValueFlavor_BoolData == valuable->get_flavor ()){
158
      // set the user bool to the current bool when changing a booldata
159
      // value to avoid a possible change in the current bool
160
      valuable->set_enabled_and_value (valuable->is_enabled (), str, CdlValueSource_User);
161
    } else {// CdlValueFlavor_Data
162
      valuable->set_value (str, CdlValueSource_User);
163
    }
164
  }
165
 
166
  return true;
167
}
168
 
169
bool CConfigItem::SetValue (double dValue, CdlTransaction transaction/*=NULL*/)
170
{
171
  ASSERT (m_Type == Double);
172
  const CdlValuable valuable = GetCdlValuable();
173
  ASSERT (valuable);
174
 
175
  if(transaction) {
176
    if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
177
      // set the user bool to the current bool when changing a booldata
178
      // value to avoid a possible change in the current bool
179
      valuable->set_enabled_and_value (transaction, valuable->is_enabled (), dValue, CdlValueSource_User);
180
    } else {// CdlValueFlavor_Data
181
      valuable->set_double_value (transaction, dValue, CdlValueSource_User);
182
    }
183
  } else {
184
    if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
185
      // set the user bool to the current bool when changing a booldata
186
      // value to avoid a possible change in the current bool
187
      valuable->set_enabled_and_value (valuable->is_enabled (), dValue, CdlValueSource_User);
188
    } else {// CdlValueFlavor_Data
189
      valuable->set_double_value (dValue, CdlValueSource_User);
190
    }
191
  }
192
 
193
  return true;
194
}
195
 
196
CConfigItem *CConfigItem::FirstRadio() const
197
{
198
  ASSERT(HasRadio ());
199
 
200
  for(CConfigItem *h=Parent()->FirstChild();h;h=h->NextSibling()){
201
    if(h->HasRadio ()){
202
      return h;
203
    }
204
  }
205
  // No radio buttons found
206
  ASSERT(false);
207
  return false;
208
}
209
 
210
bool CConfigItem::IsEnabled() const
211
{
212
  const CdlValuable valuable = GetCdlValuable();
213
  return NULL==valuable ||valuable->is_enabled();
214
}
215
 
216
bool CConfigItem::SetValue (ItemIntegerType nValue, CdlTransaction transaction/*=NULL*/)
217
{
218
  ASSERT (m_Type == Integer);
219
  const CdlValuable valuable = GetCdlValuable();
220
  ASSERT (valuable);
221
 
222
  if(transaction) {
223
    if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
224
      // set the user bool to the current bool when changing a booldata
225
      // value to avoid a possible change in the current bool
226
      valuable->set_enabled_and_value (transaction, valuable->is_enabled (), (cdl_int) nValue, CdlValueSource_User);
227
    } else { // CdlValueFlavor_Data
228
      valuable->set_integer_value (transaction, nValue, CdlValueSource_User);
229
    }
230
  } else {
231
    if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
232
      // set the user bool to the current bool when changing a booldata
233
      // value to avoid a possible change in the current bool
234
      valuable->set_enabled_and_value (valuable->is_enabled (), (cdl_int) nValue, CdlValueSource_User);
235
    } else { // CdlValueFlavor_Data
236
      valuable->set_integer_value (nValue, CdlValueSource_User);
237
    }
238
  }
239
 
240
  return true;
241
}
242
 
243
bool CConfigItem::HasModifiedChildren() const
244
{
245
  for(CConfigItem *pItem=FirstChild();pItem;pItem=pItem->NextSibling()){
246
    if(pItem->Modified()||pItem->HasModifiedChildren()){
247
      return true;
248
    }
249
  }
250
  return false;
251
}
252
 
253
ItemIntegerType CConfigItem::Value () const
254
{
255
  ASSERT (!IsPackage()); // not a package item
256
  const CdlValuable valuable = GetCdlValuable();
257
  ASSERT (valuable);
258
  ItemIntegerType nValue (0);
259
 
260
  switch (valuable->get_flavor ())
261
  {
262
    //  case CdlValueFlavor_Bool:
263
    //          nValue = valuable->is_enabled (CdlValueSource_Current) ? 1 : 0;
264
    //          break;
265
 
266
  case CdlValueFlavor_BoolData:
267
  case CdlValueFlavor_Data:
268
    nValue = (ItemIntegerType) valuable->get_integer_value (CdlValueSource_Current);
269
    break;
270
 
271
  default:
272
    ASSERT (0); // specified flavor not supported
273
  }
274
 
275
  return nValue;
276
}
277
 
278
const double CConfigItem::DoubleValue (CdlValueSource source /* = CdlValueSource_Current */ ) const
279
{
280
  ASSERT (!IsPackage()); // not a package item
281
  const CdlValuable valuable = GetCdlValuable();
282
  ASSERT (valuable);
283
  ASSERT (valuable->has_double_value (source));
284
  return valuable->get_double_value (source);
285
}
286
 
287
ItemIntegerType CConfigItem::DefaultValue () const
288
{
289
  ItemIntegerType nValue;
290
  return CUtils::StrToItemIntegerType (StringValue (CdlValueSource_Default), nValue) ? nValue : 0;
291
}
292
 
293
const CString CConfigItem::StringValue (CdlValueSource source /* = CdlValueSource_Current */ ) const
294
{
295
  //    ASSERT (!IsPackage()); // not a package item
296
  const CdlValuable valuable = GetCdlValuable();
297
  ASSERT (valuable);
298
  CString strValue (_T(""));
299
 
300
  switch (valuable->get_flavor ())
301
  {
302
    case CdlValueFlavor_Data:
303
    case CdlValueFlavor_BoolData:
304
    case CdlValueFlavor_None: // a package
305
      if (m_Type == Integer)
306
        strValue = CUtils::IntToStr (Value (), CConfigTool::GetConfigToolDoc ()->m_bHex);
307
      else if (m_Type == Double)
308
        strValue = CUtils::DoubleToStr (DoubleValue ());
309
      else
310
        strValue = valuable->get_value (source).c_str ();
311
      break;
312
 
313
    default:
314
      ASSERT (0); // specified flavor not supported
315
  }
316
 
317
  return strValue;
318
}
319
 
320
const CString CConfigItem::StringValue(WhereType where) const
321
{
322
  CString str;
323
  switch(where){
324
    case InName:
325
      str=Name();
326
      break;
327
    case InMacro:
328
      str=Macro();
329
      break;
330
    case InDesc:
331
      str=Desc();
332
      break;
333
    case InCurrentValue:
334
      str=CConfigItem::None==Type()?_T(""):StringValue(CdlValueSource_Current);
335
      break;
336
    case InDefaultValue:
337
      str=CConfigItem::None==Type()?_T(""):StringValue(CdlValueSource_Default);
338
      break;
339
    default:
340
      ASSERT(FALSE);
341
      break;
342
  }
343
  return str;
344
}
345
 
346
void CConfigItem::DumpItem()
347
{
348
  TRACE(_T("Item %08x\n\tDisplay Name='%s'\n\tMacro Name='%s'\n\tType=%s"),
349
    this,       Name(),           Macro(),    TreeItemTypeImage[m_Type]);
350
  TRACE(_T("\n\tValue=%s\n\tURL=%s\n\tParent=%08x"),StringValue(), GetURL(), Parent());
351
 
352
  TRACE(_T("\n"));
353
}
354
 
355
CConfigItem * CConfigItem::NextRadio() const
356
{
357
  ASSERT(this->HasRadio ());
358
  for(CConfigItem *pItem=NextSibling();pItem;pItem=pItem->NextSibling()){
359
    if(pItem->HasRadio()){
360
      return pItem;
361
    }
362
  }
363
  return NULL;
364
}
365
 
366
bool CConfigItem::Modified () const
367
{
368
  const CdlValuable valuable = GetCdlValuable();
369
  return
370
    valuable        // accommodate the root config item which has no CDL item
371
    && !IsPackage() // packages are never modified
372
    && valuable->get_source () != CdlValueSource_Default;
373
}
374
 
375
CString CConfigItem::ItemNameOrMacro() const
376
{
377
  CConfigToolDoc *pDoc=CConfigTool::GetConfigToolDoc();
378
  return pDoc->m_bMacroNames&&!Macro().IsEmpty()?Macro():Name();
379
}
380
 
381
bool CConfigItem::IsDescendantOf(CConfigItem * pAncestor)
382
{
383
  for(CConfigItem *pItem=Parent();pItem;pItem=pItem->Parent()){
384
    if(pItem==pAncestor){
385
      return true;
386
    }
387
  }
388
  return false;
389
}
390
 
391
int CConfigItem::EvalEnumStrings (CStringArray &arEnumStrings) const
392
{
393
  const CdlValuable valuable = GetCdlValuable();
394
  ASSERT (valuable);
395
  /*
396
  if (m_Type == Boolean)
397
  {
398
  arEnumStrings.SetSize (2);
399
  arEnumStrings.SetAt (0, _T("True"));
400
  arEnumStrings.SetAt (1, _T("False"));
401
  }
402
  else
403
  */
404
  {
405
    ASSERT (m_Type == Enum);
406
    CdlListValue list_value;
407
    CdlEvalContext context (NULL, m_CdlItem, m_CdlItem->get_property (CdlPropertyId_LegalValues));
408
    valuable->get_legal_values ()->eval (context, list_value);
409
    const std::vector<CdlSimpleValue> & table = list_value.get_table ();
410
 
411
    // add legal values to the list
412
    arEnumStrings.SetSize (table.size ());
413
    for (unsigned int nValue = 0; nValue < table.size (); nValue++)
414
    {
415
      arEnumStrings.SetAt (nValue, table [nValue].get_value ().c_str ());
416
    }
417
  }
418
  return arEnumStrings.GetSize();
419
}
420
 
421
bool CConfigItem::HasBool() const
422
{
423
  if (!m_CdlItem) {
424
    return false;
425
  } else if (IsPackage()) {
426
    return false;
427
  } else {
428
    const CdlValuable valuable = GetCdlValuable();
429
    CdlValueFlavor flavor = valuable->get_flavor ();
430
    return (flavor == CdlValueFlavor_Bool) || (flavor == CdlValueFlavor_BoolData);
431
  }
432
}
433
 
434
bool CConfigItem::HasRadio() const
435
{
436
  const CdlValuable valuable = GetCdlValuable();
437
  if (! valuable)
438
    return false;
439
 
440
  CdlWidgetHint hint;
441
  valuable->get_widget_hint (hint);
442
  return (CdlBoolWidget_Radio == hint.bool_widget);
443
}
444
 
445
bool CConfigItem::SetEnabled(bool bEnabled, CdlTransaction current_transaction/*=NULL*/)
446
{
447
  const CdlValuable valuable = GetCdlValuable();
448
  ASSERT (valuable);
449
 
450
  // use a transaction object to ensure that all config items are changed together
451
  CdlTransaction transaction = current_transaction ? current_transaction : CdlTransactionBody::make (CConfigTool::GetConfigToolDoc ()->GetCdlConfig ());
452
 
453
  if (HasRadio () && bEnabled) { // if a new radio button has been selected
454
    for (CConfigItem *pItem = FirstRadio(); pItem; pItem = pItem->NextRadio ()) { // for each radio button in the group
455
      if (pItem != this) { // if not the newly selected radio button
456
        pItem->SetEnabled (false, transaction); // disable the radio button
457
      }
458
    }
459
  }
460
 
461
  if (CdlValueFlavor_BoolData == valuable->get_flavor ()) {
462
    // set the user value to the current data value when enabling/disabling
463
    // a booldata item to avoid a possible change in the current data value
464
    CdlSimpleValue simple_value = valuable->get_simple_value ();
465
    valuable->set_enabled_and_value (transaction, bEnabled, simple_value, CdlValueSource_User);
466
  } else { // CdlValueFlavor_Bool
467
    valuable->set_enabled (transaction, bEnabled, CdlValueSource_User);
468
  }
469
 
470
  if (! current_transaction) { // if not a recursive call to disable a radio button
471
    transaction->body (); // commit the transaction
472
    deleteZ(transaction);
473
  }
474
 
475
  return true;
476
}
477
 
478
bool CConfigItem::ViewHeader()
479
{
480
  bool rc=false;
481
  const CFileName strFile(FileName());
482
  if(!strFile.IsEmpty()){
483
    CConfigToolDoc *pDoc=CConfigTool::GetConfigToolDoc();
484
    if(pDoc->BuildTree().IsEmpty()){
485
      CUtils::MessageBoxF(_T("Cannot display header file until configuration is saved"));
486
    } else {
487
#ifdef PLUGIN
488
      // Load or activate window and leave
489
      CodeCoordinate loc((LPCTSTR)strFile, 0, 0, CodeCoordinate::FILE_LINE);
490
      rc=AppInstance::getAppManager()->getEditorController()->setEditLocation(loc);
491
#else
492
      rc=CUtils::Launch(strFile,pDoc->m_strViewer);
493
#endif
494
    }
495
  }
496
  return rc;
497
}
498
 
499
bool CConfigItem::ViewURL()
500
{
501
  return CConfigTool::GetConfigToolDoc()->ShowURL(GetURL());
502
}
503
 
504
// Unload (a package)
505
bool CConfigItem::Unload()
506
{
507
  bool rc=false;
508
  CdlPackage package=dynamic_cast<CdlPackage>(GetCdlItem());
509
  ASSERT(package);
510
  CConfigToolDoc* pDoc=CConfigTool::GetConfigToolDoc();
511
  // Remove its objects from the view to prevent any painting problems
512
  CConfigTool::GetControlView()->GetTreeCtrl().DeleteItem(HItem());
513
  for(int nItem=0;nItem<pDoc->ItemCount();nItem++){
514
    CConfigItem *pItem=pDoc->Item(nItem);
515
    if(package==pItem->GetOwnerPackage()){
516
      //CConfigTool::GetControlView()->GetTreeCtrl().DeleteItem(pItem->HItem());
517
      pItem->m_hItem=NULL;   // Make sure we can't attempt to paint it
518
      pItem->m_CdlItem=NULL; // Make sure we can't access stale data
519
    }
520
  }
521
 
522
  const CString strMacroName(Macro());
523
  TRACE (_T("Unloading package %s\n"), strMacroName);
524
  try {
525
    pDoc->GetCdlConfig()->unload_package (package);
526
    rc=true;
527
  }
528
  catch (CdlStringException exception) {
529
    CUtils::MessageBoxF(_T("Error unloading package %s:\n\n%s"), strMacroName, CString (exception.get_message ().c_str ()));
530
  }
531
  catch (...) {
532
    CUtils::MessageBoxF(_T("Error unloading package %s"), strMacroName);
533
  }
534
  m_hItem=NULL;   // Make sure we can't attempt to paint it
535
  m_CdlItem=NULL; // Make sure we can't access stale data
536
  return rc;
537
}
538
 
539
// Change version (of a package)
540
bool CConfigItem::ChangeVersion(const CString &strVersion)
541
{
542
  bool rc=false;
543
  CdlPackage package=dynamic_cast<CdlPackage>(GetCdlItem());
544
  ASSERT(package);
545
  const CdlValuable valuable = GetCdlValuable();
546
  ASSERT (valuable);
547
  const CString strMacroName(Macro());
548
  if (strVersion != valuable->get_value ().c_str ()) { // if the wrong version is loaded
549
    TRACE (_T("Changing package %s to version '%s'\n"), strMacroName, strVersion);
550
    try {
551
      CConfigTool::GetConfigToolDoc()->GetCdlConfig()->change_package_version (package, CUtils::UnicodeToStdStr (strVersion), CConfigToolDoc::CdlParseErrorHandler, CConfigToolDoc::CdlParseWarningHandler);
552
      rc=true;
553
    }
554
    catch (CdlStringException exception) {
555
      CUtils::MessageBoxF(_T("Error changing package %s to version '%s':\n\n%s"), strMacroName, strVersion, CString (exception.get_message ().c_str ()));
556
    }
557
    catch (...) {
558
      CUtils::MessageBoxF(_T("Error changing package %s to version '%s'"), strMacroName, strVersion);
559
    }
560
  }
561
  return rc;
562
}
563
 
564
CConfigItem *CConfigItem::Parent() const
565
{
566
  CTreeCtrl &tree=CConfigTool::GetControlView()->GetTreeCtrl();
567
  HTREEITEM hParent=tree.GetParentItem(HItem());
568
  return (NULL==hParent||TVI_ROOT==hParent)?NULL:(CConfigItem *)tree.GetItemData(hParent);
569
}
570
 
571
CConfigItem *CConfigItem::FirstChild() const
572
{
573
  CTreeCtrl &tree=CConfigTool::GetControlView()->GetTreeCtrl();
574
  HTREEITEM hChild=tree.GetChildItem(HItem());
575
  return hChild?(CConfigItem *)tree.GetItemData(hChild):NULL;
576
}
577
 
578
CConfigItem *CConfigItem::NextSibling() const
579
{
580
  CTreeCtrl &tree=CConfigTool::GetControlView()->GetTreeCtrl();
581
  HTREEITEM hSibling=tree.GetNextSiblingItem(HItem());
582
  return hSibling?(CConfigItem *)tree.GetItemData(hSibling):NULL;
583
}

powered by: WebSVN 2.1.0

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