move clipboard functions out of misc.cpp without creating errors

This commit is contained in:
jacob1 2013-01-18 14:37:24 -05:00
parent b3a2ab735b
commit ded94b475d
11 changed files with 144 additions and 167 deletions

View File

@ -164,7 +164,7 @@ if(GetOption('lin')):
env.ParseConfig('pkg-config --libs glew gl glu')
openGLLibs = ['GL']
env.Append(LIBS=['X11', 'rt'])
env.Append(CPPDEFINES=["LIN","SDL_VIDEO_DRIVER_X11"])
env.Append(CPPDEFINES=["LIN"])
if GetOption('_64bit'):
env.Append(LINKFLAGS=['-m64'])
env.Append(CCFLAGS=['-m64'])

View File

@ -1 +1 @@
SConscript('SConscript', variant_dir='build')
SConscript('SConscript', variant_dir='build', duplicate=0)

View File

@ -49,15 +49,6 @@ std::string URLEscape(std::string source)
return finalString;
}
#if defined(USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>
SDL_SysWMinfo sdl_wminfo;
Atom XA_CLIPBOARD, XA_TARGETS;
#endif
char *clipboardText = NULL;
char *exe_name(void)
{
#if defined(WIN)
@ -368,138 +359,6 @@ vector2d v2d_new(float x, float y)
return result;
}
void clipboard_init()
{
#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
SDL_VERSION(&sdl_wminfo.version);
SDL_GetWMInfo(&sdl_wminfo);
sdl_wminfo.info.x11.lock_func();
XA_CLIPBOARD = XInternAtom(sdl_wminfo.info.x11.display, "CLIPBOARD", 1);
XA_TARGETS = XInternAtom(sdl_wminfo.info.x11.display, "TARGETS", 1);
sdl_wminfo.info.x11.unlock_func();
#endif
}
#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
void clipboard_event(SDL_Event event)
{
if (event.syswm.msg->subsystem != SDL_SYSWM_X11)
return;
sdl_wminfo.info.x11.lock_func();
XEvent xe = event.syswm.msg->event.xevent;
if (xe.type==SelectionClear)
{
if (clipboardText != NULL) {
free(clipboardText);
clipboardText = NULL;
}
}
else if (xe.type==SelectionRequest)
{
XEvent xr;
xr.xselection.type = SelectionNotify;
xr.xselection.requestor = xe.xselectionrequest.requestor;
xr.xselection.selection = xe.xselectionrequest.selection;
xr.xselection.target = xe.xselectionrequest.target;
xr.xselection.property = xe.xselectionrequest.property;
xr.xselection.time = xe.xselectionrequest.time;
if (xe.xselectionrequest.target==XA_TARGETS)
{
// send list of supported formats
Atom targets[] = {XA_TARGETS, XA_STRING};
xr.xselection.property = xe.xselectionrequest.property;
XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, XA_ATOM, 32, PropModeReplace, (unsigned char*)targets, (int)(sizeof(targets)/sizeof(Atom)));
}
// TODO: Supporting more targets would be nice
else if (xe.xselectionrequest.target==XA_STRING && clipboardText)
{
XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, xe.xselectionrequest.target, 8, PropModeReplace, (unsigned char*)clipboardText, strlen(clipboardText)+1);
}
else
{
// refuse clipboard request
xr.xselection.property = None;
}
XSendEvent(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, 0, 0, &xr);
}
sdl_wminfo.info.x11.unlock_func();
}
#endif
void clipboard_push_text(char * text)
{
if (clipboardText != NULL) {
free(clipboardText);
clipboardText = NULL;
}
clipboardText = mystrdup(text);
#ifdef MACOSX
PasteboardRef newclipboard;
if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return;
if (PasteboardClear(newclipboard)!=noErr) return;
PasteboardSynchronize(newclipboard);
CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text));
PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0);
#elif defined(WIN)
if (OpenClipboard(NULL))
{
HGLOBAL cbuffer;
char * glbuffer;
EmptyClipboard();
cbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1);
glbuffer = (char*)GlobalLock(cbuffer);
strcpy(glbuffer, text);
GlobalUnlock(cbuffer);
SetClipboardData(CF_TEXT, cbuffer);
CloseClipboard();
}
#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
sdl_wminfo.info.x11.lock_func();
XSetSelectionOwner(sdl_wminfo.info.x11.display, XA_CLIPBOARD, sdl_wminfo.info.x11.window, CurrentTime);
XFlush(sdl_wminfo.info.x11.display);
sdl_wminfo.info.x11.unlock_func();
#else
printf("Not implemented: put text on clipboard \"%s\"\n", text);
#endif
}
char * clipboard_pull_text()
{
#ifdef MACOSX
printf("Not implemented: get text from clipboard\n");
#elif defined(WIN)
if (OpenClipboard(NULL))
{
HANDLE cbuffer;
char * glbuffer;
cbuffer = GetClipboardData(CF_TEXT);
glbuffer = (char*)GlobalLock(cbuffer);
GlobalUnlock(cbuffer);
CloseClipboard();
if(glbuffer!=NULL){
return mystrdup(glbuffer);
}// else {
// return mystrdup("");
//}
}
#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
printf("Not implemented: get text from clipboard\n");
#else
printf("Not implemented: get text from clipboard\n");
#endif
if (clipboardText)
return mystrdup(clipboardText);
return mystrdup("");
}
int register_extension()
{
#if defined(WIN)

View File

@ -74,17 +74,6 @@ void strappend(char *dst, char *src);
void *file_load(char *fn, int *size);
void clipboard_init();
#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
#include <SDL/SDL.h>
void clipboard_event(SDL_Event event);
#endif
void clipboard_push_text(char * text);
char * clipboard_pull_text();
extern char *clipboard_text;
int register_extension();

View File

@ -1,3 +1,5 @@
#pragma once
void EngineProcess();
void EngineProcess();
void ClipboardPush(char * text);
char * ClipboardPull();

View File

@ -45,6 +45,15 @@
using namespace std;
#if defined(USE_SDL) && defined(LIN)
#include <SDL_syswm.h>
#endif
#if defined(USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
SDL_SysWMinfo sdl_wminfo;
Atom XA_CLIPBOARD, XA_TARGETS;
#endif
char *clipboardText = NULL;
#ifdef WIN
extern "C" IMAGE_DOS_HEADER __ImageBase;
@ -56,6 +65,79 @@ SDL_Surface * sdl_scrn;
int scale = 1;
bool fullscreen = false;
void ClipboardPush(char * text)
{
if (clipboardText != NULL) {
free(clipboardText);
clipboardText = NULL;
}
clipboardText = mystrdup(text);
#ifdef MACOSX
PasteboardRef newclipboard;
if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return;
if (PasteboardClear(newclipboard)!=noErr) return;
PasteboardSynchronize(newclipboard);
CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text));
PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0);
#elif defined(WIN)
if (OpenClipboard(NULL))
{
HGLOBAL cbuffer;
char * glbuffer;
EmptyClipboard();
cbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1);
glbuffer = (char*)GlobalLock(cbuffer);
strcpy(glbuffer, text);
GlobalUnlock(cbuffer);
SetClipboardData(CF_TEXT, cbuffer);
CloseClipboard();
}
#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
sdl_wminfo.info.x11.lock_func();
XSetSelectionOwner(sdl_wminfo.info.x11.display, XA_CLIPBOARD, sdl_wminfo.info.x11.window, CurrentTime);
XFlush(sdl_wminfo.info.x11.display);
sdl_wminfo.info.x11.unlock_func();
#else
printf("Not implemented: put text on clipboard \"%s\"\n", text);
#endif
}
char * ClipboardPull()
{
#ifdef MACOSX
printf("Not implemented: get text from clipboard\n");
#elif defined(WIN)
if (OpenClipboard(NULL))
{
HANDLE cbuffer;
char * glbuffer;
cbuffer = GetClipboardData(CF_TEXT);
glbuffer = (char*)GlobalLock(cbuffer);
GlobalUnlock(cbuffer);
CloseClipboard();
if(glbuffer!=NULL){
return mystrdup(glbuffer);
}// else {
// return mystrdup("");
//}
}
#elif defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
printf("Not implemented: get text from clipboard\n");
#else
printf("Not implemented: get text from clipboard\n");
#endif
if (clipboardText)
return mystrdup(clipboardText);
return mystrdup("");
}
#ifdef OGLI
void blit()
{
@ -364,7 +446,46 @@ void EngineProcess()
#endif
#if defined (USE_SDL) && defined(LIN) && defined(SDL_VIDEO_DRIVER_X11)
case SDL_SYSWMEVENT:
clipboard_event(event);
if (event.syswm.msg->subsystem != SDL_SYSWM_X11)
break;
sdl_wminfo.info.x11.lock_func();
XEvent xe = event.syswm.msg->event.xevent;
if (xe.type==SelectionClear)
{
if (clipboardText != NULL) {
free(clipboardText);
clipboardText = NULL;
}
}
else if (xe.type==SelectionRequest)
{
XEvent xr;
xr.xselection.type = SelectionNotify;
xr.xselection.requestor = xe.xselectionrequest.requestor;
xr.xselection.selection = xe.xselectionrequest.selection;
xr.xselection.target = xe.xselectionrequest.target;
xr.xselection.property = xe.xselectionrequest.property;
xr.xselection.time = xe.xselectionrequest.time;
if (xe.xselectionrequest.target==XA_TARGETS)
{
// send list of supported formats
Atom targets[] = {XA_TARGETS, XA_STRING};
xr.xselection.property = xe.xselectionrequest.property;
XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, XA_ATOM, 32, PropModeReplace, (unsigned char*)targets, (int)(sizeof(targets)/sizeof(Atom)));
}
// TODO: Supporting more targets would be nice
else if (xe.xselectionrequest.target==XA_STRING && clipboardText)
{
XChangeProperty(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, xe.xselectionrequest.property, xe.xselectionrequest.target, 8, PropModeReplace, (unsigned char*)clipboardText, strlen(clipboardText)+1);
}
else
{
// refuse clipboard request
xr.xselection.property = None;
}
XSendEvent(sdl_wminfo.info.x11.display, xe.xselectionrequest.requestor, 0, 0, &xr);
}
sdl_wminfo.info.x11.unlock_func();
continue;
#endif
}
@ -491,7 +612,13 @@ int main(int argc, char * argv[])
exit(-1);
}
#endif
clipboard_init();
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
SDL_VERSION(&sdl_wminfo.version);
SDL_GetWMInfo(&sdl_wminfo);
sdl_wminfo.info.x11.lock_func();
XA_CLIPBOARD = XInternAtom(sdl_wminfo.info.x11.display, "CLIPBOARD", 1);
XA_TARGETS = XInternAtom(sdl_wminfo.info.x11.display, "TARGETS", 1);
sdl_wminfo.info.x11.unlock_func();
ui::Engine::Ref().g = new Graphics();
ui::Engine::Ref().Scale = scale;
inputScale = 1.0f/float(scale);

