📄 winmain.c
字号:
/*
*********************************************************************************************************
* uC/GUI
* Universal graphic software for embedded applications
*
* (c) Copyright 2002, Micrium Inc., Weston, FL
* (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
* 礐/GUI is protected by international copyright laws. Knowledge of the
* source code may not be used to write a similar product. This file may
* only be used in accordance with a license and should not be redistributed
* in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File : WinMain.c
Purpose : Windows Simulator, main program
---------------------------END-OF-HEADER------------------------------
*/
#include <windows.h>
#include <stdio.h>
#include "ResourceSim.h"
#include "LCD.h"
#include "GUI.h"
#include "SIM.h"
#include "LCDSIM.h"
#include "Branding\Branding.h"
#include "LCDSIM_Private.h"
/*********************************************************************
*
* Defines
*
**********************************************************************
*/
#ifndef SIM_WINMAIN
#define SIM_WINMAIN 1
#endif
#define countof(exp) (sizeof(exp)/sizeof(exp[0]))
#define HARDKEYS_MAX 128
#define LOG_COLOR_RED 1
/*********************************************************************
*
* Typedefs
*
**********************************************************************
*/
typedef void TaskMain(void);
/*********************************************************************
*
* Static const data
*
**********************************************************************
*/
typedef struct {
int VKey;
int Key;
} VKEY2KEY;
const VKEY2KEY aVKey2Key[] = {
{VK_UP, GUI_KEY_UP},
{VK_DOWN, GUI_KEY_DOWN},
{VK_RIGHT, GUI_KEY_RIGHT},
{VK_LEFT, GUI_KEY_LEFT},
{VK_HOME, GUI_KEY_HOME},
{VK_END, GUI_KEY_END},
{VK_SHIFT, GUI_KEY_SHIFT},
{VK_CONTROL, GUI_KEY_CONTROL},
{VK_BACK, GUI_KEY_BACKSPACE},
{VK_INSERT, GUI_KEY_INSERT},
{VK_DELETE, GUI_KEY_DELETE},
};
/*********************************************************************
*
* Static variables
*
**********************************************************************
*/
static int _xPosLCD = -1; // position of the LCD
static int _yPosLCD = -1; // position of the LCD
static int _rgbTransparent; // color for transparency
static int _MagX = 1; // Magnification X
static int _MagY = 1; // Magnification Y
static char _MessageBoxOnError = 1;
static HANDLE _hLogo;
static HBITMAP _ahBmpDevice[2];
static int _aLUTModifyCnt[LCDSIM_MAX_DISPLAYS];
static int _aModifyCnt[LCDSIM_MAX_DISPLAYS];
static struct {
int x0, y0, x1, y1;
char IsPressed;
SIM_HARDKEY_CB* pfCallback;
int Mode;
} _aHardkey[HARDKEYS_MAX];
static int _NumHardkeys;
static HANDLE _hFileError;
static HINSTANCE _hInst; // current instance
static HWND _hWndMain;
//static HWND _hParent;
static HWND _hWndLCD1;
static char _KeyBuffer;
static char _CmdKill;
static char * _pCmdLine;
static DWORD _SuspendCount;
static int _timeStartup;
static HMENU _hMenuPopup;
/* class names */
static const char acClassNameMain[] = "uC-GUI Simulation Mainframe";
static const char acClassNameLCDInfo[] = "uC-GUI LCD Info";
static const char acClassNameLCD[] = "uC-GUI LCD";
static const char acClassNameDevice[] = "uC-GUIDevice";
static const char acClassNameHardkey[] = "uC-GUIHardkey";
static const char acClassNameLog[] = "Log";
/* LOG-window */
static char _LOG_acBuffer[5000];
static HWND _LOG_hWnd;
static int _LOG_x0 = 0;
static int _LOG_y0;
static int _LOG_xsize = 500;
static int _LOG_ysize = 60;
/* thread management */
static HANDLE _ghThread = NULL;
/* multitasking support */
static U8 _NumTask;
static U32 _aThreadID[100];
static HANDLE _ahThread[100];
/* LCD-window */
typedef struct {
HWND hWnd;
int ModifyCnt;
} LCD_WINDATA;
/*********************************************************************
*
* Forward declarations
*
**********************************************************************
*/
void main(void);
void MainTask(void);
static LRESULT CALLBACK _WndProcMain(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK _WndProcLCD(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK _WndProcDevice(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK _WndProcLCDInfo(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK _WndProcHardkey(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
/*********************************************************************
*
* Static functions
*
**********************************************************************
*/
/*********************************************************************
*
* _VirtKey2Key
*/
static int _VirtKey2Key(int VirtKey) {
int i;
for (i=0; aVKey2Key[i].VKey; i++) {
if (aVKey2Key[i].VKey == VirtKey) {
return aVKey2Key[i].Key;
}
}
return 0;
}
/*********************************************************************
*
* _Keydown2ASCII
*/
static U16 _Keydown2ASCII(WPARAM wParam) {
int Ret;
U8 aState[256];
U16 Key;
GetKeyboardState(aState);
Ret = ToAscii(wParam, MapVirtualKey(wParam, 0), aState, &Key, 0);
if (Ret == 1) {
return Key & 255;
}
return (U16)wParam;
}
/*********************************************************************
*
* _MessageBox1
*/
static void _MessageBox1(LPCTSTR lpText) {
MessageBox(_hWndMain, lpText, BRANDING_GetAppNameShort(), MB_OK | MB_ICONEXCLAMATION);
}
/*********************************************************************
*
* _LinesIntersect
*/
static int _LinesIntersect(int xLeft0 ,int xRight0, int xLeft1 ,int xRight1) {
int x0 = max(xLeft0, xLeft1);
int x1 = min(xRight0, xRight1);
return (x1 - x0 >= 0);
}
/*********************************************************************
*
* _GetWidthOfBitmap
*/
static int _GetWidthOfBitmap(HBITMAP hBmp) {
BITMAP bmp;
GetObject(hBmp, sizeof(bmp), &bmp);
return bmp.bmWidth;
}
/*********************************************************************
*
* _GetYSizeOfBitmap
*/
static int _GetYSizeOfBitmap(HBITMAP hBmp) {
BITMAP bmp;
GetObject(hBmp, sizeof(bmp), &bmp);
return bmp.bmHeight;
}
/*********************************************************************
*
* _GetScanLine
*/
static void _GetScanLine(HBITMAP hBmp, int y, DWORD* pRGB) {
if (hBmp) {
HDC hdc;
BITMAPV4HEADER BmpInfo = {0};
BmpInfo.bV4Size = sizeof(BmpInfo);
hdc = GetWindowDC(NULL);
/* Fill in the Bitmap info structure */
GetDIBits(hdc, hBmp, 0, 0, NULL, (BITMAPINFO*)&BmpInfo, DIB_RGB_COLORS);
BmpInfo.bV4V4Compression = BI_RGB;
BmpInfo.bV4BitCount = 32;
if (BmpInfo.bV4Height > 0)
y = BmpInfo.bV4Height - y - 1;
GetDIBits(hdc, hBmp, y, 1, pRGB, (BITMAPINFO*)&BmpInfo, DIB_RGB_COLORS);
}
}
/*********************************************************************
*
* _CompareBits
*/
static int _CompareBits(U32 Color0, U32 Color1) {
if ((Color0 & 0xffffff) == (Color1 & 0xffffff)) {
return 0;
}
return 1;
}
/*********************************************************************
*
* _SetBitmapRegion
This is a key function for the display of the bitmap. It assigns
a clipping region to the window identical to the outline of the
bitmap.
*/
static void _SetBitmapRegion(HWND hWnd, HBITMAP hBmp, DWORD rgbTrans, char IsHardkey) {
if (hBmp) {
HRGN hRgn = 0;
HRGN hRgn0;
POINT Point = {0};
DWORD acBits[4096];
int y;
int yOff = 0;
int xOff = 0;
int XSize = _GetWidthOfBitmap(hBmp);
int YSize = _GetYSizeOfBitmap(hBmp);
RECT WinRect;
if (IsHardkey) {
HWND hWndParent = GetParent(hWnd);
ClientToScreen(hWndParent, &Point);
GetWindowRect(hWnd, &WinRect);
YSize = WinRect.bottom - WinRect.top;
yOff = WinRect.top - Point.y;
xOff = WinRect.left - Point.x;
}
for (y = 0; y < YSize; y++) {
int i, i0, i1;
_GetScanLine(hBmp, y + yOff, &acBits[0]);
for (i = 0; i < XSize;) {
while (_CompareBits(acBits[i], rgbTrans) == 0 && i < XSize) {
i++;
}
i0 = i;
while (_CompareBits(acBits[i], rgbTrans) != 0 && i < XSize) {
i++;
}
i1 = i;
if (i0 <XSize) {
hRgn0 = CreateRectRgn(i0 - xOff, y, i1 - xOff, y + 1);
if (hRgn0) {
if (hRgn) {
CombineRgn(hRgn, hRgn, hRgn0, RGN_OR);
DeleteObject(hRgn0);
} else {
hRgn = hRgn0;
}
} else {
_MessageBox1("Could not create region");
}
}
}
}
SetWindowRgn(hWnd, hRgn,0); // Note: Region is now owned by the system, do not delete
}
}
/*********************************************************************
*
* Find hardkeys
*/
static void _AddHardkeyRegion(int y, int x0, int x1) {
//
// Find a hardkey that we can attach this segment to
//
int i;
for (i = 0; i < countof(_aHardkey); i++) {
if (x1 > x0) {
if (_aHardkey[i].y1 + 1 == y) {
if (_LinesIntersect(x0, x1, _aHardkey[i].x0, _aHardkey[i].x1)) {
_aHardkey[i].y1++;
_aHardkey[i].x0 = min(_aHardkey[i].x0, x0);
_aHardkey[i].x1 = max(_aHardkey[i].x1, x1);
return; // We are done since there can be only one Rect
}
}
}
}
//
// Find a free hardkey
//
for (i = 0; i < countof(_aHardkey); i++) {
if ((_aHardkey[i].x1 == 0) && (_aHardkey[i].x0 == 0)) {
_aHardkey[i].x0 = x0;
_aHardkey[i].x1 = x1;
_aHardkey[i].y0 = y;
_aHardkey[i].y1 = y;
_NumHardkeys++;
return;
}
}
SIM_ErrorOut("_AddHardkeyRegion: - Too many hardkeys defined in bitmap");
}
/*********************************************************************
*
* _FindHardkeys
*/
static void _FindHardkeys(HBITMAP hBmp, DWORD rgbTrans) {
DWORD aColor[4096];
if (hBmp) {
int x, y;
int XSize = _GetWidthOfBitmap(hBmp);
int yMax = _GetYSizeOfBitmap(hBmp);
for (y = 0; y < yMax; y++) {
int x0, x1;
_GetScanLine(hBmp, y, &aColor[0]);
for (x = 0; x < XSize;) {
while ((_CompareBits(aColor[x], rgbTrans) == 0) && (x < XSize)) {
x++;
}
x0 = x;
while ((_CompareBits(aColor[x], rgbTrans) != 0) && (x < XSize)) {
x++;
}
x1 = x;
if (x1 > x0) {
_AddHardkeyRegion(y, x0, x1);
}
}
}
}
}
/*********************************************************************
*
* _GetYSizePhysEx
*/
static int _GetYSizePhysEx(int LayerIndex) {
return LCD_GetYSizeEx(LayerIndex) * LCD_GetYMagEx(LayerIndex);
}
/*********************************************************************
*
* _GetXSizePhysEx
*/
static int _GetXSizePhysEx(int LayerIndex) {
return LCD_GetXSizeEx(LayerIndex) * LCD_GetXMagEx(LayerIndex);
}
/*********************************************************************
*
* _GetXSizePhys, _GetYSizePhys
*/
static int _GetYSizePhys(void) { return _GetYSizePhysEx(0);}
static int _GetXSizePhys(void) { return _GetXSizePhysEx(0);}
/*********************************************************************
*
* Static functions: Log window
*
**********************************************************************
*/
/*********************************************************************
*
* _LOG_Paint
*/
static void _LOG_Paint(HWND hWnd, HDC hdc) {
RECT rt;
int i, NumLines, WinSizeY;
HANDLE hFont;
SIZE size;
char *s = _LOG_acBuffer;
GetClientRect(hWnd, &rt);
WinSizeY = rt.bottom-rt.top;
if (*s == '\n')
s++;
hFont = GetStockObject(DEFAULT_GUI_FONT);
SelectObject(hdc, hFont);
GetTextExtentPoint32(hdc, "@", 1, &size);
//
// Count Lines
NumLines=1;
for (i = 0; *(s + i); i++)
if (*(s + i) == '\n')
NumLines++;
//
// Recalc layout
rt.top += WinSizeY-NumLines * size.cy;
while (*s) {
SIZE SegSize;
int Len;
switch (*s) {
case 0:
return;
case LOG_COLOR_RED:
SetTextColor(hdc, RGB(0, 0, 0xff));
break;
case '\n':
rt.top += size.cy;
rt.left = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -