Code:
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "resource.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
TCHAR szAppName[] = TEXT ("DibConv") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Gene Weaver 1"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxClient, cyClient ;
static int cxChar, cxCaps, cyChar ;
const int namelength = 11 ;
static OPENFILENAME ofn ;
TEXTMETRIC tm ;
static TCHAR szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
static TCHAR szFilter[] = TEXT ("Phylip Files (*.phy)\0*.PHY\0")
TEXT ("All Files (*.*)\0*.*\0\0") ;
TCHAR *CharFileName ;
static char **arraysequences ;
static char **arraynames ;
static int newchar = 0 ;
static int seqlength = 0, storeNumber = 0, storeNumber2 = 0, storeNumber3 = 0 ;
static int count = 0, charCount = 0, ntaxa = 0 ;
int i = 0 ;
HDC hdc ;
PAINTSTRUCT ps ;
FILE *In_File = NULL ;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = ( tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar/2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = szFileName ;
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrFileTitle = szTitleName ;
ofn.nMaxFileTitle = MAX_PATH ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = 0 ;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = TEXT ("phy") ;
ofn.lCustData = 0 ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
ReleaseDC (hwnd, hdc) ;
return 0 ;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDM_FILE_OPEN:
// Show the File Open dialog box
if (!GetOpenFileName (&ofn))
return 0 ;
CharFileName = (TCHAR*)szFileName ;
In_File = fopen(CharFileName, "r");
fscanf(In_File, "%d%d", &ntaxa, &seqlength);
arraysequences = malloc(ntaxa * sizeof( char *));
arraynames = malloc(ntaxa * sizeof( char *));
for( i = 0; i < ntaxa ; i++)
{
arraysequences[i] = malloc(seqlength) ;
arraynames[i] = malloc(namelength) ;
}
for ( count = 0 ; count < (ntaxa + 1) ; )
{
newchar = getc(In_File) ;
if ( newchar == EOF )
break ;
if (( count == ntaxa ) && ( newchar == '\n' ))
break;
if ( newchar == '\n' )
{
fscanf(In_File, "%s", arraynames[count] ) ;
storeNumber = charCount ;
storeNumber2 = storeNumber ;
charCount = 0 ;
count++ ;
}
if (( newchar != ' ' ) && ( newchar != '\n' ))
{
arraysequences[count-1][charCount] = newchar ;
charCount++ ;
}
}
while ( storeNumber2 < seqlength )
{
for ( count = -1 ; count < ntaxa ; )
{
newchar = getc(In_File) ;
if ( newchar == EOF )
break ;
if (( count == ntaxa ) && ( newchar == '\n' ))
break;
if ( newchar == '\n' )
{
charCount = storeNumber ;
count++ ;
}
else if ( newchar != ' ' )
{
arraysequences[count][charCount] = newchar ;
charCount++ ;
}
}
storeNumber = charCount + storeNumber ;
storeNumber2 = storeNumber ;
if ( newchar == EOF )
break ;
}
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
}
break ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
for ( count = 0 ; count < ntaxa ; count++ )
{
TextOut (hdc, 0, cyChar * count, arraynames[count], lstrlen (arraynames[count])) ;
TextOut (hdc, 11*cxCaps, cyChar * count, arraysequences[count], seqlength ) ;
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}