📄 wwkeycb.pas
字号:
end;
procedure TwwKeyCombo.SetShowAllFields(value: boolean);
begin
FShowAllFields:= value;
if (datasource<>nil) then
begin
initCombo;
end
end;
procedure TwwKeyCombo.InitCombo;
begin
InitComboWithGrid(nil);
end;
procedure TwwKeyCombo.RefreshDisplay;
begin
SkipReload:= True;
try
InitComboWithGrid(nil);
finally
SkipReload:= False;
end
end;
procedure TwwKeyCombo.InitComboWithGrid(grid: TComponent);
var parts : TStrings;
i: integer;
activeIndex, fieldTitle: string;
dataset: TDataSet;
masterSource: TDataSource;
tempIndexName : string;
tempVisible: boolean;
selIndex: integer;
IndexDefs: TIndexDefs;
PropInfo: PPropInfo;
APos: integer;
tempItemIndex: integer;
tempField: TField;
activeDescending: boolean;
function GetIndexName: string;
begin
Result:= '';
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexName');
if PropInfo<>Nil then Result:= GetStrProp(DataSource.DataSet, PropInfo);
end;
function GetIndexFieldNames: string;
begin
Result:= '';
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexFieldNames');
if PropInfo<>Nil then Result:= GetStrProp(DataSource.DataSet, PropInfo);
end;
function DisplayLabelsChanged: boolean;
var i: integer;
begin
result:= False;
for i:= 0 to Items.count-1 do begin
if wwGetFieldNameFromTitle(datasource.dataSet, Items[i])='' then
begin
result:= True;
break;
end
end
end;
begin
if datasource=nil then exit;
if DataSource.DataSet=nil then exit;
if dataSource.dataSet is TTable then
begin
dataset:= dataSource.dataSet;
indexDefs:= (datasource.dataset as TTable).indexdefs;
if wwIsTableQuery(Dataset) then begin
MessageDlg('TwwKeyCombo: DataSet cannot be a TwwTable using a Query.',
mtWarning, [mbok], 0);
exit;
end;
end
else if UseAllFields(datasource.dataset) then
begin
if not SkipReload then
begin
Items.clear;
with Datasource.dataset do
begin
for i:= 0 to FieldCount-1 do
begin
if wwValidFilterableFieldType(Fields[i].datatype) and
{ 3/18/99 - Don't add calculated or lookupfields to list}
(not Fields[i].lookup) and (not Fields[i].Calculated) then
begin
{ 9/15/98 - Only show visible fields in ClientDataSet }
if (grid=nil) then tempVisible:= Fields[i].visible
else tempVisible:= wwFindSelected((grid as TwwCustomDBGrid).selected, Fields[i].FieldName, SelIndex);
if tempVisible or ShowAllIndexes then begin
fieldTitle:= strReplaceChar(Fields[i].displayLabel, '~', ' ');
Items.add(fieldTitle);
end
end
end
end;
end;
APos:= 1;
tempIndexName:= strGetToken(GetIndexFieldNames, ';', APos);
{ 11/12/98 - Map fieldname to displayname }
tempField:= datasource.dataset.findfield(tempindexname);
if tempfield<>nil then tempIndexName:=tempField.displaylabel;
ItemIndex:= Items.indexOf(TempIndexName);
exit;
end
else begin
dataset:= dataSource.dataSet;
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexDefs');
if PropInfo<>Nil then IndexDefs:= TIndexDefs(GetOrdProp(DataSource.DataSet, PropInfo))
else begin
// MessageDlg('TwwKeyCombo: DataSet does not have index information available.',
// mtWarning, [mbok], 0);
exit;
end
end;
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'MasterSource');
if PropInfo<>Nil then masterSource:= TDataSource(GetOrdProp(DataSource.DataSet, PropInfo))
else masterSource:= nil;
if masterSource<>Nil then begin
{ MessageDlg('TwwKeyCombo: Data Source can not be a child table.' + #13 +
'Component ' + self.name + ' has a MasterSource defined.',
mtWarning, [mbok], 0);}
exit;
end;
// if (not SkipReload) or DisplayLabelsChanged then
if (not SkipReload) or (DisplayLabelsChanged and (not ShowAllIndexes)) then // 9/1/04
begin
SkipReload:= False;
Items.clear;
ActiveIndex:= '';
IndexDefs.update; { refreshes Index list }
if ShowAllIndexes then begin
for i:= 0 to IndexDefs.count-1 do begin
tempIndexName:= IndexDefs.Items[i].Name;
if tempIndexName='' then tempIndexName:= GetPrimaryName;
self.items.add(tempIndexName);
end;
end
end;
if ShowAllIndexes then begin
tempIndexName:= GetIndexName;
if tempIndexName='' then tempIndexName:= GetPrimaryName;
ItemIndex:= Items.indexOf(tempIndexName);
exit;
end;
ActiveDescending:= False;
{ Fill combo box with list of index names - but show field titles instead of index names}
parts := TStringList.create;
{ 6/6/97 - Use wwEqualStr function to make comparisons case insensitive }
for i:= 0 to IndexDefs.count-1 do begin
with IndexDefs.Items[i] do begin
strBreakApart(fields, ';', parts);
if not wwDataSetIsValidField(Dataset, parts[0]) then continue;
if (grid=nil) then tempVisible:= dataset.fieldByName(parts[0]).visible
else tempVisible:= wwFindSelected((grid as TwwCustomDBGrid).selected, parts[0], SelIndex);
if tempVisible then begin
fieldTitle:= strReplaceChar(Dataset.fieldByName(parts[0]).displayLabel, '~', ' ');
if (ixDescending in Options) then begin
if (fieldTitle<>'') then
begin
if GetIndexFieldNames = '' then begin
if wwEqualStr(IndexDefs.Items[i].name, GetIndexName) then
begin
activeIndex:= fieldTitle;
activeDescending:= True;
end;
end
else begin
if wwEqualStr(GetIndexFieldNames,Fields) then
begin
activeIndex:= fieldTitle;
activeDescending:= True;
end;
end;
if (self.items.indexOf(fieldTitle + ' - Desc')<0) then
begin
if (not SkipReload) then self.items.add(fieldTitle + ' - Desc')
end
end
end
else begin
if (fieldTitle<>'') then
begin
if GetIndexFieldNames = '' then begin
if wwEqualStr(IndexDefs.Items[i].name, GetIndexName) then
activeIndex:= fieldTitle;
end
else begin
if wwEqualStr(GetIndexFieldNames, Fields) then
activeIndex:= fieldTitle;
end;
if (self.items.indexOf(fieldTitle)<0) then
begin
if (not SkipReload) then self.items.add(fieldTitle)
end
end
end
end
end
end;
{6/16/97 - Support descending index type }
TempItemIndex:= -1;
for i:= 0 to Items.count-1 do
begin
if wwEqualStr(Items[i], activeIndex) then
begin
{ 8/17/99 - Fix bug in confusing which index is the active index }
if not ActiveDescending then TempItemIndex:= i
end
else if wwEqualStr(Items[i], activeIndex + ' - Desc') then
begin
if ActiveDescending then TempItemIndex:= i
end
end;
if ItemIndex<>TempItemIndex then
ItemIndex:= TempItemIndex;
parts.free;
end;
procedure TwwKeyCombo.Change;
var i: integer;
found: boolean;
IndexTitle: String;
parts: TStrings;
dataset: TDataset;
PropInfo: PPropInfo;
Indexdefs: TIndexDefs;
function GetIndexName: string;
begin
Result:= '';
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexName');
if PropInfo<>Nil then Result:= GetStrProp(DataSource.DataSet, PropInfo);
end;
procedure SetIndexName(val: string);
begin
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexName');
if PropInfo<>Nil then
SetStrProp(Datasource.dataset,PropInfo, val);
end;
procedure SetIndexFieldNames(val: string);
begin
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexFieldNames');
if PropInfo<>Nil then
SetStrProp(Datasource.dataset,PropInfo, val);
end;
function GetIndexFieldNames: string;
begin
Result:= '';
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexFieldNames');
if PropInfo<>Nil then Result:= GetStrProp(DataSource.DataSet, PropInfo);
end;
Function useThisIndex: boolean;
begin
result:= False;
with IndexDefs do begin
if (ixDescending in Items[i].Options) then begin
if (IndexTitle =
strReplaceChar(Dataset.fieldByName(Parts[0]).displayLabel + ' - Desc','~', ' ')) then
begin
{$ifdef wwDelphi3Up}
if Dataset is TBDEDataSet then
wwTableChangeIndex(Dataset, Items[i])
else SetIndexName(Items[i].name);
{$else}
wwTableChangeIndex(Dataset, Items[i]);
{$endif}
result:= True;
exit;
end
end
else begin
if (IndexTitle = strReplaceChar(Dataset.fieldByName(Parts[0]).displayLabel, '~', ' ')) then
begin
{$ifdef wwDelphi3Up}
if Dataset is TBDEDataSet then
wwTableChangeIndex(Dataset, Items[i])
else SetIndexName(Items[i].name);
{$else}
wwTableChangeIndex(Dataset, Items[i]);
{$endif}
result:= True;
exit;
end
end
end
end;
function IsAlone(aFields, aField: string): Boolean;
begin
Result:= aFields = aField;
end;
begin
if dataSource=Nil then exit;
if dataSource.dataset=Nil then exit; { 2/3/2000 - Check for nil or inactive dataset }
if not dataSource.dataset.active then exit;
if dataSource.dataSet is TTable then
begin
dataset:= dataSource.dataSet;
indexDefs:= (datasource.dataset as TTable).indexdefs;
if wwIsTableQuery(Dataset) then begin
MessageDlg('TwwKeyCombo: DataSet cannot be a TwwTable using a Query.',
mtWarning, [mbok], 0);
exit;
end;
end
else if useAllFields(datasource.dataset) then
begin
if Text<>'' then
begin
Patch[0]:= True;// SkipRefresh:= True;
try
SetIndexFieldNames(wwGetFieldNameFromTitle(datasource.DataSet, Text));
finally
Patch[0]:= False;// SkipRefresh:= False;
end
end;
inherited change;
exit;
end
else begin
{$ifndef wwdelphi4up}
{$ifndef ver110}
dataset:= dataSource.dataSet; { Uncomment - 8/27/98 }
{$endif}
{$endif}
PropInfo:= Typinfo.GetPropInfo(DataSource.DataSet.ClassInfo,'IndexDefs');
if PropInfo<>Nil then IndexDefs:= TIndexDefs(GetOrdProp(DataSource.DataSet, PropInfo))
else begin
MessageDlg('TwwKeyCombo: Virtual DataSet does not have index information available.',
mtWarning, [mbok], 0);
exit;
end
end;
IndexTitle:= Text;
if ShowAllIndexes then begin
if IndexTitle=GetPrimaryName then IndexTitle:= '';
SetIndexName(indexTitle);
{ (dataSource.dataSet as TTable).indexName:= indexTitle;}
inherited change; { added 7/20/95}
exit;
end;
parts:= TStringList.Create;
{ Look for case insensitive index for this field }
{ If not found then use case sensitive index }
IndexDefs.update; { refreshes Index list }
found:= False;
for i:= 0 to IndexDefs.count-1 do begin
with IndexDefs do begin
strBreakApart(Items[i].fields, ';', parts);
if not ( wwDataSetIsValidField(Dataset, Parts[0]) and
(ixCaseInsensitive in Items[i].Options)) then continue;
if useThisIndex then begin
Found:= True;
break;
end
end
end;
// 1/21/01 - Give priority to indexes with 1 field index
if not Found then begin
for i:= 0 to IndexDefs.count-1 do begin
with IndexDefs do begin
strBreakApart(Items[i].fields, ';', parts);
if not wwDataSetIsValidField(Dataset, Parts[0]) then continue;
if IsAlone(Items[i].fields, Parts[0]) and useThisIndex then begin
Found:= True;
break;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -