TMaskEdit and TCanvas.Brush lazarus / 31-LCL ---------------------------------------------------------------------- gitspielen · 2011-05-17 16:56 hi there are two environments i'm working with, one is lazarus 0.9.30 on windows 7 and the other is gtk2 ide of the same version on ubuntu 11.04 and unity. i had finished my project on windows and then i tried to make a linux version but it doesn't work well. the biggest problem i face with is concerning tmaskedit. i set its property 'EditMask' to 'aaaa.aa.aa. aa\:aa;1;_', run it, type characters in the field but only the one letter i put firstly appears and i cannot do anything with the component afterward. i entered any mask to the property for some tests such as '0000;1;_' but the results were all the same. even it crashed sometimes. secondly i wrote some texts on timage.canvas setting its brush.color for their background colors like this: Code: [Select]with Image1.Canvas do begin   Font.Color:=clRed;   Brush.Color:=clYellow;   Brush.Style:=bsSolid;   TextOut(5, 5, 'blah blah'); end; red texts on yellow displayed on windows box as i expected but only the text color changed on linux. what's wrong with that? apology for my poor english as a korean, and it could be some clues for the errors above because of its unique character system. ---------------------------------------------------------------------- danthorpe · 2011-05-17 18:43 I´m also having some problems in the MaskEdit component, running in the Ubuntu 11.04. I wonder if it has something to do with the GTK2 version (2.4.24 if I´m not mistaken) that comes with the OS. Anyone ? ---------------------------------------------------------------------- Bart · 2011-05-17 20:08 There haven't been any significant changes with TMaskEdit since > 1 year now. TMaskEdit should work independently form the widgetset (there are no different incudes per widgetset). The inner workings of TMaskEdit, however, rely heavily on the proper functioning of TCustomEdit.SelStart and TCustomEdit.SelLength, and the ability to change these within TCustomEdit.TextChanged. Recently there have been issues with that on GTK2. My guess it, that it's a widgetset issue. When testing the mask you mentioned in your firts post, but under windows, it works just fine (Laz 0.9.31 r29641: this includes the latest maskedit.pp (latest revision = r29200)). Also note that TMaskEdit currently only works with lower ASCII input, it will reject all other input. I'll test on Linux/GTK2 Bart ---------------------------------------------------------------------- Bart · 2011-05-17 20:17 Just tested with Lazarus 0.9.31 r29137 on Suse 10.0 GTK2 (gtk2-2.8.3-4), this seems to work without any problems. Bart ---------------------------------------------------------------------- gitspielen · 2011-05-17 21:46 thanks bart but it seems that your gtk2 is a little bit outdated. mine is 2.24.4-0, installed today. would you mind if i ask you to test it again under the latest version? ---------------------------------------------------------------------- Bart · 2011-05-18 09:25 Quote from: gitspielen on May 17, 2011, 11:46:02 pmbut it seems that your gtk2 is a little bit outdated. mine is 2.24.4-0, installed today. Yeah, my computer now is > 10 years old ... Quote from: gitspielen on May 17, 2011, 11:46:02 pmwould you mind if i ask you to test it again under the latest version? I don't mind, but as I said TMaskEdit hasn't been changed for a long time and it used to work correctly under win32, gtk, gtk2 at least (never received any feedback from mac or darwin). The simple fact that it does work under win32 and the way it is constructed means that, if the LCL (and TCustomEdit in particularly) behaves the same on all widgetsets, then it should must also work in GTK2. (Take a look at maskedit.pp and see that in essence the control checks for valid input and manipulates SelStart and SelLength and the text in the control.) The fact that is does with my old GTK2 and not with your, suggests to me that there is a fault in the widgetset implementation. This may, or may not, be related to issue #19220 in the bugtracker. Bart ---------------------------------------------------------------------- gitspielen · 2011-05-18 15:44 Great advice, Bart. I found a very probable clue on your comment in MaskEdit.pp: Quote So, as a horrible hack I decided  to only allow changing of the text if we coded  this change ourself. This is done by setting the FChangeAllowed field to True before any  write action (in SetInherited Text() ).  We try to intercept the messages for cut/paste/copy/clear and perform the appropriate  actions instead.  If this fails, then in TextChanged we check and will see that FChangeAllowed = False  and we will undo the changes made. Unfortunately, it seems that the way your job about FChangedAllowed works makes the problem here on the latest GTK2 Widgetset. I made breakpoints at TCustomMaskEdit.TextChanged and Keydown procedure, put a character in the field, and noticed that SetInheritedText procedure set FChangedAllowed to False. As far, the process above was just as you intended but the problem is following. I typed another letter anticipating Keydown event but the widget just called TextChanged and that's all! I'm not a professional programmer nor informed of low level programming so hardly can I touch anything from now on. :-( Thanks. ---------------------------------------------------------------------- gitspielen · 2011-05-18 15:51 And I'm still curious about my next question, background color of text on TImage.Canvas. Is there anyone who can help me? ---------------------------------------------------------------------- Bart · 2011-05-18 21:42 Quote from: gitspielenI made breakpoints at TCustomMaskEdit.TextChanged and Keydown procedure, put a character in the field, and noticed that SetInheritedText procedure set FChangedAllowed to False I'ld say you didn't get it right. When you press a (printable) key then the follwing happens: TCustomMaskedit.KeyPress (not KeyDown) b.t.w. calls TCustomMaskedit.InsertChar, which calls TCustomMaskedit.SetInheritedText (if TCustomMaskedit.CanInsertChar returns true). TCustomMaskedit.SetInheritedText then first sets the flag FChangeAllowed to true, then sets the text in the control. This then triggers the TextChanged event to be called, which checks wether FChangeAllowed = True (which it is), and allows the change. After this we return to TCustomMaskedit.SetInheritedtext, and at this point we set FChangeAllowed to False. This should be completely independant of the widgetset. I just tested my MaskEdit program (Lazarus 0.9.31 r29137) using Ububtu 11.02 (GTK2 2.20), and it works like a charm. What you can do is open maskedit.pp and replace TCustomMaskEdit.InsertChar with the following code Code: [Select]procedure TCustomMaskEdit.InsertChar(Ch : Char); Var   S    : ShortString;   i, SelectionStart, SelectionStop: Integer; begin   if CanInsertChar(FCursorPos + 1, Ch) then   begin     S := Inherited Text;     debugln('TCustomMaskEdit.InsertChar: Ch = ',Ch,' CanInsertChar = True');     if HasSelection then     begin       //replace slection with blank chars       //don't do this via DeleteChars(True), since it will do an unneccesary       //update of the control and 2 TextChanged's are triggerd for every char we enter       GetSel(SelectionStart, SelectionStop);       for i := SelectionStart + 1 to SelectionStop do S[i] := ClearChar(i);     end;     S[FCursorPos + 1] := Ch;     debugln('TCustomMaskedit.InsertChar: calling SeInheritedText(',S,')');     SetInheritedText(S);     SelectNextChar;   end   else   begin     debugln('TCustomMaskedit.InsertChar: CanInsertChar = False');     //If we have a selection > 1 (and cannot insert) then Delete the selected text: Delphi compatibility     if HasExtSelection then DeleteSelected;   end; end; Then rebuild the LCL (clean), build the program and then start it from a console. Use 'ccccccccccc;1;_' as the EditMask (this should allow you to type any (lower ASCII) char in the control). Select the control and try to type in it and report the debug output on the console. (Please also make sure that you did not accidentally set ReadOnly to True for the MaskEdit component...) Bart ---------------------------------------------------------------------- gitspielen · 2011-05-18 22:20 It is quite interesting I could know the fact by chance to build LCL that the control works well with root permission! This is debug output: Code: [Select]user@ubuntu:~/jmds$ ./jmds TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1011.05.19. 07:10) /// I kept trying to put other numbers but failed. user@ubuntu:~/jmds$ sudo ./jmds TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1011.05.19. 07:10) TCustomMaskEdit.InsertChar: Ch = 2 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1211.05.19. 07:10) TCustomMaskEdit.InsertChar: Ch = 3 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1231.05.19. 07:10) TCustomMaskEdit.InsertChar: Ch = 4 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1234.05.19. 07:10) /// It works! user@ubuntu:~/jmds$ What's the difference? ---------------------------------------------------------------------- Bart · 2011-05-19 08:02 It (the working of TMaskEdit) should make no difference whatsoever wether you are root or not. Bart ---------------------------------------------------------------------- gitspielen · 2011-05-19 08:32 Quote from: Bart on May 19, 2011, 10:02:19 amIt (the working of TMaskEdit) should make no difference whatsoever wether you are root or not. Yes, truly. Maybe the bug is related to widgetset implementation or peculiar reaction of the system though almost I didn't make personalization.  Thanks anyhow. ---------------------------------------------------------------------- gitspielen · 2011-05-19 08:41 Quote from: gitspielen on May 18, 2011, 05:44:52 pmI made breakpoints at TCustomMaskEdit.TextChanged and Keydown procedure, put a character in the field, and noticed that SetInheritedText procedure set FChangedAllowed to False. As far, the process above was just as you intended but the problem is following. I typed another letter anticipating Keydown event but the widget just called TextChanged and that's all! What do you think of my suggestion? I also made a breakpoint at KeyPress procedure at that time and found without root permission, the debugger just passed it too QuoteI'ld say you didn't get it right. When you press a (printable) key then the follwing happens: TCustomMaskedit.KeyPress (not KeyDown) b.t.w. calls TCustomMaskedit.InsertChar, which calls TCustomMaskedit.SetInheritedText (if TCustomMaskedit.CanInsertChar returns true). TCustomMaskedit.SetInheritedText then first sets the flag FChangeAllowed to true, then sets the text in the control. This then triggers the TextChanged event to be called, which checks wether FChangeAllowed = True (which it is), and allows the change. After this we return to TCustomMaskedit.SetInheritedtext, and at this point we set FChangeAllowed to False. This should be completely independant of the widgetset. Ah, I've already noticed the process by trace. I just said it simply in terms of the result. Here is my another example with a new form: Code: [Select] // I allocated a new event to MaskEdit1 procedure TForm1.MaskEdit1KeyPress(Sender: TObject; var Key: char); begin   Edit1.Text:=Edit1.Text + Key; end; The text of Edit1 changes only one time. Too much mysterious! ---------------------------------------------------------------------- Bart · 2011-05-19 09:49 In TMaskEdit.KeyPress (in maskedit.pp) place as the frits line (after begin) Code: [Select]  DebugLn('TCustomMaskdit.KeyPress: Key = ',Key); In ObjectInspector set EditMask to 'cccccccc;1;_' Also set Text to '' (empty) Rebuild LCL clean, rebuild app, run from console (as normal user). Set focus to Maskedit, then type '12345' and show me the debugoutput (only the debug output from within KeyPress please). If this also fails then install a snapshot or install trunk version using svn (see the wiki). If the problem remains, then file a bugreport in Mantis (see link in menu on the left here). Bart ---------------------------------------------------------------------- gitspielen · 2011-05-20 00:40 OK. Quote from: Bart on May 19, 2011, 11:49:46 amSet focus to Maskedit, then type '12345' and show me the debugoutput (only the debug output from within KeyPress please). Result: Code: [Select]user@ubuntu:~/test$ ./project1 TCustomMaskdit.KeyPress: Key = 1 user@ubuntu:~/test$ Failed. Quote from: Bart on May 19, 2011, 11:49:46 amIf this also fails then install a snapshot or install trunk version using svn (see the wiki). Code: [Select]user@ubuntu:/media/home/src/lazarus$ ~/maskedit/project1 TCustomMaskdit.KeyPress: Key = 1 user@ubuntu:/media/home/src/lazarus$ Failed again. Quote from: Bart on May 19, 2011, 11:49:46 amIf the problem remains, then file a bugreport in Mantis (see link in menu on the left here). I'll try. By the way, I really appreciate your concern over my thread. ---------------------------------------------------------------------- Bart · 2011-05-20 10:37 OK, this gets interesting. The key is seen and processed in KeyPress. Now again I would like to see the debug output from both KeyPress and InsertChar. Start with the MaskEdit as described above (empty text, EditMask = 'ccccccc;1;_') Set focus to the control. Type '1' (without the quotes) Report all debug output. It should look something like: Code: [Select]TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SetInheritedText(1_______) And the control should now something like read '1________' Please report back. B.t.w. if running as normal user fails (also report what fails), also run as root (since last time it seemde to work then). B.t.w. can you isolate the problem in a minimal test case (1 form 1 TMaskEdit nothing else) and see if the problem remains? You can attach the source (*.lpi;*.lfm;*.pp) here. Bart ---------------------------------------------------------------------- gitspielen · 2011-05-20 13:40 Quote from: Bart on May 20, 2011, 12:37:22 pmB.t.w. can you isolate the problem in a minimal test case (1 form 1 TMaskEdit nothing else) and see if the problem remains? You can attach the source (*.lpi;*.lfm;*.pp) here. I've already done it on the last test (~/test/project1 and ~/maskedit/project1. I needed to separate them because of permission but just the same things). They are the exact results of a new application project with a single MaskEdit component on it. Only EditMask property was changed for the test. You may be allowed to believe me! LOL I'll report your requests ASAP but not now because I gotta go out. ---------------------------------------------------------------------- gitspielen · 2011-05-21 00:56 This is the output: Code: [Select]user@ubuntu:/media/home/src/lazarus$  ~/maskedit/project1 TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1_______) user@ubuntu:/media/home/src/lazarus$ sudo ~/maskedit/project1 TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1_______) TCustomMaskdit.KeyPress: Key = 2 TCustomMaskEdit.InsertChar: Ch = 2 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(12______) TCustomMaskdit.KeyPress: Key = 3 TCustomMaskEdit.InsertChar: Ch = 3 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(123_____) TCustomMaskdit.KeyPress: Key = 4 TCustomMaskEdit.InsertChar: Ch = 4 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1234____) TCustomMaskdit.KeyPress: Key = 5 TCustomMaskEdit.InsertChar: Ch = 5 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(12345___) Failed as a normal user. Hm. ---------------------------------------------------------------------- Bart · 2011-05-22 08:50 Now I'm getting confused. As the debugoutput suggests, it sets the text in the control as expected. Code: [Select]TCustomMaskedit.InsertChar: calling SetInheritedText(12345___) What then do you mean by: "Failed as a normal user"? Does the text "12345___" appear in the MaskEdit? If not: when you clear the MaskEdit (in code do MaskEdit1.EditMask := ''; MaskEdit.EditMask := 'cccccccc;1;_'; This forces a new mask to be created, and it forces a redrawing of the text) does the control look like "________" (without the quotes)? If it does, the it proves that SetInheritedText() works OK. One other test you can do for me: Change the code for TCustomMaskEdit.TextChanged as follows: Code: [Select]procedure TCustomMaskEdit.TextChanged; { Purpose: to avoid messing up the control by   - cut/paste/clear via OS context menu     (we try to catch these messages and handle them,     but this is not garantueed to work)   - dragging selected text in the control with the mouse   If one of these happens, then the internal logic of cursorpositioning,   inserting characters is messed up.   So, we simply restore the text from our backup: FCurrenText } var S: String; begin   S := inherited Text;   Debugln('TCustomMaskEdit.TextChanged: IsMasked = ',DbgS(IsMasked),' FChangeAllowed = ',DbgS(FChangeAllowed),' Current Text = ',S);   if (not IsMasked) or FChangeAllowed then   begin     Inherited TextChanged;   end   else   begin//Undo changes: restore with value of FCurrentText     //we do not call inherited TextChanged here, because the following SetInheritedText     //will trigger TextChanged with FChangeAllowed = True and inherited TextChanged is called then     Debugln('TCustomMaskEdit.TextChanged: Disallowing the change');     SetInheritedText(FCurrentText);     //Reset cursor to last known position     SetCursorPos;   end; end; In ObjectInspector set EditMask to 'cccccccc;1;_' Also set Text to '' (empty) Rebuild LCL clean, rebuild app, run from console (as normal user). Set focus to Maskedit, then type '12345' and show me the debugoutput (all of it). If it "fails" please also make a screenshot of the running application (after you typed '12345') and attach it here. And just for the heck of it: After this: can you set EditMask to '' (empty) and run and type any text in the control: does it show any text then? And finally: do you have a valid color set for the font (and a valid font) in the control? Otherwise you may end up seeing nothing at all  Bart ---------------------------------------------------------------------- Bart · 2011-05-22 13:27 I just updated my Lazarus to r30856 and rebuild my testsuite for TMaskEdit. It runs just fine on my old machine. Then I tested the binary on Ubuntu 11.02 (GTK2 version = 2.24.2) (on a modern laptop) as normal user and as root. In both cases it worked fine. I attached my testsuite. Bart ---------------------------------------------------------------------- gitspielen · 2011-05-22 16:09 Quote from: Bart on May 22, 2011, 10:50:24 amWhat then do you mean by: "Failed as a normal user"? I meant that when I launched the application as a normal user, the problem appeared as it used to be. You may see the result in the following lines on my terminal, "$ ~/maskedit/project1". I kept typing other numbers "2345" but they didn't show up. But I did it again under root permission, it worked perfectly. See after "$ sudo ~/maskedit/project1". You were confused because you missed my sudo command. ---------------------------------------------------------------------- gitspielen · 2011-05-22 16:42 I applied the lastest revision, and I tested it again with a new debug information reporter, typing "12345": Code: [Select]user@ubuntu:~/maskedit$ ./project1 TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = ________ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = ________ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1_______) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1_______ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1_______ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change user@ubuntu:~/maskedit$ See the difference under root permission: Code: [Select]user@ubuntu:~/maskedit$ sudo ./project1 [sudo] password for user: TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = ________ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = ________ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1_______) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1_______ TCustomMaskdit.KeyPress: Key = 2 TCustomMaskEdit.InsertChar: Ch = 2 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(12______) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 12______ TCustomMaskdit.KeyPress: Key = 3 TCustomMaskEdit.InsertChar: Ch = 3 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(123_____) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 123_____ TCustomMaskdit.KeyPress: Key = 4 TCustomMaskEdit.InsertChar: Ch = 4 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1234____) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1234____ TCustomMaskdit.KeyPress: Key = 5 TCustomMaskEdit.InsertChar: Ch = 5 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(12345___) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 12345___ user@ubuntu:~/maskedit$ OMG. What in the world happened to my machine? ---------------------------------------------------------------------- gitspielen · 2011-05-22 17:37 The attachments are the screenshots of each phases as I pushed 'Print screen key -> 1 & Print screen key -> 2 & print screen' as a normal user. However, When I cleared EditMask, I could type any sentence so it seemed to work fine. Arrgh ---------------------------------------------------------------------- Bart · 2011-05-22 21:50 Quote from: gitspielen on May 22, 2011, 06:42:58 pmI applied the lastest revision, and I tested it again with a new debug information reporter, typing "12345": Code: [Select]user@ubuntu:~/maskedit$ ./project1 TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = ________ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = ________ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1_______) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1_______ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1_______ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1_______ TCustomMaskEdit.TextChanged: Disallowing the change user@ubuntu:~/maskedit$ This sucks. In this piece of info, I can only see one KeyPress (namely '1'). There seems to be an (endless?) number of TextChanged, when there should be only one. In theory: - you press '1' - KeyPress calls InsertChar - InsertChar determines the Key can be inserted - InsertChar then calculates the new text - Mind you: up until now, the text in the control has not changed! - InsertChar calls SetInheritedText with the appropriate new text content - SetInheritedText sets FChangeAllowed to True and sets the text in the control - Only now the text in the control gets changed - The widgetset (GTK2 in this case) detects this and fires a signal - The signal is caught by the app (LM_TEXTCHANGED I believe) and TextChanged will be executed - In TextChanged FChangeAllowed = True - Inherited TextChanged is called (the one from TCustomEdit) - This calls Change, but does not alter the text (in our case) - This calls the OnChange handler (if one is assigned) - TextChanged is done now - Return to SetInheritedText, which sets FChangeAllowed back to False So there should be only one call to TextChanged after each keypress, but in your case there are multiple. So something else must be somehow changing the contents of the control? The whole purpose of the FChangeAllowed business is that, for example, in GTK2 you can select text in the control and then drag it to another position. This will trigger a TextChanged, and FChangeAllowed will be False (and so it must be) in this case. What I also do not understand is the following: in the beginning the control looks like '__________' , this is achieved via the same mechanism (see TCustomMaskEdit.Clear) and it seems to work OK on your machine. Can you do me a favour and use my testsuite (see attachment in my previous message). Don't change any properties in OI, just build an run. On startup the MaskEdit (topmost edit) is not masked (so it acts as a TEdit). Now press the "Apply Mask" button. This will set the EditMask to whatever is in the second Edit control (just leave the default 'cccccc' as it is) Since setting a new EditMask also clears the MaskEdit control it's text should now read '______' Does it do that? Now press the "Set Text" button. It will try to set MaskEdit.Text to whatever is in the third Edit control (just leave the default 'ABCDEF' as it is) The text in the MaskEdit should now be 'ABCDEF' Does it do that? At this point, if there is any text in the MaskEdit, select one (or more) character(s) and press Delete. The selected chars should be replaced with '_' Does is do that? And one final question: In your own program can you create an OnChange handler for the MaskEdit. In it just do Code: [Select]  Debugln('OnChange'); (Debugln is in LCLProc unit, so you need to include that in your uses clause) Then run these 2 scenario's 1 - EditMask = '' (empty mask) - run - type '1' in the control (just once) - close the program 2 - EditMask = 'cccccc' - run - type '1' in the control (just once) - close the program How many OnChanges do you see in each of the 2 scenario's? Bart ---------------------------------------------------------------------- gitspielen · 2011-05-22 23:45 QuoteCan you do me a favour and use my testsuite (see attachment in my previous message). Don't change any properties in OI, just build an run. On startup the MaskEdit (topmost edit) is not masked (so it acts as a TEdit). Now press the "Apply Mask" button. This will set the EditMask to whatever is in the second Edit control (just leave the default 'cccccc' as it is) Since setting a new EditMask also clears the MaskEdit control it's text should now read '______' Does it do that? Yes. QuoteNow press the "Set Text" button. It will try to set MaskEdit.Text to whatever is in the third Edit control (just leave the default 'ABCDEF' as it is) The text in the MaskEdit should now be 'ABCDEF' Does it do that? Without any problem. QuoteAt this point, if there is any text in the MaskEdit, select one (or more) character(s) and press Delete. The selected chars should be replaced with '_' Does is do that? Perfectly it did, though it reacted only once QuoteThen run these 2 scenario's 1 - EditMask = '' (empty mask) - run - type '1' in the control (just once) - close the program Code: [Select]user@ubuntu:~/maskedit$ ./project1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 1 OnChange Quote2 - EditMask = 'cccccc' - run - type '1' in the control (just once) - close the program Code: [Select]user@ubuntu:~/maskedit$ ./project1 TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = _____ OnChange TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = _____ TCustomMaskEdit.TextChanged: Disallowing the change TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1____) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1____ OnChange user@ubuntu:~/maskedit$ It seems perfect with a single character ---------------------------------------------------------------------- Bart · 2011-05-23 10:10 I'm running out of ideas right now. I cannot reproduce the problem, so I cannot dig deeper. Maybe Zeljan has some ideas? Bart ---------------------------------------------------------------------- gitspielen · 2011-05-23 17:32 You did a great job anyway. All my thumbs up for you! d^^b Is there any one in the development team who runs Lazarus under non- roman alphabetical environment, especially one of the Far East languages such as Chinese, Japanese or Korean? Though it seems to be few possibilities but an input method may unexpectedly interrupt keyboard messages during IsMasked state, and it is unable to pass the events correctly due to permission problem. So there can be a chance he/she may reproduce the bug, I reckon. Quote from: danthorpe on May 17, 2011, 08:43:37 pmI´m also having some problems in the MaskEdit component, running in the Ubuntu 11.04. I wonder if it has something to do with the GTK2 version (2.4.24 if I´m not mistaken) that comes with the OS. Anyone ? Look at the guy! The man also undergoes the same issue with me. So I wonder what his or her language environment is. Best regards. ---------------------------------------------------------------------- Bart · 2011-05-23 21:16 First of all TMaskEdit rejects all input that is not in the lower ASCII range, and since the LCL is native UTF-8, it will reject all multibyte characters. I have another question about the last experiment you did for me. After you press '1' there are numerous calls to TextChanged. Do these end spontaneously, or do they go on forever? Also I came up with yet another experiment. Change TMaskEdit.TextChanged to this: Code: [Select]procedure TCustomMaskEdit.TextChanged; { Purpose: to avoid messing up the control by   - cut/paste/clear via OS context menu     (we try to catch these messages and handle them,     but this is not garantueed to work)   - dragging selected text in the control with the mouse   If one of these happens, then the internal logic of cursorpositioning,   inserting characters is messed up.   So, we simply restore the text from our backup: FCurrenText } var S: String; begin   S := inherited Text;   Debugln('TCustomMaskEdit.TextChanged: IsMasked = ',DbgS(IsMasked),' FChangeAllowed = ',DbgS(FChangeAllowed),' Current Text = ',S);   if (not IsMasked) or FChangeAllowed then   begin     Inherited TextChanged;   end   else if (FCurrentText <> S) then //<-- changed this line for you   begin//Undo changes: restore with value of FCurrentText     //we do not call inherited TextChanged here, because the following SetInheritedText     //will trigger TextChanged with FChangeAllowed = True and inherited TextChanged is called then     Debugln('TCustomMaskEdit.TextChanged: Disallowing the change');     Debugln('  -> FCurrentText = ',FCurrentText,' S = ',S);     SetInheritedText(FCurrentText);     //Reset cursor to last known position     SetCursorPos;   end; end; From the previous experiment I gathered that in TextChanged somehow FChangeAllowed was False (it should not be) and it then calls SetInheritedText(FCurrentText), triggering the next TextChanged. In all these cases it seems that the Text in the control = FCurrentText. So maybe this tweak will prevent this? Bart ---------------------------------------------------------------------- gitspielen · 2011-05-23 22:19 Quote from: Bart on May 23, 2011, 11:16:41 pmFirst of all TMaskEdit rejects all input that is not in the lower ASCII range, and since the LCL is native UTF-8, it will reject all multibyte characters. I know the way it works, and probably window managers(Unity, in my case) simply won't execute input methods(iBus, for me) for ASCIIs, or if any, the methods won't treat typical ASCII-range letters so they'll just pass the code to applications. But if the latter is what really happen -- i.e. Unity executes iBus despite of the key code and iBus determines whether the input within ASCII range, after then passes it to our MaskEdit -- my guess will come into some "possibility". QuoteI have another question about the last experiment you did for me. After you press '1' there are numerous calls to TextChanged. Do these end spontaneously, or do they go on forever? No. I'd say you misunderstood the status. I said that I typed "12345", not simply "1". So numerous TextChanged events are called. If you want to know what happened as I just put "1", carefully look at the terminal window on the screenshot file, s2.gif. I'll be back with new experiment. ---------------------------------------------------------------------- gitspielen · 2011-05-23 23:36 Code: [Select]user@ubuntu:/media/home/src/lazarus$ ~/maskedit/project1 /// I pushed "1" TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = _____ OnChange TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = _____ TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.InsertChar: Ch = 1 CanInsertChar = True TCustomMaskedit.InsertChar: calling SeInheritedText(1____) TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = True Current Text = 1____ OnChange /// pushed "2" afterward TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1____ TCustomMaskEdit.TextChanged: IsMasked = True FChangeAllowed = False Current Text = 1____ Oops. I insist we should move down to the widgetset implementation layer to find out where the problem is. Two possibilities: 1. The system doesn't give KeyPress event calling signal to our application as a normal user.  2. The system does, but widgetset implementation can't interpret it correctly because of somewhat unknown issues. ---------------------------------------------------------------------- Bart · 2011-05-24 10:49 Before you do that (and file a bugreport) please test the following scenario: in OI: - MaskEdit1.EditMask = '';  //empty mask - MaskEdit1.Text := ''; //no text Build and run as normal user. Set focus to MaskEdit1 and type '12345' How many times do you see TCustomMaskEdit.KeyPress debug output in the console? If you don't see the KeyPress debug output for one or more of the keys, run the app as root and repeat the process. Also, to determine if this is caused by changing text in TextChanged, do the following: in OI - MaskEdit1.EditMask = '';  //empty mask - MaskEdit1.Text = ''; //no text - MaskEdit1.CharCase = ecUpperCase; This will force the text in the control to be changed to uppercase, and this happens in TCustomEdit.TextChanged. Build and run as normal user. Set focus to MaskEdit1 and type 'abcde' (lower case) How many times do you see TCustomMaskEdit.KeyPress debug output in the console? If you don't see the KeyPress debug output for one or more of the keys, run the app as root and repeat the process. Bart ---------------------------------------------------------------------- gitspielen · 2011-05-25 06:03 Quote from: Bart on May 24, 2011, 12:49:49 pmBefore you do that (and file a bugreport) please test the following scenario: in OI: - MaskEdit1.EditMask = '';  //empty mask - MaskEdit1.Text := ''; //no text Build and run as normal user. Set focus to MaskEdit1 and type '12345' How many times do you see TCustomMaskEdit.KeyPress debug output in the console? Code: [Select]user@ubuntu:~/maskedit$ ./project1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 1 OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 12 OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 123 OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 1234 OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 12345 OnChange user@ubuntu:~/maskedit$ Huh? Weird. It's only one time. And sudo it: Code: [Select]user@ubuntu:~/maskedit$ sudo ./project1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = TCustomMaskdit.KeyPress: Key = 1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 1 OnChange TCustomMaskdit.KeyPress: Key = 2 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 12 OnChange TCustomMaskdit.KeyPress: Key = 3 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 123 OnChange TCustomMaskdit.KeyPress: Key = 4 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 1234 OnChange TCustomMaskdit.KeyPress: Key = 5 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = 12345 OnChange user@ubuntu:~/maskedit$ Exactly five times. QuoteAlso, to determine if this is caused by changing text in TextChanged, do the following: in OI - MaskEdit1.EditMask = '';  //empty mask - MaskEdit1.Text = ''; //no text - MaskEdit1.CharCase = ecUpperCase; This will force the text in the control to be changed to uppercase, and this happens in TCustomEdit.TextChanged. Build and run as normal user. Set focus to MaskEdit1 and type 'abcde' (lower case) How many times do you see TCustomMaskEdit.KeyPress debug output in the console? If you don't see the KeyPress debug output for one or more of the keys, run the app as root and repeat the process. Bart As a normal user: Code: [Select]user@ubuntu:~/maskedit$ ./project1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = TCustomMaskdit.KeyPress: Key = a TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = a TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = A OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = A OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = Ab TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = AB OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = AB OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABc TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABC OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABC OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCd TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCD OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCD OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCDe TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCDE OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCDE OnChange With root permission: Code: [Select]user@ubuntu:~/maskedit$ sudo ./project1 TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = TCustomMaskdit.KeyPress: Key = a TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = a TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = A OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = A OnChange TCustomMaskdit.KeyPress: Key = b TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = Ab TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = AB OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = AB OnChange TCustomMaskdit.KeyPress: Key = c TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABc TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABC OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABC OnChange TCustomMaskdit.KeyPress: Key = d TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCd TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCD OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCD OnChange TCustomMaskdit.KeyPress: Key = e TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCDe TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCDE OnChange OnChange TCustomMaskEdit.TextChanged: IsMasked = False FChangeAllowed = False Current Text = ABCDE OnChange user@ubuntu:~/maskedit$ Eh? ---------------------------------------------------------------------- Bart · 2011-05-25 08:57 OK, so you identified the problem. KeyPress is not called after the first Key anymore. See: problem for TEdit onKeyPress event in Ubuntu 11.04 topic also. I'll post it on Mantis. Bart ---------------------------------------------------------------------- Bart · 2011-05-25 09:18 Filed a bugreport on mantis: http://bugs.freepascal.org/view.php?id=19413. Can you please leave your comment on the bugtracker confirming the issue (otherwise it might be dismissed as "cannot reproduce"). (You need to have (or create) an account for this, don't let that hold you back please.) Bart ---------------------------------------------------------------------- Bart · 2011-05-25 09:32 @gitspielen: KeyPress is ultimately derived from TWinControl. Can you test OnKeyPress events from other TWinControl descendants (TForm springs to mind), and post the findings in the bugtracker? Bart ---------------------------------------------------------------------- Bart · 2011-11-20 22:15 @gitspielen: Ludo Brands reproted this issue being gone after updating his Ubuntu (see the bugtracker). Do you still have this issue? Bart