Example #1
0
    def test_scrap_put_text(self):
        scrap.put(pygame.SCRAP_TEXT, as_bytes("Hello world"))
        self.assertEqual(scrap.get(pygame.SCRAP_TEXT), as_bytes("Hello world"))

        scrap.put(pygame.SCRAP_TEXT, as_bytes("Another String"))
        self.assertEqual(scrap.get(pygame.SCRAP_TEXT),
                         as_bytes("Another String"))
Example #2
0
    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"))
Example #3
0
    def test_put__text(self):
        """Ensures put can place text into the clipboard."""
        scrap.put(pygame.SCRAP_TEXT, as_bytes("Hello world"))

        self.assertEqual(scrap.get(pygame.SCRAP_TEXT), as_bytes("Hello world"))

        scrap.put(pygame.SCRAP_TEXT, as_bytes("Another String"))

        self.assertEqual(scrap.get(pygame.SCRAP_TEXT), as_bytes("Another String"))
Example #4
0
    def test_put__bmp_image(self):
        """Ensures put can place a BMP image into the clipboard."""
        sf = pygame.image.load(trunk_relative_path("examples/data/asprite.bmp"))
        expected_string = pygame.image.tostring(sf, "RGBA")
        scrap.put(pygame.SCRAP_BMP, expected_string)

        self.assertEqual(scrap.get(pygame.SCRAP_BMP), expected_string)
Example #5
0
 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)
Example #6
0
 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)
Example #7
0
    def test_init__reinit(self):
        """Ensures reinitializing the scrap module doesn't clear its data."""
        data_type = pygame.SCRAP_TEXT
        expected_data = as_bytes("test_init__reinit")
        scrap.put(data_type, expected_data)

        scrap.init()

        self.assertEqual(scrap.get(data_type), expected_data)
Example #8
0
    def test_put(self):
        """Ensures put can place data into the clipboard
        when using a user defined type identifier.
        """
        DATA_TYPE = "arbitrary buffer"

        scrap.put(DATA_TYPE, as_bytes("buf"))
        r = scrap.get(DATA_TYPE)

        self.assertEqual(r, as_bytes("buf"))
Example #9
0
 def _window_on_key_down(self, key, scancode=None, unicode=None):
     try:
         # XXX Experimental, and work only with pygame.
         modifiers = getWindow().modifiers
         if scancode == 55 and 'ctrl' in modifiers:
             from pygame import SCRAP_TEXT
             from pygame import scrap
             scrap.init()
             text = scrap.get(SCRAP_TEXT)
             if text:
                 self.keyboard.text += text
             return True
     except Exception, e:
         pymt_logger.warning('Unable to use scrap module: %s' % str(e)) 
Example #10
0
    def test_get__not_owned(self):
        """Ensures get works when there is no data of the requested type
        in the clipboard and the clipboard is not owned by the pygame
        application.
        """
        self._skip_if_clipboard_owned()

        # Use a unique data type identifier to ensure there is no preexisting
        # data.
        DATA_TYPE = "test_get__not_owned"

        data = scrap.get(DATA_TYPE)

        self.assertIsNone(data)
Example #11
0
    def test_get__owned_empty_type(self):
        """Ensures get works when there is no data of the requested type
        in the clipboard and the clipboard is owned by the pygame application.
        """
        # Use a unique data type identifier to ensure there is no preexisting
        # data.
        DATA_TYPE = "test_get__owned_empty_type"

        if scrap.lost():
            # Try to acquire the clipboard.
            scrap.put(pygame.SCRAP_TEXT, b"text to clipboard")

            if scrap.lost():
                self.skipTest("requires the pygame application to own the " "clipboard")

        data = scrap.get(DATA_TYPE)

        self.assertIsNone(data)
Example #12
0
 def test_put(self):
     scrap.put("arbitrary buffer", as_bytes("buf"))
     r = scrap.get("arbitrary buffer")
     self.assertEquals(r, as_bytes("buf"))
Example #13
0
scrap.init()
scrap.set_mode(pg.SCRAP_CLIPBOARD)

usage()

