Created and tested in Gambas 2. - Ubuntu
UPDATE: I had discovered since I wrote this, that a CTRL & Enter will insert a line space without the <p> </p>, so this is unnecessary. I am leaving this here in case someone is searching how to substitute some other HTML code in the background.
One day I decided to create a small editor using TextEdit. I discovered that the line spacing was much more than I like. With word processors there is something called a Soft Return where you can move to the next line without a large space. To do this one uses SHIFT and RETURN. But the TextEdit control uses only inserts the <p> </p> html tags which creates an equivalent of a Hard Return and not the <br /> which is the HTML equivalent of a Soft Return. Below is a screenshot of both Hard and Soft Returns in the TextEdit program I wrote.
I could not insert the HTML tag I needed directly into the the TextEdit. I discovered the Textedit.Insert ("<br />") does exactly that. It just inserts the text <br /> into the window. When I looked at the actual HTML source within the TextEdit I found that the TextEdit had converted the HTML "<br />" to "<br />" So it was time to come up with a fix.
To do this I had to create a work around by inserting a pattern into the TextEdit and using code to work behind the scenes to replace the "<br />" pattern with the "<br />" HTML tag. First I created the TextSoftReturn procedure. Then I inserted following code to give the TextEdit a soft return.
Public Sub TextSoftReturn()
Dim txtTemp As String
Dim brTXT As String
Dim Skip As Integer
Dim ck As Integer
Dim TxtPos As Integer
TxtPos = TextEdit1.pos 'Save the current posision
brTXT = "<br />" 'HTML Pattern to search for inside TextEdit
Skip = Len(brTXT) + 1 'Get how much to skip when trimming out the "<br />" pattern.
TextEdit1.Insert("<br />") 'Now I will insert "<br />" which TextEdit converts to "<br />"
txtTemp = TextEdit1.Text 'Saving TextEdit content into a string variable
TextEdit1.Clear 'Delete all contents in TextEdit
ck = InStr(txtTemp, brTXT) - 1 'Find location of the pattern "<br />"
'Now to replace the "<br />" with the HTML tag <br /> and reinsert into TextEdit
extEdit1.Text = Mid(txtTemp, 1, ck) & "<br />" & Mid(txtTemp, ck + skip)
TextEdit1.Pos = TxtPos + 1 'Move to the new line within TextEdit
End
To make the above code work when it should I inserted the following code into the TextEdit1__KeyPress() procedure.
PUBLIC SUB TextEdit1__KeyPress()
DIM KeyCode AS Integer 'Variable to capture Key Presses
TRY KeyCode = Key.Code 'Capture Key Code
IF Key.Shift THEN 'If the Shift key is pressed
IF KeyCode = Key.Return THEN 'the same time the Return key is pressed
TextSoftReturn 'then call the TextSoftReturn procedure to insert a Soft Return
STOP EVENT 'Stop the Return Keypress Event. This prevents the paragraph <p> </p> HTML tags
'from being inserted.
RETURN 'Exit the KeyPress procedure now.
ENDIF
ENDIF
END
You may be wondering why I did not use the REPLACE$. The reason is the REPLACE$ replaces all occurrences and the code above only replaces the one time you hold the Shift key while pressing the Return key.