Browse Source

Add "Set Pure Data application" menu item and open it when editing pd files.

tags/v1.3.0
Andrew Belt 4 years ago
parent
commit
157906982d
1 changed files with 60 additions and 20 deletions
  1. +60
    -20
      src/Prototype.cpp

+ 60
- 20
src/Prototype.cpp View File

@@ -28,19 +28,30 @@ ScriptEngine* createScriptEngine(std::string extension) {
} }




static std::string editorPath;
static std::string settingsEditorPath;
static std::string settingsPdEditorPath =
#if defined ARCH_LIN
"\"/usr/bin/pd\"";
#else
"";
#endif




json_t* settingsToJson() { json_t* settingsToJson() {
json_t* rootJ = json_object(); json_t* rootJ = json_object();
json_object_set_new(rootJ, "editorPath", json_string(editorPath.c_str()));
json_object_set_new(rootJ, "editorPath", json_string(settingsEditorPath.c_str()));
json_object_set_new(rootJ, "pdEditorPath", json_string(settingsPdEditorPath.c_str()));
return rootJ; return rootJ;
} }


void settingsFromJson(json_t* rootJ) { void settingsFromJson(json_t* rootJ) {
json_t* editorPathJ = json_object_get(rootJ, "editorPath"); json_t* editorPathJ = json_object_get(rootJ, "editorPath");
if (editorPathJ) if (editorPathJ)
editorPath = json_string_value(editorPathJ);
settingsEditorPath = json_string_value(editorPathJ);

json_t* pdEditorPathJ = json_object_get(rootJ, "pdEditorPath");
if (pdEditorPathJ)
settingsPdEditorPath = json_string_value(pdEditorPathJ);
} }


void settingsLoad() { void settingsLoad() {
@@ -75,27 +86,43 @@ void settingsSave() {
json_decref(rootJ); json_decref(rootJ);
} }


void setEditorDialog() {
char* editorPathC = NULL;
std::string getApplicationPathDialog() {
char* pathC = NULL;
#if defined ARCH_LIN #if defined ARCH_LIN
editorPathC = osdialog_file(OSDIALOG_OPEN, "/usr/bin/", NULL, NULL);
pathC = osdialog_file(OSDIALOG_OPEN, "/usr/bin/", NULL, NULL);
#elif defined ARCH_WIN #elif defined ARCH_WIN
osdialog_filters* filters = osdialog_filters_parse("Executable:exe"); osdialog_filters* filters = osdialog_filters_parse("Executable:exe");
editorPathC = osdialog_file(OSDIALOG_OPEN, "C:/", NULL, filters);
pathC = osdialog_file(OSDIALOG_OPEN, "C:/", NULL, filters);
osdialog_filters_free(filters); osdialog_filters_free(filters);
#elif defined ARCH_MAC #elif defined ARCH_MAC
osdialog_filters* filters = osdialog_filters_parse("Application:app"); osdialog_filters* filters = osdialog_filters_parse("Application:app");
editorPathC = osdialog_file(OSDIALOG_OPEN, "/Applications/", NULL, filters);
pathC = osdialog_file(OSDIALOG_OPEN, "/Applications/", NULL, filters);
osdialog_filters_free(filters); osdialog_filters_free(filters);
#endif #endif
if (!editorPathC)
if (!pathC)
return "";

std::string path = "\"";
path += pathC;
path += "\"";
std::free(pathC);
return path;
}

void setEditorDialog() {
std::string path = getApplicationPathDialog();
if (path == "")
return; return;
settingsEditorPath = path;
settingsSave();
}


editorPath = "\"";
editorPath += editorPathC;
editorPath += "\"";
void setPdEditorDialog() {
std::string path = getApplicationPathDialog();
if (path == "")
return;
settingsPdEditorPath = path;
settingsSave(); settingsSave();
std::free(editorPathC);
} }




@@ -463,6 +490,13 @@ struct Prototype : Module {
} }


void editScript() { void editScript() {
std::string editorPath;
// HACK check if extension is .pd
if (string::filenameExtension(string::filename(path)) == "pd")
editorPath = settingsPdEditorPath;
else
editorPath = settingsEditorPath;

if (editorPath.empty()) if (editorPath.empty())
return; return;
if (path.empty()) if (path.empty())
@@ -539,22 +573,28 @@ struct Prototype : Module {
}; };
EditScriptItem* editScriptItem = createMenuItem<EditScriptItem>("Edit script"); EditScriptItem* editScriptItem = createMenuItem<EditScriptItem>("Edit script");
editScriptItem->module = this; editScriptItem->module = this;
editScriptItem->disabled = !doesPathExist() || editorPath == "";

// TODO fix for pdEditorPath
editScriptItem->disabled = !doesPathExist() || settingsEditorPath == "" && false;
menu->addChild(editScriptItem); menu->addChild(editScriptItem);


menu->addChild(new MenuSeparator);

struct SetEditorItem : MenuItem { struct SetEditorItem : MenuItem {
void onAction(const event::Action& e) override { void onAction(const event::Action& e) override {
setEditorDialog(); setEditorDialog();
} }
}; };
SetEditorItem* setEditorItem = createMenuItem<SetEditorItem>("Set editor application");
SetEditorItem* setEditorItem = createMenuItem<SetEditorItem>("Set text editor application");
menu->addChild(setEditorItem); menu->addChild(setEditorItem);


// if (!editorPath.empty()) {
// std::string editorBase = string::filenameBase(string::filename(editorPath));
// MenuLabel* editorBaseLabel = createMenuLabel(editorBase);
// menu->addChild(editorBaseLabel);
// }
struct SetPdEditorItem : MenuItem {
void onAction(const event::Action& e) override {
setPdEditorDialog();
}
};
SetPdEditorItem* setPdEditorItem = createMenuItem<SetPdEditorItem>("Set Pure Data application");
menu->addChild(setPdEditorItem);
} }
}; };




Loading…
Cancel
Save