2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
25 * $XConsortium: objxm_util.c /main/4 1995/11/06 18:47:59 rswiston $
27 * @(#)objxm_util.c 1.23 15 Feb 1994 cde_app_builder/src/libABobjXm
29 * RESTRICTED CONFIDENTIAL INFORMATION:
31 * The information in this document is subject to special
32 * restrictions in a confidential disclosure agreement between
33 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
34 * document outside HP, IBM, Sun, USL, SCO, or Univel without
35 * Sun's specific written approval. This document and all copies
36 * and derivative works thereof must be returned or destroyed at
39 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
45 ***********************************************************************
46 * objxm_util.c - general Motif utility routines
49 ***********************************************************************
54 #include <sys/param.h>
55 #include <X11/Intrinsic.h>
56 #include <X11/StringDefs.h>
57 #include <Xm/MenuShell.h>
59 #include <ab_private/obj.h>
60 #include <ab_private/trav.h>
64 #define LG_BUF_SIZE 1024
69 ObjxmFilenameToPixmapFunc ObjxmP_filename_to_pixmap_cb = NULL;
72 * Post instantiate func, which can be registered via
74 ObjxmPostInstantiateFunc ObjxmP_post_instantiate_cb = NULL;
77 /*************************************************************************
79 ** Private Function Declarations **
81 *************************************************************************/
84 FILE *file, int spaces
87 static int convert_filebase_to_pixmap(
92 static int convert_file_to_pixmap(
97 static int load_pixmap(
105 /*************************************************************************
107 ** Function Definitions **
109 *************************************************************************/
112 * Hook to add routine to convert filenames to callbacks.
113 * NULL callback reinstates default conversion method.
116 objxm_set_cvt_filename_to_pixmap_callback(
117 ObjxmFilenameToPixmapFunc filename_to_pixmap_cb
120 ObjxmP_filename_to_pixmap_cb = filename_to_pixmap_cb;
125 * Hook to add post instantiate routine
126 * NULL callback reinstates default conversion method.
129 objxm_set_post_instantiate_callback(
130 ObjxmPostInstantiateFunc post_instantiate_cb
133 ObjxmP_post_instantiate_cb = post_instantiate_cb;
138 * Determine if a color name exists in the rgb database
145 static Colormap colormap = NULL;
146 static Display *display = NULL;
150 if (display == NULL) /* dtbuilder doesn't handle multiple displays */
152 display = XtDisplay(ObjxmP_toplevel);
153 colormap = XDefaultColormap(display, XDefaultScreen(display));
155 return(XLookupColor(display, colormap, colorname, &exact_color, &color));
160 * Use the Xt Resource converter to convert a colorname to a pixel.
161 * This has the advantage of caching the value and automatically
162 * taking care to Free the pixel when the associated widget is
174 src.size = strlen(colorname) + 1;
175 src.addr = (XPointer)colorname;
176 dst.size = sizeof(Pixel);
177 dst.addr = (XPointer)pixel_p;
179 if (XtConvertAndStore(widget, XtRString, &src, XtRPixel, &dst))
186 objxm_filebase_to_pixmap(
192 int status = 0; /* return code */
194 if (util_strempty(filebase))
195 return ERR_BAD_PARAM2;
199 /* Use Pixmap conversion callback if it exists */
200 if (ObjxmP_filename_to_pixmap_cb != NULL)
202 return(ObjxmP_filename_to_pixmap_cb(widget, filebase, pixmap_p));
204 else /* Use internal conversion routine */
205 return (convert_filebase_to_pixmap(widget, filebase, pixmap_p));
211 objxm_pixmap_conversion_error_msg(
217 char static buf[LG_BUF_SIZE];
219 char file_buf[BUF_SIZE];
222 sprintf(buf, catgets(OBJXM_MESSAGE_CATD, 203, 14,
223 "Warning: %s :\n"), util_strsafe(obj_get_name(obj)));
227 sprintf(buf2, catgets(OBJXM_MESSAGE_CATD, 203, 15,
228 "Graphic File base '%s'\ncould not be converted to a Pixmap because\n"), filebase);
231 if (status == ERR_BAD_PARAM2)
232 strcat(buf, catgets(OBJXM_MESSAGE_CATD, 203, 16,
233 "File base is an empty string."));
236 strcat(buf, catgets(OBJXM_MESSAGE_CATD, 203, 17,
237 "None of the following files could be converted to a Pixmap:\n"));
239 if ((int)(strlen(buf) + (4 * (strlen(filebase) + 4))) > LG_BUF_SIZE)
240 strcat(buf, catgets(OBJXM_MESSAGE_CATD, 203, 18,
241 "filenames too long to display\n"));
244 sprintf(file_buf,"%s.pm\n%s.xpm\n%s.bm\n%s.xbm\n",
245 filebase, filebase, filebase, filebase);
247 strcat(buf, file_buf);
251 util_set_help_data(catgets(OBJXM_MESSAGE_CATD, 203, 20,
252 "The graphic file name specified could not be converted\ninto a pixmap. App Builder searches for a file of the\nform Filename.pm, Filename.xpm, Filename.bm, and\nFilename.xbm.\n\nEither none of those files exists in the directory specified\n(via the Graphic Filename field or if no path was specified,\nthen the current working directory), or a file exists but may\nbe corrupt."), NULL, NULL);
258 * Given the 'base' graphic filename, attempt to load in an appropriate
259 * Pixmap with the following rules:
260 * If display is color (depth > 1), try loading the following filenames
261 * in sequence until 1 succeeds:
262 * base.pm, base.xpm (XPM), base.bm, base.xbm (Bitmap)
264 * If display is monochrome, try loading in the following sequence
266 * base.bm, base.xbm (Bitmap), base.pm, base.xpm (XPM)
270 convert_filebase_to_pixmap(
278 if (ObjxmP_visual_depth > 1) /* Color - Try XPM First */
280 if ((status = load_pixmap(widget, filebase, ".pm", pixmap_p)) != OK)
281 if ((status = load_pixmap(widget, filebase, ".xpm", pixmap_p)) != OK)
282 if ((status = load_pixmap(widget, filebase, ".bm", pixmap_p)) != OK)
283 status = load_pixmap(widget, filebase, ".xbm", pixmap_p);
285 else /* Monochrome - Try Bitmap First */
287 if ((status = load_pixmap(widget, filebase, ".bm", pixmap_p)) != OK)
288 if ((status = load_pixmap(widget, filebase, ".xbm", pixmap_p)) != OK)
289 if ((status = load_pixmap(widget, filebase, ".pm", pixmap_p)) != OK)
290 status = load_pixmap(widget, filebase, ".xpm", pixmap_p);
297 * Attempt to dynamically read a bitmap file and load it into a Pixmap
300 convert_file_to_pixmap(
306 int rc = 0; /* return code */
307 Screen *screen = NULL;
316 screen = XtScreen(widget);
317 fgPixel = WhitePixelOfScreen(screen);
318 bgPixel = BlackPixelOfScreen(screen);
321 * Get proper colors for widget
323 XtVaGetValues(widget,
324 XmNforeground, &fgPixel,
325 XmNbackground, &bgPixel,
329 * In CDE, XmGetPixmap handles .xpm files, as well.
331 *pixmap_p = XmGetPixmap(screen, filename, fgPixel, bgPixel);
332 if ((*pixmap_p == NULL) || (*pixmap_p == XmUNSPECIFIED_PIXMAP))
348 char filename[BUF_SIZE];
351 if ((int)(strlen(filebase) + strlen(ext) + 1) > BUF_SIZE)
354 strcpy(filename, filebase);
355 strcat(filename, ext);
356 return(status = convert_file_to_pixmap(widget, filename, pixmap_p));
362 objxm_fontlist_to_font(
366 XmFontContext context;
368 XmStringCharSet charset;
373 XmFontListInitFontContext(&context, fontlist);
375 if (XmFontListGetNextFont(context, &charset, &font))
377 XmFontListFreeFontContext(context);
381 else if (util_get_verbosity() > 0)
383 util_puts_err("objxm_fontlist_to_font: could not convert fontlist\n");
386 XmFontListFreeFontContext(context);
400 if (util_strempty(string))
401 return((XmString)NULL);
403 src.size = strlen(string) + 1;
404 src.addr = (XPointer)string;
405 dst.size = sizeof(XmString);
406 dst.addr = (XPointer)&xmstr;
408 if (XtConvertAndStore(widget, XtRString, &src, XmRXmString, &dst))
411 return((XmString)NULL);
421 XmStringContext context;
422 XmStringCharSet char_set_tag;
423 XmStringDirection dir;
425 Boolean status = FALSE;
426 char buf[MAXPATHLEN], *seg, *p;
429 if (XmStringInitContext(&context, xmstr))
431 while(XmStringGetNextSegment(context, &seg,
432 &char_set_tag, &dir, &sep))
434 p += (strlen(strcpy(p, seg)));
442 XmStringFreeContext(context);
443 return((STRING)XtNewString(buf));
445 if (util_get_verbosity() > 0)
447 util_puts_err("objxm_xmstr_to_str: could not convert xmstring\n");
454 * Given an Accelerator String (translation Table syntax), return
455 * the string that should be displayed inside the item containing the
459 objxm_accel_to_acceltext(
464 static char acceltext[128];
467 if (util_strempty(accelstr))
470 strcpy(buf, accelstr);
472 p = strtok(buf, "<");
475 strcpy(acceltext, p);
478 /* Replace any "<..>" with "+" */
479 p = strtok(NULL, ">");
482 strcat(acceltext,"+");
483 p = strtok(NULL, "<");
485 strcat(acceltext, p);
494 objxm_linestyle_to_enum(
495 AB_LINE_TYPE linestyle
502 case AB_LINE_SINGLE_LINE:
503 value = XmSINGLE_LINE;
505 case AB_LINE_DOUBLE_LINE:
506 value = XmDOUBLE_LINE;
508 case AB_LINE_SINGLE_DASHED_LINE:
509 value = XmSINGLE_DASHED_LINE;
511 case AB_LINE_DOUBLE_DASHED_LINE:
512 value = XmDOUBLE_DASHED_LINE;
517 case AB_LINE_ETCHED_IN:
518 value = XmSHADOW_ETCHED_IN;
520 case AB_LINE_ETCHED_OUT:
521 value = XmSHADOW_ETCHED_OUT;
523 case AB_LINE_ETCHED_IN_DASH:
524 value = XmSHADOW_ETCHED_IN_DASH;
526 case AB_LINE_ETCHED_OUT_DASH:
527 value = XmSHADOW_ETCHED_OUT_DASH;
530 return (unsigned char)-1;
537 objxm_is_menu_widget(
541 /* If MenuShell OR MenuPane, return TRUE..*/
542 if (XtIsSubclass(widget, xmMenuShellWidgetClass) ||
543 XtIsSubclass(XtParent(widget), xmMenuShellWidgetClass))
551 ***********************************************************************
555 ***********************************************************************
559 objxm_print(ABObj obj)
561 return objxm_print_indented(obj, 0, util_get_verbosity());
565 objxm_print_indented(ABObj obj, int spaces, int verbosity)
567 obj_print_indented(obj, spaces, verbosity);
570 ArgList args= objxm_obj_get_ui_args(obj);
571 int num_args= objxm_get_num_args(args);
572 objxm_dump_arglist_indented(obj, args, num_args, spaces);
573 util_dprintf(0, "\n");
580 objxm_tree_print(ABObj obj)
582 objxm_tree_print_indented(obj, 0, util_get_verbosity());
583 util_dprintf(0,"\n");
589 objxm_tree_print_indented(ABObj obj, int spaces, int verbosity)
594 objxm_print_indented(obj, spaces, verbosity);
596 for (trav_open(&trav, obj, AB_TRAV_CHILDREN);
597 (child= trav_next(&trav)) != NULL; )
599 objxm_tree_print_indented(child, spaces+4, verbosity);
608 indent (FILE *file, int spaces)
611 for (i= 0; i < spaces; ++i)