Page 1 of 1

Spaces instead of Tabs in a text area, did I miss a better way?

Posted: Monday 28th November 2022 9:26pm
by Cedron
Quite a while ago I went off on tabs in source code documents.

viewtopic.php?p=1500

viewtopic.php?p=1516

Mind you, they are a superb field delimiter to use for grid data, including on the clipboard.

I am currently writing a masterpiece, well, at least one of the most intricate Gambas programs I have written so far. I'll be seeking alpha testers soon, so stay tuned on that, please, but back to the subject. One of the features is the ability to edit text notes and documents. For this I am using a TextArea control. The Tab key inserts tabs. Makes sense, except I want it to insert spaces instead. I couldn't find a corresponding property so here is my solution and it works. I prefer four space tabbing so I hard coded it, feel free to change or improve. This also works nicely at the end of a line.

'=============================================================================
Public Sub TheDocumentationTA_KeyPress()

        If Key.Code = Key.Tab Then
           Dim theColumn As Integer = TheDocumentationTA.Column
           Dim theNextTabStop As Integer = (theColumn + 4) And Not 3 
           
           Dim theFill As String = String(theNextTabStop - theColumn, " ")
           
           TheDocumentationTA.Insert(theFill)

           Stop Event 
        Endif

End
'=============================================================================

Re: Spaces instead of Tabs in a text area, did I miss a better way?

Posted: Tuesday 29th November 2022 3:36pm
by cogier
Have you had a look at the Gambas TextEditor? It has a TabSize property. Or am I missing the point?

Re: Spaces instead of Tabs in a text area, did I miss a better way?

Posted: Tuesday 29th November 2022 4:58pm
by Cedron
Thanks.

Indeed I have considered it, and intend to use it for viewing/editing source code files for which it is aware. I'm trying to stay lightweight and make the code transformable onto other platforms so I don't want to employ anything that is too Gambas specific for the core functionality.

The TabIndent and TabSize were the properties I was looking for in TextArea.

I have since added Ctrl-S for saving. The event handler now looks like this:

'=============================================================================
Public Sub TheDocumentationTA_KeyPress()

        If Key.Code = Key.Tab Then
           Dim theColumn As Integer = TheDocumentationTA.Column
           Dim theNextTabStop As Integer = (theColumn + 4) And Not 3 
           
           Dim theFill As String = String(theNextTabStop - theColumn, " ")
           
           TheDocumentationTA.Insert(theFill)

           Stop Event
           Return 
        Endif

        If Key.Control Then
           If Key.Code = 83 Then   ' Asc("S") 
              MyDocuments.Save(MyDocParms)
              Stop Event
           Endif
        Endif

End
'=============================================================================

Re: Spaces instead of Tabs in a text area, did I miss a better way?

Posted: Monday 5th December 2022 8:59am
by thatbruce
Curious. What happens if you hold down the Tab key or CTRL-S for a while? Do you get multiple tabbing spaces or multiple saves?
regards
bruce

Re: Spaces instead of Tabs in a text area, did I miss a better way?

Posted: Monday 5th December 2022 3:05pm
by Cedron
thatbruce wrote: Monday 5th December 2022 8:59am Curious. What happens if you hold down the Tab key or CTRL-S for a while? Do you get multiple tabbing spaces or multiple saves?
Keys repeating, AFAIK, is a function of the keyboard. The routine doesn't care whether it is an original key or a repeat key.

My code is in a broken state right now as I am redoing the front end, so I can't test it immediately and you could whip up a test project as easily as I.

You raised a quandary for me though, I think without intending to. Did I mention I am writing a masterpiece? Well, part of this application is a simplified hypertext protocol, where the document in question might be one that I am editing locally and want saved, or a document I am view remotely and I want to save a local copy.

In the latter case, a repeat should check the host for a change in document and fetch it if changed. Maybe it is a very volatile source. Otherwise kick a message to the message display that the file is already saved locally.

In the former though, there are two actions happening. A backup is being made of the prior version, at possibly a different location, with a timestamp in its name. Then the local copy is saved and becomes the current one. Hence the stamp on the name serves as the end date of the validity of that version of that document. In a business setting, that might have legal ramifications, so the act of saving might not just be about saving changes, but saving a valid snapshot.

I have the original text in a string in an object, and the text from the TextArea being submitted, so I can easily compare to see if they have changed even if editing has occurred.

I think I will leave it as I have it roughed in. It seems to be a solution in search of a problem to try to prevent unnecessary duplicate saves. There's a lot to be said for leaving things as simple as possible. Security and foolproofing (not necessarily independent) come best after you got it working IMO. Trying to build in the security model as you go (other then conceptually and keeping it in mind) tends to bind the code in a way that makes innovation along the way (aka agile) more difficult.