ui/winui/util.cpp

Wed, 31 Jan 2024 19:15:41 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 31 Jan 2024 19:15:41 +0100
branch
newapi
changeset 242
4ff7361dce95
parent 224
88bc21b19213
permissions
-rw-r--r--

add file open dialog (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
224
88bc21b19213 add minimal working dnd implementation (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 205
diff changeset
11 return str2wstr_len(str, len, newlen);
88bc21b19213 add minimal working dnd implementation (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 205
diff changeset
12 }
180
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13
224
88bc21b19213 add minimal working dnd implementation (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 205
diff changeset
14 wchar_t* str2wstr_len(const char* str, size_t len, int* newlen) {
180
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 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
16 int wlen = MultiByteToWideChar(
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 CP_UTF8,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 0,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 str,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 len,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 wstr,
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 len + 1
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 if (newlen) {
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 *newlen = wlen;
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 }
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 wstr[wlen] = 0;
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
f34953bf4ac7 add basic winui menu implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 return wstr;
192
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
30 }
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
31
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
32 char* wchar2utf8(const wchar_t* wstr, size_t wlen) {
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
33 size_t maxlen = wlen * 4;
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
34 char* ret = (char*)malloc(maxlen + 1);
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
35 int ret_len = WideCharToMultiByte(
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
36 CP_UTF8,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
37 0,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
38 wstr,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
39 wlen,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
40 ret,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
41 maxlen,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
42 NULL,
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
43 NULL);
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
44 ret[ret_len] = 0;
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
45 return ret;
bcacd00ea955 implement textfield (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
46 }

mercurial