Check-in [fe241039a3]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:added floline and intline widgets, updated demo2
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: fe241039a36d27525520a5e0ce801701557db115
User & Date: aldo 2016-09-13 10:47:40
Context
2016-12-15
01:04
some experimental changes check-in: 2cda79e659 user: aldo tags: test
01:00
Create new branch named "test" Leaf check-in: f47669ab62 user: aldo tags: test
2016-09-13
10:47
added floline and intline widgets, updated demo2 check-in: fe241039a3 user: aldo tags: trunk
2016-09-12
22:09
added textline editor, added text-align css support check-in: a2511885ee user: aldo tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to demos/demo2.png.

cannot compute difference between binary files

Changes to demos/demo2.ss.

36
37
38
39
40
41
42


43
44
45
46


47
48
49
50
51


52
53
54
55
56
57
58
59
60
61
62
63
64
65


66
67
68
		 (justify-content space-around)
		 (align-items center)
		 (flex-direction column)]
		[(id label1) ==>
		 (align-self stretch)]
		[ button ==> (text-align center)]
		[ label ==> (text-align center)]


		[textline ==> (background-color white)
			  (padding 5)
			  (min-width 200) (text-align right)
			  (color black)]


		))


(init-sdl "buttons")
(define my-text (make-parameter "some editable text!"))


