Skip to content

Commit a1d509c

Browse files
committed
basis of selections handling, paste with ctrl-v in edit box
1 parent 127c1c6 commit a1d509c

File tree

3 files changed

+133
-14
lines changed

3 files changed

+133
-14
lines changed

GuitorConstants.winxhead

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const int
3838

3939
// Some commonly used values:
4040
const int None = 0;
41+
const int Success = 0;
4142
const int CurrentTime = 0;
4243

4344
// Enter and Leave
@@ -62,6 +63,15 @@ const int
6263
// WM hints
6364
const int InputHint = 1;
6465

66+
// Some predefined atoms
67+
const int
68+
XA_PRIMARY = 1,
69+
XA_SECONDARY = 2,
70+
XA_ATOM = 4,
71+
XA_STRING = 31,
72+
XA_WM_COMMAND = 34,
73+
XA_WM_TRANSIENT_FOR = 68;
74+
6575
namespace Events
6676
{
6777

@@ -79,19 +89,22 @@ const int
7989
FocusChangeMask = 0x00200000;
8090
// Event types
8191
const int
82-
KeyPress = 2,
83-
KeyRelease = 3,
84-
ButtonPress = 4,
85-
ButtonRelease = 5,
86-
MotionNotify = 6,
87-
EnterNotify = 7,
88-
LeaveNotify = 8,
89-
FocusIn = 9,
90-
FocusOut = 10,
91-
Expose = 12,
92-
DestroyNotify = 17,
93-
ConfigureNotify = 22,
94-
ClientMessage = 33;
92+
KeyPress = 2,
93+
KeyRelease = 3,
94+
ButtonPress = 4,
95+
ButtonRelease = 5,
96+
MotionNotify = 6,
97+
EnterNotify = 7,
98+
LeaveNotify = 8,
99+
FocusIn = 9,
100+
FocusOut = 10,
101+
Expose = 12,
102+
DestroyNotify = 17,
103+
ConfigureNotify = 22,
104+
SelectionClear = 29,
105+
SelectionRequest = 30,
106+
SelectionNotify = 31,
107+
ClientMessage = 33;
95108

96109
}
97110

src/Guitor.winxed

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern function getfocusview;
3636
extern function getmotionview;
3737
extern function getbuttonview;
3838
extern function getkeyview;
39+
extern function getselectionview;
3940
extern function getclientview;
4041
extern function getxcolorview;
4142
extern function getxftcolorview;
@@ -1117,6 +1118,44 @@ class Window : Drawable
11171118
{
11181119
getfun("XUngrabPointer")(self.display.xdisplay, time);
11191120
}
1121+
function GetPropertyString(int property)
1122+
{
1123+
var display = self.display;
1124+
var viewint = new ["StructView"] ( [ DATATYPE_STRUCT, 1, DATATYPE_INT ] );
1125+
var viewlong = new ["StructView"] ( [ DATATYPE_STRUCT, 1, DATATYPE_LONG ] );
1126+
var viewptr = new ["StructView"] ( [ DATATYPE_STRUCT, 1, DATATYPE_PTR ] );
1127+
var actual_type_return = viewint.alloc();
1128+
var actual_format_return = viewint.alloc();
1129+
var nitems_return = viewlong.alloc();
1130+
var bytes_after_return = viewlong.alloc();
1131+
var prop_return = viewptr.alloc();
1132+
// Get 0 items, to check the size,
1133+
int r = getfun("XGetWindowProperty")(display.xdisplay, self.xdrawable,
1134+
property, 0, 0, false,
1135+
XA_STRING,
1136+
actual_type_return, actual_format_return,
1137+
nitems_return, bytes_after_return, prop_return);
1138+
if (r != Success)
1139+
return string(null);
1140+
var prop = viewptr[prop_return, 0];
1141+
Free(prop);
1142+
int n = viewint[bytes_after_return, 0];
1143+
if (n == 0)
1144+
return "";
1145+
n = (n + 3 ) / 4;
1146+
// Now get the size obtained
1147+
r = getfun("XGetWindowProperty")(display.xdisplay, self.xdrawable,
1148+
property, 0, n, true,
1149+
XA_STRING,
1150+
actual_type_return, actual_format_return, nitems_return,
1151+
bytes_after_return, prop_return);
1152+
if (r != Success)
1153+
return string(null);
1154+
prop = viewptr[prop_return, 0];
1155+
string result = prop.as_string("utf8");
1156+
Free(prop);
1157+
return result;
1158+
}
11201159
}
11211160

