File: Theme.tcl

package info (click to toggle)
coccinella 0.96.20-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,108 kB
  • ctags: 5,908
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (659 lines) | stat: -rw-r--r-- 17,396 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# Theme.tcl --
#
#       Some utitilty procedures useful when theming widgets and UI.
#       
#  Copyright (c) 2003-2008  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: Theme.tcl,v 1.60 2008-05-30 07:25:04 matben Exp $

package provide Theme 1.0

namespace eval ::Theme {}

# Theme::Init --
#
#       Reads all resource database files, also any theme rdb file.
#       Does a lot of init bookkeeping as well.
#       
# Arguments:
#       none
#       
# Results:
#       none

proc ::Theme::Init {} {
    global  this prefs
        
    # Handle theme name and locale from prefs file.
    NameAndLocalePrefs
    
    # Create named standard fonts.
    Fonts
    FontConfigSize $prefs(fontSizePlus)
    
    # Read widget resources.
    ReadResources    
}

proc ::Theme::ReadResources {} {
    global  this prefs
    
    # Priorities.
    # widgetDefault: 20
    # startupFile:   40
    # userDefault:   60
    # interactive:   80 (D)

    # Seems X11 has some system option db that must be overridden.
    if {[tk windowingsystem] eq "x11"} {
	set priority 60
    } else {
	set priority startupFile
    }

    # Read resource database files in a hierarchical order.
    # 1) always read the default rdb file.
    # 2) read rdb file for this specific platform, if exists.
    # 3) read rdb file for any theme we have chosen. Search first
    #    inside the sources and then in the alternative user directory.
    # 4) read any theme specific rdb file if exists (after tile loaded).
    option readfile [file join $this(resourcePath) default.rdb] $priority
    set f [file join $this(resourcePath) $this(platform).rdb]
    if {[file exists $f]} {
	option readfile $f $priority
    }
    
    # Any theme specific resource files.
    set dir [GetPath $prefs(themeName)]
    set rdir [file join $dir $prefs(themeName) $this(resources)]
    if {[file isdirectory $rdir]} {
	option readfile [file join $rdir default.rdb] userDefault
	set f [file join $rdir $this(platform).rdb]
	if {[file exists $f]} {
	    option readfile $f userDefault
	}
    }
}

# Theme::ReadTileResources --
# 
#       Read any standard tile theme specific resources, typically for Menu
#       and TreeCtrl.

proc ::Theme::ReadTileResources {} {
    global  this

    if {[tk windowingsystem] eq "x11"} {
	set priority 60
    } else {
	set priority startupFile
    }
    set f [file join $this(resourcePath) ${ttk::currentTheme}.rdb]
    if {[file exists $f]} {
	option readfile $f $priority
    }
}

# Theme::TileThemeChanged --
# 
#       This is a handler for tileutils ThemeChanged events which must be
#       invoked before widget specific handlers are.

proc ::Theme::TileThemeChanged {} {
    ReadResources
    ReadTileResources 

    # Configure any standard fonts the tile theme may have set.
    FontConfigStandard
    
    # Any named fonts from any resource file must be constructed.
    PostProcessFontDefs
}

# Theme::Fonts --
# 
#       Named fonts are created and configured for each platform.

proc ::Theme::Fonts {} {
    global  tcl_platform
    variable fontopts
    
    catch {font create CociDefaultFont}
    catch {font create CociSmallFont}
    catch {font create CociSmallBoldFont}
    catch {font create CociTinyFont}
    catch {font create CociLargeFont}

    switch -- [tk windowingsystem] {
	win32 {
	    if {$tcl_platform(osVersion) >= 5.0} {
		variable family "Tahoma"
	    } else {
		variable family "MS Sans Serif"
	    }
	    variable size 8
	    variable smallsize 8
	    variable largesize 14

	    font configure CociDefaultFont   -family $family -size $size
	    font configure CociSmallFont     -family $family -size $smallsize
	    font configure CociSmallBoldFont -family $family -size $smallsize -weight bold
	    font configure CociTinyFont      -family $family -size $size
	    font configure CociLargeFont     -family $family -size $largesize
	}
	aqua {
	    variable family "Lucida Grande"
	    variable size 13
	    variable viewsize 12
	    variable smallsize 11
	    variable largesize 18
	    
	    font configure CociDefaultFont   -family $family -size $size
	    font configure CociSmallFont     -family $family -size $smallsize
	    font configure CociSmallBoldFont -family $family -size $smallsize -weight bold
	    font configure CociTinyFont      -family Geneva  -size 9
	    font configure CociLargeFont     -family $family -size $largesize
	}
	x11 {
	    if {![catch {tk::pkgconfig get fontsystem} fs] && $fs eq "xft"} {
		variable family "sans-serif"
		variable size -12
		variable smallsize -12
		variable largesize -22
	    } else {
		variable family "Helvetica"
		variable size -12
		variable smallsize -10
		variable largesize -18
	    }

	    font configure CociDefaultFont   -family $family -size $size
	    font configure CociSmallFont     -family $family -size $smallsize
	    font configure CociSmallBoldFont -family $family -size $smallsize -weight bold
	    font configure CociTinyFont      -family $family -size $size
	    font configure CociLargeFont     -family $family -size $largesize
	}
    }
    set fontopts(family)    $family
    set fontopts(size)      $size
    set fontopts(smallsize) $smallsize
    set fontopts(largesize) $largesize
}

