1 /* Console functions for mswindows.
2 Copyright (C) 1996, 2000 Ben Wing.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: Not in FSF. */
25 Ben Wing: January 1996, for 19.14.
26 Rewritten for mswindows by Jonathan Harris, November 1997 for 21.0
33 #include "console-msw.h"
37 DEFINE_CONSOLE_TYPE (mswindows);
38 DEFINE_CONSOLE_TYPE (msprinter);
40 Lisp_Object Qabortretryignore;
41 Lisp_Object Qapplmodal;
42 Lisp_Object Qdefault_desktop_only;
43 Lisp_Object Qdefbutton1;
44 Lisp_Object Qdefbutton2;
45 Lisp_Object Qdefbutton3;
46 Lisp_Object Qdefbutton4;
47 /* Lisp_Object Qhelp; */
48 Lisp_Object Qiconasterisk;
49 Lisp_Object Qiconexclamation;
50 Lisp_Object Qiconhand;
51 Lisp_Object Qiconinformation;
52 Lisp_Object Qiconquestion;
53 Lisp_Object Qiconstop;
54 /* Lisp_Object Qok; */
55 Lisp_Object Qokcancel;
56 Lisp_Object Qretrycancel;
57 /* Lisp_Object Qright; */
58 Lisp_Object Qrtlreading;
59 Lisp_Object Qservice_notification;
60 Lisp_Object Qsetforeground;
61 Lisp_Object Qsystemmodal;
62 Lisp_Object Qtaskmodal;
65 Lisp_Object Qyesnocancel;
67 /* Lisp_Object Qabort; */
68 /* Lisp_Object Qcancel; */
69 /* Lisp_Object Qignore; */
70 /* Lisp_Object Qno; */
71 /* Lisp_Object Qok; */
72 /* Lisp_Object Qretry; */
73 /* Lisp_Object Qyes; */
76 /************************************************************************/
77 /* mswindows console methods */
78 /************************************************************************/
81 mswindows_initially_selected_for_input (struct console *con)
86 static HWND mswindows_console_hwnd = 0;
88 #define KLUDGE_BUFSIZE 1024 /* buffer size for console window titles */
90 /* Direct from the horse's mouth: Microsoft KB article Q124103 */
94 HWND hwndFound; /* this is what is returned to the caller */
95 char pszNewWindowTitle[KLUDGE_BUFSIZE]; /* contains fabricated WindowTitle */
96 char pszOldWindowTitle[KLUDGE_BUFSIZE]; /* contains original WindowTitle */
98 /* fetch current window title */
100 GetConsoleTitle(pszOldWindowTitle, KLUDGE_BUFSIZE);
102 /* format a "unique" NewWindowTitle */
104 wsprintf(pszNewWindowTitle,"%d/%d",
106 GetCurrentProcessId());
108 /* change current window title */
110 SetConsoleTitle(pszNewWindowTitle);
112 /* ensure window title has been updated */
116 /* look for NewWindowTitle */
118 hwndFound=FindWindow(NULL, pszNewWindowTitle);
120 /* restore original window title */
122 SetConsoleTitle(pszOldWindowTitle);
128 mswindows_get_console_hwnd (void)
130 if (!mswindows_console_hwnd)
131 mswindows_console_hwnd = GetConsoleHwnd ();
132 return mswindows_console_hwnd;
136 mswindows_ensure_console_allocated (void)
138 HWND fgwin = GetForegroundWindow ();
139 /* stupid mswin api won't let you create the console window
140 hidden! creating it changes the focus! fuck me! */
143 SetForegroundWindow (fgwin);
150 mswindows_canonicalize_console_connection (Lisp_Object connection,
153 /* Do not allow more than one mswindows device, by explicitly
154 requiring that CONNECTION is nil, the only allowed connection in
156 if (!NILP (connection))
158 if (ERRB_EQ (errb, ERROR_ME))
160 ("Invalid (non-nil) connection for mswindows device/console",
170 mswindows_canonicalize_device_connection (Lisp_Object connection,
173 return mswindows_canonicalize_console_connection (connection, errb);
177 mswindows_hide_console (void)
179 ShowWindow (mswindows_get_console_hwnd (), SW_HIDE);
183 mswindows_show_console (void)
185 HWND hwnd = mswindows_get_console_hwnd ();
186 ShowWindow (hwnd, SW_SHOWNA);
188 /* I tried to raise the window to the top without activating
189 it, but this fails. Apparently Windows just doesn't like
190 having the active window not be on top. So instead, we
191 at least put it just below our own window, where part of it
192 will likely be seen. */
193 SetWindowPos (hwnd, GetForegroundWindow (), 0, 0, 0, 0,
194 SWP_NOSIZE | SWP_NOMOVE | SWP_NOSENDCHANGING |
198 static int mswindows_console_buffered = 0;
199 HANDLE mswindows_console_buffer;
202 mswindows_ensure_console_buffered (void)
204 if (!mswindows_console_buffered)
210 mswindows_ensure_console_allocated ();
211 mswindows_console_buffer =
212 CreateConsoleScreenBuffer (GENERIC_WRITE, 0, NULL,
213 CONSOLE_TEXTMODE_BUFFER, NULL);
214 SetConsoleScreenBufferSize (mswindows_console_buffer, new_size);
215 SetConsoleActiveScreenBuffer (mswindows_console_buffer);
216 mswindows_console_buffered = 1;
220 int mswindows_message_outputted;
223 mswindows_output_console_string (CONST Extbyte *str, Extcount len)
227 mswindows_message_outputted = 1;
228 mswindows_ensure_console_buffered ();
229 mswindows_show_console ();
230 return WriteConsole (mswindows_console_buffer, str, len, &num_written, NULL);
233 /* Determine if running on Windows 9x and not NT */
235 mswindows_windows9x_p (void)
237 return GetVersion () & 0x80000000;
244 * Random helper functions for debugging.
245 * Intended for use in the MSVC "Watch" window which doesn't like
246 * the aborts that the error_check_foo() functions can make.
248 struct lrecord_header *DHEADER (Lisp_Object obj);
249 struct lrecord_header *
250 DHEADER (Lisp_Object obj)
252 return LRECORDP (obj) ? XRECORD_LHEADER (obj) : NULL;
255 void *DOPAQUE_DATA (Lisp_Object obj);
257 DOPAQUE_DATA (Lisp_Object obj)
259 return OPAQUEP (obj) ? OPAQUE_DATA (XOPAQUE (obj)) : NULL;
262 Lisp_Event *DEVENT (Lisp_Object obj);
264 DEVENT (Lisp_Object obj)
266 return EVENTP (obj) ? XEVENT (obj) : NULL;
269 Lisp_Cons *DCONS (Lisp_Object obj);
271 DCONS (Lisp_Object obj)
273 return CONSP (obj) ? XCONS (obj) : NULL;
276 Lisp_Cons *DCONSCDR (Lisp_Object obj);
278 DCONSCDR (Lisp_Object obj)
280 return (CONSP (obj) && CONSP (XCDR (obj))) ? XCONS (XCDR (obj)) : 0;
283 Bufbyte *DSTRING (Lisp_Object obj);
285 DSTRING (Lisp_Object obj)
287 return STRINGP (obj) ? XSTRING_DATA (obj) : NULL;
290 Lisp_Vector *DVECTOR (Lisp_Object obj);
292 DVECTOR (Lisp_Object obj)
294 return VECTORP (obj) ? XVECTOR (obj) : NULL;
297 Lisp_Symbol *DSYMBOL (Lisp_Object obj);
299 DSYMBOL (Lisp_Object obj)
301 return SYMBOLP (obj) ? XSYMBOL (obj) : NULL;
304 Bufbyte *DSYMNAME (Lisp_Object obj);
306 DSYMNAME (Lisp_Object obj)
308 return SYMBOLP (obj) ? string_data (XSYMBOL (obj)->name) : NULL;
311 #endif /* DEBUG_XEMACS */
313 DEFUN ("mswindows-message-box", Fmswindows_message_box, 1, 3, 0, /*
314 Pop up an MS Windows message box.
315 MESSAGE is the string to display. Optional argument FLAG controls
316 what appears in the box and how it behaves; it is a symbol or list of
317 symbols, described below. Second optional argument TITLE controls the
318 title bar; if omitted, a standard title bar will be used, probably
324 -- To specify the buttons in the message box:
327 The message box contains three push buttons: Abort, Retry, and Ignore.
329 The message box contains one push button: OK. This is the default.
331 The message box contains two push buttons: OK and Cancel.
333 The message box contains two push buttons: Retry and Cancel.
335 The message box contains two push buttons: Yes and No.
337 The message box contains three push buttons: Yes, No, and Cancel.
340 -- To display an icon in the message box:
342 iconexclamation, iconwarning
343 An exclamation-point icon appears in the message box.
344 iconinformation, iconasterisk
345 An icon consisting of a lowercase letter i in a circle appears in
348 A question-mark icon appears in the message box.
349 iconstop, iconerror, iconhand
350 A stop-sign icon appears in the message box.
353 -- To indicate the default button:
356 The first button is the default button. This is the default.
358 The second button is the default button.
360 The third button is the default button.
362 The fourth button is the default button.
365 -- To indicate the modality of the dialog box:
368 The user must respond to the message box before continuing work in
369 the window identified by the hWnd parameter. However, the user can
370 move to the windows of other applications and work in those windows.
371 Depending on the hierarchy of windows in the application, the user
372 may be able to move to other windows within the application. All
373 child windows of the parent of the message box are automatically
374 disabled, but popup windows are not. This is the default.
376 Same as applmodal except that the message box has the WS_EX_TOPMOST
377 style. Use system-modal message boxes to notify the user of serious,
378 potentially damaging errors that require immediate attention (for
379 example, running out of memory). This flag has no effect on the
380 user's ability to interact with windows other than those associated
383 Same as applmodal except that all the top-level windows belonging to
384 the current task are disabled if the hWnd parameter is NULL. Use
385 this flag when the calling application or library does not have a
386 window handle available but still needs to prevent input to other
387 windows in the current application without suspending other
391 In addition, you can specify the following flags:
394 The desktop currently receiving input must be a default desktop;
395 otherwise, the function fails. A default desktop is one an
396 application runs on after the user has logged on.
398 Adds a Help button to the message box. Choosing the Help button or
399 pressing F1 generates a Help event.
401 The text is right-justified.
403 Displays message and caption text using right-to-left reading order
404 on Hebrew and Arabic systems.
406 The message box becomes the foreground window. Internally, Windows
407 calls the SetForegroundWindow function for the message box.
409 The message box is created with the WS_EX_TOPMOST window style.
411 Windows NT only: The caller is a service notifying the user of an
412 event. The function displays a message box on the current active
413 desktop, even if there is no user logged on to the computer. If
414 this flag is set, the hWnd parameter must be NULL. This is so the
415 message box can appear on a desktop other than the desktop
416 corresponding to the hWnd.
420 The return value is one of the following menu-item values returned by
424 Abort button was selected.
426 Cancel button was selected.
428 Ignore button was selected.
430 No button was selected.
432 OK button was selected.
434 Retry button was selected.
436 Yes button was selected.
438 If a message box has a Cancel button, the function returns the
439 `cancel' value if either the ESC key is pressed or the Cancel button
440 is selected. If the message box has no Cancel button, pressing ESC has
442 (message_, flags, title))
446 Extbyte *titleout = 0;
454 CHECK_SYMBOL (flags);
455 flags = list1 (flags);
458 CHECK_STRING (message_);
459 TO_EXTERNAL_FORMAT (LISP_STRING, message_,
460 C_STRING_ALLOCA, msgout,
465 CHECK_STRING (title);
466 TO_EXTERNAL_FORMAT (LISP_STRING, title,
467 C_STRING_ALLOCA, titleout,
471 EXTERNAL_LIST_LOOP (tail, flags)
473 Lisp_Object st = XCAR (tail);
477 #define FROB(sym, val) else if (EQ (st, sym)) sty |= val
478 FROB (Qabortretryignore, MB_ABORTRETRYIGNORE);
479 FROB (Qapplmodal, MB_APPLMODAL);
480 FROB (Qdefault_desktop_only, MB_DEFAULT_DESKTOP_ONLY);
481 FROB (Qdefbutton1, MB_DEFBUTTON1);
482 FROB (Qdefbutton2, MB_DEFBUTTON2);
483 FROB (Qdefbutton3, MB_DEFBUTTON3);
484 FROB (Qdefbutton4, MB_DEFBUTTON4);
485 FROB (Qhelp, MB_HELP);
486 FROB (Qiconasterisk, MB_ICONASTERISK);
487 FROB (Qiconexclamation, MB_ICONEXCLAMATION);
488 FROB (Qiconhand, MB_ICONHAND);
489 FROB (Qiconinformation, MB_ICONINFORMATION);
490 FROB (Qiconquestion, MB_ICONQUESTION);
491 FROB (Qiconstop, MB_ICONSTOP);
493 FROB (Qokcancel, MB_OKCANCEL);
494 FROB (Qretrycancel, MB_RETRYCANCEL);
495 FROB (Qright, MB_RIGHT);
496 FROB (Qrtlreading, MB_RTLREADING);
497 FROB (Qservice_notification, MB_SERVICE_NOTIFICATION);
498 FROB (Qsetforeground, MB_SETFOREGROUND);
499 FROB (Qsystemmodal, MB_SYSTEMMODAL);
500 FROB (Qtaskmodal, MB_TASKMODAL);
501 FROB (Qtopmost, MB_TOPMOST);
502 FROB (Qyesno, MB_YESNO);
503 FROB (Qyesnocancel, MB_YESNOCANCEL);
507 signal_simple_error ("Unrecognized flag", st);
511 int retval = MessageBox (NULL, msgout, titleout, sty);
514 error ("Out of memory when calling `mswindows-message-box'");
516 #define FROB(sym, val) if (retval == val) return sym
517 FROB (Qabort, IDABORT);
518 FROB (Qcancel, IDCANCEL);
519 FROB (Qignore, IDIGNORE);
522 FROB (Qretry, IDRETRY);
526 signal_simple_error ("Unknown return value from MessageBox()",
534 mswindows_output_last_error (char *frob)
537 int errval = GetLastError();
539 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
540 | FORMAT_MESSAGE_FROM_SYSTEM,
542 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
546 stderr_out ("last error during %s is %d: %s\n",
547 frob, errval, (char*)lpMsgBuf);
551 /************************************************************************/
553 /************************************************************************/
556 syms_of_console_mswindows (void)
558 defsymbol (&Qabortretryignore, "abortretryignore");
559 defsymbol (&Qapplmodal, "applmodal");
560 defsymbol (&Qdefault_desktop_only, "default-desktop-only");
561 defsymbol (&Qdefbutton1, "defbutton1");
562 defsymbol (&Qdefbutton2, "defbutton2");
563 defsymbol (&Qdefbutton3, "defbutton3");
564 defsymbol (&Qdefbutton4, "defbutton4");
565 /* defsymbol (&Qhelp, "help"); */
566 defsymbol (&Qiconasterisk, "iconasterisk");
567 defsymbol (&Qiconexclamation, "iconexclamation");
568 defsymbol (&Qiconhand, "iconhand");
569 defsymbol (&Qiconinformation, "iconinformation");
570 defsymbol (&Qiconquestion, "iconquestion");
571 defsymbol (&Qiconstop, "iconstop");
572 /* defsymbol (&Qok, "ok"); */
573 defsymbol (&Qokcancel, "okcancel");
574 defsymbol (&Qretrycancel, "retrycancel");
575 /* defsymbol (&Qright, "right"); */
576 defsymbol (&Qrtlreading, "rtlreading");
577 defsymbol (&Qservice_notification, "service-notification");
578 defsymbol (&Qsetforeground, "setforeground");
579 defsymbol (&Qsystemmodal, "systemmodal");
580 defsymbol (&Qtaskmodal, "taskmodal");
581 defsymbol (&Qtopmost, "topmost");
582 defsymbol (&Qyesno, "yesno");
583 defsymbol (&Qyesnocancel, "yesnocancel");
585 /* defsymbol (&Qabort, "abort"); */
586 /* defsymbol (&Qcancel, "cancel"); */
587 /* defsymbol (&Qignore, "ignore"); */
588 /* defsymbol (&Qno, "no"); */
589 /* defsymbol (&Qok, "ok"); */
590 /* defsymbol (&Qretry, "retry"); */
591 /* defsymbol (&Qyes, "yes"); */
593 DEFSUBR (Fmswindows_message_box);
597 console_type_create_mswindows (void)
599 INITIALIZE_CONSOLE_TYPE (mswindows, "mswindows", "console-mswindows-p");
601 /* console methods */
602 /* CONSOLE_HAS_METHOD (mswindows, init_console); */
603 /* CONSOLE_HAS_METHOD (mswindows, mark_console); */
604 CONSOLE_HAS_METHOD (mswindows, initially_selected_for_input);
605 /* CONSOLE_HAS_METHOD (mswindows, delete_console); */
606 CONSOLE_HAS_METHOD (mswindows, canonicalize_console_connection);
607 CONSOLE_HAS_METHOD (mswindows, canonicalize_device_connection);
608 /* CONSOLE_HAS_METHOD (mswindows, semi_canonicalize_console_connection); */
609 /* CONSOLE_HAS_METHOD (mswindows, semi_canonicalize_device_connection); */
611 INITIALIZE_CONSOLE_TYPE (msprinter, "msprinter", "console-msprinter-p");
615 reinit_console_type_create_mswindows (void)
617 REINITIALIZE_CONSOLE_TYPE (mswindows);
618 REINITIALIZE_CONSOLE_TYPE (msprinter);
622 vars_of_console_mswindows (void)
624 Fprovide (Qmswindows);