Browse Source

Default "local only" clipboard handler on non-Windows platforms

pull/18/head
ocornut 10 years ago
parent
commit
a830037eab
  1. 2
      imconfig.h
  2. 72
      imgui.cpp
  3. 19
      imgui.h

2
imconfig.h

@ -13,7 +13,7 @@
//#define IM_ASSERT(_EXPR) MyAssert(_EXPR) //#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
//---- Don't implement default clipboard handlers for Windows (so as not to link with OpenClipboard(), etc.) //---- Don't implement default clipboard handlers for Windows (so as not to link with OpenClipboard(), etc.)
//#define IMGUI_DONT_IMPLEMENT_WINDOWS_CLIPBOARD_FUNCTIONS //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS
//---- If you are loading a custom font, ImGui expect to find a pure white pixel at (0,0) //---- If you are loading a custom font, ImGui expect to find a pure white pixel at (0,0)
// Change it's UV coordinate here if you can't have a white pixel at (0,0) // Change it's UV coordinate here if you can't have a white pixel at (0,0)

72
imgui.cpp

@ -146,7 +146,6 @@
- filters: handle wildcards (with implicit leading/trailing *), regexps - filters: handle wildcards (with implicit leading/trailing *), regexps
- shortcuts: add a shortcut api, e.g. parse "&Save" and/or "Save (CTRL+S)", pass in to widgets or provide simple ways to use (button=activate, input=focus) - shortcuts: add a shortcut api, e.g. parse "&Save" and/or "Save (CTRL+S)", pass in to widgets or provide simple ways to use (button=activate, input=focus)
- keyboard: full keyboard navigation and focus - keyboard: full keyboard navigation and focus
- clipboard: add a default "local" implementation of clipboard functions (user will only need to override them to connect to OS clipboard)
- misc: not thread-safe - misc: not thread-safe
- optimisation/render: use indexed rendering - optimisation/render: use indexed rendering
- optimisation/render: move clip-rect to vertex data? would allow merging all commands - optimisation/render: move clip-rect to vertex data? would allow merging all commands
@ -154,8 +153,6 @@
- optimisation: turn some the various stack vectors into statically-sized arrays - optimisation: turn some the various stack vectors into statically-sized arrays
- optimisation: better clipping for multi-component widgets - optimisation: better clipping for multi-component widgets
- optimisation: specialize for height based clipping first (assume widgets never go up + height tests before width tests?) - optimisation: specialize for height based clipping first (assume widgets never go up + height tests before width tests?)
- optimisation/portability: provide ImVector style implementation
- optimisation/portability: remove dependency on <algorithm>
*/ */
#include "imgui.h" #include "imgui.h"
@ -198,15 +195,11 @@ static ImGuiWindow* FindHoveredWindow(ImVec2 pos, bool excluding_childs);
}; // namespace ImGui }; // namespace ImGui
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Platform dependant helpers // Platform dependant default implementations
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifdef _MSC_VER static const char* GetClipboardTextFn_DefaultImpl();
#ifndef IMGUI_DONT_IMPLEMENT_WINDOWS_CLIPBOARD_FUNCTIONS static void SetClipboardTextFn_DefaultImpl(const char* text, const char* text_end);
static const char* GetClipboardTextFn_DefaultImplWindows();
static void SetClipboardTextFn_DefaultImplWindows(const char* text, const char* text_end);
#endif
#endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// User facing structures // User facing structures
@ -282,12 +275,8 @@ ImGuiIO::ImGuiIO()
MouseDoubleClickMaxDist = 6.0f; MouseDoubleClickMaxDist = 6.0f;
// Platform dependant default implementations // Platform dependant default implementations
#ifdef _MSC_VER GetClipboardTextFn = GetClipboardTextFn_DefaultImpl;
#ifndef IMGUI_DONT_IMPLEMENT_WINDOWS_CLIPBOARD_FUNCTIONS SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;
GetClipboardTextFn = GetClipboardTextFn_DefaultImplWindows;
SetClipboardTextFn = SetClipboardTextFn_DefaultImplWindows;
#endif
#endif
} }
// Pass in translated ASCII characters for text input. // Pass in translated ASCII characters for text input.
@ -630,6 +619,7 @@ struct ImGuiState
ImGuiStorage ColorEditModeStorage; // for user selection ImGuiStorage ColorEditModeStorage; // for user selection
ImGuiID ActiveComboID; ImGuiID ActiveComboID;
char Tooltip[1024]; char Tooltip[1024];
char* PrivateClipboard; // if no custom clipboard handler is defined
// Logging // Logging
bool LogEnabled; bool LogEnabled;
@ -653,6 +643,7 @@ struct ImGuiState
SliderAsInputTextId = 0; SliderAsInputTextId = 0;
ActiveComboID = 0; ActiveComboID = 0;
memset(Tooltip, 0, sizeof(Tooltip)); memset(Tooltip, 0, sizeof(Tooltip));
PrivateClipboard = NULL;
LogEnabled = false; LogEnabled = false;
LogFile = NULL; LogFile = NULL;
LogAutoExpandMaxDepth = 2; LogAutoExpandMaxDepth = 2;
@ -1313,6 +1304,12 @@ void Shutdown()
g.IO.Font = NULL; g.IO.Font = NULL;
} }
if (g.PrivateClipboard)
{
free(g.PrivateClipboard);
g.PrivateClipboard = NULL;
}
g.Initialized = false; g.Initialized = false;
} }
@ -5201,13 +5198,13 @@ void ImBitmapFont::RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& c
// PLATFORM DEPENDANT HELPERS // PLATFORM DEPENDANT HELPERS
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifdef _MSC_VER #if defined(_MSC_VER) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS)
#ifndef IMGUI_DONT_IMPLEMENT_WINDOWS_CLIPBOARD_FUNCTIONS
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
static const char* GetClipboardTextFn_DefaultImplWindows() // Win32 API clipboard implementation
static const char* GetClipboardTextFn_DefaultImpl()
{ {
static char* buf_local = NULL; static char* buf_local = NULL;
if (buf_local) if (buf_local)
@ -5215,47 +5212,62 @@ static const char* GetClipboardTextFn_DefaultImplWindows()
free(buf_local); free(buf_local);
buf_local = NULL; buf_local = NULL;
} }
if (!OpenClipboard(NULL)) if (!OpenClipboard(NULL))
return NULL; return NULL;
HANDLE buf_handle = GetClipboardData(CF_TEXT); HANDLE buf_handle = GetClipboardData(CF_TEXT);
if (buf_handle == NULL) if (buf_handle == NULL)
return NULL; return NULL;
if (char* buf_global = (char*)GlobalLock(buf_handle)) if (char* buf_global = (char*)GlobalLock(buf_handle))
buf_local = strdup(buf_global); buf_local = strdup(buf_global);
GlobalUnlock(buf_handle); GlobalUnlock(buf_handle);
CloseClipboard(); CloseClipboard();
return buf_local; return buf_local;
} }
static void SetClipboardTextFn_DefaultImplWindows(const char* text, const char* text_end) // Win32 API clipboard implementation
static void SetClipboardTextFn_DefaultImpl(const char* text, const char* text_end)
{ {
if (!OpenClipboard(NULL)) if (!OpenClipboard(NULL))
return; return;
if (!text_end) if (!text_end)
text_end = text + strlen(text); text_end = text + strlen(text);
const int buf_length = (text_end - text) + 1; const int buf_length = (text_end - text) + 1;
HGLOBAL buf_handle = GlobalAlloc(GMEM_MOVEABLE, buf_length * sizeof(char)); HGLOBAL buf_handle = GlobalAlloc(GMEM_MOVEABLE, buf_length * sizeof(char));
if (buf_handle == NULL) if (buf_handle == NULL)
return; return;
char* buf_global = (char *)GlobalLock(buf_handle); char* buf_global = (char *)GlobalLock(buf_handle);
memcpy(buf_global, text, text_end - text); memcpy(buf_global, text, text_end - text);
buf_global[text_end - text] = 0; buf_global[text_end - text] = 0;
GlobalUnlock(buf_handle); GlobalUnlock(buf_handle);
EmptyClipboard(); EmptyClipboard();
SetClipboardData(CF_TEXT, buf_handle); SetClipboardData(CF_TEXT, buf_handle);
CloseClipboard(); CloseClipboard();
} }
#endif // #ifndef IMGUI_DONT_IMPLEMENT_WINDOWS_CLIPBOARD_FUNCTIONS #else
#endif // #ifdef _MSC_VER
// Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
static const char* GetClipboardTextFn_DefaultImpl()
{
return GImGui.PrivateClipboard;
}
// Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers
static void SetClipboardTextFn_DefaultImpl(const char* text, const char* text_end)
{
if (GImGui.PrivateClipboard)
{
free(GImGui.PrivateClipboard);
GImGui.PrivateClipboard = NULL;
}
if (!text_end)
text_end = text + strlen(text);
GImGui.PrivateClipboard = (char*)malloc(text_end - text + 1);
memcpy(GImGui.PrivateClipboard, text, text_end - text);
GImGui.PrivateClipboard[text_end - text] = 0;
}
#endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// HELP // HELP

