DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1467 lines
36KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // we need this for now
  17. //#define PUGL_GRAB_FOCUS 1
  18. #include "../Base.hpp"
  19. #include "pugl/pugl.h"
  20. #if defined(__GNUC__) && (__GNUC__ >= 7)
  21. # pragma GCC diagnostic push
  22. # pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
  23. #endif
  24. #if defined(DISTRHO_OS_WINDOWS)
  25. # include "pugl/pugl_win.cpp"
  26. # undef max
  27. # undef min
  28. #elif defined(DISTRHO_OS_MAC)
  29. # define PuglWindow DISTRHO_JOIN_MACRO(PuglWindow, DGL_NAMESPACE)
  30. # define PuglOpenGLView DISTRHO_JOIN_MACRO(PuglOpenGLView, DGL_NAMESPACE)
  31. # include "pugl/pugl_osx.m"
  32. #else
  33. # include <sys/types.h>
  34. # include <unistd.h>
  35. extern "C" {
  36. # include "pugl/pugl_x11.c"
  37. }
  38. #endif
  39. #if defined(__GNUC__) && (__GNUC__ >= 7)
  40. # pragma GCC diagnostic pop
  41. #endif
  42. #include "ApplicationPrivateData.hpp"
  43. #include "WidgetPrivateData.hpp"
  44. #include "../StandaloneWindow.hpp"
  45. #include "../../distrho/extra/String.hpp"
  46. #define FOR_EACH_WIDGET(it) \
  47. for (std::list<Widget*>::iterator it = fWidgets.begin(); it != fWidgets.end(); ++it)
  48. #define FOR_EACH_WIDGET_INV(rit) \
  49. for (std::list<Widget*>::reverse_iterator rit = fWidgets.rbegin(); rit != fWidgets.rend(); ++rit)
  50. #ifdef DEBUG
  51. # define DBG(msg) std::fprintf(stderr, "%s", msg);
  52. # define DBGp(...) std::fprintf(stderr, __VA_ARGS__);
  53. # define DBGF std::fflush(stderr);
  54. #else
  55. # define DBG(msg)
  56. # define DBGp(...)
  57. # define DBGF
  58. #endif
  59. START_NAMESPACE_DGL
  60. // -----------------------------------------------------------------------
  61. // Window Private
  62. struct Window::PrivateData {
  63. PrivateData(Application& app, Window* const self)
  64. : fApp(app),
  65. fSelf(self),
  66. fView(puglInit()),
  67. fFirstInit(true),
  68. fVisible(false),
  69. fResizable(true),
  70. fUsingEmbed(false),
  71. fWidth(1),
  72. fHeight(1),
  73. fScaling(1.0),
  74. fTitle(nullptr),
  75. fWidgets(),
  76. fModal(),
  77. #if defined(DISTRHO_OS_WINDOWS)
  78. hwnd(nullptr),
  79. hwndParent(nullptr)
  80. #elif defined(DISTRHO_OS_MAC)
  81. fNeedsIdle(true),
  82. mView(nullptr),
  83. mWindow(nullptr),
  84. mParentWindow(nullptr)
  85. #else
  86. xDisplay(nullptr),
  87. xWindow(0)
  88. #endif
  89. {
  90. DBG("Creating window without parent..."); DBGF;
  91. init();
  92. }
  93. PrivateData(Application& app, Window* const self, Window& parent)
  94. : fApp(app),
  95. fSelf(self),
  96. fView(puglInit()),
  97. fFirstInit(true),
  98. fVisible(false),
  99. fResizable(true),
  100. fUsingEmbed(false),
  101. fWidth(1),
  102. fHeight(1),
  103. fScaling(1.0),
  104. fTitle(nullptr),
  105. fWidgets(),
  106. fModal(parent.pData),
  107. #if defined(DISTRHO_OS_WINDOWS)
  108. hwnd(nullptr),
  109. hwndParent(nullptr)
  110. #elif defined(DISTRHO_OS_MAC)
  111. fNeedsIdle(false),
  112. mView(nullptr),
  113. mWindow(nullptr),
  114. mParentWindow(nullptr)
  115. #else
  116. xDisplay(nullptr),
  117. xWindow(0)
  118. #endif
  119. {
  120. DBG("Creating window with parent..."); DBGF;
  121. init();
  122. const PuglInternals* const parentImpl(parent.pData->fView->impl);
  123. // NOTE: almost a 1:1 copy of setTransientWinId()
  124. #if defined(DISTRHO_OS_WINDOWS)
  125. hwndParent = parentImpl->hwnd;
  126. SetWindowLongPtr(hwnd, GWLP_HWNDPARENT, (LONG_PTR)hwndParent);
  127. #elif defined(DISTRHO_OS_MAC)
  128. mParentWindow = parentImpl->window;
  129. #else
  130. XSetTransientForHint(xDisplay, xWindow, parentImpl->win);
  131. #endif
  132. }
  133. PrivateData(Application& app, Window* const self, const intptr_t parentId)
  134. : fApp(app),
  135. fSelf(self),
  136. fView(puglInit()),
  137. fFirstInit(true),
  138. fVisible(parentId != 0),
  139. fResizable(parentId == 0),
  140. fUsingEmbed(parentId != 0),
  141. fWidth(1),
  142. fHeight(1),
  143. fScaling(1.0),
  144. fTitle(nullptr),
  145. fWidgets(),
  146. fModal(),
  147. #if defined(DISTRHO_OS_WINDOWS)
  148. hwnd(nullptr),
  149. hwndParent(nullptr)
  150. #elif defined(DISTRHO_OS_MAC)
  151. fNeedsIdle(parentId == 0),
  152. mView(nullptr),
  153. mWindow(nullptr),
  154. mParentWindow(nullptr)
  155. #else
  156. xDisplay(nullptr),
  157. xWindow(0)
  158. #endif
  159. {
  160. if (fUsingEmbed)
  161. {
  162. DBG("Creating embedded window..."); DBGF;
  163. puglInitWindowParent(fView, parentId);
  164. }
  165. else
  166. {
  167. DBG("Creating window without parent..."); DBGF;
  168. }
  169. init();
  170. if (fUsingEmbed)
  171. {
  172. DBG("NOTE: Embed window is always visible and non-resizable\n");
  173. puglShowWindow(fView);
  174. fApp.pData->oneShown();
  175. fFirstInit = false;
  176. }
  177. }
  178. void init()
  179. {
  180. if (fSelf == nullptr || fView == nullptr)
  181. {
  182. DBG("Failed!\n");
  183. return;
  184. }
  185. puglInitUserResizable(fView, fResizable);
  186. puglInitWindowSize(fView, static_cast<int>(fWidth), static_cast<int>(fHeight));
  187. puglSetHandle(fView, this);
  188. puglSetDisplayFunc(fView, onDisplayCallback);
  189. puglSetKeyboardFunc(fView, onKeyboardCallback);
  190. puglSetMotionFunc(fView, onMotionCallback);
  191. puglSetMouseFunc(fView, onMouseCallback);
  192. puglSetScrollFunc(fView, onScrollCallback);
  193. puglSetSpecialFunc(fView, onSpecialCallback);
  194. puglSetReshapeFunc(fView, onReshapeCallback);
  195. puglSetCloseFunc(fView, onCloseCallback);
  196. #ifndef DGL_FILE_BROWSER_DISABLED
  197. puglSetFileSelectedFunc(fView, fileBrowserSelectedCallback);
  198. #endif
  199. puglCreateWindow(fView, nullptr);
  200. PuglInternals* impl = fView->impl;
  201. #if defined(DISTRHO_OS_WINDOWS)
  202. hwnd = impl->hwnd;
  203. DISTRHO_SAFE_ASSERT(hwnd != 0);
  204. #elif defined(DISTRHO_OS_MAC)
  205. mView = impl->glview;
  206. mWindow = impl->window;
  207. DISTRHO_SAFE_ASSERT(mView != nullptr);
  208. if (fUsingEmbed) {
  209. DISTRHO_SAFE_ASSERT(mWindow == nullptr);
  210. } else {
  211. DISTRHO_SAFE_ASSERT(mWindow != nullptr);
  212. }
  213. #else
  214. xDisplay = impl->display;
  215. xWindow = impl->win;
  216. DISTRHO_SAFE_ASSERT(xWindow != 0);
  217. if (! fUsingEmbed)
  218. {
  219. const pid_t pid = getpid();
  220. const Atom _nwp = XInternAtom(xDisplay, "_NET_WM_PID", False);
  221. XChangeProperty(xDisplay, xWindow, _nwp, XA_CARDINAL, 32, PropModeReplace, (const uchar*)&pid, 1);
  222. const Atom _wt = XInternAtom(xDisplay, "_NET_WM_WINDOW_TYPE", False);
  223. // Setting the window to both dialog and normal will produce a decorated floating dialog
  224. // Order is important: DIALOG needs to come before NORMAL
  225. const Atom _wts[2] = {
  226. XInternAtom(xDisplay, "_NET_WM_WINDOW_TYPE_DIALOG", False),
  227. XInternAtom(xDisplay, "_NET_WM_WINDOW_TYPE_NORMAL", False)
  228. };
  229. XChangeProperty(xDisplay, xWindow, _wt, XA_ATOM, 32, PropModeReplace, (const uchar*)&_wts, 2);
  230. }
  231. #endif
  232. puglEnterContext(fView);
  233. fApp.pData->windows.push_back(fSelf);
  234. DBG("Success!\n");
  235. }
  236. ~PrivateData()
  237. {
  238. DBG("Destroying window..."); DBGF;
  239. if (fModal.enabled)
  240. {
  241. exec_fini();
  242. close();
  243. }
  244. fWidgets.clear();
  245. if (fUsingEmbed)
  246. {
  247. puglHideWindow(fView);
  248. fApp.pData->oneHidden();
  249. }
  250. if (fSelf != nullptr)
  251. {
  252. fApp.pData->windows.remove(fSelf);
  253. fSelf = nullptr;
  254. }
  255. if (fView != nullptr)
  256. {
  257. puglDestroy(fView);
  258. fView = nullptr;
  259. }
  260. if (fTitle != nullptr)
  261. {
  262. std::free(fTitle);
  263. fTitle = nullptr;
  264. }
  265. #if defined(DISTRHO_OS_WINDOWS)
  266. hwnd = 0;
  267. #elif defined(DISTRHO_OS_MAC)
  268. mView = nullptr;
  269. mWindow = nullptr;
  270. #else
  271. xDisplay = nullptr;
  272. xWindow = 0;
  273. #endif
  274. DBG("Success!\n");
  275. }
  276. // -------------------------------------------------------------------
  277. void close()
  278. {
  279. DBG("Window close\n");
  280. if (fUsingEmbed)
  281. return;
  282. setVisible(false);
  283. if (! fFirstInit)
  284. {
  285. fApp.pData->oneHidden();
  286. fFirstInit = true;
  287. }
  288. }
  289. void exec(const bool lockWait)
  290. {
  291. DBG("Window exec\n");
  292. exec_init();
  293. if (lockWait)
  294. {
  295. for (; fVisible && fModal.enabled;)
  296. {
  297. idle();
  298. d_msleep(10);
  299. }
  300. exec_fini();
  301. }
  302. else
  303. {
  304. idle();
  305. }
  306. }
  307. // -------------------------------------------------------------------
  308. void exec_init()
  309. {
  310. DBG("Window modal loop starting..."); DBGF;
  311. DISTRHO_SAFE_ASSERT_RETURN(fModal.parent != nullptr, setVisible(true));
  312. fModal.enabled = true;
  313. fModal.parent->fModal.childFocus = this;
  314. fModal.parent->setVisible(true);
  315. setVisible(true);
  316. DBG("Ok\n");
  317. }
  318. void exec_fini()
  319. {
  320. DBG("Window modal loop stopping..."); DBGF;
  321. fModal.enabled = false;
  322. if (fModal.parent != nullptr)
  323. {
  324. fModal.parent->fModal.childFocus = nullptr;
  325. // the mouse position probably changed since the modal appeared,
  326. // so send a mouse motion event to the modal's parent window
  327. #if defined(DISTRHO_OS_WINDOWS)
  328. // TODO
  329. #elif defined(DISTRHO_OS_MAC)
  330. // TODO
  331. #else
  332. int i, wx, wy;
  333. uint u;
  334. ::Window w;
  335. if (XQueryPointer(fModal.parent->xDisplay, fModal.parent->xWindow, &w, &w, &i, &i, &wx, &wy, &u) == True)
  336. fModal.parent->onPuglMotion(wx, wy);
  337. #endif
  338. }
  339. DBG("Ok\n");
  340. }
  341. // -------------------------------------------------------------------
  342. void focus()
  343. {
  344. DBG("Window focus\n");
  345. #if defined(DISTRHO_OS_WINDOWS)
  346. SetForegroundWindow(hwnd);
  347. SetActiveWindow(hwnd);
  348. SetFocus(hwnd);
  349. #elif defined(DISTRHO_OS_MAC)
  350. if (mWindow != nullptr)
  351. [mWindow makeKeyWindow];
  352. #else
  353. XRaiseWindow(xDisplay, xWindow);
  354. XSetInputFocus(xDisplay, xWindow, RevertToPointerRoot, CurrentTime);
  355. XFlush(xDisplay);
  356. #endif
  357. }
  358. // -------------------------------------------------------------------
  359. void setVisible(const bool yesNo)
  360. {
  361. if (fVisible == yesNo)
  362. {
  363. DBG("Window setVisible matches current state, ignoring request\n");
  364. return;
  365. }
  366. if (fUsingEmbed)
  367. {
  368. DBG("Window setVisible cannot be called when embedded\n");
  369. return;
  370. }
  371. DBG("Window setVisible called\n");
  372. fVisible = yesNo;
  373. if (yesNo && fFirstInit)
  374. setSize(fWidth, fHeight, true);
  375. #if defined(DISTRHO_OS_WINDOWS)
  376. if (yesNo)
  377. {
  378. if (fFirstInit)
  379. {
  380. RECT rectChild, rectParent;
  381. if (hwndParent != nullptr &&
  382. GetWindowRect(hwnd, &rectChild) &&
  383. GetWindowRect(hwndParent, &rectParent))
  384. {
  385. SetWindowPos(hwnd, hwndParent,
  386. rectParent.left + (rectChild.right-rectChild.left)/2,
  387. rectParent.top + (rectChild.bottom-rectChild.top)/2,
  388. 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE);
  389. }
  390. else
  391. {
  392. ShowWindow(hwnd, SW_SHOWNORMAL);
  393. }
  394. }
  395. else
  396. {
  397. ShowWindow(hwnd, SW_RESTORE);
  398. }
  399. }
  400. else
  401. {
  402. ShowWindow(hwnd, SW_HIDE);
  403. }
  404. UpdateWindow(hwnd);
  405. #elif defined(DISTRHO_OS_MAC)
  406. if (yesNo)
  407. {
  408. if (mWindow != nullptr)
  409. {
  410. if (mParentWindow != nullptr)
  411. [mParentWindow addChildWindow:mWindow
  412. ordered:NSWindowAbove];
  413. [mWindow setIsVisible:YES];
  414. }
  415. else
  416. {
  417. [mView setHidden:NO];
  418. }
  419. }
  420. else
  421. {
  422. if (mWindow != nullptr)
  423. {
  424. if (mParentWindow != nullptr)
  425. [mParentWindow removeChildWindow:mWindow];
  426. [mWindow setIsVisible:NO];
  427. }
  428. else
  429. {
  430. [mView setHidden:YES];
  431. }
  432. }
  433. #else
  434. if (yesNo)
  435. XMapRaised(xDisplay, xWindow);
  436. else
  437. XUnmapWindow(xDisplay, xWindow);
  438. XFlush(xDisplay);
  439. #endif
  440. if (yesNo)
  441. {
  442. if (fFirstInit)
  443. {
  444. fApp.pData->oneShown();
  445. fFirstInit = false;
  446. }
  447. }
  448. else if (fModal.enabled)
  449. exec_fini();
  450. }
  451. // -------------------------------------------------------------------
  452. void setResizable(const bool yesNo)
  453. {
  454. if (fResizable == yesNo)
  455. {
  456. DBG("Window setResizable matches current state, ignoring request\n");
  457. return;
  458. }
  459. if (fUsingEmbed)
  460. {
  461. DBG("Window setResizable cannot be called when embedded\n");
  462. return;
  463. }
  464. DBG("Window setResizable called\n");
  465. fResizable = yesNo;
  466. fView->user_resizable = yesNo;
  467. #if defined(DISTRHO_OS_WINDOWS)
  468. const int winFlags = fResizable ? GetWindowLong(hwnd, GWL_STYLE) | WS_SIZEBOX
  469. : GetWindowLong(hwnd, GWL_STYLE) & ~WS_SIZEBOX;
  470. SetWindowLong(hwnd, GWL_STYLE, winFlags);
  471. #elif defined(DISTRHO_OS_MAC)
  472. const uint flags(yesNo ? (NSViewWidthSizable|NSViewHeightSizable) : 0x0);
  473. [mView setAutoresizingMask:flags];
  474. #endif
  475. setSize(fWidth, fHeight, true);
  476. }
  477. // -------------------------------------------------------------------
  478. void setGeometryConstraints(uint width, uint height, bool aspect)
  479. {
  480. DISTRHO_SAFE_ASSERT_RETURN(fResizable,);
  481. fView->min_width = width;
  482. fView->min_height = height;
  483. puglUpdateGeometryConstraints(fView, width, height, aspect);
  484. }
  485. // -------------------------------------------------------------------
  486. void setSize(uint width, uint height, const bool forced = false)
  487. {
  488. if (width <= 1 || height <= 1)
  489. {
  490. DBGp("Window setSize called with invalid value(s) %i %i, ignoring request\n", width, height);
  491. return;
  492. }
  493. if (fWidth == width && fHeight == height && ! forced)
  494. {
  495. DBGp("Window setSize matches current size, ignoring request (%i %i)\n", width, height);
  496. return;
  497. }
  498. fWidth = width;
  499. fHeight = height;
  500. DBGp("Window setSize called %s, size %i %i, resizable %s\n", forced ? "(forced)" : "(not forced)", width, height, fResizable?"true":"false");
  501. #if defined(DISTRHO_OS_WINDOWS)
  502. const int winFlags = WS_POPUPWINDOW | WS_CAPTION | (fResizable ? WS_SIZEBOX : 0x0);
  503. RECT wr = { 0, 0, static_cast<LONG>(width), static_cast<LONG>(height) };
  504. AdjustWindowRectEx(&wr, fUsingEmbed ? WS_CHILD : winFlags, FALSE, WS_EX_TOPMOST);
  505. SetWindowPos(hwnd, 0, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
  506. SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
  507. if (! forced)
  508. UpdateWindow(hwnd);
  509. #elif defined(DISTRHO_OS_MAC)
  510. [mView setFrame:NSMakeRect(0, 0, width, height)];
  511. if (mWindow != nullptr)
  512. {
  513. const NSSize size = NSMakeSize(width, height);
  514. [mWindow setContentSize:size];
  515. if (fResizable)
  516. {
  517. [mWindow setContentMinSize:NSMakeSize(1, 1)];
  518. [mWindow setContentMaxSize:NSMakeSize(99999, 99999)];
  519. [[mWindow standardWindowButton:NSWindowZoomButton] setHidden:NO];
  520. }
  521. else
  522. {
  523. [mWindow setContentMinSize:size];
  524. [mWindow setContentMaxSize:size];
  525. [[mWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
  526. }
  527. }
  528. #else
  529. if (! fResizable)
  530. {
  531. XSizeHints sizeHints;
  532. memset(&sizeHints, 0, sizeof(sizeHints));
  533. sizeHints.flags = PSize|PMinSize|PMaxSize;
  534. sizeHints.width = static_cast<int>(width);
  535. sizeHints.height = static_cast<int>(height);
  536. sizeHints.min_width = static_cast<int>(width);
  537. sizeHints.min_height = static_cast<int>(height);
  538. sizeHints.max_width = static_cast<int>(width);
  539. sizeHints.max_height = static_cast<int>(height);
  540. XSetWMNormalHints(xDisplay, xWindow, &sizeHints);
  541. }
  542. XResizeWindow(xDisplay, xWindow, width, height);
  543. if (! forced)
  544. XFlush(xDisplay);
  545. #endif
  546. puglPostRedisplay(fView);
  547. }
  548. // -------------------------------------------------------------------
  549. const char* getTitle() const noexcept
  550. {
  551. static const char* const kFallback = "";
  552. return fTitle != nullptr ? fTitle : kFallback;
  553. }
  554. void setTitle(const char* const title)
  555. {
  556. DBGp("Window setTitle \"%s\"\n", title);
  557. if (fTitle != nullptr)
  558. std::free(fTitle);
  559. fTitle = strdup(title);
  560. #if defined(DISTRHO_OS_WINDOWS)
  561. SetWindowTextA(hwnd, title);
  562. #elif defined(DISTRHO_OS_MAC)
  563. if (mWindow != nullptr)
  564. {
  565. NSString* titleString = [[NSString alloc]
  566. initWithBytes:title
  567. length:strlen(title)
  568. encoding:NSUTF8StringEncoding];
  569. [mWindow setTitle:titleString];
  570. }
  571. #else
  572. XStoreName(xDisplay, xWindow, title);
  573. #endif
  574. }
  575. void setTransientWinId(const uintptr_t winId)
  576. {
  577. DISTRHO_SAFE_ASSERT_RETURN(winId != 0,);
  578. #if defined(DISTRHO_OS_WINDOWS)
  579. hwndParent = (HWND)winId;
  580. SetWindowLongPtr(hwnd, GWLP_HWNDPARENT, (LONG_PTR)winId);
  581. #elif defined(DISTRHO_OS_MAC)
  582. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:winId];
  583. DISTRHO_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  584. [parentWindow addChildWindow:mWindow
  585. ordered:NSWindowAbove];
  586. #else
  587. XSetTransientForHint(xDisplay, xWindow, static_cast< ::Window>(winId));
  588. #endif
  589. }
  590. // -------------------------------------------------------------------
  591. double getScaling() const noexcept
  592. {
  593. return fScaling;
  594. }
  595. void setScaling(double scaling) noexcept
  596. {
  597. DISTRHO_SAFE_ASSERT_RETURN(scaling > 0.0,);
  598. fScaling = scaling;
  599. }
  600. // -------------------------------------------------------------------
  601. void addWidget(Widget* const widget)
  602. {
  603. fWidgets.push_back(widget);
  604. }
  605. void removeWidget(Widget* const widget)
  606. {
  607. fWidgets.remove(widget);
  608. }
  609. void idle()
  610. {
  611. puglProcessEvents(fView);
  612. #ifdef DISTRHO_OS_MAC
  613. if (fNeedsIdle)
  614. {
  615. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  616. NSEvent* event;
  617. for (;;)
  618. {
  619. event = [NSApp
  620. nextEventMatchingMask:NSAnyEventMask
  621. untilDate:[NSDate distantPast]
  622. inMode:NSDefaultRunLoopMode
  623. dequeue:YES];
  624. if (event == nil)
  625. break;
  626. [NSApp sendEvent: event];
  627. }
  628. [pool release];
  629. }
  630. #endif
  631. if (fModal.enabled && fModal.parent != nullptr)
  632. fModal.parent->idle();
  633. }
  634. // -------------------------------------------------------------------
  635. void onPuglDisplay()
  636. {
  637. fSelf->onDisplayBefore();
  638. FOR_EACH_WIDGET(it)
  639. {
  640. Widget* const widget(*it);
  641. widget->pData->display(fWidth, fHeight, fScaling, false);
  642. }
  643. fSelf->onDisplayAfter();
  644. }
  645. int onPuglKeyboard(const bool press, const uint key)
  646. {
  647. DBGp("PUGL: onKeyboard : %i %i\n", press, key);
  648. if (fModal.childFocus != nullptr)
  649. {
  650. fModal.childFocus->focus();
  651. return 0;
  652. }
  653. Widget::KeyboardEvent ev;
  654. ev.press = press;
  655. ev.key = key;
  656. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  657. ev.time = puglGetEventTimestamp(fView);
  658. FOR_EACH_WIDGET_INV(rit)
  659. {
  660. Widget* const widget(*rit);
  661. if (widget->isVisible() && widget->onKeyboard(ev))
  662. return 0;
  663. }
  664. return 1;
  665. }
  666. int onPuglSpecial(const bool press, const Key key)
  667. {
  668. DBGp("PUGL: onSpecial : %i %i\n", press, key);
  669. if (fModal.childFocus != nullptr)
  670. {
  671. fModal.childFocus->focus();
  672. return 0;
  673. }
  674. Widget::SpecialEvent ev;
  675. ev.press = press;
  676. ev.key = key;
  677. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  678. ev.time = puglGetEventTimestamp(fView);
  679. FOR_EACH_WIDGET_INV(rit)
  680. {
  681. Widget* const widget(*rit);
  682. if (widget->isVisible() && widget->onSpecial(ev))
  683. return 0;
  684. }
  685. return 1;
  686. }
  687. void onPuglMouse(const int button, const bool press, int x, int y)
  688. {
  689. DBGp("PUGL: onMouse : %i %i %i %i\n", button, press, x, y);
  690. // FIXME - pugl sends 2 of these for each window on init, don't ask me why. we'll ignore it
  691. if (press && button == 0 && x == 0 && y == 0) return;
  692. if (fModal.childFocus != nullptr)
  693. return fModal.childFocus->focus();
  694. x /= fScaling;
  695. y /= fScaling;
  696. Widget::MouseEvent ev;
  697. ev.button = button;
  698. ev.press = press;
  699. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  700. ev.time = puglGetEventTimestamp(fView);
  701. FOR_EACH_WIDGET_INV(rit)
  702. {
  703. Widget* const widget(*rit);
  704. ev.pos = Point<int>(x-widget->getAbsoluteX(), y-widget->getAbsoluteY());
  705. if (widget->isVisible() && widget->onMouse(ev))
  706. break;
  707. }
  708. }
  709. void onPuglMotion(int x, int y)
  710. {
  711. // DBGp("PUGL: onMotion : %i %i\n", x, y);
  712. if (fModal.childFocus != nullptr)
  713. return;
  714. x /= fScaling;
  715. y /= fScaling;
  716. Widget::MotionEvent ev;
  717. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  718. ev.time = puglGetEventTimestamp(fView);
  719. FOR_EACH_WIDGET_INV(rit)
  720. {
  721. Widget* const widget(*rit);
  722. ev.pos = Point<int>(x-widget->getAbsoluteX(), y-widget->getAbsoluteY());
  723. if (widget->isVisible() && widget->onMotion(ev))
  724. break;
  725. }
  726. }
  727. void onPuglScroll(int x, int y, float dx, float dy)
  728. {
  729. DBGp("PUGL: onScroll : %i %i %f %f\n", x, y, dx, dy);
  730. if (fModal.childFocus != nullptr)
  731. return;
  732. x /= fScaling;
  733. y /= fScaling;
  734. dx /= fScaling;
  735. dy /= fScaling;
  736. Widget::ScrollEvent ev;
  737. ev.delta = Point<float>(dx, dy);
  738. ev.mod = static_cast<Modifier>(puglGetModifiers(fView));
  739. ev.time = puglGetEventTimestamp(fView);
  740. FOR_EACH_WIDGET_INV(rit)
  741. {
  742. Widget* const widget(*rit);
  743. ev.pos = Point<int>(x-widget->getAbsoluteX(), y-widget->getAbsoluteY());
  744. if (widget->isVisible() && widget->onScroll(ev))
  745. break;
  746. }
  747. }
  748. void onPuglReshape(const int width, const int height)
  749. {
  750. DBGp("PUGL: onReshape : %i %i\n", width, height);
  751. if (width <= 1 && height <= 1)
  752. return;
  753. fWidth = static_cast<uint>(width);
  754. fHeight = static_cast<uint>(height);
  755. fSelf->onReshape(fWidth, fHeight);
  756. FOR_EACH_WIDGET(it)
  757. {
  758. Widget* const widget(*it);
  759. if (widget->pData->needsFullViewport)
  760. widget->setSize(fWidth, fHeight);
  761. }
  762. }
  763. void onPuglClose()
  764. {
  765. DBG("PUGL: onClose\n");
  766. if (fModal.enabled)
  767. exec_fini();
  768. fSelf->onClose();
  769. if (fModal.childFocus != nullptr)
  770. fModal.childFocus->fSelf->onClose();
  771. close();
  772. }
  773. // -------------------------------------------------------------------
  774. bool handlePluginKeyboard(const bool press, const uint key)
  775. {
  776. DBGp("PUGL: handlePluginKeyboard : %i %i\n", press, key);
  777. if (fModal.childFocus != nullptr)
  778. {
  779. fModal.childFocus->focus();
  780. return true;
  781. }
  782. Widget::KeyboardEvent ev;
  783. ev.press = press;
  784. ev.key = key;
  785. ev.mod = static_cast<Modifier>(fView->mods);
  786. ev.time = 0;
  787. if ((ev.mod & kModifierShift) != 0 && ev.key >= 'a' && ev.key <= 'z')
  788. ev.key -= 'a' - 'A'; // a-z -> A-Z
  789. FOR_EACH_WIDGET_INV(rit)
  790. {
  791. Widget* const widget(*rit);
  792. if (widget->isVisible() && widget->onKeyboard(ev))
  793. return true;
  794. }
  795. return false;
  796. }
  797. bool handlePluginSpecial(const bool press, const Key key)
  798. {
  799. DBGp("PUGL: handlePluginSpecial : %i %i\n", press, key);
  800. if (fModal.childFocus != nullptr)
  801. {
  802. fModal.childFocus->focus();
  803. return true;
  804. }
  805. int mods = 0x0;
  806. switch (key)
  807. {
  808. case kKeyShift:
  809. mods |= kModifierShift;
  810. break;
  811. case kKeyControl:
  812. mods |= kModifierControl;
  813. break;
  814. case kKeyAlt:
  815. mods |= kModifierAlt;
  816. break;
  817. default:
  818. break;
  819. }
  820. if (mods != 0x0)
  821. {
  822. if (press)
  823. fView->mods |= mods;
  824. else
  825. fView->mods &= ~(mods);
  826. }
  827. Widget::SpecialEvent ev;
  828. ev.press = press;
  829. ev.key = key;
  830. ev.mod = static_cast<Modifier>(fView->mods);
  831. ev.time = 0;
  832. FOR_EACH_WIDGET_INV(rit)
  833. {
  834. Widget* const widget(*rit);
  835. if (widget->isVisible() && widget->onSpecial(ev))
  836. return true;
  837. }
  838. return false;
  839. }
  840. // -------------------------------------------------------------------
  841. Application& fApp;
  842. Window* fSelf;
  843. PuglView* fView;
  844. bool fFirstInit;
  845. bool fVisible;
  846. bool fResizable;
  847. bool fUsingEmbed;
  848. uint fWidth;
  849. uint fHeight;
  850. double fScaling;
  851. char* fTitle;
  852. std::list<Widget*> fWidgets;
  853. struct Modal {
  854. bool enabled;
  855. PrivateData* parent;
  856. PrivateData* childFocus;
  857. Modal()
  858. : enabled(false),
  859. parent(nullptr),
  860. childFocus(nullptr) {}
  861. Modal(PrivateData* const p)
  862. : enabled(false),
  863. parent(p),
  864. childFocus(nullptr) {}
  865. ~Modal()
  866. {
  867. DISTRHO_SAFE_ASSERT(! enabled);
  868. DISTRHO_SAFE_ASSERT(childFocus == nullptr);
  869. }
  870. DISTRHO_DECLARE_NON_COPY_STRUCT(Modal)
  871. } fModal;
  872. #if defined(DISTRHO_OS_WINDOWS)
  873. HWND hwnd;
  874. HWND hwndParent;
  875. #elif defined(DISTRHO_OS_MAC)
  876. bool fNeedsIdle;
  877. PuglOpenGLView* mView;
  878. id mWindow;
  879. id mParentWindow;
  880. #else
  881. Display* xDisplay;
  882. ::Window xWindow;
  883. #endif
  884. // -------------------------------------------------------------------
  885. // Callbacks
  886. #define handlePtr ((PrivateData*)puglGetHandle(view))
  887. static void onDisplayCallback(PuglView* view)
  888. {
  889. handlePtr->onPuglDisplay();
  890. }
  891. static int onKeyboardCallback(PuglView* view, bool press, uint32_t key)
  892. {
  893. return handlePtr->onPuglKeyboard(press, key);
  894. }
  895. static int onSpecialCallback(PuglView* view, bool press, PuglKey key)
  896. {
  897. return handlePtr->onPuglSpecial(press, static_cast<Key>(key));
  898. }
  899. static void onMouseCallback(PuglView* view, int button, bool press, int x, int y)
  900. {
  901. handlePtr->onPuglMouse(button, press, x, y);
  902. }
  903. static void onMotionCallback(PuglView* view, int x, int y)
  904. {
  905. handlePtr->onPuglMotion(x, y);
  906. }
  907. static void onScrollCallback(PuglView* view, int x, int y, float dx, float dy)
  908. {
  909. handlePtr->onPuglScroll(x, y, dx, dy);
  910. }
  911. static void onReshapeCallback(PuglView* view, int width, int height)
  912. {
  913. handlePtr->onPuglReshape(width, height);
  914. }
  915. static void onCloseCallback(PuglView* view)
  916. {
  917. handlePtr->onPuglClose();
  918. }
  919. #ifndef DGL_FILE_BROWSER_DISABLED
  920. static void fileBrowserSelectedCallback(PuglView* view, const char* filename)
  921. {
  922. handlePtr->fSelf->fileBrowserSelected(filename);
  923. }
  924. #endif
  925. #undef handlePtr
  926. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PrivateData)
  927. };
  928. // -----------------------------------------------------------------------
  929. // Window
  930. Window::Window(Application& app)
  931. : pData(new PrivateData(app, this)) {}
  932. Window::Window(Application& app, Window& parent)
  933. : pData(new PrivateData(app, this, parent)) {}
  934. Window::Window(Application& app, intptr_t parentId)
  935. : pData(new PrivateData(app, this, parentId)) {}
  936. Window::~Window()
  937. {
  938. delete pData;
  939. }
  940. void Window::show()
  941. {
  942. pData->setVisible(true);
  943. }
  944. void Window::hide()
  945. {
  946. pData->setVisible(false);
  947. }
  948. void Window::close()
  949. {
  950. pData->close();
  951. }
  952. void Window::exec(bool lockWait)
  953. {
  954. pData->exec(lockWait);
  955. }
  956. void Window::focus()
  957. {
  958. pData->focus();
  959. }
  960. void Window::repaint() noexcept
  961. {
  962. puglPostRedisplay(pData->fView);
  963. }
  964. // static int fib_filter_filename_filter(const char* const name)
  965. // {
  966. // return 1;
  967. // (void)name;
  968. // }
  969. #ifndef DGL_FILE_BROWSER_DISABLED
  970. bool Window::openFileBrowser(const FileBrowserOptions& options)
  971. {
  972. # ifdef SOFD_HAVE_X11
  973. using DISTRHO_NAMESPACE::String;
  974. // --------------------------------------------------------------------------
  975. // configure start dir
  976. // TODO: get abspath if needed
  977. // TODO: cross-platform
  978. String startDir(options.startDir);
  979. if (startDir.isEmpty())
  980. {
  981. if (char* const dir_name = get_current_dir_name())
  982. {
  983. startDir = dir_name;
  984. std::free(dir_name);
  985. }
  986. }
  987. DISTRHO_SAFE_ASSERT_RETURN(startDir.isNotEmpty(), false);
  988. if (! startDir.endsWith('/'))
  989. startDir += "/";
  990. DISTRHO_SAFE_ASSERT_RETURN(x_fib_configure(0, startDir) == 0, false);
  991. // --------------------------------------------------------------------------
  992. // configure title
  993. String title(options.title);
  994. if (title.isEmpty())
  995. {
  996. title = pData->getTitle();
  997. if (title.isEmpty())
  998. title = "FileBrowser";
  999. }
  1000. DISTRHO_SAFE_ASSERT_RETURN(x_fib_configure(1, title) == 0, false);
  1001. // --------------------------------------------------------------------------
  1002. // configure filters
  1003. x_fib_cfg_filter_callback(nullptr); //fib_filter_filename_filter);
  1004. // --------------------------------------------------------------------------
  1005. // configure buttons
  1006. x_fib_cfg_buttons(3, options.buttons.listAllFiles-1);
  1007. x_fib_cfg_buttons(1, options.buttons.showHidden-1);
  1008. x_fib_cfg_buttons(2, options.buttons.showPlaces-1);
  1009. // --------------------------------------------------------------------------
  1010. // show
  1011. return (x_fib_show(pData->xDisplay, pData->xWindow, /*options.width*/0, /*options.height*/0) == 0);
  1012. # else
  1013. // not implemented
  1014. return false;
  1015. // unused
  1016. (void)options;
  1017. # endif
  1018. }
  1019. #endif
  1020. bool Window::isVisible() const noexcept
  1021. {
  1022. return pData->fVisible;
  1023. }
  1024. void Window::setVisible(bool yesNo)
  1025. {
  1026. pData->setVisible(yesNo);
  1027. }
  1028. bool Window::isResizable() const noexcept
  1029. {
  1030. return pData->fResizable;
  1031. }
  1032. void Window::setResizable(bool yesNo)
  1033. {
  1034. pData->setResizable(yesNo);
  1035. }
  1036. void Window::setGeometryConstraints(uint width, uint height, bool aspect)
  1037. {
  1038. pData->setGeometryConstraints(width, height, aspect);
  1039. }
  1040. uint Window::getWidth() const noexcept
  1041. {
  1042. return pData->fWidth;
  1043. }
  1044. uint Window::getHeight() const noexcept
  1045. {
  1046. return pData->fHeight;
  1047. }
  1048. Size<uint> Window::getSize() const noexcept
  1049. {
  1050. return Size<uint>(pData->fWidth, pData->fHeight);
  1051. }
  1052. void Window::setSize(uint width, uint height)
  1053. {
  1054. pData->setSize(width, height);
  1055. }
  1056. void Window::setSize(Size<uint> size)
  1057. {
  1058. pData->setSize(size.getWidth(), size.getHeight());
  1059. }
  1060. const char* Window::getTitle() const noexcept
  1061. {
  1062. return pData->getTitle();
  1063. }
  1064. void Window::setTitle(const char* title)
  1065. {
  1066. pData->setTitle(title);
  1067. }
  1068. void Window::setTransientWinId(uintptr_t winId)
  1069. {
  1070. pData->setTransientWinId(winId);
  1071. }
  1072. double Window::getScaling() const noexcept
  1073. {
  1074. return pData->getScaling();
  1075. }
  1076. void Window::setScaling(double scaling) noexcept
  1077. {
  1078. pData->setScaling(scaling);
  1079. }
  1080. Application& Window::getApp() const noexcept
  1081. {
  1082. return pData->fApp;
  1083. }
  1084. intptr_t Window::getWindowId() const noexcept
  1085. {
  1086. return puglGetNativeWindow(pData->fView);
  1087. }
  1088. void Window::_addWidget(Widget* const widget)
  1089. {
  1090. pData->addWidget(widget);
  1091. }
  1092. void Window::_removeWidget(Widget* const widget)
  1093. {
  1094. pData->removeWidget(widget);
  1095. }
  1096. void Window::_idle()
  1097. {
  1098. pData->idle();
  1099. }
  1100. // -----------------------------------------------------------------------
  1101. void Window::addIdleCallback(IdleCallback* const callback)
  1102. {
  1103. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  1104. pData->fApp.pData->idleCallbacks.push_back(callback);
  1105. }
  1106. void Window::removeIdleCallback(IdleCallback* const callback)
  1107. {
  1108. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr,)
  1109. pData->fApp.pData->idleCallbacks.remove(callback);
  1110. }
  1111. // -----------------------------------------------------------------------
  1112. void Window::onDisplayBefore()
  1113. {
  1114. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1115. glLoadIdentity();
  1116. }
  1117. void Window::onDisplayAfter()
  1118. {
  1119. }
  1120. void Window::onReshape(uint width, uint height)
  1121. {
  1122. glEnable(GL_BLEND);
  1123. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1124. glMatrixMode(GL_PROJECTION);
  1125. glLoadIdentity();
  1126. glOrtho(0.0, static_cast<GLdouble>(width), static_cast<GLdouble>(height), 0.0, 0.0, 1.0);
  1127. glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
  1128. glMatrixMode(GL_MODELVIEW);
  1129. glLoadIdentity();
  1130. }
  1131. void Window::onClose()
  1132. {
  1133. }
  1134. #ifndef DGL_FILE_BROWSER_DISABLED
  1135. void Window::fileBrowserSelected(const char*)
  1136. {
  1137. }
  1138. #endif
  1139. bool Window::handlePluginKeyboard(const bool press, const uint key)
  1140. {
  1141. return pData->handlePluginKeyboard(press, key);
  1142. }
  1143. bool Window::handlePluginSpecial(const bool press, const Key key)
  1144. {
  1145. return pData->handlePluginSpecial(press, key);
  1146. }
  1147. // -----------------------------------------------------------------------
  1148. StandaloneWindow::StandaloneWindow()
  1149. : Application(),
  1150. Window((Application&)*this),
  1151. fWidget(nullptr) {}
  1152. void StandaloneWindow::exec()
  1153. {
  1154. Window::show();
  1155. Application::exec();
  1156. }
  1157. void StandaloneWindow::onReshape(uint width, uint height)
  1158. {
  1159. if (fWidget != nullptr)
  1160. fWidget->setSize(width, height);
  1161. Window::onReshape(width, height);
  1162. }
  1163. void StandaloneWindow::_addWidget(Widget* widget)
  1164. {
  1165. if (fWidget == nullptr)
  1166. {
  1167. fWidget = widget;
  1168. fWidget->pData->needsFullViewport = true;
  1169. }
  1170. Window::_addWidget(widget);
  1171. }
  1172. void StandaloneWindow::_removeWidget(Widget* widget)
  1173. {
  1174. if (fWidget == widget)
  1175. {
  1176. fWidget->pData->needsFullViewport = false;
  1177. fWidget = nullptr;
  1178. }
  1179. Window::_removeWidget(widget);
  1180. }
  1181. // -----------------------------------------------------------------------
  1182. END_NAMESPACE_DGL
  1183. #undef DBG
  1184. #undef DBGF