Skip to content

Instantly share code, notes, and snippets.

@lagagain
Last active December 4, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagagain/a8573ca949e85f70fb0739979873180f to your computer and use it in GitHub Desktop.
Save lagagain/a8573ca949e85f70fb0739979873180f to your computer and use it in GitHub Desktop.
Try Common Lisp GTK(cl-cffi-gtk)

install cl-cffi-gtk by quicklisp

(ql:quickload :cl-cffi-gtk)

load and exec

(require :asdf)
(asdf:load-system 'try-gtk-example)

then type (try-gtk-example:app.run), (try-gtk-example:example-hello-world) or (try-gtk-example:example-simple-window)

cannot load system?

method 1: add default-pathname or the directory path which file are in to asdf:central-registry

(push '*default-pathname-defaults*  asdf:*central-registry* )

(at file directory) try again

(asdf:load-system 'try-gtk-example)

method 2: load-asd with absoult path

(asdf:load-asd  (truename "try-gtk-example.asd"))

then load system

(asdf:load-system 'try-gtk-example)

method 3: swithc to asdf-user package and load file

(in-package asdf-user)
(load "try-gtk-example.asd")
(in-package cl-user)
(asdf:load-system 'try-gtk-example)

method 4: direct load lisp file then use it

(load "hello-world.lisp")
;; test
(try-gtk-exampl:app.run)

LICENCE

Copyright (c) 2018, lagagain All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(require :cl-cffi-gtk)
;; (use-package :gtk)
;; (use-package :gdk)
;; (use-package :gdk-pixbuf)
;; (use-package :gobject)
;; (use-package :glib)
;; (use-package :gio)
;; (use-package :pango)
;; (use-package :cairo)
;; (defvar *window* (gtk-window-new :toplevel))
;; (setf (gtk-window-title *window*) "Hello World for Test")
;; (defvar *button* (make-instance'gtk-button :label "Hello World"))
;; (g-signal-connect *window* "destroy"
;; (lambda (widget)
;; (declare (ignore widget))
;; (format t "~%Leaving...~%")
;; (leave-gtk-main)))
;; (g-signal-connect *button* "clicked"
;; (lambda (widget)
;; (declare (ignore widget))
;; (format t "~%Clicked: Click Hello~%")
;; (gtk-widget-destroy *window*)))
;; (gtk-container-add *window* *button*)
;; (gtk-widget-show-all *window*)
(defpackage :try-gtk-example
(:use :gtk :gdk :gdk-pixbuf :gobject
:glib :gio :pango :cairo :common-lisp)
(:export example-hello-world
example-simple-window
app.run))
(in-package :try-gtk-example)
(defun example-simple-window ()
(within-main-loop
(let (;; Create a toplevel window.
(window (gtk-window-new :toplevel)))
;; Signal handler for the window to handle the signal "destroy".
(g-signal-connect window "destroy"
(lambda (widget)
(declare (ignore widget))
(leave-gtk-main)))
;; Show the window.
(gtk-widget-show-all window))))
(defun example-hello-world ()
(within-main-loop
(let (;; Create a toplevel window, set a border width.
(window (make-instance 'gtk-window
:type :toplevel
:title "Hello World"
:default-width 250
:border-width 12))
;; Create a button with a label.
(button (make-instance 'gtk-button :label "Hello World")))
;; Signal handler for the button to handle the signal "clicked".
(g-signal-connect button "clicked"
(lambda (widget)
(declare (ignore widget))
(format t "Hello world.~%")
(gtk-widget-destroy window)))
;; Signal handler for the window to handle the signal "destroy".
(g-signal-connect window "destroy"
(lambda (widget)
(declare (ignore widget))
(leave-gtk-main)))
;; Signal handler for the window to handle the signal "delete-event".
(g-signal-connect window "delete-event"
(lambda (widget event)
(declare (ignore widget event))
(format t "Delete Event Occured.~%")
+gdk-event-stop+))
;; Put the button into the window.
(gtk-container-add window button)
;; Show the window and the button.
(gtk-widget-show-all window))))
;; (defun app.run nil
;; (let ((window (make-instance 'gtk-window :type :toplevel
;; :title "Hello World for test"))
;; (button (make-instance 'gtk-button :label "Hello World")))
;; (g-signal-connect window "destroy"
;; (lambda (w)
;; (declare (ignore w))
;; (format t "~%Leaving....~%")
;; (leave-gtk-main)))
;; (g-signal-connect button "clicked"
;; (lambda (w)
;; (declare (ignore w))
;; (format t "~%Clicked: Hello World.~%")))
;; (gtk-container-add window button)
;; (gtk-widget-show-all window)
;; (gtk-main))
;; 0)
;; (defun app.run nil
;; (let ((window (make-instance 'gtk-window :type :toplevel
;; :title "Hello World for test"))
;; (button (make-instance 'gtk-button :label "Hello World")))
;; (g-signal-connect window "destroy"
;; (lambda (w)
;; (declare (ignore w))
;; (format t "~%Leaving....~%")
;; (leave-gtk-main)))
;; (g-signal-connect button "event"
;; (lambda (widget event)
;; (declare (ignorable widget))
;; (format t "~%Event: ~A~%" event)))
;; (g-signal-connect button "clicked"
;; (lambda (w)
;; (declare (ignore w))
;; (format t "~%Clicked: Hello World.~%")))
;; (gtk-container-add window button)
;; (gtk-widget-show-all window)
;; (gtk-main))
;; 0)
(defvar *event-info*)
(defun app.run nil
(within-main-loop
(let ((window (make-instance 'gtk-window :type :toplevel
:title "My App Test"))
(button (make-instance 'gtk-button :label "My App Test, Click output hello wolr.")))
(g-signal-connect button "clicked"
(lambda (widget)
(declare (ignorable widget))
(format t "~&Hello, World~%")))
(g-signal-connect window "destroy"
(lambda (widget)
(declare (ignorable widget))
(format t "~%Leaving....~%")
(leave-gtk-main)))
(g-signal-connect button "button_press_event"
(lambda (widget event)
(declare (ignorable widget event))
(format t "~%Event: ~[Zero~;Left~;Middle~;Right~:;????~] ~A;~&Botton Code: ~0@*~A~%"
(slot-value event 'gdk::button)
(slot-value event 'type))
nil))
(gtk-container-add window button)
(gtk-widget-show-all window))))
Copyright (c) 2018, lagagain
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
;; (require :asdf)
;; (in-package 'asdf-user)
(defsystem try-gtk-example
:author "lagagain <lagagain@email.com>"
:licence "BSD"
:version "1.0.0"
:depends-on (:cl-cffi-gtk)
:components ((:file "hello-world")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment