OSDN Git Service

空の定型句ファイルに対して各種操作しようとするとメモリアクセスエラーが出る問題を修正(#377)。
[winbottle/winbottle.git] / bottleclient / BottleChainListFrame.pas
index 7c5cdfc..11c5179 100755 (executable)
@@ -14,6 +14,8 @@ type
     btnAdd: TButton;
     btnDown: TButton;
     btnUp: TButton;
+    btnEdit: TButton;
+    btnDuplicate: TButton;
     procedure lstRulesMeasureItem(Control: TWinControl; Index: Integer;
       var Height: Integer);
     procedure lstRulesDrawItem(Control: TWinControl; Index: Integer;
@@ -24,11 +26,12 @@ type
     procedure btnDeleteClick(Sender: TObject);
     procedure btnUpClick(Sender: TObject);
     procedure btnDownClick(Sender: TObject);
+    procedure btnDuplicateClick(Sender: TObject);
   private
-    { Private \90é\8c¾ }
-    procedure ExecuteEditRule(Rule: TBottleChainRule);
+    function ExecuteEditRule(Rule: TBottleChainRule): boolean;
+    procedure UpdateCheckBox;
+    procedure UpdateHeight;
   public
-    { Public \90é\8c¾ }
     procedure UpdateDisplay;
   end;
 
@@ -93,8 +96,10 @@ begin
   if i >= 0 then begin
     if i <= BottleChainRuleList.Count-1 then
       sel := BottleChainRuleList[i]
+    else if BottleChainRuleList.Count > 0 then
+      sel := BottleChainRuleList[BottleChainRuleList.Count-1]
     else
-      sel := BottleChainRuleList[BottleChainRuleList.Count-1];
+      sel := nil;
   end else sel := nil;
 
   lstRules.Items.BeginUpdate;
@@ -116,30 +121,53 @@ begin
 end;
 
 procedure TfrmBottleChainList.EditRule(Sender: TObject);
+var
+  Edit: TBottleChainRule;
 begin
-  if lstRules.ItemIndex <> -1 then begin
-    ExecuteEditRule(BottleChainRuleList[lstRules.ItemIndex]);
-    UpdateDisplay;
+  if lstRules.ItemIndex <> -1 then
+  begin
+    Edit := TBottleChainRule.Create(nil);
+    try
+      Edit.Assign(BottleChainRuleList[lstRules.ItemIndex]);
+      if ExecuteEditRule(Edit) then
+        BottleChainRuleList[lstRules.ItemIndex].Assign(Edit);
+    finally
+      Edit.Free;
+    end;
+    UpdateHeight;
   end;
 end;
 
 procedure TfrmBottleChainList.btnAddClick(Sender: TObject);
-var New: TBottleChainRule;
+var
+  New: TBottleChainRule;
 begin
   New := TBottleChainRule.Create(BottleChainRuleList);
-  New.RuleList := BottleChainRuleList; // \8e©\95ª\82ð\83\8a\83X\83g\82É\92Ç\89Á\82·\82é
-  ExecuteEditRule(New);
-  UpdateDisplay;
+  try
+    New.Enabled := true;
+    if ExecuteEditRule(New) then
+    begin
+      New.RuleList := BottleChainRuleList; // \8e©\95ª\82ð\83\8a\83X\83g\82É\92Ç\89Á\82·\82é
+      UpdateDisplay;
+    end
+    else
+      New.Free;
+  except
+    New.Free;
+  end;
 end;
 
 procedure TfrmBottleChainList.btnDeleteClick(Sender: TObject);
-var Del: TBottleChainRule;
+var
+  Del: TBottleChainRule;
+  Index: integer;
 begin
-  if lstRules.ItemIndex < 0 then Exit;
-  Del := BottleChainRuleList[lstRules.ItemIndex];
-  BottleChainRuleList.RemoveRule(Del);
+  Index := lstRules.ItemIndex;
+  if Index < 0 then
+    Exit;
+  Del := BottleChainRuleList[Index];
   Del.Free;
-  UpdateDisplay; 
+  lstRules.Items.Delete(Index);
 end;
 
 procedure TfrmBottleChainList.btnUpClick(Sender: TObject);
@@ -148,8 +176,9 @@ begin
   i := lstRules.ItemIndex;
   if i > 0 then begin
     BottleChainRuleList.List.Move(i, i - 1);
+    lstRules.Items.Move(i, i-1);
     lstRules.ItemIndex := i - 1;
-    UpdateDisplay;
+    UpdateHeight;
   end;
 end;
 
@@ -159,22 +188,55 @@ begin
   i := lstRules.ItemIndex;
   if (i >= 0) and (i < lstRules.Items.Count-1) then begin
     BottleChainRuleList.List.Move(i, i + 1);
+    lstRules.Items.Move(i, i+1);
     lstRules.ItemIndex := i + 1;
-    UpdateDisplay;
+    UpdateHeight;
   end;
 end;
 
-procedure TfrmBottleChainList.ExecuteEditRule(Rule: TBottleChainRule);
+function TfrmBottleChainList.ExecuteEditRule(Rule: TBottleChainRule): boolean;
 var MyfrmBottleChainRuleEditor: TfrmBottleChainRuleEditor;
 begin
   Application.CreateForm(TfrmBottleChainRuleEditor, MyfrmBottleChainRuleEditor);
   with MyfrmBottleChainRuleEditor do begin
     try
-      Execute(Rule);
+      Result := Execute(Rule);
     finally
       Release;
     end;
   end;
 end;
 
+procedure TfrmBottleChainList.btnDuplicateClick(Sender: TObject);
+var
+  New: TBottleChainRule;
+begin
+  if lstRules.ItemIndex <> -1 then
+  begin
+    New := TBottleChainRule.Create(BottleChainRuleList);
+    New.Assign(BottleChainRuleList.Rules[lstRules.ItemIndex]);
+    New.RuleList := BottleChainRuleList; // \82±\82ê\82Å\83\8b\81[\83\8b\83\8a\83X\83g\82É\82Í\92Ç\89Á\82³\82ê\82é
+    lstRules.Items.Insert(lstRules.ItemIndex + 1, New.StringExpression);
+    UpdateCheckBox;
+    UpdateHeight;
+  end;
+end;
+
+procedure TfrmBottleChainList.UpdateCheckBox;
+var
+  i: integer;
+begin
+  for i := 0 to BottleChainRuleList.Count-1 do
+    lstRules.Checked[i] := BottleChainRuleList[i].Enabled;
+end;
+
+procedure TfrmBottleChainList.UpdateHeight;
+begin
+  with lstRules do
+  begin
+    Style := lbOwnerDrawFixed;
+    Style := lbOwnerDrawVariable;
+  end;
+end;
+
 end.