1# ------------------------------------------------------------------------------
2#
3#  Copyright (c) 2005, Enthought, Inc.
4#  All rights reserved.
5#
6#  This software is provided without warranty under the terms of the BSD
7#  license included in LICENSE.txt and may be redistributed only
8#  under the conditions described in the aforementioned license.  The license
9#  is also available online at http://www.enthought.com/licenses/BSD.txt
10#
11#  Thanks for using Enthought open source!
12#
13#  Author: David C. Morrill
14#  Date:   10/21/2004
15#
16# ------------------------------------------------------------------------------
17
18""" Defines various directory editors for the wxPython user interface toolkit.
19"""
20
21
22import wx
23
24from os.path import isdir
25
26# FIXME: ToolkitEditorFactory is a proxy class defined here just for backward
27# compatibility. The class has been moved to the
28# traitsui.editors.custom_editor file.
29from traitsui.editors.directory_editor import ToolkitEditorFactory
30
31from .file_editor import (
32    SimpleEditor as SimpleFileEditor,
33    CustomEditor as CustomFileEditor,
34    PopupFile,
35)
36
37# -------------------------------------------------------------------------
38#  'SimpleEditor' class:
39# -------------------------------------------------------------------------
40
41
42class SimpleEditor(SimpleFileEditor):
43    """ Simple style of editor for directories, which displays a text field
44        and a **Browse** button that opens a directory-selection dialog box.
45    """
46
47    def _create_file_dialog(self):
48        """ Creates the correct type of file dialog.
49        """
50        dlg = wx.DirDialog(self.control, message="Select a Directory")
51        dlg.SetPath(self._file_name.GetValue())
52        return dlg
53
54    def _create_file_popup(self):
55        """ Creates the correct type of file popup.
56        """
57        return PopupDirectory(
58            control=self.control,
59            file_name=self.str_value,
60            filter=self.factory.filter,
61            height=300,
62        )
63
64
65class CustomEditor(CustomFileEditor):
66    """ Custom style of editor for directories, which displays a tree view of
67        the file system.
68    """
69
70    def get_style(self):
71        """ Returns the basic style to use for the control.
72        """
73        return wx.DIRCTRL_DIR_ONLY | wx.DIRCTRL_EDIT_LABELS
74
75    def update_object(self, event):
76        """ Handles the user changing the contents of the edit control.
77        """
78        if self.control is not None:
79            path = self.control.GetPath()
80            if isdir(path):
81                self.value = path
82
83
84class PopupDirectory(PopupFile):
85    def get_style(self):
86        """ Returns the basic style to use for the popup.
87        """
88        return wx.DIRCTRL_DIR_ONLY | wx.DIRCTRL_EDIT_LABELS
89
90    def is_valid(self, path):
91        """ Returns whether or not the path is valid.
92        """
93        return isdir(path)
94