00001 #include "RegistryGui.h"
00002
00003 #include <Midad/Gui/GuiGroup.h>
00004 #include <Midad/Gui/GuiMultiEntry.h>
00005 #include <Midad/Gui/GuiMainWindow.h>
00006 #include <Midad/Gui/GuiTextEntry.h>
00007 #include <Midad/Gui/GuiMenu.h>
00008
00009 #include <sstream>
00010 #include <string>
00011 using namespace std;
00012
00013 #include <sigc++/sigc++.h>
00014 #include <sigc++/class_slot.h>
00015 using namespace SigC;
00016
00017 RegistryGui::RegistryGui(TGWindow& parent)
00018 : GuiBox(parent,kVerticalFrame)
00019 , fGroup(0)
00020 , fMultiEntry(0)
00021 {
00022 fGroup = manage(new GuiGroup(*this,""));
00023 this->Add(*fGroup);
00024
00025 fMultiEntry = manage(new GuiMultiEntry(*fGroup));
00026 fGroup->Add(*fMultiEntry);
00027
00028 }
00029
00030 RegistryGui::~RegistryGui()
00031 {
00032 }
00033
00034 void RegistryGui::SetRegistry(const Registry& reg)
00035 {
00036 fGroup->SetTitle(reg.GetName());
00037 Registry::RegistryKey rk = reg.Key();
00038 const char* key;
00039 while ( (key = rk()) ) {
00040 string type = reg.GetTypeAsString(key);
00041
00042 if (type == "Registry") {
00043 Registry subreg;
00044 if (!reg.Get(key,subreg)) continue;
00045
00046 RegistryGui* rg = manage(new RegistryGui(*fGroup));
00047 fGroup->Add(*rg);
00048 rg->SetRegistry(subreg);
00049 fSubRegGuis[key] = rg;
00050 }
00051 else {
00052 stringstream label;
00053 label << "'" << key << "'=(" << type << ")" << ends;
00054 string value = reg.GetValueAsString(key);
00055 fMultiEntry->AddEntry(label.str().c_str(),value.c_str());
00056 }
00057 }
00058 }
00059
00060
00061 Registry RegistryGui::GetRegistry() const
00062 {
00063 Registry reg(false);
00064
00065 stringstream ss;
00066 ss << "['" << fGroup->GetTitle() << "'";
00067
00068 GuiMultiEntry::EntryMap_t emap = fMultiEntry->GetEntries();
00069 GuiMultiEntry::EntryMap_t::iterator it, done = emap.end();
00070
00071 for (it = emap.begin(); it != done; ++it) {
00072 ss << " " << it->first << it->second->GetText();
00073 }
00074 ss << "]" << ends;
00075
00076 reg.ReadStream(ss);
00077
00078 map<const char*,RegistryGui*>::const_iterator mit, mdone = fSubRegGuis.end();
00079 for (mit = fSubRegGuis.begin(); mit != mdone; ++mit) {
00080 Registry subreg = mit->second->GetRegistry();
00081 reg.Set(mit->first,subreg);
00082 }
00083
00084 return reg;
00085 }
00086
00087
00088 static void entry_updater(GuiTextEntry* gte, string entry)
00089 {
00090 gte->SetText(entry.c_str());
00091 }
00092
00093 void RegistryGui::SetPossiblesMenu(const char* label, Registry reg)
00094 {
00095 GuiMultiEntry::BoxMap_t bm = fMultiEntry->GetBoxes();
00096 GuiMultiEntry::BoxMap_t::iterator bit = bm.find(label);
00097 if (bit == bm.end()) return;
00098 GuiBox* box = bit->second;
00099
00100 GuiMultiEntry::EntryMap_t em = fMultiEntry->GetEntries();
00101 GuiMultiEntry::EntryMap_t::iterator eit = em.find(label);
00102 if (eit == em.end()) return;
00103 GuiTextEntry* entry = eit->second;
00104
00105
00106
00107 GuiMenuBar* menubar = manage(new GuiMenuBar(*box));
00108 box->Add(*menubar);
00109 box->Show(*menubar);
00110 GuiMenu* menu = manage(menubar->MakeAddMenu(reg.GetName()));
00111
00112 Registry::RegistryKey key = reg.Key();
00113 const char* kstr;
00114 while ( (kstr=key()) ) {
00115
00116
00117 menu->Add(kstr,bind(slot(entry_updater),entry,reg.GetValueAsString(kstr)));
00118 }
00119 }
00120
00121
00122 static string extract_key(string label)
00123 {
00124 unsigned int equals = label.find("=");
00125 if (equals == string::npos) return "";
00126 return label.substr(1,equals-2);
00127 }
00128
00129 void RegistryGui::SetPossibles(Registry reg)
00130 {
00131 GuiMultiEntry::EntryMap_t emap = fMultiEntry->GetEntries();
00132 GuiMultiEntry::EntryMap_t::iterator it, done = emap.end();
00133
00134
00135
00136 for (it = emap.begin(); it != done; ++it) {
00137 string label = it->first;
00138 Registry r;
00139 const char* key_string = extract_key(label).c_str();
00140 if (!reg.Get(key_string,r)) {
00141
00142 continue;
00143 }
00144 this->SetPossiblesMenu(label.c_str(),r);
00145 }
00146 map<const char*,RegistryGui*>::const_iterator mit, mdone = fSubRegGuis.end();
00147 for (mit = fSubRegGuis.begin(); mit != mdone; ++mit) {
00148 Registry r;
00149 if (!reg.Get(mit->first, r)) continue;
00150 mit->second->SetPossibles(r);
00151 }
00152
00153 GuiMainWindow* gmw = this->GetMainWindow();
00154 if (!gmw) return;
00155 gmw->Layout();
00156 }