Update to 20180809 release.

byuu says:

The Windows port can now run the emulation while navigating menus,
moving windows, and resizing windows. The main window also doesn't try
so hard to constantly clear itself. This may leave a bit of unwelcome
residue behind in some video drivers during resize, but under most
drivers, it lets you resize without a huge amount of flickering.

On all platforms, I now also run the emulation during MessageWindow
modal events, where I didn't before.

I'm thinking we should probably mute the audio during modal periods,
since it can generate a good deal of distortion.

The tooltip timeout was increased to ten seconds.

On Windows, the enter key can now activate buttons, so you can more
quickly dismiss MessageDialog windows. This part may not actually work
... I'm in the middle of trying to get messages out of the global
`Application_windowProc` hook and into the individual `Widget_windowProc`
hooks, so I need to do some testing.

I fixed a bug where changing the input driver wouldn't immediately
reload the input/hotkey settings lists properly.

I also went from disabling the driver "Change" button when the currently
active driver is selected in the list, to instead setting it to say
"Reload", and I also added a tool tip to the input driver reload button,
advising that if you're using DirectInput or SDL, you can hit "Reload"
to rescan for hotplugged gamepads without needing to restart the
emulator. XInput and udev have auto hotswap support. If we can ever get
that into DirectInput and SDL, then I'll remove the tooltip. But
regardless, the reload functionality is nice to have for all drivers.

I'm not sure what should happen when a user changes their driver
selection while a game is loaded, gets the warning dialog, chooses not
to change it, and then closes the emulator. Currently, it will make the
change happen the next time you start the emulator. This feels a bit
unexpected, but when you change the selection without a game loaded, it
takes immediate effect. So I'm not really sure what's best here.
This commit is contained in:
Tim Allen 2018-08-10 15:02:59 +10:00
parent 1e4affe5f9
commit 9a6ae6dacb
38 changed files with 282 additions and 248 deletions

View File

