Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.text tag configure -selectbackground

10 views
Skip to first unread message

Francois Vogel

unread,
Nov 10, 2006, 6:56:38 AM11/10/06
to
Hi all,

I have a text widget in which I highlight parts of the text with "mytag"
tags having a background color.

When I select this tagged text I can't see the background color any more
because the sel tag is raised above mytag.

I think this is normal since the text widget -selectbackground option
seems to take precedence.

However, I would like to see whether my selected text is tagged or not,
i.e. see it change color when selected.

My problem is therefore that .text tag configure tagname
-selectbackground does not exist.

Any suggestion perhaps?

.text tag raise is not an option here, since I want the selected tagged
text be of different background color than non selected tagged text.

Sample code:

text .t
pack .t
.t configure -selectbackground lightgreen
.t insert 1.0 "Hello world, this is my text"
.t tag add mytag 1.6 1.11
.t tag add mytag 1.18 1.23
.t tag configure mytag -background yellow
.t tag add sel 1.3 1.20
.t tag raise sel mytag

I would like to see a different background color for text tagged with
both sel and mytag than the background color used for text tagged with
mytag alone.


Thanks,
Francois

Bryan Oakley

unread,
Nov 10, 2006, 10:07:25 AM11/10/06
to
Francois Vogel wrote:
> Hi all,
>
> I have a text widget in which I highlight parts of the text with "mytag"
> tags having a background color.
>
> When I select this tagged text I can't see the background color any more
> because the sel tag is raised above mytag.
>
> I think this is normal since the text widget -selectbackground option
> seems to take precedence.
>
> However, I would like to see whether my selected text is tagged or not,
> i.e. see it change color when selected.
>
> My problem is therefore that .text tag configure tagname
> -selectbackground does not exist.
>
> Any suggestion perhaps?

Create a third tag that has the attributes you desire. Add an execution
trace on the widget command, and whenever the selection changes (eg:
when the execution trace fires on a "tag add sel"), add your tag to the
selected range.

Not a perfect solution, but maybe it's good enough.

Francois Vogel

unread,
Nov 15, 2006, 2:43:59 PM11/15/06
to
Bryan Oakley a écrit :

> Create a third tag that has the attributes you desire. Add an execution
> trace on the widget command, and whenever the selection changes (eg:
> when the execution trace fires on a "tag add sel"), add your tag to the
> selected range.
>
> Not a perfect solution, but maybe it's good enough.

Thanks a lot for having thought about my problem, Bryan.

Actually I started implementation of your suggestion, but I didn't go
very far. In my application there are many tags with different
background colors, and any text range might be tagged with more than one
tag at the same time. The highest priority tag then takes precedence, of
course, but it starts to be sort of a nightmare to make this sleep with
the scheme you suggest.

For the time being, I just gave up, I think it's not worth the effort.

Another option would be to have -selectbackground option for tags. I
guess a TIP would be needed for that.

Anyway, thanks again,
Francois

EKB

unread,
Nov 16, 2006, 7:03:17 AM11/16/06
to

Hi,

That's too bad it was frustrating. I was in a similar situation, and
the solution I found worked OK. I raised my own personal selection tag
to the top, and then cleared it out when done. I'll paste the main proc
below. If you want the whole search dialog file, I'd be happy to send
it to you. (I'd post it on the Tcler's Wiki, but there's some seriously
application-specific code in it that I would want to clear out before
doing that.)

Eric

======= CODE SNIPPET

proc searchForText {string} {
variable length

# If there is text selected, place the cursor at the start &
remove the selection
if {[$ManageFiles::activeWindow tag ranges sel] != ""} {
set selrange [$ManageFiles::activeWindow tag nextrange sel 1.0]
$ManageFiles::activeWindow mark set insert [lindex $selrange 0]
$ManageFiles::activeWindow tag remove sel 1.0 end
}

set cur [$ManageFiles::activeWindow index insert]
set w $ManageFiles::activeWindow

# Search from insert mark onward (or backward), return the
position at the end of the text

if {$string == ""} {
return
}

set searchcmd "$w search -count searchdlg::length"
if {!$::searchdlg::matchcase} {
set searchcmd "$searchcmd -nocase "
}
if {!$::searchdlg::forward} {
set searchcmd "$searchcmd -backwards "
}
set searchcmd "$searchcmd -- $string \$cur"
if {$::searchdlg::forward} {
set searchcmd "$searchcmd end"
} else {
set searchcmd "$searchcmd 1.0"
}

set cur [eval $searchcmd]
if {$cur == ""} {
tk_messageBox -icon info -title [mc "Search"] -message [mc
"Text not found"]
return
}

# Tag text as "search selected"
$w tag remove searchSel 1.0 end
$w tag add searchSel $cur "$cur + $length char"
$w yview -pickplace $cur

# Update the insert cursor
if {$::searchdlg::forward} {
$ManageFiles::activeWindow mark set insert [$w index "$cur +
$length char"]
} else {
$ManageFiles::activeWindow mark set insert $cur
}
$ManageFiles::activeWindow see [$ManageFiles::activeWindow index
insert]

}

==== END CODE SNIPPET

Francois Vogel

unread,
Nov 17, 2006, 6:51:56 AM11/17/06
to
> the solution I found worked OK. I raised my own personal selection tag
> to the top, and then cleared it out when done.


Yes of course, that's an idea. Thanks for your code, anyway.

But I think you didn't fully get my point.

Suppose I have the following text in my text widget (one liner):
"Hello world, what a wonderful world"

Suppose further that the word "world" (two occurrences) is tagged with
mytag1, and that this tag has -background red.
And suppose that the word "what" is tagged with mytag2, having
-background yellow.

Now suppose that I select this whole line with the mouse. What happens?

Well, if the sel tag has a higher priority than mytag1 and mytag2, I
won't see any more that the words "world" and "what" are tagged. And I
won't see neither that they are tagged with two different tags. The
entire line will be displayed as a uniform sel tag with the same
background color.

What I would like to have is that when I select that line, then the
words "world" and "what" change background color so that I can see that
they are both selected and tagged with mytag1 or mytag2, and I want to
be able to see visually that there are two different mytag tags in that
selected line.

So wouldn't it work just by lowering the sel tag priority below the
other tags? Yes, almost. But only almost, because in that case I would
see that the words are tagged with mytag1 or mytag2, but I would not see
that they are selected.

Therefore, I would like to have for instance some automatic processing
that would darken the background color of all the tags having a
-background option in the selected part of the text. Hmm, no, this would
also darken the background color of tagged text outside the selection.
So better create darkmytag1 and darkmytag2 and apply them at the tagged
and selected positions. And revert to normal when the sel tag is gone.
But how can I know that something has just been selected, or that the
selection changed, or that there is no selection any longer in the text
widget? And consider that I spoke here about only two mytags, but in
fact I have more than this...

That's why I said it's not worth the effort. Too complicated for too
small a ROI.

The right thing to do would be to use a -activebackground option for all
mytag tags.

I'm just looking for another option since the latter does not exist.

Francois

0 new messages