Advertisement
microwerx

FLTK Fl_Input / Fl_Output Example

Oct 31st, 2019
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. // FLTK Input and Output Example
  2. // by Jonathan Metzgar
  3. // CC-By-Attribution License
  4. //
  5. // This program will initialize a Fl_Input widget and a Fl_Output widget
  6. // The program also creates two buttons: one to quit the program and one to
  7. // process the data. The Model-View-Controller pattern is used to separate
  8. // data from the GUI.
  9.  
  10. #include <FL/Fl.H>
  11. #include <FL/Fl_Window.H>
  12. #include <FL/Fl_Box.H>
  13. #include <FL/Fl_Input.H>
  14. #include <FL/Fl_Output.H>
  15. #include <FL/Fl_Button.H>
  16.  
  17. #include <string>
  18. #include <iostream>
  19. #include <map>
  20.  
  21.  
  22. //////////////////////////////////////////////////////////////////////
  23. // E X T E R N A L   L I B R A R I E S ///////////////////////////////
  24. //////////////////////////////////////////////////////////////////////
  25.  
  26.  
  27. #pragma comment(lib, "fltk.lib")
  28. #pragma comment(lib, "comctl32.lib")
  29.  
  30.  
  31. //////////////////////////////////////////////////////////////////////
  32. // S T R U C T S /////////////////////////////////////////////////////
  33. //////////////////////////////////////////////////////////////////////
  34.  
  35.  
  36. struct Model
  37. {
  38.     std::string input;
  39. };
  40.  
  41.  
  42. struct View {
  43.     Fl_Input* input = nullptr;
  44.     Fl_Output* output = nullptr;
  45. };
  46.  
  47.  
  48. //////////////////////////////////////////////////////////////////////
  49. // G L O B A L   V A R I A B L E S ///////////////////////////////////
  50. //////////////////////////////////////////////////////////////////////
  51.  
  52.  
  53. Model model;
  54. View view;
  55.  
  56.  
  57. //////////////////////////////////////////////////////////////////////
  58. // P R O T O T Y P E S ///////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////////////////
  60.  
  61.  
  62. void TransformData(const std::string& input, std::string& output);
  63. void OnInputChanged(Fl_Widget* w, void* userdata);
  64. void OnProcessClicked(Fl_Widget* w, void* userdata);
  65. void OnQuitClicked(Fl_Widget* w, void* userdata);
  66. Fl_Window* CreateWindow();
  67.  
  68.  
  69. //////////////////////////////////////////////////////////////////////
  70. // C A L L B A C K S /////////////////////////////////////////////////
  71. //////////////////////////////////////////////////////////////////////
  72.  
  73.  
  74. void OnInputChanged(Fl_Widget* w, void* userdata) {
  75.     // check for NULL pointer
  76.     if (!view.input) return;
  77.  
  78.     // The value() method of Fl_Input will return a C-style
  79.     // string which we can use with a std::string
  80.     model.input = view.input->value();
  81. }
  82.  
  83.  
  84. void OnProcessClicked(Fl_Widget* w, void* userdata) {
  85.     if (!view.output) return;
  86.  
  87.     // This is where we can process the input in some way
  88.     std::string transformed;
  89.     TransformData(model.input, transformed);
  90.  
  91.     // Note 1: We call c_str() method from std::string to get
  92.     // a C style Null terminated string
  93.     // Note 2: the value(string) method of Fl_Output is used
  94.     // to set the current value
  95.     view.output->value(transformed.c_str());
  96. }
  97.  
  98.  
  99. void OnQuitClicked(Fl_Widget* w, void* userdata) {
  100.     // We expect userdata to be the Window
  101.     if (!userdata) return;
  102.  
  103.     // Cast the void* pointer back to a Fl_Window* pointer
  104.     Fl_Window* window = (Fl_Window*)userdata;
  105.  
  106.     // The hide() method will close the window
  107.     window->hide();
  108. }
  109.  
  110.  
  111. //////////////////////////////////////////////////////////////////////
  112. // F U N C T I O N S /////////////////////////////////////////////////
  113. //////////////////////////////////////////////////////////////////////
  114.  
  115.  
  116. // TransformData(input, output) simply adds " transformed" to the input
  117. // string
  118. void TransformData(const std::string& input, std::string& output) {
  119.     output = input + " transformed!";
  120. }
  121.  
  122.  
  123. Fl_Window* CreateWindow() {
  124.     Fl_Window* window = new Fl_Window(640, 480);
  125.     Fl_Box* box = new Fl_Box(20, 40, 600, 100, "Input/Output Example");
  126.     box->box(FL_UP_BOX);
  127.     box->labelfont(FL_BOLD + FL_ITALIC);
  128.     box->labelsize(36);
  129.     box->labeltype(FL_SHADOW_LABEL);
  130.  
  131.     // We use these to ensure adequate spacing of widgets
  132.     const int x = 100;
  133.     int y = 160;
  134.     const int w = 100;
  135.     const int h = 25;
  136.     const int Spacing = 10;
  137.  
  138.     view.input = new Fl_Input(x, y, 2 * w, h, "Input");
  139.     y += h + Spacing;
  140.     view.output = new Fl_Output(x, y, 2 * w, h, "Output");
  141.     y += h + Spacing;
  142.  
  143.     Fl_Button* processBtn = new Fl_Button(x, y, w, h, "Process");
  144.     y += h + Spacing;
  145.     Fl_Button* quitBtn = new Fl_Button(x, y, w, h, "Quit");
  146.  
  147.     // Register callbacks
  148.     view.input->callback(OnInputChanged, nullptr);
  149.     processBtn->callback(OnProcessClicked, nullptr);
  150.     quitBtn->callback(OnQuitClicked, (void*)window);
  151.  
  152.     window->end();
  153.     return window;
  154. }
  155.  
  156.  
  157. int main(int argc, char** argv) {
  158.     Fl_Window* window = CreateWindow();
  159.     window->show(argc, argv);
  160.     return Fl::run();
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement