DUECA/DUSIME
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
dueca::GtkGladeWindow Class Reference

A GUI window directly from a glade interface file. More...

#include <GtkGladeWindow.hxx>

Classes

struct  OptionMapping
 Struct for mapping enum name to representation string. More...
 
struct  OptionMappings
 Struct for describing mappings. More...
 

Public Member Functions

 GtkGladeWindow ()
 Constructor.
 
 ~GtkGladeWindow ()
 Destructor.
 
bool readGladeFile (const char *file, const char *mainwidget, gpointer client=NULL, const GladeCallbackTable *table=reinterpret_cast< GladeCallbackTable * >(NULL), bool connect_signals=false, bool warn=true)
 Initialization of the glade window.
 
void connectCallbacks (gpointer client, const GladeCallbackTable *table, bool warn=true)
 Connect callbacks to widgets.
 
void connectCallbacksAfter (gpointer client, const GladeCallbackTable *table, bool warn=true)
 Connect callbacks to widgets.
 
void connectCallbackSymbols (gpointer user_data=NULL)
 Connect GObject in-code callbacks, note that this is effective only once, and called when connect_signals=true in the readGladeFile function.
 
GtkWidgetoperator[] (const char *wname)
 Access the widgets in this interface.
 
GObjectgetObject (const char *name)
 Access anything in the interface file as objects.
 
void show (const char *widget=NULL)
 Open the window.
 
void hide (const char *widget=NULL)
 Close the window.
 
bool fillOptions (const char *dcoclass, const char *format, const char *arrformat=NULL, const OptionMappings *mapping=NULL, bool warn=false)
 Use the enum items in a DCO object to fill combobox tree models in the interface.
 
unsigned setValues (CommObjectReader &dco, const char *format, const char *arrformat=NULL, bool warn=false)
 Use a DCO object to set the state of the interface.
 
unsigned getValues (CommObjectWriter &dco, const char *format, const char *arrformat=NULL, bool warn=false)
 Find the current state of the interface and push into a DCO object.
 
template<typename T >
bool loadComboText (const char *name, const T &values)
 Initialize a text combobox with a given list or array of values.
 
void setWindow (const std::vector< int > &p)
 Change position and size of the window.
 

Detailed Description

A GUI window directly from a glade interface file.

Supply the interface file name, and a table of pointers to callback functions. As an example:

class MyGui
{
// may be a member, may derive from it
public:
// constructor
MyGui();
// callback function
};
// button press method of mygui:
MyGui::buttonPress(GtkWidget *w, gpointer gp)
{
std::cout << "button pressed" << endl;
}
// table with callbacks
static GladeCallbackTable table[] =
{
// links the button, with signal pressed, to ButtonPress
{ "mybutton", "pressed", gtk_callback(&MyGui::buttonPress),
an_optional_gpointer_variable },
{ "otherbutton", "released", gtk_callback(&MyGui::buttonPress2) },
// close off in familiar style
{ NULL, NULL, NULL, NULL }
}
// constructor, opens window and passes table, so callback is connected
MyGui::MyGui() :
mywindow()
{
// supply file and open window
mywindow.readGladeFile("mywindow.glade", "thewindow",
reinterpret_cast<gpointer>(this),
table);
mywindow.show();
}
GtkCaller * gtk_callback(RET(T::*call)(P1, gpointer))
creation of a caller, 1 parameter and the gpointer parameter.
Definition GtkGladeWindow.hxx:46
A GUI window directly from a glade interface file.
Definition GtkGladeWindow.hxx:268
This is a "light weight object" that enables you to read the latest set of data on a StreamChannelEnd...
Definition StreamReaderLatest.hxx:43

Check the signatures for the callback functions in the Gtk3 documentation. Stick to the signature generated by glade, since deviating from that signature may result in all kinds of unclean mess.

