native_dialog.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package al
  2. // Native dialogs extension
  3. /*
  4. #cgo pkg-config: allegro_dialog-5
  5. #cgo CFLAGS: -I/usr/local/include
  6. #cgo linux LDFLAGS: -lc_nonshared
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <allegro5/allegro.h>
  10. #include <allegro5/allegro_native_dialog.h>
  11. #include "helpers.h"
  12. */
  13. import "C"
  14. import "unsafe"
  15. import "runtime"
  16. const (
  17. FILECHOOSER_FILE_MUST_EXIST = 1
  18. FILECHOOSER_SAVE = 2
  19. FILECHOOSER_FOLDER = 4
  20. FILECHOOSER_PICTURES = 8
  21. FILECHOOSER_SHOW_HIDDEN = 16
  22. FILECHOOSER_MULTIPLE = 32
  23. MESSAGEBOX_WARN = 1 << 0
  24. MESSAGEBOX_ERROR = 1 << 1
  25. MESSAGEBOX_OK_CANCEL = 1 << 2
  26. MESSAGEBOX_YES_NO = 1 << 3
  27. MESSAGEBOX_QUESTION = 1 << 4
  28. TEXTLOG_NO_CLOSE = 1 << 0
  29. TEXTLOG_MONOSPACE = 1 << 1
  30. EVENT_NATIVE_DIALOG_CLOSE = 600
  31. EVENT_MENU_CLICK = 601
  32. MENU_ITEM_ENABLED = 0
  33. MENU_ITEM_CHECKBOX = 1
  34. MENU_ITEM_CHECKED = 2
  35. MENU_ITEM_DISABLED = 4
  36. )
  37. type FileChooser struct {
  38. handle *C.ALLEGRO_FILECHOOSER
  39. }
  40. // Converts a file chooser to it's underlying C pointer
  41. func (self *FileChooser) toC() *C.ALLEGRO_FILECHOOSER {
  42. return (*C.ALLEGRO_FILECHOOSER)(self.handle)
  43. }
  44. // Destroys the file chooser.
  45. func (self *FileChooser) Destroy() {
  46. if self.handle != nil {
  47. C.al_destroy_native_file_dialog(self.toC())
  48. }
  49. self.handle = nil
  50. }
  51. // Wraps a C file chooser into a go file chooser
  52. func wrapFileChooserRaw(data *C.ALLEGRO_FILECHOOSER) *FileChooser {
  53. if data == nil {
  54. return nil
  55. }
  56. return &FileChooser{data}
  57. }
  58. // Sets up a finalizer for this FileChooser that calls Destroy()
  59. func (self *FileChooser) SetDestroyFinalizer() *FileChooser {
  60. if self != nil {
  61. runtime.SetFinalizer(self, func(me *FileChooser) { me.Destroy() })
  62. }
  63. return self
  64. }
  65. // Wraps a C voice into a go mixer and sets up a finalizer that calls Destroy()
  66. func wrapFileChooser(data *C.ALLEGRO_FILECHOOSER) *FileChooser {
  67. self := wrapFileChooserRaw(data)
  68. return self.SetDestroyFinalizer()
  69. }
  70. type TextLog struct {
  71. handle *C.ALLEGRO_TEXTLOG
  72. }
  73. // Converts a native_text_log to it's underlying C pointer
  74. func (self *TextLog) toC() *C.ALLEGRO_TEXTLOG {
  75. return (*C.ALLEGRO_TEXTLOG)(self.handle)
  76. }
  77. // Closes the native_text_log.
  78. func (self *TextLog) Close() {
  79. if self.handle != nil {
  80. C.al_close_native_text_log(self.toC())
  81. }
  82. self.handle = nil
  83. }
  84. // Wraps a C native_text_log into a go native_text_log
  85. func wrapTextLogRaw(data *C.ALLEGRO_TEXTLOG) *TextLog {
  86. if data == nil {
  87. return nil
  88. }
  89. return &TextLog{data}
  90. }
  91. // Sets up a finalizer for this TextLog that calls Destroy()
  92. func (self *TextLog) SetDestroyFinalizer() *TextLog {
  93. if self != nil {
  94. runtime.SetFinalizer(self, func(me *TextLog) { me.Close() })
  95. }
  96. return self
  97. }
  98. // Wraps a C native_text_log into a go native_text_log and sets up a finalizer that calls Destroy()
  99. func wrapTextLog(data *C.ALLEGRO_TEXTLOG) *TextLog {
  100. self := wrapTextLogRaw(data)
  101. return self.SetDestroyFinalizer()
  102. }
  103. type Menu struct {
  104. handle *C.ALLEGRO_MENU
  105. }
  106. // Converts a menu to it's underlying C pointer
  107. func (self *Menu) toC() *C.ALLEGRO_MENU {
  108. return (*C.ALLEGRO_MENU)(self.handle)
  109. }
  110. // Destroys the menu.
  111. func (self *Menu) Destroy() {
  112. if self.handle != nil {
  113. C.al_destroy_menu(self.toC())
  114. }
  115. self.handle = nil
  116. }
  117. // Wraps a C menu into a go menu
  118. func wrapMenuRaw(data *C.ALLEGRO_MENU) *Menu {
  119. if data == nil {
  120. return nil
  121. }
  122. return &Menu{data}
  123. }
  124. // Sets up a finalizer for this Menu that calls Destroy()
  125. func (self *Menu) SetDestroyFinalizer() *Menu {
  126. if self != nil {
  127. runtime.SetFinalizer(self, func(me *Menu) { me.Destroy() })
  128. }
  129. return self
  130. }
  131. // Wraps a C menu into a go menu and sets up a finalizer that calls Destroy()
  132. func wrapMenu(data *C.ALLEGRO_MENU) *Menu {
  133. self := wrapMenuRaw(data)
  134. return self.SetDestroyFinalizer()
  135. }
  136. type MenuInfo C.ALLEGRO_MENU_INFO
  137. func makeMenuInfo(text *string, id, flags int, icon *Bitmap) C.ALLEGRO_MENU_INFO {
  138. res := C.ALLEGRO_MENU_INFO{}
  139. if text == nil {
  140. res.caption = nil
  141. } else {
  142. bytes := []byte(*text)
  143. res.caption = (*C.char)(unsafe.Pointer(&bytes[0]))
  144. }
  145. res.id = cui16(id)
  146. res.flags = ci(flags)
  147. res.icon = icon.handle
  148. return res
  149. }
  150. /// Formats a menuinfo element for an element of the menu.
  151. func MakeMenuInfo(text *string, id, flags int, icon *Bitmap) MenuInfo {
  152. return (MenuInfo)(makeMenuInfo(text, id, flags, icon))
  153. }
  154. // Returns a menuinfo that is a separator
  155. func MenuSeparator() MenuInfo {
  156. return MakeMenuInfo(nil, -1, 0, nil)
  157. }
  158. // Returns a menuinfo that is the start of the menu
  159. func StartOfMenu(caption string, id int) MenuInfo {
  160. return MakeMenuInfo(&caption, id, 0, nil)
  161. }
  162. // Returns a menuinfo that is the end of the menu
  163. func EndOfMenu(caption string, id int) MenuInfo {
  164. return MakeMenuInfo(nil, 0, 0, nil)
  165. }
  166. // Starts the native dialog addon
  167. func InitNativeDialogAddon() bool {
  168. return cb2b(C.al_init_native_dialog_addon())
  169. }
  170. // Stops the native dialog addon
  171. func ShutdownNativeDialogAddon() {
  172. C.al_shutdown_native_dialog_addon()
  173. }
  174. // Creates a native file dialog.
  175. func CreateNativeFileDialogRaw(path, title, patterns string, mode int) *FileChooser {
  176. return nil
  177. //return wrapFileChooser()
  178. }
  179. /*
  180. TODO:
  181. ALLEGRO_DIALOG_FUNC(ALLEGRO_FILECHOOSER *, al_create_native_file_dialog, (char const *initial_path,
  182. char const *title, char const *patterns, int mode));
  183. ALLEGRO_DIALOG_FUNC(bool, al_show_native_file_dialog, (ALLEGRO_DISPLAY *display, ALLEGRO_FILECHOOSER *dialog));
  184. ALLEGRO_DIALOG_FUNC(int, al_get_native_file_dialog_count, (const ALLEGRO_FILECHOOSER *dialog));
  185. ALLEGRO_DIALOG_FUNC(const char *, al_get_native_file_dialog_path, (const ALLEGRO_FILECHOOSER *dialog,
  186. size_t index));
  187. ALLEGRO_DIALOG_FUNC(void, al_destroy_native_file_dialog, (ALLEGRO_FILECHOOSER *dialog));
  188. ALLEGRO_DIALOG_FUNC(int, al_show_native_message_box, (ALLEGRO_DISPLAY *display, char const *title,
  189. char const *heading, char const *text, char const *buttons, int flags));
  190. ALLEGRO_DIALOG_FUNC(ALLEGRO_TEXTLOG *, al_open_native_text_log, (char const *title, int flags));
  191. ALLEGRO_DIALOG_FUNC(void, al_close_native_text_log, (ALLEGRO_TEXTLOG *textlog));
  192. ALLEGRO_DIALOG_FUNC(void, al_append_native_text_log, (ALLEGRO_TEXTLOG *textlog, char const *format, ...));
  193. ALLEGRO_DIALOG_FUNC(ALLEGRO_EVENT_SOURCE *, al_get_native_text_log_event_source, (ALLEGRO_TEXTLOG *textlog));
  194. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_create_menu, (void));
  195. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_create_popup_menu, (void));
  196. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_build_menu, (ALLEGRO_MENU_INFO *info));
  197. ALLEGRO_DIALOG_FUNC(int, al_append_menu_item, (ALLEGRO_MENU *parent, char const *title, int id, int flags,
  198. ALLEGRO_BITMAP *icon, ALLEGRO_MENU *submenu));
  199. ALLEGRO_DIALOG_FUNC(int, al_insert_menu_item, (ALLEGRO_MENU *parent, int pos, char const *title, int id,
  200. int flags, ALLEGRO_BITMAP *icon, ALLEGRO_MENU *submenu));
  201. ALLEGRO_DIALOG_FUNC(bool, al_remove_menu_item, (ALLEGRO_MENU *menu, int pos));
  202. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_clone_menu, (ALLEGRO_MENU *menu));
  203. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_clone_menu_for_popup, (ALLEGRO_MENU *menu));
  204. ALLEGRO_DIALOG_FUNC(void, al_destroy_menu, (ALLEGRO_MENU *menu));
  205. ALLEGRO_DIALOG_FUNC(const char *, al_get_menu_item_caption, (ALLEGRO_MENU *menu, int pos));
  206. ALLEGRO_DIALOG_FUNC(void, al_set_menu_item_caption, (ALLEGRO_MENU *menu, int pos, const char *caption));
  207. ALLEGRO_DIALOG_FUNC(int, al_get_menu_item_flags, (ALLEGRO_MENU *menu, int pos));
  208. ALLEGRO_DIALOG_FUNC(void, al_set_menu_item_flags, (ALLEGRO_MENU *menu, int pos, int flags));
  209. ALLEGRO_DIALOG_FUNC(int, al_toggle_menu_item_flags, (ALLEGRO_MENU *menu, int pos, int flags));
  210. ALLEGRO_DIALOG_FUNC(ALLEGRO_BITMAP *, al_get_menu_item_icon, (ALLEGRO_MENU *menu, int pos));
  211. ALLEGRO_DIALOG_FUNC(void, al_set_menu_item_icon, (ALLEGRO_MENU *menu, int pos, ALLEGRO_BITMAP *icon));
  212. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_find_menu, (ALLEGRO_MENU *haystack, int id));
  213. ALLEGRO_DIALOG_FUNC(bool, al_find_menu_item, (ALLEGRO_MENU *haystack, int id, ALLEGRO_MENU **menu, int *index));
  214. ALLEGRO_DIALOG_FUNC(ALLEGRO_EVENT_SOURCE *, al_get_default_menu_event_source, (void));
  215. ALLEGRO_DIALOG_FUNC(ALLEGRO_EVENT_SOURCE *, al_enable_menu_event_source, (ALLEGRO_MENU *menu));
  216. ALLEGRO_DIALOG_FUNC(void, al_disable_menu_event_source, (ALLEGRO_MENU *menu));
  217. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_get_display_menu, (ALLEGRO_DISPLAY *display));
  218. ALLEGRO_DIALOG_FUNC(bool, al_set_display_menu, (ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu));
  219. ALLEGRO_DIALOG_FUNC(bool, al_popup_menu, (ALLEGRO_MENU *popup, ALLEGRO_DISPLAY *display));
  220. ALLEGRO_DIALOG_FUNC(ALLEGRO_MENU *, al_remove_display_menu, (ALLEGRO_DISPLAY *display));
  221. ALLEGRO_DIALOG_FUNC(uint32_t, al_get_allegro_native_dialog_version, (void));
  222. enum {
  223. ALLEGRO_FILECHOOSER_FILE_MUST_EXIST = 1,
  224. ALLEGRO_FILECHOOSER_SAVE = 2,
  225. ALLEGRO_FILECHOOSER_FOLDER = 4,
  226. ALLEGRO_FILECHOOSER_PICTURES = 8,
  227. ALLEGRO_FILECHOOSER_SHOW_HIDDEN = 16,
  228. ALLEGRO_FILECHOOSER_MULTIPLE = 32
  229. };
  230. enum {
  231. ALLEGRO_MESSAGEBOX_WARN = 1<<0,
  232. ALLEGRO_MESSAGEBOX_ERROR = 1<<1,
  233. ALLEGRO_MESSAGEBOX_OK_CANCEL = 1<<2,
  234. ALLEGRO_MESSAGEBOX_YES_NO = 1<<3,
  235. ALLEGRO_MESSAGEBOX_QUESTION = 1<<4
  236. };
  237. enum {
  238. ALLEGRO_TEXTLOG_NO_CLOSE = 1<<0,
  239. ALLEGRO_TEXTLOG_MONOSPACE = 1<<1
  240. };
  241. enum {
  242. ALLEGRO_EVENT_NATIVE_DIALOG_CLOSE = 600,
  243. ALLEGRO_EVENT_MENU_CLICK = 601
  244. };
  245. enum {
  246. ALLEGRO_MENU_ITEM_ENABLED = 0,
  247. ALLEGRO_MENU_ITEM_CHECKBOX = 1,
  248. ALLEGRO_MENU_ITEM_CHECKED = 2,
  249. ALLEGRO_MENU_ITEM_DISABLED = 4
  250. };
  251. */