Debian Codegraph @

addons/native_dialog/win_dialog.c

raw
1 /* Each of these files implements the same, for different GUI toolkits:
2 *
3 * dialog.c - code shared between all platforms
4 * gtk_dialog.c - GTK file open dialog
5 * osx_dialog.m - OSX file open dialog
6 * qt_dialog.cpp - Qt file open dialog
7 * win_dialog.c - Windows file open dialog
8 *
9 */
10
11 #define ALLEGRO_INTERNAL_UNSTABLE
12 #include "allegro5/allegro.h"
13 #include "allegro5/allegro_native_dialog.h"
14 #include "allegro5/internal/aintern.h"
15 #include "allegro5/internal/aintern_native_dialog.h"
16
17 #include "allegro5/platform/aintwin.h"
18 #include "allegro5/internal/aintern_wunicode.h"
19
20 #include "allegro5/allegro_windows.h"
21
22 #include <string.h>
23
24 /* We use RichEdit by default. */
25 #include <richedit.h>
26 #include <shlobj.h> // for folder selector
27
28 ALLEGRO_DEBUG_CHANNEL("win_dialog")
29
30 #define WM_SHOW_POPUP (WM_APP + 42)
31 #define WM_HIDE_MENU (WM_APP + 43)
32 #define WM_SHOW_MENU (WM_APP + 44)
33
34 /* Reference count for shared resources. */
35 static int wlog_count = 0;
36
37 /* Handle of RichEdit module */
38 static void *wlog_rich_edit_module = 0;
39
40 /* Name of the edit control. Depend on system resources. */
41 static TCHAR* wlog_edit_control = TEXT("EDIT");
42
43 /* True if output support unicode */
44 static bool wlog_unicode = false;
45
46 static ALLEGRO_MUTEX* global_mutex;
47
48 static ALLEGRO_COND* wm_size_cond;
49 static bool got_wm_size_event = false;
50
51 /* For Unicode support, define some helper functions
52 * which work with either UTF-16 or ANSI code pages
53 * depending on whether UNICODE is defined or not.
54 */
55
56 /* tcreate_path:
57 * Create path from TCHARs
58 */
59 static ALLEGRO_PATH* _tcreate_path(const TCHAR* ts)
60 {
61 char* tmp = _twin_tchar_to_utf8(ts);
62 ALLEGRO_PATH* path = al_create_path(tmp);
63 al_free(tmp);
64 return path;
65 }
66 /* tcreate_path:
67 * Create directory path from TCHARs
68 */
69 static ALLEGRO_PATH* _tcreate_path_for_directory(const TCHAR* ts)
70 {
71 char* tmp = _twin_tchar_to_utf8(ts);
72 ALLEGRO_PATH* path = al_create_path_for_directory(tmp);
73 al_free(tmp);
74 return path;
75 }
76
77 bool _al_init_native_dialog_addon(void)
78 {
79 global_mutex = al_create_mutex();
80 wm_size_cond = al_create_cond();
81 if (!global_mutex || !wm_size_cond) {
82 al_destroy_mutex(global_mutex);
83 al_destroy_cond(wm_size_cond);
84 return false;
85 }
86 return true;
87 }
88
89 void _al_shutdown_native_dialog_addon(void)
90 {
91 al_destroy_mutex(global_mutex);
92 al_destroy_cond(wm_size_cond);
93 global_mutex = NULL;
94 wm_size_cond = NULL;
95 }
96
97 static INT CALLBACK _browse_callback_proc(HWND hwnd, UINT uMsg, LPARAM unused, LPARAM pData)
98 {
99 (void)unused;
100 if (uMsg == BFFM_INITIALIZED) SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
101 return 0;
102 }
103
104 static TCHAR* _extract_dirname(ALLEGRO_PATH *fullpath)
105 {
106 TCHAR* wpath = NULL;
107 bool is_dir;
108 const ALLEGRO_USTR *path = al_path_ustr(fullpath, ALLEGRO_NATIVE_PATH_SEP);
109
110 if (al_filename_exists(al_cstr(path))) {
111 ALLEGRO_FS_ENTRY *fs = al_create_fs_entry(al_cstr(path));
112 is_dir = al_get_fs_entry_mode(fs) & ALLEGRO_FILEMODE_ISDIR;
113 al_destroy_fs_entry(fs);
114 }
115 else {
116 is_dir = false;
117 }
118
119 if (is_dir) {
120 wpath = _twin_ustr_to_tchar(path);
121 }
122 else {
123 /* Extract the directory from the path. */
124
125 ALLEGRO_PATH* initial_dir_path = NULL;
126 initial_dir_path = al_clone_path(fullpath);
127 if (initial_dir_path) {
128 al_set_path_filename(initial_dir_path, NULL);
129 wpath = _twin_utf8_to_tchar(al_path_cstr(initial_dir_path, ALLEGRO_NATIVE_PATH_SEP));
130 al_destroy_path(initial_dir_path);
131 }
132 }
133 return wpath;
134 }
135
136 static bool select_folder(ALLEGRO_DISPLAY_WIN *win_display,
137 ALLEGRO_NATIVE_DIALOG *fd)
138 {
139 BROWSEINFO folderinfo;
140 TCHAR* wpath = NULL;
141 LPCITEMIDLIST pidl;
142 /* Selected path */
143 TCHAR buf[MAX_PATH] = TEXT("");
144 /* Display name */
145 TCHAR dbuf[MAX_PATH] = TEXT("");
146
147 folderinfo.hwndOwner = win_display ? win_display->window : NULL;
148 folderinfo.pidlRoot = NULL;
149 folderinfo.pszDisplayName = dbuf;
150 folderinfo.lpszTitle = _twin_ustr_to_tchar(fd->title);
151 folderinfo.ulFlags = 0;
152 folderinfo.lpfn = NULL;
153
154 if (fd->fc_initial_path) {
155 wpath = _extract_dirname(fd->fc_initial_path);
156 folderinfo.lpfn = _browse_callback_proc;
157 folderinfo.lParam = (LPARAM) wpath;
158 }
159
160 pidl = SHBrowseForFolder(&folderinfo);
161
162 al_free((void*) folderinfo.lpszTitle);
163 al_free(wpath);
164
165 if (pidl) {
166 SHGetPathFromIDList(pidl, buf);
167 fd->fc_path_count = 1;
168 fd->fc_paths = al_malloc(sizeof(void *));
169 fd->fc_paths[0] = _tcreate_path(buf);
170 return true;
171 }
172 return false;
173 }
174
175 static ALLEGRO_USTR *create_filter_string(const _AL_VECTOR *patterns)
176 {
177 ALLEGRO_USTR *filter = al_ustr_new("");
178 for (size_t i = 0; i < _al_vector_size(patterns); i++)
179 {
180 _AL_PATTERNS_AND_DESC *patterns_and_desc = _al_vector_ref(patterns, i);
181 bool any_valid = false;
182 for (size_t j = 0; j < _al_vector_size(&patterns_and_desc->patterns_vec); j++) {
183 _AL_PATTERN *pattern = _al_vector_ref(&patterns_and_desc->patterns_vec, j);
184 any_valid |= !pattern->is_mime;
185 }
186 if (!any_valid) {
187 continue;
188 }
189 const ALLEGRO_USTR *desc = al_ref_info(&patterns_and_desc->desc);
190 if (al_ustr_size(desc) > 0) {
191 al_ustr_append(filter, desc);
192 }
193 else {
194 al_ustr_append_cstr(filter, "All supported files");
195 }
196 al_ustr_append_chr(filter, '\0');
197 bool first = true;
198 for (size_t j = 0; j < _al_vector_size(&patterns_and_desc->patterns_vec); j++) {
199 _AL_PATTERN *pattern = _al_vector_ref(&patterns_and_desc->patterns_vec, j);
200 if (!pattern->is_mime) {
201 if (!first) {
202 al_ustr_append_chr(filter, ';');
203 }
204 first = false;
205 if (pattern->is_catchall)
206 al_ustr_append_cstr(filter, "*.*");
207 else
208 al_ustr_append(filter, al_ref_info(&pattern->info));
209 }
210 }
211 al_ustr_append_chr(filter, '\0');
212 }
213
214 al_ustr_append_chr(filter, '\0');
215 return filter;
216 }
217
218 static int skip_nul_terminated_string(TCHAR *s)
219 {
220 int i = 0;
221
222 while (s[i])
223 i++;
224
225 return i+1;
226 }
227
228 bool _al_show_native_file_dialog(ALLEGRO_DISPLAY *display,
229 ALLEGRO_NATIVE_DIALOG *fd)
230 {
231 OPENFILENAME ofn;
232 ALLEGRO_DISPLAY_WIN *win_display;
233 int flags = 0;
234 bool ret;
235 TCHAR buf[4096];
236 const int BUFSIZE = sizeof(buf) / sizeof(TCHAR);
237 TCHAR* wfilter = NULL;
238 TCHAR* wpath = NULL;
239 ALLEGRO_USTR *filter_string = NULL;
240
241 buf[0] = '\0';
242
243 win_display = (ALLEGRO_DISPLAY_WIN *)display;
244
245 if (fd->flags & ALLEGRO_FILECHOOSER_FOLDER) {
246 return select_folder(win_display, fd);
247 }
248
249 /* Selecting a file. */
250 memset(&ofn, 0, sizeof(OPENFILENAME));
251 ofn.lStructSize = sizeof(OPENFILENAME);
252 ofn.hwndOwner = win_display ? win_display->window : NULL;
253
254 /* Create filter string. */
255 if (_al_vector_size(&fd->fc_patterns) > 0) {
256 filter_string = create_filter_string(&fd->fc_patterns);
257 wfilter = _twin_ustr_to_tchar(filter_string);
258 ofn.lpstrFilter = wfilter;
259 }
260 else {
261 /* List all files by default. */
262 ofn.lpstrFilter = TEXT("All Files\0*.*\0\0");
263 }
264
265 /* Provide buffer for file chosen by dialog. */
266 ofn.lpstrFile = buf;
267 ofn.nMaxFile = BUFSIZE;
268
269 /* Initialize file name buffer and starting directory. */
270 if (fd->fc_initial_path) {
271
272 wpath = _extract_dirname(fd->fc_initial_path);
273 ofn.lpstrInitialDir = wpath;
274 }
275
276 if (fd->title)
277 ofn.lpstrTitle = _twin_ustr_to_tchar(fd->title);
278
279 flags |= OFN_NOCHANGEDIR | OFN_EXPLORER;
280 if (fd->flags & ALLEGRO_FILECHOOSER_SAVE) {
281 flags |= OFN_OVERWRITEPROMPT;
282 }
283 else {
284 flags |= (fd->flags & ALLEGRO_FILECHOOSER_FILE_MUST_EXIST) ? OFN_FILEMUSTEXIST : 0;
285 }
286 flags |= (fd->flags & ALLEGRO_FILECHOOSER_MULTIPLE) ? OFN_ALLOWMULTISELECT : 0;
287 flags |= (fd->flags & ALLEGRO_FILECHOOSER_SHOW_HIDDEN) ? 0x10000000 : 0; // NOTE: 0x10000000 is FORCESHOWHIDDEN
288 ofn.Flags = flags;
289
290 if (flags & OFN_OVERWRITEPROMPT) {
291 ret = GetSaveFileName(&ofn);
292 }
293 else {
294 ret = GetOpenFileName(&ofn);
295 }
296
297 al_free((void*) ofn.lpstrTitle);
298 al_free(wfilter);
299 al_free(wpath);
300 al_ustr_free(filter_string);
301
302 if (!ret) {
303 DWORD err = GetLastError();
304 if (err != ERROR_SUCCESS) {
305 ALLEGRO_ERROR("al_show_native_file_dialog failed: %s\n", _al_win_error(err));
306 }
307 return false;
308 }
309
310 if (flags & OFN_ALLOWMULTISELECT) {
311 int i = 0;
312 /* Count number of file names in buf. */
313 fd->fc_path_count = 0;
314 while (1) {
315 int j = skip_nul_terminated_string(buf + i);
316 if (j <= 1)
317 break;
318 fd->fc_path_count++;
319 i += j;
320 }
321 }
322 else {
323 fd->fc_path_count = 1;
324 }
325
326 if (fd->fc_path_count == 1) {
327 fd->fc_paths = al_malloc(sizeof(void *));
328 fd->fc_paths[0] = _tcreate_path(buf);
329 }
330 else {
331 int i, p;
332 /* If multiple files were selected, the first string in buf is the
333 * directory name, followed by each of the file names terminated by NUL.
334 */
335 fd->fc_path_count -= 1;
336 fd->fc_paths = al_malloc(fd->fc_path_count * sizeof(void *));
337 i = skip_nul_terminated_string(buf);
338 for (p = 0; p < (int)fd->fc_path_count; p++) {
339 fd->fc_paths[p] = _tcreate_path_for_directory(buf);
340 char* tmp = _twin_tchar_to_utf8(buf + i);
341 al_set_path_filename(fd->fc_paths[p], tmp);
342 al_free(tmp);
343 i += skip_nul_terminated_string(buf+i);
344 }
345 }
346
347 return true;
348 }
349
350 int _al_show_native_message_box(ALLEGRO_DISPLAY *display,
351 ALLEGRO_NATIVE_DIALOG *fd)
352 {
353 UINT type = MB_SETFOREGROUND;
354 int result;
355 TCHAR *text, *title;
356
357 /* Note: the message box code cannot assume that Allegro is installed. */
358
359 if (fd->flags & ALLEGRO_MESSAGEBOX_QUESTION)
360 type |= MB_ICONQUESTION;
361 else if (fd->flags & ALLEGRO_MESSAGEBOX_WARN)
362 type |= MB_ICONWARNING;
363 else if (fd->flags & ALLEGRO_MESSAGEBOX_ERROR)
364 type |= MB_ICONERROR;
365 else
366 type |= MB_ICONINFORMATION;
367
368 if (fd->flags & ALLEGRO_MESSAGEBOX_YES_NO)
369 type |= MB_YESNO;
370 else if (fd->flags & ALLEGRO_MESSAGEBOX_OK_CANCEL)
371 type |= MB_OKCANCEL;
372
373 /* heading + text are combined together */
374
375 if (al_ustr_size(fd->mb_heading))
376 al_ustr_append_cstr(fd->mb_heading, "\n\n");
377
378 al_ustr_append(fd->mb_heading, fd->mb_text);
379
380 text = _twin_ustr_to_tchar(fd->mb_heading);
381 if (!text) {
382 return 0;
383 }
384 title = _twin_ustr_to_tchar(fd->title);
385 if (!title) {
386 al_free(text);
387 return 0;
388 }
389 result = MessageBox(al_get_win_window_handle(display),
390 text, title, type);
391
392 al_free(text);
393 al_free(title);
394
395 if (result == IDYES || result == IDOK)
396 return 1;
397 else
398 return 2;
399 }
400
401
402 /* Emit close event. */
403 static void wlog_emit_close_event(ALLEGRO_NATIVE_DIALOG *textlog, bool keypress)
404 {
405 ALLEGRO_EVENT event;
406 event.user.type = ALLEGRO_EVENT_NATIVE_DIALOG_CLOSE;
407 event.user.timestamp = al_get_time();
408 event.user.data1 = (intptr_t)textlog;
409 event.user.data2 = (intptr_t)keypress;
410 al_emit_user_event(&textlog->tl_events, &event, NULL);
411 }
412
413 /* convert_crlf:
414 * Alter this string to change every LF to CRLF
415 */
416 static ALLEGRO_USTR* convert_crlf(ALLEGRO_USTR* s) {
417 int pos = 0;
418 int ch, prev;
419 while ((pos = al_ustr_find_chr(s, pos, '\n')) >= 0) {
420 prev = pos;
421 ch = al_ustr_prev_get(s, &prev);
422 if (ch == -2) {
423 return s;
424 } else if (ch != '\r') {
425 al_ustr_insert_chr(s, pos, '\r');
426 al_ustr_next(s, &pos);
427 }
428 al_ustr_next(s, &pos);
429 }
430 return s;
431 }
432 /* General function to output log message. */
433 static void wlog_do_append_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog)
434 {
435 int index;
436 index = GetWindowTextLength(textlog->tl_textview);
437 SendMessage(textlog->tl_textview, EM_SETSEL, (WPARAM)index, (LPARAM)index);
438 convert_crlf(textlog->tl_pending_text);
439 TCHAR* buf = _twin_utf8_to_tchar(al_cstr(textlog->tl_pending_text));
440 SendMessage(textlog->tl_textview, EM_REPLACESEL, 0, (LPARAM) buf);
441 al_free(buf);
442 al_ustr_truncate(textlog->tl_pending_text, 0);
443 textlog->tl_have_pending = false;
444
445 SendMessage(textlog->tl_textview, WM_VSCROLL, SB_BOTTOM, 0);
446 }
447
448 /* Text log window callback. */
449 static LRESULT CALLBACK wlog_text_log_callback(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
450 {
451 CREATESTRUCT* create_struct;
452
453 ALLEGRO_NATIVE_DIALOG* textlog = (ALLEGRO_NATIVE_DIALOG*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
454
455 switch (uMsg) {
456 case WM_CREATE:
457 /* Set user data for window, so we will be able to retrieve text log structure any time */
458 create_struct = (CREATESTRUCT*)lParam;
459 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)create_struct->lpCreateParams);
460 break;
461
462 case WM_CLOSE:
463 if (textlog->is_active) {
464 if (!(textlog->flags & ALLEGRO_TEXTLOG_NO_CLOSE)) {
465 wlog_emit_close_event(textlog, false);
466 }
467 return 0;
468 }
469 break;
470
471 case WM_DESTROY:
472 PostQuitMessage(0);
473 break;
474
475 case WM_KEYDOWN:
476 if (wParam == VK_ESCAPE) {
477 wlog_emit_close_event(textlog, true);
478 }
479
480 break;
481
482 case WM_MOVE:
483 InvalidateRect(hWnd, NULL, FALSE);
484 break;
485
486 case WM_SIZE:
487 /* Match text log size to parent client area */
488 if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED) {
489 RECT client_rect;
490 GetClientRect(hWnd, &client_rect);
491 MoveWindow(textlog->tl_textview, client_rect.left, client_rect.top,
492 client_rect.right - client_rect.left, client_rect.bottom - client_rect.top, TRUE);
493 }
494 break;
495
496 case WM_USER:
497 al_lock_mutex(textlog->tl_text_mutex);
498 wlog_do_append_native_text_log(textlog);
499 al_unlock_mutex(textlog->tl_text_mutex);
500 break;
501 }
502
503 return DefWindowProc(hWnd, uMsg, wParam, lParam);
504 }
505
506
507 /* We hold textlog->tl_text_mutex. */
508 static bool open_native_text_log_inner(ALLEGRO_NATIVE_DIALOG *textlog)
509 {
510 LPCTSTR font_name;
511 HWND hWnd;
512 HWND hLog;
513 RECT client_rect;
514 HFONT hFont;
515 MSG msg;
516 BOOL ret;
517 TCHAR* title;
518
519 /* Create text log window. */
520 title = _twin_ustr_to_tchar(textlog->title);
521 hWnd = CreateWindow(TEXT("Allegro Text Log"), title,
522 WS_CAPTION | WS_SIZEBOX | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU,
523 CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL,
524 (HINSTANCE)GetModuleHandle(NULL), textlog);
525 al_free(title);
526 if (!hWnd) {
527 ALLEGRO_ERROR("CreateWindow failed\n");
528 return false;
529 }
530
531 /* Get client area of the log window. */
532 GetClientRect(hWnd, &client_rect);
533
534 /* Create edit control. */
535 hLog = CreateWindow(wlog_edit_control, NULL,
536 WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN | ES_AUTOVSCROLL | ES_READONLY,
537 client_rect.left, client_rect.top, client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
538 hWnd, NULL, (HINSTANCE)GetModuleHandle(NULL), NULL);
539 if (!hLog) {
540 ALLEGRO_ERROR("CreateWindow failed\n");
541 DestroyWindow(hWnd);
542 return false;
543 }
544
545 /* Enable double-buffering. */
546 SetWindowLong(hLog, GWL_EXSTYLE, GetWindowLong(hLog, GWL_EXSTYLE) | 0x02000000L/*WS_EX_COMPOSITED*/);
547
548 /* Select font name. */
549 if (textlog->flags & ALLEGRO_TEXTLOG_MONOSPACE)
550 font_name = TEXT("Courier New");
551 else
552 font_name = TEXT("Arial");
553
554 /* Create font and set font. */
555 hFont = CreateFont(-11, 0, 0, 0, FW_LIGHT, 0, 0,
556 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
557 FF_MODERN | FIXED_PITCH, font_name);
558
559 /* Assign font to the text log. */
560 if (hFont) {
561 SendMessage(hLog, WM_SETFONT, (WPARAM)hFont, 0);
562 }
563
564 /* We are ready to show our window. */
565 ShowWindow(hWnd, SW_NORMAL);
566
567 /* Save handles for future use. */
568 textlog->window = hWnd;
569 textlog->tl_textview = hLog;
570 textlog->is_active = true;
571
572 /* Now notify al_show_native_textlog that the text log is ready. */
573 textlog->tl_done = true;
574 al_signal_cond(textlog->tl_text_cond);
575 al_unlock_mutex(textlog->tl_text_mutex);
576
577 /* Process messages. */
578 while ((ret = GetMessage(&msg, NULL, 0, 0)) != 0) {
579 if (ret != -1 && msg.message != WM_QUIT) {
580 /* Intercept child window key down messages. Needed to track
581 * hit of ESCAPE key while text log have focus. */
582 if (msg.hwnd != textlog->window && msg.message == WM_KEYDOWN) {
583 PostMessage(textlog->window, WM_KEYDOWN, msg.wParam, msg.lParam);
584 }
585 TranslateMessage(&msg);
586 DispatchMessage(&msg);
587 }
588 else
589 break;
590 }
591
592 /* Close window. Should be already closed, this is just sanity. */
593 if (IsWindow(textlog->window)) {
594 DestroyWindow(textlog->window);
595 }
596
597 /* Release font. We don't want to leave any garbage. */
598 DeleteObject(hFont);
599
600 /* Notify everyone that we're gone. */
601 al_lock_mutex(textlog->tl_text_mutex);
602 textlog->tl_done = true;
603 al_signal_cond(textlog->tl_text_cond);
604
605 return true;
606 }
607
608 bool _al_open_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog)
609 {
610 WNDCLASS text_log_class;
611 bool rc;
612
613 al_lock_mutex(textlog->tl_text_mutex);
614
615 /* Note: the class registration and rich edit module loading are not
616 * implemented in a thread-safe manner (pretty unlikely).
617 */
618
619 /* Prepare text log class info. */
620 if (wlog_count == 0) {
621 ALLEGRO_DEBUG("Register text log class\n");
622
623 memset(&text_log_class, 0, sizeof(text_log_class));
624 text_log_class.hInstance = (HINSTANCE)GetModuleHandle(NULL);
625 text_log_class.lpszClassName = TEXT("Allegro Text Log");
626 text_log_class.lpfnWndProc = wlog_text_log_callback;
627 text_log_class.hIcon = NULL;
628 text_log_class.hCursor = NULL;
629 text_log_class.lpszMenuName = NULL;
630 text_log_class.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
631
632 if (RegisterClass(&text_log_class) == 0) {
633 /* Failure, window class is a basis and we do not have one. */
634 ALLEGRO_ERROR("RegisterClass failed\n");
635 al_unlock_mutex(textlog->tl_text_mutex);
636 return false;
637 }
638 }
639
640 /* Load RichEdit control. */
641 if (wlog_count == 0) {
642 ALLEGRO_DEBUG("Load rich edit module\n");
643 ALLEGRO_ASSERT(!wlog_rich_edit_module);
644
645 if ((wlog_rich_edit_module = _al_open_library("msftedit.dll"))) {
646 /* 4.1 and emulation of 3.0, 2.0, 1.0 */
647 wlog_edit_control = TEXT("RICHEDIT50W"); /*MSFTEDIT_CLASS*/
648 wlog_unicode = true;
649 }
650 else if ((wlog_rich_edit_module = _al_open_library("riched20.dll"))) {
651 /* 3.0, 2.0 */
652 wlog_edit_control = TEXT("RichEdit20W"); /*RICHEDIT_CLASS*/
653 wlog_unicode = true;
654 }
655 else if ((wlog_rich_edit_module = _al_open_library("riched32.dll"))) {
656 /* 1.0 */
657 wlog_edit_control = TEXT("RichEdit"); /*RICHEDIT_CLASS*/
658 wlog_unicode = false;
659 }
660 else {
661 wlog_edit_control = TEXT("EDIT");
662 wlog_unicode = false;
663 }
664 }
665
666 wlog_count++;
667 ALLEGRO_DEBUG("wlog_count = %d\n", wlog_count);
668
669 rc = open_native_text_log_inner(textlog);
670
671 wlog_count--;
672 ALLEGRO_DEBUG("wlog_count = %d\n", wlog_count);
673
674 /* Release RichEdit module. */
675 if (wlog_count == 0 && wlog_rich_edit_module) {
676 ALLEGRO_DEBUG("Unload rich edit module\n");
677 _al_close_library(wlog_rich_edit_module);
678 wlog_rich_edit_module = NULL;
679 }
680
681 /* Unregister window class. */
682 if (wlog_count == 0) {
683 ALLEGRO_DEBUG("Unregister text log class\n");
684 UnregisterClass(TEXT("Allegro Text Log"), (HINSTANCE)GetModuleHandle(NULL));
685 }
686
687 al_unlock_mutex(textlog->tl_text_mutex);
688
689 return rc;
690 }
691
692 void _al_close_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog)
693 {
694 /* Just deactivate text log and send bye, bye message. */
695 if (IsWindow(textlog->window)) {
696 textlog->is_active = false;
697 PostMessage(textlog->window, WM_CLOSE, 0, 0);
698 }
699 }
700
701 void _al_append_native_text_log(ALLEGRO_NATIVE_DIALOG *textlog)
702 {
703 if (textlog->tl_have_pending)
704 return;
705 textlog->tl_have_pending = true;
706
707 /* Post text as user message. This guarantees that the whole processing will
708 * take place on text log thread.
709 */
710 if (IsWindow(textlog->window)) {
711 PostMessage(textlog->window, WM_USER, (WPARAM)textlog, 0);
712 }
713 }
714
715 static bool menu_callback(ALLEGRO_DISPLAY *display, UINT msg, WPARAM wParam, LPARAM lParam,
716 LPARAM* result, void *userdata)
717 {
718 (void) userdata;
719 *result = 0;
720
721 if (msg == WM_COMMAND && lParam == 0) {
722 const int id = LOWORD(wParam);
723 _AL_MENU_ID *menu_id = _al_find_parent_menu_by_id(display, id);
724 if (menu_id) {
725 int index;
726 if (_al_find_menu_item_unique(menu_id->menu, id, NULL, &index)) {
727 if (al_get_menu_item_flags(menu_id->menu, -index) & ALLEGRO_MENU_ITEM_CHECKBOX) {
728 /* Toggle the checkbox, since Windows doesn't do that automatically. */
729 al_toggle_menu_item_flags(menu_id->menu, -index, ALLEGRO_MENU_ITEM_CHECKED);
730 }
731 }
732 }
733 _al_emit_menu_event(display, id);
734 return true;
735 }
736 else if (msg == WM_SYSCOMMAND) {
737 if ((wParam & 0xfff0) == SC_KEYMENU && al_get_display_menu(display) != NULL) {
738 /* Allow the ALT key to open the menu
739 * XXX: do we even want to do this? Should it be optional?
740 */
741 DefWindowProc(al_get_win_window_handle(display), msg, wParam, lParam);
742 return true;
743 }
744 }
745 else if (msg == WM_SHOW_POPUP) {
746 ALLEGRO_MENU *menu = (ALLEGRO_MENU *) lParam;
747 HWND hwnd = al_get_win_window_handle(display);
748 POINT pos;
749 GetCursorPos(&pos);
750 SetForegroundWindow(hwnd);
751 TrackPopupMenuEx((HMENU) menu->extra1, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, pos.x, pos.y, hwnd, NULL);
752
753 return true;
754 }
755 else if (msg == WM_HIDE_MENU) {
756 SetMenu(al_get_win_window_handle(display), NULL);
757 return true;
758 }
759 else if (msg == WM_SHOW_MENU) {
760 ALLEGRO_MENU *menu = (ALLEGRO_MENU *) lParam;
761 SetMenu(al_get_win_window_handle(display), (HMENU) menu->extra1);
762 return true;
763 }
764 else if (msg == WM_SIZE) {
765 ALLEGRO_DEBUG("Got the WM_SIZE event.\n");
766 got_wm_size_event = true;
767 al_signal_cond(wm_size_cond);
768 }
769 else if (msg == WM_MENUSELECT) {
770 /* XXX: could use this as a way to indicate the popup menu was canceled */
771 }
772
773 return false;
774 }
775
776 static void init_menu_info(MENUITEMINFO *info, ALLEGRO_MENU_ITEM *menu)
777 {
778 memset(info, 0, sizeof(*info));
779
780 info->cbSize = sizeof(*info);
781 info->fMask = MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_STRING | MIIM_CHECKMARKS;
782 info->wID = menu->unique_id;
783
784 if (!menu->caption) {
785 info->fType = MFT_SEPARATOR;
786 }
787 else {
788 info->fType = MFT_STRING;
789 info->dwTypeData = _twin_ustr_to_tchar(menu->caption);
790 info->cch = al_ustr_size(menu->caption);
791 }
792
793 if (menu->flags & ALLEGRO_MENU_ITEM_CHECKED) {
794 info->fState |= MFS_CHECKED;
795 }
796
797 if (menu->flags & ALLEGRO_MENU_ITEM_DISABLED) {
798 info->fState |= MFS_DISABLED;
799 }
800
801 if (menu->icon) {
802 /* convert ALLEGRO_BITMAP to HBITMAP (could be moved into a public function) */
803 const int h = al_get_bitmap_height(menu->icon), w = al_get_bitmap_width(menu->icon);
804 HDC hdc;
805 HBITMAP hbmp;
806 BITMAPINFO bi;
807 uint8_t *data = NULL;
808 ALLEGRO_LOCKED_REGION *lock;
809
810 ZeroMemory(&bi, sizeof(BITMAPINFO));
811 bi.bmiHeader.biSize = sizeof(BITMAPINFO);
812 bi.bmiHeader.biWidth = w;
813 bi.bmiHeader.biHeight = -h;
814 bi.bmiHeader.biPlanes = 1;
815 bi.bmiHeader.biBitCount = 32;
816 bi.bmiHeader.biCompression = BI_RGB;
817
818 hdc = GetDC(menu->parent->display ? al_get_win_window_handle(menu->parent->display) : NULL);
819
820 hbmp = CreateDIBSection(hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS, (void **)&data, NULL, 0);
821
822 lock = al_lock_bitmap(menu->icon, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_READONLY);
823 memcpy(data, lock->data, w * h * 4);
824 al_unlock_bitmap(menu->icon);
825
826 info->hbmpUnchecked = hbmp;
827 menu->extra2 = hbmp;
828
829 ReleaseDC(menu->parent->display ? al_get_win_window_handle(menu->parent->display) : NULL, hdc);
830 }
831
832 if (menu->popup) {
833 info->hSubMenu = (HMENU) menu->popup->extra1;
834 }
835
836 }
837 static void destroy_menu_info(MENUITEMINFO *info) {
838 if (info->dwTypeData) {
839 al_free(info->dwTypeData);
840 }
841 }
842 bool _al_init_menu(ALLEGRO_MENU *menu)
843 {
844 menu->extra1 = CreateMenu();
845 return menu->extra1 != NULL;
846 }
847
848 bool _al_init_popup_menu(ALLEGRO_MENU *menu)
849 {
850 menu->extra1 = CreatePopupMenu();
851 return menu->extra1 != NULL;
852 }
853
854 bool _al_insert_menu_item_at(ALLEGRO_MENU_ITEM *item, int i)
855 {
856 MENUITEMINFO info;
857 init_menu_info(&info, item);
858
859 InsertMenuItem((HMENU) item->parent->extra1, i, TRUE, &info);
860 destroy_menu_info(&info);
861 return true;
862 }
863
864 bool _al_update_menu_item_at(ALLEGRO_MENU_ITEM *item, int i)
865 {
866 MENUITEMINFO info;
867
868 init_menu_info(&info, item);
869 SetMenuItemInfo((HMENU) item->parent->extra1, i, TRUE, &info);
870
871 if (item->parent->display)
872 DrawMenuBar(al_get_win_window_handle(item->parent->display));
873 destroy_menu_info(&info);
874 return true;
875 }
876
877 bool _al_destroy_menu_item_at(ALLEGRO_MENU_ITEM *item, int i)
878 {
879 DeleteMenu((HMENU) item->parent->extra1, i, MF_BYPOSITION);
880
881 if (item->parent->display)
882 DrawMenuBar(al_get_win_window_handle(item->parent->display));
883
884 return true;
885 }
886
887 bool _al_show_display_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu)
888 {
889 HWND hwnd = al_get_win_window_handle(display);
890 if (!hwnd) return false;
891
892 ASSERT(menu->extra1);
893
894 /* Note that duplicate callbacks are automatically filtered out, so it's safe
895 to call this many times. */
896 al_win_add_window_callback(display, menu_callback, NULL);
897
898 got_wm_size_event = false;
899 PostMessage(al_get_win_window_handle(display), WM_SHOW_MENU, 0, (LPARAM) menu);
900
901 /* Wait for the WM_SIZE event, otherwise the compensatory
902 * al_resize_display (see menu.c) won't work right. */
903 if (wm_size_cond && global_mutex) {
904 ALLEGRO_DEBUG("Sent WM_SHOW_MENU, waiting for WM_SIZE.\n");
905 al_lock_mutex(global_mutex);
906 while (!got_wm_size_event) {
907 al_wait_cond(wm_size_cond, global_mutex);
908 }
909 al_unlock_mutex(global_mutex);
910 }
911
912 return true;
913 }
914
915 bool _al_hide_display_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu)
916 {
917 HWND hwnd = al_get_win_window_handle(display);
918 if (!hwnd) return false;
919
920 /* Must be run from the main thread to avoid a crash. */
921 got_wm_size_event = false;
922 PostMessage(al_get_win_window_handle(display), WM_HIDE_MENU, 0, (LPARAM) menu);
923
924 /* Wait for the WM_SIZE event, otherwise the compensatory
925 * al_resize_display (see menu.c) won't work right. */
926 if (wm_size_cond && global_mutex) {
927 ALLEGRO_DEBUG("Sent WM_HIDE_MENU, waiting for WM_SIZE.\n");
928 al_lock_mutex(global_mutex);
929 while (!got_wm_size_event) {
930 al_wait_cond(wm_size_cond, global_mutex);
931 }
932 al_unlock_mutex(global_mutex);
933 }
934
935 (void) menu;
936
937 return true;
938 }
939
940 bool _al_show_popup_menu(ALLEGRO_DISPLAY *display, ALLEGRO_MENU *menu)
941 {
942 /* The popup request must come from the main thread. */
943
944 if (!display)
945 return false;
946
947 al_win_add_window_callback(display, menu_callback, NULL);
948 PostMessage(al_get_win_window_handle(display), WM_SHOW_POPUP, 0, (LPARAM) menu);
949
950 return true;
951 }
952
953 int _al_get_menu_display_height(void)
954 {
955 return GetSystemMetrics(SM_CYMENU);
956 }
957
958
959 /* vim: set sts=3 sw=3 et: */