PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/ide/mouseactiondialog.pas

http://github.com/graemeg/lazarus
Pascal | 344 lines | 302 code | 36 blank | 6 comment | 38 complexity | 26c6b1f145b8f4bc2337674a0f93a5e7 MD5 | raw file
  1unit MouseActionDialog;
  2
  3{$mode objfpc}{$H+}
  4
  5interface
  6
  7uses
  8  Classes, Forms, Controls, ExtCtrls, StdCtrls, ButtonPanel, Spin, CheckLst,
  9  SynEditMouseCmds, LazarusIDEStrConsts, KeyMapping, IDECommands, types;
 10
 11var
 12  ButtonName: Array [TSynMouseButton] of String;
 13  ClickName: Array [TSynMAClickCount] of String;
 14  ButtonDirName: Array [TSynMAClickDir] of String;
 15
 16type
 17
 18  { TMouseaActionDialog }
 19
 20  TMouseaActionDialog = class(TForm)
 21    ActionBox: TComboBox;
 22    ActionLabel: TLabel;
 23    AltCheck: TCheckBox;
 24    BtnDefault: TButton;
 25    BtnLabel: TLabel;
 26    ButtonBox: TComboBox;
 27    ButtonPanel1: TButtonPanel;
 28    CaretCheck: TCheckBox;
 29    chkUpRestrict: TCheckListBox;
 30    ClickBox: TComboBox;
 31    DirCheck: TCheckBox;
 32    PaintBox1: TPaintBox;
 33    PriorLabel: TLabel;
 34    OptBox: TComboBox;
 35    CtrlCheck: TCheckBox;
 36    CapturePanel: TPanel;
 37    OptLabel: TLabel;
 38    Opt2Spin: TSpinEdit;
 39    Opt2Label: TLabel;
 40    ShiftCheck: TCheckBox;
 41    PriorSpin: TSpinEdit;
 42    procedure ActionBoxChange(Sender: TObject);
 43    procedure BtnDefaultClick(Sender: TObject);
 44    procedure ButtonBoxChange(Sender: TObject);
 45    procedure CapturePanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
 46      {%H-}X, {%H-}Y: Integer);
 47    procedure DirCheckChange(Sender: TObject);
 48    procedure FormCreate(Sender: TObject);
 49    procedure PaintBox1MouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer;
 50      {%H-}MousePos: TPoint; var {%H-}Handled: Boolean);
 51  private
 52    FKeyMap: TKeyCommandRelationList;
 53    procedure AddMouseCmd(const S: string);
 54    procedure FillListbox;
 55  public
 56    { public declarations }
 57    Procedure ResetInputs;
 58    Procedure ReadFromAction(MAct: TSynEditMouseAction);
 59    Procedure WriteToAction(MAct: TSynEditMouseAction);
 60    property KeyMap: TKeyCommandRelationList read FKeyMap write FKeyMap;
 61  end;
 62
 63  function KeyMapIndexOfCommand(AKeyMap: TKeyCommandRelationList; ACmd: Word) : Integer;
 64
 65implementation
 66
 67uses Math;
 68
 69{$R *.lfm}
 70
 71const
 72  BtnToIndex: array [TSynMouseButton] of Integer = (0, 1, 2, 3, 4, 5, 6);
 73  ClickToIndex: array [ccSingle..ccAny] of Integer = (0, 1, 2, 3, 4);
 74  IndexToBtn: array [0..6] of TSynMouseButton = (mbXLeft, mbXRight, mbXMiddle, mbXExtra1, mbXExtra2, mbXWheelUp, mbXWheelDown);
 75  IndexToClick: array [0..4] of TSynMAClickCount = (ccSingle, ccDouble, ccTriple, ccQuad, ccAny);
 76
 77function KeyMapIndexOfCommand(AKeyMap: TKeyCommandRelationList; ACmd: Word): Integer;
 78var
 79  i: Integer;
 80begin
 81  for i := 0 to AKeyMap.RelationCount - 1 do
 82    if AKeyMap.Relations[i].Command = ACmd then
 83      exit(i);
 84  Result := -1;
 85end;
 86
 87{ MouseaActionDialog }
 88
 89procedure TMouseaActionDialog.AddMouseCmd(const S: string);
 90var
 91  i: Integer;
 92  s2: String;
 93begin
 94  i:=0;
 95  if IdentToSynMouseCmd(S, i) then begin
 96    s2 := MouseCommandName(i);
 97    if s2 = '' then s2 := s;
 98    ActionBox.Items.AddObject(s2, TObject(ptrint(i)));
 99  end;
