1 2 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
|
unit TranslationStringGrid;
interface
uses
Menus, Windows, Graphics, Messages, SysUtils, Classes, Controls, Grids;
type
TTranslationStringGrid = class(TStringGrid)
private
APopupMenu:TPopupMenu;
StandardFont:TFont;
fTranslationColFont: TFont;
fOnMouseUp: TMouseEvent;
fOnDrawCell: TDrawCellEvent;
fOnSetEditText: TSetEditEvent;
procedure DoSortData(AColIndex:Integer);
procedure SetColCount(AColCount:Integer);
procedure SetOptions(ANewOptions:TGridOptions);
procedure DoMouseUp(Sender:TObject; Button:TMouseButton;Shift:TShiftState;
X,Y:Integer);
procedure DoPaint(Sender:TObject; ACol,ARow:Longint; Rect:TRect;
State:TGridDrawState);
procedure DoSetEditText(Sender:TObject; ACol,ARow:Longint;
const Value:String);
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
procedure Load(FileName:String);
procedure Save(FileName:String);
published
property TranslationColFont:TFont read fTranslationColFont
write fTranslationColFont;
{Overrided properties}
property ColCount write SetColCount;
property Options write SetOptions;
{Inherited overrided events}
property OnMouseUp:TMouseEvent read fOnMouseUp write fOnMouseUp;
property OnDrawCell:TDrawCellEvent read fOnDrawCell write fOnDrawCell;
property OnSetEditText:TSetEditEvent read fOnSetEditText write fOnSetEditText;
end;
procedure Register;
implementation
uses
Dialogs, UnitExceptions, UnitTools;
const
{Ne pas définir 2 valeurs identiques pour ces constantes !}
ID_COLINDEX = 0;
CLASSNAME_COLINDEX = 1;
ORIGINALVAL_COLINDEX = 2;
TRANSLATEDVAL_COLINDEX = 3;
COMMENT_COLINDEX = 4;
TRANSLATED_STATE_COLINDEX = 5;
COLS_ARRAY:array[0..5] of Integer=(ID_COLINDEX,
CLASSNAME_COLINDEX,
ORIGINALVAL_COLINDEX,
TRANSLATEDVAL_COLINDEX,
COMMENT_COLINDEX,
TRANSLATED_STATE_COLINDEX);
procedure Inform(Msg:String); begin
ShowMessage(Msg);
end;
{ TTranslationStringGrid }
constructor TTranslationStringGrid.Create(AOwner:TComponent);
var
AMenuItem:TMenuItem;
begin
inherited;
StandardFont:=Font;
fTranslationColFont:=TFont.Create;
{Ajout du menu popup}
APopupMenu:=TPopupMenu.Create(Self);
AMenuItem:=NewItem('Sort...', TShortCut(nil), false, true, nil, 0, '');
AMenuItem.Add(NewItem('by identifier', TShortCut(nil), false, true, nil, 0, ''));
AMenuItem.Add(NewItem('by type', TShortCut(nil), false, true, nil, 0, ''));
AMenuItem.Add(NewItem('by original value', TShortCut(nil), false, true, nil, 0, ''));
AMenuItem.Add(NewItem('by translated value', TShortCut(nil), false, true, nil, 0, ''));
AMenuItem.Add(NewItem('by comment', TShortCut(nil), false, true, nil, 0, ''));
AMenuItem.Add(NewItem('by translated state', TShortCut(nil), false, true, nil, 0, ''));
APopupMenu.Items.Add(AMenuItem);
PopupMenu:=APopupMenu;
ColCount:=Succ(High(COLS_ARRAY)-Low(COLS_ARRAY));
RowCount:=50;
FixedCols:=0;
FixedRows:=1;
{Attribution des largeurs}
ColWidths[ID_COLINDEX]:=200;
ColWidths[CLASSNAME_COLINDEX]:=100;
ColWidths[ORIGINALVAL_COLINDEX]:=200;
ColWidths[TRANSLATEDVAL_COLINDEX]:=200;
ColWidths[COMMENT_COLINDEX]:=200;
ColWidths[TRANSLATED_STATE_COLINDEX]:=75;
{Lignes fines}
DefaultRowHeight:=16;
{Activation des options utiles.
Les colonnes et les lignes ne sont pas déplaçables ! (trop de problèmes
pour l'implémentation !)}
Options:=[goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
goDrawFocusSelected, goColSizing, goEditing, goTabs,
goThumbTracking];
{Gestion evénementielle}
inherited OnMouseUp:=DoMouseUp;{tri par colonne}
inherited OnDrawCell:=DoPaint;{dessin du contenu}
inherited OnSetEditText:=DoSetEditText;{modif. d'une valeur de cellule}
//inherited OnGetEditText {lorsque le composant demande le contenu des cellules, sous forme de texte !}
end;
destructor TTranslationStringGrid.Destroy; begin
APopupMenu.Free;
TranslationColFont.Free;
inherited;
end;
procedure TTranslationStringGrid.Save(FileName:String); begin
Inform('Saved ! (not implemented...)');
end;
procedure TTranslationStringGrid.Load(FileName:String); begin
Inform('Project file loaded ! (but not implemented yet...)');
end;
procedure TTranslationStringGrid.DoSortData(AColIndex: Integer); begin
Inform('Sort by colindex '+IntToStr(AColIndex)+
' ! (but not implemented yet...)');
end;
{Overrided properties implementation}
procedure TTranslationStringGrid.SetColCount(AColCount: Integer); begin
{Impossible d'assigner une autre valeur que le nombre de colonnes
disponible}
if AColCount <> Succ(High(COLS_ARRAY)-Low(COLS_ARRAY)) then
raise TIllegalArgumentException.Create(IntToStr(AColCount));
inherited ColCount:=AColCount;
end;
procedure TTranslationStringGrid.SetOptions(ANewOptions:TGridOptions); begin
{On supprime la possibilité d'activer les déplacements de lignes ou
colonnes: le reste de l'implémentation n'a pas été conçu afin que les
colonnes soient variables !}
inherited Options:=ANewOptions-[goRowMoving, goColMoving];
end;
{Events implementation}
procedure TTranslationStringGrid.DoMouseUp(Sender:TObject; Button:TMouseButton;
Shift:TShiftState; X,Y: Integer);
var
ARow, ACol:Integer;
begin
MouseToCell(X, Y, ACol, ARow);
{Clique gauche sur la ligne des titres ?}
if(ARow=0)and(Button=mbLeft)then
DoSortData(ACol);
if Assigned(fOnMouseUp) then
fOnMouseUp(Sender, Button, Shift, X, Y);
end;
procedure TTranslationStringGrid.DoPaint(Sender:TObject; ACol,ARow:Longint;
Rect:TRect; State:TGridDrawState); begin
{Ligne de titres ?}
if ARow=0 then{oui}
case ACol of
ID_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'Identifier');
CLASSNAME_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'Type');
ORIGINALVAL_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'Original value');
TRANSLATEDVAL_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'Translated value');
COMMENT_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'Comment');
TRANSLATED_STATE_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'Translated ?');
end
else{non}
case ACol of
ID_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'A leaf identifier');
CLASSNAME_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'A class identifier');
ORIGINALVAL_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'Leaf original value');
TRANSLATEDVAL_COLINDEX: begin
//Canvas.Font:=TranslationColFont; {font spéciale}
Canvas.TextOut(Rect.Left, Rect.Top, 'Leaf translated value');
//Canvas.Font:=StandardFont; {font normale}
end;
COMMENT_COLINDEX:
Canvas.TextOut(Rect.Left, Rect.Top, 'A leaf comment');
TRANSLATED_STATE_COLINDEX: begin
Canvas.Brush.Color:=clRed;
Canvas.FillRect(Rect);
end;
end;
if Assigned(fOnDrawCell) then
fOnDrawCell(Sender, ACol, ARow, Rect, State);
end;
procedure TTranslationStringGrid.DoSetEditText(Sender:TObject; ACol,ARow: Integer;
const Value:String); begin
{Les seules colonnes éditables sont celles de valeur traduite et de
commentaire !}
case ACol of
COMMENT_COLINDEX:
Inform('DoSetEditText a comment cell ! (not implemented...)');
TRANSLATEDVAL_COLINDEX:
Inform('DoSetEditText a translated cell ! (not implemented...)');
end;
if Assigned(fOnSetEditText) then
fOnSetEditText(Sender, ACol, ARow, Value);
end;
procedure Register;
begin
RegisterComponents('S.Piller', [TTranslationStringGrid]);
end;
end. |
Partager