Added Copy and Paste functions to GHOST.

- Moved WIN32 code to ghost and added code for other systems.
- Added functions getClipboard(flag), and putClipboard(buffer, flag)
    -Flag is used on X11 to request selection buffer or clipboard.
    -If any other system uses flag = 1 the function returns doing nothing.
- Changed ctrl +c/v and shift+ctrl + c/v to do the same thing (use the clipboard).
- Changed the menu items (copy, paste) to use the clipboard.
This commit is contained in:
Ricki Myers 2008-03-04 00:41:30 +00:00
parent c7d0a2a92a
commit ffcbff62dd
14 changed files with 448 additions and 54 deletions

@ -758,6 +758,18 @@ extern void GHOST_SetRectangleCenter(GHOST_RectangleHandle rectanglehandle,
*/
extern GHOST_TSuccess GHOST_ClipRectangle(GHOST_RectangleHandle rectanglehandle,
GHOST_RectangleHandle anotherrectanglehandle);
/**
* Return the data from the clipboad
* @return clipboard data
*/
extern GHOST_TUns8* getClipboard(int flag);
/**
* Put data to the Clipboard
*/
extern void putClipboard(GHOST_TInt8 *buffer, int flag);
#ifdef __cplusplus
}
#endif

@ -336,6 +336,18 @@ public:
*/
virtual GHOST_TSuccess getButtonState(GHOST_TButtonMask mask, bool& isDown) const = 0;
/**
* Returns the selection buffer
* @return Returns "unsinged char" from X11 XA_CUT_BUFFER0 buffer
*
*/
virtual GHOST_TUns8* getClipboard(int flag) const = 0;
/**
* Put data to the Clipboard
*/
virtual void putClipboard(GHOST_TInt8 *buffer, int flag) const = 0;
protected:
/**
* Initialize the system.

@ -802,3 +802,16 @@ GHOST_TSuccess GHOST_ClipRectangle(GHOST_RectangleHandle rectanglehandle,
return result;
}
GHOST_TUns8* getClipboard(int flag)
{
GHOST_ISystem* system = GHOST_ISystem::getSystem();
return system->getClipboard(flag);
}
void putClipboard(GHOST_TInt8 *buffer, int flag)
{
GHOST_ISystem* system = GHOST_ISystem::getSystem();
system->putClipboard(buffer, flag);
}

@ -257,6 +257,21 @@ public:
*/
virtual GHOST_TSuccess getButtons(GHOST_Buttons& buttons) const = 0;
/**
* Returns the selection buffer
* @param flag Only used on X11
* @return Returns the clipboard data
*
*/
virtual GHOST_TUns8* getClipboard(int flag) const = 0;
/**
* Put data to the Clipboard
* @param buffer The buffer to copy to the clipboard
* @param flag The clipboard to copy too only used on X11
*/
virtual void putClipboard(GHOST_TInt8 *buffer, int flag) const = 0;
protected:
/**
* Initialize the system.

@ -1065,3 +1065,71 @@ OSStatus GHOST_SystemCarbon::sEventHandlerProc(EventHandlerCallRef handler, Even
return err;
}
GHOST_TUns8* GHOST_SystemCarbon::getClipboard(int flag) const
{
PasteboardRef inPasteboard;
PasteboardItemID itemID;
CFDataRef flavorData;
OSStatus err = noErr;
GHOST_TUns8 * temp_buff;
CFRange range;
err = PasteboardCreate(kPasteboardClipboard, &inPasteboard);
if(err != noErr) { return NULL;}
err = PasteboardSynchronize( inPasteboard );
if(err != noErr) { return NULL;}
err = PasteboardGetItemIdentifier( inPasteboard, 1, &itemID );
if(err != noErr) { return NULL;}
err = PasteboardCopyItemFlavorData( inPasteboard, itemID, CFSTR("public.utf8-plain-text"), &flavorData);
if(err != noErr) { return NULL;}
range = CFRangeMake(0, CFDataGetLength(flavorData));
temp_buff = (GHOST_TUns8*) malloc(range.length+1);
CFDataGetBytes(flavorData, range, (UInt8*)temp_buff);
temp_buff[range.length] = '\0';
if(temp_buff) {
return temp_buff;
} else {
return NULL;
}
}
void GHOST_SystemCarbon::putClipboard(GHOST_TInt8 *buffer, int flag) const
{
if(flag == 1) {return;} //If Flag is 1 means the selection and is used on X11
PasteboardRef inPasteboard;
CFDataRef textData = NULL;
OSStatus err = noErr; /*For error checking*/
err = PasteboardCreate(kPasteboardClipboard, &inPasteboard);
if(err != noErr) { return;}
err = PasteboardSynchronize( inPasteboard );
if(err != noErr) { return;}
err = PasteboardClear( inPasteboard );
if(err != noErr) { return;}
textData = CFDataCreate(kCFAllocatorDefault, (UInt8*)buffer, strlen(buffer));
if (textData) {
err = PasteboardPutItemFlavor( inPasteboard, (PasteboardItemID)1, CFSTR("public.utf8-plain-text"), textData, 0);
if(err != noErr) {
if(textData) { CFRelease(textData);}
return;
}
}
if(textData) {
CFRelease(textData);
}
}

