Commit c6d5dc0d28b8d0295d20540c44a616a8e3411f5e

Authored by Perry Werneck
1 parent 3a197dd9

Implementing GAction wrapper for lib3270.

src/actions/abstract.c
@@ -30,20 +30,168 @@ @@ -30,20 +30,168 @@
30 #include "private.h" 30 #include "private.h"
31 31
32 static void pw3270_action_iface_init(GActionInterface *iface); 32 static void pw3270_action_iface_init(GActionInterface *iface);
33 - static void pw3270_action_class_init(pw3270ActionClass *klass);  
34 - static void pw3270_action_init(pw3270Action *action); 33 + static void pw3270Action_class_init(pw3270ActionClass *klass);
  34 + static void pw3270Action_init(pw3270Action *action);
  35 + static void pw3270_action_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
  36 + static void pw3270_action_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
35 37
36 - G_DEFINE_TYPE_WITH_CODE(pw3270Action, pw3270_action, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(G_TYPE_ACTION, pw3270_action_iface_init)) 38 + static const GVariantType * pw3270_action_get_state_type(GAction *action);
  39 + static GVariant * pw3270_action_get_state_property(GAction *action);
  40 + static GVariantType * pw3270_action_get_parameter_type(GAction *action);
  41 +
  42 +
  43 + enum {
  44 + PROP_NONE,
  45 + PROP_NAME,
  46 + PROP_PARAMETER_TYPE,
  47 + PROP_ENABLED,
  48 + PROP_STATE_TYPE,
  49 + PROP_STATE
  50 + };
  51 +
  52 + G_DEFINE_TYPE_WITH_CODE(pw3270Action, pw3270Action, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(G_TYPE_ACTION, pw3270_action_iface_init))
37 53
38 void pw3270_action_iface_init(GActionInterface *iface) { 54 void pw3270_action_iface_init(GActionInterface *iface) {
  55 + iface->get_name = pw3270_action_get_name;
  56 +// iface->get_parameter_type = pw3270_action_get_parameter_type;
  57 + iface->get_state_type = pw3270_action_get_state_type;
  58 +// iface->get_state_hint = pw3270_action_get_state_hint;
  59 + iface->get_enabled = pw3270_action_get_enabled;
  60 + iface->get_state = pw3270_action_get_state_property;
  61 +// iface->change_state = pw3270_action_change_state;
  62 + iface->activate = pw3270_action_activate;
  63 + }
39 64
  65 + static gboolean return_false(GAction G_GNUC_UNUSED(*action), GtkWidget G_GNUC_UNUSED(*window)) {
  66 + return FALSE;
40 } 67 }
41 68
42 - void pw3270_action_class_init(pw3270ActionClass *klass) { 69 + void pw3270Action_class_init(pw3270ActionClass *klass) {
  70 +
  71 + GObjectClass *object_class = G_OBJECT_CLASS(klass);
  72 +
  73 + object_class->set_property = pw3270_action_set_property;
  74 + object_class->get_property = pw3270_action_get_property;
  75 +
  76 + klass->get_enabled = return_false;
  77 +
  78 + // Install properties
  79 + g_object_class_install_property(object_class, PROP_NAME,
  80 + g_param_spec_string ("name",
  81 + N_("Action Name"),
  82 + N_("The name used to invoke the action"),
  83 + NULL,
  84 + G_PARAM_READWRITE |
  85 + G_PARAM_CONSTRUCT_ONLY |
  86 + G_PARAM_STATIC_STRINGS));
  87 +
  88 + g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
  89 + g_param_spec_boxed ("parameter-type",
  90 + N_("Parameter Type"),
  91 + N_("The type of GVariant passed to activate()"),
  92 + G_TYPE_VARIANT_TYPE,
  93 + G_PARAM_READWRITE |
  94 + G_PARAM_CONSTRUCT_ONLY |
  95 + G_PARAM_STATIC_STRINGS));
  96 +
  97 + g_object_class_install_property (object_class, PROP_ENABLED,
  98 + g_param_spec_boolean ("enabled",
  99 + N_("Enabled"),
  100 + N_("If the action can be activated"),
  101 + TRUE,
  102 + G_PARAM_READWRITE |
  103 + G_PARAM_STATIC_STRINGS));
  104 +
  105 + g_object_class_install_property (object_class, PROP_STATE_TYPE,
  106 + g_param_spec_boxed ("state-type",
  107 + N_("State Type"),
  108 + N_("The type of the state kept by the action"),
  109 + G_TYPE_VARIANT_TYPE,
  110 + G_PARAM_READABLE |
  111 + G_PARAM_STATIC_STRINGS));
  112 +
  113 + g_object_class_install_property (object_class, PROP_STATE,
  114 + g_param_spec_variant ("state",
  115 + N_("State"),
  116 + N_("The state the action is in"),
  117 + G_VARIANT_TYPE_ANY,
  118 + NULL,
  119 + G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
  120 + G_PARAM_STATIC_STRINGS));
  121 +
43 122
44 } 123 }
45 124
46 - void pw3270_action_init(pw3270Action *action) { 125 + void pw3270Action_init(pw3270Action *action) {
  126 +
  127 + action->name = "unnamed";
  128 + action->window = NULL;
47 129
48 } 130 }
49 131
  132 + void pw3270_action_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
  133 +
  134 + GAction *action = G_ACTION(object);
  135 +
  136 + switch (prop_id) {
  137 + case PROP_NAME:
  138 + g_value_set_string(value, pw3270_action_get_name(action));
  139 + break;
  140 +
  141 + case PROP_PARAMETER_TYPE:
  142 + g_value_set_boxed(value, pw3270_action_get_parameter_type(action));
  143 + break;
  144 +
  145 + case PROP_ENABLED:
  146 + g_value_set_boolean(value, pw3270_action_get_enabled(action));
  147 + break;
  148 +
  149 + case PROP_STATE_TYPE:
  150 + g_value_set_boxed(value, pw3270_action_get_state_type(action));
  151 + break;
  152 +
  153 + case PROP_STATE:
  154 + g_value_take_variant(value, pw3270_action_get_state_property(action));
  155 + break;
  156 +
  157 + default:
  158 + g_assert_not_reached ();
  159 + }
  160 +
  161 + }
  162 +
  163 + void pw3270_action_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) {
  164 + }
  165 +
  166 + const gchar * pw3270_action_get_name(GAction *action) {
  167 + return PW3270_ACTION(action)->name;
  168 + }
  169 +
  170 + const GVariantType * pw3270_action_get_state_type(GAction *action) {
  171 + return NULL;
  172 + }
  173 +
  174 + GVariant * pw3270_action_get_state_property(GAction *action) {
  175 + return g_variant_new_int16(0);
  176 + }
  177 +
  178 + GVariantType * pw3270_action_get_parameter_type(GAction *action) {
  179 + return NULL;
  180 + }
  181 +
  182 + gboolean pw3270_action_get_enabled(GAction *action) {
  183 + GtkWidget *window = PW3270_ACTION(action)->window;
  184 +
  185 + if(window)
  186 + return PW3270_ACTION_GET_CLASS(action)->get_enabled(action,window);
  187 +
  188 + return FALSE;
  189 + }
  190 +
  191 + void pw3270_action_activate(GAction *action, GVariant *parameter) {
  192 + GtkWidget *window = PW3270_ACTION(action)->window;
  193 +
  194 + if(window)
  195 + PW3270_ACTION_GET_CLASS(action)->activate(action,window);
  196 +
  197 + }