while going:
    for e in pg.event.get():
        if e.type == pg.QUIT or (e.type == pg.KEYDOWN
                                 and e.key == pg.K_ESCAPE):
            going = False

        elif e.type == pg.KEYDOWN and e.key == pg.K_g:
            # This means to look for data.
            print("Getting the different clipboard data..")
            for t in scrap.get_types():
                r = scrap.get(t)
                if r and len(r) > 500:
                    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 == pg.KEYDOWN and e.key == pg.K_p:
            # Place some text into the selection.
Example #14
0
 def test_put (self):
     scrap.put ("arbitrary buffer", as_bytes("buf"))
     r = scrap.get ("arbitrary buffer")
     self.assertEquals (r, as_bytes("buf"))
Example #15
0
    def update(self,ev):
        
        line,index = self._line,self._index
        shift = pygame.key.get_pressed()
        shift = shift[pygame.K_RSHIFT]|shift[pygame.K_LSHIFT]
        ctrl = pygame.key.get_pressed()
        ctrl = ctrl[pygame.K_RCTRL]|ctrl[pygame.K_LCTRL]

        def clear():
            p1,p2 = sorted(((self._line,self._index),self._select))
            if p1 != p2:
                selection = [len(i) for i in self._splitted[:p2[0]]]
                self.OUTPUT = self.OUTPUT[:sum(selection[:p1[0]]) + p1[0] + p1[1]] + self.OUTPUT[sum(selection[:p2[0]]) + p2[0] + p2[1]:]
                self._select = self._line,self._index = p1

     

        
        if ev.type == pygame.KEYDOWN:
            if ev.key == pygame.K_RIGHT:
                if self._index < len(self._splitted[self._line]):
                    self._index += 1
                elif self._line < len(self._splitted)-1:
                    self._index = 0
                    self._line += 1
                if not pygame.mouse.get_pressed()[0] and not shift: self._select = self._line,self._index

            elif ev.key == pygame.K_LEFT:
                if self._index > len(self._splitted[self._line]):
                    self._index = len(self._splitted[self._line])
                if self._index:
                    self._index -= 1
                elif self._line:
                    self._line -= 1
                    self._index = len(self._splitted[self._line])
                if not pygame.mouse.get_pressed()[0] and not shift: self._select = self._line,self._index
            
            elif ev.key == pygame.K_UP:
                if self._line: self._line -= 1
                if not pygame.mouse.get_pressed()[0] and not shift: self._select = self._line,self._index
                
            elif ev.key == pygame.K_DOWN:
                if self._line < len(self._splitted)-1: self._line += 1
                if not pygame.mouse.get_pressed()[0] and not shift: self._select = self._line,self._index
                
            elif ev.key == pygame.K_DELETE:
                if self._select == (self._line,self._index):
                    if self._index > len(self._splitted[self._line]):
                        self._index = len(self._splitted[self._line])
                        self._select = self._line + 1,0
                    else:
                        self._select = self._line,self._index + 1
                clear()
                
            elif ev.key == pygame.K_END:
                self._index = len(self._splitted[self._line])
                if not pygame.mouse.get_pressed()[0] and not shift: self._select = self._line,self._index

            elif ev.key == pygame.K_HOME:
                self._index = 0
                if not pygame.mouse.get_pressed()[0] and not shift and not shift: self._select = self._line,self._index

            elif ev.key == pygame.K_BACKSPACE:
                if self._select == (self._line,self._index):
                    if self._index > len(self._splitted[self._line]): self._index = len(self._splitted[self._line])
                    if self._index == 0:
                        if self._line: self._select = self._line - 1,len(self._splitted[self._line - 1])
                    else: self._select = self._line,self._index - 1
                clear()

            elif ev.key == pygame.K_TAB:
                clear()
                sp = self.TAB-self._index%self.TAB
                self._splitted[self._line] = self._splitted[self._line][:self._index] + ' '*sp + self._splitted[self._line][self._index:]
                self._index += sp
                self._select = self._line,self._index

            elif ev.key == pygame.K_RETURN or ev.key == pygame.K_KP_ENTER or ev.unicode == '\n':
                clear()
                if not self.MAXLINES or self.OUTPUT.count('\n') < self.MAXLINES - 1:
                    self._splitted[self._line] = self._splitted[self._line][:self._index] + '\n' + self._splitted[self._line][self._index:]
                    self.OUTPUT = self.OUTPUT
                    self._line += 1
                    self._index = 0
                    self._select = self._line,self._index
            elif ctrl and ev.key == pygame.K_v:
            	types = pygame.scrap.get_types()
    		for t in range(len(types)):
      			if "TEXT" in types[t]:
      	 			index = t
          	for i in range(len(scrap.get(types[index]).split("\n"))):
          		clear()
          		self._splitted[self._line] = self._splitted[self._line][:self._index] + scrap.get(types[index]).split("\n")[i] + "\n" + self._splitted[self._line][self._index:]
   			self.OUTPUT = self.OUTPUT
   			self._line += 1 
   			self._index = 0
            	self._index += len(scrap.get(types[index]).split("\n")[-1])
            	self._select = self._line,self._index
            elif ctrl and ev.key == pygame.K_c:
    		pygame.scrap.put (SCRAP_TEXT, self.SELECTION)
            elif ctrl and ev.key == pygame.K_a:
		self._line = 0
		self._index = 0
		self._select = (len(self._splitted)-1,len(self._splitted[len(self._splitted)-1]))
		
            elif ev.unicode:
                clear()
                self._splitted[self._line] = self._splitted[self._line][:self._index] + ev.unicode + self._splitted[self._line][self._index:]
                self._index += 1
                self._select = self._line,self._index
                
        elif ev.type == pygame.MOUSEBUTTONDOWN and self.collidepoint(ev.pos):
            if ev.button < 3:
                self._line = (ev.pos[1] - self._y) / self._h
                self._index = (ev.pos[0] - self._x) / self._w
                if ((ev.pos[0] - self._x) % self._w) > (self._w / 2): self._index += 1
                if self._line > len(self._splitted)-1:
                    self._line = len(self._splitted)-1
                    self._index = len(self._splitted[self._line])
                if self._index > len(self._splitted[self._line]): self._index = len(self._splitted[self._line])
                if ev.button == 2:
                    self._splitted[self._line] = self._splitted[self._line][:self._index] + self.SELECTION + self._splitted[self._line][self._index:]
                    self.OUTPUT = self.OUTPUT
                    self._index += len(self.SELECTION)
                
                self._select = self._line,self._index
                
            
            elif ev.button == 4:
                y = self._y
                if self._y + self._h*3 > self.top: self._y = self.top
                else: self._y += self._h*3
                self._rcursor.move_ip(0,self._y-y)
                return
                
            elif ev.button == 5:
                y = self._y
                h = len(self._splitted) * self._h
                if h > self.height:
                    if self._y - self._h*3 < self.bottom - h: self._y = self.bottom - h
                    else: self._y -= self._h*3
                    self._rcursor.move_ip(0,self._y-y)
                return
        
        elif ev.type == pygame.MOUSEMOTION and ev.buttons[0] and self.collidepoint(ev.pos):
            self._line = (ev.pos[1] - self._y) / self._h
            self._index = (ev.pos[0] - self._x) / self._w
            if ((ev.pos[0] - self._x) % self._w) > (self._w / 2): self._index += 1
            if self._line > len(self._splitted)-1:
                self._line = len(self._splitted)-1
                self._index = len(self._splitted[self._line])
            if self._index > len(self._splitted[self._line]): self._index = len(self._splitted[self._line])

        if (line,index) != (self._line,self._index):
            self._adjust()
# Initialize the scrap module and use the clipboard mode.
scrap.init ()
scrap.set_mode (SCRAP_CLIPBOARD)

usage ()

while going:
    for e in pygame.event.get ():
        if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
            going = False

        elif e.type == KEYDOWN and e.key == K_g:
            # This means to look for data.
            print ("Getting the different clipboard data..")
            for t in scrap.get_types ():
                r = scrap.get (t)
                if r and len (r) > 500:
                    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 = pygame.image.load(f, "." + namehint)
                        screen.blit(loaded_surf, (0,0))


        elif e.type == KEYDOWN and e.key == K_p: