OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [libgui/] [library/] [prefs.tcl] - Blame information for rev 1770

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# prefs.tcl - Preference handling.
2
# Copyright (C) 1997 Cygnus Solutions.
3
# Written by Tom Tromey <tromey@cygnus.com>.
4
 
5
# KNOWN BUGS:
6
# * When we move to the next tcl/itcl, rewrite to use namespaces and
7
#   possibly ensembles.
8
 
9
# Global state.
10
defarray PREFS_state {
11
  inhibit-event 0
12
  initialized 0
13
}
14
 
15
# This is called when a trace on some option fires.  It makes sure the
16
# relevant handlers get run.
17
proc PREFS_run_handlers {name1 name2 op} {
18
  upvar $name1 state
19
  set option [lindex $name2 0]
20
 
21
  global PREFS_state
22
  # Notify everybody else unless we've inhibited event generation.
23
  if {! $PREFS_state(inhibit-event) && $PREFS_state(ide_running)} then {
24
    ide_property set preference/$option $state([list $option value]) global
25
  }
26
 
27
  # Run local handlers.
28
  run_hooks PREFS_state([list $option handler]) $option \
29
    $state([list $option value])
30
}
31
 
32
# This is run when we see a property event.  It updates our internal
33
# state.
34
proc PREFS_handle_property_event {exists property value} {
35
  global PREFS_state
36
 
37
  # If it isn't a preference property, ignore it.
38
  if {! [string match preference/* $property]} then {
39
    return
40
  }
41
  # [string length preference/] == 11.
42
  set name [string range $property 11 end]
43
 
44
  if {$exists} then {
45
    incr PREFS_state(inhibit-event)
46
    set PREFS_state([list $name value]) $value
47
    incr PREFS_state(inhibit-event) -1
48
  } elseif {$PREFS_state(ide_running)} then {
49
    # It doesn't make sense to remove a property that mirrors some
50
    # preference.  So disallow by immediately redefining.  Use
51
    # initialize and not set because several clients are likely to run
52
    # this at once.
53
    ide_property initialize preference/$name \
54
      $PREFS_state([list $name value]) global
55
  }
56
}
57
 
58
# pref define NAME DEFAULT
59
# Define a new option
60
# NAME is the option name
61
# DEFAULT is the default value of the option
62
proc PREFS_cmd_define {name default} {
63
  global PREFS_state
64
 
65
  # If the option has already been defined, do nothing.
66
  if {[info exists PREFS_state([list $name value])]} then {
67
    return
68
  }
69
 
70
  if {$PREFS_state(ide_running)} then {
71
    # We only store the value in the database.
72
    ide_property initialize preference/$name $default global
73
    set default [ide_property get preference/$name]
74
  }
75
 
76
  # We set our internal state no matter what.  It is harmless if our
77
  # definition causes a property-set event.
78
  set PREFS_state([list $name value]) $default
79
  set PREFS_state([list $name handler]) {}
80
 
81
  # Set up a variable trace so that the handlers can be run.
82
  trace variable PREFS_state([list $name value]) w PREFS_run_handlers
83
}
84
 
85
# pref get NAME
86
# Return value of option NAME
87
proc PREFS_cmd_get {name} {
88
  global PREFS_state
89
  return $PREFS_state([list $name value])
90
}
91
 
92
# pref getd NAME
93
# Return value of option NAME
94
# or define it if necessary and return ""
95
proc PREFS_cmd_getd {name} {
96
  global PREFS_state
97
  PREFS_cmd_define $name ""
98
  return [pref get $name]
99
}
100
 
101
# pref varname NAME
102
# Return name of global variable that represents option NAME
103
# This is suitable for (eg) a -variable option on a radiobutton
104
proc PREFS_cmd_varname {name} {
105
  return PREFS_state([list $name value])
106
}
107
 
108
# pref set NAME VALUE
109
# Set the option NAME to VALUE
110
proc PREFS_cmd_set {name value} {
111
  global PREFS_state
112
 
113
  # For debugging purposes, make sure the preference has already been
114
  # defined.
115
  if {! [info exists PREFS_state([list $name value])]} then {
116
    error "attempt to set undefined preference $name"
117
  }
118
 
119
  set PREFS_state([list $name value]) $value
120
}
121
 
122
# pref setd NAME VALUE
123
# Set the option NAME to VALUE
124
# or define NAME and set the default to VALUE
125
proc PREFS_cmd_setd {name value} {
126
  global PREFS_state
127
 
128
  if {[info exists PREFS_state([list $name value])]} then {
129
    set PREFS_state([list $name value]) $value
130
  } else {
131
    PREFS_cmd_define $name $value
132
  }
133
}
134
 
135
# pref add_hook NAME HOOK
136
# Add a command to the hook that is run when the preference name NAME
137
# changes.  The command is run with the name of the changed option and
138
# the new value as arguments.
139
proc PREFS_cmd_add_hook {name hook} {
140
  add_hook PREFS_state([list $name handler]) $hook
141
}
142
 
143
# pref remove_hook NAME HOOK
144
# Remove a command from the per-preference hook.
145
proc PREFS_cmd_remove_hook {name hook} {
146
  remove_hook PREFS_state([list $name handler]) $hook
147
}
148
 
149
# pref init ?IDE_RUNNING?
150
# Initialize the preference module.  IDE_RUNNING is an optional
151
# boolean argument.  If 0, then the preference module will assume that
152
# it is not connected to the IDE backplane.  The default is based on
153
# the global variable IDE_ENABLED.
154
proc PREFS_cmd_init {{ide_running "unset"}} {
155
  global PREFS_state IDE_ENABLED
156
 
157
  if {! $PREFS_state(initialized)} then {
158
 
159
    if {$ide_running == "unset"} then {
160
      if {[info exists IDE_ENABLED]} then {
161
        set ide_running $IDE_ENABLED
162
      } else {
163
        set ide_running 0
164
      }
165
    }
166
 
167
    set PREFS_state(initialized) 1
168
    set PREFS_state(ide_running) $ide_running
169
    if {$ide_running} then {
170
      property add_hook "" PREFS_handle_property_event
171
    }
172
  }
173
}
174
 
175
# pref list
176
# Return a list of the names of all preferences defined by this
177
# application.
178
proc PREFS_cmd_list {} {
179
  global PREFS_state
180
 
181
  set list {}
182
  foreach item [array names PREFS_state] {
183
    if {[lindex $item 1] == "value"} then {
184
      lappend list [lindex $item 0]
185
    }
186
  }
187
 
188
  return $list
189
}
190
 
191
# The primary interface to all preference subcommands.
192
proc pref {dispatch args} {
193
  if {[info commands PREFS_cmd_$dispatch] == ""} then {
194
    error "unrecognized key \"$dispatch\""
195
  }
196
 
197
  eval PREFS_cmd_$dispatch $args
198
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.