+class DemoError:
+ """Wraps and stores information about the current exception"""
+ def __init__(self, exc_info):
+ import copy
+
+ excType, excValue = exc_info[:2]
+ # traceback list entries: (filename, line number, function name, text)
+ self.traceback = traceback.extract_tb(exc_info[2])
+
+ # --Based on traceback.py::format_exception_only()--
+ if type(excType) == types.ClassType:
+ self.exception_type = excType.__name__
+ else:
+ self.exception_type = excType
+
+ # If it's a syntax error, extra information needs
+ # to be added to the traceback
+ if excType is SyntaxError:
+ try:
+ msg, (filename, lineno, self.offset, line) = excValue
+ except:
+ pass
+ else:
+ if not filename:
+ filename = "<string>"
+ line = line.strip()
+ self.traceback.append( (filename, lineno, "", line) )
+ excValue = msg
+ try:
+ self.exception_details = str(excValue)
+ except:
+ self.exception_details = "<unprintable %s object>" & type(excValue).__name__
+
+ del exc_info
+
+ def __str__(self):
+ ret = "Type %s \n \
+ Traceback: %s \n \
+ Details : %s" % ( str(self.exception_type), str(self.traceback), self.exception_details )
+ return ret
+
+#---------------------------------------------------------------------------
+
+class DemoErrorPanel(wx.Panel):
+ """Panel put into the demo tab when the demo fails to run due to errors"""
+
+ def __init__(self, parent, codePanel, demoError, log):
+ wx.Panel.__init__(self, parent, -1)#, style=wx.NO_FULL_REPAINT_ON_RESIZE)
+ self.codePanel = codePanel
+ self.nb = parent
+ self.log = log
+
+ self.box = wx.BoxSizer(wx.VERTICAL)
+
+ # Main Label
+ self.box.Add(wx.StaticText(self, -1, "An error has occurred while trying to run the demo")
+ , 0, wx.ALIGN_CENTER | wx.TOP, 10)
+
+ # Exception Information
+ boxInfo = wx.StaticBox(self, -1, "Exception Info" )
+ boxInfoSizer = wx.StaticBoxSizer(boxInfo, wx.VERTICAL ) # Used to center the grid within the box
+ boxInfoGrid = wx.FlexGridSizer(0, 2, 0, 0)
+ textFlags = wx.ALIGN_RIGHT | wx.LEFT | wx.RIGHT | wx.TOP
+ boxInfoGrid.Add(wx.StaticText(self, -1, "Type: "), 0, textFlags, 5 )
+ boxInfoGrid.Add(wx.StaticText(self, -1, str(demoError.exception_type)) , 0, textFlags, 5 )
+ boxInfoGrid.Add(wx.StaticText(self, -1, "Details: ") , 0, textFlags, 5 )
+ boxInfoGrid.Add(wx.StaticText(self, -1, demoError.exception_details) , 0, textFlags, 5 )
+ boxInfoSizer.Add(boxInfoGrid, 0, wx.ALIGN_CENTRE | wx.ALL, 5 )
+ self.box.Add(boxInfoSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
+
+ # Set up the traceback list
+ # This one automatically resizes last column to take up remaining space
+ from ListCtrl import TestListCtrl
+ self.list = TestListCtrl(self, -1, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
+ self.list.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
+ self.list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
+ self.list.InsertColumn(0, "Filename")
+ self.list.InsertColumn(1, "Line", wx.LIST_FORMAT_RIGHT)
+ self.list.InsertColumn(2, "Function")
+ self.list.InsertColumn(3, "Code")
+ self.InsertTraceback(self.list, demoError.traceback)
+ self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
+ self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE)
+ self.box.Add(wx.StaticText(self, -1, "Traceback:")
+ , 0, wx.ALIGN_CENTER | wx.TOP, 5)
+ self.box.Add(self.list, 1, wx.GROW | wx.ALIGN_CENTER | wx.ALL, 5)
+ self.box.Add(wx.StaticText(self, -1, "Entries from the demo module are shown in blue\n"
+ + "Double-click on them to go to the offending line")
+ , 0, wx.ALIGN_CENTER | wx.BOTTOM, 5)
+
+ self.box.Fit(self)
+ self.SetSizer(self.box)
+
+
+ def InsertTraceback(self, list, traceback):
+ #Add the traceback data
+ for x in range(len(traceback)):
+ data = traceback[x]
+ list.InsertStringItem(x, os.path.basename(data[0])) # Filename
+ list.SetStringItem(x, 1, str(data[1])) # Line
+ list.SetStringItem(x, 2, str(data[2])) # Function
+ list.SetStringItem(x, 3, str(data[3])) # Code
+
+ # Check whether this entry is from the demo module
+ if data[0] == "<original>" or data[0] == "<modified>": # FIXME: make more generalised
+ self.list.SetItemData(x, int(data[1])) # Store line number for easy access
+ # Give it a blue colour
+ item = self.list.GetItem(x)
+ item.SetTextColour(wx.BLUE)
+ self.list.SetItem(item)
+ else:
+ self.list.SetItemData(x, -1) # Editor can't jump into this one's code
+
+
+ def OnItemSelected(self, event):
+ # This occurs before OnDoubleClick and can be used to set the
+ # currentItem. OnDoubleClick doesn't get a wxListEvent....
+ self.currentItem = event.m_itemIndex
+ event.Skip()
+
+
+ def OnDoubleClick(self, event):
+ # If double-clicking on a demo's entry, jump to the line number
+ line = self.list.GetItemData(self.currentItem)
+ if line != -1:
+ self.nb.SetSelection(1) # Switch to the code viewer tab
+ wx.CallAfter(self.codePanel.JumpToLine, line-1, True)
+ event.Skip()
+
+
+#---------------------------------------------------------------------------
+
+class DemoTaskBarIcon(wx.TaskBarIcon):
+ TBMENU_RESTORE = wx.NewId()
+ TBMENU_CLOSE = wx.NewId()
+ TBMENU_CHANGE = wx.NewId()
+ TBMENU_REMOVE = wx.NewId()
+
+ def __init__(self, frame):
+ wx.TaskBarIcon.__init__(self)
+ self.frame = frame
+
+ # Set the image
+ icon = self.MakeIcon(images.getWXPdemoImage())
+ self.SetIcon(icon, "wxPython Demo")
+ self.imgidx = 1
+
+ # bind some events
+ self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
+ self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=self.TBMENU_RESTORE)
+ self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)
+ self.Bind(wx.EVT_MENU, self.OnTaskBarChange, id=self.TBMENU_CHANGE)
+ self.Bind(wx.EVT_MENU, self.OnTaskBarRemove, id=self.TBMENU_REMOVE)
+
+
+ def CreatePopupMenu(self):
+ """
+ This method is called by the base class when it needs to popup
+ the menu for the default EVT_RIGHT_DOWN event. Just create
+ the menu how you want it and return it from this function,
+ the base class takes care of the rest.
+ """
+ menu = wx.Menu()
+ menu.Append(self.TBMENU_RESTORE, "Restore wxPython Demo")
+ menu.Append(self.TBMENU_CLOSE, "Close wxPython Demo")
+ menu.AppendSeparator()
+ menu.Append(self.TBMENU_CHANGE, "Change the TB Icon")
+ menu.Append(self.TBMENU_REMOVE, "Remove the TB Icon")
+ return menu
+
+
+ def MakeIcon(self, img):
+ """
+ The various platforms have different requirements for the
+ icon size...
+ """
+ if "wxMSW" in wx.PlatformInfo:
+ img = img.Scale(16, 16)
+ elif "wxGTK" in wx.PlatformInfo:
+ img = img.Scale(22, 22)
+ # wxMac can be any size upto 128x128, so leave the source img alone....
+ icon = wx.IconFromBitmap(img.ConvertToBitmap() )
+ return icon
+
+
+ def OnTaskBarActivate(self, evt):
+ if self.frame.IsIconized():
+ self.frame.Iconize(False)
+ if not self.frame.IsShown():
+ self.frame.Show(True)
+ self.frame.Raise()
+
+
+ def OnTaskBarClose(self, evt):
+ self.frame.Close()
+
+
+ def OnTaskBarChange(self, evt):
+ names = [ "WXPdemo", "Mondrian", "Pencil", "Carrot" ]
+ name = names[self.imgidx]
+
+ getFunc = getattr(images, "get%sImage" % name)
+ self.imgidx += 1
+ if self.imgidx >= len(names):
+ self.imgidx = 0
+
+ icon = self.MakeIcon(getFunc())
+ self.SetIcon(icon, "This is a new icon: " + name)
+
+
+ def OnTaskBarRemove(self, evt):
+ self.RemoveIcon()
+
+
+#---------------------------------------------------------------------------