Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / ConfigViewer / ConfigTreeCtrl.cpp
blob837c6329bd28c7813bed05305fe4307329894591
1 #include "stdafx.h"
2 #include "ConfigTreeCtrl.h"
3 #include "MainFrame.h"
4 #include "ValueDlg.h"
5 #include "ValueListCtrl.h"
7 enum {CFGNEWKEY=100, CFGNEWSTRING, CFGNEWUINT, CFGNEWBINARY, CFGNEWSUBMENU, CFGFIND, CFGDELETE, CFGRENAME, CFGCOPYKEYNAME};
10 BEGIN_EVENT_TABLE(ConfigTreeCtrl, wxTreeCtrl)
11 EVT_RIGHT_DOWN(ConfigTreeCtrl::OnRightDown)
12 EVT_RIGHT_UP(ConfigTreeCtrl::OnRightUp)
13 EVT_MENU(CFGNEWKEY, ConfigTreeCtrl::OnNewKey)
14 EVT_MENU(CFGNEWSTRING, ConfigTreeCtrl::OnNewString)
15 EVT_MENU(CFGNEWUINT, ConfigTreeCtrl::OnNewUINT)
16 EVT_MENU(CFGNEWBINARY, ConfigTreeCtrl::OnNewBinary)
17 EVT_MENU(CFGFIND, ConfigTreeCtrl::OnFind)
18 EVT_MENU(CFGDELETE, ConfigTreeCtrl::OnDelete)
19 EVT_TREE_SEL_CHANGED(FRAME_TREE, ConfigTreeCtrl::OnSelChanged)
20 END_EVENT_TABLE()
23 ConfigTreeCtrl::ConfigTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name)
24 : wxTreeCtrl(parent, id, pos, size, style, validator, name)
26 // Load the tree
27 LoadTree();
30 ConfigTreeCtrl::~ConfigTreeCtrl()
34 void ConfigTreeCtrl::LoadTree()
36 m_pConfig = MainFrame::Instance()->GetpConfig();
37 const ACE_Configuration_Section_Key& Key = m_pConfig->root_section();
38 wxTreeItemId Root = AppendItem(GetRootItem(), "Root");
39 LoadSection(Root, Key);
42 void ConfigTreeCtrl::LoadSection(wxTreeItemId& ParentItem, const ACE_Configuration_Section_Key& Key)
44 ACE_TString Name;
45 int Index = 0;
46 while(!m_pConfig->enumerate_sections(Key, Index, Name))
48 wxTreeItemId Item = AppendItem(ParentItem, Name.fast_rep());
49 ACE_Configuration_Section_Key Child;
50 m_pConfig->open_section(Key, Name.fast_rep(), 0, Child);
51 LoadSection( Item, Child);
52 ++Index;
56 void ConfigTreeCtrl::OnRightDown(wxMouseEvent& event)
58 //EndEditLabel(TRUE);
59 int Flags = wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMICON;
60 long ItemID = HitTest(wxPoint(event.m_x, event.m_y), Flags);
61 SelectItem(ItemID);
64 void ConfigTreeCtrl::OnRightUp(wxMouseEvent& event)
66 wxTreeItemId ItemID = GetSelection();
68 wxMenu* pMenu = new wxMenu;
69 wxMenu* pNewMenu = new wxMenu;
70 pNewMenu->Append(CFGNEWKEY, "Key");
71 pNewMenu->AppendSeparator();
72 pNewMenu->Append(CFGNEWSTRING, "String");
73 pNewMenu->Append(CFGNEWUINT, "Unsigned Int");
74 //pNewMenu->Append(CFGNEWBINARY, "Binary");
75 pMenu->Append(CFGNEWSUBMENU, "New", pNewMenu);
76 pMenu->Append(CFGFIND, "Find");
77 pMenu->AppendSeparator();
78 pMenu->Append(CFGDELETE, "Delete");
79 //pMenu->Append(CFGRENAME, "Rename"); // not supported
80 //pMenu->AppendSeparator();
81 //pMenu->Append(CFGCOPYKEYNAME, "Copy Key Name"); // not supported
82 PopupMenu(pMenu, event.m_x, event.m_y);
83 delete pMenu;
86 void ConfigTreeCtrl::ResolveKey(wxTreeItemId Item, ACE_Configuration_Section_Key& Key)
88 wxTreeItemId OriginalItem = Item;
89 ACE_TString Path("");
90 ACE_TString Temp;
91 while(Item != GetRootItem())
93 wxString Text = GetItemText(Item);
94 Temp = Path;
95 Path = Text.c_str();
96 if(Temp.length())
98 Path += "\\";
99 Path += Temp;
101 Item = GetParent(Item);
103 if(Path.length())
105 m_pConfig->expand_path(m_pConfig->root_section(), Path, Key, 0);
107 else
109 Key = m_pConfig->root_section();
114 void ConfigTreeCtrl::OnNewKey(wxCommandEvent& event)
116 wxTextEntryDialog Dlg(this, "Test", "Key Name");
117 if(Dlg.ShowModal() != wxID_OK)
119 return;
122 wxString Value = Dlg.GetValue();
124 // Get the key for this node
125 wxTreeItemId ItemID = GetSelection();
126 ACE_Configuration_Section_Key Key, NewKey;
127 ResolveKey(ItemID, Key);
128 m_pConfig->open_section(Key, Value, 1, NewKey);
129 wxTreeItemId NewItemID = AppendItem(ItemID, Value);
130 EnsureVisible(NewItemID);
133 void ConfigTreeCtrl::OnNewString(wxCommandEvent& event)
135 ValueDlg Dlg(this, true);
136 if(Dlg.ShowModal() != wxID_OK)
138 return;
141 ACE_TString Value = Dlg.GetStringValue();
142 ACE_TString Name = Dlg.GetName();
144 // Get the key for this node
145 wxTreeItemId ItemID = GetSelection();
146 ACE_Configuration_Section_Key Key;
147 ResolveKey(ItemID, Key);
148 m_pConfig->set_string_value(Key, Name.fast_rep(), Value);
149 m_pListCtrl->DisplaySection(Key);
152 void ConfigTreeCtrl::OnNewUINT(wxCommandEvent& event)
154 ValueDlg Dlg(this, false);
155 if(Dlg.ShowModal() != wxID_OK)
157 return;
160 u_int Value = Dlg.GetUINTValue();
161 ACE_TString Name = Dlg.GetName();
163 // Get the key for this node
164 wxTreeItemId ItemID = GetSelection();
165 ACE_Configuration_Section_Key Key;
166 ResolveKey(ItemID, Key);
167 m_pConfig->set_integer_value(Key, Name.fast_rep(), Value);
168 m_pListCtrl->DisplaySection(Key);
171 void ConfigTreeCtrl::OnNewBinary(wxCommandEvent& event)
173 assert(0);
175 ValueDlg Dlg(this, true);
176 if(Dlg.ShowModal() != wxID_OK)
178 return;
181 ACE_TString Value = Dlg.GetStringValue();
182 ACE_TString Name = Dlg.GetName();
184 // Get the key for this node
185 wxTreeItemId ItemID = GetSelection();
186 ACE_Configuration_Section_Key Key;
187 ResolveKey(ItemID, Key);
188 m_pConfig->set_string_value(Key, Name.fast_rep(), Value);
189 m_pListCtrl->DisplaySection(Key);
193 void ConfigTreeCtrl::OnSelChanged(wxTreeEvent& event)
195 wxTreeItemId ItemID = GetSelection();
196 ACE_Configuration_Section_Key Key;
197 ResolveKey(ItemID, Key);
198 m_pListCtrl->DisplaySection(Key);
201 void ConfigTreeCtrl::OnFind(wxCommandEvent& event)
205 void ConfigTreeCtrl::OnDelete(wxCommandEvent& event)
207 wxTreeItemId ItemID = GetSelection();
208 wxTreeItemId Parent = GetParent(ItemID);
209 ACE_Configuration_Section_Key Key;
210 ResolveKey(Parent, Key);
211 wxMessageDialog Dlg(this, "Are you sure you want to delete this section?", "Confirm Section Delete", wxYES_NO | wxICON_EXCLAMATION );
212 if(Dlg.ShowModal() != wxID_YES)
214 return;
216 wxString Text = GetItemText(ItemID);
217 m_pConfig->remove_section(Key, Text, 1);
218 // Reload parent
219 Delete(ItemID);
222 void ConfigTreeCtrl::ChangeConfig(ACE_Configuration* pConfig)
224 m_pConfig = pConfig;
225 DeleteAllItems();
226 LoadTree();