File Coverage

blib/lib/Tk/GtkSettings.pm
Criterion Covered Total %
statement 215 277 77.6
branch 72 166 43.3
condition 1 3 33.3
subroutine 31 40 77.5
pod 34 34 100.0
total 353 520 67.8


line stmt bran cond sub pod time code
1             package Tk::GtkSettings;
2              
3             =head1 NAME
4              
5             Tk::GtkSettings - Give Tk applications the looks of Gtk applications
6              
7             =cut
8              
9 1     1   118128 use strict;
  1         3  
  1         35  
10 1     1   4 use warnings;
  1         1  
  1         54  
11 1     1   5 use File::Basename;
  1         1  
  1         91  
12 1     1   5 use Config;
  1         1  
  1         64  
13             our $VERSION = '0.11';
14              
15 1     1   11 use Exporter;
  1         3  
  1         3825  
16             our @ISA = qw(Exporter);
17             our %EXPORT_TAGS = ( 'all' => [ qw(
18             $delete_output
19             $gtkpath
20             $verbose
21             $out_file
22             alterColor
23             appName
24             convertColorCode
25             export2file
26             export2Xdefaults
27             export2xrdb
28             export2Xresources
29             groupAdd
30             groupAll
31             groupDelete
32             groupExists
33             groupMembers
34             groupMembersAdd
35             groupMembersReplace
36             groupOption
37             groupOptionAll
38             groupOptionDelete
39             gtkKey
40             gtkKeyAll
41             gtkKeyDelete
42             hex2rgb
43             hexstring
44             initDefaults
45             loadGtkInfo
46             platformPermitted
47             removefromFile
48             removeFromXdefaults
49             removeFromXresources
50             removeFromxrdb
51             resetAll
52             rgb2hex
53             ) ] );
54             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} });
55              
56             our @EXPORT = qw(
57             applyGtkSettings
58             );
59              
60             sub appName;
61             sub export2xrdb;
62             sub generateOutput;
63             sub initDefaults;
64             sub loadGtkInfo;
65             sub platformPermitted;
66             sub resetAll;
67              
68             our $delete_output = 1;
69             our $gtkpath;
70             our $verbose = 0;
71             our $out_file;
72              
73             if (platformPermitted) {
74             $gtkpath = $ENV{HOME} . "/.config/gtk-3.0/";
75             $out_file = $ENV{HOME} . "/.tkgtksettings";
76             }
77              
78             my $no_gtk = 0;
79             my %gtksettings = ();
80             my %groups = (main => [[''], {}]);
81             my $app_name = '';
82             my $marker;
83              
84             my @basegtkeys = qw(
85             theme_fg_color
86             theme_bg_color
87             theme_text_color
88             theme_base_color
89             theme_view_hover_decoration_color
90             theme_hovering_selected_bg_color
91             theme_selected_bg_color
92             theme_selected_fg_color
93             theme_view_active_decoration_color
94             theme_button_background_normal
95             theme_button_decoration_hover
96             theme_button_decoration_focus
97             theme_button_foreground_normal
98             theme_button_foreground_active
99             borders
100             warning_color
101             success_color
102             error_color
103             theme_unfocused_fg_color
104             theme_unfocused_text_color
105             theme_unfocused_bg_color
106             theme_unfocused_base_color
107             theme_unfocused_selected_bg_color_alt
108             theme_unfocused_selected_bg_color
109             theme_unfocused_selected_fg_color
110             theme_button_background_backdrop
111             theme_button_decoration_hover_backdrop
112             theme_button_decoration_focus_backdrop
113             theme_button_foreground_backdrop
114             theme_button_foreground_active_backdrop
115             unfocused_borders
116             warning_color_backdrop
117             success_color_backdrop
118             error_color_backdrop
119             insensitive_fg_color
120             insensitive_base_fg_color
121             insensitive_bg_color
122             insensitive_base_color
123             insensitive_selected_bg_color
124             insensitive_selected_fg_color
125             theme_button_background_insensitive
126             theme_button_decoration_hover_insensitive
127             theme_button_decoration_focus_insensitive
128             theme_button_foreground_insensitive
129             theme_button_foreground_active_insensitive
130             insensitive_borders
131             warning_color_insensitive
132             success_color_insensitive
133             error_color_insensitive
134             insensitive_unfocused_fg_color
135             theme_unfocused_view_text_color
136             insensitive_unfocused_bg_color
137             theme_unfocused_view_bg_color
138             insensitive_unfocused_selected_bg_color
139             insensitive_unfocused_selected_fg_color
140             theme_button_background_backdrop_insensitive
141             theme_button_decoration_hover_backdrop_insensitive
142             theme_button_decoration_focus_backdrop_insensitive
143             theme_button_foreground_backdrop_insensitive
144             theme_button_foreground_active_backdrop_insensitive
145             unfocused_insensitive_borders
146             warning_color_insensitive_backdrop
147             success_color_insensitive_backdrop
148             error_color_insensitive_backdrop
149             link_color
150             link_visited_color
151             tooltip_text
152             tooltip_background
153             tooltip_border
154             content_view_bg
155             );
156              
157             my @contentwidgets = qw(
158             Entry
159             FilterEntry
160             FloatEntry
161             Spinbox
162             Text
163             TextUndo
164             TextEditor
165             ROText
166             XText
167             );
168              
169             my @listwidgets = qw(
170             Dirlist
171             DirTree
172             HList
173             ITree
174             IconList
175             Listbox
176             ListBrowser
177             Tlist
178             Tree
179             );
180              
181             my %mainoptions = qw(
182             background theme_bg_color
183             foreground theme_fg_color
184             font gtk-font-name
185             activeBackground theme_button_decoration_focus
186             activeForeground theme_fg_color
187             disabledBackground theme_button_background_insensitive
188             disabledForeground theme_button_foreground_insensitive
189             backPageColor theme_button_background_backdrop
190             headerBackground theme_button_decoration_hover
191             highlightBackground theme_bg_color
192             highlightColor theme_selected_bg_color
193             inactiveBackground theme_button_background_backdrop
194             insertBackground theme_fg_color
195             errorColor error_color
196             warningColor warning_color
197             linkColor link_color
198             selectBackground theme_selected_bg_color
199             selectForeground theme_selected_fg_color
200             troughColor insensitive_bg_color
201             );
202              
203             my %contentoptions = qw(
204             background content_view_bg
205             );
206              
207             my %listoptions = qw(
208             background content_view_bg
209             );
210              
211             appName('');
212              
213             =head1 SYNOPSIS
214              
215             =over 4
216              
217             use Tk::GtkSettings;
218             applyGtkSettings;
219            
220             #or
221            
222             use Tk::GtkSettings qw(initDefaults export2xrdb);
223             initDefaults;
224             #do your adjustments here
225             export2xrdb;
226            
227             #then initialize your perl/Tk app.
228            
229             use Tk;
230             my $w = new MainWindow;
231            
232             #Do your stuff here
233            
234             $w->MainLoop;
235              
236             =back
237              
238             =head1 ABSTRACT
239              
240             Apply Gtk colors and fonts to your perl/Tk application
241              
242             =head1 DESCRIPTION
243              
244             Tk::GtkSettings attempts to overcome some very old complaints about Tk:
245              
246             - It's ugly!
247             - It's complicated to adjust colors and fonts to your desktop style
248              
249             Tk::GtkSettings loads your Gtk configuration files and applies it's font and color settings to your perl/Tk application.
250              
251             B loads some nice (at least we think so) default settings that copies your Gtk theme pretty well.
252              
253             However, it gives plenty of tools for you to adjust it and mess it up any way you like.
254              
255             It is harmless to install on Windows or Mac. It just will not do anything on these systems. That makes it
256             smooth to add as a dependency to your own package if you want it to be able to run on Windows and Mac as well.
257              
258             In working with colors it assumes 8-bit color depth.
259              
260             =head1 EXPORTS
261              
262             =over 4
263              
264             =item B<$delete_output>
265              
266             Usefull for testing and debugging. B exports to a file which then is sent to xrdb.
267             It checks if this file should be deleted when done. Default value is 1.
268              
269             =item B<$gtkpath>
270              
271             Usefull for testing. Default value is ~/.config/gtk-3.0/. That is the location where the
272             Gtk configuration files reside. This variable is not defined when on Windows or Mac.
273              
274             =item B<$out_file>
275              
276             Default value ~/.tkgtksettings. Used by B. This variable is not defined
277             on Windows or Mac.
278              
279             =item B<$verbose>
280              
281             Usefull for testing and debugging. Default value is 0. If set B will
282             complain about everything not in order. Otherwise it will quietly fail.
283              
284             =item B(I<$hexcolor>, I<$offset>)
285              
286             Adjusts $hexcolor by $offset. It takes every color chanel and adds or substracts $offset.
287             If the channel value is greater than 127 it will substract, otherwise it will add.
288              
289             alterColor('#000000', 1) returns #010101
290             alterColor('#FFFFFF', 1) returns #FEFEFE
291              
292             =cut
293              
294             sub alterColor {
295 1     1 1 11 my ($hex, $offset) = @_;
296 1         5 my @rgb = hex2rgb($hex);
297 1         1 my @rgba = ();
298 1         2 for (@rgb) {
299 3 50       5 if ($_ < 128) {
300 3         5 my $c = $_ + $offset;
301 3 50       4 $c = 0 if $c < 0;
302 3         11 push @rgba, $c
303             } else {
304 0         0 my $c = $_ - $offset;
305 0 0       0 $c = 255 if $c > 255;
306 0         0 push @rgba, $c
307             }
308             }
309 1         5 return rgb2hex(@rgba)
310             }
311              
312             =item B
313              
314             Just making life easy. Call this one and your done, unless you require adjustments.
315             It calls B and exports the whole bunch to xrdb.
316             Exported by default.
317              
318             =cut
319              
320             sub applyGtkSettings {
321 0 0   0 1 0 return unless platformPermitted;
322 0         0 initDefaults;
323 0         0 export2xrdb;
324             }
325              
326             =item B(I<$name>)
327              
328             Sets and returns your application name. By default it is set to the basename of what is in B<$0>. Your Gtk settings
329             will only be applied to your application in xrdb. You can set it to an empty string. Then it will
330             apply your Gtk settings to all your perl/Tk applications.
331              
332             =cut
333              
334             sub appName {
335 1 50   1 1 3 if (@_ ) {
336 1         2 $app_name = shift;
337 1         55 $app_name = basename($app_name); #remove leading folders
338 1         35 $app_name =~ s/\.[^.]+$//; #remove extension
339 1         4 $marker = "!$app_name Tk::GtkSettings section\n";
340             }
341 1         2 return $app_name
342             }
343              
344             =item B(I<'rgb(255, 0, 0)'>)
345              
346             Some color settings in the Gtk configuration files are in the format 'rgb(255, 255, 255)'.
347             B converts these to a hex color string.
348              
349             =cut
350              
351             sub convertColorCode {
352 1     1 1 736 my $input = shift;
353 1 50       13 if ($input =~ /rgb\((\d+),(\d+),(\d+)\)/) {
354 1         10 my $r = substr(sprintf("0x%X", $1), 2);
355 1         5 my $g = substr(sprintf("0x%X", $2), 2);
356 1         6 my $b = substr(sprintf("0x%X", $3), 2);
357 1         6 return "#$r$g$b"
358             }
359             }
360              
361             =item B(I<$gtkfontstring>)
362              
363             Converts the font string in gtk to something Tk can handle
364              
365             =cut
366              
367             sub decodeFont {
368 2     2 1 5 my $rawfont = shift;
369 2         5 my $family = '';
370 2         3 my $style = '';
371 2         4 my $size = '';
372 2 50       27 if ($rawfont =~ s/^([^,]+),//) {
373 2         20 $family = $1;
374             }
375 2         9 $rawfont =~ s/^\s*//; #remove leading spaces
376 2 50       11 if ($rawfont =~ s/^([^\d]+)//) {
377 0         0 $style = $1;
378 0         0 $style =~ s/^\s*//; #remove leading spaces
379 0         0 $style =~ s/\s*!//; #remove trailing spaces
380 0         0 $style = lc($style);
381             }
382 2 50       9 if ($rawfont =~ s/^(\d+)//) {
383 2         10 $size = $1;
384 2         7 $size =~ s/\s*!//; #remove trailing spaces
385             }
386 2         5 return "{$family} $size $style"
387             }
388              
389             =item B(I<$file>, ?I<$removeflag>?)
390              
391             Exports your Gtk settings to $file in a format recognized by xrdb. It looks for a section
392             in the file marked by appName . "Tk::GtkSettings section\n". If it finds it it will replace this section.
393             Otherwise it will append your Gtk settings to the end of the file. If $file does not yet exist it
394             will create it. if $removeflag is true it will not export but remove the section from $file.
395              
396             =cut
397              
398             sub export2file {
399 2     2 1 6 my ($file, $remove) = @_;
400 2 100       9 $remove = 0 unless defined $remove;
401 2         4 my $out = "";
402 2         4 my $found = 0;
403 2 50       32 if (-e $file) {
404 2 50       48 unless (open(XDEF, "<$file")) {
405 0 0       0 warn "cannot open $file" if $verbose;
406             return
407 0         0 }
408 2         4 my $inside = 0;
409 2         48 while (my $l = ) {
410 53 50       65 if ($inside) {
411 0 0       0 if ($l eq $marker) {
412 0         0 $inside = 0;
413             }
414             } else {
415 53 50       62 if ($l eq $marker) {
416 0         0 $inside = 1;
417 0         0 $found = 1;
418 0 0       0 $out = "$out$l" . generateOutput . $l unless $remove;
419             } else {
420 53         107 $out = "$out$l";
421             }
422             }
423             }
424 2         16 close XDEF;
425             }
426 2 50       5 unless ($found) {
427 2         14 $out = "$out\n$marker" . generateOutput . "$marker\n"
428             }
429 2 50       164 unless (open(XDEFO, ">$file")) {
430 0 0       0 warn "cannot open $file" if $verbose;
431             return
432 0         0 }
433 2         74 print XDEFO $out;
434 2         350 close XDEFO;
435             }
436              
437             =item B(?I<$removeflag>?)
438              
439             Same as B, however the file is always '~/.Xdefaults'.
440              
441             =cut
442              
443             sub export2Xdefaults {
444 0     0 1 0 export2file( $ENV{HOME} . '/.Xdefaults');
445             }
446              
447             =item B(?I<$removeflag>?)
448              
449             Same as B, however the file is always '~/.Xresources'.
450              
451             =cut
452              
453             sub export2Xresources {
454 0     0 1 0 export2file( $ENV{HOME} . '/.Xresources');
455             }
456              
457             =item B
458              
459             exports your Gtk settings directly to the xrdb database.
460              
461             =cut
462              
463             sub export2xrdb {
464 0 0   0 1 0 return unless platformPermitted;
465 0 0       0 return if $no_gtk;
466 0 0       0 if (open(OFILE, ">", $out_file)) {
467 0         0 print OFILE generateOutput;
468 0         0 close OFILE;
469 0         0 system "xrdb $out_file";
470 0 0       0 unlink $out_file if $delete_output;
471             }
472             }
473              
474             =item B
475              
476             Generates the output used by the export functions. Returns a string.
477              
478             =cut
479              
480             sub generateOutput {
481 2 50   2 1 5 return if $no_gtk;
482 2 50       6 return unless platformPermitted;
483 2         4 my $output = '';
484             #group main has to be done first.
485 2         5 my (@g) = ('main');
486 2         11 for (sort keys %groups) {
487 8 100       15 push @g, $_ unless $_ eq 'main';
488             }
489 2         4 for (@g) {
490 8         10 my $name = $_;
491 8         13 my $group = $groups{$name};
492 8         10 my $options = $group->[1];
493 8         10 my $mem = $group->[0];
494 8         12 for (@$mem) {
495 28         37 my $member = $_;
496 28         74 for (sort keys %$options) {
497 84         121 my $val = gtkKey($options->{$_});
498 84 100       139 $val = $options->{$_} unless defined $val;
499 84 100       110 unless ($name eq 'main') {
500 44         86 $output = $output . $app_name . "*$member." . $_ . ": " . $val . "\n";
501             } else {
502 40         58 $output = $output . $app_name . '*' . $_ . ": " . $val . "\n";
503             }
504             }
505             }
506             }
507 2         24 return $output
508             }
509              
510             =item B(I<$groupname>, I<\@members>, I<\%options>)
511              
512             Adds $groupname to the groups hash. If @members or %options are not specified,
513             it will leave them empty.
514              
515             =cut
516              
517             sub groupAdd {
518 7     7 1 350 my ($group, $members, $options) = @_;
519 7 50       11 unless (defined $group) {
520 0 0       0 warn "group is not defined" if $verbose;
521             return
522 0         0 }
523 7 50       11 $members = [] unless defined $members;
524 7 50       12 $options = {} unless defined $options;
525 7 50       13 unless (exists $groups{$group}) {
526 7         24 $groups{$group} = [$members, $options]
527             } else {
528 0 0       0 warn "group $group already exists" if $verbose
529             }
530             }
531              
532             =item B
533              
534             Returns a list of all available groups.
535              
536             =cut
537              
538             sub groupAll {
539 1     1 1 13 return keys %groups
540             }
541              
542             =item B(I<$groupname>)
543              
544             Removes $groupsname from the groups hash. You cannot delete the 'main' group.
545              
546             =cut
547              
548             sub groupDelete {
549 2     2 1 5 my $group = shift;
550 2 50       7 if (groupExists($group)) {
551 2 100       6 if ($group eq 'main') {
552 1 50       5 warn "deleting main group is not allowed" if $verbose;
553 1         3 return 0
554             }
555 1         5 delete $groups{$group};
556             }
557 1         2 return 1
558             }
559              
560             =item B(I<$groupname>)
561              
562             Returns true if $groupname is available.
563              
564             =cut
565              
566             sub groupExists {
567 57     57 1 72 my $group = shift;
568 57 50       115 unless (defined $group) {
569 0 0       0 warn "group not specified or is not defined" if $verbose;
570 0         0 return 0
571             }
572 57 100       109 unless (exists $groups{$group}) {
573 1 50       5 warn "group $group does not exist" if $verbose;
574 1         6 return 0
575             }
576 56         127 return 1
577             }
578              
579             =item B(I<$groupname>)
580              
581             Returns the list of existing members of $groupname. It will return an empty list
582             if $groupname equals 'main'.
583              
584             =cut
585              
586             sub groupMembers {
587 2     2 1 9 my $group = shift;
588 2 50       6 if (groupExists($group)) {
589 2 50       7 if ($group eq 'main') {
590 0         0 warn "no access to main group members";
591             return ()
592 0         0 }
593 2         5 my $l = $groups{$group}->[0];
594 2         7 return @$l;
595             }
596             }
597              
598             =item B(I<$groupname>, I<@newmembers>)
599              
600             Adds new members to $groupname. You cannot add members to the 'main' group.
601              
602             =cut
603              
604             sub groupMembersAdd {
605 1     1 1 465 my $group = shift;
606 1 50       8 if (groupExists($group)) {
607 1 50       6 if ($group eq 'main') {
608 0         0 warn "no access to main group members";
609             return
610 0         0 }
611 1         4 my $l = $groups{$group}->[0];
612 1         5 push @$l, @_;
613             }
614             }
615              
616             =item B(I<$groupname>, I<@members>)
617              
618             Replaces the list of members in $groupsname by @members. You cannot modify the members list of the 'main' group.
619              
620             =cut
621              
622             sub groupMembersReplace {
623 1     1 1 389 my $group = shift;
624 1 50       5 if (groupExists($group)) {
625 1 50       5 if ($group eq 'main') {
626 0         0 warn "No access to main group members";
627             return
628 0         0 }
629 1         3 my $l = $groups{$group}->[0];
630 1         5 @$l = @_;
631             }
632             }
633              
634             =item B(I<$groupname>, I<$option>, ?I<$value>?)
635              
636             Sets and returns the value of $option in $groupname. $value should be a corresponding key from
637             the Gtk hash. If that key is not found, it assumes a direct value.
638              
639             =cut
640              
641             sub groupOption {
642 45     45 1 498 my $group = shift;
643 45 50       55 if (groupExists($group)) {
644 45         49 my $option = shift;
645 45 50       57 unless (defined $option) {
646 0 0       0 warn "option not defined or specified" if $verbose;
647             return
648 0         0 }
649 45 100       59 if (@_) {
650 43         51 my $value = shift;
651 43         78 $groups{$group}->[1]->{$option} = $value;
652             }
653 45         75 return $groups{$group}->[1]->{$option}
654             }
655             }
656              
657             =item B(I<$groupname>)
658              
659             Returns a list of all available options in $groupname.
660              
661             =cut
662              
663             sub groupOptionAll {
664 2     2 1 367 my $group = shift;
665 2 50       6 if (groupExists($group)) {
666 2         7 my $opt = $groups{$group}->[1];
667 2         9 return keys %$opt
668             }
669             }
670              
671             =item B(I<$groupname>, I<$option>)
672              
673             Removes $option from $groupname
674              
675             =cut
676              
677             sub groupOptionDelete {
678 1     1 1 457 my $group = shift;
679 1 50       6 if (groupExists($group)) {
680 1         3 my $option = shift;
681 1 50       4 unless (defined $option) {
682 0 0       0 warn "option not defined or specified" if $verbose;
683             return
684 0         0 }
685 1         5 delete $groups{$group}->[1]->{$option};
686             }
687             }
688              
689             =item B(I<$key>, ?I<$value>?)
690              
691             Sets and returns the value of $key in the Gtk hash
692              
693             =cut
694              
695             sub gtkKey {
696 89     89 1 133 my ($key, $val) = @_;
697 89 50       126 return undef if $no_gtk;
698 89 100       119 $gtksettings{$key} = $val if defined $val;
699 89 100       182 if (exists $gtksettings{$key}) {
700 64         184 return $gtksettings{$key}
701             } else {
702 25 50       59 warn "item $key not present in gtk settings" if $verbose;
703             }
704             return undef
705 25         50 }
706              
707             =item B
708              
709             Returns a list of all available keys in the Gtk hash.
710              
711             =cut
712              
713             sub gtkKeyAll {
714 0 0   0 1 0 return 0 if $no_gtk;
715 0         0 return keys %gtksettings
716             }
717              
718             =item B(I<$key>)
719              
720             Delets $key from the Gtk hash.
721              
722             =cut
723              
724             sub gtkKeyDelete {
725 1     1 1 4 my $key = shift;
726 1 50       4 return 0 if $no_gtk;
727 1 50       5 if (exists $gtksettings{$key}) {
728 1         4 delete $gtksettings{$key}
729             } else {
730 0 0       0 warn "item $key not present in gtk settings" if $verbose;
731             }
732             }
733              
734             =item B(I<$hex_color>)
735              
736             Returns and array with the decimal values of red, green and blue.
737              
738             =cut
739              
740             sub hex2rgb {
741 2     2 1 17 my $hex = shift;
742 2         14 $hex =~ s/^(\#|Ox)//;
743 2         6 $_ = $hex;
744 2         12 my ($r, $g, $b) = m/(\w{2})(\w{2})(\w{2})/;
745 2         5 my @rgb = ();
746 2         7 $rgb[0] = CORE::hex($r);
747 2         2 $rgb[1] = CORE::hex($g);
748 2         4 $rgb[2] = CORE::hex($b);
749             return @rgb
750 2         7 }
751              
752             =item B(I<$num>)
753              
754             Return the hexadecimal representation of $num in a two character string.
755              
756             =cut
757              
758             sub hexstring {
759 7     7 1 420 my $num = shift;
760 7         21 my $hex = substr(sprintf("0x%X", $num), 2);
761 7 100       17 if (length($hex) < 2) { $hex = "0$hex" }
  5         7  
762 7         14 return $hex
763             }
764              
765             =item B
766              
767             Initializes some sensible defaults. Also does a full reset and loads Gtk configuration files.
768              
769             =cut
770              
771             sub initDefaults {
772 2 50   2 1 172016 return unless platformPermitted;
773 2         11 resetAll;
774 2         15 loadGtkInfo;
775              
776 2         16 for (keys %mainoptions) {
777 38         54 groupOption('main', $_, $mainoptions{$_})
778             }
779 2         10 my $iconlib = gtkKey('gtk-icon-theme-name');
780 2 50       9 groupOption('main', 'iconTheme', $iconlib) if defined $iconlib;
781              
782 2         7 my @cw = @contentwidgets;
783 2         6 my %co = %contentoptions;
784 2         10 groupAdd('content', \@cw, \%co);
785 2         7 my @lw = @listwidgets;
786 2         5 my %lo = %listoptions;
787 2         5 groupAdd('list', \@lw, \%lo);
788 2         7 groupOption('list', 'highlightThickness', 1);
789 2         9 groupAdd('menu', ['Menu', 'NoteBook'], {borderWidth => 1});
790             }
791              
792             =item B
793              
794             Empties the Gtk hash and (re)loads the Gtk configuration files.
795              
796             =cut
797              
798             sub loadGtkInfo {
799 2 50   2 1 8 return unless platformPermitted;
800 2         53 %gtksettings = ();
801 2         6 my $cf = $gtkpath . "colors.css";
802 2 50       187 if (open(OFILE, "<", $cf)) {
803 2         160 while () {
804 140         160 my $line = $_;
805 140 50       408 if ($line =~ s/\@define-color\s//) {
806 140 50       370 if ($line =~ /([^\s]+)\s([^;]+);/) {
807 140         188 my $key = $1;
808 140         153 my $color = $2;
809 140 50       190 $color = convertColorCode($color) if $color =~ /^rgb\(/;
810 140         185 $key = _truncate($key);
811 140         504 $gtksettings{$key} = $color
812             }
813             }
814             }
815 2         44 close OFILE
816             } else {
817 0 0       0 warn "cannot open Gtk colors.css" if $verbose;
818 0         0 $no_gtk = 1;
819             }
820 2         6 my $sf = $gtkpath . "settings.ini";
821 2 50       123 if (open(OFILE, "<", $sf)) {
822 2         62 while () {
823 24         43 my $line = $_;
824 24 100       78 if ($line =~ /([^=]+)=([^\n]+)/) {
825 22         72 $gtksettings{$1} = $2
826             }
827             }
828 2         16 close OFILE;
829 2 50       8 if (exists $gtksettings{'gtk-font-name'}) {
830 2         11 my $font = decodeFont($gtksettings{'gtk-font-name'});
831 2         6 $gtksettings{'gtk-font-name'} = $font;
832             }
833             } else {
834 0 0       0 warn "cannot open Gtk settings.ini" if $verbose;
835 0         0 $no_gtk = 1;
836             }
837             }
838              
839             =item B
840              
841             Returns true if you are not on Windows or Mac.
842              
843             =cut
844              
845             sub platformPermitted {
846 8     8 1 33 my $platform = $^O;
847 8 50 33     145 return 0 if (($Config{osname} eq 'MSWin32') or ($Config{osname} eq 'darwin'));
848 8         31 return 1
849             }
850              
851             =item B(I<$file>)
852              
853             Same as export2file($file, 1)
854              
855             =cut
856              
857             sub removeFromfile {
858 0     0 1 0 my $f = shift;
859 0         0 export2file($f, 1);
860             }
861              
862             =item B
863              
864             Same as export2Xdefaults(1)
865              
866             =cut
867              
868             sub removeFromXdefaults {
869 0     0 1 0 export2file('~/.Xdefaults', 1);
870             }
871              
872             =item B
873              
874             Same as export2Xresources(1)
875              
876             =cut
877              
878             sub removeFromXresources {
879 0     0 1 0 export2file('~/.Xresouces', 1);
880             }
881              
882             =item B
883              
884             Removes all the settings previously defined from the xrdb database
885              
886             =cut
887              
888             sub removeFromxrdb {
889 0 0   0 1 0 return unless platformPermitted;
890 0 0       0 return if $no_gtk;
891 0 0       0 if (open(OFILE, ">", $out_file)) {
892 0         0 print OFILE generateOutput;
893 0         0 close OFILE;
894 0         0 system "xrdb -remove $out_file";
895 0 0       0 unlink $out_file if $delete_output;
896             }
897             }
898              
899             =item B
900              
901             Removes all groups and options. The group 'main' will remain, but all its options are also deleted.
902             This does not affect the Gtk hash.
903              
904             =cut
905              
906             sub resetAll {
907 2     2 1 34 %groups = (
908             main => [[''], {}]
909             )
910             }
911              
912             =item B(I<$red>, I<$green>, I<$blue>)
913              
914             Converts the decimval values $red, $green and $blue into a hex color string.
915              
916             =cut
917              
918             sub rgb2hex {
919 2     2 1 408 my ($red, $green, $blue) = @_;
920 2         9 my $r = hexstring($red);
921 2         5 my $g = hexstring($green);
922 2         4 my $b = hexstring($blue);
923 2         9 return "#$r$g$b"
924              
925             }
926              
927             sub _truncate {
928 140     140   160 my $name = shift;
929 140         158 for (@basegtkeys) {
930 3734         3523 my $key = $_;
931 3734 100       4608 if (substr($name, 0, length($key)) eq $key) {
932 140         198 return $key
933             }
934             }
935 0           return $name
936             }
937              
938             =back
939              
940             =head1 COPYRIGHT AND LICENSE
941              
942             Copyright 2022 - 2023 by Hans Jeuken
943              
944             Same as Perl
945              
946             =head1 AUTHOR
947              
948             Hans Jeuken (jeuken dot hans at gmail dot com)
949              
950             =head1 BUGS AND CAVEATS
951              
952             If you find any bugs, please contact the author.
953              
954             =head1 TODO
955              
956             =cut
957              
958              
959             1;
960             __END__