hello i have this (define-syntax (on-delete stx)
(syntax-parse stx
[m:id (raise-syntax-error 'on-delete "bad syntax, use like begin" stx #'m)]
[(_ x ...)
#:with name (format-id stx "on-delete")
#:with (arg ...) (map (lambda (k) (format-id stx "~a" k)) '(entry))
#'(begin
(define (name arg ...)
x ...)
(provide name))]))
how do i make it throw a better error if i write something like (define (on-delete) (void))
Which error do you get now?
(define (on-delete) (void))
Looks like perfectly fine definition. Do you even get an error now?
wait… nevermind, im actually really stupid
what was the correct way to escape tiple-dots in macros that generate macros? i believed it to be ... ...
but it wasn’t, i think this a common question, but i can’t seem to know what terms to use for google to throw me an answer, so i guess #,'...
could work?
(... ...)
will work.
I sometimes do: (with-syntax ([ooo #'(... ...)])
(syntax/loc stx
yada yada ooo more yada yada))
oh i see thank you!
Since you use syntax-parse
, there’s no need to use with-syntax
. You can just add #:with ooo (quote-syntax ...)
.
But what are you trying to do? I don’t think (define (on-delete) (void))
is gonna work since define
is expanded first, so on-delete
wouldn’t be expanded at all.
nono, they were different issues, first was me not paying attention to what i was doing, second came up when automating something related to the first, and yes i am using #:with
but thank you for asking :)
thank you!!
thank you!!
can you please clarify for me: does it means an https://docs.racket-lang.org/gui/editor-canvas_.html\|editor-canvas% handle objects that are i.e vectors, images etc.. and https://docs.racket-lang.org/gui/editor-snip_.html\|editor-snip% handle text editor ?
editor-canvas%
is a canvas<%>
(a subwindow/control) that shows an editor<%>
editor-snip%
is a snip%
(an object%
with special functionality to be used inside an editor<%>
) that contains an editor<%>
editor<%>
: the ones provided by racket/gui
are pasteboard%
and text%
text%
provides functionality for text edition, pasteboard%
is more for vectors and the such.
i have been trying hard to have a mental picture of the whole concept
i see, is not easy to use editor<%>
, you might want to check <https://alex-hhh.github.io/tags/racket.html|Alex Harsányi posts about racket> that include some tutorials about using pasteboard%
, you might also want to read the <https://docs.racket-lang.org/framework/index.html|framework module> though that is more sophisticated and it depends on what you want to do.
i have been reading his posts too
wel, in the nutshell, i’m trying to build a wireframe editor, i have started my hand with a rectangle vector, i want to be able to either create a new rectangle or create an inner rectangle, which in turn will generate a code/dom node
but i’m wondering where to start from and what are the appropriate modules to use
ah i see, well you got me, that seems a little hard to do efficiently. from what i get from the chess tutorial by alex, those kind of interactions are done at the editor level/pasteboard i believe?
you overwrite the mouse event handlers, when clicking check if a bounding box of a underlying rectangle is colliding with your new rectangle, and have a children list in each rectangle-snip%
, i think you could just use racket/gui
this is what i have done so far and i’m stuck
huh i see, but how does this tie to your questions? mainly, what are the snip%
classes to be read/of interest. also what is the setup for the editor-canvas%
?
well, i tried following some examples where i have faint understanding on some of the concept,
no, wait that’ snot the code
hold a sec
here is the appropriate code,
#lang racket
(require racket/gui racket/draw pict)
(define board (new pasteboard%))
(define toplevel (new frame%
[label "My board"]
[width 500]
[height 500]))
(define canvas (new editor-canvas%
[parent toplevel]
[editor board]))
(send toplevel show #t)
(define my-snip-class
(new (class snip-class%
(super-new)
(send this set-classname "my-snip"))))
(send (get-the-snip-class-list) add my-snip-class)
(define rectangle-snip%
(class snip%
(init-field w h)
(super-new)
(send this set-snipclass my-snip-class)
(define/override (get-extent dc x y width height . other)
(when width (set-box! width w))
(when height (set-box! height h)))
(define/override (draw dc x y . other)
(draw-pict (rectangle w h) dc x y))))
(send board insert (new rectangle-snip% [w 30] [h 80]) 100 300)
I have come accrosss handles-all-mouse-events
and on-event
but how do i coordinate with them? I needed to click on the rectangle object to generate a new rectangle
wrote before your last comment: well for starters i am going to assume you want to create more rectangles with left click, subclass pasteboard%
and override https://docs.racket-lang.org/gui/pasteboard_.html#%28meth._%28%28%28lib._mred%2Fmain..rkt%29._pasteboard~25%29._on-default-event%29%29\|on-default-event which receives a https://docs.racket-lang.org/gui/mouse-event_.html\|mouse-event%
ok, let’s assume, a key-combination instead
ok, i’m reading on on-default event, i think mouse event is ok,
i have not used it, but for that i think that keymap%
is the way? if you need a combination of mouse + keyboard
ok
so, in the nutshell (define board (new pasteboard%) (super-new)
(new mouse-event% [event-type 'enter ] [[middle-down #t ]] ) )
is it correct ?
why the double brackets?
i think there is a typo somehwhere
also, i suggest reading the <https://docs.racket-lang.org/guide/classes.html|Classes and Objects in the Racket Guide> . to subclass you use the class*
form
also you don’t create mouse-event%
manually, they are handed to you through the handlers like on-default-event
can you please post an example ?
i think there must be a function that will create a new rectangle snip too
(class* pasteboard% ()
(init)
(super-new)
(define/override (on-default-event evt)
(do-something evt)))
yeah that would be handy
what’s the difference between class
and class*
?
class
just omits the part where you put the interfaces, so you dont need to (class* object% () (super-new))
see those empty brackets, you dont have to put them with class
this is much simpler :slightly_smiling_face:
from the above code, (do-something evt)
will be called to generate a new snip, right ?
I’ve added the new class but the rectangle is not drawn. how do i configure the event ?
#lang racket
(require racket/gui racket/draw pict)
(class* pasteboard% ()
(init)
(super-new)
(define/override (on-default-event evt)
(new-rect evt)))
(define board (new pasteboard%))
(define toplevel (new frame%
[label "My board"]
[width 500]
[height 500]))
(define canvas (new editor-canvas%
[parent toplevel]
[editor board]))
(send toplevel show #t)
(define my-snip-class
(new (class snip-class%
(super-new)
(send this set-classname "my-snip"))))
(send (get-the-snip-class-list) add my-snip-class)
(define rectangle-snip%
(class snip%
(init-field w h)
(super-new)
(send this set-snipclass my-snip-class)
(define/override (get-extent dc x y width height . other)
(when width (set-box! width w))
(when height (set-box! height h)))
(define/override (draw dc x y . other)
(draw-pict (rectangle w h) dc x y))))
(define (new-rect) (send board insert (new rectangle-snip% [w 30] [h 80]) 100 300))
ok so, class forms return classes value, so the pattern is (define my-pasteboard%
(class pasteboard%
...))
i recommend reading the racket guide
(define board (new pasteboard%))
then would be (define board (new my-pasteboard%))
how do i find all modules from the base package? i keep missing things like the data
module, etc.
@cris2000.espinoza677 You can see a list here: https://github.com/racket/racket/tree/master/racket/collects
oh thank you!
(if I guessed correctly about the meaning of “base package”)
when you open a module doc, you can see the (require <name>)
and to the left package: base
so i guess yes :smile:
what about the events?
on-default-event
accepts one argument that is of type mouse-event%
from mouse-event%
you can use things like (send evt get-x)
to get x, etc. all methods are in the mouse-event%
documentation
i have been reading about them but i’m always confused, i need some guidance about them. I’m still a novice in racket
are you a novice with programming too or just racket?
just racket, I know now to instantiate class, override class etc.. in other languages, i.e python, C++ but i’m still struggling with racket
(i suggest reading htdp book or the racket guide, although i just read beautiful racket instead of that)
ok so the thing goes, racket most of the time treats things as first class features that is one of the great things you can get from a lisp, most stuff can be easily implemented as a first class citizen in the language.
• <https://docs.racket-lang.org/racket-cheat/index.html|racket cheatsheet> the class system in racket i admit is kind of weird, again if you read the racket guide entry about classes and objects many of the doubts you have may disappear, also the racket reference has the nitty gritty details.
to subclass just bind the value of the class
/class*
form to an identifier with let
or define
. eg. (define my-object% (class object% (super-new)))
to call a method, also known as sending a message (i believe this comes from smalltalk) use the send
form.
for overriding, augmenting, etc methods in a subclass read https://docs.racket-lang.org/reference/createclass.html#%28part._clmethoddefs%29\|this because honestly i still dont quite understand it completely.
well, i’m having some slight notions about classes in racket, but, it’s easier in Qt to implement GUI as well, as graphics, but racket, i guess due to its lipsy nature, and there are lack of examples to follow which left to wonder the modules responsibilities and when to use them
it’s less complex than Qt, i find the window classes to be easier and more straightforward, but again the editor<%>
classes are not that easy, you have to do more lower level stuff…
*lower level in comparison to what other frameworks let you do.
my confusions lies within, editor<%>
, pasteboards
, snips
editor<%>
is an interface implemented by pasteboard%
, in which you can insert snip%
by using the insert
method.
snips are just an object that represent a thing inside editor<%>
so it saves its bounding box, how it’s drawn, and maybe some other data, but their positions are controlled by the editor<%>
object
i really think you should complete the chessboard tutorial by alex if you havent already
i’m still reading it
i hope i will pick up soon, there is a project i need to implement urgently, I have build part of it already in elisp, but I need to integrate the GUI aspect that’s why i needed to use Racket,
i wish you good luck
thanks,
a quick one, how do i keep record of the generated rectangle snips? I need to duplicate and build some additional properties/labels around them. i.e the, the xml properties they will hold
you could use mutation and a list inside of your class definition (define snip-list '())
(define (register-snip snip)
(set! snip-list (cons snip snip-list)))
for the properties see init
init-field
very buggy but like is it something like this that you want?
yes, this one step,
but i will need to refine it though,
but it’s a good start
thanks, however i have little questions, even though i’ve read them over and over, can you please clarify the use of box
and set-box
?
my computer died when i was about to answer: that is really a quirk of the library, i suppose it’s not have so many values returned as part of the function. box is like a 1 item list in python, it just literally a data structure (mutable in this case) that contains one element.
ok, noted,
playing with the code you’ve sent me, you’ve helped a lot,
you’re welcome, it is quite daunting when you have no examples…
will you be around tomorrow?
gah i promise nothing, im quite busy this week… but folks here might be glad to help
ok,
there is a little bug here, when i create a new rectangle and drag it, it duplicates
i think a new rectangle gets created based on the previous location info
yeah, the thing with my overriding of on-default-event
is that it calls the super
method without checking cases where i dont want certain behavior to occur, you can see in the gif that if you drag a rectangle it will create another underneath, that is because my code only checks if is the first left click and the release.
ok
could we constrain that event on mouse-click alone ? i think event
seems to be global
we might need to specify on-right-click or something
you could do that
what are use-cases of https://docs.racket-lang.org/reference/boxes.html\|Boxes ?