@ -168,6 +168,20 @@ public:
*/
virtual GHOST_TSuccess getButtons(GHOST_Buttons& buttons) const;
/**
* Returns Clipboard data
* @param flag Indicate which buffer to return
* @return Returns the selected buffer
*/
virtual GHOST_TUns8* GHOST_SystemCarbon::getClipboard(int flag) const;
/**
* Puts buffer to system clipboard
* @param buffer The buffer to be copied
* @param flag Indicates which buffer to copy too Only used on X11
*/
virtual void putClipboard(GHOST_TInt8 *buffer, int flag) const;
protected:
/**
* Initializes the system.

@ -873,3 +873,54 @@ LRESULT WINAPI GHOST_SystemWin32::s_wndProc(HWND hwnd, UINT msg, WPARAM wParam,
}
return lResult;
}
GHOST_TUns8* GHOST_SystemWin32::getClipboard(int flag) const
{
char *buffer;
char *temp_buff;
if ( OpenClipboard(NULL) ) {
HANDLE hData = GetClipboardData( CF_TEXT );
buffer = (char*)GlobalLock( hData );
temp_buff = (char*) malloc(strlen(buffer)+1);
strcpy(temp_buff, buffer);
GlobalUnlock( hData );
CloseClipboard();
temp_buff[strlen(buffer)] = '\0';
if (buffer) {
return (GHOST_TUns8*)temp_buff;
} else {
return NULL;
}
} else {
return NULL;
}
}
void GHOST_SystemWin32::putClipboard(GHOST_TInt8 *buffer, int flag) const
{
if(flag == 1) {return;} //If Flag is 1 means the selection and is used on X11
if (OpenClipboard(NULL)) {
HLOCAL clipbuffer;
char *data;
if (buffer) {
EmptyClipboard();
clipbuffer = LocalAlloc(LMEM_FIXED,((strlen(buffer)+1)));
data = (char*)GobalLock(clipbuffer);
strcpy(data, (char*)buffer);
data[strlen(buffer)] = '\0';
LocalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
}
CloseClipboard();
} else {
return;
}
}

@ -170,6 +170,20 @@ public:
*/
virtual GHOST_TSuccess getButtons(GHOST_Buttons& buttons) const;
/**
* Returns unsinged char from CUT_BUFFER0
* @param flag Flag is not used on win32 on used on X11
* @return Returns the Clipboard
*/
virtual GHOST_TUns8* getClipboard(int flag) const;
/**
* Puts buffer to system clipboard
* @param flag Flag is not used on win32 on used on X11
* @return No return
*/
virtual void putClipboard(GHOST_TInt8 *buffer, int flag) const;
protected:
/**
* Initializes the system.

@ -95,6 +95,9 @@
#include <unistd.h>
#include <vector>
//these are for copy and select copy
static char *txt_cut_buffer= NULL;
static char *txt_select_buffer= NULL;
using namespace std;
@ -336,7 +339,7 @@ GHOST_SystemX11::processEvent(XEvent *xe)
if (!window) {
return;
}
switch (xe->type) {
case Expose:
{
@ -504,8 +507,57 @@ GHOST_SystemX11::processEvent(XEvent *xe)
case MappingNotify:
case ReparentNotify:
break;
default: {
case SelectionRequest:
{
XEvent nxe;
Atom target, string, compound_text, c_string;
XSelectionRequestEvent *xse = &xe->xselectionrequest;
target = XInternAtom(m_display, "TARGETS", False);
string = XInternAtom(m_display, "STRING", False);
compound_text = XInternAtom(m_display, "COMPOUND_TEXT", False);
c_string = XInternAtom(m_display, "C_STRING", False);
/* support obsolete clients */
if (xse->property == None) {
xse->property = xse->target;
}
nxe.xselection.type = SelectionNotify;
nxe.xselection.requestor = xse->requestor;
nxe.xselection.property = xse->property;
nxe.xselection.display = xse->display;
nxe.xselection.selection = xse->selection;
nxe.xselection.target = xse->target;
nxe.xselection.time = xse->time;
/*Check to see if the requestor is asking for String*/
if(xse->target == string || xse->target == compound_text || xse->target == c_string) {
if (xse->selection == XInternAtom(m_display, "PRIMARY", False)) {
XChangeProperty(m_display, xse->requestor, xse->property, xse->target, 8, PropModeReplace, (unsigned char*)txt_select_buffer, strlen(txt_select_buffer));
} else if (xse->selection == XInternAtom(m_display, "CLIPBOARD", False)) {
XChangeProperty(m_display, xse->requestor, xse->property, xse->target, 8, PropModeReplace, (unsigned char*)txt_cut_buffer, strlen(txt_cut_buffer));
}
} else if (xse->target == target) {
Atom alist[4];
alist[0] = target;
alist[1] = string;
alist[2] = compound_text;
alist[3] = c_string;
XChangeProperty(m_display, xse->requestor, xse->property, xse->target, 32, PropModeReplace, (unsigned char*)alist, 4);
XFlush(m_display);
} else {
//Change property to None because we do not support anything but STRING
nxe.xselection.property = None;
}
//Send the event to the client 0 0 == False, SelectionNotify
XSendEvent(m_display, xse->requestor, 0, 0, &nxe);
XFlush(m_display);
break;
}
default: {
if(xe->type == window->GetXTablet().MotionEvent)
{
XDeviceMotionEvent* data = (XDeviceMotionEvent*)xe;
@ -876,3 +928,113 @@ convertXKey(
}
#undef GXMAP
GHOST_TUns8*
GHOST_SystemX11::
getClipboard(int flag
) const {
//Flag
//0 = Regular clipboard 1 = selection
static Atom Primary_atom, clip_String, compound_text;
Atom rtype;
Window m_window, owner;
unsigned char *data, *tmp_data;
int bits;
unsigned long len, bytes;
XEvent xevent;
vector<GHOST_IWindow *> & win_vec = m_windowManager->getWindows();
vector<GHOST_IWindow *>::iterator win_it = win_vec.begin();
GHOST_WindowX11 * window = static_cast<GHOST_WindowX11 *>(*win_it);
m_window = window->getXWindow();
clip_String = XInternAtom(m_display, "_BLENDER_STRING", False);
compound_text = XInternAtom(m_display, "COMPOUND_TEXT", False);
//lets check the owner and if it is us then return the static buffer
if(flag == 0) {
Primary_atom = XInternAtom(m_display, "CLIPBOARD", False);
owner = XGetSelectionOwner(m_display, Primary_atom);
if (owner == m_window) {
data = (unsigned char*) malloc(strlen(txt_cut_buffer));
strcpy((char*)data, txt_cut_buffer);
return (GHOST_TUns8*)data;
} else if (owner == None) {
return NULL;
}
} else {
Primary_atom = XInternAtom(m_display, "PRIMARY", False);
owner = XGetSelectionOwner(m_display, Primary_atom);
if (owner == m_window) {
data = (unsigned char*) malloc(strlen(txt_select_buffer));
strcpy((char*)data, txt_select_buffer);
return (GHOST_TUns8*)data;
} else if (owner == None) {
return NULL;
}
}
if(!Primary_atom) {
return NULL;
}
XDeleteProperty(m_display, m_window, Primary_atom);
XConvertSelection(m_display, Primary_atom, compound_text, clip_String, m_window, CurrentTime); //XA_STRING
XFlush(m_display);
//This needs to change so we do not wait for ever or check owner first
while(1) {
XNextEvent(m_display, &xevent);
if(xevent.type == SelectionNotify) {
if(XGetWindowProperty(m_display, m_window, xevent.xselection.property, 0L, 4096L, False, AnyPropertyType, &rtype, &bits, &len, &bytes, &data) == Success) {
tmp_data = (unsigned char*) malloc(strlen((char*)data));
strcpy((char*)tmp_data, (char*)data);
XFree(data);
return (GHOST_TUns8*)tmp_data;
}
return NULL;
}
}
}
void
GHOST_SystemX11::
putClipboard(
GHOST_TInt8 *buffer, int flag) const
{
static Atom Primary_atom;
Window m_window, owner;
if(!buffer) {return;}
if(flag == 0) {
Primary_atom = XInternAtom(m_display, "CLIPBOARD", False);
if(txt_cut_buffer) { free((void*)txt_cut_buffer); }
txt_cut_buffer = (char*) malloc(strlen(buffer));
strcpy(txt_cut_buffer, buffer);
} else {
Primary_atom = XInternAtom(m_display, "PRIMARY", False);
if(txt_select_buffer) { free((void*)txt_select_buffer); }
txt_select_buffer = (char*) malloc(strlen(buffer));
strcpy(txt_select_buffer, buffer);
}
vector<GHOST_IWindow *> & win_vec = m_windowManager->getWindows();
vector<GHOST_IWindow *>::iterator win_it = win_vec.begin();
GHOST_WindowX11 * window = static_cast<GHOST_WindowX11 *>(*win_it);
m_window = window->getXWindow();
if(!Primary_atom) {
return;
}
XSetSelectionOwner(m_display, Primary_atom, m_window, CurrentTime);
owner = XGetSelectionOwner(m_display, Primary_atom);
if (owner != m_window)
printf("failed to own primary\n");
return;
}

@ -196,7 +196,21 @@ public:
return m_display;
}
/**
* Returns unsinged char from CUT_BUFFER0
* @param flag Flag indicates which buffer to return 0 for clipboard 1 for selection
* @return Returns the Clipboard indicated by Flag
*/
GHOST_TUns8*
getClipboard(int flag) const;
/**
* Puts buffer to system clipboard
* @param buffer The buffer to copy to the clipboard
* @param flag Flag indicates which buffer to set ownership of 0 for clipboard 1 for selection
*/
virtual void putClipboard(GHOST_TInt8 *buffer, int flag) const;
private :
Display * m_display;

@ -204,7 +204,7 @@ GHOST_WindowX11(
KeyPressMask | KeyReleaseMask |
EnterWindowMask | LeaveWindowMask |
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask | FocusChangeMask;
PointerMotionMask | FocusChangeMask | PropertyChangeMask;
// create the window!
@ -766,6 +766,15 @@ validate(
GHOST_WindowX11::
~GHOST_WindowX11(
){
static Atom Primary_atom, Clipboard_atom;
Window p_owner, c_owner;
/*Change the owner of the Atoms to None if we are the owner*/
Primary_atom = XInternAtom(m_display, "PRIMARY", False);
Clipboard_atom = XInternAtom(m_display, "CLIPBOARD", False);
p_owner = XGetSelectionOwner(m_display, Primary_atom);
c_owner = XGetSelectionOwner(m_display, Clipboard_atom);
std::map<unsigned int, Cursor>::iterator it = m_standard_cursors.begin();
for (; it != m_standard_cursors.end(); it++) {
XFreeCursor(m_display, it->second);
@ -784,6 +793,14 @@ GHOST_WindowX11::
}
glXDestroyContext(m_display, m_context);
}
if (p_owner == m_window) {
XSetSelectionOwner(m_display, Primary_atom, None, CurrentTime);
}
if (c_owner == m_window) {
XSetSelectionOwner(m_display, Clipboard_atom, None, CurrentTime);
}
XDestroyWindow(m_display, m_window);
XFree(m_visual);
}

@ -95,7 +95,8 @@ void indent (struct Text *text);
void uncomment (struct Text *text);
int setcurr_tab (struct Text *text);
void convert_tabs (struct SpaceText *st, int tab);
void txt_copy_clipboard (struct Text *text);
void txt_paste_clipboard (struct Text *text);
/* Undo opcodes */
/* Simple main cursor movement */

@ -85,6 +85,8 @@
#include "mydevice.h"
#include "blendef.h"
#include "GHOST_C-api.h"
#define TEXTXLOC 38
/* forward declarations */
@ -92,10 +94,9 @@
void drawtextspace(ScrArea *sa, void *spacedata);
void winqreadtextspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
void txt_copy_selectbuffer (Text *text);
void txt_paste_clipboard(Text *text); /* blank on non Win32 */
void txt_copy_clipboard(Text *text); /* blank on non Win32 */
void do_brackets();
void get_selection_buffer(Text *text);
int check_bracket(char *string);
static int check_delim(char *string);
static int check_numbers(char *string);
@ -1306,8 +1307,6 @@ void txt_copy_selectbuffer (Text *text)
bufferlength = length;
}
#ifdef _WIN32
static char *unixNewLine(char *buffer)
{
char *p, *p2, *output;
@ -1340,55 +1339,42 @@ static char *winNewLine(char *buffer)
return(output);
}
#endif
void txt_paste_clipboard(Text *text) {
#ifdef _WIN32
char * buffer = NULL;
if ( OpenClipboard(NULL) ) {
HANDLE hData = GetClipboardData( CF_TEXT );
buffer = (char*)GlobalLock( hData );
if (buffer) {
buffer = unixNewLine(buffer);
if (buffer) txt_insert_buf(text, buffer);
}
GlobalUnlock( hData );
CloseClipboard();
MEM_freeN(buffer);
char * buff;
char *temp_buff;
buff = (char*)getClipboard(0);
if(buff) {
temp_buff = unixNewLine(buff);
txt_insert_buf(text, temp_buff);
if(buff){free((void*)buff);}
if(temp_buff){MEM_freeN(temp_buff);}
}
#endif
}
void get_selection_buffer(Text *text)
{
char *buff = (char*)getClipboard(1);
txt_insert_buf(text, buff);
}
void txt_copy_clipboard(Text *text) {
#ifdef _WIN32
char *temp;
txt_copy_selectbuffer(text);
if (OpenClipboard(NULL)) {
HLOCAL clipbuffer;
char* buffer;
if (copybuffer) {
copybuffer = winNewLine(copybuffer);
EmptyClipboard();
clipbuffer = LocalAlloc(LMEM_FIXED,((bufferlength+1)));
buffer = (char *) LocalLock(clipbuffer);
strncpy(buffer, copybuffer, bufferlength);
buffer[bufferlength] = '\0';
LocalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
}
CloseClipboard();
}
if (copybuffer) {
copybuffer[bufferlength] = '\0';
temp = winNewLine(copybuffer);
putClipboard((GHOST_TInt8*)temp, 0);
MEM_freeN(temp);
MEM_freeN(copybuffer);
copybuffer= NULL;
}
#endif
}
/*
@ -1537,19 +1523,29 @@ void winqreadtextspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
if (event==LEFTMOUSE) {
if (val) {
short mval[2];
char *buffer;
set_tabs(text);
getmouseco_areawin(mval);
if (mval[0]>2 && mval[0]<20 && mval[1]>2 && mval[1]<curarea->winy-2) {
do_textscroll(st, 2);
} else {
} else {
do_selection(st, G.qual&LR_SHIFTKEY);
buffer = txt_sel_to_buf(text);
putClipboard((GHOST_TInt8*)buffer, 1);
MEM_freeN(buffer);
do_draw= 1;
}
}
} else if (event==MIDDLEMOUSE) {
if (val) {
do_textscroll(st, 1);
#ifdef _WIN32 || __APPLE__
do_textscroll(st, 1);
#else
do_selection(st, G.qual&LR_SHIFTKEY);
get_selection_buffer(text);
do_draw= 1;
#endif
}
} else if (event==RIGHTMOUSE) {
if (val) {
@ -1606,7 +1602,7 @@ void winqreadtextspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
if(G.qual & LR_SHIFTKEY)
txt_copy_clipboard(text);
else
txt_copy_sel(text);
txt_copy_clipboard(text);
do_draw= 1;
}
@ -1630,15 +1626,18 @@ void winqreadtextspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
if (G.qual == (LR_ALTKEY|LR_SHIFTKEY)) {
switch(pupmenu("Edit %t|Cut %x0|Copy %x1|Paste %x2|Print Cut Buffer %x3")) {
case 0:
txt_copy_clipboard(text); //First copy to clipboard
txt_cut_sel(text);
do_draw= 1;
break;
case 1:
txt_copy_sel(text);
txt_copy_clipboard(text);
//txt_copy_sel(text);
do_draw= 1;
break;
case 2:
txt_paste(text);
//txt_paste(text);
txt_paste_clipboard(text);
if (st->showsyntax) get_format_string(st);
do_draw= 1;
break;
@ -1804,7 +1803,7 @@ void winqreadtextspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
if (G.qual & LR_SHIFTKEY)
txt_paste_clipboard(text);
else
txt_paste(text);
txt_paste_clipboard(text);
if (st->showsyntax) get_format_string(st);
do_draw= 1;
pop_space_text(st);

@ -348,14 +348,16 @@ static void do_text_editmenu(void *arg, int event)
txt_do_redo(text);
break;
case 3:
txt_copy_clipboard(text);
txt_cut_sel(text);
pop_space_text(st);
break;
case 4:
txt_copy_sel(text);
//txt_copy_sel(text);
txt_copy_clipboard(text);
break;
case 5:
txt_paste(text);
txt_paste_clipboard(text);
if (st->showsyntax) get_format_string(st);
break;
case 6: