3 * $XConsortium: objxm_util.c /main/4 1995/11/06 18:47:59 rswiston $
5 * @(#)objxm_util.c 1.23 15 Feb 1994 cde_app_builder/src/libABobjXm
7 * RESTRICTED CONFIDENTIAL INFORMATION:
9 * The information in this document is subject to special
10 * restrictions in a confidential disclosure agreement between
11 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
12 * document outside HP, IBM, Sun, USL, SCO, or Univel without
13 * Sun's specific written approval. This document and all copies
14 * and derivative works thereof must be returned or destroyed at
17 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
23 ***********************************************************************
24 * objxm_util.c - general Motif utility routines
27 ***********************************************************************
32 #include <sys/param.h>
33 #include <X11/Intrinsic.h>
34 #include <X11/StringDefs.h>
35 #include <Xm/MenuShell.h>
37 #include <ab_private/obj.h>
38 #include <ab_private/trav.h>
42 #define LG_BUF_SIZE 1024
47 ObjxmFilenameToPixmapFunc ObjxmP_filename_to_pixmap_cb = NULL;
50 * Post instantiate func, which can be registered via
52 ObjxmPostInstantiateFunc ObjxmP_post_instantiate_cb = NULL;
55 /*************************************************************************
57 ** Private Function Declarations **
59 *************************************************************************/
62 FILE *file, int spaces
65 static int convert_filebase_to_pixmap(
70 static int convert_file_to_pixmap(
75 static int load_pixmap(
83 /*************************************************************************
85 ** Function Definitions **
87 *************************************************************************/
90 * Hook to add routine to convert filenames to callbacks.
91 * NULL callback reinstates default conversion method.
94 objxm_set_cvt_filename_to_pixmap_callback(
95 ObjxmFilenameToPixmapFunc filename_to_pixmap_cb
98 ObjxmP_filename_to_pixmap_cb = filename_to_pixmap_cb;
103 * Hook to add post instantiate routine
104 * NULL callback reinstates default conversion method.
107 objxm_set_post_instantiate_callback(
108 ObjxmPostInstantiateFunc post_instantiate_cb
111 ObjxmP_post_instantiate_cb = post_instantiate_cb;
116 * Determine if a color name exists in the rgb database
123 static Colormap colormap = NULL;
124 static Display *display = NULL;
128 if (display == NULL) /* dtbuilder doesn't handle multiple displays */
130 display = XtDisplay(ObjxmP_toplevel);
131 colormap = XDefaultColormap(display, XDefaultScreen(display));
133 return(XLookupColor(display, colormap, colorname, &exact_color, &color));
138 * Use the Xt Resource converter to convert a colorname to a pixel.
139 * This has the advantage of caching the value and automatically
140 * taking care to Free the pixel when the associated widget is
152 src.size = strlen(colorname) + 1;
153 src.addr = (XPointer)colorname;
154 dst.size = sizeof(Pixel);
155 dst.addr = (XPointer)pixel_p;
157 if (XtConvertAndStore(widget, XtRString, &src, XtRPixel, &dst))
164 objxm_filebase_to_pixmap(
170 int status = 0; /* return code */
172 if (util_strempty(filebase))
173 return ERR_BAD_PARAM2;
177 /* Use Pixmap conversion callback if it exists */
178 if (ObjxmP_filename_to_pixmap_cb != NULL)
180 return(ObjxmP_filename_to_pixmap_cb(widget, filebase, pixmap_p));
182 else /* Use internal conversion routine */
183 return (convert_filebase_to_pixmap(widget, filebase, pixmap_p));
189 objxm_pixmap_conversion_error_msg(
195 char static buf[LG_BUF_SIZE];
197 char file_buf[BUF_SIZE];
200 sprintf(buf, catgets(OBJXM_MESSAGE_CATD, 203, 14,
201 "Warning: %s :\n"), util_strsafe(obj_get_name(obj)));
205 sprintf(buf2, catgets(OBJXM_MESSAGE_CATD, 203, 15,
206 "Graphic File base '%s'\ncould not be converted to a Pixmap because\n"), filebase);
209 if (status == ERR_BAD_PARAM2)
210 strcat(buf, catgets(OBJXM_MESSAGE_CATD, 203, 16,
211 "File base is an empty string."));
214 strcat(buf, catgets(OBJXM_MESSAGE_CATD, 203, 17,
215 "None of the following files could be converted to a Pixmap:\n"));
217 if ((int)(strlen(buf) + (4 * (strlen(filebase) + 4))) > LG_BUF_SIZE)
218 strcat(buf, catgets(OBJXM_MESSAGE_CATD, 203, 18,
219 "filenames too long to display\n"));
222 sprintf(file_buf,"%s.pm\n%s.xpm\n%s.bm\n%s.xbm\n",
223 filebase, filebase, filebase, filebase);
225 strcat(buf, file_buf);
229 util_set_help_data(catgets(OBJXM_MESSAGE_CATD, 203, 20,
230 "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);
236 * Given the 'base' graphic filename, attempt to load in an appropriate
237 * Pixmap with the following rules:
238 * If display is color (depth > 1), try loading the following filenames
239 * in sequence until 1 succeeds:
240 * base.pm, base.xpm (XPM), base.bm, base.xbm (Bitmap)
242 * If display is monochrome, try loading in the following sequence
244 * base.bm, base.xbm (Bitmap), base.pm, base.xpm (XPM)
248 convert_filebase_to_pixmap(
256 if (ObjxmP_visual_depth > 1) /* Color - Try XPM First */
258 if ((status = load_pixmap(widget, filebase, ".pm", pixmap_p)) != OK)
259 if ((status = load_pixmap(widget, filebase, ".xpm", pixmap_p)) != OK)
260 if ((status = load_pixmap(widget, filebase, ".bm", pixmap_p)) != OK)
261 status = load_pixmap(widget, filebase, ".xbm", pixmap_p);
263 else /* Monochrome - Try Bitmap First */
265 if ((status = load_pixmap(widget, filebase, ".bm", pixmap_p)) != OK)
266 if ((status = load_pixmap(widget, filebase, ".xbm", pixmap_p)) != OK)
267 if ((status = load_pixmap(widget, filebase, ".pm", pixmap_p)) != OK)
268 status = load_pixmap(widget, filebase, ".xpm", pixmap_p);
275 * Attempt to dynamically read a bitmap file and load it into a Pixmap
278 convert_file_to_pixmap(
284 int rc = 0; /* return code */
285 Screen *screen = NULL;
294 screen = XtScreen(widget);
295 fgPixel = WhitePixelOfScreen(screen);
296 bgPixel = BlackPixelOfScreen(screen);
299 * Get proper colors for widget
301 XtVaGetValues(widget,
302 XmNforeground, &fgPixel,
303 XmNbackground, &bgPixel,
307 * In CDE, XmGetPixmap handles .xpm files, as well.
309 *pixmap_p = XmGetPixmap(screen, filename, fgPixel, bgPixel);
310 if ((*pixmap_p == NULL) || (*pixmap_p == XmUNSPECIFIED_PIXMAP))
326 char filename[BUF_SIZE];
329 if ((int)(strlen(filebase) + strlen(ext) + 1) > BUF_SIZE)
332 strcpy(filename, filebase);
333 strcat(filename, ext);
334 return(status = convert_file_to_pixmap(widget, filename, pixmap_p));
340 objxm_fontlist_to_font(
344 XmFontContext context;
346 XmStringCharSet charset;
351 XmFontListInitFontContext(&context, fontlist);
353 if (XmFontListGetNextFont(context, &charset, &font))
355 XmFontListFreeFontContext(context);
359 else if (util_get_verbosity() > 0)
361 util_puts_err("objxm_fontlist_to_font: could not convert fontlist\n");
364 XmFontListFreeFontContext(context);
378 if (util_strempty(string))
379 return((XmString)NULL);
381 src.size = strlen(string) + 1;
382 src.addr = (XPointer)string;
383 dst.size = sizeof(XmString);
384 dst.addr = (XPointer)&xmstr;
386 if (XtConvertAndStore(widget, XtRString, &src, XmRXmString, &dst))
389 return((XmString)NULL);
399 XmStringContext context;
400 XmStringCharSet char_set_tag;
401 XmStringDirection dir;
403 Boolean status = FALSE;
404 char buf[MAXPATHLEN], *seg, *p;
407 if (XmStringInitContext(&context, xmstr))
409 while(XmStringGetNextSegment(context, &seg,
410 &char_set_tag, &dir, &sep))
412 p += (strlen(strcpy(p, seg)));
420 XmStringFreeContext(context);
421 return((STRING)XtNewString(buf));
423 if (util_get_verbosity() > 0)
425 util_puts_err("objxm_xmstr_to_str: could not convert xmstring\n");
432 * Given an Accelerator String (translation Table syntax), return
433 * the string that should be displayed inside the item containing the
437 objxm_accel_to_acceltext(
442 static char acceltext[128];
445 if (util_strempty(accelstr))
448 strcpy(buf, accelstr);
450 p = strtok(buf, "<");
453 strcpy(acceltext, p);
456 /* Replace any "<..>" with "+" */
457 p = strtok(NULL, ">");
460 strcat(acceltext,"+");
461 p = strtok(NULL, "<");
463 strcat(acceltext, p);
472 objxm_linestyle_to_enum(
473 AB_LINE_TYPE linestyle
480 case AB_LINE_SINGLE_LINE:
481 value = XmSINGLE_LINE;
483 case AB_LINE_DOUBLE_LINE:
484 value = XmDOUBLE_LINE;
486 case AB_LINE_SINGLE_DASHED_LINE:
487 value = XmSINGLE_DASHED_LINE;
489 case AB_LINE_DOUBLE_DASHED_LINE:
490 value = XmDOUBLE_DASHED_LINE;
495 case AB_LINE_ETCHED_IN:
496 value = XmSHADOW_ETCHED_IN;
498 case AB_LINE_ETCHED_OUT:
499 value = XmSHADOW_ETCHED_OUT;
501 case AB_LINE_ETCHED_IN_DASH:
502 value = XmSHADOW_ETCHED_IN_DASH;
504 case AB_LINE_ETCHED_OUT_DASH:
505 value = XmSHADOW_ETCHED_OUT_DASH;
508 return (unsigned char)-1;
515 objxm_is_menu_widget(
519 /* If MenuShell OR MenuPane, return TRUE..*/
520 if (XtIsSubclass(widget, xmMenuShellWidgetClass) ||
521 XtIsSubclass(XtParent(widget), xmMenuShellWidgetClass))
529 ***********************************************************************
533 ***********************************************************************
537 objxm_print(ABObj obj)
539 return objxm_print_indented(obj, 0, util_get_verbosity());
543 objxm_print_indented(ABObj obj, int spaces, int verbosity)
545 obj_print_indented(obj, spaces, verbosity);
548 ArgList args= objxm_obj_get_ui_args(obj);
549 int num_args= objxm_get_num_args(args);
550 objxm_dump_arglist_indented(obj, args, num_args, spaces);
551 util_dprintf(0, "\n");
558 objxm_tree_print(ABObj obj)
560 objxm_tree_print_indented(obj, 0, util_get_verbosity());
561 util_dprintf(0,"\n");
567 objxm_tree_print_indented(ABObj obj, int spaces, int verbosity)
572 objxm_print_indented(obj, spaces, verbosity);
574 for (trav_open(&trav, obj, AB_TRAV_CHILDREN);
575 (child= trav_next(&trav)) != NULL; )
577 objxm_tree_print_indented(child, spaces+4, verbosity);
586 indent (FILE *file, int spaces)
589 for (i= 0; i < spaces; ++i)