campo-sirio/include/os_win32.cpp
guy 4f99172ae0 Patch level :
Files correlati     :
Ricompilazione Demo : [ ]
Commento            :
archives.cpp  Aggiunto flag inutile ancora specificato dai vecchi programmi
*.c           Eliminato uso di CUpString
controls.cpp  Corretta resize_rect per tener conto della status bar
currency.cpp  Supporto cambio contro EURO
date.cpp      Migliorato operator=(long) in modo da accettare TODAY
dongle.cpp    Corretta gestione del prawin.ini
expr.cpp      Sostituiti alcuni "" con EMPTY_STRING
form.cpp      Supporto currency
isam.cpp      Aggiunte () per evitare warning di compilazione
os_win*.cpp   Migliorato supporto per minimizzazioe e massimizzazione
prefix.cpp    Aggiunta class TFirm coi dati della ditta
printapp.cpp  Supporto currency
printwin.cpp  Aggiunte () per evitare warning di compilazione
relapp.cpp    Migliorato delete_mode
text.cpp      Sostituiti CHECK inutili con NFCHECK
tree.cpp      Tolti ; errati da statement if
viswin.cpp    Supporto zoom
window.cpp    Aggiunta char2pixel per calcolare bene la dimensione dei caratteri
xvtility.cpp  Corretta resize rect per tener conto della status bar


git-svn-id: svn://10.65.10.50/trunk@7664 c028cbd2-c16b-5b4b-a496-9718f37d4682
1999-01-19 09:15:17 +00:00

538 lines
13 KiB
C++
Executable File

