guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
Public Member Functions | Static Public Member Functions | List of all members
guit::GApp Class Reference

Application Context (must be created first). More...

Public Member Functions

 GApp (int argc, const char **argv)
 Inits the application context; must be called before creating any other object.
 
int start (GWindow &main_window)
 Starts the event loop This function is blocking and must be called last.
 

Static Public Member Functions

static int status ()
 Returns the value given as an argument to exit().
 
static bool hasArg (GString const &name)
 Searches if one of the arguments of the command line is name. More...
 
static bool hasArg (GString const &name, GString &value, GString const &assign="=")
 Searches if one of the arguments of the command line is name or name=value. More...
 
static GDim screenSize ()
 Returns the size of the main screen.
 
static GWindowmainWindow ()
 Returns the Main Window. More...
 
static GadgetcurrentFocus ()
 Returns the gadget having the focus (null otherwise).
 
static void updateAll ()
 Requests all gadgets to be repainted. More...
 
static void paintAll ()
 Immediately repaints all gadgets. More...
 
static void exec (GFunction const &fun)
 Executes a function in the main thread. More...
 
static void sleep (GTime milliseconds)
 Makes the thread where this function is called to sleep for the specified value (in milliseconds). More...
 
static void error (GString const &where, GString const &message, GObject const *={})
 Prints an error message. More...
 
static void onError (std::function< void(GString const &where, GString const &message, GObject const *object)> fun)
 Changes the function that manages errors. More...
 
static void onException (std::function< void(GString const &type, GString const &message, bool isGFatalError)> fun)
 Changes the function that manages exceptions. More...
 
static class GInspectorinspector ()
 Allows inspecting gadgets. More...
 
static void showInspector ()
 Shows gadget inspector.
 
static void close ()
 Closes or quits the application. More...
 
static GStrings const & resourceDirs ()
 Directories where files (CSS, icons, etc.) are searched by default.
 
static GString const & guitDir ()
 Returns the user's Guit directory. More...
 
static GString const & appDir ()
 Returns guitDir()/execName() The ! magic character refers to this directory. More...
 
static GString execName ()
 Returns the name and the path of the executable. More...
 
static int argc ()
 Returns the arguments of the command line. More...
 
static bool addCssFile (GString const &css_file)
 Adds CSS.
 

Detailed Description

Application Context (must be created first).

The app context must be created before creating any Guit object, as in the example below:

#include <guit.hpp>
using namespace guit;
int main(int argc, const char **argv) {
GApp app(argc, argv);
app.inspector();
// loads CSS rules
app
<< "#myapp {margin: 15 15}"
<< ".mypanel {border: rounded; font: bold 16 arial,sans-serif}";
// loads CSS files
app << "myapp.css" << "whatever.css";
// starts the event loop, the window given as an argument (here an
// instance of a custom MyWindow class) become the 'main window'
return start(new MyWindow);
}

CSSs should be loaded before creating the gadgets.

Files (CSSs, icons, etc.) that do not start with / \ . and magic characters ~ : ! are searched in the application resource directory (MacOSX only), then in guit-dir/exec-name where guit-dir is the Guit directory of the user, and exec-name the name of the executable. guit-dir is located here:

Magic characters must be the first character of a file name:

start() starts the event loop and blocks until the application is terminated. start() returns a status which is usually 0 (see quit() and exit()). Its argument must derive from GWindow and should be created in the heap (i.e. using new). This window will be the main window of the application. By default, the application is terminated when this window is closed. This can be changed by adding a GQuitDialog to it or on::done callbacks (see GWindow).

Example:

class MyWindow : public GWindow {
public:
MyWindow() : GWindow("#myapp My Nice Window")
{
auto& panel = VBox(".mypanel")
<< TextField("initial text")
<< Button("Click Me")
<< ScrollBar("50");
// add gadgets to the window
*this
<< QuitDialog()
<< panel
<< ...;
}
};

