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.

105 lines
3.5KB

  1. //
  2. // "$Id: native-filechooser.cxx 8165 2011-01-01 20:27:07Z matt $"
  3. //
  4. // Simple test of the Fl_Native_File_Chooser.
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. // Copyright 2004 Greg Ercolano.
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Library General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // Library General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU Library General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. // USA.
  23. //
  24. // Please report all bugs and problems on the following page:
  25. //
  26. // http://www.fltk.org/str.php
  27. //
  28. #include <stdio.h>
  29. #include <FL/Fl.H>
  30. #include <FL/fl_ask.H> // fl_beep()
  31. #include <FL/Fl_Window.H>
  32. #include <FL/Fl_Button.H>
  33. #include <FL/Fl_Input.H>
  34. #include <FL/Fl_Box.H>
  35. #include <FL/Fl_Native_File_Chooser.H>
  36. // GLOBALS
  37. Fl_Input *G_filename = NULL;
  38. void Butt_CB(Fl_Widget*, void*) {
  39. // Create native chooser
  40. Fl_Native_File_Chooser native;
  41. native.title("Pick a file");
  42. native.type(Fl_Native_File_Chooser::BROWSE_FILE);
  43. native.filter("Text\t*.txt\n"
  44. "C Files\t*.{cxx,h,c}\n"
  45. "Apps\t*.{app}\n"); // TODO: need to add kNavSupportPackages to non-cocoa <FNFC>_MAC.cxx
  46. native.preset_file(G_filename->value());
  47. // Show native chooser
  48. switch ( native.show() ) {
  49. case -1: fprintf(stderr, "ERROR: %s\n", native.errmsg()); break; // ERROR
  50. case 1: fprintf(stderr, "*** CANCEL\n"); fl_beep(); break; // CANCEL
  51. default: // PICKED FILE
  52. if ( native.filename() ) {
  53. G_filename->value(native.filename());
  54. } else {
  55. G_filename->value("NULL");
  56. }
  57. break;
  58. }
  59. }
  60. int main(int argc, char **argv) {
  61. //// For a nicer looking browser under linux, call Fl_File_Icon::load_system_icons();
  62. //// (If you do this, you'll need to link with fltk_images)
  63. //// NOTE: If you do not load the system icons, the file chooser will still work, but
  64. //// no icons will be shown. However, this means you do not need to link in the
  65. //// fltk_images library, potentially reducing the size of your executable.
  66. //// Loading the system icons is not required by the OSX or Windows native file choosers.
  67. #if !defined(WIN32) && !defined(__APPLE__)
  68. Fl_File_Icon::load_system_icons();
  69. #endif
  70. int argn = 1;
  71. #ifdef __APPLE__
  72. // OS X may add the process number as the first argument - ignore
  73. if (argc>argn && strncmp(argv[1], "-psn_", 5)==0)
  74. argn++;
  75. #endif
  76. Fl_Window *win = new Fl_Window(600, 100, "Native File Chooser Test");
  77. win->size_range(300, 100, 0, 100);
  78. win->begin();
  79. {
  80. int y = 10;
  81. G_filename = new Fl_Input(80, y, win->w()-80-10, 25, "Filename");
  82. G_filename->value(argc <= argn ? "." : argv[argn]);
  83. G_filename->tooltip("Default filename");
  84. y += G_filename->h() + 5;
  85. Fl_Button *but = new Fl_Button(win->w()-80-10, win->h()-25-10, 80, 25, "Pick File");
  86. but->callback(Butt_CB);
  87. Fl_Box *dummy = new Fl_Box(80, 0, 430, 100);
  88. dummy->hide();
  89. win->resizable(dummy);
  90. }
  91. win->end();
  92. win->show(argc, argv);
  93. return(Fl::run());
  94. }
  95. //
  96. // End of "$Id: native-filechooser.cxx 8165 2011-01-01 20:27:07Z matt $".
  97. //