Tedit from Cusomcontrol. Component lazarus / 31-LCL ---------------------------------------------------------------------- Onur2x · 2022-02-08 14:48 I am trying to write an Edit component derived from Cusomcontrol. I need help. There are some problems. For example, the text position. instant status of caret. Selected text vs. I am attaching the file as an attachment. I recommend that you review it. There may be mistakes in my English. sorry. file updated ---------------------------------------------------------------------- Bart · 2022-02-08 17:33 Just to make my life (and that of all who read this code) a bit easier, can you please change the magic numbers like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if not ((key = 35) or (key = 36) or (key = 46) or (key = 37) or    (key = 39) or (key = 8) or (key = 67) or (key = 88) or (key = 86))into some meanigfully named constants? Bart ---------------------------------------------------------------------- Bart · 2022-02-08 17:54 And, of course, the unit will not compile at all. Please fix that first in the way you see fit. Code: [Select]butonu.pas(559,78) Error: Wrong number of parameters specified for call to "CarettoCharlengt" butonu.pas(18,17) Error: Forward declaration not solved "constructor Create(TPersistent);" butonu.pas(25,14) Error: Forward declaration not solved "paint:Boolean;" When I "fix" that and I create a TOCustomEdit and show it on a form then I immediatley get a Range Check Error if I click the mouse on the control: Code: [Select]TApplication.HandleException: ERangeError Range check error   Stack trace:   $0055A294  TOCUSTOMEDIT__MOUSEDOWN,  line 942 of butonu.pas .... The control looks nice though, allbeit more like a button than an edit...  Bart ---------------------------------------------------------------------- Bart · 2022-02-08 18:13 Quote from: Bart on February 08, 2022, 06:54:58 pm...I immediatley get a Range Check Error if I click the mouse on the control: Code: [Select]TApplication.HandleException: ERangeError Range check error   Stack trace:   $0055A294  TOCUSTOMEDIT__MOUSEDOWN,  line 942 of butonu.pas .... This is because the for loop starts at 0, and FText[0] is invalid, since TFext is of type WideString, so it starts at 1. So, I fixed that, clicked on the control, pressed left-arrow key, and again an exception: Code: [Select]TApplication.HandleException: ERangeError Range check error   Stack trace:   $0055AA5C  TOCUSTOMEDIT__KEYDOWN,  line 1065 of butonu.pas This is because at that line caretchar is zero and then you try to do Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i prv := caretchar - 1;And, since prv is of type byte, you cannot assign the value of -1 to it. I redeclared prv as integer. I click on the control (caret is at the end), I press left-arrow: caret goes to first position. Why? Pressing right-arrow has no effect (no matter where you place the caret using the mouse). Home and end keys seem to function though. Using Shift+any of the arro-keys or Home/End seems to do nothing? Pressing Bacspace removes the caret, but does not alter the text. Pressing Delete seems to do nothing at all. At this point I simply gave up. What exactly are you trying to accomplish? Why the need for Text to be WideString? (There are numerous compiler warnings like: butonu.pas(1223,71) Warning: Implicit string type conversion with potential data loss from "WideString" to "AnsiString".) And so much more questions.... Bart ---------------------------------------------------------------------- Onur2x · 2022-02-08 18:47 Quote from: Bart on February 08, 2022, 06:33:57 pmJust to make my life (and that of all who read this code) a bit easier, can you please change the magic numbers like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if not ((key = 35) or (key = 36) or (key = 46) or (key = 37) or    (key = 39) or (key = 8) or (key = 67) or (key = 88) or (key = 86))into some meanigfully named constants? Bart This means that the keys are not used. CarettoCharlengt I forgot to add the CarettoCharlengt function to the private section. sorry I stated that the component is not completely finished, I am currently in the development phase. There may be things I'm stuck on and mistakes. I will fix these errors, but for now, there are problems when selecting all with the mouse or typing long text, like Tedit, and I could not solve them. The reason for choosing widestring is that when some characters are strings, they appear as empty or empty boxes, so not all characters are written. ---------------------------------------------------------------------- Bart · 2022-02-08 21:36 Quote from: Onur2x on February 08, 2022, 07:47:09 pmQuote from: Bart on February 08, 2022, 06:33:57 pmJust to make my life (and that of all who read this code) a bit easier, can you please change the magic numbers like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if not ((key = 35) or (key = 36) or (key = 46) or (key = 37) or    (key = 39) or (key = 8) or (key = 67) or (key = 88) or (key = 86))into some meanigfully named constants? Bart This means that the keys are not used. And from that I, the reader of this code, am supposed to know what 88, 86 or 36 means? Presumably 8 means BackSpace, I assume 88 also is bound to a certain key? So, just give those constants names please. Do you really think that, when you yourself reads this code after, let's say one year, you remember what 88 actually stands for? You do know that Lazarus already has constants defined for most keys (in the OnKeyDown/OnKeyUp events)? See: https://wiki.lazarus.freepascal.org/Lazarus_FAQ#Virtual_key_constants. Quote from: Onur2x on February 08, 2022, 07:47:09 pm...but for now, there are problems when selecting all with the mouse or typing long text, like Tedit, and I could not solve them. In the state you offered your code, it was impossible to test, so how did you test the behaviour then? Quote from: Onur2x on February 08, 2022, 07:47:09 pmThe reason for choosing widestring is that when some characters are strings, they appear as empty or empty boxes, so not all characters are written. Well, you're doing something wrong then. As long as the font actually supports the "character" you want to display, using String (in a default Lazarus) will actually do fine. Strings in Lazarus are UTF8 encoded by default and so are fully Unicicode enabled. Everything you can represent in a UnicodeString, you can represent in an UTF8 encoded string, without any loss. Still this leaves the question unanswered what you actually want to achieve. To me it looks like you just want to change the appearence of the edit. Re-implementing almost from scratch a TCustomEdit like control is a hell of a job. Just take a look at the implementation of TCustomEdit. Especially since you want to implement all the editing by yourself, something that we were happy to let the OS/WidgetSet do. It's not impossible, but why do you want te re-invent the wheel? The same goes for implementing the caret and the actual behaviour of the mouse (selecting text, calculating where to place the caret upon MouseDown etc.). Bart ---------------------------------------------------------------------- Bart · 2022-02-09 08:55 Quote from: Onur2x on February 08, 2022, 07:47:09 pmQuote from: Bart on February 08, 2022, 06:33:57 pmJust to make my life (and that of all who read this code) a bit easier, can you please change the magic numbers like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if not ((key = 35) or (key = 36) or (key = 46) or (key = 37) or    (key = 39) or (key = 8) or (key = 67) or (key = 88) or (key = 86))into some meanigfully named constants? Bart This means that the keys are not used. And by that you make it impossible to type any of the following chars into the control (at least on my machine here): # $ . % ' C X V I assume that this is on purpose? Your component never ever needs one of those characters typed into the control. I'm getting more and more curious about what this control is all about  Bart ---------------------------------------------------------------------- Onur2x · 2022-02-09 09:05 Actually, Black Shark also has a Tedit control. I think this can help us with mouse, text selection etc. movements for Tedit. Why would I want this control, because an Edit is customizable. You can put a picture on the background. region can be created etc. ---------------------------------------------------------------------- Bart · 2022-02-09 10:17 Yet another mysterious observation. I enter the control (mouse after last cahacter). I repeatedly press a "normal" key and at some point in time SelText becomes not empty and then delSelected triggers yet another range check error, but this only happens when I press either z, x, c or v, and only lowercase. And this only happens if insertion of the character is NOT at the very end (a condition that is only met when you start out with a blank edit, see my next remark). Oh and b.t.w.: characters are inserted at the wrong place, 1 character before the position of the caret. And you also seem to want handle Ctrl+A .. Ctrl+Z as printable characters, which is an interesting feature. (And even though you think using WideStrings will make them display correctly: they don't.) Bart ---------------------------------------------------------------------- Onur2x · 2022-02-09 10:35 If there is an update you have added, can you share the file? At least it can be helpful for friends who want to improve ---------------------------------------------------------------------- Bart · 2022-02-09 11:10 Quote from: Bart on February 09, 2022, 11:17:22 amYet another mysterious observation. I enter the control (mouse after last character). I repeatedly press a "normal" key and at some point in time SelText becomes not empty and then delSelected triggers yet another range check error, but this only happens when I press either z, x, c or v, and only lowercase. I can reproduce this reliably: In the constructor do not set Text to OEdit, just leave it empty, so you start with wen empty control. Click on the control: Type "T" (without the quotes) Click the mouse after the T MouseDown will set SelStart and SelEnd to 6 (this in itself is a bug) repeatedly press "v" In KeyDown observe this: Code: [Select]KeyDown: SelEnd=6, SelStart=6,fText=T, SelText -> "" KeyDown: SelEnd=6, SelStart=6,fText=vT, SelText -> "" KeyDown: SelEnd=6, SelStart=6,fText=vvT, SelText -> "" KeyDown: SelEnd=6, SelStart=6,fText=vvvT, SelText -> "" KeyDown: SelEnd=6, SelStart=6,fText=vvvvT, SelText -> "" KeyDown: SelEnd=6, SelStart=6,fText=vvvvvT, SelText -> "T" This happens in the line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if SelEnd > 0 then  seltext := copy(Ftext, SelStart, SelEnd - SelStart + 1);// else seltext:= ''; That line of code is only triggered it he user presses x, c or v (lowercase only). Notice that if SelStart = SelEnd, there is no selection, so the part that sets SelText is actually wrong as well. In KeyDown I also noticed something that I find a little odd. You check for certain keys and act on it: so far so good. After you have done that you go on and check if SelText is not empty. You do that actually 3 times in a row. If SelText is not empty you check the pressed key to see if you need to copy, paste or cut. Wouldn't it be more logical to check for Ctrl+C etc. and when handling that key check for SelText not empty? At least that would be consistent with the rest of the code in KeyDown. B.t.w. In KeyDown you also allow Ctrl+C to be handled if the control is disabled. In itself maybe a good idea, but KeyDown will not be fired if the control is not enabled. (That piece of code is harmless though.) What also puzzles me is that if I press e.g. Left-Arrow or Home an OnChange is triggered, but I never receive an OnKeyDown regardless of what key I press. (Well actually it puzzles me why you chose to implement it that way.) Bart ---------------------------------------------------------------------- Bart · 2022-02-09 11:17 Quote from: Onur2x on February 09, 2022, 11:35:53 amIf there is an update you have added, can you share the file? At least it can be helpful for friends who want to improve To be quite honest: I think it is broken beyond repair. You better rethink what you want to achieve and then is this can be achieved deriving form a control that actually already implements editing text. Nevertheless I attached the modified butonu.pas file. Notice that I use writeln statements for debugging (you use the parents Caption for that, which is a really bad idea), so you need to make sure that in Compiler Options -> Config and Target, you uncheck the option "Win32 GUI application", otherwise the application will crash instantly. (This only applies if you are on Windows of course.) Bart ---------------------------------------------------------------------- lainz · 2022-02-09 15:36 Try custom drawn package, there is a custom edit control. ---------------------------------------------------------------------- Onur2x · 2022-02-10 19:13 thank you, now i think i will apply and share ---------------------------------------------------------------------- Onur2x · 2022-02-25 14:11 I made the component, there may be a deficiency. I've put it on github now, I'll add new components and make the necessary adjustments as I find time. including a small demo. https://github.com/Onur2x/Gradient_comp