2c3c011bc0b4cbc65f298304243b3f713ae7b5de
[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 (object);
80 }
81
82 \f
83 /* Internal API */
84
85 /*** @} */ 
86 #endif /* !FOR_DOXYGEN || DOXYGEN_INTERNAL_MODULE */
87
88 \f
89 /* External API */
90
91 void
92 m17n_init_win (void)
93 {
94   int mdebug_mask = MDEBUG_INIT;
95
96   if (win_initialized)
97     return;
98   m17n_init ();
99   if (merror_code < 0)
100     return;
101
102   Mfont = msymbol ("font");
103   Mfont_width = msymbol ("font-width");
104   Mfont_ascent = msymbol ("font-ascent");
105   Mfont_descent = msymbol ("font-descent");
106
107   MDEBUG_PUSH_TIME ();
108   MDEBUG_PUSH_TIME ();
109   if (mfont__init () < 0)
110     goto err;
111   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize font module."));
112   if (mwin__init () < 0)
113     goto err;
114   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize win module."));
115   if (mfont__fontset_init () < 0)
116     goto err;
117   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize fontset module."));
118   if (mface__init () < 0)
119     goto err;
120   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize face module."));
121   if (mdraw__init () < 0)
122     goto err;
123   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize draw module."));
124   if (minput__win_init () < 0)
125     goto err;
126   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize input-win module."));
127   mframe_default = NULL;
128   win_initialized = 1;
129
130  err:
131   MDEBUG_POP_TIME ();
132   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize the m17n GUI module."));
133   MDEBUG_POP_TIME ();
134   return;
135 }
136
137 void
138 m17n_fini_win (void)
139 {
140   int mdebug_mask = MDEBUG_FINI;
141
142   if (win_initialized)
143     {
144       MDEBUG_PUSH_TIME ();
145       MDEBUG_PUSH_TIME ();
146       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize input-gui module."));
147       minput__win_fini ();
148       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize draw module."));
149       mdraw__fini ();
150       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize face module."));
151       mface__fini ();
152       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize fontset module."));
153       mfont__fontset_fini ();
154       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize window module."));
155       mwin__fini ();
156       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize font module."));
157       mfont__fini ();
158       mframe_default = NULL;
159       MDEBUG_POP_TIME ();
160       MDEBUG_PRINT_TIME ("INIT", (stderr, " to finalize the gui modules."));
161       MDEBUG_POP_TIME ();
162       win_initialized = 0;
163     }
164   m17n_fini ();
165 }
166
167 /*** @addtogroup m17nFrame */
168 /***en
169     @brief A @e frame is an object corresponding to the physical device.
170
171     A @e frame is an object of the type #MFrame to hold various
172     information about each physical display/input device.  Almost all
173     m17n GUI functions require a pointer to a frame as an
174     argument.  */
175
176 /***ja
177     @brief ¥Õ¥ì¡¼¥à¤È¤ÏʪÍýŪ¥Ç¥Ð¥¤¥¹¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë
178
179     ¥Õ¥ì¡¼¥à¤È¤Ï #MFrame ·¿¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ê¡¢¸Ä¡¹¤ÎʪÍýŪ¤Êɽ¼¨¡¿
180     ÆþÎϥǥХ¤¥¹¤Î¾ðÊó¤ò³ÊǼ¤¹¤ë¤¿¤á¤ËÍѤ¤¤é¤ì¤ë¡£¤Û¤È¤ó¤É¤¹¤Ù¤Æ¤Î 
181     m17n-win API ¤Ï¡¢°ú¿ô¤È¤·¤Æ¥Õ¥ì¡¼¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÍ׵᤹¤ë¡£  */
182
183 /*** @{ */
184 /*=*/
185
186 /***en
187     @name Variables: Keys of frame property (common).
188  */ 
189 /*** @{ */ 
190 /*=*/
191 MSymbol Mfont;
192 MSymbol Mfont_width;
193 MSymbol Mfont_ascent;
194 MSymbol Mfont_descent;
195
196 /*=*/
197 /*** @} */ 
198 /*=*/
199
200 /***en
201     @brief Create a new frame.
202
203     The mframe () function creates a new frame with parameters listed
204     in $PLIST.
205
206     The recognized keys in $PLIST are window system dependent.
207
208     The following key is always recognized.
209
210     <ul>
211
212     <li> #Mface, the value type must be <tt>(MFace *)</tt>.
213
214     The value is used as the default face of the frame.
215
216     </ul>
217
218     In addition, in the m17n-X library, the following keys are
219     recognized.  They are to specify the root window and the depth of
220     drawables that can be used with the frame.
221
222     <ul>
223
224     <li> #Mdrawable, the value type must be <tt>Drawable</tt>
225
226     A parameter of key #Mdisplay must also be specified.  The
227     created frame can be used for drawables whose root window and
228     depth are the same as those of the specified drawable on the
229     specified display.
230
231     When this parameter is specified, the parameter of key #Mscreen
232     is ignored.
233
234     <li> #Mwidget, the value type must be <tt>Widget</tt>.
235
236     The created frame can be used for drawables whose root window and
237     depth are the same as those of the specified widget.
238
239     If a parameter of key #Mface is not specified, the default face
240     is created from the resources of the widget.
241
242     When this parameter is specified, the parameters of key #Mdisplay,
243     #Mscreen, #Mdrawable, #Mdepth are ignored.
244
245     <li> #Mdepth, the value type must be <tt>unsigned</tt>.
246
247     The created frame can be used for drawables of the specified
248     depth.
249
250     <li> #Mscreen, the value type must be <tt>(Screen *)</tt>.
251
252     The created frame can be used for drawables whose root window is
253     the same as the root window of the specified screen, and depth is
254     the same at the default depth of the screen.
255
256     When this parameter is specified, parameter of key #Mdisplay is
257     ignored.
258
259     <li> #Mdisplay, the value type must be <tt>(Display *)</tt>.
260
261     The created frame can be used for drawables whose root window is
262     the same as the root window for the default screen of the display,
263     and depth is the same as the default depth of the screen.
264
265     <li> #Mcolormap, the value type must be <tt>(Colormap)</tt>.
266
267     The created frame uses the specified colormap.
268
269     </ul>
270
271     @return
272     If the operation was successful, mframe () returns a pointer to a
273     newly created frame.  Otherwise, it returns @c NULL.  */
274
275 /***ja
276     @brief  ¿·¤·¤¤¥Õ¥ì¡¼¥à¤òºî¤ë
277
278     ´Ø¿ô mframe () ¤Ï¿·¤·¤¤¥Õ¥ì¡¼¥à¤òºî¤ë¡£°ìÈ̤ˡ¢°ú¿ô $ARGC ¤È $ARGV 
279     ¤Ï³Æ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î <tt>main ()</tt> ´Ø¿ô¤ËÍ¿¤¨¤é¤ì¤ë¤â¤Î¤ÈƱ¤¸
280     ¤â¤Î¤Ç¤¢¤ë¡£¤Ä¤Þ¤ê¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¿ô¤ò´Þ¤ó¤Ç¤¤¤ëɬÍפ¬¤¢¤ë¡£»ÈÍѤÇ
281     ¤­¤ë°ú¿ô¤Ï¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¤Ë°Í¸¤¹¤ë¡£
282
283     m17n-X ¥é¥¤¥Ö¥é¥ê¤Ë¤ª¤±¤ë¤³¤Î´Ø¿ô¤Ï¡¢<tt>XOpenDisplay (NULL)</tt> 
284     ¤ò»È¤Ã¤Æ¥Ç¥Õ¥©¥ë¥È¤Î¥Ç¥£¥¹¥×¥ì¥¤¤ò³«¤­¡¢¼¡¤¤¤Ç <tt>DefaultScreen
285     (DISPLAY)</tt> ¤ò»È¤Ã¤Æ¥Ç¥Õ¥©¥ë¥È¤Î¥¹¥¯¥ê¡¼¥ó¤òÆÀ¤¿¸å¡¢ºî¤é¤ì¤¿¥Õ¥ì¡¼¥à
286     ¤Ë¤³¤Îξ¼Ô¤ò´ØÏ¢ÉÕ¤±¤ë¡£
287
288     m17n-X ¥é¥¤¥Ö¥é¥ê¤Ï°Ê²¼¤Î°ú¿ô¤ò¼õ¤±ÉÕ¤±¤ë¡£
289
290        @li @c -fn @e font : ¥Õ¥ì¡¼¥à¤Î¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥©¥ó¥È¤ò @e font 
291                             ¤Ë¥»¥Ã¥È¤¹¤ë
292        @li @c -fg @e color : ¥Õ¥ì¡¼¥à¤Î¥Ç¥Õ¥©¥ë¥È¤ÎÁ°·Ê¿§¤ò @e color 
293                              ¤Ë¥»¥Ã¥È¤¹¤ë
294        @li @c -bg @e color : ¥Õ¥ì¡¼¥à¤Î¥Ç¥Õ¥©¥ë¥È¤ÎÇØ·Ê¿§¤ò @e color 
295                              ¤Ë¥»¥Ã¥È¤¹¤ë
296        @li @c -rv : Á°·Ê¿§¤ÈÇØ·Ê¿§¤ò¸ò´¹¤¹¤ë
297
298     @return
299     À®¸ù¤¹¤ì¤Ð mframe() ¤Ï¿·¤·¤¤¥Õ¥ì¡¼¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£¤½¤¦¤Ç¤Ê¤±
300     ¤ì¤Ð @c NULL ¤òÊÖ¤¹¡£  */
301
302 MFrame *
303 mframe (MPlist *plist)
304 {
305   MFrame *frame;
306   MSymbol key;
307
308   M17N_OBJECT (frame, free_frame, MERROR_FRAME);
309   frame->device = mwin__open_device (frame, plist);
310   if (! frame->device)
311     {
312       free (frame);
313       MERROR (MERROR_WIN, NULL);
314     }
315
316   frame->face = mface_copy (mface__default);
317   if (plist)
318     for (; (key = mplist_key (plist)) != Mnil; plist = mplist_next (plist))
319       if (key == Mface)
320         mface_merge (frame->face, (MFace *) mplist_value (plist));
321
322   frame->rface = mface__realize (frame, NULL, 0, Mnil, Mnil, 0);
323   if (! frame->rface->rfont)
324     MERROR (MERROR_WIN, NULL);
325   frame->space_width = frame->rface->space_width;
326   frame->ascent = frame->rface->ascent;
327   frame->descent = frame->rface->descent;
328
329   if (! mframe_default)
330     mframe_default = frame;
331
332   return frame;
333 }
334
335 /*=*/
336
337 /***en
338     @brief Return property value of frame.
339
340     The mframe_get_prop () function returns a value of property $KEY
341     of frame $FRAME.  The valid keys and the corresponding return
342     values are as follows.
343
344 @verbatim
345
346         key             type of value   meaning of value
347         ---             -------------   ----------------
348         Mface           MFace *         The default face.
349
350         Mfont           MFont *         The default font.
351
352         Mfont_width     int             Width of the default font.
353
354         Mfont_ascent    int             Ascent of the default font.
355
356         Mfont_descent   int             Descent of the default font.
357
358 @endverbatim
359
360     In the m17n-X library, the followings are also accepted.
361
362 @verbatim
363
364         key             type of value   meaning of value
365         ---             -------------   ----------------
366         Mdisplay        Display *       Display associated with the frame.
367
368         Mscreen         int             Screen number of a screen associated
369                                         with the frame.
370
371         Mcolormap       Colormap        Colormap of the frame.
372
373         Mdepth          unsigned                Depth of the frame.
374 @endverbatim
375 */
376
377 /***ja
378     @brief ¥Õ¥ì¡¼¥à¤ÎMWDevice¤òÊÖ¤¹
379
380     ´Ø¿ô mframe_device () ¤Ï¥Õ¥ì¡¼¥à $FRAME ¤Ë³ÊǼ¤µ¤ì¤Æ¤¤¤ë @c
381     MWDevice ¹½Â¤ÂΤؤΥݥ¤¥¿¤òÊÖ¤¹¡£#MWDevice ¤Î·Á¼°¤Ï¥¦¥£¥ó¥É¥¦¥·
382     ¥¹¥Æ¥à¤Ë°Í¸¤¹¤ë¡£  */
383
384 void *
385 mframe_get_prop (MFrame *frame, MSymbol key)
386 {
387   if (key == Mface)
388     return frame->face;
389   if (key == Mfont)
390     return &frame->rface->rfont->font;
391   if (key == Mfont_width)
392     return (void *) (frame->space_width);
393   if (key == Mfont_ascent)
394     return (void *) (frame->ascent);
395   if (key == Mfont_descent)
396     return (void *) (frame->descent);
397   return mwin__device_get_prop (frame->device, key);
398 }
399
400 /*=*/
401
402 /***en
403     @brief The default frame.
404
405     The external variable #mframe_default contains a pointer to the
406     default frame that is created by the first call of mframe ().  */
407
408 /***ja
409     ¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥ì¡¼¥à 
410
411     ³°ÉôÊÑ¿ô #mframe_default ¤Ï¡¢¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥ì¡¼¥à¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò
412     »ý¤Ä¡£¥Ç¥Õ¥©¥ë¥È¤Î¥Õ¥ì¡¼¥à¤Ï¡¢ºÇ½é¤Ë mframe () ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿¤È¤­¤Ë
413     ºî¤é¤ì¤ë¡£  */
414
415 MFrame *mframe_default;
416
417 /*** @} */
418
419 /*
420   Local Variables:
421   coding: euc-japan
422   End:
423 */