Make a copy of "freeze" library for storing stuff in registry, to

make //depot/solvespace/... entirely self-contained.

[git-p4: depot-paths = "//depot/solvespace/": change = 2216]
pull/3/head
Jonathan Westhues 2013-08-20 21:54:54 -08:00
parent defeb6411a
commit affbeafc6c
3 changed files with 244 additions and 7 deletions

View File

@ -1,16 +1,15 @@
DEFINES = /D_WIN32_WINNT=0x500 /DISOLATION_AWARE_ENABLED /D_WIN32_IE=0x500 /DWIN32_LEAN_AND_MEAN /DWIN32
# Use the multi-threaded static libc because libpng and zlib do; not sure if anything bad
# happens if those mix, but don't want to risk it.
CFLAGS = /W3 /nologo -MT -Iextlib -I..\common\win32 /D_DEBUG /D_CRT_SECURE_NO_WARNINGS /I. /Zi /EHs # /O2
CFLAGS = /W3 /nologo -MT -Iextlib /D_DEBUG /D_CRT_SECURE_NO_WARNINGS /I. /Zi /EHs # /O2
HEADERS = ..\common\win32\freeze.h ui.h solvespace.h dsc.h sketch.h expr.h polygon.h srf\surface.h
HEADERS = win32\freeze.h ui.h solvespace.h dsc.h sketch.h expr.h polygon.h srf\surface.h
OBJDIR = obj
FREEZE = $(OBJDIR)\freeze.obj
W32OBJS = $(OBJDIR)\w32main.obj \
$(OBJDIR)\w32util.obj \
$(OBJDIR)\freeze.obj \
SSOBJS = $(OBJDIR)\solvespace.obj \
$(OBJDIR)\textwin.obj \
@ -84,9 +83,6 @@ $(SRFOBJS): srf\$(@B).cpp $(HEADERS)
$(W32OBJS): win32/$(@B).cpp $(HEADERS)
@$(CC) $(CFLAGS) $(DEFINES) -c -Fo$(OBJDIR)/$(@B).obj win32/$(@B).cpp
$(FREEZE): ..\common\win32\$(@B).cpp $(HEADERS)
@$(CC) $(CFLAGS) $(DEFINES) -c -Fo$(OBJDIR)/$(@B).obj ..\common\win32\$(@B).cpp
$(RES): win32/$(@B).rc icon.ico
rc win32/$(@B).rc
mv win32/$(@B).res $(OBJDIR)/$(@B).res

208
win32/freeze.cpp Normal file
View File

