]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/wxPython/demo/Main.py
Fixes to avoid an endless event looping for wxGTK
[wxWidgets.git] / utils / wxPython / demo / Main.py
index a17ff06e0f77c8f28fc721f637de9a9d93ca6742..fea3339ceb8cffc59e0eab2b525628907f8d869c 100644 (file)
 
 import sys, os
 from   wxPython.wx import *
-
+from   wxPython.lib.splashscreen import SplashScreen
 
 #---------------------------------------------------------------------------
 
+_useSplitter       = true
+_useNestedSplitter = true
 
 _treeList = [
+    ('New since last release', ['wxMVCTree', 'wxVTKRenderWindow',
+                                'FileBrowseButton', 'GenericButtons',
+                                'wxMask', 'wxEditor']),
+
     ('Managed Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame']),
 
-    ('Miscellaneous Windows', ['wxGrid', 'wxSashWindow',
+    ('Non-Managed Windows', ['wxGrid', 'wxSashWindow',
                                'wxScrolledWindow', 'wxSplitterWindow',
-                               'wxStatusBar', 'wxToolBar', 'wxNotebook']),
+                               'wxStatusBar', 'wxNotebook',
+                               'wxHtmlWindow']),
 
     ('Common Dialogs', ['wxColourDialog', 'wxDirDialog', 'wxFileDialog',
                         'wxSingleChoiceDialog', 'wxTextEntryDialog',
                         'wxFontDialog', 'wxPageSetupDialog', 'wxPrintDialog',
-                        'wxMessageDialog']),
+                        'wxMessageDialog', 'wxProgressDialog']),
 
     ('Controls', ['wxButton', 'wxCheckBox', 'wxCheckListBox', 'wxChoice',
                   'wxComboBox', 'wxGauge', 'wxListBox', 'wxListCtrl', 'wxTextCtrl',
                   'wxTreeCtrl', 'wxSpinButton', 'wxStaticText', 'wxStaticBitmap',
-                  'wxRadioBox', 'wxSlider']),
+                  'wxRadioBox', 'wxSlider', 'wxToolBar', #'wxToggleButton'
+                  ]),
 
-    ('Window Layout', ['wxLayoutConstraints']),
+    ('Window Layout', ['wxLayoutConstraints', 'Sizers', 'OldSizers']),
 
-    ('Micellaneous', ['wxTimer', 'wxGLCanvas', 'DialogUnits', 'wxImage']),
+    ('Miscellaneous', [ 'DragAndDrop', 'CustomDragAndDrop', 'FontEnumerator',
+                        'wxTimer', 'wxValidator', 'wxGLCanvas', 'DialogUnits',
+                        'wxImage', 'wxMask', 'PrintFramework', 'wxOGL',
+                        'PythonEvents', 'Threads']),
 
     ('wxPython Library', ['Layoutf', 'wxScrolledMessageDialog',
-                          'wxMultipleChoiceDialog', 'wxPlotCanvas']),
+                          'wxMultipleChoiceDialog', 'wxPlotCanvas', 'wxFloatBar',
+                          'PyShell', 'wxCalendar', 'wxMVCTree', 'wxVTKRenderWindow',
+                          'FileBrowseButton', 'GenericButtons', 'wxEditor']),
 
-    ('Cool Contribs', ['pyTree', 'hangman', 'SlashDot']),
+    ('Cool Contribs', ['pyTree', 'hangman', 'SlashDot', 'XMLtreeview']),
 
     ]
 
