summaryrefslogtreecommitdiff
path: root/frontends/windows/gui.c
blob: 490f234336c12a7d4a0a2942849269229ee0f698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright 2008 Vincent Sanders <vince@simtec.co.uk>
 * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * win32 gui implementation.
 */

#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <windows.h>

#include "utils/errors.h"
#include "utils/nsurl.h"
#include "utils/log.h"
#include "utils/corestrings.h"
#include "utils/url.h"
#include "utils/file.h"
#include "utils/messages.h"
#include "netsurf/browser_window.h"

#include "windows/schedule.h"
#include "windows/window.h"
#include "windows/gui.h"

/**
 * win32 application instance handle.
 *
 * This handle is set in the main windows entry point.
 */
HINSTANCE hinst;

static bool win32_quit = false;

struct dialog_list_entry {
	struct dialog_list_entry *next;
	HWND hwnd;
};

static struct dialog_list_entry *dlglist = NULL;

/* exported interface documented in gui.h */
nserror nsw32_add_dialog(HWND hwndDlg)
{
	struct dialog_list_entry *nentry;
	nentry = malloc(sizeof(struct dialog_list_entry));
	if (nentry == NULL) {
		return NSERROR_NOMEM;
	}

	nentry->hwnd = hwndDlg;
	nentry->next = dlglist;
	dlglist = nentry;

	return NSERROR_OK;
}

/* exported interface documented in gui.h */
nserror nsw32_del_dialog(HWND hwndDlg)
{
	struct dialog_list_entry **prev;
	struct dialog_list_entry *cur;

	prev = &dlglist;
	cur = *prev;

	while (cur != NULL) {
		if (cur->hwnd == hwndDlg) {
			/* found match */
			*prev = cur->next;
			NSLOG(netsurf, DEBUG,
			      "removed hwnd %p entry %p", cur->hwnd, cur);
			free(cur);
			return NSERROR_OK;
		}
		prev = &cur->next;
		cur = *prev;
	}
	NSLOG(netsurf, INFO, "did not find hwnd %p", hwndDlg);

	return NSERROR_NOT_FOUND;
}

/**
 * walks dialog list and attempts to process any messages for them
 */
static nserror handle_dialog_message(LPMSG lpMsg)
{
	struct dialog_list_entry *cur;
	cur = dlglist;
	while (cur != NULL) {
		if (IsDialogMessage(cur->hwnd, lpMsg)) {
			NSLOG(netsurf, DEBUG,
			      "dispatched dialog hwnd %p", cur->hwnd);
			return NSERROR_OK;
		}
		cur = cur->next;
	}

	return NSERROR_NOT_FOUND;
}

/* exported interface documented in gui.h */
void win32_set_quit(bool q)
{
	win32_quit = q;
}

/* exported interface documented in gui.h */
void win32_run(void)
{
	MSG Msg; /* message from system */
	BOOL bRet; /* message fetch result */
	int timeout; /* timeout in miliseconds */
	UINT timer_id = 0;

	NSLOG(netsurf, INFO, "Starting messgae dispatcher");

	while (!win32_quit) {
		/* run the scheduler and discover how long to wait for
		 * the next event.
		 */
		timeout = schedule_run();

		if (timeout == 0) {
			bRet = PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE);
		} else {
			if (timeout > 0) {
				/* set up a timer to ensure we get woken */
				timer_id = SetTimer(NULL, 0, timeout, NULL);
			}

			/* wait for a message */
			bRet = GetMessage(&Msg, NULL, 0, 0);

			/* if a timer was sucessfully created remove it */
			if (timer_id != 0) {
				KillTimer(NULL, timer_id);
				timer_id = 0;
			}
		}

		if ((bRet > 0) &&
		    (handle_dialog_message(&Msg) != NSERROR_OK)) {
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}


/* exported function documented in windows/gui.h */
nserror win32_warning(const char *warning, const char *detail)
{
	size_t len = 1 + ((warning != NULL) ? strlen(messages_get(warning)) :
			  0) + ((detail != 0) ? strlen(detail) : 0);
	char message[len];
	snprintf(message, len, messages_get(warning), detail);
	MessageBox(NULL, message, "Warning", MB_ICONWARNING);

	return NSERROR_OK;
}