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
|
char Spec_A[] = {192, 193, 194, 195, 196, 197, 198,0};
char Spec_a[] = {'a', 225, 226, 227, 228, 229, 230,0};
__fastcall jMemo::jMemo(TComponent *AOwner)
: TMemo(AOwner)
{
StatusBar = new TStatusBar(this);
StatusBar->Parent = this;
StatusBar->SimplePanel = true;
StatusBar->SimpleText = "y = 0, x = 0";
ListBox = new TListBox(this);
ListBox->Parent = this;
ListBox->Visible = false;
ListBox->Font->Size = 18;
ListBox->OnClick = HisClickItem;
if(AOwner->InheritsFrom(__classid(TWinControl)))
{
Parent = (TWinControl*)AOwner;
StatusBar->Parent = (TWinControl*)AOwner;
ListBox->Parent = (TWinControl*)AOwner;
}
OnKeyUp = HisKeyUp;
OnKeyPress = HisKeyPress;
}
__fastcall jMemo::~jMemo()
{
}
void __fastcall jMemo::HisKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
}
void __fastcall jMemo::HisKeyPress(TObject *Sender, char &Key)
{
//L'astuce consiste à mémoriser la touche ici
//pour faire la différence entre maj/min
key = Key;
}
void __fastcall jMemo::HisKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
//On va utiliser la propriété key et non le paramère Key
x = CaretPos.x;
y = CaretPos.y;
StatusBar->SimpleText = "y = " + IntToStr(y) +
", x = " + IntToStr(x);
AnsiString N = Lines->Strings[y];
if(key != '&')
{
if(x > 1)
{
if(N[x-1] == '&')
{
switch(key)
{
case 'A' :
ShowSpec(Spec_A);
break;
case 'a' :
ShowSpec(Spec_a);
break;
}
}
}
}
}
//Fonction simple qui va retourner les coordonnées en pixels du caret
// via une API qu'il ne faut pas confondre avec TCustomMemo::GetCaretPos
// donc c'est pour cette raison que cette fonction n'est pas membre de jMemo
TPoint GiveCaretPos()
{
TPoint P;
GetCaretPos(&P);
return P;
}
void __fastcall jMemo::ShowSpec(char *T)
{
Spec = T;
ListBox->Items->Clear();
int n = 0;
while(T[n] != 0)
{
ListBox->Items->Add((AnsiString)T[n]);
n++;
}
ListBox->BringToFront();
// A améliorer... sans doute
TPoint P = GiveCaretPos(); // << appel à la fonction
ListBox->SetBounds(Left + P.x,
Top + P.y,
ListBox->Width,
ListBox->Height);
ListBox->Visible = true;
}
void __fastcall jMemo::HisClickItem(TObject *Object)
{
ListBox->Visible = false;
AnsiString N = Lines->Strings[y];
char c = Spec[ListBox->ItemIndex];
AnsiString A;
AnsiString B;
if((x - 2) != 0) A = N.SubString(1, x-2);
if(x < N.Length()) B = N.SubString(x, N.Length());
Lines->Strings[y] = A + (AnsiString)c + B;
SetFocus();
} |
Partager