@ -75,7 +75,7 @@ auto InputManager::bindHotkeys() -> void {
}
auto InputManager::pollHotkeys() -> void {
if(!program.focused()) return;
if(Application::modal() || !program.focused()) return;
for(auto& hotkey : hotkeys) {
auto state = hotkey.poll();

View File

@ -187,7 +187,7 @@ auto InputManager::initialize() -> void {
input.onChange({&InputManager::onChange, this});
lastPoll = chrono::millisecond();
lastPoll = 0; //force a poll event immediately
frequency = max(1u, settings.input.frequency);
turboCounter = 0;
@ -254,6 +254,8 @@ auto InputManager::bind() -> void {
}
auto InputManager::poll() -> void {
if(Application::modal()) return;
//polling actual hardware devices is time-consuming; skip if poll was called too recently
auto thisPoll = chrono::millisecond();
if(thisPoll - lastPoll < frequency) return;

View File

@ -190,12 +190,6 @@ auto Presentation::create() -> void {
resizeWindow();
setCentered();
#if defined(PLATFORM_WINDOWS)
Application::Windows::onModalChange([&](bool modal) {
if(modal) audio.clear();
});
#endif
#if defined(PLATFORM_MACOS)
Application::Cocoa::onAbout([&] { about.doActivate(); });
Application::Cocoa::onActivate([&] { setFocused(); });
@ -283,7 +277,7 @@ auto Presentation::resizeViewport() -> void {
paddingWidth - paddingWidth / 2, paddingHeight - paddingHeight / 2
});
clearViewport();
//clearViewport();
}
auto Presentation::resizeWindow() -> void {

View File

@ -11,6 +11,9 @@ auto Program::load() -> void {
screenshot = {};
frameAdvance = false;
if(!verified() && emulatorSettings.warnOnUnverifiedGames.checked()) {
//Emulator::loaded() is true at this point:
//prevent Program::main() from calling Emulator::run() during this dialog window
auto lock = acquire();
auto response = MessageDialog(
"Warning: this game image is unverified.\n"
"Running it *may* be a security risk.\n\n"

View File

@ -73,16 +73,14 @@ auto Program::create(vector<string> arguments) -> void {
}
auto Program::main() -> void {
if(Application::modal()) return;
updateStatus();
video.poll();
inputManager.poll();
inputManager.pollHotkeys();
if(paused()) {
if(inactive()) {
audio.clear();
usleep(20 * 1000);
if(!Application::modal()) usleep(20 * 1000);
return;
}

View File

@ -1,4 +1,4 @@
struct Program : Emulator::Platform {
struct Program : Lock, Emulator::Platform {
Application::Namespace tr{"Program"};
//program.cpp
@ -91,7 +91,7 @@ struct Program : Emulator::Platform {
auto showFrameRate(string text) -> void;
auto updateStatus() -> void;
auto captureScreenshot() -> bool;
auto paused() -> bool;
auto inactive() -> bool;
auto focused() -> bool;
//patch.cpp

View File

@ -60,7 +60,8 @@ auto Program::captureScreenshot() -> bool {
return false;
}
auto Program::paused() -> bool {
auto Program::inactive() -> bool {
if(locked()) return true;
if(!emulator->loaded()) return true;
if(presentation.pauseEmulation.checked()) return true;
if(!focused() && emulatorSettings.pauseEmulation.checked()) return true;

View File

@ -8,13 +8,13 @@ auto DriverSettings::create() -> void {
videoLayout.setSize({2, 2});
videoDriverLabel.setText("Driver:");
videoDriverOption.onChange([&] {
videoDriverUpdate.setEnabled(videoDriverOption.selected().text() != video.driver());
videoDriverUpdate.setText(videoDriverOption.selected().text() != video.driver() ? "Change" : "Reload");
});
videoDriverUpdate.setText("Change").onActivate([&] { videoDriverChange(); });
videoFormatLabel.setText("Format:");
videoFormatOption.onChange([&] { videoFormatChange(); });
videoExclusiveToggle.setText("Exclusive").setToolTip(
"(Direct3D only)\n\n"
"(Direct3D driver only)\n\n"
"Acquires exclusive access to the display in fullscreen mode.\n"
"Eliminates compositing issues such as video stuttering."
).onToggle([&] {
@ -33,7 +33,7 @@ auto DriverSettings::create() -> void {
presentation.speedMenu.setEnabled(!videoBlockingToggle.checked() && audioBlockingToggle.checked());
});
videoFlushToggle.setText("GPU sync").setToolTip({
"(OpenGL only)\n\n"
"(OpenGL driver only)\n\n"
"Causes the GPU to wait until frames are fully rendered.\n"
"In the best case, this can remove up to one frame of input lag.\n"
"However, it incurs a roughly 20% performance penalty.\n\n"
@ -48,7 +48,7 @@ auto DriverSettings::create() -> void {
audioLayout.setSize({2, 2});
audioDriverLabel.setText("Driver:");
audioDriverOption.onChange([&] {
audioDriverUpdate.setEnabled(audioDriverOption.selected().text() != audio.driver());
audioDriverUpdate.setText(audioDriverOption.selected().text() != audio.driver() ? "Change" : "Reload");
});
audioDriverUpdate.setText("Change").onActivate([&] { audioDriverChange(); });
audioDeviceLabel.setText("Device:");
@ -58,7 +58,7 @@ auto DriverSettings::create() -> void {
audioLatencyLabel.setText("Latency:");
audioLatencyOption.onChange([&] { audioLatencyChange(); });
audioExclusiveToggle.setText("Exclusive").setToolTip(
"(ASIO, WASAPI only)\n\n"
"(ASIO, WASAPI drivers only)\n\n"
"Acquires exclusive control of the sound card device.\n"
"This can significantly reduce audio latency.\n"
"However, it will block sounds from all other applications."
@ -77,7 +77,7 @@ auto DriverSettings::create() -> void {
presentation.speedMenu.setEnabled(!videoBlockingToggle.checked() && audioBlockingToggle.checked());
});
audioDynamicToggle.setText("Dynamic rate").setToolTip(
"(OSS only)\n\n"
"(OSS, XAudio drivers only)\n\n"
"Dynamically adjusts the audio frequency by tiny amounts.\n"
"Use this with video sync enabled, and audio sync disabled.\n\n"
"This can produce perfectly smooth video and clean audio,\n"
@ -93,9 +93,13 @@ auto DriverSettings::create() -> void {
inputLayout.setSize({2, 1});
inputDriverLabel.setText("Driver:");
inputDriverOption.onChange([&] {
inputDriverUpdate.setEnabled(inputDriverOption.selected().text() != input.driver());
inputDriverUpdate.setText(inputDriverOption.selected().text() != input.driver() ? "Change" : "Reload");
});
inputDriverUpdate.setText("Change").onActivate([&] { inputDriverChange(); });
inputDriverUpdate.setText("Change").setToolTip(
"A driver reload can be used to detect hotplugged devices.\n"
"This is useful for APIs that lack auto-hotplug support,\n"
"such as DirectInput and SDL."
).onActivate([&] { inputDriverChange(); });
//this will hide the video format setting for simplicity, as it's not very useful just yet ...
//videoLayout.setSize({2, 1});

View File

@ -37,6 +37,10 @@ NSTimer* applicationTimer = nullptr;
namespace hiro {
auto pApplication::modal() -> bool {
return Application::state().modal > 0;
}
auto pApplication::run() -> void {
//applicationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1667 target:cocoaDelegate selector:@selector(updateInDock:) userInfo:nil repeats:YES];

View File

@ -11,6 +11,7 @@
namespace hiro {
struct pApplication {
static auto modal() -> bool;
static auto run() -> void;
static auto pendingEvents() -> bool;
static auto processEvents() -> void;

View File

@ -18,7 +18,7 @@ auto Application::locale() -> Locale& {
}
auto Application::modal() -> bool {
return state().modal > 0;
return pApplication::modal();
}
auto Application::name() -> string {
@ -93,17 +93,6 @@ auto Application::unscale(float value) -> float {
return value * (1.0 / state().scale);
}
//Windows
//=======
auto Application::Windows::doModalChange(bool modal) -> void {
if(state().windows.onModalChange) return state().windows.onModalChange(modal);
}
auto Application::Windows::onModalChange(const function<void (bool)>& callback) -> void {
state().windows.onModalChange = callback;
}
//Cocoa
//=====
@ -144,9 +133,9 @@ auto Application::Cocoa::onQuit(const function<void ()>& callback) -> void {
auto Application::initialize() -> void {
if(!state().initialized) {
state().initialized = true;
hiro::initialize();
pApplication::initialize();
state().initialized = true;
pApplication::setScreenSaver(state().screenSaver);
}
}

View File

@ -24,11 +24,6 @@ struct Application {
static auto toolTips() -> bool;
static auto unscale(float value) -> float;
struct Windows {
static auto doModalChange(bool modal) -> void;
static auto onModalChange(const function<void (bool)>& callback = {}) -> void;
};
struct Cocoa {
static auto doAbout() -> void;
static auto doActivate() -> void;
@ -57,11 +52,6 @@ struct Application {
bool screenSaver = true;
bool toolTips = true;
struct Windows {
function<void (bool)> onModalChange;
function<bool ()> onScreenSaver;
} windows;
struct Cocoa {
function<void ()> onAbout;
function<void ()> onActivate;

View File

@ -466,42 +466,8 @@ private:
virtual auto allocate() -> pObject*; \
#include "object.hpp"
#if defined(Hiro_Group)
struct mGroup : mObject {
Declare(Group)
using mObject::remove;
auto append(sObject object) -> type&;
auto object(uint offset) const -> Object;
auto objectCount() const -> uint;
auto objects() const -> vector<Object>;
auto remove(sObject object) -> type&;
//private:
struct State {
vector<wObject> objects;
} state;
};
#endif
#if defined(Hiro_Timer)
struct mTimer : mObject {
Declare(Timer)
auto doActivate() const -> void;
auto interval() const -> uint;
auto onActivate(const function<void ()>& callback = {}) -> type&;
auto setInterval(uint interval = 0) -> type&;
//private:
struct State {
uint interval = 0;
function<void ()> onActivate;
} state;
};
#endif
#include "group.hpp"
#include "timer.hpp"
#include "window.hpp"
#if defined(Hiro_StatusBar)

17
hiro/core/group.hpp Normal file
View File

@ -0,0 +1,17 @@
#if defined(Hiro_Group)
struct mGroup : mObject {
Declare(Group)
using mObject::remove;
auto append(sObject object) -> type&;
auto object(uint offset) const -> Object;
auto objectCount() const -> uint;
auto objects() const -> vector<Object>;
auto remove(sObject object) -> type&;
//private:
struct State {
vector<wObject> objects;
} state;
};
#endif

View File

@ -1,5 +1,9 @@
#if defined(Hiro_Timer)
mTimer::mTimer() {
mObject::state.enabled = false;
}
auto mTimer::allocate() -> pObject* {
return new pTimer(*this);
}

18
hiro/core/timer.hpp Normal file
View File

@ -0,0 +1,18 @@
#if defined(Hiro_Timer)
struct mTimer : mObject {
Declare(Timer)
mTimer();
auto doActivate() const -> void;
auto interval() const -> uint;
auto onActivate(const function<void ()>& callback = {}) -> type&;
auto setInterval(uint interval = 0) -> type&;
//private:
struct State {
uint interval = 0;
function<void ()> onActivate;
} state;
};
#endif

View File

@ -290,6 +290,7 @@ auto mWindow::setMinimumSize(Size size) -> type& {
}
auto mWindow::setModal(bool modal) -> type& {
if(state.modal == modal) return *this;
state.modal = modal;
if(modal) {
Application::state().modal++;

View File

@ -2,6 +2,10 @@
namespace hiro {
auto pApplication::modal() -> bool {
return Application::state().modal > 0;
}
auto pApplication::run() -> void {
while(!Application::state().quit) {
Application::doMain();

View File

@ -3,6 +3,7 @@
namespace hiro {
struct pApplication {
static auto modal() -> bool;
static auto run() -> void;
static auto pendingEvents() -> bool;
static auto processEvents() -> void;

View File

@ -2,6 +2,10 @@
namespace hiro {
auto pApplication::modal() -> bool {
return Application::state().modal > 0;
}
auto pApplication::run() -> void {
if(Application::state().onMain) {
while(!Application::state().quit) {

View File

@ -3,6 +3,7 @@
namespace hiro {
struct pApplication {
static auto modal() -> bool;
static auto run() -> void;
static auto pendingEvents() -> bool;
static auto processEvents() -> void;

View File

@ -4,7 +4,11 @@ namespace hiro {
static auto Application_keyboardProc(HWND, UINT, WPARAM, LPARAM) -> bool;
static auto Application_processDialogMessage(MSG&) -> void;
static auto CALLBACK Application_windowProc(HWND, UINT, WPARAM, LPARAM) -> LRESULT;
static auto CALLBACK Window_windowProc(HWND, UINT, WPARAM, LPARAM) -> LRESULT;
auto pApplication::modal() -> bool {
return state().modalCount > 0;
}
auto pApplication::run() -> void {
MSG msg;
@ -70,7 +74,7 @@ auto pApplication::initialize() -> void {
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(2));
wc.hInstance = GetModuleHandle(0);
wc.lpfnWndProc = Application_windowProc;
wc.lpfnWndProc = Window_windowProc;
wc.lpszClassName = L"hiroWindow";
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
@ -151,8 +155,9 @@ static auto Application_keyboardProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM
window->doKeyRelease(code);
}
}
if(window->state.dismissable && msg == WM_KEYDOWN && wparam == VK_ESCAPE) {
self->onClose();
//TODO: does this really need to be hooked here?
if(msg == WM_KEYDOWN && wparam == VK_ESCAPE && window->state.dismissable) {
if(auto result = self->windowProc(self->hwnd, WM_CLOSE, wparam, lparam)) return result();
}
}
}
@ -165,56 +170,13 @@ static auto Application_keyboardProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM
}
if(msg == WM_KEYDOWN) {
if(0);
#if defined(Hiro_TableView)
else if(auto tableView = dynamic_cast<mTableView*>(object)) {
if(wparam == VK_RETURN) {
if(tableView->selected()) return true; //returning true generates LVN_ITEMACTIVATE message
}
}
#endif
#if defined(Hiro_LineEdit)
else if(auto lineEdit = dynamic_cast<mLineEdit*>(object)) {
if(wparam == VK_RETURN) {
lineEdit->doActivate();
}
}
#endif
#if defined(Hiro_TextEdit)
else if(auto textEdit = dynamic_cast<mTextEdit*>(object)) {
if(wparam == 'A' && GetKeyState(VK_CONTROL) < 0) {
//Ctrl+A = select all text
//note: this is not a standard accelerator on Windows
Edit_SetSel(textEdit->self()->hwnd, 0, ~0);
return true;
} else if(wparam == 'V' && GetKeyState(VK_CONTROL) < 0) {
//Ctrl+V = paste text
//note: this formats Unix (LF) and OS9 (CR) line-endings to Windows (CR+LF) line-endings
//this is necessary as the EDIT control only supports Windows line-endings
OpenClipboard(hwnd);
if(auto handle = GetClipboardData(CF_UNICODETEXT)) {
if(auto text = (wchar_t*)GlobalLock(handle)) {
string data = (const char*)utf8_t(text);
data.replace("\r\n", "\n");
data.replace("\r", "\n");
data.replace("\n", "\r\n");
GlobalUnlock(handle);
utf16_t output(data);
if(auto resource = GlobalAlloc(GMEM_MOVEABLE, (wcslen(output) + 1) * sizeof(wchar_t))) {
if(auto write = (wchar_t*)GlobalLock(resource)) {
wcscpy(write, output);
GlobalUnlock(write);
if(SetClipboardData(CF_UNICODETEXT, resource) == NULL) {
GlobalFree(resource);
}
}
}
}
//TODO: does this really need to be hooked here?
#if defined(Hiro_Widget)
if(auto widget = dynamic_cast<mWidget*>(object)) {
if(auto self = widget->self()) {
if(auto result = self->windowProc(self->hwnd, msg, wparam, lparam)) {
return result();
}
CloseClipboard();
}
}
#endif
@ -223,46 +185,6 @@ static auto Application_keyboardProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM
return false;
}
/*
case WM_GETMINMAXINFO: {
MINMAXINFO* mmi = (MINMAXINFO*)lparam;
mmi->ptMinTrackSize.x = 256 + window.p.frameMargin().width;
mmi->ptMinTrackSize.y = 256 + window.p.frameMargin().height;
return TRUE;
break;
}
*/
static auto CALLBACK Application_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> LRESULT {
if(Application::state().quit) return DefWindowProc(hwnd, msg, wparam, lparam);
auto object = (mObject*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if(!object) return DefWindowProc(hwnd, msg, wparam, lparam);
auto window = dynamic_cast<mWindow*>(object);
if(!window) window = object->parentWindow(true);
if(!window) return DefWindowProc(hwnd, msg, wparam, lparam);
auto pWindow = window->self();
if(!pWindow) return DefWindowProc(hwnd, msg, wparam, lparam);
if(pWindow->_modalityDisabled()) return DefWindowProc(hwnd, msg, wparam, lparam);
switch(msg) {
case WM_CLOSE: pWindow->onClose(); return true;
case WM_MOVE: pWindow->onMove(); break;
case WM_SIZE: pWindow->onSize(); break;
case WM_DROPFILES: pWindow->onDrop(wparam); return false;
case WM_ERASEBKGND: if(pWindow->onEraseBackground()) return true; break;
case WM_ENTERMENULOOP: case WM_ENTERSIZEMOVE: pWindow->onModalBegin(); return false;
case WM_EXITMENULOOP: case WM_EXITSIZEMOVE: pWindow->onModalEnd(); return false;
case WM_SYSCOMMAND:
if(wparam == SC_SCREENSAVE || wparam == SC_MONITORPOWER) {
if(!Application::screenSaver()) return 0;
}
}
return Shared_windowProc(DefWindowProc, hwnd, msg, wparam, lparam);
}
}
#endif

View File

@ -3,6 +3,7 @@
namespace hiro {
struct pApplication {
static auto modal() -> bool;
static auto run() -> void;
static auto pendingEvents() -> bool;
static auto processEvents() -> void;
@ -12,6 +13,8 @@ struct pApplication {
static auto initialize() -> void;
struct State {
int modalCount = 0; //number of modal loops
Timer modalTimer; //to run Application during modal events
pToolTip* toolTip = nullptr; //active toolTip
};
static auto state() -> State&;

View File

@ -1,5 +1,8 @@
#if defined(Hiro_Timer)
//timeBeginPeriod(1) + timeSetEvent does not seem any more performant than SetTimer
//it also seems buggier, and requires libwinmm
namespace hiro {
static vector<pTimer*> timers;
@ -16,20 +19,22 @@ auto pTimer::construct() -> void {
}
auto pTimer::destruct() -> void {
setEnabled(false);
if(auto index = timers.find(this)) timers.remove(*index);
}
auto pTimer::setEnabled(bool enabled) -> void {
if(htimer) {
KillTimer(NULL, htimer);
KillTimer(nullptr, htimer);
htimer = 0;
}
if(enabled == true) {
htimer = SetTimer(NULL, 0u, state().interval, Timer_timeoutProc);
htimer = SetTimer(nullptr, 0, state().interval, Timer_timeoutProc);
}
}
auto pTimer::setInterval(unsigned interval) -> void {
auto pTimer::setInterval(uint interval) -> void {
//destroy and recreate timer if interval changed
setEnabled(self().enabled(true));
}

View File

@ -6,9 +6,9 @@ struct pTimer : pObject {
Declare(Timer, Object)
auto setEnabled(bool enabled) -> void override;
auto setInterval(unsigned interval) -> void;
auto setInterval(uint interval) -> void;
UINT_PTR htimer;
UINT_PTR htimer = 0;
};
}

View File

@ -22,7 +22,7 @@ pToolTip::pToolTip(const string& toolTipText) {
tracking.x = -1;
tracking.y = -1;
timeout.setInterval(5000);
timeout.setInterval(Timeout);
timeout.onActivate([&] { hide(); });
}

View File

@ -1,6 +1,8 @@
namespace hiro {
struct pToolTip {
enum : uint { Delay = 1000, Timeout = 10000 };
pToolTip(const string& text);
~pToolTip();

View File

@ -424,7 +424,7 @@ static auto CALLBACK Shared_windowProc(WindowProc windowProc, HWND hwnd, UINT ms
TRACKMOUSEEVENT event{sizeof(TRACKMOUSEEVENT)};
event.hwndTrack = hwnd;
event.dwFlags = TME_LEAVE | TME_HOVER;
event.dwHoverTime = 1500;
event.dwHoverTime = pToolTip::Delay;
TrackMouseEvent(&event);
POINT p{};
GetCursorPos(&p);

View File

@ -74,6 +74,11 @@ auto pButton::onActivate() -> void {
//note: letting hiro paint bordered buttons will lose the fade animations on Vista+;
//however, it will allow placing icons immediately next to text (original forces icon left alignment)
auto pButton::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
if(msg == WM_KEYDOWN) {
//very useful for MessageDialog
self().doActivate();
}
if(msg == WM_PAINT) {
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);

View File

@ -43,11 +43,23 @@ auto pLineEdit::setText(const string& text) -> void {
SetWindowText(hwnd, utf16_t(text));
}
//
auto pLineEdit::onChange() -> void {
state().text = _text();
if(!locked()) self().doChange();
}
auto pLineEdit::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
if(msg == WM_KEYDOWN) {
self().doActivate();
}
return pWidget::windowProc(hwnd, msg, wparam, lparam);
}
//
auto pLineEdit::_text() -> string {
unsigned length = GetWindowTextLength(hwnd);
wchar_t text[length + 1];

View File

@ -12,6 +12,7 @@ struct pLineEdit : pWidget {
auto setText(const string& text) -> void;
auto onChange() -> void;
auto windowProc(HWND, UINT, WPARAM, LPARAM) -> maybe<LRESULT> override;
auto _text() -> string;

View File

@ -288,6 +288,13 @@ auto pTableView::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -
//the control should be inactive when disabled; so we intercept the messages here
return false;
}
if(msg == WM_KEYDOWN && wparam == VK_RETURN) {
if(self().selected()) {
//returning true generates LVN_ITEMACTIVATE message
return true;
}
}
}
return pWidget::windowProc(hwnd, msg, wparam, lparam);

View File

@ -66,10 +66,50 @@ auto pTextEdit::text() const -> string {
return text;
}
//
auto pTextEdit::onChange() -> void {
if(!locked()) self().doChange();
}
auto pTextEdit::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
if(msg == WM_KEYDOWN) {
if(wparam == 'A' && GetKeyState(VK_CONTROL) < 0) {
//Ctrl+A = select all text
//note: this is not a standard accelerator on Windows
Edit_SetSel(hwnd, 0, ~0);
return true;
} else if(wparam == 'V' && GetKeyState(VK_CONTROL) < 0) {
//Ctrl+V = paste text
//note: this formats Unix (LF) and OS9 (CR) line-endings to Windows (CR+LF) line-endings
//this is necessary as the EDIT control only supports Windows line-endings
OpenClipboard(hwnd);
if(auto handle = GetClipboardData(CF_UNICODETEXT)) {
if(auto text = (wchar_t*)GlobalLock(handle)) {
string data = (const char*)utf8_t(text);
data.replace("\r\n", "\n");
data.replace("\r", "\n");
data.replace("\n", "\r\n");
GlobalUnlock(handle);
utf16_t output(data);
if(auto resource = GlobalAlloc(GMEM_MOVEABLE, (wcslen(output) + 1) * sizeof(wchar_t))) {
if(auto write = (wchar_t*)GlobalLock(resource)) {
wcscpy(write, output);
GlobalUnlock(write);
if(SetClipboardData(CF_UNICODETEXT, resource) == nullptr) {
GlobalFree(resource);
}
}
}
}
}
CloseClipboard();
}
}
return pWidget::windowProc(hwnd, msg, wparam, lparam);
}
}
#endif

View File

@ -14,6 +14,7 @@ struct pTextEdit : pWidget {
auto text() const -> string;
auto onChange() -> void;
auto windowProc(HWND, UINT, WPARAM, LPARAM) -> maybe<LRESULT> override;
HBRUSH backgroundBrush = nullptr;
};

View File

@ -39,14 +39,15 @@ auto pViewport::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) ->
}
if(msg == WM_ERASEBKGND) {
PAINTSTRUCT ps;
//this will cause flickering during window resize
/*PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
auto brush = CreateSolidBrush(RGB(0, 0, 0));
RECT rc{};
GetClientRect(hwnd, &rc);
FillRect(ps.hdc, &rc, brush);
DeleteObject(brush);
EndPaint(hwnd, &ps);
EndPaint(hwnd, &ps);*/
return true;
}

View File

@ -101,7 +101,7 @@ auto pWidget::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> m
TRACKMOUSEEVENT event{sizeof(TRACKMOUSEEVENT)};
event.hwndTrack = hwnd;
event.dwFlags = TME_LEAVE | TME_HOVER;
event.dwHoverTime = 1500;
event.dwHoverTime = pToolTip::Delay;
TrackMouseEvent(&event);
POINT p{};
GetCursorPos(&p);

View File

@ -2,12 +2,32 @@
namespace hiro {
static auto CALLBACK Window_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> LRESULT {
if(Application::state().quit) return DefWindowProc(hwnd, msg, wparam, lparam);
if(auto window = (mWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA)) {
if(auto self = window->self()) {
if(self->_modalityDisabled()) {
return DefWindowProc(hwnd, msg, wparam, lparam);
}
if(auto result = self->windowProc(hwnd, msg, wparam, lparam)) {
return result();
}
}
}
return Shared_windowProc(DefWindowProc, hwnd, msg, wparam, lparam);
}
static const uint FixedStyle = WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_BORDER | WS_CLIPCHILDREN;
static const uint ResizableStyle = WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN;
uint pWindow::minimumStatusHeight = 0;
auto pWindow::initialize() -> void {
pApplication::state().modalTimer.setInterval(1);
pApplication::state().modalTimer.onActivate([] { Application::doMain(); });
HWND hwnd = CreateWindow(L"hiroWindow", L"", ResizableStyle, 128, 128, 256, 256, 0, 0, GetModuleHandle(0), 0);
HWND hstatus = CreateWindow(STATUSCLASSNAME, L"", WS_CHILD, 0, 0, 0, 0, hwnd, nullptr, GetModuleHandle(0), 0);
SetWindowPos(hstatus, nullptr, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED);
@ -100,8 +120,8 @@ auto pWindow::setFont(const Font& font) -> void {
}
auto pWindow::setFullScreen(bool fullScreen) -> void {
auto lock = acquire();
auto style = GetWindowLongPtr(hwnd, GWL_STYLE) & WS_VISIBLE;
lock();
if(fullScreen) {
windowedGeometry = self().geometry();
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
@ -121,11 +141,10 @@ auto pWindow::setFullScreen(bool fullScreen) -> void {
SetWindowLongPtr(hwnd, GWL_STYLE, style | (state().resizable ? ResizableStyle : FixedStyle));
self().setGeometry(windowedGeometry);
}
unlock();
}
auto pWindow::setGeometry(Geometry geometry) -> void {
lock();
auto lock = acquire();
Geometry margin = frameMargin();
SetWindowPos(
hwnd, nullptr,
@ -141,42 +160,38 @@ auto pWindow::setGeometry(Geometry geometry) -> void {
if(auto& sizable = state().sizable) {
sizable->setGeometry(geometry.setPosition());
}
unlock();
}
auto pWindow::setMaximized(bool maximized) -> void {
if(state().minimized) return;
lock();
auto lock = acquire();
ShowWindow(hwnd, maximized ? SW_MAXIMIZE : SW_SHOWNOACTIVATE);
unlock();
}
auto pWindow::setMaximumSize(Size size) -> void {
//todo
}
auto pWindow::setMinimized(bool minimized) -> void {
lock();
auto lock = acquire();
ShowWindow(hwnd, minimized ? SW_MINIMIZE : state().maximized ? SW_MAXIMIZE : SW_SHOWNOACTIVATE);
unlock();
}
auto pWindow::setMinimumSize(Size size) -> void {
//todo
}
//never call this directly: use Window::setModal() instead
//this function does not confirm the modality has actually changed before adjusting modalCount
auto pWindow::setModal(bool modality) -> void {
if(modality) {
modalIncrement();
_modalityUpdate();
while(state().modal) {
Application::processEvents();
if(Application::state().onMain) {
Application::doMain();
} else {
usleep(20 * 1000);
}
if(!Application::state().onMain) usleep(20 * 1000);
}
_modalityUpdate();
} else {
modalDecrement();
}
}
@ -196,59 +211,81 @@ auto pWindow::setVisible(bool visible) -> void {
if(auto& sizable = state().sizable) {
sizable->setGeometry(self().geometry().setPosition());
}
if(!visible) setModal(false);
if(!visible) self().setModal(false);
}
//
auto pWindow::onClose() -> void {
if(state().onClose) self().doClose();
else self().setVisible(false);
if(state().modal && !self().visible()) self().setModal(false);
auto pWindow::modalIncrement() -> void {
if(pApplication::state().modalCount++ == 0) {
pApplication::state().modalTimer.setEnabled(true);
}
}
auto pWindow::onDrop(WPARAM wparam) -> void {
auto paths = DropPaths(wparam);
if(paths) self().doDrop(paths);
auto pWindow::modalDecrement() -> void {
if(--pApplication::state().modalCount == 0) {
pApplication::state().modalTimer.setEnabled(false);
}
}
auto pWindow::onEraseBackground() -> bool {
if(hbrush == 0) return false;
RECT rc;
GetClientRect(hwnd, &rc);
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
FillRect(ps.hdc, &rc, hbrush);
EndPaint(hwnd, &ps);
return true;
}
auto pWindow::windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> maybe<LRESULT> {
if(msg == WM_CLOSE || (msg == WM_KEYDOWN && wparam == VK_ESCAPE && state().dismissable)) {
if(state().onClose) self().doClose();
else self().setVisible(false);
if(state().modal && !self().visible()) self().setModal(false);
return true;
}
auto pWindow::onModalBegin() -> void {
Application::Windows::doModalChange(true);
}
if(msg == WM_MOVE && !locked()) {
state().geometry.setPosition(_geometry().position());
self().doMove();
}
auto pWindow::onModalEnd() -> void {
Application::Windows::doModalChange(false);
}
if(msg == WM_SIZE && !locked()) {
if(auto statusBar = state().statusBar) {
if(auto self = statusBar->self()) {
SetWindowPos(self->hwnd, nullptr, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
state().geometry.setSize(_geometry().size());
if(auto& sizable = state().sizable) {
sizable->setGeometry(_geometry().setPosition());
}
self().doSize();
}
auto pWindow::onMove() -> void {
if(locked()) return;
state().geometry.setPosition(_geometry().position());
self().doMove();
}
if(msg == WM_DROPFILES) {
if(auto paths = DropPaths(wparam)) self().doDrop(paths);
return false;
}
auto pWindow::onSize() -> void {
if(locked()) return;
if(auto statusBar = state().statusBar) {
if(auto self = statusBar->self()) {
SetWindowPos(self->hwnd, nullptr, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED);
if(msg == WM_ERASEBKGND && hbrush) {
RECT rc;
GetClientRect(hwnd, &rc);
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
FillRect(ps.hdc, &rc, hbrush);
EndPaint(hwnd, &ps);
return true;
}
if(msg == WM_ENTERMENULOOP || msg == WM_ENTERSIZEMOVE) {
modalIncrement();
return false;
}
if(msg == WM_EXITMENULOOP || msg == WM_EXITSIZEMOVE) {
modalDecrement();
return false;
}
if(msg == WM_SYSCOMMAND) {
if(wparam == SC_SCREENSAVE || wparam == SC_MONITORPOWER) {
if(!Application::screenSaver()) return 0;
}
}
state().geometry.setSize(_geometry().size());
if(auto& sizable = state().sizable) {
sizable->setGeometry(_geometry().setPosition());
}
self().doSize();
return {};
}
//
@ -258,7 +295,7 @@ auto pWindow::_geometry() -> Geometry {
RECT rc;
if(IsIconic(hwnd)) {
//GetWindowRect returns -32000(x),-32000(y) when window is minimized
//GetWindowRect returns x=-32000,y=-32000 when window is minimized
WINDOWPLACEMENT wp;
GetWindowPlacement(hwnd, &wp);
rc = wp.rcNormalPosition;

View File

@ -35,13 +35,9 @@ struct pWindow : pObject {
auto setTitle(string text) -> void;
auto setVisible(bool visible) -> void;
auto onClose() -> void;
auto onDrop(WPARAM wparam) -> void;
auto onEraseBackground() -> bool;
auto onModalBegin() -> void;
auto onModalEnd() -> void;
auto onMove() -> void;
auto onSize() -> void;
auto modalIncrement() -> void;
auto modalDecrement() -> void;
auto windowProc(HWND, UINT, WPARAM, LPARAM) -> maybe<LRESULT>;
auto _geometry() -> Geometry;
auto _modalityCount() -> unsigned;