@ -0,0 +1,208 @@
/*
* A library for storing parameters in the registry.
*
* Jonathan Westhues, 2002
*/
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
/*
* store a window's position in the registry, or fail silently if the registry calls don't work
*/
void FreezeWindowPosF(HWND hwnd, char *subKey, char *name)
{
RECT r;
GetWindowRect(hwnd, &r);
HKEY software;
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS)
return;
char *keyName = (char *)malloc(strlen(name) + 30);
if(!keyName)
return;
HKEY sub;
if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_left", name);
if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.left), sizeof(DWORD)) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_right", name);
if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.right), sizeof(DWORD)) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_top", name);
if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.top), sizeof(DWORD)) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_bottom", name);
if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.bottom), sizeof(DWORD)) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_maximized", name);
DWORD v = IsZoomed(hwnd);
if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(v), sizeof(DWORD)) != ERROR_SUCCESS)
return;
free(keyName);
}
static void Clamp(LONG *v, LONG min, LONG max)
{
if(*v < min) *v = min;
if(*v > max) *v = max;
}
/*
* retrieve a window's position from the registry, or do nothing if there is no info saved
*/
void ThawWindowPosF(HWND hwnd, char *subKey, char *name)
{
HKEY software;
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS)
return;
HKEY sub;
if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS)
return;
char *keyName = (char *)malloc(strlen(name) + 30);
if(!keyName)
return;
DWORD l;
RECT r;
sprintf(keyName, "%s_left", name);
l = sizeof(DWORD);
if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.left), &l) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_right", name);
l = sizeof(DWORD);
if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.right), &l) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_top", name);
l = sizeof(DWORD);
if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.top), &l) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_bottom", name);
l = sizeof(DWORD);
if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.bottom), &l) != ERROR_SUCCESS)
return;
sprintf(keyName, "%s_maximized", name);
DWORD v;
l = sizeof(DWORD);
if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&v, &l) != ERROR_SUCCESS)
return;
if(v)
ShowWindow(hwnd, SW_MAXIMIZE);
RECT dr;
GetWindowRect(GetDesktopWindow(), &dr);
// If it somehow ended up off-screen, then put it back.
Clamp(&(r.left), dr.left, dr.right);
Clamp(&(r.right), dr.left, dr.right);
Clamp(&(r.top), dr.top, dr.bottom);
Clamp(&(r.bottom), dr.top, dr.bottom);
if(r.right - r.left < 100) {
r.left -= 300; r.right += 300;
}
if(r.bottom - r.top < 100) {
r.top -= 300; r.bottom += 300;
}
Clamp(&(r.left), dr.left, dr.right);
Clamp(&(r.right), dr.left, dr.right);
Clamp(&(r.top), dr.top, dr.bottom);
Clamp(&(r.bottom), dr.top, dr.bottom);
MoveWindow(hwnd, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE);
free(keyName);
}
/*
* store a DWORD setting in the registry
*/
void FreezeDWORDF(DWORD val, char *subKey, char *name)
{
HKEY software;
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS)
return;
HKEY sub;
if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS)
return;
if(RegSetValueEx(sub, name, 0, REG_DWORD, (BYTE *)&val, sizeof(DWORD)) != ERROR_SUCCESS)
return;
}
/*
* retrieve a DWORD setting, or return the default if that setting is unavailable
*/
DWORD ThawDWORDF(DWORD val, char *subKey, char *name)
{
HKEY software;
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS)
return val;
HKEY sub;
if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS)
return val;
DWORD l = sizeof(DWORD);
DWORD v;
if(RegQueryValueEx(sub, name, NULL, NULL, (BYTE *)&v, &l) != ERROR_SUCCESS)
return val;
return v;
}
/*
* store a string setting in the registry
*/
void FreezeStringF(char *val, char *subKey, char *name)
{
HKEY software;
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS)
return;
HKEY sub;
if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS)
return;
if(RegSetValueEx(sub, name, 0, REG_SZ, (BYTE *)val, strlen(val)+1) != ERROR_SUCCESS)
return;
}
/*
* retrieve a string setting, or return the default if that setting is unavailable
*/
void ThawStringF(char *val, int max, char *subKey, char *name)
{
HKEY software;
if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS)
return;
HKEY sub;
if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS)
return;
DWORD l = max;
if(RegQueryValueEx(sub, name, NULL, NULL, (BYTE *)val, &l) != ERROR_SUCCESS)
return;
if(l >= (DWORD)max) return;
val[l] = '\0';
return;
}

33
win32/freeze.h Normal file
View File

@ -0,0 +1,33 @@
/*
* A library for storing parameters in the registry.
*
* Jonathan Westhues, 2002
*/
#ifndef __FREEZE_H
#define __FREEZE_H
#ifndef FREEZE_SUBKEY
#error must define FREEZE_SUBKEY to a string uniquely identifying the app
#endif
#define FreezeWindowPos(hwnd) FreezeWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd)
void FreezeWindowPosF(HWND hWnd, char *subKey, char *name);
#define ThawWindowPos(hwnd) ThawWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd)
void ThawWindowPosF(HWND hWnd, char *subKey, char *name);
#define FreezeDWORD(val) FreezeDWORDF(val, FREEZE_SUBKEY, #val)
void FreezeDWORDF(DWORD val, char *subKey, char *name);
#define ThawDWORD(val) val = ThawDWORDF(val, FREEZE_SUBKEY, #val)
DWORD ThawDWORDF(DWORD val, char *subKey, char *name);
#define FreezeString(val) FreezeStringF(val, FREEZE_SUBKEY, #val)
void FreezeStringF(char *val, char *subKey, char *name);
#define ThawString(val, max) ThawStringF(val, max, FREEZE_SUBKEY, #val)
void ThawStringF(char *val, int max, char *subKey, char *name);
#endif