19
imgui.h

@ -384,10 +384,16 @@ struct ImGuiIO
bool FontAllowScaling; // = false // Set to allow scaling text with CTRL+Wheel. bool FontAllowScaling; // = false // Set to allow scaling text with CTRL+Wheel.
float PixelCenterOffset; // = 0.5f // Set to 0.0f for DirectX <= 9, 0.5f for Direct3D >= 10 and OpenGL. float PixelCenterOffset; // = 0.5f // Set to 0.0f for DirectX <= 9, 0.5f for Direct3D >= 10 and OpenGL.
// Settings - Functions (fill once) // Settings - Rendering function (REQUIRED)
void (*RenderDrawListsFn)(ImDrawList** const draw_lists, int count); // Required // See example code if you are unsure of how to implement this.
const char* (*GetClipboardTextFn)(); // Required for clipboard support void (*RenderDrawListsFn)(ImDrawList** const draw_lists, int count);
void (*SetClipboardTextFn)(const char* text, const char* text_end); // Required for clipboard support (nb- the string is *NOT* zero-terminated at 'text_end')
// Settings - Clipboard Support
// Override to provide your clipboard handlers.
// On Windows architecture, defaults to use the native Win32 clipboard, otherwise default to use a ImGui private clipboard.
// NB- for SetClipboardTextFn, the string is *NOT* zero-terminated at 'text_end'
const char* (*GetClipboardTextFn)();
void (*SetClipboardTextFn)(const char* text, const char* text_end);
// Input - Fill before calling NewFrame() // Input - Fill before calling NewFrame()
ImVec2 MousePos; // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.) ImVec2 MousePos; // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
@ -525,8 +531,11 @@ struct ImDrawVert
// User is responsible for providing a renderer for this in ImGuiIO::RenderDrawListFn // User is responsible for providing a renderer for this in ImGuiIO::RenderDrawListFn
struct ImDrawList struct ImDrawList
{ {
ImVector<ImDrawCmd> commands; // This is what you have to render
ImVector<ImDrawCmd> commands; // commands
ImVector<ImDrawVert> vtx_buffer; // each command consume ImDrawCmd::vtx_count of those ImVector<ImDrawVert> vtx_buffer; // each command consume ImDrawCmd::vtx_count of those
// [Internal to ImGui]
ImVector<ImVec4> clip_rect_stack; // [internal] clip rect stack while building the command-list (so text command can perform clipping early on) ImVector<ImVec4> clip_rect_stack; // [internal] clip rect stack while building the command-list (so text command can perform clipping early on)
ImDrawVert* vtx_write; // [internal] point within vtx_buffer after each add command (to avoid using the ImVector<> operators too much) ImDrawVert* vtx_write; // [internal] point within vtx_buffer after each add command (to avoid using the ImVector<> operators too much)

Loading…
Cancel
Save