#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <shlobj.h>
#include <math.h>
#include <stdlib.h>
#include "cbgps.h"
#include "cbrc.h"
#include "cbgpx.h"
#include <string.h>
Data Structures | |
struct | _config |
Present configuration of the program. More... | |
struct | _cbsientry |
One configuration entry in the list of entries set in the serial port information callback. More... | |
struct | _cbsictx |
Context for the serial port information callback. More... | |
struct | _gpscbcontrols |
Window handles of controls to fill with current coordinates. More... | |
Defines | |
#define | MAX_STR_LEN 127 |
Maximal string length, withou tthe trailing 0. | |
#define | CFG_STR_LEN 31 |
Length of an configuration item. | |
#define | VAL_STR_LEN 15 |
Length of the string for a value retrieved by the GPS callback. | |
#define | SPRTF _snprintf |
#define | MENU_START_ENABLED 1 |
#define | MENU_STOP_ENABLED 2 |
#define | MENU_CONFIG_ENABLED 4 |
#define | MENU_GPXSTART_ENABLED 8 |
#define | MENU_GPXSTOP_ENABLED 16 |
#define | MENU_EXIT_ENABLED 32 |
#define | MENU_ALL_ENABLED ~0 |
#define | WM_CBSIREADY WM_USER + 1234 |
Message sent to the configuration window after the serial info callback is ready. | |
#define | WM_NEWCOORD WM_USER + 1235 |
Message sent to the main window, if new coordinates arrived. | |
#define | COORD_UTC 0x0001 |
Bitwise information, that UTC time changed. | |
#define | COORD_ALT 0x0002 |
Bitwise information, that the altitude changed. | |
#define | COORD_LAT 0x0004 |
Bitwise information, that latitude changed. | |
#define | COORD_LON 0x0008 |
Bitwise information, that longitude changed. | |
#define | COORD_UTMN 0x0010 |
Bitwise information, that UTM northing changed. | |
#define | COORD_UTME 0x0020 |
Bitwise information, that UTM easting changed. | |
#define | ENMENU(enmask, flag) |
Creates the value for enabling or disabling a menu item depending on the given flag and mask. | |
Functions | |
static void | _fmtErrMsg () |
Pronts last system error in a message box. | |
static void | _cbsientry_release (struct _cbsientry *first) |
Releases memory occupied by the list of serial port informations. | |
static void | _cbserinfocb (const char *aName, CBSERINFO aType, void *aCtx) |
Callback function for serial port information. | |
static void | _gpscb (double anUTC, double anNLat, double anELon, double anAlt, double aUTMN, double aUTME, void *aCtx) |
Callback routine retrieving current coordinates. | |
static void | _popupmenu (HWND hwnd, int x, int y, int enabled) |
Shows a popup menu at the given position. | |
static short | _dlggpxfilename (HWND hwnd, TCHAR *theFileName, size_t aLen) |
Opens common dialog for selecting the gpx file to create. | |
static BOOL CALLBACK | _cfgdlgproc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
Configuration dialog procedure. | |
static void | _cfgfilename (TCHAR *theFileName) |
Builds the configuration file name. The configuration file name is [ApplicationData]\[_APPNAME].cfg, if not compiled with -DUSE_ENV, else it is $HOME/[_APPNAME].cfg. | |
static void | _rdcfg (struct _config *aCfg) |
Reads configuration from a file into the given config structure. | |
static void | _wrcfg (const struct _config *aCfg) |
Writes configuration to the configuration file. | |
static LRESULT CALLBACK | _wndproc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
The standard MS Windows window procedure. | |
static HMODULE | _nlsresmod (HMODULE defHandle) |
Returns module handle for the resource dll responsible for the current locale. | |
int WINAPI | WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) |
The main function of the application. | |
Variables | |
static const TCHAR | _APPNAME [] = TEXT("cb") |
static TCHAR | _szQuit [MAX_STR_LEN+1] |
static TCHAR | _szOpenErr [MAX_STR_LEN+1] |
static TCHAR | _szCloseErr [MAX_STR_LEN+1] |
static TCHAR | _szFmtUTC [VAL_STR_LEN+1] |
static TCHAR | _szFmtNLat [VAL_STR_LEN+1] |
static TCHAR | _szFmtSLat [VAL_STR_LEN+1] |
static TCHAR | _szFmtELon [VAL_STR_LEN+1] |
static TCHAR | _szFmtWLon [VAL_STR_LEN+1] |
static TCHAR | _szFmtAlt [VAL_STR_LEN+1] |
static TCHAR | _szFmtUTME [VAL_STR_LEN+1] |
static TCHAR | _szFmtUTMN [VAL_STR_LEN+1] |
static TCHAR | _szGPXFilter [MAX_STR_LEN+1] |
static HMODULE | _hresmod |
Module for loading localized resources. |
#define CFG_STR_LEN 31 |
Length of an configuration item.
A configuration item is for example the name of the COM port, baud rate, number of databits etc.
#define COORD_ALT 0x0002 |
Bitwise information, that the altitude changed.
#define COORD_LAT 0x0004 |
Bitwise information, that latitude changed.
#define COORD_LON 0x0008 |
Bitwise information, that longitude changed.
#define COORD_UTC 0x0001 |
Bitwise information, that UTC time changed.
#define COORD_UTME 0x0020 |
Bitwise information, that UTM easting changed.
#define COORD_UTMN 0x0010 |
Bitwise information, that UTM northing changed.
#define ENMENU | ( | enmask, | |||
flag | ) |
Value:
(((enmask) & (flag)) == (flag))? (MF_BYCOMMAND | MF_ENABLED) :\ (MF_BYCOMMAND | MF_GRAYED)
enmask | the bitwise mask of currently enabled menu items. | |
flag | eg. MENU_START_ENABLED, MENU_STOP_ENABLED etc. |
#define MAX_STR_LEN 127 |
Maximal string length, withou tthe trailing 0.
#define MENU_ALL_ENABLED ~0 |
#define MENU_CONFIG_ENABLED 4 |
#define MENU_EXIT_ENABLED 32 |
#define MENU_GPXSTART_ENABLED 8 |
#define MENU_GPXSTOP_ENABLED 16 |
#define MENU_START_ENABLED 1 |
#define MENU_STOP_ENABLED 2 |
#define SPRTF _snprintf |
#define VAL_STR_LEN 15 |
Length of the string for a value retrieved by the GPS callback.
#define WM_CBSIREADY WM_USER + 1234 |
Message sent to the configuration window after the serial info callback is ready.
#define WM_NEWCOORD WM_USER + 1235 |
Message sent to the main window, if new coordinates arrived.
Message is sent in _gpscb() to the main window with a bitwise combination of the COORD_ constants in wParam.
static void _cbserinfocb | ( | const char * | aName, | |
CBSERINFO | aType, | |||
void * | aCtx | |||
) | [static] |
Callback function for serial port information.
Called for each available serial port parameter item: serial port name, baud, number of databits, parity, stopbits.
aName | the name of the setting, eg. name of the serial port, number of databits, stopbits as string, parity or baudrate name. It may be also NULL! | |
aType | type of the information in aName. For CBSI_BAUDRATE, CBSI_DATABITS, CBSI_PARITY, CBSI_STOPBITS, CBSI_SERIALPORT the values of aName and aType is added to the list of values. For CBSI_READY the configuration window is informed (PostMessage, WM_CBSIREADY), that it may put the informations into the controls. | |
aCtx | casted to struct _cbsictx. |
static void _cbsientry_release | ( | struct _cbsientry * | first | ) | [static] |
Releases memory occupied by the list of serial port informations.
Such a list is build in _cbserinfocb() and it should be released, if not needed anymore, eg. after the message WM_CBSIREADY has been sent.
first | begin of the list to release |
static BOOL CALLBACK _cfgdlgproc | ( | HWND | hwnd, | |
UINT | message, | |||
WPARAM | wParam, | |||
LPARAM | lParam | |||
) | [static] |
Configuration dialog procedure.
Puts the presently configured values into the controls of the dialog. If the dialog box has been closed using the OK button, then it puts the values from the controls back into the current configuration. The current configuration structure is expected under lParam.
hwnd | the handle of the configuration dialog | |
message | the windows message, WM_INITDIALOH, WM_COMMAND and WM_CBSIREADY are handled in this procedure. | |
wParam | the procedure expects here the ID of the button pressed (IDOK, IDCANCEL) on WM_COMMAND. | |
lParam | the pointer to struct _config containing the current configuration is expected here for WM_INITDIALOG. |
static void _cfgfilename | ( | TCHAR * | theFileName | ) | [static] |
Builds the configuration file name. The configuration file name is [ApplicationData]\[_APPNAME].cfg, if not compiled with -DUSE_ENV, else it is $HOME/[_APPNAME].cfg.
theFileName | buffer for the resulting file name, must be able to hold at least MAX_PATH+1 characters. |
static short _dlggpxfilename | ( | HWND | hwnd, | |
TCHAR * | theFileName, | |||
size_t | aLen | |||
) | [static] |
Opens common dialog for selecting the gpx file to create.
hwnd | the window owning the common dialog for selecting the file name. | |
theFileName | buffer where to put the selected file name. | |
aLen | lenght of the buffer. |
static void _fmtErrMsg | ( | ) | [static] |
Pronts last system error in a message box.
static void _gpscb | ( | double | anUTC, | |
double | anNLat, | |||
double | anELon, | |||
double | anAlt, | |||
double | aUTMN, | |||
double | aUTME, | |||
void * | aCtx | |||
) | [static] |
Callback routine retrieving current coordinates.
Prepares WM_NEWCOORD message and new coordinates in text format in the context. Sending WM_NEWCOORD to the main window causes showing new coordinates in the controls.
anUTC | the UTC time, | |
anNLat | the latitude, positive for North, negative for South. | |
anELon | the longitude, positive for East, negative for West. | |
anAlt | the altitude in meters. | |
aUTMN | the UTM northing in kilometers. | |
aUTME | the UTM easting in kilometers. | |
aCtx | casted to struct _gpscbcontrols. |
static HMODULE _nlsresmod | ( | HMODULE | defHandle | ) | [static] |
Returns module handle for the resource dll responsible for the current locale.
The function looks for resource DLLs having the directory of the current executable plus filename of the executable plus a 4 digit hex number ID of the language + extension dll. The language ID according to the return value of GetUserDefaultLangID or GetSystemDefaultLangID (eg. 0407 hex for DE, 0409 hex for EN, 0415 hex for PL). If the DLL with the appropriate name has been found, the the function loads this DLL using LoadLibrary and returns the handle of it. If no suitable DLLs have been found for the user or system default laungage ID, then GetModuleHandle(NULL) (the handle of the exe) is returned meaning, that the application has to use the default locales.
Example of the dll file name for the locale:
Wondering, why not for wine? Wine doesn't need this complicated way of localized resource dll loading. We can put all localized resources into one rc file and wine will automatically select the proper resource for the current system language in the environment variable LANG. This is because wine properly sets GetUser/SystemDefaultUILanguage() and windows doesn't. The default UI language in windows is this one, which you buyed in windows: spanish windows - spanish UI.
defHandle | the default handle to return, if the DLLs have not been found. |
static void _popupmenu | ( | HWND | hwnd, | |
int | x, | |||
int | y, | |||
int | enabled | |||
) | [static] |
Shows a popup menu at the given position.
hwnd | the window, within which to display the menu. | |
x | the x position | |
y | the y position | |
enabled | bitwise mask of enabled menu items |
static void _rdcfg | ( | struct _config * | aCfg | ) | [static] |
Reads configuration from a file into the given config structure.
If the file doesn't exist or if reading the configuration fails for some reason, then the configuration structure is filled with plausible defaults.
aCfg | pointer to the configuration structure. |
static LRESULT CALLBACK _wndproc | ( | HWND | hwnd, | |
UINT | message, | |||
WPARAM | wParam, | |||
LPARAM | lParam | |||
) | [static] |
The standard MS Windows window procedure.
On WM_INITDIALOG the function initialozes internal data structure: it reads the current configuration from the configuration file into a variable of type struct _config, initializes the GPS handle etc. On WM_COMMAND it handles the menu items pressed: starts the GPS reading thread on M_START, stops it on M_STOP, starts the configuration dialog on M_CFG or exits the application on M_EXIT.
hwnd | the handle of the window | |
message | window message, WM_INITDIALOG, WM_COMMAND, WM_CLOSE, WM_LBUTTONDOWN, WM_RBUTTONDOWN. | |
wParam | message parameter, on WM_COMMAND M_START, M_STOP, M_CFG or M_EXIT are handled here. | |
lParam | message parameter, ignored. |
static void _wrcfg | ( | const struct _config * | aCfg | ) | [static] |
Writes configuration to the configuration file.
The configuration file name is always [ApplicationData]\[_APPNAME].cfg.
aCfg | pointer to the configuration structure. |
int WINAPI WinMain | ( | HINSTANCE | hInstance, | |
HINSTANCE | hPrevInstance, | |||
PSTR | szCmdLine, | |||
int | iCmdShow | |||
) |
The main function of the application.
Sets the dialog cbmain as the main window and _wndproc as the window procedure.
hInstance | the handle of the instance. | |
hPrevInstance | ignored. | |
szCmdLine | ignored. | |
iCmdShow | needed for ShowWindow. |
const TCHAR _APPNAME[] = TEXT("cb") [static] |
HMODULE _hresmod [static] |
Module for loading localized resources.
Estimated using _nlsresmod().
TCHAR _szCloseErr[MAX_STR_LEN+1] [static] |
TCHAR _szFmtAlt[VAL_STR_LEN+1] [static] |
TCHAR _szFmtELon[VAL_STR_LEN+1] [static] |
TCHAR _szFmtNLat[VAL_STR_LEN+1] [static] |
TCHAR _szFmtSLat[VAL_STR_LEN+1] [static] |
TCHAR _szFmtUTC[VAL_STR_LEN+1] [static] |
TCHAR _szFmtUTME[VAL_STR_LEN+1] [static] |
TCHAR _szFmtUTMN[VAL_STR_LEN+1] [static] |
TCHAR _szFmtWLon[VAL_STR_LEN+1] [static] |
TCHAR _szGPXFilter[MAX_STR_LEN+1] [static] |
TCHAR _szOpenErr[MAX_STR_LEN+1] [static] |
TCHAR _szQuit[MAX_STR_LEN+1] [static] |