View File

@ -211,11 +211,11 @@ void Label::copySelection()
std::string currentText = text;
if(selectionIndex1 > selectionIndex0) {
clipboard_push_text((char*)currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str());
ClipboardPush((char*)currentText.substr(selectionIndex0, selectionIndex1-selectionIndex0).c_str());
} else if(selectionIndex0 > selectionIndex1) {
clipboard_push_text((char*)currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str());
ClipboardPush((char*)currentText.substr(selectionIndex1, selectionIndex0-selectionIndex1).c_str());
} else {
clipboard_push_text((char*)currentText.c_str());
ClipboardPush((char*)currentText.c_str());
}
}

View File

@ -4,7 +4,7 @@
#include <string>
#include "Component.h"
#include "Misc.h"
#include "PowderToy.h"
#include "Colour.h"
namespace ui

View File

@ -125,20 +125,20 @@ void Textbox::OnContextMenuAction(int item)
void Textbox::cutSelection()
{
char * clipboardText;
clipboardText = clipboard_pull_text();
clipboardText = ClipboardPull();
std::string newText = std::string(clipboardText);
free(clipboardText);
if(HasSelection())
{
if(getLowerSelectionBound() < 0 || getHigherSelectionBound() > backingText.length())
return;
clipboard_push_text((char*)backingText.substr(getLowerSelectionBound(), getHigherSelectionBound()-getLowerSelectionBound()).c_str());
ClipboardPush((char*)backingText.substr(getLowerSelectionBound(), getHigherSelectionBound()-getLowerSelectionBound()).c_str());
backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound());
cursor = getLowerSelectionBound();
}
else
{
clipboard_push_text((char*)backingText.c_str());
ClipboardPush((char*)backingText.c_str());
backingText.clear();
}
ClearSelection();
@ -181,7 +181,7 @@ void Textbox::selectAll()
void Textbox::pasteIntoSelection()
{
char * clipboardText;
clipboardText = clipboard_pull_text();
clipboardText = ClipboardPull();
std::string newText = std::string(clipboardText);
free(clipboardText);
if(HasSelection())

View File

@ -4,7 +4,7 @@
#include <string>
#include "Label.h"
#include "Misc.h"
#include "PowderToy.h"
namespace ui
{

View File

@ -179,7 +179,7 @@ PreviewView::PreviewView():
CopyIDAction(PreviewView * v_){ v = v_; }
virtual void ActionCallback(ui::Button * sender)
{
clipboard_push_text((char*)v->saveIDTextbox->GetText().c_str());
ClipboardPush((char*)v->saveIDTextbox->GetText().c_str());
}
};