00001
00002
00003
00004
00005 #ifndef __DATA_DLG_TOOLS
00006 #define __DATA_DLG_TOOLS
00007
00008 #include "GProperties.h"
00009 #include "GScrollBar.h"
00010 #include "GTextLabel.h"
00011
00012 enum DataDlgType
00013 {
00014 DATA_STR = 1,
00015 DATA_BOOL,
00016 DATA_INT,
00017 DATA_FLOAT,
00018 DATA_PASSWORD,
00019 DATA_STR_SYSTEM
00020
00021
00022 };
00023
00024 class DataDlgTools;
00025
00027 class DataDlgField
00028 {
00029 friend class DataDlgTools;
00030
00031 protected:
00032 int Type;
00033 int CtrlId;
00034 char *Option;
00035 char *Desc;
00036
00037 public:
00038 DataDlgField(int type, int ctrlid, char *opt, char *desc = 0);
00039 ~DataDlgField();
00040
00041 int GetType() { return Type; }
00042 int GetCtrl() { return CtrlId; }
00043 char *GetOption() { return Option; }
00044 char *GetDesc() { return Desc; }
00045
00046 void SetOption(char *o) { char *Opt = NewStr(o); DeleteArray(Option); Option = Opt; }
00047 };
00048
00049 typedef List<DataDlgField> DataDlgFieldList;
00050
00052 class DataDlgTools
00053 {
00054 protected:
00055 bool DeleteEmptyStrings;
00056 GView *Dlg;
00057 ObjProperties *Options;
00058
00059 public:
00060 DataDlgTools();
00061
00062 void Set(GView *Dlg, ObjProperties *Options);
00063 ObjProperties *GetOptions();
00064
00065 bool ProcessField(DataDlgField *f, bool Write, char *OptionOverride = 0);
00066 bool ProcessFields(DataDlgFieldList &Field, bool Write);
00067 bool LoadFields(DataDlgFieldList &Field);
00068 bool SaveFields(DataDlgFieldList &Field);
00069 };
00070
00071 class DRecordSetObj
00072 {
00073 public:
00074 virtual bool Serialize(ObjProperties &f, bool Write)
00075 {
00076 return false;
00077 }
00078 };
00079
00080 template <class Record, class Lst>
00081 class DRecordSetCtrls : public DataDlgTools
00082 {
00083
00084 GText *Description;
00085 GScrollBar *Scroll;
00086 int NewRecordId;
00087 int DeleteRecordId;
00088
00089 DataDlgFieldList *Fields;
00090
00091 protected:
00092 Record *Current;
00093 Lst *Records;
00094
00095 public:
00096 char *RecordStringTemplate;
00097
00098 DRecordSetCtrls( GView *window,
00099 DataDlgFieldList *fields,
00100 Lst *records,
00101 int DescId,
00102 int ScrollId,
00103 int NewId,
00104 int DelId,
00105 char *Template)
00106 {
00107 Current = 0;
00108 RecordStringTemplate = NewStr(Template);
00109 Dlg = window;
00110 Fields = fields;
00111 Records = records;
00112 NewRecordId = NewId;
00113 DeleteRecordId = DelId;
00114
00115 if (Dlg)
00116 {
00117 Description = dynamic_cast<GText*>(Dlg->FindControl(DescId));
00118 Scroll = dynamic_cast<GScrollBar*>(Dlg->FindControl(ScrollId));
00119 OnMoveRecord(dynamic_cast<Record*>(Records->First()));
00120 }
00121 }
00122 ~DRecordSetCtrls()
00123 {
00124 DeleteArray(RecordStringTemplate);
00125 }
00126
00127 virtual Record *NewObj() { return 0; }
00128 virtual void DelObj(Record *Obj) {}
00129
00130 int GetCurrentIndex()
00131 {
00132 return (Records AND Current) ? Records->IndexOf(Current) : -1;
00133 }
00134
00135 void Serialize(bool Write)
00136 {
00137 if (Fields AND Dlg AND Current)
00138 {
00139 ObjProperties Temp;
00140 Options = &Temp;
00141
00142 if (Write)
00143 {
00144
00145 SaveFields(*Fields);
00146
00147
00148 Current->Serialize(Temp, false);
00149 }
00150 else
00151 {
00152
00153 Current->Serialize(Temp, true);
00154
00155
00156 LoadFields(*Fields);
00157 }
00158
00159 Options = 0;
00160 }
00161 }
00162
00163 void OnMoveRecord(Record *r)
00164 {
00165 if (Dlg AND Fields)
00166 {
00167 Serialize(true);
00168 Current = r;
00169 Serialize(false);
00170
00171
00172 for (DataDlgField *f = Fields->First(); f; f = Fields->Next())
00173 {
00174 GView *Ctrl = dynamic_cast<GView*>(Dlg->FindControl(f->GetCtrl()));
00175 if (Ctrl)
00176 {
00177 if (!Current)
00178 {
00179
00180 }
00181
00182 Ctrl->Enabled(Current != 0);
00183 }
00184 }
00185
00186
00187 if (Scroll)
00188 {
00189 Scroll->SetLimits(0, Records->Length() - 1);
00190 Scroll->Value((Current) ? Records->IndexOf(Current) : 0);
00191 Scroll->SetPage(1);
00192 }
00193
00194
00195 if (Description)
00196 {
00197 char Str[256];
00198 sprintf(Str, RecordStringTemplate ? RecordStringTemplate : (char*)"Record: %i of %i", GetCurrentIndex()+1, (Records) ? Records->Length() : 0);
00199 Description->Name(Str);
00200 }
00201 }
00202 }
00203
00204 int OnNotify(GViewI *Col, int Flags)
00205 {
00206 if (Fields AND Dlg AND Records)
00207 {
00208 if (Col == Scroll)
00209 {
00210 int CurrentIndex = (Current) ? Records->IndexOf(Current) : -1;
00211 int NewIndex = Col->Value();
00212 if (CurrentIndex != NewIndex)
00213 {
00214 OnMoveRecord(dynamic_cast<Record*>((*Records)[NewIndex]));
00215 }
00216 }
00217
00218 if (Col->GetId() == NewRecordId)
00219 {
00220 Record *r = NewObj();
00221 if (r)
00222 {
00223 Records->Insert(r);
00224 OnMoveRecord(r);
00225 }
00226 }
00227
00228 if (Col->GetId() == DeleteRecordId)
00229 {
00230 int Index = GetCurrentIndex();
00231 LgiAssert(Index >= 0);
00232
00233 Record *r = dynamic_cast<Record*>((*Records)[Index]);
00234 if (r)
00235 {
00236 Records->Delete(r);
00237
00238 Index = limit(Index, 0, Records->Length()-1);
00239 OnMoveRecord(Index >= 0 ? dynamic_cast<Record*>((*Records)[Index]) : 0);
00240 DelObj(r);
00241 }
00242 }
00243 }
00244
00245 return 0;
00246 }
00247 };
00248
00249 template <class Record, class Lst>
00250 class DRecordSet : public DRecordSetCtrls<Record, Lst>
00251 {
00252 public:
00253 typedef Record *(*Allocator)(GView *);
00254
00255 DRecordSet
00256 (
00257 GView *window,
00258 DataDlgFieldList *fields,
00259 Lst *records,
00260 int DescId,
00261 int ScrollId,
00262 int NewId,
00263 int DelId,
00264 char *Template,
00265 Allocator a
00266 ) : DRecordSetCtrls<Record, Lst>
00267 (
00268 window,
00269 fields,
00270 records,
00271 DescId,
00272 ScrollId,
00273 NewId,
00274 DelId,
00275 Template
00276 )
00277 {
00278 Alloc = a;
00279 }
00280
00281 Record *NewObj()
00282 {
00283 return Alloc ? Alloc(DRecordSetCtrls<Record, Lst>::Dlg) : 0;
00284 }
00285
00286 void DelObj(Record *Obj)
00287 {
00288 if (Obj)
00289 {
00290 DeleteObj(Obj);
00291 }
00292 }
00293
00294 Record *ItemAt(int i=-1)
00295 {
00296 return (i<0 OR !DRecordSetCtrls<Record, Lst>::Records)
00297 ?
00298 dynamic_cast<Record*>(DRecordSetCtrls<Record, Lst>::Current)
00299 :
00300 dynamic_cast<Record*>( (*DRecordSetCtrls<Record, Lst>::Records)[i] );
00301 }
00302
00303 void OnMoveRecord(Record *r)
00304 {
00305 DRecordSetCtrls<Record, Lst>::OnMoveRecord(r);
00306 }
00307
00308 private:
00309 Allocator Alloc;
00310 };
00311
00312 #endif
00313