I have been working on my new version of my customRect widget the days.
My goal was to set the label text of the widget on the canvas like in a text field right or left aligned. Also, i wanted to have the ability to set the label to top or bottom as well.
Preferably via the property inspector, since I don't have to script the position of the label every time.
Unfortunately the default editor com.livecode.pi.textalign which is used for this only allows center, right and left justified. So every customRect label can only be adjusted via script?
Nope, is out of question for me lazy dog. I rather build my own *.pi -function so that I don't have to set the label by script in the future.
So let's take a closer look at the whole construct.
I have created a small example widget which shows me a label and I can test the function of the alignment.
You can find the structure of the widget here: https://github.com/Hoerwi/openXTalk-custom.pi
How the widget is built doesn't matter. It is mainly about how the function of the label-align is built up.
But let's have a look how it looks like in the standard:
With the metadata textalign.editor the entry in the Property-Inspector is displayed with com.livecode.pi.textalign. As you can see only Left - Center - Right is possible. Offsetting to top and bottom is not possible.
If we search now in the program directory of Livecode for "com.livecode.pi" we find some entries under \Toolset\palettes\inspector\editors
There you can also find two files for the com.livecode.pi.textalign
This is a script and a Stack file.
If we now look at the stack file we see that just the segmented widget in there. Nothing more. No script, only the appearance defined by the Property-Inspectors. The function of the Align tool is described in the appropriate script.
What annoys me personally about it is that it, like almost all *.pi -functions is hardly or not at all documented or commented.
If you look at it this way, the structure is so simple that you can quickly assemble a new *.pi- function yourself.
We use the standard Align tool as a template. But we do not change it, because other functions of the IDE depend on this pi.
Let's first build the appropriate stack
So we open a default stack in LC / oXT and drag in the segmented widget on it.
We make the settings as follow in the propertys of this widget:
Under Basic:
[*]Number of Segments: 5
[*]Segment names and labels on left / center / right / top / bottom
[*]minimum segment widths = 30
[*]Clicking toggels highlight = true
[*]Show border = true
[*]Display style = Icons
[*]Corner radius = 5
Under Icons we set the Segment Icons and Hilited Segment icons.
Here I chose the Chevron Signs for the direction of the alignment, for Center just the black Circle.
Under Position we set the Width to 150 and Height to 21
At this point, the developers got a little mean and put the Segmentcontrol widget group in a Group "template"!
Of course we do this as well.
!!! If this is not done, nothing will be displayed in the Property Inspector!!!
Now we can save the stack with the name custom.pi.btnalign to \Toolset\palettes\inspector\editors
With this we already have the representation of our Property-Inspector entry.
Now we only need the appropriate script for it.
We have to write this as a separate script. Writing the whole thing directly into the custom.pi.btnalign stack unfortunately doesn't work.
But here we get really lazy and copy the content of the file com.livecode.pi.textalign.behavior.livecodescript into a new file and save it under the name custom.pi.btnalign.behavior.livecodescript
I recommend to do this in a separate editor like Notepad++ or Atom
Now we need to make a few small adjustments in the new script at the following places:
Code: Select all
1: script "custom.pi.btnalign.behavior"
...
3: set the editorMinWidth from me to 150
4: set the editorMaxWidth from me to 150
...
28: set the width of widget 1 of me to 150
...
Once everything is saved, we need to restart Livecode or openXTalk!
Now we can prepare and test this in the example widget
I will now only go into the required entries in the LCB script.
The full script for the test widget can be found on GitHub.
First we have to define a variable for the mBtnAlign:
Code: Select all
private variable mBtnAlign as String
with the metadata btnalign.editor we call our new *.pi.*- element we just created
Code: Select all
property "btnalign" get mBtnAlign set setBtnAlign
metadata btnalign.editor is "custom.pi.btnalign“
metadata btnalign.section is "Basic"
Code: Select all
public handler setBtnAlign(in pBtnAlign as String) returns nothing
put pBtnAlign into mBtnAlign
redraw all
end handler
Code: Select all
Public handler onPaint()
…
if mBtnAlign is "right" then
fill text mLabel at right of tRectangle on this canvas
else if mBtnAlign is "left" then
fill text mLabel at left of tRectangle on this canvas
else if mBtnAlign is "top" then
fill text mLabel at top of tRectangle on this canvas
else if mBtnAlign is "bottom" then
fill text mLabel at bottom of tRectangle on this canvas
else
fill text mLabel at center of tRectangle on this canvas
end if
…
end handler
Oh I forgot to mention: the widget created with this "library" can only be used by you locally when scripting. If you pass the widget on, it will not work for others.
This only affects the widget itself, not the standalones!