iPlug2 - C++ Audio Plug-in Framework
ReaperExt_include_in_plug_src.h
1 
2 #ifndef NO_IGRAPHICS
3 #define BUNDLE_ID ""
4 #include "IGraphics_include_in_plug_src.h"
5 #endif
6 
7 #define REAPERAPI_IMPLEMENT
8 void (*AttachWindowTopmostButton)(HWND hwnd);
9 #include "reaper_plugin_functions.h"
10 
11 #include "resource.h"
12 #include <vector>
13 #include <map>
14 
15 REAPER_PLUGIN_HINSTANCE gHINSTANCE;
16 HWND gParent;
17 HWND gHWND = NULL;
18 std::unique_ptr<PLUG_CLASS_NAME> gPlug;
19 RECT gPrevBounds;
20 int gErrorCount = 0;
21 
24 {
25  int* pToggle = nullptr;
26  gaccel_register_t accel = {{0,0,0}, ""};
27  std::function<void()> func;
28  bool addMenuItem = false;
29 };
30 
31 std::vector<ReaperAction> gActions;
32 
33 //TODO: don't #include cpp here
34 #include "ReaperExtBase.cpp"
35 
36 // super nasty looking macro here but allows importing functions from Reaper with simple looking code
37 #define IMPAPI(x) if (!((*((void **)&(x)) = (void *)pRec->GetFunc(#x)))) gErrorCount++;
38 
39 #pragma mark - ENTRY POINT
40 extern "C"
41 {
42  REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(REAPER_PLUGIN_HINSTANCE hInstance, reaper_plugin_info_t* pRec)
43  {
44  gHINSTANCE = hInstance;
45 
46  if (pRec)
47  {
48  if (pRec->caller_version != REAPER_PLUGIN_VERSION || !pRec->GetFunc)
49  return 0;
50 
51  gPlug = std::make_unique<PLUG_CLASS_NAME>(pRec);
52 
53  // initialize API function pointers from Reaper
54  IMPAPI(Main_OnCommand);
55  IMPAPI(GetResourcePath);
56  IMPAPI(AddExtensionsMainMenu);
57  IMPAPI(AttachWindowTopmostButton);
58  IMPAPI(ShowConsoleMsg);
59  IMPAPI(DockWindowAdd);
60  IMPAPI(DockWindowActivate);
61 
62  if (gErrorCount > 0)
63  return 0;
64 
65  pRec->Register("hookcommand", (void*) ReaperExtBase::HookCommandProc);
66  pRec->Register("toggleaction", (void*) ReaperExtBase::ToggleActionCallback);
67 
68  AddExtensionsMainMenu();
69 
70  gParent = pRec->hwnd_main;
71 
72  HMENU hMenu = GetSubMenu(GetMenu(gParent),
73 #ifdef OS_WIN
74  8
75 #else // OS X has one extra menu
76  9
77 #endif
78  );
79 
80  int menuIdx = 6;
81 
82  for(auto& action : gActions)
83  {
84  if(action.addMenuItem)
85  {
86  MENUITEMINFO mi={sizeof(MENUITEMINFO),};
87  mi.fMask = MIIM_TYPE | MIIM_ID;
88  mi.fType = MFT_STRING;
89  mi.dwTypeData = LPSTR(action.accel.desc);
90  mi.wID = action.accel.accel.cmd;
91  InsertMenuItem(hMenu, menuIdx++, TRUE, &mi);
92  }
93  }
94 
95  return 1;
96  }
97  else
98  {
99  return 0;
100  }
101  }
102 };
103 
104 
105 #ifndef OS_WIN
106 #define SWELL_DLG_FLAGS_AUTOGEN SWELL_DLG_WS_FLIPPED//|SWELL_DLG_WS_RESIZABLE
107 #include "swell-dlggen.h"
108 #include "main.rc_mac_dlg"
109 #undef BEGIN
110 #undef END
111 #include "swell-menugen.h"
112 #include "main.rc_mac_menu"
113 #else
114 
115 UINT(WINAPI* __GetDpiForWindow)(HWND);
116 
117 float GetScaleForHWND(HWND hWnd)
118 {
119  if (!__GetDpiForWindow)
120  {
121  HINSTANCE h = LoadLibraryA("user32.dll");
122  if (h) *(void**)&__GetDpiForWindow = GetProcAddress(h, "GetDpiForWindow");
123 
124  if (!__GetDpiForWindow)
125  return 1;
126  }
127 
128  int dpi = __GetDpiForWindow(hWnd);
129 
130  if (dpi != USER_DEFAULT_SCREEN_DPI)
131  return static_cast<float>(dpi) / USER_DEFAULT_SCREEN_DPI;
132 
133  return 1;
134 }
135 
136 #endif
Helper struct for registering Reaper Actions.