src/actions/actions.cbp
@@ -32,16 +32,20 @@ @@ -32,16 +32,20 @@
32 </Build> 32 </Build>
33 <Compiler> 33 <Compiler>
34 <Add option="-Wall" /> 34 <Add option="-Wall" />
35 - <Add option="`pkg-config --cflags gio-2.0 glib-2.0 lib3270 libv3270`" /> 35 + <Add option="`pkg-config --cflags gtk+-3.0 gio-2.0 glib-2.0 lib3270 libv3270`" />
36 <Add directory="../include" /> 36 <Add directory="../include" />
37 </Compiler> 37 </Compiler>
38 <Linker> 38 <Linker>
39 - <Add option="`pkg-config --libs gio-2.0 glib-2.0 lib3270 libv3270`" /> 39 + <Add option="`pkg-config --libs gtk+-3.0 gio-2.0 glib-2.0 lib3270 libv3270`" />
40 </Linker> 40 </Linker>
41 <Unit filename="../include/pw3270/actions.h" /> 41 <Unit filename="../include/pw3270/actions.h" />
  42 + <Unit filename="../include/pw3270/window.h" />
42 <Unit filename="abstract.c"> 43 <Unit filename="abstract.c">
43 <Option compilerVar="CC" /> 44 <Option compilerVar="CC" />
44 </Unit> 45 </Unit>
  46 + <Unit filename="lib3270.c">
  47 + <Option compilerVar="CC" />
  48 + </Unit>