# Theme::FontConfigStandard --
# 
#       A resource file can override the standard font attributes as hardcoded
#       above.

proc ::Theme::FontConfigStandard {} {
    variable fontopts

    # Beware, resource names must start with lower case!
    foreach name {
	CociDefaultFont CociSmallFont CociSmallBoldFont CociTinyFont CociLargeFont
    } {
	set rname [string tolower [string index $name 0]][string range $name 1 end]
	set spec [option get . $rname {}]
	if {[string length $spec]} {
	    eval {font configure $name} $spec
	    if {$name eq "CociSmallFont"} {
		array set fontA [font actual $name]
		set fontopts(smallsize) $fontA(-size)
	    }
	}
    }
}

proc ::Theme::FontConfigSize {increase} {
    variable fontopts
    
    # @@@ Not sure how to handle the unnamed system fonts?
    if {$fontopts(smallsize) > 0} {
	set size [expr {$fontopts(smallsize) + $increase}]
    } else {
	set size [expr {$fontopts(smallsize) - $increase}]
    }
    font configure CociSmallFont -size $size
    font configure CociSmallBoldFont -size $size

    if {$fontopts(size) > 0} {
	set size [expr {$fontopts(size) + $increase}]
    } else {
	set size [expr {$fontopts(size) - $increase}]
    }
    font configure CociDefaultFont -size $size
}

# Theme::PostProcessFontDefs --
# 
#       If a resource file specifies a font as:
#       
#       *fontNames:        myCoolFont ...
#       *myCoolFont:       {Helvetica 24 bold}
#       
#       then the actual font with that name is constructed here.
#       Note: Must start with LOWER case!

proc ::Theme::PostProcessFontDefs {} {
    
    foreach name [option get . fontNames {}] {
	catch {font create $name}
	set spec [option get . $name {}]
	if {$spec != {}} {
	    eval {font configure $name} [font actual $spec]
	}
    }
}

proc ::Theme::NameAndLocalePrefs {} {
    global  this prefs
    
    set rootTheme [GetRootTheme]
    
    set prefs(rootTheme)     $rootTheme
    set prefs(themeName)     $rootTheme
    set prefs(themeParent)   ""
    set prefs(messageLocale) ""
    set prefs(fontSizePlus)  0
    
    ::PrefUtils::Add [list  \
      [list prefs(themeName)      prefs_themeName      $prefs(themeName)] \
      [list prefs(messageLocale)  prefs_messageLocale  $prefs(messageLocale)] \
      [list prefs(fontSizePlus)   prefs_fontSizePlus   $prefs(fontSizePlus)] \
      ]    

    set appName    [option get . appName {}]
    set theAppName [option get . theAppName {}]
    if {$appName ne ""} {
	set prefs(appName) $appName
    }
    if {$theAppName ne ""} {
	set prefs(theAppName) $theAppName
    }
    
    # Check here that the theme folder still exists.
    if {[GetPath $prefs(themeName)] eq ""} {
	set prefs(themeName) $prefs(rootTheme)
    }
}

#--- This is the actual themeing engine ----------------------------------------

proc ::Theme::GetAllWithFilter {{filterL {}}} {
    global  this
    
    set themes [list]
    foreach path [GetAllPathsWithFilter $filterL] {
	lappend themes [file tail $path]
    }
    return [lsort $themes]
}

proc ::Theme::GetAllPathsWithFilter {{filterL {}}} {
    global  this
    
    set paths [list]
    foreach dir [list $this(themesPath) $this(altThemesPath)] {
	foreach path [glob -nocomplain -types d -directory $dir -- *] {
	    set name [file tail $path]
	    if {$name eq "CVS"} { continue }
	    if {[llength $filterL]} {
		set infoL [GetInfo $path]
		if {[llength [lintersect $infoL $filterL]]} {
		    lappend paths $path
		}
	    } else {
		lappend paths $path
	    }
	}
    }
    return $paths
}

