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)
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
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.
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.