ui/winui/util.cpp

Sun, 29 Sep 2024 13:32:51 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Sep 2024 13:32:51 +0200
changeset 44
473954dc6b74
parent 0
2483f517c562
permissions
-rw-r--r--

update toolkit: gtk4 improvements

0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 #include "pch.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 #include "util.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 #include <stdlib.h>
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 wchar_t* str2wstr(const char* str, int* newlen) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 size_t len = strlen(str);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 return str2wstr_len(str, len, newlen);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 wchar_t* str2wstr_len(const char* str, size_t len, int* newlen) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 wchar_t* wstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t));
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 int wlen = MultiByteToWideChar(
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 CP_UTF8,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 0,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 str,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 len,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 wstr,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 len + 1
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 );
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 if (newlen) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 *newlen = wlen;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 wstr[wlen] = 0;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 return wstr;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 char* wchar2utf8(const wchar_t* wstr, size_t wlen) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 size_t maxlen = wlen * 4;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 char* ret = (char*)malloc(maxlen + 1);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 int ret_len = WideCharToMultiByte(
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 CP_UTF8,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 0,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 wstr,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 wlen,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 ret,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 maxlen,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 NULL,
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 NULL);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 ret[ret_len] = 0;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 return ret;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 }

mercurial