Formating parts of a string in TCL/ Tk tablelist cell - tcl

I wonder if it is possible to format part of a text string when using the (example)
.tbl cellconfigure $row,$col -text "ThisBoldArial AndThisAsSubscript"
command?
I do know about eg the -font option, but this sets the font of the whole cell. Can I somehow format different parts of the string different?

I don't think you can do it easily. Looking at the documentation, I can't see any way of indicating index ranges of cell text contents (which you'd need in order to apply a rendering variation to them). I guess you could work around it by embedding a text widget as the cell's renderer window with the -window cell option; the use of a text (or ctext) widget for this purpose is mentioned in passing in documentation of the -windowupdate cell option so it must be possible to fake it that way, but you'd need to figure out the details of how to make it happen right.
This example shows how to do window embedding, albeit with a frame or button instead of a text. You'll need to do some work to get a text widget in there (basically make it borderless, read only and not scrollable at all).
proc createButton {tbl row col w} {
set key [$tbl getkeys $row]
button $w -image openImg -highlightthickness 0 -takefocus 0 \
-command [list viewFile $tbl $key]
}
…
$tbl cellconfigure $row,$column -window createButton
Naturally, you'll want to do more work to make the embedded text widget render as you want. That's potentially its own special set of complexity…

Related

Docs Inserted Image always before all text

Making a simple app script that puts images and Text into a Google Doc separated by 2 Columns, for whatever reason, no matter the way I try it the images are always above the text (Inline) in the Doc, even though they should be layered (Inline),
//Replace QR Code
let qrText = editLocalBody.findText("{{qrCode}}");
let setImagePlace = qrText.getElement().asText().replaceText("{{qrCode}}", "");
let qrCodeImage = setImagePlace.getParent().asParagraph().insertInlineImage(0, qrCodeBlob);
From what I've seen this should insert an image wherever the text was previously located, but when it runs this it's always in the wrong spot, somehow above the text it was suppost to be in!
//Edit - To Show The Progression Of What Is Suppose To Happen And What Actually Happens:
I'm making QR Code badges for a propriety system that runs integrated tightly with Google, so I'm using appscript to get an entry from a google form containing an amount of badges (With relevent data) and autofill a Google Doc Accordingly.
// Loop Start
I fill my template with a text line that has key words in it I can select and replace later, with a keyword it can use to insert another this (This Part Works)
I first edit (findText("{{qrCode}}");) the QR Code, replacing (.replaceText) the keyword for it to nothing ("")
I then get the parent of the piece of code I ran above, which is a block of text (I think all the text in the Doc, I think this is where the issue lies, it puts it above the text because it's just one 'paragraph' or not multiple 'bodies' of text, if I could separate this I think it would work!) As a paragraph, and insert An Inline Image at Child Index (0, of the image ,qrCodeBlob)
I've debugged this script quite a bit, so I know It's that final line that inserting images fails, it sees all the text as 'one'.
// I want this (In Descending Order, each it's own full line):
Image
Text
Image
Text
//What It Gives Me (In Descending Order, each it's own full line):
Image
Image
Text
Text
let qrCodeImage = setImagePlace.getParent().asParagraph().insertInlineImage(0, qrCodeBlob);

Enable text copy from canvas in tcl

I want to enable copying of the text placed on canvas. Is it possible to do so?
I have placed text using:
.c.canvasName create text 100 90 $var -font {Courier -12} ...
where var contains a tcl tk matrix.
As Donal wrote, ctext.tcl gives many helpful hints on how to manage text items in canvases.
Note that it doesn't demonstrate copying text to the clipboard. Use the following code for a rudimentary clipboard copy function:
$c bind text <<Copy>> "textCopy $c"
...
proc textCopy {w} {
clipboard clear
clipboard append [selection get]
selection clear
}
clipboard clear empties the Tk clipboard, and clipboard append copies new text to it. On Windows you can then paste this text using the normal Ctrl+V.
selection get copies text from the current selection and throws an error if no text is selected. Use
catch {clipboard append [selection get]}
to suppress such errors.
selection clear unselects the selection.

How to add auto-complete Sublime Text 3

I would like to add custom auto-complete key bindings much like built-in:
Example: html+tab auto-completes the Doctype Block.
I tried adding html custom key binding: type c + o + l + tab to generate <div class="col-">
Preferences > Key Bindings > Default (OSX).sublime-keymap -- User
{"keys": ["c+o+l+tab"], "command": "insert_snippet", "args": {"contents": "<div class=\"col-$0\">"}},
However, two issues:
the new key binding overrides all other auto completes
the initial col or characters remains in front of the
generated tag. col<div class="col-">
What is the correct way to add this type of key binding?
The correct way to do something like this is to use either snippets or completions. Although there are some differences, generally speaking they both work the same way in the end, and which one you choose depends on how many such items you want to create and how complex you want them to be.
Using a snippet, you would select Tools > Developer > New Snippet... from the menu and fill out the snippet template, then save it as a sublime-snippet file in the location that Sublime defaults to (which is your User package).
For example, that might look like the following based on the example in your question:
<snippet>
<content><![CDATA[
<div class="col-$0">
]]></content>
<description>Insert DIV with column class</description>
<tabTrigger>col</tabTrigger>
<scope>text.html</scope>
</snippet>
Snippets are XML formatted, and everything between ![CDATA[ and ]] is inserted into the buffer (don't remove the CDATA even if you think you don't need it; Sublime will ignore the snippet if you do).
The tabTrigger specifies the text that you want to be the trigger for the snippet, the scope says what sort of files the snippet should trigger in, and the description will be displayed next to the snippet in the auto-completions panel.
In a snippet, the tabTrigger, scope and description are all optional. If you don't specify a tabTrigger you can only expand the snippet from the Command Palette or via the insert_snippet command (for example in a key binding). Without a scope the snippet applies everywhere, and without description it has no description in the panel.
If you have many such items that you want to add snippets for, you can also use completions instead. These are stored in JSON files with an extension of sublime-completions and should be saved in your User package (use Preferences > Browse Packages... if you don't know where that is.
An example of such a file would be:
{
"scope": "text.html",
"completions": [
{ "trigger": "col\tInsert DIV with column class", "contents": "<div class=\"col-$0\">" },
]
}
In this format, the trigger is always the text to trigger and the description (still optional) is separated from the trigger by a \t character in the trigger key.
In completions you only specify the scope once at the top instead of every time, but there are some functional differences between completions and snippets.
There can only be one snippet per sublime-snippet file, but a sublime-completions file can contain many completions in a single file; the completions key is an array so you can place more than one completion in the same file.
Completions are JSON, so contents that are multi line or contain JSON specific characters such as a " character are harder to enter; completions are better for shorter sequences while snippets are better for more complex things.
When autocomplete triggers, if there is a completion and a snippet that could be autocompleted, snipptets always "win" and are inserted, whereas completions cycle. That means that for example in this particular example you need to press Tab twice because col is also the name of a tag.
Snippets automatically appear in the command palette (when they apply) but completions do not. In the command palette, Snippets appear as commands like Snippet: Something, where Something is the description if it exists and the name of the file if it does not.
In either case, you can make the snippet/completion apply only in certain types of files by applying a scope; to determine the appropriate scope, position the cursor in a file at the appropriate place and select Tools > Developer > Show Scope Name...; the more of the displayed scope you use the more specific it becomes. Generally just the top level such as text.html is all that's needed unless you're doing something special.

Is there a way to have a global style for button in tcl?

So I have this type of button with this options.
button .but1 -text "Sample" -width 10 -height 2 -background $btnColor -activeforeground $actForegrd -state normal -activebackground $activbtnColor -relief flat -highlightthickness 0
is there a way to make a global variable with those options for button, I know about style configure, but in order to use style, I need to change my buttons to ttk::button and those don't have the same options as the regular button.
I try putting all the options in a string and pass it when creating a button but it doesn't work
example:
set style "-width 10 -height 2 -background $btnColor -activeforeground $actForegrd -state normal -activebackground $activbtnColor -relief flat -highlightthickness 0"
button .but1 $style
There's two basic possibilities.
1. Use expansion
What you tried is almost right. Try this:
set style "-width 10 -height 2 -background $btnColor -activeforeground $actForegrd -state normal -activebackground $activbtnColor -relief flat -highlightthickness 0"
button .but1 {*}$style
The {*} (which entered Tcl in 8.5) makes what follows it be expanded to many arguments.
2. Use the option "database"
Instead, you can put all those values into the option database using the option command:
option add *.Button.width 10
option add *.Button.height 2
option add *.Button.background $btnColor
option add *.Button.activeForeground $actForegrd
option add *.Button.state normal
option add *.Button.activeBackground $activbtnColor
option add *.Button.relief flat
option add *.Button.highlightThickness 10
## You can also pull the above from a separate file with:
# option readfile ~/thefile.opts
button .but1
Using the option database right and well is tricky, but it lets you set the defaults for many widgets at once (or a single one, or just widgets that are children of a particular window, or of a class of windows, or …) You'd be truly splitting the style from the code.
I wouldn't normally have the state set by this mechanism though, as that's more of a semantic option; if a button is going to be disabled or not really does matter to code (just as the callback to run when that happens is another one I'd not set via options).
It should be noted that the Ttk style/theme mechanism largely supersedes this, as it allows for much more substantial modifications and cleaner runtime switching between them. But there are a number of effects that are quite a bit harder to do that way; we still maintain the classic button, etc. for a reason.

Create a tiff with only text and no images from a postscript file with ghostscript

Is it possible to create a tiff file from a postscript-file (created from a pdf-document with readable text and images) into a tiff file without the images and only the text?
Like add a maxbuffer so images will be removed and only text remaining?
And if boxes and lines around text could be removed as well that would be awesome.
Best regards!
You can redefine the various 'image' operators so that they don't do anything:
/image {
type /dicttype eq not { % uses up argument, only one if dict form
pop pop pop pop % remove the arguments for the non-dictionary form.
} ifelse
} bind def
/imagemask {
type /dicttype eq not { % uses up argument, only one if dict form
pop pop pop pop % remove the arguments for the non-dictionary form.
} ifelse
} bind def
/colorimage {
type /integertype eq {
pop % multi
0 1 3 -1 roll {pop} for % one for each colour component
} {
pop pop pop
} ifelse
} bind def
Save that as a file, and add the file to your GS invocation.
You can remove linework similarly by redefining the stroke operator:
/stroke {
newpath
} bind def
rectstroke is harder, I suggest you read the PLRM if you need that one.
Possibly also the fill operator:
/fill {
newpath
} bind def
/eofill {
newpath
} bind def
Beware! Some text is not drawn using the text 'show' operators, but is constructed from linework, or drawn as images. These techniques will be defeated if you redefine the operators as shown above.
Note that the PDF interpreter often doesn't allow re-definition of operators, so you may first have to convert your PDF file to PostScript, using the ps2write device, then run the resulting file through GS to get a TIFF file.
gs -sDEVICE=bitrgbtags -o out.tags <myfile>
will create a ppm file with tags - tags label each pixel as text, vector, image etc.
Then you can use the C programs in ghostpdl/tools/GOT to process the image. It sounds like you want to write a new C program to to set each non text pixel to the background color or maybe just white, this is fairly straightforward with the example C programs in the GOT subdirectory as a guide (if you are a programmer). Then you would convert the ppm to tiff. Ken provided a different way of doing this that doesn't require pixel processing.