home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvcmisc.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  13KB  |  341 lines

  1. /* Copyright (C) 1993, 1994, 1995, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvcmisc.c */
  19. /* Miscellaneous GSview routines common to Windows and PM */
  20.  
  21. #ifdef _Windows
  22. #include "gvwin.h"
  23. #else
  24. #include "gvpm.h"
  25. #endif
  26.  
  27. /* display error message and post quit message */
  28. void 
  29. error_message(char *str)
  30. {
  31.     message_box(str, MB_ICONHAND);
  32.     post_close();
  33. }
  34.  
  35. void
  36. info_init(HWND hwnd)
  37. {
  38.     char buf[MAXSTR];
  39.     int n;
  40.     if (psfile.name[0] != '\0') {
  41.     SetDlgItemText(hwnd, INFO_FILE, psfile.name);
  42.     if (doc) {
  43.         if (psfile.ctrld)
  44.         load_string(IDS_NOTDSC, buf, sizeof(buf));
  45.         else  {
  46.         if (psfile.ispdf) {
  47.             load_string(IDS_PDF, buf, sizeof(buf));
  48.         }
  49.         else if (doc->epsf) {
  50.             switch (psfile.preview) {
  51.             case IDS_EPSI:
  52.               load_string(IDS_EPSI, buf, sizeof(buf));
  53.               break;
  54.             case IDS_EPST:
  55.               load_string(IDS_EPST, buf, sizeof(buf));
  56.               break;
  57.             case IDS_EPSW:
  58.               load_string(IDS_EPSW, buf, sizeof(buf));
  59.               break;
  60.             default:
  61.               load_string(IDS_EPSF, buf, sizeof(buf));
  62.             }
  63.         }
  64.         else
  65.             load_string(IDS_DSC, buf, sizeof(buf));
  66.         }
  67.         SetDlgItemText(hwnd, INFO_TYPE, buf);
  68.         SetDlgItemText(hwnd, INFO_TITLE, doc->title ? doc->title : "");
  69.         SetDlgItemText(hwnd, INFO_DATE, doc->date ? doc->date : "");
  70.         sprintf(buf, "%d %d %d %d", doc->boundingbox[LLX], doc->boundingbox[LLY], 
  71.         doc->boundingbox[URX], doc->boundingbox[URY]);
  72.         SetDlgItemText(hwnd, INFO_BBOX, buf);
  73.         switch(doc->orientation) {
  74.         case LANDSCAPE:
  75.             load_string(IDS_LANDSCAPE, buf, sizeof(buf));
  76.             break;
  77.         case PORTRAIT:
  78.             load_string(IDS_PORTRAIT, buf, sizeof(buf));
  79.             break;
  80.         default:
  81.             buf[0] = '\0';
  82.         }
  83.         SetDlgItemText(hwnd, INFO_ORIENT, buf);
  84.         switch(doc->pageorder) {
  85.         case ASCEND: 
  86.             load_string(IDS_ASCEND, buf, sizeof(buf));
  87.             break;
  88.         case DESCEND: 
  89.             load_string(IDS_DESCEND, buf, sizeof(buf));
  90.             break;
  91.         case SPECIAL:
  92.             load_string(IDS_SPECIAL, buf, sizeof(buf));
  93.             break;
  94.         default:
  95.             buf[0] = '\0';
  96.         }
  97.         SetDlgItemText(hwnd, INFO_ORDER, buf);
  98.         if (doc->default_page_media && doc->default_page_media->name) {
  99.         sprintf(buf,"%s %d %d",doc->default_page_media->name,
  100.             doc->default_page_media->width,
  101.             doc->default_page_media->height);
  102.         }
  103.         else {
  104.         buf[0] = '\0';
  105.         }
  106.         SetDlgItemText(hwnd, INFO_DEFMEDIA, buf);
  107.         sprintf(buf, "%d", doc->numpages);
  108.         SetDlgItemText(hwnd, INFO_NUMPAGES, buf);
  109.         n = map_page(psfile.pagenum - 1);
  110.         if (doc->pages)
  111.             sprintf(buf, "\"%s\"   %d", doc->pages[n].label ? doc->pages[n].label : "", psfile.pagenum);
  112.         else
  113.             buf[0] = '\0';
  114.         SetDlgItemText(hwnd, INFO_PAGE, buf);
  115.     
  116.     }
  117.     else {
  118.         if (option.ignore_dsc)
  119.             load_string(IDS_IGNOREDSC, buf, sizeof(buf));
  120.         else
  121.             load_string(IDS_NOTDSC, buf, sizeof(buf));
  122.         SetDlgItemText(hwnd, INFO_TYPE, buf);
  123.     }
  124.     sprintf(buf, "%d x %d", display.width, display.height);
  125.     SetDlgItemText(hwnd, INFO_BITMAP, buf);
  126.     }
  127.     else {
  128.     load_string(IDS_NOFILE, buf, sizeof(buf));
  129.     SetDlgItemText(hwnd, INFO_FILE, buf);
  130.     }
  131. }
  132.  
  133. /* read settings fron INI file */
  134. void
  135. read_profile()
  136. {
  137. int i;
  138. char profile[128];
  139. char *section = INISECTION;
  140. char *device_ptr;
  141. PROFILE *prf;
  142.     prf = profile_open(szIniFile);
  143.     profile_read_string(prf, section, "Version", "", profile, sizeof(profile));
  144.     if (strcmp(profile, GSVIEW_VERSION)!=0)
  145.         changed_version = TRUE;
  146.     profile_read_string(prf, section, "Origin", "", profile, sizeof(profile));
  147.     if (sscanf(profile,"%d %d", &option.img_origin.x, &option.img_origin.y) != 2) {
  148.         option.img_origin.x = option.img_origin.y = CW_USEDEFAULT;
  149.     }
  150.     profile_read_string(prf, section, "Size", "", profile, sizeof(profile));
  151.     if (sscanf(profile,"%d %d", &option.img_size.x, &option.img_size.y) != 2) {
  152.         option.img_size.x = option.img_size.y = CW_USEDEFAULT;
  153.     }
  154.     if ((option.img_size.x<32) || (option.img_size.y<32))
  155.         option.img_size.x = option.img_size.y = CW_USEDEFAULT;
  156.     profile_read_string(prf, section, "Maximized", "", profile, sizeof(profile));
  157.     if (sscanf(profile,"%d", &option.img_max) != 1)
  158.         option.img_max = 0;
  159.     profile_read_string(prf, section, "SaveSettings", "", profile, sizeof(profile));
  160.     if (sscanf(profile,"%d", &i) == 1)
  161.         option.settings = i;
  162.     profile_read_string(prf, section, "ButtonBar", "", profile, sizeof(profile));
  163.     if (sscanf(profile,"%d", &i) == 1)
  164.         option.button_show = i;
  165.     profile_read_string(prf, section, "FitWindowToPage", "", profile, sizeof(profile));
  166.     if (sscanf(profile,"%d", &i) == 1)
  167.         option.fit_page = i;
  168.     profile_read_string(prf, section, "Resolution", "", profile, sizeof(profile));
  169.     if (sscanf(profile,"%f %f", &option.xdpi, &option.ydpi) != 2) {
  170.         option.xdpi = option.ydpi = DEFAULT_RESOLUTION;
  171.     }
  172.     profile_read_string(prf, section, "ZoomResolution", "", profile, sizeof(profile));
  173.     if (sscanf(profile,"%f %f", &option.zoom_xdpi, &option.zoom_ydpi) != 2) {
  174.         option.zoom_xdpi = option.zoom_ydpi = DEFAULT_ZOOMRES;
  175.     }
  176.     profile_read_string(prf, section, "Depth", "", profile, sizeof(profile));
  177.     if (sscanf(profile,"%d", &i) == 1)
  178.         option.depth = i;
  179.     profile_read_string(prf, section, "Media", "", profile, sizeof(profile));
  180.     if (strlen(profile)!=0) {
  181.         char thismedia[20];
  182.         for (i=IDM_LETTER; i<IDM_USERSIZE; i++) {
  183.             if (!get_menu_string(IDM_MEDIAMENU, i, thismedia, sizeof(thismedia))) {
  184.             /* couldn't get menu string so flag as unknown */
  185.             option.media = IDM_USERSIZE+1;
  186.                 strncpy(option.medianame,profile,sizeof(option.medianame));
  187.             break;
  188.             }
  189.             if (!stricmp(thismedia, profile)) {
  190.                 break;
  191.             }
  192.         }
  193.         if (option.media <= IDM_USERSIZE) {
  194.             option.media = i;
  195.             strncpy(option.medianame,thismedia,sizeof(option.medianame));
  196.         }
  197.         }
  198.     profile_read_string(prf, section, "UserSize", "", profile, sizeof(profile));
  199.     if (sscanf(profile,"%d %d", &option.user_width, &option.user_height) != 2) {
  200.         /* this gives 640x480 pixels at 96dpi */
  201.         option.user_width = 480;
  202.         option.user_height = 360;
  203.     }
  204.     gsview_check_usersize();
  205.     profile_read_string(prf, section, "EpsfClip", "", profile, sizeof(profile));
  206.     if (sscanf(profile,"%d", &i) == 1)
  207.         option.epsf_clip = i;
  208.     profile_read_string(prf, section, "EpsfWarn", "", profile, sizeof(profile));
  209.     if (sscanf(profile,"%d", &i) == 1)
  210.         option.epsf_warn = i;
  211.     profile_read_string(prf, section, "IgnoreDSC", "", profile, sizeof(profile));
  212.     if (sscanf(profile,"%d", &i) == 1)
  213.         option.ignore_dsc = i;
  214.     profile_read_string(prf, section, "ShowBBox", "", profile, sizeof(profile));
  215.     if (sscanf(profile,"%d", &i) == 1)
  216.         option.show_bbox = i;
  217.     profile_read_string(prf, section, "Orientation", "", profile, sizeof(profile));
  218.     if (sscanf(profile,"%d", &i) == 1)
  219.         option.orientation = i+IDM_PORTRAIT;
  220.     profile_read_string(prf, section, "SwapLandscape", "", profile, sizeof(profile));
  221.     if (sscanf(profile,"%d", &i) == 1)
  222.         option.swap_landscape = i;
  223.     profile_read_string(prf, section, "Unit", "", profile, sizeof(profile));
  224.     if (sscanf(profile,"%d", &i) == 1)
  225.         option.unit = i+IDM_UNITPT;
  226.     profile_read_string(prf, section, "QuickOpen", "", profile, sizeof(profile));
  227.     if (sscanf(profile,"%d", &i) == 1)
  228.         option.quick = i;
  229.     profile_read_string(prf, section, "Safer", "", profile, sizeof(profile));
  230.     if (sscanf(profile,"%d", &i) == 1)
  231.         option.safer = i;
  232.     profile_read_string(prf, section, "AutoRedisplay", "", profile, sizeof(profile));
  233.     if (sscanf(profile,"%d", &i) == 1)
  234.         option.redisplay = i;
  235.     profile_read_string(prf, section, "SaveLastDir", "", profile, sizeof(profile));
  236.     if (sscanf(profile,"%d", &i) == 1)
  237.         option.save_dir = i;
  238.     if (option.save_dir) {
  239.         profile_read_string(prf, section, "LastDir", "", profile, sizeof(profile));
  240.         _chdir(profile);
  241. _getcwd(profile, sizeof(profile));
  242.     }
  243.     profile_read_string(prf, section, "Ghostscript", "", profile, sizeof(profile));
  244.     if (profile[0] != '\0')    /* don't copy a default - assume already set */
  245.         strcpy(option.gscommand, profile);
  246.     profile_read_string(prf, section, "GS261", "", profile, sizeof(profile));
  247.     if (sscanf(profile,"%d", &i) == 1)
  248.         option.gsversion = i ? IDM_GS261 : IDM_GS3;
  249.     profile_read_string(prf, section, "Printer", ",", profile, sizeof(profile));
  250.     device_ptr = strtok(profile, ",");
  251.     if (device_ptr != (char *)NULL) {
  252.         strcpy(option.device_name, device_ptr);
  253.         device_ptr = strtok(NULL, ",");
  254.         if (device_ptr != (char *)NULL)
  255.             strcpy(option.device_resolution, device_ptr);
  256.     }
  257.     profile_read_string(prf, section, "PrinterPort", "", profile, sizeof(option.printer_port)-1);
  258.     if (profile[0] != '\0')
  259.         strcpy(option.printer_port, profile);
  260.     for (i=0; i<NUMSOUND; i++) {
  261.         profile_read_string(prf, section, sound[i].entry, sound[i].file, profile, sizeof(profile));
  262.         strcpy(sound[i].file, profile);
  263.     }
  264.     profile_close(prf);
  265. }
  266.  
  267. /* write settings to INI file */
  268. void
  269. write_profile()
  270. {
  271. char profile[MAXSTR];
  272. char *section = INISECTION;
  273. int i;
  274. PROFILE *prf;
  275.     prf = profile_open(szIniFile);
  276.     profile_write_string(prf, section, "Version", GSVIEW_VERSION);
  277.     sprintf(profile, "%d %d", option.img_origin.x, option.img_origin.y);
  278.     profile_write_string(prf, section, "Origin", profile);
  279.     sprintf(profile, "%d %d", option.img_size.x, option.img_size.y);
  280.     profile_write_string(prf, section, "Size", profile);
  281.     sprintf(profile, "%d", option.img_max);
  282.     profile_write_string(prf, section, "Maximized", profile);
  283.     sprintf(profile, "%d", option.settings);
  284.     profile_write_string(prf, section, "SaveSettings", profile);
  285.     sprintf(profile, "%d", option.button_show);
  286.     profile_write_string(prf, section, "ButtonBar", profile);
  287.     sprintf(profile, "%d", option.fit_page);
  288.     profile_write_string(prf, section, "FitWindowToPage", profile);
  289.     sprintf(profile, "%g %g", option.xdpi, option.ydpi);
  290.     profile_write_string(prf, section, "Resolution", profile);
  291.     sprintf(profile, "%g %g", option.zoom_xdpi, option.zoom_ydpi);
  292.     profile_write_string(prf, section, "ZoomResolution", profile);
  293.     sprintf(profile, "%d", option.depth);
  294.     profile_write_string(prf, section, "Depth", profile);
  295.     if (option.media == IDM_USERSIZE)
  296.         strcpy(profile, "User Defined");
  297.     else
  298.         strcpy(profile, option.medianame);
  299.     profile_write_string(prf, section, "Media", profile);
  300.     sprintf(profile, "%u %u", option.user_width, option.user_height);
  301.     profile_write_string(prf, section, "UserSize", profile);
  302.     sprintf(profile, "%d", option.epsf_clip);
  303.     profile_write_string(prf, section, "EpsfClip", profile);
  304.     sprintf(profile, "%d", option.epsf_warn);
  305.     profile_write_string(prf, section, "EpsfWarn", profile);
  306.     sprintf(profile, "%d", option.ignore_dsc);
  307.     profile_write_string(prf, section, "IgnoreDSC", profile);
  308.     sprintf(profile, "%d", option.show_bbox);
  309.     profile_write_string(prf, section, "ShowBBox", profile);
  310.     sprintf(profile, "%d", option.orientation - IDM_PORTRAIT);
  311.     profile_write_string(prf, section, "Orientation", profile);
  312.     sprintf(profile, "%d", option.swap_landscape);
  313.     profile_write_string(prf, section, "SwapLandscape", profile);
  314.     sprintf(profile, "%d", option.unit - IDM_UNITPT);
  315.     profile_write_string(prf, section, "Unit", profile);
  316.     sprintf(profile, "%d", option.quick);
  317.     profile_write_string(prf, section, "QuickOpen", profile);
  318.     sprintf(profile, "%d", option.safer);
  319.     profile_write_string(prf, section, "Safer", profile);
  320.     sprintf(profile, "%d", option.redisplay);
  321.     profile_write_string(prf, section, "AutoRedisplay", profile);
  322.     sprintf(profile, "%d", option.save_dir);
  323.     profile_write_string(prf, section, "SaveLastDir", profile);
  324.     if (option.save_dir) {
  325.         _getcwd(profile, sizeof(profile));
  326.         profile_write_string(prf, section, "LastDir", profile);
  327.     }
  328.     profile_write_string(prf, section, "Ghostscript", option.gscommand);
  329.     sprintf(profile, "%d", (option.gsversion == IDM_GS261 ? 1 : 0));
  330.     profile_write_string(prf, section, "GS261", profile);
  331.     if (option.device_name[0] != '\0') {
  332.         sprintf(profile,"%s,%s",option.device_name,option.device_resolution);
  333.         profile_write_string(prf, section, "Printer", profile);
  334.     }
  335.     profile_write_string(prf, section, "PrinterPort", option.printer_port);
  336.     for (i=0; i<NUMSOUND; i++)
  337.         profile_write_string(prf, section, sound[i].entry, sound[i].file);
  338.     profile_close(prf);
  339. }
  340.  
  341.