summaryrefslogtreecommitdiff
path: root/include/m_utils.h
blob: 567e9689a6aaa1836fb6f46c1ef3e7b86101ecb7 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (ñ) 2012-17 Miranda NG project (https://miranda-ng.org)
Copyright (c) 2000-12 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program 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; either version 2
of the License, or (at your option) any later version.

This program 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef M_UTILS_H__
#define M_UTILS_H__ 1

#ifdef _MSC_VER
	#pragma warning(disable:4201)
#endif

#include <stdio.h>

#if !defined(M_SYSTEM_H__)
#include <m_system.h>
#endif

#if !defined(M_STRING_H__)
#include <m_string.h>
#endif

/////////////////////////////////////////////////////////////////////////////////////////
// Opens a URL in the user's default web browser
//
// bOpenInNewWindow should be zero to open the URL in the browser window the user
// last used, or nonzero to open in a new browser window. If there's no browser
// running, one will be opened to show the URL.

EXTERN_C MIR_CORE_DLL(void) Utils_OpenUrl(const char *pszUrl, bool bOpenInNewWindow = true);
EXTERN_C MIR_CORE_DLL(void) Utils_OpenUrlW(const wchar_t *pszUrl, bool bOpenInNewWindow = true);

/////////////////////////////////////////////////////////////////////////////////////////
// Resizes a dialog by calling a custom routine to move the individual
// Returns 0 on success, or nonzero on failure
// Does not support dialogtemplateex dialog boxes, and will return failure if you try to resize one
// The dialog itself should have been resized prior to calling this service
// pfnResizer is called once for each control in the dialog
// pfnResizer should return a combination of one rd_anchorx_ and one rd_anchory constant

#define RD_ANCHORX_CUSTOM	0	// function did everything required to the x axis, do no more processing
#define RD_ANCHORX_LEFT 	0	// move the control to keep it constant distance from the left edge of the dialog
#define RD_ANCHORX_RIGHT	1	// move the control to keep it constant distance from the right edge of the dialog
#define RD_ANCHORX_WIDTH	2	// size the control to keep it constant distance from both edges of the dialog
#define RD_ANCHORX_CENTRE	4	// move the control to keep it constant distance from the centre of the dialog
#define RD_ANCHORY_CUSTOM	0
#define RD_ANCHORY_TOP		0
#define RD_ANCHORY_BOTTOM	8
#define RD_ANCHORY_HEIGHT	16
#define RD_ANCHORY_CENTRE	32

struct UTILRESIZECONTROL
{
	int cbSize;
	UINT wId;             // control ID
	RECT rcItem;          // original control rectangle, relative to dialog
	                      // modify in-place to specify the new position
	SIZE dlgOriginalSize; // size of dialog client area in template
	SIZE dlgNewSize;      // current size of dialog client area
};

typedef int (*DIALOGRESIZERPROC)(HWND hwndDlg, LPARAM lParam, UTILRESIZECONTROL *urc);

EXTERN_C MIR_CORE_DLL(int) Utils_ResizeDialog(HWND hwndDlg, HINSTANCE hInstance, LPCSTR lpTemplate, DIALOGRESIZERPROC pfnResizer, LPARAM lParam = 0);

/* Gets the name of a country given its number		v0.1.2.0+
wParam = countryId
lParam = 0
Returns a pointer to the string containing the country name on success,
or NULL on failure
*/
#define MS_UTILS_GETCOUNTRYBYNUMBER "Utils/GetCountryByNumber"

/* Gets the name of a country given its ISO code	v0.1.2.0+
wParam = (char*)ISOcode
lParam = 0
Returns a pointer to the string containing the country name on success,
or NULL on failure
*/
#define MS_UTILS_GETCOUNTRYBYISOCODE "Utils/GetCountryByISOCode"

/* Gets the full list of country IDs	 v0.1.2.0+
wParam = (WPARAM)(int*)piCount
lParam = (LPARAM)(struct CountryListEntry**)ppList
Returns 0 always
Neither wParam nor lParam can be NULL.
The list is sorted alphabetically by country name, on the assumption that it's
quicker to search numbers out of order than it is to search names out of order
*/
struct CountryListEntry {
	int id;
	const char *szName;
	char ISOcode[3];
};
#define MS_UTILS_GETCOUNTRYLIST "Utils/GetCountryList"

/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Window lists //////////////////////////////////////

#if defined(MIR_CORE_EXPORTS)
typedef struct TWindowList *MWindowList;
#else
DECLARE_HANDLE(MWindowList);
#endif

/////////////////////////////////////////////////////////////////////////////////////////
// allocates a window list
// returns a handle to the new window list

EXTERN_C MIR_CORE_DLL(MWindowList) WindowList_Create(void);

/////////////////////////////////////////////////////////////////////////////////////////
// destroys a window list

EXTERN_C MIR_CORE_DLL(void) WindowList_Destroy(MWindowList &hList);

/////////////////////////////////////////////////////////////////////////////////////////
// adds a window to the specified window list
// returns 0 on success, nonzero on failure

EXTERN_C MIR_CORE_DLL(int) WindowList_Add(MWindowList hList, HWND hwnd, MCONTACT hContact);

/////////////////////////////////////////////////////////////////////////////////////////
// removes a window from the specified window list
// returns 0 on success, nonzero on failure

EXTERN_C MIR_CORE_DLL(int) WindowList_Remove(MWindowList hList, HWND hwnd);

/////////////////////////////////////////////////////////////////////////////////////////
// finds a window given the hContact
// returns the window handle on success, or NULL on failure

EXTERN_C MIR_CORE_DLL(HWND) WindowList_Find(MWindowList hList, MCONTACT hContact);

/////////////////////////////////////////////////////////////////////////////////////////
// sends a message to all windows in a list using SendMessage
// returns 0 on success, nonzero on failure

EXTERN_C MIR_CORE_DLL(int) WindowList_Broadcast(MWindowList hList, UINT message, WPARAM wParam, LPARAM lParam);

/////////////////////////////////////////////////////////////////////////////////////////
// sends a message to all windows in a list using PostMessage
// returns 0 on success, nonzero on failure

EXTERN_C MIR_CORE_DLL(int) WindowList_BroadcastAsync(MWindowList hList, UINT message, WPARAM wParam, LPARAM lParam);

/***************************** Hyperlink windows ********************************/

//there aren't any services here, because you don't need them.
#define WNDCLASS_HYPERLINK	 L"Hyperlink"
//the control will obey the SS_LEFT (0), SS_CENTER (1), and SS_RIGHT (2) styles
//the control will send STN_CLICKED via WM_COMMAND when the link itself is clicked

/////////////////////////////////////////////////////////////////////////////////////////
// Use this in a SendMessage to set the color of the url when control is enabled
// wParam = DWORD color
// lParam = not used
#define HLK_SETENABLECOLOUR	 (WM_USER+101) // added in 0.3.1

/////////////////////////////////////////////////////////////////////////////////////////
// Use this in a SendMessage to set the color of the url when control is disabled
// wParam = DWORD color
// lParam = not used
#define HLK_SETDISABLECOLOUR (WM_USER+102) // added in 0.3.1

/////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Window Position Saving ////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// saves the position of a window in the database
// returns 0 on success, nonzero on failure

EXTERN_C MIR_CORE_DLL(int) Utils_SaveWindowPosition(HWND hwnd, MCONTACT hContact, const char *szModule, const char *szNamePrefix);

/////////////////////////////////////////////////////////////////////////////////////////
// restores the position of a window from the database	 v0.1.1.0+
// wParam = flags
// lParam = (LPARAM)(SAVEWINDOWPOS*)&swp
// returns 0 on success, nonzero on failure
// if no position was found in the database, the function returns 1 and does
// nothing
// the NoSize version won't use stored size information: the window is left the same size.

#define RWPF_NOSIZE 	1  //don't use stored size info: leave dialog same size
#define RWPF_NOMOVE 	2  //don't use stored position
#define RWPF_NOACTIVATE 4  //show but don't activate v0.3.3.0+
#define RWPF_HIDDEN		8  //make it hidden

EXTERN_C MIR_CORE_DLL(int) Utils_RestoreWindowPosition(HWND hwnd, MCONTACT hContact, const char *szModule, const char *szNamePrefix, int flags = 0);

__forceinline int Utils_RestoreWindowPositionNoSize(HWND hwnd, MCONTACT hContact, const char *szModule, const char *szNamePrefix)
{	return Utils_RestoreWindowPosition(hwnd, hContact, szModule, szNamePrefix, RWPF_NOSIZE);
}
__forceinline int Utils_RestoreWindowPositionNoMove(HWND hwnd, MCONTACT hContact, const char *szModule, const char *szNamePrefix)
{	return Utils_RestoreWindowPosition(hwnd, hContact, szModule, szNamePrefix, RWPF_NOMOVE);
}

/////////////////////////////////////////////////////////////////////////////////////////
// Moves a RECT inside screen if it is outside. It works with multiple monitors
// returns < 0 on error, 0 if not changed the rect, 1 if changed the rect

EXTERN_C MIR_CORE_DLL(int) Utils_AssertInsideScreen(RECT *rc);

/************************ Colour Picker Control (0.1.2.1+) **********************/

#define WNDCLASS_COLOURPICKER  "ColourPicker"

#define CPM_SETCOLOUR		   0x1000	  //lParam = new colour
#define CPM_GETCOLOUR		   0x1001	  //returns colour
#define CPM_SETDEFAULTCOLOUR   0x1002	  //lParam = default, used as first custom colour
#define CPM_GETDEFAULTCOLOUR   0x1003	  //returns colour
#define CPN_COLOURCHANGED	   1		  //sent through WM_COMMAND

/////////////////////////////////////////////////////////////////////////////////////////
// Gets the filter strings for use in the open file dialog
// See the MSDN under OPENFILENAME.lpstrFilter for the formatting
// An 'All Bitmaps' item is always first and 'All Files' is last.
// The returned string is already translated.

EXTERN_C MIR_CORE_DLL(HBITMAP) Bitmap_Load(const wchar_t *ptszFileName);
EXTERN_C MIR_CORE_DLL(void) Bitmap_GetFilter(wchar_t *dest, size_t destLen);

/////////////////////////////////////////////////////////////////////////////////////////
// Converts a path to a relative path
// Only saves as a relative path if the file is in the miranda directory (or
// sub directory)
//
// [pszSrc] is the path to convert and [pszOut] is the buffer that
// the new path is copied too. pszOut MUST be at least of the size MAX_PATH.
//
// [pszBase] is the folder that is treated as root for 'relativity'
// by default = path to miranda32.exe
//
// Returns number of chars copied.

#if defined( __cplusplus )
EXTERN_C MIR_CORE_DLL(int) PathToRelative(const char *pszSrc, char *pszOut, const char* pszBase = 0);
EXTERN_C MIR_CORE_DLL(int) PathToRelativeW(const wchar_t *pwszSrc, wchar_t *pwszOut, const wchar_t* pwszBase = 0);
#else
EXTERN_C MIR_CORE_DLL(int) PathToRelative(const char *pszSrc, char *pszOut, const char* pszBase);
EXTERN_C MIR_CORE_DLL(int) PathToRelativeW(const wchar_t *pwszSrc, wchar_t *pwszOut, const wchar_t* pwszBase);
#endif

/////////////////////////////////////////////////////////////////////////////////////////
// Saves a path to a absolute path (from the miranda directory)
//
// [pszSrc] is the path to convert and [pszOut] is the buffer that
// the new path is copied too. pszOut MUST be of the size MAX_PATH.
//
// [pszBase] is the folder that is treated as root for 'relativity'
// by default = path to miranda32.exe
//
// Returns numbers of chars copied.

#if defined( __cplusplus )
EXTERN_C MIR_CORE_DLL(int) PathToAbsolute(const char *pszSrc, char *pszOut, const char* pszBase = 0);
EXTERN_C MIR_CORE_DLL(int) PathToAbsoluteW(const wchar_t *pwszSrc, wchar_t *pwszOut, const wchar_t* pwszBase = 0);
#else
EXTERN_C MIR_CORE_DLL(int) PathToAbsolute(const char *pszSrc, char *pszOut, const char* pszBase);
EXTERN_C MIR_CORE_DLL(int) PathToAbsoluteW(const wchar_t *pwszSrc, wchar_t *pwszOut, const wchar_t* pwszBase);
#endif

/////////////////////////////////////////////////////////////////////////////////////////
// Creates a directory tree (even more than one directories levels are missing)
// Returns 0 on success or an error code otherwise

EXTERN_C MIR_CORE_DLL(int) CreateDirectoryTree(const char *pszDir);
EXTERN_C MIR_CORE_DLL(int) CreateDirectoryTreeW(const wchar_t *pwszDir);

/////////////////////////////////////////////////////////////////////////////////////////
// Creates all subdirectories required to create a file with the file name given
// Returns 0 on success or an error code otherwise

EXTERN_C MIR_CORE_DLL(void) CreatePathToFile(char *wszFilePath);
EXTERN_C MIR_CORE_DLL(void) CreatePathToFileW(wchar_t *wszFilePath);

/////////////////////////////////////////////////////////////////////////////////////////
// Checks if a file name is absolute or not
// returns TRUE if yes or FALSE if not

EXTERN_C MIR_CORE_DLL(int) PathIsAbsolute(const char *pSrc);
EXTERN_C MIR_CORE_DLL(int) PathIsAbsoluteW(const wchar_t *pSrc);

/////////////////////////////////////////////////////////////////////////////////////////
// Generates Random number of any length
// cbLen = length of the random number to generate
// pszDest = pointer to array to fill with random number

EXTERN_C MIR_CORE_DLL(void) Utils_GetRandom(void *pszDest, size_t cbLen);

/////////////////////////////////////////////////////////////////////////////////////////
// Checks if a string is RTL

EXTERN_C MIR_CORE_DLL(bool) Utils_IsRtl(const wchar_t *pszwText);

/////////////////////////////////////////////////////////////////////////////////////////
// Replace variables in text
// wParam = (char*/wchar_t*/wchar_t*)string (depends on RVF_UNICODE/RVF_TCHAR flag)
// lParam = (REPLACEVARSDATA *) data about variables, item with key = 0 terminates the list
// returns new string, use mir_free to destroy

// variables known by the core:
// ----------------------------
// %miranda_profilesdir%  -> same as MS_DB_GETPROFILEPATH, base folder for all profiles
// %miranda_userdata% -> the active profile folder (home of the .dat file and all
//                       profile data)
// %miranda_path%     -> home path of the miranda installation (installation path
//                       of miranda32/64.exe
// %miranda_profilename% -> Name of the profile in use. Essentially, the name of the
//                          .dat file without file name extension. Also: the folder name
//                          relative to %miranda_profilesdir% where all profile data is stored.
// %miranda_logpath%     -> base folder for log files. This is \Logs relative to the
//                          current profile folder.
// %miranda_avatarcache% -> base folder for all protocol avatars. internal use only.
//
// the following variables operate on contacts. REPLACEVARSDATA::hContact must be
// supplied by the caller.
//
// %nick%                -> a contact nick name.
// %proto%               -> internal protocol name for a given contact. NOT the user-
//                          defined account name.
// %accountname%         -> user-defined account name for a given contact.
// %userid%              -> Unique ID for a given contact (UIN, JID etc.)

// the following variables are system variables - unrelated to miranda profiles.

// %appdata%			-> same as %APPDATA% environment variable.
// %destkop%            -> location of the desktop folder in a user's profile.
// %mydocuments%        -> location of the "My Documents" shell folder.

struct REPLACEVARSARRAY
{
	MAllStrings key, value;
};

EXTERN_C MIR_APP_DLL(char*) Utils_ReplaceVars(const char *szData, MCONTACT hContact = 0, REPLACEVARSARRAY *vars = NULL);
EXTERN_C MIR_APP_DLL(wchar_t*) Utils_ReplaceVarsW(const wchar_t *szData, MCONTACT hContact = 0, REPLACEVARSARRAY *vars = NULL);

#if defined(__cplusplus)
	#if !defined(M_SYSTEM_CPP_H__)
		#include "m_system_cpp.h"
	#endif

	struct VARS : public ptrA
	{
		__forceinline VARS(const char *str) :
			ptrA(Utils_ReplaceVars(str))
		{}
	};

	struct VARSW : public ptrW
	{
		__forceinline VARSW(const wchar_t *str) :
			ptrW(Utils_ReplaceVarsW(str))
		{}
	};
#endif

/////////////////////////////////////////////////////////////////////////////////////////
// one field form

#define ESF_MULTILINE 1
#define ESF_COMBO     2
#define ESF_RICHEDIT  3
#define ESF_PASSWORD  4

struct ENTER_STRING
{
	int     cbSize;         // structure size
	int     type;           // one of ESF_* constants
	LPCSTR  szModuleName;   // module name to save window size and combobox strings
	LPCSTR  szDataPrefix;   // prefix for stored database variables
	LPCTSTR caption;        // window caption
	union {
		LPCTSTR ptszInitVal; // initial value (note: the core DOES NOT free it)
		LPTSTR  ptszResult;  // result entered (must be freed via mir_free)
	};
	int     recentCount;    // number of combobox strings to store
	int     timeout;        // timeout for the form auto-close
};

/////////////////////////////////////////////////////////////////////////////////////////
// enters one string
// wParam = 0 (unused)
// lParam = ENTER_STRING* (form description)
// returns TRUE on pressing OK or FALSE if Cancel was pressed

#define MS_UTILS_ENTERSTRING "Utils/EnterString"

__forceinline BOOL EnterString(ENTER_STRING *pForm)
{
	return (BOOL)CallService(MS_UTILS_ENTERSTRING, 0, (LPARAM)pForm);
}

#endif // M_UTILS_H__