100end;
101
102procedure TMouseaActionDialog.FillListbox;
103const
104  cCheckSize=35;
105var
106  r: TSynMAUpRestriction;
107  s: string;
108  i, Len: integer;
109begin
110  for r := low(TSynMAUpRestriction) to high(TSynMAUpRestriction) do
111    case r of
112      crLastDownPos:          chkUpRestrict.AddItem(synfMatchActionPosOfMouseDown, nil);
113      crLastDownPosSameLine:  chkUpRestrict.AddItem(synfMatchActionLineOfMouseDown, nil);
114      crLastDownPosSearchAll: chkUpRestrict.AddItem(synfSearchAllActionOfMouseDown, nil);
115      crLastDownButton:       chkUpRestrict.AddItem(synfMatchActionButtonOfMouseDown, nil);
116      crLastDownShift:        chkUpRestrict.AddItem(synfMatchActionModifiersOfMouseDown, nil);
117      crAllowFallback:        chkUpRestrict.AddItem(synfContinueWithNextMouseUpAction, nil);
118      else begin
119        WriteStr(s, r);
120        chkUpRestrict.AddItem(s, nil);
121      end;
122    end;
123
124  // update scrollbar
125  Len := 0;
126  with chkUpRestrict do
127  begin
128    for i := 0 to Items.Count-1 do
129      Len := Max(Len, Canvas.TextWidth(Items[i])+cCheckSize);
130    ScrollWidth := Len;
131  end;
132end;
133
134procedure TMouseaActionDialog.FormCreate(Sender: TObject);
135var
136  mb: TSynMouseButton;
137  cc: TSynMAClickCount;
138begin
139  ButtonName[mbXLeft]:=dlgMouseOptBtnLeft;
140  ButtonName[mbXRight]:=dlgMouseOptBtnRight;
141  ButtonName[mbXMiddle]:=dlgMouseOptBtnMiddle;
142  ButtonName[mbXExtra1]:=dlgMouseOptBtnExtra1;
143  ButtonName[mbXExtra2]:=dlgMouseOptBtnExtra2;
144  ButtonName[mbXWheelUp]:=dlgMouseOptBtnWheelUp;
145  ButtonName[mbXWheelDown]:=dlgMouseOptBtnWheelDown;
146
147  ClickName[ccSingle]:=dlgMouseOptBtn1;
148  ClickName[ccDouble]:=dlgMouseOptBtn2;
149  ClickName[ccTriple]:=dlgMouseOptBtn3;
150  ClickName[ccQuad]:=dlgMouseOptBtn4;
151  ClickName[ccAny]:=dlgMouseOptBtnAny;
152
153  FillListbox;
154
155  ButtonDirName[cdUp]:=lisUp;
156  ButtonDirName[cdDown]:=lisDown;
157
158  Caption := dlgMouseOptDlgTitle;
159  CapturePanel.Caption := dlgMouseOptCapture;
160  CapturePanel.ControlStyle := ControlStyle + [csTripleClicks, csQuadClicks];
161  CaretCheck.Caption := dlgMouseOptCaretMove;
162  ActionBox.Clear;
163  GetEditorMouseCommandValues(@AddMouseCmd);
164  ButtonBox.Clear;
165  for mb := low(TSynMouseButton) to high(TSynMouseButton) do
166    ButtonBox.Items.add(ButtonName[mb]);
167  ClickBox.Clear;
168  for cc:= low(TSynMAClickCount) to high(TSynMAClickCount) do
169    ClickBox.Items.add(ClickName[cc]);
170  DirCheck.Caption   := dlgMouseOptCheckUpDown;
171  ShiftCheck.Caption := dlgMouseOptModShift;
172  AltCheck.Caption   := dlgMouseOptModAlt;
173  CtrlCheck.Caption  := dlgMouseOptModCtrl;
174  ActionLabel.Caption := dlgMouseOptDescAction;
175  BtnLabel.Caption := dlgMouseOptDescButton;
176  BtnDefault.Caption :=  dlgMouseOptBtnModDef;
177  PriorLabel.Caption := dlgMouseOptPriorLabel;
178  Opt2Label.Caption := dlgMouseOptOpt2Label;
179end;
180
181procedure TMouseaActionDialog.ResetInputs;
182var
183  r: TSynMAUpRestriction;
184begin
185  ActionBox.ItemIndex := 0;
186  ButtonBox.ItemIndex := 0;
187  ClickBox.ItemIndex  := 0;
188  DirCheck.Checked    := False;
189  ShiftCheck.State := cbGrayed;
190  AltCheck.State := cbGrayed;
191  CtrlCheck.State := cbGrayed;
192  for r := low(TSynMAUpRestriction) to high(TSynMAUpRestriction) do
193    chkUpRestrict.Checked[ord(r)] := False;
194
195  ActionBoxChange(nil);
196  OptBox.ItemIndex := 0;
197end;
198
199procedure TMouseaActionDialog.BtnDefaultClick(Sender: TObject);
200begin
201  ShiftCheck.State := cbGrayed;
202  AltCheck.State := cbGrayed;
203  CtrlCheck.State := cbGrayed;
204end;
205
206procedure TMouseaActionDialog.ButtonBoxChange(Sender: TObject);
207begin
208  DirCheck.Enabled := not(IndexToBtn[ButtonBox.ItemIndex] in [mbXWheelUp, mbXWheelDown]);
209  chkUpRestrict.Enabled := DirCheck.Enabled and DirCheck.Checked;
210end;
211
212procedure TMouseaActionDialog.ActionBoxChange(Sender: TObject);
213var
214  ACmd: TSynEditorMouseCommand;
215  i: Integer;
216begin
217  OptBox.Items.Clear;
218  ACmd := TSynEditorMouseCommand({%H-}PtrUInt(Pointer(ActionBox.items.Objects[ActionBox.ItemIndex])));
219  if ACmd =  emcSynEditCommand then begin
220    OptBox.Enabled := True;
221    OptBox.Clear;
222    for i := 0 to KeyMap.RelationCount - 1 do
223      if (KeyMap.Relations[i].Category.Scope = IDECmdScopeSrcEdit) or
224         (KeyMap.Relations[i].Category.Scope = IDECmdScopeSrcEditOnly)
225      then
226        OptBox.Items.AddObject(KeyMap.Relations[i].GetLocalizedName,
227                               TObject({%H-}Pointer(PtrUInt(KeyMap.Relations[i].Command))));
228    OptLabel.Caption := dlgMouseOptionsynCommand;
229    OptBox.ItemIndex := 0;
230  end
231  else
232  begin
233    OptBox.Items.CommaText := MouseCommandConfigName(ACmd);
234    if OptBox.Items.Count > 0 then begin
235      OptLabel.Caption := OptBox.Items[0];
236      OptBox.Items.Delete(0);
237      OptBox.Enabled := True;
238      OptBox.ItemIndex := 0;
239    end else begin
240      OptLabel.Caption := '';
241      OptBox.Enabled := False
242    end;
243  end;
244end;
245
246procedure TMouseaActionDialog.CapturePanelMouseDown(Sender: TObject;
247  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
248begin
249  ButtonBox.ItemIndex := BtnToIndex[SynMouseButtonMap[Button]];
250  ClickBox.ItemIndex := 0;
251  if ssDouble in Shift then ClickBox.ItemIndex := 1;
252  if ssTriple in Shift then ClickBox.ItemIndex := 2;
253  if ssQuad in Shift then ClickBox.ItemIndex := 3;
254  ShiftCheck.Checked := ssShift in Shift;
255  AltCheck.Checked   := ssAlt in Shift;
256  CtrlCheck.Checked  := ssCtrl in Shift;
257end;
258
259procedure TMouseaActionDialog.DirCheckChange(Sender: TObject);
260begin
261  chkUpRestrict.Enabled := DirCheck.Checked;
262end;
263
264procedure TMouseaActionDialog.PaintBox1MouseWheel(Sender: TObject; Shift: TShiftState;
265  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
266begin
267  if WheelDelta > 0
268  then ButtonBox.ItemIndex := BtnToIndex[mbXWheelUp]
269  else ButtonBox.ItemIndex := BtnToIndex[mbXWheelDown];
270  ClickBox.ItemIndex := 4;
271  ShiftCheck.Checked := ssShift in Shift;
272  AltCheck.Checked   := ssAlt in Shift;
273  CtrlCheck.Checked  := ssCtrl in Shift;
274end;
275
276procedure TMouseaActionDialog.ReadFromAction(MAct: TSynEditMouseAction);
277var
278  r: TSynMAUpRestriction;
279begin
280  ActionBox.ItemIndex := ActionBox.Items.IndexOfObject(TObject({%H-}Pointer(PtrUInt(MAct.Command))));
281  ButtonBox.ItemIndex := BtnToIndex[MAct.Button];
282  ClickBox.ItemIndex  := ClickToIndex[MAct.ClickCount];
283  DirCheck.Checked    := MAct.ClickDir = cdUp;
284  CaretCheck.Checked := MAct.MoveCaret;
285  ShiftCheck.Checked := (ssShift in MAct.ShiftMask) and (ssShift in MAct.Shift);
286  if not(ssShift in MAct.ShiftMask) then ShiftCheck.State := cbGrayed;
287  AltCheck.Checked := (ssAlt in MAct.ShiftMask) and (ssAlt in MAct.Shift);
288  if not(ssAlt in MAct.ShiftMask) then AltCheck.State := cbGrayed;
289  CtrlCheck.Checked := (ssCtrl in MAct.ShiftMask) and (ssCtrl in MAct.Shift);
290  if not(ssCtrl in MAct.ShiftMask) then CtrlCheck.State := cbGrayed;
291  PriorSpin.Value := MAct.Priority;
292  Opt2Spin.Value := MAct.Option2;
293  for r := low(TSynMAUpRestriction) to high(TSynMAUpRestriction) do
294    chkUpRestrict.Checked[ord(r)] := r in MAct.ButtonUpRestrictions;
295
296  ActionBoxChange(nil);
297  ButtonBoxChange(nil);
298  if OptBox.Enabled then begin
299    if MAct.Command =  emcSynEditCommand then
300      OptBox.ItemIndex := OptBox.Items.IndexOfObject(TObject({%H-}Pointer(PtrUInt(MAct.Option))))
301    else
302      OptBox.ItemIndex := MAct.Option;
303  end;
304end;
305
306procedure TMouseaActionDialog.WriteToAction(MAct: TSynEditMouseAction);
307var
308  r: TSynMAUpRestriction;
309begin
310  MAct.Command := TSynEditorMouseCommand({%H-}PtrUInt(Pointer(ActionBox.items.Objects[ActionBox.ItemIndex])));
311  MAct.Button := IndexToBtn[ButtonBox.ItemIndex];
312  MAct.ClickCount := IndexToClick[ClickBox.ItemIndex];
313  MAct.MoveCaret := CaretCheck.Checked;
314  if DirCheck.Checked
315  then MAct.ClickDir := cdUp
316  else MAct.ClickDir := cdDown;
317  MAct.Shift := [];
318  MAct.ShiftMask := [];
319  if ShiftCheck.State <> cbGrayed then MAct.ShiftMask := MAct.ShiftMask + [ssShift];
320  if AltCheck.State <> cbGrayed then MAct.ShiftMask := MAct.ShiftMask + [ssAlt];
321  if CtrlCheck.State <> cbGrayed then MAct.ShiftMask := MAct.ShiftMask + [ssCtrl];
322  if ShiftCheck.Checked then MAct.Shift := MAct.Shift + [ssShift];
323  if AltCheck.Checked then MAct.Shift := MAct.Shift + [ssAlt];
324  if CtrlCheck.Checked then MAct.Shift := MAct.Shift + [ssCtrl];
325  MAct.Priority := PriorSpin.Value;
326  MAct.Option2 := Opt2Spin.Value;
327  MAct.ButtonUpRestrictions := [];
328  for r := low(TSynMAUpRestriction) to high(TSynMAUpRestriction) do
329    if chkUpRestrict.Checked[ord(r)] then
330      MAct.ButtonUpRestrictions := MAct.ButtonUpRestrictions + [r];
331
332  if OptBox.Enabled then begin
333    if MAct.Command =  emcSynEditCommand then begin
334      MAct.Option := TSynEditorMouseCommandOpt({%H-}PtrUInt(Pointer(OptBox.Items.Objects[OptBox.ItemIndex])));
335    end
336    else
337      MAct.Option := OptBox.ItemIndex;
338  end
339  else
340    MAct.Option := 0;
341end;
342
343end.
344