Skip to content

Commit 82609ec

Browse files
committed
Added trayicon (no functionaliy yet)
1 parent 8c45a4d commit 82609ec

File tree

4 files changed

+114
-4
lines changed

4 files changed

+114
-4
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ CFLAGS=-DWINVER=0x500 -DWIN32_WINNT=0x500 -Os -DWITH_ICON=1
77
LDFLAGS=-mwindows
88

99
PROG=neoplace.exe
10-
OBJS=main.o resources.o
10+
OBJS=main.o resources.o trayicon.o
11+
12+
13+
ifdef DEBUG
14+
CFLAGS+= -g
15+
LDFLAGS:=$(filter-out -mwindows, $(LDFLAGS))
16+
endif
1117

1218
all: ${PROG}
1319

main.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <stdio.h>
2424
#include <stdbool.h>
2525
#include "resources.h"
26+
#include "trayicon.h"
2627

2728
enum {
2829
ID_BUTTON_RESIZE,
@@ -45,12 +46,12 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
4546
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
4647
HWND createMatrixButton(HWND parent, HINSTANCE hInstance, unsigned i, unsigned j);
4748
HWND createMainWindow(HINSTANCE current_instance);
49+
void exitApplication();
4850

4951
HWND top_window, main_window;
5052

5153
HWND buttons[BUTTON_MAX][BUTTON_MAX];
52-
bool is_checking = false;
53-
//<is currently checking the size matrix ?
54+
bool is_checking = false; //<is currently checking the size matrix ?
5455
POINT last_click_position;
5556

5657
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
@@ -59,6 +60,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
5960

6061
top_window = GetForegroundWindow();
6162

63+
trayicon_init(LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPICON)), APPNAME);
64+
trayicon_add_item(NULL, &exitApplication);
65+
trayicon_add_item("Exit", &exitApplication);
66+
6267
main_window = createMainWindow(hInstance);
6368
ShowWindow(main_window, nCmdShow);
6469
UpdateWindow(main_window);
@@ -166,6 +171,7 @@ HWND createMainWindow(HINSTANCE current_instance)
166171

167172
void exitApplication()
168173
{
174+
trayicon_remove();
169175
PostQuitMessage(0);
170176
}
171177

@@ -179,7 +185,6 @@ HWND createMatrixButton(HWND parent, HINSTANCE hInstance, unsigned i, unsigned j
179185
MARGIN + j * (BUTTON_SIZE + BUTTON_SPACE),
180186
MARGIN + i * (BUTTON_SIZE + BUTTON_SPACE),
181187
BUTTON_SIZE, BUTTON_SIZE, parent, NULL, hInstance, NULL);
182-
//ID_LAST + i * BUTTON_MAX_ROWS + j);
183188
return button;
184189
}
185190

trayicon.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
#include <windows.h>
3+
#include <tchar.h>
4+
#include "trayicon.h"
5+
#include "resources.h"
6+
7+
#define WM_ICON_CLICK (WM_USER+1)
8+
#define LOG_ERROR(text) MessageBox(NULL, TEXT(text), TEXT("Error"), MB_ICONERROR | MB_OK);
9+
#define WND_CLASS " toolbar"
10+
11+
NOTIFYICONDATA trayicon_data = { };
12+
13+
LRESULT CALLBACK trayicon_messageloop(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
14+
15+
bool trayicon_init(HICON icon, char tooltip[])
16+
{
17+
HWND hidden_window;
18+
WNDCLASSEX wc = { 0 };
19+
TCHAR class_name[256];
20+
21+
// build new class name
22+
_tcscpy(class_name, tooltip);
23+
_tcscat(class_name, WND_CLASS);
24+
25+
wc.cbSize = sizeof(wc);
26+
wc.cbClsExtra = 0;
27+
wc.cbWndExtra = 0;
28+
wc.style = 0;
29+
wc.lpfnWndProc = &trayicon_messageloop;
30+
wc.hInstance = GetModuleHandle(NULL);
31+
wc.lpszClassName = class_name;
32+
if (!RegisterClassEx(&wc)) {
33+
LOG_ERROR("Error registering window class.");
34+
return false;
35+
}
36+
hidden_window =
37+
CreateWindowEx(0, class_name, class_name,
38+
0, CW_USEDEFAULT, CW_USEDEFAULT, 100, 100, NULL, NULL,
39+
GetModuleHandle(NULL), NULL);
40+
41+
if (!hidden_window) {
42+
LOG_ERROR("Error creating hidden window.");
43+
return false;
44+
}
45+
46+
trayicon_data.cbSize = sizeof(trayicon_data);
47+
trayicon_data.hIcon = icon;
48+
trayicon_data.hWnd = hidden_window;
49+
trayicon_data.uCallbackMessage = WM_ICON_CLICK;
50+
trayicon_data.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
51+
52+
strncpy(trayicon_data.szTip, tooltip, 64);
53+
trayicon_data.szTip[64] = '\0';
54+
Shell_NotifyIcon(NIM_ADD, &trayicon_data);
55+
}
56+
57+
void trayicon_remove()
58+
{
59+
Shell_NotifyIcon(NIM_DELETE, &trayicon_data);
60+
}
61+
62+
void trayicon_add_item(char *text, void (functionPtr) ())
63+
{
64+
//FIXME: must be implemented
65+
}
66+
67+
LRESULT CALLBACK trayicon_messageloop(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
68+
{
69+
switch (msg) {
70+
case WM_CREATE:
71+
{
72+
break;
73+
}
74+
case WM_ICON_CLICK:
75+
{
76+
break;
77+
}
78+
79+
}
80+
return DefWindowProc(hWnd, msg, wParam, lParam);
81+
}

trayicon.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _TRAYICON_H
2+
#define _TRAYICON_H
3+
4+
#include <windows.h>
5+
#include <stdbool.h>
6+
7+
typedef enum {
8+
TRAYICON_CLICKED,
9+
TRAYICON_EXIT_CLICKED,
10+
TRAYICON_BEFORE_REMOVE
11+
} TRAYICON_EVENT;
12+
13+
bool trayicon_init(HICON icon, char tooltip[]);
14+
void trayicon_remove();
15+
16+
void trayicon_add_item(char *text, void (functionPtr) ());
17+
18+
#endif

0 commit comments

Comments
 (0)