Recent

Author Topic: Automating font size  (Read 4627 times)

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Automating font size
« on: October 17, 2016, 09:00:21 pm »
I have built the following code for changing font sizes by key a key toggle. Alt-UpArrow sizes up, and Alt-DownArrow sizes down. If it is a selection it all works perfectly, but there is a problem when nothing is selected.

If I press UP for 1 or 2 times, it works fine. Then if I press DOWN for 1 to 3 times it works well. Yet if I do one more with either, my next keystroke is either not posted or a small box with a question mark appears. With either, if I press the key again the letter is posted properly with the new size.

I can't figure out what is happening to cause the malfunction.

Code: Pascal  [Select]
  1. if (Key = VK_UP) and (ssAlt in Shift) then  // font size up by 1
  2.      begin
  3.      Key:= 0;  // uses WORD instead of CHAR... ie. not #0
  4.      if PageMemo.SelLength>0
  5.         then PageMemo.Perform(EM_SETFONTSIZE, 1, 0)
  6.         else begin
  7.              x:= cboFontSize.ItemIndex;
  8.              if x<16
  9.                 then begin
  10.                      cboFontSize.ItemIndex:= x+1;
  11.                      cboFontSizeSelect(Self);
  12.                      end;
  13.              end;
  14.      end;
  15.   if (Key = VK_DOWN) and (ssAlt in Shift) then   // font size down by 1
  16.      begin
  17.      Key:= 0;  // uses WORD instead of CHAR... ie. not #0
  18.      if PageMemo.SelLength>0
  19.         then PageMemo.Perform(EM_SETFONTSIZE, -1, 0)
  20.         else begin
  21.              x:= cboFontSize.ItemIndex;
  22.              if x>0
  23.                 then begin
  24.                      cboFontSize.ItemIndex:= x-1;
  25.                      cboFontSizeSelect(Self);
  26.                      end;
  27.              end;
  28.      end;
  29.  

Rick
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2186
    • havefunsoft.com
Re: Automating font size
« Reply #1 on: October 17, 2016, 10:02:10 pm »
Try this code
Code: Pascal  [Select]
  1. procedure TForm1.RichMemo1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. var
  4.   fp : TFontParams;
  5. begin
  6.  
  7.   if (Key in [VK_UP,VK_DOWN]) and (ssAlt in Shift) then begin
  8.  
  9.     RichMemo1.GetTextAttributes(RichMemo1.SelStart, fp);
  10.  
  11.     if Key = VK_UP
  12.       then fp.Size:=fp.Size+1
  13.       else fp.Size:=fp.Size-1;
  14.  
  15.     RichMemo1.SetRangeParams(RichMemo1.SelStart, RichMemo1.SelLength, [tmm_Size],fp, [], []);
  16.  
  17.     Key:=0;
  18.   end;
  19. end;
  20.  
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #2 on: October 18, 2016, 02:37:04 pm »
Your code isn't doing the same thing that mine is. With mine, if a string of mixed sizes are selected, it individually increments or decrements each character in the selected string... a size 10 becomes a 11, and another at 28 becomes a 36 (they increase privately by 1 for 8 through 11, then by 2 for 12 through 26, and then steps from 28 to 36, 48 and 72). It is the EM_SETFONTSIZE system.

Your code is changing the size, but applying it uniformly to the string... ie. all become size 10.

Also, by mine having the same steps in my font size combo-box, I am stepping through them with its ItemIndex attribute. So it behaves the same way as EM_SETFONTSIZE.

Although your code is not doing these things, it still acted as mine does with unselected text. Hit the UP 3 times, and then the character, and you get the ? box.

I think I had not described the problem well. As just said, if I hit the UP key once or twice in succession, before hitting a character key, it works.

If I hit the UP key 3 times or more in succession, the character key will either not post or a box with a ? in it will appear... yet if I hit the character key one more time, it will post properly (and the ? box remains).

The DOWN key acts the same way, but it doesn't malfunction until I have hit the DOWN key 4 times (once more than with the UP key). Hitting the character key one more time will post the sized character after the ? box.

Rick
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #3 on: October 18, 2016, 02:42:07 pm »
I need to add... with both your code and mine, it seems that a stack corruption occurs after 3 successive UP's or 4 successive DOWN's. Garbage is left in the keyboard stack.

Perhaps you know of way for clearing the stack buffer?
« Last Edit: October 18, 2016, 02:47:51 pm by rick2691 »
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2186
    • havefunsoft.com
Re: Automating font size
« Reply #4 on: October 18, 2016, 04:53:09 pm »
I need to add... with both your code and mine, it seems that a stack corruption occurs after 3 successive UP's or 4 successive DOWN's. Garbage is left in the keyboard stack.

Perhaps you know of way for clearing the stack buffer?
I cannot confirm the issue. Please review the attached example. Please adjust it to expose the issue you're experiencing.

I'm also thinking that the issue might be caused by an error while using the combobox. (note I don't have it in my example at all)

Btw, thanks for the showing the usage of EM_SETFONTSIZE. I didn't realize it even existed :)
I should use it somehow in RichMemo. Currently to increase a font size gradually, one should loop from the style ranges in the selection and adjust each range.
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #5 on: October 18, 2016, 05:03:47 pm »
Your code does it with 3 UP's and then a key, or 4 DOWN's and then a key, the same as mine. Maybe it is an XP issue, but even if it is, do know a way to clear the keyboard buffer?

Rick
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #6 on: October 18, 2016, 06:14:10 pm »
I think I found the problem. I held the ALT key down and pressed UP, then released the ALT key, and repeated 4 times. It acted properly.

I think it may be an auto repeat function by holding down the ALT key continuously. It seems to me that I only have to clear the SHIFT parameter, but I don't see a way of assigning NONE to the parameter.

Rick
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #7 on: October 18, 2016, 06:22:24 pm »
I just did a rapid bunch of DOWN's and I got this character... 囎

That is a Far East unicode character. Since you can type unicode by holding down the ALT key, and then typing a number, I think the system thinking that I am doing unicode.

I chose the ALT key because the system already responds to CTRL arrows for document navigation. It also responds to SHIFT arrows for selection. I don't seem to have any options here.

Rick
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2186
    • havefunsoft.com
Re: Automating font size
« Reply #8 on: October 18, 2016, 07:29:30 pm »
Wait! are you pressing DOWN on your keypad?
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #9 on: October 18, 2016, 07:56:01 pm »
Down Arrow
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #10 on: October 18, 2016, 09:12:13 pm »
I fixed it. I put the code in the OnKeyUp event. That made it process things before Windows can do the ALT-# routine for unicode.

It doesn't make sense to me, but I gave it a shot, and that is what needed to happen.

Rick
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Re: Automating font size
« Reply #11 on: October 18, 2016, 09:54:29 pm »
I thought I might mention about EM_SETFONTSIZE. You can set its jump to any figure, but using 1 or -1 is all you need to do. It rounds to the next step. So within 8 to 11 it adds or subtracts one. Then 12 to 27 it steps by the nearest two's with the same setting. Then with 28 (the 1 for an increment) it steps through 36, 48, 72. Once hitting 80 it steps by 10's.

Rick
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit