Skip to content

Commit 1d9230f

Browse files
ClaudiaFrankchcg
authored andcommitted
store/read user scripts relative to plugins config directory (bruderstein#99)
* store/read user scripts relative to plugins config directory Fixes bruderstein#15 * introducing shortenPathIfPossible and expandPathIfNeeded functions adding NULL checks * First step to replace chm with html help files (see bruderstein#96) removed unnecessary declaration from ConfigFile * prevent memory leaks by correctly deleting newly created array change getFiles which fixes bruderstein#98 * revert UNICODE/ANSI block changes * missing curly braces added Run edit control converted to combobox - fixes issue bruderstein#69 * add missing identifier IDC_COMBO1 * Set stdout to console as default, fixes bruderstein#78 * recommit rc changes make last command stay in editbox * slight position adjustment to RUN button * make selected combobox item stay make edit control in combobox scrolling * changing console combobox and run button position and dimensions
1 parent d54a2b4 commit 1d9230f

File tree

10 files changed

+194
-261
lines changed

10 files changed

+194
-261
lines changed

PythonScript/res/PythonScript.rc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK
2525
// TEXTINCLUDE
2626
//
2727

28-
1 TEXTINCLUDE
28+
1 TEXTINCLUDE
2929
BEGIN
3030
"resource.h\0"
3131
END
3232

33-
2 TEXTINCLUDE
33+
2 TEXTINCLUDE
3434
BEGIN
3535
"#include ""afxres.h""\r\n"
3636
"\0"
3737
END
3838

39-
3 TEXTINCLUDE
39+
3 TEXTINCLUDE
4040
BEGIN
4141
"\r\n"
4242
"\0"
@@ -68,9 +68,9 @@ STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSM
6868
CAPTION "Python Script"
6969
FONT 8, "MS Shell Dlg", 400, 0, 0x1
7070
BEGIN
71-
EDITTEXT IDC_INPUT,28,156,121,14,ES_AUTOHSCROLL | ES_WANTRETURN
72-
PUSHBUTTON "Run",IDC_RUN,152,156,40,14
73-
LTEXT ">>>",IDC_PROMPT,7,161,17,8
71+
PUSHBUTTON "Run",IDC_RUN,152,156,40,13
72+
LTEXT ">>>",IDC_PROMPT,7,158,17,11
73+
COMBOBOX IDC_COMBO1,25,156,125,50,CBS_DROPDOWN | CBS_HASSTRINGS | CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP
7474
END
7575

7676
IDD_SCRIPTCONFIG DIALOGEX 0, 0, 377, 404
@@ -204,6 +204,11 @@ BEGIN
204204
0
205205
END
206206

207+
IDD_CONSOLE AFX_DIALOG_LAYOUT
208+
BEGIN
209+
0
210+
END
211+
207212
#endif // English (United Kingdom) resources
208213
/////////////////////////////////////////////////////////////////////////////
209214

PythonScript/res/resource.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define IDI_ICON1 114
1717
#define IDD_PROMPTDIALOG 116
1818
#define IDB_PYTHONPOWERED 118
19-
#define IDC_INPUT 1001
19+
#define IDC_COMBO1 1001
2020
#define IDC_RUN 1002
2121
#define IDC_PROMPT 1003
2222
#define IDC_FILETREE 1004
@@ -43,13 +43,14 @@
4343
#define IDC_CHECKCOLORIZEOUTPUT 1023
4444
#define IDC_COLORCHOOSER 1024
4545

46+
4647
// Next default values for new objects
4748
//
4849
#ifdef APSTUDIO_INVOKED
4950
#ifndef APSTUDIO_READONLY_SYMBOLS
50-
#define _APS_NEXT_RESOURCE_VALUE 121
51+
#define _APS_NEXT_RESOURCE_VALUE 122
5152
#define _APS_NEXT_COMMAND_VALUE 40001
52-
#define _APS_NEXT_CONTROL_VALUE 1025
53+
#define _APS_NEXT_CONTROL_VALUE 1028
5354
#define _APS_NEXT_SYMED_VALUE 101
5455
#endif
5556
#endif

PythonScript/src/ConfigFile.cpp

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,31 @@ void ConfigFile::readConfig()
6262

6363
while (startupFile.good())
6464
{
65+
tstring scriptFullPath = _T("");
6566
startupFile.getline(buffer, 500);
6667
char *context;
6768
char *element = strtok_s(buffer, "/", &context);
6869
if (element)
6970
{
70-
7171
// Menu item
7272
if (0 == strcmp(element, "ITEM"))
7373
{
7474
element = strtok_s(NULL, "/", &context);
75-
m_menuItems.push_back(tstring(WcharMbcsConverter::char2tchar(element).get()));
76-
m_menuScripts.push_back(tstring(WcharMbcsConverter::char2tchar(element).get()));
75+
scriptFullPath = expandPathIfNeeded(element);
76+
if (scriptFullPath != L"")
77+
{
78+
m_menuItems.push_back(scriptFullPath);
79+
m_menuScripts.push_back(scriptFullPath);
80+
}
7781
}
7882

7983
// Toolbar item
8084
else if (0 == strcmp(element, "TOOLBAR"))
8185
{
86+
tstring iconFullPath = _T("");
8287
element = strtok_s(NULL, "/", &context);
88+
scriptFullPath = expandPathIfNeeded(element);
89+
8390
char *iconPath = strtok_s(NULL, "/", &context);
8491
if (!iconPath || !(*iconPath))
8592
{
@@ -88,11 +95,13 @@ void ConfigFile::readConfig()
8895
}
8996
else
9097
{
91-
hIcon = static_cast<HBITMAP>(LoadImage(NULL, WcharMbcsConverter::char2tchar(iconPath).get(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE));
98+
iconFullPath = expandPathIfNeeded(iconPath);
99+
hIcon = static_cast<HBITMAP>(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE));
100+
}
101+
if (scriptFullPath != L"")
102+
{
103+
m_toolbarItems.push_back(std::pair<tstring, std::pair<HBITMAP, tstring> >(scriptFullPath, std::pair<HBITMAP, tstring>(hIcon, iconPath ? iconFullPath : tstring())));
92104
}
93-
94-
95-
m_toolbarItems.push_back(std::pair<tstring, std::pair<HBITMAP, tstring> >(tstring(WcharMbcsConverter::char2tchar(element).get()), std::pair<HBITMAP, tstring>(hIcon, iconPath ? tstring(WcharMbcsConverter::char2tchar(iconPath).get()) : tstring())));
96105
}
97106
else if (0 == strcmp(element, "SETTING"))
98107
{
@@ -113,19 +122,44 @@ void ConfigFile::clearItems()
113122
m_toolbarItems.erase(m_toolbarItems.begin(), m_toolbarItems.end());
114123
}
115124

125+
tstring ConfigFile::expandPathIfNeeded(char *userPath)
126+
{
127+
tstring fullPath = L"";
128+
if (userPath)
129+
{
130+
if ((userPath[1] == ':') || (userPath[1] == '\\'))
131+
{
132+
fullPath = WcharMbcsConverter::char2tchar(userPath).get();
133+
}
134+
else
135+
{
136+
fullPath.append(m_userScriptsDir).append(tstring(WcharMbcsConverter::char2tchar(userPath).get()));
137+
}
138+
}
139+
return fullPath;
140+
}
141+
142+
std::string ConfigFile::shortenPathIfPossible(tstring userPath)
143+
{
144+
std::string userScriptsDir(WcharMbcsConverter::tchar2char((m_userScriptsDir).c_str()).get());
145+
std::string fullPath = WcharMbcsConverter::tchar2char((userPath).c_str()).get();
146+
return (fullPath.find(userScriptsDir, 0) == 0) ? fullPath.replace(0, userScriptsDir.length(), "") : fullPath;
147+
}
148+
116149
void ConfigFile::save()
117150
{
118151
//just char(UTF8) as TCHAR is not working as expected, because stream is converted to char implicitly
119152
//see also https://www.codeproject.com/Articles/38242/Reading-UTF-with-C-streams
153+
120154
std::ofstream startupFile(m_configFilename.c_str(), std::ios_base::out | std::ios_base::trunc);
121155
for(MenuItemsTD::iterator it = m_menuItems.begin(); it != m_menuItems.end(); ++it)
122156
{
123-
startupFile << "ITEM/" << WcharMbcsConverter::tchar2char((*it).c_str()).get() << "\n";
157+
startupFile << "ITEM/" << shortenPathIfPossible(*it) << "\n";
124158
}
125159

126160
for(ToolbarItemsTD::iterator it = m_toolbarItems.begin(); it != m_toolbarItems.end(); ++it)
127161
{
128-
startupFile << "TOOLBAR/" << WcharMbcsConverter::tchar2char((it->first).c_str()).get() << "/" << WcharMbcsConverter::tchar2char((it->second.second).c_str()).get() << "\n";
162+
startupFile << "TOOLBAR/" << shortenPathIfPossible(it->first) << "/" << shortenPathIfPossible(it->second.second) << "\n";
129163
}
130164

131165
for(SettingsTD::iterator it = m_settings.begin(); it != m_settings.end(); ++it)

PythonScript/src/ConfigFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class ConfigFile
3131

3232
void refresh() { clearItems(); readConfig(); };
3333

34+
std::string shortenPathIfPossible(tstring userPath);
35+
tstring expandPathIfNeeded(char *userPath);
3436

3537
const tstring& getMachineScriptsDir() { return m_machineScriptsDir; };
3638
const tstring& getUserScriptsDir() { return m_userScriptsDir; };

0 commit comments

Comments
 (0)