forked from reaper-oss/sws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sws_util.cpp
555 lines (499 loc) · 14.2 KB
/
sws_util.cpp
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/******************************************************************************
/ sws_util.cpp
/
/ Copyright (c) 2010 and later Tim Payne (SWS), Jeffos
/
/
/ Permission is hereby granted, free of charge, to any person obtaining a copy
/ of this software and associated documentation files (the "Software"), to deal
/ in the Software without restriction, including without limitation the rights to
/ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
/ of the Software, and to permit persons to whom the Software is furnished to
/ do so, subject to the following conditions:
/
/ The above copyright notice and this permission notice shall be included in all
/ copies or substantial portions of the Software.
/
/ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
/ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
/ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
/ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
/ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
/ OTHER DEALINGS IN THE SOFTWARE.
/
******************************************************************************/
#include "stdafx.h"
#include "Breeder/BR_Util.h"
#include "WDL/sha.h"
#include "reaper/localize.h"
// Globals
double g_d0 = 0.0;
int g_i0 = 0;
int g_i1 = 1;
int g_i2 = 2;
bool g_bTrue = true;
bool g_bFalse = false;
MTRand g_MTRand;
extern double g_runningReaVer; // SnM_Project.cpp/GlobalStartupActionTimer()
#ifndef _WIN32
const GUID GUID_NULL = { 0, 0, 0, "\0\0\0\0\0\0\0" };
#endif
BOOL IsCommCtrlVersion6()
{
#ifndef _WIN32
return true;
#else
static BOOL isCommCtrlVersion6 = -1;
if (isCommCtrlVersion6 != -1)
return isCommCtrlVersion6;
//The default value
isCommCtrlVersion6 = FALSE;
HINSTANCE commCtrlDll = LoadLibrary("comctl32.dll");
if (commCtrlDll)
{
DLLGETVERSIONPROC pDllGetVersion;
pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(commCtrlDll, "DllGetVersion");
if (pDllGetVersion)
{
DLLVERSIONINFO dvi = {0};
dvi.cbSize = sizeof(DLLVERSIONINFO);
(*pDllGetVersion)(&dvi);
isCommCtrlVersion6 = (dvi.dwMajorVersion == 6);
}
FreeLibrary(commCtrlDll);
}
return isCommCtrlVersion6;
#endif
}
void SaveWindowPos(HWND hwnd, const char* cKey)
{
// Remember the dialog position
char str[256];
RECT r;
GetWindowRect(hwnd, &r);
sprintf(str, "%d %d %d %d", (int)r.left, (int)r.top, (int)(r.right-r.left), (int)(r.bottom-r.top));
WritePrivateProfileString(SWS_INI, cKey, str, get_ini_file());
}
void RestoreWindowPos(HWND hwnd, const char* cKey, bool bRestoreSize)
{
char str[256];
GetPrivateProfileString(SWS_INI, cKey, "unknown", str, 256,get_ini_file());
LineParser lp(false);
if (!lp.parse(str) && lp.getnumtokens() == 4)
{
RECT r;
r.left = lp.gettoken_int(0);
r.top = lp.gettoken_int(1);
r.right = lp.gettoken_int(0) + lp.gettoken_int(2);
r.bottom = lp.gettoken_int(1) + lp.gettoken_int(3);
EnsureNotCompletelyOffscreen(&r);
if (bRestoreSize)
SetWindowPos(hwnd, NULL, r.left, r.top, r.right-r.left, r.bottom-r.top, SWP_NOZORDER);
else
SetWindowPos(hwnd, NULL, r.left, r.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
}
void SetWindowPosAtMouse(HWND hwnd)
{
RECT r;
GetWindowRect(hwnd, &r);
POINT p;
GetCursorPos(&p);
r.right = p.x + abs(r.right-r.left);
r.bottom = p.y + abs(r.bottom-r.top);
r.left = p.x;
r.top = p.y;
EnsureNotCompletelyOffscreen(&r);
SetWindowPos(hwnd, NULL, r.left, r.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
#ifdef _WIN32
void SWS_ShowTextScrollbar(HWND hwnd, bool show)
{
DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
if (!show) dwStyle &= ~WS_HSCROLL;
else dwStyle |= WS_HSCROLL;
SetWindowLong(hwnd, GWL_STYLE, dwStyle);
}
#else
int GetMenuString(HMENU hMenu, UINT uIDItem, char* lpString, int nMaxCount, UINT uFlag)
{
if (hMenu && lpString)
{
MENUITEMINFO xInfo;
xInfo.cbSize = sizeof(MENUITEMINFO);
/* On Win OS
xInfo.fMask = MIIM_STRING;
*/
xInfo.fMask = MIIM_TYPE; // ok on OSX/SWELL
xInfo.fType = MFT_STRING;
xInfo.fState = 0;
xInfo.wID = 0;
xInfo.hSubMenu = NULL;
xInfo.hbmpChecked = NULL;
xInfo.hbmpUnchecked = NULL;
xInfo.dwItemData = 0;
xInfo.dwTypeData = lpString;
xInfo.cch = nMaxCount;
if (GetMenuItemInfo(hMenu, uIDItem, (uFlag&MF_BYPOSITION) == MF_BYPOSITION, &xInfo))
return strlen(lpString);
}
return 0;
}
#endif
MediaTrack* GetFirstSelectedTrack()
{
for (int j = 0; j <= GetNumTracks(); j++)
{
MediaTrack* tr = CSurf_TrackFromID(j, false);
if (tr && *(int*)GetSetMediaTrackInfo(tr, "I_SELECTED", NULL))
return tr;
}
return NULL;
}
// NumSelTracks takes the master track into account (contrary to CountSelectedTracks())
int NumSelTracks()
{
int iCount = 0;
for (int i = 0; i <= GetNumTracks(); i++)
{
MediaTrack* tr = CSurf_TrackFromID(i, false);
if (*(int*)GetSetMediaTrackInfo(tr, "I_SELECTED", NULL))
iCount++;
}
return iCount;
}
static MediaTrack** g_pSelTracks = NULL;
static int g_iNumSel = 0;
void SaveSelected()
{
if (g_pSelTracks)
{
delete [] g_pSelTracks;
g_pSelTracks=NULL;
}
g_iNumSel = NumSelTracks();
if (g_iNumSel)
{
g_pSelTracks = new MediaTrack*[g_iNumSel];
int i = 0;
for (int j = 0; j <= GetNumTracks(); j++)
if (*(int*)GetSetMediaTrackInfo(CSurf_TrackFromID(j, false), "I_SELECTED", NULL))
g_pSelTracks[i++] = CSurf_TrackFromID(j, false);
}
}
void RestoreSelected()
{
if (!g_pSelTracks) return;
int iSel = 1;
for (int i = 0; i < g_iNumSel; i++)
if (CSurf_TrackToID(g_pSelTracks[i], false) >= 0)
GetSetMediaTrackInfo(g_pSelTracks[i], "I_SELECTED", &iSel);
}
void ClearSelected()
{
int iSel = 0;
for (int i = 0; i <= GetNumTracks(); i++)
GetSetMediaTrackInfo(CSurf_TrackFromID(i, false), "I_SELECTED", &iSel);
}
int GetFolderDepth(MediaTrack* tr, int* iType, MediaTrack** nextTr) // iType: 0=normal, 1=parent, < 0=last in folder
{
//static MediaTrack* nextTr = NULL; Let calling fcn keep track so state is reset appropriately
static int iLastFolder = 0;
int iFolder;
if (tr == *nextTr)
{
*nextTr = CSurf_TrackFromID(CSurf_TrackToID(tr, false) + 1, false);
if (CSurf_TrackToID(tr, false) == 0) // Special case for master
{
if (iType)
*iType = 1;
return -1; // Master is at '-1' depth
}
iFolder = *(int*)GetSetMediaTrackInfo(tr, "I_FOLDERDEPTH", NULL);
if (iType)
*iType = iFolder;
if (iFolder == 0)
return iLastFolder;
else if (iFolder == 1)
{
iLastFolder++;
return iLastFolder - 1; // Count parent as at the "previous" level
}
else if (iFolder < 0)
{
int iCur = iLastFolder;
iLastFolder += iFolder;
return iCur;
}
}
else
{
// Must iterate until reaching the track passed in
iLastFolder = 0;
*nextTr = CSurf_TrackFromID(0, false);
while (tr != *nextTr)
GetFolderDepth(*nextTr, NULL, nextTr);
return GetFolderDepth(tr, iType, nextTr);
}
return -1;
}
int GetTrackVis(MediaTrack* tr) // &1 == mcp, &2 == tcp
{
int iTrack = CSurf_TrackToID(tr, false);
if (iTrack == 0)
return *ConfigVar<int>("showmaintrack") ? 3 : 1; // For now, always return master vis in MCP
else if (iTrack < 0)
return 0;
int iVis = *(bool*)GetSetMediaTrackInfo(tr, "B_SHOWINMIXER", NULL) ? 1 : 0;
iVis |= *(bool*)GetSetMediaTrackInfo(tr, "B_SHOWINTCP", NULL) ? 2 : 0;
return iVis;
}
void SetTrackVis(MediaTrack* tr, int vis) // &1 == mcp, &2 == tcp
{
int iTrack = CSurf_TrackToID(tr, false);
if (iTrack == 0)
{ // TODO - obey master in mcp
if ((vis & 2) != (*ConfigVar<int>("showmaintrack") ? 2 : 0))
Main_OnCommand(40075, 0);
}
else if (iTrack > 0)
{
if (vis != GetTrackVis(tr))
{
GetSetMediaTrackInfo(tr, "B_SHOWINTCP", vis & 2 ? &g_bTrue : &g_bFalse);
GetSetMediaTrackInfo(tr, "B_SHOWINMIXER", vis & 1 ? &g_bTrue : &g_bFalse);
}
}
}
HWND GetTrackWnd()
{
return GetArrangeWnd(); // BR: will take care of any localization issues
}
HWND GetRulerWnd()
{
return GetRulerWndAlt(); // BR: will take care of any localization issues
}
// overrides the native GetTrackGUID(): returns a special GUID for the master track
// (useful for persistence as no GUID is stored in RPP files for the master..)
// note: TrackToGuid(NULL) will return also NULL, contrary to GetTrackGUID(NULL)
const GUID* TrackToGuid(MediaTrack* tr)
{
if (tr)
{
if (tr == GetMasterTrack(NULL))
return &GUID_NULL;
else
return GetTrackGUID(tr);
}
return NULL;
}
MediaTrack* GuidToTrack(const GUID* guid)
{
if (guid)
for (int i=0; i<=GetNumTracks(); i++)
if (MediaTrack* tr = CSurf_TrackFromID(i, false))
if (TrackMatchesGuid(tr, guid))
return tr;
return NULL;
}
bool GuidsEqual(const GUID* g1, const GUID* g2)
{
return g1 && g2 && !memcmp(g1, g2, sizeof(GUID));
}
bool TrackMatchesGuid(MediaTrack* tr, const GUID* g)
{
if (tr && g)
{
if (GuidsEqual(GetTrackGUID(tr), g))
return true;
// 2nd try for the master
if (tr == GetMasterTrack(NULL))
return GuidsEqual(g, &GUID_NULL);
}
return false;
}
const char *stristr(const char* a, const char* b)
{
int i;
int len = strlen(b);
int n = strlen(a)-len;
for (i = 0; i <= n; ++i)
{
#ifdef _WIN32
if (!_strnicmp(a+i, b, len)) return a+i;
#else
if (!strnicmp(a+i, b, len)) return a+i;
#endif
}
return NULL;
}
#ifdef _WIN32
void dprintf(const char* format, ...)
{
va_list args;
va_start(args, format);
int len = _vscprintf(format, args) + 1;
char* buffer = new char[len];
vsprintf_s(buffer, len, format, args); // C4996
OutputDebugString(buffer);
delete[] buffer;
}
#endif
void SWS_GetAllTracks(WDL_TypedBuf<MediaTrack*>* buf, bool bMaster)
{
buf->Resize(0);
for (int i = (bMaster ? 0 : 1); i <= GetNumTracks(); i++)
{
MediaTrack* tr = CSurf_TrackFromID(i, false);
int pos = buf->GetSize();
buf->Resize(pos + 1);
buf->Get()[pos] = tr;
}
}
void SWS_GetSelectedTracks(WDL_TypedBuf<MediaTrack*>* buf, bool bMaster)
{
buf->Resize(0);
for (int i = (bMaster ? 0 : 1); i <= GetNumTracks(); i++)
{
MediaTrack* tr = CSurf_TrackFromID(i, false);
if (*(int*)GetSetMediaTrackInfo(tr, "I_SELECTED", NULL))
{
int pos = buf->GetSize();
buf->Resize(pos + 1);
buf->Get()[pos] = tr;
}
}
}
void SWS_GetSelectedMediaItems(WDL_TypedBuf<MediaItem*>* buf)
{
buf->Resize(0);
for (int i = 1; i <= GetNumTracks(); i++)
{
MediaTrack* tr = CSurf_TrackFromID(i, false);
for (int j = 0; j < GetTrackNumMediaItems(tr); j++)
{
MediaItem* item = GetTrackMediaItem(tr, j);
if (*(bool*)GetSetMediaItemInfo(item, "B_UISEL", NULL))
{
int pos = buf->GetSize();
buf->Resize(pos + 1);
buf->Get()[pos] = item;
}
}
}
}
void SWS_GetSelectedMediaItemsOnTrack(WDL_TypedBuf<MediaItem*>* buf, MediaTrack* tr)
{
buf->Resize(0);
if (CSurf_TrackToID(tr, false) <= 0)
return;
for (int j = 0; j < GetTrackNumMediaItems(tr); j++)
{
MediaItem* item = GetTrackMediaItem(tr, j);
if (*(bool*)GetSetMediaItemInfo(item, "B_UISEL", NULL))
{
int pos = buf->GetSize();
buf->Resize(pos + 1);
buf->Get()[pos] = item;
}
}
}
int SWS_GetModifiers()
{
int iKeys = GetAsyncKeyState(VK_CONTROL) & 0x8000 ? SWS_CTRL : 0;
iKeys |= GetAsyncKeyState(VK_MENU) & 0x8000 ? SWS_ALT : 0;
iKeys |= GetAsyncKeyState(VK_SHIFT) & 0x8000 ? SWS_SHIFT : 0;
return iKeys;
}
MODIFIER g_modifiers[NUM_MODIFIERS] =
{
{ 0, "None" },
{ 2, "Ctrl" },
{ 1, "Alt" },
{ 4, "Shift" },
{ 3, "Ctrl + Alt" },
{ 6, "Ctrl + Shift" },
{ 5, "Alt + Shift" },
{ 7, "Ctrl + Alt + Shift" }
};
bool SWS_IsWindow(HWND hwnd)
{
#ifdef _WIN32
return IsWindow(hwnd) ? true : false;
#else
// ISSUE 498: OSX IsWindow is broken for docked windows!
// Justin recommends not using IsWindow at all, but there are a lot of cases
// where we rely on it for error checking. Instead of removing all calls and
// doing internal window state handling I replaced all IsWindow calls with
// this function that checks for docked windows on OSX. It's a bit of a
// hack, but less risky IMO than rewriting tons of window handling code.
//
// Maybe could replace with return hwnd != NULL;
return (bool)IsWindow(hwnd) ? true : (DockIsChildOfDock(hwnd, NULL) != -1);
#endif
}
// Localization
WDL_FastString* g_LangPack = NULL;
// Never returns NULL ("" means no langpack defined)
WDL_FastString* GetLangPack()
{
// lazy init, works because swtching LangPacks requires REAPER restart
if (!g_LangPack)
{
g_LangPack = new WDL_FastString;
#ifdef _SWS_LOCALIZATION
char fn[BUFFER_SIZE] = "";
GetPrivateProfileString("REAPER", "langpack", "", fn, sizeof(fn), get_ini_file());
if (*fn && strcmp(fn, "<>"))
{
g_LangPack->Set(fn);
if (!FileExists(fn)) // retry with fn as "short name"
{
g_LangPack->SetFormatted(BUFFER_SIZE, "%s%cLangPack%c%s", GetResourcePath(), PATH_SLASH_CHAR, PATH_SLASH_CHAR, fn);
if (!FileExists(g_LangPack->Get())) // does not exist either..
g_LangPack->Set("");
}
}
#endif
}
return g_LangPack;
}
bool IsLocalized()
{
#ifdef _SWS_LOCALIZATION
return (GetLangPack()->GetLength() > 0);
#else
return false;
#endif
}
// check that action tags ("SWS: ", "SWS/FNG: ", SWS/S&M: ", etc..) are preserved
const char* GetLocalizedActionName(const char* _defaultStr, int _flags, const char* _section)
{
#ifdef _SWS_LOCALIZATION
const char* p = __localizeFunc(_defaultStr, _section, _flags);
if (IsSwsAction(p))
return p;
#endif
return _defaultStr;
}
bool IsLocalizableAction(const char* _customId) {
return (!strstr(_customId, "_CYCLACTION") && !strstr(_customId, "_SWSCONSOLE_CUST"));
}
// wrappers to ignore localized envelope names
TrackEnvelope* SWS_GetTakeEnvelopeByName(MediaItem_Take* take, const char* envname) {
return GetTakeEnvelopeByName(take, __localizeFunc(envname, "item", 0));
}
TrackEnvelope* SWS_GetTrackEnvelopeByName(MediaTrack* track, const char* envname) {
return GetTrackEnvelopeByName(track, __localizeFunc(envname, "envname", 0));
}
void UpdateStretchMarkersAfterSetTakeStartOffset(MediaItem_Take* take, double takeStartOffset_multiplyPlayrate)
{
for (int i = 0; i < GetTakeNumStretchMarkers(take); i++) {
double posOut;
GetTakeStretchMarker(take, i, &posOut, NULL);
SetTakeStretchMarker(take, i, posOut + takeStartOffset_multiplyPlayrate, NULL);
}
UpdateItemInProject(GetMediaItemTake_Item(take));
}