dynwatch

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