1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
21 #include <X11/Intrinsic.h>
22 #include <X11/IntrinsicP.h>
23 #include <X11/StringDefs.h>
24 #include <X11/Shell.h>
25 #include <X11/Xaw/Form.h>
33 #include "gui_stuff.h"
35 void put_line_8(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[]);
36 void put_line_16(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[]);
37 void put_line_24(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[]);
38 void put_line_32(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[]);
40 /**************************************************************************
42 **************************************************************************/
43 void xaw_set_relative_position(Widget ref
, Widget w
, int px
, int py
)
46 Dimension width
, height
;
48 XtVaGetValues(ref
, XtNwidth
, &width
, XtNheight
, &height
, NULL
);
49 XtTranslateCoords(ref
, (Position
) px
*width
/100, (Position
) py
*height
/100, &x
, &y
);
50 XtVaSetValues(w
, XtNx
, x
, XtNy
, y
, NULL
);
54 /**************************************************************************
56 **************************************************************************/
57 void xaw_horiz_center(Widget w
)
59 Dimension width
, width2
;
61 XtVaGetValues(XtParent(w
), XtNwidth
, &width
, NULL
);
62 XtVaGetValues(w
, XtNwidth
, &width2
, NULL
);
63 XtVaSetValues(w
, XtNhorizDistance
, (width
-width2
)/2, NULL
);
66 /**************************************************************************
68 **************************************************************************/
69 void xaw_set_bitmap(Widget w
, Pixmap pm
)
71 XtVaSetValues(w
, XtNbitmap
, (XtArgVal
)pm
, NULL
);
76 /**************************************************************************
78 **************************************************************************/
79 void xaw_set_label(Widget w
, const char *text
)
83 XtVaGetValues(w
, XtNlabel
, &str
, NULL
);
84 if (strcmp(str
, text
) != 0) {
85 XtVaSetValues(w
, XtNlabel
, (XtArgVal
)text
, NULL
);
91 /**************************************************************************
93 **************************************************************************/
94 void xaw_expose_now(Widget w
)
96 Dimension width
, height
;
100 xeev
.display
= XtDisplay (w
);
101 xeev
.window
= XtWindow(w
);
104 XtVaGetValues(w
, XtNwidth
, &width
, XtNheight
, &height
, NULL
);
105 (XtClass(w
))->core_class
.expose(w
, (XEvent
*)&xeev
, NULL
);
108 /**************************************************************************
110 **************************************************************************/
111 void x_simulate_button_click(Widget w
)
115 ev
.display
= XtDisplay(w
);
116 ev
.window
= XtWindow(w
);
122 XtDispatchEvent((XEvent
*)&ev
);
123 ev
.type
=ButtonRelease
;
124 XtDispatchEvent((XEvent
*)&ev
);
127 /**************************************************************************
129 **************************************************************************/
130 Pixmap
x_scale_pixmap(Pixmap src
, int src_w
, int src_h
, int dst_w
, int dst_h
,
134 XImage
*xi_src
, *xi_dst
;
135 int xoffset_table
[4096];
136 int x
, xoffset
, xadd
, xremsum
, xremadd
;
137 int y
, yoffset
, yadd
, yremsum
, yremadd
;
141 xi_src
=XGetImage(display
, src
, 0, 0, src_w
, src_h
, AllPlanes
, ZPixmap
);
142 xi_dst
=XCreateImage(display
, DefaultVisual(display
, screen_number
),
143 xi_src
->depth
, ZPixmap
,
146 xi_src
->bitmap_pad
, 0);
148 xi_dst
->data
=fc_calloc(xi_dst
->bytes_per_line
* xi_dst
->height
, 1);
150 /* for each pixel in dst, calculate pixel offset in src */
156 for (x
= 0; x
< dst_w
; x
++) {
157 xoffset_table
[x
]=xoffset
;
171 for (y
= 0; y
< dst_h
; y
++) {
172 psrc_data
=xi_src
->data
+ (yoffset
* xi_src
->bytes_per_line
);
173 pdst_data
=xi_dst
->data
+ (y
* xi_dst
->bytes_per_line
);
175 switch(xi_src
->bits_per_pixel
) {
177 put_line_8(psrc_data
, pdst_data
, dst_w
, xoffset_table
);
180 put_line_16(psrc_data
, pdst_data
, dst_w
, xoffset_table
);
183 put_line_24(psrc_data
, pdst_data
, dst_w
, xoffset_table
);
186 put_line_32(psrc_data
, pdst_data
, dst_w
, xoffset_table
);
189 memcpy(pdst_data
, psrc_data
, (src_w
<dst_w
) ? src_w
: dst_w
);
201 dst
=XCreatePixmap(display
, root
, dst_w
, dst_h
, display_depth
);
202 XPutImage(display
, dst
, civ_gc
, xi_dst
, 0, 0, 0, 0, dst_w
, dst_h
);
204 xi_src
->f
.destroy_image(xi_src
);
205 xi_dst
->f
.destroy_image(xi_dst
);
210 void put_line_8(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[])
213 for (x
= 0; x
< dst_w
; x
++)
214 *pdst
++=*(psrc
+xoffset_table
[x
]+0);
217 void put_line_16(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[])
220 for (x
= 0; x
< dst_w
; x
++) {
221 *pdst
++=*(psrc
+2*xoffset_table
[x
]+0);
222 *pdst
++=*(psrc
+2*xoffset_table
[x
]+1);
226 void put_line_24(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[])
229 for (x
= 0; x
< dst_w
; x
++) {
230 *pdst
++=*(psrc
+3*xoffset_table
[x
]+0);
231 *pdst
++=*(psrc
+3*xoffset_table
[x
]+1);
232 *pdst
++=*(psrc
+3*xoffset_table
[x
]+2);
236 void put_line_32(char *psrc
, char *pdst
, int dst_w
, int xoffset_table
[])
239 for (x
= 0; x
< dst_w
; x
++) {
240 *pdst
++=*(psrc
+4*xoffset_table
[x
]+0);
241 *pdst
++=*(psrc
+4*xoffset_table
[x
]+1);
242 *pdst
++=*(psrc
+4*xoffset_table
[x
]+2);
243 *pdst
++=*(psrc
+4*xoffset_table
[x
]+3);
248 /**************************************************************************
249 Returns 1 if string has gettext markings _inside_ string
250 (marking must be strictly at ends of string).
251 **************************************************************************/
252 static int has_intl_marking(const char *str
)
256 if (!(str
[0] == '_' && str
[1] == '(' && str
[2] == '\"'))
261 return (len
>= 5 && str
[len
-2] == '\"' && str
[len
-1] == ')');
264 /**************************************************************************
265 For a string which _has_ intl marking (caller should check beforehand)
266 returns gettext result of string inside marking.
267 Below buf is an internal buffer which is never free'd.
268 **************************************************************************/
269 static const char *convert_intl_marking(const char *str
)
271 static struct astring buf
= ASTRING_INIT
;
272 int len
= strlen(str
);
274 fc_assert_ret_val(len
>= 5, str
);
275 astr_set(&buf
, "%s", str
+ 3);
276 *((char *) &astr_str(&buf
)[len
- 5]) = '\0';
278 return Q_(astr_str(&buf
));
281 /**************************************************************************
282 Check if the current widget label has i18n/gettext markings
283 _inside_ the string (for full string only). If so, then
284 strip off those markings, pass to gettext, and re-set label
285 to gettext-returned string. Note this is intended for resources
286 which are specified in data/Freeciv; if the label is set via
287 code, it should be gettext'd via code before/when set.
288 Note all this is necessary even if not using gettext, to
289 remove the gettext markings!
290 Returns the same widget (eg, to nest calls of this and following funcs).
291 **************************************************************************/
292 Widget
xaw_intl_label(Widget w
)
297 XtVaGetValues(w
, XtNlabel
, &str
, XtNresizable
, &rszbl
, NULL
);
299 if (has_intl_marking(str
)) {
301 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)True
, NULL
);
302 XtVaSetValues(w
, XtNlabel
, (XtArgVal
)convert_intl_marking(str
), NULL
);
304 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)False
, NULL
);
310 /**************************************************************************
311 As above, but also making sure to preserve the widget width.
312 Should probably use this version rather than above
313 when data/Freeciv specifies the width.
314 **************************************************************************/
315 Widget
xaw_intl_label_width(Widget w
)
321 XtVaGetValues(w
, XtNlabel
, &str
, XtNresizable
, &rszbl
,
322 XtNwidth
, &width
, NULL
);
324 if (has_intl_marking(str
)) {
326 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)True
, NULL
);
327 XtVaSetValues(w
, XtNlabel
, (XtArgVal
)convert_intl_marking(str
),
328 XtNwidth
, width
, NULL
);
330 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)False
, NULL
);
336 /**************************************************************************
337 As above, for widget title.
338 **************************************************************************/
339 Widget
xaw_intl_title(Widget w
)
343 XtVaGetValues(w
, XtNtitle
, &str
, NULL
);
345 if (has_intl_marking(str
))
346 XtVaSetValues(w
, XtNtitle
, (XtArgVal
)convert_intl_marking(str
), NULL
);
350 /**************************************************************************
351 As above, for widget icon name.
352 **************************************************************************/
353 Widget
xaw_intl_icon_name(Widget w
)
357 XtVaGetValues(w
, XtNiconName
, &str
, NULL
);
359 if (has_intl_marking(str
))
360 XtVaSetValues(w
, XtNiconName
, (XtArgVal
)convert_intl_marking(str
), NULL
);
364 /**************************************************************************
365 As above, for widget String contents.
366 **************************************************************************/
367 Widget
xaw_intl_string(Widget w
)
372 XtVaGetValues(w
, XtNstring
, &str
, XtNresizable
, &rszbl
, NULL
);
374 if (has_intl_marking(str
)) {
376 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)True
, NULL
);
377 XtVaSetValues(w
, XtNstring
, (XtArgVal
)convert_intl_marking(str
), NULL
);
379 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)False
, NULL
);
385 /**************************************************************************
386 As label_width above, for widget String contents.
387 **************************************************************************/
388 Widget
xaw_intl_string_width(Widget w
)
394 XtVaGetValues(w
, XtNstring
, &str
, XtNresizable
, &rszbl
,
395 XtNwidth
, &width
, NULL
);
397 if (has_intl_marking(str
)) {
399 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)True
, NULL
);
400 XtVaSetValues(w
, XtNstring
, (XtArgVal
)convert_intl_marking(str
),
401 XtNwidth
, width
, NULL
);
403 XtVaSetValues(w
, XtNresizable
, (XtArgVal
)False
, NULL
);