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 */
43 WMRange prevselection
;
47 WMTextFieldDelegate
*delegate
;
50 WMHandlerID timerID
; /* for cursor blinking */
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;
70 unsigned int notIllegalMovement
:1;
75 #define NOTIFY(T,C,N,A) { WMNotification *notif = WMCreateNotification(N,T,A);\
76 if ((T)->delegate && (T)->delegate->C)\
77 (*(T)->delegate->C)((T)->delegate,notif);\
78 WMPostNotification(notif);\
79 WMReleaseNotification(notif);}
82 #define MIN_TEXT_BUFFER 2
83 #define TEXT_BUFFER_INCR 8
86 #define WM_EMACSKEYMASK ControlMask
88 #define WM_EMACSKEY_LEFT XK_b
89 #define WM_EMACSKEY_RIGHT XK_f
90 #define WM_EMACSKEY_HOME XK_a
91 #define WM_EMACSKEY_END XK_e
92 #define WM_EMACSKEY_BS XK_h
93 #define WM_EMACSKEY_DEL XK_d
97 #define DEFAULT_WIDTH 60
98 #define DEFAULT_HEIGHT 20
99 #define DEFAULT_BORDERED True
100 #define DEFAULT_ALIGNMENT WALeft
104 static void destroyTextField(TextField
*tPtr
);
105 static void paintTextField(TextField
*tPtr
);
107 static void handleEvents(XEvent
*event
, void *data
);
108 static void handleTextFieldActionEvents(XEvent
*event
, void *data
);
109 static void didResizeTextField();
111 struct W_ViewDelegate _TextFieldViewDelegate
= {
120 #define TEXT_WIDTH(tPtr, start) (WMWidthOfString((tPtr)->font, \
121 &((tPtr)->text[(start)]), (tPtr)->textLen - (start) + 1))
123 #define TEXT_WIDTH2(tPtr, start, end) (WMWidthOfString((tPtr)->font, \
124 &((tPtr)->text[(start)]), (end) - (start) + 1))
128 memmv(char *dest
, char *src
, int size
)
133 for (i
=size
-1; i
>=0; i
--) {
136 } else if (dest
< src
) {
137 for (i
=0; i
<size
; i
++) {
145 incrToFit(TextField
*tPtr
)
147 int vp
= tPtr
->viewPosition
;
149 while (TEXT_WIDTH(tPtr
, tPtr
->viewPosition
) > tPtr
->usableWidth
) {
150 tPtr
->viewPosition
++;
152 return vp
!=tPtr
->viewPosition
;
157 incrToFit2(TextField
*tPtr
)
159 int vp
= tPtr
->viewPosition
;
160 while (TEXT_WIDTH2(tPtr
, tPtr
->viewPosition
, tPtr
->cursorPosition
)
161 >= tPtr
->usableWidth
)
162 tPtr
->viewPosition
++;
165 return vp
!=tPtr
->viewPosition
;
170 decrToFit(TextField
*tPtr
)
172 while (TEXT_WIDTH(tPtr
, tPtr
->viewPosition
-1) < tPtr
->usableWidth
173 && tPtr
->viewPosition
>0)
174 tPtr
->viewPosition
--;
181 requestHandler(WMWidget
*w
, Atom selection
, Atom target
, Atom
*type
,
182 void **value
, unsigned *length
, int *format
)
186 Display
*dpy
= tPtr
->view
->screen
->display
;
189 text
= XGetAtomName(tPtr
->view
->screen
->display
,target
);
191 text
= XGetAtomName(tPtr
->view
->screen
->display
,selection
);
197 count
= tPtr
->selection
.count
< 0
198 ? tPtr
->selection
.position
+ tPtr
->selection
.count
199 : tPtr
->selection
.position
;
201 if (target
== XA_STRING
||
202 target
== XInternAtom(dpy
, "TEXT", False
) ||
203 target
== XInternAtom(dpy
, "COMPOUND_TEXT", False
)) {
204 *value
= wstrdup(&(tPtr
->text
[count
]));
205 *length
= abs(tPtr
->selection
.count
);
211 _TARGETS
= XInternAtom(dpy
, "TARGETS", False
);
212 if (target
== _TARGETS
) {
216 ptr
= *value
= (char *) wmalloc(4 * sizeof(Atom
));
219 ptr
[2] = XInternAtom(dpy
, "TEXT", False
);
220 ptr
[3] = XInternAtom(dpy
, "COMPOUND_TEXT", False
);
225 *target = XA_PRIMARY;
233 lostHandler(WMWidget
*w
, Atom selection
)
235 TextField
*tPtr
= (WMTextField
*)w
;
237 tPtr
->selection
.count
= 0;
238 paintTextField(tPtr
);
242 _notification(void *observerData
, WMNotification
*notification
)
244 WMTextField
*to
= (WMTextField
*)observerData
;
245 WMTextField
*tw
= (WMTextField
*)WMGetNotificationClientData(notification
);
246 if (to
!= tw
) lostHandler(to
, 0);
250 WMCreateTextField(WMWidget
*parent
)
254 tPtr
= wmalloc(sizeof(TextField
));
255 memset(tPtr
, 0, sizeof(TextField
));
257 tPtr
->widgetClass
= WC_TextField
;
259 tPtr
->view
= W_CreateView(W_VIEW(parent
));
264 tPtr
->view
->self
= tPtr
;
266 tPtr
->view
->delegate
= &_TextFieldViewDelegate
;
268 tPtr
->view
->attribFlags
|= CWCursor
;
269 tPtr
->view
->attribs
.cursor
= tPtr
->view
->screen
->textCursor
;
271 W_SetViewBackgroundColor(tPtr
->view
, tPtr
->view
->screen
->white
);
273 tPtr
->text
= wmalloc(MIN_TEXT_BUFFER
);
276 tPtr
->bufferSize
= MIN_TEXT_BUFFER
;
278 tPtr
->flags
.enabled
= 1;
280 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
281 |FocusChangeMask
, handleEvents
, tPtr
);
283 tPtr
->font
= WMRetainFont(tPtr
->view
->screen
->normalFont
);
285 tPtr
->flags
.bordered
= DEFAULT_BORDERED
;
286 tPtr
->flags
.beveled
= True
;
287 tPtr
->flags
.alignment
= DEFAULT_ALIGNMENT
;
289 WMAX((tPtr
->view
->size
.height
- WMFontHeight(tPtr
->font
))/2, 1);
291 W_ResizeView(tPtr
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
293 WMCreateEventHandler(tPtr
->view
, EnterWindowMask
|LeaveWindowMask
294 |ButtonReleaseMask
|ButtonPressMask
|KeyPressMask
|Button1MotionMask
,
295 handleTextFieldActionEvents
, tPtr
);
297 WMCreateSelectionHandler(tPtr
, XA_PRIMARY
, CurrentTime
, requestHandler
,
299 WMAddNotificationObserver(_notification
, tPtr
, "_lostOwnership", tPtr
);
302 tPtr
->flags
.cursorOn
= 1;
309 WMSetTextFieldDelegate(WMTextField
*tPtr
, WMTextFieldDelegate
*delegate
)
311 tPtr
->delegate
= delegate
;
316 WMInsertTextFieldText(WMTextField
*tPtr
, char *text
, int position
)
320 CHECK_CLASS(tPtr
, WC_TextField
);
327 /* check if buffer will hold the text */
328 if (len
+ tPtr
->textLen
>= tPtr
->bufferSize
) {
329 tPtr
->bufferSize
= tPtr
->textLen
+ len
+ TEXT_BUFFER_INCR
;
330 tPtr
->text
= realloc(tPtr
->text
, tPtr
->bufferSize
);
333 if (position
< 0 || position
>= tPtr
->textLen
) {
334 /* append the text at the end */
335 strcat(tPtr
->text
, text
);
339 tPtr
->textLen
+= len
;
340 tPtr
->cursorPosition
+= len
;
342 /* insert text at position */
343 memmv(&(tPtr
->text
[position
+len
]), &(tPtr
->text
[position
]),
344 tPtr
->textLen
-position
+1);
346 memcpy(&(tPtr
->text
[position
]), text
, len
);
348 tPtr
->textLen
+= len
;
349 if (position
>= tPtr
->cursorPosition
) {
350 tPtr
->cursorPosition
+= len
;
357 paintTextField(tPtr
);
362 WMDeleteTextFieldRange(WMTextField
*tPtr
, WMRange range
)
364 CHECK_CLASS(tPtr
, WC_TextField
);
366 if (range
.position
>= tPtr
->textLen
)
369 if (range
.count
< 1) {
370 if (range
.position
< 0)
372 tPtr
->text
[range
.position
] = 0;
373 tPtr
->textLen
= range
.position
;
375 tPtr
->cursorPosition
= 0;
376 tPtr
->viewPosition
= 0;
378 if (range
.position
+ range
.count
> tPtr
->textLen
)
379 range
.count
= tPtr
->textLen
- range
.position
;
380 memmv(&(tPtr
->text
[range
.position
]), &(tPtr
->text
[range
.position
+range
.count
]),
381 tPtr
->textLen
- (range
.position
+range
.count
) + 1);
382 tPtr
->textLen
-= range
.count
;
384 if (tPtr
->cursorPosition
> range
.position
)
385 tPtr
->cursorPosition
-= range
.count
;
390 paintTextField(tPtr
);
396 WMGetTextFieldText(WMTextField
*tPtr
)
398 CHECK_CLASS(tPtr
, WC_TextField
);
400 return wstrdup(tPtr
->text
);
405 WMSetTextFieldText(WMTextField
*tPtr
, char *text
)
407 if ((text
&& strcmp(tPtr
->text
, text
) == 0) ||
408 (!text
&& tPtr
->textLen
== 0))
415 tPtr
->textLen
= strlen(text
);
417 if (tPtr
->textLen
>= tPtr
->bufferSize
) {
418 tPtr
->bufferSize
= tPtr
->textLen
+ TEXT_BUFFER_INCR
;
419 tPtr
->text
= realloc(tPtr
->text
, tPtr
->bufferSize
);
421 strcpy(tPtr
->text
, text
);
424 if (tPtr->textLen < tPtr->cursorPosition)
425 tPtr->cursorPosition = tPtr->textLen;
427 tPtr
->cursorPosition
= tPtr
->textLen
;
428 tPtr
->viewPosition
= 0;
429 tPtr
->selection
.count
= 0;
431 if (tPtr
->view
->flags
.realized
)
432 paintTextField(tPtr
);
437 WMSetTextFieldFont(WMTextField
*tPtr
, WMFont
*font
)
439 /* TODO: update font change after field is mapped */
440 WMReleaseFont(tPtr
->font
);
441 tPtr
->font
= WMRetainFont(font
);
446 WMSetTextFieldAlignment(WMTextField
*tPtr
, WMAlignment alignment
)
448 tPtr
->flags
.alignment
= alignment
;
449 if (alignment
!=WALeft
) {
450 wwarning("only left alignment is supported in textfields");
454 if (tPtr
->view
->flags
.realized
) {
455 paintTextField(tPtr
);
461 WMSetTextFieldBordered(WMTextField
*tPtr
, Bool bordered
)
463 tPtr
->flags
.bordered
= bordered
;
465 if (tPtr
->view
->flags
.realized
) {
466 paintTextField(tPtr
);
472 WMSetTextFieldBeveled(WMTextField
*tPtr
, Bool flag
)
474 tPtr
->flags
.beveled
= flag
;
476 if (tPtr
->view
->flags
.realized
) {
477 paintTextField(tPtr
);
484 WMSetTextFieldSecure(WMTextField
*tPtr
, Bool flag
)
486 tPtr
->flags
.secure
= flag
;
488 if (tPtr
->view
->flags
.realized
) {
489 paintTextField(tPtr
);
495 WMGetTextFieldEditable(WMTextField
*tPtr
)
497 return tPtr
->flags
.enabled
;
502 WMSetTextFieldEditable(WMTextField
*tPtr
, Bool flag
)
504 tPtr
->flags
.enabled
= flag
;
506 if (tPtr
->view
->flags
.realized
) {
507 paintTextField(tPtr
);
513 WMSelectTextFieldRange(WMTextField
*tPtr
, WMRange range
)
515 if (tPtr
->flags
.enabled
) {
516 if (range
.position
< 0) {
517 range
.count
+= range
.position
;
518 range
.count
= (range
.count
< 0) ? 0 : range
.count
;
520 } else if (range
.position
> tPtr
->textLen
) {
521 range
.position
= tPtr
->textLen
;
525 if (range
.position
+ range
.count
> tPtr
->textLen
)
526 range
.count
= tPtr
->textLen
- range
.position
;
528 tPtr
->prevselection
= tPtr
->selection
; /* check if this is needed */
530 tPtr
->selection
= range
;
532 if (tPtr
->view
->flags
.realized
) {
533 paintTextField(tPtr
);
540 WMSetTextFieldCursorPosition(WMTextField
*tPtr
, unsigned int position
)
542 if (tPtr
->flags
.enabled
) {
543 if (position
> tPtr
->textLen
)
544 position
= tPtr
->textLen
;
546 tPtr
->cursorPosition
= position
;
547 if (tPtr
->view
->flags
.realized
) {
548 paintTextField(tPtr
);
555 WMSetTextFieldNextTextField(WMTextField
*tPtr
, WMTextField
*next
)
557 CHECK_CLASS(tPtr
, WC_TextField
);
559 if (tPtr
->view
->nextFocusChain
)
560 tPtr
->view
->nextFocusChain
->prevFocusChain
= NULL
;
561 tPtr
->view
->nextFocusChain
= NULL
;
565 CHECK_CLASS(next
, WC_TextField
);
567 if (tPtr
->view
->nextFocusChain
)
568 tPtr
->view
->nextFocusChain
->prevFocusChain
= NULL
;
569 if (next
->view
->prevFocusChain
)
570 next
->view
->prevFocusChain
->nextFocusChain
= NULL
;
572 tPtr
->view
->nextFocusChain
= next
->view
;
573 next
->view
->prevFocusChain
= tPtr
->view
;
578 WMSetTextFieldPrevTextField(WMTextField
*tPtr
, WMTextField
*prev
)
580 CHECK_CLASS(tPtr
, WC_TextField
);
582 if (tPtr
->view
->prevFocusChain
)
583 tPtr
->view
->prevFocusChain
->nextFocusChain
= NULL
;
584 tPtr
->view
->prevFocusChain
= NULL
;
588 CHECK_CLASS(prev
, WC_TextField
);
590 if (tPtr
->view
->prevFocusChain
)
591 tPtr
->view
->prevFocusChain
->nextFocusChain
= NULL
;
592 if (prev
->view
->nextFocusChain
)
593 prev
->view
->nextFocusChain
->prevFocusChain
= NULL
;
595 tPtr
->view
->prevFocusChain
= prev
->view
;
596 prev
->view
->nextFocusChain
= tPtr
->view
;
601 didResizeTextField(W_ViewDelegate
*self
, WMView
*view
)
603 WMTextField
*tPtr
= (WMTextField
*)view
->self
;
606 WMAX((tPtr
->view
->size
.height
- WMFontHeight(tPtr
->font
))/2, 1);
608 tPtr
->usableWidth
= tPtr
->view
->size
.width
- 2*tPtr
->offsetWidth
+ 2;
613 makeHiddenString(int length
)
615 char *data
= wmalloc(length
+1);
617 memset(data
, '*', length
);
625 paintCursor(TextField
*tPtr
)
628 WMScreen
*screen
= tPtr
->view
->screen
;
632 if (tPtr
->flags
.secure
)
633 text
= makeHiddenString(strlen(tPtr
->text
));
637 cx
= WMWidthOfString(tPtr
->font
, &(text
[tPtr
->viewPosition
]),
638 tPtr
->cursorPosition
-tPtr
->viewPosition
);
640 switch (tPtr
->flags
.alignment
) {
642 textWidth
= WMWidthOfString(tPtr
->font
, text
, tPtr
->textLen
);
643 if (textWidth
< tPtr
->usableWidth
)
644 cx
+= tPtr
->offsetWidth
+ tPtr
->usableWidth
- textWidth
+ 1;
646 cx
+= tPtr
->offsetWidth
+ 1;
649 cx
+= tPtr
->offsetWidth
+ 1;
654 textWidth
= WMWidthOfString(tPtr
->font
, text
, tPtr
->textLen
);
655 if (textWidth
< tPtr
->usableWidth
)
656 cx
+= tPtr
->offsetWidth
+ (tPtr
->usableWidth
-textWidth
)/2;
658 cx
+= tPtr
->offsetWidth
;
662 XDrawRectangle(screen->display, tPtr->view->window, screen->xorGC,
663 cx, tPtr->offsetWidth, 1,
664 tPtr->view->size.height - 2*tPtr->offsetWidth - 1);
665 printf("%d %d\n",cx,tPtr->cursorPosition);
667 XDrawLine(screen
->display
, tPtr
->view
->window
, screen
->xorGC
,
668 cx
, tPtr
->offsetWidth
, cx
,
669 tPtr
->view
->size
.height
- tPtr
->offsetWidth
- 1);
671 if (tPtr
->flags
.secure
)
678 drawRelief(WMView
*view
, Bool beveled
)
680 WMScreen
*scr
= view
->screen
;
681 Display
*dpy
= scr
->display
;
685 int width
= view
->size
.width
;
686 int height
= view
->size
.height
;
688 dgc
= WMColorGC(scr
->darkGray
);
691 XDrawRectangle(dpy
, view
->window
, dgc
, 0, 0, width
-1, height
-1);
695 wgc
= WMColorGC(scr
->white
);
696 lgc
= WMColorGC(scr
->gray
);
699 XDrawLine(dpy
, view
->window
, dgc
, 0, 0, width
-1, 0);
700 XDrawLine(dpy
, view
->window
, dgc
, 0, 1, width
-2, 1);
702 XDrawLine(dpy
, view
->window
, dgc
, 0, 0, 0, height
-2);
703 XDrawLine(dpy
, view
->window
, dgc
, 1, 0, 1, height
-3);
706 XDrawLine(dpy
, view
->window
, wgc
, 0, height
-1, width
-1, height
-1);
707 XDrawLine(dpy
, view
->window
, lgc
, 1, height
-2, width
-2, height
-2);
709 XDrawLine(dpy
, view
->window
, wgc
, width
-1, 0, width
-1, height
-1);
710 XDrawLine(dpy
, view
->window
, lgc
, width
-2, 1, width
-2, height
-3);
715 paintTextField(TextField
*tPtr
)
717 W_Screen
*screen
= tPtr
->view
->screen
;
718 W_View
*view
= tPtr
->view
;
728 if (!view
->flags
.realized
|| !view
->flags
.mapped
)
731 if (!tPtr
->flags
.bordered
) {
737 if (tPtr
->flags
.secure
) {
738 text
= makeHiddenString(strlen(tPtr
->text
));
743 totalWidth
= tPtr
->view
->size
.width
- 2*bd
;
745 drawbuffer
= XCreatePixmap(screen
->display
, view
->window
,
746 view
->size
.width
, view
->size
.height
, screen
->depth
);
747 XFillRectangle(screen
->display
, drawbuffer
, WMColorGC(screen
->white
),
748 0,0, view
->size
.width
,view
->size
.height
);
749 /* this is quite dirty */
750 viewbuffer
.screen
= view
->screen
;
751 viewbuffer
.size
= view
->size
;
752 viewbuffer
.window
= drawbuffer
;
755 if (tPtr
->textLen
> 0) {
756 tw
= WMWidthOfString(tPtr
->font
, &(text
[tPtr
->viewPosition
]),
757 tPtr
->textLen
- tPtr
->viewPosition
);
759 th
= WMFontHeight(tPtr
->font
);
761 ty
= tPtr
->offsetWidth
;
762 switch (tPtr
->flags
.alignment
) {
764 tx
= tPtr
->offsetWidth
+ 1;
765 if (tw
< tPtr
->usableWidth
)
766 XFillRectangle(screen
->display
, drawbuffer
,
767 WMColorGC(screen
->white
),
768 bd
+tw
,bd
, totalWidth
-tw
,view
->size
.height
-2*bd
);
772 tx
= tPtr
->offsetWidth
+ (tPtr
->usableWidth
- tw
) / 2;
773 if (tw
< tPtr
->usableWidth
)
774 XClearArea(screen
->display
, view
->window
, bd
, bd
,
775 totalWidth
, view
->size
.height
-2*bd
, False
);
780 tx
= tPtr
->offsetWidth
+ tPtr
->usableWidth
- tw
- 1;
781 if (tw
< tPtr
->usableWidth
)
782 XClearArea(screen
->display
, view
->window
, bd
, bd
,
783 totalWidth
-tw
, view
->size
.height
-2*bd
, False
);
787 if (!tPtr
->flags
.enabled
)
788 WMSetColorInGC(screen
->darkGray
, screen
->textFieldGC
);
790 WMDrawImageString(screen
, drawbuffer
, screen
->textFieldGC
,
792 &(text
[tPtr
->viewPosition
]),
793 tPtr
->textLen
- tPtr
->viewPosition
);
795 if (tPtr
->selection
.count
) {
798 count
= tPtr
->selection
.count
< 0
799 ? tPtr
->selection
.position
+ tPtr
->selection
.count
800 : tPtr
->selection
.position
;
801 count2
= abs(tPtr
->selection
.count
);
802 if (count
< tPtr
->viewPosition
) {
803 count2
= abs(count2
- abs(tPtr
->viewPosition
- count
));
804 count
= tPtr
->viewPosition
;
808 rx
= tPtr
->offsetWidth
+ 1 + WMWidthOfString(tPtr
->font
,text
,count
)
809 - WMWidthOfString(tPtr
->font
,text
,tPtr
->viewPosition
);
811 XSetBackground(screen
->display
, screen
->textFieldGC
,
812 screen
->gray
->color
.pixel
);
814 WMDrawImageString(screen
, drawbuffer
, screen
->textFieldGC
,
815 tPtr
->font
, rx
, ty
, &(text
[count
]),
818 XSetBackground(screen
->display
, screen
->textFieldGC
,
819 screen
->white
->color
.pixel
);
822 if (!tPtr
->flags
.enabled
)
823 WMSetColorInGC(screen
->black
, screen
->textFieldGC
);
825 XFillRectangle(screen
->display
, drawbuffer
,
826 WMColorGC(screen
->white
),
827 bd
,bd
, totalWidth
,view
->size
.height
-2*bd
);
831 if (tPtr
->flags
.bordered
) {
832 drawRelief(&viewbuffer
, tPtr
->flags
.beveled
);
835 if (tPtr
->flags
.secure
)
837 XCopyArea(screen
->display
, drawbuffer
, view
->window
,
838 screen
->copyGC
, 0,0, view
->size
.width
,
839 view
->size
.height
,0,0);
840 XFreePixmap(screen
->display
, drawbuffer
);
843 if (tPtr
->flags
.focused
&& tPtr
->flags
.enabled
&& tPtr
->flags
.cursorOn
) {
851 blinkCursor(void *data
)
853 TextField
*tPtr
= (TextField
*)data
;
855 if (tPtr
->flags
.cursorOn
) {
856 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_OFF_DELAY
, blinkCursor
,
859 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
, blinkCursor
,
863 tPtr
->flags
.cursorOn
= !tPtr
->flags
.cursorOn
;
869 handleEvents(XEvent
*event
, void *data
)
871 TextField
*tPtr
= (TextField
*)data
;
873 CHECK_CLASS(data
, WC_TextField
);
876 switch (event
->type
) {
878 if (W_FocusedViewOfToplevel(W_TopLevelOfView(tPtr
->view
))!=tPtr
->view
)
880 tPtr
->flags
.focused
= 1;
882 if (!tPtr
->timerID
) {
883 tPtr
->timerID
= WMAddTimerHandler(CURSOR_BLINK_ON_DELAY
,
887 paintTextField(tPtr
);
889 NOTIFY(tPtr
, didBeginEditing
, WMTextDidBeginEditingNotification
, NULL
);
891 tPtr
->flags
.notIllegalMovement
= 0;
895 tPtr
->flags
.focused
= 0;
898 WMDeleteTimerHandler(tPtr
->timerID
);
899 tPtr
->timerID
= NULL
;
902 paintTextField(tPtr
);
903 if (!tPtr
->flags
.notIllegalMovement
) {
904 NOTIFY(tPtr
, didEndEditing
, WMTextDidEndEditingNotification
,
905 (void*)WMIllegalTextMovement
);
910 if (event
->xexpose
.count
!=0)
912 paintTextField(tPtr
);
916 destroyTextField(tPtr
);
923 handleTextFieldKeyPress(TextField
*tPtr
, XEvent
*event
)
927 int count
, refresh
= 0;
928 int control_pressed
= 0;
930 if (((XKeyEvent
*) event
)->state
& WM_EMACSKEYMASK
) {
934 count
= XLookupString(&event
->xkey
, buffer
, 63, &ksym
, NULL
);
935 buffer
[count
] = '\0';
937 if (!(event
->xkey
.state
& ShiftMask
)) {
938 if (tPtr
->selection
.count
)
940 tPtr
->prevselection
= tPtr
->selection
;
941 tPtr
->selection
.position
= tPtr
->cursorPosition
;
942 tPtr
->selection
.count
= 0;
945 /* Be careful in any case in this switch statement, never to call
946 * to more than a function that can generate text change notifications.
947 * Only one text change notification should be sent in any case.
948 * Else hazardous things can happen.
949 * Maybe we need a better solution than the function wrapper to inform
950 * functions that change text in text fields, if they need to send a
951 * change notification or not. -Dan
955 case XK_ISO_Left_Tab
:
956 if (event
->xkey
.state
& ShiftMask
) {
957 if (tPtr
->view
->prevFocusChain
) {
958 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr
->view
),
959 tPtr
->view
->prevFocusChain
);
960 tPtr
->flags
.notIllegalMovement
= 1;
962 NOTIFY(tPtr
, didEndEditing
, WMTextDidEndEditingNotification
,
963 (void*)WMBacktabTextMovement
);
965 if (tPtr
->view
->nextFocusChain
) {
966 W_SetFocusOfTopLevel(W_TopLevelOfView(tPtr
->view
),
967 tPtr
->view
->nextFocusChain
);
968 tPtr
->flags
.notIllegalMovement
= 1;
970 NOTIFY(tPtr
, didEndEditing
, WMTextDidEndEditingNotification
,
971 (void*)WMTabTextMovement
);
976 NOTIFY(tPtr
, didEndEditing
, WMTextDidEndEditingNotification
,
977 (void*)WMReturnTextMovement
);
980 case WM_EMACSKEY_LEFT
:
981 if (!control_pressed
) {
986 if (tPtr
->cursorPosition
> 0) {
988 if (event
->xkey
.state
& ControlMask
) {
990 for (i
= tPtr
->cursorPosition
- 1; i
>= 0; i
--)
991 if (tPtr
->text
[i
] == ' ' || i
== 0) {
992 tPtr
->cursorPosition
= i
;
996 tPtr
->cursorPosition
--;
998 if (tPtr
->cursorPosition
< tPtr
->viewPosition
) {
999 tPtr
->viewPosition
= tPtr
->cursorPosition
;
1007 case WM_EMACSKEY_RIGHT
:
1008 if (!control_pressed
) {
1013 if (tPtr
->cursorPosition
< tPtr
->textLen
) {
1015 if (event
->xkey
.state
& ControlMask
) {
1017 for (i
= tPtr
->cursorPosition
+ 1; i
<= tPtr
->textLen
; i
++)
1018 if (tPtr
->text
[i
] == ' ' || i
== tPtr
->textLen
) {
1019 tPtr
->cursorPosition
= i
;
1023 tPtr
->cursorPosition
++;
1025 while (WMWidthOfString(tPtr
->font
,
1026 &(tPtr
->text
[tPtr
->viewPosition
]),
1027 tPtr
->cursorPosition
-tPtr
->viewPosition
)
1028 > tPtr
->usableWidth
) {
1029 tPtr
->viewPosition
++;
1037 case WM_EMACSKEY_HOME
:
1038 if (!control_pressed
) {
1043 if (tPtr
->cursorPosition
> 0) {
1045 tPtr
->cursorPosition
= 0;
1046 if (tPtr
->viewPosition
> 0) {
1047 tPtr
->viewPosition
= 0;
1055 case WM_EMACSKEY_END
:
1056 if (!control_pressed
) {
1061 if (tPtr
->cursorPosition
< tPtr
->textLen
) {
1063 tPtr
->cursorPosition
= tPtr
->textLen
;
1064 tPtr
->viewPosition
= 0;
1065 while (WMWidthOfString(tPtr
->font
,
1066 &(tPtr
->text
[tPtr
->viewPosition
]),
1067 tPtr
->textLen
-tPtr
->viewPosition
)
1068 > tPtr
->usableWidth
) {
1069 tPtr
->viewPosition
++;
1077 case WM_EMACSKEY_BS
:
1078 if (!control_pressed
) {
1082 if (tPtr
->cursorPosition
> 0) {
1085 if (tPtr
->prevselection
.count
) {
1086 range
.position
= tPtr
->prevselection
.count
< 0
1087 ? tPtr
->prevselection
.position
+ tPtr
->prevselection
.count
1088 : tPtr
->prevselection
.position
;
1090 range
.count
= abs(tPtr
->prevselection
.count
);
1092 range
.position
= tPtr
->cursorPosition
- 1;
1095 WMDeleteTextFieldRange(tPtr
, range
);
1096 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
1097 (void*)WMDeleteTextEvent
);
1101 case WM_EMACSKEY_DEL
:
1102 if (!control_pressed
) {
1107 if (tPtr
->cursorPosition
< tPtr
->textLen
|| tPtr
->prevselection
.count
) {
1110 if (tPtr
->prevselection
.count
) {
1111 range
.position
= tPtr
->prevselection
.count
< 0
1112 ? tPtr
->prevselection
.position
+ tPtr
->prevselection
.count
1113 : tPtr
->prevselection
.position
;
1115 range
.count
= abs(tPtr
->prevselection
.count
);
1117 range
.position
= tPtr
->cursorPosition
;
1120 WMDeleteTextFieldRange(tPtr
, range
);
1121 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
1122 (void*)WMDeleteTextEvent
);
1128 if (count
> 0 && !iscntrl(buffer
[0])) {
1131 if (tPtr
->prevselection
.count
) {
1132 range
.position
= tPtr
->prevselection
.count
< 0
1133 ? tPtr
->prevselection
.position
+ tPtr
->prevselection
.count
1134 : tPtr
->prevselection
.position
;
1136 range
.count
= abs(tPtr
->prevselection
.count
);
1138 range
.position
= tPtr
->cursorPosition
;
1141 if (tPtr
->prevselection
.count
)
1142 WMDeleteTextFieldRange(tPtr
, range
);
1143 WMInsertTextFieldText(tPtr
, buffer
, tPtr
->cursorPosition
);
1144 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
1145 (void*)WMInsertTextEvent
);
1151 if (event
->xkey
.state
& ShiftMask
) {
1152 if (tPtr
->selection
.count
== 0)
1153 tPtr
->selection
.position
= tPtr
->cursorPosition
;
1154 tPtr
->selection
.count
= tPtr
->cursorPosition
- tPtr
->selection
.position
;
1157 tPtr
->prevselection
.count
= 0;
1159 paintTextField(tPtr
);
1165 pointToCursorPosition(TextField
*tPtr
, int x
)
1170 if (tPtr
->flags
.bordered
)
1173 a
= tPtr
->viewPosition
;
1174 b
= tPtr
->viewPosition
+ tPtr
->textLen
;
1175 if (WMWidthOfString(tPtr
->font
, &(tPtr
->text
[tPtr
->viewPosition
]),
1176 tPtr
->textLen
- tPtr
->viewPosition
) < x
)
1177 return tPtr
->textLen
;
1179 while (a
< b
&& b
-a
>1) {
1181 tw
= WMWidthOfString(tPtr
->font
, &(tPtr
->text
[tPtr
->viewPosition
]),
1182 mid
- tPtr
->viewPosition
);
1195 handleTextFieldActionEvents(XEvent
*event
, void *data
)
1197 TextField
*tPtr
= (TextField
*)data
;
1200 CHECK_CLASS(data
, WC_TextField
);
1202 switch (event
->type
) {
1204 if (tPtr
->flags
.enabled
&& tPtr
->flags
.focused
) {
1205 handleTextFieldKeyPress(tPtr
, event
);
1206 XGrabPointer(WMScreenDisplay(W_VIEW(tPtr
)->screen
),
1207 W_VIEW(tPtr
)->window
, False
,
1208 PointerMotionMask
|ButtonPressMask
|ButtonReleaseMask
,
1209 GrabModeAsync
, GrabModeAsync
, None
,
1210 W_VIEW(tPtr
)->screen
->invisibleCursor
,
1212 tPtr
->flags
.pointerGrabbed
= 1;
1218 if (tPtr
->flags
.pointerGrabbed
) {
1219 tPtr
->flags
.pointerGrabbed
= 0;
1220 XUngrabPointer(WMScreenDisplay(W_VIEW(tPtr
)->screen
), CurrentTime
);
1223 if (tPtr
->flags
.enabled
&& (event
->xmotion
.state
& Button1Mask
)) {
1225 if (tPtr
->viewPosition
< tPtr
->textLen
&& event
->xmotion
.x
>
1226 tPtr
->usableWidth
) {
1227 if (WMWidthOfString(tPtr
->font
,
1228 &(tPtr
->text
[tPtr
->viewPosition
]),
1229 tPtr
->cursorPosition
-tPtr
->viewPosition
)
1230 > tPtr
->usableWidth
) {
1231 tPtr
->viewPosition
++;
1233 } else if (tPtr
->viewPosition
> 0 && event
->xmotion
.x
< 0) {
1235 tPtr
->viewPosition
--;
1238 if (!tPtr
->selection
.count
) {
1239 tPtr
->selection
.position
= tPtr
->cursorPosition
;
1242 tPtr
->cursorPosition
=
1243 pointToCursorPosition(tPtr
, event
->xmotion
.x
);
1245 tPtr
->selection
.count
= tPtr
->cursorPosition
- tPtr
->selection
.position
;
1248 printf("notify %d %d\n",event->xmotion.x,tPtr->usableWidth);
1252 paintTextField(tPtr
);
1256 XSetSelectionOwner(tPtr
->view
->screen
->display
,
1257 XA_PRIMARY
, tPtr
->view
->window
, CurrentTime
);
1259 WMNotification
*notif
= WMCreateNotification("_lostOwnership",
1262 WMPostNotification(notif
);
1263 WMReleaseNotification(notif
);
1269 if (tPtr
->flags
.pointerGrabbed
) {
1270 tPtr
->flags
.pointerGrabbed
= 0;
1271 XUngrabPointer(WMScreenDisplay(W_VIEW(tPtr
)->screen
), CurrentTime
);
1276 switch (tPtr
->flags
.alignment
) {
1279 textWidth
= WMWidthOfString(tPtr
->font
, tPtr
->text
, tPtr
->textLen
);
1280 if (tPtr
->flags
.enabled
&& !tPtr
->flags
.focused
) {
1281 WMSetFocusToWidget(tPtr
);
1282 } else if (tPtr
->flags
.focused
) {
1283 tPtr
->selection
.count
= 0;
1285 if(textWidth
< tPtr
->usableWidth
){
1286 tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1287 event
->xbutton
.x
- tPtr
->usableWidth
1290 else tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1293 tPtr->cursorPosition = pointToCursorPosition(tPtr,
1295 tPtr->cursorPosition += tPtr->usableWidth - textWidth;
1298 tPtr->cursorPosition = pointToCursorPosition(tPtr,
1301 paintTextField(tPtr
);
1305 if (tPtr
->flags
.enabled
&& !tPtr
->flags
.focused
) {
1306 WMSetFocusToWidget(tPtr
);
1307 tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1309 paintTextField(tPtr
);
1310 } else if (tPtr
->flags
.focused
) {
1311 tPtr
->cursorPosition
= pointToCursorPosition(tPtr
,
1313 tPtr
->selection
.count
= 0;
1314 paintTextField(tPtr
);
1316 if (event
->xbutton
.button
== Button2
&& tPtr
->flags
.enabled
) {
1319 text
= W_GetTextSelection(tPtr
->view
->screen
, XA_PRIMARY
);
1322 text
= W_GetTextSelection(tPtr
->view
->screen
,
1323 tPtr
->view
->screen
->clipboardAtom
);
1326 text
= W_GetTextSelection(tPtr
->view
->screen
,
1330 WMInsertTextFieldText(tPtr
, text
, tPtr
->cursorPosition
);
1332 NOTIFY(tPtr
, didChange
, WMTextDidChangeNotification
,
1333 (void*)WMInsertTextEvent
);
1341 if (tPtr
->flags
.pointerGrabbed
) {
1342 tPtr
->flags
.pointerGrabbed
= 0;
1343 XUngrabPointer(WMScreenDisplay(W_VIEW(tPtr
)->screen
), CurrentTime
);
1353 destroyTextField(TextField
*tPtr
)
1357 WMDeleteTimerHandler(tPtr
->timerID
);
1360 WMReleaseFont(tPtr
->font
);
1361 WMDeleteSelectionHandler(tPtr
, XA_PRIMARY
);
1362 WMRemoveNotificationObserver(tPtr
);