Comments (4)
It's strange that GTK doesn't use signals here. You can use cffi:defcallback
however:
(gtk:define-application (:name test-menu :id "okaynoobz.org")
(defun test-menu-menu ()
(let ((menu (gio:make-menu)))
(let ((submenu (gio:make-menu)))
(gio:menu-append-item submenu (gio:make-menu-item :model menu :label "Load Excel File"
:detailed-action "app.open"))
(gio:menu-append-submenu menu "File" submenu))
menu))
(defun setup-file-dialog ()
(let ((dialog (gtk:make-file-dialog)))
dialog))
(cffi:defcallback file-open-callback :void ((file-dialog :pointer)
(result :pointer)
(data :pointer))
(declare (ignore data))
(setf file-dialog (gobj:pointer-object file-dialog 'gtk:file-dialog))
(funcall
(gtk::attach-restarts
(lambda ()
(let ((gfile (gtk:file-dialog-open-finish file-dialog result)))
(format t "~A~%" (gio:file-path gfile)))))))
(gtk:define-main-window (window (gtk:make-application-window :application gtk:*application*))
(setf (gtk:window-title window) "Test Menu")
(let ((action (gio:make-simple-action :name "open" :parameter-type nil)))
(gio:action-map-add-action gtk:*application* action)
(gtk:connect action "activate" (lambda (action param)
(declare (ignore action param))
(let ((dialog (gtk:make-file-dialog)))
(gtk:file-dialog-open dialog window nil (cffi:callback file-open-callback) (cffi:null-pointer))))))
(let ((window-box (gtk:make-box :orientation gtk:+orientation-vertical+ :spacing 0))
(menubar (gtk:make-popover-menu-bar :model (test-menu-menu))))
(gtk:box-append window-box menubar)
(setf (gtk:window-child window) window-box))
(unless (gtk:widget-visible-p window)
(gtk:window-present window))))
from cl-gtk4.
Okay, that does work with a small problem though. When cancelled, it shows an error as the gtk:with-restarts catches it. I tried ignoring the errors but that doesn't work. This solution doesn't work either but I got pretty far after learning callbacks...
(gtk:define-application (:name test-menu :id "okaynoobz.org")
(defun test-menu-menu ()
(let ((menu (gio:make-menu)))
(let ((submenu (gio:make-menu)))
(gio:menu-append-item submenu (gio:make-menu-item :model menu :label "Load Excel File"
:detailed-action "app.open"))
(gio:menu-append-submenu menu "File" submenu))
menu))
(cffi:defcallback file-open-callback :void ((file-dialog :pointer)
(result :pointer)
(data :pointer))
(declare (ignore data))
(setf file-dialog (gobj:pointer-object file-dialog 'gtk:file-dialog))
(funcall (gtk::attach-restarts (lambda ()
(let ((gfile (gtk:file-dialog-open-finish file-dialog result)))
(format t "The filepath: ~a~%" (gio:file-path gfile)))))))
(cffi:defcallback file-cancel-callback :void ()
(format t "Actually runs???!?!?!~%"))
(cffi:defcallback file-cancel-destroy-func :void ((data :pointer))
(declare (ignore data))
(format t "Please run~%")
(gtk::destroy-all-windows-and-quit))
(gtk:define-main-window (window (gtk:make-application-window :application gtk:*application*))
(setf (gtk:window-title window) "Test Menu")
(let ((action (gio:make-simple-action :name "open" :parameter-type nil)))
(gio:action-map-add-action gtk:*application* action)
(gtk:connect action "activate" (lambda (action param)
(declare (ignore action param))
(let ((dialog (gtk:make-file-dialog))
(cancellable (gio:make-cancellable)))
(gio:cancellable-connect cancellable
(cffi:callback file-cancel-callback)
(cffi:null-pointer)
(cffi:callback file-cancel-destroy-func))
(gtk:file-dialog-open dialog window cancellable
(cffi:callback file-open-callback)
(cffi:null-pointer))))))
(let ((window-box (gtk:make-box :orientation gtk:+orientation-vertical+ :spacing 0))
(menubar (gtk:make-popover-menu-bar :model (test-menu-menu))))
(gtk:box-append window-box menubar)
(setf (gtk:window-child window) window-box))
(unless (gtk:widget-visible-p window)
(gtk:window-present window))))
Kindly explain to me where I went wrong. Thanks.
Regards,
Hamza
from cl-gtk4.
You may try this to ignore errors from GTK:
(alexandria:when-let ((gfile (ignore-errors (gtk:file-dialog-open-finish file-dialog result))))
(format t "~A~%" (gio:file-path gfile)))
from cl-gtk4.
It works, thanks!
from cl-gtk4.
Related Issues (20)
- Typelib file for namespace 'Gtk', version '4.0' not found HOT 4
- How to make a GListStore of fixnums / How to subclass GObject HOT 2
- Library needs more documentation HOT 1
- Can't create simple application. HOT 4
- FLOATING-POINT-INVALID-OPERATION HOT 5
- arithmetic error DIVISION-BY-ZERO signalled HOT 2
- Cant create wigets to store it in variable HOT 1
- When vertically scaling the window, there is an artifact with the buttons. HOT 1
- No symbol named "POINTER-OBJECT" HOT 2
- Error g_error_free on Windows 11 HOT 6
- Can't properly pass "user_data" to callback for open file dialog HOT 2
- Support for Subclassing? HOT 1
- Radio buttons HOT 1
- Connect widget to key-pressed event HOT 3
- Can I create my own widgets? HOT 2
- (ql:quickload :cl-gtk4) fails, many issues: HOT 3
- Hoping for examples or documents about dialogs HOT 2
- Making executable HOT 2
- cl-gtk4 not working under SBCL v2.4.5 HOT 2
- Compilation error when expanding RUN-IN-MAIN-EVENT-LOOP macro HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cl-gtk4.