"Fossies" - the Fresh Open Source Software Archive

Member "AutoHotkey_L-1.1.33.09/source/clipboard.h" (8 May 2021, 3758 Bytes) of package /windows/misc/AutoHotkey_L-1.1.33.09.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "clipboard.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2 AutoHotkey
    3 
    4 Copyright 2003-2009 Chris Mallett (support@autohotkey.com)
    5 
    6 This program is free software; you can redistribute it and/or
    7 modify it under the terms of the GNU General Public License
    8 as published by the Free Software Foundation; either version 2
    9 of the License, or (at your option) any later version.
   10 
   11 This program is distributed in the hope that it will be useful,
   12 but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 GNU General Public License for more details.
   15 */
   16 
   17 #ifndef clipboard_h
   18 #define clipboard_h
   19 
   20 #include "defines.h"
   21 
   22 
   23 #define CANT_OPEN_CLIPBOARD_READ _T("Can't open clipboard for reading.")
   24 #define CANT_OPEN_CLIPBOARD_WRITE _T("Can't open clipboard for writing.")
   25 
   26 #ifdef UNICODE
   27 // In unicode version, always try CF_UNICODETEXT first, then CF_TEXT.
   28 #define CF_NATIVETEXT   CF_UNICODETEXT
   29 #define CF_OTHERTEXT    CF_TEXT
   30 #else
   31 #define CF_NATIVETEXT   CF_TEXT
   32 #define CF_OTHERTEXT    CF_UNICODETEXT
   33 #endif
   34 
   35 
   36 class Clipboard
   37 {
   38 public:
   39     HGLOBAL mClipMemNow, mClipMemNew;
   40     LPTSTR mClipMemNowLocked, mClipMemNewLocked;
   41     // NOTE: Both mLength and mCapacity are count in characters (NOT in bytes).
   42     size_t mLength;  // Last-known length of the clipboard contents (for internal use only because it's valid only during certain specific times).
   43     UINT mCapacity;  // Capacity of mClipMemNewLocked.
   44     BOOL mIsOpen;  // Whether the clipboard is physically open due to action by this class.  BOOL vs. bool improves some benchmarks slightly due to this item being frequently checked.
   45 
   46     // It seems best to default to many attempts, because a failure
   47     // to open the clipboard may result in the early termination
   48     // of a large script due to the fear that it's generally
   49     // unsafe to continue in such cases.  Update: Increased default
   50     // number of attempts from 20 to 40 because Jason (Payam) reported
   51     // that he was getting an error on rare occasions (but not reproducible).
   52     ResultType Open();
   53     HANDLE GetClipboardDataTimeout(UINT uFormat, BOOL *aNullIsOkay = NULL);
   54 
   55     // Below: Whether the clipboard is ready to be written to.  Note that the clipboard is not
   56     // usually physically open even when this is true, unless the caller specifically opened
   57     // it in that mode also:
   58     bool IsReadyForWrite() {return mClipMemNewLocked != NULL;}
   59 
   60     #define CLIPBOARD_FAILURE UINT_MAX
   61     size_t Get(LPTSTR aBuf = NULL);
   62 
   63     ResultType Set(LPCTSTR aBuf = NULL, UINT_PTR aLength = UINT_MAX);
   64     LPTSTR PrepareForWrite(size_t aAllocSize);
   65     ResultType Commit(UINT aFormat = CF_NATIVETEXT);
   66     ResultType AbortWrite(LPTSTR aErrorMessage = _T(""));
   67     ResultType Close(LPTSTR aErrorMessage = NULL);
   68     LPTSTR Contents()
   69     {
   70         if (mClipMemNewLocked)
   71             // Its set up for being written to, which takes precedence over the fact
   72             // that it may be open for read also, so return the write-buffer:
   73             return mClipMemNewLocked;
   74         if (!IsClipboardFormatAvailable(CF_NATIVETEXT))
   75             // We check for both CF_TEXT and CF_HDROP in case it's possible for
   76             // the clipboard to contain both formats simultaneously.  In this case,
   77             // just do this for now, to remind the caller that in these cases, it should
   78             // call Get(buf), providing the target buf so that we have some memory to
   79             // transcribe the (potentially huge) list of files into:
   80             return IsClipboardFormatAvailable(CF_HDROP) ? _T("<<>>") : _T("");
   81         else
   82             // Some callers may rely upon receiving empty string rather than NULL on failure:
   83             return (Get() == CLIPBOARD_FAILURE) ? _T("") : mClipMemNowLocked;
   84     }
   85 
   86     Clipboard() // Constructor
   87         : mIsOpen(false)  // Assumes our app doesn't already have it open.
   88         , mClipMemNow(NULL), mClipMemNew(NULL)
   89         , mClipMemNowLocked(NULL), mClipMemNewLocked(NULL)
   90         , mLength(0), mCapacity(0)
   91     {}
   92 };
   93 
   94 
   95 #endif