@@ -50,8 +63,10 @@ _treeList = [
 
 class wxPythonDemo(wxFrame):
     def __init__(self, parent, id, title):
-        wxFrame.__init__(self, parent, -1, title,
-                         wxDefaultPosition, wxSize(700, 550))
+        wxFrame.__init__(self, parent, -1, title, size = (725, 550))
+
+        self.cwd = os.getcwd()
+
         if wxPlatform == '__WXMSW__':
             self.icon = wxIcon('bitmaps/mondrian.ico', wxBITMAP_TYPE_ICO)
             self.SetIcon(self.icon)
@@ -61,8 +76,24 @@ class wxPythonDemo(wxFrame):
 
         self.Centre(wxBOTH)
         self.CreateStatusBar(1, wxST_SIZEGRIP)
-        splitter = wxSplitterWindow(self, -1)
-        splitter2 = wxSplitterWindow(splitter, -1)
+
+        if _useSplitter:
+            splitter = wxSplitterWindow(self, -1)
+            if _useNestedSplitter:
+                splitter2 = wxSplitterWindow(splitter, -1)
+                logParent = nbParent = splitter2
+            else:
+                nbParent = splitter
+                logParent = wxFrame(self, -1, "wxPython Demo: log window",
+                                (0,0), (500, 150))
+                logParent.Show(true)
+        else:
+            nbParent = self
+            logParent = wxFrame(self, -1, "wxPython Demo: log window",
+                                (0,0), (500, 150))
+            logParent.Show(true)
+
+
 
         # Prevent TreeCtrl from displaying all items after destruction
         self.dying = false
@@ -70,79 +101,126 @@ class wxPythonDemo(wxFrame):
         # Make a File menu
         self.mainmenu = wxMenuBar()
         menu = wxMenu()
-        mID = NewId()
-        menu.Append(mID, 'E&xit', 'Get the heck outta here!')
-        EVT_MENU(self, mID, self.OnFileExit)
+        exitID = wxNewId()
+        menu.Append(exitID, 'E&xit\tAlt-X', 'Get the heck outta here!')
+        EVT_MENU(self, exitID, self.OnFileExit)
         self.mainmenu.Append(menu, '&File')
 
+        # Make a Demo menu
+        menu = wxMenu()
+        for item in _treeList:
+            submenu = wxMenu()
+            for childItem in item[1]:
+                mID = wxNewId()
+                submenu.Append(mID, childItem)
+                EVT_MENU(self, mID, self.OnDemoMenu)
+            menu.AppendMenu(wxNewId(), item[0], submenu)
+        self.mainmenu.Append(menu, '&Demo')
+
+
         # Make a Help menu
-        mID = NewId()
+        helpID = wxNewId()
         menu = wxMenu()
-        menu.Append(mID, '&About', 'wxPython RULES!!!')
-        EVT_MENU(self, mID, self.OnHelpAbout)
+        menu.Append(helpID, '&About\tCtrl-H', 'wxPython RULES!!!')
+        EVT_MENU(self, helpID, self.OnHelpAbout)
         self.mainmenu.Append(menu, '&Help')
         self.SetMenuBar(self.mainmenu)
 
-        # Create a TreeCtrl
-        tID = NewId()
-        self.tree = wxTreeCtrl(splitter, tID)
-        root = self.tree.AddRoot("Overview")
-        for item in _treeList:
-            child = self.tree.AppendItem(root, item[0])
-            for childItem in item[1]:
-                self.tree.AppendItem(child, childItem)
-        self.tree.Expand(root)
-        EVT_TREE_ITEM_EXPANDED   (self.tree, tID, self.OnItemExpanded)
-        EVT_TREE_ITEM_COLLAPSED  (self.tree, tID, self.OnItemCollapsed)
-        EVT_TREE_SEL_CHANGED     (self.tree, tID, self.OnSelChanged)
+        # set the menu accellerator table...
+        aTable = wxAcceleratorTable([(wxACCEL_ALT,  ord('X'), exitID),
+                                     (wxACCEL_CTRL, ord('H'), helpID)])
+        self.SetAcceleratorTable(aTable)
+
 
+        # Create a TreeCtrl
+        if _useSplitter:
+            tID = wxNewId()
+            self.treeMap = {}
+            self.tree = wxTreeCtrl(splitter, tID)
+            root = self.tree.AddRoot("Overview")
+            firstChild = None
+            for item in _treeList:
+                child = self.tree.AppendItem(root, item[0])
+                if not firstChild: firstChild = child
+                for childItem in item[1]:
+                    theDemo = self.tree.AppendItem(child, childItem)
+                    self.treeMap[childItem] = theDemo
+
+            self.tree.Expand(root)
+            self.tree.Expand(firstChild)
+            EVT_TREE_ITEM_EXPANDED   (self.tree, tID, self.OnItemExpanded)
+            EVT_TREE_ITEM_COLLAPSED  (self.tree, tID, self.OnItemCollapsed)
+            EVT_TREE_SEL_CHANGED     (self.tree, tID, self.OnSelChanged)
 
         # Create a Notebook
-        self.nb = wxNotebook(splitter2, -1)
+        self.nb = wxNotebook(nbParent, -1)
 
         # Set up a TextCtrl on the Overview Notebook page
-        self.ovr = wxTextCtrl(self.nb, -1, '', wxDefaultPosition, wxDefaultSize,
-                              wxTE_MULTILINE|wxTE_READONLY)
+        self.ovr = wxTextCtrl(self.nb, -1, style = wxTE_MULTILINE|wxTE_READONLY)
         self.nb.AddPage(self.ovr, "Overview")
 
 
         # Set up a TextCtrl on the Demo Code Notebook page
-        self.txt = wxTextCtrl(self.nb, -1, '', wxDefaultPosition, wxDefaultSize,
-                              wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
+        self.txt = wxTextCtrl(self.nb, -1,
+                              style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
+        self.txt.SetFont(wxFont(9, wxMODERN, wxNORMAL, wxNORMAL, false))
         self.nb.AddPage(self.txt, "Demo Code")
 
 
-        # select initial items
-        self.nb.SetSelection(0)
-        self.tree.SelectItem(root)
-
         # Set up a log on the View Log Notebook page
-        self.log = wxTextCtrl(splitter2, -1, '', wxDefaultPosition, wxDefaultSize,
-                              wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
+        self.log = wxTextCtrl(logParent, -1,
+                              style = wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL)
         (w, self.charHeight) = self.log.GetTextExtent('X')
         self.WriteText('wxPython Demo Log:\n')
 
+        self.Show(true)
 
         # add the windows to the splitter and split it.
-        splitter.SplitVertically(self.tree, splitter2)
-        splitter.SetSashPosition(180, true)
-        splitter.SetMinimumPaneSize(20)
+        if _useSplitter:
+            if _useNestedSplitter:
+                splitter2.SplitHorizontally(self.nb, self.log)
+                splitter2.SetSashPosition(360, true)
+                splitter2.SetMinimumPaneSize(20)
+
+                splitter.SplitVertically(self.tree, splitter2)
+            else:
+                splitter.SplitVertically(self.tree, self.nb)
+
+            splitter.SetSashPosition(180, true)
+            splitter.SetMinimumPaneSize(20)
 
-        splitter2.SplitHorizontally(self.nb, self.log)
-        splitter2.SetSashPosition(360, true)
-        splitter2.SetMinimumPaneSize(20)
 
         # make our log window be stdout
-        sys.stdout = self
+        #sys.stdout = self
+
+        # select initial items
+        self.nb.SetSelection(0)
+        if _useSplitter:
+            self.tree.SelectItem(root)
+
+        if len(sys.argv) == 2:
+            try:
+                selectedDemo = self.treeMap[sys.argv[1]]
+            except:
+                selectedDemo = None
+            if selectedDemo and _useSplitter:
+                self.tree.SelectItem(selectedDemo)
+                self.tree.EnsureVisible(selectedDemo)
+
+
+        self.WriteText('window handle: %s\n' % self.GetHandle())
+
 
     #---------------------------------------------
     def WriteText(self, text):
         self.log.WriteText(text)
-        if wxPlatform == '__WXMSW__':
-            w, h = self.log.GetClientSizeTuple()
-            numLines = h/self.charHeight
-            x, y = self.log.PositionToXY(self.log.GetLastPosition())
-            self.log.ShowPosition(self.log.XYToPosition(x, y-numLines+1))
+        w, h = self.log.GetClientSizeTuple()
+        numLines = h/self.charHeight
+        x, y = self.log.PositionToXY(self.log.GetLastPosition())
+        if y > numLines:
+            self.log.ShowPosition(self.log.XYToPosition(x, y-numLines))
+            ##self.log.ShowPosition(self.log.GetLastPosition())
+        self.log.SetInsertionPointEnd()
 
     def write(self, txt):
         self.WriteText(txt)
@@ -162,49 +240,65 @@ class wxPythonDemo(wxFrame):
         if self.dying:
             return
 
+        item = event.GetItem()
+        itemText = self.tree.GetItemText(item)
+        self.RunDemo(itemText)
+
+
+    #---------------------------------------------
+    def RunDemo(self, itemText):
+        os.chdir(self.cwd)
         if self.nb.GetPageCount() == 3:
             if self.nb.GetSelection() == 2:
                 self.nb.SetSelection(0)
             self.nb.DeletePage(2)
 
-        item = event.GetItem()
-        itemText = self.tree.GetItemText(item)
-
         if itemText == 'Overview':
             self.GetDemoFile('Main.py')
             self.SetOverview('Overview', overview)
-            #self.nb.ResizeChildren();
             self.nb.Refresh();
-            #wxYield()
+            self.window = None
 
         else:
             if os.path.exists(itemText + '.py'):
+                wxBeginBusyCursor()
                 self.GetDemoFile(itemText + '.py')
                 module = __import__(itemText, globals())
                 self.SetOverview(itemText, module.overview)
+                wxEndBusyCursor()
 
                 # in case runTest is modal, make sure things look right...
                 self.nb.Refresh();
                 wxYield()
 
-                window = module.runTest(self, self.nb, self)
-                if window:
-                    self.nb.AddPage(window, 'Demo')
+                self.window = module.runTest(self, self.nb, self)
+                if self.window:
+                    self.nb.AddPage(self.window, 'Demo')
+                    #self.nb.ResizeChildren()
                     self.nb.SetSelection(2)
-                    self.nb.ResizeChildren();
+                    #self.nb.ResizeChildren()
+                    #if self.window.GetAutoLayout():
+                    #    self.window.Layout()
 
             else:
                 self.ovr.Clear()
                 self.txt.Clear()
+                self.window = None
+
 
 
     #---------------------------------------------
     # Get the Demo files
     def GetDemoFile(self, filename):
         self.txt.Clear()
-        if not self.txt.LoadFile(filename):
+        #if not self.txt.LoadFile(filename):
+        #    self.txt.WriteText("Cannot open %s file." % filename)
+        try:
+            self.txt.SetValue(open(filename).read())
+        except IOError:
             self.txt.WriteText("Cannot open %s file." % filename)
 
+
         self.txt.SetInsertionPoint(0)
         self.txt.ShowPosition(0)
 
@@ -223,13 +317,15 @@ class wxPythonDemo(wxFrame):
 
 
     def OnHelpAbout(self, event):
-        about = wxMessageDialog(self,
-                                "wxPython is a Python extension module that\n"
-                                "encapsulates the wxWindows GUI classes.\n\n"
-                                "This demo shows off some of the capabilities\n"
-                                "of wxPython.\n\n"
-                                "          Developed by Robin Dunn",
-                               "About wxPython", wxOK)
+        #about = wxMessageDialog(self,
+        #                        "wxPython is a Python extension module that\n"
+        #                        "encapsulates the wxWindows GUI classes.\n\n"
+        #                        "This demo shows off some of the capabilities\n"
+        #                        "of wxPython.\n\n"
+        #                        "          Developed by Robin Dunn",
+        #                       "About wxPython", wxOK)
+        from About import MyAboutBox
+        about = MyAboutBox(self)
         about.ShowModal()
         about.Destroy()
 
@@ -237,14 +333,29 @@ class wxPythonDemo(wxFrame):
     #---------------------------------------------
     def OnCloseWindow(self, event):
         self.dying = true
+        self.window = None
+        self.mainmenu = None
         self.Destroy()
 
     #---------------------------------------------
     def OnIdle(self, event):
         if self.otherWin:
             self.otherWin.Raise()
+            self.window = self.otherWin
             self.otherWin = None
 
+    #---------------------------------------------
+    def OnDemoMenu(self, event):
+        if _useSplitter:
+            try:
+                selectedDemo = self.treeMap[self.mainmenu.GetLabel(event.GetId())]
+            except:
+                selectedDemo = None
+            if selectedDemo:
+                self.tree.SelectItem(selectedDemo)
+                self.tree.EnsureVisible(selectedDemo)
+        else:
+            self.RunDemo(self.mainmenu.GetLabel(event.GetId()))
 
 #---------------------------------------------------------------------------
 #---------------------------------------------------------------------------
@@ -254,7 +365,16 @@ class MyApp(wxApp):
         wxImage_AddHandler(wxJPEGHandler())
         wxImage_AddHandler(wxPNGHandler())
         wxImage_AddHandler(wxGIFHandler())
-        frame = wxPythonDemo(NULL, -1, "wxPython: (A Demonstration)")
+
+        self.splash = SplashScreen(None, bitmapfile='bitmaps/splash.gif',
+                              duration=4000, callback=self.AfterSplash)
+        self.splash.Show(true)
+        wxYield()
+        return true
+
+    def AfterSplash(self):
+        self.splash.Close(true)
+        frame = wxPythonDemo(None, -1, "wxPython: (A Demonstration)")
         frame.Show(true)
         self.SetTopWindow(frame)
         return true