45 <Unit filename="private.h" /> 49 <Unit filename="private.h" />
46 <Unit filename="testprogram/testprogram.c"> 50 <Unit filename="testprogram/testprogram.c">
47 <Option compilerVar="CC" /> 51 <Option compilerVar="CC" />
src/actions/lib3270.c 0 → 100644
@@ -0,0 +1,108 @@ @@ -0,0 +1,108 @@
  1 +/*
  2 + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
  3 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
  4 + * aplicativos mainframe. Registro no INPI sob o nome G3270.
  5 + *
  6 + * Copyright (C) <2008> <Banco do Brasil S.A.>
  7 + *
  8 + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
  9 + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
  10 + * Free Software Foundation.
  11 + *
  12 + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
  13 + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
  14 + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
  15 + * obter mais detalhes.
  16 + *
  17 + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
  18 + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
  19 + * St, Fifth Floor, Boston, MA 02110-1301 USA
  20 + *
  21 + * Este programa está nomeado como - e possui - linhas de código.
  22 + *
  23 + * Contatos:
  24 + *
  25 + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
  26 + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
  27 + *
  28 + */
  29 +
  30 + /**
  31 + * @brief Implement GAction "wrapper" for lib3270's actions.
  32 + *
  33 + */
  34 +
  35 + #include "private.h"
  36 + #include <pw3270/window.h>
  37 +
  38 + #define PW3270_TYPE_LIB3270_ACTION (Lib3270Action_get_type())
  39 + #define PW3270_LIB3270_ACTION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), PW3270_TYPE_LIB3270_ACTION, Lib3270Action))
  40 + #define PW3270_IS_LIB3270_ACTION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), PW3270_TYPE_LIB3270_ACTION))
  41 +
  42 + typedef struct _Lib3270ActionClass {
  43 + pw3270ActionClass parent_class;
  44 +
  45 + } Lib3270ActionClass;
  46 +
  47 + typedef struct _Lib3270Action {
  48 + pw3270Action parent;
  49 +
  50 + const LIB3270_ACTION * definition;
  51 +
  52 +
  53 + } Lib3270Action;
  54 +
  55 + static void Lib3270Action_class_init(Lib3270ActionClass *klass);
  56 + static void Lib3270Action_init(Lib3270Action *action);
  57 +
  58 +
  59 + G_DEFINE_TYPE(Lib3270Action, Lib3270Action, PW3270_TYPE_ACTION);
  60 +
  61 + static gboolean action_enabled(GAction *action, GtkWidget *window) {
  62 +
  63 + H3270 * hSession = pw3270_window_get_session_handle(window);
  64 +
  65 + if(hSession)
  66 + return PW3270_LIB3270_ACTION(action)->definition->activatable(hSession) > 0 ? TRUE : FALSE;
  67 +
  68 + return FALSE;
  69 + }
  70 +
  71 + static void action_activate(GAction *action, GtkWidget *window) {
  72 +
  73 + H3270 * hSession = pw3270_window_get_session_handle(window);
  74 +
  75 + if(hSession)
  76 + PW3270_LIB3270_ACTION(action)->definition->activate(hSession);
  77 +
  78 + }
  79 +
  80 + void Lib3270Action_class_init(Lib3270ActionClass *klass) {
  81 +
  82 + pw3270ActionClass * action = PW3270_ACTION_CLASS(klass);
  83 +
  84 + action->get_enabled = action_enabled;
  85 + action->activate = action_activate;
  86 +
  87 +
  88 + }
  89 +
  90 + void Lib3270Action_init(Lib3270Action *action) {
  91 + }
  92 +
  93 + GAction * pw3270_action_get_from_lib3270(const LIB3270_ACTION * definition) {
  94 +
  95 + Lib3270Action * action = (Lib3270Action *) g_object_new(PW3270_TYPE_LIB3270_ACTION, NULL);
  96 + action->definition = definition;
  97 +
  98 + {
  99 + pw3270Action * abstract = PW3270_ACTION(action);
  100 +
  101 + abstract->name = definition->name;
  102 +
  103 + }
  104 +
  105 + return G_ACTION(action);
  106 + }
  107 +
  108 +
src/actions/private.h
@@ -39,19 +39,24 @@ @@ -39,19 +39,24 @@
39 39
40 #include <libintl.h> 40 #include <libintl.h>
41 #include <glib/gi18n.h> 41 #include <glib/gi18n.h>
  42 + #include <gtk/gtk.h>
42 43
43 #include <pw3270/actions.h> 44 #include <pw3270/actions.h>
44 #include <lib3270/log.h> 45 #include <lib3270/log.h>
45 46
46 struct _pw3270Action { 47 struct _pw3270Action {
47 - GObject parent; 48 + GObject parent;
  49 +
  50 + GtkWidget * window;
  51 + const gchar * name;
48 52
49 }; 53 };
50 54
51 struct _pw3270ActionClass { 55 struct _pw3270ActionClass {
  56 + GObjectClass parent_class;
52 57
53 - GObject parent_class;  
54 - 58 + gboolean (*get_enabled)(GAction *action, GtkWidget *window);
  59 + void (*activate)(GAction *action, GtkWidget *window);
55 60
56 }; 61 };
57 62
src/actions/testprogram/testprogram.c
@@ -30,14 +30,29 @@ @@ -30,14 +30,29 @@
30 30
31 #include <config.h> 31 #include <config.h>
32 #include <pw3270/actions.h> 32 #include <pw3270/actions.h>
  33 + #include <pw3270/window.h>
  34 + #include <lib3270.h>
