- fixed a frame size in WPrefs menu editor
[wmaker-crm.git] / WINGs / wtextfield.c
blob1d2de3fea6581fbded76c46af5000cd00044ea5e
5 #include "WINGsP.h"
6 #include "wconfig.h"
8 #include <X11/keysym.h>
9 #include <X11/Xatom.h>
11 #include <ctype.h>
13 #define CURSOR_BLINK_ON_DELAY 600
14 #define CURSOR_BLINK_OFF_DELAY 300
18 char *WMTextDidChangeNotification = "WMTextDidChangeNotification";
19 char *WMTextDidBeginEditingNotification = "WMTextDidBeginEditingNotification";
20 char *WMTextDidEndEditingNotification = "WMTextDidEndEditingNotification";
23 typedef struct W_TextField {
24 W_Class widgetClass;
25 W_View *view;
27 #if 0
28 struct W_TextField *nextField; /* next textfield in the chain */
29 struct W_TextField *prevField;
30 #endif
32 char *text;
33 int textLen; /* size of text */
34 int bufferSize; /* memory allocated for text */
36 int viewPosition; /* position of text being shown */
38 int cursorPosition; /* position of the insertion cursor */
40 short usableWidth;
41 short offsetWidth; /* offset of text from border */
43 WMRange selection;
45 WMFont *font;
47 WMTextFieldDelegate *delegate;
49 #if 0
50 WMHandlerID timerID; /* for cursor blinking */
51 #endif
52 struct {
53 WMAlignment alignment:2;
55 unsigned int bordered:1;
57 unsigned int beveled:1;
59 unsigned int enabled:1;
61 unsigned int focused:1;
63 unsigned int cursorOn:1;
65 unsigned int secure:1; /* password entry style */
67 unsigned int pointerGrabbed:1;
69 unsigned int ownsSelection:1;
71 unsigned int waitingSelection:1; /* requested selection, but
72 * didnt get yet */
74 /**/
75 unsigned int notIllegalMovement:1;
76 } flags;
77 } TextField;
80 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
81 if ((T)->delegate && (T)->delegate->C)\
82 (*(T)->delegate->C)((T)->delegate,notif);\
83 WMPostNotification(notif);\
84 WMReleaseNotification(notif);}
87 #define MIN_TEXT_BUFFER 2
88 #define TEXT_BUFFER_INCR 8
91 #define WM_EMACSKEYMASK ControlMask
93 #define WM_EMACSKEY_LEFT XK_b
94 #define WM_EMACSKEY_RIGHT XK_f
95 #define WM_EMACSKEY_HOME XK_a
96 #define WM_EMACSKEY_END XK_e
97 #define WM_EMACSKEY_BS XK_h
98 #define WM_EMACSKEY_DEL XK_d
102 #define DEFAULT_WIDTH 60
103 #define DEFAULT_HEIGHT 20
104 #define DEFAULT_BORDERED True
105 #define DEFAULT_ALIGNMENT WALeft
109 static void destroyTextField(TextField *tPtr);
110 static void paintTextField(TextField *tPtr);
112 static void handleEvents(XEvent *event, void *data);
113 static void handleTextFieldActionEvents(XEvent *event, void *data);
114 static void didResizeTextField();
116 struct W_ViewDelegate _TextFieldViewDelegate = {
117 NULL,
118 NULL,
119 didResizeTextField,
120 NULL,
121 NULL
126 static void lostSelection(WMView *view, Atom selection, void *cdata);
128 static WMData *requestHandler(WMView *view, Atom selection, Atom target,
129 void *cdata, Atom *type);
132 static WMSelectionProcs selectionHandler = {
133 requestHandler,
134 lostSelection,
135 NULL
139 #define TEXT_WIDTH(tPtr, start) (WMWidthOfString((tPtr)->font, \
140 &((tPtr)->text[(start)]), (tPtr)->textLen - (start) + 1))
142 #define TEXT_WIDTH2(tPtr, start, end) (WMWidthOfString((tPtr)->font, \
143 &((tPtr)->text[(start)]), (end) - (start) + 1))
146 static void
147 normalizeRange(TextField *tPtr, WMRange *range)
149 if (range->position < 0 && range->count < 0)
150 range->count = 0;
152 if (range->count == 0) {
153 /*range->position = 0; why is this?*/
154 return;
157 /* (1,-2) ~> (0,1) ; (1,-1) ~> (0,1) ; (2,-1) ~> (1,1) */
158 if (range->count < 0) { /* && range->position >= 0 */
159 if (range->position + range->count < 0) {
160 range->count = range->position;
161 range->position = 0;
162 } else {
163 range->count = -range->count;
164 range->position -= range->count;
166 /* (-2,1) ~> (0,0) ; (-1,1) ~> (0,0) ; (-1,2) ~> (0,1) */
167 } else if (range->position < 0) { /* && range->count > 0 */
168 if (range->position + range->count < 0) {
169 range->position = range->count = 0;
170 } else {
171 range->count += range->position;
172 range->position = 0;
176 if (range->position + range->count > tPtr->textLen)
177 range->count = tPtr->textLen - range->position;
180 static void
181 memmv(char *dest, char *src, int size)
183 int i;
185 if (dest > src) {
186 for (i=size-1; i>=0; i--) {
187 dest[i] = src[i];
189 } else if (dest < src) {
190 for (i=0; i<size; i++) {
191 dest[i] = src[i];
197 static int
198 incrToFit(TextField *tPtr)
200 int vp = tPtr->viewPosition;
202 while (TEXT_WIDTH(tPtr, tPtr->viewPosition) > tPtr->usableWidth) {
203 tPtr->viewPosition++;
205 return vp!=tPtr->viewPosition;
209 static int
210 incrToFit2(TextField *tPtr)
212 int vp = tPtr->viewPosition;
213 while (TEXT_WIDTH2(tPtr, tPtr->viewPosition, tPtr->cursorPosition)
214 >= tPtr->usableWidth)
215 tPtr->viewPosition++;
218 return vp!=tPtr->viewPosition;
222 static void
223 decrToFit(TextField *tPtr)
225 while (TEXT_WIDTH(tPtr, tPtr->viewPosition-1) < tPtr->usableWidth
226 && tPtr->viewPosition>0)
227 tPtr->viewPosition--;
230 #undef TEXT_WIDTH
231 #undef TEXT_WIDTH2
235 static WMData*
236 requestHandler(WMView *view, Atom selection, Atom target, void *cdata,
237 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 count = tPtr->selection.count < 0
248 ? tPtr->selection.position + tPtr->selection.count
249 : tPtr->selection.position;
251 if (target == XA_STRING || target == TEXT || target == COMPOUND_TEXT) {
253 data = WMCreateDataWithBytes(&(tPtr->text[count]),
254 abs(tPtr->selection.count));
255 WMSetDataFormat(data, 8);
256 *type = target;
258 return data;
261 _TARGETS = XInternAtom(dpy, "TARGETS", False);
262 if (target == _TARGETS) {
263 Atom *ptr;
265 ptr = wmalloc(4 * sizeof(Atom));
266 ptr[0] = _TARGETS;
267 ptr[1] = XA_STRING;
268 ptr[2] = TEXT;
269 ptr[3] = COMPOUND_TEXT;
271 data = WMCreateDataWithBytes(ptr, 4*4);
272 WMSetDataFormat(data, 32);
274 *type = target;
275 return data;
278 return NULL;
283 static void
284 lostSelection(WMView *view, Atom selection, void *cdata)
286 TextField *tPtr = (WMTextField*)view->self;
288 if (tPtr->flags.ownsSelection) {
289 WMDeleteSelectionHandler(view, selection, CurrentTime);
290 tPtr->flags.ownsSelection = 0;
292 if (tPtr->selection.count != 0) {
293 tPtr->selection.count = 0;
294 paintTextField(tPtr);
299 static void
300 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);
315 WMTextField*
316 WMCreateTextField(WMWidget *parent)
318 TextField *tPtr;
320 tPtr = wmalloc(sizeof(TextField));
321 memset(tPtr, 0, sizeof(TextField));
323 tPtr->widgetClass = WC_TextField;
325 tPtr->view = W_CreateView(W_VIEW(parent));
326 if (!tPtr->view) {
327 wfree(tPtr);
328 return NULL;
330 tPtr->view->self = tPtr;
332 tPtr->view->delegate = &_TextFieldViewDelegate;
334 tPtr->view->attribFlags |= CWCursor;
335 tPtr->view->attribs.cursor = tPtr->view->screen->textCursor;
337 W_SetViewBackgroundColor(tPtr->view, tPtr->view->screen->white);
339 tPtr->text = wmalloc(MIN_TEXT_BUFFER);
340 tPtr->text[0] = 0;
341 tPtr->textLen = 0;
342 tPtr->bufferSize = MIN_TEXT_BUFFER;
344 tPtr->flags.enabled = 1;
346 WMCreateEventHandler(tPtr->view, ExposureMask|StructureNotifyMask
347 |FocusChangeMask, handleEvents, tPtr);
349 tPtr->font = WMRetainFont(tPtr->view->screen->normalFont);
351 tPtr->flags.bordered = DEFAULT_BORDERED;
352 tPtr->flags.beveled = True;
353 tPtr->flags.alignment = DEFAULT_ALIGNMENT;
354 tPtr->offsetWidth =
355 WMAX((tPtr->view->size.height - WMFontHeight(tPtr->font))/2, 1);
357 W_ResizeView(tPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
359 WMCreateEventHandler(tPtr->view, EnterWindowMask|LeaveWindowMask
360 |ButtonReleaseMask|ButtonPressMask|KeyPressMask|Button1MotionMask,
361 handleTextFieldActionEvents, tPtr);
363 WMAddNotificationObserver(selectionNotification, tPtr->view,
364 WMSelectionOwnerDidChangeNotification,
365 (void*)XA_PRIMARY);
368 tPtr->flags.cursorOn = 1;
370 return tPtr;
374 void
375 WMSetTextFieldDelegate(WMTextField *tPtr, WMTextFieldDelegate *delegate)
377 CHECK_CLASS(tPtr, WC_TextField);
379 tPtr->delegate = delegate;
383 WMTextFieldDelegate*
384 WMGetTextFieldDelegate(WMTextField *tPtr)
386 CHECK_CLASS(tPtr, WC_TextField);
388 return tPtr->delegate;
392 void
393 WMInsertTextFieldText(WMTextField *tPtr, char *text, int position)
395 int len;
397 CHECK_CLASS(tPtr, WC_TextField);
399 if (!text)
400 return;
402 len = strlen(text);
404 /* check if buffer will hold the text */
405 if (len + tPtr->textLen >= tPtr->bufferSize) {
406 tPtr->bufferSize = tPtr->textLen + len + TEXT_BUFFER_INCR;
407 tPtr->text = wrealloc(tPtr->text, tPtr->bufferSize);
410 if (position < 0 || position >= tPtr->textLen) {
411 /* append the text at the end */
412 strcat(tPtr->text, text);
414 incrToFit(tPtr);
416 tPtr->textLen += len;
417 tPtr->cursorPosition += len;
418 } else {
419 /* insert text at position */
420 memmv(&(tPtr->text[position+len]), &(tPtr->text[position]),
421 tPtr->textLen-position+1);
423 memcpy(&(tPtr->text[position]), text, len);
425 tPtr->textLen += len;
426 if (position >= tPtr->cursorPosition) {
427 tPtr->cursorPosition += len;
428 incrToFit2(tPtr);
429 } else {
430 incrToFit(tPtr);
434 paintTextField(tPtr);
437 void
438 WMDeleteTextFieldRange(WMTextField *tPtr, WMRange range)
440 CHECK_CLASS(tPtr, WC_TextField);
442 normalizeRange(tPtr, &range);
444 if (!range.count)
445 return;
447 memmv(&(tPtr->text[range.position]), &(tPtr->text[range.position+range.count]),
448 tPtr->textLen - (range.position+range.count) + 1);
450 tPtr->textLen -= range.count;
452 /* try to keep cursorPosition at the same place */
453 tPtr->viewPosition -= range.count;
454 if (tPtr->viewPosition < 0)
455 tPtr->viewPosition = 0;
456 tPtr->cursorPosition = range.position;
458 decrToFit(tPtr);
460 paintTextField(tPtr);
465 char*
466 WMGetTextFieldText(WMTextField *tPtr)
468 CHECK_CLASS(tPtr, WC_TextField);
470 return wstrdup(tPtr->text);
474 void
475 WMSetTextFieldText(WMTextField *tPtr, char *text)
477 CHECK_CLASS(tPtr, WC_TextField);
479 if ((text && strcmp(tPtr->text, text) == 0) ||
480 (!text && tPtr->textLen == 0))
481 return;
483 if (text==NULL) {
484 tPtr->text[0] = 0;
485 tPtr->textLen = 0;
486 } else {
487 tPtr->textLen = strlen(text);
489 if (tPtr->textLen >= tPtr->bufferSize) {
490 tPtr->bufferSize = tPtr->textLen + TEXT_BUFFER_INCR;
491 tPtr->text = wrealloc(tPtr->text, tPtr->bufferSize);
493 strcpy(tPtr->text, text);
496 tPtr->cursorPosition = tPtr->selection.position = tPtr->textLen;
497 tPtr->viewPosition = 0;
498 tPtr->selection.count = 0;
500 if (tPtr->view->flags.realized)
501 paintTextField(tPtr);
505 void
506 WMSetTextFieldAlignment(WMTextField *tPtr, WMAlignment alignment)
508 CHECK_CLASS(tPtr, WC_TextField);
510 tPtr->flags.alignment = alignment;
512 if (alignment!=WALeft) {
513 wwarning("only left alignment is supported in textfields");
514 return;
517 if (tPtr->view->flags.realized) {
518 paintTextField(tPtr);
523 void
524 WMSetTextFieldBordered(WMTextField *tPtr, Bool bordered)
526 CHECK_CLASS(tPtr, WC_TextField);
528 tPtr->flags.bordered = bordered;
530 if (tPtr->view->flags.realized) {
531 paintTextField(tPtr);
536 void
537 WMSetTextFieldBeveled(WMTextField *tPtr, Bool flag)
539 CHECK_CLASS(tPtr, WC_TextField);
541 tPtr->flags.beveled = ((flag==0) ? 0 : 1);
543 if (tPtr->view->flags.realized) {
544 paintTextField(tPtr);
550 void
551 WMSetTextFieldSecure(WMTextField *tPtr, Bool flag)
553 CHECK_CLASS(tPtr, WC_TextField);
555 tPtr->flags.secure = ((flag==0) ? 0 : 1);
557 if (tPtr->view->flags.realized) {
558 paintTextField(tPtr);
563 Bool
564 WMGetTextFieldEditable(WMTextField *tPtr)
566 CHECK_CLASS(tPtr, WC_TextField);
568 return tPtr->flags.enabled;
572 void
573 WMSetTextFieldEditable(WMTextField *tPtr, Bool flag)
575 CHECK_CLASS(tPtr, WC_TextField);
577 tPtr->flags.enabled = ((flag==0) ? 0 : 1);
579 if (tPtr->view->flags.realized) {
580 paintTextField(tPtr);
585 void
586 WMSelectTextFieldRange(WMTextField *tPtr, WMRange range)
588 CHECK_CLASS(tPtr, WC_TextField);
590 if (tPtr->flags.enabled) {
591 normalizeRange(tPtr, &range);
593 tPtr->selection = range;
595 tPtr->cursorPosition = range.position + range.count;
597 if (tPtr->view->flags.realized) {
598 paintTextField(tPtr);
604 void
605 WMSetTextFieldCursorPosition(WMTextField *tPtr, unsigned int position)
607 CHECK_CLASS(tPtr, WC_TextField);
609 if (tPtr->flags.enabled) {
610 if (position > tPtr->textLen)
611 position = tPtr->textLen;
613 tPtr->cursorPosition = position;
614 if (tPtr->view->flags.realized) {
615 paintTextField(tPtr);
621 void
622 WMSetTextFieldNextTextField(WMTextField *tPtr, WMTextField *next)
624 CHECK_CLASS(tPtr, WC_TextField);
625 if (next == NULL) {
626 if (tPtr->view->nextFocusChain)
627 tPtr->view->nextFocusChain->prevFocusChain = NULL;
628 tPtr->view->nextFocusChain = NULL;
629 return;
632 CHECK_CLASS(next, WC_TextField);
634 if (tPtr->view->nextFocusChain)
635 tPtr->view->nextFocusChain->prevFocusChain = NULL;
636 if (next->view->prevFocusChain)
637 next->view->prevFocusChain->nextFocusChain = NULL;
639 tPtr->view->nextFocusChain = next->view;
640 next->view->prevFocusChain = tPtr->view;
644 void
645 WMSetTextFieldPrevTextField(WMTextField *tPtr, WMTextField *prev)
647 CHECK_CLASS(tPtr, WC_TextField);
648 if (prev == NULL) {
649 if (tPtr->view->prevFocusChain)
650 tPtr->view->prevFocusChain->nextFocusChain = NULL;
651 tPtr->view->prevFocusChain = NULL;
652 return;
655 CHECK_CLASS(prev, WC_TextField);
657 if (tPtr->view->prevFocusChain)
658 tPtr->view->prevFocusChain->nextFocusChain = NULL;
659 if (prev->view->nextFocusChain)
660 prev->view->nextFocusChain->prevFocusChain = NULL;
662 tPtr->view->prevFocusChain = prev->view;
663 prev->view->nextFocusChain = tPtr->view;
667 void
668 WMSetTextFieldFont(WMTextField *tPtr, WMFont *font)
670 CHECK_CLASS(tPtr, WC_TextField);
672 if (tPtr->font)
673 WMReleaseFont(tPtr->font);
674 tPtr->font = WMRetainFont(font);
676 tPtr->offsetWidth =
677 WMAX((tPtr->view->size.height - WMFontHeight(tPtr->font))/2, 1);
679 if (tPtr->view->flags.realized) {
680 paintTextField(tPtr);
686 WMFont*
687 WMGetTextFieldFont(WMTextField *tPtr)
689 return tPtr->font;
693 static void
694 didResizeTextField(W_ViewDelegate *self, WMView *view)
696 WMTextField *tPtr = (WMTextField*)view->self;
698 tPtr->offsetWidth =
699 WMAX((tPtr->view->size.height - WMFontHeight(tPtr->font))/2, 1);
701 tPtr->usableWidth = tPtr->view->size.width - 2*tPtr->offsetWidth /*+ 2*/;
705 static char*
706 makeHiddenString(int length)
708 char *data = wmalloc(length+1);
710 memset(data, '*', length);
711 data[length] = '\0';
713 return data;
717 static void
718 paintCursor(TextField *tPtr)
720 int cx;
721 WMScreen *screen = tPtr->view->screen;
722 int textWidth;
723 char *text;
725 if (tPtr->flags.secure)
726 text = makeHiddenString(strlen(tPtr->text));
727 else
728 text = tPtr->text;
730 cx = WMWidthOfString(tPtr->font, &(text[tPtr->viewPosition]),
731 tPtr->cursorPosition-tPtr->viewPosition);
733 switch (tPtr->flags.alignment) {
734 case WARight:
735 textWidth = WMWidthOfString(tPtr->font, text, tPtr->textLen);
736 if (textWidth < tPtr->usableWidth)
737 cx += tPtr->offsetWidth + tPtr->usableWidth - textWidth + 1;
738 else
739 cx += tPtr->offsetWidth + 1;
740 break;
741 case WALeft:
742 cx += tPtr->offsetWidth + 1;
743 break;
744 case WAJustified:
745 /* not supported */
746 case WACenter:
747 textWidth = WMWidthOfString(tPtr->font, text, tPtr->textLen);
748 if (textWidth < tPtr->usableWidth)
749 cx += tPtr->offsetWidth + (tPtr->usableWidth-textWidth)/2;
750 else
751 cx += tPtr->offsetWidth;
752 break;
755 XDrawRectangle(screen->display, tPtr->view->window, screen->xorGC,
756 cx, tPtr->offsetWidth, 1,
757 tPtr->view->size.height - 2*tPtr->offsetWidth - 1);
758 printf("%d %d\n",cx,tPtr->cursorPosition);
760 XDrawLine(screen->display, tPtr->view->window, screen->xorGC,
761 cx, tPtr->offsetWidth, cx,
762 tPtr->view->size.height - tPtr->offsetWidth - 1);
764 if (tPtr->flags.secure)
765 wfree(text);
770 static void
771 drawRelief(WMView *view, Bool beveled)
773 WMScreen *scr = view->screen;
774 Display *dpy = scr->display;
775 GC wgc;
776 GC lgc;
777 GC dgc;
778 int width = view->size.width;
779 int height = view->size.height;
781 dgc = WMColorGC(scr->darkGray);
783 if (!beveled) {
784 XDrawRectangle(dpy, view->window, dgc, 0, 0, width-1, height-1);
786 return;
788 wgc = WMColorGC(scr->white);
789 lgc = WMColorGC(scr->gray);
791 /* top left */
792 XDrawLine(dpy, view->window, dgc, 0, 0, width-1, 0);
793 XDrawLine(dpy, view->window, dgc, 0, 1, width-2, 1);
795 XDrawLine(dpy, view->window, dgc, 0, 0, 0, height-2);
796 XDrawLine(dpy, view->window, dgc, 1, 0, 1, height-3);
798 /* bottom right */
799 XDrawLine(dpy, view->window, wgc, 0, height-1, width-1, height-1);
800 XDrawLine(dpy, view->window, lgc, 1, height-2, width-2, height-2);
802 XDrawLine(dpy, view->window, wgc, width-1, 0, width-1, height-1);
803 XDrawLine(dpy, view->window, lgc, width-2, 1, width-2, height-3);
807 static void
808 paintTextField(TextField *tPtr)
810 W_Screen *screen = tPtr->view->screen;
811 W_View *view = tPtr->view;
812 W_View viewbuffer;
813 int tx, ty, tw, th;
814 int rx;
815 int bd;
816 int totalWidth;
817 char *text;
818 Pixmap drawbuffer;
821 if (!view->flags.realized || !view->flags.mapped)
822 return;
824 if (!tPtr->flags.bordered) {
825 bd = 0;
826 } else {
827 bd = 2;
830 if (tPtr->flags.secure) {
831 text = makeHiddenString(strlen(tPtr->text));
832 } else {
833 text = tPtr->text;
836 totalWidth = tPtr->view->size.width - 2*bd;
838 drawbuffer = XCreatePixmap(screen->display, view->window,
839 view->size.width, view->size.height, screen->depth);
840 XFillRectangle(screen->display, drawbuffer, WMColorGC(screen->white),
841 0,0, view->size.width,view->size.height);
842 /* this is quite dirty */
843 viewbuffer.screen = view->screen;
844 viewbuffer.size = view->size;
845 viewbuffer.window = drawbuffer;
848 if (tPtr->textLen > 0) {
849 tw = WMWidthOfString(tPtr->font, &(text[tPtr->viewPosition]),
850 tPtr->textLen - tPtr->viewPosition);
852 th = WMFontHeight(tPtr->font);
854 ty = tPtr->offsetWidth;
855 switch (tPtr->flags.alignment) {
856 case WALeft:
857 tx = tPtr->offsetWidth + 1;
858 if (tw < tPtr->usableWidth)
859 XFillRectangle(screen->display, drawbuffer,
860 WMColorGC(screen->white),
861 bd+tw,bd, totalWidth-tw,view->size.height-2*bd);
862 break;
864 case WACenter:
865 tx = tPtr->offsetWidth + (tPtr->usableWidth - tw) / 2;
866 if (tw < tPtr->usableWidth)
867 XClearArea(screen->display, view->window, bd, bd,
868 totalWidth, view->size.height-2*bd, False);
869 break;
871 default:
872 case WARight:
873 tx = tPtr->offsetWidth + tPtr->usableWidth - tw - 1;
874 if (tw < tPtr->usableWidth)
875 XClearArea(screen->display, view->window, bd, bd,
876 totalWidth-tw, view->size.height-2*bd, False);
877 break;
880 if (!tPtr->flags.enabled)
881 WMSetColorInGC(screen->darkGray, screen->textFieldGC);
883 WMDrawImageString(screen, drawbuffer, screen->textFieldGC,
884 tPtr->font, tx, ty,
885 &(text[tPtr->viewPosition]),
886 tPtr->textLen - tPtr->viewPosition);
888 if (tPtr->selection.count) {
889 int count,count2;
891 count = tPtr->selection.count < 0
892 ? tPtr->selection.position + tPtr->selection.count
893 : tPtr->selection.position;
894 count2 = abs(tPtr->selection.count);
895 if (count < tPtr->viewPosition) {
896 count2 = abs(count2 - abs(tPtr->viewPosition - count));
897 count = tPtr->viewPosition;
901 rx = tPtr->offsetWidth + 1 + WMWidthOfString(tPtr->font,text,count)
902 - WMWidthOfString(tPtr->font,text,tPtr->viewPosition);
904 XSetBackground(screen->display, screen->textFieldGC,
905 screen->gray->color.pixel);
907 WMDrawImageString(screen, drawbuffer, screen->textFieldGC,
908 tPtr->font, rx, ty, &(text[count]),
909 count2);
911 XSetBackground(screen->display, screen->textFieldGC,
912 screen->white->color.pixel);
915 if (!tPtr->flags.enabled)
916 WMSetColorInGC(screen->black, screen->textFieldGC);
917 } else {
918 XFillRectangle(screen->display, drawbuffer,
919 WMColorGC(screen->white),
920 bd,bd, totalWidth,view->size.height-2*bd);
923 /* draw relief */
924 if (tPtr->flags.bordered) {
925 drawRelief(&viewbuffer, tPtr->flags.beveled);
928 if (tPtr->flags.secure)
929 wfree(text);
930 XCopyArea(screen->display, drawbuffer, view->window,
931 screen->copyGC, 0,0, view->size.width,
932 view->size.height,0,0);
933 XFreePixmap(screen->display, drawbuffer);
935 /* draw cursor */
936 if (tPtr->flags.focused && tPtr->flags.enabled && tPtr->flags.cursorOn) {
937 paintCursor(tPtr);
942 #if 0
943 static void
944 blinkCursor(void *data)
946 TextField *tPtr = (TextField*)data;
948 if (tPtr->flags.cursorOn) {
949 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY, blinkCursor,
950 data);
951 } else {
952 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY, blinkCursor,
953 data);
955 paintCursor(tPtr);
956 tPtr->flags.cursorOn = !tPtr->flags.cursorOn;
958 #endif
961 static void
962 handleEvents(XEvent *event, void *data)
964 TextField *tPtr = (TextField*)data;
966 CHECK_CLASS(data, WC_TextField);
969 switch (event->type) {
970 case FocusIn:
971 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr->view))!=tPtr->view)
972 return;
973 tPtr->flags.focused = 1;
974 #if 0
975 if (!tPtr->timerID) {
976 tPtr->timerID = WMAddTimerHandler(CURSOR_BLINK_ON_DELAY,
977 blinkCursor, tPtr);
979 #endif
980 paintTextField(tPtr);
982 NOTIFY(tPtr, didBeginEditing, WMTextDidBeginEditingNotification, NULL);
984 tPtr->flags.notIllegalMovement = 0;
985 break;
987 case FocusOut:
988 tPtr->flags.focused = 0;
989 #if 0
990 if (tPtr->timerID)
991 WMDeleteTimerHandler(tPtr->timerID);
992 tPtr->timerID = NULL;
993 #endif
995 paintTextField(tPtr);
996 if (!tPtr->flags.notIllegalMovement) {
997 NOTIFY(tPtr, didEndEditing, WMTextDidEndEditingNotification,
998 (void*)WMIllegalTextMovement);
1000 break;
1002 case Expose:
1003 if (event->xexpose.count!=0)
1004 break;
1005 paintTextField(tPtr);
1006 break;
1008 case DestroyNotify:
1009 destroyTextField(tPtr);
1010 break;
1015 static void
1016 handleTextFieldKeyPress(TextField *tPtr, XEvent *event)
1018 char buffer[64];
1019 KeySym ksym;
1020 char *textEvent = NULL;
1021 void *data = NULL;
1022 int count, refresh = 0;
1023 int control_pressed = 0;
1024 int cancelSelection = 1;
1025 Bool shifted, controled, modified;
1026 Bool relay = True;
1028 /*printf("(%d,%d) -> ", tPtr->selection.position, tPtr->selection.count);*/
1029 if (((XKeyEvent *) event)->state & WM_EMACSKEYMASK)
1030 control_pressed = 1;
1032 shifted = (event->xkey.state & ShiftMask ? True : False);
1033 controled = (event->xkey.state & ControlMask ? True : False);
1034 modified = shifted || controled;
1036 count = XLookupString(&event->xkey, buffer, 63, &ksym, NULL);
1037 buffer[count] = '\0';
1039 switch (ksym) {
1040 case XK_Tab:
1041 #ifdef XK_ISO_Left_Tab
1042 case XK_ISO_Left_Tab:
1043 #endif
1044 if (!controled) {
1045 if (shifted) {
1046 if (tPtr->view->prevFocusChain) {
1047 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr->view),
1048 tPtr->view->prevFocusChain);
1049 tPtr->flags.notIllegalMovement = 1;
1051 data = (void*)WMBacktabTextMovement;
1052 } else {
1053 if (tPtr->view->nextFocusChain) {
1054 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr->view),
1055 tPtr->view->nextFocusChain);
1056 tPtr->flags.notIllegalMovement = 1;
1058 data = (void*)WMTabTextMovement;
1060 textEvent = WMTextDidEndEditingNotification;
1062 cancelSelection = 0;
1064 relay = False;
1066 break;
1068 case XK_Escape:
1069 if (!modified) {
1070 data = (void*)WMEscapeTextMovement;
1071 textEvent = WMTextDidEndEditingNotification;
1073 relay = False;
1075 break;
1077 case XK_Return:
1078 if (!modified) {
1079 data = (void*)WMReturnTextMovement;
1080 textEvent = WMTextDidEndEditingNotification;
1082 relay = False;
1084 break;
1086 case WM_EMACSKEY_LEFT:
1087 if (!control_pressed)
1088 goto normal_key;
1089 else
1090 controled = False;
1092 #ifdef XK_KP_Left
1093 case XK_KP_Left:
1094 #endif
1095 case XK_Left:
1096 if (tPtr->cursorPosition > 0) {
1097 paintCursor(tPtr);
1098 if (controled) {
1099 int i = tPtr->cursorPosition - 1;
1101 while (i > 0 && tPtr->text[i] != ' ') i--;
1102 while (i > 0 && tPtr->text[i] == ' ') i--;
1104 tPtr->cursorPosition = (i > 0) ? i + 1 : 0;
1105 } else
1106 tPtr->cursorPosition--;
1108 if (tPtr->cursorPosition < tPtr->viewPosition) {
1109 tPtr->viewPosition = tPtr->cursorPosition;
1110 refresh = 1;
1111 } else
1112 paintCursor(tPtr);
1114 if (shifted)
1115 cancelSelection = 0;
1117 relay = False;
1119 break;
1121 case WM_EMACSKEY_RIGHT:
1122 if (!control_pressed)
1123 goto normal_key;
1124 else
1125 controled = False;
1127 #ifdef XK_KP_Right
1128 case XK_KP_Right:
1129 #endif
1130 case XK_Right:
1131 if (tPtr->cursorPosition < tPtr->textLen) {
1132 paintCursor(tPtr);
1133 if (controled) {
1134 int i = tPtr->cursorPosition;
1136 while (tPtr->text[i] && tPtr->text[i] != ' ') i++;
1137 while (tPtr->text[i] == ' ') i++;
1139 tPtr->cursorPosition = i;
1140 } else {
1141 tPtr->cursorPosition++;
1143 while (WMWidthOfString(tPtr->font,
1144 &(tPtr->text[tPtr->viewPosition]),
1145 tPtr->cursorPosition-tPtr->viewPosition)
1146 > tPtr->usableWidth) {
1147 tPtr->viewPosition++;
1148 refresh = 1;
1150 if (!refresh)
1151 paintCursor(tPtr);
1153 if (shifted)
1154 cancelSelection = 0;
1156 relay = False;
1158 break;
1160 case WM_EMACSKEY_HOME:
1161 if (!control_pressed)
1162 goto normal_key;
1163 else
1164 controled = False;
1166 #ifdef XK_KP_Home
1167 case XK_KP_Home:
1168 #endif
1169 case XK_Home:
1170 if (!controled) {
1171 if (tPtr->cursorPosition > 0) {
1172 paintCursor(tPtr);
1173 tPtr->cursorPosition = 0;
1174 if (tPtr->viewPosition > 0) {
1175 tPtr->viewPosition = 0;
1176 refresh = 1;
1177 } else
1178 paintCursor(tPtr);
1180 if (shifted)
1181 cancelSelection = 0;
1183 relay = False;
1185 break;
1187 case WM_EMACSKEY_END:
1188 if (!control_pressed)
1189 goto normal_key;
1190 else
1191 controled = False;
1193 #ifdef XK_KP_End
1194 case XK_KP_End:
1195 #endif
1196 case XK_End:
1197 if (!controled) {
1198 if (tPtr->cursorPosition < tPtr->textLen) {
1199 paintCursor(tPtr);
1200 tPtr->cursorPosition = tPtr->textLen;
1201 tPtr->viewPosition = 0;
1202 while (WMWidthOfString(tPtr->font,
1203 &(tPtr->text[tPtr->viewPosition]),
1204 tPtr->textLen-tPtr->viewPosition)
1205 > tPtr->usableWidth) {
1206 tPtr->viewPosition++;
1207 refresh = 1;
1209 if (!refresh)
1210 paintCursor(tPtr);
1212 if (shifted)
1213 cancelSelection = 0;
1215 relay = False;
1217 break;
1219 case WM_EMACSKEY_BS:
1220 if (!control_pressed)
1221 goto normal_key;
1222 else
1223 modified = False;
1225 case XK_BackSpace:
1226 if (!modified) {
1227 if (tPtr->selection.count) {
1228 WMDeleteTextFieldRange(tPtr, tPtr->selection);
1229 data = (void*)WMDeleteTextEvent;
1230 textEvent = WMTextDidChangeNotification;
1231 } else if (tPtr->cursorPosition > 0) {
1232 WMRange range;
1233 range.position = tPtr->cursorPosition - 1;
1234 range.count = 1;
1235 WMDeleteTextFieldRange(tPtr, range);
1236 data = (void*)WMDeleteTextEvent;
1237 textEvent = WMTextDidChangeNotification;
1240 relay = False;
1242 break;
1244 case WM_EMACSKEY_DEL:
1245 if (!control_pressed)
1246 goto normal_key;
1247 else
1248 modified = False;
1250 #ifdef XK_KP_Delete
1251 case XK_KP_Delete:
1252 #endif
1253 case XK_Delete:
1254 if (!modified) {
1255 if (tPtr->selection.count) {
1256 WMDeleteTextFieldRange(tPtr, tPtr->selection);
1257 data = (void*)WMDeleteTextEvent;
1258 textEvent = WMTextDidChangeNotification;
1259 } else if (tPtr->cursorPosition < tPtr->textLen) {
1260 WMRange range;
1261 range.position = tPtr->cursorPosition;
1262 range.count = 1;
1263 WMDeleteTextFieldRange(tPtr, range);
1264 data = (void*)WMDeleteTextEvent;
1265 textEvent = WMTextDidChangeNotification;
1268 relay = False;
1270 break;
1272 normal_key:
1273 default:
1274 if (!controled) {
1275 if (count > 0 && !iscntrl(buffer[0])) {
1276 if (tPtr->selection.count)
1277 WMDeleteTextFieldRange(tPtr, tPtr->selection);
1278 WMInsertTextFieldText(tPtr, buffer, tPtr->cursorPosition);
1279 data = (void*)WMInsertTextEvent;
1280 textEvent = WMTextDidChangeNotification;
1282 relay = False;
1285 break;
1288 if (relay) {
1289 WMRelayToNextResponder(W_VIEW(tPtr), event);
1290 return;
1293 /* Do not allow text selection in secure text fields */
1294 if (cancelSelection || tPtr->flags.secure) {
1295 lostSelection(tPtr->view, XA_PRIMARY, NULL);
1297 if (tPtr->selection.count) {
1298 tPtr->selection.count = 0;
1299 refresh = 1;
1301 tPtr->selection.position = tPtr->cursorPosition;
1302 } else {
1303 if (tPtr->selection.count != tPtr->cursorPosition - tPtr->selection.position) {
1305 tPtr->selection.count = tPtr->cursorPosition - tPtr->selection.position;
1307 refresh = 1;
1311 /*printf("(%d,%d)\n", tPtr->selection.position, tPtr->selection.count);*/
1313 if (textEvent) {
1314 WMNotification *notif = WMCreateNotification(textEvent, tPtr, data);
1316 if (tPtr->delegate) {
1317 if (textEvent==WMTextDidBeginEditingNotification &&
1318 tPtr->delegate->didBeginEditing)
1319 (*tPtr->delegate->didBeginEditing)(tPtr->delegate, notif);
1321 else if (textEvent==WMTextDidEndEditingNotification &&
1322 tPtr->delegate->didEndEditing)
1323 (*tPtr->delegate->didEndEditing)(tPtr->delegate, notif);
1325 else if (textEvent==WMTextDidChangeNotification &&
1326 tPtr->delegate->didChange)
1327 (*tPtr->delegate->didChange)(tPtr->delegate, notif);
1330 WMPostNotification(notif);
1331 WMReleaseNotification(notif);
1334 if (refresh)
1335 paintTextField(tPtr);
1337 /*printf("(%d,%d)\n", tPtr->selection.position, tPtr->selection.count);*/
1341 static int
1342 pointToCursorPosition(TextField *tPtr, int x)
1344 int a, b, mid;
1345 int tw;
1347 if (tPtr->flags.bordered)
1348 x -= 2;
1350 a = tPtr->viewPosition;
1351 b = tPtr->viewPosition + tPtr->textLen;
1352 if (WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
1353 tPtr->textLen - tPtr->viewPosition) < x)
1354 return tPtr->textLen;
1356 while (a < b && b-a>1) {
1357 mid = (a+b)/2;
1358 tw = WMWidthOfString(tPtr->font, &(tPtr->text[tPtr->viewPosition]),
1359 mid - tPtr->viewPosition);
1360 if (tw > x)
1361 b = mid;
1362 else if (tw < x)
1363 a = mid;
1364 else
1365 return mid;
1367 return (a+b)/2;
1372 static void
1373 pasteText(WMView *view, Atom selection, Atom target, Time timestamp,
1374 void *cdata, WMData *data)
1376 TextField *tPtr = (TextField*)view->self;
1377 char *str;
1379 tPtr->flags.waitingSelection = 0;
1381 if (data != NULL) {
1382 str = (char*)WMDataBytes(data);
1384 WMInsertTextFieldText(tPtr, str, tPtr->cursorPosition);
1385 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
1386 (void*)WMInsertTextEvent);
1387 } else {
1388 int n;
1390 str = XFetchBuffer(tPtr->view->screen->display, &n, 0);
1392 if (str != NULL) {
1393 str[n] = 0;
1394 WMInsertTextFieldText(tPtr, str, tPtr->cursorPosition);
1395 XFree(str);
1396 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
1397 (void*)WMInsertTextEvent);
1403 static void
1404 handleTextFieldActionEvents(XEvent *event, void *data)
1406 TextField *tPtr = (TextField*)data;
1407 static int move = 0;
1408 static Time lastButtonReleasedEvent = 0;
1409 Display *dpy = event->xany.display;
1411 CHECK_CLASS(data, WC_TextField);
1413 switch (event->type) {
1414 case KeyPress:
1415 if (tPtr->flags.waitingSelection) {
1416 return;
1418 if (tPtr->flags.enabled && tPtr->flags.focused) {
1419 handleTextFieldKeyPress(tPtr, event);
1420 XDefineCursor(dpy, W_VIEW(tPtr)->window,
1421 W_VIEW(tPtr)->screen->invisibleCursor);
1422 tPtr->flags.pointerGrabbed = 1;
1424 break;
1426 case MotionNotify:
1428 if (tPtr->flags.pointerGrabbed) {
1429 tPtr->flags.pointerGrabbed = 0;
1430 XDefineCursor(dpy, W_VIEW(tPtr)->window,
1431 W_VIEW(tPtr)->screen->textCursor);
1433 if (tPtr->flags.waitingSelection) {
1434 return;
1437 if (tPtr->flags.enabled && (event->xmotion.state & Button1Mask)) {
1439 if (tPtr->viewPosition < tPtr->textLen && event->xmotion.x >
1440 tPtr->usableWidth) {
1441 if (WMWidthOfString(tPtr->font,
1442 &(tPtr->text[tPtr->viewPosition]),
1443 tPtr->cursorPosition-tPtr->viewPosition)
1444 > tPtr->usableWidth) {
1445 tPtr->viewPosition++;
1447 } else if (tPtr->viewPosition > 0 && event->xmotion.x < 0) {
1448 paintCursor(tPtr);
1449 tPtr->viewPosition--;
1452 tPtr->cursorPosition =
1453 pointToCursorPosition(tPtr, event->xmotion.x);
1455 /* Do not allow text selection in secure textfields */
1456 if (tPtr->flags.secure) {
1457 tPtr->selection.position = tPtr->cursorPosition;
1460 tPtr->selection.count = tPtr->cursorPosition - tPtr->selection.position;
1462 paintCursor(tPtr);
1463 paintTextField(tPtr);
1466 break;
1468 case ButtonPress:
1469 if (tPtr->flags.pointerGrabbed) {
1470 tPtr->flags.pointerGrabbed = 0;
1471 XDefineCursor(dpy, W_VIEW(tPtr)->window,
1472 W_VIEW(tPtr)->screen->textCursor);
1473 break;
1476 if (tPtr->flags.waitingSelection) {
1477 break;
1480 move = 1;
1481 switch (tPtr->flags.alignment) {
1482 int textWidth;
1483 case WARight:
1484 textWidth = WMWidthOfString(tPtr->font, tPtr->text, tPtr->textLen);
1485 if (tPtr->flags.enabled && !tPtr->flags.focused) {
1486 WMSetFocusToWidget(tPtr);
1488 if (tPtr->flags.focused) {
1489 tPtr->selection.position = tPtr->cursorPosition;
1490 tPtr->selection.count = 0;
1492 if (textWidth < tPtr->usableWidth) {
1493 tPtr->cursorPosition = pointToCursorPosition(tPtr,
1494 event->xbutton.x - tPtr->usableWidth
1495 + textWidth);
1496 } else tPtr->cursorPosition = pointToCursorPosition(tPtr,
1497 event->xbutton.x);
1499 paintTextField(tPtr);
1500 break;
1502 case WALeft:
1503 if (tPtr->flags.enabled && !tPtr->flags.focused) {
1504 WMSetFocusToWidget(tPtr);
1506 if (tPtr->flags.focused && event->xbutton.button == Button1) {
1507 tPtr->cursorPosition = pointToCursorPosition(tPtr,
1508 event->xbutton.x);
1509 tPtr->selection.position = tPtr->cursorPosition;
1510 tPtr->selection.count = 0;
1511 paintTextField(tPtr);
1513 if (event->xbutton.button == Button2 && tPtr->flags.enabled) {
1514 char *text;
1515 int n;
1517 if (!WMRequestSelection(tPtr->view, XA_PRIMARY, XA_STRING,
1518 event->xbutton.time,
1519 pasteText, NULL)) {
1520 text = XFetchBuffer(tPtr->view->screen->display, &n, 0);
1522 if (text) {
1523 text[n] = 0;
1524 WMInsertTextFieldText(tPtr, text, tPtr->cursorPosition);
1525 XFree(text);
1526 NOTIFY(tPtr, didChange, WMTextDidChangeNotification,
1527 (void*)WMInsertTextEvent);
1529 } else {
1530 tPtr->flags.waitingSelection = 1;
1533 break;
1534 default:
1535 break;
1537 break;
1539 case ButtonRelease:
1540 if (tPtr->flags.pointerGrabbed) {
1541 tPtr->flags.pointerGrabbed = 0;
1542 XDefineCursor(dpy, W_VIEW(tPtr)->window,
1543 W_VIEW(tPtr)->screen->textCursor);
1545 if (tPtr->flags.waitingSelection) {
1546 break;
1549 if (!tPtr->flags.secure && tPtr->selection.count!=0) {
1550 int start, count;
1551 XRotateBuffers(dpy, 1);
1553 count = abs(tPtr->selection.count);
1554 if (tPtr->selection.count < 0)
1555 start = tPtr->selection.position - count;
1556 else
1557 start = tPtr->selection.position;
1559 XStoreBuffer(dpy, &tPtr->text[start], count, 0);
1562 move = 0;
1564 if (!tPtr->flags.secure &&
1565 event->xbutton.time - lastButtonReleasedEvent
1566 <= WINGsConfiguration.doubleClickDelay) {
1567 tPtr->selection.position = 0;
1568 tPtr->selection.count = tPtr->textLen;
1569 paintTextField(tPtr);
1571 if (!tPtr->flags.ownsSelection) {
1572 tPtr->flags.ownsSelection =
1573 WMCreateSelectionHandler(tPtr->view,
1574 XA_PRIMARY,
1575 event->xbutton.time,
1576 &selectionHandler, NULL);
1578 } else if (!tPtr->flags.secure && tPtr->selection.count!=0 &&
1579 !tPtr->flags.ownsSelection) {
1580 tPtr->flags.ownsSelection =
1581 WMCreateSelectionHandler(tPtr->view,
1582 XA_PRIMARY,
1583 event->xbutton.time,
1584 &selectionHandler, NULL);
1587 lastButtonReleasedEvent = event->xbutton.time;
1589 break;
1594 static void
1595 destroyTextField(TextField *tPtr)
1597 #if 0
1598 if (tPtr->timerID)
1599 WMDeleteTimerHandler(tPtr->timerID);
1600 #endif
1602 WMReleaseFont(tPtr->font);
1603 /*// use lostSelection() instead of WMDeleteSelectionHandler here?*/
1604 WMDeleteSelectionHandler(tPtr->view, XA_PRIMARY, CurrentTime);
1605 WMRemoveNotificationObserver(tPtr);
1607 if (tPtr->text)
1608 wfree(tPtr->text);
1610 wfree(tPtr);