proc ::Theme::GetAllThemePaths {} {
    global  this
    
    set paths [list]
    foreach dir [list $this(themesPath) $this(altThemesPath)] {
	foreach path [glob -nocomplain -types d -directory $dir -- *] {
	    if {[file tail $path] eq "CVS"} { continue }
	    if {[file exists [file join $path themeInfo]]} {
		lappend paths $path
	    }
	}
    }
    return $paths
}

proc ::Theme::GetRootTheme {} {
    global  this
    
    foreach path [glob -nocomplain -types d -directory $this(themesPath) -- *] {
	if {[file tail $path] eq "CVS"} { continue }
	if {"root" in [GetInfo $path]} {
	    return [file tail $path]
	}
    }
    return
}

proc ::Theme::GetInfo {path} {
     variable infoCache

     set name [file tail $path]
     if {[info exists infoCache($name)]} {
	 return $infoCache($name)
     } else {
	 return [ReadInfo $path]
     }
}

proc ::Theme::ReadInfo {path} {
     variable infoCache
   
    set info ""
    set infoFile [file join $path themeInfo]
    if {[file exists $infoFile]} {
	set fd [open $infoFile]
	set info [lsort -unique [string trim [read $fd]]]
	close $fd
	set name [file tail $path]
	set infoCache($name) $info
    }
    return $info
}

namespace eval ::Theme {
    
    # Caches search paths for a certain theme 'name' including any parent theme.
    variable themePaths
    
    # Caches the info file content per theme 'name'.
    variable infoCache
}

proc ::Theme::CacheAllInfo {} {
    variable infoCache
    
    foreach path [GetAllThemePaths] {
	set name [file tail $path]
	set infoCache($name) [ReadInfo $path]
    }
}

# Theme::GetPath --
#
#       Seraches for the given theme name in app bundle or prefs folder.
#       Returns sempty if not found. 
#       Internal usage only???  Cache it???

proc ::Theme::GetPath {theme} {
    global this
    
    set path ""
    set dir [file join $this(themesPath) $theme]
    if {[file isdirectory $dir]} {
	set path $dir
    } else {
	set dir [file join $this(altThemesPath) $theme]
	if {[file isdirectory $dir]} {
	    set path $dir
	}
    }
    return $path
}

# Theme::GetPathsForTheme --
#
#       Returns a list of paths for 'theme' and 'themeParent'.
#       Caches results.

proc ::Theme::GetPathsForTheme {theme themeParent} {
    global  this prefs
    variable themePaths
    
    if {[info exists themePaths($theme)]} {
	set paths $themePaths($theme)
    } else {

	# Build up a list of search paths.
	set paths [list]
	if {$theme ne ""} {
	    set path [GetPath $theme]
	    if {$path ne ""} {
		lappend paths $path
	    }
	    if {$themeParent ne ""} {
		set path [GetPath $themeParent]
		if {$path ne ""} {
		    lappend paths $path
		}		
	    }
	}
	
	# This MUST always be searched for last since it is our fallback.
	if {$theme ne $prefs(rootTheme)} {
	    lappend paths [file join $this(themesPath) $prefs(rootTheme)]
	}
	set themePaths($theme) $paths
    }
    return $paths
}

proc ::Theme::GetPresentSearchPaths {} {
    global prefs
    return [GetPathsForTheme $prefs(themeName) $prefs(themeParent)]
}

# Theme::GetPathsFor --
#
#       Make a list of paths where we shall search for things like sounds
#       and emoticon sets. These are ordered so in some cases they shall
#       be searched for in reversed order.

proc ::Theme::GetPathsFor {subPath} {
    global this
    
    set paths [list]
    lappend paths [file join $this(path) $subPath]
    set path [file join $this(themesPath) $subPath]
    if {[file isdirectory $path]} {
	lappend paths $path
    }
    lappend paths [file join $this(prefsPath) $subPath]
    set path [file join $this(altThemesPath) $subPath]
    if {[file isdirectory $path]} {
	lappend paths $path
    }
    return $paths
}

# Theme::FindIcon, FindIconWithName --
#
#       Searches for image using complex dir specifier.
#       spec is typically icons/32x32/send which is a sub path to an image file
#       but without any file suffix.

