dynwatch

annotate 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
rev   line source
nuclear@0 1 /*
nuclear@0 2 This file is part of dynwatch, a win32 system tray applet which
nuclear@0 3 updates automatically the dyndns entry of quake.gr.
nuclear@0 4
nuclear@0 5 Copyright (c) 2005 John Tsiombikas <nuclear@siggraph.org>
nuclear@0 6
nuclear@0 7 This program is free software; you can redistribute it and/or modify
nuclear@0 8 it under the terms of the GNU General Public License as published by
nuclear@0 9 the Free Software Foundation; either version 2 of the License, or
nuclear@0 10 (at your option) any later version.
nuclear@0 11
nuclear@0 12 This program is distributed in the hope that it will be useful,
nuclear@0 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 15 GNU General Public License for more details.
nuclear@0 16
nuclear@0 17 You should have received a copy of the GNU General Public License
nuclear@0 18 along with this program; if not, write to the Free Software
nuclear@0 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
nuclear@0 20 */
nuclear@0 21
nuclear@0 22 #include "gui.h"
nuclear@0 23 #include "events.h"
nuclear@0 24 #include "resource.h"
nuclear@0 25
nuclear@0 26 HWND win_main, bn_exit, bn_tray, bn_config;
nuclear@0 27 HWND win_cfg, lb_dnshost, lb_user, lb_pass, lb_hosts;
nuclear@0 28 HWND en_dnshost, en_user, en_pass, en_hosts;
nuclear@0 29 HWND bn_ok, bn_cancel;
nuclear@0 30 int trayfied;
nuclear@0 31
nuclear@0 32 void create_gui(void) {
nuclear@0 33 static const int lb_xsz = 130;
nuclear@0 34 static const int lb_ysz = 20;
nuclear@0 35 static const int en_xsz = 100;
nuclear@0 36 static const int en_ysz = 20;
nuclear@0 37 static const int yinc = 25;
nuclear@0 38 int ypos = 5;
nuclear@0 39
nuclear@0 40 reg_win_class("dynwatch", win_proc);
nuclear@0 41 reg_win_class("dynwatch.config", cfg_proc);
nuclear@0 42
nuclear@0 43 win_main = create_window("dynwatch 1.0", 330, 80, "dynwatch");
nuclear@0 44 bn_config = create_button("Configure", 5, 5, 100, 30, win_main);
nuclear@0 45 bn_tray = create_button("Send to tray", 110, 5, 100, 30, win_main);
nuclear@0 46 bn_exit = create_button("Exit", 215, 5, 100, 30, win_main);
nuclear@0 47 ShowWindow(win_main, 1);
nuclear@0 48
nuclear@0 49 win_cfg = create_window("Configuration", 270, 190, "dynwatch.config");
nuclear@0 50
nuclear@0 51 lb_dnshost = create_label("dyndns host:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
nuclear@0 52 en_dnshost = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);
nuclear@0 53 ypos += yinc;
nuclear@0 54 lb_user = create_label("user name:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
nuclear@0 55 en_user = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);
nuclear@0 56 ypos += yinc;
nuclear@0 57 lb_pass = create_label("password:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
nuclear@0 58 en_pass = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);
nuclear@0 59 ypos += yinc;
nuclear@0 60 /*lb_hosts = create_label("hosts to update:", 5, ypos, lb_xsz, lb_ysz, win_cfg);
nuclear@0 61 en_hosts = create_entry("", lb_xsz + 10, ypos, en_xsz, en_ysz, win_cfg);*/
nuclear@0 62
nuclear@0 63 ypos += 35;
nuclear@0 64 bn_ok = create_button("OK", 5, ypos, 100, 25, win_cfg);
nuclear@0 65 bn_cancel = create_button("Cancel", 140, ypos, 100, 25, win_cfg);
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 void tray(int trayfy) {
nuclear@0 69 NOTIFYICONDATA nicon;
nuclear@0 70 memset(&nicon, 0, sizeof nicon);
nuclear@0 71 nicon.cbSize = sizeof nicon;
nuclear@0 72 nicon.hWnd = win_main;
nuclear@0 73 nicon.uID = 0;
nuclear@0 74 nicon.uCallbackMessage = WM_USER;
nuclear@0 75 nicon.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
nuclear@0 76 nicon.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(ICON_DYNWATCH));
nuclear@0 77 strcpy(nicon.szTip, "Click to open the program window");
nuclear@0 78
nuclear@0 79 if(trayfy) {
nuclear@0 80 ShowWindow(win_main, 0);
nuclear@0 81 Shell_NotifyIcon(NIM_ADD, &nicon);
nuclear@0 82 } else {
nuclear@0 83 Shell_NotifyIcon(NIM_DELETE, &nicon);
nuclear@0 84 ShowWindow(win_main, 1);
nuclear@0 85 }
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 HWND create_window(const char *title, int xsz, int ysz, const char *class_name) {
nuclear@0 89 int x = (GetSystemMetrics(SM_CXSCREEN) - xsz) / 2;
nuclear@0 90 int y = (GetSystemMetrics(SM_CYSCREEN) - ysz) / 2;
nuclear@0 91 unsigned long style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
nuclear@0 92 return CreateWindow(class_name, title, style, x, y, xsz, ysz, 0, 0, GetModuleHandle(0), 0);
nuclear@0 93 }
nuclear@0 94
nuclear@0 95 HWND create_button(const char *title, int x, int y, int xsz, int ysz, HWND parent) {
nuclear@0 96 unsigned long style = BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP;
nuclear@0 97 return CreateWindow("BUTTON", title, style, x, y, xsz, ysz, parent, 0, GetModuleHandle(0), 0);
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 HWND create_label(const char *text, int x, int y, int xsz, int ysz, HWND parent) {
nuclear@0 101 unsigned long style = SS_RIGHT | WS_CHILD | WS_VISIBLE;
nuclear@0 102 return CreateWindow("STATIC", text, style, x, y, xsz, ysz, parent, 0, GetModuleHandle(0), 0);
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 HWND create_entry(const char *text, int x, int y, int xsz, int ysz, HWND parent) {
nuclear@0 106 unsigned long style = ES_AUTOHSCROLL | WS_CHILD | WS_BORDER | WS_TABSTOP | WS_VISIBLE;
nuclear@0 107 return CreateWindow("EDIT", text, style, x, y, xsz, ysz, parent, 0, GetModuleHandle(0), 0);
nuclear@0 108 }
nuclear@0 109
nuclear@0 110 void en_set_text(HWND entry, const char *text) {
nuclear@0 111 SendMessage(entry, WM_SETTEXT, 0, (long)text);
nuclear@0 112 }
nuclear@0 113
nuclear@0 114 const char *en_get_text(HWND entry) {
nuclear@0 115 static char buf[512];
nuclear@0 116 SendMessage(entry, WM_GETTEXT, 512, (long)buf);
nuclear@0 117 return buf;
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 void reg_win_class(const char *class_name, msg_handler_t handler) {
nuclear@0 121 WNDCLASSEX wc;
nuclear@0 122 HINSTANCE pid = GetModuleHandle(0);
nuclear@0 123
nuclear@0 124 memset(&wc, 0, sizeof wc);
nuclear@0 125 wc.cbSize = sizeof wc;
nuclear@0 126 wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
nuclear@0 127 wc.hCursor = LoadCursor(pid, MAKEINTRESOURCE(IDC_ARROW));
nuclear@0 128 wc.hIcon = wc.hIconSm = LoadIcon(pid, MAKEINTRESOURCE(ICON_DYNWATCH));
nuclear@0 129 wc.hInstance = pid;
nuclear@0 130 wc.lpfnWndProc = handler;
nuclear@0 131 wc.lpszClassName = class_name;
nuclear@0 132 wc.style = CS_HREDRAW | CS_VREDRAW;
nuclear@0 133 RegisterClassEx(&wc);
nuclear@0 134 }