#define XVT_INCL_NATIVE
#define WIN32_LEAN_AND_MEAN
#include <os_dep.h>
#include <commdlg.h>
#include <ctype.h>
#include <applicat.h>
#include <colors.h>
#include <config.h>
#include <utility.h>
#include <window.h>
///////////////////////////////////////////////////////////
// Win32 event hook
///////////////////////////////////////////////////////////
HIDDEN BOOLEAN event_hook(HWND hwnd,
UINT msg,
UINT wparam,
ULONG lparam,
long* ret)
{
switch(msg)
{
case WM_MENUCHAR:
if (wparam > ' ' && wparam <= 'z')
{
WINDOW win = cur_win();
if (win != NULL_WIN)
{
const KEY key = toupper(wparam)+K_CTRL;
dispatch_e_char(win, key);
*ret = 2 << 16;
}
}
break;
case WM_KEYDOWN:
if (wparam == VK_F1)
{
if ((lparam & (1<<29)) == 0) // Il tasto alt non e' premuto
{
KEY k = K_F1;
int sc = GetAsyncKeyState(VK_CONTROL); // Stato del tasto control
if (sc & 0x8000) k += K_CTRL;
int ss = GetAsyncKeyState(VK_SHIFT); // Stato del tasto shift
if (ss & 0x8000) k += K_SHIFT;
WINDOW win = cur_win();
if (win != NULL_WIN)
dispatch_e_char(win, k);
}
}
break;
default:
break;
}
return TRUE; // Continua col processo normale
}
///////////////////////////////////////////////////////////
// Operating system dependent functions
///////////////////////////////////////////////////////////
bool os_allow_another_instance()
{
return TRUE;
}
COLOR os_choose_color(COLOR col, WINDOW win)
{
CHOOSECOLOR cc;
memset(&cc, 0, sizeof(cc)); // Azzera struttura
if (win == NULL_WIN) win = TASK_WIN; // Sceglie una finestra valida
HWND hwnd = (HWND)xvt_vobj_get_attr(win, ATTR_NATIVE_WINDOW);
HDC hdc = GetDC(hwnd);
// Legge la palette di sistema
PALETTEENTRY* pe = NULL;
int max_entries = 0;
if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
{
max_entries = GetDeviceCaps(hdc, SIZEPALETTE);
pe = new PALETTEENTRY[max_entries];
GetSystemPaletteEntries(hdc, 0, max_entries, pe);
}
ReleaseDC(hwnd, hdc);
// Definisce i 16 colori customizzabili
unsigned long custom_colors[16];
for (int c = 0; c < 16; c++)
{
if (pe)
{
const PALETTEENTRY& e = pe[c < 8 ? c : max_entries - 16 + c];
custom_colors[c] = RGB(e.peRed, e.peGreen, e.peBlue);
}
else
{
const unsigned char val = (c & 0x8) ? 255 : 127;
const unsigned char red = (c & 0x1) ? val : 0;
const unsigned char green = (c & 0x2) ? val : 0;
const unsigned char blue = (c & 0x4) ? val : 0;
custom_colors[c] = RGB(red, green, blue);
}
}
if (pe)
{
delete pe;
pe = NULL;
}
cc.lStructSize = sizeof(cc); // Setta dimensioni
cc.hwndOwner = hwnd; // Setta finestra padre
cc.rgbResult = RGB(XVT_COLOR_GET_RED(c), XVT_COLOR_GET_GREEN(c), XVT_COLOR_GET_BLUE(c));
cc.lpCustColors = custom_colors; // Fissa colori custom
cc.Flags = CC_RGBINIT; // Usa col come primo colore
if (ChooseColor(&cc) != 0)
col = RGB2COLOR(GetRValue(cc.rgbResult), GetGValue(cc.rgbResult), GetBValue(cc.rgbResult));
else
col = COLOR_INVALID;
return col;
}
bool os_deny_another_instance()
{
return FALSE;
}
bool os_destroy_native_icon(unsigned icon)
{
return DestroyIcon((HICON)icon) != 0;
}
void os_draw_native_icon(WINDOW win, const RCT& rct, unsigned icon)
{
HDC hdc = (HDC)xvt_vobj_get_attr(win, ATTR_NATIVE_GRAPHIC_CONTEXT);
int x = (rct.right + rct.left - 32) / 2;
int y = (rct.bottom + rct.top - 32) / 2;
DrawIcon(hdc, x, y, (HICON)icon);
}
unsigned long os_get_free_memory()
{
MEMORYSTATUS ms;
GlobalMemoryStatus(&ms);
return ms.dwAvailPhys;
}
bool os_is_removable_drive(const char* path)
{
return GetDriveType(path) == DRIVE_REMOVABLE;
}
bool os_is_network_drive(const char* path)
{
return GetDriveType(path) == DRIVE_REMOTE;
}
bool os_is_fixed_drive(const char* path)
{
UINT dt = GetDriveType(path);
return dt == DRIVE_FIXED || dt == DRIVE_CDROM;
}
bool os_test_disk_free_space(const char* path, unsigned long filesize)
{
/* Works on WinNT and Win95 OSR2 Only!
ULARGE_INTEGER nRequired;
ULARGE_INTEGER nAvailable;
nRequired.QuadPart = filesize;
GetDiskFreeSpaceEx(path, &nAvailable, NULL, NULL);
return nRequired.QuadPart <= nAvailable.QuadPart;
*/
// On Win95 may fail for disks > 2GB
DWORD nSecPerClust, nBytePerSec, nFreeClust;
bool ok = GetDiskFreeSpace(path, &nSecPerClust, &nBytePerSec, &nFreeClust, NULL) != 0;
if (ok && nBytePerSec >= 512)
{
__int64 nFree = nFreeClust;
nFree *= nSecPerClust;
nFree *= nBytePerSec;
ok = nFree > filesize;
}
else
ok = TRUE;
return ok;
}
unsigned long os_get_disk_size(const char* path)
{
DWORD nSecPerClust, nBytePerSec, nFreeClust, nTotalClust;
GetDiskFreeSpace(path, &nSecPerClust, &nBytePerSec, &nFreeClust, &nTotalClust);
__int64 nFree = nTotalClust;
nFree *= nSecPerClust;
nFree *= nBytePerSec;
unsigned long nVal = nFree > INT_MAX ? (unsigned long)INT_MAX
: (unsigned long)nFree;
return nVal;
}
void os_exec_help_command(MENU_TAG tag, const char* key)
{
TFilename hlp("prassi.hlp");
TString mod(key);
if (mod.not_empty())
{
mod.cut(2); mod.lower();
if (mod != "ba")
hlp.insert(mod, 0);
}
else
{
if (tag == M_HELP_ONCONTEXT)
tag = M_HELP_CONTENTS;
}
HWND hwnd = (HWND)xvt_vobj_get_attr(TASK_WIN, ATTR_NATIVE_WINDOW);
switch(tag)
{
case M_HELP_CONTENTS:
WinHelp(hwnd, hlp, HELP_CONTENTS, 0);
break;
case M_HELP_SEARCH:
WinHelp(hwnd, hlp, HELP_PARTIALKEY, (DWORD)"");
break;
case M_HELP_HELPONHELP:
WinHelp(hwnd, hlp, HELP_HELPONHELP, 0);
break;
case M_HELP_ONCONTEXT:
if (hlp.exist())
{
struct MULTIGUY
{
DWORD mkSize;
TCHAR mkKeylist;
TCHAR mkKeyphrase[16];
} mk;
mk.mkSize = sizeof(MULTIGUY);
mk.mkKeylist = 'M';
strncpy(mk.mkKeyphrase, key, sizeof(mk.mkKeyphrase));
WinHelp(hwnd, hlp, HELP_MULTIKEY, (DWORD)&mk);
}
break;
default: break;
}
}
int os_execute(const TFilename& path, bool sync, bool iconizetask, bool showchild)
{
int exitcode = -1;
STARTUPINFO start;
PROCESS_INFORMATION pi;
memset(&start, 0, sizeof(start));
start.cb = sizeof(start);
start.dwX = start.dwY = start.dwXSize = start.dwYSize = CW_USEDEFAULT;
start.dwFlags = STARTF_USESHOWWINDOW;
start.wShowWindow = showchild ? SW_SHOW : SW_HIDE;
BOOL started = CreateProcess(NULL,(char*)(const char*)path, NULL, NULL, FALSE, 0,
NULL, NULL, &start, &pi);
if (started)
{
HANDLE hProcess = pi.hProcess;
if (sync)
{
ATOM aBa0Atom = NULL;
if (iconizetask && showchild && main_app().name() == "ba0100")
{
TString str;
str << "ba0100->" << cmd2name(path);
aBa0Atom = GlobalAddAtom(str);
}
WaitForInputIdle(hProcess, INFINITE);
bool was_maximized = FALSE;
if (iconizetask)
{
TTemp_window tw(TASK_WIN);
RCT rct_scr, rct_tsk;
xvt_vobj_get_client_rect(SCREEN_WIN, &rct_scr);
xvt_vobj_get_client_rect(tw.win(), &rct_tsk);
was_maximized = rct_scr.right == rct_tsk.right;
tw.iconize();
tw.deactivate();
}
if (WaitForSingleObject(hProcess, INFINITE) != 0xFFFFFFFF)
{
unsigned long exit_code;
if (GetExitCodeProcess(hProcess, &exit_code))
exitcode = int(exit_code);
else
exitcode = -2;
}
if (aBa0Atom)
GlobalDeleteAtom(aBa0Atom);
if (iconizetask)
{
TTemp_window tw(TASK_WIN);
if (was_maximized)
tw.maximize();
else
os_restore_window(tw.win());
tw.activate();
do_events();
}
}
else
exitcode = 0;
CloseHandle(pi.hThread);
CloseHandle(hProcess);
}
else
{
exitcode = GetLastError();
error_box("Impossibile eseguire '%s': %d", (const char*)path, exitcode);
}
return exitcode;
}
int os_get_printer_names(TToken_string& t)
{
char* buf = t.get_buffer(4096); // ammazzao'
GetProfileString("devices", NULL, "", buf, t.size());
for (int i = 0; i < t.size(); i++)
{
if (buf[i] == '\0')
{
if (buf[i+1] != '\0')
buf[i] = t.separator();
else
break;
}
}
return t.items();
}
bool os_set_default_printer(const char* name)
{
CHECK(name && *name > ' ', "Null printer name");
TString pdev(name);
if (pdev.find(',') < 0)
{
TString szDevice(256);
GetProfileString ("devices", pdev, "", szDevice.get_buffer(), szDevice.size());
pdev << ',' << szDevice;
}
bool ok = WriteProfileString("windows", "device", pdev) != 0;
return ok;
}
bool os_get_default_printer(TString& name)
{
char* buf = name.get_buffer(256);
bool ok = GetProfileString ("windows", "device", ",,,", buf, name.size()) != 0;
return ok;
}
bool os_spawn_by_menu()
{
#ifdef DBG
bool ok = TRUE;
#else
TString str = main_app().name();
bool ok = str == "ba0100";
if (!ok)
{
str.insert("ba0100->", 0);
ok = GlobalFindAtom(str) != NULL;
}
#endif
return ok;
}
bool os_get_image_editor_path(TFilename& name)
{
// where is EasyDoc installed?
GetPrivateProfileString("Easydoc", "Path", "\\EASYDOC",
name.get_buffer(), name.size(),
"EasyDoc.ini");
// You're unlucky there is no EasyDoc
if (name.empty())
return FALSE;
// paste EasyDoc path
name.add("easydoc.exe");
// is EasyDoc present?
bool ok = name.exist();
return ok;
}
os_type os_get_type()
{
OSVERSIONINFO ovi;
GetVersionEx(&ovi);
os_type t;
switch (ovi.dwPlatformId)
{
case VER_PLATFORM_WIN32s : t = os_Win32s; break;
case VER_PLATFORM_WIN32_NT : t = os_WindowsNT; break;
case VER_PLATFORM_WIN32_WINDOWS: t = os_Windows95; break;
default : t = os_Unknown; break;
}
return t;
}
void os_post_menu_event(WINDOW win, MENU_TAG tag)
{
HWND w = (HWND)xvt_vobj_get_attr(win, ATTR_NATIVE_WINDOW);
PostMessage(w, WM_COMMAND, tag, 0L);
}
void os_set_event_hook()
{
xvt_vobj_set_attr(NULL_WIN, ATTR_EVENT_HOOK, (long)event_hook);
long twin_style = WSF_ICONIZABLE | WSF_CLOSE | WSF_SIZE;
const int scx = GetSystemMetrics(SM_CXSCREEN);
const int scy = GetSystemMetrics(SM_CYSCREEN);
static RCT rct;
if (scx <= 640 && os_get_type() >= os_Windows95)
{
const int bcx = GetSystemMetrics(SM_CXFRAME);
const int bcy = GetSystemMetrics(SM_CYFRAME);
rct.left = -bcx;
rct.top = GetSystemMetrics(SM_CYCAPTION)-bcy-1;
rct.right = scx+bcx;
rct.bottom = scy+bcy;
xvt_vobj_set_attr(NULL_WIN, ATTR_WIN_PM_TWIN_STARTUP_RCT, long(&rct));
}
else
{
TConfig cfg(CONFIG_USER, "Colors");
const int res = cfg.get_int("Resolution");
if (res >= 640 && res < scx)
{
const int width = res + 2*GetSystemMetrics(SM_CXFRAME);
const int height = (res * 3) / 4 + 2*GetSystemMetrics(SM_CYFRAME) - GetSystemMetrics(SM_CYCAPTION) - 1;
const int deltax = (scx - width) / 2;
const int deltay = (scy - height) / 2;
rct.left = deltax;
rct.top = deltay;
rct.right = deltax + width;
rct.bottom = deltay + height;
xvt_vobj_set_attr(NULL_WIN, ATTR_WIN_PM_TWIN_STARTUP_RCT, long(&rct));
}
else
twin_style |= WSF_MAXIMIZED;
}
xvt_vobj_set_attr(NULL_WIN,ATTR_WIN_PM_TWIN_STARTUP_STYLE, twin_style);
}
void os_wake_up_caller()
{
HWND hwnd = (HWND)xvt_vobj_get_attr(TASK_WIN, ATTR_NATIVE_WINDOW);
WinHelp(hwnd, "prassi.hlp", HELP_QUIT, 0L);
}
bool os_file_exist(const char* file)
{
return FALSE;
}
bool os_open_spool_row(const char* device)
{
return TRUE;
}
bool os_spool_row(const char* str)
{
return FALSE;
}
bool os_close_spool_row()
{
return TRUE;
}
void os_iconize_window(WINDOW win)
{
HWND hwnd = (HWND)xvt_vobj_get_attr(win, ATTR_NATIVE_WINDOW);
ShowWindow(hwnd, SW_MINIMIZE);
}
void os_maximize_window(WINDOW win)
{
HWND hwnd = (HWND)xvt_vobj_get_attr(win, ATTR_NATIVE_WINDOW);
HWND twin = (HWND)xvt_vobj_get_attr(TASK_WIN, ATTR_NATIVE_WINDOW);
if (hwnd != twin)
{
HWND pare = GetParent(hwnd);
RECT rct; GetClientRect(pare, &rct);
if (pare == twin)
rct.bottom -= 24;
SetWindowPos(hwnd, pare,
rct.left, rct.top, rct.right, rct.bottom,
SWP_NOZORDER);
}
else
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
}
void os_restore_window(WINDOW win)
{
HWND hwnd = (HWND)xvt_vobj_get_attr(win, ATTR_NATIVE_WINDOW);
ShowWindow(hwnd, SW_NORMAL);
}
bool os_dongle_server_running()
{
ATOM a = GlobalFindAtom("DONGLE_SERVER_ATOM");
return a != 0;
}