[Openmcl-devel] how to show NSAlert on Lisp startup
R. Matthew Emerson
rme at clozure.com
Fri Sep 16 19:45:51 UTC 2011
On Sep 14, 2011, at 5:47 PM, Alexander Repenning wrote:
> We have a need to show some NSAlerts at the start of running a CCL-based app on Mac/PC using Cocoa/Cocotron. So far we have not found any permutation that works of *restore-lisp-functions* and :toplevel-function (initarg to build-application) on PCs or Macs.
> The code below will not run the login function but not bring up the modal NSAlert on a Mac and crash on a PC. A variation using (ccl::build-application :name "Crasher" :toplevel-function 'login)
> does even worse. I am not completely surprised and assume that its just too early in the game of the Cocoa app launch. The main thread may not be running or just not be ready. We did try to call our function from #/applicationDidFinishLaunching which is called but that does not work either.
It should work to show an alert from #/applicationDidFinishLaunching. You should also be able to have an object listen for NSApplicationDidFinishLaunchingNotification and display an alert from there, as well.
If I put the following snippet at the end of #/applicationDidFinishLaunching, in ccl:cocoa-ide;app-delegate.lisp, it shows the alert for me.
(ccl::with-autoreleased-nsstrings ((title "Yow!")
(msg "Legally imposed CULTURE-reduction is CABBAGE-BRAINED!"))
(let ((alert (#/init (#/alloc ns:ns-alert))))
(#/setMessageText: alert title)
(#/setInformativeText: alert msg)
> Is there or how would one create a *Cocoa-Startup-Functions* capable to run modal NSAlerts on CCL Mac and PC?
> Any suggestions would be appreciated.
> (in-package :ccl)
> (defun NATIVE-STRING (String) "
> Return a native string"
> (#/autorelease (ccl::%make-nsstring String)))
> (defmethod STANDARD-ALERT-DIALOG ((Message string) &key
> (Yes-Text "OK")
> (No-Text nil)
> (Cancel-Text nil)
> (Is-Critical nil))
> (let ((Alert (#/init (#/alloc ns:ns-alert))))
> (#/setMessageText: Alert (native-string Message))
> (when Yes-Text (#/addButtonWithTitle: Alert (native-string Yes-Text)))
> (when No-Text (#/addButtonWithTitle: Alert (native-string No-Text)))
> (when Cancel-Text (#/addButtonWithTitle: Alert (native-string Cancel-Text)))
> (when Explanation-Text (#/setInformativeText: Alert (native-string Explanation-Text)))
> (#/setAlertStyle: Alert (if Is-Critical #$NSCriticalAlertStyle #$NSWarningAlertStyle))
> (case (#/runModal Alert)
> (#.#$NSAlertFirstButtonReturn t)
> (#.#$NSAlertSecondButtonReturn nil)
> (#.#$NSAlertThirdButtonReturn (throw :cancel nil)))))
> (defvar *Evidence-of-Login-Execution* nil)
> (defun LOGIN ()
> (setq *Evidence-of-Login-Execution* t)
> (standard-alert-dialog "Too good to be true?"))
> (pushnew 'login ccl::*restore-lisp-functions*)
> (require :build-application)
> (ccl::build-application :name "Crasher")
More information about the Openmcl-devel