| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 
 | {
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
unit ColumnDlg;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, LCLProc, Forms, Controls, Graphics, Dialogs, LResources,
  ComCtrls, StdCtrls, Buttons, ExtCtrls, ObjInspStrConsts;
type
  // TODO create more generic collection editor.
  TColumnDlg = class(TForm)
    ColumnsListBox: TLISTBOX;
    CaptionLabel: TLABEL;
    CaptionEdit: TEDIT;
    WidthLabel: TLABEL;
    WidthEdit: TEDIT;
    AddButton: TBUTTON;
    DeleteButton: TBUTTON;
    AlignmentRadioGroup: TRADIOGROUP;
    MoveUpButton: TBUTTON;
    MoveDownButton: TBUTTON;
    btnOK : TBitBtn;
    btnCancel : TBitBtn;
    cbVisible : TCheckbox;
    cbAutoSize : TCheckBox;
  private
    FColumns: TListColumns;
    FSelectedIndex : Integer;
    procedure DisplayColumn(Value : Integer);
    procedure SetColumns(const AValue: TListColumns);
  protected
    procedure AddButtonOnClick(sender : TObject);
    procedure DeleteButtonOnClick(sender : TObject);
    procedure MoveUpButtonOnClick(sender : TObject);
    procedure MoveDownButtonOnClick(sender : TObject);
    procedure AlignmentRadioGroupOnClick(sender : TObject);
    procedure ColumnsListBoxOnClick(sender : TObject);
    Procedure CaptionEditOnChange(Sender : TObject);
    Procedure WidthEditOnChange(Sender : TObject);
    Procedure cbVisibleOnClick(Sender : TObject);
    Procedure cbAutoSizeOnClick(Sender : TObject);
    Procedure FormOnShow(Sender : TObject);
    procedure EnableComponents;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteDebugReport;
    property Columns: TListColumns read FColumns write SetColumns;
  end;
implementation
{ TColumnDlg }
constructor TColumnDlg.Create(TheOwner: TComponent);
Begin
  inherited Create(TheOwner);
  Caption := rscdColumnEditor;
  Width := 400;
  Height := 340;
  OnShow := @FormOnShow;
  Position := poScreenCenter;
  
  ColumnsListBox := TListBox.Create(self);
  with ColumnsListBox do
   Begin
     Parent := Self;
     Left := 1;
     Width := 170;
     Top := 1;
     Height := Self.ClientHeight-Top-40;
     OnClick := @ColumnsListBoxOnClick;
   end;
   
  CaptionLabel := TLabel.Create(self);
  with CaptionLabel do
   Begin
      Parent := self;
      Caption := rscdCaption;
      Left := ColumnsListBox.Left+ColumnsListBox.Width+10;
      Top := 15;
   end;
  CaptionEdit := TEdit.Create(self);
  with CaptionEdit do
   Begin
     Parent := Self;
     Text := '';
     Left := CaptionLabel.Left;
     Height := 25;
     Top := CaptionLabel.Top+CaptionLabel.Height+5;
     OnChange := @CaptionEditOnChange;
   end;
  WidthLabel := TLabel.Create(self);
  with WidthLabel do
   Begin
     Parent := self;
     Caption := rscdWidth;
     Left := CaptionLabel.Left;
     Top := CaptionEdit.Top+CaptionEdit.Height+5;
   end;
  WidthEdit := TEdit.Create(self);
  with WidthEdit do
   Begin
     Parent := Self;
     Text := '';
     Left := WidthLabel.Left;
     Height := 25;
     Top := WidthLabel.Top+WidthLabel.Height+5;
     OnChange := @WidthEditOnChange;
   end;
  AlignmentRadioGroup := TRadioGroup.Create(self);
  with AlignmentRadioGroup do
   Begin
     Parent := Self;
     Caption := rscdAlignment;
     Left := CaptionLabel.Left;
     Top := WidthEdit.Top+WidthEdit.Height+5;
     Columns := 3;
     Height := 50;
     Width := 200;
     Items.Add(rscdLeft);
     Items.Add(sccsILEdtCenter);
     Items.Add(rscdRight);
     ItemIndex := 0;
     OnClick := @AlignmentRadioGroupOnClick;
   end;
  cbVisible := TCheckBox.Create(self);
  with cbVisible do
   begin
     Parent := Self;
     Caption := rscdVisible;
     Left := CaptionLabel.Left;
     Top :=  AlignmentRadioGroup.Top+AlignmentRadioGroup.Height+5;
     Height := 25;
     Checked := True;
     OnClick := @cbVisibleOnClick;
   end;
  cbAutoSize := TCheckBox.Create(self);
  with cbAutoSize do
   begin
     Parent := Self;
     Caption := rscdAutoSize;
     Left := CaptionLabel.Left;
     Top :=  cbVisible.Top + cbVisible.Height + 5;
     Height := 25;
     Checked := True;
     OnClick := @cbAutoSizeOnClick;
   end;
  AddButton := TButton.Create(self);
  with AddButton do
   Begin
     Parent := self;
     Caption := oiColEditAdd;
     Left := CaptionLabel.Left;
     Top := cbAutoSize.Top+cbAutoSize.Height+5;
     OnClick := @AddButtonOnClick;
   end;
  DeleteButton := TButton.Create(self);
  with DeleteButton do
   Begin
     Parent := self;
     Caption := oisDelete;
     Left := AddButton.Left+AddButton.Width+5;
     Top := AddButton.Top;
     OnClick := @DeleteButtonOnClick;
   end;
  MoveUpButton := TButton.Create(self);
  with MoveUpButton do
   Begin
     Parent := self;
     Caption := rscdMoveUp;
     Left := 5;
     Top := ColumnsListBox.Top+ColumnsListBox.Height+5;
     OnClick := @MoveUpButtonOnClick;
   end;
  MoveDownButton := TButton.Create(self);
  with MoveDownButton do
   Begin
     Parent := self;
     Caption := rscdMoveDown;
     Left := MoveUpButton.Left+MoveUpButton.Width+5;
     Top := MoveUpButton.Top;
     OnClick := @MoveDownButtonOnClick;
   end;
  btnOK := TBitbtn.Create(self);
  with btnOK do
   Begin
     Parent := self;
     Caption := rscdOK;
     Left := CaptionLabel.Left;
     Top := MoveUpButton.Top;
     kind := bkOK;
   end;
  btnCancel := TBitbtn.Create(self);
  with btnCancel do
   Begin
     Parent := self;
     Caption := oiStdActDataSetCancel1Hint;
     Left := btnOK.Left + btnOK.Width + 5;
     Top := btnOK.Top;
     Kind := bkCancel;
   end;
   
  FColumns := TListColumns.Create(nil);
  FSelectedIndex:= -1;
end;
destructor TColumnDlg.Destroy;
begin
  FreeAndNil(FColumns);
  inherited Destroy;
end;
procedure TColumnDlg.AddButtonOnClick(sender : TObject);
var
  Column : TListColumn;
Begin
  //add
  Column := FColumns.Add;
  Column.Caption := rscdCaption;
  FSelectedIndex := Column.Index;
  ColumnsListBox.Items.Add(Column.Caption);
  ColumnsListBox.Selected[FSelectedIndex] := True;
  DisplayColumn(FSelectedIndex);
  //WriteDebugReport;
end;
procedure TColumnDlg.ColumnsListBoxOnClick(sender : TObject);
var
  I : Integer;
begin
  CaptionEdit.ReadOnly := True;
  FSelectedIndex := -1;
  if ColumnsListBox.SelCount = 0 then Exit;
  CaptionEdit.ReadOnly := False;
  I := 0;
  While not ColumnsListBox.Selected[i] do
    inc(i);
  DisplayColumn(I);
end;
Procedure TColumnDlg.CaptionEditOnChange(Sender : TObject);
Var
  ListColumn : TListColumn;
begin
  if FSelectedIndex = -1 then Exit;
  ListColumn := FColumns[FSelectedIndex];
  ListColumn.Caption := CaptionEdit.Caption;
  ColumnsListBox.Items[FSelectedIndex] := CaptionEdit.Caption;
  ColumnsListBox.Selected[FSelectedIndex] := True;
end;
Procedure TColumnDlg.WidthEditOnChange(Sender : TObject);
Var
  ListColumn : TListColumn;
begin
  if FSelectedIndex = -1 then Exit;
  ListColumn := FColumns[FSelectedIndex];
  if WidthEdit.Caption = '' then
    ListColumn.Width := 0
  else
    try
      ListColumn.Width := StrToInt(WidthEdit.Caption);
    except
      showmessage(rscdInvalidNumericValue);
      // revert to previous value
      WidthEdit.Caption := IntToStr(ListColumn.Width);
    end;
end;
procedure TColumnDlg.DeleteButtonOnClick(sender : TObject);
var
  Index : Integer;
begin
  //delete
  if FSelectedIndex = -1 then Exit;
  Index := FSelectedIndex;
  FSelectedIndex := -1;
  FColumns[Index].Free;
  ColumnsListBox.Items.Delete(Index);
  if Index > 0 then
  ColumnsListBox.Selected[Index-1] := True;
  DisplayColumn(Index-1);
end;
procedure TColumnDlg.MoveUpButtonOnClick(sender : TObject);
Var
  ListColumn : TListColumn;
  Index : Integer;
begin
  //move up
  if FSelectedIndex <= 0 then Exit;
  Index := FSelectedIndex;
  FSelectedIndex := -1;
  ListColumn := FColumns[Index];
  ListColumn.Index := Index - 1;
  
  ColumnsListBox.Items.Insert(Index-1,ListColumn.Caption);
  ColumnsListBox.Items.Delete(Index+1);
  ColumnsListBox.Selected[Index-1] := True;
  DisplayColumn(Index-1);
end;
procedure TColumnDlg.MoveDownButtonOnClick(sender : TObject);
Var
  ListColumn : TListColumn;
  Index : Integer;
begin
  //move down
  if FSelectedIndex = -1 then Exit;
  if (FSelectedIndex >= ColumnsListBox.Items.Count-1) then Exit;
  Index := FSelectedIndex;
  FSelectedIndex := -1;
  ListColumn := FColumns[Index];
  ListColumn.Index := Index + 1;
  ColumnsListBox.Items.Insert(Index+2,ListColumn.Caption);
  ColumnsListBox.Items.Delete(Index);
  ColumnsListBox.Selected[Index+1] := True;
  DisplayColumn(Index+1);
end;
Procedure TColumnDlg.DisplayColumn(Value : Integer);
Var
  ListColumn : TListColumn;
begin
  FSelectedIndex := -1;
  if Value >=0 then begin
    ListColumn := FColumns[Value];
    CaptionEdit.Caption := ListColumn.Caption;
    WidthEdit.Caption := IntToStr(Integer(ListColumn.Width));
    case ListColumn.Alignment of
      taLeftJustify :  AlignmentRadioGroup.ItemIndex := 0;
      taCenter:        AlignmentRadioGroup.ItemIndex := 1;
      taRightJustify : AlignmentRadioGroup.ItemIndex := 2;
    end;  //case
    cbVisible.Checked := ListColumn.Visible;
    cbAutoSize.Checked := ListColumn.AutoSize;
  end;
  FSelectedIndex := Value;
  EnableComponents;
end;
procedure TColumnDlg.SetColumns(const AValue: TListColumns);
begin
  FColumns.Assign(AValue);
end;
procedure TColumnDlg.AlignmentRadioGroupOnClick(sender : TObject);
Var
  ListColumn : TListColumn;
begin
  if FSelectedIndex = -1 then Exit;
  ListColumn := FColumns[FSelectedIndex];
  case  AlignmentRadioGroup.ItemIndex of
     0 : ListColumn.Alignment := taLeftJustify;
     1 : ListColumn.Alignment := taCenter;
     2 : ListColumn.Alignment := taRightJustify;
  end;
end;
Procedure TColumnDlg.FormOnShow(Sender : TObject);
var
  I : Integer;
begin
  //clear the listbox and display the items if any...
  ColumnsListBox.Items.Clear;
  for I := 0 to FColumns.Count-1 do begin
    DebugLn('TColumnDlg.FormOnShow ',IntToStr(i),' "',FColumns[i].Caption,'"');
    ColumnsListBox.Items.Add(FColumns[i].Caption);
  end;
  if ColumnsListBox.Items.Count > 0 then
  begin
    ColumnsListBox.Selected[0] := True;
    DisplayColumn(0);
  end;
  EnableComponents;
end;
procedure TColumnDlg.EnableComponents;
var
  AColumnIsSelected: boolean;
begin
  AColumnIsSelected:=FSelectedIndex>=0;
  CaptionEdit.Enabled:=AColumnIsSelected;
  WidthEdit.Enabled:=AColumnIsSelected;
  AlignmentRadioGroup.Enabled:=AColumnIsSelected;
end;
procedure TColumnDlg.WriteDebugReport;
var
  I: Integer;
begin
  DebugLn('TColumnDlg.WriteDebugReport: ');
  for I := 0 to ColumnsListBox.Items.Count-1 do begin
    DebugLn('ListBox: ',IntToStr(i),' "',ColumnsListBox.Items[I],'"');
  end;
  for I := 0 to FColumns.Count-1 do begin
    DebugLn('Columns: ',IntToStr(i),' "',FColumns[i].Caption,'" ');
  end;
end;
procedure TColumnDlg.cbVisibleOnClick(Sender : TObject);
begin
  if FSelectedIndex = -1 then Exit;
  FColumns[FSelectedIndex].Visible := cbVisible.Checked;
end;
procedure TColumnDlg.cbAutoSizeOnClick(Sender : TObject);
begin
  if FSelectedIndex = -1 then Exit;
  FColumns[FSelectedIndex].AutoSize := cbAutoSize.Checked;
end;
end.
 |