*** empty log message ***
[m17n/m17n-lib.git] / src / m17n-gui.c
1 /* m17n-gui.c -- body of the GUI API.
2    Copyright (C) 2003, 2004
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n library.
7
8    The m17n library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public License
10    as published by the Free Software Foundation; either version 2.1 of
11    the License, or (at your option) any later version.
12
13    The m17n library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with the m17n library; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 /***en
24     @addtogroup m17nGUI
25     @brief GUI support for a window system
26
27     This section defines the m17n GUI API concerning M-text drawing
28     and inputting under a window system.
29
30     All the definitions here are independent of window systems.  An
31     actual library file, however, can depend on a specific window
32     system.  For instance, the library file m17n-X.so is an example of
33     implementation of the m17n GUI API for the X Window System.
34
35     Actually the GUI API is mainly for toolkit libraries or to
36     implement XOM, not for direct use from application programs.
37 */
38
39 /***ja
40     @addtogroup m17nGUI
41     @brief ¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à´ØÏ¢¤Î¿¸À¸ìÂбþ 
42
43     ¤³¤Î¥»¥¯¥·¥ç¥ó¤Ï¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¾å¤Ç¤Î M-text ¤Îɽ¼¨¤ÈÆþÎϤò°·¤¦ 
44     m17n-win API ¤òÄêµÁ¤¹¤ë¡£
45
46     ¤³¤³¤Ç¤Î¤¹¤Ù¤Æ¤ÎÄêµÁ¤Ï¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¤È¤ÏÆÈΩ¤Ç¤¢¤ë¡£¤·¤«¤·¼ÂÁõ
47     ¤Ï¸ÄÊ̤Υ¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¤Ë°Í¸¤¹¤ë¾ì¹ç¤¬¤¢¤ë¡£m17n-X ¥é¥¤¥Ö¥é¥ê
48     ¤Ï X ¥¦¥£¥ó¥É¥¦ÍѤμÂÁõÎã¤Ç¤¢¤ë¡£  */
49
50 /*=*/
51
52 #if !defined (FOR_DOXYGEN) || defined (DOXYGEN_INTERNAL_MODULE)
53 /*** @addtogroup m17nInternal
54      @{ */
55
56 #include "config.h"
57
58 #include <stdio.h>
59 #include <string.h>
60 #include <stdlib.h>
61
62 #include "m17n-gui.h"
63 #include "m17n-misc.h"
64 #include "internal.h"
65 #include "internal-gui.h"
66 #include "font.h"
67 #include "fontset.h"
68 #include "face.h"
69
70 static int win_initialized;
71
72 static void
73 free_frame (void *object)
74 {
75   MFrame *frame = (MFrame *) object;
76
77   M17N_OBJECT_UNREF (frame->face);
78   mwin__close_device ((MFrame *) object);
79   free (frame->font);
80   free (object);
81 }
82
83 \f
84 /* Internal API */
85
86 /*** @} */ 
87 #endif /* !FOR_DOXYGEN || DOXYGEN_INTERNAL_MODULE */
88
89 \f
90 /* External API */
91
92 void
93 m17n_init_win (void)
94 {
95   int mdebug_mask = MDEBUG_INIT;
96
97   if (win_initialized)
98     return;
99   m17n_init ();
100   if (merror_code < 0)
101     return;
102
103   Mfont = msymbol ("font");
104   Mfont_width = msymbol ("font-width");
105   Mfont_ascent = msymbol ("font-ascent");
106   Mfont_descent = msymbol ("font-descent");
107
108   MDEBUG_PUSH_TIME ();
109   MDEBUG_PUSH_TIME ();
110   if (mfont__init () < 0)
111     goto err;
112   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize font module."));
113   if (mwin__init () < 0)
114     goto err;
115   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize win module."));
116   if (mfont__fontset_init () < 0)
117     goto err;
118   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize fontset module."));
119   if (mface__init () < 0)
120     goto err;
121   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize face module."));
122   if (mdraw__init () < 0)
123     goto err;
124   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize draw module."));
125   if (minput__win_init () < 0)
126     goto err;
127   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize input-win module."));
128   mframe_default = NULL;
129   win_initialized = 1;
130
131  err:
132   MDEBUG_POP_TIME ();
133   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize the m17n GUI module."));
134   MDEBUG_POP_TIME ();
135   return;
136 }
137
138 void
139 m17n_fini_win (void)
140 {
141   int mdebug_mask = MDEBUG_FINI;
142
143   if (win_initialized)
144     {
145       MDEBUG_PUSH_TIME ();
146       MDEBUG_PUSH_TIME ();
147       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize input-gui module."));
148       minput__win_fini ();
149       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize draw module."));
150       mdraw__fini ();
151       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize face module."));
152       mface__fini ();
153       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize fontset module."));
154       mfont__fontset_fini ();
155       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize window module."));
156       mwin__fini ();
157       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize font module."));
158       mfont__fini ();
159       mframe_default = NULL;
160       MDEBUG_POP_TIME ();
161       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize the gui modules."));
162       MDEBUG_POP_TIME ();
163       win_initialized = 0;
164     }
165   m17n_fini ();
166 }
167
168 /*** @addtogroup m17nFrame */
169 /***en
170     @brief A @e frame is an object corresponding to the physical device.
171
172     A @e frame is an object of the type #MFrame to hold various
173     information about each physical display/input device.  Almost all
174     m17n GUI functions require a pointer to a frame as an
175     argument.  */
176
177 /***ja
178     @brief ¥Õ¥ì¡¼¥à¤È¤ÏʪÍýŪ¥Ç¥Ð¥¤¥¹¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë
179
180     ¥Õ¥ì¡¼¥à¤È¤Ï #MFrame ·¿¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ê¡¢¸Ä¡¹¤ÎʪÍýŪ¤Êɽ¼¨¡¿
181     ÆþÎϥǥХ¤¥¹¤Î¾ðÊó¤ò³ÊǼ¤¹¤ë¤¿¤á¤ËÍѤ¤¤é¤ì¤ë¡£¤Û¤È¤ó¤É¤¹¤Ù¤Æ¤Î 
182     m17n-win API ¤Ï¡¢°ú¿ô¤È¤·¤Æ¥Õ¥ì¡¼¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÍ׵᤹¤ë¡£  */
183
184 /*** @{ */
185 /*=*/
186
187 /***en
188     @name Variables: Keys of frame property (common).
189  */ 
190 /*** @{ */ 
191 /*=*/
192 MSymbol Mfont;
193 MSymbol Mfont_width;
194 MSymbol Mfont_ascent;
195 MSymbol Mfont_descent;
196
197 /*=*/
198 /*** @} */ 
199 /*=*/
200
201 /***en
202     @brief Create a new frame.
203
204     The mframe () function creates a new frame with parameters listed
205     in $PLIST.
206
207     The recognized keys in $PLIST are window system dependent.
208
209     The following key is always recognized.
210
211     <ul>
212
213     <li> #Mface, the value type must be <tt>(MFace *)</tt>.
214
215     The value is used as the default face of the frame.
216
217     </ul>
218
219     In addition, in the m17n-X library, the following keys are
220     recognized.  They are to specify the root window and the depth of
221     drawables that can be used with the frame.
222
223     <ul>
224
225     <li> #Mdrawable, the value type must be <tt>Drawable</tt>
226
227     A parameter of key #Mdisplay must also be specified.  The
228     created frame can be used for drawables whose root window and
229     depth are the same as those of the specified drawable on the
230     specified display.
231
232     When this parameter is specified, the parameter of key #Mscreen
233     is ignored.
234
235     <li> #Mwidget, the value type must be <tt>Widget</tt>.
236
237     The created frame can be used for drawables whose root window and
238     depth are the same as those of the specified widget.
239
240     If a parameter of key #Mface is not specified, the default face
241     is created from the resources of the widget.
242
243     When this parameter is specified, the parameters of key #Mdisplay,
244     #Mscreen, #Mdrawable, #Mdepth are ignored.
245
246     <li> #Mdepth, the value type must be <tt>unsigned</tt>.
247
248     The created frame can be used for drawables of the specified
249     depth.
250
251     <li> #Mscreen, the value type must be <tt>(Screen *)</tt>.
252
253     The created frame can be used for drawables whose root window is
254     the same as the root window of the specified screen, and depth is
255     the same at the default depth of the screen.
256
257     When this parameter is specified, parameter of key #Mdisplay is
258     ignored.
259
260     <li> #Mdisplay, the value type must be <tt>(Display *)</tt>.
261
262     The created frame can be used for drawables whose root window is
263     the same as the root window for the default screen of the display,
264     and depth is the same as the default depth of the screen.
265
266     <li> #Mcolormap, the value type must be <tt>(Colormap)</tt>.
267
268     The created frame uses the specified colormap.
269
270     </ul>
271
272     @return
273     If the operation was successful, mframe () returns a pointer to a
274     newly created frame.  Otherwise, it returns @c NULL.  */
275
276 /***ja
277     @brief  ¿·¤·¤¤¥Õ¥ì¡¼¥à¤òºî¤ë
278
279     ´Ø¿ô mframe () ¤Ï¿·¤·¤¤¥Õ¥ì¡¼¥à¤òºî¤ë¡£°ìÈ̤ˡ¢°ú¿ô $ARGC ¤È $ARGV 
280     ¤Ï³Æ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î <tt>main ()</tt> ´Ø¿ô¤ËÍ¿¤¨¤é¤ì¤ë¤â¤Î¤ÈƱ¤¸
281     ¤â¤Î¤Ç¤¢¤ë¡£¤Ä¤Þ¤ê¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¿ô¤ò´Þ¤ó¤Ç¤¤¤ëɬÍפ¬¤¢¤ë¡£»ÈÍѤÇ
282     ¤­¤ë°ú¿ô¤Ï¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¤Ë°Í¸¤¹¤ë¡£
283
284     m17n-X ¥é¥¤¥Ö¥é¥ê¤Ë¤ª¤±¤ë¤³¤Î´Ø¿ô¤Ï¡¢<tt>XOpenDisplay (NULL)</tt> 
285     ¤ò»È¤Ã¤Æ¥Ç¥Õ¥©¥ë¥È¤Î¥Ç¥£¥¹¥×¥ì¥¤¤ò³«¤­¡¢¼¡¤¤¤Ç <tt>DefaultScreen
286     (DISPLAY)</tt> ¤ò»È¤Ã¤Æ¥Ç¥Õ¥©¥ë¥È¤Î¥¹¥¯¥ê¡¼¥ó¤òÆÀ¤¿¸å¡¢ºî¤é¤ì¤¿¥Õ¥ì¡¼¥à
287     ¤Ë¤³¤Îξ¼Ô¤ò´ØÏ¢ÉÕ¤±¤ë¡£
288
289     m17n-X ¥é¥¤¥Ö¥é¥ê¤Ï°Ê²¼¤Î°ú¿ô¤ò¼õ¤±ÉÕ¤±¤ë¡£
290
291        @li @c -fn @e font : ¥Õ¥ì¡¼¥à¤Î¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥©¥ó¥È¤ò @e font 
292                             ¤Ë¥»¥Ã¥È¤¹¤ë
293        @li @c -fg @e color : ¥Õ¥ì¡¼¥à¤Î¥Ç¥Õ¥©¥ë¥È¤ÎÁ°·Ê¿§¤ò @e color 
294                              ¤Ë¥»¥Ã¥È¤¹¤ë
295        @li @c -bg @e color : ¥Õ¥ì¡¼¥à¤Î¥Ç¥Õ¥©¥ë¥È¤ÎÇØ·Ê¿§¤ò @e color 
296                              ¤Ë¥»¥Ã¥È¤¹¤ë
297        @li @c -rv : Á°·Ê¿§¤ÈÇØ·Ê¿§¤ò¸ò´¹¤¹¤ë
298
299     @return
300     À®¸ù¤¹¤ì¤Ð mframe() ¤Ï¿·¤·¤¤¥Õ¥ì¡¼¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£¤½¤¦¤Ç¤Ê¤±
301     ¤ì¤Ð @c NULL ¤òÊÖ¤¹¡£  */
302
303 MFrame *
304 mframe (MPlist *plist)
305 {
306   MFrame *frame;
307   MSymbol key;
308
309   M17N_OBJECT (frame, free_frame, MERROR_FRAME);
310   frame->device = mwin__open_device (frame, plist);
311   if (! frame->device)
312     {
313       free (frame);
314       MERROR (MERROR_WIN, NULL);
315     }
316
317   frame->face = mface_from_font (frame->font);
318   frame->face->property[MFACE_FONTSET] = mfontset (NULL);
319   M17N_OBJECT_REF (mface__default->property[MFACE_FONTSET]);
320   if (plist)
321     for (; (key = mplist_key (plist)) != Mnil; plist = mplist_next (plist))
322       if (key == Mface)
323         mface_merge (frame->face, (MFace *) mplist_value (plist));
324
325   frame->rface = mface__realize (frame, NULL, 0, Mnil, Mnil, 0);
326   if (! frame->rface->rfont)
327     MERROR (MERROR_WIN, NULL);
328   frame->space_width = frame->rface->space_width;
329   frame->ascent = frame->rface->ascent;
330   frame->descent = frame->rface->descent;
331
332   if (! mframe_default)
333     mframe_default = frame;
334
335   return frame;
336 }
337
338 /*=*/
339
340 /***en
341     @brief Return property value of frame.
342
343     The mframe_get_prop () function returns a value of property $KEY
344     of frame $FRAME.  The valid keys and the corresponding return
345     values are as follows.
346
347 @verbatim
348
349         key             type of value   meaning of value
350         ---             -------------   ----------------
351         Mface           MFace *         The default face.
352
353         Mfont           MFont *         The default font.
354
355         Mfont_width     int             Width of the default font.
356
357         Mfont_ascent    int             Ascent of the default font.
358
359         Mfont_descent   int             Descent of the default font.
360
361 @endverbatim
362
363     In the m17n-X library, the followings are also accepted.
364
365 @verbatim
366
367         key             type of value   meaning of value
368         ---             -------------   ----------------
369         Mdisplay        Display *       Display associated with the frame.
370
371         Mscreen         int             Screen number of a screen associated
372                                         with the frame.
373
374         Mcolormap       Colormap        Colormap of the frame.
375
376         Mdepth          unsigned                Depth of the frame.
377 @endverbatim
378 */
379
380 /***ja
381     @brief ¥Õ¥ì¡¼¥à¤ÎMWDevice¤òÊÖ¤¹
382
383     ´Ø¿ô mframe_device () ¤Ï¥Õ¥ì¡¼¥à $FRAME ¤Ë³ÊǼ¤µ¤ì¤Æ¤¤¤ë @c
384     MWDevice ¹½Â¤ÂΤؤΥݥ¤¥¿¤òÊÖ¤¹¡£#MWDevice ¤Î·Á¼°¤Ï¥¦¥£¥ó¥É¥¦¥·
385     ¥¹¥Æ¥à¤Ë°Í¸¤¹¤ë¡£  */
386
387 void *
388 mframe_get_prop (MFrame *frame, MSymbol key)
389 {
390   if (key == Mface)
391     return frame->face;
392   if (key == Mfont)
393     return &frame->rface->rfont->font;
394   if (key == Mfont_width)
395     return (void *) (frame->space_width);
396   if (key == Mfont_ascent)
397     return (void *) (frame->ascent);
398   if (key == Mfont_descent)
399     return (void *) (frame->descent);
400   return mwin__device_get_prop (frame->device, key);
401 }
402
403 /*=*/
404
405 /***en
406     @brief The default frame.
407
408     The external variable #mframe_default contains a pointer to the
409     default frame that is created by the first call of mframe ().  */
410
411 /***ja
412     ¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥ì¡¼¥à 
413
414     ³°ÉôÊÑ¿ô #mframe_default ¤Ï¡¢¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥ì¡¼¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò
415     »ý¤Ä¡£¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥ì¡¼¥à¤Ï¡¢ºÇ½é¤Ë mframe () ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿¤È¤­¤Ë
416     ºî¤é¤ì¤ë¡£  */
417
418 MFrame *mframe_default;
419
420 /*** @} */
421
422 /*
423   Local Variables:
424   coding: euc-japan
425   End:
426 */