A CSS class (.mypanel) and/or ID (#mywin) can be given as an argument to the gadget constructors. They can be followed by text that will be the title or value of the gadget (eg the title of a GWindow/GBox, the text of a GButton/GItem/GTextField, the value of a GSlider/GChartItem)

VBox(), TextField(), Button(), etc. are shortcut functions that instantiate the corresponding class. For instance, Button("Click me") is equivalent to: * new GButton("Click Me"). Note that classes start with a 'G', contrary to shortcut functions.

Guit relies on TrueType fonts. A font list cache is created in the users' Guit directory the first type a Guit application is launched. It should be removed when new fonts are added to the system, so that it can be built again.

Member Function Documentation

void guit::GApp::close ( )
static

Closes or quits the application.

  • close() opens a GQuitDialog if one was added to the main window, otherwise it calls quit()
  • quit() and exit() quit the application unconditionally
  • start() or status() will return the value given as an argument to exit() (0 if quit() is called)
    See Also
    GWIndow.
GString const & guit::GApp::guitDir ( )
static

Returns the user's Guit directory.

Returns:

  • ~/Library/Guit on MacOS
  • ~/Guit on Windows
  • ~/.guit on Linux (note the dot)
GString const & guit::GApp::appDir ( )
static

Returns guitDir()/execName() The ! magic character refers to this directory.

GString guit::GApp::execName ( )
static

Returns the name and the path of the executable.

execPath() returns argv[0] and execName() its basename().

int guit::GApp::argc ( )
static

Returns the arguments of the command line.

  • argc() returns the number of arguments
  • argv(n) returns the n-th argument (empty string if n is out of range).
bool guit::GApp::hasArg ( GString const &  name)
static

Searches if one of the arguments of the command line is name.

Note that arvg(0) is ignored.

bool guit::GApp::hasArg ( GString const &  name,
GString &  value,
GString const &  assign = "=" 
)
static

Searches if one of the arguments of the command line is name or name=value.

  • returns the associated value (empty string if no value)
  • assign specifies the assignement sign(s), eg: if assign is ":" then : will be searched instead of = moreover both : and = will be searched is assign is "=:"
  • note that arvg(0) is ignored.
GWindow * guit::GApp::mainWindow ( )
static

Returns the Main Window.

The main window is the argument that was provided to start().

void guit::GApp::updateAll ( )
static

Requests all gadgets to be repainted.

repaintAll().

void guit::GApp::paintAll ( )
static

Immediately repaints all gadgets.

updateAll() .

void guit::GApp::exec ( GFunction const &  fun)
static

Executes a function in the main thread.

calls gexec().

Warning
Functions that modify/update the GUI must always be called in the main thread.
void guit::GApp::sleep ( GTime  milliseconds)
static

Makes the thread where this function is called to sleep for the specified value (in milliseconds).

calls gsleep().

void guit::GApp::error ( GString const &  where,
GString const &  message,
GObject const *  obj = {} 
)
static

Prints an error message.

calls gerror(). By default, errors are printed on std::cerr. onError() changes how errors are managed.

void guit::GApp::onError ( std::function< void(GString const &where, GString const &message, GObject const *object)>  fun)
static

Changes the function that manages errors.

  • If fun is not null, it will be called whenever gerror() or GApp::error() is called.
  • if fun is null, errors are printed out on std::cerr (default behavior).
    Note
    if object is not null this address is written in the error message.
void guit::GApp::onException ( std::function< void(GString const &type, GString const &message, bool isGFatalError)>  fun)
static

Changes the function that manages exceptions.

  • If fun is not null, it will be called whenever an exception is thrown.
  • if fun is null, a dialog box is opened to ask the user if she wants to quit or to continue. This feature may allow saving important data before quiting the program, but there is no guarantee it will work, especially when the exception was not thrown by the toolkit.
GInspector & guit::GApp::inspector ( )
static

Allows inspecting gadgets.

A Meta-click or a Cmd-click (depending on the OS) on a gedget will open the inspector.


The documentation for this class was generated from the following files: