1 #include "Command.h"
2 
Command(const Callback & callback)3 Command::Command (const Callback& callback) :
4 	_callback(callback)
5 {
6 }
7 
8 // Invoke the registered callback
execute()9 void Command::execute ()
10 {
11 	if (_enabled) {
12 		_callback();
13 	}
14 }
15 
16 // Override the derived keyDown method
keyDown()17 void Command::keyDown ()
18 {
19 	// Execute the command on key down event
20 	execute();
21 }
22 
23 // Connect the given menuitem or toolbutton to this Command
connectWidget(GtkWidget * widget)24 void Command::connectWidget (GtkWidget* widget)
25 {
26 	if (GTK_IS_MENU_ITEM(widget)) {
27 		// Connect the static callback function and pass the pointer to this class
28 		g_signal_connect(G_OBJECT(widget), "activate", G_CALLBACK(onMenuItemClicked), this);
29 	} else if (GTK_IS_TOOL_BUTTON(widget)) {
30 		// Connect the static callback function and pass the pointer to this class
31 		g_signal_connect(G_OBJECT(widget), "clicked", G_CALLBACK(onToolButtonPress), this);
32 	} else if (GTK_IS_BUTTON(widget)) {
33 		// Connect the static callback function and pass the pointer to this class
34 		g_signal_connect(G_OBJECT(widget), "clicked", G_CALLBACK(onButtonPress), this);
35 	}
36 }
37 
onButtonPress(GtkButton * button,gpointer data)38 gboolean Command::onButtonPress (GtkButton* button, gpointer data)
39 {
40 	// Retrieve the right Command object from the user <data>
41 	Command* self = reinterpret_cast<Command*> (data);
42 
43 	// Execute the command
44 	self->execute();
45 
46 	return true;
47 }
48 
onToolButtonPress(GtkToolButton * toolButton,gpointer data)49 gboolean Command::onToolButtonPress (GtkToolButton* toolButton, gpointer data)
50 {
51 	// Retrieve the right Command object from the user <data>
52 	Command* self = reinterpret_cast<Command*> (data);
53 
54 	// Execute the command
55 	self->execute();
56 
57 	return true;
58 }
59 
onMenuItemClicked(GtkMenuItem * menuitem,gpointer data)60 gboolean Command::onMenuItemClicked (GtkMenuItem* menuitem, gpointer data)
61 {
62 	// Retrieve the right Command object from the user <data>
63 	Command* self = reinterpret_cast<Command*> (data);
64 
65 	// Execute the command
66 	self->execute();
67 
68 	return true;
69 }
70