11221161
//**************************************************************
@@ -1229,6 +1268,7 @@ class ListenerWindow : Window
12291268
var OnLeave;
12301269
var OnFocusIn;
12311270
var OnFocusOut;
1271+
var OnSelectionNotify;
12321272
var OnClientMessage;
12331273
function ListenerWindow(controller)
12341274
{
@@ -1244,6 +1284,7 @@ class ListenerWindow : Window
12441284
self.OnLeave = new EventHandler(self, LeaveWindowMask);
12451285
self.OnFocusIn = new EventHandler(self, FocusChangeMask);
12461286
self.OnFocusOut = new EventHandler(self, FocusChangeMask);
1287+
self.OnSelectionNotify = new EventHandler(self, 0);
12471288
self.OnClientMessage = new EventHandler(self, 0);
12481289
self.controller = controller;
12491290
controller.register(self);
@@ -1294,6 +1335,9 @@ class ListenerWindow : Window
12941335
case LeaveNotify:
12951336
handler = self.OnLeave;
12961337
break;
1338+
case SelectionNotify:
1339+
handler = self.OnSelectionNotify;
1340+
break;
12971341
case ClientMessage:
12981342
handler = self.OnClientMessage;
12991343
break;
@@ -1431,6 +1475,7 @@ class EditBox : ChildWindow
14311475
self.OnKeyPress += function (event) { self.onkeypress(event); };
14321476
self.OnFocusIn += function (event) { self.onfocusin(event); };
14331477
self.OnFocusOut += function (event) { self.onfocusout(event); };
1478+
self.OnSelectionNotify += function (event) { self.onselectionnotify(event); };
14341479
}
14351480
function getValue()
14361481
{
@@ -1606,6 +1651,9 @@ class EditBox : ChildWindow
16061651
switch (key) {
16071652
case "\n": case "\r": case "\e":
16081653
return;
1654+
case "\x{16}":
1655+
self.paste(event.time());
1656+
return;
16091657
}
16101658
self.cursor();
16111659
text = substr(text, 0, pos) + key + substr(text, pos);
@@ -1622,6 +1670,40 @@ class EditBox : ChildWindow
16221670
self.drawall();
16231671
self.cursor();
16241672
}
1673+
function onselectionnotify(event)
1674+
{
1675+
var view = getselectionview();
1676+
var data = event.eventdata;
1677+
int selection = view[data, 5];
1678+
int property = view[data, 7];
1679+
if (property == None)
1680+
return;
1681+
string pasted = self.GetPropertyString(property);
1682+
if (pasted == null)
1683+
return;
1684+
int l = length(pasted);
1685+
self.cursor();
1686+
int pos = self.pos;
1687+
string text = self.text;
1688+
text = substr(text, 0, pos) + pasted + substr(text, pos);
1689+
pos += l;
1690+
self.text =: text;
1691+
self.pos =: pos;
1692+
int offset = self.offset;
1693+
int width = self.width;
1694+
int x = self.font.getTextxOff(self.display, substr(text, 0, pos));
1695+
if (x - offset >= width)
1696+
self.offset =: x - width / 4;
1697+
self.drawall();
1698+
self.cursor();
1699+
}
1700+
function paste(int time)
1701+
{
1702+
var display = self.display;
1703+
int clipboard = display.InternAtom("CLIPBOARD");
1704+
getfun("XConvertSelection")(display.xdisplay,
1705+
clipboard, XA_STRING, clipboard, self.xdrawable, time);
1706+
}
16251707
}
16261708

16271709
//**************************************************************

src/GuitorNci.winxed

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const string
2929
EXPOSEVIEW = "guitor_exposeview",
3030
FOCUSVIEW = "guitor_focusview",
3131
CROSSINGVIEW = "guitor_crossingview",
32+
SELECTIONVIEW = "guitor_selectionview",
3233
CLIENTVIEW = "guitor_clientview",
3334
XAWMHINTSVIEW = "guitor_xawmhintsview",
3435
ATOMVIEW = "guitor_atomview",
@@ -354,7 +355,24 @@ function getcrossingview()
354355
return st;
355356
}
356357

357-
358+
function getselectionview()
359+
{
360+
var ns = namespace Xlib__private;
361+
var st = ns[SELECTIONVIEW];
362+
if (st == null)
363+
ns[SELECTIONVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 9,
364+
DATATYPE_INT, // type
365+
DATATYPE_LONG, // Serial
366+
XLIB_Bool, // send event
367+
DATATYPE_PTR, // display
368+
XLIB_Window, // requestor
369+
XLIB_Atom, // selection
370+
XLIB_Atom, // target
371+
XLIB_Atom, // property
372+
XLIB_time_t // time
373+
] );
374+
return st;
375+
}
358376

359377
function getclientview()
360378
{
@@ -667,6 +685,12 @@ function create_function(string funcname)
667685
case "XUngrabPointer":
668686
sig = "vpl";
669687
break;
688+
case "XGetWindowProperty":
689+
sig = "ipiilliippppp";
690+
break;
691+
case "XConvertSelection":
692+
sig = "vpiiiil";
693+
break;
670694
default:
671695
throw "Function " + funcname + " unknown";
672696
}

0 commit comments

Comments
 (0)