-
Notifications
You must be signed in to change notification settings - Fork 2
/
qapp.lisp
155 lines (136 loc) · 5.59 KB
/
qapp.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
;;; Copyright (c) 2009 David Lichteblau. 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.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; 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 AUTHOR 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.
(in-package :qt)
(named-readtables:in-readtable :qt)
;;; Create a QApplication from command line arguments.
;;;
;;; Usually, programs just pass argc and argv from their `main' function here.
;;; We take a Lisp list and set up a fresh argv array.
;;;
;;; First return value is the QApplication.
;;;
;;; Second return value is an update array of arguments.
;;; For example, "-display" "foo" will have been removed afterwards.
(defvar *qapplication* nil)
(defvar *qapplication-create-hooks* ())
(defmacro with-main-window ((window form) &body body)
`(progn
(make-qapplication)
(with-objects ((,window ,form))
,@body
(#_show ,window)
(#_exec *qapplication*))))
(defun make-qapplication (&rest args)
(cond (*qapplication*)
(t
(ensure-smoke :qtcore)
(ensure-smoke :qtgui)
(let ((instance (#_QCoreApplication::instance)))
(setf *qapplication*
(if (null-qobject-p instance)
(%make-qapplication (cons "argv0dummy" args))
instance)))
(dolist (hook *qapplication-create-hooks*)
(funcall hook *qapplication*))
*qapplication*)))
(defun %make-qapplication (args &optional (guip t))
(unless args
(error "argv[0] not specified"))
(mapc (lambda (arg) (check-type arg string)) args)
(let* ((args (coerce args 'simple-vector))
(argv
;; Memory leak: This array must not be freed earlier than the
;; QApplication. Let's just leak it.
(string-vector-to-char** args))
(&argc
;; Apparently this, too, needs to have extent for more than the ctor.
(cffi:foreign-alloc :int)))
(setf (cffi:mem-aref &argc :int) (length args))
(let* ((qapplication
(if guip
(#_new QApplication &argc argv)
(#_new QCoreApplication &argc argv)))
(updated-argc (cffi:mem-aref &argc :int)))
(values qapplication
(char**-to-string-vector argv updated-argc nil)))))
(defun describe-metaobject-methods (mo)
(let* ((offset (#_methodOffset mo)))
(format t "Metaobject ~A~%" mo)
(format t " superClass:~%")
(format t "~10T~A~%" (#_superClass mo))
(format t " inherited methods:~%")
(dotimes (i offset)
(format t "~10T~A~%" (#_signature (#_method mo i))))
(format t " direct methods:~%")
(loop for i from offset below (#_methodCount mo) do
(format t "~10T~A~%" (#_signature (#_method mo i))))))
(defun describe-metamethods (object)
(format t "Metaobject for ~A:~%" object)
(describe-metaobject-methods (qobject-metaobject object)))
(defun windows-version ()
(let ((v (sw_windows_version)))
(if (minusp v) nil v)))
(defvar +xp+ #x30)
(defvar +2003+ #x40)
(defvar +vista+ #x80)
(defun set-nice-theme ()
;; This function isn't called by CommonQt automatically, but user
;; code can use it if desired.
;;
;; The native look on Vista is great, but XP's widgets look antiquated.
;; Let's use Plastique on XP instead.
;;
;; On non-Windows, we don't have this problem, so do nothing.
;;
(ensure-smoke :qtcore)
(ensure-smoke :qtgui)
(let ((v (windows-version)))
(when (and v (< v +vista+))
(#_QApplication::setStyle "Plastique"))))
(defmacro with-int& ((var value) &body body)
`(invoke-with-int& (lambda (,var) ,@body) ,value))
(defun invoke-with-int& (fun value)
(cffi:with-foreign-object (reference :int)
(setf (cffi:mem-aref reference :int) value)
(funcall fun reference)
(cffi:mem-aref reference :int)))
(defmacro with-&bool ((var value) &body body)
`(invoke-with-&bool (lambda (,var) ,@body) ,value))
(defun invoke-with-&bool (fun value)
(cffi:with-foreign-object (reference :int)
(setf (cffi:mem-aref reference :int) (if value 1 0))
(funcall fun reference)
(logtest 1 (cffi:mem-aref reference :int))))
(defmacro with-char** ((var string-list) &body body)
`(invoke-with-char** (lambda (,var) ,@body) ,string-list))
(defun invoke-with-char** (fun data)
(loop
for item across data
do (check-type item string))
(cffi:with-foreign-object (argv :pointer (length data))
(string-vector-to-char**! argv data)
(funcall fun argv)
(char**-to-string-vector! data argv (length data) t)))