Skip to content

Commit

Permalink
Add CLIP_ENABLE_IMAGE option (on by default) to exclude image functio…
Browse files Browse the repository at this point in the history
…ns (fix #38)

In some cases users want to just copy and paste text, and the image
functions are too much extra code that is not required.
  • Loading branch information
dacap committed Mar 5, 2024
1 parent 94693e2 commit 276c7a9
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 22 deletions.
14 changes: 6 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
fail-fast: false
matrix:
os: [windows-latest, macos-latest, ubuntu-latest]
enable_image: [on, off]
steps:
- uses: actions/checkout@v2
- uses: ilammy/msvc-dev-cmd@v1
Expand All @@ -15,18 +16,15 @@ jobs:
shell: bash
run: |
if [[ "${{ runner.os }}" == "Windows" ]] ; then
cmake . -G "NMake Makefiles"
cmake . -G "NMake Makefiles" \
-DCLIP_ENABLE_IMAGE=${{ matrix.enable_image }}
else
cmake . -G "Unix Makefiles"
cmake . -G "Unix Makefiles" \
-DCLIP_ENABLE_IMAGE=${{ matrix.enable_image }}
fi
- name: Compiling
shell: bash
run: |
if [[ "${{ runner.os }}" == "Windows" ]] ; then
nmake
else
make
fi
run: cmake --build .
- name: Running Tests
shell: bash
run: |
Expand Down
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Clip Library
# Copyright (c) 2015-2021 David Capello
# Copyright (c) 2015-2024 David Capello

cmake_minimum_required(VERSION 3.1.2)

Expand All @@ -14,13 +14,18 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()

option(CLIP_ENABLE_IMAGE "Compile with support to copy/paste images" on)
option(CLIP_EXAMPLES "Compile clip examples" on)
option(CLIP_TESTS "Compile clip tests" on)
if(UNIX AND NOT APPLE)
option(CLIP_X11_WITH_PNG "Compile with libpng to support copy/paste image in png format" on)
endif()

set(CLIP_SOURCES clip.cpp image.cpp)
set(CLIP_SOURCES clip.cpp)

if(CLIP_ENABLE_IMAGE)
list(APPEND CLIP_SOURCES image.cpp)
endif()

if(WIN32)
if(MSVC)
Expand Down Expand Up @@ -65,6 +70,11 @@ endif()

add_library(clip ${CLIP_SOURCES})

if(CLIP_ENABLE_IMAGE)
target_compile_definitions(clip PUBLIC
-DCLIP_ENABLE_IMAGE=1)
endif()

if(WIN32)
option(CLIP_SUPPORT_WINXP "Enable Windows XP support" OFF)
if (CLIP_SUPPORT_WINXP)
Expand All @@ -87,7 +97,10 @@ elseif(APPLE)
elseif(UNIX)
if(HAVE_XCB_XLIB_H)
target_link_libraries(clip xcb pthread)
if(CLIP_X11_WITH_PNG AND HAVE_PNG_H AND PNG_LIBRARY)
if(CLIP_ENABLE_IMAGE AND
CLIP_X11_WITH_PNG AND
HAVE_PNG_H AND
PNG_LIBRARY)
target_link_libraries(clip ${PNG_LIBRARY})
endif()
endif()
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015-2023 David Capello
Copyright (c) 2015-2024 David Capello

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Clip Library
*Copyright (c) 2015-2023 David Capello*
*Copyright (c) 2015-2024 David Capello*

[![build](https://github.com/dacap/clip/workflows/build/badge.svg)](https://github.com/dacap/clip/actions?query=workflow%3Abuild)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
Expand Down
10 changes: 10 additions & 0 deletions clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ size_t lock::get_data_length(format f) const {
return p->get_data_length(f);
}

#if CLIP_ENABLE_IMAGE

bool lock::set_image(const image& img) {
return p->set_image(img);
}
Expand All @@ -68,9 +70,13 @@ bool lock::get_image_spec(image_spec& spec) const {
return p->get_image_spec(spec);
}

#endif // CLIP_ENABLE_IMAGE

format empty_format() { return 0; }
format text_format() { return 1; }
#if CLIP_ENABLE_IMAGE
format image_format() { return 2; }
#endif

bool has(format f) {
lock l;
Expand Down Expand Up @@ -120,6 +126,8 @@ bool get_text(std::string& value) {
}
}

#if CLIP_ENABLE_IMAGE

bool set_image(const image& img) {
lock l;
if (l.locked()) {
Expand Down Expand Up @@ -154,6 +162,8 @@ bool get_image_spec(image_spec& spec) {
return l.get_image_spec(spec);
}

#endif // CLIP_ENABLE_IMAGE

void set_error_handler(error_handler handler) {
g_error_handler = handler;
}
Expand Down
12 changes: 11 additions & 1 deletion clip.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Clip Library
// Copyright (c) 2015-2022 David Capello
// Copyright (c) 2015-2024 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -51,10 +51,12 @@ namespace clip {
bool get_data(format f, char* buf, size_t len) const;
size_t get_data_length(format f) const;

#if CLIP_ENABLE_IMAGE
// For images
bool set_image(const image& image);
bool get_image(image& image) const;
bool get_image_spec(image_spec& spec) const;
#endif // CLIP_ENABLE_IMAGE

private:
class impl;
Expand All @@ -69,8 +71,10 @@ namespace clip {
// When the clipboard has UTF8 text.
format text_format();

#if CLIP_ENABLE_IMAGE
// When the clipboard has an image.
format image_format();
#endif

// Returns true if the clipboard has content of the given type.
bool has(format f);
Expand All @@ -84,7 +88,9 @@ namespace clip {

enum class ErrorCode {
CannotLock,
#if CLIP_ENABLE_IMAGE
ImageNotSupported,
#endif
};

typedef void (*error_handler)(ErrorCode code);
Expand All @@ -105,6 +111,8 @@ namespace clip {
// Image
// ======================================================================

#if CLIP_ENABLE_IMAGE

struct image_spec {
unsigned long width = 0;
unsigned long height = 0;
Expand Down Expand Up @@ -165,6 +173,8 @@ namespace clip {
bool get_image(image& img);
bool get_image_spec(image_spec& spec);

#endif // CLIP_ENABLE_IMAGE

// ======================================================================
// Platform-specific
// ======================================================================
Expand Down
10 changes: 10 additions & 0 deletions clip_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
std::map<std::string, format> g_name_to_format;
std::map<format, std::string> g_format_to_name;

#if CLIP_ENABLE_IMAGE

bool get_image_from_clipboard(image* output_img,
image_spec* output_spec)
{
Expand Down Expand Up @@ -133,6 +135,8 @@ bool get_image_from_clipboard(image* output_img,
return true;
}

#endif // CLIP_ENABLE_IMAGE

}

lock::impl::impl(void*) : m_locked(true) {
Expand All @@ -157,10 +161,12 @@ bool get_image_from_clipboard(image* output_img,
if (f == text_format()) {
result = [pasteboard availableTypeFromArray:[NSArray arrayWithObject:NSPasteboardTypeString]];
}
#if CLIP_ENABLE_IMAGE
else if (f == image_format()) {
result = [pasteboard availableTypeFromArray:
[NSArray arrayWithObjects:NSPasteboardTypeTIFF,NSPasteboardTypePNG,nil]];
}
#endif // CLIP_ENABLE_IMAGE
else {
auto it = g_format_to_name.find(f);
if (it != g_format_to_name.end()) {
Expand Down Expand Up @@ -283,6 +289,8 @@ bool get_image_from_clipboard(image* output_img,
}
}

#if CLIP_ENABLE_IMAGE

bool lock::impl::set_image(const image& image) {
@autoreleasepool {
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
Expand Down Expand Up @@ -346,6 +354,8 @@ bool get_image_from_clipboard(image* output_img,
return get_image_from_clipboard(nullptr, &spec);
}

#endif // CLIP_ENABLE_IMAGE

format register_format(const std::string& name) {
// Check if the format is already registered
auto it = g_name_to_format.find(name);
Expand Down
12 changes: 12 additions & 0 deletions clip_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

#include <windows.h>

#if CLIP_ENABLE_IMAGE
#include "clip_win_wic.h"
#endif // CLIP_ENABLE_IMAGE

#ifndef LCS_WINDOWS_COLOR_SPACE
#define LCS_WINDOWS_COLOR_SPACE 'Win '
Expand Down Expand Up @@ -72,6 +74,8 @@ class Hglobal {
HGLOBAL m_handle;
};

#if CLIP_ENABLE_IMAGE

struct BitmapInfo {
BITMAPV5HEADER* b5 = nullptr;
BITMAPINFO* bi = nullptr;
Expand Down Expand Up @@ -205,6 +209,8 @@ struct BitmapInfo {

};

#endif // CLIP_ENABLE_IMAGE

}

lock::impl::impl(void* hwnd) : m_locked(false) {
Expand Down Expand Up @@ -239,9 +245,11 @@ bool lock::impl::is_convertible(format f) const {
IsClipboardFormatAvailable(CF_UNICODETEXT) ||
IsClipboardFormatAvailable(CF_OEMTEXT));
}
#if CLIP_ENABLE_IMAGE
else if (f == image_format()) {
return (IsClipboardFormatAvailable(CF_DIB) ? true: false);
}
#endif // CLIP_ENABLE_IMAGE
else if (IsClipboardFormatAvailable(f))
return true;
else
Expand Down Expand Up @@ -406,6 +414,8 @@ size_t lock::impl::get_data_length(format f) const {
return len;
}

#if CLIP_ENABLE_IMAGE

bool lock::impl::set_image(const image& image) {
const image_spec& spec = image.spec();

Expand Down Expand Up @@ -632,6 +642,8 @@ bool lock::impl::get_image_spec(image_spec& spec) const {
return true;
}

#endif // CLIP_ENABLE_IMAGE

format register_format(const std::string& name) {
int reqsize = 1+MultiByteToWideChar(CP_UTF8, 0,
name.c_str(), name.size(), NULL, 0);
Expand Down
Loading

0 comments on commit 276c7a9

Please sign in to comment.