1 | // ============================================================= // |
---|
2 | // // |
---|
3 | // File : macro_gui.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in May 2005 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // ============================================================= // |
---|
11 | |
---|
12 | #include "macros.hxx" |
---|
13 | #include "macros_local.hxx" |
---|
14 | #include "trackers.hxx" |
---|
15 | |
---|
16 | #include <arbdb.h> |
---|
17 | #include <arb_file.h> |
---|
18 | |
---|
19 | #include <aw_window.hxx> |
---|
20 | #include <aw_edit.hxx> |
---|
21 | #include <aw_file.hxx> |
---|
22 | #include <aw_awar.hxx> |
---|
23 | #include <aw_msg.hxx> |
---|
24 | #include <arbdbt.h> |
---|
25 | #include <ad_cb.h> |
---|
26 | |
---|
27 | #define ma_assert(bed) arb_assert(bed) |
---|
28 | |
---|
29 | #define AWAR_MACRO_RECORDING_MACRO_TEXT AWAR_MACRO_BASE"/button_label" |
---|
30 | #define AWAR_MACRO_RECORDING_EXPAND AWAR_MACRO_BASE"/expand" |
---|
31 | #define AWAR_MACRO_RECORDING_RUNB4 AWAR_MACRO_BASE"/runb4" |
---|
32 | |
---|
33 | static void delete_macro_cb(AW_window *aww) { |
---|
34 | AW_root *awr = aww->get_root(); |
---|
35 | char *macroName = AW_get_selected_fullname(awr, AWAR_MACRO_BASE); |
---|
36 | |
---|
37 | if (GB_unlink(macroName)<0) aw_message(GB_await_error()); |
---|
38 | else AW_refresh_fileselection(awr, AWAR_MACRO_BASE); |
---|
39 | |
---|
40 | free(macroName); |
---|
41 | } |
---|
42 | |
---|
43 | static void macro_execution_finished(AW_root *awr, char *macroName) { |
---|
44 | #if defined(DEBUG) |
---|
45 | fprintf(stderr, "macro_execution_finished(%s)\n", macroName); |
---|
46 | #endif |
---|
47 | |
---|
48 | AW_set_selected_fullname(awr, AWAR_MACRO_BASE, macroName); // reset selected macro (needed if macro calls other macro(s)) |
---|
49 | |
---|
50 | free(macroName); |
---|
51 | } |
---|
52 | |
---|
53 | static void exec_macro_cb(AW_window *aww, bool loop_marked) { |
---|
54 | AW_root *awr = aww->get_root(); |
---|
55 | char *macroName = AW_get_selected_fullname(awr, AWAR_MACRO_BASE); // @@@ instead use function returning plain name w/o dir |
---|
56 | GB_ERROR error = getMacroRecorder(awr)->execute(macroName, loop_marked, makeRootCallback(macro_execution_finished, macroName)); |
---|
57 | if (error) { |
---|
58 | aw_message(error); |
---|
59 | free(macroName); // only free in error-case (see macro_execution_finished) |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | inline void update_macro_record_button(AW_root *awr) { |
---|
64 | awr->awar(AWAR_MACRO_RECORDING_MACRO_TEXT)->write_string(awr->is_tracking() ? "STOP" : "RECORD"); |
---|
65 | } |
---|
66 | |
---|
67 | static void start_macro_cb(AW_window *aww) { |
---|
68 | AW_root *awr = aww->get_root(); |
---|
69 | GB_ERROR error = NULp; |
---|
70 | |
---|
71 | if (awr->is_tracking()) { |
---|
72 | error = getMacroRecorder(awr)->stop_recording(); |
---|
73 | } |
---|
74 | else { |
---|
75 | bool expand = awr->awar(AWAR_MACRO_RECORDING_EXPAND)->read_int(); |
---|
76 | bool runb4 = expand && awr->awar(AWAR_MACRO_RECORDING_RUNB4)->read_int(); |
---|
77 | |
---|
78 | char *macroName = AW_get_selected_fullname(awr, AWAR_MACRO_BASE); |
---|
79 | if (GB_is_directory(macroName)) { |
---|
80 | error = "Please specify name of macro to record"; |
---|
81 | } |
---|
82 | else { |
---|
83 | if (runb4) exec_macro_cb(aww, false); |
---|
84 | |
---|
85 | char *sac = GBS_global_string_copy("%s/%s", aww->window_defaults_name, MACRO_RECORD_ID); |
---|
86 | error = getMacroRecorder(awr)->start_recording(macroName, sac, expand); |
---|
87 | free(sac); |
---|
88 | } |
---|
89 | free(macroName); |
---|
90 | } |
---|
91 | |
---|
92 | AW_refresh_fileselection(awr, AWAR_MACRO_BASE); |
---|
93 | update_macro_record_button(awr); |
---|
94 | if (error) aw_message(error); |
---|
95 | } |
---|
96 | |
---|
97 | static void edit_macro_cb(AW_window *aww) { |
---|
98 | char *path = AW_get_selected_fullname(aww->get_root(), AWAR_MACRO_BASE); |
---|
99 | AW_edit(path); |
---|
100 | free(path); |
---|
101 | } |
---|
102 | |
---|
103 | static void macro_recording_changed_cb() { |
---|
104 | update_macro_record_button(AW_root::SINGLETON); |
---|
105 | } |
---|
106 | |
---|
107 | static void create_macro_variables(AW_root *aw_root) { |
---|
108 | AW_create_fileselection_awars(aw_root, AWAR_MACRO_BASE, ".", ".amc", ""); |
---|
109 | aw_root->awar_string(AWAR_MACRO_RECORDING_MACRO_TEXT, "RECORD"); |
---|
110 | aw_root->awar_int(AWAR_MACRO_RECORDING_EXPAND, 0); |
---|
111 | aw_root->awar_int(AWAR_MACRO_RECORDING_RUNB4, 0); |
---|
112 | |
---|
113 | { |
---|
114 | UserActionTracker *tracker = aw_root->get_tracker(); |
---|
115 | MacroRecorder *macroRecorder = dynamic_cast<MacroRecorder*>(tracker); |
---|
116 | |
---|
117 | GBDATA *gb_main = macroRecorder->get_gbmain(); |
---|
118 | GB_ERROR error = NULp; |
---|
119 | |
---|
120 | GB_transaction ta(gb_main); |
---|
121 | |
---|
122 | GBDATA *gb_recording = GB_searchOrCreate_int(gb_main, MACRO_TRIGGER_RECORDING, 0); |
---|
123 | if (!gb_recording) error = GB_await_error(); |
---|
124 | else error = GB_add_callback(gb_recording, GB_CB_CHANGED, makeDatabaseCallback(macro_recording_changed_cb)); |
---|
125 | |
---|
126 | if (error) aw_message(GBS_global_string("Failed to bind macro_recording_changed_cb (Reason: %s)", error)); |
---|
127 | } |
---|
128 | |
---|
129 | update_macro_record_button(aw_root); |
---|
130 | } |
---|
131 | |
---|
132 | static void popup_macro_window(AW_window *aww) { |
---|
133 | static AW_window_simple *aws = NULp; |
---|
134 | if (!aws) { |
---|
135 | AW_root *aw_root = aww->get_root(); |
---|
136 | |
---|
137 | aws = new AW_window_simple; |
---|
138 | aws->init(aw_root, MACRO_WINDOW_ID, "MACROS"); |
---|
139 | aws->load_xfig("macro_select.fig"); |
---|
140 | |
---|
141 | create_macro_variables(aw_root); |
---|
142 | |
---|
143 | aws->at("close"); aws->callback(AW_POPDOWN); |
---|
144 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
145 | |
---|
146 | aws->at("help"); aws->callback(makeHelpCallback("macro.hlp")); |
---|
147 | aws->create_button("HELP", "HELP"); |
---|
148 | |
---|
149 | aws->at("record"); aws->callback(start_macro_cb); |
---|
150 | aws->create_button(MACRO_RECORD_ID, AWAR_MACRO_RECORDING_MACRO_TEXT); |
---|
151 | |
---|
152 | aws->at("expand"); aws->create_toggle(AWAR_MACRO_RECORDING_EXPAND); |
---|
153 | aws->at("runb4"); aws->create_toggle(AWAR_MACRO_RECORDING_RUNB4); |
---|
154 | |
---|
155 | aws->at("edit"); aws->callback(edit_macro_cb); aws->create_button("EDIT", "EDIT"); |
---|
156 | aws->at("delete"); aws->callback(delete_macro_cb); aws->create_button("DELETE", "DELETE"); |
---|
157 | |
---|
158 | aws->at("exec"); |
---|
159 | aws->callback(makeWindowCallback(exec_macro_cb, false)); |
---|
160 | aws->create_button(MACRO_PLAYBACK_ID, "EXECUTE"); |
---|
161 | |
---|
162 | aws->at("execWith"); |
---|
163 | aws->callback(makeWindowCallback(exec_macro_cb, true)); |
---|
164 | aws->create_autosize_button(MACRO_PLAYBACK_MARKED_ID, "Execute with each marked species"); |
---|
165 | |
---|
166 | AW_create_fileselection(aws, AWAR_MACRO_BASE, "", "ARBMACROHOME^ARBMACRO", ANY_DIR, false); |
---|
167 | } |
---|
168 | aws->activate(); |
---|
169 | } |
---|
170 | |
---|
171 | void insert_macro_menu_entry(AW_window *awm, bool prepend_separator) { |
---|
172 | if (getMacroRecorder(awm->get_root())) { |
---|
173 | if (prepend_separator) awm->sep______________(); |
---|
174 | awm->insert_menu_topic("macros", "Macros ", "M", "macro.hlp", AWM_ALL, popup_macro_window); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | static void dont_announce_done(AW_root*) {} |
---|
179 | |
---|
180 | void execute_macro(AW_root *root, const char *macroname) { |
---|
181 | // used to execute macro passed via CLI |
---|
182 | GB_ERROR error = NULp; |
---|
183 | { |
---|
184 | // @@@ allow macro playback from client? (using server via AWAR) |
---|
185 | MacroRecorder *recorder = getMacroRecorder(root); |
---|
186 | if (!recorder) error = "macro playback only available in server"; |
---|
187 | else error = recorder->execute(macroname, false, makeRootCallback(dont_announce_done)); |
---|
188 | } |
---|
189 | |
---|
190 | if (error) { |
---|
191 | aw_message(GBS_global_string("Can't execute macro '%s' (Reason: %s)", macroname, error)); |
---|
192 | } |
---|
193 | } |
---|