Permalink
Browse files

basis of selections handling, paste with ctrl-v in edit box

  • Loading branch information...
NotFound committed Dec 6, 2011
1 parent 127c1c6 commit a1d509c8bcbec65759cd3d589da3175075fdfd11
Showing with 133 additions and 14 deletions.
  1. +26 −13 GuitorConstants.winxhead
  2. +82 −0 src/Guitor.winxed
  3. +25 −1 src/GuitorNci.winxed
View
@@ -38,6 +38,7 @@ const int
// Some commonly used values:
const int None = 0;
+const int Success = 0;
const int CurrentTime = 0;
// Enter and Leave
@@ -62,6 +63,15 @@ const int
// WM hints
const int InputHint = 1;
+// Some predefined atoms
+const int
+ XA_PRIMARY = 1,
+ XA_SECONDARY = 2,
+ XA_ATOM = 4,
+ XA_STRING = 31,
+ XA_WM_COMMAND = 34,
+ XA_WM_TRANSIENT_FOR = 68;
+
namespace Events
{
@@ -79,19 +89,22 @@ const int
FocusChangeMask = 0x00200000;
// Event types
const int
- KeyPress = 2,
- KeyRelease = 3,
- ButtonPress = 4,
- ButtonRelease = 5,
- MotionNotify = 6,
- EnterNotify = 7,
- LeaveNotify = 8,
- FocusIn = 9,
- FocusOut = 10,
- Expose = 12,
- DestroyNotify = 17,
- ConfigureNotify = 22,
- ClientMessage = 33;
+ KeyPress = 2,
+ KeyRelease = 3,
+ ButtonPress = 4,
+ ButtonRelease = 5,
+ MotionNotify = 6,
+ EnterNotify = 7,
+ LeaveNotify = 8,
+ FocusIn = 9,
+ FocusOut = 10,
+ Expose = 12,
+ DestroyNotify = 17,
+ ConfigureNotify = 22,
+ SelectionClear = 29,
+ SelectionRequest = 30,
+ SelectionNotify = 31,
+ ClientMessage = 33;
}
View
@@ -36,6 +36,7 @@ extern function getfocusview;
extern function getmotionview;
extern function getbuttonview;
extern function getkeyview;
+extern function getselectionview;
extern function getclientview;
extern function getxcolorview;
extern function getxftcolorview;
@@ -1117,6 +1118,44 @@ class Window : Drawable
{
getfun("XUngrabPointer")(self.display.xdisplay, time);
}
+ function GetPropertyString(int property)
+ {
+ var display = self.display;
+ var viewint = new ["StructView"] ( [ DATATYPE_STRUCT, 1, DATATYPE_INT ] );
+ var viewlong = new ["StructView"] ( [ DATATYPE_STRUCT, 1, DATATYPE_LONG ] );
+ var viewptr = new ["StructView"] ( [ DATATYPE_STRUCT, 1, DATATYPE_PTR ] );
+ var actual_type_return = viewint.alloc();
+ var actual_format_return = viewint.alloc();
+ var nitems_return = viewlong.alloc();
+ var bytes_after_return = viewlong.alloc();
+ var prop_return = viewptr.alloc();
+ // Get 0 items, to check the size,
+ int r = getfun("XGetWindowProperty")(display.xdisplay, self.xdrawable,
+ property, 0, 0, false,
+ XA_STRING,
+ actual_type_return, actual_format_return,
+ nitems_return, bytes_after_return, prop_return);
+ if (r != Success)
+ return string(null);
+ var prop = viewptr[prop_return, 0];
+ Free(prop);
+ int n = viewint[bytes_after_return, 0];
+ if (n == 0)
+ return "";
+ n = (n + 3 ) / 4;
+ // Now get the size obtained
+ r = getfun("XGetWindowProperty")(display.xdisplay, self.xdrawable,
+ property, 0, n, true,
+ XA_STRING,
+ actual_type_return, actual_format_return, nitems_return,
+ bytes_after_return, prop_return);
+ if (r != Success)
+ return string(null);
+ prop = viewptr[prop_return, 0];
+ string result = prop.as_string("utf8");
+ Free(prop);
+ return result;
+ }
}
//**************************************************************
@@ -1229,6 +1268,7 @@ class ListenerWindow : Window
var OnLeave;
var OnFocusIn;
var OnFocusOut;
+ var OnSelectionNotify;
var OnClientMessage;
function ListenerWindow(controller)
{
@@ -1244,6 +1284,7 @@ class ListenerWindow : Window
self.OnLeave = new EventHandler(self, LeaveWindowMask);
self.OnFocusIn = new EventHandler(self, FocusChangeMask);
self.OnFocusOut = new EventHandler(self, FocusChangeMask);
+ self.OnSelectionNotify = new EventHandler(self, 0);
self.OnClientMessage = new EventHandler(self, 0);
self.controller = controller;
controller.register(self);
@@ -1294,6 +1335,9 @@ class ListenerWindow : Window
case LeaveNotify:
handler = self.OnLeave;
break;
+ case SelectionNotify:
+ handler = self.OnSelectionNotify;
+ break;
case ClientMessage:
handler = self.OnClientMessage;
break;
@@ -1431,6 +1475,7 @@ class EditBox : ChildWindow
self.OnKeyPress += function (event) { self.onkeypress(event); };
self.OnFocusIn += function (event) { self.onfocusin(event); };
self.OnFocusOut += function (event) { self.onfocusout(event); };
+ self.OnSelectionNotify += function (event) { self.onselectionnotify(event); };
}
function getValue()
{
@@ -1606,6 +1651,9 @@ class EditBox : ChildWindow
switch (key) {
case "\n": case "\r": case "\e":
return;
+ case "\x{16}":
+ self.paste(event.time());
+ return;
}
self.cursor();
text = substr(text, 0, pos) + key + substr(text, pos);
@@ -1622,6 +1670,40 @@ class EditBox : ChildWindow
self.drawall();
self.cursor();
}
+ function onselectionnotify(event)
+ {
+ var view = getselectionview();
+ var data = event.eventdata;
+ int selection = view[data, 5];
+ int property = view[data, 7];
+ if (property == None)
+ return;
+ string pasted = self.GetPropertyString(property);
+ if (pasted == null)
+ return;
+ int l = length(pasted);
+ self.cursor();
+ int pos = self.pos;
+ string text = self.text;
+ text = substr(text, 0, pos) + pasted + substr(text, pos);
+ pos += l;
+ self.text =: text;
+ self.pos =: pos;
+ int offset = self.offset;
+ int width = self.width;
+ int x = self.font.getTextxOff(self.display, substr(text, 0, pos));
+ if (x - offset >= width)
+ self.offset =: x - width / 4;
+ self.drawall();
+ self.cursor();
+ }
+ function paste(int time)
+ {
+ var display = self.display;
+ int clipboard = display.InternAtom("CLIPBOARD");
+ getfun("XConvertSelection")(display.xdisplay,
+ clipboard, XA_STRING, clipboard, self.xdrawable, time);
+ }
}
//**************************************************************
View
@@ -29,6 +29,7 @@ const string
EXPOSEVIEW = "guitor_exposeview",
FOCUSVIEW = "guitor_focusview",
CROSSINGVIEW = "guitor_crossingview",
+ SELECTIONVIEW = "guitor_selectionview",
CLIENTVIEW = "guitor_clientview",
XAWMHINTSVIEW = "guitor_xawmhintsview",
ATOMVIEW = "guitor_atomview",
@@ -354,7 +355,24 @@ function getcrossingview()
return st;
}
-
+function getselectionview()
+{
+ var ns = namespace Xlib__private;
+ var st = ns[SELECTIONVIEW];
+ if (st == null)
+ ns[SELECTIONVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 9,
+ DATATYPE_INT, // type
+ DATATYPE_LONG, // Serial
+ XLIB_Bool, // send event
+ DATATYPE_PTR, // display
+ XLIB_Window, // requestor
+ XLIB_Atom, // selection
+ XLIB_Atom, // target
+ XLIB_Atom, // property
+ XLIB_time_t // time
+ ] );
+ return st;
+}
function getclientview()
{
@@ -667,6 +685,12 @@ function create_function(string funcname)
case "XUngrabPointer":
sig = "vpl";
break;
+ case "XGetWindowProperty":
+ sig = "ipiilliippppp";
+ break;
+ case "XConvertSelection":
+ sig = "vpiiiil";
+ break;
default:
throw "Function " + funcname + " unknown";
}

0 comments on commit a1d509c

Please sign in to comment.