(miogui-user-render
 (lambda ()
  (fps 25)
  (panel 'panel-1
	 (lambda () 
	   (if (button 'button1 "BUTTON 1")
	       (printf "BUTTON 1 CLICKED!\n"))
	   
	   (if (button 'button2 (format "FRAME NUMBER: ~d" (mi-frame-number)))
	       (printf "BUTTON 2 CLICKED!\n"))
	   (when (button 'button3 (format "FPS: ~,2F" mi-stat-fps))
		 (printf "BUTTON3 CLICKED!\n"))
	   (label 'label1 "1\nGOOD MORNING!\nLine 2\nLine 3\nLine 4")
	   (textline 'text1 my-text)))


  (debug-tooltip)))

(miogui-run)







>
>
|
|
|
|
>
>





>
>













|
>
>



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		 (justify-content space-around)
		 (align-items center)
		 (flex-direction column)]
		[(id label1) ==>
		 (align-self stretch)]
		[ button ==> (text-align center)]
		[ label ==> (text-align center)]
		[(or textline intline floline) 
		 ==> 
		 (background-color white)
		 (padding 5)
		 (min-width 200)
		 (color black)]
		[intline ==>  (text-align center)]
		[floline ==> (text-align right)]
		))


(init-sdl "buttons")
(define my-text (make-parameter "some editable text!"))
(define my-int (make-parameter 543210))
(define my-flo (make-parameter 3.141592))
(miogui-user-render
 (lambda ()
  (fps 25)
  (panel 'panel-1
	 (lambda () 
	   (if (button 'button1 "BUTTON 1")
	       (printf "BUTTON 1 CLICKED!\n"))
	   
	   (if (button 'button2 (format "FRAME NUMBER: ~d" (mi-frame-number)))
	       (printf "BUTTON 2 CLICKED!\n"))
	   (when (button 'button3 (format "FPS: ~,2F" mi-stat-fps))
		 (printf "BUTTON3 CLICKED!\n"))
	   (label 'label1 "1\nGOOD MORNING!\nLine 2\nLine 3\nLine 4")
	   (textline 'text1 my-text)
	   (intline 'int1 my-int)
	   (floline 'flo1 my-flo 4)))
  (debug-tooltip)))

(miogui-run)

Changes to widgets.ss.

147
148
149
150
151
152
153
154
155
156






157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

200
201
202
203
204
205

206
207
208
209
210
211
212
213
214
215
216
217
218
219
...
223
224
225
226
227
228
229


































	       (if (> val 1) (set! val 1))
	     (cond [(not (= (state) val))
		    (state val)
		    #t]
		   [else #f])))))))


(define (textline id text)
  (import (only (srfi s14 char-sets) char-set)
	  (only (thunder-utils) string-split string-replace))






  (create-element 
   'textline id #t
   (lambda ()
     (define-values (x y w h) (values (mi-x) (mi-y) (mi-w) (mi-h)))
     (define (cursor-pos) (mi-wget id 'cursor-pos 0))
     (define (cursor-pos-move dir)
       (let ([cp (cursor-pos)])
	 (cond
	  [(and (< dir 0) (> cp 0))
	   (mi-wset id 'cursor-pos (- cp 1))]
	  [(and (> dir 0) (< cp (string-length (text))))
	   (mi-wset id 'cursor-pos (+ cp 1))])))
     (draw-rect x y w h)

     (if (> (cursor-pos) (string-length (my-text)))
	 (mi-wset id 'cursor-pos (string-length (my-text))))

     (when (eq? (mi-kbd-item) id)
       (let ([txt (text)]
	     [txt-len (string-length (text))])
	 (case (mi-key)
	   [backspace
	    (when (and (> txt-len 0) (> (cursor-pos) 0))
................................................................................
	   [delete
	    (when (and (> txt-len 0) (>= (cursor-pos) 0)
		       (< (cursor-pos) txt-len))
	      (text (string-append
		     (substring txt 0 (cursor-pos))
		     (substring txt (+ (cursor-pos) 1) txt-len))))
	    (mi-key #f)]
	   [left  (cursor-pos-move -1)  (mi-key #f)]
	   [right (cursor-pos-move 1)  (mi-key #f)]
	   [home  (mi-wset id 'cursor-pos 0)
		  (mi-key #f)]
	   [end   (mi-wset id 'cursor-pos txt-len)  
		  (mi-key #f)]
	   [else
	    (when (mi-txt)

	      (text (string-append (substring txt 0 (cursor-pos)) (mi-txt)
				   (substring txt (cursor-pos) txt-len)))
	      (mi-wset id 'cursor-pos (+ (string-length (mi-txt))
					 (mi-wget id 'cursor-pos 0)))
	      (mi-txt #f))])))


     (let* ([extents (draw-text/padding (text) x y w h)]
	    [w* (car extents)]
	    [h* (mi-font-size)])
       (mi-element-content-size-set! (mi-el) (list w* h*))
       (when (and (eq? (mi-kbd-item) id)
		  (not (= 0 (logand (bitwise-arithmetic-shift-right (sdl-get-ticks) 9) 1))))
	 (let* ([cursor-pos (mi-wget id 'cursor-pos 0)]
		[size (text-extents (string-replace (substring (text) 0 cursor-pos) #\space #\-))]
		[padding (mi-padding)]
		[text-align (mi-text-align)])
	   (draw! 
	    (lambda ()
	      (define x1
		(case text-align
................................................................................
	      (with-cairo (mi-cr)
			  (set-source-color (mi-color))
			  (move-to x1 (+ y padding))
			  (line-to x1 (- (+ y h ) padding))
			  (set-line-width 1)
			  (stroke)))))))
     #f)))









































<
|
|
>
>
>
>
>
>

|











<
|
|







 







|







>
|
|
|
|


>






|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

175
176
177
178
179
180
181
182
183
...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	       (if (> val 1) (set! val 1))
	     (cond [(not (= (state) val))
		    (state val)
		    #t]
		   [else #f])))))))



(import (only (srfi s14 char-sets) char-set char-set:digit char-set-contains?)
	(only (thunder-utils) string-split string-replace))

;; TODO: improve the cursor calculation and maybe use the cairo-show-glyphs api
;; to have more control, then also text selection could be implemented...
;; need to implement the position of cursor when clicked
;; keyboard arrows, delete, backspace, home & end supported already
(define (line-editor el id text validator)
  (create-element 
   el id #t
   (lambda ()
     (define-values (x y w h) (values (mi-x) (mi-y) (mi-w) (mi-h)))
     (define (cursor-pos) (mi-wget id 'cursor-pos 0))
     (define (cursor-pos-move dir)
       (let ([cp (cursor-pos)])
	 (cond
	  [(and (< dir 0) (> cp 0))
	   (mi-wset id 'cursor-pos (- cp 1))]
	  [(and (> dir 0) (< cp (string-length (text))))
	   (mi-wset id 'cursor-pos (+ cp 1))])))
     (draw-rect x y w h)

     (if (> (cursor-pos) (string-length (text)))
	 (mi-wset id 'cursor-pos (string-length (text))))

     (when (eq? (mi-kbd-item) id)
       (let ([txt (text)]
	     [txt-len (string-length (text))])
	 (case (mi-key)
	   [backspace
	    (when (and (> txt-len 0) (> (cursor-pos) 0))
................................................................................
	   [delete
	    (when (and (> txt-len 0) (>= (cursor-pos) 0)
		       (< (cursor-pos) txt-len))
	      (text (string-append
		     (substring txt 0 (cursor-pos))
		     (substring txt (+ (cursor-pos) 1) txt-len))))
	    (mi-key #f)]
	   [left  (cursor-pos-move -1) (mi-key #f)]
	   [right (cursor-pos-move 1)  (mi-key #f)]
	   [home  (mi-wset id 'cursor-pos 0)
		  (mi-key #f)]
	   [end   (mi-wset id 'cursor-pos txt-len)  
		  (mi-key #f)]
	   [else
	    (when (mi-txt)
	      (when (and (string? (mi-txt)) (validator (string-ref (mi-txt) 0)))
		(text (string-append (substring txt 0 (cursor-pos)) (mi-txt)
				     (substring txt (cursor-pos) txt-len)))
		(mi-wset id 'cursor-pos (+ (string-length (mi-txt))
					   (mi-wget id 'cursor-pos 0))))
	      (mi-txt #f))])))

     ;; BLINKING CURSOR
     (let* ([extents (draw-text/padding (text) x y w h)]
	    [w* (car extents)]
	    [h* (mi-font-size)])
       (mi-element-content-size-set! (mi-el) (list w* h*))
       (when (and (eq? (mi-kbd-item) id)
		  (not (= 0 (logand (bitwise-arithmetic-shift-right (sdl-get-ticks) 9) 1))))
	 (let* ([cursor-pos (cursor-pos)]
		[size (text-extents (string-replace (substring (text) 0 cursor-pos) #\space #\-))]
		[padding (mi-padding)]
		[text-align (mi-text-align)])
	   (draw! 
	    (lambda ()
	      (define x1
		(case text-align
................................................................................
	      (with-cairo (mi-cr)
			  (set-source-color (mi-color))
			  (move-to x1 (+ y padding))
			  (line-to x1 (- (+ y h ) padding))
			  (set-line-width 1)
			  (stroke)))))))
     #f)))


(define (textline id text)
  (line-editor 'textline id text (lambda (x) x)))

(define (intline id value)
  (define str (number->string (if (number? (value)) (value) 0)))
  (define txt (make-parameter (if str str "0")))

  (line-editor 'intline id txt
	       (lambda (x) 
		 (char-set-contains? char-set:digit x)))
  (let ([num (string->number (txt))])
    (if num
	(value num)
	(value 0))))

;; FIXME, improve floline editing behavior (now little bit buggy)
;; maybe store the editing text as property, then if valid number assign that,
;; otherwise keep the old value and somehow visualize that the field is not valid
;; or better don't allow the user to insert invalid number

(define (floline id value digits)
  (define str (format (format "~d~d~d" "~," digits "F")(if (number? (value)) (value) 0)))
  (define txt (make-parameter (if str str "0")))

  (line-editor 'floline id txt
	       (lambda (x) 
		 (or (char-set-contains? char-set:digit x)
		     (eq? x #\.))))
  (let ([num (string->number (txt))])
    (if num
	(value num)
	(value 0))))