GUI elements from a function not being displayed in Red language - function

I am using following code to try to get GUI elements from a function:
mypanelfn: func[] [
collect[
repeat i 10 [
print append copy "i in loop: " i
keep [t: text] keep append copy "message: " i
keep [field "entry"
button "Click" [t/text: "clicked"] return]]]]
view [
do [mypanelfn]]
There are no error messages and loop go on all right and a windows is also displayed. But this is only a small empty windows without any text, fields or buttons.
What is wrong with this code?
Edit: putting probe before collect shows (I have added line breaks for clarity):
[t: text "message: 1" field "entry" button "Click" [t/text: "clicked"] return
t: text "message: 2" field "entry" button "Click" [t/text: "clicked"] return
t: text "message: 3" field "entry" button "Click" [t/text: "clicked"] return
t: text "message: 4" field "entry" button "Click" [t/text: "clicked"] return
t: text "message: 5" field "entry" button "Click" [t/text: "clicked"] return

This method does not require setting any variables—it works by containing each iteration of faces within a common parent (panel):
view collect [
keep [below space 0x0]
repeat i 10 [
keep compose/deep [
panel [
origin 0x0
text (rejoin ["Message Number: " i])
field "entry"
button "Click" [face/parent/pane/1/text: "clicked"]
]
]
]
]
face/parent is the panel face whose first child (pane/1) is the text box (origin does not create a face).

You don't necessarily need the function there, however:
view mypanelfn
Works.
Note: the equivalent code in Rebol requires layout: view layout mypanelfn
The reason this happens is because view processes blocks! (anything inside []). So you don't have to do it.
In general, it's better to think of Red as a functional, message passing language. Everything is an expression, in contrast to imperative languages with procedures and statements.

once again; you need unique names for elements you want to address. Here a solution with reduce instead of compose
mypanelfn: func[] [
collect[
repeat i 10 [
tname: to-word rejoin ['t i]
print append copy "i in loop: " i
keep reduce [to-set-word tname 'text] keep append copy "message: " i
keep reduce [
'field "entry" 'button "Click" reduce [
to-set-path reduce [
tname 'text ]
"clicked" ]
'return ] ] ] ]
I recommend that you use the commands in the console to see what they do. E.g.
rejoin ['t i] creates a string "t1"with t and the (reduced/get-)value of i.
to-word changes that to a Red(bol) word t1
to-setword tname creates a set-word t1:
to-set-path reduce [tname 'text ]creates a set-path t1/text:

this works for me in rebol/view:
lay: mypanelfn
insert head lay 'across
view layout lay
I think while you're learning this stuff you need to look at the generated VID code to check that there are no problems before trying to View it.

Related

Is there some way to use html in qml as I need?

I want to place my text so i can get something like:
"SomeText1
SomeText2 One ...
two ...
three ..."
And I need to customize each of sometexts and 1,2,3 separately.
So I use the code
Label{
...
textFormat: Text.RichText
text: <html><b><div style='font-size: 12pt;'> SomeText1</div></b></html>+
<html><i><div style='font-size: 12pt;'> SomeText2</div></i></html>+
(here must be a function which back some big text)
...
}
This code works great except one thing... It acts like ".simplefield()" and just "eat" all 2+ spaces and doesn't allow to use "\n". What can I do to change this?
I found the solution of my problem. When I type:
"1
2
3"
I see "1 2 3", but actually it is "1\n2\n3". My html-ed label doesn't react to "\n". So I just added a function in my .cpp, which replaced "\n" with "br". Here is it
str.replace("\n","<br>");

How to extract text as well as hyperlink text in scrapy?

I want to extract from following html code:
<li>
<a test="test" href="abc.html" id="11">Click Here</a>
"for further reference"
</li>
I'm trying to do with following extract command
response.css("article div#section-2 li::text").extract()
But it is giving only "for further reference" line
And Expected output is "Click Here for further reference" as a one string.
How to do this?
How to modify this to do the same if following patterns are there:
Text Hyperlink Text
Hyperlink Text
Text Hyperlink
There are at least a couple of ways to do that:
Let's first build a test selector that mimics your response:
>>> response = scrapy.Selector(text="""<li>
... <a test="test" href="abc.html" id="11">Click Here</a>
... "for further reference"
... </li>""")
First option, with a minor change to your CSS selector.
Look at all text descendants, not only text children (notice the space between li and ::text pseudo element):
# this is your CSS select,
# which only gives direct children text of your selected LI
>>> response.css("li::text").extract()
[u'\n ', u'\n "for further reference"\n']
# notice the extra space
# here
# |
# v
>>> response.css("li ::text").extract()
[u'\n ', u'Click Here', u'\n "for further reference"\n']
# using Python's join() to concatenate and build the full sentence
>>> ''.join(response.css("li ::text").extract())
u'\n Click Here\n "for further reference"\n'
Another option is to chain your .css() call with XPath 1.0 string() or normalize-space() inside a subsequent .xpath() call:
>>> response.css("li").xpath('string()').extract()
[u'\n Click Here\n "for further reference"\n']
>>> response.css("li").xpath('normalize-space()').extract()
[u'Click Here "for further reference"']
# calling `.extract_first()` gives you a string directly, not a list of 1 string
>>> response.css("li").xpath('normalize-space()').extract_first()
u'Click Here "for further reference"'
I use xpath if that is the case the selector will be:
response.xpath('//article/div[#id="section-2"]/li/a/text()').extract()#this will give you text of mentioned hyper link >> "Click Here"
response.xpath('//article/div[#id="section-2"]/li/a/#href').extract()#this will give you link of mentioned hyper link >> "abc.html"
response.xpath('//article/div[#id="section-2"]/li/text()').extract()#this will give you text of li >> "for further reference"

Select multiple TCL/TK text widgets at once

I am trying to create a table using TCL/TK and am building it without using the Tktable widget.
I am using a canvas to build the table from ground up using the approach provided in:
simple Tktable
I have a whole bunch of customizations which (so far) I feel is easier if I build the table in a canvas and am hence using this approach instead of Tktable.
My table will be filled with read only text widgets, editable text widgets and some drop down menus.
Here is my question:
1) How can I allow a user to select multiple text widgets at the same time and retrieve the selection? Such as say, the user selects an entire row/col etc.
Please see the simplified code below:
package require Tk
proc makeWindow {} {
set toplevelWindow .gui
destroy $toplevelWindow
## Make the toplevel window
toplevel $toplevelWindow
wm title $toplevelWindow "Test case 1"
wm minsize $toplevelWindow 200 200
set pathName $toplevelWindow.testMultiSelection
## Create the canvas where I build the table
destroy $pathName
frame $pathName
set col 0
for {set i 0} {$i < 4} {incr i} {
set w "$pathName\_$i"
destroy $w
text $w -width 9 -height 1 -state normal
$w insert end $i
$w configure -state disabled
grid $w -row $i -column $col -sticky ew
}
grid config $toplevelWindow.testMultiSelection -column 0 -row 0 -sticky w
}
makeWindow
In the above example, I expect to select a few numbers (basically something like the ctrl+select) and somehow store the selection somewhere and retrieve them for later use.
Please let me know if the condensed test-case above is not clear.
EDIT 1
Forgot to add that I know how to retrieve single text widget selections using the selection get command.
Selections are restricted to a single widget at a time by default. If you turn off exporting of the selection at the widget level for each widget (configure -exportselection 0) then you can take over the management of that. However, some platforms also don't display the selection unless the widget has focus (due to platform GUI rules) so you might well also need to manage a text tag to apply the look of the selection. (The sel tag is the selection; it's managed specially, but you can copy its look fairly easily.)
Remember that you can embed widgets inside both canvases and text widgets (if the sub-widgets have names that make them child of the container). The window subcommand and/or item type is what you're looking for there if you're going that route.
The other big thing is managing the clipboard. You'll want to take that over explicitly. The usual command for that is clipboard, though the selection command can also be used (it gives access to the lower-level parts of the clipboard/selection mechanism; the clipboard is the CLIPBOARD selection).
Understand this, what you're doing is complicated and likely to take quite a bit of time and effort. Ask yourself whether your project is really justified in this level of complexity.
Gist of this idea is to use text tags to mark the selected text so you not only get the appearance of selected text you also mark that text as "selected". Then your copy operation merely iterates over all text widgets and gets the contents if it possesses the tag "selected". You can unselect selected text as you get the contents by removing the tag or as I have done below where the button command replaces itself with its own undo command.
So the flow is click on the column button, all text widgets in that column have their contents tagged as selected (selected text is white background & black foreground and so looks selected ) click on a popup menu or a menu button that accesses all selected items and performs operation on the contents then click on column button to unselect the column. all selected text widgets have the selected tag removed with restores the default look of the text.
Assuming you click the top of the column ( I assume its a button ) and you know the text widgets in that column you can tag the text in each text widget with a tag to indicate it is selected. Then to copy you can iterate over all text widgets and pull the content in each text widget that is tagged. To unselect iterate over all text widgets and untag the content. In the code below Im assuming that I have the following functions : getAllTextWindowPaths - gives me list of all textwidgets in the canvas, getAllTextPathsInColumn colnumber - gives me list of all textwidgets in canvas in column $colnumber.
# configure tags for all your text windows
foreach t [ getAllTextWindowPaths ] {
$t tag configure selected -background white -foreground black
}
# configure the column select button
button .canvas.button.column1 -text {1} -command [ list clickColumn .canvas.button.column1 1 ]
proc selectText { textpath { select 1 } } {
if { $select == 1 } {
$textpath tag add 1.0 end selected
} else {
$textpath tag remove 1.0 end selected
}
}
proc clickColumn { colpath colnum } {
# now call this click handler for for your column of textwidgets
# you can make same for a row
foreach t [getTextPathsInColumn $colnum ] {
selectText $t 1
}
$colpath configure -command [list unclickColumn $colpath $colnum ]
}
proc unclickColumn { colpath colnum } {
# now call this click handler for for your column of textwidgets
# you can make same for a row
foreach t [getTextPathsInColumn $colnum ] {
selectText $t 1
}
$colpath configure -command [list clickColumn $colpath $colnum ]
}
proc getSelectedContents { } {
# im adding to a list all the seleted contents but you can arrange
# it anyway you want
set retval {}
foreach t [getAllTextWindowPaths ] {
if { "selected" in [$t tag names 1.0 ] } {
lappend retval [ $t get 1.0 end ]
}
}
# optional call selectText $t 0 here foreach selected widget
# to clear selection or handle the column click handler again
return $retval
}

Exiting a block enclosed in curly braces in Sublime

Say if I have the following code in Sublime:
if (condition) {
// code
}
When my cursor is at the end of // code, I would like to set a key bind (e.g. Tab) that will exit the if-statement block and move it to the end of }. Thanks.
The BracketHighlighter plugin can provide this functionality natively... sort of. In its example shortcuts file, Example.sublime-keymap, there is a "Go to Right Bracket" example key binding:
// Go to right bracket
{
"keys": ["ctrl+alt+super+down"],
"command": "bh_key",
"args":
{
"lines" : true,
"plugin":
{
"type": ["__all__"],
"command": "bh_modules.bracketselect",
"args": {"select": "right"}
}
}
},
The only problem is that the called bracketselect command moves the cursor to the left side of the right bracket, requiring another keypress to fully escape from the block. I don't think that's what you want.
Worry not! Thankfully, BracketHighlighter provides a very intuitive plugin API, and I found that I could modify the bracketselect plugin to create a command that would escape from a bracket-enclosed block—basically the same as bracketselect, but it moves the cursor to the right side of the closing bracket rather than the left, and doesn't need any extra arguments.
You'll first need to install BracketHighlighter if you haven't yet.
Next, save blockescape.py (see below if the link ever dies) to
Preferences -> Browse Packages... -> BracketHighlighter/bh_modules/blockescape.py
Then, add this entry to the top of your user key bindings (Preferences -> Key Bindings — User):
{
"keys": ["tab"],
"command": "bh_key",
"args":
{
"lines" : true,
"plugin":
{
"type": ["__all__"],
"command": "bh_modules.blockescape"
}
}
},
I wouldn't recommend using tab as your trigger key, because tab has an important role already with expansions. Of course, you could define a special context in which to use tab, but that is up to you.
In case Github is ever down, here's the plugin code:
import bh_plugin
import sublime
DEFAULT_TAGS = ["cfml", "html", "angle"]
class BlockEscape(bh_plugin.BracketPluginCommand):
def run(self, edit, name, tags=DEFAULT_TAGS):
current_left, current_right = self.selection[0].begin(), self.selection[0].end()
left, right = self.left, self.right
first, last = left.end, right.begin
if left.end != right.end:
if name in tags and left.size() > 1:
first, last = right.begin + 1, right.begin + 1
if first == current_left and last == current_right:
first, last = right.end, right.end
else:
first, last = right.begin, right.begin
if first == current_left and last == current_right:
first, last = right.end, right.end
else:
# There is no second bracket, so just select the first
if name in tags and left.size() > 1:
first, last = left.begin + 1, left.begin + 1
else:
first, last = right.end, right.end
if first == current_left and last == current_right:
first, last = right.end, right.end
self.selection = [sublime.Region(first+1, last+1)]
def plugin():
return BlockEscape
Since I more or less hacked the plugin together, it might not work properly. In that case, feel free to edit it yourself or leave a comment on the Gist page.
You can add the $0 in a snippet to tab to that location:
<snippet>
<description>If Condition</description>
<content><![CDATA[if (${1:/* condition */}){
${2:/* code */}
}${0}]]></content>
<tabTrigger>if</tabTrigger>
<scope>source.c</scope>
</snippet>

Labels in xtable captions for crossreferencing in lyx

I am using pgfsweave with Lyx 1.6.8 and xtable.
providing table captions by
<<result=tex>>=
print(xtable(<dataframe>,caption="Here is my caption"))
#.
How can I insert a label into the table caption that I can crossreference in the text from the lyx>Insert>Cross-reference menu?
I have tried to insert>float>table and inserted
print(xtable(<dataframe>,floating=FALSE)) and
"Here is my caption" in the Table caption inner frame
but this results in (literally):
[float Table:
<...Table ...>
[Table 2: "Here is my caption" ] ]
Even a barefoot workaround to crossreference
<<result=tex>>=
print(xtable(<dataframe>,caption="Here is my caption",label = "tab:one"))
#
from an ERT-box would help.
SOLVED:
Just use TWO arguments in the xtable-function call in the R-code:
xtable(<dataframe>
, caption = "My caption\\label{tab:MyTable1}"
,label="tab:MyTable1")
The \\label{tab:MyTable1} inside the caption is changed to
\label{tab:MyTable1} by R and then interpreted by LaTeX.
The argument label="tab:MyTable1" is ignored by R and therefore at your disposal to trick Lyx into allowing for crossreferencing to the label table label.
Use Insert>label to insert the label "tab:MyTable1" (excluding the quotes) here.