/* NetHalt - GUI library functions * Copyright (C) 2008 Daniel Collins * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of the author nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include "gui.h" #include "lib.h" /* Display a printf() format string in a message box dialog * Returns the MessageBox() return value */ int msgboxf(int flags, char const *title, char const *fmt, ...) { char msg[1024]; va_list argv; va_start(argv, fmt); vsnprintf(msg, 1024, fmt, argv); va_end(argv); return MessageBox(NULL, msg, title, flags); } /* Create a child window * Returns a handle to the newly created window */ HWND gui_mkchild(HWND parent, int id, int xpos, int ypos, int width, int height) { char const *class; DWORD style = WS_CHILD | WS_VISIBLE; HWND hwnd; if(id >= 300 && id < 400) { class = "STATIC"; } if(id >= 400 && id < 500) { class = "BUTTON"; style |= BS_PUSHBUTTON; } hwnd = CreateWindow( class, "", style, xpos, ypos, width, height, parent, (HMENU)id, GetModuleHandle(NULL), NULL ); if(!hwnd) { die( "Failed to create window (%d): %s", id, w32_error(GetLastError()) ); } return hwnd; } /* NOTE: * * The following functions take a hwnd and id argument, if id is negative they * will use the hwnd handle, otherwise they will use the child window of hwnd * specified by id. */ /* Set the text of a window */ void gui_stext(HWND hwnd, int id, char const *fmt, ...) { va_list argv; char buf[1024]; va_start(argv, fmt); vsnprintf(buf, 1024, fmt, argv); va_end(argv); if(id >= 0) { hwnd = GetDlgItem(hwnd, id); } SetWindowText(hwnd, buf); } /* Append text to a window */ void gui_atext(HWND hwnd, int id, char const *fmt, ...) { va_list argv; char abuf[1024], buf[1024]; va_start(argv, fmt); vsnprintf(abuf, 1024, fmt, argv); va_end(argv); if(id >= 0) { hwnd = GetDlgItem(hwnd, id); } GetWindowText(hwnd, buf, 1024); strncat(buf, abuf, 1024); SetWindowText(hwnd, buf); } /* Set the font of a window */ void gui_font(HWND hwnd, int id, char const *typeface, int height, int attribs) { LOGFONT finfo; HFONT font; finfo.lfHeight = height; finfo.lfWidth = 0; finfo.lfEscapement = 0; finfo.lfOrientation = 0; finfo.lfWeight = (attribs & FONT_BOLD ? FW_BOLD : FW_NORMAL); finfo.lfItalic = (attribs & FONT_ITALIC ? TRUE : FALSE); finfo.lfUnderline = (attribs & FONT_UNDERLINE ? TRUE : FALSE); finfo.lfStrikeOut = FALSE; finfo.lfCharSet = ANSI_CHARSET; finfo.lfOutPrecision = OUT_DEFAULT_PRECIS; finfo.lfClipPrecision = CLIP_DEFAULT_PRECIS; finfo.lfQuality = ANTIALIASED_QUALITY; finfo.lfPitchAndFamily = DEFAULT_PITCH; strcpy(finfo.lfFaceName, typeface); font = CreateFontIndirect(&finfo); if(!font) { die("Failed to create font: %s", w32_error(GetLastError())); } if(id >= 0) { hwnd = GetDlgItem(hwnd, id); } SendMessage(hwnd, WM_SETFONT, (WPARAM)font, MAKELPARAM(FALSE, 0)); } /* Enable/disable a window */ void gui_enable(HWND hwnd, int id, int enabled) { if(id >= 0) { hwnd = GetDlgItem(hwnd, id); } EnableWindow(hwnd, (enabled ? TRUE : FALSE)); } /* Get a positive integer value from a control * Returns -1 if the value is not an integer */ int gui_pint(HWND hwnd, int id) { TCHAR buf[1024]; size_t len, n; if(id >= 0) { hwnd = GetDlgItem(hwnd, id); } len = SendMessage(hwnd, WM_GETTEXT, 1024, (LPARAM)buf); if(len == 0) { return -1; } for(n = 0; n < len; n++) { if(!isdigit(buf[n])) { return -1; } } return atoi(buf); } /* Check if a checkbox control is checked * Returns 1 if checked, zero otherwise */ int gui_checked(HWND hwnd, int id) { if(id >= 0) { hwnd = GetDlgItem(hwnd, id); } return (SendMessage(hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED ? 1 : 0); }