The last argument of the callback function, the gpointer argument, is normally a pointer to user data that one can add to the signal connection in gtk. However, the callback system used by GtkGladeWindow already uses that gpointer. You can add a new value by specifying it in the last column of the GladeCallbackTable.

It is also possible to quickly link and load DCO objects to elements in your interface. Take the following dco object as an example:

(Type float)
(Enum CmdType On Off)
(Object TestObject
(float a (Default 0.1f))
(CmdType command (Default Off))
)

If you now ensure that the widgets holding this data (for the float a TextEntry, an Adjustment, a SpinButton or a Range, for the enum a ComboBox) are properly named, the GtkGladeWindow can:

As an example, for a glade window with a SpinButton named "mywidgets_a" and a ComboBox named "mywidgets_command", the following code should work:

// define a mapping between the enum values, and interface strings
// note that whithout mappings (use NULL), the enum values are used
// directly in the interface
// each enum gets a mapping to label strings
{ "On", "Device on" },
{ "Off", "Device off" },
{ NULL, NULL }
};
// all mappings together, linked to the member name
static const OptionMappings mappings[] = {
{ "command", mapping_command },
{ NULL, NULL }
};
// apply the mappings to the opened window
mywindow.fillOptions("TestObject", "mywidgets_%s", NULL,
mappings, true);
// set the default values of a TestObject on the interface
TestObject deflt;
CommObjectReader reader("TestObject", reinterpret_cast<void*>(&deflt));
mywindow.setValues(reader, "mywidgets_%s", NULL, true);
// .... later, after changes, read the values from the interface
// send over a channel if applicable
{
// a DCOWriter is also a CommObjectWriter
DCOWriter writer(w_mytoken, ts);
mywindow.getValues(writer, "mywidgets_%s", NULL, true);
}
Struct for mapping enum name to representation string.
Definition GtkGladeWindow.hxx:418

You can also use (fixed-size) arrays with values in the DCO objects, and have the interface fill these; the second format string "arrformat", describes how these are labeled.

In your glade gui, ensure that:

Constructor & Destructor Documentation

◆ ~GtkGladeWindow()

dueca::GtkGladeWindow::~GtkGladeWindow ( )

Destructor.

Also closes the window if not already closed.

Member Function Documentation

◆ readGladeFile()

bool dueca::GtkGladeWindow::readGladeFile ( const char * file,
const char * mainwidget,
gpointer client = NULL,
const GladeCallbackTable * table = reinterpret_castGladeCallbackTable * >(NULL),
bool connect_signals = false,
bool warn = true )

Initialization of the glade window.

Loads the interface code from the file and creates the main widget. Connects callbacks given in the table, and optionally connects callback signals to symbols found in the application's symbol table.

Parameters
fileName of the glade interface file
mainwidgetTop-level widget (normally a window) in that file that will be opened.
clientPointer to the class that will be receiving the callbacks.
tableTable linking widget, signal, callback function and optionally the pointer argument to the callback function.
connect_signalsConnect gobject callback signals. The user_data argument to callback functions is obtained from the "client" argument.
warnWarn when widgets in the callback table are not found in the interface.
Returns
true if all OK.

◆ connectCallbacks()

void dueca::GtkGladeWindow::connectCallbacks ( gpointer client,
const GladeCallbackTable * table,
bool warn = true )

Connect callbacks to widgets.

You can repeatedly use this function, adding more callbacks to the widgets.

Parameters
clientPointer to the class that will be receiving the callbacks.
tableTable linking widget, signal, callback function and optionally the pointer argument to the callback function.
warnWarn when widgets in the callback table are not found in the interface.

◆ connectCallbacksAfter()

void dueca::GtkGladeWindow::connectCallbacksAfter ( gpointer client,
const GladeCallbackTable * table,
bool warn = true )

Connect callbacks to widgets.

Callbacks will be added as last. You can repeatedly use this function, adding more callbacks to the widgets.

