def hasClipboardBitmap(): u"""Returns true if clipboard has bitmap .""" #$NON-NLS-1$ ok = False try: wx.TheClipboard.Open() ok = wx.TheClipboard.IsSupported(wx.DataFormat( wx.DF_BITMAP)) or wx.TheClipboard.IsSupported( wx.DataFormat(wx.DF_DIB)) wx.TheClipboard.Close() except: pass return ok
def test_DataObjectComposite(self): do = wx.DataObjectComposite() df1 = wx.DataFormat("data type 1") df2 = wx.DataFormat("data type 2") d1 = wx.CustomDataObject(df1) d2 = wx.CustomDataObject(df2) do.Add(d1, True) do.Add(d2) self.assertTrue(do.GetPreferredFormat() == df1) d3 = do.GetObject(df2) self.assertTrue(isinstance(d3, wx.CustomDataObject)) self.assertTrue(d3 is d2)
def getBitmapFromClipboard(): u"""Returns clipboard bitmap if available or None otherwise.""" #$NON-NLS-1$ bmp = None try: wx.TheClipboard.Open() ok = wx.TheClipboard.IsSupported(wx.DataFormat( wx.DF_BITMAP)) or wx.TheClipboard.IsSupported( wx.DataFormat(wx.DF_DIB)) if ok: bmpdata = wx.BitmapDataObject() if wx.TheClipboard.GetData(bmpdata): bmp = bmpdata.GetBitmap() wx.TheClipboard.Close() except Exception, e: getLoggerService().exception(e)
def GetClipboard(self): """Get the clipboard data. Returns the controldata in the clipboard, or None if the clipboard is empty or contains the wrong type of data. """ # Check to see if data is present if not wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): return None textdata = wx.PyTextDataObject() if not wx.TheClipboard.IsOpened(): opened = wx.TheClipboard.Open() if not opened: return None success = wx.TheClipboard.GetData(textdata) wx.TheClipboard.Close() if not success: return None cdatastring = textdata.GetText() cdata = None if cdatastring[:16] == "pdfgui_cliboard=": cdatastring = cdatastring[16:] try: cdata = cPickle.loads(str(cdatastring)) except: pass #try: # cdata = cPickle.loads(str(cdatastring)) #except (cPickle.UnpicklingError, UnicodeEncodeError, EOFError): # cdata = None return cdata
def pasteWidgets(self, pos=(0, 0), logicals=False): """Pastes widgets from the clipboard.""" clipFormat = wx.DataFormat(StoryPanel.CLIPBOARD_FORMAT) clipData = wx.CustomDataObject(clipFormat) if wx.TheClipboard.Open(): gotData = wx.TheClipboard.IsSupported( clipFormat) and wx.TheClipboard.GetData(clipData) wx.TheClipboard.Close() if gotData: data = pickle.loads(clipData.GetData()) self.eachWidget(lambda w: w.setSelected(False, False)) if not pos: pos = StoryPanel.INSET if not logicals: pos = self.toLogical(pos) for widget in data: newPassage = PassageWidget(self, self.app, state=widget, pos=pos, title=self.untitledName( widget['passage'].title)) newPassage.findSpace() newPassage.setSelected(True, False) self.widgetDict[newPassage.passage.title] = newPassage self.snapWidget(newPassage, False) self.parent.setDirty(True, action='Paste') self.resize() self.Refresh()
def on_right_click(self, _): selections = self.entry_list.GetSelections() if not selections: return menu = wx.Menu() copy = menu.Append(wx.ID_COPY, "&Copy\tCtrl+C", "Copy entry") paste = menu.Append(wx.ID_PASTE, "&Paste\tCtrl+V", "Paste entry") delete = menu.Append(wx.ID_DELETE, "&Delete\tDelete", "Delete entry(s)") append = menu.Append(self.append_id, "&Append\tCtrl+A", "Append entry after") insert = menu.Append(self.insert_id, "&Insert\tCtrl+I", "Insert entry before") menu.Append(wx.ID_ADD, "Add &New Child\tCtrl+N", "Add child entry") enabled = len(selections) == 1 and selections[ 0] != self.entry_list.GetRootItem() copy.Enable(enabled) success = False if enabled and wx.TheClipboard.Open(): success = wx.TheClipboard.IsSupported(wx.DataFormat("BCMEntry")) wx.TheClipboard.Close() paste.Enable(success) delete.Enable(enabled) append.Enable(enabled) insert.Enable(enabled) self.PopupMenu(menu) menu.Destroy()
def getClipboard(): data = None try: if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): do = wx.TextDataObject() wx.TheClipboard.GetData(do) data = do.GetText() elif wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_BITMAP)): do = wx.BitmapDataObject() wx.TheClipboard.GetData(do) data = do.GetBitmap() wx.TheClipboard.Close() except: data = None return data
def get_data(): "Read from the clipboard content, return a suitable object (string or bitmap)" data = None try: if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): do = wx.TextDataObject() wx.TheClipboard.GetData(do) data = do.GetText() elif wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_BITMAP)): do = wx.BitmapDataObject() wx.TheClipboard.GetData(do) data = do.GetBitmap() wx.TheClipboard.Close() except: data = None return data
def test_set_mimedata(self): data_wrapper = DataWrapper() toolkit_data = data_wrapper.toolkit_data data_wrapper.set_mimedata('text/plain', b'hello world') self.assertEqual(data_wrapper.mimetypes(), {'text/plain'}) wx_format = wx.DataFormat('text/plain') self.assertTrue(toolkit_data.IsSupported(wx_format)) text_data = toolkit_data.GetObject(wx_format) self.assertEqual(text_data.GetData(), b'hello world')
def test_get_mimedata(self): toolkit_data = wx.DataObjectComposite() text_data = wx.CustomDataObject(wx.DataFormat('text/plain')) text_data.SetData(b'hello world') toolkit_data.Add(text_data) data_wrapper = DataWrapper(toolkit_data=toolkit_data) self.assertEqual(data_wrapper.mimetypes(), {'text/plain'}) self.assertEqual(data_wrapper.get_mimedata('text/plain'), b'hello world')
def on_clipboard(self, e): cb = wx.TheClipboard if cb.IsSupported(wx.DataFormat(wx.DF_BITMAP)): data = wx.BitmapDataObject() cb.GetData(data) self.set_image(data.Bitmap) else: wx.MessageBox('No image data found in clipboard.', 'Icon From Clipboard')
def __getSelectedTextWX(self): text = '' SendKeys.SendKeys('^c') if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): data = wx.TextDataObject() wx.TheClipboard.GetData(data) text = data.GetText() wx.TheClipboard.Close() return text
def read_files_from_clipboard_cb(files_callback): """Reads file paths from the clipboard and passes them to the specified callback once the current even chain is done executing.""" if not files_callback: return with _OpenClipboard() as clip_opened: if not clip_opened: return if _wx.TheClipboard.IsSupported(_wx.DataFormat(_wx.DF_FILENAME)): clip_data = _wx.FileDataObject() _wx.TheClipboard.GetData(clip_data) _wx.CallAfter(files_callback, clip_data.GetFilenames())
def canvasImportFromClipboard_(pathName): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)) \ and wx.TheClipboard.Open(): inBuffer = wx.TextDataObject() if wx.TheClipboard.GetData(inBuffer): if self._promptSaveChanges(): rc, error = self.parentCanvas.canvas.importStore.importTextBuffer(io.StringIO(inBuffer.GetText())) wx.TheClipboard.Close() else: rc, error = False, "Clipboard does not contain text data and/or cannot be opened" return (rc, error, self.parentCanvas.canvas.importStore.outMap, None, self.parentCanvas.canvas.importStore.inSize)
def PasteAndRun(self): """Replace selection with clipboard contents, run commands.""" text = '' if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): data = wx.TextDataObject() if wx.TheClipboard.GetData(data): text = data.GetText() wx.TheClipboard.Close() if text: self.Execute(text)
def GetClipboardText(self): if wx.TheClipboard.IsOpened(): return None s = None if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): data = wx.TextDataObject() wx.TheClipboard.GetData(data) s = data.GetText() wx.TheClipboard.Close() return s
def test_ignore_non_custom(self): toolkit_data = wx.DataObjectComposite() html_data = wx.HTMLDataObject() html_data.SetHTML("hello world") toolkit_data.Add(html_data) text_data = wx.CustomDataObject(wx.DataFormat('text/plain')) text_data.SetData(b'hello world') toolkit_data.Add(text_data) data_wrapper = DataWrapper(toolkit_data=toolkit_data) self.assertTrue('text/plain' in data_wrapper.mimetypes()) self.assertEqual(data_wrapper.get_mimedata('text/plain'), b'hello world')
def copyWidgets(self): """Copies selected widgets into the clipboard.""" data = [] for widget in self.widgetDict.itervalues(): if widget.selected: data.append(widget.serialize()) clipData = wx.CustomDataObject( wx.DataFormat(StoryPanel.CLIPBOARD_FORMAT)) clipData.SetData(pickle.dumps(data, 1)) if wx.TheClipboard.Open(): wx.TheClipboard.SetData(clipData) wx.TheClipboard.Close()
def OnTimerClear(self, evt): if not wx.TheClipboard.IsOpened(): wx.TheClipboard.Open() if not wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): self.last_copied_pass = None return cdata = wx.TextDataObject() wx.TheClipboard.GetData(cdata) if cdata.GetText() == self.last_copied_pass: wx.TheClipboard.Clear() wx.TheClipboard.Close() else: self.timer_clear.Start(1000 * 3, wx.TIMER_ONE_SHOT)
def ClipBoardFiles(): ret = [] try: if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): do = wx.TextDataObject() wx.TheClipboard.GetData(do) filenames = do.GetText().splitlines() for f in filenames: try: fp = Path(f) if fp.is_dir(): for fp2 in fp.resolve().glob("*"): if fp2.is_file(): ret.append(str(fp2)) else: ret.append(f) except (OSError, IOError): pass elif wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_FILENAME)): do = wx.FileDataObject() wx.TheClipboard.GetData(do) filenames = do.GetFilenames() for f in filenames: try: fp = Path(f) if fp.is_dir(): for fp2 in fp.resolve().glob("*"): if fp2.is_file(): ret.append(str(fp2)) else: ret.append(f) except (OSError, IOError): pass wx.TheClipboard.Close() except (OSError, IOError): pass return ret
def pasteImage(self): cb = wx.Clipboard().Get() if cb.Open(): if cb.IsSupported(wx.DataFormat(format=wx.DF_BITMAP)): data = wx.BitmapDataObject() cb.GetData(data) bmp = data.GetBitmap() fn = '%s.png' % (uuid.uuid1().hex) if bmp.SaveFile(fn, wx.BITMAP_TYPE_PNG): self.image = fn self.bmp = bmp self.rebuild(unit=self.dumpUnit()) else: raise Exception('Could not save %s' % (fn)) cb.Close()
def keyPressed(self, evt, pss=True): key = evt.GetKeyCode() if key == ord("V") and evt.ControlDown(): # PASTE if not wx.TheClipboard.IsOpened(): wx.TheClipboard.Open() # success = wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_BITMAP)) clipText = wx.TheClipboard.IsSupported( wx.DataFormat(wx.DF_TEXT)) wx.TheClipboard.Close() if clipText: self.entry.AppendText(str(clipText)) if key == wx.WXK_F1: self.parentWin.windowMover() if key == wx.WXK_TAB: self.entry.AppendText(self.completionPanel.GetValue()) self.completionPanel.SetValue("") self.entry.SetInsertionPointEnd() elif key == wx.WXK_RETURN: entry = self.entry.GetValue() self.history[ -1] = entry # Overwrite the last history with what the user actually selected self.history.append("") # Add a new empty line to the history self.entry.SetValue("") self.historyFile.write(entry + "\n") self.historyFile.flush() self.historyPos = -1 # Back to bottom if self.executeHandler: self.executeHandler(entry) elif key == wx.WXK_UP: if self.historyPos == -1 or self.historyPos == len( self.history) - 1: self.history[-1] = self.entry.GetValue( ) # If he was on the current edit line and is moving away, then save the current edit line self.historyPos = self.historyPos - 1 try: self.entry.SetValue( self.history[self.historyPos]) # + "completion test") #tmp = self.entry.GetDefaultStyle() #self.entry.SetDefaultStyle(wx.TextAttr(wx.RED)) #self.entry.AppendText("completion test") #self.entry.SetDefaultStyle(tmp) #result = self.entry.SetStyle(len(self.history[self.historyPos]), len(self.history[self.historyPos] + "completion test"), wx.TextAttr(wx.RED, (84, 84, 84))) #print result except IndexError, e: self.historyPos = -1 self.entry.SetValue(self.history[self.historyPos]) self.entry.SetInsertionPointEnd()
def Paste(self): """Replace selection with clipboard contents.""" if self.CanPaste() and wx.TheClipboard.Open(): ps2 = str(sys.ps2) if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): data = wx.TextDataObject() if wx.TheClipboard.GetData(data): self.ReplaceSelection('') command = data.GetText() command = command.rstrip() command = self.fixLineEndings(command) command = self.lstripPrompt(text=command) command = command.replace(os.linesep + ps2, '\n') command = command.replace(os.linesep, '\n') command = command.replace('\n', os.linesep + ps2) self.write(command) wx.TheClipboard.Close()
def PasteAndRun(self): """Replace selection with clipboard contents, run commands.""" if wx.TheClipboard.Open(): ps1 = str(sys.ps1) ps2 = str(sys.ps2) if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): data = wx.TextDataObject() if wx.TheClipboard.GetData(data): endpos = self.GetTextLength() self.SetCurrentPos(endpos) startpos = self.promptPosEnd self.SetSelection(startpos, endpos) self.ReplaceSelection('') text = data.GetText() text = text.lstrip() text = self.fixLineEndings(text) text = self.lstripPrompt(text) text = text.replace(os.linesep + ps1, '\n') text = text.replace(os.linesep + ps2, '\n') text = text.replace(os.linesep, '\n') lines = text.split('\n') commands = [] command = '' for line in lines: if line.strip() == ps2.strip(): # If we are pasting from something like a # web page that drops the trailing space # from the ps2 prompt of a blank line. line = '' if line.strip() != '' and line.lstrip() == line: # New command. if command: # Add the previous command to the list. commands.append(command) # Start a new command, which may be multiline. command = line else: # Multiline command. Add to the command. command += '\n' command += line commands.append(command) for command in commands: command = command.replace('\n', os.linesep + ps2) self.write(command) self.processLine() wx.TheClipboard.Close()
def SystemGet(cls): """Get text from the system clipboard @return: string """ text = None if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): text = wx.TextDataObject() wx.TheClipboard.GetData(text) wx.TheClipboard.Close() if text is not None: return text.GetText() else: return u''
def canPasteIntoCells(panel): """Check if clipboard contents are formatted for grid insertion. This also checks to see if the cell selection is appropriate for pasting. """ grid = panel.gridAtoms individuals = grid.GetSelectedCells() topleft = grid.GetSelectionBlockTopLeft() if len(individuals) + len(topleft) != 1: return False # Get the text if not wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): return False textdata = wx.TextDataObject() if not wx.TheClipboard.IsOpened(): opened = wx.TheClipboard.Open() if not opened: return False success = wx.TheClipboard.GetData(textdata) wx.TheClipboard.Close() if not success: return False copytext = textdata.GetText() # Remove any trailing newline copytext = copytext.rstrip('\n') # Make sure it is of the appropriate format try: rowlist = copytext.split('\n') # Strip any trailing tabs rowlist = [r.rstrip('\t') for r in rowlist] celllist = [r.split('\t') for r in rowlist] except: return False if len(celllist) == 0: return False ncol = len(celllist[0]) for row in celllist: if len(row) != ncol: return False if ncol == 0: return False global clipcells clipcells = celllist return True
def menu_paste_handler(self, e): self.can_update = False if wx.TheClipboard.Open(): wx.TheClipboard.UsePrimarySelection(True) if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): data = wx.TextDataObject() if wx.TheClipboard.GetData(data): try: out = open(clip_file, 'w') except IOError: logging.error("Unable to open temporary clipboard file: {0}".format(clip_file)) wx.TheClipboard.Close() return # sometimes some unicode muck can get in there, as when pasting from web pages. # TBD, this needs review as pasting some encodings will dump out.write(data.GetText().encode("utf-8")) out.close() self.analyze_loadorder(clip_file) wx.TheClipboard.Close()
def MenuLfCb(self, event): if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)) == True: do = wx.TextDataObject() wx.TheClipboard.GetData(do) wx.TheClipboard.Close() else: wx.MessageBox("Unsupported data in the clipboard!", "Error", wx.OK | wx.ICON_ERROR) wx.TheClipboard.Close() return else: wx.MessageBox("Unable to open clipboard!", "Error", wx.OK | wx.ICON_ERROR) return fn = "<<<from clipboard>>>" self.file.openFile(fn, do.GetText()) self.fileLoad(fn)
def on_right_click(self, _): selected = self.entry_list.GetSelections() if not selected: return menu = wx.Menu() menu.Append(wx.ID_NEW) menu.Append(wx.ID_DELETE) menu.Append(wx.ID_COPY) paste = menu.Append(wx.ID_PASTE) add = menu.Append(wx.ID_ADD, '&Add Copied Entry') success = False # Check Clipboard if wx.TheClipboard.Open(): success = wx.TheClipboard.IsSupported(wx.DataFormat("BDMEntry")) wx.TheClipboard.Close() add.Enable(success) paste.Enable(success) self.PopupMenu(menu) menu.Destroy()
def _paste_from_clipboard(self): """Paste the content of the clipboard to the self._url_list widget. It also adds a new line at the end of the data if not exist. """ if not wx.TheClipboard.IsOpened(): if wx.TheClipboard.Open(): if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)): data = wx.TextDataObject() wx.TheClipboard.GetData(data) data = data.GetText() if data[-1] != '\n': data += '\n' self._url_list.WriteText(data) wx.TheClipboard.Close()