Example

Example application showing to use of the fileselector widget in C++ This program is a hush Tcl interpreter extending Tcl with a fileselector command.
  #include <iostream.h>           // for C++ stream I/O
  #include <hush/kit.h>           // for kit (tk) class
  #include <hush/session.h>       // for session class
  #include "fileselector.h"       // for new fileselector widget
  

Application class

This class will model our small example application:
  class application : public session {
    public:
      application(int argc, char* argv[]) : session(argc,argv, "example") {}
      void prelude();  // do inits here 
      int main();      // main setup here 
  };

  

Prelude

Introduce "fileselector" as a new Tcl command. Note: the prelude is a good place to do this kind of initializations. since it is always exectuted before any command line supplied scripts. This means that we can use our new command in such scripts as well! See also the example script: script.tcl.

Note the use of _register() to register the new handler for deletion. See Garbage collection for more information on _register.

  void application::prelude() {
      fileselector* f = new fileselector; _register(f);
      tk->bind(f->type(), f);     // Create script command 
  }

  

Main

We will create a fileselector widget, just to see how this works in C++. The title() member is inherited from the toplevel class. We will not list the current working directory, but the users home directory (~) by using the dirpath() member.

Note: the main routine will not be called if a script is supplied on the command line. So the stuff below does not appear if we use this program as an interpreter.

  int application::main() {
      widget* root = tk->root();
      fileselector* f = new fileselector(root, "just_an_example");
      f->title("Select a file");          
      f->dirpath("~");                    // cd ~ (HOME)
      cout << "Wait until file selected." << endl;
      const char* filename =  f->get();
      if (filename) 
          cout << "You selected file: " << filename << endl;
      else
          cout << "You canceled fileselection." <<endl;

      return OK;
  }

  

C main routine

This the good old C main function which starts up the whole program. The compiler will generate code which will call this function parameterized with the command line arguments.
  int main(int argc, char* argv[]) {
      application* a = new application(argc,argv);
      int result = a->run();
      delete a;
      session::statistics();
      return result;
  }