fl_x.cxx
来自「SRI international 发布的OAA框架软件」· CXX 代码 · 共 1,259 行 · 第 1/3 页
CXX
1,259 行
e.xany.type = ClientMessage;
e.xany.window = window;
e.xclient.message_type = message;
e.xclient.format = 32;
e.xclient.data.l[0] = (long)d0;
e.xclient.data.l[1] = (long)d1;
e.xclient.data.l[2] = (long)d2;
e.xclient.data.l[3] = (long)d3;
e.xclient.data.l[4] = (long)d4;
XSendEvent(fl_display, window, 0, 0, &e);
}
////////////////////////////////////////////////////////////////
// Code for copying to clipboard and DnD out of the program:
void Fl::copy(const char *stuff, int len, int clipboard) {
if (!stuff || len<0) return;
if (len+1 > fl_selection_buffer_length[clipboard]) {
delete[] fl_selection_buffer[clipboard];
fl_selection_buffer[clipboard] = new char[len+100];
fl_selection_buffer_length[clipboard] = len+100;
}
memcpy(fl_selection_buffer[clipboard], stuff, len);
fl_selection_buffer[clipboard][len] = 0; // needed for direct paste
fl_selection_length[clipboard] = len;
fl_i_own_selection[clipboard] = 1;
Atom property = clipboard ? CLIPBOARD : XA_PRIMARY;
XSetSelectionOwner(fl_display, property, fl_message_window, fl_event_time);
}
////////////////////////////////////////////////////////////////
const XEvent* fl_xevent; // the current x event
ulong fl_event_time; // the last timestamp from an x event
char fl_key_vector[32]; // used by Fl::get_key()
// Record event mouse position and state from an XEvent:
static int px, py;
static ulong ptime;
static void set_event_xy() {
# if CONSOLIDATE_MOTION
send_motion = 0;
# endif
Fl::e_x_root = fl_xevent->xbutton.x_root;
Fl::e_x = fl_xevent->xbutton.x;
Fl::e_y_root = fl_xevent->xbutton.y_root;
Fl::e_y = fl_xevent->xbutton.y;
Fl::e_state = fl_xevent->xbutton.state << 16;
fl_event_time = fl_xevent->xbutton.time;
# ifdef __sgi
// get the meta key off PC keyboards:
if (fl_key_vector[18]&0x18) Fl::e_state |= FL_META;
# endif
// turn off is_click if enough time or mouse movement has passed:
if (abs(Fl::e_x_root-px)+abs(Fl::e_y_root-py) > 3 ||
fl_event_time >= ptime+1000)
Fl::e_is_click = 0;
}
// if this is same event as last && is_click, increment click count:
static inline void checkdouble() {
if (Fl::e_is_click == Fl::e_keysym)
Fl::e_clicks++;
else {
Fl::e_clicks = 0;
Fl::e_is_click = Fl::e_keysym;
}
px = Fl::e_x_root;
py = Fl::e_y_root;
ptime = fl_event_time;
}
static Fl_Window* resize_bug_fix;
extern "C" {
static Bool fake_keyup_test(Display*, XEvent* event, char* previous) {
return
event->type == KeyPress &&
event->xkey.keycode == ((XKeyEvent*)previous)->keycode &&
event->xkey.time == ((XKeyEvent*)previous)->time;
}
}
////////////////////////////////////////////////////////////////
int fl_handle(const XEvent& thisevent)
{
XEvent xevent = thisevent;
fl_xevent = &thisevent;
Window xid = xevent.xany.window;
switch (xevent.type) {
case KeymapNotify:
memcpy(fl_key_vector, xevent.xkeymap.key_vector, 32);
return 0;
case MappingNotify:
XRefreshKeyboardMapping((XMappingEvent*)&xevent.xmapping);
return 0;
case SelectionNotify: {
if (!fl_selection_requestor) return 0;
static unsigned char* buffer;
if (buffer) {XFree(buffer); buffer = 0;}
long bytesread = 0;
if (fl_xevent->xselection.property) for (;;) {
// The Xdnd code pastes 64K chunks together, possibly to avoid
// bugs in X servers, or maybe to avoid an extra round-trip to
// get the property length. I copy this here:
Atom actual; int format; unsigned long count, remaining;
unsigned char* portion;
if (XGetWindowProperty(fl_display,
fl_xevent->xselection.requestor,
fl_xevent->xselection.property,
bytesread/4, 65536, 1, 0,
&actual, &format, &count, &remaining,
&portion)) break; // quit on error
if (bytesread) { // append to the accumulated buffer
buffer = (unsigned char*)realloc(buffer, bytesread+count*format/8+remaining);
memcpy(buffer+bytesread, portion, count*format/8);
XFree(portion);
} else { // Use the first section without moving the memory:
buffer = portion;
}
bytesread += count*format/8;
if (!remaining) break;
}
Fl::e_text = (char*)buffer;
Fl::e_length = bytesread;
fl_selection_requestor->handle(FL_PASTE);
// Detect if this paste is due to Xdnd by the property name (I use
// XA_SECONDARY for that) and send an XdndFinished message. It is not
// clear if this has to be delayed until now or if it can be done
// immediatly after calling XConvertSelection.
if (fl_xevent->xselection.property == XA_SECONDARY &&
fl_dnd_source_window) {
fl_sendClientMessage(fl_dnd_source_window, fl_XdndFinished,
fl_xevent->xselection.requestor);
fl_dnd_source_window = 0; // don't send a second time
}
return 1;}
case SelectionClear: {
int clipboard = fl_xevent->xselectionclear.selection == CLIPBOARD;
fl_i_own_selection[clipboard] = 0;
return 1;}
case SelectionRequest: {
XSelectionEvent e;
e.type = SelectionNotify;
e.requestor = fl_xevent->xselectionrequest.requestor;
e.selection = fl_xevent->xselectionrequest.selection;
int clipboard = e.selection == CLIPBOARD;
e.target = fl_xevent->xselectionrequest.target;
e.time = fl_xevent->xselectionrequest.time;
e.property = fl_xevent->xselectionrequest.property;
if (e.target == TARGETS) {
Atom a = XA_STRING;
XChangeProperty(fl_display, e.requestor, e.property,
XA_ATOM, sizeof(Atom)*8, 0, (unsigned char*)&a,
sizeof(Atom));
} else if (/*e.target == XA_STRING &&*/ fl_selection_length[clipboard]) {
XChangeProperty(fl_display, e.requestor, e.property,
e.target, 8, 0,
(unsigned char *)fl_selection_buffer[clipboard],
fl_selection_length[clipboard]);
} else {
// char* x = XGetAtomName(fl_display,e.target);
// fprintf(stderr,"selection request of %s\n",x);
// XFree(x);
e.property = 0;
}
XSendEvent(fl_display, e.requestor, 0, 0, (XEvent *)&e);}
return 1;
// events where interesting window id is in a different place:
case CirculateNotify:
case CirculateRequest:
case ConfigureNotify:
case ConfigureRequest:
case CreateNotify:
case DestroyNotify:
case GravityNotify:
case MapNotify:
case MapRequest:
case ReparentNotify:
case UnmapNotify:
xid = xevent.xmaprequest.window;
break;
}
int event = 0;
Fl_Window* window = fl_find(xid);
if (window) switch (xevent.type) {
case ClientMessage: {
Atom message = fl_xevent->xclient.message_type;
const long* data = fl_xevent->xclient.data.l;
if ((Atom)(data[0]) == WM_DELETE_WINDOW) {
event = FL_CLOSE;
} else if (message == fl_XdndEnter) {
fl_xmousewin = window;
fl_dnd_source_window = data[0];
// version number is data[1]>>24
if (data[1]&1) {
// get list of data types:
Atom actual; int format; unsigned long count, remaining;
unsigned char *buffer = 0;
XGetWindowProperty(fl_display, fl_dnd_source_window, fl_XdndTypeList,
0, 0x8000000L, False, XA_ATOM, &actual, &format,
&count, &remaining, &buffer);
if (actual != XA_ATOM || format != 32 || count<4 || !buffer)
goto FAILED;
delete [] fl_dnd_source_types;
fl_dnd_source_types = new Atom[count+1];
for (unsigned i = 0; i < count; i++)
fl_dnd_source_types[i] = ((Atom*)buffer)[i];
fl_dnd_source_types[count] = 0;
} else {
FAILED:
// less than four data types, or if the above messes up:
if (!fl_dnd_source_types) fl_dnd_source_types = new Atom[4];
fl_dnd_source_types[0] = data[2];
fl_dnd_source_types[1] = data[3];
fl_dnd_source_types[2] = data[4];
fl_dnd_source_types[3] = 0;
}
fl_dnd_type = fl_dnd_source_types[0]; // should pick text or url
event = FL_DND_ENTER;
break;
} else if (message == fl_XdndPosition) {
fl_xmousewin = window;
fl_dnd_source_window = data[0];
Fl::e_x_root = data[2]>>16;
Fl::e_y_root = data[2]&0xFFFF;
if (window) {
Fl::e_x = Fl::e_x_root-window->x();
Fl::e_y = Fl::e_y_root-window->y();
}
fl_event_time = data[3];
fl_dnd_source_action = data[4];
fl_dnd_action = fl_XdndActionCopy;
int accept = Fl::handle(FL_DND_DRAG, window);
fl_sendClientMessage(data[0], fl_XdndStatus,
fl_xevent->xclient.window,
accept ? 1 : 0,
0, // used for xy rectangle to not send position inside
0, // used for width+height of rectangle
accept ? fl_dnd_action : None);
return 1;
} else if (message == fl_XdndLeave) {
fl_dnd_source_window = 0; // don't send a finished message to it
event = FL_DND_LEAVE;
break;
} else if (message == fl_XdndDrop) {
fl_xmousewin = window;
fl_dnd_source_window = data[0];
fl_event_time = data[2];
Window to_window = fl_xevent->xclient.window;
if (Fl::handle(FL_DND_RELEASE, window)) {
fl_selection_requestor = Fl::belowmouse();
XConvertSelection(fl_display, fl_XdndSelection,
fl_dnd_type, XA_SECONDARY,
to_window, fl_event_time);
} else {
// Send the finished message if I refuse the drop.
// It is not clear whether I can just send finished always,
// or if I have to wait for the SelectionNotify event as the
// code is currently doing.
fl_sendClientMessage(fl_dnd_source_window, fl_XdndFinished, to_window);
fl_dnd_source_window = 0;
}
return 1;
}
break;}
case UnmapNotify:
event = FL_HIDE;
break;
case Expose:
Fl_X::i(window)->wait_for_expose = 0;
# if 0
// try to keep windows on top even if WM_TRANSIENT_FOR does not work:
// opaque move/resize window managers do not like this, so I disabled it.
if (Fl::first_window()->non_modal() && window != Fl::first_window())
Fl::first_window()->show();
# endif
case GraphicsExpose:
window->damage(FL_DAMAGE_EXPOSE, xevent.xexpose.x, xevent.xexpose.y,
xevent.xexpose.width, xevent.xexpose.height);
return 1;
case FocusIn:
event = FL_FOCUS;
break;
case FocusOut:
event = FL_UNFOCUS;
break;
case KeyPress:
case KeyRelease: {
KEYPRESS:
int keycode = xevent.xkey.keycode;
fl_key_vector[keycode/8] |= (1 << (keycode%8));
static char buffer[21];
int len;
KeySym keysym;
if (xevent.type == KeyPress) {
event = FL_KEYDOWN;
//static XComposeStatus compose;
len = XLookupString((XKeyEvent*)&(xevent.xkey),
buffer, 20, &keysym, 0/*&compose*/);
if (keysym && keysym < 0x400) { // a character in latin-1,2,3,4 sets
// force it to type a character (not sure if this ever is needed):
if (!len) {buffer[0] = char(keysym); len = 1;}
// ignore all effects of shift on the keysyms, which makes it a lot
// easier to program shortcuts and is Windoze-compatable:
keysym = XKeycodeToKeysym(fl_display, keycode, 0);
}
if (Fl::event_state(FL_CTRL) && keysym == '-') buffer[0] = 0x1f; // ^_
buffer[len] = 0;
Fl::e_text = buffer;
Fl::e_length = len;
} else {
// Stupid X sends fake key-up events when a repeating key is held
// down, probably due to some back compatability problem. Fortunatley
// we can detect this because the repeating KeyPress event is in
// the queue, get it and execute it instead:
XEvent temp;
if (XCheckIfEvent(fl_display,&temp,fake_keyup_test,(char*)(&xevent))){
xevent = temp;
goto KEYPRESS;
}
event = FL_KEYUP;
fl_key_vector[keycode/8] &= ~(1 << (keycode%8));
// keyup events just get the unshifted keysym:
keysym = XKeycodeToKeysym(fl_display, keycode, 0);
}
# ifdef __sgi
// You can plug a microsoft keyboard into an sgi but the extra shift
// keys are not translated. Make them translate like XFree86 does:
if (!keysym) switch(keycode) {
case 147: keysym = FL_Meta_L; break;
case 148: keysym = FL_Meta_R; break;
case 149: keysym = FL_Menu; break;
}
# endif
# if BACKSPACE_HACK
// Attempt to fix keyboards that send "delete" for the key in the
// upper-right corner of the main keyboard. But it appears that
// very few of these remain?
static int got_backspace;
if (!got_backspace) {
if (keysym == FL_Delete) keysym = FL_BackSpace;
else if (keysym == FL_BackSpace) got_backspace = 1;
}
# endif
// We have to get rid of the XK_KP_function keys, because they are
// not produced on Windoze and thus case statements tend not to check
// for them. There are 15 of these in the range 0xff91 ... 0xff9f
if (keysym >= 0xff91 && keysym <= 0xff9f) {
// Try to make them turn into FL_KP+'c' so that NumLock is
// irrelevant, by looking at the shifted code. This matches the
// behavior of the translator in Fl_win32.cxx, and IMHO is the
// user-friendly result:
unsigned long keysym1 = XKeycodeToKeysym(fl_display, keycode, 1);
if (keysym1 <= 0x7f || keysym1 > 0xff9f && keysym1 <= FL_KP_Last) {
keysym = keysym1 | FL_KP;
buffer[0] = char(keysym1) & 0x7F;
len = 1;
} else {
// If that failed to work, just translate them to the matching
// normal function keys:
static const unsigned short table[15] = {
FL_F+1, FL_F+2, FL_F+3, FL_F+4,
FL_Home, FL_Left, FL_Up, FL_Right,
FL_Down, FL_Page_Up, FL_Page_Down, FL_End,
0xff0b/*XK_Clear*/, FL_Insert, FL_Delete};
keysym = table[keysym-0xff91];
}
}
Fl::e_keysym = int(keysym);
set_event_xy();
Fl::e_is_click = 0;
break;}
case ButtonPress:
Fl::e_keysym = FL_Button + xevent.xbutton.button;
set_event_xy();
if (xevent.xbutton.button == Button4) {
Fl::e_dy = -1; // Up
event = FL_MOUSEWHEEL;
} else if (xevent.xbutton.button == Button5) {
Fl::e_dy = +1; // Down
event = FL_MOUSEWHEEL;
} else {
Fl::e_state |= (FL_BUTTON1 << (xevent.xbutton.button-1));
event = FL_PUSH;
checkdouble();
}
fl_xmousewin = window;
break;
case MotionNotify:
set_event_xy();
# if CONSOLIDATE_MOTION
send_motion = fl_xmousewin = window;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?