source: trunk/gui/scripts/deviceEditor.tcl @ 115

Last change on this file since 115 was 115, checked in by mmc, 16 years ago

Updated all copyright notices.

File size: 5.3 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: deviceEditor - general-purpose device editor
3#
4#  This widget takes a <structure> description and chooses a specific
5#  editor appropriate for the contents of the structure.  If the
6#  <structure> contains a molecule, then it chooses a MoleculeViewer.
7#  If it contains a 1D device, then it chooses a deviceViewer1D.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2005  Purdue Research Foundation
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itk
16
17option add *DeviceEditor.autoCleanUp yes widgetDefault
18
19itcl::class Rappture::DeviceEditor {
20    inherit itk::Widget Rappture::ControlOwner
21
22    itk_option define -autocleanup autoCleanUp AutoCleanUp 1
23
24    constructor {owner args} {
25        Rappture::ControlOwner::constructor $owner
26    } { # defined below }
27
28    public method value {args}
29
30    protected method _redraw {}
31    protected method _type {xmlobj}
32
33    private variable _current ""  ;# active device editor
34}
35                                                                               
36itk::usual DeviceEditor {
37}
38
39# ----------------------------------------------------------------------
40# CONSTRUCTOR
41# ----------------------------------------------------------------------
42itcl::body Rappture::DeviceEditor::constructor {owner args} {
43    itk_component add top {
44        frame $itk_interior.top
45    }
46    pack $itk_component(top) -fill x
47
48    itk_component add editors {
49        Rappture::Notebook $itk_interior.editors
50    }
51    pack $itk_component(editors) -expand yes -fill both
52
53    eval itk_initialize $args
54}
55
56
57# ----------------------------------------------------------------------
58# USAGE: value ?-check? ?<newval>?
59#
60# Clients use this to query/set the value for this widget.  With
61# no args, it returns the current value for the widget.  If the
62# <newval> is specified, it sets the value of the widget and
63# sends a <<Value>> event.  If the -check flag is included, the
64# new value is not actually applied, but just checked for correctness.
65# ----------------------------------------------------------------------
66itcl::body Rappture::DeviceEditor::value {args} {
67    set onlycheck 0
68    set i [lsearch -exact $args -check]
69    if {$i >= 0} {
70        set onlycheck 1
71        set args [lreplace $args $i $i]
72    }
73
74    if {[llength $args] == 1} {
75        if {$_xmlobj != ""} {
76            if {$itk_option(-autocleanup)} {
77                # delete any existing object
78                itcl::delete object $_xmlobj
79            }
80            set _xmlobj ""
81        }
82
83        set newval [lindex $args 0]
84        if {$newval != ""} {
85            if {$onlycheck} {
86                return
87            }
88            if {![Rappture::library isvalid $newval]} {
89                error "bad value \"$newval\": should be Rappture::Library"
90            }
91            set _xmlobj $newval
92        }
93        _redraw
94        event generate $itk_component(hull) <<Value>>
95
96    } elseif {[llength $args] == 0} {
97        sync  ;# querying -- must sync controls with the value
98    } else {
99        error "wrong # args: should be \"value ?-check? ?newval?\""
100    }
101    return $_xmlobj
102}
103
104# ----------------------------------------------------------------------
105# USAGE: _redraw
106#
107# Used internally to load new device data into the appropriate
108# editor.  If the editor needs to be created, it is created and
109# activated within this widget.  Then, the data is loaded into
110# the editor.
111# ----------------------------------------------------------------------
112itcl::body Rappture::DeviceEditor::_redraw {} {
113    if {$_current != ""} {
114        $_current configure -device ""
115        set _current ""
116    }
117    switch -- [_type $_xmlobj] {
118        molecule {
119            if {[catch {$itk_component(editors) page molecule} p]} {
120                set p [$itk_component(editors) insert end molecule]
121                Rappture::MoleculeViewer $p.mol $this
122                pack $p.mol -expand yes -fill both
123            }
124            $p.mol configure -device $_xmlobj
125            $itk_component(editors) current molecule
126
127            set _current $p.mol
128        }
129        device1D {
130            if {[catch {$itk_component(editors) page device1D} p]} {
131                set p [$itk_component(editors) insert end device1D]
132                Rappture::DeviceViewer1D $p.dev $this
133                pack $p.dev -expand yes -fill both
134            }
135            $p.dev configure -device $_xmlobj
136            $itk_component(editors) current device1D
137
138            set _current $p.dev
139        }
140    }
141}
142
143# ----------------------------------------------------------------------
144# USAGE: _type <xmlobj>
145#
146# Used internally to determine the type of the device data stored
147# within the <xmlobj>.  Returns a name such as "molecule" or
148# "device1D" indicating the type of editor that should be used to
149# display the data.
150# ----------------------------------------------------------------------
151itcl::body Rappture::DeviceEditor::_type {xmlobj} {
152    if {$xmlobj == ""} {
153        return ""
154    }
155    if {[llength [$xmlobj children -type molecule components]] > 0} {
156        return "molecule"
157    }
158    return "device1D"
159}
Note: See TracBrowser for help on using the repository browser.