diff options
author | Ricki Myers <antihc3@gmail.com> | 2008-03-04 03:41:30 +0300 |
---|---|---|
committer | Ricki Myers <antihc3@gmail.com> | 2008-03-04 03:41:30 +0300 |
commit | ffcbff62dd569b880c39084889fe68be76f67f51 (patch) | |
tree | 61e5ba1ed5395117a41b90323c25a54e3ab7c234 /intern | |
parent | c7d0a2a92a0029573130896ae93171cfb055c50f (diff) |
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.
Diffstat (limited to 'intern')
-rw-r--r-- | intern/ghost/GHOST_C-api.h | 12 | ||||
-rw-r--r-- | intern/ghost/GHOST_ISystem.h | 12 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_C-api.cpp | 13 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_System.h | 15 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_SystemCarbon.cpp | 68 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_SystemCarbon.h | 14 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_SystemWin32.cpp | 51 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_SystemWin32.h | 14 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_SystemX11.cpp | 168 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_SystemX11.h | 14 | ||||
-rw-r--r-- | intern/ghost/intern/GHOST_WindowX11.cpp | 19 |
11 files changed, 396 insertions, 4 deletions
diff --git a/intern/ghost/GHOST_C-api.h b/intern/ghost/GHOST_C-api.h index 4c4094409dd..a819713df88 100644 --- a/intern/ghost/GHOST_C-api.h +++ b/intern/ghost/GHOST_C-api.h @@ -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 diff --git a/intern/ghost/GHOST_ISystem.h b/intern/ghost/GHOST_ISystem.h index dffd81bdb13..5e0473203db 100644 --- a/intern/ghost/GHOST_ISystem.h +++ b/intern/ghost/GHOST_ISystem.h @@ -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. diff --git a/intern/ghost/intern/GHOST_C-api.cpp b/intern/ghost/intern/GHOST_C-api.cpp index c2b8eca9573..6943e85f12f 100644 --- a/intern/ghost/intern/GHOST_C-api.cpp +++ b/intern/ghost/intern/GHOST_C-api.cpp @@ -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); +} + diff --git a/intern/ghost/intern/GHOST_System.h b/intern/ghost/intern/GHOST_System.h index 85e7b2d6b44..1ded6e77a36 100644 --- a/intern/ghost/intern/GHOST_System.h +++ b/intern/ghost/intern/GHOST_System.h @@ -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. diff --git a/intern/ghost/intern/GHOST_SystemCarbon.cpp b/intern/ghost/intern/GHOST_SystemCarbon.cpp index e1e3853e8a8..46c0f51ede0 100644 --- a/intern/ghost/intern/GHOST_SystemCarbon.cpp +++ b/intern/ghost/intern/GHOST_SystemCarbon.cpp @@ -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); + } +} + diff --git a/intern/ghost/intern/GHOST_SystemCarbon.h b/intern/ghost/intern/GHOST_SystemCarbon.h index 93022aa78ff..dc03cd2a84d 100644 --- a/intern/ghost/intern/GHOST_SystemCarbon.h +++ b/intern/ghost/intern/GHOST_SystemCarbon.h @@ -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. diff --git a/intern/ghost/intern/GHOST_SystemWin32.cpp b/intern/ghost/intern/GHOST_SystemWin32.cpp index 835f7da3038..0090b7ec93f 100644 --- a/intern/ghost/intern/GHOST_SystemWin32.cpp +++ b/intern/ghost/intern/GHOST_SystemWin32.cpp @@ -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; + } +} + diff --git a/intern/ghost/intern/GHOST_SystemWin32.h b/intern/ghost/intern/GHOST_SystemWin32.h index 218fc5794eb..762e5a988a4 100644 --- a/intern/ghost/intern/GHOST_SystemWin32.h +++ b/intern/ghost/intern/GHOST_SystemWin32.h @@ -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. diff --git a/intern/ghost/intern/GHOST_SystemX11.cpp b/intern/ghost/intern/GHOST_SystemX11.cpp index 14383ad3624..cfd77e5cef1 100644 --- a/intern/ghost/intern/GHOST_SystemX11.cpp +++ b/intern/ghost/intern/GHOST_SystemX11.cpp @@ -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; +} + diff --git a/intern/ghost/intern/GHOST_SystemX11.h b/intern/ghost/intern/GHOST_SystemX11.h index c8d8d73404a..0e947c12fde 100644 --- a/intern/ghost/intern/GHOST_SystemX11.h +++ b/intern/ghost/intern/GHOST_SystemX11.h @@ -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; diff --git a/intern/ghost/intern/GHOST_WindowX11.cpp b/intern/ghost/intern/GHOST_WindowX11.cpp index 0c999587d9b..1b9b3c491d5 100644 --- a/intern/ghost/intern/GHOST_WindowX11.cpp +++ b/intern/ghost/intern/GHOST_WindowX11.cpp @@ -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); } |