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.

162 lines
5.3KB

  1. //
  2. // "$Id: nativefilechooser-simple-app.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
  3. //
  4. // An example of how to use Fl_Native_File_Chooser to open & save files.
  5. //
  6. // Copyright 2010 Greg Ercolano.
  7. // Copyright 1998-2010 by Bill Spitzak and others.
  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> // printf
  29. #include <stdlib.h> // exit,malloc
  30. #include <string.h> // strerror
  31. #include <errno.h> // errno
  32. #include <FL/Fl.H>
  33. #include <FL/Fl_Window.H>
  34. #include <FL/Fl_Menu_Bar.H>
  35. #include <FL/Fl_Native_File_Chooser.H>
  36. #include <FL/Fl_Box.H>
  37. #include <FL/fl_ask.H>
  38. class Application : public Fl_Window {
  39. Fl_Native_File_Chooser *fc;
  40. // Does file exist?
  41. int exist(const char *filename) {
  42. FILE *fp = fopen(filename, "r");
  43. if (fp) { fclose(fp); return(1); }
  44. else { return(0); }
  45. }
  46. // 'Open' the file
  47. void open(const char *filename) {
  48. printf("Open '%s'\n", filename);
  49. }
  50. // 'Save' the file
  51. // Create the file if it doesn't exist
  52. // and save something in it.
  53. //
  54. void save(const char *filename) {
  55. printf("Saving '%s'\n", filename);
  56. if ( !exist(filename) ) {
  57. FILE *fp = fopen(filename, "w"); // create file if it doesn't exist
  58. if ( fp ) {
  59. // A real app would do something useful here.
  60. fprintf(fp, "Hello world.\n");
  61. fclose(fp);
  62. } else {
  63. fl_message("Error: %s: %s", filename, strerror(errno));
  64. }
  65. } else {
  66. // A real app would do something useful here.
  67. }
  68. }
  69. // Handle an 'Open' request from the menu
  70. static void open_cb(Fl_Widget *w, void *v) {
  71. Application *app = (Application*)v;
  72. app->fc->title("Open");
  73. app->fc->type(Fl_Native_File_Chooser::BROWSE_FILE); // only picks files that exist
  74. switch ( app->fc->show() ) {
  75. case -1: break; // Error
  76. case 1: break; // Cancel
  77. default: // Choice
  78. app->fc->preset_file(app->fc->filename());
  79. app->open(app->fc->filename());
  80. break;
  81. }
  82. }
  83. // Handle a 'Save as' request from the menu
  84. static void saveas_cb(Fl_Widget *w, void *v) {
  85. Application *app = (Application*)v;
  86. app->fc->title("Save As");
  87. app->fc->type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE); // need this if file doesn't exist yet
  88. switch ( app->fc->show() ) {
  89. case -1: break; // Error
  90. case 1: break; // Cancel
  91. default: // Choice
  92. app->fc->preset_file(app->fc->filename());
  93. app->save(app->fc->filename());
  94. break;
  95. }
  96. }
  97. // Handle a 'Save' request from the menu
  98. static void save_cb(Fl_Widget *w, void *v) {
  99. Application *app = (Application*)v;
  100. if ( strlen(app->fc->filename()) == 0 ) {
  101. saveas_cb(w,v);
  102. } else {
  103. app->save(app->fc->filename());
  104. }
  105. }
  106. static void quit_cb(Fl_Widget *w, void *v) {
  107. exit(0);
  108. }
  109. // Return an 'untitled' default pathname
  110. const char* untitled_default() {
  111. static char *filename = 0;
  112. if ( !filename ) {
  113. const char *home =
  114. getenv("HOME") ? getenv("HOME") : // unix
  115. getenv("HOME_PATH") ? getenv("HOME_PATH") : // windows
  116. "."; // other
  117. filename = (char*)malloc(strlen(home)+20);
  118. sprintf(filename, "%s/untitled.txt", home);
  119. }
  120. return(filename);
  121. }
  122. public:
  123. // CTOR
  124. Application() : Fl_Window(400,200,"Native File Chooser Example") {
  125. Fl_Menu_Bar *menu = new Fl_Menu_Bar(0,0,400,25);
  126. menu->add("&File/&Open", FL_COMMAND+'o', open_cb, (void*)this);
  127. menu->add("&File/&Save", FL_COMMAND+'s', save_cb, (void*)this);
  128. menu->add("&File/&Save As", 0, saveas_cb, (void*)this);
  129. menu->add("&File/&Quit", FL_COMMAND+'q', quit_cb);
  130. // Describe the demo..
  131. Fl_Box *box = new Fl_Box(20,25+20,w()-40,h()-40-25);
  132. box->color(45);
  133. box->box(FL_FLAT_BOX);
  134. box->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
  135. box->label("This demo shows an example of implementing "
  136. "common 'File' menu operations like:\n"
  137. " File/Open, File/Save, File/Save As\n"
  138. "..using the Fl_Native_File_Chooser widget.\n\n"
  139. "Note 'Save' and 'Save As' really *does* create files! "
  140. "This is to show how behavior differs when "
  141. "files exist vs. do not.");
  142. box->labelsize(12);
  143. // Initialize the file chooser
  144. fc = new Fl_Native_File_Chooser();
  145. fc->filter("Text\t*.txt\n");
  146. fc->preset_file(untitled_default());
  147. end();
  148. }
  149. };
  150. int main(int argc, char *argv[]) {
  151. Fl::scheme("gtk+");
  152. Application *app = new Application();
  153. app->show(argc,argv);
  154. return(Fl::run());
  155. }
  156. //
  157. // End of "$Id: nativefilechooser-simple-app.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".
  158. //