<commit_before>"""
Support class that wraps up the boilerplate toolkit calls that virtually all
demo programs have to use.
"""

from enthought.etsconfig.api import ETSConfig

# FIXME - it should be enough to do the following import, but because of the
# PyQt/traits problem (see below) we can't because it would drag in traits too
# early.  Until it is fixed we just assume wx if we can import it.
# Force the selection of a valid toolkit.
#import enthought.enable.toolkit 
if not ETSConfig.toolkit:
    for toolkit in ('wx', 'qt4'):
        try:
            exec "import " + toolkit
            ETSConfig.toolkit = toolkit
            break
        except ImportError:
            pass
    else:
        raise RuntimeError("Can't load wx or qt4 backend for Chaco.")


if ETSConfig.toolkit == 'wx':
    import wx

    class DemoFrame(wx.Frame):
        def __init__ ( self, *args, **kw ):
            wx.Frame.__init__( *(self,) + args, **kw )
            #self.SetTitle("Enable Demo")
            self.SetAutoLayout( True )
    
            # Create the subclass's window
            self.enable_win = self._create_window()
        
            # Listen for the Activate event so we can restore keyboard focus.
            wx.EVT_ACTIVATE( self, self._on_activate )
        
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.enable_win.control, 1, wx.EXPAND)
            self.SetSizer(sizer)
            self.Show( True )
            return

        def _on_activate(self, event):
            if self.enable_win is not None and self.enable_win.control is not None:
                self.enable_win.control.SetFocus()

        def _create_window(self):
            "Subclasses should override this method and return an enable.Window"
            raise NotImplementedError


    def demo_main(demo_class, size=(400,400), title="Enable Demo"):
        "Takes the class of the demo to run as an argument."
        app = wx.PySimpleApp()
        frame = demo_class(None, size=size, title=title)
        app.SetTopWindow(frame)
        app.MainLoop()

elif ETSConfig.toolkit == 'qt4':
    from PyQt4 import QtGui

    _app = QtGui.QApplication.instance()

    if _app is None:
        import sys
        _app = QtGui.QApplication(sys.argv)

    class DemoFrame(QtGui.QWidget):
        def __init__ (self, parent, **kw):
            QtGui.QWidget.__init__(self)

            # Create the subclass's window
            self.enable_win = self._create_window()

            layout = QtGui.QVBoxLayout()
            layout.setMargin(0)
            layout.addWidget(self.enable_win.control)

            self.setLayout(layout)

            if 'size' in kw:
                self.resize(*kw['size'])

            if 'title' in kw:
                self.setWindowTitle(kw['title'])

            self.show()

        def _create_window(self):
            "Subclasses should override this method and return an enable.Window"
            raise NotImplementedError


    def demo_main(demo_class, size=(400,400), title="Enable Demo"):
        "Takes the class of the demo to run as an argument."
        frame = demo_class(None, size=size, title=title)
        _app.exec_()


elif ETSConfig.toolkit == 'pyglet':
    from enthought.enable.pyglet_backend.pyglet_app import get_app, PygletApp

    class DemoFrame(object):
        def __init__(self):
            app = get_app()
            if app:
                window = self._create_window()
                if window:
                    self.enable_win = window
                    app.add_window(window.control)
                else:
                    self.enable_win = None
            return

        def _create_window(self):
            raise NotImplementedError

    def demo_main(demo_class, size=(640,480), title="Enable Example"):
        """ Runs a simple application in Pyglet using an instance of
        **demo_class** as the main window or frame.

        **demo_class** should be a subclass of DemoFrame or the pyglet
        backend's Window class.
        """
        app = PygletApp()
        if issubclass(demo_class, DemoFrame):
            frame = demo_class()
            if frame.enable_win is not None:
                window = frame.enable_win.control
            else:
                window = None
        else:
            window = demo_class().control

        if window is not None:
            if not window.fullscreen:
                window.set_size(*size)
            window.set_caption(title)
            app.set_main_window(window)
        app.run()
        

# EOF
<commit_msg>fixed up example support to use new enable_toolkit attribute on ETSConfig<commit_after>"""
Support class that wraps up the boilerplate toolkit calls that virtually all
demo programs have to use.
"""

