?? main.c
字號:
/* !! DO NOT REMOVE THIS COMMENT !!
*
* Author: Ga歱er Raj歟k
* E-mail, updates & bugs report: gape.korn@volja.net
*
* Created: 03.08.2003 at 12.00
*
* Description: main menu nad main functions for this program
*
*/
# include "main.h"
# include "help.h"
# include "author.h"
int main ()
{
short ASCII = 0, Line = 0, Row = 0, Result = 0, SpaceBetweenNumbers = 0;
long CodePage = 0;
SHAPE_COMPONENT *ASCIIFrame = NULL, *MainFrame = NULL, *lcLine = NULL;
GROUPBOX_COMPONENT *gbCodePage = NULL;
// input variables
INPUT_RECORD ir = {0};
DWORD Read = 0;
// common variables
short i = 0, j = 0; // counters
short HelpIsDisplayed = 0; // flag which becomes true if help text is displayed in the status line
// variables for menu buttons (Show ASCII Table, Help, Author, Exit)
short MainMenuLoop = 0, xStartMenu[MAIN_MENU_NO_OF_ITEMS] = {0}, MainMenuValue = MAIN_MENU_DEFAULT_SELECTED;
short xEndMenu[MAIN_MENU_NO_OF_ITEMS] = {0}, yStartMenu[MAIN_MENU_NO_OF_ITEMS] = {0}, ReDrawMainMenu = 0;
short yItemNameLength = 0, xItemNameLength = 0, MenuTempIndex = 0, MenuSelected = 1;
short WriteMenu = 0, MarkMenu = 0, MenuIndex = 0, MenuClicked = 0;
char *MainMenuItems[MAIN_MENU_NO_OF_ITEMS] = {0}, *MainMenuHelp[MAIN_MENU_NO_OF_ITEMS] = {0};
// edit box variables
short ebX[CODEPAGE_EB_WHOLE_NO] = {0}, ebY[CODEPAGE_EB_WHOLE_NO] = {0}, ebLength[CODEPAGE_EB_WHOLE_NO] = {0};
short ebSelected[CODEPAGE_EB_WHOLE_NO] = {0}, x[CODEPAGE_EB_WHOLE_NO] = {0}, ebIndex[CODEPAGE_EB_WHOLE_NO] = {0};
WORD ebColor[CODEPAGE_EB_WHOLE_NO] = {0};
char ebCodePage[11] = {0};
// options
HANDLE Stdin = GetStdHandle (STD_INPUT_HANDLE);
WORD ItemColor = 0, ItemHighLightColor = 0, DisabledItemColor = 0, DisabledItemHighLightColor = 0;
WORD HelpColor = 0;
short DisabledItem[MAIN_MENU_NO_OF_ITEMS] = {0}, ShowHelp = 0;
// initialization
ShowScreenCursor (0);
SetConsoleCP (1250);
SetConsoleOutputCP (1250);
ClrScr (0, 0, 80, 25, BACK_LIGHTGREY, ' ');
Print (29, 1, BACK_LIGHTGREY, "A S C I I T A B L E");
// reload menu item colors
ItemColor = BACK_LIGHTGREY;
ItemHighLightColor = FRONT_WHITE | BACK_LIGHTGREY;
DisabledItemColor = FRONT_DARKGREEN;
DisabledItemHighLightColor = FRONT_LIGHTGREEN;
HelpColor = FRONT_WHITE | BACK_LIGHTGREY;
// enable buttons
DisabledItem[0] = FALSE;
DisabledItem[1] = FALSE;
DisabledItem[2] = FALSE;
DisabledItem[3] = FALSE;
ShowHelp = TRUE;
// set console title
ConsoleTitle (PROGRAM_TITLE);
// group box component for code page
if ((gbCodePage = (GROUPBOX_COMPONENT *) malloc (sizeof (GROUPBOX_COMPONENT))) != NULL)
{
gbCodePage->x = 39;
gbCodePage->y = 9;
gbCodePage->Color = FRONT_DARKBLUE | BACK_LIGHTGREY;
gbCodePage->DoubleLine = TRUE;
gbCodePage->Height = 13;
gbCodePage->Length = 39;
gbCodePage->Pattern = ' ';
strncpy (gbCodePage->Title, "Codepage & ASCII code", strlen ("Codepage & ASCII code") + 1);
gbCodePage->TitleColor = FRONT_LIGHTBLUE | BACK_LIGHTGREY;
GroupBoxComponent (gbCodePage);
}
else
{
ErrorMsg ("Error occured while allocating space for gbCodePage variable in function main (file \"main.c\").");
}
// straight line
if ((lcLine = (SHAPE_COMPONENT *) malloc (sizeof (SHAPE_COMPONENT))) != NULL)
{
lcLine->LINE.x = gbCodePage->x + 2;
lcLine->LINE.y = gbCodePage->y + 9;
lcLine->LINE.Height = 1;
lcLine->LINE.Length = 33;
lcLine->LINE.DoubleLine = FALSE;
lcLine->LINE.Horizontal = TRUE;
lcLine->LINE.Color = FRONT_DARKBLUE | BACK_LIGHTGREY;
LineComponent (lcLine);
free (lcLine);
lcLine = NULL;
}
else
{
ErrorMsg ("Error occured while allocating space for lcLine variable in function main (file \"main.c\").");
}
// enter title for the edit box
Print (gbCodePage->x + 2, gbCodePage->y + 1, BACK_LIGHTGREY, "Enter code page:");
// text in the ASCII & codepage groupbox
Print (gbCodePage->x + 2, gbCodePage->y + 10, BACK_LIGHTGREY, "Formula = 16 * Line + Row");
// draws a main frame
if ((MainFrame = (SHAPE_COMPONENT *) malloc (sizeof (SHAPE_COMPONENT))) != NULL)
{
MainFrame->InputCodePage = 1250;
MainFrame->OutputCodePage = 1250;
MainFrame->FRAME.x = 0;
MainFrame->FRAME.y = 0;
MainFrame->FRAME.Length = 79;
MainFrame->FRAME.Height = 24;
MainFrame->FRAME.DoubleLine = TRUE;
MainFrame->FRAME.WithStatusLine = TRUE;
MainFrame->FRAME.Color = FRONT_WHITE | BACK_LIGHTGREY;
FrameComponent (MainFrame);
// free memory
free (MainFrame);
MainFrame = NULL;
}
else
{
ErrorMsg ("Error occured while allocating space for variable MainFrame in function main (file \"main.c\").");
}
// draws a frame for ASCII Table
if ((ASCIIFrame = (SHAPE_COMPONENT *) malloc (sizeof (SHAPE_COMPONENT))) != NULL)
{
ASCIIFrame->FRAME.x = vX - 1;
ASCIIFrame->FRAME.y = hY - 1;
ASCIIFrame->FRAME.Color = FRONT_DARKBLUE | BACK_LIGHTGREY;
ASCIIFrame->FRAME.DoubleLine = TRUE;
ASCIIFrame->FRAME.Height = 19;
ASCIIFrame->FRAME.Length = 35;
ASCIIFrame->FRAME.WithStatusLine = FALSE;
ASCIIFrame->InputCodePage = 1250;
ASCIIFrame->OutputCodePage = 1250;
FrameComponent (ASCIIFrame);
free (ASCIIFrame);
ASCIIFrame = NULL;
}
else
{
ErrorMsg ("Error occured while allocating space for ASCIIFrame variable in function DrawASCIIFramework (file \"main.c\").");
}
// print numbers 0 - 15
for (i = 0, SpaceBetweenNumbers = 0; i < 16; i++, SpaceBetweenNumbers += 2)
{
if (i == 10) { Print (hX+SpaceBetweenNumbers, hY, BACK_LIGHTGREY, "A"); Print (vX, vY+i, BACK_LIGHTGREY, "A"); }
else if (i == 11) { Print (hX+SpaceBetweenNumbers, hY, BACK_LIGHTGREY, "B"); Print (vX, vY+i, BACK_LIGHTGREY, "B"); }
else if (i == 12) { Print (hX+SpaceBetweenNumbers, hY, BACK_LIGHTGREY, "C"); Print (vX, vY+i, BACK_LIGHTGREY, "C"); }
else if (i == 13) { Print (hX+SpaceBetweenNumbers, hY, BACK_LIGHTGREY, "D"); Print (vX, vY+i, BACK_LIGHTGREY, "D"); }
else if (i == 14) { Print (hX+SpaceBetweenNumbers, hY, BACK_LIGHTGREY, "E"); Print (vX, vY+i, BACK_LIGHTGREY, "E"); }
else if (i == 15) { Print (hX+SpaceBetweenNumbers, hY, BACK_LIGHTGREY, "F"); Print (vX, vY+i, BACK_LIGHTGREY, "F"); }
else { Print (hX+SpaceBetweenNumbers, hY, BACK_LIGHTGREY, "%d", i); Print (vX, vY+i, BACK_LIGHTGREY, "%d", i); }
}
// text: Show ASCII Table
if ((MainMenuItems[0] = (char *) malloc (sizeof (char) * (strlen ("Show ASCII Table") + 1))) != NULL)
{
strncpy (MainMenuItems[0], "Show ASCII Table", strlen ("Show ASCII Table") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[0] in function MainMenu (file \"menu.c\").");
}
// text: Help
if ((MainMenuItems[1] = (char *) malloc (sizeof (char) * (strlen ("Help") + 1))) != NULL)
{
strncpy (MainMenuItems[1], "Help", strlen ("Help") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[1] in function MainMenu (file \"menu.c\").");
}
// text: Author
if ((MainMenuItems[2] = (char *) malloc (sizeof (char) * (strlen ("Author") + 1))) != NULL)
{
strncpy (MainMenuItems[2], "Author", strlen ("Author") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[2] in function MainMenu (file \"menu.c\").");
}
// text: Exit
if ((MainMenuItems[3] = (char *) malloc (sizeof (char) * (strlen ("Exit") + 1))) != NULL)
{
strncpy (MainMenuItems[3], "Exit", strlen ("Exit") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[3] in function MainMenu (file \"menu.c\").");
}
// help for "Show ASCII Table"
if ((MainMenuHelp[0] = (char *) malloc (sizeof (char) * (strlen ("Shows ASCII Table for selected code page.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[0], "Shows ASCII Table for selected code page.", strlen ("Shows ASCII Table for selected code page.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[0] in function MainMenu (file \"menu.c\").");
}
// help for "Help"
if ((MainMenuHelp[1] = (char *) malloc (sizeof (char) * (strlen ("Shows help about this program.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[1], "Shows help about this program.", strlen ("Shows help about this program.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[1] in function MainMenu (file \"menu.c\").");
}
// help for "Author"
if ((MainMenuHelp[2] = (char *) malloc (sizeof (char) * (strlen ("Shows information about the author.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[2], "Shows information about the author.", strlen ("Shows information about the author.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[2] in function MainMenu (file \"menu.c\").");
}
// help for "Author"
if ((MainMenuHelp[3] = (char *) malloc (sizeof (char) * (strlen ("Exit the program.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[3], "Exit the program.", strlen ("Exit the program.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[3] in function MainMenu (file \"menu.c\").");
}
// with this, before the help gets printed, I can avoid to delete the previous text - very useful
// I don't have to use ClrScr function
for (i = 0; i < MAIN_MENU_NO_OF_ITEMS; i++)
{
for (j = 0; j < HELP_ERASE_LENGTH; j++)
{
if (MainMenuHelp[i][j] == '\0')
{
MainMenuHelp[i][j] = ' ';
MainMenuHelp[i][j+1] = '\0';
}
}
}
// calculate main menu coordinates
for (i = 0, xItemNameLength = 0, yItemNameLength = 0; i < MAIN_MENU_NO_OF_ITEMS; i++)
{
xStartMenu[i] = MAIN_MENU_X;
xEndMenu[i] = MAIN_MENU_X + (short) strlen (MainMenuItems[i]) - 1;
// checking which text is the longest
if (xEndMenu[i] - MAIN_MENU_X + 1 > xItemNameLength) xItemNameLength = xEndMenu[i] - MAIN_MENU_X + 1;
yStartMenu[i] = MAIN_MENU_Y + yItemNameLength;
// count the length of menu button names -> that is 1 + space between items
yItemNameLength += 1;
if (i != MAIN_MENU_NO_OF_ITEMS-1) yItemNameLength += 0 + MAIN_MENU_SPACE_BETWEEN_ITEMS;
}
// print main menu
for (i = 0; i < MAIN_MENU_NO_OF_ITEMS; i++)
{
if (MAIN_MENU_DEFAULT_SELECTED == i)
{
// checking if the menu item is disabled. If it is then color it diferently
if (DisabledItem[i]) Print (xStartMenu[i], yStartMenu[i], DisabledItemHighLightColor, "%s", MainMenuItems[i]);
else Print (xStartMenu[i], yStartMenu[i], ItemHighLightColor, "%s", MainMenuItems[i]);
MarkMenu = MenuSelected = 1;
MenuTempIndex = i;
// print help in the status line
if (ShowHelp)
{
Print (HELP_X, HELP_Y, HelpColor, "%s", MainMenuHelp[i]);
HelpIsDisplayed = 1;
}
}
else
{
if (DisabledItem[i]) Print (xStartMenu[i], yStartMenu[i], DisabledItemColor, "%s", MainMenuItems[i]);
else Print (xStartMenu[i], yStartMenu[i], ItemColor, "%s", MainMenuItems[i]);
}
}
// file name edit box initialization
ebX[CODEPAGE_EB_CODEPAGE] = 41;
ebY[CODEPAGE_EB_CODEPAGE] = 11;
ebLength[CODEPAGE_EB_CODEPAGE] = 10;
ebColor[CODEPAGE_EB_CODEPAGE] = BACK_WHITE;
for (i = 0; i < ebLength[CODEPAGE_EB_CODEPAGE]; i++)
{
Print (ebX[CODEPAGE_EB_CODEPAGE]+i, ebY[CODEPAGE_EB_CODEPAGE], ebColor[CODEPAGE_EB_CODEPAGE], " ");
}
// main menu loop
MainMenuLoop = 1;
while (MainMenuLoop)
{
if (ReDrawMainMenu)
{
// reload menu item colors
ItemColor = BACK_LIGHTGREY;
ItemHighLightColor = FRONT_WHITE | BACK_LIGHTGREY;
DisabledItemColor = FRONT_DARKGREEN;
DisabledItemHighLightColor = FRONT_LIGHTGREEN;
HelpColor = FRONT_WHITE | BACK_LIGHTGREY;
// enable buttons
DisabledItem[0] = FALSE;
DisabledItem[1] = FALSE;
DisabledItem[2] = FALSE;
DisabledItem[3] = FALSE;
// text: Show ASCII Table
if ((MainMenuItems[0] = (char *) malloc (sizeof (char) * (strlen ("Show ASCII Table") + 1))) != NULL)
{
strncpy (MainMenuItems[0], "Show ASCII Table", strlen ("Show ASCII Table") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[0] in function MainMenu (file \"menu.c\").");
}
// text: Help
if ((MainMenuItems[1] = (char *) malloc (sizeof (char) * (strlen ("Help") + 1))) != NULL)
{
strncpy (MainMenuItems[1], "Help", strlen ("Help") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[1] in function MainMenu (file \"menu.c\").");
}
// text: Author
if ((MainMenuItems[2] = (char *) malloc (sizeof (char) * (strlen ("Author") + 1))) != NULL)
{
strncpy (MainMenuItems[2], "Author", strlen ("Author") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[2] in function MainMenu (file \"menu.c\").");
}
// text: Exit
if ((MainMenuItems[3] = (char *) malloc (sizeof (char) * (strlen ("Exit") + 1))) != NULL)
{
strncpy (MainMenuItems[3], "Exit", strlen ("Exit") + 1);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuItems[3] in function MainMenu (file \"menu.c\").");
}
// help for "Show ASCII Table"
if ((MainMenuHelp[0] = (char *) malloc (sizeof (char) * (strlen ("Shows ASCII Table for selected code page.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[0], "Shows ASCII Table for selected code page.", strlen ("Shows ASCII Table for selected code page.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[0] in function MainMenu (file \"menu.c\").");
}
// help for "Help"
if ((MainMenuHelp[1] = (char *) malloc (sizeof (char) * (strlen ("Shows help about this program.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[1], "Shows help about this program.", strlen ("Shows help about this program.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[1] in function MainMenu (file \"menu.c\").");
}
// help for "Author"
if ((MainMenuHelp[2] = (char *) malloc (sizeof (char) * (strlen ("Shows information about the author.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[2], "Shows information about the author.", strlen ("Shows information about the author.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[2] in function MainMenu (file \"menu.c\").");
}
// help for "Author"
if ((MainMenuHelp[3] = (char *) malloc (sizeof (char) * (strlen ("Exit the program.") + 1 + HELP_ERASE_LENGTH))) != NULL)
{
strncpy (MainMenuHelp[3], "Exit the program.", strlen ("Exit the program.") + 1 + HELP_ERASE_LENGTH);
}
else
{
ErrorMsg ("Error occured while allocationg space for variable MainMenuHelp[3] in function MainMenu (file \"menu.c\").");
}
// with this, before the help gets printed, I can avoid to delete the previous text - very useful
// I don't have to use ClrScr function
for (i = 0; i < MAIN_MENU_NO_OF_ITEMS; i++)
{
for (j = 0; j < HELP_ERASE_LENGTH; j++)
{
if (MainMenuHelp[i][j] == '\0')
{
MainMenuHelp[i][j] = ' ';
MainMenuHelp[i][j+1] = '\0';
}
}
}
// calculate main menu coordinates
for (i = 0, xItemNameLength = 0, yItemNameLength = 0; i < MAIN_MENU_NO_OF_ITEMS; i++)
{
xStartMenu[i] = MAIN_MENU_X;
xEndMenu[i] = MAIN_MENU_X + (short) strlen (MainMenuItems[i]) - 1;
// checking which text is the longest
if (xEndMenu[i] - MAIN_MENU_X + 1 > xItemNameLength) xItemNameLength = xEndMenu[i] - MAIN_MENU_X + 1;
yStartMenu[i] = MAIN_MENU_Y + yItemNameLength;
// count the length of menu button names -> that is 1 + space between items
yItemNameLength += 1;
if (i != MAIN_MENU_NO_OF_ITEMS-1) yItemNameLength += 0 + MAIN_MENU_SPACE_BETWEEN_ITEMS;
}
// flags and values
MainMenuValue = MAIN_MENU_DEFAULT_SELECTED;
WriteMenu = 1;
MarkMenu = 1;
MenuSelected = 1;
MenuClicked = 0;
ReDrawMainMenu = FALSE;
} // end if ReDrawMainMenu
ReadConsoleInput (Stdin, &ir, 1, &Read);
// mouse
if (ir.EventType == MOUSE_EVENT)
{
if (ir.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
{
// within the range of a main menu items
if ( (ir.Event.MouseEvent.dwMousePosition.X >= xStartMenu[MenuIndex]) &&
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -