Commit 63a2fe4d4034a0cb85b46512863b9d7af9cd466f
1 parent
3585c67f
Exists in
master
and in
1 other branch
Loading accelerators from keyfile.
Fixing memory leaks.
Showing
7 changed files
with
127 additions
and
6 deletions
Show diff stats
src/include/v3270/actions.h
| ... | ... | @@ -85,6 +85,7 @@ |
| 85 | 85 | LIB3270_EXPORT void v3270_accelerator_map_reset(GtkWidget *widget); |
| 86 | 86 | LIB3270_EXPORT void v3270_accelerator_map_foreach(GtkWidget *widget,void (*call)(const V3270Accelerator * accel, const char *keys, gpointer ptr), gpointer ptr); |
| 87 | 87 | LIB3270_EXPORT void v3270_accelerator_map_to_key_file(GtkWidget *widget, GKeyFile *key_file, const gchar *group_name); |
| 88 | + LIB3270_EXPORT gboolean v3270_accelerator_map_load_key_file(GtkWidget *widget, GKeyFile *key_file, const gchar *group_name); | |
| 88 | 89 | LIB3270_EXPORT V3270Accelerator * v3270_accelerator_map_add_entry(GtkWidget *widget, const gchar *name, guint accel_key, GdkModifierType accel_mods, GCallback callback, gpointer data); |
| 89 | 90 | |
| 90 | 91 | LIB3270_EXPORT const V3270Accelerator * v3270_get_accelerator(GtkWidget *widget, guint keyval, GdkModifierType state); | ... | ... |
src/terminal/keyboard/accelerator.c
| ... | ... | @@ -43,6 +43,26 @@ |
| 43 | 43 | return a->arg - b->arg; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | + V3270Accelerator * v3270_accelerator_copy(const V3270Accelerator *accel) | |
| 47 | + { | |
| 48 | + V3270Accelerator * rc = NULL; | |
| 49 | + | |
| 50 | + if(accel->type == V3270_ACCELERATOR_TYPE_CUSTOM) | |
| 51 | + { | |
| 52 | + V3270CustomAccelerator * customAccel = g_new0(V3270CustomAccelerator,1); | |
| 53 | + *customAccel = *((V3270CustomAccelerator *) accel); | |
| 54 | + rc = (V3270Accelerator *) customAccel; | |
| 55 | + } | |
| 56 | + else | |
| 57 | + { | |
| 58 | + rc = g_new0(V3270Accelerator,1); | |
| 59 | + *rc = *accel; | |
| 60 | + } | |
| 61 | + | |
| 62 | + return rc; | |
| 63 | + } | |
| 64 | + | |
| 65 | + | |
| 46 | 66 | void v3270_accelerator_map_reset(GtkWidget *widget) |
| 47 | 67 | { |
| 48 | 68 | v3270 * terminal = GTK_V3270(widget); |
| ... | ... | @@ -166,7 +186,7 @@ |
| 166 | 186 | |
| 167 | 187 | } |
| 168 | 188 | |
| 169 | - g_string_free(str,FALSE); | |
| 189 | + g_string_free(str,TRUE); | |
| 170 | 190 | |
| 171 | 191 | } |
| 172 | 192 | ... | ... |
src/terminal/keyboard/keyfile.c
| ... | ... | @@ -29,6 +29,7 @@ |
| 29 | 29 | |
| 30 | 30 | #include <config.h> |
| 31 | 31 | #include <gtk/gtk.h> |
| 32 | + #include <terminal.h> | |
| 32 | 33 | #include "private.h" |
| 33 | 34 | |
| 34 | 35 | //#include <v3270/actions.h> |
| ... | ... | @@ -66,3 +67,93 @@ |
| 66 | 67 | v3270_accelerator_map_foreach(GTK_WIDGET(widget),save_accelerator,&args); |
| 67 | 68 | |
| 68 | 69 | } |
| 70 | + | |
| 71 | + void v3270_accelerator_map_set_entry(v3270 *terminal, const gchar *name, const gchar *keys) | |
| 72 | + { | |
| 73 | + V3270Accelerator * accel = NULL; | |
| 74 | + | |
| 75 | + // Find accelerator by name | |
| 76 | + { | |
| 77 | + GSList * ix = terminal->accelerators; | |
| 78 | + | |
| 79 | + while(ix) | |
| 80 | + { | |
| 81 | + const gchar *accel_name = v3270_accelerator_get_name((V3270Accelerator *) ix->data); | |
| 82 | + | |
| 83 | + if(accel_name && !g_ascii_strcasecmp(accel_name,name)) | |
| 84 | + { | |
| 85 | + // It's the same name, steal it. | |
| 86 | + if(!accel) | |
| 87 | + accel = (V3270Accelerator *) ix->data; | |
| 88 | + else | |
| 89 | + g_free(ix->data); | |
| 90 | + | |
| 91 | + terminal->accelerators = g_slist_remove_link(terminal->accelerators, ix); | |
| 92 | + ix = terminal->accelerators; | |
| 93 | + | |
| 94 | + } | |
| 95 | + else | |
| 96 | + { | |
| 97 | + // Not the same name, get the next one. | |
| 98 | + ix = g_slist_next(ix); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + } | |
| 103 | + | |
| 104 | + if(!accel) | |
| 105 | + { | |
| 106 | + debug("Can't find accelerator \"%s\"",name); | |
| 107 | + g_warning("Can't find accelerator \"%s\"",name); | |
| 108 | + return; | |
| 109 | + } | |
| 110 | + | |
| 111 | + debug("Recreating accelerators for action \"%s\"",v3270_accelerator_get_name(accel)); | |
| 112 | + | |
| 113 | + { | |
| 114 | + size_t ix; | |
| 115 | + gchar ** keycodes = g_strsplit(keys,",",-1); | |
| 116 | + | |
| 117 | + for(ix=0;keycodes[ix];ix++) | |
| 118 | + { | |
| 119 | + V3270Accelerator * acc = v3270_accelerator_copy(accel); | |
| 120 | + gtk_accelerator_parse(keycodes[ix],&acc->key,&acc->mods); | |
| 121 | + terminal->accelerators = g_slist_prepend(terminal->accelerators,acc); | |
| 122 | + } | |
| 123 | + | |
| 124 | + g_strfreev(keycodes); | |
| 125 | + } | |
| 126 | + | |
| 127 | + g_free(accel); | |
| 128 | + } | |
| 129 | + | |
| 130 | + gboolean v3270_accelerator_map_load_key_file(GtkWidget *widget, GKeyFile *key_file, const gchar *group_name) | |
| 131 | + { | |
| 132 | + g_return_val_if_fail(GTK_IS_V3270(widget),FALSE); | |
| 133 | + | |
| 134 | + if(!group_name) | |
| 135 | + group_name = "accelerators"; | |
| 136 | + | |
| 137 | + v3270 * terminal = GTK_V3270(widget); | |
| 138 | + | |
| 139 | + gchar **keys = g_key_file_get_keys(key_file,group_name,NULL,NULL); | |
| 140 | + | |
| 141 | + if(!keys) | |
| 142 | + return FALSE; | |
| 143 | + | |
| 144 | + size_t ix; | |
| 145 | + for(ix = 0; keys[ix]; ix++) | |
| 146 | + { | |
| 147 | + g_autofree gchar * value = g_key_file_get_string(key_file, group_name, keys[ix],NULL); | |
| 148 | + | |
| 149 | + if(value) | |
| 150 | + v3270_accelerator_map_set_entry(terminal,keys[ix],value); | |
| 151 | + | |
| 152 | + } | |
| 153 | + | |
| 154 | + g_strfreev(keys); | |
| 155 | + | |
| 156 | + v3270_accelerator_map_sort(terminal); | |
| 157 | + | |
| 158 | + return TRUE; | |
| 159 | + } | ... | ... |
src/terminal/keyboard/private.h
| ... | ... | @@ -32,6 +32,7 @@ |
| 32 | 32 | #include <v3270/actions.h> |
| 33 | 33 | #include <internals.h> |
| 34 | 34 | |
| 35 | - G_GNUC_INTERNAL void v3270_accelerator_map_sort(v3270 *widget); | |
| 35 | + G_GNUC_INTERNAL void v3270_accelerator_map_sort(v3270 *widget); | |
| 36 | + G_GNUC_INTERNAL V3270Accelerator * v3270_accelerator_copy(const V3270Accelerator *accel); | |
| 36 | 37 | |
| 37 | 38 | ... | ... |
src/terminal/keyfile.c
| ... | ... | @@ -195,7 +195,7 @@ |
| 195 | 195 | switch(pspec->value_type) |
| 196 | 196 | { |
| 197 | 197 | case G_TYPE_STRING: |
| 198 | - g_value_set_string(&value, g_key_file_get_string(key_file,group_name,name,NULL)); | |
| 198 | + g_value_take_string(&value, g_key_file_get_string(key_file,group_name,name,NULL)); | |
| 199 | 199 | break; |
| 200 | 200 | |
| 201 | 201 | case G_TYPE_BOOLEAN: |
| ... | ... | @@ -255,6 +255,9 @@ |
| 255 | 255 | v3270 * terminal = GTK_V3270(widget); |
| 256 | 256 | v3270Class * klass = GTK_V3270_GET_CLASS(widget); |
| 257 | 257 | |
| 258 | + if(!group_name) | |
| 259 | + group_name = "terminal"; | |
| 260 | + | |
| 258 | 261 | g_object_freeze_notify(G_OBJECT(widget)); |
| 259 | 262 | |
| 260 | 263 | // Load Toggles | ... | ... |
src/terminal/properties/get.c
| ... | ... | @@ -129,7 +129,9 @@ |
| 129 | 129 | { |
| 130 | 130 | if(ix) |
| 131 | 131 | g_string_append_c(str,';'); |
| 132 | - g_string_append_printf(str,"%s",gdk_rgba_to_string(v3270_get_color(GTK_WIDGET(object),ix))); | |
| 132 | + | |
| 133 | + g_autofree gchar * rgb = gdk_rgba_to_string(v3270_get_color(GTK_WIDGET(object),ix)); | |
| 134 | + g_string_append(str,rgb); | |
| 133 | 135 | } |
| 134 | 136 | |
| 135 | 137 | g_value_take_string(value,g_string_free(str,FALSE)); | ... | ... |
src/testprogram/testprogram.c
| ... | ... | @@ -159,7 +159,9 @@ |
| 159 | 159 | #else |
| 160 | 160 | debug("%s: Loading settings...",__FUNCTION__); |
| 161 | 161 | GKeyFile * key_file = get_key_file(); |
| 162 | - v3270_load_key_file(terminal,key_file,"terminal"); | |
| 162 | + v3270_load_key_file(terminal,key_file,NULL); | |
| 163 | + v3270_accelerator_map_load_key_file(terminal,key_file,NULL); | |
| 164 | + | |
| 163 | 165 | g_key_file_free(key_file); |
| 164 | 166 | #endif // _WIN32 |
| 165 | 167 | |
| ... | ... | @@ -169,7 +171,8 @@ |
| 169 | 171 | v3270_set_trace(terminal,TRUE); |
| 170 | 172 | |
| 171 | 173 | // Setup and show main window |
| 172 | - gtk_window_set_title(GTK_WINDOW(window),v3270_get_session_title(terminal)); | |
| 174 | + g_autofree gchar * title = v3270_get_session_title(terminal); | |
| 175 | + gtk_window_set_title(GTK_WINDOW(window),title); | |
| 173 | 176 | gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); |
| 174 | 177 | gtk_window_set_default_size (GTK_WINDOW (window), 800, 500); |
| 175 | 178 | gtk_container_add(GTK_CONTAINER(window),vBox); | ... | ... |