source: trunk/GSASIIctrls.py @ 2109

Last change on this file since 2109 was 2109, checked in by toby, 7 years ago

Andrey's enhancement: keep track of last GPX, import & export directories; optionally save the 1st two

  • Property svn:eol-style set to native
File size: 170.7 KB
Line 
1# -*- coding: utf-8 -*-
2#GSASIIctrls - Custom GSAS-II GUI controls
3########### SVN repository information ###################
4# $Date: $
5# $Author: $
6# $Revision: $
7# $URL: $
8# $Id: $
9########### SVN repository information ###################
10'''
11*GSASIIctrls: Custom GUI controls*
12-------------------------------------------
13
14A library of GUI controls for reuse throughout GSAS-II
15
16(at present many are still in GSASIIgrid, but with time will be moved here)
17
18'''
19import os
20import sys
21import wx
22import wx.grid as wg
23# import wx.wizard as wz
24import wx.aui
25import wx.lib.scrolledpanel as wxscroll
26import time
27import copy
28# import cPickle
29# import numpy as np
30# import numpy.ma as ma
31# import scipy.optimize as so
32import wx.html        # could postpone this for quicker startup
33import webbrowser     # could postpone this for quicker startup
34
35import GSASIIpath
36GSASIIpath.SetVersionNumber("$Revision: 1614 $")
37# import GSASIImath as G2mth
38# import GSASIIIO as G2IO
39# import GSASIIstrIO as G2stIO
40# import GSASIIlattice as G2lat
41# import GSASIIplot as G2plt
42import GSASIIpwdGUI as G2pdG
43# import GSASIIimgGUI as G2imG
44# import GSASIIphsGUI as G2phG
45# import GSASIIspc as G2spc
46# import GSASIImapvars as G2mv
47# import GSASIIconstrGUI as G2cnstG
48# import GSASIIrestrGUI as G2restG
49import GSASIIpy3 as G2py3
50# import GSASIIobj as G2obj
51# import GSASIIexprGUI as G2exG
52import GSASIIlog as log
53
54# Define a short names for convenience
55WHITE = (255,255,255)
56DULL_YELLOW = (230,230,190)
57VERY_LIGHT_GREY = wx.Colour(235,235,235)
58WACV = wx.ALIGN_CENTER_VERTICAL
59
60################################################################################
61#### Tree Control
62################################################################################
63class G2TreeCtrl(wx.TreeCtrl):
64    '''Create a wrapper around the standard TreeCtrl so we can "wrap"
65    various events.
66   
67    This logs when a tree item is selected (in :meth:`onSelectionChanged`)
68
69    This also wraps lists and dicts pulled out of the tree to track where
70    they were retrieved from.
71    '''
72    def __init__(self,parent=None,*args,**kwargs):
73        super(self.__class__,self).__init__(parent=parent,*args,**kwargs)
74        self.G2frame = parent.GetParent()
75        self.root = self.AddRoot('Loaded Data: ')
76        self.SelectionChanged = None
77        self.textlist = None
78        log.LogInfo['Tree'] = self
79
80    def _getTreeItemsList(self,item):
81        '''Get the full tree hierarchy from a reference to a tree item.
82        Note that this effectively hard-codes phase and histogram names in the
83        returned list. We may want to make these names relative in the future.
84        '''
85        textlist = [self.GetItemText(item)]
86        parent = self.GetItemParent(item)
87        while parent:
88            if parent == self.root: break
89            textlist.insert(0,self.GetItemText(parent))
90            parent = self.GetItemParent(parent)
91        return textlist
92
93    def onSelectionChanged(self,event):
94        '''Log each press on a tree item here.
95        '''
96        if self.SelectionChanged:
97            textlist = self._getTreeItemsList(event.GetItem())
98            if log.LogInfo['Logging'] and event.GetItem() != self.root:
99                textlist[0] = self.GetRelativeHistNum(textlist[0])
100                if textlist[0] == "Phases" and len(textlist) > 1:
101                    textlist[1] = self.GetRelativePhaseNum(textlist[1])
102                log.MakeTreeLog(textlist)
103            if textlist == self.textlist:
104                return      #same as last time - don't get it again
105            self.textlist = textlist
106            self.SelectionChanged(event)
107
108    def Bind(self,eventtype,handler,*args,**kwargs):
109        '''Override the Bind() function so that page change events can be trapped
110        '''
111        if eventtype == wx.EVT_TREE_SEL_CHANGED:
112            self.SelectionChanged = handler
113            wx.TreeCtrl.Bind(self,eventtype,self.onSelectionChanged)
114            return
115        wx.TreeCtrl.Bind(self,eventtype,handler,*args,**kwargs)
116
117    # commented out, disables Logging
118    # def GetItemPyData(self,*args,**kwargs):
119    #    '''Override the standard method to wrap the contents
120    #    so that the source can be logged when changed
121    #    '''
122    #    data = super(self.__class__,self).GetItemPyData(*args,**kwargs)
123    #    textlist = self._getTreeItemsList(args[0])
124    #    if type(data) is dict:
125    #        return log.dictLogged(data,textlist)
126    #    if type(data) is list:
127    #        return log.listLogged(data,textlist)
128    #    if type(data) is tuple: #N.B. tuples get converted to lists
129    #        return log.listLogged(list(data),textlist)
130    #    return data
131
132    def GetRelativeHistNum(self,histname):
133        '''Returns list with a histogram type and a relative number for that
134        histogram, or the original string if not a histogram
135        '''
136        histtype = histname.split()[0]
137        if histtype != histtype.upper(): # histograms (only) have a keyword all in caps
138            return histname
139        item, cookie = self.GetFirstChild(self.root)
140        i = 0
141        while item:
142            itemtext = self.GetItemText(item)
143            if itemtext == histname:
144                return histtype,i
145            elif itemtext.split()[0] == histtype:
146                i += 1
147            item, cookie = self.GetNextChild(self.root, cookie)
148        else:
149            raise Exception("Histogram not found: "+histname)
150
151    def ConvertRelativeHistNum(self,histtype,histnum):
152        '''Converts a histogram type and relative histogram number to a
153        histogram name in the current project
154        '''
155        item, cookie = self.GetFirstChild(self.root)
156        i = 0
157        while item:
158            itemtext = self.GetItemText(item)
159            if itemtext.split()[0] == histtype:
160                if i == histnum: return itemtext
161                i += 1
162            item, cookie = self.GetNextChild(self.root, cookie)
163        else:
164            raise Exception("Histogram #'+str(histnum)+' of type "+histtype+' not found')
165       
166    def GetRelativePhaseNum(self,phasename):
167        '''Returns a phase number if the string matches a phase name
168        or else returns the original string
169        '''
170        item, cookie = self.GetFirstChild(self.root)
171        while item:
172            itemtext = self.GetItemText(item)
173            if itemtext == "Phases":
174                parent = item
175                item, cookie = self.GetFirstChild(parent)
176                i = 0
177                while item:
178                    itemtext = self.GetItemText(item)
179                    if itemtext == phasename:
180                        return i
181                    item, cookie = self.GetNextChild(parent, cookie)
182                    i += 1
183                else:
184                    return phasename # not a phase name
185            item, cookie = self.GetNextChild(self.root, cookie)
186        else:
187            raise Exception("No phases found ")
188
189    def ConvertRelativePhaseNum(self,phasenum):
190        '''Converts relative phase number to a phase name in
191        the current project
192        '''
193        item, cookie = self.GetFirstChild(self.root)
194        while item:
195            itemtext = self.GetItemText(item)
196            if itemtext == "Phases":
197                parent = item
198                item, cookie = self.GetFirstChild(parent)
199                i = 0
200                while item:
201                    if i == phasenum:
202                        return self.GetItemText(item)
203                    item, cookie = self.GetNextChild(parent, cookie)
204                    i += 1
205                else:
206                    raise Exception("Phase "+str(phasenum)+" not found")
207            item, cookie = self.GetNextChild(self.root, cookie)
208        else:
209            raise Exception("No phases found ")
210
211    def GetImageLoc(self,TreeId):
212        '''Get Image data from the Tree. Handles cases where the
213        image name is specified, as well as where the image file name is
214        a tuple containing the image file and an image number
215        '''
216       
217        size,imagefile = self.GetItemPyData(TreeId)
218        if type(imagefile) is tuple or type(imagefile) is list:
219            return size,imagefile[0],imagefile[1]
220        else:
221            return size,imagefile,None
222
223################################################################################
224#### TextCtrl that stores input as entered with optional validation
225################################################################################
226class ValidatedTxtCtrl(wx.TextCtrl):
227    '''Create a TextCtrl widget that uses a validator to prevent the
228    entry of inappropriate characters and changes color to highlight
229    when invalid input is supplied. As valid values are typed,
230    they are placed into the dict or list where the initial value
231    came from. The type of the initial value must be int,
232    float or str or None (see :obj:`key` and :obj:`typeHint`);
233    this type (or the one in :obj:`typeHint`) is preserved.
234
235    Float values can be entered in the TextCtrl as numbers or also
236    as algebraic expressions using operators + - / \* () and \*\*,
237    in addition pi, sind(), cosd(), tand(), and sqrt() can be used,
238    as well as appreviations s, sin, c, cos, t, tan and sq.
239
240    :param wx.Panel parent: name of panel or frame that will be
241      the parent to the TextCtrl. Can be None.
242
243    :param dict/list loc: the dict or list with the initial value to be
244      placed in the TextCtrl.
245
246    :param int/str key: the dict key or the list index for the value to be
247      edited by the TextCtrl. The ``loc[key]`` element must exist, but may
248      have value None. If None, the type for the element is taken from
249      :obj:`typeHint` and the value for the control is set initially
250      blank (and thus invalid.) This is a way to specify a field without a
251      default value: a user must set a valid value.
252      If the value is not None, it must have a base
253      type of int, float, str or unicode; the TextCrtl will be initialized
254      from this value.
255     
256    :param list nDig: number of digits & places ([nDig,nPlc]) after decimal to use
257      for display of float. Alternately, None can be specified which causes
258      numbers to be displayed with approximately 5 significant figures
259      (Default=None).
260
261    :param bool notBlank: if True (default) blank values are invalid
262      for str inputs.
263     
264    :param number min: minimum allowed valid value. If None (default) the
265      lower limit is unbounded.
266
267    :param number max: maximum allowed valid value. If None (default) the
268      upper limit is unbounded
269
270    :param function OKcontrol: specifies a function or method that will be
271      called when the input is validated. The called function is supplied
272      with one argument which is False if the TextCtrl contains an invalid
273      value and True if the value is valid.
274      Note that this function should check all values
275      in the dialog when True, since other entries might be invalid.
276      The default for this is None, which indicates no function should
277      be called.
278
279    :param function OnLeave: specifies a function or method that will be
280      called when the focus for the control is lost.
281      The called function is supplied with (at present) three keyword arguments:
282
283         * invalid: (*bool*) True if the value for the TextCtrl is invalid
284         * value:   (*int/float/str*)  the value contained in the TextCtrl
285         * tc:      (*wx.TextCtrl*)  the TextCtrl name
286
287      The number of keyword arguments may be increased in the future should needs arise,
288      so it is best to code these functions with a \*\*kwargs argument so they will
289      continue to run without errors
290
291      The default for OnLeave is None, which indicates no function should
292      be called.
293
294    :param type typeHint: the value of typeHint is overrides the initial value
295      for the dict/list element ``loc[key]``, if set to
296      int or float, which specifies the type for input to the TextCtrl.
297      Defaults as None, which is ignored.
298
299    :param bool CIFinput: for str input, indicates that only printable
300      ASCII characters may be entered into the TextCtrl. Forces output
301      to be ASCII rather than Unicode. For float and int input, allows
302      use of a single '?' or '.' character as valid input.
303
304    :param dict OnLeaveArgs: a dict with keyword args that are passed to
305      the :attr:`OnLeave` function. Defaults to ``{}``
306
307    :param (other): other optional keyword parameters for the
308      wx.TextCtrl widget such as size or style may be specified.
309
310    '''
311    def __init__(self,parent,loc,key,nDig=None,notBlank=True,min=None,max=None,
312                 OKcontrol=None,OnLeave=None,typeHint=None,
313                 CIFinput=False, OnLeaveArgs={}, **kw):
314        # save passed values needed outside __init__
315        self.result = loc
316        self.key = key
317        self.nDig = nDig
318        self.OKcontrol=OKcontrol
319        self.OnLeave = OnLeave
320        self.OnLeaveArgs = OnLeaveArgs
321        self.CIFinput = CIFinput
322        self.notBlank = notBlank
323        self.type = str
324        # initialization
325        self.invalid = False   # indicates if the control has invalid contents
326        self.evaluated = False # set to True when the validator recognizes an expression
327        val = loc[key]
328        if 'style' in kw: # add a "Process Enter" to style
329            kw['style'] += kw['style'] | wx.TE_PROCESS_ENTER
330        else:
331            kw['style'] = wx.TE_PROCESS_ENTER
332        if isinstance(val,int) or typeHint is int:
333            self.type = int
334            wx.TextCtrl.__init__(
335                self,parent,wx.ID_ANY,
336                validator=NumberValidator(int,result=loc,key=key,
337                                          min=min,max=max,
338                                          OKcontrol=OKcontrol,
339                                          CIFinput=CIFinput),
340                **kw)
341            if val is not None:
342                self._setValue(val)
343            else: # no default is invalid for a number
344                self.invalid = True
345                self._IndicateValidity()
346
347        elif isinstance(val,float) or typeHint is float:
348            self.type = float
349            wx.TextCtrl.__init__(
350                self,parent,wx.ID_ANY,
351                validator=NumberValidator(float,result=loc,key=key,
352                                          min=min,max=max,
353                                          OKcontrol=OKcontrol,
354                                          CIFinput=CIFinput),
355                **kw)
356            if val is not None:
357                self._setValue(val)
358            else:
359                self.invalid = True
360                self._IndicateValidity()
361
362        elif isinstance(val,str) or isinstance(val,unicode) or typeHint is str:
363            if self.CIFinput:
364                wx.TextCtrl.__init__(
365                    self,parent,wx.ID_ANY,
366                    validator=ASCIIValidator(result=loc,key=key),
367                    **kw)
368            else:
369                wx.TextCtrl.__init__(self,parent,wx.ID_ANY,**kw)
370            if val is not None:
371                self.SetValue(val)
372            if notBlank:
373                self.Bind(wx.EVT_CHAR,self._onStringKey)
374                self.ShowStringValidity() # test if valid input
375            else:
376                self.invalid = False
377                self.Bind(wx.EVT_CHAR,self._GetStringValue)
378        elif val is None:
379            raise Exception,("ValidatedTxtCtrl error: value of "+str(key)+
380                             " element is None and typeHint not defined as int or float")
381        else:
382            raise Exception,("ValidatedTxtCtrl error: Unknown element ("+str(key)+
383                             ") type: "+str(type(val)))
384        # When the mouse is moved away or the widget loses focus,
385        # display the last saved value, if an expression
386        self.Bind(wx.EVT_LEAVE_WINDOW, self._onLeaveWindow)
387        self.Bind(wx.EVT_TEXT_ENTER, self._onLoseFocus)
388        self.Bind(wx.EVT_KILL_FOCUS, self._onLoseFocus)
389        # patch for wx 2.9 on Mac
390        i,j= wx.__version__.split('.')[0:2]
391        if int(i)+int(j)/10. > 2.8 and 'wxOSX' in wx.PlatformInfo:
392            self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
393
394    def SetValue(self,val):
395        if self.result is not None: # note that this bypasses formatting
396            self.result[self.key] = val
397            log.LogVarChange(self.result,self.key)
398        self._setValue(val)
399
400    def _setValue(self,val,show=True):
401        '''Check the validity of an int or float value and convert to a str.
402        Possibly format it. If show is True, display the formatted value in
403        the Text widget.
404        '''
405        self.invalid = False
406        if self.type is int:
407            try:
408                if int(val) != val:
409                    self.invalid = True
410                else:
411                    val = int(val)
412            except:
413                if self.CIFinput and (val == '?' or val == '.'):
414                    pass
415                else:
416                    self.invalid = True
417            if show: wx.TextCtrl.SetValue(self,str(val))
418        elif self.type is float:
419            try:
420                val = float(val) # convert strings, if needed
421            except:
422                if self.CIFinput and (val == '?' or val == '.'):
423                    pass
424                else:
425                    self.invalid = True
426            if self.nDig and show:
427                wx.TextCtrl.SetValue(self,str(G2py3.FormatValue(val,self.nDig)))
428            elif show:
429                wx.TextCtrl.SetValue(self,str(G2py3.FormatSigFigs(val)).rstrip('0'))
430        else:
431            if show: wx.TextCtrl.SetValue(self,str(val))
432            self.ShowStringValidity() # test if valid input
433            return
434       
435        self._IndicateValidity()
436        if self.OKcontrol:
437            self.OKcontrol(not self.invalid)
438
439    def OnKeyDown(self,event):
440        'Special callback for wx 2.9+ on Mac where backspace is not processed by validator'
441        key = event.GetKeyCode()
442        if key in [wx.WXK_BACK, wx.WXK_DELETE]:
443            if self.Validator: wx.CallAfter(self.Validator.TestValid,self)
444        if key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER:
445            self._onLoseFocus(None)
446        event.Skip()
447                   
448    def _onStringKey(self,event):
449        event.Skip()
450        if self.invalid: # check for validity after processing the keystroke
451            wx.CallAfter(self.ShowStringValidity,True) # was invalid
452        else:
453            wx.CallAfter(self.ShowStringValidity,False) # was valid
454
455    def _IndicateValidity(self):
456        'Set the control colors to show invalid input'
457        if self.invalid:
458            self.SetForegroundColour("red")
459            self.SetBackgroundColour("yellow")
460            self.SetFocus()
461            self.Refresh()
462        else: # valid input
463            self.SetBackgroundColour(
464                wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
465            self.SetForegroundColour("black")
466            self.Refresh()
467
468    def ShowStringValidity(self,previousInvalid=True):
469        '''Check if input is valid. Anytime the input is
470        invalid, call self.OKcontrol (if defined) because it is fast.
471        If valid, check for any other invalid entries only when
472        changing from invalid to valid, since that is slower.
473       
474        :param bool previousInvalid: True if the TextCtrl contents were
475          invalid prior to the current change.
476         
477        '''
478        val = self.GetValue().strip()
479        if self.notBlank:
480            self.invalid = not val
481        else:
482            self.invalid = False
483        self._IndicateValidity()
484        if self.invalid:
485            if self.OKcontrol:
486                self.OKcontrol(False)
487        elif self.OKcontrol and previousInvalid:
488            self.OKcontrol(True)
489        # always store the result
490        if self.CIFinput: # for CIF make results ASCII
491            self.result[self.key] = val.encode('ascii','replace') 
492        else:
493            self.result[self.key] = val
494        log.LogVarChange(self.result,self.key)
495
496    def _GetStringValue(self,event):
497        '''Get string input and store.
498        '''
499        event.Skip() # process keystroke
500        wx.CallAfter(self._SaveStringValue)
501       
502    def _SaveStringValue(self):
503        val = self.GetValue().strip()
504        # always store the result
505        if self.CIFinput: # for CIF make results ASCII
506            self.result[self.key] = val.encode('ascii','replace') 
507        else:
508            self.result[self.key] = val
509        log.LogVarChange(self.result,self.key)
510
511    def _onLeaveWindow(self,event):
512        '''If the mouse leaves the text box, save the result, if valid,
513        but (unlike _onLoseFocus) don't update the textbox contents.
514        '''
515        if self.evaluated and not self.invalid: # deal with computed expressions
516            self.evaluated = False # expression has been recast as value, reset flag
517        if self.invalid: # don't update an invalid expression
518            if event: event.Skip()
519            return
520        self._setValue(self.result[self.key],show=False) # save value quietly
521        if self.OnLeave: self.OnLeave(invalid=self.invalid,
522                                      value=self.result[self.key],
523                                      tc=self,
524                                      **self.OnLeaveArgs)
525        if event: event.Skip()
526           
527    def _onLoseFocus(self,event):
528        '''Enter has been pressed or focus transferred to another control,
529        Evaluate and update the current control contents
530        '''
531        if self.evaluated: # deal with computed expressions
532            if self.invalid: # don't substitute for an invalid expression
533                if event: event.Skip()
534                return 
535            self.evaluated = False # expression has been recast as value, reset flag
536            self._setValue(self.result[self.key])
537        elif self.result is not None: # show formatted result, as Bob wants
538            if not self.invalid: # don't update an invalid expression
539                self._setValue(self.result[self.key])
540        if self.OnLeave: self.OnLeave(invalid=self.invalid,
541                                      value=self.result[self.key],
542                                      tc=self,
543                                      **self.OnLeaveArgs)
544        if event: event.Skip()
545
546################################################################################
547class NumberValidator(wx.PyValidator):
548    '''A validator to be used with a TextCtrl to prevent
549    entering characters other than digits, signs, and for float
550    input, a period and exponents.
551   
552    The value is checked for validity after every keystroke
553      If an invalid number is entered, the box is highlighted.
554      If the number is valid, it is saved in result[key]
555
556    :param type typ: the base data type. Must be int or float.
557
558    :param bool positiveonly: If True, negative integers are not allowed
559      (default False). This prevents the + or - keys from being pressed.
560      Used with typ=int; ignored for typ=float.
561
562    :param number min: Minimum allowed value. If None (default) the
563      lower limit is unbounded
564
565    :param number max: Maximum allowed value. If None (default) the
566      upper limit is unbounded
567     
568    :param dict/list result: List or dict where value should be placed when valid
569
570    :param any key: key to use for result (int for list)
571
572    :param function OKcontrol: function or class method to control
573      an OK button for a window.
574      Ignored if None (default)
575
576    :param bool CIFinput: allows use of a single '?' or '.' character
577      as valid input.
578     
579    '''
580    def __init__(self, typ, positiveonly=False, min=None, max=None,
581                 result=None, key=None, OKcontrol=None, CIFinput=False):
582        'Create the validator'
583        wx.PyValidator.__init__(self)
584        # save passed parameters
585        self.typ = typ
586        self.positiveonly = positiveonly
587        self.min = min
588        self.max = max
589        self.result = result
590        self.key = key
591        self.OKcontrol = OKcontrol
592        self.CIFinput = CIFinput
593        # set allowed keys by data type
594        self.Bind(wx.EVT_CHAR, self.OnChar)
595        if self.typ == int and self.positiveonly:
596            self.validchars = '0123456789'
597        elif self.typ == int:
598            self.validchars = '0123456789+-'
599        elif self.typ == float:
600            # allow for above and sind, cosd, sqrt, tand, pi, and abbreviations
601            # also addition, subtraction, division, multiplication, exponentiation
602            self.validchars = '0123456789.-+eE/cosindcqrtap()*'
603        else:
604            self.validchars = None
605            return
606        if self.CIFinput:
607            self.validchars += '?.'
608    def Clone(self):
609        'Create a copy of the validator, a strange, but required component'
610        return NumberValidator(typ=self.typ, 
611                               positiveonly=self.positiveonly,
612                               min=self.min, max=self.max,
613                               result=self.result, key=self.key,
614                               OKcontrol=self.OKcontrol,
615                               CIFinput=self.CIFinput)
616    def TransferToWindow(self):
617        'Needed by validator, strange, but required component'
618        return True # Prevent wxDialog from complaining.
619    def TransferFromWindow(self):
620        'Needed by validator, strange, but required component'
621        return True # Prevent wxDialog from complaining.
622    def TestValid(self,tc):
623        '''Check if the value is valid by casting the input string
624        into the current type.
625
626        Set the invalid variable in the TextCtrl object accordingly.
627
628        If the value is valid, save it in the dict/list where
629        the initial value was stored, if appropriate.
630
631        :param wx.TextCtrl tc: A reference to the TextCtrl that the validator
632          is associated with.
633        '''
634        tc.invalid = False # assume valid
635        if self.CIFinput:
636            val = tc.GetValue().strip()
637            if val == '?' or val == '.':
638                self.result[self.key] = val
639                log.LogVarChange(self.result,self.key)
640                return
641        try:
642            val = self.typ(tc.GetValue())
643        except (ValueError, SyntaxError) as e:
644            if self.typ is float: # for float values, see if an expression can be evaluated
645                val = G2py3.FormulaEval(tc.GetValue())
646                if val is None:
647                    tc.invalid = True
648                    return
649                else:
650                    tc.evaluated = True
651            else: 
652                tc.invalid = True
653                return
654        # if self.max != None and self.typ == int:
655        #     if val > self.max:
656        #         tc.invalid = True
657        # if self.min != None and self.typ == int:
658        #     if val < self.min:
659        #         tc.invalid = True  # invalid
660        if self.max != None:
661            if val > self.max:
662                tc.invalid = True
663        if self.min != None:
664            if val < self.min:
665                tc.invalid = True  # invalid
666        if self.key is not None and self.result is not None and not tc.invalid:
667            self.result[self.key] = val
668            log.LogVarChange(self.result,self.key)
669
670    def ShowValidity(self,tc):
671        '''Set the control colors to show invalid input
672
673        :param wx.TextCtrl tc: A reference to the TextCtrl that the validator
674          is associated with.
675
676        '''
677        if tc.invalid:
678            tc.SetForegroundColour("red")
679            tc.SetBackgroundColour("yellow")
680            tc.SetFocus()
681            tc.Refresh()
682            return False
683        else: # valid input
684            tc.SetBackgroundColour(
685                wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
686            tc.SetForegroundColour("black")
687            tc.Refresh()
688            return True
689
690    def CheckInput(self,previousInvalid):
691        '''called to test every change to the TextCtrl for validity and
692        to change the appearance of the TextCtrl
693
694        Anytime the input is invalid, call self.OKcontrol
695        (if defined) because it is fast.
696        If valid, check for any other invalid entries only when
697        changing from invalid to valid, since that is slower.
698
699        :param bool previousInvalid: True if the TextCtrl contents were
700          invalid prior to the current change.
701        '''
702        tc = self.GetWindow()
703        self.TestValid(tc)
704        self.ShowValidity(tc)
705        # if invalid
706        if tc.invalid and self.OKcontrol:
707            self.OKcontrol(False)
708        if not tc.invalid and self.OKcontrol and previousInvalid:
709            self.OKcontrol(True)
710
711    def OnChar(self, event):
712        '''Called each type a key is pressed
713        ignores keys that are not allowed for int and float types
714        '''
715        key = event.GetKeyCode()
716        tc = self.GetWindow()
717        if key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER:
718            if tc.invalid:
719                self.CheckInput(True) 
720            else:
721                self.CheckInput(False) 
722            event.Skip()
723            return
724        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: # control characters get processed
725            event.Skip()
726            if tc.invalid:
727                wx.CallAfter(self.CheckInput,True) 
728            else:
729                wx.CallAfter(self.CheckInput,False) 
730            return
731        elif chr(key) in self.validchars: # valid char pressed?
732            event.Skip()
733            if tc.invalid:
734                wx.CallAfter(self.CheckInput,True) 
735            else:
736                wx.CallAfter(self.CheckInput,False) 
737            return
738        if not wx.Validator_IsSilent(): wx.Bell()
739        return  # Returning without calling event.Skip, which eats the keystroke
740
741################################################################################
742class ASCIIValidator(wx.PyValidator):
743    '''A validator to be used with a TextCtrl to prevent
744    entering characters other than ASCII characters.
745   
746    The value is checked for validity after every keystroke
747      If an invalid number is entered, the box is highlighted.
748      If the number is valid, it is saved in result[key]
749
750    :param dict/list result: List or dict where value should be placed when valid
751
752    :param any key: key to use for result (int for list)
753
754    '''
755    def __init__(self, result=None, key=None):
756        'Create the validator'
757        import string
758        wx.PyValidator.__init__(self)
759        # save passed parameters
760        self.result = result
761        self.key = key
762        self.validchars = string.ascii_letters + string.digits + string.punctuation + string.whitespace
763        self.Bind(wx.EVT_CHAR, self.OnChar)
764    def Clone(self):
765        'Create a copy of the validator, a strange, but required component'
766        return ASCIIValidator(result=self.result, key=self.key)
767        tc = self.GetWindow()
768        tc.invalid = False # make sure the validity flag is defined in parent
769    def TransferToWindow(self):
770        'Needed by validator, strange, but required component'
771        return True # Prevent wxDialog from complaining.
772    def TransferFromWindow(self):
773        'Needed by validator, strange, but required component'
774        return True # Prevent wxDialog from complaining.
775    def TestValid(self,tc):
776        '''Check if the value is valid by casting the input string
777        into ASCII.
778
779        Save it in the dict/list where the initial value was stored
780
781        :param wx.TextCtrl tc: A reference to the TextCtrl that the validator
782          is associated with.
783        '''
784        self.result[self.key] = tc.GetValue().encode('ascii','replace')
785        log.LogVarChange(self.result,self.key)
786
787    def OnChar(self, event):
788        '''Called each type a key is pressed
789        ignores keys that are not allowed for int and float types
790        '''
791        key = event.GetKeyCode()
792        tc = self.GetWindow()
793        if key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER:
794            self.TestValid(tc)
795            event.Skip()
796            return
797        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: # control characters get processed
798            event.Skip()
799            self.TestValid(tc)
800            return
801        elif chr(key) in self.validchars: # valid char pressed?
802            event.Skip()
803            self.TestValid(tc)
804            return
805        if not wx.Validator_IsSilent():
806            wx.Bell()
807        return  # Returning without calling event.Skip, which eats the keystroke
808
809################################################################################
810def HorizontalLine(sizer,parent):
811    '''Draws a horizontal line as wide as the window.
812    This shows up on the Mac as a very thin line, no matter what I do
813    '''
814    line = wx.StaticLine(parent,-1, size=(-1,3), style=wx.LI_HORIZONTAL)
815    sizer.Add(line, 0, wx.EXPAND|wx.ALIGN_CENTER|wx.ALL, 10)
816
817################################################################################
818class G2LoggedButton(wx.Button):
819    '''A version of wx.Button that creates logging events. Bindings are saved
820    in the object, and are looked up rather than directly set with a bind.
821    An index to these buttons is saved as log.ButtonBindingLookup
822    :param wx.Panel parent: parent widget
823    :param int id: Id for button
824    :param str label: label for button
825    :param str locationcode: a label used internally to uniquely indentify the button
826    :param function handler: a routine to call when the button is pressed
827    '''
828    def __init__(self,parent,id=wx.ID_ANY,label='',locationcode='',
829                 handler=None,*args,**kwargs):
830        super(self.__class__,self).__init__(parent,id,label,*args,**kwargs)
831        self.label = label
832        self.handler = handler
833        self.locationcode = locationcode
834        key = locationcode + '+' + label # hash code to find button
835        self.Bind(wx.EVT_BUTTON,self.onPress)
836        log.ButtonBindingLookup[key] = self
837    def onPress(self,event):
838        'create log event and call handler'
839        log.MakeButtonLog(self.locationcode,self.label)
840        self.handler(event)
841       
842################################################################################
843class EnumSelector(wx.ComboBox):
844    '''A customized :class:`wxpython.ComboBox` that selects items from a list
845    of choices, but sets a dict (list) entry to the corresponding
846    entry from the input list of values.
847
848    :param wx.Panel parent: the parent to the :class:`~wxpython.ComboBox` (usually a
849      frame or panel)
850    :param dict dct: a dict (or list) to contain the value set
851      for the :class:`~wxpython.ComboBox`.
852    :param item: the dict key (or list index) where ``dct[item]`` will
853      be set to the value selected in the :class:`~wxpython.ComboBox`. Also, dct[item]
854      contains the starting value shown in the widget. If the value
855      does not match an entry in :data:`values`, the first value
856      in :data:`choices` is used as the default, but ``dct[item]`` is
857      not changed.   
858    :param list choices: a list of choices to be displayed to the
859      user such as
860      ::
861     
862      ["default","option 1","option 2",]
863
864      Note that these options will correspond to the entries in
865      :data:`values` (if specified) item by item.
866    :param list values: a list of values that correspond to
867      the options in :data:`choices`, such as
868      ::
869     
870      [0,1,2]
871     
872      The default for :data:`values` is to use the same list as
873      specified for :data:`choices`.
874    :param (other): additional keyword arguments accepted by
875      :class:`~wxpython.ComboBox` can be specified.
876    '''
877    def __init__(self,parent,dct,item,choices,values=None,**kw):
878        if values is None:
879            values = choices
880        if dct[item] in values:
881            i = values.index(dct[item])
882        else:
883            i = 0
884        startval = choices[i]
885        wx.ComboBox.__init__(self,parent,wx.ID_ANY,startval,
886                             choices = choices,
887                             style=wx.CB_DROPDOWN|wx.CB_READONLY,
888                             **kw)
889        self.choices = choices
890        self.values = values
891        self.dct = dct
892        self.item = item
893        self.Bind(wx.EVT_COMBOBOX, self.onSelection)
894    def onSelection(self,event):
895        # respond to a selection by setting the enum value in the CIF dictionary
896        if self.GetValue() in self.choices: # should always be true!
897            self.dct[self.item] = self.values[self.choices.index(self.GetValue())]
898        else:
899            self.dct[self.item] = self.values[0] # unknown
900
901################################################################################
902class G2ChoiceButton(wx.Choice):
903    '''A customized version of a wx.Choice that automatically initializes
904    the control to match a supplied value and saves the choice directly
905    into an array or list. Optionally a function can be called each time a
906    choice is selected. The widget can be used with an array item that is set to
907    to the choice by number (``indLoc[indKey]``) or by string value
908    (``strLoc[strKey]``) or both. The initial value is taken from ``indLoc[indKey]``
909    if not None or ``strLoc[strKey]`` if not None.
910
911    :param wx.Panel parent: name of panel or frame that will be
912      the parent to the widget. Can be None.
913    :param list choiceList: a list or tuple of choices to offer the user.
914    :param dict/list indLoc: a dict or list with the initial value to be
915      placed in the Choice button. If this is None, this is ignored.
916    :param int/str indKey: the dict key or the list index for the value to be
917      edited by the Choice button. If indLoc is not None then this
918      must be specified and the ``indLoc[indKey]`` will be set. If the value
919      for ``indLoc[indKey]`` is not None, it should be an integer in
920      range(len(choiceList)). The Choice button will be initialized to the
921      choice corresponding to the value in this element if not None.
922    :param dict/list strLoc: a dict or list with the string value corresponding to
923      indLoc/indKey. Default (None) means that this is not used.
924    :param int/str strKey: the dict key or the list index for the string value
925      The ``strLoc[strKey]`` element must exist or strLoc must be None (default).
926    :param function onChoice: name of a function to call when the choice is made.
927    '''
928    def __init__(self,parent,choiceList,indLoc=None,indKey=None,strLoc=None,strKey=None,
929                 onChoice=None,**kwargs):
930        wx.Choice.__init__(self,parent,choices=choiceList,id=wx.ID_ANY,**kwargs)
931        self.choiceList = choiceList
932        self.indLoc = indLoc
933        self.indKey = indKey
934        self.strLoc = strLoc
935        self.strKey = strKey
936        self.onChoice = None
937        self.SetSelection(wx.NOT_FOUND)
938        if self.indLoc is not None and self.indLoc.get(self.indKey) is not None:
939            self.SetSelection(self.indLoc[self.indKey])
940            if self.strLoc is not None:
941                self.strLoc[self.strKey] = self.GetStringSelection()
942                log.LogVarChange(self.strLoc,self.strKey)
943        elif self.strLoc is not None and self.strLoc.get(self.strKey) is not None:
944            try:
945                self.SetSelection(choiceList.index(self.strLoc[self.strKey]))
946                if self.indLoc is not None:
947                    self.indLoc[self.indKey] = self.GetSelection()
948                    log.LogVarChange(self.indLoc,self.indKey)
949            except ValueError:
950                pass
951        self.Bind(wx.EVT_CHOICE, self._OnChoice)
952        #if self.strLoc is not None: # make sure strLoc gets initialized
953        #    self._OnChoice(None) # note that onChoice will not be called
954        self.onChoice = onChoice
955    def _OnChoice(self,event):
956        if self.indLoc is not None:
957            self.indLoc[self.indKey] = self.GetSelection()
958            log.LogVarChange(self.indLoc,self.indKey)
959        if self.strLoc is not None:
960            self.strLoc[self.strKey] = self.GetStringSelection()
961            log.LogVarChange(self.strLoc,self.strKey)
962        if self.onChoice:
963            self.onChoice()
964
965############################################################### Custom checkbox that saves values into dict/list as used
966class G2CheckBox(wx.CheckBox):
967    '''A customized version of a CheckBox that automatically initializes
968    the control to a supplied list or dict entry and updates that
969    entry as the widget is used.
970
971    :param wx.Panel parent: name of panel or frame that will be
972      the parent to the widget. Can be None.
973    :param str label: text to put on check button
974    :param dict/list loc: the dict or list with the initial value to be
975      placed in the CheckBox.
976    :param int/str key: the dict key or the list index for the value to be
977      edited by the CheckBox. The ``loc[key]`` element must exist.
978      The CheckBox will be initialized from this value.
979      If the value is anything other that True (or 1), it will be taken as
980      False.
981    :param function OnChange: specifies a function or method that will be
982      called when the CheckBox is changed (Default is None).
983      The called function is supplied with one argument, the calling event.
984    '''
985    def __init__(self,parent,label,loc,key,OnChange=None):
986        wx.CheckBox.__init__(self,parent,id=wx.ID_ANY,label=label)
987        self.loc = loc
988        self.key = key
989        self.OnChange = OnChange
990        self.SetValue(self.loc[self.key]==True)
991        self.Bind(wx.EVT_CHECKBOX, self._OnCheckBox)
992    def _OnCheckBox(self,event):
993        self.loc[self.key] = self.GetValue()
994        log.LogVarChange(self.loc,self.key)
995        if self.OnChange: self.OnChange(event)
996                   
997################################################################################
998#### Commonly used dialogs
999################################################################################
1000def CallScrolledMultiEditor(parent,dictlst,elemlst,prelbl=[],postlbl=[],
1001                 title='Edit items',header='',size=(300,250),
1002                             CopyButton=False, **kw):
1003    '''Shell routine to call a ScrolledMultiEditor dialog. See
1004    :class:`ScrolledMultiEditor` for parameter definitions.
1005
1006    :returns: True if the OK button is pressed; False if the window is closed
1007      with the system menu or the Cancel button.
1008
1009    '''
1010    dlg = ScrolledMultiEditor(parent,dictlst,elemlst,prelbl,postlbl,
1011                              title,header,size,
1012                              CopyButton, **kw)
1013    if dlg.ShowModal() == wx.ID_OK:
1014        dlg.Destroy()
1015        return True
1016    else:
1017        dlg.Destroy()
1018        return False
1019
1020################################################################################
1021class ScrolledMultiEditor(wx.Dialog):
1022    '''Define a window for editing a potentially large number of dict- or
1023    list-contained values with validation for each item. Edited values are
1024    automatically placed in their source location. If invalid entries
1025    are provided, the TextCtrl is turned yellow and the OK button is disabled.
1026
1027    The type for each TextCtrl validation is determined by the
1028    initial value of the entry (int, float or string).
1029    Float values can be entered in the TextCtrl as numbers or also
1030    as algebraic expressions using operators + - / \* () and \*\*,
1031    in addition pi, sind(), cosd(), tand(), and sqrt() can be used,
1032    as well as appreviations s(), sin(), c(), cos(), t(), tan() and sq().
1033
1034    :param wx.Frame parent: name of parent window, or may be None
1035
1036    :param tuple dictlst: a list of dicts or lists containing values to edit
1037
1038    :param tuple elemlst: a list of keys for each item in a dictlst. Must have the
1039      same length as dictlst.
1040
1041    :param wx.Frame parent: name of parent window, or may be None
1042   
1043    :param tuple prelbl: a list of labels placed before the TextCtrl for each
1044      item (optional)
1045   
1046    :param tuple postlbl: a list of labels placed after the TextCtrl for each
1047      item (optional)
1048
1049    :param str title: a title to place in the frame of the dialog
1050
1051    :param str header: text to place at the top of the window. May contain
1052      new line characters.
1053
1054    :param wx.Size size: a size parameter that dictates the
1055      size for the scrolled region of the dialog. The default is
1056      (300,250).
1057
1058    :param bool CopyButton: if True adds a small button that copies the
1059      value for the current row to all fields below (default is False)
1060     
1061    :param list minvals: optional list of minimum values for validation
1062      of float or int values. Ignored if value is None.
1063    :param list maxvals: optional list of maximum values for validation
1064      of float or int values. Ignored if value is None.
1065    :param list sizevals: optional list of wx.Size values for each input
1066      widget. Ignored if value is None.
1067     
1068    :param tuple checkdictlst: an optional list of dicts or lists containing bool
1069      values (similar to dictlst).
1070    :param tuple checkelemlst: an optional list of dicts or lists containing bool
1071      key values (similar to elemlst). Must be used with checkdictlst.
1072    :param string checklabel: a string to use for each checkbutton
1073     
1074    :returns: the wx.Dialog created here. Use method .ShowModal() to display it.
1075   
1076    *Example for use of ScrolledMultiEditor:*
1077
1078    ::
1079
1080        dlg = <pkg>.ScrolledMultiEditor(frame,dictlst,elemlst,prelbl,postlbl,
1081                                        header=header)
1082        if dlg.ShowModal() == wx.ID_OK:
1083             for d,k in zip(dictlst,elemlst):
1084                 print d[k]
1085
1086    *Example definitions for dictlst and elemlst:*
1087
1088    ::
1089     
1090          dictlst = (dict1,list1,dict1,list1)
1091          elemlst = ('a', 1, 2, 3)
1092
1093      This causes items dict1['a'], list1[1], dict1[2] and list1[3] to be edited.
1094   
1095    Note that these items must have int, float or str values assigned to
1096    them. The dialog will force these types to be retained. String values
1097    that are blank are marked as invalid.
1098    '''
1099   
1100    def __init__(self,parent,dictlst,elemlst,prelbl=[],postlbl=[],
1101                 title='Edit items',header='',size=(300,250),
1102                 CopyButton=False,
1103                 minvals=[],maxvals=[],sizevals=[],
1104                 checkdictlst=[], checkelemlst=[], checklabel=""):
1105        if len(dictlst) != len(elemlst):
1106            raise Exception,"ScrolledMultiEditor error: len(dictlst) != len(elemlst) "+str(len(dictlst))+" != "+str(len(elemlst))
1107        if len(checkdictlst) != len(checkelemlst):
1108            raise Exception,"ScrolledMultiEditor error: len(checkdictlst) != len(checkelemlst) "+str(len(checkdictlst))+" != "+str(len(checkelemlst))
1109        wx.Dialog.__init__( # create dialog & sizer
1110            self,parent,wx.ID_ANY,title,
1111            style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
1112        mainSizer = wx.BoxSizer(wx.VERTICAL)
1113        self.orig = []
1114        self.dictlst = dictlst
1115        self.elemlst = elemlst
1116        self.checkdictlst = checkdictlst
1117        self.checkelemlst = checkelemlst
1118        self.StartCheckValues = [checkdictlst[i][checkelemlst[i]] for i in range(len(checkdictlst))]
1119        self.ButtonIndex = {}
1120        for d,i in zip(dictlst,elemlst):
1121            self.orig.append(d[i])
1122        # add a header if supplied
1123        if header:
1124            subSizer = wx.BoxSizer(wx.HORIZONTAL)
1125            subSizer.Add((-1,-1),1,wx.EXPAND)
1126            subSizer.Add(wx.StaticText(self,wx.ID_ANY,header))
1127            subSizer.Add((-1,-1),1,wx.EXPAND)
1128            mainSizer.Add(subSizer,0,wx.EXPAND,0)
1129        # make OK button now, because we will need it for validation
1130        self.OKbtn = wx.Button(self, wx.ID_OK)
1131        self.OKbtn.SetDefault()
1132        # create scrolled panel and sizer
1133        panel = wxscroll.ScrolledPanel(self, wx.ID_ANY,size=size,
1134            style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER)
1135        cols = 4
1136        if CopyButton: cols += 1
1137        subSizer = wx.FlexGridSizer(cols=cols,hgap=2,vgap=2)
1138        self.ValidatedControlsList = [] # make list of TextCtrls
1139        self.CheckControlsList = [] # make list of CheckBoxes
1140        for i,(d,k) in enumerate(zip(dictlst,elemlst)):
1141            if i >= len(prelbl): # label before TextCtrl, or put in a blank
1142                subSizer.Add((-1,-1)) 
1143            else:
1144                subSizer.Add(wx.StaticText(panel,wx.ID_ANY,str(prelbl[i])))
1145            kargs = {}
1146            if i < len(minvals):
1147                if minvals[i] is not None: kargs['min']=minvals[i]
1148            if i < len(maxvals):
1149                if maxvals[i] is not None: kargs['max']=maxvals[i]
1150            if i < len(sizevals):
1151                if sizevals[i]: kargs['size']=sizevals[i]
1152            if CopyButton:
1153                import wx.lib.colourselect as wscs
1154                but = wscs.ColourSelect(label='v', # would like to use u'\u2193' or u'\u25BC' but not in WinXP
1155                                        # is there a way to test?
1156                                        parent=panel,
1157                                        colour=(255,255,200),
1158                                        size=wx.Size(30,23),
1159                                        style=wx.RAISED_BORDER)
1160                but.Bind(wx.EVT_BUTTON, self._OnCopyButton)
1161                but.SetToolTipString('Press to copy adjacent value to all rows below')
1162                self.ButtonIndex[but] = i
1163                subSizer.Add(but)
1164            # create the validated TextCrtl, store it and add it to the sizer
1165            ctrl = ValidatedTxtCtrl(panel,d,k,OKcontrol=self.ControlOKButton,
1166                                    **kargs)
1167            self.ValidatedControlsList.append(ctrl)
1168            subSizer.Add(ctrl)
1169            if i < len(postlbl): # label after TextCtrl, or put in a blank
1170                subSizer.Add(wx.StaticText(panel,wx.ID_ANY,str(postlbl[i])))
1171            else:
1172                subSizer.Add((-1,-1))
1173            if i < len(checkdictlst):
1174                ch = G2CheckBox(panel,checklabel,checkdictlst[i],checkelemlst[i])
1175                self.CheckControlsList.append(ch)
1176                subSizer.Add(ch)                   
1177            else:
1178                subSizer.Add((-1,-1))
1179        # finish up ScrolledPanel
1180        panel.SetSizer(subSizer)
1181        panel.SetAutoLayout(1)
1182        panel.SetupScrolling()
1183        # patch for wx 2.9 on Mac
1184        i,j= wx.__version__.split('.')[0:2]
1185        if int(i)+int(j)/10. > 2.8 and 'wxOSX' in wx.PlatformInfo:
1186            panel.SetMinSize((subSizer.GetSize()[0]+30,panel.GetSize()[1]))       
1187        mainSizer.Add(panel,1, wx.ALL|wx.EXPAND,1)
1188
1189        # Sizer for OK/Close buttons. N.B. on Close changes are discarded
1190        # by restoring the initial values
1191        btnsizer = wx.BoxSizer(wx.HORIZONTAL)
1192        btnsizer.Add(self.OKbtn)
1193        btn = wx.Button(self, wx.ID_CLOSE,"Cancel") 
1194        btn.Bind(wx.EVT_BUTTON,self._onClose)
1195        btnsizer.Add(btn)
1196        mainSizer.Add(btnsizer, 0, wx.ALIGN_CENTER|wx.ALL, 5)
1197        # size out the window. Set it to be enlarged but not made smaller
1198        self.SetSizer(mainSizer)
1199        mainSizer.Fit(self)
1200        self.SetMinSize(self.GetSize())
1201
1202    def _OnCopyButton(self,event):
1203        'Implements the copy down functionality'
1204        but = event.GetEventObject()
1205        n = self.ButtonIndex.get(but)
1206        if n is None: return
1207        for i,(d,k,ctrl) in enumerate(zip(self.dictlst,self.elemlst,self.ValidatedControlsList)):
1208            if i < n: continue
1209            if i == n:
1210                val = d[k]
1211                continue
1212            d[k] = val
1213            ctrl.SetValue(val)
1214        for i in range(len(self.checkdictlst)):
1215            if i < n: continue
1216            self.checkdictlst[i][self.checkelemlst[i]] = self.checkdictlst[n][self.checkelemlst[n]]
1217            self.CheckControlsList[i].SetValue(self.checkdictlst[i][self.checkelemlst[i]])
1218    def _onClose(self,event):
1219        'Used on Cancel: Restore original values & close the window'
1220        for d,i,v in zip(self.dictlst,self.elemlst,self.orig):
1221            d[i] = v
1222        for i in range(len(self.checkdictlst)):
1223            self.checkdictlst[i][self.checkelemlst[i]] = self.StartCheckValues[i]
1224        self.EndModal(wx.ID_CANCEL)
1225       
1226    def ControlOKButton(self,setvalue):
1227        '''Enable or Disable the OK button for the dialog. Note that this is
1228        passed into the ValidatedTxtCtrl for use by validators.
1229
1230        :param bool setvalue: if True, all entries in the dialog are
1231          checked for validity. if False then the OK button is disabled.
1232
1233        '''
1234        if setvalue: # turn button on, do only if all controls show as valid
1235            for ctrl in self.ValidatedControlsList:
1236                if ctrl.invalid:
1237                    self.OKbtn.Disable()
1238                    return
1239            else:
1240                self.OKbtn.Enable()
1241        else:
1242            self.OKbtn.Disable()
1243
1244###############################################  Multichoice Dialog with set all, toggle & filter options
1245class G2MultiChoiceDialog(wx.Dialog):
1246    '''A dialog similar to MultiChoiceDialog except that buttons are
1247    added to set all choices and to toggle all choices.
1248
1249    :param wx.Frame ParentFrame: reference to parent frame
1250    :param str title: heading above list of choices
1251    :param str header: Title to place on window frame
1252    :param list ChoiceList: a list of choices where one will be selected
1253    :param bool toggle: If True (default) the toggle and select all buttons
1254      are displayed
1255    :param bool monoFont: If False (default), use a variable-spaced font;
1256      if True use a equally-spaced font.
1257    :param bool filterBox: If True (default) an input widget is placed on
1258      the window and only entries matching the entered text are shown.
1259    :param kw: optional keyword parameters for the wx.Dialog may
1260      be included such as size [which defaults to `(320,310)`] and
1261      style (which defaults to `wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.CENTRE| wx.OK | wx.CANCEL`);
1262      note that `wx.OK` and `wx.CANCEL` controls
1263      the presence of the eponymous buttons in the dialog.
1264    :returns: the name of the created dialog 
1265    '''
1266    def __init__(self,parent, title, header, ChoiceList, toggle=True,
1267                 monoFont=False, filterBox=True, **kw):
1268        # process keyword parameters, notably style
1269        options = {'size':(320,310), # default Frame keywords
1270                   'style':wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.CENTRE| wx.OK | wx.CANCEL,
1271                   }
1272        options.update(kw)
1273        self.ChoiceList = ChoiceList # list of choices (list of str values)
1274        self.Selections = len(self.ChoiceList) * [False,] # selection status for each choice (list of bools)
1275        self.filterlist = range(len(self.ChoiceList)) # list of the choice numbers that have been filtered (list of int indices)
1276        if options['style'] & wx.OK:
1277            useOK = True
1278            options['style'] ^= wx.OK
1279        else:
1280            useOK = False
1281        if options['style'] & wx.CANCEL:
1282            useCANCEL = True
1283            options['style'] ^= wx.CANCEL
1284        else:
1285            useCANCEL = False       
1286        # create the dialog frame
1287        wx.Dialog.__init__(self,parent,wx.ID_ANY,header,**options)
1288        # fill the dialog
1289        Sizer = wx.BoxSizer(wx.VERTICAL)
1290        topSizer = wx.BoxSizer(wx.HORIZONTAL)
1291        topSizer.Add(wx.StaticText(self,wx.ID_ANY,title,size=(-1,35)),
1292            1,wx.ALL|wx.EXPAND|WACV,1)
1293        if filterBox:
1294            self.timer = wx.Timer()
1295            self.timer.Bind(wx.EVT_TIMER,self.Filter)
1296            topSizer.Add(wx.StaticText(self,wx.ID_ANY,'Name \nFilter: '),0,wx.ALL|WACV,1)
1297            self.filterBox = wx.TextCtrl(self, wx.ID_ANY, size=(80,-1),style=wx.TE_PROCESS_ENTER)
1298            self.filterBox.Bind(wx.EVT_TEXT,self.onChar)
1299            self.filterBox.Bind(wx.EVT_TEXT_ENTER,self.Filter)
1300            topSizer.Add(self.filterBox,0,wx.ALL|WACV,0)
1301        Sizer.Add(topSizer,0,wx.ALL|wx.EXPAND,8)
1302        self.settingRange = False
1303        self.rangeFirst = None
1304        self.clb = wx.CheckListBox(self, wx.ID_ANY, (30,30), wx.DefaultSize, ChoiceList)
1305        self.clb.Bind(wx.EVT_CHECKLISTBOX,self.OnCheck)
1306        if monoFont:
1307            font1 = wx.Font(self.clb.GetFont().GetPointSize(),
1308                            wx.MODERN, wx.NORMAL, wx.NORMAL, False)
1309            self.clb.SetFont(font1)
1310        Sizer.Add(self.clb,1,wx.LEFT|wx.RIGHT|wx.EXPAND,10)
1311        Sizer.Add((-1,10))
1312        # set/toggle buttons
1313        if toggle:
1314            tSizer = wx.FlexGridSizer(cols=2,hgap=5,vgap=5)
1315            setBut = wx.Button(self,wx.ID_ANY,'Set All')
1316            setBut.Bind(wx.EVT_BUTTON,self._SetAll)
1317            tSizer.Add(setBut)
1318            togBut = wx.Button(self,wx.ID_ANY,'Toggle All')
1319            togBut.Bind(wx.EVT_BUTTON,self._ToggleAll)
1320            tSizer.Add(togBut)
1321            self.rangeBut = wx.ToggleButton(self,wx.ID_ANY,'Set Range')
1322            self.rangeBut.Bind(wx.EVT_TOGGLEBUTTON,self.SetRange)
1323            tSizer.Add(self.rangeBut)           
1324            self.rangeCapt = wx.StaticText(self,wx.ID_ANY,'')
1325            tSizer.Add(self.rangeCapt)
1326            Sizer.Add(tSizer,0,wx.LEFT,12)
1327        # OK/Cancel buttons
1328        btnsizer = wx.StdDialogButtonSizer()
1329        if useOK:
1330            self.OKbtn = wx.Button(self, wx.ID_OK)
1331            self.OKbtn.SetDefault()
1332            btnsizer.AddButton(self.OKbtn)
1333        if useCANCEL:
1334            btn = wx.Button(self, wx.ID_CANCEL)
1335            btnsizer.AddButton(btn)
1336        btnsizer.Realize()
1337        Sizer.Add((-1,5))
1338        Sizer.Add(btnsizer,0,wx.ALIGN_RIGHT,50)
1339        Sizer.Add((-1,20))
1340        # OK done, let's get outa here
1341        self.SetSizer(Sizer)
1342        self.CenterOnParent()
1343
1344    def SetRange(self,event):
1345        '''Respond to a press of the Set Range button. Set the range flag and
1346        the caption next to the button
1347        '''
1348        self.settingRange = self.rangeBut.GetValue()
1349        if self.settingRange:
1350            self.rangeCapt.SetLabel('Select range start')
1351        else:
1352            self.rangeCapt.SetLabel('')           
1353        self.rangeFirst = None
1354       
1355    def GetSelections(self):
1356        'Returns a list of the indices for the selected choices'
1357        # update self.Selections with settings for displayed items
1358        for i in range(len(self.filterlist)):
1359            self.Selections[self.filterlist[i]] = self.clb.IsChecked(i)
1360        # return all selections, shown or hidden
1361        return [i for i in range(len(self.Selections)) if self.Selections[i]]
1362       
1363    def SetSelections(self,selList):
1364        '''Sets the selection indices in selList as selected. Resets any previous
1365        selections for compatibility with wx.MultiChoiceDialog. Note that
1366        the state for only the filtered items is shown.
1367
1368        :param list selList: indices of items to be selected. These indices
1369          are referenced to the order in self.ChoiceList
1370        '''
1371        self.Selections = len(self.ChoiceList) * [False,] # reset selections
1372        for sel in selList:
1373            self.Selections[sel] = True
1374        self._ShowSelections()
1375
1376    def _ShowSelections(self):
1377        'Show the selection state for displayed items'
1378        self.clb.SetChecked(
1379            [i for i in range(len(self.filterlist)) if self.Selections[self.filterlist[i]]]
1380            ) # Note anything previously checked will be cleared.
1381           
1382    def _SetAll(self,event):
1383        'Set all viewed choices on'
1384        self.clb.SetChecked(range(len(self.filterlist)))
1385       
1386    def _ToggleAll(self,event):
1387        'flip the state of all viewed choices'
1388        for i in range(len(self.filterlist)):
1389            self.clb.Check(i,not self.clb.IsChecked(i))
1390           
1391    def onChar(self,event):
1392        'Respond to keyboard events in the Filter box'
1393        self.OKbtn.Enable(False)
1394        if self.timer.IsRunning():
1395            self.timer.Stop()
1396        self.timer.Start(1000,oneShot=True)
1397        event.Skip()
1398       
1399    def OnCheck(self,event):
1400        '''for CheckListBox events; if Set Range is in use, this sets/clears all
1401        entries in range between start and end according to the value in start.
1402        Repeated clicks on the start change the checkbox state, but do not trigger
1403        the range copy.
1404        The caption next to the button is updated on the first button press.
1405        '''
1406        if self.settingRange:
1407            id = event.GetInt()
1408            if self.rangeFirst is None:
1409                name = self.clb.GetString(id)
1410                self.rangeCapt.SetLabel(name+' to...')
1411                self.rangeFirst = id
1412            elif self.rangeFirst == id:
1413                pass
1414            else:
1415                for i in range(min(self.rangeFirst,id), max(self.rangeFirst,id)+1):
1416                    self.clb.Check(i,self.clb.IsChecked(self.rangeFirst))
1417                self.rangeBut.SetValue(False)
1418                self.rangeCapt.SetLabel('')
1419            return
1420       
1421    def Filter(self,event):
1422        '''Read text from filter control and select entries that match. Called by
1423        Timer after a delay with no input or if Enter is pressed.
1424        '''
1425        if self.timer.IsRunning():
1426            self.timer.Stop()
1427        self.GetSelections() # record current selections
1428        txt = self.filterBox.GetValue()
1429        self.clb.Clear()
1430       
1431        self.Update()
1432        self.filterlist = []
1433        if txt:
1434            txt = txt.lower()
1435            ChoiceList = []
1436            for i,item in enumerate(self.ChoiceList):
1437                if item.lower().find(txt) != -1:
1438                    ChoiceList.append(item)
1439                    self.filterlist.append(i)
1440        else:
1441            self.filterlist = range(len(self.ChoiceList))
1442            ChoiceList = self.ChoiceList
1443        self.clb.AppendItems(ChoiceList)
1444        self._ShowSelections()
1445        self.OKbtn.Enable(True)
1446
1447def SelectEdit1Var(G2frame,array,labelLst,elemKeysLst,dspLst,refFlgElem):
1448    '''Select a variable from a list, then edit it and select histograms
1449    to copy it to.
1450
1451    :param wx.Frame G2frame: main GSAS-II frame
1452    :param dict array: the array (dict or list) where values to be edited are kept
1453    :param list labelLst: labels for each data item
1454    :param list elemKeysLst: a list of lists of keys needed to be applied (see below)
1455      to obtain the value of each parameter
1456    :param list dspLst: list list of digits to be displayed (10,4) is 10 digits
1457      with 4 decimal places. Can be None.
1458    :param list refFlgElem: a list of lists of keys needed to be applied (see below)
1459      to obtain the refine flag for each parameter or None if the parameter
1460      does not have refine flag.
1461
1462    Example::
1463      array = data
1464      labelLst = ['v1','v2']
1465      elemKeysLst = [['v1'], ['v2',0]]
1466      refFlgElem = [None, ['v2',1]]
1467
1468     * The value for v1 will be in data['v1'] and this cannot be refined while,
1469     * The value for v2 will be in data['v2'][0] and its refinement flag is data['v2'][1]
1470    '''
1471    def unkey(dct,keylist):
1472        '''dive into a nested set of dicts/lists applying keys in keylist
1473        consecutively
1474        '''
1475        d = dct
1476        for k in keylist:
1477            d = d[k]
1478        return d
1479
1480    def OnChoice(event):
1481        'Respond when a parameter is selected in the Choice box'
1482        valSizer.DeleteWindows()
1483        lbl = event.GetString()
1484        copyopts['currentsel'] = lbl
1485        i = labelLst.index(lbl)
1486        OKbtn.Enable(True)
1487        ch.SetLabel(lbl)
1488        args = {}
1489        if dspLst[i]:
1490            args = {'nDig':dspLst[i]}
1491        Val = ValidatedTxtCtrl(
1492            dlg,
1493            unkey(array,elemKeysLst[i][:-1]),
1494            elemKeysLst[i][-1],
1495            **args)
1496        copyopts['startvalue'] = unkey(array,elemKeysLst[i])
1497        #unkey(array,elemKeysLst[i][:-1])[elemKeysLst[i][-1]] =
1498        valSizer.Add(Val,0,wx.LEFT,5)
1499        dlg.SendSizeEvent()
1500       
1501    # SelectEdit1Var execution begins here
1502    saveArray = copy.deepcopy(array) # keep original values
1503    TreeItemType = G2frame.PatternTree.GetItemText(G2frame.PickId)
1504    copyopts = {'InTable':False,"startvalue":None,'currentsel':None}       
1505    hst = G2frame.PatternTree.GetItemText(G2frame.PatternId)
1506    histList = G2pdG.GetHistsLikeSelected(G2frame)
1507    if not histList:
1508        G2frame.ErrorDialog('No match','No histograms match '+hst,G2frame.dataFrame)
1509        return
1510    dlg = wx.Dialog(G2frame.dataDisplay,wx.ID_ANY,'Set a parameter value',
1511        style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
1512    mainSizer = wx.BoxSizer(wx.VERTICAL)
1513    mainSizer.Add((5,5))
1514    subSizer = wx.BoxSizer(wx.HORIZONTAL)
1515    subSizer.Add((-1,-1),1,wx.EXPAND)
1516    subSizer.Add(wx.StaticText(dlg,wx.ID_ANY,'Select a parameter and set a new value'))
1517    subSizer.Add((-1,-1),1,wx.EXPAND)
1518    mainSizer.Add(subSizer,0,wx.EXPAND,0)
1519    mainSizer.Add((0,10))
1520
1521    subSizer = wx.FlexGridSizer(0,2,5,0)
1522    subSizer.Add(wx.StaticText(dlg,wx.ID_ANY,'Parameter: '))
1523    ch = wx.Choice(dlg, wx.ID_ANY, choices = sorted(labelLst))
1524    ch.SetSelection(-1)
1525    ch.Bind(wx.EVT_CHOICE, OnChoice)
1526    subSizer.Add(ch)
1527    subSizer.Add(wx.StaticText(dlg,wx.ID_ANY,'Value: '))
1528    valSizer = wx.BoxSizer(wx.HORIZONTAL)
1529    subSizer.Add(valSizer)
1530    mainSizer.Add(subSizer)
1531
1532    mainSizer.Add((-1,20))
1533    subSizer = wx.BoxSizer(wx.HORIZONTAL)
1534    subSizer.Add(G2CheckBox(dlg, 'Edit in table ', copyopts, 'InTable'))
1535    mainSizer.Add(subSizer)
1536
1537    btnsizer = wx.StdDialogButtonSizer()
1538    OKbtn = wx.Button(dlg, wx.ID_OK,'Continue')
1539    OKbtn.Enable(False)
1540    OKbtn.SetDefault()
1541    OKbtn.Bind(wx.EVT_BUTTON,lambda event: dlg.EndModal(wx.ID_OK))
1542    btnsizer.AddButton(OKbtn)
1543    btn = wx.Button(dlg, wx.ID_CANCEL)
1544    btnsizer.AddButton(btn)
1545    btnsizer.Realize()
1546    mainSizer.Add((-1,5),1,wx.EXPAND,1)
1547    mainSizer.Add(btnsizer,0,wx.ALIGN_CENTER,0)
1548    mainSizer.Add((-1,10))
1549
1550    dlg.SetSizer(mainSizer)
1551    dlg.CenterOnParent()
1552    if dlg.ShowModal() != wx.ID_OK:
1553        array.update(saveArray)
1554        dlg.Destroy()
1555        return
1556    dlg.Destroy()
1557
1558    copyList = []
1559    lbl = copyopts['currentsel']
1560    dlg = G2MultiChoiceDialog(G2frame.dataFrame,'Copy parameter '+lbl+' from\n'+hst,
1561        'Copy parameters', histList)
1562    dlg.CenterOnParent()
1563    try:
1564        if dlg.ShowModal() == wx.ID_OK:
1565            for i in dlg.GetSelections(): 
1566                copyList.append(histList[i])
1567        else:
1568            # reset the parameter since cancel was pressed
1569            array.update(saveArray)
1570            return
1571    finally:
1572        dlg.Destroy()
1573
1574    prelbl = [hst]
1575    i = labelLst.index(lbl)
1576    keyLst = elemKeysLst[i]
1577    refkeys = refFlgElem[i]
1578    dictlst = [unkey(array,keyLst[:-1])]
1579    if refkeys is not None:
1580        refdictlst = [unkey(array,refkeys[:-1])]
1581    else:
1582        refdictlst = None
1583    Id = GetPatternTreeItemId(G2frame,G2frame.root,hst)
1584    hstData = G2frame.PatternTree.GetItemPyData(GetPatternTreeItemId(G2frame,Id,'Instrument Parameters'))[0]
1585    for h in copyList:
1586        Id = GetPatternTreeItemId(G2frame,G2frame.root,h)
1587        instData = G2frame.PatternTree.GetItemPyData(GetPatternTreeItemId(G2frame,Id,'Instrument Parameters'))[0]
1588        if len(hstData) != len(instData) or hstData['Type'][0] != instData['Type'][0]:  #don't mix data types or lam & lam1/lam2 parms!
1589            print h+' not copied - instrument parameters not commensurate'
1590            continue
1591        hData = G2frame.PatternTree.GetItemPyData(GetPatternTreeItemId(G2frame,Id,TreeItemType))
1592        if TreeItemType == 'Instrument Parameters':
1593            hData = hData[0]
1594        #copy the value if it is changed or we will not edit in a table
1595        valNow = unkey(array,keyLst)
1596        if copyopts['startvalue'] != valNow or not copyopts['InTable']:
1597            unkey(hData,keyLst[:-1])[keyLst[-1]] = valNow
1598        prelbl += [h]
1599        dictlst += [unkey(hData,keyLst[:-1])]
1600        if refdictlst is not None:
1601            refdictlst += [unkey(hData,refkeys[:-1])]
1602    if refdictlst is None:
1603        args = {}
1604    else:
1605        args = {'checkdictlst':refdictlst,
1606                'checkelemlst':len(dictlst)*[refkeys[-1]],
1607                'checklabel':'Refine?'}
1608    if copyopts['InTable']:
1609        dlg = ScrolledMultiEditor(
1610            G2frame.dataDisplay,dictlst,
1611            len(dictlst)*[keyLst[-1]],prelbl,
1612            header='Editing parameter '+lbl,
1613            CopyButton=True,**args)
1614        dlg.CenterOnParent()
1615        if dlg.ShowModal() != wx.ID_OK:
1616            array.update(saveArray)
1617        dlg.Destroy()
1618
1619################################################################        Single choice Dialog with filter options
1620class G2SingleChoiceDialog(wx.Dialog):
1621    '''A dialog similar to wx.SingleChoiceDialog except that a filter can be
1622    added.
1623
1624    :param wx.Frame ParentFrame: reference to parent frame
1625    :param str title: heading above list of choices
1626    :param str header: Title to place on window frame
1627    :param list ChoiceList: a list of choices where one will be selected
1628    :param bool monoFont: If False (default), use a variable-spaced font;
1629      if True use a equally-spaced font.
1630    :param bool filterBox: If True (default) an input widget is placed on
1631      the window and only entries matching the entered text are shown.
1632    :param kw: optional keyword parameters for the wx.Dialog may
1633      be included such as size [which defaults to `(320,310)`] and
1634      style (which defaults to ``wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.CENTRE | wx.OK | wx.CANCEL``);
1635      note that ``wx.OK`` and ``wx.CANCEL`` controls
1636      the presence of the eponymous buttons in the dialog.
1637    :returns: the name of the created dialog
1638    '''
1639    def __init__(self,parent, title, header, ChoiceList, 
1640                 monoFont=False, filterBox=True, **kw):
1641        # process keyword parameters, notably style
1642        options = {'size':(320,310), # default Frame keywords
1643                   'style':wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.CENTRE| wx.OK | wx.CANCEL,
1644                   }
1645        options.update(kw)
1646        self.ChoiceList = ChoiceList
1647        self.filterlist = range(len(self.ChoiceList))
1648        if options['style'] & wx.OK:
1649            useOK = True
1650            options['style'] ^= wx.OK
1651        else:
1652            useOK = False
1653        if options['style'] & wx.CANCEL:
1654            useCANCEL = True
1655            options['style'] ^= wx.CANCEL
1656        else:
1657            useCANCEL = False       
1658        # create the dialog frame
1659        wx.Dialog.__init__(self,parent,wx.ID_ANY,header,**options)
1660        # fill the dialog
1661        Sizer = wx.BoxSizer(wx.VERTICAL)
1662        topSizer = wx.BoxSizer(wx.HORIZONTAL)
1663        topSizer.Add(
1664            wx.StaticText(self,wx.ID_ANY,title,size=(-1,35)),
1665            1,wx.ALL|wx.EXPAND|WACV,1)
1666        if filterBox:
1667            self.timer = wx.Timer()
1668            self.timer.Bind(wx.EVT_TIMER,self.Filter)
1669            topSizer.Add(wx.StaticText(self,wx.ID_ANY,'Filter: '),0,wx.ALL,1)
1670            self.filterBox = wx.TextCtrl(self, wx.ID_ANY, size=(80,-1),
1671                                         style=wx.TE_PROCESS_ENTER)
1672            self.filterBox.Bind(wx.EVT_CHAR,self.onChar)
1673            self.filterBox.Bind(wx.EVT_TEXT_ENTER,self.Filter)
1674        topSizer.Add(self.filterBox,0,wx.ALL,0)
1675        Sizer.Add(topSizer,0,wx.ALL|wx.EXPAND,8)
1676        self.clb = wx.ListBox(self, wx.ID_ANY, (30,30), wx.DefaultSize, ChoiceList)
1677        self.clb.Bind(wx.EVT_LEFT_DCLICK,self.onDoubleClick)
1678        if monoFont:
1679            font1 = wx.Font(self.clb.GetFont().GetPointSize(),
1680                            wx.MODERN, wx.NORMAL, wx.NORMAL, False)
1681            self.clb.SetFont(font1)
1682        Sizer.Add(self.clb,1,wx.LEFT|wx.RIGHT|wx.EXPAND,10)
1683        Sizer.Add((-1,10))
1684        # OK/Cancel buttons
1685        btnsizer = wx.StdDialogButtonSizer()
1686        if useOK:
1687            self.OKbtn = wx.Button(self, wx.ID_OK)
1688            self.OKbtn.SetDefault()
1689            btnsizer.AddButton(self.OKbtn)
1690        if useCANCEL:
1691            btn = wx.Button(self, wx.ID_CANCEL)
1692            btnsizer.AddButton(btn)
1693        btnsizer.Realize()
1694        Sizer.Add((-1,5))
1695        Sizer.Add(btnsizer,0,wx.ALIGN_RIGHT,50)
1696        Sizer.Add((-1,20))
1697        # OK done, let's get outa here
1698        self.SetSizer(Sizer)
1699    def GetSelection(self):
1700        'Returns the index of the selected choice'
1701        i = self.clb.GetSelection()
1702        if i < 0 or i >= len(self.filterlist):
1703            return wx.NOT_FOUND
1704        return self.filterlist[i]
1705    def onChar(self,event):
1706        self.OKbtn.Enable(False)
1707        if self.timer.IsRunning():
1708            self.timer.Stop()
1709        self.timer.Start(1000,oneShot=True)
1710        event.Skip()
1711    def Filter(self,event):
1712        if self.timer.IsRunning():
1713            self.timer.Stop()
1714        txt = self.filterBox.GetValue()
1715        self.clb.Clear()
1716        self.Update()
1717        self.filterlist = []
1718        if txt:
1719            txt = txt.lower()
1720            ChoiceList = []
1721            for i,item in enumerate(self.ChoiceList):
1722                if item.lower().find(txt) != -1:
1723                    ChoiceList.append(item)
1724                    self.filterlist.append(i)
1725        else:
1726            self.filterlist = range(len(self.ChoiceList))
1727            ChoiceList = self.ChoiceList
1728        self.clb.AppendItems(ChoiceList)
1729        self.OKbtn.Enable(True)
1730    def onDoubleClick(self,event):
1731        self.EndModal(wx.ID_OK)
1732
1733################################################################################
1734def G2MessageBox(parent,msg,title='Error'):
1735    '''Simple code to display a error or warning message
1736    '''
1737    dlg = wx.MessageDialog(parent,StripIndents(msg), title, wx.OK)
1738    dlg.ShowModal()
1739    dlg.Destroy()
1740   
1741################################################################################
1742class PickTwoDialog(wx.Dialog):
1743    '''This does not seem to be in use
1744    '''
1745    def __init__(self,parent,title,prompt,names,choices):
1746        wx.Dialog.__init__(self,parent,-1,title, 
1747            pos=wx.DefaultPosition,style=wx.DEFAULT_DIALOG_STYLE)
1748        self.panel = wx.Panel(self)         #just a dummy - gets destroyed in Draw!
1749        self.prompt = prompt
1750        self.choices = choices
1751        self.names = names
1752        self.Draw()
1753
1754    def Draw(self):
1755        Indx = {}
1756       
1757        def OnSelection(event):
1758            Obj = event.GetEventObject()
1759            id = Indx[Obj.GetId()]
1760            self.choices[id] = Obj.GetValue().encode()  #to avoid Unicode versions
1761            self.Draw()
1762           
1763        self.panel.DestroyChildren()
1764        self.panel.Destroy()
1765        self.panel = wx.Panel(self)
1766        mainSizer = wx.BoxSizer(wx.VERTICAL)
1767        mainSizer.Add(wx.StaticText(self.panel,-1,self.prompt),0,wx.ALIGN_CENTER)
1768        for isel,name in enumerate(self.choices):
1769            lineSizer = wx.BoxSizer(wx.HORIZONTAL)
1770            lineSizer.Add(wx.StaticText(self.panel,-1,'Reference atom '+str(isel+1)),0,wx.ALIGN_CENTER)
1771            nameList = self.names[:]
1772            if isel:
1773                if self.choices[0] in nameList:
1774                    nameList.remove(self.choices[0])
1775            choice = wx.ComboBox(self.panel,-1,value=name,choices=nameList,
1776                style=wx.CB_READONLY|wx.CB_DROPDOWN)
1777            Indx[choice.GetId()] = isel
1778            choice.Bind(wx.EVT_COMBOBOX, OnSelection)
1779            lineSizer.Add(choice,0,WACV)
1780            mainSizer.Add(lineSizer)
1781        OkBtn = wx.Button(self.panel,-1,"Ok")
1782        OkBtn.Bind(wx.EVT_BUTTON, self.OnOk)
1783        CancelBtn = wx.Button(self.panel,-1,'Cancel')
1784        CancelBtn.Bind(wx.EVT_BUTTON, self.OnCancel)
1785        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
1786        btnSizer.Add((20,20),1)
1787        btnSizer.Add(OkBtn)
1788        btnSizer.Add(CancelBtn)
1789        btnSizer.Add((20,20),1)
1790        mainSizer.Add(btnSizer,0,wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
1791        self.panel.SetSizer(mainSizer)
1792        self.panel.Fit()
1793        self.Fit()
1794       
1795    def GetSelection(self):
1796        return self.choices
1797
1798    def OnOk(self,event):
1799        parent = self.GetParent()
1800        parent.Raise()
1801        self.EndModal(wx.ID_OK)             
1802       
1803    def OnCancel(self,event):
1804        parent = self.GetParent()
1805        parent.Raise()
1806        self.EndModal(wx.ID_CANCEL)
1807
1808################################################################################
1809class SingleFloatDialog(wx.Dialog):
1810    'Dialog to obtain a single float value from user'
1811    def __init__(self,parent,title,prompt,value,limits=[0.,1.],format='%.5g'):
1812        wx.Dialog.__init__(self,parent,-1,title, 
1813            pos=wx.DefaultPosition,style=wx.DEFAULT_DIALOG_STYLE)
1814        self.panel = wx.Panel(self)         #just a dummy - gets destroyed in Draw!
1815        self.limits = limits
1816        self.value = value
1817        self.prompt = prompt
1818        self.format = format
1819        self.Draw()
1820       
1821    def Draw(self):
1822       
1823        def OnValItem(event):
1824            try:
1825                val = float(valItem.GetValue())
1826                if val < self.limits[0] or val > self.limits[1]:
1827                    raise ValueError
1828            except ValueError:
1829                val = self.value
1830            self.value = val
1831            valItem.SetValue(self.format%(self.value))
1832           
1833        self.panel.Destroy()
1834        self.panel = wx.Panel(self)
1835        mainSizer = wx.BoxSizer(wx.VERTICAL)
1836        mainSizer.Add(wx.StaticText(self.panel,-1,self.prompt),0,wx.ALIGN_CENTER)
1837        valItem = wx.TextCtrl(self.panel,-1,value=self.format%(self.value),style=wx.TE_PROCESS_ENTER)
1838        mainSizer.Add(valItem,0,wx.ALIGN_CENTER)
1839        valItem.Bind(wx.EVT_TEXT_ENTER,OnValItem)
1840        valItem.Bind(wx.EVT_KILL_FOCUS,OnValItem)
1841        OkBtn = wx.Button(self.panel,-1,"Ok")
1842        OkBtn.Bind(wx.EVT_BUTTON, self.OnOk)
1843        CancelBtn = wx.Button(self.panel,-1,'Cancel')
1844        CancelBtn.Bind(wx.EVT_BUTTON, self.OnCancel)
1845        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
1846        btnSizer.Add((20,20),1)
1847        btnSizer.Add(OkBtn)
1848        btnSizer.Add(CancelBtn)
1849        btnSizer.Add((20,20),1)
1850        mainSizer.Add(btnSizer,0,wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
1851        self.panel.SetSizer(mainSizer)
1852        self.panel.Fit()
1853        self.Fit()
1854
1855    def GetValue(self):
1856        return self.value
1857       
1858    def OnOk(self,event):
1859        parent = self.GetParent()
1860        parent.Raise()
1861        self.EndModal(wx.ID_OK)             
1862       
1863    def OnCancel(self,event):
1864        parent = self.GetParent()
1865        parent.Raise()
1866        self.EndModal(wx.ID_CANCEL)
1867
1868################################################################################
1869class SingleStringDialog(wx.Dialog):
1870    '''Dialog to obtain a single string value from user
1871   
1872    :param wx.Frame parent: name of parent frame
1873    :param str title: title string for dialog
1874    :param str prompt: string to tell use what they are inputting
1875    :param str value: default input value, if any
1876    '''
1877    def __init__(self,parent,title,prompt,value='',size=(200,-1)):
1878        wx.Dialog.__init__(self,parent,wx.ID_ANY,title, 
1879                           pos=wx.DefaultPosition,
1880                           style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
1881        self.value = value
1882        self.prompt = prompt
1883        self.CenterOnParent()
1884        self.panel = wx.Panel(self)
1885        mainSizer = wx.BoxSizer(wx.VERTICAL)
1886        mainSizer.Add(wx.StaticText(self.panel,-1,self.prompt),0,wx.ALIGN_CENTER)
1887        self.valItem = wx.TextCtrl(self.panel,-1,value=self.value,size=size)
1888        mainSizer.Add(self.valItem,0,wx.ALIGN_CENTER)
1889        btnsizer = wx.StdDialogButtonSizer()
1890        OKbtn = wx.Button(self.panel, wx.ID_OK)
1891        OKbtn.SetDefault()
1892        btnsizer.AddButton(OKbtn)
1893        btn = wx.Button(self.panel, wx.ID_CANCEL)
1894        btnsizer.AddButton(btn)
1895        btnsizer.Realize()
1896        mainSizer.Add(btnsizer,0,wx.ALIGN_CENTER)
1897        self.panel.SetSizer(mainSizer)
1898        self.panel.Fit()
1899        self.Fit()
1900
1901    def Show(self):
1902        '''Use this method after creating the dialog to post it
1903        :returns: True if the user pressed OK; False if the User pressed Cancel
1904        '''
1905        if self.ShowModal() == wx.ID_OK:
1906            self.value = self.valItem.GetValue()
1907            return True
1908        else:
1909            return False
1910
1911    def GetValue(self):
1912        '''Use this method to get the value entered by the user
1913        :returns: string entered by user
1914        '''
1915        return self.value
1916
1917################################################################################
1918class MultiStringDialog(wx.Dialog):
1919    '''Dialog to obtain a multi string values from user
1920   
1921    :param wx.Frame parent: name of parent frame
1922    :param str title: title string for dialog
1923    :param str prompts: strings to tell use what they are inputting
1924    :param str values: default input values, if any
1925    '''
1926    def __init__(self,parent,title,prompts,values=[]):      #,size=(200,-1)?
1927       
1928        wx.Dialog.__init__(self,parent,wx.ID_ANY,title, 
1929                           pos=wx.DefaultPosition,
1930                           style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
1931        self.values = values
1932        self.prompts = prompts
1933        self.CenterOnParent()
1934        self.panel = wx.Panel(self)
1935        mainSizer = wx.BoxSizer(wx.VERTICAL)
1936        promptSizer = wx.FlexGridSizer(0,2,5,5)
1937        self.Indx = {}
1938        for prompt,value in zip(prompts,values):
1939            promptSizer.Add(wx.StaticText(self.panel,-1,prompt),0,WACV)
1940            valItem = wx.TextCtrl(self.panel,-1,value=value,style=wx.TE_PROCESS_ENTER)
1941            self.Indx[valItem.GetId()] = prompt
1942            valItem.Bind(wx.EVT_TEXT,self.newValue)
1943            promptSizer.Add(valItem,0,WACV)
1944        mainSizer.Add(promptSizer,0)
1945        btnsizer = wx.StdDialogButtonSizer()
1946        OKbtn = wx.Button(self.panel, wx.ID_OK)
1947        OKbtn.SetDefault()
1948        btnsizer.AddButton(OKbtn)
1949        btn = wx.Button(self.panel, wx.ID_CANCEL)
1950        btnsizer.AddButton(btn)
1951        btnsizer.Realize()
1952        mainSizer.Add(btnsizer,0,wx.ALIGN_CENTER)
1953        self.panel.SetSizer(mainSizer)
1954        self.panel.Fit()
1955        self.Fit()
1956       
1957    def newValue(self,event):
1958        Obj = event.GetEventObject()
1959        item = self.Indx[Obj.GetId()]
1960        id = self.prompts.index(item)
1961        self.values[id] = Obj.GetValue()
1962
1963    def Show(self):
1964        '''Use this method after creating the dialog to post it
1965        :returns: True if the user pressed OK; False if the User pressed Cancel
1966        '''
1967        if self.ShowModal() == wx.ID_OK:
1968            return True
1969        else:
1970            return False
1971
1972    def GetValues(self):
1973        '''Use this method to get the value entered by the user
1974        :returns: string entered by user
1975        '''
1976        return self.values
1977
1978################################################################################
1979class G2ColumnIDDialog(wx.Dialog):
1980    '''A dialog for matching column data to desired items; some columns may be ignored.
1981   
1982    :param wx.Frame ParentFrame: reference to parent frame
1983    :param str title: heading above list of choices
1984    :param str header: Title to place on window frame
1985    :param list ChoiceList: a list of possible choices for the columns
1986    :param list ColumnData: lists of column data to be matched with ChoiceList
1987    :param bool monoFont: If False (default), use a variable-spaced font;
1988      if True use a equally-spaced font.
1989    :param kw: optional keyword parameters for the wx.Dialog may
1990      be included such as size [which defaults to `(320,310)`] and
1991      style (which defaults to ``wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.CENTRE | wx.OK | wx.CANCEL``);
1992      note that ``wx.OK`` and ``wx.CANCEL`` controls
1993      the presence of the eponymous buttons in the dialog.
1994    :returns: the name of the created dialog
1995   
1996    '''
1997
1998    def __init__(self,parent, title, header,Comments,ChoiceList, ColumnData,
1999                 monoFont=False, **kw):
2000
2001        def OnOk(sevent):
2002            OK = True
2003            selCols = []
2004            for col in self.sel:
2005                item = col.GetValue()
2006                if item != ' ' and item in selCols:
2007                    OK = False
2008                    break
2009                else:
2010                    selCols.append(item)
2011            parent = self.GetParent()
2012            if not OK:
2013                parent.ErrorDialog('Duplicate',item+' selected more than once')
2014                return
2015            parent.Raise()
2016            self.EndModal(wx.ID_OK)
2017           
2018        def OnModify(event):
2019            Obj = event.GetEventObject()
2020            icol,colData = Indx[Obj.GetId()]
2021            modify = Obj.GetValue()
2022            if not modify:
2023                return
2024            print 'Modify column',icol,' by', modify
2025            for i,item in enumerate(self.ColumnData[icol]):
2026                self.ColumnData[icol][i] = str(eval(item+modify))
2027            colData.SetValue('\n'.join(self.ColumnData[icol]))
2028            Obj.SetValue('')
2029           
2030        # process keyword parameters, notably style
2031        options = {'size':(600,310), # default Frame keywords
2032                   'style':wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.CENTRE| wx.OK | wx.CANCEL,
2033                   }
2034        options.update(kw)
2035        self.Comments = ''.join(Comments)
2036        self.ChoiceList = ChoiceList
2037        self.ColumnData = ColumnData
2038        nCol = len(ColumnData)
2039        if options['style'] & wx.OK:
2040            useOK = True
2041            options['style'] ^= wx.OK
2042        else:
2043            useOK = False
2044        if options['style'] & wx.CANCEL:
2045            useCANCEL = True
2046            options['style'] ^= wx.CANCEL
2047        else:
2048            useCANCEL = False       
2049        # create the dialog frame
2050        wx.Dialog.__init__(self,parent,wx.ID_ANY,header,**options)
2051        panel = wxscroll.ScrolledPanel(self)
2052        # fill the dialog
2053        Sizer = wx.BoxSizer(wx.VERTICAL)
2054        Sizer.Add((-1,5))
2055        Sizer.Add(wx.StaticText(panel,label=title),0,WACV)
2056        if self.Comments:
2057            Sizer.Add(wx.StaticText(panel,label=' Header lines:'),0,WACV)
2058            Sizer.Add(wx.TextCtrl(panel,value=self.Comments,size=(200,-1),
2059                style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP),0,wx.ALL|wx.EXPAND|WACV,8)
2060        columnsSizer = wx.FlexGridSizer(0,nCol,5,10)
2061        self.sel = []
2062        self.mod = []
2063        Indx = {}
2064        for icol,col in enumerate(self.ColumnData):
2065            colSizer = wx.BoxSizer(wx.VERTICAL)
2066            colSizer.Add(wx.StaticText(panel,label=' Column #%d Select:'%(icol)),0,WACV)
2067            self.sel.append(wx.ComboBox(panel,value=' ',choices=self.ChoiceList,style=wx.CB_READONLY|wx.CB_DROPDOWN))
2068            colSizer.Add(self.sel[-1])
2069            colData = wx.TextCtrl(panel,value='\n'.join(self.ColumnData[icol]),size=(120,-1),
2070                style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP)
2071            colSizer.Add(colData,0,WACV)
2072            colSizer.Add(wx.StaticText(panel,label=' Modify by:'),0,WACV)
2073            mod = wx.TextCtrl(panel,size=(120,-1),value='',style=wx.TE_PROCESS_ENTER)
2074            mod.Bind(wx.EVT_TEXT_ENTER,OnModify)
2075            mod.Bind(wx.EVT_KILL_FOCUS,OnModify)
2076            Indx[mod.GetId()] = [icol,colData]
2077            colSizer.Add(mod,0,WACV)
2078            columnsSizer.Add(colSizer)
2079        Sizer.Add(columnsSizer)
2080        Sizer.Add(wx.StaticText(panel,label=' For modify by, enter arithmetic string eg. "-12345.67". "+","-","*","/","**" all allowed'),0,WACV) 
2081        Sizer.Add((-1,10))
2082        # OK/Cancel buttons
2083        btnsizer = wx.StdDialogButtonSizer()
2084        if useOK:
2085            self.OKbtn = wx.Button(panel, wx.ID_OK)
2086            self.OKbtn.SetDefault()
2087            btnsizer.AddButton(self.OKbtn)
2088            self.OKbtn.Bind(wx.EVT_BUTTON, OnOk)
2089        if useCANCEL:
2090            btn = wx.Button(panel, wx.ID_CANCEL)
2091            btnsizer.AddButton(btn)
2092        btnsizer.Realize()
2093        Sizer.Add((-1,5))
2094        Sizer.Add(btnsizer,0,wx.ALIGN_LEFT,20)
2095        Sizer.Add((-1,5))
2096        # OK done, let's get outa here
2097        panel.SetSizer(Sizer)
2098        panel.SetAutoLayout(1)
2099        panel.SetupScrolling()
2100        Size = [450,375]
2101        panel.SetSize(Size)
2102        Size[0] += 25; Size[1]+= 25
2103        self.SetSize(Size)
2104       
2105    def GetSelection(self):
2106        'Returns the selected sample parm for each column'
2107        selCols = []
2108        for item in self.sel:
2109            selCols.append(item.GetValue())
2110        return selCols,self.ColumnData
2111   
2112################################################################################
2113class G2HistoDataDialog(wx.Dialog):
2114    '''A dialog for editing histogram data globally.
2115   
2116    :param wx.Frame ParentFrame: reference to parent frame
2117    :param str title: heading above list of choices
2118    :param str header: Title to place on window frame
2119    :param list ParmList: a list of names for the columns
2120    :param list ParmFmt: a list of formatting strings for the columns
2121    :param list: HistoList: a list of histogram names
2122    :param list ParmData: a list of lists of data matched to ParmList; one for each item in HistoList
2123    :param bool monoFont: If False (default), use a variable-spaced font;
2124      if True use a equally-spaced font.
2125    :param kw: optional keyword parameters for the wx.Dialog may
2126      be included such as size [which defaults to `(320,310)`] and
2127      style (which defaults to
2128      ``wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.CENTRE | wx.OK | wx.CANCEL``);
2129      note that ``wx.OK`` and ``wx.CANCEL`` controls the presence of the eponymous buttons in the dialog.
2130    :returns: the modified ParmData
2131   
2132    '''
2133
2134    def __init__(self,parent, title, header,ParmList,ParmFmt,HistoList,ParmData,
2135                 monoFont=False, **kw):
2136
2137        def OnOk(sevent):
2138            parent.Raise()
2139            self.EndModal(wx.ID_OK)
2140           
2141        def OnModify(event):
2142            Obj = event.GetEventObject()
2143            irow,it = Indx[Obj.GetId()]
2144            try:
2145                val = float(Obj.GetValue())
2146            except ValueError:
2147                val = self.ParmData[irow][it]
2148            self.ParmData[irow][it] = val
2149            Obj.SetValue(self.ParmFmt[it]%val)
2150                       
2151        # process keyword parameters, notably style
2152        options = {'size':(600,310), # default Frame keywords
2153                   'style':wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.CENTRE| wx.OK | wx.CANCEL,
2154                   }
2155        options.update(kw)
2156        self.ParmList = ParmList
2157        self.ParmFmt = ParmFmt
2158        self.HistoList = HistoList
2159        self.ParmData = ParmData
2160        nCol = len(ParmList)
2161        if options['style'] & wx.OK:
2162            useOK = True
2163            options['style'] ^= wx.OK
2164        else:
2165            useOK = False
2166        if options['style'] & wx.CANCEL:
2167            useCANCEL = True
2168            options['style'] ^= wx.CANCEL
2169        else:
2170            useCANCEL = False       
2171        # create the dialog frame
2172        wx.Dialog.__init__(self,parent,wx.ID_ANY,header,**options)
2173        panel = wxscroll.ScrolledPanel(self)
2174        # fill the dialog
2175        Sizer = wx.BoxSizer(wx.VERTICAL)
2176        Sizer.Add((-1,5))
2177        Sizer.Add(wx.StaticText(panel,label=title),0,WACV)
2178        dataSizer = wx.FlexGridSizer(0,nCol+1,0,0)
2179        self.sel = []
2180        self.mod = []
2181        Indx = {}
2182        for item in ['Histogram',]+self.ParmList:
2183            dataSizer.Add(wx.StaticText(panel,-1,label=' %10s '%(item)),0,WACV)
2184        for irow,name in enumerate(self.HistoList):
2185            dataSizer.Add(wx.StaticText(panel,label=name),0,WACV|wx.LEFT|wx.RIGHT,10)
2186            for it,item in enumerate(self.ParmData[irow]):
2187                dat = wx.TextCtrl(panel,-1,value=self.ParmFmt[it]%(item),style=wx.TE_PROCESS_ENTER)
2188                dataSizer.Add(dat,0,WACV)
2189                dat.Bind(wx.EVT_TEXT_ENTER,OnModify)
2190                dat.Bind(wx.EVT_KILL_FOCUS,OnModify)
2191                Indx[dat.GetId()] = [irow,it]
2192        Sizer.Add(dataSizer)
2193        Sizer.Add((-1,10))
2194        # OK/Cancel buttons
2195        btnsizer = wx.StdDialogButtonSizer()
2196        if useOK:
2197            self.OKbtn = wx.Button(panel, wx.ID_OK)
2198            self.OKbtn.SetDefault()
2199            btnsizer.AddButton(self.OKbtn)
2200            self.OKbtn.Bind(wx.EVT_BUTTON, OnOk)
2201        if useCANCEL:
2202            btn = wx.Button(panel, wx.ID_CANCEL)
2203            btnsizer.AddButton(btn)
2204        btnsizer.Realize()
2205        Sizer.Add((-1,5))
2206        Sizer.Add(btnsizer,0,wx.ALIGN_LEFT,20)
2207        Sizer.Add((-1,5))
2208        # OK done, let's get outa here
2209        panel.SetSizer(Sizer)
2210        panel.SetAutoLayout(1)
2211        panel.SetupScrolling()
2212        Size = [450,375]
2213        panel.SetSize(Size)
2214        Size[0] += 25; Size[1]+= 25
2215        self.SetSize(Size)
2216       
2217    def GetData(self):
2218        'Returns the modified ParmData'
2219        return self.ParmData
2220   
2221################################################################################
2222def ItemSelector(ChoiceList, ParentFrame=None,
2223                 title='Select an item',
2224                 size=None, header='Item Selector',
2225                 useCancel=True,multiple=False):
2226    ''' Provide a wx dialog to select a single item or multiple items from list of choices
2227
2228    :param list ChoiceList: a list of choices where one will be selected
2229    :param wx.Frame ParentFrame: Name of parent frame (default None)
2230    :param str title: heading above list of choices (default 'Select an item')
2231    :param wx.Size size: Size for dialog to be created (default None -- size as needed)
2232    :param str header: Title to place on window frame (default 'Item Selector')
2233    :param bool useCancel: If True (default) both the OK and Cancel buttons are offered
2234    :param bool multiple: If True then multiple items can be selected (default False)
2235   
2236    :returns: the selection index or None or a selection list if multiple is true
2237    '''
2238    if multiple:
2239        if useCancel:
2240            dlg = G2MultiChoiceDialog(
2241                ParentFrame,title, header, ChoiceList)
2242        else:
2243            dlg = G2MultiChoiceDialog(
2244                ParentFrame,title, header, ChoiceList,
2245                style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.OK|wx.CENTRE)
2246    else:
2247        if useCancel:
2248            dlg = wx.SingleChoiceDialog(
2249                ParentFrame,title, header, ChoiceList)
2250        else:
2251            dlg = wx.SingleChoiceDialog(
2252                ParentFrame,title, header,ChoiceList,
2253                style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.OK|wx.CENTRE)
2254    if size: dlg.SetSize(size)
2255    if dlg.ShowModal() == wx.ID_OK:
2256        if multiple:
2257            dlg.Destroy()
2258            return dlg.GetSelections()
2259        else:
2260            dlg.Destroy()
2261            return dlg.GetSelection()
2262    else:
2263        dlg.Destroy()
2264        return None
2265    dlg.Destroy()
2266
2267######################################################### Column-order selection dialog
2268def GetItemOrder(parent,keylist,vallookup,posdict):
2269    '''Creates a panel where items can be ordered into columns
2270   
2271    :param list keylist: is a list of keys for column assignments
2272    :param dict vallookup: is a dict keyed by names in keylist where each item is a dict.
2273       Each inner dict contains variable names as keys and their associated values
2274    :param dict posdict: is a dict keyed by names in keylist where each item is a dict.
2275       Each inner dict contains column numbers as keys and their associated
2276       variable name as a value. This is used for both input and output.
2277       
2278    '''
2279    dlg = wx.Dialog(parent,style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
2280    sizer = wx.BoxSizer(wx.VERTICAL)
2281    spanel = OrderBox(dlg,keylist,vallookup,posdict)
2282    spanel.Fit()
2283    sizer.Add(spanel,1,wx.EXPAND)
2284    btnsizer = wx.StdDialogButtonSizer()
2285    btn = wx.Button(dlg, wx.ID_OK)
2286    btn.SetDefault()
2287    btnsizer.AddButton(btn)
2288    #btn = wx.Button(dlg, wx.ID_CANCEL)
2289    #btnsizer.AddButton(btn)
2290    btnsizer.Realize()
2291    sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.EXPAND|wx.ALL, 5)
2292    dlg.SetSizer(sizer)
2293    sizer.Fit(dlg)
2294    val = dlg.ShowModal()
2295
2296################################################################################
2297class OrderBox(wxscroll.ScrolledPanel):
2298    '''Creates a panel with scrollbars where items can be ordered into columns
2299   
2300    :param list keylist: is a list of keys for column assignments
2301    :param dict vallookup: is a dict keyed by names in keylist where each item is a dict.
2302      Each inner dict contains variable names as keys and their associated values
2303    :param dict posdict: is a dict keyed by names in keylist where each item is a dict.
2304      Each inner dict contains column numbers as keys and their associated
2305      variable name as a value. This is used for both input and output.
2306     
2307    '''
2308    def __init__(self,parent,keylist,vallookup,posdict,*arg,**kw):
2309        self.keylist = keylist
2310        self.vallookup = vallookup
2311        self.posdict = posdict
2312        self.maxcol = 0
2313        for nam in keylist:
2314            posdict = self.posdict[nam]
2315            if posdict.keys():
2316                self.maxcol = max(self.maxcol, max(posdict))
2317        wxscroll.ScrolledPanel.__init__(self,parent,wx.ID_ANY,*arg,**kw)
2318        self.GBsizer = wx.GridBagSizer(4,4)
2319        self.SetBackgroundColour(WHITE)
2320        self.SetSizer(self.GBsizer)
2321        colList = [str(i) for i in range(self.maxcol+2)]
2322        for i in range(self.maxcol+1):
2323            wid = wx.StaticText(self,wx.ID_ANY,str(i),style=wx.ALIGN_CENTER)
2324            wid.SetBackgroundColour(DULL_YELLOW)
2325            wid.SetMinSize((50,-1))
2326            self.GBsizer.Add(wid,(0,i),flag=wx.ALIGN_CENTER|wx.EXPAND)
2327        self.chceDict = {}
2328        for row,nam in enumerate(self.keylist):
2329            posdict = self.posdict[nam]
2330            for col in posdict:
2331                lbl = posdict[col]
2332                pnl = wx.Panel(self,wx.ID_ANY)
2333                pnl.SetBackgroundColour(VERY_LIGHT_GREY)
2334                insize = wx.BoxSizer(wx.VERTICAL)
2335                wid = wx.Choice(pnl,wx.ID_ANY,choices=colList)
2336                insize.Add(wid,0,wx.EXPAND|wx.BOTTOM,3)
2337                wid.SetSelection(col)
2338                self.chceDict[wid] = (row,col)
2339                wid.Bind(wx.EVT_CHOICE,self.OnChoice)
2340                wid = wx.StaticText(pnl,wx.ID_ANY,lbl)
2341                insize.Add(wid,0,flag=wx.EXPAND)
2342                val = G2py3.FormatSigFigs(self.vallookup[nam][lbl],maxdigits=8)
2343                wid = wx.StaticText(pnl,wx.ID_ANY,'('+val+')')
2344                insize.Add(wid,0,flag=wx.EXPAND)
2345                pnl.SetSizer(insize)
2346                self.GBsizer.Add(pnl,(row+1,col),flag=wx.EXPAND)
2347        self.SetAutoLayout(1)
2348        self.SetupScrolling()
2349        self.SetMinSize((
2350            min(700,self.GBsizer.GetSize()[0]),
2351            self.GBsizer.GetSize()[1]+20))
2352    def OnChoice(self,event):
2353        '''Called when a column is assigned to a variable
2354        '''
2355        row,col = self.chceDict[event.EventObject] # which variable was this?
2356        newcol = event.Selection # where will it be moved?
2357        if newcol == col:
2358            return # no change: nothing to do!
2359        prevmaxcol = self.maxcol # save current table size
2360        key = self.keylist[row] # get the key for the current row
2361        lbl = self.posdict[key][col] # selected variable name
2362        lbl1 = self.posdict[key].get(col+1,'') # next variable name, if any
2363        # if a posXXX variable is selected, and the next variable is posXXX, move them together
2364        repeat = 1
2365        if lbl[:3] == 'pos' and lbl1[:3] == 'int' and lbl[3:] == lbl1[3:]:
2366            repeat = 2
2367        for i in range(repeat): # process the posXXX and then the intXXX (or a single variable)
2368            col += i
2369            newcol += i
2370            if newcol in self.posdict[key]:
2371                # find first non-blank after newcol
2372                for mtcol in range(newcol+1,self.maxcol+2):
2373                    if mtcol not in self.posdict[key]: break
2374                l1 = range(mtcol,newcol,-1)+[newcol]
2375                l = range(mtcol-1,newcol-1,-1)+[col]
2376            else:
2377                l1 = [newcol]
2378                l = [col]
2379            # move all of the items, starting from the last column
2380            for newcol,col in zip(l1,l):
2381                #print 'moving',col,'to',newcol
2382                self.posdict[key][newcol] = self.posdict[key][col]
2383                del self.posdict[key][col]
2384                self.maxcol = max(self.maxcol,newcol)
2385                obj = self.GBsizer.FindItemAtPosition((row+1,col))
2386                self.GBsizer.SetItemPosition(obj.GetWindow(),(row+1,newcol))
2387                for wid in obj.GetWindow().Children:
2388                    if wid in self.chceDict:
2389                        self.chceDict[wid] = (row,newcol)
2390                        wid.SetSelection(self.chceDict[wid][1])
2391        # has the table gotten larger? If so we need new column heading(s)
2392        if prevmaxcol != self.maxcol:
2393            for i in range(prevmaxcol+1,self.maxcol+1):
2394                wid = wx.StaticText(self,wx.ID_ANY,str(i),style=wx.ALIGN_CENTER)
2395                wid.SetBackgroundColour(DULL_YELLOW)
2396                wid.SetMinSize((50,-1))
2397                self.GBsizer.Add(wid,(0,i),flag=wx.ALIGN_CENTER|wx.EXPAND)
2398            colList = [str(i) for i in range(self.maxcol+2)]
2399            for wid in self.chceDict:
2400                wid.SetItems(colList)
2401                wid.SetSelection(self.chceDict[wid][1])
2402        self.GBsizer.Layout()
2403        self.FitInside()
2404
2405################################################################################
2406def GetImportFile(G2frame, message, defaultDir="", defaultFile="", style=wx.OPEN,
2407                  *args, **kwargs):
2408    '''Uses a customized dialog that gets files from the appropriate import directory.
2409    Arguments are used the same as in :func:`wx.FileDialog`. Selection of
2410    multiple files is allowed if argument style includes wx.MULTIPLE.
2411
2412    The default initial directory (unless overridden with argument defaultDir)
2413    is found in G2frame.TutorialImportDir, config setting Import_directory or
2414    G2frame.LastImportDir, see :func:`GetImportPath`.
2415
2416    The path of the first file entered is used to set G2frame.LastImportDir
2417    and optionally config setting Import_directory.
2418
2419    :returns: a list of files or an empty list
2420    '''
2421    dlg = wx.FileDialog(G2frame, message, defaultDir, defaultFile, *args,
2422                        style=style, **kwargs)
2423    pth = GetImportPath(G2frame)
2424    if not defaultDir and pth: dlg.SetDirectory(pth)
2425    try:
2426        if dlg.ShowModal() == wx.ID_OK:
2427            if style & wx.MULTIPLE:
2428                filelist = dlg.GetPaths()
2429                if len(filelist) == 0: return []
2430            else:
2431                filelist = [dlg.GetPath(),]
2432            # not sure if we want to do this (why use wx.CHANGE_DIR?)
2433            if style & wx.CHANGE_DIR: # to get Mac/Linux to change directory like windows!
2434                os.chdir(dlg.GetDirectory())
2435        else: # cancel was pressed
2436            return []
2437    finally:
2438        dlg.Destroy()
2439    # save the path of the first file and reset the TutorialImportDir variable
2440    pth = os.path.split(os.path.abspath(filelist[0]))[0]
2441    if GSASIIpath.GetConfigValue('Save_paths'): G2G.SaveImportDirectory(pth)
2442    G2frame.LastImportDir = pth
2443    G2frame.TutorialImportDir = None
2444    return filelist
2445
2446def GetImportPath(G2frame):
2447    '''Determines the default location to use for importing files. Tries sequentially
2448    G2frame.TutorialImportDir, config var Import_directory and G2frame.LastImportDir.
2449   
2450    :returns: a string containing the path to be used when reading files or None
2451      if none of the above are specified.
2452    '''
2453    if G2frame.TutorialImportDir:
2454        if os.path.exists(G2frame.TutorialImportDir):
2455            return G2frame.TutorialImportDir
2456        elif GSASIIpath.GetConfigValue('debug'):
2457            print('Tutorial location (TutorialImportDir) not found: '+G2frame.TutorialImportDir)
2458    pth = GSASIIpath.GetConfigValue('Import_directory')
2459    if pth:
2460        pth = os.path.expanduser(pth)
2461        if os.path.exists(pth):
2462            return pth
2463        elif GSASIIpath.GetConfigValue('debug'):
2464            print('Ignoring Config Import_directory value: '+
2465                      GSASIIpath.GetConfigValue('Import_directory'))
2466    if G2frame.LastImportDir:
2467        if os.path.exists(G2frame.LastImportDir):
2468            return G2frame.LastImportDir
2469        elif GSASIIpath.GetConfigValue('debug'):
2470            print('Warning: G2frame.LastImportDir not found = '+G2frame.LastImportDir)
2471    return None
2472
2473def GetExportPath(G2frame):
2474    '''Determines the default location to use for writing files. Tries sequentially
2475    G2frame.LastExportDir and G2frame.LastGPXdir.
2476   
2477    :returns: a string containing the path to be used when writing files or '.'
2478      if none of the above are specified.
2479    '''
2480    if G2frame.LastExportDir:
2481        return G2frame.LastExportDir
2482    elif G2frame.LastGPXdir:
2483        return G2frame.LastGPXdir
2484    else:
2485        return '.'
2486
2487################################################################################
2488#####  Customized Grid Support
2489################################################################################           
2490class GSGrid(wg.Grid):
2491    '''Basic wx.Grid implementation
2492    '''
2493    def __init__(self, parent, name=''):
2494        wg.Grid.__init__(self,parent,-1,name=name)                   
2495        #self.SetSize(parent.GetClientSize())
2496        # above removed to speed drawing of initial grid
2497        # does not appear to be needed
2498           
2499    def Clear(self):
2500        wg.Grid.ClearGrid(self)
2501       
2502    def SetCellReadOnly(self,r,c,readonly=True):
2503        self.SetReadOnly(r,c,isReadOnly=readonly)
2504       
2505    def SetCellStyle(self,r,c,color="white",readonly=True):
2506        self.SetCellBackgroundColour(r,c,color)
2507        self.SetReadOnly(r,c,isReadOnly=readonly)
2508       
2509    def GetSelection(self):
2510        #this is to satisfy structure drawing stuff in G2plt when focus changes
2511        return None
2512
2513    def InstallGridToolTip(self, rowcolhintcallback,
2514                           colLblCallback=None,rowLblCallback=None):
2515        '''code to display a tooltip for each item on a grid
2516        from http://wiki.wxpython.org/wxGrid%20ToolTips (buggy!), expanded to
2517        column and row labels using hints from
2518        https://groups.google.com/forum/#!topic/wxPython-users/bm8OARRVDCs
2519
2520        :param function rowcolhintcallback: a routine that returns a text
2521          string depending on the selected row and column, to be used in
2522          explaining grid entries.
2523        :param function colLblCallback: a routine that returns a text
2524          string depending on the selected column, to be used in
2525          explaining grid columns (if None, the default), column labels
2526          do not get a tooltip.
2527        :param function rowLblCallback: a routine that returns a text
2528          string depending on the selected row, to be used in
2529          explaining grid rows (if None, the default), row labels
2530          do not get a tooltip.
2531        '''
2532        prev_rowcol = [None,None,None]
2533        def OnMouseMotion(event):
2534            # event.GetRow() and event.GetCol() would be nice to have here,
2535            # but as this is a mouse event, not a grid event, they are not
2536            # available and we need to compute them by hand.
2537            x, y = self.CalcUnscrolledPosition(event.GetPosition())
2538            row = self.YToRow(y)
2539            col = self.XToCol(x)
2540            hinttext = ''
2541            win = event.GetEventObject()
2542            if [row,col,win] == prev_rowcol: # no change from last position
2543                event.Skip()
2544                return
2545            if win == self.GetGridWindow() and row >= 0 and col >= 0:
2546                hinttext = rowcolhintcallback(row, col)
2547            elif win == self.GetGridColLabelWindow() and col >= 0:
2548                if colLblCallback: hinttext = colLblCallback(col)
2549            elif win == self.GetGridRowLabelWindow() and row >= 0:
2550                if rowLblCallback: hinttext = rowLblCallback(row)
2551            else: # this should be the upper left corner, which is empty
2552                event.Skip()
2553                return
2554            if hinttext is None: hinttext = ''
2555            win.SetToolTipString(hinttext)
2556            prev_rowcol[:] = [row,col,win]
2557            event.Skip()
2558
2559        wx.EVT_MOTION(self.GetGridWindow(), OnMouseMotion)
2560        if colLblCallback: wx.EVT_MOTION(self.GetGridColLabelWindow(), OnMouseMotion)
2561        if rowLblCallback: wx.EVT_MOTION(self.GetGridRowLabelWindow(), OnMouseMotion)
2562                                                   
2563################################################################################           
2564class Table(wg.PyGridTableBase):
2565    '''Basic data table for use with GSgrid
2566    '''
2567    def __init__(self, data=[], rowLabels=None, colLabels=None, types = None):
2568        wg.PyGridTableBase.__init__(self)
2569        self.colLabels = colLabels
2570        self.rowLabels = rowLabels
2571        self.dataTypes = types
2572        self.data = data
2573       
2574    def AppendRows(self, numRows=1):
2575        self.data.append([])
2576        return True
2577       
2578    def CanGetValueAs(self, row, col, typeName):
2579        if self.dataTypes:
2580            colType = self.dataTypes[col].split(':')[0]
2581            if typeName == colType:
2582                return True
2583            else:
2584                return False
2585        else:
2586            return False
2587
2588    def CanSetValueAs(self, row, col, typeName):
2589        return self.CanGetValueAs(row, col, typeName)
2590
2591    def DeleteRow(self,pos):
2592        data = self.GetData()
2593        self.SetData([])
2594        new = []
2595        for irow,row in enumerate(data):
2596            if irow <> pos:
2597                new.append(row)
2598        self.SetData(new)
2599       
2600    def GetColLabelValue(self, col):
2601        if self.colLabels:
2602            return self.colLabels[col]
2603           
2604    def GetData(self):
2605        data = []
2606        for row in range(self.GetNumberRows()):
2607            data.append(self.GetRowValues(row))
2608        return data
2609       
2610    def GetNumberCols(self):
2611        try:
2612            return len(self.colLabels)
2613        except TypeError:
2614            return None
2615       
2616    def GetNumberRows(self):
2617        return len(self.data)
2618       
2619    def GetRowLabelValue(self, row):
2620        if self.rowLabels:
2621            return self.rowLabels[row]
2622       
2623    def GetColValues(self, col):
2624        data = []
2625        for row in range(self.GetNumberRows()):
2626            data.append(self.GetValue(row, col))
2627        return data
2628       
2629    def GetRowValues(self, row):
2630        data = []
2631        for col in range(self.GetNumberCols()):
2632            data.append(self.GetValue(row, col))
2633        return data
2634       
2635    def GetTypeName(self, row, col):
2636        try:
2637            if self.data[row][col] is None: return None
2638            return self.dataTypes[col]
2639        except (TypeError,IndexError):
2640            return None
2641
2642    def GetValue(self, row, col):
2643        try:
2644            if self.data[row][col] is None: return ""
2645            return self.data[row][col]
2646        except IndexError:
2647            return None
2648           
2649    def InsertRows(self, pos, rows):
2650        for row in range(rows):
2651            self.data.insert(pos,[])
2652            pos += 1
2653       
2654    def IsEmptyCell(self,row,col):
2655        try:
2656            return not self.data[row][col]
2657        except IndexError:
2658            return True
2659       
2660    def OnKeyPress(self, event):
2661        dellist = self.GetSelectedRows()
2662        if event.GetKeyCode() == wx.WXK_DELETE and dellist:
2663            grid = self.GetView()
2664            for i in dellist: grid.DeleteRow(i)
2665               
2666    def SetColLabelValue(self, col, label):
2667        numcols = self.GetNumberCols()
2668        if col > numcols-1:
2669            self.colLabels.append(label)
2670        else:
2671            self.colLabels[col]=label
2672       
2673    def SetData(self,data):
2674        for row in range(len(data)):
2675            self.SetRowValues(row,data[row])
2676               
2677    def SetRowLabelValue(self, row, label):
2678        self.rowLabels[row]=label
2679           
2680    def SetRowValues(self,row,data):
2681        self.data[row] = data
2682           
2683    def SetValue(self, row, col, value):
2684        def innerSetValue(row, col, value):
2685            try:
2686                self.data[row][col] = value
2687            except TypeError:
2688                return
2689            except IndexError: # has this been tested?
2690                #print row,col,value
2691                # add a new row
2692                if row > self.GetNumberRows():
2693                    self.data.append([''] * self.GetNumberCols())
2694                elif col > self.GetNumberCols():
2695                    for row in range(self.GetNumberRows()): # bug fixed here
2696                        self.data[row].append('')
2697                #print self.data
2698                self.data[row][col] = value
2699        innerSetValue(row, col, value)
2700
2701################################################################################
2702class GridFractionEditor(wg.PyGridCellEditor):
2703    '''A grid cell editor class that allows entry of values as fractions as well
2704    as sine and cosine values [as s() and c()]
2705    '''
2706    def __init__(self,grid):
2707        wg.PyGridCellEditor.__init__(self)
2708
2709    def Create(self, parent, id, evtHandler):
2710        self._tc = wx.TextCtrl(parent, id, "")
2711        self._tc.SetInsertionPoint(0)
2712        self.SetControl(self._tc)
2713
2714        if evtHandler:
2715            self._tc.PushEventHandler(evtHandler)
2716
2717        self._tc.Bind(wx.EVT_CHAR, self.OnChar)
2718
2719    def SetSize(self, rect):
2720        self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
2721                               wx.SIZE_ALLOW_MINUS_ONE)
2722
2723    def BeginEdit(self, row, col, grid):
2724        self.startValue = grid.GetTable().GetValue(row, col)
2725        self._tc.SetValue(str(self.startValue))
2726        self._tc.SetInsertionPointEnd()
2727        self._tc.SetFocus()
2728        self._tc.SetSelection(0, self._tc.GetLastPosition())
2729
2730    def EndEdit(self, row, col, grid, oldVal=None):
2731        changed = False
2732
2733        self.nextval = self.startValue
2734        val = self._tc.GetValue().lower()
2735        if val != self.startValue:
2736            changed = True
2737            neg = False
2738            if '-' in val:
2739                neg = True
2740            if '/' in val and '.' not in val:
2741                val += '.'
2742            elif 's' in val and not 'sind(' in val:
2743                if neg:
2744                    val = '-sind('+val.strip('-s')+')'
2745                else:
2746                    val = 'sind('+val.strip('s')+')'
2747            elif 'c' in val and not 'cosd(' in val:
2748                if neg:
2749                    val = '-cosd('+val.strip('-c')+')'
2750                else:
2751                    val = 'cosd('+val.strip('c')+')'
2752            try:
2753                self.nextval = val = float(eval(val))
2754            except (SyntaxError,NameError,ZeroDivisionError):
2755                val = self.startValue
2756                return None
2757           
2758            if oldVal is None: # this arg appears in 2.9+; before, we should go ahead & change the table
2759                grid.GetTable().SetValue(row, col, val) # update the table
2760            # otherwise self.ApplyEdit gets called
2761
2762        self.startValue = ''
2763        self._tc.SetValue('')
2764        return changed
2765   
2766    def ApplyEdit(self, row, col, grid):
2767        """ Called only in wx >= 2.9
2768        Save the value of the control into the grid if EndEdit() returns as True
2769        """
2770        grid.GetTable().SetValue(row, col, self.nextval) # update the table
2771
2772    def Reset(self):
2773        self._tc.SetValue(self.startValue)
2774        self._tc.SetInsertionPointEnd()
2775
2776    def Clone(self):
2777        return GridFractionEditor(grid)
2778
2779    def StartingKey(self, evt):
2780        self.OnChar(evt)
2781        if evt.GetSkipped():
2782            self._tc.EmulateKeyPress(evt)
2783
2784    def OnChar(self, evt):
2785        key = evt.GetKeyCode()
2786        if key == 15:
2787            return
2788        if key > 255:
2789            evt.Skip()
2790            return
2791        char = chr(key)
2792        if char in '.+-/0123456789cosind()':
2793            self._tc.WriteText(char)
2794        else:
2795            evt.Skip()
2796           
2797################################################################################
2798#####  Customized Notebook
2799################################################################################           
2800class GSNoteBook(wx.aui.AuiNotebook):
2801    '''Notebook used in various locations; implemented with wx.aui extension
2802    '''
2803    def __init__(self, parent, name='',size = None):
2804        wx.aui.AuiNotebook.__init__(self, parent, -1,
2805                                    style=wx.aui.AUI_NB_TOP |
2806                                    wx.aui.AUI_NB_SCROLL_BUTTONS)
2807        if size: self.SetSize(size)
2808        self.parent = parent
2809        self.PageChangeHandler = None
2810       
2811    def PageChangeEvent(self,event):
2812        G2frame = self.parent.G2frame
2813        page = event.GetSelection()
2814        if self.PageChangeHandler:
2815            if log.LogInfo['Logging']:
2816                log.MakeTabLog(
2817                    G2frame.dataFrame.GetTitle(),
2818                    G2frame.dataDisplay.GetPageText(page)
2819                    )
2820            self.PageChangeHandler(event)
2821           
2822    def Bind(self,eventtype,handler,*args,**kwargs):
2823        '''Override the Bind() function so that page change events can be trapped
2824        '''
2825        if eventtype == wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED:
2826            self.PageChangeHandler = handler
2827            wx.aui.AuiNotebook.Bind(self,eventtype,self.PageChangeEvent)
2828            return
2829        wx.aui.AuiNotebook.Bind(self,eventtype,handler,*args,**kwargs)
2830                                                     
2831    def Clear(self):       
2832        GSNoteBook.DeleteAllPages(self)
2833       
2834    def FindPage(self,name):
2835        numPage = self.GetPageCount()
2836        for page in range(numPage):
2837            if self.GetPageText(page) == name:
2838                return page
2839
2840    def ChangeSelection(self,page):
2841        # in wx.Notebook ChangeSelection is like SetSelection, but it
2842        # does not invoke the event related to pressing the tab button
2843        # I don't see a way to do that in aui.
2844        oldPage = self.GetSelection()
2845        self.SetSelection(page)
2846        return oldPage
2847
2848    # def __getattribute__(self,name):
2849    #     '''This method provides a way to print out a message every time
2850    #     that a method in a class is called -- to see what all the calls
2851    #     might be, or where they might be coming from.
2852    #     Cute trick for debugging!
2853    #     '''
2854    #     attr = object.__getattribute__(self, name)
2855    #     if hasattr(attr, '__call__'):
2856    #         def newfunc(*args, **kwargs):
2857    #             print('GSauiNoteBook calling %s' %attr.__name__)
2858    #             result = attr(*args, **kwargs)
2859    #             return result
2860    #         return newfunc
2861    #     else:
2862    #         return attr
2863           
2864################################################################################
2865#### Help support routines
2866################################################################################
2867class MyHelp(wx.Menu):
2868    '''
2869    A class that creates the contents of a help menu.
2870    The menu will start with two entries:
2871
2872    * 'Help on <helpType>': where helpType is a reference to an HTML page to
2873      be opened
2874    * About: opens an About dialog using OnHelpAbout. N.B. on the Mac this
2875      gets moved to the App menu to be consistent with Apple style.
2876
2877    NOTE: for this to work properly with respect to system menus, the title
2878    for the menu must be &Help, or it will not be processed properly:
2879
2880    ::
2881
2882       menu.Append(menu=MyHelp(self,...),title="&Help")
2883
2884    '''
2885    def __init__(self,frame,helpType=None,helpLbl=None,morehelpitems=[],title=''):
2886        wx.Menu.__init__(self,title)
2887        self.HelpById = {}
2888        self.frame = frame
2889        self.Append(help='', id=wx.ID_ABOUT, kind=wx.ITEM_NORMAL,
2890            text='&About GSAS-II')
2891        frame.Bind(wx.EVT_MENU, self.OnHelpAbout, id=wx.ID_ABOUT)
2892        if GSASIIpath.whichsvn():
2893            helpobj = self.Append(
2894                help='', id=wx.ID_ANY, kind=wx.ITEM_NORMAL,
2895                text='&Check for updates')
2896            frame.Bind(wx.EVT_MENU, self.OnCheckUpdates, helpobj)
2897            helpobj = self.Append(
2898                help='', id=wx.ID_ANY, kind=wx.ITEM_NORMAL,
2899                text='&Regress to an old GSAS-II version')
2900            frame.Bind(wx.EVT_MENU, self.OnSelectVersion, helpobj)
2901        for lbl,indx in morehelpitems:
2902            helpobj = self.Append(text=lbl,
2903                id=wx.ID_ANY, kind=wx.ITEM_NORMAL)
2904            frame.Bind(wx.EVT_MENU, self.OnHelpById, helpobj)
2905            self.HelpById[helpobj.GetId()] = indx
2906        # add a help item only when helpType is specified
2907        if helpType is not None:
2908            self.AppendSeparator()
2909            if helpLbl is None: helpLbl = helpType
2910            helpobj = self.Append(text='Help on '+helpLbl,
2911                                  id=wx.ID_ANY, kind=wx.ITEM_NORMAL)
2912            frame.Bind(wx.EVT_MENU, self.OnHelpById, helpobj)
2913            self.HelpById[helpobj.GetId()] = helpType
2914       
2915    def OnHelpById(self,event):
2916        '''Called when Help on... is pressed in a menu. Brings up
2917        a web page for documentation.
2918        '''
2919        helpType = self.HelpById.get(event.GetId())
2920        if helpType is None:
2921            print 'Error: help lookup failed!',event.GetEventObject()
2922            print 'id=',event.GetId()
2923        elif helpType == 'Tutorials': 
2924            dlg = OpenTutorial(self.frame)
2925            dlg.ShowModal()
2926            dlg.Destroy()
2927            return
2928        else:
2929            ShowHelp(helpType,self.frame)
2930
2931    def OnHelpAbout(self, event):
2932        "Display an 'About GSAS-II' box"
2933        import GSASII
2934        info = wx.AboutDialogInfo()
2935        info.Name = 'GSAS-II'
2936        ver = GSASIIpath.svnGetRev()
2937        if ver: 
2938            info.Version = 'Revision '+str(ver)+' (svn), version '+GSASII.__version__
2939        else:
2940            info.Version = 'Revision '+str(GSASIIpath.GetVersionNumber())+' (.py files), version '+GSASII.__version__
2941        #info.Developers = ['Robert B. Von Dreele','Brian H. Toby']
2942        info.Copyright = ('(c) ' + time.strftime('%Y') +
2943''' Argonne National Laboratory
2944This product includes software developed
2945by the UChicago Argonne, LLC, as
2946Operator of Argonne National Laboratory.''')
2947        info.Description = '''General Structure Analysis System-II (GSAS-II)
2948Robert B. Von Dreele and Brian H. Toby
2949
2950Please cite as:
2951B.H. Toby & R.B. Von Dreele, J. Appl. Cryst. 46, 544-549 (2013) '''
2952
2953        info.WebSite = ("https://subversion.xray.aps.anl.gov/trac/pyGSAS","GSAS-II home page")
2954        wx.AboutBox(info)
2955
2956    def OnCheckUpdates(self,event):
2957        '''Check if the GSAS-II repository has an update for the current source files
2958        and perform that update if requested.
2959        '''
2960        if not GSASIIpath.whichsvn():
2961            dlg = wx.MessageDialog(self.frame,
2962                                   'No Subversion','Cannot update GSAS-II because subversion (svn) was not found.',
2963                                   wx.OK)
2964            dlg.ShowModal()
2965            dlg.Destroy()
2966            return
2967        wx.BeginBusyCursor()
2968        local = GSASIIpath.svnGetRev()
2969        if local is None: 
2970            wx.EndBusyCursor()
2971            dlg = wx.MessageDialog(self.frame,
2972                                   'Unable to run subversion on the GSAS-II current directory. Is GSAS-II installed correctly?',
2973                                   'Subversion error',
2974                                   wx.OK)
2975            dlg.ShowModal()
2976            dlg.Destroy()
2977            return
2978        print 'Installed GSAS-II version: '+local
2979        repos = GSASIIpath.svnGetRev(local=False)
2980        wx.EndBusyCursor()
2981        if repos is None: 
2982            dlg = wx.MessageDialog(self.frame,
2983                                   'Unable to access the GSAS-II server. Is this computer on the internet?',
2984                                   'Server unavailable',
2985                                   wx.OK)
2986            dlg.ShowModal()
2987            dlg.Destroy()
2988            return
2989        print 'GSAS-II version on server: '+repos
2990        if local == repos:
2991            dlg = wx.MessageDialog(self.frame,
2992                                   'GSAS-II is up-to-date. Version '+local+' is already loaded.',
2993                                   'GSAS-II Up-to-date',
2994                                   wx.OK)
2995            dlg.ShowModal()
2996            dlg.Destroy()
2997            return
2998        mods = GSASIIpath.svnFindLocalChanges()
2999        if mods:
3000            dlg = wx.MessageDialog(self.frame,
3001                                   'You have version '+local+
3002                                   ' of GSAS-II installed, but the current version is '+repos+
3003                                   '. However, '+str(len(mods))+
3004                                   ' file(s) on your local computer have been modified.'
3005                                   ' Updating will attempt to merge your local changes with '
3006                                   'the latest GSAS-II version, but if '
3007                                   'conflicts arise, local changes will be '
3008                                   'discarded. It is also possible that the '
3009                                   'local changes my prevent GSAS-II from running. '
3010                                   'Press OK to start an update if this is acceptable:',
3011                                   'Local GSAS-II Mods',
3012                                   wx.OK|wx.CANCEL)
3013            if dlg.ShowModal() != wx.ID_OK:
3014                dlg.Destroy()
3015                return
3016            else:
3017                dlg.Destroy()
3018        else:
3019            dlg = wx.MessageDialog(self.frame,
3020                                   'You have version '+local+
3021                                   ' of GSAS-II installed, but the current version is '+repos+
3022                                   '. Press OK to start an update:',
3023                                   'GSAS-II Updates',
3024                                   wx.OK|wx.CANCEL)
3025            if dlg.ShowModal() != wx.ID_OK:
3026                dlg.Destroy()
3027                return
3028            dlg.Destroy()
3029        print 'start updates'
3030        dlg = wx.MessageDialog(self.frame,
3031                               'Your project will now be saved, GSAS-II will exit and an update '
3032                               'will be performed and GSAS-II will restart. Press Cancel to '
3033                               'abort the update',
3034                               'Start update?',
3035                               wx.OK|wx.CANCEL)
3036        if dlg.ShowModal() != wx.ID_OK:
3037            dlg.Destroy()
3038            return
3039        dlg.Destroy()
3040        self.frame.OnFileSave(event)
3041        GSASIIpath.svnUpdateProcess(projectfile=self.frame.GSASprojectfile)
3042        return
3043
3044    def OnSelectVersion(self,event):
3045        '''Allow the user to select a specific version of GSAS-II
3046        '''
3047        if not GSASIIpath.whichsvn():
3048            dlg = wx.MessageDialog(self,'No Subversion','Cannot update GSAS-II because subversion (svn) '+
3049                                   'was not found.'
3050                                   ,wx.OK)
3051            dlg.ShowModal()
3052            return
3053        local = GSASIIpath.svnGetRev()
3054        if local is None: 
3055            dlg = wx.MessageDialog(self.frame,
3056                                   'Unable to run subversion on the GSAS-II current directory. Is GSAS-II installed correctly?',
3057                                   'Subversion error',
3058                                   wx.OK)
3059            dlg.ShowModal()
3060            return
3061        mods = GSASIIpath.svnFindLocalChanges()
3062        if mods:
3063            dlg = wx.MessageDialog(self.frame,
3064                                   'You have version '+local+
3065                                   ' of GSAS-II installed'
3066                                   '. However, '+str(len(mods))+
3067                                   ' file(s) on your local computer have been modified.'
3068                                   ' Downdating will attempt to merge your local changes with '
3069                                   'the selected GSAS-II version. '
3070                                   'Downdating is not encouraged because '
3071                                   'if merging is not possible, your local changes will be '
3072                                   'discarded. It is also possible that the '
3073                                   'local changes my prevent GSAS-II from running. '
3074                                   'Press OK to continue anyway.',
3075                                   'Local GSAS-II Mods',
3076                                   wx.OK|wx.CANCEL)
3077            if dlg.ShowModal() != wx.ID_OK:
3078                dlg.Destroy()
3079                return
3080            dlg.Destroy()
3081        dlg = downdate(parent=self.frame)
3082        if dlg.ShowModal() == wx.ID_OK:
3083            ver = dlg.getVersion()
3084        else:
3085            dlg.Destroy()
3086            return
3087        dlg.Destroy()
3088        print('start regress to '+str(ver))
3089        GSASIIpath.svnUpdateProcess(
3090            projectfile=self.frame.GSASprojectfile,
3091            version=str(ver)
3092            )
3093        self.frame.OnFileSave(event)
3094        return
3095
3096################################################################################
3097class AddHelp(wx.Menu):
3098    '''For the Mac: creates an entry to the help menu of type
3099    'Help on <helpType>': where helpType is a reference to an HTML page to
3100    be opened.
3101
3102    NOTE: when appending this menu (menu.Append) be sure to set the title to
3103    '&Help' so that wx handles it correctly.
3104    '''
3105    def __init__(self,frame,helpType,helpLbl=None,title=''):
3106        wx.Menu.__init__(self,title)
3107        self.frame = frame
3108        if helpLbl is None: helpLbl = helpType
3109        # add a help item only when helpType is specified
3110        helpobj = self.Append(text='Help on '+helpLbl,
3111                              id=wx.ID_ANY, kind=wx.ITEM_NORMAL)
3112        frame.Bind(wx.EVT_MENU, self.OnHelpById, helpobj)
3113        self.HelpById = helpType
3114       
3115    def OnHelpById(self,event):
3116        '''Called when Help on... is pressed in a menu. Brings up
3117        a web page for documentation.
3118        '''
3119        ShowHelp(self.HelpById,self.frame)
3120
3121################################################################################
3122class HelpButton(wx.Button):
3123    '''Create a help button that displays help information.
3124    The text is displayed in a modal message window.
3125
3126    TODO: it might be nice if it were non-modal: e.g. it stays around until
3127    the parent is deleted or the user closes it, but this did not work for
3128    me.
3129
3130    :param parent: the panel which will be the parent of the button
3131    :param str msg: the help text to be displayed
3132    '''
3133    def __init__(self,parent,msg):
3134        if sys.platform == "darwin": 
3135            wx.Button.__init__(self,parent,wx.ID_HELP)
3136        else:
3137            wx.Button.__init__(self,parent,wx.ID_ANY,'?',style=wx.BU_EXACTFIT)
3138        self.Bind(wx.EVT_BUTTON,self._onPress)
3139        self.msg=StripIndents(msg)
3140        self.parent = parent
3141    def _onClose(self,event):
3142        self.dlg.EndModal(wx.ID_CANCEL)
3143    def _onPress(self,event):
3144        'Respond to a button press by displaying the requested text'
3145        #dlg = wx.MessageDialog(self.parent,self.msg,'Help info',wx.OK)
3146        self.dlg = wx.Dialog(self.parent,wx.ID_ANY,'Help information', 
3147                        style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
3148        #self.dlg.SetBackgroundColour(wx.WHITE)
3149        mainSizer = wx.BoxSizer(wx.VERTICAL)
3150        txt = wx.StaticText(self.dlg,wx.ID_ANY,self.msg)
3151        mainSizer.Add(txt,1,wx.ALL|wx.EXPAND,10)
3152        txt.SetBackgroundColour(wx.WHITE)
3153
3154        btnsizer = wx.BoxSizer(wx.HORIZONTAL)
3155        btn = wx.Button(self.dlg, wx.ID_CLOSE) 
3156        btn.Bind(wx.EVT_BUTTON,self._onClose)
3157        btnsizer.Add(btn)
3158        mainSizer.Add(btnsizer, 0, wx.ALIGN_CENTER|wx.ALL, 5)
3159        self.dlg.SetSizer(mainSizer)
3160        mainSizer.Fit(self.dlg)
3161        self.dlg.CenterOnParent()
3162        self.dlg.ShowModal()
3163        self.dlg.Destroy()
3164################################################################################
3165class MyHtmlPanel(wx.Panel):
3166    '''Defines a panel to display HTML help information, as an alternative to
3167    displaying help information in a web browser.
3168    '''
3169    def __init__(self, frame, id):
3170        self.frame = frame
3171        wx.Panel.__init__(self, frame, id)
3172        sizer = wx.BoxSizer(wx.VERTICAL)
3173        back = wx.Button(self, -1, "Back")
3174        back.Bind(wx.EVT_BUTTON, self.OnBack)
3175        self.htmlwin = G2HtmlWindow(self, id, size=(750,450))
3176        sizer.Add(self.htmlwin, 1,wx.EXPAND)
3177        sizer.Add(back, 0, wx.ALIGN_LEFT, 0)
3178        self.SetSizer(sizer)
3179        sizer.Fit(frame)       
3180        self.Bind(wx.EVT_SIZE,self.OnHelpSize)
3181    def OnHelpSize(self,event):         #does the job but weirdly!!
3182        anchor = self.htmlwin.GetOpenedAnchor()
3183        if anchor:           
3184            self.htmlwin.ScrollToAnchor(anchor)
3185            wx.CallAfter(self.htmlwin.ScrollToAnchor,anchor)
3186            event.Skip()
3187    def OnBack(self, event):
3188        self.htmlwin.HistoryBack()
3189    def LoadFile(self,file):
3190        pos = file.rfind('#')
3191        if pos != -1:
3192            helpfile = file[:pos]
3193            helpanchor = file[pos+1:]
3194        else:
3195            helpfile = file
3196            helpanchor = None
3197        self.htmlwin.LoadPage(helpfile)
3198        if helpanchor is not None:
3199            self.htmlwin.ScrollToAnchor(helpanchor)
3200            xs,ys = self.htmlwin.GetViewStart()
3201            self.htmlwin.Scroll(xs,ys-1)
3202################################################################################
3203class G2HtmlWindow(wx.html.HtmlWindow):
3204    '''Displays help information in a primitive HTML browser type window
3205    '''
3206    def __init__(self, parent, *args, **kwargs):
3207        self.parent = parent
3208        wx.html.HtmlWindow.__init__(self, parent, *args, **kwargs)
3209    def LoadPage(self, *args, **kwargs):
3210        wx.html.HtmlWindow.LoadPage(self, *args, **kwargs)
3211        self.TitlePage()
3212    def OnLinkClicked(self, *args, **kwargs):
3213        wx.html.HtmlWindow.OnLinkClicked(self, *args, **kwargs)
3214        xs,ys = self.GetViewStart()
3215        self.Scroll(xs,ys-1)
3216        self.TitlePage()
3217    def HistoryBack(self, *args, **kwargs):
3218        wx.html.HtmlWindow.HistoryBack(self, *args, **kwargs)
3219        self.TitlePage()
3220    def TitlePage(self):
3221        self.parent.frame.SetTitle(self.GetOpenedPage() + ' -- ' + 
3222            self.GetOpenedPageTitle())
3223
3224################################################################################
3225def StripIndents(msg):
3226    'Strip indentation from multiline strings'
3227    msg1 = msg.replace('\n ','\n')
3228    while msg != msg1:
3229        msg = msg1
3230        msg1 = msg.replace('\n ','\n')
3231    return msg.replace('\n\t','\n')
3232       
3233################################################################################
3234# configuration routines (for editing config.py)
3235def SaveGPXdirectory(path):
3236    import config_example
3237    vars = GetConfigValsDocs()
3238    try:
3239        vars['Starting_directory'][1] = path
3240        if GSASIIpath.GetConfigValue('debug'): print('Saving GPX path: '+path)
3241        SaveConfigVars(vars)
3242    except KeyError:
3243        pass
3244
3245def SaveImportDirectory(path):
3246    import config_example
3247    vars = GetConfigValsDocs()
3248    try:
3249        vars['Import_directory'][1] = path
3250        if GSASIIpath.GetConfigValue('debug'): print('Saving Import path: '+path)
3251        SaveConfigVars(vars)
3252    except KeyError:
3253        pass
3254
3255def GetConfigValsDocs():
3256    '''Reads the module referenced in fname (often <module>.__file__) and
3257    return a dict with names of global variables as keys.
3258    For each global variable, the value contains four items:
3259
3260    :returns: a dict where keys are names defined in module config_example.py
3261      where the value is a list of four items, as follows:
3262
3263         * item 0: the default value
3264         * item 1: the current value
3265         * item 2: the initial value (starts same as item 1)
3266         * item 3: the "docstring" that follows variable definition
3267
3268    '''
3269    import config_example
3270    import ast
3271    fname = os.path.splitext(config_example.__file__)[0]+'.py' # convert .pyc to .py
3272    with open(fname, 'r') as f:
3273        fstr = f.read()
3274    fstr = fstr.replace('\r\n', '\n').replace('\r', '\n')
3275    if not fstr.endswith('\n'):
3276        fstr += '\n'
3277    tree = ast.parse(fstr)
3278    d = {}
3279    key = None
3280    for node in ast.walk(tree):
3281        if isinstance(node,ast.Assign):
3282            key = node.targets[0].id
3283            d[key] = [config_example.__dict__.get(key),
3284                      GSASIIpath.configDict.get(key),
3285                      GSASIIpath.configDict.get(key),'']
3286        elif isinstance(node,ast.Expr) and key:
3287            d[key][3] = node.value.s.strip()
3288        else:
3289            key = None
3290    return d
3291
3292def SaveConfigVars(vars,parent=None):
3293    '''Write the current config variable values to config.py
3294
3295    :params dict vars: a dictionary of variable settings and meanings as
3296      created in :func:`GetConfigValsDocs`.
3297    :param parent: wx.Frame object or None (default) for parent
3298      of error message if no file can be written.
3299    :returns: True if unable to write the file, None otherwise
3300    '''
3301    # try to write to where an old config file is located
3302    try:
3303        import config
3304        savefile = config.__file__
3305    except ImportError: # no config.py file yet
3306        savefile = os.path.join(GSASIIpath.path2GSAS2,'config.py')
3307    # try to open file for write
3308    try:
3309        savefile = os.path.splitext(savefile)[0]+'.py' # convert .pyc to .py
3310        fp = open(savefile,'w')
3311    except IOError:  # can't write there, write in local mods directory
3312        # create a local mods directory, if needed
3313        if not os.path.exists(os.path.expanduser('~/.G2local/')):
3314            print('Creating directory '+os.path.expanduser('~/.G2local/'))
3315            os.mkdir(os.path.expanduser('~/.G2local/'))
3316            sys.path.insert(0,os.path.expanduser('~/.G2local/'))
3317        savefile = os.path.join(os.path.expanduser('~/.G2local/'),'config.py')
3318        try:
3319            fp = open(savefile,'w')
3320        except IOError:
3321            if parent:
3322                G2MessageBox(parent,
3323                             'Error trying to write configuration to '+savefile,
3324                             'Unable to save')
3325            else:
3326                print('Error trying to write configuration to '+savefile)
3327            return True
3328    import datetime
3329    fp.write("'''\n")
3330    fp.write("*config.py: Configuration options*\n----------------------------------\n")
3331    fp.write("This file created in SelectConfigSetting on {:%d %b %Y %H:%M}\n".
3332             format(datetime.datetime.now()))
3333    fp.write("'''\n\n")
3334    fp.write("import os.path\n")
3335    fp.write("import GSASIIpath\n\n")
3336    for var in sorted(vars.keys(),key=lambda s: s.lower()):
3337        if vars[var][1] is None: continue
3338        if vars[var][1] == '': continue
3339        if vars[var][0] == vars[var][1]: continue
3340        try:
3341            float(vars[var][1]) # test for number
3342            fp.write(var + ' = ' + str(vars[var][1])+'\n')
3343        except:
3344            try:
3345                eval(vars[var][1]) # test for an expression
3346                fp.write(var + ' = ' + str(vars[var][1])+'\n')
3347            except: # must be a string
3348                fp.write(var + ' = "' + str(vars[var][1])+'"\n')
3349        if vars[var][3]:
3350            fp.write("'''" + str(vars[var][3]) + "\n'''\n\n")
3351    fp.close()
3352    print('wrote file '+savefile)
3353
3354class SelectConfigSetting(wx.Dialog):
3355    '''Dialog to select configuration variables and set associated values.
3356    '''
3357    def __init__(self,parent=None):
3358        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
3359        wx.Dialog.__init__(self, parent, wx.ID_ANY, 'Set Config Variable', style=style)
3360        self.sizer = wx.BoxSizer(wx.VERTICAL)
3361        self.vars = GetConfigValsDocs()
3362       
3363        label = wx.StaticText(
3364            self,  wx.ID_ANY,
3365            'Select a GSAS-II configuration variable to change'
3366            )
3367        self.sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
3368        self.choice = {}
3369        btn = G2ChoiceButton(self, sorted(self.vars.keys(),key=lambda s: s.lower()),
3370                                 strLoc=self.choice,strKey=0,
3371                                 onChoice=self.OnSelection)
3372        btn.SetLabel("")
3373        self.sizer.Add(btn)
3374
3375        self.varsizer = wx.BoxSizer(wx.VERTICAL)
3376        self.sizer.Add(self.varsizer,1,wx.ALL|wx.EXPAND,1)
3377       
3378        self.doclbl = wx.StaticBox(self, wx.ID_ANY, "")
3379        self.doclblsizr = wx.StaticBoxSizer(self.doclbl)
3380        self.docinfo = wx.StaticText(self,  wx.ID_ANY, "")
3381        self.doclblsizr.Add(self.docinfo, 0, wx.ALIGN_LEFT|wx.ALL, 5)
3382        self.sizer.Add(self.doclblsizr, 0, wx.ALIGN_LEFT|wx.EXPAND|wx.ALL, 5)
3383        btnsizer = wx.BoxSizer(wx.HORIZONTAL)
3384        self.saveBtn = wx.Button(self,-1,"Save current settings")
3385        btnsizer.Add(self.saveBtn, 0, wx.ALL, 2) 
3386        self.saveBtn.Bind(wx.EVT_BUTTON, self.OnSave)
3387        self.saveBtn.Enable(False)
3388        self.applyBtn = wx.Button(self,-1,"Use current (no save)")
3389        btnsizer.Add(self.applyBtn, 0, wx.ALL, 2) 
3390        self.applyBtn.Bind(wx.EVT_BUTTON, self.OnApplyChanges)
3391        self.applyBtn.Enable(False)
3392       
3393        btn = wx.Button(self,wx.ID_CANCEL)
3394        btnsizer.Add(btn, 0, wx.ALL, 2) 
3395        self.sizer.Add(btnsizer, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 
3396               
3397        self.SetSizer(self.sizer)
3398        self.sizer.Fit(self)
3399        self.CenterOnParent()
3400
3401    def OnChange(self,event=None):
3402        ''' Check if anything been changed. Turn the save button on/off.
3403        '''
3404        for var in self.vars:
3405            if self.vars[var][0] is None and self.vars[var][1] is not None:
3406                # make blank strings into None, if that is the default
3407                if self.vars[var][1].strip() == '': self.vars[var][1] = None
3408            if self.vars[var][1] != self.vars[var][2]:
3409                #print 'changed',var,self.vars[var][:3]
3410                self.saveBtn.Enable(True)
3411                self.applyBtn.Enable(True)
3412                break
3413        else:
3414            self.saveBtn.Enable(False)
3415            self.applyBtn.Enable(False)
3416        try:
3417            self.resetBtn.Enable(True)
3418        except:
3419            pass
3420       
3421    def OnApplyChanges(self,event=None):
3422        'Set config variables to match the current settings'
3423        GSASIIpath.SetConfigValue(self.vars)
3424        self.EndModal(wx.ID_OK)
3425       
3426    def OnSave(self,event):
3427        '''Write the config variables to config.py and then set them
3428        as the current settings
3429        '''
3430        if not SaveConfigVars(self.vars,parent=self):
3431            self.OnApplyChanges() # force a reload of the config settings
3432            self.EndModal(wx.ID_OK)
3433
3434    def OnBoolSelect(self,event):
3435        'Respond to a change in a True/False variable'
3436        rb = event.GetEventObject()
3437        var = self.choice[0]
3438        self.vars[var][1] = (rb.GetSelection() == 0)
3439        self.OnChange()
3440        wx.CallAfter(self.OnSelection)
3441       
3442    def onSelDir(self,event):
3443        'Select a directory from a menu'
3444        dlg = wx.DirDialog(self, "Choose a directory:",style=wx.DD_DEFAULT_STYLE)
3445        if dlg.ShowModal() == wx.ID_OK:
3446            var = self.choice[0]
3447            self.vars[var][1] = dlg.GetPath()
3448            self.strEd.SetValue(self.vars[var][1])
3449            self.OnChange()
3450        dlg.Destroy()
3451       
3452    def OnSelection(self):
3453        'show a selected variable'
3454        self.varsizer.DeleteWindows()
3455        var = self.choice[0]
3456        showdef = True
3457        if var not in self.vars:
3458            raise Exception,"How did this happen?"
3459        if type(self.vars[var][0]) is int:
3460            ed = ValidatedTxtCtrl(self,self.vars[var],1,typeHint=int,OKcontrol=self.OnChange)
3461            self.varsizer.Add(ed, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
3462        elif type(self.vars[var][0]) is float:
3463            ed = ValidatedTxtCtrl(self,self.vars[var],1,typeHint=float,OKcontrol=self.OnChange)
3464            self.varsizer.Add(ed, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
3465        elif type(self.vars[var][0]) is bool:
3466            showdef = False
3467            lbl = "value for "+var
3468            ch = []
3469            for i,v in enumerate((True,False)):
3470                s = str(v)
3471                if v == self.vars[var][0]:
3472                    defopt = i
3473                    s += ' (default)'
3474                ch += [s]
3475            rb = wx.RadioBox(
3476                    self, wx.ID_ANY, lbl, wx.DefaultPosition, wx.DefaultSize,
3477                    ch, 1, wx.RA_SPECIFY_COLS
3478            )
3479            # set initial value
3480            if self.vars[var][1] is None:
3481                rb.SetSelection(defopt)
3482            elif self.vars[var][1]:
3483                rb.SetSelection(0)
3484            else:
3485                rb.SetSelection(1)
3486            rb.Bind(wx.EVT_RADIOBOX,self.OnBoolSelect)
3487            self.varsizer.Add(rb, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
3488        else:
3489            if var.endswith('_directory') or var.endswith('_location'):
3490                btn = wx.Button(self,wx.ID_ANY,'Select from dialog...')
3491                sz = (400,-1)
3492            else:
3493                btn = None
3494                sz = (250,-1)
3495            self.strEd = ValidatedTxtCtrl(self,self.vars[var],1,typeHint=str,OKcontrol=self.OnChange,
3496                                              size=sz)
3497            if self.vars[var][1] is not None:
3498                self.strEd.SetValue(self.vars[var][1])
3499            self.varsizer.Add(self.strEd, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
3500            if btn:
3501                btn.Bind(wx.EVT_BUTTON,self.onSelDir)
3502                self.varsizer.Add(btn, 0, wx.ALIGN_CENTRE|wx.ALL, 5) 
3503        # button for reset to default value
3504        lbl = "Reset to Default"
3505        if showdef: # spell out default when needed
3506            lbl += ' (='+str(self.vars[var][0])+')'
3507            #label = wx.StaticText(self,  wx.ID_ANY, 'Default value = '+str(self.vars[var][0]))
3508            #self.varsizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALL, 5)
3509        self.resetBtn = wx.Button(self,-1,lbl)
3510        self.resetBtn.Bind(wx.EVT_BUTTON, self.OnClear)
3511        if self.vars[var][1] is not None and self.vars[var][1] != '': # show current value, if one
3512            #label = wx.StaticText(self,  wx.ID_ANY, 'Current value = '+str(self.vars[var][1]))
3513            #self.varsizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALL, 5)
3514            self.resetBtn.Enable(True)
3515        else:
3516            self.resetBtn.Enable(False)
3517        self.varsizer.Add(self.resetBtn, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
3518        # show meaning, if defined
3519        self.doclbl.SetLabel("Description of "+str(var)) 
3520        if self.vars[var][3]:
3521            self.docinfo.SetLabel(self.vars[var][3])
3522        else:
3523            self.docinfo.SetLabel("(not documented)")
3524        self.sizer.Fit(self)
3525        self.CenterOnParent()
3526        wx.CallAfter(self.SendSizeEvent)
3527
3528    def OnClear(self, event):
3529        var = self.choice[0]
3530        self.vars[var][1] = self.vars[var][0]
3531        self.OnChange()
3532        wx.CallAfter(self.OnSelection)
3533       
3534################################################################################
3535class downdate(wx.Dialog):
3536    '''Dialog to allow a user to select a version of GSAS-II to install
3537    '''
3538    def __init__(self,parent=None):
3539        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
3540        wx.Dialog.__init__(self, parent, wx.ID_ANY, 'Select Version', style=style)
3541        pnl = wx.Panel(self)
3542        sizer = wx.BoxSizer(wx.VERTICAL)
3543        insver = GSASIIpath.svnGetRev(local=True)
3544        curver = int(GSASIIpath.svnGetRev(local=False))
3545        label = wx.StaticText(
3546            pnl,  wx.ID_ANY,
3547            'Select a specific GSAS-II version to install'
3548            )
3549        sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
3550        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
3551        sizer1.Add(
3552            wx.StaticText(pnl,  wx.ID_ANY,
3553                          'Currently installed version: '+str(insver)),
3554            0, wx.ALIGN_CENTRE|wx.ALL, 5)
3555        sizer.Add(sizer1)
3556        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
3557        sizer1.Add(
3558            wx.StaticText(pnl,  wx.ID_ANY,
3559                          'Select GSAS-II version to install: '),
3560            0, wx.ALIGN_CENTRE|wx.ALL, 5)
3561        self.spin = wx.SpinCtrl(pnl, wx.ID_ANY,size=(150,-1))
3562        self.spin.SetRange(1, curver)
3563        self.spin.SetValue(curver)
3564        self.Bind(wx.EVT_SPINCTRL, self._onSpin, self.spin)
3565        self.Bind(wx.EVT_KILL_FOCUS, self._onSpin, self.spin)
3566        sizer1.Add(self.spin)
3567        sizer.Add(sizer1)
3568
3569        line = wx.StaticLine(pnl,-1, size=(-1,3), style=wx.LI_HORIZONTAL)
3570        sizer.Add(line, 0, wx.EXPAND|wx.ALIGN_CENTER|wx.ALL, 10)
3571
3572        self.text = wx.StaticText(pnl,  wx.ID_ANY, "")
3573        sizer.Add(self.text, 0, wx.ALIGN_LEFT|wx.EXPAND|wx.ALL, 5)
3574
3575        line = wx.StaticLine(pnl,-1, size=(-1,3), style=wx.LI_HORIZONTAL)
3576        sizer.Add(line, 0, wx.EXPAND|wx.ALIGN_CENTER|wx.ALL, 10)
3577        sizer.Add(
3578            wx.StaticText(
3579                pnl,  wx.ID_ANY,
3580                'If "Install" is pressed, your project will be saved;\n'
3581                'GSAS-II will exit; The specified version will be loaded\n'
3582                'and GSAS-II will restart. Press "Cancel" to abort.'),
3583            0, wx.EXPAND|wx.ALL, 10)
3584        btnsizer = wx.StdDialogButtonSizer()
3585        btn = wx.Button(pnl, wx.ID_OK, "Install")
3586        btn.SetDefault()
3587        btnsizer.AddButton(btn)
3588        btn = wx.Button(pnl, wx.ID_CANCEL)
3589        btnsizer.AddButton(btn)
3590        btnsizer.Realize()
3591        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER|wx.ALL, 5)
3592        pnl.SetSizer(sizer)
3593        sizer.Fit(self)
3594        self.topsizer=sizer
3595        self.CenterOnParent()
3596        self._onSpin(None)
3597
3598    def _onSpin(self,event):
3599        'Called to load info about the selected version in the dialog'
3600        ver = self.spin.GetValue()
3601        d = GSASIIpath.svnGetLog(version=ver)
3602        date = d.get('date','?').split('T')[0]
3603        s = '(Version '+str(ver)+' created '+date
3604        s += ' by '+d.get('author','?')+')'
3605        msg = d.get('msg')
3606        if msg: s += '\n\nComment: '+msg
3607        self.text.SetLabel(s)
3608        self.topsizer.Fit(self)
3609
3610    def getVersion(self):
3611        'Get the version number in the dialog'
3612        return self.spin.GetValue()
3613
3614################################################################################
3615#### Display Help information
3616################################################################################
3617# define some globals
3618htmlPanel = None
3619htmlFrame = None
3620htmlFirstUse = True
3621helpLocDict = {}
3622path2GSAS2 = os.path.dirname(os.path.realpath(__file__)) # save location of this file
3623def ShowHelp(helpType,frame):
3624    '''Called to bring up a web page for documentation.'''
3625    global htmlFirstUse
3626    # look up a definition for help info from dict
3627    helplink = helpLocDict.get(helpType)
3628    if helplink is None:
3629        # no defined link to use, create a default based on key
3630        helplink = 'gsasII.html#'+helpType.replace(' ','_')
3631    helplink = os.path.join(path2GSAS2,'help',helplink)
3632    # determine if a web browser or the internal viewer should be used for help info
3633    if GSASIIpath.GetConfigValue('Help_mode'):
3634        helpMode = GSASIIpath.GetConfigValue('Help_mode')
3635    else:
3636        helpMode = 'browser'
3637    if helpMode == 'internal':
3638        try:
3639            htmlPanel.LoadFile(helplink)
3640            htmlFrame.Raise()
3641        except:
3642            htmlFrame = wx.Frame(frame, -1, size=(610, 510))
3643            htmlFrame.Show(True)
3644            htmlFrame.SetTitle("HTML Window") # N.B. reset later in LoadFile
3645            htmlPanel = MyHtmlPanel(htmlFrame,-1)
3646            htmlPanel.LoadFile(helplink)
3647    else:
3648        pfx = "file://"
3649        if sys.platform.lower().startswith('win'):
3650            pfx = ''
3651        if htmlFirstUse:
3652            webbrowser.open_new(pfx+helplink)
3653            htmlFirstUse = False
3654        else:
3655            webbrowser.open(pfx+helplink, new=0, autoraise=True)
3656
3657def ShowWebPage(URL,frame):
3658    '''Called to show a tutorial web page.
3659    '''
3660    global htmlFirstUse
3661    # determine if a web browser or the internal viewer should be used for help info
3662    if GSASIIpath.GetConfigValue('Help_mode'):
3663        helpMode = GSASIIpath.GetConfigValue('Help_mode')
3664    else:
3665        helpMode = 'browser'
3666    if helpMode == 'internal':
3667        try:
3668            htmlPanel.LoadFile(URL)
3669            htmlFrame.Raise()
3670        except:
3671            htmlFrame = wx.Frame(frame, -1, size=(610, 510))
3672            htmlFrame.Show(True)
3673            htmlFrame.SetTitle("HTML Window") # N.B. reset later in LoadFile
3674            htmlPanel = MyHtmlPanel(htmlFrame,-1)
3675            htmlPanel.LoadFile(URL)
3676    else:
3677        if URL.startswith('http'): 
3678            pfx = ''
3679        elif sys.platform.lower().startswith('win'):
3680            pfx = ''
3681        else:
3682            pfx = "file://"
3683        if htmlFirstUse:
3684            webbrowser.open_new(pfx+URL)
3685            htmlFirstUse = False
3686        else:
3687            webbrowser.open(pfx+URL, new=0, autoraise=True)
3688
3689################################################################################
3690#### Tutorials support
3691################################################################################
3692G2BaseURL = "https://subversion.xray.aps.anl.gov/pyGSAS"
3693# N.B. tutorialCatalog is generated by routine catalog.py, which also generates the appropriate
3694# empty directories (.../MT/* .../trunk/GSASII/* *=[help,Exercises])
3695tutorialCatalog = (
3696    # tutorial dir,      exercise dir,      web page file name,      title for page
3697
3698    ['StartingGSASII', 'StartingGSASII', 'Starting GSAS.htm',
3699        'Starting GSAS-II'],
3700       
3701    ['FitPeaks', 'FitPeaks', 'Fit Peaks.htm',
3702        'Fitting individual peaks & autoindexing'],
3703    ['BkgFit', 'BkgFit', 'FitBkgTut.htm',
3704       'Fitting the Starting Background using Fixed Points'],
3705       
3706    ['CWNeutron', 'CWNeutron', 'Neutron CW Powder Data.htm',
3707        'CW Neutron Powder fit for Yttrium-Iron Garnet'],
3708    ['LabData', 'LabData', 'Laboratory X.htm',
3709        'Fitting laboratory X-ray powder data for fluoroapatite'],
3710    ['CWCombined', 'CWCombined', 'Combined refinement.htm',
3711        'Combined X-ray/CW-neutron refinement of PbSO4'],
3712    ['TOF-CW Joint Refinement', 'TOF-CW Joint Refinement', 'TOF combined XN Rietveld refinement in GSAS.htm',
3713        'Combined X-ray/TOF-neutron Rietveld refinement'],
3714    ['SeqRefine', 'SeqRefine', 'SequentialTutorial.htm',
3715        'Sequential refinement of multiple datasets'],
3716    ['SeqParametric', 'SeqParametric', 'ParametricFitting.htm',
3717        'Parametric Fitting and Pseudo Variables for Sequential Fits'],
3718       
3719    ['CFXraySingleCrystal','CFXraySingleCrystal','CFSingleCrystal.htm',
3720        'Charge Flipping structure solution with Xray single crystal data'],       
3721    ['CFjadarite', 'CFjadarite', 'Charge Flipping in GSAS.htm',
3722        'Charge Flipping structure solution for jadarite'],
3723    ['CFsucrose', 'CFsucrose', 'Charge Flipping - sucrose.htm',
3724        'Charge Flipping structure solution for sucrose'],
3725    ['TOF Charge Flipping', 'TOF Charge Flipping', 'Charge Flipping with TOF single crystal data in GSASII.htm',
3726        'Charge flipping with neutron TOF single crystal data'],
3727    ['MCsimanneal', 'MCsimanneal', 'MCSA in GSAS.htm',
3728        'Monte-Carlo simulated annealing structure'],
3729
3730    ['2DCalibration', '2DCalibration', 'Calibration of an area detector in GSAS.htm',
3731        'Calibration of an area detector'],
3732    ['2DIntegration', '2DIntegration', 'Integration of area detector data in GSAS.htm',
3733        'Integration of area detector data'],
3734    ['TOF Calibration', 'TOF Calibration', 'Calibration of a TOF powder diffractometer.htm',
3735        'Calibration of a Neutron TOF diffractometer'],
3736    ['TOF Single Crystal Refinement', 'TOF Single Crystal Refinement', 'TOF single crystal refinement in GSAS.htm',
3737        'Single crystal refinement from TOF data'],
3738       
3739    ['2DStrain', '2DStrain', 'Strain fitting of 2D data in GSAS-II.htm',
3740        'Strain fitting of 2D data'],
3741    ['2DTexture', '2DTexture', 'Texture analysis of 2D data in GSAS-II.htm',
3742        'Texture analysis of 2D data'],
3743             
3744    ['SAimages', 'SAimages', 'Small Angle Image Processing.htm',
3745        'Image Processing of small angle x-ray data'],
3746    ['SAfit', 'SAfit', 'Fitting Small Angle Scattering Data.htm',
3747        'Fitting small angle x-ray data (alumina powder)'],
3748    ['SAsize', 'SAsize', 'Small Angle Size Distribution.htm',
3749        'Small angle x-ray data size distribution (alumina powder)'],
3750    ['SAseqref', 'SAseqref', 'Sequential Refinement of Small Angle Scattering Data.htm',
3751        'Sequential refinement with small angle scattering data'],
3752   
3753    #['TOF Sequential Single Peak Fit', 'TOF Sequential Single Peak Fit', '', ''],
3754    )
3755if GSASIIpath.GetConfigValue('Tutorial_location'):
3756    tutorialPath = os.path.abspath(GSASIIpath.GetConfigValue('Tutorial_location'))
3757else:
3758    # pick a default directory in a logical place
3759    if sys.platform.lower().startswith('win') and os.path.exists(os.path.abspath(os.path.expanduser('~/My Documents'))):
3760        tutorialPath = os.path.abspath(os.path.expanduser('~/My Documents/G2tutorials'))
3761    else:
3762        tutorialPath = os.path.abspath(os.path.expanduser('~/G2tutorials'))
3763
3764class OpenTutorial(wx.Dialog):
3765    '''Open a tutorial, optionally copying it to the local disk. Always copy
3766    the data files locally.
3767
3768    For now tutorials will always be copied into the source code tree, but it
3769    might be better to have an option to copy them somewhere else, for people
3770    who don't have write access to the GSAS-II source code location.
3771    '''
3772    # TODO: set default input-file open location to the download location
3773    def __init__(self,parent=None):
3774        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
3775        wx.Dialog.__init__(self, parent, wx.ID_ANY, 'Open Tutorial', style=style)
3776        self.frame = parent
3777        pnl = wx.Panel(self)
3778        sizer = wx.BoxSizer(wx.VERTICAL)
3779        sizer1 = wx.BoxSizer(wx.HORIZONTAL)       
3780        label = wx.StaticText(
3781            pnl,  wx.ID_ANY,
3782            'Select the tutorial to be run and the mode of access'
3783            )
3784        msg = '''To save download time for GSAS-II tutorials and their
3785        sample data files are being moved out of the standard
3786        distribution. This dialog allows users to load selected
3787        tutorials to their computer.
3788
3789        Tutorials can be viewed over the internet or downloaded
3790        to this computer. The sample data can be downloaded or not,
3791        (but it is not possible to run the tutorial without the
3792        data). If no web access is available, tutorials that were
3793        previously downloaded can be viewed.
3794
3795        By default, files are downloaded into the location used
3796        for the GSAS-II distribution, but this may not be possible
3797        if the software is installed by a administrator. The
3798        download location can be changed using the "Set data
3799        location" or the "Tutorial_location" configuration option
3800        (see config_example.py).
3801        '''
3802        hlp = HelpButton(pnl,msg)
3803        sizer1.Add((-1,-1),1, wx.EXPAND, 0)
3804        sizer1.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 0)
3805        sizer1.Add((-1,-1),1, wx.EXPAND, 0)
3806        sizer1.Add(hlp,0,wx.ALIGN_RIGHT|wx.ALL)
3807        sizer.Add(sizer1,0,wx.EXPAND|wx.ALL,0)
3808        sizer.Add((10,10))
3809        self.BrowseMode = 1
3810        choices = [
3811            'make local copy of tutorial and data, then open',
3812            'run from web (copy data locally)',
3813            'browse on web (data not loaded)', 
3814            'open from local tutorial copy',
3815        ]
3816        self.mode = wx.RadioBox(pnl,wx.ID_ANY,'access mode:',
3817                                wx.DefaultPosition, wx.DefaultSize,
3818                                choices, 1, wx.RA_SPECIFY_COLS)
3819        self.mode.SetSelection(self.BrowseMode)
3820        self.mode.Bind(wx.EVT_RADIOBOX, self.OnModeSelect)
3821        sizer.Add(self.mode,0,WACV)
3822        sizer.Add((10,10))
3823        label = wx.StaticText(pnl,  wx.ID_ANY,'Click on tutorial to be opened:')
3824        sizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALL, 2)
3825        self.listbox = wx.ListBox(pnl, wx.ID_ANY, size=(450, 100), style=wx.LB_SINGLE)
3826        self.listbox.Bind(wx.EVT_LISTBOX, self.OnTutorialSelected)
3827        sizer.Add(self.listbox,1,WACV|wx.EXPAND|wx.ALL,1)
3828        sizer.Add((10,10))
3829        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
3830        btn = wx.Button(pnl, wx.ID_ANY, "Set download location")
3831        btn.Bind(wx.EVT_BUTTON, self.SelectDownloadLoc)
3832        sizer1.Add(btn,0,WACV)
3833        self.dataLoc = wx.StaticText(pnl, wx.ID_ANY,tutorialPath)
3834        sizer1.Add(self.dataLoc,0,WACV)
3835        sizer.Add(sizer1)
3836        label = wx.StaticText(
3837            pnl,  wx.ID_ANY,
3838            'Tutorials and Exercise files will be downloaded to:'
3839            )
3840        sizer.Add(label, 0, wx.ALIGN_LEFT|wx.ALL, 1)
3841        self.TutorialLabel = wx.StaticText(pnl,wx.ID_ANY,'')
3842        sizer.Add(self.TutorialLabel, 0, wx.ALIGN_LEFT|wx.EXPAND, 5)
3843        self.ExerciseLabel = wx.StaticText(pnl,wx.ID_ANY,'')
3844        sizer.Add(self.ExerciseLabel, 0, wx.ALIGN_LEFT|wx.EXPAND, 5)
3845        self.ShowTutorialPath()
3846        self.OnModeSelect(None)
3847       
3848        btnsizer = wx.StdDialogButtonSizer()
3849        btn = wx.Button(pnl, wx.ID_CANCEL)
3850        btnsizer.AddButton(btn)
3851        btnsizer.Realize()
3852        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER|wx.ALL, 5)
3853        pnl.SetSizer(sizer)
3854        sizer.Fit(self)
3855        self.topsizer=sizer
3856        self.CenterOnParent()
3857    # def OpenOld(self,event):
3858    #     '''Open old tutorials. This is needed only until we get all the tutorials items moved
3859    #     '''
3860    #     self.EndModal(wx.ID_OK)
3861    #     ShowHelp('Tutorials',self.frame)
3862    def OnModeSelect(self,event):
3863        '''Respond when the mode is changed
3864        '''
3865        self.BrowseMode = self.mode.GetSelection()
3866        if self.BrowseMode == 3:
3867            import glob
3868            filelist = glob.glob(os.path.join(tutorialPath,'help','*','*.htm'))
3869            taillist = [os.path.split(f)[1] for f in filelist]
3870            itemlist = [tut[-1] for tut in tutorialCatalog if tut[2] in taillist]
3871        else:
3872            itemlist = [tut[-1] for tut in tutorialCatalog if tut[-1]]
3873        self.listbox.Clear()
3874        self.listbox.AppendItems(itemlist)
3875    def OnTutorialSelected(self,event):
3876        '''Respond when a tutorial is selected. Load tutorials and data locally,
3877        as needed and then display the page
3878        '''
3879        for tutdir,exedir,htmlname,title in tutorialCatalog:
3880            if title == event.GetString(): break
3881        else:
3882            raise Exception("Match to file not found")
3883        if self.BrowseMode == 0 or self.BrowseMode == 1:
3884            try: 
3885                self.ValidateTutorialDir(tutorialPath,G2BaseURL)
3886            except:
3887                G2MessageBox(self.frame,
3888            '''The selected directory is not valid.
3889           
3890            You must use a directory that you have write access
3891            to. You can reuse a directory previously used for
3892            downloads, but the help and Tutorials subdirectories
3893             must be created by this routine.
3894            ''')
3895                return
3896        #self.dataLoc.SetLabel(tutorialPath)
3897        self.EndModal(wx.ID_OK)
3898        wx.BeginBusyCursor()
3899        if self.BrowseMode == 0:
3900            # xfer data & web page locally, then open web page
3901            self.LoadTutorial(tutdir,tutorialPath,G2BaseURL)
3902            self.LoadExercise(exedir,tutorialPath,G2BaseURL)
3903            URL = os.path.join(tutorialPath,'help',tutdir,htmlname)
3904            self.frame.TutorialImportDir = os.path.join(tutorialPath,'Exercises',exedir)
3905            ShowWebPage(URL,self.frame)
3906        elif self.BrowseMode == 1:
3907            # xfer data locally, open web page remotely
3908            self.LoadExercise(exedir,tutorialPath,G2BaseURL)
3909            URL = os.path.join(G2BaseURL,'Tutorials',tutdir,htmlname)
3910            self.frame.TutorialImportDir = os.path.join(tutorialPath,'Exercises',exedir)
3911            ShowWebPage(URL,self.frame)
3912        elif self.BrowseMode == 2:
3913            # open web page remotely, don't worry about data
3914            URL = os.path.join(G2BaseURL,'Tutorials',tutdir,htmlname)
3915            ShowWebPage(URL,self.frame)
3916        elif self.BrowseMode == 3:
3917            # open web page that has already been transferred
3918            URL = os.path.join(tutorialPath,'help',tutdir,htmlname)
3919            self.frame.TutorialImportDir = os.path.join(tutorialPath,'Exercises',exedir)
3920            ShowWebPage(URL,self.frame)
3921        else:
3922            wx.EndBusyCursor()
3923            raise Exception("How did this happen!")
3924        wx.EndBusyCursor()
3925    def ShowTutorialPath(self):
3926        'Show the help and exercise directory names'
3927        self.TutorialLabel.SetLabel('\t'+
3928                                    os.path.join(tutorialPath,"help") +
3929                                    ' (tutorials)')
3930        self.ExerciseLabel.SetLabel('\t'+
3931                                    os.path.join(tutorialPath,"Exercises") +
3932                                    ' (exercises)')
3933    def ValidateTutorialDir(self,fullpath=tutorialPath,baseURL=G2BaseURL):
3934        '''Load help to new directory or make sure existing directory looks correctly set up
3935        throws an exception if there is a problem.
3936        '''
3937        wx.BeginBusyCursor()
3938        wx.Yield()
3939        if os.path.exists(fullpath):
3940            if os.path.exists(os.path.join(fullpath,"help")):
3941                if not GSASIIpath.svnGetRev(os.path.join(fullpath,"help")):
3942                    print("Problem with "+fullpath+" dir help exists but is not in SVN")
3943                    wx.EndBusyCursor()
3944                    raise Exception
3945            if os.path.exists(os.path.join(fullpath,"Exercises")):
3946                if not GSASIIpath.svnGetRev(os.path.join(fullpath,"Exercises")):
3947                    print("Problem with "+fullpath+" dir Exercises exists but is not in SVN")
3948                    wx.EndBusyCursor()
3949                    raise Exception
3950            if (os.path.exists(os.path.join(fullpath,"help")) and
3951                    os.path.exists(os.path.join(fullpath,"Exercises"))):
3952                if self.BrowseMode != 3:
3953                    print('Checking for directory updates')
3954                    GSASIIpath.svnUpdateDir(os.path.join(fullpath,"help"))
3955                    GSASIIpath.svnUpdateDir(os.path.join(fullpath,"Exercises"))
3956                wx.EndBusyCursor()
3957                return True # both good
3958            elif (os.path.exists(os.path.join(fullpath,"help")) or
3959                    os.path.exists(os.path.join(fullpath,"Exercises"))):
3960                print("Problem: dir "+fullpath+" exists has either help or Exercises, not both")
3961                wx.EndBusyCursor()
3962                raise Exception
3963        if not GSASIIpath.svnInstallDir(baseURL+"/MT",fullpath):
3964            wx.EndBusyCursor()
3965            print("Problem transferring empty directory from web")
3966            raise Exception
3967        wx.EndBusyCursor()
3968        return True
3969
3970    def LoadTutorial(self,tutorialname,fullpath=tutorialPath,baseURL=G2BaseURL):
3971        'Load a Tutorial to the selected location'
3972        if GSASIIpath.svnSwitchDir("help",tutorialname,baseURL+"/Tutorials",fullpath):
3973            return True
3974        print("Problem transferring Tutorial from web")
3975        raise Exception
3976       
3977    def LoadExercise(self,tutorialname,fullpath=tutorialPath,baseURL=G2BaseURL):
3978        'Load Exercise file(s) for a Tutorial to the selected location'
3979        if GSASIIpath.svnSwitchDir("Exercises",tutorialname,baseURL+"/Exercises",fullpath):
3980            return True
3981        print ("Problem transferring Exercise from web")
3982        raise Exception
3983       
3984    def SelectDownloadLoc(self,event):
3985        '''Select a download location,
3986        Cancel resets to the default
3987        '''
3988        global tutorialPath
3989        dlg = wx.DirDialog(self, "Choose a directory for downloads:",
3990                           defaultPath=tutorialPath)#,style=wx.DD_DEFAULT_STYLE)
3991                           #)
3992        try:
3993            if dlg.ShowModal() != wx.ID_OK:
3994                return
3995            pth = dlg.GetPath()
3996        finally:
3997            dlg.Destroy()
3998
3999        if not os.path.exists(pth):
4000            try:
4001                os.makedirs(pth)
4002            except OSError:
4003                msg = 'The selected directory is not valid.\n\t'
4004                msg += pth
4005                msg += '\n\nAn attempt to create the directory failed'
4006                G2MessageBox(self.frame,msg)
4007                return
4008        try:
4009            self.ValidateTutorialDir(pth,G2BaseURL)
4010            tutorialPath = pth
4011        except:
4012            G2MessageBox(self.frame,
4013            '''Error downloading to the selected directory
4014
4015            Are you connected to the internet? If not, you can
4016            only view previously downloaded tutorials (select
4017            "open from local...")
4018           
4019            You must use a directory that you have write access
4020            to. You can reuse a directory previously used for
4021            downloads, but the help and Tutorials subdirectories
4022            must have been created by this routine.
4023            ''')
4024        self.dataLoc.SetLabel(tutorialPath)
4025        self.ShowTutorialPath()
4026        self.OnModeSelect(None)
4027   
4028if __name__ == '__main__':
4029    app = wx.PySimpleApp()
4030    GSASIIpath.InvokeDebugOpts()
4031    frm = wx.Frame(None) # create a frame
4032    frm.Show(True)
4033    #dlg = OpenTutorial(frm)
4034    #if dlg.ShowModal() == wx.ID_OK:
4035    #    print "OK"
4036    #else:
4037    #    print "Cancel"
4038    #dlg.Destroy()
4039    #import sys
4040    #sys.exit()
4041    #======================================================================
4042    # test ScrolledMultiEditor
4043    #======================================================================
4044    # Data1 = {
4045    #      'Order':1,
4046    #      'omega':'string',
4047    #      'chi':2.0,
4048    #      'phi':'',
4049    #      }
4050    # elemlst = sorted(Data1.keys())
4051    # prelbl = sorted(Data1.keys())
4052    # dictlst = len(elemlst)*[Data1,]
4053    #Data2 = [True,False,False,True]
4054    #Checkdictlst = len(Data2)*[Data2,]
4055    #Checkelemlst = range(len(Checkdictlst))
4056    # print 'before',Data1,'\n',Data2
4057    # dlg = ScrolledMultiEditor(
4058    #     frm,dictlst,elemlst,prelbl,
4059    #     checkdictlst=Checkdictlst,checkelemlst=Checkelemlst,
4060    #     checklabel="Refine?",
4061    #     header="test")
4062    # if dlg.ShowModal() == wx.ID_OK:
4063    #     print "OK"
4064    # else:
4065    #     print "Cancel"
4066    # print 'after',Data1,'\n',Data2
4067    # dlg.Destroy()
4068    Data3 = {
4069         'Order':1.0,
4070         'omega':1.1,
4071         'chi':2.0,
4072         'phi':2.3,
4073         'Order1':1.0,
4074         'omega1':1.1,
4075         'chi1':2.0,
4076         'phi1':2.3,
4077         'Order2':1.0,
4078         'omega2':1.1,
4079         'chi2':2.0,
4080         'phi2':2.3,
4081         }
4082    elemlst = sorted(Data3.keys())
4083    dictlst = len(elemlst)*[Data3,]
4084    prelbl = elemlst[:]
4085    prelbl[0]="this is a much longer label to stretch things out"
4086    Data2 = len(elemlst)*[False,]
4087    Data2[1] = Data2[3] = True
4088    Checkdictlst = len(elemlst)*[Data2,]
4089    Checkelemlst = range(len(Checkdictlst))
4090    #print 'before',Data3,'\n',Data2
4091    #print dictlst,"\n",elemlst
4092    #print Checkdictlst,"\n",Checkelemlst
4093    # dlg = ScrolledMultiEditor(
4094    #     frm,dictlst,elemlst,prelbl,
4095    #     checkdictlst=Checkdictlst,checkelemlst=Checkelemlst,
4096    #     checklabel="Refine?",
4097    #     header="test",CopyButton=True)
4098    # if dlg.ShowModal() == wx.ID_OK:
4099    #     print "OK"
4100    # else:
4101    #     print "Cancel"
4102    #print 'after',Data3,'\n',Data2
4103
4104    # Data2 = list(range(100))
4105    # elemlst += range(2,6)
4106    # postlbl += range(2,6)
4107    # dictlst += len(range(2,6))*[Data2,]
4108
4109    # prelbl = range(len(elemlst))
4110    # postlbl[1] = "a very long label for the 2nd item to force a horiz. scrollbar"
4111    # header="""This is a longer\nmultiline and perhaps silly header"""
4112    # dlg = ScrolledMultiEditor(frm,dictlst,elemlst,prelbl,postlbl,
4113    #                           header=header,CopyButton=True)
4114    # print Data1
4115    # if dlg.ShowModal() == wx.ID_OK:
4116    #     for d,k in zip(dictlst,elemlst):
4117    #         print k,d[k]
4118    # dlg.Destroy()
4119    # if CallScrolledMultiEditor(frm,dictlst,elemlst,prelbl,postlbl,
4120    #                            header=header):
4121    #     for d,k in zip(dictlst,elemlst):
4122    #         print k,d[k]
4123
4124    #======================================================================
4125    # test G2MultiChoiceDialog
4126    #======================================================================
4127    choices = []
4128    for i in range(21):
4129        choices.append("option_"+str(i))
4130    dlg = G2MultiChoiceDialog(frm, 'Sequential refinement',
4131                              'Select dataset to include',
4132                              choices)
4133    sel = range(2,11,2)
4134    dlg.SetSelections(sel)
4135    dlg.SetSelections((1,5))
4136    if dlg.ShowModal() == wx.ID_OK:
4137        for sel in dlg.GetSelections():
4138            print sel,choices[sel]
4139   
4140    #======================================================================
4141    # test wx.MultiChoiceDialog
4142    #======================================================================
4143    # dlg = wx.MultiChoiceDialog(frm, 'Sequential refinement',
4144    #                           'Select dataset to include',
4145    #                           choices)
4146    # sel = range(2,11,2)
4147    # dlg.SetSelections(sel)
4148    # dlg.SetSelections((1,5))
4149    # if dlg.ShowModal() == wx.ID_OK:
4150    #     for sel in dlg.GetSelections():
4151    #         print sel,choices[sel]
4152
4153    # pnl = wx.Panel(frm)
4154    # siz = wx.BoxSizer(wx.VERTICAL)
4155
4156    # td = {'Goni':200.,'a':1.,'calc':1./3.,'string':'s'}
4157    # for key in sorted(td):
4158    #     txt = ValidatedTxtCtrl(pnl,td,key)
4159    #     siz.Add(txt)
4160    # pnl.SetSizer(siz)
4161    # siz.Fit(frm)
4162    # app.MainLoop()
4163    # print td
Note: See TracBrowser for help on using the repository browser.