Parameters
clientPointer to the class that will be receiving the callbacks.
tableTable linking widget, signal, callback function and optionally the pointer argument to the callback function.
warnWarn when widgets in the callback table are not found in the interface.

◆ operator[]()

GtkWidget * dueca::GtkGladeWindow::operator[] ( const char * wname)

Access the widgets in this interface.

Most objects in the interface will be widgets; note that for GtkListStore and GtkTreeStore objects you need getObject().

Parameters
wnameWidget name.
Returns
A widget object, NULL when the object is not found.

◆ getObject()

GObject * dueca::GtkGladeWindow::getObject ( const char * name)

Access anything in the interface file as objects.

Parameters
nameObject name.
Returns
A GObject pointer, NULL when the object is not found.

◆ show()

void dueca::GtkGladeWindow::show ( const char * widget = NULL)

Open the window.

By default, this uses the main window/widget.

Parameters
widgetOptional, widget name to show other widget or window

◆ hide()

void dueca::GtkGladeWindow::hide ( const char * widget = NULL)

Close the window.

By default, this uses the main window/widget.

Parameters
widgetOptional, widget name to hide other widget or window.

◆ fillOptions()

bool dueca::GtkGladeWindow::fillOptions ( const char * dcoclass,
const char * format,
const char * arrformat = NULL,
const OptionMappings * mapping = NULL,
bool warn = false )

Use the enum items in a DCO object to fill combobox tree models in the interface.

Parameters
dcoclassObject class
formatFormat string, to be written with sprintf, use "%s" to insert the element name, e.g., "mywidgets_%s"
arrformatFormat string to be used when connecting to an array element, e.g., "mywidgets_%s_%02d"
mappingOptional mapping table, defining sets of member name + enum string, to representation, NULL-terminated.
warnIf true, warn for DCO members that are not matched in the interface ID's.

◆ setValues()

unsigned dueca::GtkGladeWindow::setValues ( CommObjectReader & dco,
const char * format,
const char * arrformat = NULL,
bool warn = false )

Use a DCO object to set the state of the interface.

Parses the DCO object, create an ID based on the name and possibly an element number and tries to find matching elements in the interface based on the format, then pushes the DCO values in the corresponding widgets, if possible.

Parameters
dcoReader object
formatFormat string, to be written with sprintf, use "%s" to insert the element name, e.g., "mywidgets_%s"
arrformatFormat string to be used when connecting to an array element, e.g. "mywidgets_%s_%02d"
warnWarn if either an element is not found, or widget and datatype do not match.
Returns
The number of successfully set values

◆ getValues()

unsigned dueca::GtkGladeWindow::getValues ( CommObjectWriter & dco,
const char * format,
const char * arrformat = NULL,
bool warn = false )

Find the current state of the interface and push into a DCO object.

Parses the DCO object, create an ID based on the name and possibly an element number and tries to find matching elements in the interface based on the format, then reads the interface state and sets the values in the DCO.

Parameters
dcoWriter object
formatFormat string, to be written with sprintf, use "%s" to insert the element name, e.g., "mywidgets_%s"
arrformatFormat string to be used when connecting to an array element, e.g. "mywidgets_%s_%02d"
warnWarn if either an element is not found, or widget and datatype do not match.
Returns
The number of successfully read values

◆ loadComboText()

template<typename T >
bool dueca::GtkGladeWindow::loadComboText ( const char * name,
const T & values )

Initialize a text combobox with a given list or array of values.

Parameters
nameName of the combo box.
valuesIterable (list, vector, etc.) of std::string (or string-like) objects, these need a "c_str()" method.

◆ setWindow()

void dueca::GtkGladeWindow::setWindow ( const std::vector< int > & p)
inline

Change position and size of the window.

Parameters
pVector with offset and size elements, p[0] offset x. p[1] offset y, if either < 0, position hints are ignored. p[2] width, p[3] height, if either <= 0, size hints are ignored. Vector must have 2 or 4 elements.

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