Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native mac copy/paste support to imgui.cpp #2546

Merged

Conversation

andrewwillmott
Copy link
Contributor

This small addition adds a Mac version of the Win32 "built in" copy/paste support, in roughly the same number of lines of code. It is written using the old CF API to avoid the need for a second .mm file. The added dependencies are from AppKit.framework, which is the base framework necessary for any Mac app. (It is brought in by the umbrella framework "Cocoa" used by the imgui Mac examples, for instance.)

I've removed the additional manual Set/GetClipboardText hooks from imgui_impl_osx.mm now they're no longer necessary, and also some stray file references from the example_apple_opengl2 xcode project.

@ocornut
Copy link
Owner

ocornut commented May 10, 2019

Thank you Andrew.

XCode is generally very stringent when it comes to outlawing or warning on old code/api, do you know how well the old CF/Carbon functions would fare there?

@andrewwillmott
Copy link
Contributor Author

These calls are not (yet!) marked as deprecated in the headers. Their only markup is

AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER

So currently (Xcode 10/SDK 10.12) there are no warnings or errors. There is no guarantee this won't change in future of course, Apple being Apple, but it has been the same way since OS X 10.3.

@ocornut ocornut merged commit 02de498 into ocornut:master May 18, 2019
@ocornut
Copy link
Owner

ocornut commented May 18, 2019

Merged with minor tweaks, thank you Andrew!

ocornut added a commit that referenced this pull request May 18, 2019
…as with other implementation. Minor style tweaks. (#2546)

Fixed IMGUI_DISABLE_WIN32_FUNCTIONS not disabling IME code.
@jpincho
Copy link

jpincho commented Jun 26, 2019

Hi there. Running on OSX Mojave, using cmake and clang for a c++ project.
I tried updating imgui from 1.70 to 1.71, and got a linker error while building, while 1.70 works perfectly fine.
I’m currently using the core files for imgui, plus the opengl3 and sdl2 example files.
This is what I get:
Undefined symbols for architecture x86_64: “_CFArrayGetCount”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “_CFDataCreate”, referenced from: SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) “_CFDataGetBytes”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “_CFDataGetLength”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “_CFRelease”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) “_PasteboardClear”, referenced from: SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) “_PasteboardCopyItemFlavorData”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “_PasteboardCopyItemFlavors”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “_PasteboardCreate”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) “_PasteboardGetItemCount”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “_PasteboardGetItemIdentifier”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “_PasteboardPutItemFlavor”, referenced from: SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) “_PasteboardSynchronize”, referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) “___CFConstantStringClassReference”, referenced from: CFString in libimgui.a(imgui.cpp.o) CFString in libimgui.a(imgui.cpp.o) “_kCFAllocatorDefault”, referenced from: SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) ld: symbol(s) not found for architecture x86_64
Any idea how this came to be, and why it does not fail in imgui 1.70? How can I solve this?
Thank you

@andrewwillmott
Copy link
Contributor Author

Hi there. Could you try adding "-framework CoreFoundation" to the link line to resolve it?

I'm surprised that you're getting this, as to run any OSX GUI app, you have to be linking against at least that framework. E.g., the example_sdl_opengl3 sample compiles for me on Mojave. Perhaps it's a link line ordering issue.

@andrewwillmott andrewwillmott deleted the features/mac-native-copy-paste branch June 26, 2019 09:36
@andrewwillmott
Copy link
Contributor Author

I tried removing all frameworks other than OpenGL from the Makefile in that example, and it still links, because the OpenGL framework has a dependency on CoreFoundation (visible via otool). So given you must be linking with OpenGL, the question is why that's not being applied at your link point. Feel free to dump the linker command line that cmake is running in here and I can have a look.

@jpincho
Copy link

jpincho commented Jun 26, 2019

ok, I tried that. another linker error

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -Wall -Werror -Werror=implicit-function-declaration -Wno-old-style-cast -pedantic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/IllusionTest_Lighting.dir/Tests/LightingMain.cpp.o CMakeFiles/IllusionTest_Lighting.dir/Tests/Lighting.cpp.o CMakeFiles/IllusionTest_Lighting.dir/Tests/CameraInput.cpp.o -o output/bin/IllusionTest_Lighting -Wl,-rpath,/Users/jpincho/Programming/build-Illusionv2-Desktop-Debug/output/lib output/lib/libIllusion.a /usr/local/lib/libassimp.dylib -ldl /usr/local/lib/libfreeimage.dylib output/lib/libLogger.a output/lib/libFilesystem.a output/lib/libimgui.a output/lib/libOpenGLBackend.a /usr/local/lib/libSDL2.dylib output/lib/libcpplib.a output/lib/libglad.dylib -framework CoreFoundation Undefined symbols for architecture x86_64: "_PasteboardClear", referenced from: SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) "_PasteboardCopyItemFlavorData", referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) "_PasteboardCopyItemFlavors", referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) "_PasteboardCreate", referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) "_PasteboardGetItemCount", referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) "_PasteboardGetItemIdentifier", referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) "_PasteboardPutItemFlavor", referenced from: SetClipboardTextFn_DefaultImpl(void*, char const*) in libimgui.a(imgui.cpp.o) "_PasteboardSynchronize", referenced from: GetClipboardTextFn_DefaultImpl(void*) in libimgui.a(imgui.cpp.o) ld: symbol(s) not found for architecture x86_64

@thedmd
Copy link
Contributor

thedmd commented Jun 26, 2019

Missing symbols are defined in ApplicationServices framework and it is not listed in build command.

@andrewwillmott
Copy link
Contributor Author

Okay, so that fixed _CFArrayGetCount and friends, and the rest as thedmd says are in ApplicationServices. That's the minimal set, but usually people just use -framework Cocoa.

Another alternative if you don't use the built-in copy and paste is to compile with IMGUI_DISABLE_OSX_FUNCTIONS.

@jpincho
Copy link

jpincho commented Jun 27, 2019

but usually people just use -framework Cocoa.

You're assuming people are building something for OSX, not platform agnostic.
I wouldn't assume that for something like imgui, that has no ties with any particular toolkit or operating system.

what's the difference, in terms of functionality, between v1.70 and v1.71?

@ocornut
Copy link
Owner

ocornut commented Jul 22, 2019

I think we need to be reverting this, I've seen many team stumbling on the same problem.
Will work on undoing this..

ocornut added a commit that referenced this pull request Jul 22, 2019
…in core library (added in 1.71), because it needs application to be linked with '-framework ApplicationServices'. It can be explicitly enabled back by using '#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS' in imconfig.h. Re-added equivalent using NSPasteboard api in the imgui_impl_osx.mm experimental back-end. (#2546)
@ocornut
Copy link
Owner

ocornut commented Jul 22, 2019

This is now disabled by default by 29d9394
But can be enabled back using IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS in imconfig.h

ocornut added a commit that referenced this pull request Jul 22, 2019
… support for framework using/embedding any version of imgui. Amend 29d9394. (#2546)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants