XEmacs 21.2.5
[chise/xemacs-chise.git.1] / src / device-msw.c
1 /* device functions for mswindows.
2    Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
3    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Synched up with: Not in FSF. */
23
24 /* Authorship:
25
26    Original authors: Jamie Zawinski and the FSF
27    Rewritten by Ben Wing and Chuck Thompson.
28    Rewritten for mswindows by Jonathan Harris, November 1997 for 21.0.
29 */
30
31
32 #include <config.h>
33 #include "lisp.h"
34
35 #include "console-msw.h"
36 #include "console-stream.h"
37 #include "events.h"
38 #include "faces.h"
39 #include "frame.h"
40 #include "sysdep.h"
41
42 /* win32 DDE management library globals */
43 #ifdef HAVE_DRAGNDROP
44 DWORD mswindows_dde_mlid;
45 HSZ mswindows_dde_service;
46 HSZ mswindows_dde_topic_system;
47 HSZ mswindows_dde_item_open;
48 #endif
49
50 /* Control conversion of upper case file names to lower case.
51    nil means no, t means yes. */
52 Lisp_Object Vmswindows_downcase_file_names;
53
54 /* Control whether stat() attempts to determine file type and link count
55    exactly, at the expense of slower operation.  Since true hard links
56    are supported on NTFS volumes, this is only relevant on NT.  */
57 Lisp_Object Vmswindows_get_true_file_attributes;
58
59 Lisp_Object Qinit_pre_mswindows_win, Qinit_post_mswindows_win;
60
61
62 /************************************************************************/
63 /*                               helpers                                */
64 /************************************************************************/
65
66 static Lisp_Object
67 build_syscolor_string (int index)
68 {
69   DWORD clr;
70   char buf[16];
71
72   if (index < 0)
73     return Qnil;
74
75   clr = GetSysColor (index);
76   sprintf (buf, "#%02X%02X%02X",
77            GetRValue (clr),
78            GetGValue (clr),
79            GetBValue (clr));
80   return build_string (buf);
81 }
82
83 static Lisp_Object
84 build_syscolor_cons (int index1, int index2)
85 {
86   Lisp_Object color1, color2;
87   struct gcpro gcpro1;
88   GCPRO1 (color1);
89   color1 = build_syscolor_string (index1);
90   color2 = build_syscolor_string (index2);
91   RETURN_UNGCPRO (Fcons (color1, color2));
92 }
93
94 static Lisp_Object
95 build_sysmetrics_cons (int index1, int index2)
96 {
97   return Fcons (index1 < 0 ? Qnil : make_int (GetSystemMetrics (index1)),
98                 index2 < 0 ? Qnil : make_int (GetSystemMetrics (index2)));
99 }
100
101
102 \f
103 /************************************************************************/
104 /*                               methods                                */
105 /************************************************************************/
106
107 static void
108 mswindows_init_device (struct device *d, Lisp_Object props)
109 {
110   WNDCLASSEX wc;
111   HDC hdc;
112
113   DEVICE_CLASS (d) = Qcolor;
114   DEVICE_INFD (d) = DEVICE_OUTFD (d) = -1;
115   init_baud_rate (d);
116   init_one_device (d);
117
118   d->device_data = xnew_and_zero (struct mswindows_device);
119   hdc = CreateCompatibleDC (NULL);
120   assert (hdc!=NULL);
121   DEVICE_MSWINDOWS_LOGPIXELSX(d) =  GetDeviceCaps(hdc, LOGPIXELSX);
122   DEVICE_MSWINDOWS_LOGPIXELSY(d) =  GetDeviceCaps(hdc, LOGPIXELSY);
123   DEVICE_MSWINDOWS_PLANES(d) = GetDeviceCaps(hdc, PLANES);
124   /* #### SIZEPALETTE only valid if RC_PALETTE bit set in RASTERCAPS,
125      what should we return for a non-palette-based device? */
126   DEVICE_MSWINDOWS_CELLS(d) = GetDeviceCaps(hdc, SIZEPALETTE);
127   DEVICE_MSWINDOWS_HORZRES(d) = GetDeviceCaps(hdc, HORZRES);
128   DEVICE_MSWINDOWS_VERTRES(d) = GetDeviceCaps(hdc, VERTRES);
129   DEVICE_MSWINDOWS_HORZSIZE(d) = GetDeviceCaps(hdc, HORZSIZE);
130   DEVICE_MSWINDOWS_VERTSIZE(d) = GetDeviceCaps(hdc, VERTSIZE);
131   DEVICE_MSWINDOWS_BITSPIXEL(d) = GetDeviceCaps(hdc, BITSPIXEL);
132   DeleteDC (hdc);
133
134   mswindows_enumerate_fonts (d);
135
136   /* Register the main window class */
137   wc.cbSize = sizeof (WNDCLASSEX);
138   wc.style = CS_OWNDC;  /* One DC per window */
139   wc.lpfnWndProc = (WNDPROC) mswindows_wnd_proc;
140   wc.cbClsExtra = 0;
141   wc.cbWndExtra = MSWINDOWS_WINDOW_EXTRA_BYTES;
142   wc.hInstance = NULL;  /* ? */
143   wc.hIcon = LoadIcon (GetModuleHandle(NULL), XEMACS_CLASS);
144   wc.hCursor = LoadCursor (NULL, IDC_ARROW);
145   /* Background brush is only used during sizing, when XEmacs cannot
146      take over */
147   wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
148   wc.lpszMenuName = NULL;
149
150   wc.lpszClassName = XEMACS_CLASS;
151   wc.hIconSm = LoadImage (GetModuleHandle (NULL), XEMACS_CLASS,
152                           IMAGE_ICON, 16, 16, 0);
153   RegisterClassEx (&wc);
154 #ifdef HAVE_TOOLBARS
155   InitCommonControls ();
156 #endif
157 }
158
159 static void
160 mswindows_finish_init_device (struct device *d, Lisp_Object props)
161 {
162   /* Initialize DDE management library and our related globals. We execute a
163    * dde Open("file") by simulating a drop, so this depends on dnd support. */
164 #ifdef HAVE_DRAGNDROP
165   mswindows_dde_mlid = 0;
166   DdeInitialize (&mswindows_dde_mlid, (PFNCALLBACK)mswindows_dde_callback,
167                  APPCMD_FILTERINITS|CBF_FAIL_SELFCONNECTIONS|CBF_FAIL_ADVISES|
168                  CBF_FAIL_POKES|CBF_FAIL_REQUESTS|CBF_SKIP_ALLNOTIFICATIONS, 0);
169   
170   mswindows_dde_service = DdeCreateStringHandle (mswindows_dde_mlid, XEMACS_CLASS, 0);
171   mswindows_dde_topic_system = DdeCreateStringHandle (mswindows_dde_mlid, SZDDESYS_TOPIC, 0);
172   mswindows_dde_item_open = DdeCreateStringHandle (mswindows_dde_mlid,
173                                                    TEXT(MSWINDOWS_DDE_ITEM_OPEN), 0);
174   DdeNameService (mswindows_dde_mlid, mswindows_dde_service, 0L, DNS_REGISTER);
175 #endif
176 }
177
178 static void
179 mswindows_delete_device (struct device *d)
180 {
181   struct mswindows_font_enum *fontlist, *next;
182
183   fontlist = DEVICE_MSWINDOWS_FONTLIST (d);
184   while (fontlist)
185     {
186       next = fontlist->next;
187       free (fontlist);
188       fontlist = next;
189     }
190
191 #ifdef HAVE_DRAGNDROP
192   DdeNameService (mswindows_dde_mlid, 0L, 0L, DNS_REGISTER);
193   DdeUninitialize (mswindows_dde_mlid);
194 #endif
195
196   free (d->device_data);
197 }
198
199 static Lisp_Object
200 mswindows_device_system_metrics (struct device *d,
201                                  enum device_metrics m)
202 {
203   switch (m)
204     {
205     case DM_size_device:
206       return Fcons (make_int (DEVICE_MSWINDOWS_HORZRES(d)),
207                     make_int (DEVICE_MSWINDOWS_VERTRES(d)));
208       break;
209     case DM_size_device_mm:
210       return Fcons (make_int (DEVICE_MSWINDOWS_HORZSIZE(d)),
211                     make_int (DEVICE_MSWINDOWS_VERTSIZE(d)));
212       break;
213     case DM_num_bit_planes:
214       /* this is what X means by bitplanes therefore we ought to be
215          consistent. num planes is always 1 under mswindows and
216          therefore useless */
217       return make_int (DEVICE_MSWINDOWS_BITSPIXEL(d));
218       break;
219     case DM_num_color_cells:
220       return make_int (DEVICE_MSWINDOWS_CELLS(d));
221       break;
222
223       /*** Colors ***/
224 #define FROB(met, index1, index2)                       \
225     case DM_##met:                                      \
226       return build_syscolor_cons (index1, index2);
227       
228       FROB (color_default, COLOR_WINDOW, COLOR_WINDOWTEXT);
229       FROB (color_select, COLOR_HIGHLIGHT, COLOR_HIGHLIGHTTEXT);
230       FROB (color_balloon, COLOR_INFOBK, COLOR_INFOTEXT);
231       FROB (color_3d_face, COLOR_3DFACE, COLOR_BTNTEXT);
232       FROB (color_3d_light, COLOR_3DLIGHT, COLOR_3DHILIGHT);
233       FROB (color_3d_dark, COLOR_3DSHADOW, COLOR_3DDKSHADOW);
234       FROB (color_menu, COLOR_MENU, COLOR_MENUTEXT);
235       FROB (color_menu_highlight, COLOR_HIGHLIGHT, COLOR_HIGHLIGHTTEXT);
236       FROB (color_menu_button, COLOR_MENU, COLOR_MENUTEXT);
237       FROB (color_menu_disabled, COLOR_MENU, COLOR_GRAYTEXT);
238       FROB (color_toolbar, COLOR_BTNFACE, COLOR_BTNTEXT);
239       FROB (color_scrollbar, COLOR_SCROLLBAR, COLOR_CAPTIONTEXT);
240       FROB (color_desktop, -1, COLOR_DESKTOP);
241       FROB (color_workspace, -1, COLOR_APPWORKSPACE);
242 #undef FROB
243
244       /*** Sizes ***/
245 #define FROB(met, index1, index2)                       \
246     case DM_##met:                                      \
247       return build_sysmetrics_cons (index1, index2);
248
249       FROB (size_cursor, SM_CXCURSOR, SM_CYCURSOR);
250       FROB (size_scrollbar, SM_CXVSCROLL, SM_CYHSCROLL);
251       FROB (size_menu, -1, SM_CYMENU);
252       FROB (size_icon, SM_CXICON, SM_CYICON);
253       FROB (size_icon_small, SM_CXSMICON, SM_CYSMICON);
254 #undef FROB
255
256     case DM_size_workspace:
257       {
258         RECT rc;
259         SystemParametersInfo (SPI_GETWORKAREA, 0, &rc, 0);
260         return Fcons (make_int (rc.right - rc.left),
261                       make_int (rc.bottom - rc.top));
262       }
263       /*
264         case DM_size_toolbar:
265         case DM_size_toolbar_button:
266         case DM_size_toolbar_border:
267       */
268
269       /*** Features ***/
270 #define FROB(met, index)                        \
271     case DM_##met:                              \
272       return make_int (GetSystemMetrics (index));
273
274       FROB (mouse_buttons, SM_CMOUSEBUTTONS);
275       FROB (swap_buttons, SM_SWAPBUTTON);
276       FROB (show_sounds, SM_SHOWSOUNDS);
277       FROB (slow_device, SM_SLOWMACHINE);
278       FROB (security, SM_SECURE);
279 #undef FROB
280
281     }
282
283   /* Do not know such property */
284   return Qunbound;
285 }
286
287 static unsigned int
288 mswindows_device_implementation_flags (void)
289 {
290   return XDEVIMPF_PIXEL_GEOMETRY;
291 }
292
293 \f
294 /************************************************************************/
295 /*                            initialization                            */
296 /************************************************************************/
297
298 void
299 syms_of_device_mswindows (void)
300 {
301   defsymbol (&Qinit_pre_mswindows_win, "init-pre-mswindows-win");
302   defsymbol (&Qinit_post_mswindows_win, "init-post-mswindows-win");
303
304   DEFVAR_LISP ("mswindows-downcase-file-names", &Vmswindows_downcase_file_names /*
305 Non-nil means convert all-upper case file names to lower case.
306 This applies when performing completions and file name expansion.*/ );
307   Vmswindows_downcase_file_names = Qnil;
308
309   DEFVAR_LISP ("mswindows-get-true-file-attributes", &Vmswindows_get_true_file_attributes /*
310     "Non-nil means determine accurate link count in file-attributes.
311 This option slows down file-attributes noticeably, so is disabled by
312 default.  Note that it is only useful for files on NTFS volumes,
313 where hard links are supported.
314 */ );
315   Vmswindows_get_true_file_attributes = Qnil;
316 }
317
318 void
319 console_type_create_device_mswindows (void)
320 {
321   CONSOLE_HAS_METHOD (mswindows, init_device);
322   CONSOLE_HAS_METHOD (mswindows, finish_init_device);
323 /*  CONSOLE_HAS_METHOD (mswindows, mark_device); */
324   CONSOLE_HAS_METHOD (mswindows, delete_device);
325   CONSOLE_HAS_METHOD (mswindows, device_system_metrics);
326   CONSOLE_HAS_METHOD (mswindows, device_implementation_flags);
327 }
328
329 void
330 vars_of_device_mswindows (void)
331 {
332 }