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.

1399 lines
34KB

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