packages feed

Clipboard 2.1.0 → 2.2.0

raw patch · 2 files changed

+63/−42 lines, 2 files

Files

Clipboard.cabal view
@@ -1,12 +1,12 @@ Name:           Clipboard
-Version:        2.1.0
-Author:         Sævar Berg
-Homepage:       http://ddiaz.asofilak.es/packages/Clipboard
+Version:        2.2.0
+Author:         Sævar Berg, Daniel Díaz
+Homepage:       http://sites.google.com/site/dheltadiaz/packages/clipboard
 License:        BSD3
 License-file:   license
-Maintainer:     Daniel Diaz <danieldiaz@asofilak.es>
+Maintainer:     Daniel Diaz <dhelta.diaz@gmail.com>
 Category:       System
-Synopsis:       Access to the (Windows) clipboard.
+Synopsis:       System clipboard interface.
 Description:    
          /Clipboard/ is a package that allows you to interact with the system clipboard easily.
          .
@@ -14,9 +14,16 @@          .
          Changes from last version:
          .
-         * Now @System.Clipboard@ exports 'modifyClipboardString'.
+         * Added compatibility with the unicode character set with 'getClipboardString'.
+         .
+         * Unicode character set is the new standard for 'setClipboardString'.
+         .
+         * Fixed the function 'modifyClipboardString'.
 Build-type:     Simple
 Cabal-version:  >= 1.6
+Source-repository head
+ type: git
+ location: git://github.com/DheltaDiaz/Clipboard.git
 
 Library
   Exposed-modules: System.Clipboard
System/Clipboard.hs view
@@ -1,22 +1,25 @@--- | Operations over the system clipboard.
+-- | Interface to the system clipboard.
 --
--- Currently, this library only works for Windows.
 -- For more information, see "Graphics.Win32.GDI.Clip"
 -- or documentation for /GetClipboardData/ on MSDN.
 module System.Clipboard
-    (
+    ( -- * Clipboard interface
       setClipboardString
     , getClipboardString 
     , modifyClipboardString
+      -- * Clipboard format
+    , cF_UNICODETEXT
     ) where
 
 import System.Win32.Mem 
     (globalAlloc, globalLock, globalUnlock, copyMemory, gHND)
 import Graphics.Win32.GDI.Clip 
-    (openClipboard, closeClipboard, emptyClipboard, 
-    getClipboardData, setClipboardData, cF_TEXT, isClipboardFormatAvailable)
-import Foreign.C 
-    (withCString, peekCString)
+    ( openClipboard, closeClipboard, emptyClipboard, 
+      getClipboardData, setClipboardData
+    , cF_TEXT, ClipboardFormat 
+    , isClipboardFormatAvailable)
+import Foreign.C
+   (withCAString, peekCAString, withCWString, peekCWString)
 import Foreign.Ptr
     (castPtr, nullPtr)
 import Data.List
@@ -26,42 +29,53 @@ import Data.Maybe
     (isJust)
 
--- | Puts a string on the clipboard.
+-- | Clipboard format of Unicode text.
+cF_UNICODETEXT :: ClipboardFormat
+cF_UNICODETEXT = 13
+
+-- | Puts a string on the clipboard, using the 'cF_UNICODETEXT' clipboard format.
 setClipboardString :: String -> IO ()
-setClipboardString str = 
-    withCString str $ \cstring -> do
-    mem <- globalAlloc gHND strLen
-    bracket (globalLock mem) globalUnlock $ \mem' -> do
-        copyMemory mem' (castPtr cstring) strLen
-        bracket_ (openClipboard nullPtr) closeClipboard $ do
-            emptyClipboard
-            setClipboardData cF_TEXT mem'
-            return ()
-  where
-    strLen = genericLength str + 1
+setClipboardString str =
+   withCWString str $ \cstring -> do
+   mem <- globalAlloc gHND strLen
+   bracket (globalLock mem) globalUnlock $ \mem' -> do
+       copyMemory mem' (castPtr cstring) strLen
+       bracket_ (openClipboard nullPtr) closeClipboard $ do
+           emptyClipboard
+           setClipboardData cF_UNICODETEXT mem'
+           return ()
+ where
+   strLen = 2 * (genericLength str + 1)
 
 -- | Gets the contents of the clipboard as a 'String'.
 -- Returns 'Nothing' if the clipboard content is not
--- of type 'cF_TEXT', i.e. it doesn't contain /textual/ data.
+-- of type 'cF_TEXT' or 'cF_UNICODETEXT', i.e. it
+-- doesn't contain /textual/ data.
 getClipboardString :: IO (Maybe String)
-getClipboardString =  
-    bracket_ (openClipboard nullPtr) closeClipboard $ do
-        isFormatAvailable <- isClipboardFormatAvailable cF_TEXT
-        if isFormatAvailable 
-          then do
-              handle <- getClipboardData cF_TEXT
-              mem <- globalLock handle
-              str <- peekCString (castPtr mem)
-              globalUnlock mem
-              return $ Just str
-          else
-              return Nothing
+getClipboardString =
+   bracket_ (openClipboard nullPtr) closeClipboard $ do
+     isUnicodeAvailable <- isClipboardFormatAvailable cF_UNICODETEXT
+     if isUnicodeAvailable
+       then do handle <- getClipboardData cF_UNICODETEXT
+               mem <- globalLock handle
+               str <- peekCWString (castPtr mem)
+               globalUnlock mem
+               return $ Just str
+       else do isAnsiAvailable <- isClipboardFormatAvailable cF_TEXT
+               if isAnsiAvailable
+                 then do handle <- getClipboardData cF_TEXT
+                         mem <- globalLock handle
+                         str <- peekCAString (castPtr mem)
+                         globalUnlock mem
+                         return $ Just str
+                 else return Nothing
 
 -- | Modifies the clipboard content.
 -- If the clipboard has /textual/ data, this function modifies its content,
 -- and return 'True'. Otherwise, it does nothing and return 'False'.
 modifyClipboardString :: (String -> String) -> IO Bool
-modifyClipboardString f = getClipboardString >>= return . isJust . fmap (setClipboardString . f)
-
-
-
+modifyClipboardString f = do
+ s <- getClipboardString
+ case s of
+   Nothing -> return False
+   Just sc -> setClipboardString (f sc) >> return True