wmaker: add miniwindow apercu
[wmaker-crm.git] / WINGs / wtextfield.c
blobadf90c4d0c41d3f11d5d9eaf8e4c4ab1eed81489
2 #include "WINGsP.h"
3 #include "wconfig.h"
5 #include <X11/keysym.h>
6 #include <X11/Xatom.h>
8 #include <ctype.h>
10 #define CURSOR_BLINK_ON_DELAY 600
11 #define CURSOR_BLINK_OFF_DELAY 300
13 char *WMTextDidChangeNotification = "WMTextDidChangeNotification";
14 char *WMTextDidBeginEditingNotification = "WMTextDidBeginEditingNotification";
15 char *WMTextDidEndEditingNotification = "WMTextDidEndEditingNotification";
17 typedef struct W_TextField {
18 W_Class widgetClass;
19 W_View *view;
21 #if 0
22 struct W_TextField *nextField; /* next textfield in the chain */
23 struct W_TextField *prevField;
24 #endif
26 char *text;
27 int textLen; /* size of text */
28 int bufferSize; /* memory allocated for text */
30 int viewPosition; /* position of text being shown */
32 int cursorPosition; /* position of the insertion cursor */
34 short usableWidth;
35 short offsetWidth; /* offset of text from border */
37 WMRange selection;
39 WMFont *font;
41 WMTextFieldDelegate *delegate;
43 WMHandlerID timerID; /* for cursor blinking */
45 struct {
46 WMAlignment alignment:2;
48 unsigned int bordered:1;
50 unsigned int beveled:1;
52 unsigned int enabled:1;
54 unsigned int focused:1;
56 unsigned int cursorOn:1;
58 unsigned int secure:1; /* password entry style */
60 unsigned int pointerGrabbed:1;
62 unsigned int ownsSelection:1;
64 unsigned int waitingSelection:1; /* requested selection, but
65 * didnt get yet */
67 unsigned int notIllegalMovement:1;
68 } flags;
69 } TextField;
71 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
72 if ((T)->delegate && (T)->delegate->C)\
73 (*(T)->delegate->C)((T)->delegate,notif);\
74 WMPostNotification(notif);\
75 WMReleaseNotification(notif);}
77 #define MIN_TEXT_BUFFER 2
78 #define TEXT_BUFFER_INCR 8
80 #define WM_EMACSKEYMASK ControlMask
82 #define WM_EMACSKEY_LEFT XK_b
83 #define WM_EMACSKEY_RIGHT XK_f
84 #define WM_EMACSKEY_HOME XK_a
85 #define WM_EMACSKEY_END XK_e
86 #define WM_EMACSKEY_BS XK_h
87 #define WM_EMACSKEY_DEL XK_d
89 #define DEFAULT_WIDTH 60
90 #define DEFAULT_HEIGHT 20
91 #define DEFAULT_BORDERED True
92 #define DEFAULT_ALIGNMENT WALeft
94 static void destroyTextField(TextField * tPtr);
95 static void paintTextField(TextField * tPtr);
97 static void handleEvents(XEvent * event, void *data);
98 static void handleTextFieldActionEvents(XEvent * event, void *data);
99 static void didResizeTextField(W_ViewDelegate * self, WMView * view);
101 struct W_ViewDelegate _TextFieldViewDelegate = {
102 NULL,
103 NULL,
104 didResizeTextField,
105 NULL,
106 NULL
109 static void lostSelection(WMView * view, Atom selection, void *cdata);
111 static WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type);
113 static WMSelectionProcs selectionHandler = {
114 requestHandler,
115 lostSelection,
116 NULL
119 #define TEXT_WIDTH(tPtr, start) (WMWidthOfString((tPtr)->font, \
120 &((tPtr)->text[(start)]), (tPtr)->textLen - (start)))
122 #define TEXT_WIDTH2(tPtr, start, end) (WMWidthOfString((tPtr)->font, \
123 &((tPtr)->text[(start)]), (end) - (start)))
125 static inline int oneUTF8CharBackward(const char *str, int len)
127 const unsigned char *ustr = (const unsigned char *)str;
128 int pos = 0;
130 while (len-- > 0 && ustr[--pos] >= 0x80 && ustr[pos] <= 0xbf) ;
131 return pos;
134 static inline int oneUTF8CharForward(const char *str, int len)
136 const unsigned char *ustr = (const unsigned char *)str;
137 int pos = 0;
139 while (len-- > 0 && ustr[++pos] >= 0x80 && ustr[pos] <= 0xbf) ;
140 return pos;
143 // find the beginning of the UTF8 char pointed by str
144 static inline int seekUTF8CharStart(const char *str, int len)
146 const unsigned char *ustr = (const unsigned char *)str;
147 int pos = 0;
149 while (len-- > 0 && ustr[pos] >= 0x80 && ustr[pos] <= 0xbf)
150 --pos;
151 return pos;
154 static void normalizeRange(TextField * tPtr, WMRange * range)
156 if (range->position < 0 && range->count < 0)
157 range->count = 0;
159 if (range->count == 0) {
160 /*range->position = 0; why is this? */
161 return;
164 /* (1,-2) ~> (0,1) ; (1,-1) ~> (0,1) ; (2,-1) ~> (1,1) */
165 if (range->count < 0) { /* && range->position >= 0 */
166 if (range->position + range->count < 0) {
167 range->count = range->position;
168 range->position = 0;
169 } else {
170 range->count = -range->count;
171 range->position -= range->count;
173 /* (-2,1) ~> (0,0) ; (-1,1) ~> (0,0) ; (-1,2) ~> (0,1) */
174 } else if (range->position < 0) { /* && range->count > 0 */
175 if (range->position + range->count < 0) {
176 range->position = range->count = 0;
177 } else {
178 range->count += range->position;
179 range->position = 0;
183 if (range->position + range->count > tPtr->textLen)
184 range->count = tPtr->textLen - range->position;
187 static void memmv(char *dest, const char *src, int size)
189 int i;
191 if (dest > src) {
192 for (i = size - 1; i >= 0; i--) {
193 dest[i] = src[i];
195 } else if (dest < src) {
196 for (i = 0; i < size; i++) {
197 dest[i] = src[i];
202 static int incrToFit(TextField * tPtr)
204 int vp = tPtr->viewPosition;
206 while (TEXT_WIDTH(tPtr, tPtr->viewPosition) > tPtr->usableWidth) {
207 tPtr->viewPosition += oneUTF8CharForward(&tPtr->text[tPtr->viewPosition],
208 tPtr->textLen - tPtr->viewPosition);
210 return vp != tPtr->viewPosition;
213 static int incrToFit2(TextField * tPtr)
215 int vp = tPtr->viewPosition;
217 while (TEXT_WIDTH2(tPtr, tPtr->viewPosition, tPtr->cursorPosition)
218 >= tPtr->usableWidth)
219 tPtr->viewPosition += oneUTF8CharForward(&tPtr->text[tPtr->viewPosition],
220 tPtr->cursorPosition - tPtr->viewPosition);
221 return vp != tPtr->viewPosition;
224 static void decrToFit(TextField * tPtr)
226 int vp = tPtr->viewPosition;
228 while (vp > 0 && (vp += oneUTF8CharBackward(&tPtr->text[vp], vp),
229 TEXT_WIDTH(tPtr, vp)) < tPtr->usableWidth) {
230 tPtr->viewPosition = vp;
234 #undef TEXT_WIDTH
235 #undef TEXT_WIDTH2
237 static WMData *requestHandler(WMView * view, Atom selection, Atom target, void *cdata, Atom * type)
239 TextField *tPtr = view->self;
240 int count;
241 Display *dpy = tPtr->view->screen->display;
242 Atom _TARGETS;
243 Atom TEXT = XInternAtom(dpy, "TEXT", False);
244 Atom COMPOUND_TEXT = XInternAtom(dpy, "COMPOUND_TEXT", False);
245 WMData *data;
247 /* Parameter not used, but tell the compiler that it is ok */
248 (void) selection;
249 (void) cdata;
251 count = tPtr->selection.count < 0
252 ? tPtr->selection.position + tPtr->selection.count : tPtr->selection.position;
254 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
256 data = WMCreateDataWithBytes(&(tPtr->text[count]), abs(tPtr->selection.count));
257 WMSetDataFormat(data, 8);
258 *type = target;
260 return data;
263 _TARGETS = XInternAtom(dpy, "TARGETS", False);
264 if (target == _TARGETS) {
265 Atom supported_type[4];
267 supported_type[0] = _TARGETS;
268 supported_type[1] = XA_STRING;
269 supported_type[2] = TEXT;
270 supported_type[3] = COMPOUND_TEXT;
272 data = WMCreateDataWithBytes(supported_type, sizeof(supported_type));
273 WMSetDataFormat(data, 32);
275 *type = target;
276 return data;
279 return NULL;
283 static void lostSelection(WMView * view, Atom selection, void *cdata)
285 TextField *tPtr = (WMTextField *) view->self;
287 /* Parameter not used, but tell the compiler that it is ok */
288 (void) cdata;
290 if (tPtr->flags.ownsSelection) {
291 WMDeleteSelectionHandler(view, selection, CurrentTime);
292 tPtr->flags.ownsSelection = 0;
294 if (tPtr->selection.count != 0) {
295 tPtr->selection.count = 0;
296 paintTextField(tPtr);
300 static void selectionNotification(void *observerData, WMNotification * notification)
302 WMView *observerView = (WMView *) observerData;
303 WMView *newOwnerView = (WMView *) WMGetNotificationClientData(notification);
305 if (observerView != newOwnerView) {
307 //if (tPtr->flags.ownsSelection)
308 // WMDeleteSelectionHandler(observerView, XA_PRIMARY, CurrentTime);
310 lostSelection(observerView, XA_PRIMARY, NULL);
314 static void realizeObserver(void *self, WMNotification * not)
316 /* Parameter not used, but tell the compiler that it is ok */
317 (void) not;
319 W_CreateIC(((TextField *) self)->view);
322 WMTextField *WMCreateTextField(WMWidget * parent)
324 TextField *tPtr;
326 tPtr = wmalloc(sizeof(TextField));
327 tPtr->widgetClass = WC_TextField;
329 tPtr->view = W_CreateView(W_VIEW(parent));
330 if (!tPtr->view) {
331 wfree(tPtr);
332 return NULL;
334 tPtr->view->self = tPtr;
336 tPtr->view->delegate = &_TextFieldViewDelegate;
338 tPtr->view->attribFlags |= CWCursor;
339 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
341 W_SetViewBackgroundColor(tPtr->view, tPtr->view->screen->white);
343 tPtr->text = wmalloc(MIN_TEXT_BUFFER);
344 tPtr->textLen = 0;
345 tPtr->bufferSize = MIN_TEXT_BUFFER;
347 tPtr->flags.enabled = 1;
349 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask | FocusChangeMask, handleEvents, tPtr);
351 tPtr->font = WMRetainFont(tPtr->view->screen->normalFont);
353 tPtr->flags.bordered = DEFAULT_BORDERED;
354 tPtr->flags.beveled = True;
355 tPtr->flags.alignment = DEFAULT_ALIGNMENT;
356 tPtr->offsetWidth = WMAX((tPtr->view->size.height - WMFontHeight(tPtr->font)) / 2, 1);
358 W_ResizeView(tPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
360 WMCreateEventHandler(tPtr->view, EnterWindowMask | LeaveWindowMask
361 | ButtonReleaseMask | ButtonPressMask | KeyPressMask | Button1MotionMask,
362 handleTextFieldActionEvents, tPtr);
364 WMAddNotificationObserver(selectionNotification, tPtr->view,
365 WMSelectionOwnerDidChangeNotification, (void *)XA_PRIMARY);
367 WMAddNotificationObserver(realizeObserver, tPtr, WMViewRealizedNotification, tPtr->view);
369 tPtr->flags.cursorOn = 1;
371 return tPtr;
374 void WMSetTextFieldDelegate(WMTextField * tPtr, WMTextFieldDelegate * delegate)
376 CHECK_CLASS(tPtr, WC_TextField);
378 tPtr->delegate = delegate;
381 WMTextFieldDelegate *WMGetTextFieldDelegate(WMTextField * tPtr)
383 CHECK_CLASS(tPtr, WC_TextField);
385 return tPtr->delegate;
388 void WMInsertTextFieldText(WMTextField * tPtr, const char *text, int position)
390 int len;
392 CHECK_CLASS(tPtr, WC_TextField);
394 if (!text)
395 return;
397 len = strlen(text);
399 /* check if buffer will hold the text */
400 if (len + tPtr->textLen >= tPtr->bufferSize) {
401 tPtr->bufferSize = tPtr->textLen + len + TEXT_BUFFER_INCR;
402 tPtr->text = wrealloc(tPtr->text, tPtr->bufferSize);
405 if (position < 0 || position >= tPtr->textLen) {
406 /* append the text at the end */
407 wstrlcat(tPtr->text, text, tPtr->bufferSize);
408 tPtr->textLen += len;
409 tPtr->cursorPosition += len;
410 incrToFit(tPtr);
411 } else {
412 /* insert text at position */
413 memmv(&(tPtr->text[position + len]), &(tPtr->text[position]), tPtr->textLen - position + 1);
415 memcpy(&(tPtr->text[position]), text, len);
417 tPtr->textLen += len;
418 if (position >= tPtr->cursorPosition) {
419 tPtr->cursorPosition += len;
420 incrToFit2(tPtr);
421 } else {
422 incrToFit(tPtr);
426 paintTextField(tPtr);
429 void WMDeleteTextFieldRange(WMTextField * tPtr, WMRange range)
431 CHECK_CLASS(tPtr, WC_TextField);
433 normalizeRange(tPtr, &range);
435 if (!range.count)
436 return;
438 memmv(&(tPtr->text[range.position]), &(tPtr->text[range.position + range.count]),
439 tPtr->textLen - (range.position + range.count) + 1);
441 /* better than nothing ;) */
442 if (tPtr->cursorPosition > range.position)
443 tPtr->viewPosition += oneUTF8CharBackward(&tPtr->text[tPtr->viewPosition], tPtr->viewPosition);
444 tPtr->textLen -= range.count;
445 tPtr->cursorPosition = range.position;
447 decrToFit(tPtr);
449 paintTextField(tPtr);
452 char *WMGetTextFieldText(WMTextField * tPtr)
454 CHECK_CLASS(tPtr, WC_TextField);
456 return wstrdup(tPtr->text);
459 void WMSetTextFieldText(WMTextField * tPtr, const char *text)
461 CHECK_CLASS(tPtr, WC_TextField);
463 if ((text && strcmp(tPtr->text, text) == 0) || (!text && tPtr->textLen == 0))
464 return;
466 if (text == NULL) {
467 tPtr->text[0] = 0;
468 tPtr->textLen = 0;
469 } else {
470 tPtr->textLen = strlen(text);
472 if (tPtr->textLen >= tPtr->bufferSize) {
473 tPtr->bufferSize = tPtr->textLen + TEXT_BUFFER_INCR;
474 tPtr->text = wrealloc(tPtr->text, tPtr->bufferSize);
476 wstrlcpy(tPtr->text, text, tPtr->bufferSize);
479 tPtr->cursorPosition = tPtr->selection.position = tPtr->textLen;
480 tPtr->viewPosition = 0;
481 tPtr->selection.count = 0;
483 if (tPtr->view->flags.realized)
484 paintTextField(tPtr);
487 void WMSetTextFieldAlignment(WMTextField * tPtr, WMAlignment alignment)
489 CHECK_CLASS(tPtr, WC_TextField);
491 tPtr->flags.alignment = alignment;
493 if (alignment != WALeft) {
494 wwarning("only left alignment is supported in textfields");
495 return;
498 if (tPtr->view->flags.realized) {
499 paintTextField(tPtr);
503 void WMSetTextFieldBordered(WMTextField * tPtr, Bool bordered)
505 CHECK_CLASS(tPtr, WC_TextField);
507 tPtr->flags.bordered = bordered;
509 if (tPtr->view->flags.realized) {
510 paintTextField(tPtr);
514 void WMSetTextFieldBeveled(WMTextField * tPtr, Bool flag)
516 CHECK_CLASS(tPtr, WC_TextField);
518 tPtr->flags.beveled = ((flag == 0) ? 0 : 1);
520 if (tPtr->view->flags.realized) {
521 paintTextField(tPtr);
525 void WMSetTextFieldSecure(WMTextField * tPtr, Bool flag)
527 CHECK_CLASS(tPtr, WC_TextField);
529 tPtr->flags.secure = ((flag == 0) ? 0 : 1);
531 if (tPtr->view->flags.realized) {
532 paintTextField(tPtr);
536 Bool WMGetTextFieldEditable(WMTextField * tPtr)
538 CHECK_CLASS(tPtr, WC_TextField);
540 return tPtr->flags.enabled;
543 void WMSetTextFieldEditable(WMTextField * tPtr, Bool flag)
545 CHECK_CLASS(tPtr, WC_TextField);
547 tPtr->flags.enabled = ((flag == 0) ? 0 : 1);
549 if (tPtr->view->flags.realized) {
550 paintTextField(tPtr);
554 void WMSelectTextFieldRange(WMTextField * tPtr, WMRange range)
556 CHECK_CLASS(tPtr, WC_TextField);
558 if (tPtr->flags.enabled) {
559 normalizeRange(tPtr, &range);
561 tPtr->selection = range;
563 tPtr->cursorPosition = range.position + range.count;
565 if (tPtr->view->flags.realized) {
566 paintTextField(tPtr);
571 void WMSetTextFieldCursorPosition(WMTextField * tPtr, unsigned int position)
573 CHECK_CLASS(tPtr, WC_TextField);
575 if (tPtr->flags.enabled) {
576 if (position > tPtr->textLen)
577 position = tPtr->textLen;
579 tPtr->cursorPosition = position;
580 if (tPtr->view->flags.realized) {
581 paintTextField(tPtr);
586 unsigned WMGetTextFieldCursorPosition(WMTextField *tPtr)
588 CHECK_CLASS(tPtr, WC_TextField);
590 return tPtr->cursorPosition;
593 void WMSetTextFieldNextTextField(WMTextField * tPtr, WMTextField * next)
595 CHECK_CLASS(tPtr, WC_TextField);
596 if (next == NULL) {
597 if (tPtr->view->nextFocusChain)
598 tPtr->view->nextFocusChain->prevFocusChain = NULL;
599 tPtr->view->nextFocusChain = NULL;
600 return;
603 CHECK_CLASS(next, WC_TextField);
605 if (tPtr->view->nextFocusChain)
606 tPtr->view->nextFocusChain->prevFocusChain = NULL;
607 if (next->view->prevFocusChain)
608 next->view->prevFocusChain->nextFocusChain = NULL;
610 tPtr->view->nextFocusChain = next->view;
611 next->view->prevFocusChain = tPtr->view;
614 void WMSetTextFieldPrevTextField(WMTextField * tPtr, WMTextField * prev)
616 CHECK_CLASS(tPtr, WC_TextField);
617 if (prev == NULL) {
618 if (tPtr->view->prevFocusChain)
619 tPtr->view->prevFocusChain->nextFocusChain = NULL;
620 tPtr->view->prevFocusChain = NULL;
621 return;
624 CHECK_CLASS(prev, WC_TextField);
626 if (tPtr->view->prevFocusChain)
627 tPtr->view->prevFocusChain->nextFocusChain = NULL;
628 if (prev->view->nextFocusChain)
629 prev->view->nextFocusChain->prevFocusChain = NULL;
631 tPtr->view->prevFocusChain = prev->view;
632 prev->view->nextFocusChain = tPtr->view;
635 void WMSetTextFieldFont(WMTextField * tPtr, WMFont * font)
637 CHECK_CLASS(tPtr, WC_TextField);
639 if (tPtr->font)
640 WMReleaseFont(tPtr->font);
641 tPtr->font = WMRetainFont(font);
643 tPtr->offsetWidth = WMAX((tPtr->view->size.height - WMFontHeight(tPtr->font)) / 2, 1);
645 if (tPtr->view->flags.realized) {
646 paintTextField(tPtr);
650 WMFont *WMGetTextFieldFont(WMTextField * tPtr)
652 return tPtr->font;
655 static void didResizeTextField(W_ViewDelegate * self, WMView * view)
657 WMTextField *tPtr = (WMTextField *) view->self;
659 /* Parameter not used, but tell the compiler that it is ok */
660 (void) self;
662 tPtr->offsetWidth = WMAX((tPtr->view->size.height - WMFontHeight(tPtr->font)) / 2, 1);
664 tPtr->usableWidth = tPtr->view->size.width - 2 * tPtr->offsetWidth /*+ 2 */ ;
667 static char *makeHiddenString(int length)
669 char *data = wmalloc(length + 1);
671 memset(data, '*', length);
672 data[length] = '\0';
674 return data;
677 static void paintCursor(TextField * tPtr)
679 int cx;
680 WMScreen *screen = tPtr->view->screen;
681 int textWidth;
682 char *text;
684 if (tPtr->flags.secure)
685 text = makeHiddenString(strlen(tPtr->text));
686 else
687 text = tPtr->text;
689 cx = WMWidthOfString(tPtr->font, &(text[tPtr->viewPosition]), tPtr->cursorPosition - tPtr->viewPosition);
691 switch (tPtr->flags.alignment) {
692 case WARight:
693 textWidth = WMWidthOfString(tPtr->font, text, tPtr->textLen);
694 if (textWidth < tPtr->usableWidth)
695 cx += tPtr->offsetWidth + tPtr->usableWidth - textWidth + 1;
696 else
697 cx += tPtr->offsetWidth + 1;
698 break;
699 case WALeft:
700 cx += tPtr->offsetWidth + 1;
701 break;
702 case WAJustified:
703 /* not supported */
704 case WACenter:
705 textWidth = WMWidthOfString(tPtr->font, text, tPtr->textLen);
706 if (textWidth < tPtr->usableWidth)
707 cx += tPtr->offsetWidth + (tPtr->usableWidth - textWidth) / 2;
708 else
709 cx += tPtr->offsetWidth;
710 break;
713 XDrawRectangle(screen->display, tPtr->view->window, screen->xorGC,
714 cx, tPtr->offsetWidth, 1,
715 tPtr->view->size.height - 2*tPtr->offsetWidth - 1);
716 printf("%d %d\n",cx,tPtr->cursorPosition);
719 XDrawLine(screen->display, tPtr->view->window, screen->xorGC,
720 cx, tPtr->offsetWidth, cx, tPtr->view->size.height - tPtr->offsetWidth - 1);
722 W_SetPreeditPositon(tPtr->view, cx, 0);
724 if (tPtr->flags.secure) {
725 wfree(text);
729 static void drawRelief(WMView * view, Bool beveled)
731 WMScreen *scr = view->screen;
732 Display *dpy = scr->display;
733 GC wgc;
734 GC lgc;
735 GC dgc;
736 int width = view->size.width;
737 int height = view->size.height;
739 dgc = WMColorGC(scr->darkGray);
741 if (!beveled) {
742 XDrawRectangle(dpy, view->window, dgc, 0, 0, width - 1, height - 1);
744 return;
746 wgc = WMColorGC(scr->white);
747 lgc = WMColorGC(scr->gray);
749 /* top left */
750 XDrawLine(dpy, view->window, dgc, 0, 0, width - 1, 0);
751 XDrawLine(dpy, view->window, dgc, 0, 1, width - 2, 1);
753 XDrawLine(dpy, view->window, dgc, 0, 0, 0, height - 2);
754 XDrawLine(dpy, view->window, dgc, 1, 0, 1, height - 3);
756 /* bottom right */
757 XDrawLine(dpy, view->window, wgc, 0, height - 1, width - 1, height - 1);
758 XDrawLine(dpy, view->window, lgc, 1, height - 2, width - 2, height - 2);
760 XDrawLine(dpy, view->window, wgc, width - 1, 0, width - 1, height - 1);
761 XDrawLine(dpy, view->window, lgc, width - 2, 1, width - 2, height - 3);
764 static void paintTextField(TextField * tPtr)
766 W_Screen *screen = tPtr->view->screen;
767 W_View *view = tPtr->view;
768 W_View viewbuffer;
769 int tx, ty, tw;
770 int rx;
771 int bd;
772 int totalWidth;
773 char *text;
774 Pixmap drawbuffer;
775 WMColor *color;
777 if (!view->flags.realized || !view->flags.mapped)
778 return;
780 if (!tPtr->flags.bordered) {
781 bd = 0;
782 } else {
783 bd = 2;
786 if (tPtr->flags.secure) {
787 text = makeHiddenString(strlen(tPtr->text));
788 } else {
789 text = tPtr->text;
792 totalWidth = tPtr->view->size.width - 2 * bd;
794 drawbuffer = XCreatePixmap(screen->display, view->window,
795 view->size.width, view->size.height, screen->depth);
796 XFillRectangle(screen->display, drawbuffer, WMColorGC(screen->white),
797 0, 0, view->size.width, view->size.height);
798 /* this is quite dirty */
799 viewbuffer.screen = view->screen;
800 viewbuffer.size = view->size;
801 viewbuffer.window = drawbuffer;
803 if (tPtr->textLen > 0) {
804 tw = WMWidthOfString(tPtr->font, &(text[tPtr->viewPosition]), tPtr->textLen - tPtr->viewPosition);
806 ty = tPtr->offsetWidth;
807 switch (tPtr->flags.alignment) {
808 case WALeft:
809 tx = tPtr->offsetWidth + 1;
810 if (tw < tPtr->usableWidth)
811 XFillRectangle(screen->display, drawbuffer,
812 WMColorGC(screen->white),
813 bd + tw, bd, totalWidth - tw, view->size.height - 2 * bd);
814 break;
816 case WACenter:
817 tx = tPtr->offsetWidth + (tPtr->usableWidth - tw) / 2;
818 if (tw < tPtr->usableWidth)
819 XClearArea(screen->display, view->window, bd, bd,
820 totalWidth, view->size.height - 2 * bd, False);
821 break;
823 default:
824 case WARight:
825 tx = tPtr->offsetWidth + tPtr->usableWidth - tw - 1;
826 if (tw < tPtr->usableWidth)
827 XClearArea(screen->display, view->window, bd, bd,
828 totalWidth - tw, view->size.height - 2 * bd, False);
829 break;
832 color = tPtr->flags.enabled ? screen->black : screen->darkGray;
834 WMDrawImageString(screen, drawbuffer, color, screen->white,
835 tPtr->font, tx, ty, &(text[tPtr->viewPosition]),
836 tPtr->textLen - tPtr->viewPosition);
838 if (tPtr->selection.count) {
839 int count, count2;
841 count = tPtr->selection.count < 0
842 ? tPtr->selection.position + tPtr->selection.count : tPtr->selection.position;
843 count2 = abs(tPtr->selection.count);
844 if (count < tPtr->viewPosition) {
845 count2 = abs(count2 - abs(tPtr->viewPosition - count));
846 count = tPtr->viewPosition;
849 rx = tPtr->offsetWidth + 1 + WMWidthOfString(tPtr->font, text, count)
850 - WMWidthOfString(tPtr->font, text, tPtr->viewPosition);
852 WMDrawImageString(screen, drawbuffer, color, screen->gray,
853 tPtr->font, rx, ty, &(text[count]), count2);
855 } else {
856 XFillRectangle(screen->display, drawbuffer, WMColorGC(screen->white),
857 bd, bd, totalWidth, view->size.height - 2 * bd);
860 /* draw relief */
861 if (tPtr->flags.bordered) {
862 drawRelief(&viewbuffer, tPtr->flags.beveled);
865 if (tPtr->flags.secure)
866 wfree(text);
867 XCopyArea(screen->display, drawbuffer, view->window,
868 screen->copyGC, 0, 0, view->size.width, view->size.height, 0, 0);
869 XFreePixmap(screen->display, drawbuffer);
871 /* draw cursor */
872 if (tPtr->flags.focused && tPtr->flags.enabled && tPtr->flags.cursorOn) {
873 paintCursor(tPtr);
877 static void blinkCursor(void *data)
879 TextField *tPtr = (TextField *) data;
881 if (tPtr->flags.cursorOn) {
882 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor, data);
883 } else {
884 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, data);
886 paintCursor(tPtr);
887 tPtr->flags.cursorOn = !tPtr->flags.cursorOn;
890 static void handleEvents(XEvent * event, void *data)
892 TextField *tPtr = (TextField *) data;
894 CHECK_CLASS(data, WC_TextField);
896 switch (event->type) {
897 case FocusIn:
898 W_FocusIC(tPtr->view);
899 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view)) != tPtr->view)
900 return;
901 tPtr->flags.focused = 1;
903 if (!tPtr->timerID) {
904 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor, tPtr);
907 paintTextField(tPtr);
909 NOTIFY(tPtr, didBeginEditing, WMTextDidBeginEditingNotification, NULL);
911 tPtr->flags.notIllegalMovement = 0;
912 break;
914 case FocusOut:
915 W_UnFocusIC(tPtr->view);
916 tPtr->flags.focused = 0;
918 if (tPtr->timerID)
919 WMDeleteTimerHandler(tPtr->timerID);
920 tPtr->timerID = NULL;
922 paintTextField(tPtr);
923 if (!tPtr->flags.notIllegalMovement) {
924 NOTIFY(tPtr, didEndEditing, WMTextDidEndEditingNotification,
925 (void *)WMIllegalTextMovement);
927 break;
929 case Expose:
930 if (event->xexpose.count != 0)
931 break;
932 paintTextField(tPtr);
933 break;
935 case DestroyNotify:
936 destroyTextField(tPtr);
937 break;
941 static void handleTextFieldKeyPress(TextField * tPtr, XEvent * event)
943 char buffer[64];
944 KeySym ksym;
945 char *textEvent = NULL;
946 void *data = NULL;
947 int count, refresh = 0;
948 int control_pressed = 0;
949 int cancelSelection = 1;
950 Bool shifted, controled, modified;
951 Bool relay = True;
953 /*printf("(%d,%d) -> ", tPtr->selection.position, tPtr->selection.count); */
954 if (((XKeyEvent *) event)->state & WM_EMACSKEYMASK)
955 control_pressed = 1;
957 shifted = (event->xkey.state & ShiftMask ? True : False);
958 controled = (event->xkey.state & ControlMask ? True : False);
959 modified = shifted || controled;
961 count = W_LookupString(tPtr->view, &event->xkey, buffer, 63, &ksym, NULL);
962 //count = XLookupString(&event->xkey, buffer, 63, &ksym, NULL);
963 buffer[count] = '\0';
965 switch (ksym) {
966 case XK_Tab:
967 #ifdef XK_ISO_Left_Tab
968 case XK_ISO_Left_Tab:
969 #endif
970 if (!controled) {
971 if (shifted) {
972 if (tPtr->view->prevFocusChain) {
973 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr->view),
974 tPtr->view->prevFocusChain);
975 tPtr->flags.notIllegalMovement = 1;
977 data = (void *)WMBacktabTextMovement;
978 } else {
979 if (tPtr->view->nextFocusChain) {
980 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr->view),
981 tPtr->view->nextFocusChain);
982 tPtr->flags.notIllegalMovement = 1;
984 data = (void *)WMTabTextMovement;
986 textEvent = WMTextDidEndEditingNotification;
988 cancelSelection = 0;
990 relay = False;
992 break;
994 case XK_Escape:
995 if (!modified) {
996 data = (void *)WMEscapeTextMovement;
997 textEvent = WMTextDidEndEditingNotification;
999 relay = False;
1001 break;
1003 case XK_Return:
1004 if (!modified) {
1005 data = (void *)WMReturnTextMovement;
1006 textEvent = WMTextDidEndEditingNotification;
1008 relay = False;
1010 break;
1012 case WM_EMACSKEY_LEFT:
1013 if (!control_pressed)
1014 goto normal_key;
1015 else
1016 controled = False;
1018 #ifdef XK_KP_Left
1019 case XK_KP_Left:
1020 #endif
1021 case XK_Left:
1022 if (tPtr->cursorPosition > 0) {
1023 int i;
1024 paintCursor(tPtr);
1026 i = tPtr->cursorPosition;
1027 i += oneUTF8CharBackward(&tPtr->text[i], i);
1028 if (controled) {
1029 while (i > 0 && tPtr->text[i] != ' ')
1030 i--;
1031 while (i > 0 && tPtr->text[i] == ' ')
1032 i--;
1034 tPtr->cursorPosition = (i > 0) ? i + 1 : 0;
1035 } else
1036 tPtr->cursorPosition = i;
1038 if (tPtr->cursorPosition < tPtr->viewPosition) {
1039 tPtr->viewPosition = tPtr->cursorPosition;
1040 refresh = 1;
1041 } else
1042 paintCursor(tPtr);
1044 if (shifted)
1045 cancelSelection = 0;
1047 relay = False;
1049 break;
1051 case WM_EMACSKEY_RIGHT:
1052 if (!control_pressed)
1053 goto normal_key;
1054 else
1055 controled = False;
1057 #ifdef XK_KP_Right
1058 case XK_KP_Right:
1059 #endif
1060 case XK_Right:
1061 if (tPtr->cursorPosition < tPtr->textLen) {
1062 int i;
1063 paintCursor(tPtr);
1065 i = tPtr->cursorPosition;
1066 if (controled) {
1067 while (tPtr->text[i] && tPtr->text[i] != ' ')
1068 i++;
1069 while (tPtr->text[i] == ' ')
1070 i++;
1071 } else {
1072 i += oneUTF8CharForward(&tPtr->text[i], tPtr->textLen - i);
1074 tPtr->cursorPosition = i;
1076 refresh = incrToFit2(tPtr);
1078 if (!refresh)
1079 paintCursor(tPtr);
1081 if (shifted)
1082 cancelSelection = 0;
1084 relay = False;
1086 break;
1088 case WM_EMACSKEY_HOME:
1089 if (!control_pressed)
1090 goto normal_key;
1091 else
1092 controled = False;
1094 #ifdef XK_KP_Home
1095 case XK_KP_Home:
1096 #endif
1097 case XK_Home:
1098 if (!controled) {
1099 if (tPtr->cursorPosition > 0) {
1100 paintCursor(tPtr);
1101 tPtr->cursorPosition = 0;
1102 if (tPtr->viewPosition > 0) {
1103 tPtr->viewPosition = 0;
1104 refresh = 1;
1105 } else
1106 paintCursor(tPtr);
1108 if (shifted)
1109 cancelSelection = 0;
1111 relay = False;
1113 break;
1115 case WM_EMACSKEY_END:
1116 if (!control_pressed)
1117 goto normal_key;
1118 else
1119 controled = False;
1121 #ifdef XK_KP_End
1122 case XK_KP_End:
1123 #endif
1124 case XK_End:
1125 if (!controled) {
1126 if (tPtr->cursorPosition < tPtr->textLen) {
1127 paintCursor(tPtr);
1128 tPtr->cursorPosition = tPtr->textLen;
1129 tPtr->viewPosition = 0;
1131 refresh = incrToFit(tPtr);
1133 if (!refresh)
1134 paintCursor(tPtr);
1136 if (shifted)
1137 cancelSelection = 0;
1139 relay = False;
1141 break;
1143 case WM_EMACSKEY_BS:
1144 if (!control_pressed)
1145 goto normal_key;
1146 else
1147 modified = False;
1149 case XK_BackSpace:
1150 if (!modified) {
1151 if (tPtr->selection.count) {
1152 WMDeleteTextFieldRange(tPtr, tPtr->selection);
1153 data = (void *)WMDeleteTextEvent;
1154 textEvent = WMTextDidChangeNotification;
1155 } else if (tPtr->cursorPosition > 0) {
1156 int i = oneUTF8CharBackward(&tPtr->text[tPtr->cursorPosition],
1157 tPtr->cursorPosition);
1158 WMRange range;
1159 range.position = tPtr->cursorPosition + i;
1160 range.count = -i;
1161 WMDeleteTextFieldRange(tPtr, range);
1162 data = (void *)WMDeleteTextEvent;
1163 textEvent = WMTextDidChangeNotification;
1166 relay = False;
1168 break;
1170 case WM_EMACSKEY_DEL:
1171 if (!control_pressed)
1172 goto normal_key;
1173 else
1174 modified = False;
1176 #ifdef XK_KP_Delete
1177 case XK_KP_Delete:
1178 #endif
1179 case XK_Delete:
1180 if (!modified) {
1181 if (tPtr->selection.count) {
1182 WMDeleteTextFieldRange(tPtr, tPtr->selection);
1183 data = (void *)WMDeleteTextEvent;
1184 textEvent = WMTextDidChangeNotification;
1185 } else if (tPtr->cursorPosition < tPtr->textLen) {
1186 WMRange range;
1187 range.position = tPtr->cursorPosition;
1188 range.count = oneUTF8CharForward(&tPtr->text[tPtr->cursorPosition],
1189 tPtr->textLen - tPtr->cursorPosition);
1190 WMDeleteTextFieldRange(tPtr, range);
1191 data = (void *)WMDeleteTextEvent;
1192 textEvent = WMTextDidChangeNotification;
1195 relay = False;
1197 break;
1199 normal_key:
1200 default:
1201 if (!controled) {
1202 if (count > 0 && !iscntrl(buffer[0])) {
1203 if (tPtr->selection.count)
1204 WMDeleteTextFieldRange(tPtr, tPtr->selection);
1205 WMInsertTextFieldText(tPtr, buffer, tPtr->cursorPosition);
1206 data = (void *)WMInsertTextEvent;
1207 textEvent = WMTextDidChangeNotification;
1209 relay = False;
1212 break;
1215 if (relay) {
1216 WMRelayToNextResponder(W_VIEW(tPtr), event);
1217 return;
1220 /* Do not allow text selection in secure text fields */
1221 if (cancelSelection || tPtr->flags.secure) {
1222 lostSelection(tPtr->view, XA_PRIMARY, NULL);
1224 if (tPtr->selection.count) {
1225 tPtr->selection.count = 0;
1226 refresh = 1;
1228 tPtr->selection.position = tPtr->cursorPosition;
1229 } else {
1230 if (tPtr->selection.count != tPtr->cursorPosition - tPtr->selection.position) {
1232 tPtr->selection.count = tPtr->cursorPosition - tPtr->selection.position;
1234 refresh = 1;
1238 /*printf("(%d,%d)\n", tPtr->selection.position, tPtr->selection.count); */
1240 if (textEvent) {
1241 WMNotification *notif = WMCreateNotification(textEvent, tPtr, data);
1243 if (tPtr->delegate) {
1244 if (textEvent == WMTextDidBeginEditingNotification && tPtr->delegate->didBeginEditing)
1245 (*tPtr->delegate->didBeginEditing) (tPtr->delegate, notif);
1247 else if (textEvent == WMTextDidEndEditingNotification && tPtr->delegate->didEndEditing)
1248 (*tPtr->delegate->didEndEditing) (tPtr->delegate, notif);
1250 else if (textEvent == WMTextDidChangeNotification && tPtr->delegate->didChange)
1251 (*tPtr->delegate->didChange) (tPtr->delegate, notif);
1254 WMPostNotification(notif);
1255 WMReleaseNotification(notif);
1258 if (refresh)
1259 paintTextField(tPtr);
1261 /*printf("(%d,%d)\n", tPtr->selection.position, tPtr->selection.count); */
1264 static int pointToCursorPosition(TextField * tPtr, int x)
1266 int a, b, pos, prev, tw;
1268 if (tPtr->flags.bordered)
1269 x -= 2;
1271 if (WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
1272 tPtr->textLen - tPtr->viewPosition) <= x)
1273 return tPtr->textLen;
1275 a = tPtr->viewPosition;
1276 b = tPtr->textLen;
1278 /* we halve the text until we get into a 10 byte vicinity of x */
1279 while (b - a > 10) {
1280 pos = (a + b) / 2;
1281 pos += seekUTF8CharStart(&tPtr->text[pos], pos - a);
1282 tw = WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]), pos - tPtr->viewPosition);
1283 if (tw > x) {
1284 b = pos;
1285 } else if (tw < x) {
1286 a = pos;
1287 } else {
1288 return pos;
1292 /* at this point x can be positioned on any glyph between 'a' and 'b-1'
1293 * inclusive, with the exception of the left border of the 'a' glyph and
1294 * the right border or the 'b-1' glyph
1296 * ( <--- range for x's position ---> )
1297 * a a+1 .......................... b-1 b
1299 pos = prev = a;
1300 while (pos <= b) {
1301 tw = WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]), pos - tPtr->viewPosition);
1302 if (tw > x) {
1303 return prev;
1304 } else if (pos == b) {
1305 break;
1307 prev = pos;
1308 pos += oneUTF8CharForward(&tPtr->text[pos], b - pos);
1311 return b;
1314 static void pasteText(WMView * view, Atom selection, Atom target, Time timestamp, void *cdata, WMData * data)
1316 TextField *tPtr = (TextField *) view->self;
1317 char *str;
1319 /* Parameter not used, but tell the compiler that it is ok */
1320 (void) selection;
1321 (void) target;
1322 (void) timestamp;
1323 (void) cdata;
1325 tPtr->flags.waitingSelection = 0;
1327 if (data != NULL) {
1328 str = (char *)WMDataBytes(data);
1330 WMInsertTextFieldText(tPtr, str, tPtr->cursorPosition);
1331 NOTIFY(tPtr, didChange, WMTextDidChangeNotification, (void *)WMInsertTextEvent);
1332 } else {
1333 int n;
1335 str = XFetchBuffer(tPtr->view->screen->display, &n, 0);
1337 if (str != NULL) {
1338 str[n] = 0;
1339 WMInsertTextFieldText(tPtr, str, tPtr->cursorPosition);
1340 XFree(str);
1341 NOTIFY(tPtr, didChange, WMTextDidChangeNotification, (void *)WMInsertTextEvent);
1346 static void handleTextFieldActionEvents(XEvent * event, void *data)
1348 TextField *tPtr = (TextField *) data;
1349 static Time lastButtonReleasedEvent = 0;
1350 static Time lastButtonReleasedEvent2 = 0;
1351 Display *dpy = event->xany.display;
1353 CHECK_CLASS(data, WC_TextField);
1355 switch (event->type) {
1356 case KeyPress:
1357 if (tPtr->flags.waitingSelection) {
1358 return;
1360 if (tPtr->flags.enabled && tPtr->flags.focused) {
1361 handleTextFieldKeyPress(tPtr, event);
1362 XDefineCursor(dpy, W_VIEW(tPtr)->window, W_VIEW(tPtr)->screen->invisibleCursor);
1363 tPtr->flags.pointerGrabbed = 1;
1365 break;
1367 case MotionNotify:
1369 if (tPtr->flags.pointerGrabbed) {
1370 tPtr->flags.pointerGrabbed = 0;
1371 XDefineCursor(dpy, W_VIEW(tPtr)->window, W_VIEW(tPtr)->screen->textCursor);
1373 if (tPtr->flags.waitingSelection) {
1374 return;
1377 if (tPtr->flags.enabled && (event->xmotion.state & Button1Mask)) {
1379 if (tPtr->viewPosition < tPtr->textLen && event->xmotion.x > tPtr->usableWidth) {
1380 if (WMWidthOfString(tPtr->font,
1381 &(tPtr->text[tPtr->viewPosition]),
1382 tPtr->cursorPosition - tPtr->viewPosition)
1383 > tPtr->usableWidth) {
1384 tPtr->viewPosition += oneUTF8CharForward(&tPtr->text[tPtr->viewPosition],
1385 tPtr->textLen -
1386 tPtr->viewPosition);
1388 } else if (tPtr->viewPosition > 0 && event->xmotion.x < 0) {
1389 paintCursor(tPtr);
1390 tPtr->viewPosition += oneUTF8CharBackward(&tPtr->text[tPtr->viewPosition],
1391 tPtr->viewPosition);
1394 tPtr->cursorPosition = pointToCursorPosition(tPtr, event->xmotion.x);
1396 /* Do not allow text selection in secure textfields */
1397 if (tPtr->flags.secure) {
1398 tPtr->selection.position = tPtr->cursorPosition;
1401 tPtr->selection.count = tPtr->cursorPosition - tPtr->selection.position;
1403 paintCursor(tPtr);
1404 paintTextField(tPtr);
1407 break;
1409 case ButtonPress:
1410 if (tPtr->flags.pointerGrabbed) {
1411 tPtr->flags.pointerGrabbed = 0;
1412 XDefineCursor(dpy, W_VIEW(tPtr)->window, W_VIEW(tPtr)->screen->textCursor);
1413 break;
1416 if (tPtr->flags.waitingSelection) {
1417 break;
1420 switch (tPtr->flags.alignment) {
1421 int textWidth;
1422 case WARight:
1423 textWidth = WMWidthOfString(tPtr->font, tPtr->text, tPtr->textLen);
1424 if (tPtr->flags.enabled && !tPtr->flags.focused) {
1425 WMSetFocusToWidget(tPtr);
1427 if (tPtr->flags.focused) {
1428 tPtr->selection.position = tPtr->cursorPosition;
1429 tPtr->selection.count = 0;
1431 if (textWidth < tPtr->usableWidth) {
1432 tPtr->cursorPosition = pointToCursorPosition(tPtr,
1433 event->xbutton.x - tPtr->usableWidth
1434 + textWidth);
1435 } else
1436 tPtr->cursorPosition = pointToCursorPosition(tPtr, event->xbutton.x);
1438 paintTextField(tPtr);
1439 break;
1441 case WALeft:
1442 if (tPtr->flags.enabled && !tPtr->flags.focused) {
1443 WMSetFocusToWidget(tPtr);
1445 if (tPtr->flags.focused && event->xbutton.button == Button1) {
1446 tPtr->cursorPosition = pointToCursorPosition(tPtr, event->xbutton.x);
1447 tPtr->selection.position = tPtr->cursorPosition;
1448 tPtr->selection.count = 0;
1449 paintTextField(tPtr);
1451 if (event->xbutton.button == Button2 && tPtr->flags.enabled) {
1452 char *text;
1453 int n;
1455 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
1456 event->xbutton.time, pasteText, NULL)) {
1457 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
1459 if (text) {
1460 text[n] = 0;
1461 WMInsertTextFieldText(tPtr, text, tPtr->cursorPosition);
1462 XFree(text);
1463 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
1464 (void *)WMInsertTextEvent);
1466 } else {
1467 tPtr->flags.waitingSelection = 1;
1470 break;
1471 default:
1472 break;
1474 break;
1476 case ButtonRelease:
1477 if (tPtr->flags.pointerGrabbed) {
1478 tPtr->flags.pointerGrabbed = 0;
1479 XDefineCursor(dpy, W_VIEW(tPtr)->window, W_VIEW(tPtr)->screen->textCursor);
1481 if (tPtr->flags.waitingSelection) {
1482 break;
1485 if (!tPtr->flags.secure && tPtr->selection.count != 0) {
1486 int start, count;
1487 XRotateBuffers(dpy, 1);
1489 count = abs(tPtr->selection.count);
1490 if (tPtr->selection.count < 0)
1491 start = tPtr->selection.position - count;
1492 else
1493 start = tPtr->selection.position;
1495 XStoreBuffer(dpy, &tPtr->text[start], count, 0);
1498 if (!tPtr->flags.secure &&
1499 event->xbutton.time - lastButtonReleasedEvent <= WINGsConfiguration.doubleClickDelay) {
1501 if (event->xbutton.time - lastButtonReleasedEvent2 <=
1502 2 * WINGsConfiguration.doubleClickDelay) {
1503 tPtr->selection.position = 0;
1504 tPtr->selection.count = tPtr->textLen;
1505 } else {
1506 int pos, cnt;
1507 char *txt;
1508 pos = tPtr->selection.position;
1509 cnt = tPtr->selection.count;
1510 txt = tPtr->text;
1511 while (pos >= 0) {
1512 if (txt[pos] == ' ' || txt[pos] == '\t')
1513 break;
1514 pos--;
1516 pos++;
1518 while (pos + cnt < tPtr->textLen) {
1519 if (txt[pos + cnt] == ' ' || txt[pos + cnt] == '\t')
1520 break;
1521 cnt++;
1523 tPtr->selection.position = pos;
1524 tPtr->selection.count = cnt;
1526 paintTextField(tPtr);
1528 if (!tPtr->flags.ownsSelection) {
1529 tPtr->flags.ownsSelection =
1530 WMCreateSelectionHandler(tPtr->view,
1531 XA_PRIMARY,
1532 event->xbutton.time, &selectionHandler, NULL);
1534 } else if (!tPtr->flags.secure && tPtr->selection.count != 0 && !tPtr->flags.ownsSelection) {
1535 tPtr->flags.ownsSelection =
1536 WMCreateSelectionHandler(tPtr->view,
1537 XA_PRIMARY, event->xbutton.time, &selectionHandler, NULL);
1540 lastButtonReleasedEvent2 = lastButtonReleasedEvent;
1541 lastButtonReleasedEvent = event->xbutton.time;
1543 break;
1547 static void destroyTextField(TextField * tPtr)
1549 if (tPtr->timerID)
1550 WMDeleteTimerHandler(tPtr->timerID);
1552 W_DestroyIC(tPtr->view);
1554 WMReleaseFont(tPtr->font);
1555 /*// use lostSelection() instead of WMDeleteSelectionHandler here? */
1556 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
1557 WMRemoveNotificationObserver(tPtr);
1559 if (tPtr->text)
1560 wfree(tPtr->text);
1562 wfree(tPtr);