chrstphrchvz / perl-tcl-ptk

Tcl::pTk - Interface to Tcl/Tk with Perl/Tk compatible syntax
https://metacpan.org/pod/Tcl::pTk
Other
8 stars 2 forks source link

ttk::treeview ttkStyleConfigure #8

Open KellyOBrian opened 4 years ago

KellyOBrian commented 4 years ago

Is it possible to configure the style of the treeview from within Tcl::pTk? In particular, I am trying to configure the header. Since I am missing any syntax example for that, I just started with configuring the entire widget. In the example below, I simply try to change the background of the entire treeview, but it seems that ttkStyleConfigure (documented in Tcl::pTk::Tile) does not produce any effect.

use Tcl::pTk;

my $mw = MainWindow->new();
my $int = $mw->interp;

my $tree = $mw->Scrolled('ttkTreeview',-columns => [qw/ id token/], -show => 'headings', -scrollbars => 'se' )->pack(-fill => 'both', -expand => 1);
$tree -> tagConfigure('title', -background=>'red');
$tree -> tagConfigure('oddrow', -background=>'white');
$tree -> tagConfigure('evenrow', -background=>'yellow');

my $style = $tree->cget(-style);
my $font = $tree->ttkStyleLookup( $style, -font);
$tree->ttkStyleConfigure($style, -background=>'gray'); #trying to mimic Tcl 'ttk::style configure Treeview -background red' Here must lay my problem in TCL->Tcl::pTk conversion...

## Code to insert the data nicely
foreach my $col (qw/ id token/){
    my $name = uc($col);
    $tree->heading($col, -command => [\&SortBy, $tree->Subwidget('scrolled'), $col, 0], -text => $name);
    my $len = $tree->fontMeasure($font, $name);
    $tree->column($col, -width, $len+110);
}

$tree->insert('', 'end', -values => ['1234', '5678'], -tags => 'oddrow');
$tree->insert('', 'end', -values => ['abcd', 'efgh'], -tags => 'evenrow');

my $but = $mw->ttkButton(
    -text => "Close", 
    -command => sub {
        $mw->destroy;
    }
)->pack();

$int->MainLoop;

sub SortBy{

}
1;

Also any idea on how to configure the header?

chrstphrchvz commented 4 years ago

Hi Kelly, your example code looks nearly complete.

By default the -style of a treeview (and other Ttk widgets) is undefined ({}; I'm not sure if or where Tcl/Tk documents this), so $tree->cget(-style) will not return anything at first. And ttk::style lookup $style -font will return TkDefaultFont for any undefined/invalid style, or styles that don't have a configured -font.

I would refer to "Styling Options" section in https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_treeview.htm#M5. To configure all treeview widgets without a set -style, the style to use is Treeview, where the default item -background can be set (although your example already configures the background of each item using tags), as well as options like -fieldbackground (which I now notice doesn't currently have an effect for the default 'vista' or 'xpnative' Ttk themes on Windows). The header can similarly be configured using the Heading style.

Here is a revised example: Screenshot at 2020-07-19 10-26-21

use warnings;
use strict;

use Tcl::pTk;

my $mw = MainWindow->new();
my $int = $mw->interp;

my $tree = $mw->Scrolled('ttkTreeview',-columns => [qw/ id token/], -show => 'headings', -scrollbars => 'se' )->pack(-fill => 'both', -expand => 1);
$tree -> tagConfigure('title', -background=>'red');
$tree -> tagConfigure('oddrow', -background=>'white');
$tree -> tagConfigure('evenrow', -background=>'yellow');

#my $style = $tree->cget(-style);
my $style = 'Treeview';
my $font = $tree->ttkStyleLookup( $style, -font);
$mw->ttkStyleConfigure($style, -background=>'sky blue', -fieldbackground => 'sea green');
$mw->ttkStyleConfigure('Heading', -background=>'light salmon');

## Code to insert the data nicely
foreach my $col (qw/ id token/){
    my $name = uc($col);
    $tree->heading($col, -command => [\&SortBy, $tree->Subwidget('scrolled'), $col, 0], -text => $name);
    my $len = $tree->fontMeasure($font, $name);
    $tree->column($col, -width, $len+110);
}

$tree->insert('', 'end', -values => ['1234', '5678'], -tags => 'oddrow');
$tree->insert('', 'end', -values => ['abcd', 'efgh'], -tags => 'evenrow');
$tree->insert('', 'end', -values => ['!@#$', '%^&*'],); # An item without a tag

my $but = $mw->ttkButton(
    -text => "Close", 
    -command => sub {
        $mw->destroy;
    }
)->pack();

$int->MainLoop;

sub SortBy{

}

(In GitHub markdown you can use

```perl
…
```

to get Perl syntax highlighting)

For $tree->ttkStyleConfigure, like many other Perl/Tk widget methods (e.g. $widget->idletasks), it doesn't matter which widget the method is called from.

Also be aware there was an issue with tag configure for treeview in Tk 8.6.9 (fixed in 8.6.10), which affects this example: https://core.tcl-lang.org/tk/tktview?name=509cafafae

Does this help?

KellyOBrian commented 4 years ago

Hi Christopher,

this helps a lot to understand the 'bridge' between Tcl and Tcl::pTk. However, I ran your adapted script on Windows 10, Tk 8.6.10, Tcl::pTk 1.08. I get the following:

screenshotWIN

On macOS (Mojave) also Tk 8.6.10, Tcl::pTk 1.08, I get the following:

ScreenshotMAC

Is it possible that the behavior is different based on the OS (for example it seems that on macOS the headings get their own style). I am pretty sure Tcl is linking the right Tk (1.08). Having several Tcl/Tk installations, I had in the past some problems with this, but my tests say the script is run against the right (latest) installation.

Any idea?

chrstphrchvz commented 4 years ago

The first screenshot is indeed what I saw for the 'vista' and 'xpnative' Ttk themes. I hadn't tried the script on Aqua, but doing so now I do see similar issues with settings not being observed correctly for various Ttk themes (for example the header uses the Treeview -fieldbackground setting when using dark mode). I can look into reporting or fixing these if no one already has, and I am not sure if improvements have already been made since Tk 8.6.10.

Keep in mind that if either of the foreground or background colors is manually set, the other should be manually set as well. Otherwise, dark mode/high contrast mode/etc. might use automatic colors which are difficult to see next to the manually-set ones.

I have also encountered difficulty getting Tcl.pm to use the correct Tcl/Tk installation on occasion, so that is something else I want to look into improving.

chrstphrchvz commented 4 years ago

Upstream bug reports:

Custom treeview -fieldbackground and header -background unused for vista/xpnative Ttk themes Custom treeview header -background unused for aqua Ttk theme

chrstphrchvz commented 4 years ago

The upstream reports I opened are likely to be closed as "won't fix". Tcl/Tk developers' response so far is that Ttk widgets (including ttk::treeview) emphasize having appearance which is consistent with platform style, and so themes may intentionally not support customization. One suggests using another table-like widget (e.g. Tablelist or TreeCtrl) if customizations are desired.