Text Area Margin

Hi everyone,

I have the following code which worked a few years back when I last used Xojo.
It allowed me to set a margin in a text area, basically giving it some padding.

Unfortunately it now no longer works using the latest Xojo on an intel macOS 11 system.

Does anyone know if it is the declares which need updating?

// CREATE A STRUCTURE IN THE WINDOW CALLED "NSSize".
// CREATE 2 ENTRIES IN THE STRUCTURE:   "width As Single", and "height As Single"


// INSERT INTO THE TEXT AREA'S OPEN EVENT

Dim margin As NSSize
margin.width = 15
margin.height = 15
  
Declare Function documentView Lib "Cocoa" Selector "documentView" (obj_id As Integer) As Ptr
Dim ref As Ptr = documentView(Me.Handle)
  
Declare Sub setTextContainerInset Lib "Cocoa" Selector "setTextContainerInset:" (obj_id As Ptr, value As NSSize)
setTextContainerInset(ref, margin)

Thank you all in advance.

Change your NSSize to use double’s instead of singles, OR CGFloat’s

2 Likes

Thanks Graham, I will try that tonight.
Much appreciated!

The following is my attempt at implementing a cross-platform compatible method to achieve this.

The code for macOS and Windows works fine, but I’m struggling with the implementation for Linux. I believe I’m using the right GTK calls and the correct library in my declares, but something isn’t working, and the reason currently eludes me. Perhaps I need to do something with the TextArea handles before passing them to the gtk calls? I’m hoping that perhaps someone who is more well versed with GTK calls (such as e.g. @Ulrich_Bogun :-)) can sanity check this and tell me what I’m doing wrong.

Structure NSSize
  width as double
  height as double
End Structure

Public Sub SetMargin(extends t as DesktopTextArea, hMargin as integer, vMargin as integer = 0)
  // hMargin: horizontal margin, left and right side
  // vMargin: vertical margin, top and bottom
  
  #If TargetMacOS
    
    var margin As NSSize
    margin.width = hMargin
    margin.height = vMargin
    
    Declare Function documentView Lib "Cocoa" Selector "documentView" (obj_id As Integer) As Ptr
    var ref As Ptr = documentView(integer(t.Handle))
    
    Declare Sub setTextContainerInset Lib "Cocoa" Selector "setTextContainerInset:" (obj_id As Ptr, value As NSSize)
    setTextContainerInset(ref, margin)
    
    
  #elseif TargetWindows
    
    const EC_LEFTMARGIN = 1
    const EC_RIGHTMARGIN = 2
    const EM_SETMARGINS = &hD3
    var lMargin as integer = hMargin
    var rMargin as integer = 2^16 * hMargin
    #Pragma Unused vMargin
    
    Declare Sub SendMessage Lib "user32" Alias "SendMessageW" (hwnd As ptr, msg As Integer, wparam As Integer, lparam as Integer)
    SendMessage(t.Handle, EM_SETMARGINS, EC_LEFTMARGIN + EC_RIGHTMARGIN, lMargin + rMargin)
    
    
  #elseif TargetLinux
    
    Declare Sub gtk_text_view_set_left_margin Lib "gtk-3.so" (ref As ptr, margin as integer)
    gtk_text_view_set_left_margin(t.Handle, hmargin)
    
    Declare Sub gtk_text_view_set_right_margin Lib "gtk-3.so" (ref As ptr, margin as integer)
    gtk_text_view_set_right_margin(t.Handle, hmargin)
    
    Declare Sub gtk_text_view_set_top_margin Lib "gtk-3.so" (ref As ptr, margin as integer)
    gtk_text_view_set_top_margin(t.Handle, vmargin)
    
    Declare Sub gtk_text_view_set_bottom_margin Lib "gtk-3.so" (ref As ptr, margin as integer)
    gtk_text_view_set_bottom_margin(t.Handle, vmargin)
    
  #EndIf
  
End Sub

Hi Roger,
the first could-be-issue I see is that margin for the gtk declares should be an int32. I will check in my code and return to you later …

Thanks, @Ulrich_Bogun.

I changed my code to use int32 instead, but that doesn’t seem to make it work either. Must be something else.

Roger

Yes indeed. Docs say

Tags in the buffer may override the default.

Might be possible you have to set buffer properties instead, or that you have to provide a clear buffer. If a try to assign a left margin to a TextArea, property will revert to 0.

Thanks! Not sure how to do that, but I’ll look into it.

Well, no dice so far. I can’t seem to even get a valid reference to the buffer.

var ref as ptr = t.Handle

Declare Function gtk_text_view_get_buffer Lib "gtk-3.so" (ref As ptr) as ptr
var buf as Ptr = gtk_text_view_get_buffer(ref)

buf comes back as &00000000, regardless of whether I call this in the Opening event of the DesktopTextArea of some time after. I must be missing something.

Yes, that’s what I found too. Even creating an own textbuffer and assigning it to the widget will return nil.
I have a bug in my gtype implementation, so I cannot check reliably if what we got is indeed the handle to a textView and not in fact the enclosing scrollView. But if it were so, I would think that textView declares cast on it should result in exceptions.

Where do we go from here?