proc ::Theme::FindIcon {spec} {
    
    # The image create a command as well which must be namespaced due
    # to its design flaw.
    return [FindIconWithName $spec ::theme::$spec]
}

proc ::Theme::FindIconWithName {spec name} {
    
    set image ""
    if {$image ni [image names]} {
	set paths [GetPresentSearchPaths]
	set image [MakeIconFromPaths $spec $name $paths]
    } else {
	set image $name
    }
    return $image
}

# Theme::MakeIconFromPaths --
#
#       Searches the specified directories for an image in subpath 'spec'.
#       If 'name' empty we use a default name, else image gets 'name'.

proc ::Theme::MakeIconFromPaths {spec name paths} {
    
    set found 0
    set image ""
    foreach path $paths {
	foreach fmt {png gif} {
	    set f [file join $path $spec].$fmt
	    
	    # We provide a single step fallback: 
	    #   list-add-user-Dis -> list-add-user   etc.
	    if {![file exists $f]} {
		set tail [file tail $spec]
		set parts [split $tail -]
		if {[llength $parts] > 1} {
		    set f [join [lrange [split $spec /] 0 end-1] /]
		    append f / [join [lrange $parts 0 end-1] -]
		    set f [file join $path $f].$fmt
		}
	    }
	    if {[file exists $f]} {
		if {$name ne ""} {
		    set image [image create photo $name -file $f -format $fmt]
		} else {
		    set image [image create photo -file $f -format $fmt]
		}
		set found 1
		break
	    }
	}
	if {$found} { break }
    }
    return $image
}

# Theme::FindExactIconFile --
# 
#       Searches the exact image name and returns its complete path if found.

proc ::Theme::FindExactIconFile {subPath} {
    
    foreach path [GetPresentSearchPaths] {
	set f [file join $path $subPath]
	if {[file exists $f]} {
	    return $f
	}
    }
    return
}

# Theme::FindIconFileWithSuffixes --
# 
#       Searches each path for matching image file with any of the suffixes.
#       Note the search order where the search paths have higher precedence
#       than the image formats.

proc ::Theme::FindIconFileWithSuffixes {spec suffL} {

    foreach path [GetPresentSearchPaths] {
	foreach suff $suffL {
	    set f [file join $path $spec$suff]
	    if {[file exists $f]} {
		return $f
	    }
	}
    }    
    return
}

proc ::Theme::FindIconSize {size name} {
    return [FindIcon icons/${size}x${size}/$name]
}

# Theme::Find16Icon, Find32Icon, Find64Icon --
#
#       Helper functions which look up images using the resource database.

proc ::Theme::Find16Icon {w resource} {    
    set rsrc [option get $w $resource {}]
    if {$rsrc ne ""} {
	return [FindIcon icons/16x16/[option get $w $resource {}]]
    } 
    return
}

proc ::Theme::Find32Icon {w resource} {    
    set rsrc [option get $w $resource {}]
    if {$rsrc ne ""} {
	return [FindIcon icons/32x32/[option get $w $resource {}]]
    } 
    return
}

proc ::Theme::Find64Icon {w resource} {    
    set rsrc [option get $w $resource {}]
    if {$rsrc ne ""} {
	return [FindIcon icons/64x64/[option get $w $resource {}]]
    } 
    return
}

proc ::Theme::Find128Icon {w resource} {    
    set rsrc [option get $w $resource {}]
    if {$rsrc ne ""} {
	return [FindIcon icons/128x128/[option get $w $resource {}]]
    } 
    return
}

proc ::Theme::Create16IconWithName {w resource} {
    set rsrc [option get $w $resource {}]
    if {$rsrc ne ""} {
	return [FindIconWithName icons/16x16/$rsrc $rsrc]
    } 
    return
}

# Theme::ThemeChanged --
#
#       Mainly refresh all theme images with updated versions.

proc ::Theme::ThemeChanged {} {
    
    set paths [GetPresentSearchPaths]
    set all [lsearch -inline -all [image names] ::theme::*]
    foreach name $all {
	set spec [string map {::theme:: ""} $name]
	set tmp [MakeIconFromPaths $spec "" $paths]
	$name copy $tmp -compositingrule set
	image delete $tmp
    }
}

# Theme::ReloadIcon --
#
#       Call this to get a refreshed image when theme changed.

proc ::Theme::ReloadIcon {spec} {

    set paths [GetPresentSearchPaths] 
    set name ::theme::$spec
    set tmp [MakeIconFromPaths $spec "" $paths]
    $name copy $tmp -compositingrule set
    image delete $tmp
}

#-------------------------------------------------------------------------------