dynwatch

diff gui.c @ 0:ce3c5e4c75bf

dynwatch DynDNS updater for windows
author John Tsiombikas <nuclear@siggraph.org>
date Wed, 18 May 2011 05:53:29 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gui.c	Wed May 18 05:53:29 2011 +0300
     1.3 @@ -0,0 +1,134 @@
     1.4 +/*
     1.5 +This file is part of dynwatch, a win32 system tray applet which
     1.6 +updates automatically the dyndns entry of quake.gr.
     1.7 +
     1.8 +Copyright (c) 2005 John Tsiombikas <nuclear@siggraph.org>
     1.9 +
    1.10 +This program is free software; you can redistribute it and/or modify
    1.11 +it under the terms of the GNU General Public License as published by
    1.12 +the Free Software Foundation; either version 2 of the License, or
    1.13 +(at your option) any later version.
    1.14 +
    1.15 +This program is distributed in the hope that it will be useful,
    1.16 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 +GNU General Public License for more details.
    1.19 +
    1.20 +You should have received a copy of the GNU General Public License
    1.21 +along with this program; if not, write to the Free Software
    1.22 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.23 +*/
    1.24 +
    1.25 +#include "gui.h"
    1.26 +#include "events.h"
    1.27 +#include "resource.h"
    1.28 +
    1.29 +HWND win_main, bn_exit, bn_tray, bn_config;
    1.30 +HWND win_cfg, lb_dnshost, lb_user, lb_pass, lb_hosts;
    1.31 +HWND en_dnshost, en_user, en_pass, en_hosts;
    1.32 +HWND bn_ok, bn_cancel;
    1.33 +int trayfied;
    1.34 +
    1.35 +void create_gui(void) {
    1.36 +	static const int lb_xsz = 130;
    1.37 +	static const int lb_ysz = 20;
    1.38 +	static const int en_xsz = 100;
    1.39 +	static const int en_ysz = 20;
    1.40 +	static const int yinc = 25;
    1.41 +	int ypos = 5;
    1.42 +
    1.43 +	reg_win_class("dynwatch", win_proc);
    1.44 +	reg_win_class("dynwatch.config", cfg_proc);
    1.45 +
    1.46 +	win_main = create_window("dynwatch 1.0", 330, 80, "dynwatch");
    1.47 +	bn_config = create_button("Configure", 5, 5, 100, 30, win_main);
    1.48 +	bn_tray = create_button("Send to tray", 110, 5, 100, 30, win_main);
    1.49 +	bn_exit = create_button("Exit", 215, 5, 100, 30, win_main);
    1.50 +	ShowWindow(win_main, 1);
    1.51 +
    1.52 +	win_cfg = create_window("Configuration", 270, 190, "dynwatch.config");
    1.53 +
    1.54 +	lb_dnshost = create_label("dyndns host:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
    1.55 +	en_dnshost = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);
    1.56 +	ypos += yinc;
    1.57 +	lb_user = create_label("user name:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
    1.58 +	en_user = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);
    1.59 +	ypos += yinc;
    1.60 +	lb_pass = create_label("password:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
    1.61 +	en_pass = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);
    1.62 +	ypos += yinc;
    1.63 +	/*lb_hosts = create_label("hosts to update:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
    1.64 +	en_hosts = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);*/
    1.65 +
    1.66 +	ypos += 35;
    1.67 +	bn_ok = create_button("OK", 5, ypos, 100, 25, win_cfg);
    1.68 +	bn_cancel = create_button("Cancel", 140, ypos, 100, 25, win_cfg);
    1.69 +}
    1.70 +
    1.71 +void tray(int trayfy) {
    1.72 +	NOTIFYICONDATA nicon;
    1.73 +	memset(&nicon, 0, sizeof nicon);
    1.74 +	nicon.cbSize = sizeof nicon;
    1.75 +	nicon.hWnd = win_main;
    1.76 +	nicon.uID = 0;
    1.77 +	nicon.uCallbackMessage = WM_USER;
    1.78 +	nicon.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
    1.79 +	nicon.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(ICON_DYNWATCH));
    1.80 +	strcpy(nicon.szTip, "Click to open the program window");
    1.81 +
    1.82 +	if(trayfy) {
    1.83 +		ShowWindow(win_main, 0);
    1.84 +        Shell_NotifyIcon(NIM_ADD, &nicon);
    1.85 +	} else {
    1.86 +		Shell_NotifyIcon(NIM_DELETE, &nicon);
    1.87 +		ShowWindow(win_main, 1);
    1.88 +	}
    1.89 +}
    1.90 +
    1.91 +HWND create_window(const char *title, int xsz, int ysz, const char *class_name) {
    1.92 +	int x = (GetSystemMetrics(SM_CXSCREEN) - xsz) / 2;
    1.93 +	int y = (GetSystemMetrics(SM_CYSCREEN) - ysz) / 2;
    1.94 +	unsigned long style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
    1.95 +	return CreateWindow(class_name, title, style, x, y, xsz, ysz, 0, 0, GetModuleHandle(0), 0);
    1.96 +}
    1.97 +
    1.98 +HWND create_button(const char *title, int x, int y, int xsz, int ysz, HWND parent) {
    1.99 +	unsigned long style = BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP;
   1.100 +	return CreateWindow("BUTTON", title, style, x, y, xsz, ysz, parent, 0, GetModuleHandle(0), 0);
   1.101 +}
   1.102 +
   1.103 +HWND create_label(const char *text, int x, int y, int xsz, int ysz, HWND parent) {
   1.104 +	unsigned long style = SS_RIGHT | WS_CHILD | WS_VISIBLE;
   1.105 +	return CreateWindow("STATIC", text, style, x, y, xsz, ysz, parent, 0, GetModuleHandle(0), 0);
   1.106 +}
   1.107 +
   1.108 +HWND create_entry(const char *text, int x, int y, int xsz, int ysz, HWND parent) {
   1.109 +	unsigned long style = ES_AUTOHSCROLL | WS_CHILD | WS_BORDER | WS_TABSTOP | WS_VISIBLE;
   1.110 +	return CreateWindow("EDIT", text, style, x, y, xsz, ysz, parent, 0, GetModuleHandle(0), 0);
   1.111 +}
   1.112 +
   1.113 +void en_set_text(HWND entry, const char *text) {
   1.114 +	SendMessage(entry, WM_SETTEXT, 0, (long)text);
   1.115 +}
   1.116 +
   1.117 +const char *en_get_text(HWND entry) {
   1.118 +	static char buf[512];
   1.119 +	SendMessage(entry, WM_GETTEXT, 512, (long)buf);
   1.120 +	return buf;
   1.121 +}
   1.122 +
   1.123 +void reg_win_class(const char *class_name, msg_handler_t handler) {
   1.124 +	WNDCLASSEX wc;
   1.125 +	HINSTANCE pid = GetModuleHandle(0);
   1.126 +
   1.127 +	memset(&wc, 0, sizeof wc);
   1.128 +	wc.cbSize = sizeof wc;
   1.129 +	wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
   1.130 +	wc.hCursor = LoadCursor(pid, MAKEINTRESOURCE(IDC_ARROW));
   1.131 +	wc.hIcon = wc.hIconSm = LoadIcon(pid, MAKEINTRESOURCE(ICON_DYNWATCH));
   1.132 +	wc.hInstance = pid;
   1.133 +	wc.lpfnWndProc = handler;
   1.134 +	wc.lpszClassName = class_name;
   1.135 +	wc.style = CS_HREDRAW | CS_VREDRAW;
   1.136 +	RegisterClassEx(&wc);
   1.137 +}