add hooks for when the window gets/loses focus.

properly refocus draw area.
lay base for dragdrop.
This commit is contained in:
StapleButter 2017-09-23 02:58:04 +02:00
parent a9cacb9dd7
commit 14b7cf1987
3 changed files with 89 additions and 7 deletions

View File

@ -106,8 +106,6 @@ _UI_EXTERN void uiWindowContentSize(uiWindow *w, int *width, int *height);
_UI_EXTERN void uiWindowSetContentSize(uiWindow *w, int width, int height);
_UI_EXTERN int uiWindowFullscreen(uiWindow *w);
_UI_EXTERN void uiWindowSetFullscreen(uiWindow *w, int fullscreen);
_UI_EXTERN void uiWindowOnContentSizeChanged(uiWindow *w, void (*f)(uiWindow *, void *), void *data);
_UI_EXTERN void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *w, void *data), void *data);
_UI_EXTERN int uiWindowBorderless(uiWindow *w);
_UI_EXTERN void uiWindowSetBorderless(uiWindow *w, int borderless);
_UI_EXTERN void uiWindowSetChild(uiWindow *w, uiControl *child);
@ -115,6 +113,12 @@ _UI_EXTERN int uiWindowMargined(uiWindow *w);
_UI_EXTERN void uiWindowSetMargined(uiWindow *w, int margined);
_UI_EXTERN uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar);
_UI_EXTERN void uiWindowOnContentSizeChanged(uiWindow *w, void (*f)(uiWindow *, void *), void *data);
_UI_EXTERN void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *w, void *data), void *data);
_UI_EXTERN void uiWindowOnDropFile(uiWindow *w, void (*f)(uiWindow *w, char *file, void *data), void *data);
_UI_EXTERN void uiWindowOnGetFocus(uiWindow *w, void (*f)(uiWindow *w, void *data), void *data);
_UI_EXTERN void uiWindowOnLoseFocus(uiWindow *w, void (*f)(uiWindow *w, void *data), void *data);
typedef struct uiButton uiButton;
#define uiButton(this) ((uiButton *) (this))
_UI_EXTERN char *uiButtonText(uiButton *b);

View File

@ -10,16 +10,23 @@ struct uiWindow {
uiControl *child;
BOOL shownOnce;
int visible;
int (*onClosing)(uiWindow *, void *);
void *onClosingData;
int margined;
BOOL hasMenubar;
void (*onContentSizeChanged)(uiWindow *, void *);
void *onContentSizeChangedData;
BOOL changingSize;
int fullscreen;
WINDOWPLACEMENT fsPrevPlacement;
int borderless;
int (*onClosing)(uiWindow *, void *);
void *onClosingData;
void (*onContentSizeChanged)(uiWindow *, void *);
void *onContentSizeChangedData;
void (*onDropFile)(uiWindow *, char *, void *);
void *onDropFileData;
void (*onGetFocus)(uiWindow *, void *);
void *onGetFocusData;
void (*onLoseFocus)(uiWindow *, void *);
void *onLoseFocusData;
};
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
@ -108,6 +115,37 @@ static LRESULT CALLBACK windowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARA
mmi->ptMinTrackSize.x = width;
mmi->ptMinTrackSize.y = height;
return lResult;
case WM_DROPFILES:
if (w->onDropFile)
{
HDROP eggdrop = (HDROP)wParam;
WCHAR filename[1024];
DragQueryFile(eggdrop, 0, filename, 1024);
char* filename8 = toUTF8(filename);
w->onDropFile(w, filename8, w->onDropFileData);
uiFreeText(filename8);
DragFinish(eggdrop);
}
break;
case WM_SETFOCUS:
if (w->onGetFocus)
{
w->onGetFocus(w, w->onGetFocusData);
return 0;
}
break;
case WM_KILLFOCUS:
if (w->onLoseFocus)
{
w->onLoseFocus(w, w->onLoseFocusData);
return 0;
}
break;
case WM_PRINTCLIENT:
// we do no special painting; just erase the background
// don't worry about the return value; we let DefWindowProcW() handle this message
@ -383,6 +421,24 @@ void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
w->onClosingData = data;
}
void uiWindowOnDropFile(uiWindow *w, void (*f)(uiWindow *, char *, void *), void *data)
{
w->onDropFile = f;
w->onDropFileData = data;
}
void uiWindowOnGetFocus(uiWindow *w, void (*f)(uiWindow *, void *), void *data)
{
w->onGetFocus = f;
w->onGetFocusData = data;
}
void uiWindowOnLoseFocus(uiWindow *w, void (*f)(uiWindow *, void *), void *data)
{
w->onLoseFocus = f;
w->onLoseFocusData = data;
}
int uiWindowBorderless(uiWindow *w)
{
return w->borderless;
@ -492,6 +548,10 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
uiWindowOnClosing(w, defaultOnClosing, NULL);
uiWindowOnContentSizeChanged(w, defaultOnPositionContentSizeChanged, NULL);
uiWindowOnDropFile(w, NULL, NULL);
uiWindowOnGetFocus(w, NULL, NULL);
uiWindowOnLoseFocus(w, NULL, NULL);
windows[w] = true;
return w;
}

View File

@ -295,6 +295,21 @@ int OnCloseWindow(uiWindow* window, void* blarg)
return 1;
}
void OnDropFile(uiWindow* window, char* file, void* blarg)
{
printf("DROP: %s\n", file);
}
void OnGetFocus(uiWindow* window, void* blarg)
{
uiControlSetFocus(uiControl(MainDrawArea));
}
void OnLoseFocus(uiWindow* window, void* blarg)
{
// TODO: shit here?
}
void OnCloseByMenu(uiMenuItem* item, uiWindow* window, void* blarg)
{
uiControlDestroy(uiControl(window));
@ -431,6 +446,9 @@ int main(int argc, char** argv)
MainWindow = uiNewWindow("melonDS " MELONDS_VERSION, 256, 384, 1);
uiWindowOnClosing(MainWindow, OnCloseWindow, NULL);
uiWindowOnDropFile(MainWindow, OnDropFile, NULL);
uiWindowOnGetFocus(MainWindow, OnGetFocus, NULL);
uiWindowOnLoseFocus(MainWindow, OnLoseFocus, NULL);
uiMenuItemDisable(MenuItem_Pause);
uiMenuItemDisable(MenuItem_Reset);
@ -452,7 +470,7 @@ int main(int argc, char** argv)
EmuThread = SDL_CreateThread(EmuThreadFunc, "melonDS magic", NULL);
uiControlShow(uiControl(MainWindow));
//uiControlSetFocus(uiControl(MainDrawArea)); // TODO: this needs to be done when the window regains focus
uiControlSetFocus(uiControl(MainDrawArea));
uiMain();
EmuRunning = 0;