from enthought.etsconfig.api import ETSConfig

# FIXME - it should be enough to do the following import, but because of the
# PyQt/traits problem (see below) we can't because it would drag in traits too
# early.  Until it is fixed we just assume wx if we can import it.
# Force the selection of a valid toolkit.
#import enthought.enable.toolkit 
if not ETSConfig.toolkit:
    for toolkit in ('wx', 'qt4'):
        try:
            exec "import " + toolkit
            ETSConfig.toolkit = toolkit
            break
        except ImportError:
            pass
    else:
        raise RuntimeError("Can't load wx or qt4 backend for Chaco.")


if ETSConfig.enable_toolkit == 'wx':
    import wx

    class DemoFrame(wx.Frame):
        def __init__ ( self, *args, **kw ):
            wx.Frame.__init__( *(self,) + args, **kw )
            #self.SetTitle("Enable Demo")
            self.SetAutoLayout( True )
    
            # Create the subclass's window
            self.enable_win = self._create_window()
        
            # Listen for the Activate event so we can restore keyboard focus.
            wx.EVT_ACTIVATE( self, self._on_activate )
        
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.enable_win.control, 1, wx.EXPAND)
            self.SetSizer(sizer)
            self.Show( True )
            return

        def _on_activate(self, event):
            if self.enable_win is not None and self.enable_win.control is not None:
                self.enable_win.control.SetFocus()

        def _create_window(self):
            "Subclasses should override this method and return an enable.Window"
            raise NotImplementedError


    def demo_main(demo_class, size=(400,400), title="Enable Demo"):
        "Takes the class of the demo to run as an argument."
        app = wx.PySimpleApp()
        frame = demo_class(None, size=size, title=title)
        app.SetTopWindow(frame)
        app.MainLoop()

elif ETSConfig.enable_toolkit == 'qt4':
    from PyQt4 import QtGui

    _app = QtGui.QApplication.instance()

    if _app is None:
        import sys
        _app = QtGui.QApplication(sys.argv)

    class DemoFrame(QtGui.QWidget):
        def __init__ (self, parent, **kw):
            QtGui.QWidget.__init__(self)

            # Create the subclass's window
            self.enable_win = self._create_window()

            layout = QtGui.QVBoxLayout()
            layout.setMargin(0)
            layout.addWidget(self.enable_win.control)

            self.setLayout(layout)

            if 'size' in kw:
                self.resize(*kw['size'])

            if 'title' in kw:
                self.setWindowTitle(kw['title'])

            self.show()

        def _create_window(self):
            "Subclasses should override this method and return an enable.Window"
            raise NotImplementedError


    def demo_main(demo_class, size=(400,400), title="Enable Demo"):
        "Takes the class of the demo to run as an argument."
        frame = demo_class(None, size=size, title=title)
        _app.exec_()


elif ETSConfig.enable_toolkit == 'pyglet':
    from enthought.enable.pyglet_backend.pyglet_app import get_app, PygletApp

    class DemoFrame(object):
        def __init__(self):
            app = get_app()
            if app:
                window = self._create_window()
                if window:
                    self.enable_win = window
                    app.add_window(window.control)
                else:
                    self.enable_win = None
            return

        def _create_window(self):
            raise NotImplementedError

    def demo_main(demo_class, size=(640,480), title="Enable Example"):
        """ Runs a simple application in Pyglet using an instance of
        **demo_class** as the main window or frame.

        **demo_class** should be a subclass of DemoFrame or the pyglet
        backend's Window class.
        """
        app = PygletApp()
        if issubclass(demo_class, DemoFrame):
            frame = demo_class()
            if frame.enable_win is not None:
                window = frame.enable_win.control
            else:
                window = None
        else:
            window = demo_class().control

        if window is not None:
            if not window.fullscreen:
                window.set_size(*size)
            window.set_caption(title)
            app.set_main_window(window)
        app.run()
        

# EOF

{'fraction_non_alphanumeric': 0.0843177630865937, 'fraction_numerical': 0.005202124200715292, 'mean_word_length': 1.9248811410459588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2997019', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2553, 'n_words': 770}