[racket] GUI: text field on a canvas

140 views
Skip to first unread message

Dmitry Pavlov

unread,
Jul 17, 2014, 4:24:22 PM7/17/14
to us...@racket-lang.org
Hello,

I have the need to put a text field into a certain location of a canvas,
without decorations.
What I came up with so far is:

#lang racket
(require racket/gui)

(define frame (new frame% (label "Test")))
(define pasteboard (new pasteboard%))

(define editor-canvas
(new editor-canvas%
(editor pasteboard)
(parent frame)
(style '(no-border no-hscroll no-vscroll))))

(define t (new text%))
(define ts
(new editor-snip%
(editor t)
(left-margin 0)
(right-margin 0)
(top-margin 0)
(bottom-margin 0)
(with-border? #t)))
(send pasteboard insert ts 0 0)

(define yellow-delta (send (make-object style-delta%)
set-delta-background (make-object color% "yellow")))
(send t change-style yellow-delta)
(send pasteboard set-caret-owner ts)

(send frame resize 100 100)
(send frame show #t)


Two questions:

1. Is it the most natural way to do what I need?
(I also looked into embedded-gui but did not find
anything that looks like an embedded text field).

2. What I see is:
http://imgur.com/Ws4R140
It is obvious that the text is off the (0,0) point where
it should be by about 5 pixels by x and y.
I tried placing it in (-5, -5) and got this:
http://imgur.com/WrSE1qG
Setting (with-border? #f) did not help.
How can it be fixed?

Best regards,

Dmitry
____________________
Racket Users list:
http://lists.racket-lang.org/users

Robby Findler

unread,
Jul 18, 2014, 1:24:04 AM7/18/14
to Dmitry Pavlov, us...@racket-lang.org
You can put the text% object directly into the editor-canvas if you want. Also, the 5 pixels is part of the margin I believe and you can set it to 0 if you want. 

Robby

Matthew Flatt

unread,
Jul 18, 2014, 1:51:27 AM7/18/14
to Dmitry Pavlov, us...@racket-lang.org
To get rid of the 5 pixels, you want to set the "inset", not the
"margin". See `vertical-inset` and `horizontal-inset` in `editor-canvas%`.

Dmitry Pavlov

unread,
Jul 18, 2014, 3:36:20 AM7/18/14
to Matthew Flatt, Robby Findler, us...@racket-lang.org
Matthew,

> To get rid of the 5 pixels, you want to set the "inset", not the
> "margin". See `vertical-inset` and `horizontal-inset` in `editor-canvas%`.

Thanks, it worked!

Robby,

>> You can put the text% object directly into the editor-canvas if you want.

Err, how do I do that? I just tried

(require racket/gui)
(define frame (new frame% (label "Test")))
(define pasteboard (new pasteboard%))
(define editor-canvas
(new editor-canvas%
(editor pasteboard)
(parent frame)
(style '(no-border no-hscroll no-vscroll))))
(define t (new text%))
(send pasteboard insert t 0 0)
(send frame resize 100 100)
(send frame show #t)

and got

insert in pasteboard%: bad argument combination: (object:text% ...) 0 0

Robby Findler

unread,
Jul 18, 2014, 3:49:41 AM7/18/14
to Dmitry Pavlov, Matthew Flatt, us...@racket-lang.org


On Friday, July 18, 2014, Dmitry Pavlov <dpa...@ipa.nw.ru> wrote:
Matthew,

> To get rid of the 5 pixels, you want to set the "inset", not the
> "margin". See `vertical-inset` and `horizontal-inset` in `editor-canvas%`.

Thanks, it worked!

Robby,

>> You can put the text% object directly into the editor-canvas if you want.

Err, how do I do that? I just tried

(require racket/gui)
(define frame (new frame% (label "Test")))
(define pasteboard (new pasteboard%))
(define editor-canvas
  (new editor-canvas%
       (editor pasteboard)

Use [editor t] on the line just above here (and move the definition of t up).

And sorry for getting the margin and inset mixed up!

Robby

Dmitry Pavlov

unread,
Jul 18, 2014, 4:09:04 AM7/18/14
to Robby Findler, us...@racket-lang.org
Robby,

> Use [editor t] on the line just above here (and move the definition of t up).

Ah, I see. But what I actually need is an editor that
does not occupy the whole canvas (other area of
the canvas can have some graphics on it), and also
I need to place the editor arbitrarily on the canvas.

With that in mind, the pasteboard becomes necessary,
am I right?

Robby Findler

unread,
Jul 18, 2014, 4:16:51 AM7/18/14
to Dmitry Pavlov, us...@racket-lang.org
Oh, right. 

Robby


On Friday, July 18, 2014, Dmitry Pavlov <dpa...@ipa.nw.ru> wrote:
Reply all
Reply to author
Forward
0 new messages