How to fix the Unbound module Graphics in an ocaml project

Image
From ~/pr/gitl/ocaml-gol In a constant effort to learn new programming languages, I'm currently trying to use ocaml , a free and open-source general-purpose, multi-paradigm programming language maintained at the Inria . It's basically an extension of Caml with object-oriented features. I'm mostly interested by its functionnal and pattern matching features but the module part of the language can be a bit difficult to understand for someone with little to none ML (Meta Language) background.   The error When trying to use the graphics module to create a graphical window and go just a little further than the simplest helloworld program, here is the result : If the project uses dune : (executable (name ocaml_project) (libraries lwt.unix graphics) ) with this code : let () = Printf.printf "Hello, world!\n";; Lwt_io.printf "Hello, world!\n";; Graphics.open_graph " 800x600";; The first times I built this project running the du

Using the fox toolkit FileDialog static public functions

This trick comes from a brand-new project I hosted at github : elecrud, more specifically the MainWindow::onFileOpen() method you can find in the src/editor/MainWindow.cpp file.


 

The dialog class

The FXFileDialog class reference can be found online here for the 1.6 version of the fox toolkit library.

So, obviously, you could instantiate FXFileDialog but the simplest way to use it is to use Static Public Member Functions, for example getSaveFilename() or getOpenFilename() :

FXString filename = FXFileDialog::getOpenFilename(this, 
                               "Open a project...", "~");
// use filename here

The owner class

The only limitation is that you must have a valid owner as first parameter. If you try nullptr you may have a silly segmentation fault. The simplest solution is to call it from a class that inherits a Fox class, for example a MainWindow :

class MainWindow : public FXMainWindow
{
  // Macro for class hierarchy declarations
  FXDECLARE(MainWindow)

public:
  // MainWindow's constructor
  MainWindow(FXApp* a, const FX::FXString& windowTitle);

  // Messages for our class
  enum{
    ID_CANVAS=FXMainWindow::ID_LAST,
    ID_CLEAR,

    ID_LAST,
    };

  // Initialize
  virtual void create();
  virtual ~MainWindow();
 
protected:
  MainWindow();
};

The main() function

You will also need a valid application object :

int main(int argc,char *argv[]){

  FXApp application("Scribble","FoxTest");
  application.init(argc,argv);
  new MainWindow(&application);

  // Create the application's windows and run app
  application.create();
  return application.run();
  }

Conclusion

Even if the public static functions are easier to use for the most common cases, a subclass can offer more customisation. Choose your fate.

Comments

Popular posts from this blog

How to make a map of variant in C++