Skip to content

Commit

Permalink
Make browser and editor examples work again
Browse files Browse the repository at this point in the history
Also, Gtk upstream says to prefer grid to hbox and vbox
  • Loading branch information
spk121 committed Sep 2, 2019
1 parent 314eb02 commit 35a846d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
7 changes: 5 additions & 2 deletions examples/browser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https:;;www.gnu.org/licenses/>.
(use-modules (gi) (gi repository)
(use-modules (gi)
(gi repository)
(gi types)
(srfi srfi-26)
(oop goops))

Expand All @@ -22,6 +24,7 @@
'("2.0" "3.0" "4.0"))

(load-by-name "Gio" "Application")
(load-by-name "Gio" "ApplicationFlags")
(load-by-name "WebKit2" "WebView")

(for-each
Expand All @@ -39,7 +42,7 @@
(show-all window)))

(define (main)
(let ((app (application:new "org.gtk.example" 0)))
(let ((app (application:new "org.gtk.example" (number->application-flags 0))))
(connect app application:activate activate)
(run app (command-line))))

Expand Down
77 changes: 35 additions & 42 deletions examples/editor.scm
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,17 @@

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https:;;www.gnu.org/licenses/>.
(use-modules (gi) (gi repository)
(srfi srfi-26)
(oop goops)
(use-modules (gi)
(gi repository)
(gi types)
(gi util)
(oop goops describe)
(ice-9 receive))

(map require
'("Gio" "Gtk" "Gdk")
'("2.0" "3.0" "3.0"))

(load-by-name "Gdk" "Event")
(load-by-name "Gdk" "EventMask")
(load-by-name "Gio" "Application")

(for-each
(cute load-by-name "Gtk" <>)
'("ApplicationWindow" "Application"
"Button" "VBox" "ButtonBox"
;; base types that we use for some methods
"Container" "Window" "Widget"
"TextView" "TextBuffer" "TextIter"))
(push-duplicate-handler! 'merge-generics)
(use-typelibs (("Gio" "2.0") #:renamer (protect* '(application:new receive)))
("Gtk" "3.0")
("Gdk" "3.0"))

(define (print-goodbye widget)
(display "Goodbye World\n"))
Expand All @@ -45,42 +36,44 @@

(define (activate app)
(let ((window (application-window:new app))
(vbox (vbox:new 0 0))
(grid (grid:new))
(editor (text-view:new))
(button-box (button-box:new 0))
(button-box (button-box:new (symbol->orientation 'horizontal)))
(button (button:new-with-label "Quit"))
(button2 (button:new-with-label "Hello")))
(add-events editor EVENT_MASK_KEY_PRESS_MASK)
(button2 (button:new-with-label "Hello"))
(key-press-mask (list->event-mask '(key-press-mask))))
(set-vexpand editor #t)
(set-hexpand editor #t)
(add-events editor (flags->number key-press-mask))
(attach grid editor 0 0 1 1)

(map add
(list button-box button-box vbox vbox window)
(list button2 button editor button-box vbox))
(set-layout button-box (symbol->button-box-style 'end))
(add button-box button2)
(add button-box button)
(attach grid button-box 0 1 1 1)

(add window grid)
(set-title window "Window")
(set-default-size window 200 200)

(map connect
(list editor button button button2)
(list key-press-event clicked clicked clicked)
(list key-press
print-goodbye (lambda x (destroy window))
;; When the 'hello' button is clicked, write the current contents
;; of the editor to the console, and replace the buffer contents
;; with 'Hello, world'.
(lambda x
(let ((buffer (get-buffer editor))
(iter1 (make <GtkTextIter>))
(iter2 (make <GtkTextIter>)))
(get-bounds buffer iter1 iter2)
(write (get-text buffer iter1 iter2 #t))
(newline)
(set-text buffer "Hello, world" 12)))))
(connect editor (make <signal> #:name "key-press-event") key-press)

This comment has been minimized.

Copy link
@LordYuuma

LordYuuma Sep 2, 2019

Collaborator

After some printf debugging, I found that key-press-event gets invalidated due to "event" ending up as a G_TYPE_NONE in its amap/type_meta resolution. For some reason, the gir refers to EventKey rather than Event and it turns out the former has no GType. So, what should we do in such a situation?

This comment has been minimized.

Copy link
@spk121

spk121 Sep 2, 2019

Author Owner

Curious. Looks like GdkEvent can handle signals despite not being a GObject since it is a union whose members (like GdkEventKey) are GObject types.

Gtk does make GdkEvent a GObject in the Gtk4 pre-release 3.93.0.

Don't know what the best strategy is. Let's make it an issue, post 0.2.0. When I get a chance, I'll scan the major typelibs for similar cases.

This comment has been minimized.

Copy link
@LordYuuma

LordYuuma Sep 2, 2019

Collaborator

Well, according to the introspection it is an interface with members who are struct types. As a result, we can't even go back to GdkEvent from introspection.

If GdkEvent is a proper GObject (with GType'd subclasses) in GTK-4, then we can consider the problem "solved on upstream", but I think we're going to be stuck with GTK-3 for a little while longer.

(connect button clicked print-goodbye)
(connect button clicked (lambda x (destroy window)))
(connect button2 clicked
(lambda x
(let ((buffer (get-buffer editor))
(iter1 (make <GtkTextIter>))
(iter2 (make <GtkTextIter>)))
(get-bounds buffer iter1 iter2)
(write (get-text buffer iter1 iter2 #t))
(newline)
(set-text buffer "Hello, world" 12))))

(grab-focus editor)
(show-all window)))

(define (main)
(let ((app (application:new "org.gtk.example" 0)))
(let ((app (application:new "org.gtk.example" (number->application-flags 0))))
(connect app application:activate activate)
(run app (command-line))))

Expand Down

0 comments on commit 35a846d

Please sign in to comment.