This code isn't working

General TRichView support forum. Please post your questions here
Post Reply
DelphiLove

This code isn't working

Post by DelphiLove »

This is continuation from my previous thread(which I would've replied under but not sure if it would be looked at again considering I didn't show signs of needing any more help).

Anyway the following doesn't work and freezes and takes place in OnChange. In OnKeyPress I insert an image named bold if ctrl+b is found.

Code: Select all

  CursorPos := RVGetLinearCaretPos( CommandEdit );

  I := 0;
  while I < CommandEdit.ItemCount - 1 do
  begin

    EndItemNo := -1;
    EndItemOffs := -1;

    if CommandEdit.GetItem( I ) is TRVGraphicItemInfo then
    begin

      if CommandEdit.GetItemText( I ) = 'Bold' then
      begin
        StartItemNo := I;
        StartItemOffs := CommandEdit.GetOffsAfterItem( StartItemNo );

        Inc( I );

        while I < CommandEdit.ItemCount - 1 do
        begin
          if CommandEdit.GetItem( I ) is TRVGraphicItemInfo then
          begin

            if CommandEdit.GetItemText( I ) = 'Bold' then
            begin
              EndItemNo := I;
              EndItemOffs := CommandEdit.GetOffsBeforeItem( EndItemNo );

              CommandEdit.SetSelectionBounds( StartItemNo, StartItemOffs, EndItemNo, EndItemOffs );

              Break;
            end;
          end;
          Inc( I );
        end;

        if ( EndItemNo = -1 ) and ( EndItemOffs = -1 ) then
        begin

          EndItemNo := CommandEdit.ItemCount - 1;
          EndItemOffs := CommandEdit.GetOffsBeforeItem( EndItemNo );

          Caption := Format('%d %d %d %d',[ StartItemNo, StartItemOffs, EndItemNo, EndItemOffs]);

          CommandEdit.SetSelectionBounds( StartItemNo, StartItemOffs, EndItemNo, EndItemOffs );
          CommandEdit.ApplyTextStyle( 1 );
        end;

      end;
    end;
    Inc( I );
  end;

  RVSetLinearCaretPos( CommandEdit, CursorPos );
  CommandEdit.Invalidate;
Guest

Post by Guest »

Sorry I insert named Bold not bold, just incase that would've came across your mind.
DelphiLove

Post by DelphiLove »

Bumping it up to top.
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

It seems that this code is correct, but ApplyTextStyle generates OnChange itself, so this procedure can be called infinitely until your program runs out of stack.

Add a variable to the form:

Code: Select all

Working: Boolean;
in OnChange:

Code: Select all

if Working then
  exit;
Working := True;
try
  <your code here>
finally
  Working := False;
end;
DelphiLove

Post by DelphiLove »

Ok this helped with freezing but I can still see the cursor show up for a second after the image also the code I pasted didn't make it bold for me.

When you tested it, did it make it bold for you?
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I did not tested, I just read this code in browser.
Your code makes the text bold by the command
CommandEdit.ApplyTextStyle( 1 );
Does RVStyle1.TextStyles[1].Style have fsBold included (I assume that CommandEdit.Style = RVStyle1). If you do not have a fixed set of styles, it's better to use ApplyStyleConversion+OnStyleConversion instead of ApplyTextStyle (one example of using them is shown in http://www.trichview.com/forums/viewtopic.php?t=57)

For improving efficiency:
1) Check, may be the text between these images is already bold, so you do not need to reapply it.
2) Call SendMessage(CommandEdit.Handle, WM_SETREDRAW, 0, 0) before, and SendMessage(CommandEdit.Handle, WM_SETREDRAW, 1, 0); CommandEdit.Invalidate after your code (for even more efficiency, call it only if you really changed document)
Guest

Post by Guest »

This I will try. Thanks a million. :)
Post Reply