Clipboard 1.0.0 → 2.0.0
raw patch · 3 files changed
+86/−42 lines, 3 filesdep ~Win32
Dependency ranges changed: Win32
Files
- Clipboard.cabal +20/−9
- System/Clipboard.hs +66/−0
- System/Windows/Clipboard.hs +0/−33
Clipboard.cabal view
@@ -1,19 +1,30 @@ Name: Clipboard -Version: 1.0.0 -Author: Daniel Diaz +Version: 2.0.0 +Author: Sævar Berg Homepage: http://ddiaz.asofilak.es/packages/Clipboard License: BSD3 License-file: license Maintainer: Daniel Diaz <danieldiaz@asofilak.es> Category: System -Synopsis: Access to the Windows Clipboard. +Synopsis: Access to the (Windows) clipboard. Description: - /Clipboard/ is a package that allows you to interact with the Windows Clipboard easily. + /Clipboard/ is a package that allows you to interact with the system clipboard easily. . - Based on the Win32 package. + Currently, only in a Windows system. + . + Changes from last version: + . + * New definitions of 'setClipboardString' and 'getClipboardString', + according to the API specifications on MSDN. So, it's highly recommended update + to this version. + . + * Added 'modifyClipboardString'. + . + * Shorter name of the module @System.Windows.Clipboard@: @System.Clipboard@. Build-type: Simple -Build-depends: base == 4.* - , Win32 Cabal-version: >= 1.6 -Exposed-modules: - System.Windows.Clipboard+ +Library + Exposed-modules: System.Clipboard + Build-depends: base == 4.* + , Win32 == 2.2.*
+ System/Clipboard.hs view
@@ -0,0 +1,66 @@+-- | Operations over 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 + ( + setClipboardString + , getClipboardString + ) 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) +import Foreign.Ptr + (castPtr, nullPtr) +import Data.List + (genericLength) +import Control.Exception + (bracket_, bracket) +import Data.Maybe + (isJust) + +-- | Puts a string on the clipboard. +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 + +-- | 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. +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 + +-- | 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) + + +
− System/Windows/Clipboard.hs
@@ -1,33 +0,0 @@- --- | Operations over the Windows Clipboard. -module System.Windows.Clipboard - ( setClipboardString - , getClipboardString - ) where - -import Foreign.Ptr (castPtr,nullPtr) -import Foreign.C.String (newCString,peekCString) -import System.Win32.Types (HANDLE) -import Graphics.Win32.GDI.Clip - -setClipboardh :: ClipboardFormat -> HANDLE -> IO () -setClipboardh f h = - do openClipboard nullPtr - emptyClipboard - setClipboardData f h - closeClipboard - -getClipboardh :: ClipboardFormat -> IO HANDLE -getClipboardh f = - do openClipboard nullPtr - h <- getClipboardData f - closeClipboard - return h - --- | Set the clipboard content to the given 'String'. -setClipboardString :: String -> IO () -setClipboardString = (>>= setClipboardh cF_TEXT . castPtr) . newCString - --- | Get the clipboard content as a 'String'. -getClipboardString :: IO String -getClipboardString = getClipboardh cF_TEXT >>= peekCString . castPtr