ui/winui/util.cpp

Thu, 12 Oct 2023 16:03:35 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 12 Oct 2023 16:03:35 +0200
branch
newapi
changeset 213
7e39db525fd9
parent 205
b1ac0dd1d38b
child 224
88bc21b19213
permissions
-rw-r--r--

add icons (WinUI3)

205
b1ac0dd1d38b new winui vs project, add toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 192
diff changeset
1 #include "pch.h"
180
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 #include "util.h"
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 #include <stdlib.h>
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 wchar_t* str2wstr(const char* str, int* newlen) {
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 size_t len = strlen(str);
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 wchar_t* wstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t));
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 int wlen = MultiByteToWideChar(
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 CP_UTF8,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 0,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 str,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 len,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 wstr,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 len + 1
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 );
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 if (newlen) {
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 *newlen = wlen;
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 }
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 wstr[wlen] = 0;
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 return wstr;
192
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
27 }
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
28
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
29 char* wchar2utf8(const wchar_t* wstr, size_t wlen) {
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
30 size_t maxlen = wlen * 4;
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
31 char* ret = (char*)malloc(maxlen + 1);
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
32 int ret_len = WideCharToMultiByte(
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
33 CP_UTF8,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
34 0,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
35 wstr,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
36 wlen,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
37 ret,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
38 maxlen,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
39 NULL,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
40 NULL);
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
41 ret[ret_len] = 0;
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
42 return ret;
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
43 }

mercurial