DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.9KB

  1. /* libSOFD - Simple Open File Dialog [for X11 without toolkit]
  2. *
  3. * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #ifndef LIBSOFD_H
  24. #define LIBSOFD_H
  25. #include <X11/Xlib.h>
  26. ///////////////////////////////////////////////////////////////////////////////
  27. /* public API */
  28. /** open a file select dialog
  29. * @param dpy X Display connection
  30. * @param parent (optional) if not NULL, become transient for given window
  31. * @param x if >0 set explict initial width of the window
  32. * @param y if >0 set explict initial height of the window
  33. * @return 0 on success
  34. */
  35. int x_fib_show (Display *dpy, Window parent, int x, int y);
  36. /** force close the dialog.
  37. * This is normally not needed, the dialog closes itself
  38. * when a file is selected or the user cancels selection.
  39. * @param dpy X Display connection
  40. */
  41. void x_fib_close (Display *dpy);
  42. /** non-blocking X11 event handler.
  43. * It is safe to run this function even if the dialog is
  44. * closed or was not initialized.
  45. *
  46. * @param dpy X Display connection
  47. * @param event the XEvent to process
  48. * @return status
  49. * 0: the event was not for this window, or file-dialog still
  50. * active, or the dialog window is not displayed.
  51. * >0: file was selected, dialog closed
  52. * <0: file selection was cancelled.
  53. */
  54. int x_fib_handle_events (Display *dpy, XEvent *event);
  55. /** last status of the dialog
  56. * @return >0: file was selected, <0: canceled or inactive. 0: active
  57. */
  58. int x_fib_status ();
  59. /** query the selected filename
  60. * @return NULL if none set, or allocated string to be free()ed by the called
  61. */
  62. char *x_fib_filename ();
  63. /** customize/configure the dialog before calling \ref x_fib_show
  64. * changes only have any effect if the dialog is not visible.
  65. * @param k key to change
  66. * 0: set current dir to display (must end with slash)
  67. * 1: set title of dialog window
  68. * 2: specify a custom X11 font to use
  69. * 3: specify a custom 'places' file to include
  70. * (following gtk-bookmark convention)
  71. * @param v value
  72. * @return 0 on success.
  73. */
  74. int x_fib_configure (int k, const char *v);
  75. /** customize/configure the dialog before calling \ref x_fib_show
  76. * changes only have any effect if the dialog is not visible.
  77. *
  78. * @param k button to change:
  79. * 1: show hidden files
  80. * 2: show places
  81. * 3: show filter/list all (automatically hidden if there is no
  82. * filter function)
  83. * @param v <0 to hide the button >=0 show button,
  84. * 0: set button-state to not-checked
  85. * 1: set button-state to checked
  86. * >1: retain current state
  87. * @return 0 on success.
  88. */
  89. int x_fib_cfg_buttons (int k, int v);
  90. /** set custom callback to filter file-names.
  91. * NULL will disable the filter and hide the 'show all' button.
  92. * changes only have any effect if the dialog is not visible.
  93. *
  94. * @param cb callback function to check file
  95. * the callback function is called with the file name (basename only)
  96. * and is expected to return 1 if the file passes the filter
  97. * and 0 if the file should not be listed by default.
  98. * @return 0 on success.
  99. */
  100. int x_fib_cfg_filter_callback (int (*cb)(const char*));
  101. /* 'recently used' API. x-platform
  102. * NOTE: all functions use a static cache and are not reentrant.
  103. * It is expected that none of these functions are called in
  104. * parallel from different threads.
  105. */
  106. /** release static resources of 'recently used files'
  107. */
  108. void x_fib_free_recent ();
  109. /** add an entry to the recently used list
  110. *
  111. * The dialog does not add files automatically on open,
  112. * if the application succeeds to open a selected file,
  113. * this function should be called.
  114. *
  115. * @param path complete path to file
  116. * @param atime time of last use, 0: NOW
  117. * @return -1 on error, number of current entries otherwise
  118. */
  119. int x_fib_add_recent (const char *path, time_t atime);
  120. /** get a platform specific path to a good location for
  121. * saving the recently used file list.
  122. * (follows XDG_DATA_HOME on Unix, and CSIDL_LOCAL_APPDATA spec)
  123. *
  124. * @param application-name to use to include in file
  125. * @return pointer to static path or NULL
  126. */
  127. const char *x_fib_recent_file(const char *appname);
  128. /** save the current list of recently used files to the given filename
  129. * (the format is one file per line, filename URL encoded and space separated
  130. * with last-used timestamp)
  131. *
  132. * This function tries to creates the containing directory if it does
  133. * not exist.
  134. *
  135. * @param fn file to save the list to
  136. * @return 0: on success
  137. */
  138. int x_fib_save_recent (const char *fn);
  139. /** load a recently used file list.
  140. *
  141. * @param fn file to load the list from
  142. * @return 0: on success
  143. */
  144. int x_fib_load_recent (const char *fn);
  145. /** get number of entries in the current list
  146. * @return number of entries in the recently used list
  147. */
  148. unsigned int x_fib_recent_count ();
  149. /** get recently used entry at given position
  150. *
  151. * @param i entry to query
  152. * @return pointer to static string
  153. */
  154. const char *x_fib_recent_at (unsigned int i);
  155. #endif // LIBSOFD_H