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.

99 lines
3.3KB

  1. /* SpiralSound
  2. * Copyleft (C) 2001 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "WaveChooser.h"
  19. #include <string>
  20. #include <FL/Fl_File_Chooser.H>
  21. #include "../../../config.h"
  22. using namespace std;
  23. #ifdef USE_LIBSNDFILE
  24. #include <sndfile.h>
  25. #endif
  26. // As this stands, we get one load dialog per plugin,
  27. // I'd rather have just ONE dialog for all plugins
  28. // large chunks of this are based on fl_file_chooser() from the FLTK source code
  29. // strlcpy() and strlcat() are some really useful BSD string functions
  30. // that work the way strncpy() and strncat() *should* have worked.
  31. size_t our_strlcpy (char *dst, const char *src, size_t size) {
  32. size_t srclen;
  33. size--;
  34. srclen = strlen (src);
  35. if (srclen > size) srclen = size;
  36. memcpy (dst, src, srclen);
  37. dst[srclen] = '\0';
  38. return (srclen);
  39. }
  40. static Fl_File_Chooser *fc = (Fl_File_Chooser *)0;
  41. static void (*current_callback)(const char*) = 0;
  42. static void callback(Fl_File_Chooser *, void*) {
  43. if (current_callback && fc->value()) (*current_callback)(fc->value());
  44. }
  45. static char retname[1024];
  46. char *WaveFileName (void) {
  47. string AvailFmt;
  48. #ifdef USE_LIBSNDFILE
  49. string FmtName;
  50. SF_FORMAT_INFO info;
  51. int major_count, m, p;
  52. sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int));
  53. for (m = 0 ; m < major_count ; m++) {
  54. info.format = m;
  55. sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info));
  56. FmtName = info.name;
  57. while ((p=FmtName.find ('(')) >= 0 ) FmtName.replace (p, 1, "[");
  58. while ((p=FmtName.find (')')) >= 0 ) FmtName.replace (p, 1, "]");
  59. if (!AvailFmt.empty()) AvailFmt += '\t';
  60. AvailFmt += (string)FmtName + (string)" (*." + (string)info.extension + ')';
  61. }
  62. #else
  63. AvailFmt = "{*.wav,*.WAV}";
  64. #endif
  65. char *fname;
  66. char *title = "Load a wave";
  67. if (!fc) {
  68. fname = ".";
  69. // as ever, I'm a bit worried that this is never deallocated
  70. fc = new Fl_File_Chooser (fname, AvailFmt.c_str(), Fl_File_Chooser::CREATE, title);
  71. fc->callback (callback, 0);
  72. } else {
  73. // strip away the old filename, but keep the directory
  74. our_strlcpy (retname, fc->value(), sizeof(retname));
  75. char *p = strrchr(retname, '/');
  76. if (p) {
  77. if (p == retname) retname[1] = '\0';
  78. else *p = '\0';
  79. }
  80. fc->directory (retname);
  81. }
  82. fc->show();
  83. while (fc->shown()) Fl::wait();
  84. if (fc->value()) return (char *)fc->value();
  85. else return 0;
  86. }