def test_scrap_put_image(self): if "pygame.image" not in sys.modules: return sf = pygame.image.load(trunk_relative_path("examples/data/asprite.bmp")) string = pygame.image.tostring(sf, "RGBA") scrap.put(pygame.SCRAP_BMP, string) self.assertEquals(scrap.get(pygame.SCRAP_BMP), string)
def test_scrap_put_text (self): scrap.put (pygame.SCRAP_TEXT, as_bytes("Hello world")) self.assertEquals (scrap.get (pygame.SCRAP_TEXT), as_bytes("Hello world")) scrap.put (pygame.SCRAP_TEXT, as_bytes("Another String")) self.assertEquals (scrap.get (pygame.SCRAP_TEXT), as_bytes("Another String"))
def test_issue_208(self): """PATCH: pygame.scrap on X11, fix copying into PRIMARY selection Copying into theX11 PRIMARY selection (mouse copy/paste) would not work due to a confusion between content type and clipboard type. """ from pygame import display, event, freetype from pygame.locals import SCRAP_SELECTION, SCRAP_TEXT from pygame.locals import KEYDOWN, K_y, QUIT success = False freetype.init() font = freetype.Font(None, 24) display.init() display.set_caption("Interactive X11 Paste Test") screen = display.set_mode((600, 200)) screen.fill(pygame.Color("white")) text = "Scrap put() succeeded." msg = ( "Some text has been placed into the X11 clipboard." " Please click the center mouse button in an open" " text window to retrieve it." '\n\nDid you get "{}"? (y/n)' ).format(text) word_wrap(screen, msg, font, 6) display.flip() event.pump() scrap.init() scrap.set_mode(SCRAP_SELECTION) scrap.put(SCRAP_TEXT, text.encode("UTF-8")) while True: e = event.wait() if e.type == QUIT: break if e.type == KEYDOWN: success = e.key == K_y break pygame.display.quit() self.assertTrue(success)
def test_put (self): scrap.put ("arbitrary buffer", as_bytes("buf")) r = scrap.get ("arbitrary buffer") self.assertEquals (r, as_bytes("buf"))
elif r is None: print ("Type %s : None" % (t,)) else: print ("Type %s : '%s'" % (t, r.decode('ascii', 'ignore'))) if "image" in t: namehint = t.split("/")[1] if namehint in ['bmp', 'png', 'jpg']: f = BytesIO(r) loaded_surf = pygame.image.load(f, "." + namehint) screen.blit(loaded_surf, (0,0)) elif e.type == KEYDOWN and e.key == K_p: # Place some text into the selection. print ("Placing clipboard text.") scrap.put (SCRAP_TEXT, as_bytes("Hello. This is a message from scrap.")) elif e.type == KEYDOWN and e.key == K_a: # Get all available types. print ("Getting the available types from the clipboard.") types = scrap.get_types () print (types) if len (types) > 0: print ("Contains %s: %s" % (types[0], scrap.contains (types[0]))) print ("Contains _INVALID_: ", scrap.contains ("_INVALID_")) elif e.type == KEYDOWN and e.key == K_i: print ("Putting image into the clipboard.") scrap.set_mode (SCRAP_CLIPBOARD) fp = open (os.path.join(main_dir, 'data', 'liquid.bmp'), 'rb')
if r and len (r) > 500: print ("Type %s : (large buffer)" % t) else: print ("Type %s : %s" % (t, r)) if "image" in t: namehint = t.split("/")[1] if namehint in ['bmp', 'png', 'jpg']: f = StringIO.StringIO(r) loaded_surf = pygame.image.load(f, "." + namehint) screen.blit(loaded_surf, (0,0)) elif e.type == KEYDOWN and e.key == K_p: # Place some text into the selection. print ("Placing clipboard text.") scrap.put (SCRAP_TEXT, "Hello. This is a message from scrap.") elif e.type == KEYDOWN and e.key == K_a: # Get all available types. print ("Getting the available types from the clipboard.") types = scrap.get_types () print (types) if len (types) > 0: print ("Contains %s: %s" % (types[0], scrap.contains (types[0]))) print ("Contains _INVALID_: ", scrap.contains ("_INVALID_")) elif e.type == KEYDOWN and e.key == K_i: print ("Putting image into the clipboard.") scrap.set_mode (SCRAP_CLIPBOARD) fp = open (os.path.join(main_dir, 'data', 'liquid.bmp'), 'rb') buf = fp.read ()
print("Type %s : (large %i byte buffer)" % (t, len(r))) elif r is None: print("Type %s : None" % (t, )) else: print("Type %s : '%s'" % (t, r.decode("ascii", "ignore"))) if "image" in t: namehint = t.split("/")[1] if namehint in ["bmp", "png", "jpg"]: f = BytesIO(r) loaded_surf = pg.image.load(f, "." + namehint) screen.blit(loaded_surf, (0, 0)) elif e.type == KEYDOWN and e.key == pg.K_p: # Place some text into the selection. print("Placing clipboard text.") scrap.put(SCRAP_TEXT, as_bytes("Hello. This is a message from scrap.")) elif e.type == KEYDOWN and e.key == pg.K_a: # Get all available types. print("Getting the available types from the clipboard.") types = scrap.get_types() print(types) if len(types) > 0: print("Contains %s: %s" % (types[0], scrap.contains(types[0]))) print("Contains _INVALID_: ", scrap.contains("_INVALID_")) elif e.type == KEYDOWN and e.key == pg.K_i: print("Putting image into the clipboard.") scrap.set_mode(SCRAP_CLIPBOARD) fp = open(os.path.join(main_dir, "data", "liquid.bmp"), "rb") buf = fp.read()
def test_put(self): scrap.put("arbitrary buffer", as_bytes("buf")) r = scrap.get("arbitrary buffer") self.assertEquals(r, as_bytes("buf"))
print(f"Type {t} : (large {len(r)} byte buffer)") elif r is None: print(f"Type {t} : None") else: print(f"Type {t} : '{r.decode('ascii', 'ignore')}'") if "image" in t: namehint = t.split("/")[1] if namehint in ["bmp", "png", "jpg"]: f = BytesIO(r) loaded_surf = pg.image.load(f, "." + namehint) screen.blit(loaded_surf, (0, 0)) elif e.type == pg.KEYDOWN and e.key == pg.K_p: # Place some text into the selection. print("Placing clipboard text.") scrap.put(pg.SCRAP_TEXT, b"Hello. This is a message from scrap.") elif e.type == pg.KEYDOWN and e.key == pg.K_a: # Get all available types. print("Getting the available types from the clipboard.") types = scrap.get_types() print(types) if len(types) > 0: print(f"Contains {types[0]}: {scrap.contains(types[0])}") print("Contains _INVALID_: ", scrap.contains("_INVALID_")) elif e.type == pg.KEYDOWN and e.key == pg.K_i: print("Putting image into the clipboard.") scrap.set_mode(pg.SCRAP_CLIPBOARD) fp = open(os.path.join(main_dir, "data", "liquid.bmp"), "rb") buf = fp.read()
def test_scrap_put_image(self): sf = pygame.image.load( trunk_relative_path("examples/data/asprite.bmp")) string = pygame.image.tostring(sf, "RGBA") scrap.put(pygame.SCRAP_BMP, string) self.assertEquals(scrap.get(pygame.SCRAP_BMP), string)
def test_scrap_put_text(self): scrap.put(pygame.SCRAP_TEXT, "Hello world") self.assertEquals(scrap.get(pygame.SCRAP_TEXT), "Hello world") scrap.put(pygame.SCRAP_TEXT, "Another String") self.assertEquals(scrap.get(pygame.SCRAP_TEXT), "Another String")