native_dialog.go 9.3 KB

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