ARB
macro_gui.cxx
Go to the documentation of this file.
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());
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 
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();
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 
86  error = getMacroRecorder(awr)->start_recording(macroName, sac, expand);
87  free(sac);
88  }
89  free(macroName);
90  }
91 
94  if (error) aw_message(error);
95 }
96 
97 static void edit_macro_cb(AW_window *aww) {
99  AW_edit(path);
100  free(path);
101 }
102 
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");
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 
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);
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 }
const char * GB_ERROR
Definition: arb_core.h:25
GB_ERROR start_recording(const char *file, const char *stop_action_name, bool expand_existing)
Definition: trackers.cxx:74
void update_macro_record_button(AW_root *awr)
Definition: macro_gui.cxx:63
bool is_tracking() const
Definition: aw_root.hxx:179
void AW_set_selected_fullname(AW_root *awr, const char *awar_prefix, const char *to_fullname)
Definition: AW_file.cxx:940
void insert_menu_topic(const char *id, const char *name, const char *mnemonic, const char *help_text_, AW_active mask, const WindowCallback &wcb)
Definition: AW_window.cxx:592
#define MACRO_WINDOW_ID
GB_ERROR GB_add_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback &dbcb)
Definition: ad_cb.cxx:356
void AW_edit(const char *path)
Definition: AW_edit.cxx:16
UserActionTracker * get_tracker()
Definition: aw_root.hxx:180
#define AWAR_MACRO_RECORDING_EXPAND
Definition: macro_gui.cxx:30
GB_ERROR execute(const char *macroFile, bool loop_marked, const RootCallback &execution_done_cb)
Definition: trackers.cxx:210
long read_int() const
Definition: AW_awar.cxx:187
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:204
char * AW_get_selected_fullname(AW_root *awr, const char *awar_prefix)
Definition: AW_file.cxx:906
MacroRecorder * getMacroRecorder(AW_root *aw_root)
Definition: trackers.hxx:144
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
char * window_defaults_name
window title
Definition: aw_window.hxx:336
int GB_unlink(const char *path)
Definition: arb_file.cxx:188
static void dont_announce_done(AW_root *)
Definition: macro_gui.cxx:178
#define AWAR_MACRO_RECORDING_RUNB4
Definition: macro_gui.cxx:31
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:353
static AW_root * SINGLETON
Definition: aw_root.hxx:102
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
#define AWAR_MACRO_RECORDING_MACRO_TEXT
Definition: macro_gui.cxx:29
static void macro_recording_changed_cb()
Definition: macro_gui.cxx:103
static void error(const char *msg)
Definition: mkptypes.cxx:96
void AW_create_fileselection_awars(AW_root *awr, const char *awar_base, const char *directories, const char *filter, const char *file_name)
Definition: AW_file.cxx:72
GBDATA * GB_searchOrCreate_int(GBDATA *gb_container, const char *fieldpath, long default_value)
Definition: adquery.cxx:569
static void exec_macro_cb(AW_window *aww, bool loop_marked)
Definition: macro_gui.cxx:53
static void edit_macro_cb(AW_window *aww)
Definition: macro_gui.cxx:97
void execute_macro(AW_root *root, const char *macroname)
Definition: macro_gui.cxx:180
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
static void popup_macro_window(AW_window *aww)
Definition: macro_gui.cxx:132
void AW_refresh_fileselection(AW_root *awr, const char *awar_prefix)
Definition: AW_file.cxx:944
static AW_window_menu_modes_opengl * awm
static void start_macro_cb(AW_window *aww)
Definition: macro_gui.cxx:67
static void create_macro_variables(AW_root *aw_root)
Definition: macro_gui.cxx:107
AW_awar * awar_int(const char *var_name, long default_value=0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:580
#define MACRO_RECORD_ID
bool GB_is_directory(const char *path)
Definition: arb_file.cxx:176
static void macro_execution_finished(AW_root *awr, char *macroName)
Definition: macro_gui.cxx:43
#define MACRO_TRIGGER_RECORDING
Definition: arbdbt.h:25
#define AWAR_MACRO_BASE
void aw_message(const char *msg)
Definition: AW_status.cxx:932
void insert_macro_menu_entry(AW_window *awm, bool prepend_separator)
Definition: macro_gui.cxx:171
AW_root * get_root()
Definition: aw_window.hxx:348
#define NULp
Definition: cxxforward.h:97
GB_ERROR stop_recording()
Definition: trackers.cxx:91
void AW_create_fileselection(AW_window *aws, const char *awar_prefix, const char *at_prefix, const char *pwd, DirDisplay disp_dirs, bool allow_wildcards)
Definition: AW_file.cxx:870
GB_ERROR write_string(const char *aw_string)
void sep______________()
Definition: AW_window.cxx:750
static void delete_macro_cb(AW_window *aww)
Definition: macro_gui.cxx:33
GBDATA * get_gbmain()
Definition: trackers.hxx:66
GB_transaction ta(gb_var)
GBDATA * gb_main
Definition: adname.cxx:33
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
#define MACRO_PLAYBACK_MARKED_ID
#define MACRO_PLAYBACK_ID
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:195