I´m migrating a Delphi 2007 application to Delphi 10.3. One of my forms has a TJvDBRichEdit that is associated with a BLOB sub_type text Firebird 3.0/64 database. Both Delphi versions use the same JVCL version.
I later (in another module) use the contents of the field, to convert it to HMTL using a TJvRichEdit (not DB).
Var
HTML : TStringList;
JvRichEditcontrol.Text := table.BLOBField.AsString;
JvRichEditToHtml.ConvertToHtmlStrings(JvRichEditcontrol,HTML);
This runs smoothly on Delphi on 2007. Let´s say the field contains
'{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\lang2058\b\i\f0\fs16 Italic and bold\b0\i0\par
}
'
After reading back JvRichEditcontrol.Text it returns
'Italic and bold'
However, for Delphi 10.3 returns the very same text
'{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\lang2058\b\i\f0\fs16 Italic and bold\b0\i0\par
}
'
and then, ConvertToHtmlStrings returns HTML with RTF tags embedded
<P STYLE=3D"text-align: left;">
<SPAN style=3D"color: #000000; font-size: 8pt; font-family: Tahoma;">
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
<BR>\viewkind4\uc1\pard\lang2058\b\i\f0\fs16 Italic and bold\ul\par
<BR>}
</SPAN>
</P>
It should return
<P STYLE=3D"text-align: left;">
<SPAN style=3D"color: #000000; font-size: 8pt; font-family: Tahoma;">
<BR><b><i>Italic and bold</i></b>
<BR>
</SPAN>
</P>
This is what I have tried, with same result
Playing with the PlainText and StreamFormat properties
Using a TRichEdit instead of a TJvRichEdit
Pasting from the clipboard
Loading from a file
Curiously, TJvDBRichEdit works fine. Gets the text from the field and displays properly. Being a descendant of TJvRichEdit it should set the text somewhere (I have debugged a lot but so far, I´ve been unable to find how does it set it)
Am I missing something here? Does it have something to do with the Delphi 10 UnicodeString?
How do I correctly set the TJvRichEdit text property?
Maybe I could use the TJvDBRichEdit but that implies passing it as a parameter or the like since the code that uses its contents (and many others from other forms) is a TDatamodule that handles the logic, and so, it currently receives the text from the field (in both Delphi 2007 and 10.3)
Oversimplified, assign using a string stream:
Var
SS : TStringStream;
SS := TStringStream.Create(someString);
JvRichEdit.Lines.LoadFromStream(SS);
PlainText is false and StreamFormat is sfDefault
I still don´t know what is the problem. Setting the text property has the same code on both 2007 and 10.3 despite the directives, so the problem should be deep buried in the runtime.
// Delphi 2007
procedure TControl.SetText(const Value: TCaption);
begin
if GetText <> Value then SetTextBuf(PChar(Value));
end;
procedure TControl.SetTextBuf(Buffer: PChar);
begin
Perform(WM_SETTEXT, 0, Longint(Buffer));
Perform(CM_TEXTCHANGED, 0,0);
end;
// Delphi 10.3
procedure TControl.SetText(const Value: TCaption);
begin
if GetText <> Value then
{$IF DEFINED(CLR)}
begin
FText := Value;
Perform(CM_TEXTCHANGED, 0, 0);
end;
{$ELSE}
SetTextBuf(PChar(Value));
{$ENDIF}
end;
{$IF DEFINED(CLR)}
procedure TControl.SetTextBuf(Buffer: string);
{$ELSE}
procedure TControl.SetTextBuf(Buffer: PChar);
{$ENDIF}
begin
Perform(WM_SETTEXT, 0, Buffer);
Perform(CM_TEXTCHANGED, 0, 0);
end;
Related
I am trying to use the DSiGetHtmlFormatFromClipboard function from the well known DSiWin32 library.
Edit: There is a much newer version of DSIWin32.pas 1.94 from 2016-10-19 which is contained in the current version of OmniThreadLibrary_3.07.1. The one I've linked to in the first line of my question is much older: 1.66 from 2012-04-20. However, also in this newer version of DSIWin32.pas the function DSiGetHtmlFormatFromClipboard does not work although I've made sure that no other clipboard programs are running.
So I put some text on the clipboard which includes the HTML format e.g. by copying some text from Chrome web-browser.
And then I use this code to get the HTML format from the clipboard:
if DSiWin32.DSiIsHtmlFormatOnClipboard then
begin
CodeSite.Send('HTML-Format string:', DSiWin32.DSiGetHtmlFormatFromClipboard);
end;
While the DSiIsHtmlFormatOnClipboard function does work (it gives back True if there is HTML Format on the clipboard and gives back False if there is no HTML Format on the clipboard), the DSiGetHtmlFormatFromClipboard function always gives back an empty string although there is HTML Format in the clipboard:
So I debugged function DSiGetHtmlFormatFromClipboard: string; in DSiWin32.pas:
On this line:
hClipData := GetClipboardData(GCF_HTML);
hClipData is always 0, so the following code is not executed.
GetClipboardData is a function from Winapi.Windows and according to MSDN documentation:
Retrieves data from the clipboard in a specified format. The clipboard
must have been opened previously.
Which is the case in the DSiWin32 code.
So why does the DSiGetHtmlFormatFromClipboard always give back an empty string?
OS: Windows 7 x64
GetLastError retrieved immediately after the line hClipData := GetClipboardData(GCF_HTML);:
ERROR_CLIPBOARD_NOT_OPEN 1418 (0x58A) Thread does not have a
clipboard open.
This is strange because the preceding line is: Win32Check(OpenClipboard(0)); and it does not fail.
Here is the relevant parts of the MCVE:
var
GCF_HTML: UINT;
function DSiIsHtmlFormatOnClipboard: boolean;
begin
Result := IsClipboardFormatAvailable(GCF_HTML);
end; { DSiIsHtmlFormatOnClipboard }
function DSiGetHtmlFormatFromClipboard: string;
var
hClipData : THandle;
idxEndFragment : integer;
idxStartFragment: integer;
pClipData : PChar;
begin
Result := '';
if DSiIsHtmlFormatOnClipboard then
begin
Win32Check(OpenClipboard(0));
try
hClipData := GetClipboardData(GCF_HTML);
if hClipData = 0 then
RaiseLastOSError;
pClipData := GlobalLock(hClipData);
Win32Check(assigned(pClipData));
try
idxStartFragment := Pos('<!--StartFragment-->', pClipData); // len = 20
idxEndFragment := Pos('<!--EndFragment-->', pClipData);
if (idxStartFragment >= 0) and (idxEndFragment >= idxStartFragment) then
Result := Copy(pClipData, idxStartFragment + 20, idxEndFragment - idxStartFragment - 20);
finally GlobalUnlock(hClipData); end;
finally Win32Check(CloseClipboard); end;
end;
end; { DSiGetHtmlFormatFromClipboard }
procedure TForm1.Button1Click(Sender: TObject);
begin
if DSiIsHtmlFormatOnClipboard then
ShowMessage(DSiGetHtmlFormatFromClipboard)
else
ShowMessage('No HTML Format on Clipboard');
end;
initialization
GCF_HTML := RegisterClipboardFormat('HTML Format');
end.
I want to detect on which HTML tag (more exactly hyperlink) is the caret.
procedure THTMLEdit.ShowTag;
var
CursorPos: TPoint;
HtmlElement: IHTMLElement;
iHTMLDoc: IHtmlDocument2;
begin
if Supports(wbBrowser.Document, IHtmlDocument2, iHTMLDoc) then
begin
if GetcaretPos(CursorPos) then
begin
CursorPos := wbBrowser.screentoclient(CursorPos);
HtmlElement := iHTMLDoc.ElementFromPoint(CursorPos.X, CursorPos.Y); // I NEED KEYBOARD CARET HERE, NOT MOUSE CURSOR
if HtmlElement <> NIL
then label1.Caption:= HtmlElement.tagName;
end;
end;
end;
Notes:
TWebBrowser is in DesignMode ( DesignMode := 'On' ).
TWebBrowser is in its own form at design time but at runtime is re-parented in another form (in a panel).
UPDATE:
The thing that I need is IHTMLTxtRange (I think). It works when I double click a link/word. But I don't know how to get the tag under caret when no text/link is selected.
GetcaretPos(CursorPos) returns client (relative) coordinates (See GetCaretPos function)
Remove wbBrowser.screentoclient(CursorPos) and it should work fine. I have tested with your code sample above
I'm using the following code added to my DBGrid - OnCellClick event
procedure TForm2.DBGrid1CellClick(Column: TColumn);
begin
if dbmodule.comenziDataSet.Active then
begin
if not Assigned(dbgridCelulaForm) then
begin
dbgridCelulaForm := TdbgridCelulaForm.Create(Self);
dbgridCelulaForm.DBMemoCelula.DataSource := dbmodule.comenziSource;
end;
dbgridCelulaForm.Visible := False;
dbgridCelulaForm.Visible := True;
dbgridCelulaForm.DBMemoCelula.DataField := Column.FieldName;
dbgridCelulaForm.Caption := Format('%s / randul: %d',[Column.FieldName, DBGrid1.DataSource.DataSet.RecNo]);
end;
end;
dbgridCelulaForm = name of the form containing the TDBMemo
DBMemoCelula = name of the TDBMemo
dbmodule.comenziDataSet = comenziDataSet is the name of the DataSet and dbmodule is the name of a data module (unit, like forms) - the DataSet is on the data module, so, dbmodule.comenziDataSet
dbmodule.comenziSource = same as data set, a DataSource on a data module, the source is named comenziSource
Ok so what this code does:
Once I click a cell on my DBGrid it pops up a form (named dbgridCelulaForm) which contains a TBMemo (named DBMemoCelula) and it shows me the information contained in that cell (like, a Customer Name for example, or whatever the cell is holding, in my db)
This is fine, my problem is I can't select rows now in DBGrid, well, I can but once I do the 1st place I click (a cell, any) on the particular row I want to select with my mouse, then cell activates and the form pops up.
Is it possible to use this code in DBGrid - OnDblClick event instead of the OnCellClick ?
Meaning once i double click a row / cell the form should pop up and show me the info, but double click - not single click.
That way, I can still select the row and still view the info in the cell if I need to.
Or any other way/place to use/receive this functionality.
Any thoughts?
I can post a quick video of everything if my explanation is ambiguous and you think that would help, just tell me in the comment / answer.
Also, I'm using RAD Studio 10 Seattle and dbexpress components for the database - if that helps.
Thanks!
The following code shows how to access the Column and Row coordinates of a dbl-clicked cell of a TDBGrid, and the string value of the cell contents.
As written, it displays the cell's Column and Row number + string contents on the form's caption. Up to you what you actually do with these values.
It work because the dataset cursor on the dataset connected to the DBGrid is moved to the dataset row corresponding to the cell where the mouse pointer is.
type
TMyDBGrid = class(TDBGrid);
procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
ARow,
ACol : Integer;
Pt : TPoint;
CellValue : String;
begin
// First, get the mouse pointer coordinates
Pt.X := Mouse.CursorPos.X;
Pt.Y := Mouse.CursorPos.Y;
// Translate them into the coordinate system of the DBGrid
Pt := DBGrid1.ScreenToClient(Pt);
// Use TDBGrids inbuilt functionality to identify the Column and
// row number.
ACol := DBGrid1.MouseCoord(Pt.X, Pt.Y).X -1;
ARow := DBGrid1.MouseCoord(Pt.X, Pt.Y).Y;
CellValue := DBGrid1.Columns[ACol].Field.AsString;
Caption := Format('Col:%d Row:%d Cell Value:%s', [ACol, ARow, CellValue]);
end;
Note that I've used the Caption property of the form to display the grid cell info just as a quick n dirty way of showing the information somewhere. Of course you could equally well display it on another area of the form or somewhere on a different form entirely. The above code will work equally well in the grid's OnCellClick event, btw.
As noted in a comment, you can use the grid's SelectedField property instead of the above, but personally I think the above is more instructive of how to work with a DBGrid, because it shows how to get the cell's Column and Row coordinates. See the DBGrid's SelectedField, SelectedIndex and SelectedRows properties in the Online Help for more info on useful properties of the TDBGrid.
Update You asked in a comment for an example of showing the information on another form. Let's suppose this form is called OtherForm, is in a unit OtherFormu.Pas and is created before the DBGrid1DblClick evenbt is called. You need to use this unit in the Useslist of the unit which contains the DBGrid. Let's suppose this other form contains a TMemo control called Memo1. Then, you could write your DBGrid1DblClick hanndler like this:
procedure TForm1.DBGrid1DblClick(Sender: TObject);
[as above]
begin
[ as above ]
CellValue := DBGrid1.Columns[ACol].Field.AsString;
OtherForm.Memo1.Lines.Add(Format('Col:%d Row:%d Cell Value:%s', [ACol, ARow, CellValue]));
end;
I'm working on a Free Pascal-Lazarus (1.4.4) project where I need to highlight (using HTML tags) all occurrences of a string with another string. However, I only want to replace the string if it is text, i.e. I must ignore occurrences inside HTML tags.
For example:
I want to highlight all ocurrences of word grid (must be case insensitive):
<p class="Body"><span style="layout-grid-mode: line;" lang="EN-GB">Rome was the most important city in the world. Grid just for test. Grid again...</span></p>
Like This:
<p class="Body"><span style="layout-grid-mode: line;" lang="EN-GB">Rome was the most important city in the world. <span style="background-color: #FA8072 ; color: #ffffff;">Grid</span> just for test. <span style="background-color: #FA8072 ; color: #ffffff;">Grid</span> again...</span></p>
Please, observe that I have to ignore the word grid inside the HTML tag and change only the text.
Best regards
Here is a rustic solution using regular expressions. I assumed 1° that in text the word "grid" is neither preceded nor followed by a hyphen; 2° that in tags the word "grid" is always preceded or followed by an hyphen. If these assumptions are true, maybe this code could be a way to solve your problem.
{$APPTYPE CONSOLE}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
uses
regexpr;
const
SUBJECT = '<p class="Body"><span style="layout-grid-mode: line;" lang="EN-GB">Rome was the most important city in the world. Grid just for test. Grid again...</span></p>';
PATTERN = '([^-])([Gg][Rr][Ii][Dd])([^-])';
REPLACEMENT = '$1<span style="background-color: #FA8072 ; color: #ffffff;">$2</span>$3';
var
result: string;
begin
result := ReplaceRegExpr(
PATTERN,
SUBJECT,
REPLACEMENT,
TRUE // Use substitution
);
WriteLn(result);
ReadLn;
end.
fpc has a nice html parser in the unit "fasthtmlparser". It provides an event OnFoundText which fires whenever the parser finds html text (outside tags). This text is provided as a parameter, you can search the "Grid" in it and replace by whatever you want. Then you append this modified text to the previously found/modified texts.
Here is a working console project. For simplicity I am only calling StringReplace() to find the "Grid", but of course this will also replace "Grid" if contained inside a word - but you can apply Roland Chastain's RegExpr solution to avoid this.
program Project1;
{$mode objfpc}{$H+}
uses
fasthtmlparser, SysUtils;
type
TMyHTMLParser = class(THTMLParser)
private
FModifiedText: String;
procedure FoundTextHandler(Text: string);
public
property ModifiedText: String read FModifiedText write FModifiedText;
end;
procedure TMyHTMLParser.FoundTextHandler(Text: String);
begin
Text := StringReplace(Text,
'Grid',
'<span style="background-color: #FA8072 ; color: #ffffff;">Grid</span>',
[rfReplaceAll, rfIgnoreCase]
);
FModifiedText := FModifiedText + Text;
end;
const
html =
'<p class="Body">' +
'<span style="layout-grid-mode: line;" lang="EN-GB">' +
'Rome was the most important city in the world. '+
'Grid just for test. Grid again...'+
'</span>'+
'</p>:';
var
parser: TMyHTMLParser;
begin
parser := TMyHTMLParser.Create(html);
try
parser.ModifiedText := '';
parser.OnFoundText := #parser.FoundTextHandler;
parser.Exec;
WriteLn(parser.ModifiedText);
finally
parser.Free;
end;
end.
I am developing an application using Lazarus and I have to get the characters of a text that the user has entered in a TMemo component. I am using the following code to fetch the characters one by one (here mmText is the name of the TMemo component):
var
I, J: Integer;
Line: String;
Symbol: Char;
begin
for I := 0 to mmText.Lines.Count-1 do
begin
Line := mmText.Lines[I];
for J := 1 to Length(Line) do
begin
Symbol := Line[J];
ShowMessage(Symbol); //this line is for debugging purposes
...
When latin characters are entered in the TMemo component, popup messages with each letter appear but when the cycle reaches a cyrillic character there is nothing in the popup message box.
Could you give me advice what I should do to achieve the desired result?
For those who are interested, the answer is here:
http://forum.lazarus.freepascal.org/index.php?topic=29146.msg183536#msg183536