33 #include <lib3270/log.h> 35 #include <lib3270/log.h>
34 36
35 /*---[ Implement ]----------------------------------------------------------------------------------*/ 37 /*---[ Implement ]----------------------------------------------------------------------------------*/
36 38
  39 + GtkWidget * pw3270_window_get_terminal_widget(GtkWidget G_GNUC_UNUSED(*window)) {
  40 + return NULL;
  41 + }
  42 +
  43 + H3270 * pw3270_window_get_session_handle(GtkWidget G_GNUC_UNUSED(*window)) {
  44 + return NULL;
  45 + }
  46 +
37 int main (int argc, char **argv) { 47 int main (int argc, char **argv) {
38 48
  49 + GAction * action = pw3270_action_get_from_lib3270(lib3270_action_get_by_name("testpattern"));
  50 +
  51 + g_message("Action name is \"%s\"",g_action_get_name(action));
  52 + g_message("Action is %s",g_action_get_enabled(action) ? "enabled" : "disabled");
39 53
40 - return 0; 54 + g_object_unref(action);
  55 + return 0;
41 56
42 } 57 }
43 58
src/include/pw3270/actions.h
@@ -42,15 +42,22 @@ @@ -42,15 +42,22 @@
42 42
43 G_BEGIN_DECLS 43 G_BEGIN_DECLS
44 44
45 - #define PW3270_TYPE_ACTION (pw3270_action_get_type ())  
46 -  
47 -// #define PW3270_ACTION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), PW3270_TYPE_ACTION, pw3270Action))  
48 -// #define PW3270_IS_ACTION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), PW3270_TYPE_ACTION)) 45 + #define PW3270_TYPE_ACTION (pw3270Action_get_type())
  46 + #define PW3270_ACTION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), PW3270_TYPE_ACTION, pw3270Action))
  47 + #define PW3270_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PW3270_TYPE_ACTION, pw3270ActionClass))
  48 + #define PW3270_IS_ACTION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), PW3270_TYPE_ACTION))
  49 + #define PW3270_IS_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PW3270_TYPE_ACTION))
  50 + #define PW3270_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PW3270_TYPE_ACTION, pw3270ActionClass))
49 51
50 typedef struct _pw3270Action pw3270Action; 52 typedef struct _pw3270Action pw3270Action;
51 typedef struct _pw3270ActionClass pw3270ActionClass; 53 typedef struct _pw3270ActionClass pw3270ActionClass;
52 54
53 - GType pw3270_action_get_type(void) G_GNUC_CONST; 55 + GType pw3270Action_get_type(void) G_GNUC_CONST;
  56 +
  57 + GAction * pw3270_action_get_from_lib3270(const LIB3270_ACTION * definition);
  58 + const gchar * pw3270_action_get_name(GAction *action);
  59 + gboolean pw3270_action_get_enabled(GAction *action);
  60 + void pw3270_action_activate(GAction *action, GVariant *parameter);
54 61
55 G_END_DECLS 62 G_END_DECLS
56 63
src/include/pw3270/window.h
@@ -37,6 +37,7 @@ @@ -37,6 +37,7 @@
37 #define PW3270_WINDOW_H_INCLUDED 37 #define PW3270_WINDOW_H_INCLUDED
38 38
39 #include <gtk/gtk.h> 39 #include <gtk/gtk.h>
  40 + #include <lib3270.h>
40 41
41 G_BEGIN_DECLS 42 G_BEGIN_DECLS
42 43
@@ -62,6 +63,11 @@ @@ -62,6 +63,11 @@
62 /// @brief Create a new terminal tab. 63 /// @brief Create a new terminal tab.
63 GtkWidget * pw3270_terminal_new(GtkWidget *window); 64 GtkWidget * pw3270_terminal_new(GtkWidget *window);
64 65
  66 + /// @brief Get the active terminal widget.
  67 + GtkWidget * pw3270_window_get_terminal_widget(GtkWidget *window);
  68 +
  69 + /// @brief Get the active session handle.
  70 + H3270 * pw3270_window_get_session_handle(GtkWidget *window);
65 71
66 G_END_DECLS 72 G_END_DECLS
67 73