From 4f1398e267ac1fbddea5a754889672e17a90ad8c Mon Sep 17 00:00:00 2001
From: Mooffie <mooffie@gmail.com>
Date: Sun, 14 Feb 2016 19:40:22 +0200
Subject: [PATCH 1/2] Simplify buttonbar and menu handling.
We make WButtonBar send the command directly to the target widget. This
lets us simplify the MSG_ACTION case in dialog handlers. The menu handling
too is simplified here.
---
lib/widget/buttonbar.c | 5 ++++-
src/diffviewer/ydiff.c | 16 ++++------------
src/editor/editwidget.c | 31 ++++++++++++-------------------
src/filemanager/midnight.c | 16 ++--------------
src/help.c | 13 ++-----------
src/viewer/actions_cmd.c | 16 ++++------------
6 files changed, 28 insertions(+), 69 deletions(-)
diff --git a/lib/widget/buttonbar.c b/lib/widget/buttonbar.c
index f879969..a987967 100644
a
|
b
|
buttonbar_call (WButtonBar * bb, int i) |
148 | 148 | { |
149 | 149 | cb_ret_t ret = MSG_NOT_HANDLED; |
150 | 150 | Widget *w = WIDGET (bb); |
| 151 | Widget *target; |
| 152 | |
| 153 | target = (bb->labels[i].receiver != NULL) ? bb->labels[i].receiver : WIDGET (w->owner); |
151 | 154 | |
152 | 155 | if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey)) |
153 | | ret = send_message (w->owner, w, MSG_ACTION, bb->labels[i].command, bb->labels[i].receiver); |
| 156 | ret = send_message (target, w, MSG_ACTION, bb->labels[i].command, NULL); |
154 | 157 | return ret; |
155 | 158 | } |
156 | 159 | |
diff --git a/src/diffviewer/ydiff.c b/src/diffviewer/ydiff.c
index e3aaeb0..c4c8f67 100644
a
|
b
|
dview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, |
3406 | 3406 | return MSG_HANDLED; |
3407 | 3407 | |
3408 | 3408 | case MSG_ACTION: |
3409 | | /* shortcut */ |
3410 | | if (sender == NULL) |
3411 | | return dview_execute_cmd (NULL, parm); |
3412 | | /* message from buttonbar */ |
3413 | | if (sender == WIDGET (find_buttonbar (h))) |
3414 | | { |
3415 | | if (data != NULL) |
3416 | | return send_message (data, NULL, MSG_ACTION, parm, NULL); |
| 3409 | /* Handle shortcuts. */ |
3417 | 3410 | |
3418 | | dview = (WDiff *) find_widget_type (h, dview_callback); |
3419 | | return dview_execute_cmd (dview, parm); |
3420 | | } |
3421 | | return MSG_NOT_HANDLED; |
| 3411 | /* Note: the buttonbar sends messages directly to the the WDiff, not to |
| 3412 | * here, which is why we can pass NULL in the following call. */ |
| 3413 | return dview_execute_cmd (NULL, parm); |
3422 | 3414 | |
3423 | 3415 | case MSG_VALIDATE: |
3424 | 3416 | dview = (WDiff *) find_widget_type (h, dview_callback); |
diff --git a/src/editor/editwidget.c b/src/editor/editwidget.c
index 9c2fcb3..565b12c 100644
a
|
b
|
edit_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, v |
1020 | 1020 | return MSG_HANDLED; |
1021 | 1021 | |
1022 | 1022 | case MSG_ACTION: |
1023 | | /* shortcut */ |
1024 | | if (sender == NULL) |
1025 | | return edit_dialog_command_execute (h, parm); |
1026 | | /* message from menu */ |
1027 | | menubar = find_menubar (h); |
1028 | | if (sender == WIDGET (menubar)) |
1029 | | { |
1030 | | if (edit_dialog_command_execute (h, parm) == MSG_HANDLED) |
1031 | | return MSG_HANDLED; |
1032 | | /* try send command to the current window */ |
1033 | | return send_message (h->current->data, NULL, MSG_ACTION, parm, NULL); |
1034 | | } |
1035 | | /* message from buttonbar */ |
1036 | | buttonbar = find_buttonbar (h); |
1037 | | if (sender == WIDGET (buttonbar)) |
1038 | 1023 | { |
1039 | | if (data != NULL) |
1040 | | return send_message (data, NULL, MSG_ACTION, parm, NULL); |
1041 | | return edit_dialog_command_execute (h, parm); |
| 1024 | /* Handle shortcuts, menu, and buttonbar. */ |
| 1025 | |
| 1026 | cb_ret_t result; |
| 1027 | |
| 1028 | result = edit_dialog_command_execute (h, parm); |
| 1029 | |
| 1030 | /* We forward any commands coming from the menu, and which haven't been |
| 1031 | handled by the dialog, to the focused WEdit window. */ |
| 1032 | if ((result == MSG_NOT_HANDLED) && (sender == WIDGET (find_menubar (h)))) |
| 1033 | result = send_message (h->current->data, NULL, MSG_ACTION, parm, NULL); |
| 1034 | |
| 1035 | return result; |
1042 | 1036 | } |
1043 | | return MSG_NOT_HANDLED; |
1044 | 1037 | |
1045 | 1038 | case MSG_KEY: |
1046 | 1039 | { |
diff --git a/src/filemanager/midnight.c b/src/filemanager/midnight.c
index c21a378..c2c75c6 100644
a
|
b
|
midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void |
1565 | 1565 | return MSG_HANDLED; |
1566 | 1566 | |
1567 | 1567 | case MSG_ACTION: |
1568 | | /* shortcut */ |
1569 | | if (sender == NULL) |
1570 | | return midnight_execute_cmd (NULL, parm); |
1571 | | /* message from menu */ |
1572 | | if (sender == WIDGET (the_menubar)) |
1573 | | return midnight_execute_cmd (sender, parm); |
1574 | | /* message from buttonbar */ |
1575 | | if (sender == WIDGET (the_bar)) |
1576 | | { |
1577 | | if (data != NULL) |
1578 | | return send_message (data, NULL, MSG_ACTION, parm, NULL); |
1579 | | return midnight_execute_cmd (sender, parm); |
1580 | | } |
1581 | | return MSG_NOT_HANDLED; |
| 1568 | /* Handle shortcuts, menu, and buttonbar. */ |
| 1569 | return midnight_execute_cmd (sender, parm); |
1582 | 1570 | |
1583 | 1571 | case MSG_END: |
1584 | 1572 | panel_deinit (); |
diff --git a/src/help.c b/src/help.c
index 7dacdfa..cbf4c2c 100644
a
|
b
|
help_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *da |
966 | 966 | return help_handle_key (h, parm); |
967 | 967 | |
968 | 968 | case MSG_ACTION: |
969 | | /* shortcut */ |
970 | | if (sender == NULL) |
971 | | return help_execute_cmd (parm); |
972 | | /* message from buttonbar */ |
973 | | if (sender == WIDGET (find_buttonbar (h))) |
974 | | { |
975 | | if (data != NULL) |
976 | | return send_message (data, NULL, MSG_ACTION, parm, NULL); |
977 | | return help_execute_cmd (parm); |
978 | | } |
979 | | return MSG_NOT_HANDLED; |
| 969 | /* Handle shortcuts and buttonbar. */ |
| 970 | return help_execute_cmd (parm); |
980 | 971 | |
981 | 972 | default: |
982 | 973 | return dlg_default_callback (w, sender, msg, parm, data); |
diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c
index 4514882..2e15a86 100644
a
|
b
|
mcview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, |
724 | 724 | return MSG_HANDLED; |
725 | 725 | |
726 | 726 | case MSG_ACTION: |
727 | | /* shortcut */ |
728 | | if (sender == NULL) |
729 | | return mcview_execute_cmd (NULL, parm); |
730 | | /* message from buttonbar */ |
731 | | if (sender == WIDGET (find_buttonbar (h))) |
732 | | { |
733 | | if (data != NULL) |
734 | | return send_message (data, NULL, MSG_ACTION, parm, NULL); |
| 727 | /* Handle shortcuts. */ |
735 | 728 | |
736 | | view = (WView *) find_widget_type (h, mcview_callback); |
737 | | return mcview_execute_cmd (view, parm); |
738 | | } |
739 | | return MSG_NOT_HANDLED; |
| 729 | /* Note: the buttonbar sends messages directly to the the WView, not to |
| 730 | * here, which is why we can pass NULL in the following call. */ |
| 731 | return mcview_execute_cmd (NULL, parm); |
740 | 732 | |
741 | 733 | case MSG_VALIDATE: |
742 | 734 | view = (WView *) find_widget_type (h, mcview_callback); |