diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2017-05-29 21:58:03 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2017-06-25 16:35:27 +0200 |
commit | 48aefca89da48e37fa06ab7f7e134eb2b2110a00 (patch) | |
tree | 36fdb7176a9f7820d3a4e44b7544c46c7733eaa4 /src/plugins | |
parent | 66124db85601ace508dd267967dfdad756eadb34 (diff) | |
download | weechat-48aefca89da48e37fa06ab7f7e134eb2b2110a00.zip |
fset: add mouse actions (select line, add to value, toggl boolean, set value)
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/fset/fset-command.c | 23 | ||||
-rw-r--r-- | src/plugins/fset/fset-mouse.c | 147 |
2 files changed, 165 insertions, 5 deletions
diff --git a/src/plugins/fset/fset-command.c b/src/plugins/fset/fset-command.c index ee1283e8b..a192369ff 100644 --- a/src/plugins/fset/fset-command.c +++ b/src/plugins/fset/fset-command.c @@ -136,6 +136,22 @@ fset_command_fset (const void *pointer, void *data, return WEECHAT_RC_OK; } + if (weechat_strcasecmp (argv[1], "-go") == 0) + { + if (fset_buffer) + { + if (argc < 3) + WEECHAT_COMMAND_ERROR; + error = NULL; + value = strtol (argv[2], &error, 10); + if (!error || error[0]) + WEECHAT_COMMAND_ERROR; + fset_buffer_set_current_line ((int)value); + fset_buffer_check_line_outside_window (); + } + return WEECHAT_RC_OK; + } + if (argv[1][0] == '-') { ptr_fset_option = weechat_arraylist_get (fset_options, @@ -243,9 +259,10 @@ fset_command_init () N_("fast set WeeChat and plugins options"), N_("-bar" " || -refresh" - " || -up|-down [number]" + " || -up|-down [<number>]" + " || -go <line>" " || -toggle" - " || -add [value]" + " || -add [<value>]" " || -reset" " || -unset" " || -set" @@ -255,6 +272,7 @@ fset_command_init () "-refresh: force the refresh of the \"fset\" bar item\n" " -up: move the selected line up by \"number\" lines\n" " -down: move the selected line down by \"number\" lines\n" + " -go: select a line by number\n" " -toggle: toggle the boolean value\n" " -add: add \"value\", which can be a negative number " "(only for integers and colors)\n" @@ -276,6 +294,7 @@ fset_command_init () " || -refresh" " || -up 1|2|3|4|5" " || -down 1|2|3|4|5" + " || -go" " || -toggle" " || -add -1|1" " || -reset" diff --git a/src/plugins/fset/fset-mouse.c b/src/plugins/fset/fset-mouse.c index 587a7b80a..6a2200a50 100644 --- a/src/plugins/fset/fset-mouse.c +++ b/src/plugins/fset/fset-mouse.c @@ -29,6 +29,7 @@ #include "fset-bar-item.h" #include "fset-buffer.h" #include "fset-config.h" +#include "fset-option.h" /* @@ -38,14 +39,94 @@ struct t_hashtable * fset_focus_cb (const void *pointer, void *data, struct t_hashtable *info) { + const char *buffer; + int rc; + long unsigned int value; + struct t_gui_buffer *ptr_buffer; + long y; + char *error, str_value[128]; + struct t_fset_option *ptr_fset_option; + /* make C compiler happy */ (void) pointer; (void) data; + if (!fset_buffer) + return info; + + buffer = weechat_hashtable_get (info, "_buffer"); + if (!buffer) + return info; + + rc = sscanf (buffer, "%lx", &value); + if ((rc == EOF) || (rc == 0)) + return info; + + ptr_buffer = (struct t_gui_buffer *)value; + + if (!ptr_buffer || (ptr_buffer != fset_buffer)) + return info; + + error = NULL; + y = strtol (weechat_hashtable_get (info, "_chat_line_y"), &error, 10); + if (!error || error[0]) + return info; + + if (y < 0) + return info; + + ptr_fset_option = weechat_arraylist_get (fset_options, y); + if (!ptr_fset_option) + return info; + + snprintf (str_value, sizeof (str_value), + "0x%lx", (long unsigned int)ptr_fset_option); + weechat_hashtable_set (info, "fset_option", str_value); + weechat_hashtable_set (info, "fset_option_name", ptr_fset_option->name); + weechat_hashtable_set (info, "fset_option_parent_name", ptr_fset_option->parent_name); + weechat_hashtable_set (info, "fset_option_type", ptr_fset_option->type); + weechat_hashtable_set (info, "fset_option_default_value", ptr_fset_option->default_value); + weechat_hashtable_set (info, "fset_option_value", ptr_fset_option->value); + weechat_hashtable_set (info, "fset_option_parent_value", ptr_fset_option->parent_value); + weechat_hashtable_set (info, "fset_option_min", ptr_fset_option->min); + weechat_hashtable_set (info, "fset_option_max", ptr_fset_option->max); + return info; } /* + * Get distance between x and x2 (as a positive integer); + */ + +int +fset_mouse_get_distance_x (struct t_hashtable *hashtable) +{ + int distance; + char *error; + long x, x2; + + distance = 0; + error = NULL; + x = strtol (weechat_hashtable_get (hashtable, "_chat_line_x"), + &error, 10); + if (error && !error[0]) + { + error = NULL; + x2 = strtol (weechat_hashtable_get (hashtable, "_chat_line_x2"), + &error, 10); + if (error && !error[0]) + { + distance = (x2 - x) / 3; + if (distance < 0) + distance *= -1; + else if (distance == 0) + distance = 1; + } + } + return distance; +} + +/* * Callback called when a mouse action occurs in fset bar or bar item. */ @@ -53,11 +134,71 @@ int fset_hsignal_cb (const void *pointer, void *data, const char *signal, struct t_hashtable *hashtable) { + const char *ptr_key, *ptr_chat_line_y, *ptr_fset_option_pointer; + char str_command[1024]; + struct t_fset_option *ptr_fset_option; + long unsigned int value; + int rc, distance; + /* make C compiler happy */ (void) pointer; (void) data; (void) signal; + if (!fset_buffer) + return WEECHAT_RC_OK; + + ptr_key = weechat_hashtable_get (hashtable, "_key"); + ptr_chat_line_y = weechat_hashtable_get (hashtable, "_chat_line_y"); + ptr_fset_option_pointer = weechat_hashtable_get (hashtable, "fset_option"); + + if (!ptr_key || !ptr_chat_line_y || !ptr_fset_option_pointer) + return WEECHAT_RC_OK; + + rc = sscanf (ptr_fset_option_pointer, "%lx", &value); + if ((rc == EOF) || (rc == 0)) + return WEECHAT_RC_OK; + ptr_fset_option = (struct t_fset_option *)value; + if (!ptr_fset_option) + return WEECHAT_RC_OK; + + snprintf (str_command, sizeof (str_command), + "/fset -go %s", + ptr_chat_line_y); + weechat_command (fset_buffer, str_command); + + if (strcmp (ptr_key, "button2") == 0) + { + snprintf (str_command, sizeof (str_command), + "/fset %s", + (strcmp (ptr_fset_option->type, "boolean") == 0) ? "-toggle" : "-set"); + weechat_command (fset_buffer, str_command); + } + else if (weechat_string_match (ptr_key, "button2-gesture-left*", 1)) + { + distance = fset_mouse_get_distance_x (hashtable); + if ((strcmp (ptr_fset_option->type, "integer") == 0) + || (strcmp (ptr_fset_option->type, "color") == 0)) + { + snprintf (str_command, sizeof (str_command), + "/fset -add -%d", + distance); + weechat_command (fset_buffer, str_command); + } + } + else if (weechat_string_match (ptr_key, "button2-gesture-right*", 1)) + { + distance = fset_mouse_get_distance_x (hashtable); + if ((strcmp (ptr_fset_option->type, "integer") == 0) + || (strcmp (ptr_fset_option->type, "color") == 0)) + { + snprintf (str_command, sizeof (str_command), + "/fset -add %d", + distance); + weechat_command (fset_buffer, str_command); + } + } + return WEECHAT_RC_OK; } @@ -81,15 +222,15 @@ fset_mouse_init () if (!keys) return 0; - weechat_hook_focus (FSET_BAR_ITEM_NAME, &fset_focus_cb, NULL, NULL); + weechat_hook_focus ("chat", &fset_focus_cb, NULL, NULL); weechat_hook_hsignal(FSET_MOUSE_HSIGNAL, &fset_hsignal_cb, NULL, NULL); weechat_hashtable_set ( keys, - "@chat(" FSET_PLUGIN_NAME "." FSET_BUFFER_NAME "):button1*", - "hsignal:" FSET_MOUSE_HSIGNAL); + "@chat(" FSET_PLUGIN_NAME "." FSET_BUFFER_NAME "):button1", + "/window ${_window_number};/fset -go ${_chat_line_y}"); weechat_hashtable_set ( keys, "@chat(" FSET_PLUGIN_NAME "." FSET_BUFFER_NAME "):button2*", |