1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
#include "GmshConfig.h"
#if !defined(HAVE_NO_STDINT_H)
#include <stdint.h>
#elif defined(HAVE_NO_INTPTR_T)
typedef unsigned long intptr_t;
#endif
#include <vector>
#include <string.h>
#include <FL/Fl_Return_Button.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Value_Input.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Tabs.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Help_View.H>
#include "FlGui.h"
#include "drawContext.h"
#include "pluginWindow.h"
#include "paletteWindow.h"
#include "PView.h"
#include "PluginManager.h"
#include "Plugin.h"
#include "GModel.h"
#include "MVertex.h"
#include "Context.h"
#include "scriptStringInterface.h"
#include "StringUtils.h"
#include "OS.h"
#define MAX_PLUGIN_OPTIONS 50
class PluginDialogBox {
public:
Fl_Group *group;
Fl_Value_Input *value[MAX_PLUGIN_OPTIONS];
Fl_Input *input[MAX_PLUGIN_OPTIONS];
};
void plugin_cb(Fl_Widget *w, void *data)
{
FlGui::instance()->plugins->show((intptr_t)data);
}
static void plugin_input_value_cb(Fl_Widget *w, void *data)
{
double (*f)(int, int, double) = (double (*)(int, int, double))data;
Fl_Value_Input *input = (Fl_Value_Input *)w;
f(-1, 0, input->value());
}
static void plugin_input_cb(Fl_Widget *w, void *data)
{
std::string (*f)(int, int, std::string) =
(std::string(*)(int, int, std::string))data;
Fl_Input *input = (Fl_Input *)w;
f(-1, 0, input->value());
}
static void plugin_browser_cb(Fl_Widget *w, void *data)
{
// get selected plugin
GMSH_Plugin *p = nullptr;
for(int i = 1; i <= FlGui::instance()->plugins->browser->size(); i++) {
if(FlGui::instance()->plugins->browser->selected(i)) {
p = (GMSH_Plugin *)FlGui::instance()->plugins->browser->data(i);
break;
}
}
if(!p) return;
// get first first selected view
int iView = -1;
for(int i = 1; i <= FlGui::instance()->plugins->view_browser->size(); i++) {
if(FlGui::instance()->plugins->view_browser->selected(i)) {
iView = i - 1;
break;
}
}
// set the Fl_Value_Input callbacks and configure the input value
// fields (we get step, min and max by calling the option function
// with action==1, 2 and 3, respectively)
int n = p->getNbOptions();
if(n > MAX_PLUGIN_OPTIONS) n = MAX_PLUGIN_OPTIONS;
for(int i = 0; i < n; i++) {
StringXNumber *sxn = p->getOption(i);
if(sxn->function) {
p->dialogBox->value[i]->callback(plugin_input_value_cb,
(void *)sxn->function);
if(iView >= 0) {
if(CTX::instance()->inputScrolling)
p->dialogBox->value[i]->step(sxn->function(iView, 1, 0.), 1);
p->dialogBox->value[i]->minimum(sxn->function(iView, 2, 0.));
p->dialogBox->value[i]->maximum(sxn->function(iView, 3, 0.));
}
}
}
// set the Fl_Input callbacks
int m = p->getNbOptionsStr();
if(m > MAX_PLUGIN_OPTIONS) m = MAX_PLUGIN_OPTIONS;
for(int i = 0; i < m; i++) {
StringXString *sxs = p->getOptionStr(i);
if(sxs->function) {
p->dialogBox->input[i]->callback(plugin_input_cb, (void *)sxs->function);
}
}
// hide all plugin groups except the selected one
for(int i = 1; i <= FlGui::instance()->plugins->browser->size(); i++)
((GMSH_Plugin *)FlGui::instance()->plugins->browser->data(i))
->dialogBox->group->hide();
p->dialogBox->group->show();
}
static void add_scripting(GMSH_PostPlugin *p, PView *view)
{
if(!FlGui::instance()->plugins->record->value()) return;
std::string fileName;
int oldIndex = -1;
if(view) {
for(int i = 0; i < p->getNbOptions(); i++) {
if(std::string(p->getOption(i)->str) == "View") {
oldIndex = (int)(p->getOption(i)->def);
p->getOption(i)->def = view->getIndex();
}
}
fileName = view->getData()->getFileName();
}
else
fileName = GModel::current()->getFileName();
fileName += ".opt";
FILE *fp = Fopen(fileName.c_str(), "a");
if(!fp) { Msg::Error("Could not open file '%s'", fileName.c_str()); }
else {
fprintf(fp, "%s", p->serialize().c_str());
fclose(fp);
}
if(view && oldIndex != -1) {
for(int i = 0; i < p->getNbOptions(); i++) {
if(std::string(p->getOption(i)->str) == "View") {
p->getOption(i)->def = oldIndex;
}
}
}
}
static void plugin_run_cb(Fl_Widget *w, void *data)
{
GMSH_Plugin *p = (GMSH_Plugin *)data;
// get the values from the GUI
int m = p->getNbOptionsStr();
int n = p->getNbOptions();
if(m > MAX_PLUGIN_OPTIONS) m = MAX_PLUGIN_OPTIONS;
if(n > MAX_PLUGIN_OPTIONS) n = MAX_PLUGIN_OPTIONS;
for(int i = 0; i < m; i++) {
StringXString *sxs = p->getOptionStr(i);
sxs->def = p->dialogBox->input[i]->value();
}
for(int i = 0; i < n; i++) {
StringXNumber *sxn = p->getOption(i);
sxn->def = p->dialogBox->value[i]->value();
}
if(p->getType() == GMSH_Plugin::GMSH_POST_PLUGIN) {
GMSH_PostPlugin *pp = (GMSH_PostPlugin *)p;
// run on all selected views
bool no_view_selected = true;
for(int i = 1; i <= FlGui::instance()->plugins->view_browser->size(); i++) {
if(FlGui::instance()->plugins->view_browser->selected(i)) {
no_view_selected = false;
try {
if(i - 1 >= 0 && i - 1 < (int)PView::list.size()) {
PView *view = PView::list[i - 1];
if(view->getData()->isRemote())
pp->executeRemote(view);
else {
pp->execute(view);
add_scripting(pp, view);
}
}
else {
pp->execute(nullptr);
add_scripting(pp, nullptr);
}
} catch(GMSH_Plugin *err) {
char tmp[256];
pp->catchErrorMessage(tmp);
Msg::Warning("%s", tmp);
}
}
}
if(no_view_selected) {
pp->execute(nullptr);
add_scripting(pp, nullptr);
}
}
else {
p->run();
}
FlGui::instance()->updateViews(true, true);
GMSH_Plugin::draw = nullptr;
drawContext::global()->draw();
}
void pluginWindow::_createDialogBox(GMSH_Plugin *p, int x, int y, int width,
int height)
{
p->dialogBox = new PluginDialogBox;
p->dialogBox->group = new Fl_Group(x, y, width, height);
Fl_Box *title = new Fl_Box(x, y, width, BH, strdup(p->getName().c_str()));
title->labelfont(FL_BOLD);
title->labelsize(FL_NORMAL_SIZE + 3);
title->align(FL_ALIGN_INSIDE);
Fl_Box *help =
new Fl_Box(x, y + BH, width, BH + WB, strdup(p->getShortHelp().c_str()));
help->align(FL_ALIGN_WRAP | FL_ALIGN_CLIP | FL_ALIGN_TOP | FL_ALIGN_INSIDE);
int top = 2 * BH + WB;
{
Fl_Tabs *o = new Fl_Tabs(x, y + top, width, height - top);
{
Fl_Group *g =
new Fl_Group(x, y + top + BH, width, height - top - BH, "Options");
Fl_Scroll *s = new Fl_Scroll(x + WB, y + top + BH + WB, width - 2 * WB,
height - top - 2 * BH - 3 * WB);
int m = p->getNbOptionsStr();
if(m > MAX_PLUGIN_OPTIONS) m = MAX_PLUGIN_OPTIONS;
int n = p->getNbOptions();
if(n > MAX_PLUGIN_OPTIONS) n = MAX_PLUGIN_OPTIONS;
int k = 0;
for(int i = 0; i < m; i++) {
StringXString *sxs = p->getOptionStr(i);
p->dialogBox->input[i] =
new Fl_Input(x + WB, y + top + (k + 1) * BH + WB, IW, BH, sxs->str);
p->dialogBox->input[i]->align(FL_ALIGN_RIGHT);
p->dialogBox->input[i]->value(sxs->def.c_str());
k++;
}
for(int i = 0; i < n; i++) {
StringXNumber *sxn = p->getOption(i);
p->dialogBox->value[i] = new Fl_Value_Input(
x + WB, y + top + (k + 1) * BH + WB, IW, BH, sxn->str);
p->dialogBox->value[i]->align(FL_ALIGN_RIGHT);
p->dialogBox->value[i]->value(sxn->def);
k++;
}
s->end();
Fl_Return_Button *run = new Fl_Return_Button(
x + width - BB - WB, y + height - BH - WB, BB, BH, "Run");
run->callback(plugin_run_cb, (void *)p);
g->resizable(new Fl_Box(x + 2 * WB, y + top + 2 * BH, 1, 1));
g->end();
o->resizable(g);
}
{
Fl_Group *g =
new Fl_Group(x, y + top + BH, width, height - top - BH, "Help");
Fl_Help_View *o =
new Fl_Help_View(x + WB, y + top + BH + WB, width - 2 * WB,
height - top - 2 * BH - 3 * WB);
std::string help = p->getHelp();
ConvertToHTML(help);
help += std::string("<p><em>Author(s): ") + p->getAuthor() + "</em>";
o->value(help.c_str());
o->textfont(FL_HELVETICA);
o->textsize(FL_NORMAL_SIZE);
g->resizable(new Fl_Box(x + 2 * WB, y + top + 2 * BH, 1, 1));
g->end();
}
o->end();
}
p->dialogBox->group->end();
p->dialogBox->group->hide();
}
pluginWindow::pluginWindow(int deltaFontSize)
{
FL_NORMAL_SIZE -= deltaFontSize;
int width0 = 37 * FL_NORMAL_SIZE + WB;
int height0 = 12 * BH + 4 * WB;
int L1 = 13 * FL_NORMAL_SIZE, L2 = 6 * FL_NORMAL_SIZE;
int width = (CTX::instance()->pluginSize[0] < width0) ?
width0 :
CTX::instance()->pluginSize[0];
int height = (CTX::instance()->pluginSize[1] < height0) ?
height0 :
CTX::instance()->pluginSize[1];
win = new paletteWindow(
width, height, CTX::instance()->nonModalWindows ? true : false, "Plugins");
win->box(GMSH_WINDOW_BOX);
browser = new Fl_Hold_Browser(0, 0, L1, height);
browser->callback(plugin_browser_cb);
browser->box(GMSH_SIMPLE_RIGHT_BOX);
browser->has_scrollbar(Fl_Browser_::VERTICAL);
view_browser = new Fl_Multi_Browser(L1, 0, L2, height);
view_browser->has_scrollbar(Fl_Browser_::VERTICAL);
view_browser->callback(plugin_browser_cb);
view_browser->box(GMSH_SIMPLE_RIGHT_BOX);
for(auto it = PluginManager::instance()->begin();
it != PluginManager::instance()->end(); ++it) {
GMSH_Plugin *p = it->second;
if(p->getType() == GMSH_Plugin::GMSH_POST_PLUGIN ||
p->getType() == GMSH_Plugin::GMSH_MESH_PLUGIN) {
browser->add(p->getName().c_str(), p);
_createDialogBox(p, L1 + L2 + WB, WB, width - L1 - L2 - 2 * WB,
height - 2 * WB);
// select first plugin by default
if(it == PluginManager::instance()->begin()) {
browser->select(1);
p->dialogBox->group->show();
}
}
}
record = new Fl_Check_Button(L1 + L2 + 2 * WB, height - BH - 2 * WB, BB, BH,
"Record");
record->type(FL_TOGGLE_BUTTON);
record->tooltip(
"Append scripting command to file options when plugin is run");
win->resizable(new Fl_Box(L1 + L2 + 2 * BH, height - 4 * BH, 10, 10));
win->size_range(width0, height0);
win->position(CTX::instance()->pluginPosition[0],
CTX::instance()->pluginPosition[1]);
win->end();
FL_NORMAL_SIZE += deltaFontSize;
}
void pluginWindow::show(int viewIndex)
{
resetViewBrowser();
if(viewIndex >= 0 && viewIndex < (int)PView::list.size()) {
view_browser->deselect();
view_browser->select(viewIndex + 1);
plugin_browser_cb(nullptr, nullptr);
}
win->show();
}
void pluginWindow::resetViewBrowser()
{
// save selected state
std::vector<int> state;
for(int i = 0; i < view_browser->size(); i++) {
if(view_browser->selected(i + 1))
state.push_back(1);
else
state.push_back(0);
}
char str[128];
view_browser->clear();
if(PView::list.size()) {
view_browser->activate();
for(unsigned int i = 0; i < PView::list.size(); i++) {
sprintf(str, "View [%d]", i);
view_browser->add(str);
}
for(int i = 0; i < view_browser->size(); i++) {
if(i < (int)state.size() && state[i]) view_browser->select(i + 1);
}
}
else {
view_browser->add("No Views");
view_browser->deactivate();
}
plugin_browser_cb(nullptr, nullptr);
}
|