7 #include <X11/keysym.h>
12 #define CURSOR_BLINK_ON_DELAY 600
13 #define CURSOR_BLINK_OFF_DELAY 300
17 char *WMTextDidChangeNotification
= "WMTextDidChangeNotification";
18 char *WMTextDidBeginEditingNotification
= "WMTextDidBeginEditingNotification";
19 char *WMTextDidEndEditingNotification
= "WMTextDidEndEditingNotification";
22 typedef struct W_TextField
{
27 struct W_TextField
*nextField
; /* next textfield in the chain */
28 struct W_TextField
*prevField
;
32 int textLen
; /* size of text */
33 int bufferSize
; /* memory allocated for text */
35 int viewPosition
; /* position of text being shown */
37 int cursorPosition
; /* position of the insertion cursor */
40 short offsetWidth
; /* offset of text from border */
46 WMTextFieldDelegate
*delegate
;
49 WMHandlerID timerID
; /* for cursor blinking */
52 WMAlignment alignment
:2;
54 unsigned int bordered
:1;
56 unsigned int beveled
:1;
58 unsigned int enabled
:1;
60 unsigned int focused
:1;
62 unsigned int cursorOn
:1;
64 unsigned int secure
:1; /* password entry style */
66 unsigned int pointerGrabbed
:1;
68 unsigned int ownsSelection
:1;
70 unsigned int waitingSelection
:1; /* requested selection, but
74 unsigned int notIllegalMovement
:1;
79 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
80 if ((T)->delegate && (T)->delegate->C)\
81 (*(T)->delegate->C)((T)->delegate,notif);\
82 WMPostNotification(notif);\
83 WMReleaseNotification(notif);}
86 #define MIN_TEXT_BUFFER 2
87 #define TEXT_BUFFER_INCR 8
90 #define WM_EMACSKEYMASK ControlMask
92 #define WM_EMACSKEY_LEFT XK_b
93 #define WM_EMACSKEY_RIGHT XK_f
94 #define WM_EMACSKEY_HOME XK_a
95 #define WM_EMACSKEY_END XK_e
96 #define WM_EMACSKEY_BS XK_h
97 #define WM_EMACSKEY_DEL XK_d
101 #define DEFAULT_WIDTH 60
102 #define DEFAULT_HEIGHT 20
103 #define DEFAULT_BORDERED True
104 #define DEFAULT_ALIGNMENT WALeft
108 static void destroyTextField(TextField
*tPtr
);
109 static void paintTextField(TextField
*tPtr
);
111 static void handleEvents(XEvent
*event
, void *data
);
112 static void handleTextFieldActionEvents(XEvent
*event
, void *data
);
113 static void didResizeTextField();
115 struct W_ViewDelegate _TextFieldViewDelegate
= {
125 static void lostHandler(WMView
*view
, Atom selection
, void *cdata
);
127 static WMData
*requestHandler(WMView
*view
, Atom selection
, Atom target
,
128 void *cdata
, Atom
*type
);
131 static WMSelectionProcs selectionHandler
= {
138 #define TEXT_WIDTH(tPtr, start) (WMWidthOfString((tPtr)->font, \
139 &((tPtr)->text[(start)]), (tPtr)->textLen - (start) + 1))
141 #define TEXT_WIDTH2(tPtr, start, end) (WMWidthOfString((tPtr)->font, \
142 &((tPtr)->text[(start)]), (end) - (start) + 1))
146 normalizeRange(TextField
*tPtr
, WMRange
*range
)
148 if (range
->position
< 0 && range
->count
< 0)
151 if (range
->count
== 0) {
152 /*range->position = 0; why is this?*/
156 /* (1,-2) ~> (0,1) ; (1,-1) ~> (0,1) ; (2,-1) ~> (1,1) */
157 if (range
->count
< 0) { /* && range->position >= 0 */
158 if (range
->position
+ range
->count
< 0) {
159 range
->count
= range
->position
;
162 range
->count
= -range
->count
;
163 range
->position
-= range
->count
;
165 /* (-2,1) ~> (0,0) ; (-1,1) ~> (0,0) ; (-1,2) ~> (0,1) */
166 } else if (range
->position
< 0) { /* && range->count > 0 */
167 if (range
->position
+ range
->count
< 0) {
168 range
->position
= range
->count
= 0;
170 range
->count
+= range
->position
;
175 if (range
->position
+ range
->count
> tPtr
->textLen
)
176 range
->count
= tPtr
->textLen
- range
->position
;
180 memmv(char *dest
, char *src
, int size
)
185 for (i
=size
-1; i
>=0; i
--) {
188 } else if (dest
< src
) {
189 for (i
=0; i
<size
; i
++) {
197 incrToFit(TextField
*tPtr
)
199 int vp
= tPtr
->viewPosition
;
201 while (TEXT_WIDTH(tPtr
, tPtr
->viewPosition
) > tPtr
->usableWidth
) {
202 tPtr
->viewPosition
++;
204 return vp
!=tPtr
->viewPosition
;
209 incrToFit2(TextField
*tPtr
)
211 int vp
= tPtr
->viewPosition
;
212 while (TEXT_WIDTH2(tPtr
, tPtr
->viewPosition
, tPtr
->cursorPosition
)
213 >= tPtr
->usableWidth
)
214 tPtr
->viewPosition
++;
217 return vp
!=tPtr
->viewPosition
;
222 decrToFit(TextField
*tPtr
)
224 while (TEXT_WIDTH(tPtr
, tPtr
->viewPosition
-1) < tPtr
->usableWidth
225 && tPtr
->viewPosition
>0)
226 tPtr
->viewPosition
--;
235 requestHandler(WMView
*view
, Atom selection
, Atom target
, void *cdata
,
238 TextField
*tPtr
= view
->self
;
240 Display
*dpy
= tPtr
->view
->screen
->display
;
242 Atom TEXT
= XInternAtom(dpy
, "TEXT", False
);
243 Atom COMPOUND_TEXT
= XInternAtom(dpy
, "COMPOUND_TEXT", False
);
246 count
= tPtr
->selection
.count
< 0
247 ? tPtr
->selection
.position
+ tPtr
->selection
.count
248 : tPtr
->selection
.position
;
250 if (target
== XA_STRING
|| target
== TEXT
|| target
== COMPOUND_TEXT
) {
252 data
= WMCreateDataWithBytes(&(tPtr
->text
[count
]),
253 abs(tPtr
->selection
.count
));
254 WMSetDataFormat(data
, 8);
260 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
261 if (target
== _TARGETS
) {
264 ptr
= wmalloc(4 * sizeof(Atom
));
268 ptr
[3] = COMPOUND_TEXT
;
270 data
= WMCreateDataWithBytes(ptr
, 4*4);
271 WMSetDataFormat(data
, 32);
283 lostHandler(WMView
*view
, Atom selection
, void *cdata
)
285 TextField
*tPtr
= (WMTextField
*)view
->self
;
287 tPtr
->flags
.ownsSelection
= 0;
288 tPtr
->selection
.count
= 0;
289 paintTextField(tPtr
);
294 _notification(void *observerData
, WMNotification
*notification
)
296 WMTextField
*to
= (WMTextField
*)observerData
;
297 WMTextField
*tw
= (WMTextField
*)WMGetNotificationClientData(notification
);
298 if (to
!= tw
) lostHandler(to
->view
, XA_PRIMARY
, NULL
);
302 WMCreateTextField(WMWidget
*parent
)
306 tPtr
= wmalloc(sizeof(TextField
));
307 memset(tPtr
, 0, sizeof(TextField
));
309 tPtr
->widgetClass
= WC_TextField
;
311 tPtr
->view
= W_CreateView(W_VIEW(parent
));
316 tPtr
->view
->self
= tPtr
;
318 tPtr
->view
->delegate
= &_TextFieldViewDelegate
;
320 tPtr
->view
->attribFlags
|= CWCursor
;
321 tPtr
->view
->attribs
.cursor
= tPtr
->view
->screen
->textCursor
;
323 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->view
->screen
->white
);
325 tPtr
->text
= wmalloc(MIN_TEXT_BUFFER
);
328 tPtr
->bufferSize
= MIN_TEXT_BUFFER
;
330 tPtr
->flags
.enabled
= 1;
332 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
333 |FocusChangeMask
, handleEvents
, tPtr
);
335 tPtr
->font
= WMRetainFont(tPtr
->view
->screen
->normalFont
);
337 tPtr
->flags
.bordered
= DEFAULT_BORDERED
;
338 tPtr
->flags
.beveled
= True
;
339 tPtr
->flags
.alignment
= DEFAULT_ALIGNMENT
;
341 WMAX((tPtr
->view
->size
.height
- WMFontHeight(tPtr
->font
))/2, 1);
343 W_ResizeView(tPtr
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
345 WMCreateEventHandler(tPtr
->view
, EnterWindowMask
|LeaveWindowMask
346 |ButtonReleaseMask
|ButtonPressMask
|KeyPressMask
|Button1MotionMask
,
347 handleTextFieldActionEvents
, tPtr
);
349 WMAddNotificationObserver(_notification
, tPtr
, "_lostOwnership", tPtr
);
352 tPtr
->flags
.cursorOn
= 1;
359 WMSetTextFieldDelegate(WMTextField
*tPtr
, WMTextFieldDelegate
*delegate
)
361 CHECK_CLASS(tPtr
, WC_TextField
);
363 tPtr
->delegate
= delegate
;
368 WMInsertTextFieldText(WMTextField
*tPtr
, char *text
, int position
)
372 CHECK_CLASS(tPtr
, WC_TextField
);
379 /* check if buffer will hold the text */
380 if (len
+ tPtr
->textLen
>= tPtr
->bufferSize
) {
381 tPtr
->bufferSize
= tPtr
->textLen
+ len
+ TEXT_BUFFER_INCR
;
382 tPtr
->text
= wrealloc(tPtr
->text
, tPtr
->bufferSize
);
385 if (position
< 0 || position
>= tPtr
->textLen
) {
386 /* append the text at the end */
387 strcat(tPtr
->text
, text
);
391 tPtr
->textLen
+= len
;
392 tPtr
->cursorPosition
+= len
;
394 /* insert text at position */
395 memmv(&(tPtr
->text
[position
+len
]), &(tPtr
->text
[position
]),
396 tPtr
->textLen
-position
+1);
398 memcpy(&(tPtr
->text
[position
]), text
, len
);
400 tPtr
->textLen
+= len
;
401 if (position
>= tPtr
->cursorPosition
) {
402 tPtr
->cursorPosition
+= len
;
409 paintTextField(tPtr
);
413 WMDeleteTextFieldRange(WMTextField
*tPtr
, WMRange range
)
415 CHECK_CLASS(tPtr
, WC_TextField
);
417 normalizeRange(tPtr
, &range
);
422 memmv(&(tPtr
->text
[range
.position
]), &(tPtr
->text
[range
.position
+range
.count
]),
423 tPtr
->textLen
- (range
.position
+range
.count
) + 1);
425 tPtr
->textLen
-= range
.count
;
427 /* try to keep cursorPosition at the same place */
428 tPtr
->viewPosition
-= range
.count
;
429 if (tPtr
->viewPosition
< 0)
430 tPtr
->viewPosition
= 0;
431 tPtr
->cursorPosition
= range
.position
;
435 paintTextField(tPtr
);
441 WMGetTextFieldText(WMTextField
*tPtr
)
443 CHECK_CLASS(tPtr
, WC_TextField
);
445 return wstrdup(tPtr
->text
);
450 WMSetTextFieldText(WMTextField
*tPtr
, char *text
)
452 CHECK_CLASS(tPtr
, WC_TextField
);
454 if ((text
&& strcmp(tPtr
->text
, text
) == 0) ||
455 (!text
&& tPtr
->textLen
== 0))
462 tPtr
->textLen
= strlen(text
);
464 if (tPtr
->textLen
>= tPtr
->bufferSize
) {
465 tPtr
->bufferSize
= tPtr
->textLen
+ TEXT_BUFFER_INCR
;
466 tPtr
->text
= wrealloc(tPtr
->text
, tPtr
->bufferSize
);
468 strcpy(tPtr
->text
, text
);
471 tPtr
->cursorPosition
= tPtr
->selection
.position
= tPtr
->textLen
;
472 tPtr
->viewPosition
= 0;
473 tPtr
->selection
.count
= 0;
475 if (tPtr
->view
->flags
.realized
)
476 paintTextField(tPtr
);
481 WMSetTextFieldAlignment(WMTextField
*tPtr
, WMAlignment alignment
)
483 CHECK_CLASS(tPtr
, WC_TextField
);
485 tPtr
->flags
.alignment
= alignment
;
487 if (alignment
!=WALeft
) {
488 wwarning("only left alignment is supported in textfields");
492 if (tPtr
->view
->flags
.realized
) {
493 paintTextField(tPtr
);
499 WMSetTextFieldBordered(WMTextField
*tPtr
, Bool bordered
)
501 CHECK_CLASS(tPtr
, WC_TextField
);
503 tPtr
->flags
.bordered
= bordered
;
505 if (tPtr
->view
->flags
.realized
) {
506 paintTextField(tPtr
);
512 WMSetTextFieldBeveled(WMTextField
*tPtr
, Bool flag
)
514 CHECK_CLASS(tPtr
, WC_TextField
);
516 tPtr
->flags
.beveled
= flag
;
518 if (tPtr
->view
->flags
.realized
) {
519 paintTextField(tPtr
);
526 WMSetTextFieldSecure(WMTextField
*tPtr
, Bool flag
)
528 CHECK_CLASS(tPtr
, WC_TextField
);
530 tPtr
->flags
.secure
= flag
;
532 if (tPtr
->view
->flags
.realized
) {
533 paintTextField(tPtr
);
539 WMGetTextFieldEditable(WMTextField
*tPtr
)
541 CHECK_CLASS(tPtr
, WC_TextField
);
543 return tPtr
->flags
.enabled
;
548 WMSetTextFieldEditable(WMTextField
*tPtr
, Bool flag
)
550 CHECK_CLASS(tPtr
, WC_TextField
);
552 tPtr
->flags
.enabled
= flag
;
554 if (tPtr
->view
->flags
.realized
) {
555 paintTextField(tPtr
);
561 WMSelectTextFieldRange(WMTextField
*tPtr
, WMRange range
)
563 CHECK_CLASS(tPtr
, WC_TextField
);
565 if (tPtr
->flags
.enabled
) {
566 normalizeRange(tPtr
, &range
);
568 tPtr
->selection
= range
;
570 tPtr
->cursorPosition
= range
.position
+ range
.count
;
572 if (tPtr
->view
->flags
.realized
) {
573 paintTextField(tPtr
);
580 WMSetTextFieldCursorPosition(WMTextField
*tPtr
, unsigned int position
)
582 CHECK_CLASS(tPtr
, WC_TextField
);
584 if (tPtr
->flags
.enabled
) {
585 if (position
> tPtr
->textLen
)
586 position
= tPtr
->textLen
;
588 tPtr
->cursorPosition
= position
;
589 if (tPtr
->view
->flags
.realized
) {
590 paintTextField(tPtr
);
597 WMSetTextFieldNextTextField(WMTextField
*tPtr
, WMTextField
*next
)
599 CHECK_CLASS(tPtr
, WC_TextField
);
601 if (tPtr
->view
->nextFocusChain
)
602 tPtr
->view
->nextFocusChain
->prevFocusChain
= NULL
;
603 tPtr
->view
->nextFocusChain
= NULL
;
607 CHECK_CLASS(next
, WC_TextField
);
609 if (tPtr
->view
->nextFocusChain
)
610 tPtr
->view
->nextFocusChain
->prevFocusChain
= NULL
;
611 if (next
->view
->prevFocusChain
)
612 next
->view
->prevFocusChain
->nextFocusChain
= NULL
;
614 tPtr
->view
->nextFocusChain
= next
->view
;
615 next
->view
->prevFocusChain
= tPtr
->view
;
620 WMSetTextFieldPrevTextField(WMTextField
*tPtr
, WMTextField
*prev
)
622 CHECK_CLASS(tPtr
, WC_TextField
);
624 if (tPtr
->view
->prevFocusChain
)
625 tPtr
->view
->prevFocusChain
->nextFocusChain
= NULL
;
626 tPtr
->view
->prevFocusChain
= NULL
;
630 CHECK_CLASS(prev
, WC_TextField
);
632 if (tPtr
->view
->prevFocusChain
)
633 tPtr
->view
->prevFocusChain
->nextFocusChain
= NULL
;
634 if (prev
->view
->nextFocusChain
)
635 prev
->view
->nextFocusChain
->prevFocusChain
= NULL
;
637 tPtr
->view
->prevFocusChain
= prev
->view
;
638 prev
->view
->nextFocusChain
= tPtr
->view
;
643 WMSetTextFieldFont(WMTextField
*tPtr
, WMFont
*font
)
645 CHECK_CLASS(tPtr
, WC_TextField
);
648 WMReleaseFont(tPtr
->font
);
649 tPtr
->font
= WMRetainFont(font
);
652 WMAX((tPtr
->view
->size
.height
- WMFontHeight(tPtr
->font
))/2, 1);
654 if (tPtr
->view
->flags
.realized
) {
655 paintTextField(tPtr
);
662 WMGetTextFieldFont(WMTextField
*tPtr
)
669 didResizeTextField(W_ViewDelegate
*self
, WMView
*view
)
671 WMTextField
*tPtr
= (WMTextField
*)view
->self
;
674 WMAX((tPtr
->view
->size
.height
- WMFontHeight(tPtr
->font
))/2, 1);
676 tPtr
->usableWidth
= tPtr
->view
->size
.width
- 2*tPtr
->offsetWidth
+ 2;
681 makeHiddenString(int length
)
683 char *data
= wmalloc(length
+1);
685 memset(data
, '*', length
);
693 paintCursor(TextField
*tPtr
)
696 WMScreen
*screen
= tPtr
->view
->screen
;
700 if (tPtr
->flags
.secure
)
701 text
= makeHiddenString(strlen(tPtr
->text
));
705 cx
= WMWidthOfString(tPtr
->font
, &(text
[tPtr
->viewPosition
]),
706 tPtr
->cursorPosition
-tPtr
->viewPosition
);
708 switch (tPtr
->flags
.alignment
) {
710 textWidth
= WMWidthOfString(tPtr
->font
, text
, tPtr
->textLen
);
711 if (textWidth
< tPtr
->usableWidth
)
712 cx
+= tPtr
->offsetWidth
+ tPtr
->usableWidth
- textWidth
+ 1;
714 cx
+= tPtr
->offsetWidth
+ 1;
717 cx
+= tPtr
->offsetWidth
+ 1;
722 textWidth
= WMWidthOfString(tPtr
->font
, text
, tPtr
->textLen
);
723 if (textWidth
< tPtr
->usableWidth
)
724 cx
+= tPtr
->offsetWidth
+ (tPtr
->usableWidth
-textWidth
)/2;
726 cx
+= tPtr
->offsetWidth
;
730 XDrawRectangle(screen->display, tPtr->view->window, screen->xorGC,
731 cx, tPtr->offsetWidth, 1,
732 tPtr->view->size.height - 2*tPtr->offsetWidth - 1);
733 printf("%d %d\n",cx,tPtr->cursorPosition);
735 XDrawLine(screen
->display
, tPtr
->view
->window
, screen
->xorGC
,
736 cx
, tPtr
->offsetWidth
, cx
,
737 tPtr
->view
->size
.height
- tPtr
->offsetWidth
- 1);
739 if (tPtr
->flags
.secure
)
746 drawRelief(WMView
*view
, Bool beveled
)
748 WMScreen
*scr
= view
->screen
;
749 Display
*dpy
= scr
->display
;
753 int width
= view
->size
.width
;
754 int height
= view
->size
.height
;
756 dgc
= WMColorGC(scr
->darkGray
);
759 XDrawRectangle(dpy
, view
->window
, dgc
, 0, 0, width
-1, height
-1);
763 wgc
= WMColorGC(scr
->white
);
764 lgc
= WMColorGC(scr
->gray
);
767 XDrawLine(dpy
, view
->window
, dgc
, 0, 0, width
-1, 0);
768 XDrawLine(dpy
, view
->window
, dgc
, 0, 1, width
-2, 1);
770 XDrawLine(dpy
, view
->window
, dgc
, 0, 0, 0, height
-2);
771 XDrawLine(dpy
, view
->window
, dgc
, 1, 0, 1, height
-3);
774 XDrawLine(dpy
, view
->window
, wgc
, 0, height
-1, width
-1, height
-1);
775 XDrawLine(dpy
, view
->window
, lgc
, 1, height
-2, width
-2, height
-2);
777 XDrawLine(dpy
, view
->window
, wgc
, width
-1, 0, width
-1, height
-1);
778 XDrawLine(dpy
, view
->window
, lgc
, width
-2, 1, width
-2, height
-3);
783 paintTextField(TextField
*tPtr
)
785 W_Screen
*screen
= tPtr
->view
->screen
;
786 W_View
*view
= tPtr
->view
;
796 if (!view
->flags
.realized
|| !view
->flags
.mapped
)
799 if (!tPtr
->flags
.bordered
) {
805 if (tPtr
->flags
.secure
) {
806 text
= makeHiddenString(strlen(tPtr
->text
));
811 totalWidth
= tPtr
->view
->size
.width
- 2*bd
;
813 drawbuffer
= XCreatePixmap(screen
->display
, view
->window
,
814 view
->size
.width
, view
->size
.height
, screen
->depth
);
815 XFillRectangle(screen
->display
, drawbuffer
, WMColorGC(screen
->white
),
816 0,0, view
->size
.width
,view
->size
.height
);
817 /* this is quite dirty */
818 viewbuffer
.screen
= view
->screen
;
819 viewbuffer
.size
= view
->size
;
820 viewbuffer
.window
= drawbuffer
;
823 if (tPtr
->textLen
> 0) {
824 tw
= WMWidthOfString(tPtr
->font
, &(text
[tPtr
->viewPosition
]),
825 tPtr
->textLen
- tPtr
->viewPosition
);
827 th
= WMFontHeight(tPtr
->font
);
829 ty
= tPtr
->offsetWidth
;
830 switch (tPtr
->flags
.alignment
) {
832 tx
= tPtr
->offsetWidth
+ 1;
833 if (tw
< tPtr
->usableWidth
)
834 XFillRectangle(screen
->display
, drawbuffer
,
835 WMColorGC(screen
->white
),
836 bd
+tw
,bd
, totalWidth
-tw
,view
->size
.height
-2*bd
);
840 tx
= tPtr
->offsetWidth
+ (tPtr
->usableWidth
- tw
) / 2;
841 if (tw
< tPtr
->usableWidth
)
842 XClearArea(screen
->display
, view
->window
, bd
, bd
,
843 totalWidth
, view
->size
.height
-2*bd
, False
);
848 tx
= tPtr
->offsetWidth
+ tPtr
->usableWidth
- tw
- 1;
849 if (tw
< tPtr
->usableWidth
)
850 XClearArea(screen
->display
, view
->window
, bd
, bd
,
851 totalWidth
-tw
, view
->size
.height
-2*bd
, False
);
855 if (!tPtr
->flags
.enabled
)
856 WMSetColorInGC(screen
->darkGray
, screen
->textFieldGC
);
858 WMDrawImageString(screen
, drawbuffer
, screen
->textFieldGC
,
860 &(text
[tPtr
->viewPosition
]),
861 tPtr
->textLen
- tPtr
->viewPosition
);
863 if (tPtr
->selection
.count
) {
866 count
= tPtr
->selection
.count
< 0
867 ? tPtr
->selection
.position
+ tPtr
->selection
.count
868 : tPtr
->selection
.position
;
869 count2
= abs(tPtr
->selection
.count
);
870 if (count
< tPtr
->viewPosition
) {
871 count2
= abs(count2
- abs(tPtr
->viewPosition
- count
));
872 count
= tPtr
->viewPosition
;
876 rx
= tPtr
->offsetWidth
+ 1 + WMWidthOfString(tPtr
->font
,text
,count
)
877 - WMWidthOfString(tPtr
->font
,text
,tPtr
->viewPosition
);
879 XSetBackground(screen
->display
, screen
->textFieldGC
,
880 screen
->gray
->color
.pixel
);
882 WMDrawImageString(screen
, drawbuffer
, screen
->textFieldGC
,
883 tPtr
->font
, rx
, ty
, &(text
[count
]),
886 XSetBackground(screen
->display
, screen
->textFieldGC
,
887 screen
->white
->color
.pixel
);
890 if (!tPtr
->flags
.enabled
)
891 WMSetColorInGC(screen
->black
, screen
->textFieldGC
);
893 XFillRectangle(screen
->display
, drawbuffer
,
894 WMColorGC(screen
->white
),
895 bd
,bd
, totalWidth
,view
->size
.height
-2*bd
);
899 if (tPtr
->flags
.bordered
) {
900 drawRelief(&viewbuffer
, tPtr
->flags
.beveled
);
903 if (tPtr
->flags
.secure
)
905 XCopyArea(screen
->display
, drawbuffer
, view
->window
,
906 screen
->copyGC
, 0,0, view
->size
.width
,
907 view
->size
.height
,0,0);
908 XFreePixmap(screen
->display
, drawbuffer
);
911 if (tPtr
->flags
.focused
&& tPtr
->flags
.enabled
&& tPtr
->flags
.cursorOn
) {
919 blinkCursor(void *data
)
921 TextField
*tPtr
= (TextField
*)data
;
923 if (tPtr
->flags
.cursorOn
) {
924 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
, blinkCursor
,
927 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
, blinkCursor
,
931 tPtr
->flags
.cursorOn
= !tPtr
->flags
.cursorOn
;
937 handleEvents(XEvent
*event
, void *data
)
939 TextField
*tPtr
= (TextField
*)data
;
941 CHECK_CLASS(data
, WC_TextField
);
944 switch (event
->type
) {
946 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))!=tPtr
->view
)
948 tPtr
->flags
.focused
= 1;
950 if (!tPtr
->timerID
) {
951 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
,
955 paintTextField(tPtr
);
957 NOTIFY(tPtr
, didBeginEditing
, WMTextDidBeginEditingNotification
, NULL
);
959 tPtr
->flags
.notIllegalMovement
= 0;
963 tPtr
->flags
.focused
= 0;
966 WMDeleteTimerHandler(tPtr
->timerID
);
967 tPtr
->timerID
= NULL
;
970 paintTextField(tPtr
);
971 if (!tPtr
->flags
.notIllegalMovement
) {
972 NOTIFY(tPtr
, didEndEditing
, WMTextDidEndEditingNotification
,
973 (void*)WMIllegalTextMovement
);
978 if (event
->xexpose
.count
!=0)
980 paintTextField(tPtr
);
984 destroyTextField(tPtr
);
991 handleTextFieldKeyPress(TextField
*tPtr
, XEvent
*event
)
995 char *textEvent
= NULL
;
997 int count
, refresh
= 0;
998 int control_pressed
= 0;
999 int cancelSelection
= 1;
1001 /*printf("(%d,%d) -> ", tPtr->selection.position, tPtr->selection.count);*/
1002 if (((XKeyEvent
*) event
)->state
& WM_EMACSKEYMASK
)
1003 control_pressed
= 1;
1005 count
= XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
);
1006 buffer
[count
] = '\0';
1010 #ifdef XK_ISO_Left_Tab
1011 case XK_ISO_Left_Tab
:
1013 if (event
->xkey
.state
& ShiftMask
) {
1014 if (tPtr
->view
->prevFocusChain
) {
1015 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr
->view
),
1016 tPtr
->view
->prevFocusChain
);
1017 tPtr
->flags
.notIllegalMovement
= 1;
1019 data
= (void*)WMBacktabTextMovement
;
1021 if (tPtr
->view
->nextFocusChain
) {
1022 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr
->view
),
1023 tPtr
->view
->nextFocusChain
);
1024 tPtr
->flags
.notIllegalMovement
= 1;
1026 data
= (void*)WMTabTextMovement
;
1028 textEvent
= WMTextDidEndEditingNotification
;
1032 data
= (void*)WMReturnTextMovement
;
1033 textEvent
= WMTextDidEndEditingNotification
;
1036 case WM_EMACSKEY_LEFT
:
1037 if (!control_pressed
) {
1044 if (tPtr
->cursorPosition
> 0) {
1046 if (event
->xkey
.state
& ControlMask
) {
1047 int i
= tPtr
->cursorPosition
- 1;
1049 while (i
> 0 && tPtr
->text
[i
] != ' ') i
--;
1050 while (i
> 0 && tPtr
->text
[i
] == ' ') i
--;
1052 tPtr
->cursorPosition
= (i
> 0) ? i
+ 1 : 0;
1054 tPtr
->cursorPosition
--;
1056 if (tPtr
->cursorPosition
< tPtr
->viewPosition
) {
1057 tPtr
->viewPosition
= tPtr
->cursorPosition
;
1062 if (event
->xkey
.state
& ShiftMask
)
1063 cancelSelection
= 0;
1066 case WM_EMACSKEY_RIGHT
:
1067 if (!control_pressed
) {
1074 if (tPtr
->cursorPosition
< tPtr
->textLen
) {
1076 if (event
->xkey
.state
& ControlMask
) {
1077 int i
= tPtr
->cursorPosition
;
1079 while (tPtr
->text
[i
] && tPtr
->text
[i
] != ' ') i
++;
1080 while (tPtr
->text
[i
] == ' ') i
++;
1082 tPtr
->cursorPosition
= i
;
1084 tPtr
->cursorPosition
++;
1086 while (WMWidthOfString(tPtr
->font
,
1087 &(tPtr
->text
[tPtr
->viewPosition
]),
1088 tPtr
->cursorPosition
-tPtr
->viewPosition
)
1089 > tPtr
->usableWidth
) {
1090 tPtr
->viewPosition
++;
1096 if (event
->xkey
.state
& ShiftMask
)
1097 cancelSelection
= 0;
1100 case WM_EMACSKEY_HOME
:
1101 if (!control_pressed
) {
1108 if (tPtr
->cursorPosition
> 0) {
1110 tPtr
->cursorPosition
= 0;
1111 if (tPtr
->viewPosition
> 0) {
1112 tPtr
->viewPosition
= 0;
1117 if (event
->xkey
.state
& ShiftMask
)
1118 cancelSelection
= 0;
1121 case WM_EMACSKEY_END
:
1122 if (!control_pressed
) {
1129 if (tPtr
->cursorPosition
< tPtr
->textLen
) {
1131 tPtr
->cursorPosition
= tPtr
->textLen
;
1132 tPtr
->viewPosition
= 0;
1133 while (WMWidthOfString(tPtr
->font
,
1134 &(tPtr
->text
[tPtr
->viewPosition
]),
1135 tPtr
->textLen
-tPtr
->viewPosition
)
1136 > tPtr
->usableWidth
) {
1137 tPtr
->viewPosition
++;
1143 if (event
->xkey
.state
& ShiftMask
)
1144 cancelSelection
= 0;
1147 case WM_EMACSKEY_BS
:
1148 if (!control_pressed
) {
1152 if (tPtr
->selection
.count
) {
1153 WMDeleteTextFieldRange(tPtr
, tPtr
->selection
);
1154 data
= (void*)WMDeleteTextEvent
;
1155 textEvent
= WMTextDidChangeNotification
;
1156 } else if (tPtr
->cursorPosition
> 0) {
1158 range
.position
= tPtr
->cursorPosition
- 1;
1160 WMDeleteTextFieldRange(tPtr
, range
);
1161 data
= (void*)WMDeleteTextEvent
;
1162 textEvent
= WMTextDidChangeNotification
;
1166 case WM_EMACSKEY_DEL
:
1167 if (!control_pressed
) {
1174 if (tPtr
->selection
.count
) {
1175 WMDeleteTextFieldRange(tPtr
, tPtr
->selection
);
1176 data
= (void*)WMDeleteTextEvent
;
1177 textEvent
= WMTextDidChangeNotification
;
1178 } else if (tPtr
->cursorPosition
< tPtr
->textLen
) {
1180 range
.position
= tPtr
->cursorPosition
;
1182 WMDeleteTextFieldRange(tPtr
, range
);
1183 data
= (void*)WMDeleteTextEvent
;
1184 textEvent
= WMTextDidChangeNotification
;
1190 if (count
> 0 && isprint(buffer
[0])) {
1191 if (tPtr
->selection
.count
)
1192 WMDeleteTextFieldRange(tPtr
, tPtr
->selection
);
1193 WMInsertTextFieldText(tPtr
, buffer
, tPtr
->cursorPosition
);
1194 data
= (void*)WMInsertTextEvent
;
1195 textEvent
= WMTextDidChangeNotification
;
1197 /* should we rather break and goto cancel selection below? -Dan */
1203 if (!cancelSelection
) {
1204 if (tPtr
->selection
.count
!= tPtr
->cursorPosition
- tPtr
->selection
.position
) {
1206 tPtr
->selection
.count
= tPtr
->cursorPosition
- tPtr
->selection
.position
;
1208 WMPostNotificationName("_lostOwnership", NULL
, tPtr
);
1214 lostHandler(tPtr
->view
, XA_PRIMARY
, NULL
);
1216 if (tPtr
->selection
.count
) {
1217 tPtr
->selection
.count
= 0;
1220 tPtr
->selection
.position
= tPtr
->cursorPosition
;
1223 /*printf("(%d,%d)\n", tPtr->selection.position, tPtr->selection.count);*/
1226 WMNotification
*notif
= WMCreateNotification(textEvent
, tPtr
, data
);
1228 if (tPtr
->delegate
) {
1229 if (textEvent
==WMTextDidBeginEditingNotification
&&
1230 tPtr
->delegate
->didBeginEditing
)
1231 (*tPtr
->delegate
->didBeginEditing
)(tPtr
->delegate
, notif
);
1232 else if (textEvent
==WMTextDidEndEditingNotification
&&
1233 tPtr
->delegate
->didEndEditing
)
1234 (*tPtr
->delegate
->didEndEditing
)(tPtr
->delegate
, notif
);
1235 else if (textEvent
==WMTextDidChangeNotification
&&
1236 tPtr
->delegate
->didChange
)
1237 (*tPtr
->delegate
->didChange
)(tPtr
->delegate
, notif
);
1240 WMPostNotification(notif
);
1241 WMReleaseNotification(notif
);
1245 paintTextField(tPtr
);
1247 /*printf("(%d,%d)\n", tPtr->selection.position, tPtr->selection.count);*/
1252 pointToCursorPosition(TextField
*tPtr
, int x
)
1257 if (tPtr
->flags
.bordered
)
1260 a
= tPtr
->viewPosition
;
1261 b
= tPtr
->viewPosition
+ tPtr
->textLen
;
1262 if (WMWidthOfString(tPtr
->font
, &(tPtr
->text
[tPtr
->viewPosition
]),
1263 tPtr
->textLen
- tPtr
->viewPosition
) < x
)
1264 return tPtr
->textLen
;
1266 while (a
< b
&& b
-a
>1) {
1268 tw
= WMWidthOfString(tPtr
->font
, &(tPtr
->text
[tPtr
->viewPosition
]),
1269 mid
- tPtr
->viewPosition
);
1283 pasteText(WMView
*view
, Atom selection
, Atom target
, Time timestamp
,
1284 void *cdata
, WMData
*data
)
1286 TextField
*tPtr
= (TextField
*)view
->self
;
1289 tPtr
->flags
.waitingSelection
= 0;
1292 str
= (char*)WMDataBytes(data
);
1294 WMInsertTextFieldText(tPtr
, str
, tPtr
->cursorPosition
);
1295 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
1296 (void*)WMInsertTextEvent
);
1300 str
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
1304 WMInsertTextFieldText(tPtr
, str
, tPtr
->cursorPosition
);
1306 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
1307 (void*)WMInsertTextEvent
);
1314 handleTextFieldActionEvents(XEvent
*event
, void *data
)
1316 TextField
*tPtr
= (TextField
*)data
;
1317 static int move
= 0;
1318 static Time lastButtonReleasedEvent
= 0;
1319 Display
*dpy
= event
->xany
.display
;
1321 CHECK_CLASS(data
, WC_TextField
);
1323 switch (event
->type
) {
1325 if (tPtr
->flags
.waitingSelection
) {
1328 if (tPtr
->flags
.enabled
&& tPtr
->flags
.focused
) {
1329 handleTextFieldKeyPress(tPtr
, event
);
1330 XGrabPointer(dpy
, W_VIEW(tPtr
)->window
, False
,
1331 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
,
1332 GrabModeAsync
, GrabModeAsync
, None
,
1333 W_VIEW(tPtr
)->screen
->invisibleCursor
,
1335 tPtr
->flags
.pointerGrabbed
= 1;
1341 if (tPtr
->flags
.pointerGrabbed
) {
1342 tPtr
->flags
.pointerGrabbed
= 0;
1343 XUngrabPointer(dpy
, CurrentTime
);
1345 if (tPtr
->flags
.waitingSelection
) {
1349 if (tPtr
->flags
.enabled
&& (event
->xmotion
.state
& Button1Mask
)) {
1351 if (tPtr
->viewPosition
< tPtr
->textLen
&& event
->xmotion
.x
>
1352 tPtr
->usableWidth
) {
1353 if (WMWidthOfString(tPtr
->font
,
1354 &(tPtr
->text
[tPtr
->viewPosition
]),
1355 tPtr
->cursorPosition
-tPtr
->viewPosition
)
1356 > tPtr
->usableWidth
) {
1357 tPtr
->viewPosition
++;
1359 } else if (tPtr
->viewPosition
> 0 && event
->xmotion
.x
< 0) {
1361 tPtr
->viewPosition
--;
1364 tPtr
->cursorPosition
=
1365 pointToCursorPosition(tPtr
, event
->xmotion
.x
);
1367 tPtr
->selection
.count
= tPtr
->cursorPosition
- tPtr
->selection
.position
;
1369 if (tPtr
->selection
.count
!= 0) {
1370 if (!tPtr
->flags
.ownsSelection
) {
1371 WMCreateSelectionHandler(tPtr
->view
,
1373 event
->xbutton
.time
,
1374 &selectionHandler
, NULL
);
1375 tPtr
->flags
.ownsSelection
= 1;
1380 paintTextField(tPtr
);
1386 if (tPtr
->flags
.pointerGrabbed
) {
1387 tPtr
->flags
.pointerGrabbed
= 0;
1388 XUngrabPointer(dpy
, CurrentTime
);
1392 if (tPtr
->flags
.waitingSelection
) {
1397 switch (tPtr
->flags
.alignment
) {
1400 textWidth
= WMWidthOfString(tPtr
->font
, tPtr
->text
, tPtr
->textLen
);
1401 if (tPtr
->flags
.enabled
&& !tPtr
->flags
.focused
) {
1402 WMSetFocusToWidget(tPtr
);
1404 } else if (tPtr
->flags
.focused
) {
1405 tPtr
->selection
.position
= tPtr
->cursorPosition
;
1406 tPtr
->selection
.count
= 0;
1408 if(textWidth
< tPtr
->usableWidth
) {
1409 tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1410 event
->xbutton
.x
- tPtr
->usableWidth
1412 } else tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1415 paintTextField(tPtr
);
1419 if (tPtr
->flags
.enabled
&& !tPtr
->flags
.focused
) {
1420 WMSetFocusToWidget(tPtr
);
1421 tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1423 paintTextField(tPtr
);
1424 } else if (tPtr
->flags
.focused
1425 && event
->xbutton
.button
== Button1
) {
1426 tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1428 tPtr
->selection
.position
= tPtr
->cursorPosition
;
1429 tPtr
->selection
.count
= 0;
1430 paintTextField(tPtr
);
1432 if (event
->xbutton
.button
== Button2
&& tPtr
->flags
.enabled
) {
1436 if (!WMRequestSelection(tPtr
->view
, XA_PRIMARY
, XA_STRING
,
1437 event
->xbutton
.time
,
1439 text
= XFetchBuffer(tPtr
->view
->screen
->display
, &n
, 0);
1443 WMInsertTextFieldText(tPtr
, text
, tPtr
->cursorPosition
);
1445 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
1446 (void*)WMInsertTextEvent
);
1449 tPtr
->flags
.waitingSelection
= 1;
1459 if (tPtr
->flags
.pointerGrabbed
) {
1460 tPtr
->flags
.pointerGrabbed
= 0;
1461 XUngrabPointer(dpy
, CurrentTime
);
1463 if (tPtr
->flags
.waitingSelection
) {
1467 if (tPtr
->selection
.count
!= 0) {
1469 XRotateBuffers(dpy
, 1);
1471 count
= abs(tPtr
->selection
.count
);
1472 if (tPtr
->selection
.count
< 0)
1473 start
= tPtr
->selection
.position
- count
;
1475 start
= tPtr
->selection
.position
;
1477 XStoreBuffer(dpy
, &tPtr
->text
[start
], count
, 0);
1482 if (event
->xbutton
.time
- lastButtonReleasedEvent
1483 <= WINGsConfiguration
.doubleClickDelay
) {
1484 tPtr
->selection
.position
= 0;
1485 tPtr
->selection
.count
= tPtr
->textLen
;
1486 paintTextField(tPtr
);
1488 if (!tPtr
->flags
.ownsSelection
) {
1489 WMCreateSelectionHandler(tPtr
->view
,
1491 event
->xbutton
.time
,
1492 &selectionHandler
, NULL
);
1493 tPtr
->flags
.ownsSelection
= 1;
1496 WMPostNotificationName("_lostOwnership", NULL
, tPtr
);
1498 lastButtonReleasedEvent
= event
->xbutton
.time
;
1506 destroyTextField(TextField
*tPtr
)
1510 WMDeleteTimerHandler(tPtr
->timerID
);
1513 WMReleaseFont(tPtr
->font
);
1514 WMDeleteSelectionHandler(tPtr
->view
, XA_PRIMARY
, CurrentTime
);
1515 WMRemoveNotificationObserver(tPtr
);